Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit eaad7f8

Browse files
getting close to passing and no more wreck
1 parent cbe6582 commit eaad7f8

File tree

11 files changed

+191
-174
lines changed

11 files changed

+191
-174
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ before_install:
1818

1919
script:
2020
- npm run lint
21-
- npm run test:node
21+
- npm run test
2222
- npm run coverage
2323

2424
before_script:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ If are using this module in a browser with something like browserify, then you w
9999

100100
```bash
101101
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]"
102+
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "\"true\""
103+
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"
102104
```
103105

104106
## Usage

package.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"main": "src/index.js",
66
"browser": {
77
"glob": false,
8-
"fs": false,
9-
"http": "stream-http",
10-
"https": "https-browserify"
8+
"fs": false
119
},
1210
"scripts": {
1311
"test": "gulp test",
@@ -23,27 +21,25 @@
2321
},
2422
"dependencies": {
2523
"async": "^2.1.2",
26-
"bl": "^1.1.2",
2724
"bs58": "^3.0.0",
25+
"concat-stream": "^1.5.2",
2826
"detect-node": "^2.0.3",
2927
"flatmap": "0.0.3",
3028
"glob": "^7.1.1",
31-
"https-browserify": "0.0.1",
3229
"ipfs-block": "^0.5.0",
3330
"ipld-dag-pb": "^0.8.0",
3431
"is-ipfs": "^0.2.1",
3532
"isstream": "^0.1.2",
3633
"multiaddr": "^2.0.3",
3734
"multipart-stream": "^2.0.1",
3835
"ndjson": "^1.4.3",
36+
"once": "^1.4.0",
3937
"peer-id": "^0.8.0",
4038
"peer-info": "^0.8.0",
4139
"promisify-es6": "^1.0.2",
4240
"qs": "^6.3.0",
43-
"stream-http": "^2.4.1",
4441
"streamifier": "^0.1.1",
45-
"tar-stream": "^1.5.2",
46-
"wreck": "^10.0.0"
42+
"tar-stream": "^1.5.2"
4743
},
4844
"engines": {
4945
"node": ">=4.0.0"

src/api/block.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const bl = require('bl')
4+
const concat = require('concat-stream')
5+
const once = require('once')
56
const Block = require('ipfs-block')
67
const multihash = require('multihashes')
78
const CID = require('cids')
@@ -17,6 +18,7 @@ module.exports = (send) => {
1718
callback = opts
1819
opts = {}
1920
}
21+
callback = once(callback)
2022

2123
return send({
2224
path: 'block/get',
@@ -29,12 +31,9 @@ module.exports = (send) => {
2931
if (Buffer.isBuffer(res)) {
3032
callback(null, new Block(res))
3133
} else {
32-
res.pipe(bl((err, data) => {
33-
if (err) {
34-
return callback(err)
35-
}
36-
callback(null, new Block(data))
37-
}))
34+
res
35+
.once('error', callback)
36+
.pipe(concat((data) => callback(null, new Block(data))))
3837
}
3938
})
4039
}),

src/api/object.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const DAGNode = dagPB.DAGNode
55
const DAGLink = dagPB.DAGLink
66
const promisify = require('promisify-es6')
77
const bs58 = require('bs58')
8-
const bl = require('bl')
8+
const concat = require('concat-stream')
9+
const once = require('once')
910
const cleanMultihash = require('../clean-multihash')
1011

