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

.cat is not returning a stream anymore #99

Merged
merged 2 commits into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/request-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

const request = require('request')
const getFilesStream = require('./get-files-stream')
const stream = require('stream')

const isNode = !global.window

// -- Internal

function onEnd (buffer, result, cb) {
function onEnd (buffer, result, passThrough, cb) {
return (err, res, body) => {
if (err) {
return cb(err)
Expand All @@ -17,6 +18,13 @@ function onEnd (buffer, result, cb) {
return cb(new Error(`Server responded with ${res.statusCode}: ${body}`))
}

if (result.stream) {
cb(null, passThrough)
passThrough.resume()
passThrough.end()
return
}

if ((result.stream && !buffer) ||
(result.chunkedObjects && buffer)) {
return cb(null, body)
Expand All @@ -33,8 +41,12 @@ function onEnd (buffer, result, cb) {
}
}

function onData (result) {
function onData (result, passThrough) {
return chunk => {
if (result.stream) {
passThrough.write(chunk)
return
}
if (!result.chunkedObjects) return

try {
Expand Down Expand Up @@ -63,8 +75,10 @@ function makeRequest (opts, buffer, cb) {
objects: []
}

return request(opts, onEnd(buffer, result, cb))
.on('data', onData(result))
var passThrough = new stream.PassThrough()

return request(opts, onEnd(buffer, result, passThrough, cb))
.on('data', onData(result, passThrough))
.on('response', onResponse(result))
}

Expand Down
4 changes: 3 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ describe('IPFS Node.js API wrapper tests', function () {
this.timeout(10000)

apiClients['a'].cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', function (err, res) {
if (err) throw err
if (err) {
throw err
}

if (typeof res === 'string') {
// Just a string
Expand Down