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

Commit f46cdc5

Browse files
dignifiedquiredaviddias
authored andcommitted
refactor: replace wreck with raw request (#414)
* refactor: replace wreck with raw request * fix: parsing and example * fix
1 parent 1d0d57e commit f46cdc5

File tree

10 files changed

+152
-120
lines changed

10 files changed

+152
-120
lines changed

examples/browser-add/index.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@
44
<meta charset="UTF-8"/>
55
<title>JS IPFS API - Example - Browser - Add</title>
66
<script src="bundle.js"></script>
7+
<style>
8+
.content {
9+
border: 1px solid black;
10+
padding: 10px;
11+
margin: 5px 0;
12+
}
13+
</style>
714
</head>
815
<body>
916
<h1>JS IPFS API - Add file from the browser</h1>
1017
<textarea id="source">
1118
</textarea>
1219
<button id="store">create in ipfs</button>
13-
<div><div>found in ipfs:</div>
14-
<div id="hash">[ipfs hash]</div>
15-
<div id="content">[ipfs content]</div>
20+
<div>
21+
<div>found in ipfs:</div>
22+
<div class="content" id="hash">[ipfs hash]</div>
23+
<div class="content" id="content">[ipfs content]</div>
1624
</div>
1725
</body>
1826
</html>

examples/browser-add/index.js

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

3-
var IPFS = require('ipfs-api')
3+
var IPFS = require('../../src')
44
var ipfs = IPFS()
55

66
function store () {
@@ -11,24 +11,26 @@ function store () {
1111
}
1212

1313
res.forEach(function (file) {
14-
console.log('successfully stored', file.Hash)
15-
display(file.Hash)
14+
if (file && file.hash) {
15+
console.log('successfully stored', file.hash)
16+
display(file.hash)
17+
}
1618
})
1719
})
1820
}
1921

