This repository was archived by the owner on Aug 12, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +18
-16
lines changed Expand file tree Collapse file tree 1 file changed +18
-16
lines changed Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
+ const traverse = require ( 'pull-traverse' )
3
4
const UnixFS = require ( 'ipfs-unixfs' )
4
5
const pull = require ( 'pull-stream' )
5
6
6
7
// Logic to export a single (possibly chunked) unixfs file.
7
8
module . exports = ( node , name , ds ) => {
8
- const file = UnixFS . unmarshal ( node . data )
9
- let content
9
+ function getData ( node ) {
10
+ try {
11
+ const file = UnixFS . unmarshal ( node . data )
12
+ return file . data || new Buffer ( 0 )
13
+ } catch ( err ) {
14
+ throw new Error ( 'Failed to unmarshal node' )
15
+ }
16
+ }
10
17
11
- if ( node . links . length === 0 ) {
12
- content = pull . values ( [ file . data ] )
13
- } else {
14
- content = pull (
18
+ function visitor ( node ) {
19
+ return pull (
15
20
pull . values ( node . links ) ,
16
21
pull . map ( ( link ) => ds . getStream ( link . hash ) ) ,
17
- pull . flatten ( ) ,
18
- pull . map ( ( node ) => {
19
- try {
20
- const ex = UnixFS . unmarshal ( node . data )
21
- return ex . data
22
- } catch ( err ) {
23
- console . error ( node )
24
- throw new Error ( 'Failed to unmarshal node' )
25
- }
26
- } )
22
+ pull . flatten ( )
27
23
)
28
24
}
29
25
26
+ let content = pull (
27
+ traverse . depthFirst ( node , visitor ) ,
28
+ pull . map ( getData )
29
+ )
30
+
31
+ const file = UnixFS . unmarshal ( node . data )
30
32
return pull . values ( [ {
31
33
content : content ,
32
34
path : name ,
You can’t perform that action at this time.
0 commit comments