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

Commit b7382d4

Browse files
committed
Merge pull request #99 from ipfs/return-streams
.cat is not returning a stream anymore
2 parents 253d5b6 + aca18cf commit b7382d4

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ describe('IPFS Node.js API wrapper tests', function () {
152152
this.timeout(10000)
153153

154154
apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) {
155-
if (err) throw err
155+
if (err) {
156+
throw err
157+
}
156158

157159
if (typeof res === 'string') {
158160
// Just a string

0 commit comments

Comments
 (0)