44const isIpfs = require ( 'is-ipfs' )
55const loadFixture = require ( 'aegir/fixtures' )
66const hat = require ( 'hat' )
7- const waterfall = require ( 'async/waterfall' )
87const multibase = require ( 'multibase' )
98const { spawnNodeWithId } = require ( '../utils/spawn' )
10- const { connect } = require ( '../utils/swarm' )
119const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
1210
1311module . exports = ( createCommon , options ) => {
@@ -16,150 +14,88 @@ module.exports = (createCommon, options) => {
1614 const common = createCommon ( )
1715
1816 describe ( '.resolve' , ( ) => {
19- let factory , ipfs
17+ let ipfs
18+ let nodeId
2019
2120 before ( function ( done ) {
22- // CI takes longer to instantiate the daemon, so we need to increase the
23- // timeout for the before step
24- this . timeout ( 60 * 1000 )
25-
26- common . setup ( ( err , f ) => {
21+ common . setup ( ( err , factory ) => {
2722 expect ( err ) . to . not . exist ( )
28- factory = f
29- factory . spawnNode ( ( err , node ) => {
23+ spawnNodeWithId ( factory , ( err , node ) => {
3024 expect ( err ) . to . not . exist ( )
25+
3126 ipfs = node
27+ nodeId = node . peerId . id
3228 done ( )
3329 } )
3430 } )
3531 } )
3632
37- after ( function ( done ) {
38- this . timeout ( 10 * 1000 )
39- common . teardown ( done )
40- } )
33+ after ( common . teardown )
4134
42- it ( 'should resolve an IPFS hash' , ( done ) => {
35+ it ( 'should resolve an IPFS hash' , async ( ) => {
4336 const content = loadFixture ( 'test/fixtures/testfile.txt' , 'interface-ipfs-core' )
4437
45- ipfs . add ( content , ( err , res ) => {
46- expect ( err ) . to . not . exist ( )
47- expect ( isIpfs . cid ( res [ 0 ] . hash ) ) . to . be . true ( )
48-
49- ipfs . resolve ( `/ipfs/${ res [ 0 ] . hash } ` , ( err , path ) => {
50- expect ( err ) . to . not . exist ( )
51- expect ( path ) . to . equal ( `/ipfs/${ res [ 0 ] . hash } ` )
52- done ( )
53- } )
54- } )
38+ const [ { hash } ] = await ipfs . add ( content )
39+ const path = await ipfs . resolve ( `/ipfs/${ hash } ` )
40+ expect ( path ) . to . equal ( `/ipfs/${ hash } ` )
5541 } )
5642
57- it ( 'should resolve an IPFS hash and return a base64url encoded CID in path' , ( done ) => {
58- const content = Buffer . from ( 'TEST' + Date . now ( ) )
59-
60- ipfs . add ( content , ( err , res ) => {
61- expect ( err ) . to . not . exist ( )
43+ it ( 'should resolve an IPFS hash and return a base64url encoded CID in path' , async ( ) => {
44+ const [ { hash } ] = await ipfs . add ( Buffer . from ( 'base64url encoded' ) )
45+ const path = await ipfs . resolve ( `/ipfs/${ hash } ` , { cidBase : 'base64url' } )
46+ const [ , , cid ] = path . split ( '/' )
6247
63- ipfs . resolve ( `/ipfs/${ res [ 0 ] . hash } ` , { cidBase : 'base64url' } , ( err , path ) => {
64- expect ( err ) . to . not . exist ( )
65- const cid = path . split ( '/' ) [ 2 ]
66- expect ( multibase . isEncoded ( cid ) ) . to . equal ( 'base64url' )
67- done ( )
68- } )
69- } )
48+ expect ( multibase . isEncoded ( cid ) ) . to . equal ( 'base64url' )
7049 } )
7150
7251 // Test resolve turns /ipfs/QmRootHash/path/to/file into /ipfs/QmFileHash
73- it ( 'should resolve an IPFS path link' , ( done ) => {
52+ it ( 'should resolve an IPFS path link' , async ( ) => {
7453 const path = 'path/to/testfile.txt'
7554 const content = loadFixture ( 'test/fixtures/testfile.txt' , 'interface-ipfs-core' )
55+ const [ { hash : fileHash } , , , { hash : rootHash } ] = await ipfs . add ( [ { path, content } ] , { wrapWithDirectory : true } )
56+ const resolve = await ipfs . resolve ( `/ipfs/${ rootHash } /${ path } ` )
7657
77- ipfs . add ( [ { path, content } ] , { wrapWithDirectory : true } , ( err , res ) => {
78- expect ( err ) . to . not . exist ( )
79-
80- const rootHash = res . find ( r => r . path === '' ) . hash
81- const fileHash = res . find ( r => r . path === path ) . hash
82-
83- ipfs . resolve ( `/ipfs/${ rootHash } /${ path } ` , ( err , path ) => {
84- expect ( err ) . to . not . exist ( )
85- expect ( path ) . to . equal ( `/ipfs/${ fileHash } ` )
86- done ( )
87- } )
88- } )
58+ expect ( resolve ) . to . equal ( `/ipfs/${ fileHash } ` )
8959 } )
9060
91- it ( 'should resolve up to the last node' , ( done ) => {
61+ it ( 'should resolve up to the last node' , async ( ) => {
9262 const content = { path : { to : { file : hat ( ) } } }
9363 const options = { format : 'dag-cbor' , hashAlg : 'sha2-256' }
64+ const cid = await ipfs . dag . put ( content , options )
65+ const path = `/ipfs/${ cid } /path/to/file`
66+ const resolved = await ipfs . resolve ( path )
9467
95- ipfs . dag . put ( content , options , ( err , cid ) => {
96- expect ( err ) . to . not . exist ( )
97-
98- const path = `/ipfs/${ cid } /path/to/file`
99- ipfs . resolve ( path , ( err , resolved ) => {
100- expect ( err ) . to . not . exist ( )
101- expect ( resolved ) . to . equal ( path )
102- done ( )
103- } )
104- } )
68+ expect ( resolved ) . to . equal ( path )
10569 } )
10670
107- it ( 'should resolve up to the last node across multiple nodes' , ( done ) => {
71+ it ( 'should resolve up to the last node across multiple nodes' , async ( ) => {
10872 const options = { format : 'dag-cbor' , hashAlg : 'sha2-256' }
73+ const childCid = await ipfs . dag . put ( { node : { with : { file : hat ( ) } } } , options )
74+ const parentCid = await ipfs . dag . put ( { path : { to : childCid } } , options )
75+ const resolved = await ipfs . resolve ( `/ipfs/${ parentCid } /path/to/node/with/file` )
10976
110- waterfall ( [
111- cb => {
112- const content = { node : { with : { file : hat ( ) } } }
113- ipfs . dag . put ( content , options , cb )
114- } ,
115- ( childCid , cb ) => {
116- const content = { path : { to : childCid } }
117- ipfs . dag . put ( content , options , ( err , parentCid ) => cb ( err , { childCid, parentCid } ) )
118- }
119- ] , ( err , res ) => {
120- expect ( err ) . to . not . exist ( )
121-
122- const path = `/ipfs/${ res . parentCid } /path/to/node/with/file`
123- ipfs . resolve ( path , ( err , resolved ) => {
124- expect ( err ) . to . not . exist ( )
125- expect ( resolved ) . to . equal ( `/ipfs/${ res . childCid } /node/with/file` )
126- done ( )
127- } )
128- } )
77+ expect ( resolved ) . to . equal ( `/ipfs/${ childCid } /node/with/file` )
12978 } )
13079
13180 // Test resolve turns /ipns/domain.com into /ipfs/QmHash
132- it ( 'should resolve an IPNS DNS link' , function ( done ) {
133- this . timeout ( 20 * 1000 )
81+ it ( 'should resolve an IPNS DNS link' , async function ( ) {
82+ this . retries ( 3 )
83+ const resolved = await ipfs . resolve ( '/ipns/ipfs.io' )
13484
135- ipfs . resolve ( '/ipns/ipfs.io' , { r : true } , ( err , path ) => {
136- expect ( err ) . to . not . exist ( )
137- expect ( isIpfs . ipfsPath ( path ) ) . to . be . true ( )
138- done ( )
139- } )
85+ expect ( isIpfs . ipfsPath ( resolved ) ) . to . be . true ( )
14086 } )
14187
142- // Test resolve turns /ipns/QmPeerHash into /ipns/domain.com into /ipfs/QmHash
143- it ( 'should resolve IPNS link recursively' , function ( done ) {
144- this . timeout ( 5 * 60 * 1000 )
145-
146- waterfall ( [
147- // Ensure node has another node to publish a name to
148- ( cb ) => spawnNodeWithId ( factory , cb ) ,
149- ( ipfsB , cb ) => {
150- const addr = ipfsB . peerId . addresses . find ( ( a ) => a . includes ( '127.0.0.1' ) )
151- connect ( ipfs , addr , cb )
152- } ,
153- ( cb ) => ipfs . name . publish ( '/ipns/ipfs.io' , { resolve : false } , cb ) ,
154- ( res , cb ) => {
155- ipfs . resolve ( `/ipns/${ res . name } ` , { recursive : true } , ( err , res ) => {
156- expect ( err ) . to . not . exist ( )
157- expect ( res ) . to . not . equal ( '/ipns/ipfs.io' )
158- expect ( isIpfs . ipfsPath ( res ) ) . to . be . true ( )
159- cb ( )
160- } )
161- }
162- ] , done )
88+ it ( 'should resolve IPNS link recursively' , async function ( ) {
89+ this . timeout ( 20 * 1000 )
90+
91+ const [ { path } ] = await ipfs . add ( Buffer . from ( 'should resolve a record recursive === true' ) )
92+ const { id : keyId } = await ipfs . key . gen ( 'key-name' , { type : 'rsa' , size : 2048 } )
93+
94+ await ipfs . name . publish ( path , { 'allow-offline' : true } )
95+ await ipfs . name . publish ( `/ipns/${ nodeId } ` , { 'allow-offline' : true , key : 'key-name' } )
96+
97+ return expect ( await ipfs . resolve ( `/ipns/${ keyId } ` , { recursive : true } ) )
98+ . to . eq ( `/ipfs/${ path } ` )
16399 } )
164100 } )
165101}
0 commit comments