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

Commit aca18cf

Browse files
committed
fix streaming
1 parent 8174b52 commit aca18cf

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/request-api.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const request = require('request')
44
const getFilesStream = require('./get-files-stream')
5+
const stream = require('stream')
56

67
const isNode = !global.window
78

89
// -- Internal
910

10-
function onEnd (buffer, result, cb) {
11+
function onEnd (buffer, result, passThrough, cb) {
1112
return (err, res, body) => {
1213
if (err) {
1314
return cb(err)
@@ -17,6 +18,13 @@ function onEnd (buffer, result, cb) {
1718
return cb(new Error(`Server responded with ${res.statusCode}: ${body}`))
1819
}
1920

21+
if (result.stream) {
22+
cb(null, passThrough)
23+
passThrough.resume()
24+
passThrough.end()
25+
return
26+
}
27+
2028
if ((result.stream && !buffer) ||
2129
(result.chunkedObjects && buffer)) {
2230
return cb(null, body)
@@ -33,8 +41,12 @@ function onEnd (buffer, result, cb) {
3341
}
3442
}
3543

36-
function onData (result) {
44+
function onData (result, passThrough) {
3745
return chunk => {
46+
if (result.stream) {
47+
passThrough.write(chunk)
48+
return
49+
}
3850
if (!result.chunkedObjects) return
3951

4052
try {
@@ -63,8 +75,10 @@ function makeRequest (opts, buffer, cb) {
6375
objects: []
6476
}
6577

66-
return request(opts, onEnd(buffer, result, cb))
67-
.on('data', onData(result))
78+
var passThrough = new stream.PassThrough()
79+
80+
return request(opts, onEnd(buffer, result, passThrough, cb))
81+
.on('data', onData(result, passThrough))
6882
.on('response', onResponse(result))
6983
}
7084

test/tests.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,21 @@ describe('IPFS Node.js API wrapper tests', function () {
156156
throw err
157157
}
158158

159-
if (isNode) {
160-
var buf = ''
161-
res
162-
.on('error', function (err) { throw err })
163-
.on('data', function (data) { buf += data })
164-
.on('end', function () {
165-
assert.equal(buf, testfile)
166-
done()
167-
})
168-
} else {
169-
if (typeof res === 'string') {
170-
// Just a string
171-
assert.equal(res, testfile)
172-
done()
173-
return
174-
}
159+
if (typeof res === 'string') {
160+
// Just a string
161+
assert.equal(res, testfile)
162+
done()
163+
return
175164
}
165+
166+
var buf = ''
167+
res
168+
.on('error', function (err) { throw err })
169+
.on('data', function (data) { buf += data })
170+
.on('end', function () {
171+
assert.equal(buf, testfile)
172+
done()
173+
})
176174
})
177175
})
178176
})

0 commit comments

Comments
 (0)