This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathresolve.js
97 lines (77 loc) · 3.61 KB
/
resolve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* eslint-env mocha */
'use strict'
const isIpfs = require('is-ipfs')
const loadFixture = require('aegir/fixtures')
const hat = require('hat')
const multibase = require('multibase')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const all = require('it-all')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.resolve', function () {
this.timeout(60 * 1000)
let ipfs
before(async () => {
ipfs = (await common.spawn()).api
})
after(() => common.clean())
it('should resolve an IPFS hash', async () => {
const content = loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core')
const [{ cid }] = await all(ipfs.add(content))
const path = await ipfs.resolve(`/ipfs/${cid}`)
expect(path).to.equal(`/ipfs/${cid}`)
})
it('should resolve an IPFS hash and return a base64url encoded CID in path', async () => {
const [{ cid }] = await all(ipfs.add(Buffer.from('base64url encoded')))
const path = await ipfs.resolve(`/ipfs/${cid}`, { cidBase: 'base64url' })
const [,, cidStr] = path.split('/')
expect(multibase.isEncoded(cidStr)).to.equal('base64url')
})
// Test resolve turns /ipfs/QmRootHash/path/to/file into /ipfs/QmFileHash
it('should resolve an IPFS path link', async () => {
const path = 'path/to/testfile.txt'
const content = loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core')
const [{ cid: fileCid }, , , { cid: rootCid }] = await all(ipfs.add([{ path, content }], { wrapWithDirectory: true }))
const resolve = await ipfs.resolve(`/ipfs/${rootCid}/${path}`)
expect(resolve).to.equal(`/ipfs/${fileCid}`)
})
it('should resolve up to the last node', async () => {
const content = { path: { to: { file: hat() } } }
const options = { format: 'dag-cbor', hashAlg: 'sha2-256' }
const cid = await ipfs.dag.put(content, options)
const path = `/ipfs/${cid}/path/to/file`
const resolved = await ipfs.resolve(path)
expect(resolved).to.equal(path)
})
it('should resolve up to the last node across multiple nodes', async () => {
const options = { format: 'dag-cbor', hashAlg: 'sha2-256' }
const childCid = await ipfs.dag.put({ node: { with: { file: hat() } } }, options)
const parentCid = await ipfs.dag.put({ path: { to: childCid } }, options)
const resolved = await ipfs.resolve(`/ipfs/${parentCid}/path/to/node/with/file`)
expect(resolved).to.equal(`/ipfs/${childCid}/node/with/file`)
})
// Test resolve turns /ipns/domain.com into /ipfs/QmHash
it('should resolve an IPNS DNS link', async function () {
this.retries(3)
const resolved = await ipfs.resolve('/ipns/ipfs.io')
expect(isIpfs.ipfsPath(resolved)).to.be.true()
})
it('should resolve IPNS link recursively', async function () {
this.timeout(20 * 1000)
const node = (await common.spawn()).api
await ipfs.swarm.connect(node.peerId.addresses[0])
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record recursive === true')))
const { id: keyId } = await ipfs.key.gen('key-name', { type: 'rsa', size: 2048 })
await ipfs.name.publish(path, { allowOffline: true })
await ipfs.name.publish(`/ipns/${ipfs.peerId.id}`, { allowOffline: true, key: 'key-name', resolve: false })
return expect(await ipfs.resolve(`/ipns/${keyId}`, { recursive: true }))
.to.eq(`/ipfs/${path}`)
})
})
}