1
1
'use strict'
2
2
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
5
6
const UnixFS = require ( 'ipfs-unixfs' )
6
- const bs58 = require ( 'bs58' )
7
7
const through = require ( 'through2' )
8
8
const isStream = require ( 'isstream' )
9
9
const promisify = require ( 'promisify-es6' )
10
+ const Duplex = require ( 'stream' ) . Duplex
11
+ const multihashes = require ( 'multihashes' )
10
12
11
13
module . exports = function files ( self ) {
12
14
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 ( )
17
23
}
18
- } ) ,
19
24
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
+ } ,
20
55
add : promisify ( ( data , callback ) => {
21
56
// Buffer input
22
57
if ( Buffer . isBuffer ( data ) ) {
@@ -33,7 +68,7 @@ module.exports = function files (self) {
33
68
} ]
34
69
}
35
70
if ( ! callback || typeof callback !== 'function' ) {
36
- callback = function oop ( ) { }
71
+ callback = function noop ( ) { }
37
72
}
38
73
if ( ! Array . isArray ( data ) ) {
39
74
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) {
43
78
const res = [ ]
44
79
45
80
// 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 )
48
83
self . _dagS . get ( mh , ( err , node ) => {
49
84
if ( err ) return callback ( err )
50
85
var obj = {
@@ -54,7 +89,7 @@ module.exports = function files (self) {
54
89
res . push ( obj )
55
90
next ( )
56
91
} )
57
- } , function end ( done ) {
92
+ } , ( done ) => {
58
93
callback ( null , res )
59
94
} ) )
60
95
0 commit comments