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
114 lines (93 loc) · 3.43 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* eslint-env mocha */
'use strict'
const isIpfs = require('is-ipfs')
const loadFixture = require('aegir/fixtures')
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.resolve', () => {
let ipfs
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})
after(function (done) {
this.timeout(10 * 1000)
common.teardown(done)
})
it('should resolve an IPFS hash', (done) => {
const content = loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core')
ipfs.files.add(content, (err, res) => {
expect(err).to.not.exist()
expect(isIpfs.cid(res[0].hash)).to.be.true()
ipfs.resolve(`/ipfs/${res[0].hash}`, (err, path) => {
expect(err).to.not.exist()
expect(path).to.equal(`/ipfs/${res[0].hash}`)
done()
})
})
})
// Test resolve turns /ipfs/QmRootHash/path/to/file into /ipfs/QmFileHash
it('should resolve an IPFS path link', (done) => {
const path = '/path/to/testfile.txt'
const content = loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core')
ipfs.files.add([{ path, content }], { wrapWithDirectory: true }, (err, res) => {
expect(err).to.not.exist()
const rootHash = res.find(r => r.path === '').hash
const fileHash = res.find(r => r.path === path).hash
ipfs.resolve(`/ipfs/${rootHash}${path}`, (err, path) => {
expect(err).to.not.exist()
expect(path).to.equal(`/ipfs/${fileHash}`)
done()
})
})
})
it('should not resolve an IPFS path non-link', (done) => {
const content = { path: { to: { file: hat() } } }
const options = { format: 'dag-cbor', hashAlg: 'sha2-256' }
ipfs.dag.put(content, options, (err, cid) => {
expect(err).to.not.exist()
const path = `/ipfs/${cid.toBaseEncodedString()}/path/to/file`
ipfs.resolve(path, (err, path) => {
expect(err).to.exist()
expect(err.message).to.equal('found non-link at given path')
done()
})
})
})
// Test resolve turns /ipns/domain.com into /ipfs/QmHash
it('should resolve an IPNS DNS link', function (done) {
this.timeout(20 * 1000)
ipfs.resolve('/ipns/ipfs.io', (err, path) => {
expect(err).to.not.exist()
expect(isIpfs.ipfsPath(path)).to.be.true()
done()
})
})
// Test resolve turns /ipns/QmPeerHash into /ipns/domain.com into /ipfs/QmHash
it('should resolve IPNS link recursively', function (done) {
this.timeout(2 * 60 * 1000)
ipfs.name.publish('/ipns/ipfs.io', { resolve: false }, (err, res) => {
expect(err).to.not.exist()
ipfs.resolve(`/ipns/${res.name}`, { recursive: true }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.not.equal('/ipns/ipfs.io')
expect(isIpfs.ipfsPath(res)).to.be.true()
done()
})
})
})
})
}