2022
function display (hash) {
21-
ipfs.cat(hash, function (err, res) {
23+
// buffer: true results in the returned result being a buffer rather than a stream
24+
ipfs.cat(hash, {buffer: true}, function (err, res) {
2225
if (err || !res) {
2326
return console.error('ipfs cat error', err, res)
2427
}
25-
if (res.readable) {
26-
console.error('unhandled: cat result is a pipe', res)
27-
} else {
28-
document.getElementById('hash').innerText = hash
29-
document.getElementById('content').innerText = res
30-
}
28+
29+
document.getElementById('hash').innerText = hash
30+
document.getElementById('content').innerText = res.toString()
3131
})
3232
}
3333

34-
document.getElementById('store').onclick = store
34+
document.addEventListener('DOMContentLoaded', function () {
35+
document.getElementById('store').onclick = store
36+
})

examples/browser-add/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "browserify -t brfs index.js > bundle.js && http-server -a 127.0.0.1 -p 8888"
7+
"start": "browserify index.js > bundle.js && http-server -a 127.0.0.1 -p 8888"
88
},
99
"keywords": [],
1010
"author": "Friedel Ziegelmayer",
1111
"license": "MIT",
1212
"devDependencies": {
13-
"brfs": "^1.4.3",
1413
"browserify": "^13.0.1",
15-
"http-server": "^0.9.0",
16-
"ipfs-api": "^6.0.3"
14+
"http-server": "^0.9.0"
1715
}
1816
}

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"browser": {
77
"glob": false,
88
"fs": false,
9-
"stream": "readable-stream"
9+
"stream": "readable-stream",
10+
"http": "stream-http"
1011
},
1112
"scripts": {
1213
"test": "gulp test",
@@ -24,6 +25,7 @@
2425
"async": "^2.1.2",
2526
"bl": "^1.1.2",
2627
"bs58": "^3.0.0",
28+
"concat-stream": "^1.5.2",
2729
"detect-node": "^2.0.3",
2830
"flatmap": "0.0.3",
2931
"glob": "^7.1.1",
@@ -40,9 +42,9 @@
4042
"promisify-es6": "^1.0.2",
4143
"qs": "^6.3.0",
4244
"readable-stream": "^1.1.14",
45+
"stream-http": "^2.5.0",
4346
"streamifier": "^0.1.1",
44-
"tar-stream": "^1.5.2",
45-
"wreck": "^10.0.0"
47+
"tar-stream": "^1.5.2"
4648
},
4749
"engines": {
4850
"node": ">=4.0.0"

src/api/cat.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ const promisify = require('promisify-es6')
44
const cleanMultihash = require('../clean-multihash')
55

66
module.exports = (send) => {
7-
return promisify((hash, callback) => {
7+
return promisify((hash, opts, callback) => {
8+
if (typeof opts === 'function') {
9+
callback = opts
10+
opts = {}
11+
}
12+
813
try {
914
hash = cleanMultihash(hash)
1015
} catch (err) {
@@ -13,7 +18,8 @@ module.exports = (send) => {
1318

1419
send({
1520
path: 'cat',
16-
args: hash
21+
args: hash,
22+
buffer: opts.buffer
1723
}, callback)
1824
})
1925
}

src/api/util/url-add.js

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

3-
const Wreck = require('wreck')
4-
const addToDagNodesTransform = require('./../../add-to-dagnode-transform')
5-
63
const promisify = require('promisify-es6')
4+
const once = require('once')
5+
const parseUrl = require('url').parse
6+
7+
const request = require('../../request')
8+
const addToDagNodesTransform = require('./../../add-to-dagnode-transform')
79

810
module.exports = (send) => {
911
return promisify((url, opts, callback) => {
@@ -27,17 +29,19 @@ module.exports = (send) => {
2729
}
2830

2931
const sendWithTransform = send.withTransform(addToDagNodesTransform)
32+
callback = once(callback)
3033

31-
Wreck.request('GET', url, null, (err, res) => {
32-
if (err) {
33-
return callback(err)
34+
request(parseUrl(url).protocol)(url, (res) => {
35+
res.once('error', callback)
36+
if (res.statusCode >= 400) {
37+
return callback(new Error(`Failed to download with ${res.statusCode}`))
3438
}
3539

3640
sendWithTransform({
3741
path: 'add',
3842
qs: opts,
3943
files: res
4044
}, callback)
41-
})
45+
}).end()
4246
})
4347
}

src/request-api.js

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

3-
const Wreck = require('wreck')
43
const Qs = require('qs')
54
const ndjson = require('ndjson')
6-
const getFilesStream = require('./get-files-stream')
7-
85
const isNode = require('detect-node')
6+
const once = require('once')
7+
const concat = require('concat-stream')
8+
9+
const getFilesStream = require('./get-files-stream')
10+
const request = require('./request')
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+
}
2938

39+
let res
40+
try {
41+
res = JSON.parse(data)
42+
} catch (err) {
43+
return cb(err)
44+
}
45+
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
}
@@ -51,20 +73,21 @@ function onRes (buffer, cb, uri) {
5173
return cb(null, res)
5274
}
5375

54-
if (chunkedObjects) {
55-
if (isJson) {
56-
return parseChunkedJson(res, cb)
57-
}
76+
if (chunkedObjects && isJson) {
77+
return parseChunkedJson(res, cb)
78+
}
5879

59-
return Wreck.read(res, null, cb)
80+
if (isJson) {
81+
return parseJson(res, cb)
6082
}
6183

62-
Wreck.read(res, {json: isJson}, cb)
84+
parseRaw(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
@@ -99,29 +122,38 @@ function requestAPI (config, options, callback) {
99122
// this option is only used internally, not passed to daemon
100123
delete options.qs.followSymlinks
101124

102-
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-
}
125+
const method = 'POST'
126+
const headers = {}
109127

110128
if (isNode) {
111129
// Browsers do not allow you to modify the user agent
112-
opts.headers['User-Agent'] = config['user-agent']
130+
headers['User-Agent'] = config['user-agent']
113131
}
114132

115133
if (options.files) {
116134
if (!stream.boundary) {
117135
return callback(new Error('No boundary in multipart stream'))
118136
}
119137

120-
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}`
121-
opts.payload = stream
138+
headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}`
139+
}
140+
141+
const qs = Qs.stringify(options.qs, {arrayFormat: 'repeat'})
142+
const req = request(config.protocol)({
143+
hostname: config.host,
144+
path: `${config['api-path']}${options.path}?${qs}`,
145+
port: config.port,
146+
method: method,
147+
headers: headers
148+
}, 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
//

src/request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
const httpRequest = require('http').request
4+
const httpsRequest = require('https').request
5+
6+
module.exports = (protocol) => {
7+
if (protocol.indexOf('https') === 0) {
8+
return httpsRequest
9+
}
10+
11+
return httpRequest
12+
}

0 commit comments

Comments
 (0)