Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: pull-stream-to-stream replaced with duplex stream #809

Merged
merged 5 commits into from
Mar 24, 2017
Merged
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const promisify = require('promisify-es6')
const multihashes = require('multihashes')
const pull = require('pull-stream')
const sort = require('pull-sort')
const pushable = require('pull-pushable')
const toStream = require('pull-stream-to-stream')
const toPull = require('stream-to-pull-stream')
const CID = require('cids')
const waterfall = require('async/waterfall')
const isStream = require('isstream')
const Duplex = require('stream').Duplex

module.exports = function files (self) {
const createAddPullStream = (options) => {
Expand All @@ -30,7 +32,19 @@ module.exports = function files (self) {
callback = options
options = undefined
}
callback(null, toStream(createAddPullStream(options)))

const addPullStream = createAddPullStream(options)
const p = pushable()
const s = pull(
p,
addPullStream
)

const retStream = new AddStreamDuplex(s, p)

retStream.once('finish', () => p.end())

callback(null, retStream)
},

createAddPullStream: createAddPullStream,
Expand Down Expand Up @@ -164,3 +178,28 @@ function normalizeContent (content) {
}

function noop () {}

class AddStreamDuplex extends Duplex {
constructor (pullStream, push, options) {
super(Object.assign({}, { objectMode: true }, options))
this._pullStream = pullStream
this._pushable = push
}

_read () {
this._pullStream(null, (err, data) => {
if (err) {
if (err instanceof Error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about true for end?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of calling it err I'm now calling it end.

this.emit('error', err)
}
} else {
this.push(data)
}
})
}

_write (chunk, encoding, callback) {
this._pushable.push(chunk)
callback()
}
}