@@ -12,35 +12,30 @@ const PathUtil = require('./utils/path')
12
12
13
13
const INDEX_HTML_FILES = [ 'index.html' , 'index.htm' , 'index.shtml' ]
14
14
15
+ function noop ( ) { }
16
+
15
17
const resolveDirectory = promisify ( ( ipfs , path , multihash , callback ) => {
16
- if ( ! callback ) {
17
- callback = noop
18
- }
18
+ callback = callback || noop
19
19
20
20
mh . validate ( mh . fromB58String ( multihash ) )
21
21
22
- ipfs
23
- . object
24
- . get ( multihash , { enc : 'base58' } )
25
- . then ( ( DAGNode ) => {
26
- const links = DAGNode . links
27
- const indexFiles = links . filter ( ( link ) => INDEX_HTML_FILES . indexOf ( link . name ) !== - 1 )
22
+ ipfs . object . get ( multihash , { enc : 'base58' } , ( err , dagNode ) => {
23
+ if ( err ) { return callback ( err ) }
28
24
29
- // found index file in links
30
- if ( indexFiles . length > 0 ) {
31
- return callback ( null , indexFiles )
32
- }
25
+ const links = dagNode . links
26
+ const indexFiles = links . filter ( ( link ) => INDEX_HTML_FILES . indexOf ( link . name ) !== - 1 )
33
27
34
- return callback ( null , html . build ( path , links ) )
35
- } )
36
- } )
28
+ // found index file in links
29
+ if ( indexFiles . length > 0 ) {
30
+ return callback ( null , indexFiles )
31
+ }
37
32
38
- const noop = function ( ) { }
33
+ return callback ( null , html . build ( path , links ) )
34
+ } )
35
+ } )
39
36
40
37
const resolveMultihash = promisify ( ( ipfs , path , callback ) => {
41
- if ( ! callback ) {
42
- callback = noop
43
- }
38
+ callback = callback || noop
44
39
45
40
const parts = PathUtil . splitPath ( path )
46
41
const partsLength = parts . length
@@ -53,62 +48,57 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
53
48
log ( 'currentMultihash: ' , currentMultihash )
54
49
log ( 'currentIndex: ' , currentIndex , '/' , partsLength )
55
50
56
- ipfs
57
- . object
58
- . get ( currentMultihash , { enc : 'base58' } )
59
- . then ( ( DAGNode ) => {
60
- // log('DAGNode: ', DAGNode)
61
- if ( currentIndex === partsLength - 1 ) {
62
- // leaf node
63
- log ( 'leaf node: ' , currentMultihash )
64
- // log('DAGNode: ', DAGNode.links)
65
-
66
- if ( DAGNode . links &&
67
- DAGNode . links . length > 0 &&
68
- DAGNode . links [ 0 ] . name . length > 0 ) {
69
- // this is a directory.
70
- let isDirErr = new Error ( 'This dag node is a directory' )
71
- // add currentMultihash as a fileName so it can be used by resolveDirectory
72
- isDirErr . fileName = currentMultihash
73
- return next ( isDirErr )
74
- }
75
-
76
- next ( )
77
- } else {
78
- // find multihash of requested named-file
79
- // in current DAGNode's links
80
- let multihashOfNextFile
81
- const nextFileName = parts [ currentIndex + 1 ]
82
- const links = DAGNode . links
83
-
84
- for ( let link of links ) {
85
- if ( link . name === nextFileName ) {
86
- // found multihash of requested named-file
87
- multihashOfNextFile = mh . toB58String ( link . multihash )
88
- log ( 'found multihash: ' , multihashOfNextFile )
89
- break
90
- }
91
- }
92
-
93
- if ( ! multihashOfNextFile ) {
94
- log . error ( `no link named "${ nextFileName } " under ${ currentMultihash } ` )
95
- throw new Error ( `no link named "${ nextFileName } " under ${ currentMultihash } ` )
96
- }
97
-
98
- currentMultihash = multihashOfNextFile
99
- next ( )
100
- }
101
- } )
51
+ ipfs . object . get ( currentMultihash , { enc : 'base58' } , ( err , dagNode ) => {
52
+ if ( err ) { return next ( err ) }
53
+
54
+ if ( currentIndex === partsLength - 1 ) {
55
+ // leaf node
56
+ log ( 'leaf node: ' , currentMultihash )
57
+
58
+ // TODO: Check if it is a directory by using Unixfs Type, right now
59
+ // it won't detect empty dirs
60
+ if ( dagNode . links &&
61
+ dagNode . links . length > 0 &&
62
+ dagNode . links [ 0 ] . name . length > 0 ) {
63
+ // this is a directory.
64
+
65
+ let isDirErr = new Error ( 'This dag node is a directory' )
66
+ // add currentMultihash as a fileName so it can be used by resolveDirectory
67
+ isDirErr . fileName = currentMultihash
68
+ return next ( isDirErr )
69
+ }
70
+ return next ( )
71
+ }
72
+
73
+ // find multihash of requested named-file in current dagNode's links
74
+ let multihashOfNextFile
75
+ const nextFileName = parts [ currentIndex + 1 ]
76
+ const links = dagNode . links
77
+
78
+ for ( let link of links ) {
79
+ if ( link . name === nextFileName ) {
80
+ // found multihash of requested named-file
81
+ multihashOfNextFile = mh . toB58String ( link . multihash )
82
+ log ( 'found multihash: ' , multihashOfNextFile )
83
+ break
84
+ }
85
+ }
86
+
87
+ if ( ! multihashOfNextFile ) {
88
+ log . error ( `no link named "${ nextFileName } " under ${ currentMultihash } ` )
89
+ return next ( new Error ( `no link named "${ nextFileName } " under ${ currentMultihash } ` ) )
90
+ }
91
+
92
+ currentMultihash = multihashOfNextFile
93
+ next ( )
94
+ } )
102
95
} , ( err ) => {
103
- if ( err ) {
104
- log . error ( err )
105
- return callback ( err )
106
- }
107
- callback ( null , { multihash : currentMultihash } )
96
+ if ( err ) { return callback ( err ) }
97
+ callback ( null , { multihash : currentMultihash } )
108
98
} )
109
99
} )
110
100
111
101
module . exports = {
112
- resolveDirectory,
113
- resolveMultihash
102
+ resolveDirectory : resolveDirectory ,
103
+ resolveMultihash : resolveMultihash
114
104
}
0 commit comments