This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpreload.spec.js
328 lines (272 loc) · 10.2 KB
/
preload.spec.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* eslint-env mocha */
import { nanoid } from 'nanoid'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { expect } from 'aegir/chai'
import all from 'it-all'
import { waitForCids, defaultAddr, clearPreloadCids } from './utils/mock-preload-node-utils.js'
import createNode from './utils/create-node.js'
import * as dagPB from '@ipld/dag-pb'
describe('preload', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
/** @type {() => Promise<void>} */
let cleanup
before(async () => {
const res = await createNode({
preload: {
enabled: true,
addresses: [defaultAddr]
}
})
ipfs = res.ipfs
cleanup = res.cleanup
})
after(() => cleanup())
afterEach(() => clearPreloadCids())
it('should not preload content multiple times', async function () {
this.timeout(50 * 1000)
const { cid } = await ipfs.add(uint8ArrayFromString(nanoid()), { preload: false })
await all(ipfs.cat(cid))
await waitForCids(cid)
// should not preload the second time
await clearPreloadCids()
await all(ipfs.cat(cid))
await expect(waitForCids(cid)).to.eventually.be.rejectedWith('Timed out waiting for CIDs to be preloaded')
})
it('should preload content added with add', async function () {
this.timeout(50 * 1000)
const res = await ipfs.add(uint8ArrayFromString(nanoid()))
await waitForCids(res.cid)
})
it('should preload multiple content added with add', async function () {
this.timeout(50 * 1000)
const res = await all(ipfs.addAll([{
content: uint8ArrayFromString(nanoid())
}, {
content: uint8ArrayFromString(nanoid())
}, {
content: uint8ArrayFromString(nanoid())
}]))
await waitForCids(res.map(file => file.cid))
})
it('should preload multiple content and intermediate dirs added with add', async function () {
this.timeout(50 * 1000)
const res = await all(ipfs.addAll([{
path: 'dir0/dir1/file0',
content: uint8ArrayFromString(nanoid())
}, {
path: 'dir0/dir1/file1',
content: uint8ArrayFromString(nanoid())
}, {
path: 'dir0/file2',
content: uint8ArrayFromString(nanoid())
}]))
const rootDir = res.find(file => file.path === 'dir0')
expect(rootDir).to.exist()
if (!rootDir) {
throw new Error('rootDir did not exist')
}
await waitForCids(rootDir.cid)
})
it('should preload multiple content and wrapping dir for content added with add and wrapWithDirectory option', async function () {
this.timeout(50 * 1000)
const res = await all(ipfs.addAll([{
path: 'dir0/dir1/file0',
content: uint8ArrayFromString(nanoid())
}, {
path: 'dir0/dir1/file1',
content: uint8ArrayFromString(nanoid())
}, {
path: 'dir0/file2',
content: uint8ArrayFromString(nanoid())
}], { wrapWithDirectory: true }))
const wrappingDir = res.find(file => file.path === '')
expect(wrappingDir).to.exist()
if (!wrappingDir) {
throw new Error('wrappingDir did not exist')
}
await waitForCids(wrappingDir.cid)
})
it('should preload content retrieved with cat', async function () {
this.timeout(50 * 1000)
const res = await ipfs.add(uint8ArrayFromString(nanoid()), { preload: false })
await all(ipfs.cat(res.cid))
await waitForCids(res.cid)
})
it('should preload content retrieved with get', async function () {
this.timeout(50 * 1000)
const res = await ipfs.add(uint8ArrayFromString(nanoid()), { preload: false })
await all(ipfs.get(res.cid))
await waitForCids(res.cid)
})
it('should preload content retrieved with ls', async function () {
this.timeout(50 * 1000)
const res = await all(ipfs.addAll([{
path: 'dir0/dir1/file0',
content: uint8ArrayFromString(nanoid())
}, {
path: 'dir0/dir1/file1',
content: uint8ArrayFromString(nanoid())
}, {
path: 'dir0/file2',
content: uint8ArrayFromString(nanoid())
}], { wrapWithDirectory: true, preload: false }))
const wrappingDir = res.find(file => file.path === '')
expect(wrappingDir).to.exist()
if (!wrappingDir) {
throw new Error('wrappingDir did not exist')
}
// Adding these files with have preloaded wrappingDir.hash, clear it out
await clearPreloadCids()
await all(ipfs.ls(wrappingDir.cid))
await waitForCids(wrappingDir.cid)
})
it('should preload content added with object.new', async function () {
this.timeout(50 * 1000)
const cid = await ipfs.object.new()
await waitForCids(cid)
})
it('should preload content added with object.put', async function () {
this.timeout(50 * 1000)
const cid = await ipfs.object.put({ Data: uint8ArrayFromString(nanoid()), Links: [] })
await waitForCids(cid)
})
it('should preload content added with object.patch.addLink', async function () {
this.timeout(50 * 1000)
const createNode = async () => {
const cid = await ipfs.object.put({ Data: uint8ArrayFromString(nanoid()), Links: [] })
const node = await ipfs.object.get(cid)
return { cid, node }
}
const [parent, link] = await Promise.all([createNode(), createNode()])
await clearPreloadCids()
const cid = await ipfs.object.patch.addLink(parent.cid, {
Name: 'link',
Hash: link.cid,
Tsize: dagPB.encode(link.node).length
})
await waitForCids(cid)
})
it('should preload content added with object.patch.rmLink', async function () {
this.timeout(50 * 1000)
const linkCid = await ipfs.object.put({ Data: uint8ArrayFromString(nanoid()), Links: [] })
const linkNode = await ipfs.object.get(linkCid)
const linkBuf = dagPB.encode(linkNode)
const parentCid = await ipfs.object.put({
Data: uint8ArrayFromString(nanoid()),
Links: [{
Name: 'link',
Hash: linkCid,
Tsize: linkBuf.length
}]
})
await clearPreloadCids()
const cid = await ipfs.object.patch.rmLink(parentCid, 'link')
await waitForCids(cid)
})
it('should preload content added with object.patch.setData', async function () {
this.timeout(50 * 1000)
const originalCid = await ipfs.object.put({ Data: uint8ArrayFromString(nanoid()), Links: [] })
await clearPreloadCids()
const patchedCid = await ipfs.object.patch.setData(originalCid, uint8ArrayFromString(nanoid()))
await waitForCids(patchedCid)
})
it('should preload content added with object.patch.appendData', async function () {
this.timeout(50 * 1000)
const originalCid = await ipfs.object.put({ Data: uint8ArrayFromString(nanoid()), Links: [] })
await clearPreloadCids()
const patchedCid = await ipfs.object.patch.appendData(originalCid, uint8ArrayFromString(nanoid()))
await waitForCids(patchedCid)
})
it('should preload content retrieved with object.get', async function () {
this.timeout(50 * 1000)
const cid = await ipfs.object.put({ Data: uint8ArrayFromString(nanoid()), Links: [] }, { preload: false })
await clearPreloadCids()
await ipfs.object.get(cid)
await waitForCids(cid)
})
it('should preload content added with block.put', async function () {
this.timeout(50 * 1000)
const cid = await ipfs.block.put(uint8ArrayFromString(nanoid()))
await waitForCids(cid)
})
it('should preload content retrieved with block.get', async function () {
this.timeout(50 * 1000)
const cid = await ipfs.block.put(uint8ArrayFromString(nanoid()), { preload: false })
await clearPreloadCids()
await ipfs.block.get(cid)
await waitForCids(cid)
})
it('should preload content retrieved with block.stat', async function () {
this.timeout(50 * 1000)
const cid = await ipfs.block.put(uint8ArrayFromString(nanoid()), { preload: false })
await clearPreloadCids()
await ipfs.block.stat(cid)
await waitForCids(cid)
})
it('should preload content added with dag.put', async function () {
this.timeout(50 * 1000)
const obj = { test: nanoid() }
const cid = await ipfs.dag.put(obj, { storeCodec: 'dag-cbor', hashAlg: 'sha2-256' })
await waitForCids(cid)
})
it('should preload content retrieved with dag.get', async function () {
this.timeout(50 * 1000)
const obj = { test: nanoid() }
const opts = { storeCodec: 'dag-cbor', hashAlg: 'sha2-256', preload: false }
const cid = await ipfs.dag.put(obj, opts)
await clearPreloadCids()
await ipfs.dag.get(cid)
await waitForCids(cid)
})
it('should preload content retrieved with files.ls', async () => {
const res = await ipfs.add({ path: `/t/${nanoid()}`, content: uint8ArrayFromString(nanoid()) }, { preload: false })
const dirCid = res.cid
await clearPreloadCids()
await all(ipfs.files.ls(`/ipfs/${dirCid}`))
await waitForCids(`/ipfs/${dirCid}`)
})
it('should preload content retrieved with files.ls by CID', async () => {
const res = await ipfs.add({ path: `/t/${nanoid()}`, content: uint8ArrayFromString(nanoid()) }, { preload: false })
const dirCid = res.cid
await all(ipfs.files.ls(dirCid))
await waitForCids(dirCid)
})
it('should preload content retrieved with files.read', async () => {
const { cid } = await ipfs.add(uint8ArrayFromString(nanoid()), { preload: false })
await clearPreloadCids()
await ipfs.files.read(`/ipfs/${cid}`)
await waitForCids(`/ipfs/${cid}`)
})
it('should preload content retrieved with files.stat', async () => {
const { cid: fileCid } = await ipfs.add(uint8ArrayFromString(nanoid()), { preload: false })
await clearPreloadCids()
await ipfs.files.stat(`/ipfs/${fileCid}`)
await waitForCids(`/ipfs/${fileCid}`)
})
})
describe('preload disabled', function () {
this.timeout(50 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
/** @type {() => Promise<void>} */
let cleanup
before(async () => {
const res = await createNode({
preload: {
enabled: false,
addresses: [defaultAddr]
}
})
ipfs = res.ipfs
cleanup = res.cleanup
})
after(() => cleanup())
it('should not preload if disabled', async () => {
const { cid } = await ipfs.add(uint8ArrayFromString(nanoid()))
return expect(waitForCids(cid))
.to.eventually.be.rejected()
.and.have.property('code')
.that.equals('ERR_TIMEOUT')
})
})