From d5c7157e36f5c07569b7d95a341f2e9801e3c705 Mon Sep 17 00:00:00 2001 From: nginnever Date: Thu, 5 May 2016 09:42:34 -0700 Subject: [PATCH] fix exporter event emitter sequence --- src/exporter.js | 28 +++++++++++++++++++++++----- test/test-exporter.js | 10 +++++++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/exporter.js b/src/exporter.js index 7383dc71..1764bcdb 100644 --- a/src/exporter.js +++ b/src/exporter.js @@ -37,20 +37,29 @@ function exporter (hash, dagService, options, callback) { return ee function fileExporter (node, name, dir, callback) { + let init + if (typeof dir === 'function') { callback = dir; dir = {} } var rs = new Readable() if (node.links.length === 0) { const unmarshaledData = UnixFS.unmarshal(node.data) + init = false + rs._read = () => { + if (init) { + return + } + init = true + rs.push(unmarshaledData.data) + rs.push(null) + } ee.emit('file', { stream: rs, path: name, dir: dir }) - rs.push(unmarshaledData.data) - rs.push(null) if (callback) { callback() } return } else { ee.emit('file', { stream: rs, path: name, dir: dir }) - var init = false + init = false rs._read = () => { if (init) { return @@ -83,10 +92,19 @@ function exporter (hash, dagService, options, callback) { } function dirExporter (node, name, callback) { + let init + var rs = new Readable() if (node.links.length === 0) { - rs.push(node.data) - rs.push(null) + init = false + rs._read = () => { + if (init) { + return + } + init = true + rs.push(node.data) + rs.push(null) + } ee.emit('file', {stream: rs, path: name}) if (callback) { callback() diff --git a/test/test-exporter.js b/test/test-exporter.js index c62c7c31..9c4ee3e5 100644 --- a/test/test-exporter.js +++ b/test/test-exporter.js @@ -7,6 +7,7 @@ const expect = require('chai').expect const BlockService = require('ipfs-block-service') const DAGService = require('ipfs-merkle-dag').DAGService const UnixFS = require('ipfs-unixfs') +const bl = require('bl') let ds @@ -27,10 +28,13 @@ module.exports = function (repo) { const testExport = exporter(hash, ds) testExport.on('file', (data) => { ds.get(hash, (err, fetchedNode) => { - expect(err).to.not.exist const unmarsh = UnixFS.unmarshal(fetchedNode.data) - expect(unmarsh.data).to.deep.equal(data.stream._readableState.buffer[0]) - done() + expect(err).to.not.exist + data.stream.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata).to.deep.equal(unmarsh.data) + done() + })) }) }) })