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
136 lines (106 loc) · 3.46 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* eslint max-nested-callbacks: ["error", 6] */
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const { fixture } = require('./utils')
const { spawnNodeWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.name.resolve', function () {
const keyName = hat()
let ipfs
let nodeId
let keyId
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()
spawnNodeWithId(factory, (err, node) => {
expect(err).to.not.exist()
ipfs = node
nodeId = node.peerId.id
ipfs.files.add(fixture.data, { pin: false }, done)
})
})
})
after((done) => common.teardown(done))
it('should resolve a record with the default params after a publish', (done) => {
this.timeout(50 * 1000)
const value = fixture.cid
ipfs.name.publish(value, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
ipfs.name.resolve(nodeId, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res.path).to.equal(`/ipfs/${value}`)
done()
})
})
})
it('should not get the entry if its validity time expired', (done) => {
this.timeout(50 * 1000)
const value = fixture.cid
const publishOptions = {
resolve: true,
lifetime: '1ms',
ttl: '10s',
key: 'self'
}
ipfs.name.publish(value, publishOptions, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
// guarantee that the record has an expired validity.
setTimeout(function () {
ipfs.name.resolve(nodeId, (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist()
done()
})
}, 1)
})
})
it('should recursively resolve to an IPFS hash', (done) => {
this.timeout(100 * 1000)
const value = fixture.cid
const publishOptions = {
resolve: true,
lifetime: '24h',
ttl: '10s',
key: 'self'
}
// Generate new key
ipfs.key.gen(keyName, { type: 'rsa', size: 2048 }, (err, key) => {
expect(err).to.not.exist()
keyId = key.id
// publish ipfs
ipfs.name.publish(value, publishOptions, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
publishOptions.key = keyName
// publish ipns with the generated key
ipfs.name.publish(`/ipns/${nodeId}`, publishOptions, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
const resolveOptions = {
nocache: false,
recursive: true
}
// recursive resolve (will get ipns first, and will resolve again to find the ipfs)
ipfs.name.resolve(keyId, resolveOptions, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res.path).to.equal(`/ipfs/${value}`)
done()
})
})
})
})
})
})
}