@@ -38,6 +38,18 @@ const keyLookup = (ipfsNode, kname, callback) => {
38
38
} )
39
39
}
40
40
41
+ const appendRemainder = ( cb , remainder ) => {
42
+ return ( err , result ) => {
43
+ if ( err ) {
44
+ return cb ( err )
45
+ }
46
+ if ( remainder . length ) {
47
+ return cb ( null , result + '/' + remainder . join ( '/' ) )
48
+ }
49
+ return cb ( null , result )
50
+ }
51
+ }
52
+
41
53
/**
42
54
* @typedef { import("../index") } IPFS
43
55
*/
@@ -46,7 +58,7 @@ const keyLookup = (ipfsNode, kname, callback) => {
46
58
* IPNS - Inter-Planetary Naming System
47
59
*
48
60
* @param {IPFS } self
49
- * @returns {Function }
61
+ * @returns {Object }
50
62
*/
51
63
module . exports = function name ( self ) {
52
64
return {
@@ -162,27 +174,28 @@ module.exports = function name (self) {
162
174
name = `/ipns/${ name } `
163
175
}
164
176
165
- const [ , hash ] = name . slice ( 1 ) . split ( '/' )
177
+ const [ namespace , hash , ... remainder ] = name . slice ( 1 ) . split ( '/' )
166
178
try {
167
179
mh . fromB58String ( hash )
168
-
169
- // ipns resolve needs a online daemon
170
- if ( ! self . isOnline ( ) && ! offline ) {
171
- const errMsg = utils . OFFLINE_ERROR
172
-
173
- log . error ( errMsg )
174
- return callback ( errcode ( errMsg , 'OFFLINE_ERROR' ) )
175
- }
176
- self . _ipns . resolve ( name , options , callback )
177
180
} catch ( err ) {
178
181
// lets check if we have a domain ex. /ipns/ipfs.io and resolve with dns
179
182
if ( isDomain ( hash ) ) {
180
- return self . dns ( hash , options , callback )
183
+ return self . dns ( hash , options , appendRemainder ( callback , remainder ) )
181
184
}
182
185
183
186
log . error ( err )
184
- callback ( errcode ( new Error ( 'Invalid IPNS name.' ) , 'ERR_IPNS_INVALID_NAME' ) )
187
+ return callback ( errcode ( new Error ( 'Invalid IPNS name.' ) , 'ERR_IPNS_INVALID_NAME' ) )
188
+ }
189
+
190
+ // multihash is valid lets resolve with IPNS
191
+ // IPNS resolve needs a online daemon
192
+ if ( ! self . isOnline ( ) && ! offline ) {
193
+ const errMsg = utils . OFFLINE_ERROR
194
+
195
+ log . error ( errMsg )
196
+ return callback ( errcode ( errMsg , 'OFFLINE_ERROR' ) )
185
197
}
198
+ self . _ipns . resolve ( `/${ namespace } /${ hash } ` , options , appendRemainder ( callback , remainder ) )
186
199
} ) ,
187
200
pubsub : namePubsub ( self )
188
201
}
0 commit comments