-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfreeway.spec.js
More file actions
766 lines (676 loc) · 23.8 KB
/
freeway.spec.js
File metadata and controls
766 lines (676 loc) · 23.8 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import { describe, before, beforeEach, after, it } from 'node:test'
import assert from 'node:assert'
import { randomBytes } from 'node:crypto'
import { Log, LogLevel, Miniflare } from 'miniflare'
import * as Link from 'multiformats/link'
import { sha256 } from 'multiformats/hashes/sha2'
import * as raw from 'multiformats/codecs/raw'
import { equals } from 'multiformats/bytes'
import { Map as LinkMap } from 'lnmap'
import { CARReaderStream } from 'carstream'
import {
MultipartByteRangeDecoder,
decodePartHeader,
getBoundary
} from 'multipart-byte-range/decoder'
import { Builder, toBlobKey } from '../helpers/builder.js'
import {
generateBlockLocationClaims,
mockClaimsService,
generateLocationClaim,
generateIndexClaim
} from '../helpers/content-claims.js'
import { mockBucketService } from '../helpers/bucket.js'
import { fromShardArchives } from '@storacha/blob-index/util'
import { CAR_CODE } from '../../src/constants.js'
import http from 'node:http'
/** @import { Block, Position } from 'carstream/api' */
/**
* @param {{ arrayBuffer: () => Promise<ArrayBuffer> }} a
* @param {{ arrayBuffer: () => Promise<ArrayBuffer> }} b
*/
const assertBlobEqual = async (a, b) => {
const [abuf, bbuf] = await Promise.all([a.arrayBuffer(), b.arrayBuffer()])
assert.deepEqual(new Uint8Array(abuf), new Uint8Array(bbuf))
}
describe('freeway', () => {
/** @type {Miniflare} */
let miniflare
/** @type {Builder} */
let builder
/** @type {import('../helpers/content-claims.js').MockClaimsService} */
let claimsService
/** @type {import('miniflare').ReplaceWorkersTypes<R2Bucket>} */
let bucket
/** @type {http.Server} */
let server
/** @type {import('../helpers/bucket.js').MockBucketService} */
let bucketService
/** @type {URL} */
let url
before(async () => {
claimsService = await mockClaimsService()
server = http.createServer()
await new Promise((resolve) => server.listen(resolve))
// @ts-expect-error
const { port } = server.address()
url = new URL(`http://127.0.0.1:${port}`)
miniflare = new Miniflare({
host: '127.0.0.1',
port: 8787,
inspectorPort: 9898,
log: new Log(LogLevel.INFO),
cache: false, // Disable Worker Global Cache to test cache middlewares
bindings: {
CONTENT_CLAIMS_SERVICE_URL: claimsService.url.toString(),
CARPARK_PUBLIC_BUCKET_URL: url.toString(),
GATEWAY_SERVICE_DID: 'did:example:gateway',
DAGPB_CONTENT_CACHE: 'DAGPB_CONTENT_CACHE',
FF_DAGPB_CONTENT_CACHE_ENABLED: 'true',
FF_DAGPB_CONTENT_CACHE_TTL_SECONDS: 300,
FF_DAGPB_CONTENT_CACHE_MAX_SIZE_MB: 2
},
scriptPath: 'dist/worker.mjs',
modules: true,
compatibilityFlags: ['nodejs_compat'],
compatibilityDate: '2024-09-23',
r2Buckets: ['CARPARK'],
kvNamespaces: ['DAGPB_CONTENT_CACHE']
})
// @ts-expect-error miniflare types are not compatible
bucket = await miniflare.getR2Bucket('CARPARK')
bucketService = await mockBucketService(
/** @type {import('@web3-storage/public-bucket').Bucket} */
(bucket),
server
)
builder = new Builder(bucket)
})
beforeEach(async () => {
claimsService.resetCallCount()
claimsService.resetClaims()
bucketService.resetCallCount()
const dagpbCache = await miniflare.getKVNamespace('DAGPB_CONTENT_CACHE')
const keys = await dagpbCache.list()
for (const key of keys.keys) {
await dagpbCache.delete(key.name)
}
})
after(() => {
claimsService.close()
server.closeAllConnections()
server.close()
miniflare.dispose()
})
it('should get a file', async () => {
const input = new Blob([randomBytes(256)])
const { root, shards } = await builder.add(input)
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const claims = await generateBlockLocationClaims(
claimsService.signer,
shard,
res.body,
location
)
claimsService.addClaims(claims)
}
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}`
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
assertBlobEqual(input, await res.blob())
})
it('should get a file through a sharded dag index', async () => {
const input = new Blob([randomBytes(256)])
const { root, shards } = await builder.add(input)
/** @type {Uint8Array[]} */
const archives = []
/** @type {import('../helpers/content-claims.js').Claims} */
const claims = new LinkMap()
// get all archives and build location claims for them
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const shardContents = new Uint8Array(await res.arrayBuffer())
archives.push(shardContents)
const blocks = claims.get(shard) || []
blocks.push(
await generateLocationClaim(
claimsService.signer,
shard,
location,
0,
shardContents.length
)
)
claims.set(shard, blocks)
}
// build sharded dag index
const index = await fromShardArchives(root, archives)
const indexArchive = await index.archive()
assert(indexArchive.ok)
const digest = await sha256.digest(indexArchive.ok)
const indexLink = Link.create(CAR_CODE, digest)
// store sharded dag index
await bucket.put(toBlobKey(digest), indexArchive.ok)
// generate location claim for the index
const blocks = claims.get(indexLink) || []
const location = new URL(toBlobKey(indexLink.multihash), url)
blocks.push(
await generateLocationClaim(
claimsService.signer,
indexLink,
location,
0,
indexArchive.ok.length
)
)
claims.set(indexLink, blocks)
// generate index claim
const indexBlocks = claims.get(root) || []
indexBlocks.push(
await generateIndexClaim(claimsService.signer, root, indexLink)
)
claims.set(root, indexBlocks)
claimsService.addClaims(claims)
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}`
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
assertBlobEqual(input, await res.blob())
})
it('should get a file through a sharded dag index, even without location claims for shards', async () => {
const input = new Blob([randomBytes(256)])
const { root, shards } = await builder.add(input)
/** @type {Uint8Array[]} */
const archives = []
/** @type {import('../helpers/content-claims.js').Claims} */
const claims = new LinkMap()
// get all archives and build location claims for them
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const shardContents = new Uint8Array(await res.arrayBuffer())
archives.push(shardContents)
}
// build sharded dag index
const index = await fromShardArchives(root, archives)
const indexArchive = await index.archive()
assert(indexArchive.ok)
const digest = await sha256.digest(indexArchive.ok)
const indexLink = Link.create(CAR_CODE, digest)
// store sharded dag index
await bucket.put(toBlobKey(digest), indexArchive.ok)
// generate index claim
const indexBlocks = claims.get(root) || []
indexBlocks.push(
await generateIndexClaim(claimsService.signer, root, indexLink)
)
claims.set(root, indexBlocks)
claimsService.addClaims(claims)
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}`
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
assertBlobEqual(input, await res.blob())
})
it('should get a file in a directory', async () => {
const input = [
new File([randomBytes(256)], 'data.txt'),
new File([randomBytes(512)], 'image.png')
]
const { root, shards } = await builder.add(input)
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const claims = await generateBlockLocationClaims(
claimsService.signer,
shard,
res.body,
location
)
claimsService.addClaims(claims)
}
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}/${input[0].name}`
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
assertBlobEqual(input[0], await res.blob())
})
it('should get a big file', async () => {
const input = [new File([randomBytes(109_261_780)], 'sargo.tar.xz')]
const { root, shards } = await builder.add(input)
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const claims = await generateBlockLocationClaims(
claimsService.signer,
shard,
res.body,
location
)
claimsService.addClaims(claims)
}
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}/${input[0].name}`
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
assertBlobEqual(input[0], await res.blob())
})
it('should get a CAR via Accept headers', async () => {
const input = new Blob([randomBytes(256)])
const { root, shards } = await builder.add(input)
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const claims = await generateBlockLocationClaims(
claimsService.signer,
shard,
res.body,
location
)
claimsService.addClaims(claims)
}
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}`,
{
headers: { Accept: 'application/vnd.ipld.car;order=dfs;' }
}
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
const contentType = res.headers.get('Content-Type')
assert(contentType)
assert(contentType.includes('application/vnd.ipld.car'))
assert(contentType.includes('order=dfs'))
assert(res.body)
await assert.doesNotReject(
/** @type {ReadableStream<Uint8Array>} */
(res.body).pipeThrough(new CARReaderStream()).pipeTo(new WritableStream())
)
})
it('should GET a CAR by CAR CID', async () => {
const input = new Blob([randomBytes(10)])
const { root, shards } = await builder.add(input)
assert.equal(shards.length, 1)
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${shards[0]}`
)
assert(res.ok)
assert(res.body)
const source = /** @type {ReadableStream<Uint8Array>} */ (res.body)
const carStream = new CARReaderStream()
/** @type {(Block & Position)[]} */
const blocks = []
await source.pipeThrough(carStream).pipeTo(
new WritableStream({
write: (block) => {
blocks.push(block)
}
})
)
assert.equal(blocks.length, 1)
assert.equal(blocks[0].cid.toString(), root.toString())
const header = await carStream.getHeader()
assert.equal(header.roots.length, 1)
assert.equal(header.roots[0].toString(), root.toString())
const contentLength = parseInt(res.headers.get('Content-Length') ?? '0')
assert(contentLength)
const carpark = await miniflare.getR2Bucket('CARPARK')
const obj = await carpark.head(toBlobKey(shards[0].multihash))
assert(obj)
assert.equal(contentLength, obj.size)
assert.equal(
res.headers.get('Content-Type'),
'application/vnd.ipld.car; version=1;'
)
assert.equal(res.headers.get('Etag'), `"${shards[0]}"`)
})
it('should HEAD a CAR by CAR CID', async () => {
const input = new Blob([randomBytes(10)])
const { shards } = await builder.add(input)
assert.equal(shards.length, 1)
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${shards[0]}`,
{
method: 'HEAD'
}
)
assert(res.ok)
const contentLength = parseInt(res.headers.get('Content-Length') ?? '0')
assert(contentLength)
const carpark = await miniflare.getR2Bucket('CARPARK')
const obj = await carpark.head(toBlobKey(shards[0].multihash))
assert(obj)
assert.equal(contentLength, obj.size)
assert.equal(res.headers.get('Accept-Ranges'), 'bytes')
assert.equal(res.headers.get('Etag'), `"${shards[0]}"`)
})
it('should GET a byte range by CAR CID', async () => {
const input = new Blob([randomBytes(10)])
const { root, shards } = await builder.add(input)
assert.equal(shards.length, 1)
const carpark = await miniflare.getR2Bucket('CARPARK')
const obj = await carpark.get(toBlobKey(shards[0].multihash))
assert(obj)
assert(obj.body)
const source = /** @type {ReadableStream<Uint8Array>} */ (obj.body)
const carStream = new CARReaderStream()
/** @type {(Block & Position)[]} */
const blocks = []
await source.pipeThrough(carStream).pipeTo(
new WritableStream({
write: (block) => {
blocks.push(block)
}
})
)
assert.equal(blocks.length, 1)
assert.equal(blocks[0].cid.toString(), root.toString())
const rootBlock = blocks.find((e) => e.cid.toString() === root.toString())
assert(rootBlock)
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${shards[0]}`,
{
headers: {
Range: `bytes=${rootBlock.blockOffset}-${
rootBlock.blockOffset + rootBlock.blockLength - 1
}`
}
}
)
assert(res.ok)
const bytes = new Uint8Array(await res.arrayBuffer())
assert(equals(bytes, rootBlock.bytes))
const contentLength = parseInt(res.headers.get('Content-Length') ?? '0')
assert(contentLength)
assert.equal(contentLength, rootBlock.bytes.length)
assert.equal(
res.headers.get('Content-Range'),
`bytes ${rootBlock.blockOffset}-${
rootBlock.blockOffset + rootBlock.blockLength - 1
}/${obj.size}`
)
assert.equal(
res.headers.get('Content-Type'),
'application/vnd.ipld.car; version=1;'
)
assert.equal(res.headers.get('Etag'), `"${shards[0]}"`)
})
it('should GET a raw block', async () => {
const input = new Uint8Array(randomBytes(138))
const root = Link.create(raw.code, await sha256.digest(input))
const carpark = await miniflare.getR2Bucket('CARPARK')
const blobKey = toBlobKey(root.multihash)
await carpark.put(blobKey, input)
const location = new URL(blobKey, url)
const claim = await generateLocationClaim(
claimsService.signer,
root,
location,
0,
input.length
)
claimsService.addClaims(new LinkMap([[root, [claim]]]))
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}?format=raw`
)
assert(res.ok)
const output = new Uint8Array(await res.arrayBuffer())
assert.equal(output.length, input.length)
const contentLength = parseInt(res.headers.get('Content-Length') ?? '0')
assert(contentLength)
assert.equal(contentLength, input.length)
assert.equal(res.headers.get('Content-Type'), 'application/vnd.ipld.raw')
assert.equal(res.headers.get('Etag'), `"${root}.raw"`)
})
it('should HEAD a raw block', async () => {
const input = new Uint8Array(randomBytes(138))
const cid = Link.create(raw.code, await sha256.digest(input))
const carpark = await miniflare.getR2Bucket('CARPARK')
const blobKey = toBlobKey(cid.multihash)
await carpark.put(blobKey, input)
const location = new URL(blobKey, url)
const claim = await generateLocationClaim(
claimsService.signer,
cid,
location,
0,
input.length
)
claimsService.addClaims(new LinkMap([[cid, [claim]]]))
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${cid}?format=raw`,
{
method: 'HEAD'
}
)
assert(res.ok)
const contentLength = parseInt(res.headers.get('Content-Length') ?? '0')
assert(contentLength)
assert.equal(contentLength, input.length)
assert.equal(res.headers.get('Accept-Ranges'), 'bytes')
assert.equal(res.headers.get('Etag'), `"${cid}.raw"`)
})
it('should GET a byte range of raw block', async () => {
const input = [new File([randomBytes(1024 * 1024 * 5)], 'sargo.tar.xz')]
const { shards } = await builder.add(input)
const location = new URL(toBlobKey(shards[0].multihash), url)
const claim = await generateLocationClaim(
claimsService.signer,
shards[0],
location,
0,
input[0].size
)
claimsService.addClaims(new LinkMap([[shards[0], [claim]]]))
const res = await fetch(location)
assert(res.ok)
assert(res.body)
await res.body.pipeThrough(new CARReaderStream()).pipeTo(
new WritableStream({
async write ({ bytes, blockOffset, blockLength }) {
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${shards[0]}?format=raw`,
{
headers: {
Range: `bytes=${blockOffset}-${blockOffset + blockLength - 1}`
}
}
)
assert(res.ok)
assert(equals(new Uint8Array(await res.arrayBuffer()), bytes))
const contentLength = parseInt(
res.headers.get('Content-Length') ?? '0'
)
assert(contentLength)
assert.equal(contentLength, bytes.length)
assert.equal(
res.headers.get('Content-Range'),
`bytes ${blockOffset}-${blockOffset + blockLength - 1}/${
input[0].size
}`
)
assert.equal(
res.headers.get('Content-Type'),
'application/vnd.ipld.raw'
)
assert.equal(res.headers.get('Etag'), `"${shards[0]}.raw"`)
}
})
)
})
it('should GET a multipart byte range of raw block', async () => {
const input = [new File([randomBytes(1024 * 1024 * 5)], 'sargo.tar.xz')]
const { shards } = await builder.add(input)
const location = new URL(toBlobKey(shards[0].multihash), url)
const claim = await generateLocationClaim(
claimsService.signer,
shards[0],
location,
0,
input[0].size
)
claimsService.addClaims(new LinkMap([[shards[0], [claim]]]))
const res0 = await fetch(location)
assert(res0.ok)
assert(res0.body)
/** @type {Array<import('carstream/api').Block & import('carstream/api').Position>} */
const blocks = []
await res0.body.pipeThrough(new CARReaderStream()).pipeTo(
new WritableStream({
write (block) {
blocks.push(block)
}
})
)
const res1 = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${shards[0]}?format=raw`,
{
headers: {
Range: `bytes=${blocks
.map((b) => `${b.blockOffset}-${b.blockOffset + b.blockLength - 1}`)
.join(',')}`
}
}
)
assert(res1.ok)
assert(res1.body)
const boundary = getBoundary(new Headers([...res1.headers.entries()]))
assert(boundary)
let partsCount = 0
await /** @type {ReadableStream<Uint8Array>} */ (res1.body)
.pipeThrough(new MultipartByteRangeDecoder(boundary))
.pipeTo(
new WritableStream({
write (part) {
const block = blocks[partsCount]
const range = [
block.blockOffset,
block.blockOffset + block.blockLength - 1
]
const headers = decodePartHeader(part.header)
assert.equal(
headers.get('content-type'),
'application/vnd.ipld.raw'
)
assert.equal(
headers.get('content-range'),
`bytes ${range[0]}-${range[1]}/${input[0].size}`
)
partsCount++
}
})
)
})
it('should be faster to get a file in a directory when the protobuf directory structure is cached', async () => {
// Generate 3 files wrapped in a folder, >2MB each to force a unixfs file header block (dag protobuf)
const input = [
new File([randomBytes(2_050_550)], 'data.txt'),
new File([randomBytes(2_050_550)], 'image.png'),
new File([randomBytes(2_050_550)], 'image2.png')
]
// Adding to the builder will generate the unixfs file header block
const { root, shards } = await builder.add(input)
assert.equal(
root.code,
112,
'Root should be a protobuf directory code 112'
)
// Generate claims for the shards
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const claims = await generateBlockLocationClaims(
claimsService.signer,
shard,
res.body,
location
)
claimsService.addClaims(claims)
}
// Check that the cache is empty
const dagpb = await miniflare.getKVNamespace('DAGPB_CONTENT_CACHE')
const cachedContent1 = await dagpb.list()
assert.equal(cachedContent1.keys.length, 0, 'Cache should be empty')
// First request adds the file to the cache, so it takes longer
const start = performance.now()
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}/${input[2].name}`,
{
headers: {
'Cache-Control': 'no-cache'
}
}
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
const end = performance.now()
assertBlobEqual(input[2], await res.blob())
const cachedContent2 = await dagpb.list()
assert(
cachedContent2.keys.length > 0,
'Cache should have one or more keys'
)
// Second request retrieves the file from the cache, so it should take less time than the first request
const start2 = performance.now()
console.log('SECOND REQUEST')
const res2 = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}/${input[2].name}`,
{
headers: {
'Cache-Control': 'no-cache'
}
}
)
if (!res2.ok) assert.fail(`unexpected response: ${await res2.text()}`)
const end2 = performance.now()
assertBlobEqual(input[2], await res2.blob())
assert(
end2 - start2 < end - start,
'Second request should take less time than the first request'
)
})
it('should not cache content if it is not dag protobuf content', async () => {
// Generate 1 file, >1MB each and do not wrap it in a folder
const input = new File([randomBytes(256)], 'data.txt')
const { root, shards } = await builder.add(input)
assert.equal(root.code, 85, 'Root should be a raw file code 85')
// Generate claims for the shards
for (const shard of shards) {
const location = new URL(toBlobKey(shard.multihash), url)
const res = await fetch(location)
assert(res.body)
const claims = await generateBlockLocationClaims(
claimsService.signer,
shard,
res.body,
location
)
claimsService.addClaims(claims)
}
// Check that the cache is empty
const dagpb = await miniflare.getKVNamespace('DAGPB_CONTENT_CACHE')
const cachedContent = await dagpb.list()
assert.equal(cachedContent.keys.length, 0, 'Cache should be empty')
// It should not add the file to the cache, because it is not dag protobuf content
const res = await miniflare.dispatchFetch(
`http://localhost:8787/ipfs/${root}`,
{
headers: {
'Cache-Control': 'no-cache'
}
}
)
if (!res.ok) assert.fail(`unexpected response: ${await res.text()}`)
assertBlobEqual(input, await res.blob())
assert.equal(cachedContent.keys.length, 0, 'Cache should be empty')
})
})