@@ -4,6 +4,7 @@ const debug = require('debug')
4
4
const log = debug ( 'exporter' )
5
5
log . err = debug ( 'exporter:error' )
6
6
const UnixFS = require ( 'ipfs-unixfs' )
7
+ const series = require ( 'run-series' )
7
8
const async = require ( 'async' )
8
9
const Readable = require ( 'readable-stream' ) . Readable
9
10
const pathj = require ( 'path' )
@@ -14,24 +15,21 @@ exports = module.exports = Exporter
14
15
util . inherits ( Exporter , Readable )
15
16
16
17
function Exporter ( hash , dagService , options ) {
17
- Readable . call ( this , { objectMode : true } )
18
-
19
18
if ( ! ( this instanceof Exporter ) ) {
20
- return new Exporter ( hash , dagService )
19
+ return new Exporter ( hash , dagService , options )
21
20
}
22
21
23
- if ( options ) {
24
- this . options = options
25
- } else {
26
- this . options = { }
27
- }
22
+ Readable . call ( this , { objectMode : true } )
23
+
24
+ this . options = options || { }
28
25
29
26
this . _read = ( n ) => { }
30
27
31
- this . fileExporter = ( node , name , dir , callback ) => {
28
+ let fileExporter = ( node , name , callback ) => {
32
29
let init
33
30
34
- if ( typeof dir === 'function' ) { callback = dir ; dir = { } }
31
+ if ( ! callback ) { callback = function noop ( ) { } }
32
+
35
33
var rs = new Readable ( )
36
34
if ( node . links . length === 0 ) {
37
35
const unmarshaledData = UnixFS . unmarshal ( node . data )
@@ -44,10 +42,8 @@ function Exporter (hash, dagService, options) {
44
42
rs . push ( unmarshaledData . data )
45
43
rs . push ( null )
46
44
}
47
- this . push ( { stream : rs , path : name , dir : dir } )
48
- if ( callback ) {
49
- callback ( )
50
- }
45
+ this . push ( { stream : rs , path : name } )
46
+ callback ( )
51
47
return
52
48
} else {
53
49
init = false
@@ -56,7 +52,29 @@ function Exporter (hash, dagService, options) {
56
52
return
57
53
}
58
54
init = true
59
- async . forEachSeries ( node . links , ( link , callback ) => {
55
+
56
+ const array = node . links . map ( ( link ) => {
57
+ return ( cb ) => {
58
+ dagService . get ( link . hash , ( err , res ) => {
59
+ if ( err ) {
60
+ cb ( err )
61
+ }
62
+ var unmarshaledData = UnixFS . unmarshal ( res . data )
63
+ rs . push ( unmarshaledData . data )
64
+ cb ( )
65
+ } )
66
+ }
67
+ } )
68
+ series ( array , ( err , res ) => {
69
+ if ( err ) {
70
+ callback ( )
71
+ return
72
+ }
73
+ rs . push ( null )
74
+ callback ( )
75
+ return
76
+ } )
77
+ /* async.forEachSeries(node.links, (link, callback) => {
60
78
dagService.get(link.hash, (err, res) => {
61
79
if (err) {
62
80
callback(err)
@@ -67,28 +85,25 @@ function Exporter (hash, dagService, options) {
67
85
})
68
86
}, (err) => {
69
87
if (err) {
70
- if ( callback ) {
71
- return callback ( err )
72
- }
88
+ callback()
73
89
return
74
90
}
75
91
rs.push(null)
76
- if ( callback ) {
77
- callback ( )
78
- }
92
+ callback()
79
93
return
80
- } )
81
- }
82
- this . push ( { stream : rs , path : name , dir : dir } )
83
- if ( callback ) {
84
- callback ( )
94
+ })*/
85
95
}
96
+ this . push ( { stream : rs , path : name } )
97
+ callback ( )
86
98
return
87
99
}
88
100
}
89
101
90
- this . dirExporter = ( node , name , callback ) => {
102
+ let dirExporter = ( node , name , callback ) => {
91
103
let init
104
+
105
+ if ( ! callback ) { callback = function noop ( ) { } }
106
+
92
107
var rs = new Readable ( )
93
108
if ( node . links . length === 0 ) {
94
109
init = false
@@ -100,10 +115,8 @@ function Exporter (hash, dagService, options) {
100
115
rs . push ( node . data )
101
116
rs . push ( null )
102
117
}
103
- this . push ( { stream : rs , path : name } )
104
- if ( callback ) {
105
- callback ( )
106
- }
118
+ this . push ( { stream : null , path : name } )
119
+ callback ( )
107
120
return
108
121
} else {
109
122
async . forEachSeries ( node . links , ( link , callback ) => {
@@ -113,40 +126,37 @@ function Exporter (hash, dagService, options) {
113
126
}
114
127
var unmarshaledData = UnixFS . unmarshal ( res . data )
115
128
if ( unmarshaledData . type === 'file' ) {
116
- return ( this . fileExporter ( res , pathj . join ( name , link . name ) , callback ) )
129
+ return ( fileExporter ( res , pathj . join ( name , link . name ) , callback ) )
117
130
}
118
131
if ( unmarshaledData . type === 'directory' ) {
119
- return ( this . dirExporter ( res , pathj . join ( name , link . name ) , callback ) )
132
+ return ( dirExporter ( res , pathj . join ( name , link . name ) , callback ) )
120
133
}
121
134
callback ( )
122
135
} )
123
136
} , ( err ) => {
124
137
if ( err ) {
125
- if ( callback ) {
126
- callback ( err )
127
- }
128
- return
129
- }
130
- if ( callback ) {
131
138
callback ( )
139
+ return
132
140
}
141
+ callback ( )
133
142
return
134
143
} )
135
144
}
136
145
}
137
146
138
147
dagService . get ( hash , ( err , fetchedNode ) => {
139
148
if ( err ) {
149
+ this . emit ( 'error' , err )
140
150
return
141
151
}
142
152
const data = UnixFS . unmarshal ( fetchedNode . data )
143
153
const type = data . type
144
154
145
155
if ( type === 'directory' ) {
146
- this . dirExporter ( fetchedNode , hash )
156
+ dirExporter ( fetchedNode , hash )
147
157
}
148
158
if ( type === 'file' ) {
149
- this . fileExporter ( fetchedNode , hash , false )
159
+ fileExporter ( fetchedNode , hash )
150
160
}
151
161
} )
152
162
0 commit comments