@@ -20,43 +20,21 @@ module.exports = (self) => {
20
20
return setImmediate ( ( ) => cb ( new Error ( 'invalid argument' ) ) )
21
21
}
22
22
23
- resolve ( name , opts , cb )
24
- } )
25
-
26
- function resolve ( name , opts , cb ) {
27
- if ( isIpfs . ipfsPath ( name ) ) {
28
- const split = name . split ( '/' ) // ['', 'ipfs', 'hash', ...path]
29
- const cid = new CID ( split [ 2 ] )
30
-
31
- if ( split . length === 3 ) {
32
- return setImmediate ( ( ) => cb ( null , `/ipfs/${ cidToString ( cid , { base : opts . cidBase } ) } ` ) )
33
- }
23
+ const split = name . split ( '/' ) // ['', 'ipfs', 'hash or domain', ...path]
24
+ const hashOrDomain = split [ 2 ]
25
+ const path = split . slice ( 3 ) . join ( '/' )
34
26
35
- const path = split . slice ( 3 ) . join ( '/' )
36
-
37
- resolveCID ( cid , path , ( err , res ) => {
38
- if ( err ) return cb ( err )
39
- const { cid, remainderPath } = res
40
- cb ( null , `/ipfs/${ cidToString ( cid , { base : opts . cidBase } ) } ${ remainderPath ? '/' + remainderPath : '' } ` )
41
- } )
42
- } else {
43
- const domain = name . split ( '/' ) [ 2 ]
44
- const path = name . split ( '/' ) . slice ( 3 ) . join ( '/' )
45
- console . log ( domain , path )
46
- return self . dns ( domain , ( err , result ) => {
47
- if ( err ) return cb ( err )
48
- const cid = new CID ( result . split ( '/' ) [ 2 ] )
49
- resolveCID ( cid , path , ( err , res ) => {
50
- if ( err ) return cb ( err )
51
- const { cid, remainderPath } = res
52
- cb ( null , `/ipfs/${ cidToString ( cid , { base : opts . cidBase } ) } ${ remainderPath ? '/' + remainderPath : '' } ` )
53
- } )
54
- } )
27
+ if ( isIpfs . cid ( hashOrDomain ) ) {
28
+ return resolveCID ( hashOrDomain , path , opts , cb )
55
29
}
56
- }
57
30
58
- // Resolve the given CID + path to a CID.
59
- function resolveCID ( cid , path , callback ) {
31
+ // if its not a cid then its probably a domain name to resolve
32
+ return resolveDomain ( hashOrDomain , path , opts , cb )
33
+ } )
34
+
35
+ // Resolve the given CID + path to a CID (recursive).
36
+ function resolveCID ( hash , path , opts , callback ) {
37
+ let cid = new CID ( hash )
60
38
let value , remainderPath
61
39
doUntil (
62
40
( cb ) => {
@@ -96,8 +74,21 @@ module.exports = (self) => {
96
74
} ,
97
75
( err ) => {
98
76
if ( err ) return callback ( err )
99
- callback ( null , { cid, remainderPath : path } )
77
+ callback ( null , `/ipfs/ ${ cidToString ( cid , { base : opts . cidBase } ) } ${ remainderPath ? '/' + remainderPath : '' } ` )
100
78
}
101
79
)
102
80
}
81
+
82
+ function resolveDomain ( domain , path , opts , callback ) {
83
+ const recursive = opts . recursive && opts . recursive . toString ( ) === 'true'
84
+ return self . dns ( domain , ( err , result ) => {
85
+ if ( err ) return callback ( err )
86
+ const hash = result . split ( '/' ) [ 2 ]
87
+ const remainderPath = path
88
+ if ( recursive ) {
89
+ return resolveCID ( hash , remainderPath , opts , callback )
90
+ }
91
+ callback ( null , `/ipfs/${ cidToString ( hash , { base : opts . cidBase } ) } ${ remainderPath ? '/' + remainderPath : '' } ` )
92
+ } )
93
+ }
103
94
}
0 commit comments