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

Commit ea0eb40

Browse files
committed
add createAddStream feature
1 parent dd344e4 commit ea0eb40

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"form-data": "^1.0.0-rc3",
4646
"gulp": "^3.9.1",
4747
"idb-plus-blob-store": "^1.1.2",
48-
"interface-ipfs-core": "^0.2.0",
48+
"interface-ipfs-core": "^0.2.2",
4949
"left-pad": "^1.1.0",
5050
"lodash": "^4.11.2",
5151
"mocha": "^2.5.1",
@@ -67,7 +67,7 @@
6767
"glob": "^7.0.3",
6868
"hapi": "^13.4.1",
6969
"ipfs-bitswap": "^0.4.1",
70-
"ipfs-api": "^5.0.0",
70+
"ipfs-api": "^5.0.1",
7171
"ipfs-block": "^0.3.0",
7272
"ipfs-block-service": "^0.4.0",
7373
"ipfs-merkle-dag": "^0.6.0",

src/core/ipfs/files.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
11
'use strict'
22

3-
const Importer = require('ipfs-unixfs-engine').importer
4-
const Exporter = require('ipfs-unixfs-engine').exporter
3+
const unixfsEngine = require('ipfs-unixfs-engine')
4+
const Importer = unixfsEngine.Importer
5+
const Exporter = unixfsEngine.Exporter
56
const UnixFS = require('ipfs-unixfs')
67
const bs58 = require('bs58')
78
const through = require('through2')
89
const isStream = require('isstream')
910
const promisify = require('promisify-es6')
11+
const Duplex = require('stream').Duplex
1012

1113
module.exports = function files (self) {
1214
return {
13-
createAddStream: promisify((callback) => {
14-
// TODO: wip
15-
if (data === undefined) {
16-
return new Importer(self._dagS)
15+
createAddStream: (callback) => {
16+
const i = new Importer(self._dagS)
17+
const ds = new Duplex({ objectMode: true })
18+
19+
ds._read = (n) => {}
20+
ds._write = (file, enc, next) => {
21+
i.write(file)
22+
next()
1723
}
18-
}),
1924

25+
ds.end = () => {
26+
i.end()
27+
}
28+
29+
let counter = 0
30+
31+
i.on('data', (file) => {
32+
counter++
33+
self.object.get(file.multihash, (err, node) => {
34+
if (err) {
35+
return ds.emit('error', err)
36+
}
37+
ds.push({path: file.path, node: node})
38+
counter--
39+
})
40+
})
41+
42+
i.on('end', () => {
43+
function canFinish () {
44+
if (counter === 0) {
45+
ds.push(null)
46+
} else {
47+
setTimeout(canFinish, 100)
48+
}
49+
}
50+
canFinish()
51+
})
52+
53+
callback(null, ds)
54+
},
2055
add: promisify((data, callback) => {
2156
// Buffer input
2257
if (Buffer.isBuffer(data)) {

0 commit comments

Comments
 (0)