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

Commit 903022c

Browse files
committed
add createAddStream feature
1 parent dd344e4 commit 903022c

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
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: 47 additions & 12 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')
6-
const bs58 = require('bs58')
77
const through = require('through2')
88
const isStream = require('isstream')
99
const promisify = require('promisify-es6')
10+
const Duplex = require('stream').Duplex
11+
const multihashes = require('multihashes')
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)) {
@@ -33,7 +68,7 @@ module.exports = function files (self) {
3368
}]
3469
}
3570
if (!callback || typeof callback !== 'function') {
36-
callback = function oop () {}
71+
callback = function noop () {}
3772
}
3873
if (!Array.isArray(data)) {
3974
return callback(new Error('"data" must be an array of { path: string, content: Buffer|Readable } or Buffer or Readable'))
@@ -43,8 +78,8 @@ module.exports = function files (self) {
4378
const res = []
4479

4580
// Transform file info tuples to DAGNodes
46-
i.pipe(through.obj(function transform (info, enc, next) {
47-
const mh = bs58.encode(info.multihash).toString()
81+
i.pipe(through.obj((info, enc, next) => {
82+
const mh = multihashes.toB58String(info.multihash)
4883
self._dagS.get(mh, (err, node) => {
4984
if (err) return callback(err)
5085
var obj = {
@@ -54,7 +89,7 @@ module.exports = function files (self) {
5489
res.push(obj)
5590
next()
5691
})
57-
}, function end (done) {
92+
}, (done) => {
5893
callback(null, res)
5994
}))
6095

test/http-api/test-files.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ module.exports = (httpAPI) => {
99
describe('api', () => {
1010
let api
1111

12-
before((done) => {
12+
before(() => {
1313
api = httpAPI.server.select('API')
14-
done()
1514
})
1615

1716
describe('/files/cat', () => {

0 commit comments

Comments
 (0)