1112
module.exports = (send) => {
@@ -136,6 +137,8 @@ module.exports = (send) => {
136137
options = {}
137138
}
138139

140+
callback = once(callback)
141+
139142
try {
140143
multihash = cleanMultihash(multihash, options)
141144
} catch (err) {
@@ -151,7 +154,9 @@ module.exports = (send) => {
151154
}
152155

153156
if (typeof result.pipe === 'function') {
154-
result.pipe(bl(callback))
157+
result
158+
.once('error', callback)
159+
.pipe(concat((data) => callback(null, data)))
155160
} else {
156161
callback(null, result)
157162
}

src/api/util/url-add.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
const Wreck = require('wreck')
3+
const hyperquest = require('hyperquest')
44
const addToDagNodesTransform = require('./../../add-to-dagnode-transform')
55

66
const promisify = require('promisify-es6')
7+
const once = require('once')
78

89
module.exports = (send) => {
910
return promisify((url, opts, callback) => {
@@ -27,17 +28,16 @@ module.exports = (send) => {
2728
}
2829

2930
const sendWithTransform = send.withTransform(addToDagNodesTransform)
30-
31-
Wreck.request('GET', url, null, (err, res) => {
32-
if (err) {
33-
return callback(err)
34-
}
35-
36-
sendWithTransform({
37-
path: 'add',
38-
qs: opts,
39-
files: res
40-
}, callback)
41-
})
31+
callback = once(callback)
32+
33+
hyperquest.get(url)
34+
.once('error', callback)
35+
.once('response', (res) => {
36+
sendWithTransform({
37+
path: 'add',
38+
qs: opts,
39+
files: res
40+
}, callback)
41+
})
4242
})
4343
}

src/get-dagnode.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,38 @@
11
'use strict'
22

33
const DAGNode = require('ipld-dag-pb').DAGNode
4-
const bl = require('bl')
4+
const concat = require('concat-stream')
5+
const once = require('once')
56
const parallel = require('async/parallel')
67

78
module.exports = function (send, hash, callback) {
9+
callback = once(callback)
10+
811
// Retrieve the object and its data in parallel, then produce a DAGNode
912
// instance using this information.
1013
parallel([
11-
function get (done) {
12-
send({
13-
path: 'object/get',
14-
args: hash
15-
}, done)
16-
},
14+
(cb) => send({path: 'object/get', args: hash}, cb),
1715

18-
function data (done) {
1916
// WORKAROUND: request the object's data separately, since raw bits in JSON
2017
// are interpreted as UTF-8 and corrupt the data.
2118
// See https://github.com/ipfs/go-ipfs/issues/1582 for more details.
22-
send({
23-
path: 'object/data',
24-
args: hash
25-
}, done)
26-
}],
27-
28-
function done (err, res) {
29-
if (err) {
30-
return callback(err)
31-
}
32-
33-
var object = res[0]
34-
var stream = res[1]
19+
(cb) => send({path: 'object/data', args: hash}, cb)
20+
], (err, res) => {
21+
if (err) {
22+
return callback(err)
23+
}
3524

36-
if (Buffer.isBuffer(stream)) {
37-
callback(err, new DAGNode(stream, object.Links))
38-
} else {
39-
stream.pipe(bl(function (err, data) {
40-
if (err) {
41-
return callback(err)
42-
}
25+
const object = res[0]
26+
const stream = res[1]
4327

44-
callback(err, new DAGNode(data, object.Links))
28+
if (Buffer.isBuffer(stream)) {
29+
callback(err, new DAGNode(stream, object.Links))
30+
} else {
31+
stream
32+
.once('error', callback)
33+
.pipe(concat((data) => {
34+
callback(null, new DAGNode(data, object.Links))
4535
}))
46-
}
47-
})
36+
}
37+
})
4838
}

src/request-api.js

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
'use strict'
22

3-
const Wreck = require('wreck')
3+
const hyperquest = require('hyperquest')
44
const Qs = require('qs')
55
const ndjson = require('ndjson')
6-
const getFilesStream = require('./get-files-stream')
7-
86
const isNode = require('detect-node')
7+
const once = require('once')
8+
const concat = require('concat-stream')
9+
10+
const getFilesStream = require('./get-files-stream')
911

1012
// -- Internal
1113

1214
function parseChunkedJson (res, cb) {
13-
const parsed = []
1415
res
1516
.pipe(ndjson.parse())
16-
.on('data', (obj) => {
17-
parsed.push(obj)
18-
})
19-
.on('end', () => {
20-
cb(null, parsed)
21-
})
17+
.once('error', cb)
18+
.pipe(concat((data) => cb(null, data)))
2219
}
2320

24-
function onRes (buffer, cb, uri) {
25-
return (err, res) => {
26-
if (err) {
27-
return cb(err)
28-
}
21+
function parseRaw (res, cb) {
22+
res
23+
.once('error', cb)
24+
.pipe(concat((data) => cb(null, data)))
25+
}
26+
27+
function parseJson (res, cb) {
28+
res
29+
.once('error', cb)
30+
.pipe(concat((data) => {
31+
if (!data || data.length === 0) {
32+
return cb()
33+
}
34+
35+
if (Buffer.isBuffer(data)) {
36+
data = data.toString()
37+
}
38+
39+
let res
40+
try {
41+
res = JSON.parse(data)
42+
} catch (err) {
43+
return cb(err)
44+
}
2945

46+
cb(null, res)
47+
}))
48+
}
49+
50+
function onRes (buffer, cb) {
51+
return (res) => {
3052
const stream = Boolean(res.headers['x-stream-output'])
3153
const chunkedObjects = Boolean(res.headers['x-chunked-output'])
3254
const isJson = res.headers['content-type'] &&
@@ -35,7 +57,7 @@ function onRes (buffer, cb, uri) {
3557
if (res.statusCode >= 400 || !res.statusCode) {
3658
const error = new Error(`Server responded with ${res.statusCode}`)
3759

38-
return Wreck.read(res, {json: true}, (err, payload) => {
60+
parseJson(res, (err, payload) => {
3961
if (err) {
4062
return cb(err)
4163
}
@@ -56,15 +78,16 @@ function onRes (buffer, cb, uri) {
5678
return parseChunkedJson(res, cb)
5779
}
5880

59-
return Wreck.read(res, null, cb)
81+
return parseRaw(res, cb)
6082
}
6183

62-
Wreck.read(res, {json: isJson}, cb)
84+
parseJson(res, cb)
6385
}
6486
}
6587

6688
function requestAPI (config, options, callback) {
6789
options.qs = options.qs || {}
90+
callback = once(callback)
6891

6992
if (Array.isArray(options.files)) {
7093
options.qs.recursive = true
@@ -100,28 +123,37 @@ function requestAPI (config, options, callback) {
100123
delete options.qs.followSymlinks
101124

102125
const port = config.port ? `:${config.port}` : ''
103-
104-
const opts = {
105-
method: 'POST',
106-
uri: `${config.protocol}://${config.host}${port}${config['api-path']}${options.path}?${Qs.stringify(options.qs, {arrayFormat: 'repeat'})}`,
107-
headers: {}
108-
}
126+
const uri = `${config.protocol}://${config.host}${port}${config['api-path']}${options.path}?${Qs.stringify(options.qs, {arrayFormat: 'repeat'})}`
127+
const method = 'POST'
128+
const headers = {}
109129

110130
if (isNode) {
111131
// Browsers do not allow you to modify the user agent
112-
opts.headers['User-Agent'] = config['user-agent']
132+
headers['User-Agent'] = config['user-agent']
113133
}
114134

115135
if (options.files) {
116136
if (!stream.boundary) {
117137
return callback(new Error('No boundary in multipart stream'))
118138
}
119139

120-
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}`
121-
opts.payload = stream
140+
headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}`
141+
}
142+
143+
const req = hyperquest(uri, {
144+
method: method,
145+
headers: headers
146+
})
147+
.on('error', callback)
148+
.on('response', onRes(options.buffer, callback))
149+
150+
if (options.files) {
151+
stream.pipe(req)
152+
} else {
153+
req.end()
122154
}
123155

124-
return Wreck.request(opts.method, opts.uri, opts, onRes(options.buffer, callback, opts.uri))
156+
return req
125157
}
126158

127159
//

0 commit comments

Comments
 (0)