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

Commit d5c7157

Browse files
committed
fix exporter event emitter sequence
1 parent 22ebdaf commit d5c7157

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/exporter.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,29 @@ function exporter (hash, dagService, options, callback) {
3737
return ee
3838

3939
function fileExporter (node, name, dir, callback) {
40+
let init
41+
4042
if (typeof dir === 'function') { callback = dir; dir = {} }
4143
var rs = new Readable()
4244
if (node.links.length === 0) {
4345
const unmarshaledData = UnixFS.unmarshal(node.data)
46+
init = false
47+
rs._read = () => {
48+
if (init) {
49+
return
50+
}
51+
init = true
52+
rs.push(unmarshaledData.data)
53+
rs.push(null)
54+
}
4455
ee.emit('file', { stream: rs, path: name, dir: dir })
45-
rs.push(unmarshaledData.data)
46-
rs.push(null)
4756
if (callback) {
4857
callback()
4958
}
5059
return
5160
} else {
5261
ee.emit('file', { stream: rs, path: name, dir: dir })
53-
var init = false
62+
init = false
5463
rs._read = () => {
5564
if (init) {
5665
return
@@ -83,10 +92,19 @@ function exporter (hash, dagService, options, callback) {
8392
}
8493

8594
function dirExporter (node, name, callback) {
95+
let init
96+
8697
var rs = new Readable()
8798
if (node.links.length === 0) {
88-
rs.push(node.data)
89-
rs.push(null)
99+
init = false
100+
rs._read = () => {
101+
if (init) {
102+
return
103+
}
104+
init = true
105+
rs.push(node.data)
106+
rs.push(null)
107+
}
90108
ee.emit('file', {stream: rs, path: name})
91109
if (callback) {
92110
callback()

test/test-exporter.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const expect = require('chai').expect
77
const BlockService = require('ipfs-block-service')
88
const DAGService = require('ipfs-merkle-dag').DAGService
99
const UnixFS = require('ipfs-unixfs')
10+
const bl = require('bl')
1011

1112
let ds
1213

@@ -27,10 +28,13 @@ module.exports = function (repo) {
2728
const testExport = exporter(hash, ds)
2829
testExport.on('file', (data) => {
2930
ds.get(hash, (err, fetchedNode) => {
30-
expect(err).to.not.exist
3131
const unmarsh = UnixFS.unmarshal(fetchedNode.data)
32-
expect(unmarsh.data).to.deep.equal(data.stream._readableState.buffer[0])
33-
done()
32+
expect(err).to.not.exist
33+
data.stream.pipe(bl((err, bldata) => {
34+
expect(err).to.not.exist
35+
expect(bldata).to.deep.equal(unmarsh.data)
36+
done()
37+
}))
3438
})
3539
})
3640
})

0 commit comments

Comments
 (0)