1
1
'use strict'
2
2
3
3
const debug = require ( 'debug' )
4
- const log = debug ( 'exporter ' )
5
- log . err = debug ( 'exporter :error' )
4
+ const log = debug ( 'unixfs ' )
5
+ log . err = debug ( 'unixfs :error' )
6
6
const isIPFS = require ( 'is-ipfs' )
7
- const bs58 = require ( 'bs58' )
8
7
const UnixFS = require ( 'ipfs-unixfs' )
9
8
const series = require ( 'run-series' )
10
9
const Readable = require ( 'readable-stream' ) . Readable
@@ -21,13 +20,10 @@ function Exporter (hash, dagService, options) {
21
20
return new Exporter ( hash , dagService , options )
22
21
}
23
22
24
- // Sanitize hash.
23
+ // Sanitize hash
25
24
if ( ! isIPFS . multihash ( hash ) ) {
26
25
throw new Error ( 'not valid multihash' )
27
26
}
28
- if ( Buffer . isBuffer ( hash ) ) {
29
- hash = bs58 . encode ( hash )
30
- }
31
27
32
28
Readable . call ( this , { objectMode : true } )
33
29
@@ -36,61 +32,52 @@ function Exporter (hash, dagService, options) {
36
32
this . _read = ( n ) => { }
37
33
38
34
let fileExporter = ( node , name , done ) => {
39
- let init = false
35
+ if ( ! done ) {
36
+ throw new Error ( 'done must be set' )
37
+ }
40
38
41
- if ( ! done ) throw new Error ( 'done must be set' )
39
+ const contentRS = new Readable ( )
40
+ contentRS . _read = ( ) => { }
42
41
43
42
// Logic to export a single (possibly chunked) unixfs file.
44
- var rs = new Readable ( )
45
43
if ( node . links . length === 0 ) {
46
44
const unmarshaledData = UnixFS . unmarshal ( node . data )
47
- rs . _read = ( ) => {
48
- if ( init ) {
49
- return
50
- }
51
- init = true
52
- rs . push ( unmarshaledData . data )
53
- rs . push ( null )
54
- }
55
- this . push ( { content : rs , path : name } )
45
+ contentRS . push ( unmarshaledData . data )
46
+ contentRS . push ( null )
47
+ this . push ( { content : contentRS , path : name } )
56
48
done ( )
57
49
} else {
58
- rs . _read = ( ) => {
59
- if ( init ) {
60
- return
50
+ const array = node . links . map ( ( link ) => {
51
+ return ( cb ) => {
52
+ dagService . get ( link . hash , ( err , res ) => {
53
+ if ( err ) {
54
+ return cb ( err )
55
+ }
56
+ var unmarshaledData = UnixFS . unmarshal ( res . data )
57
+ contentRS . push ( unmarshaledData . data )
58
+ cb ( )
59
+ } )
61
60
}
62
- init = true
63
-
64
- const array = node . links . map ( ( link ) => {
65
- return ( cb ) => {
66
- dagService . get ( link . hash , ( err , res ) => {
67
- if ( err ) {
68
- return cb ( err )
69
- }
70
- var unmarshaledData = UnixFS . unmarshal ( res . data )
71
- rs . push ( unmarshaledData . data )
72
- cb ( )
73
- } )
74
- }
75
- } )
76
- series ( array , ( err , res ) => {
77
- if ( err ) {
78
- rs . emit ( 'error' , err )
79
- return
80
- }
81
- rs . push ( null )
82
- return
83
- } )
84
- }
85
- this . push ( { content : rs , path : name } )
61
+ } )
62
+ series ( array , ( err ) => {
63
+ if ( err ) {
64
+ return contentRS . emit ( 'error' , err )
65
+ }
66
+ contentRS . push ( null )
67
+ } )
68
+ this . push ( { content : contentRS , path : name } )
86
69
done ( )
87
70
}
88
71
}
89
72
90
73
// Logic to export a unixfs directory.
91
74
let dirExporter = ( node , name , add , done ) => {
92
- if ( ! add ) throw new Error ( 'add must be set' )
93
- if ( ! done ) throw new Error ( 'done must be set' )
75
+ if ( ! add ) {
76
+ throw new Error ( 'add must be set' )
77
+ }
78
+ if ( ! done ) {
79
+ throw new Error ( 'done must be set' )
80
+ }
94
81
95
82
this . push ( { content : null , path : name } )
96
83
@@ -104,32 +91,29 @@ function Exporter (hash, dagService, options) {
104
91
}
105
92
106
93
// Traverse the DAG asynchronously
107
- var self = this
108
- fieldtrip ( [ { path : hash , hash : hash } ] , visit , ( err ) => {
94
+ fieldtrip ( [ { path : hash , hash : hash } ] , visit . bind ( this ) , ( err ) => {
109
95
if ( err ) {
110
- self . emit ( 'error' , err )
111
- return
96
+ return this . emit ( 'error' , err )
112
97
}
113
- self . push ( null )
98
+ this . push ( null )
114
99
} )
115
100
116
101
// Visit function: called once per node in the exported graph
117
102
function visit ( item , add , done ) {
118
- dagService . get ( item . hash , ( err , fetchedNode ) => {
103
+ dagService . get ( item . hash , ( err , node ) => {
119
104
if ( err ) {
120
- self . emit ( 'error' , err )
121
- return
105
+ return this . emit ( 'error' , err )
122
106
}
123
107
124
- const data = UnixFS . unmarshal ( fetchedNode . data )
108
+ const data = UnixFS . unmarshal ( node . data )
125
109
const type = data . type
126
110
127
111
if ( type === 'directory' ) {
128
- dirExporter ( fetchedNode , item . path , add , done )
112
+ dirExporter ( node , item . path , add , done )
129
113
}
130
114
131
115
if ( type === 'file' ) {
132
- fileExporter ( fetchedNode , item . path , done )
116
+ fileExporter ( node , item . path , done )
133
117
}
134
118
} )
135
119
}
0 commit comments