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
184 lines (145 loc) · 7.07 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* eslint-env mocha */
'use strict'
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
const CID = require('cids')
const all = require('it-all')
const last = require('it-last')
/** @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('.name.resolve offline', function () {
let ipfs
let nodeId
before(async () => {
ipfs = (await common.spawn()).api
nodeId = ipfs.peerId.id
})
after(() => common.clean())
it('should resolve a record default options', async function () {
this.timeout(20 * 1000)
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record default options')))
const { id: keyId } = await ipfs.key.gen('key-name-default', { type: 'rsa', size: 2048 })
await ipfs.name.publish(path, { allowOffline: true })
await ipfs.name.publish(`/ipns/${nodeId}`, { allowOffline: true, key: 'key-name-default' })
expect(await last(ipfs.name.resolve(`/ipns/${keyId}`)))
.to.eq(`/ipfs/${path}`)
})
it('should resolve a record from peerid as cidv1 in base32', async function () {
this.timeout(20 * 1000)
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record from cidv1b32')))
const { id: peerId } = await ipfs.id()
await ipfs.name.publish(path, { allowOffline: true })
// Represent Peer ID as CIDv1 Base32
// https://github.com/libp2p/specs/blob/master/RFC/0001-text-peerid-cid.md
const keyCid = new CID(peerId).toV1().toString('base32')
const resolvedPath = await last(ipfs.name.resolve(`/ipns/${keyCid}`))
expect(resolvedPath).to.equal(`/ipfs/${path}`)
})
it('should resolve a record recursive === false', async () => {
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record recursive === false')))
await ipfs.name.publish(path, { allowOffline: true })
expect(await last(ipfs.name.resolve(`/ipns/${nodeId}`, { recursive: false })))
.to.eq(`/ipfs/${path}`)
})
it('should resolve a record recursive === true', async function () {
this.timeout(20 * 1000)
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/${nodeId}`, { allowOffline: true, key: 'key-name' })
expect(await last(ipfs.name.resolve(`/ipns/${keyId}`, { recursive: true })))
.to.eq(`/ipfs/${path}`)
})
it('should resolve a record default options with remainder', async function () {
this.timeout(20 * 1000)
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record default options with remainder')))
const { id: keyId } = await ipfs.key.gen('key-name-remainder-default', { type: 'rsa', size: 2048 })
await ipfs.name.publish(path, { allowOffline: true })
await ipfs.name.publish(`/ipns/${nodeId}`, { allowOffline: true, key: 'key-name-remainder-default' })
expect(await last(ipfs.name.resolve(`/ipns/${keyId}/remainder/file.txt`)))
.to.eq(`/ipfs/${path}/remainder/file.txt`)
})
it('should resolve a record recursive === false with remainder', async () => {
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record recursive = false with remainder')))
await ipfs.name.publish(path, { allowOffline: true })
expect(await last(ipfs.name.resolve(`/ipns/${nodeId}/remainder/file.txt`, { recursive: false })))
.to.eq(`/ipfs/${path}/remainder/file.txt`)
})
it('should resolve a record recursive === true with remainder', async function () {
this.timeout(20 * 1000)
const [{ path }] = await all(ipfs.add(Buffer.from('should resolve a record recursive = true with remainder')))
const { id: keyId } = await ipfs.key.gen('key-name-remainder', { type: 'rsa', size: 2048 })
await ipfs.name.publish(path, { allowOffline: true })
await ipfs.name.publish(`/ipns/${nodeId}`, { allowOffline: true, key: 'key-name-remainder' })
expect(await last(ipfs.name.resolve(`/ipns/${keyId}/remainder/file.txt`, { recursive: true })))
.to.eq(`/ipfs/${path}/remainder/file.txt`)
})
it('should not get the entry if its validity time expired', async () => {
const publishOptions = {
lifetime: '100ms',
ttl: '10s',
allowOffline: true
}
// we add new data instead of re-using fixture to make sure lifetime handling doesn't break
const [{ path }] = await all(ipfs.add(Buffer.from('should not get the entry if its validity time expired')))
await ipfs.name.publish(path, publishOptions)
await delay(500)
// go only has 1 possible error https://github.com/ipfs/go-ipfs/blob/master/namesys/interface.go#L51
// so here we just expect an Error and don't match the error type to expiration
try {
await last(ipfs.name.resolve(nodeId))
} catch (error) {
expect(error).to.exist()
}
})
})
describe('.name.resolve dns', function () {
let ipfs
this.retries(5)
before(async () => {
ipfs = (await common.spawn()).api
})
after(() => common.clean())
it('should resolve /ipns/ipfs.io', async () => {
expect(await last(ipfs.name.resolve('/ipns/ipfs.io')))
.to.match(/\/ipfs\/.+$/)
})
it('should resolve /ipns/ipfs.io recursive === false', async () => {
expect(await last(ipfs.name.resolve('/ipns/ipfs.io', { recursive: false })))
.to.match(/\/ipns\/.+$/)
})
it('should resolve /ipns/ipfs.io recursive === true', async () => {
expect(await last(ipfs.name.resolve('/ipns/ipfs.io', { recursive: true })))
.to.match(/\/ipfs\/.+$/)
})
it('should resolve /ipns/ipfs.io with remainder', async () => {
expect(await last(ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg')))
.to.match(/\/ipfs\/.+\/images\/ipfs-logo.svg$/)
})
it('should resolve /ipns/ipfs.io with remainder recursive === false', async () => {
expect(await last(ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: false })))
.to.match(/\/ipns\/.+\/images\/ipfs-logo.svg$/)
})
it('should resolve /ipns/ipfs.io with remainder recursive === true', async () => {
expect(await last(ipfs.name.resolve('/ipns/ipfs.io/images/ipfs-logo.svg', { recursive: true })))
.to.match(/\/ipfs\/.+\/images\/ipfs-logo.svg$/)
})
it('should fail to resolve /ipns/ipfs.a', async () => {
try {
await last(ipfs.name.resolve('ipfs.a'))
} catch (error) {
expect(error).to.exist()
}
})
it('should resolve ipns path with hamt-shard recursive === true', async () => {
expect(await last(ipfs.name.resolve('/ipns/tr.wikipedia-on-ipfs.org/wiki/Anasayfa.html', { recursive: true })))
.to.match(/\/ipfs\/.+$/)
})
})
}