-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathretriever.test.js
More file actions
1213 lines (1103 loc) · 38.2 KB
/
retriever.test.js
File metadata and controls
1213 lines (1103 loc) · 38.2 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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect, vi, beforeAll } from 'vitest'
import worker from '../bin/piece-retriever.js'
import { createHash } from 'node:crypto'
import { retrieveFile } from '../lib/retrieval.js'
import {
env,
createExecutionContext,
waitOnExecutionContext,
} from 'cloudflare:test'
import {
withDataSetPieces,
withApprovedProvider,
withBadBits,
withWalletDetails,
withRequest,
} from './test-helpers.js'
import { CONTENT_STORED_ON_CALIBRATION } from './test-data.js'
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
export const DNS_ROOT = '.filbeam.io'
env.DNS_ROOT = DNS_ROOT
const botTokens = { secret: 'testbot' }
env.BOT_TOKENS = JSON.stringify(botTokens)
const botName = Object.values(botTokens)[0]
const botHeaders = { authorization: `Bearer ${Object.keys(botTokens)[0]}` }
describe('piece-retriever.fetch', () => {
const defaultPayerAddress = '0xc83dbfdf61616778537211a7e5ca2e87ec6cf0ed'
const { pieceCid: realPieceCid, dataSetId: realDataSetId } =
CONTENT_STORED_ON_CALIBRATION[0]
beforeAll(async () => {
await env.DB.batch([
env.DB.prepare('DELETE FROM pieces'),
env.DB.prepare('DELETE FROM data_sets'),
env.DB.prepare('DELETE FROM wallet_details'),
])
let cursor
while (true) {
const list = await env.BAD_BITS_KV.list({ cursor })
for (const key of list.keys) {
await env.BAD_BITS_KV.delete(key)
}
if (list.list_complete) break
cursor = list.cursor
}
let i = 1
for (const {
serviceProviderId,
serviceUrl,
pieceCid,
dataSetId,
} of CONTENT_STORED_ON_CALIBRATION) {
const pieceId = `root-${i}`
await withDataSetPieces(env, {
pieceId,
pieceCid,
dataSetId,
serviceProviderId,
payerAddress: defaultPayerAddress,
withCDN: true,
cdnEgressQuota: 100,
cacheMissEgressQuota: 100,
})
await withApprovedProvider(env, {
id: serviceProviderId,
serviceUrl,
})
i++
}
})
it('redirects to https://filbeam.com when no CID was provided', async () => {
const ctx = createExecutionContext()
const req = new Request(`https://${defaultPayerAddress}${DNS_ROOT}/`)
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(302)
expect(res.headers.get('Location')).toBe('https://filbeam.com/')
})
it('redirects to https://filbeam.com when no CID and no wallet address were provided', async () => {
const ctx = createExecutionContext()
const req = new Request(`https://${DNS_ROOT.slice(1)}/`)
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(302)
expect(res.headers.get('Location')).toBe('https://filbeam.com/')
})
it('redirects to https://*.filcdn.io/* when old domain was used', async () => {
const ctx = createExecutionContext()
const req = new Request(`https://foo.filcdn.io/bar`)
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(301)
expect(res.headers.get('Location')).toBe(`https://foo.filbeam.io/bar`)
})
it('returns 405 for unsupported request methods', async () => {
const ctx = createExecutionContext()
const req = withRequest(1, 'foo', 'POST')
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(405)
expect(await res.text()).toBe('Method Not Allowed')
})
it('returns 400 if required fields are missing', async () => {
const ctx = createExecutionContext()
const mockRetrieveFile = vi.fn()
const req = withRequest(undefined, 'foo')
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.status).toBe(400)
expect(await res.text()).toBe(
'Invalid hostname: filbeam.io. It must end with .filbeam.io.',
)
})
it('returns 400 if provided payer address is invalid', async () => {
const ctx = createExecutionContext()
const mockRetrieveFile = vi.fn()
const req = withRequest('bar', realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.status).toBe(400)
expect(await res.text()).toBe(
'Invalid address: bar. Address must be a valid ethereum address.',
)
})
it('returns the response from retrieveFile', async () => {
const fakeResponse = new Response('hello', {
status: 201,
headers: { 'X-Test': 'yes' },
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
expect(res.status).toBe(201)
expect(await res.text()).toBe('hello')
expect(res.headers.get('X-Test')).toBe('yes')
await waitOnExecutionContext(ctx)
})
it('sets Content-Control response header', async () => {
const originResponse = new Response('hello')
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: originResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await res.text()
await waitOnExecutionContext(ctx)
const cacheControlHeaders = res.headers.get('Cache-Control')
expect(cacheControlHeaders).toContain('public')
expect(cacheControlHeaders).toContain(`max-age=${env.CLIENT_CACHE_TTL}`)
})
it('sets Content-Control response on empty body', async () => {
const originResponse = new Response(null)
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: originResponse,
cacheMiss: false,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
const cacheControlHeaders = res.headers.get('Cache-Control')
expect(cacheControlHeaders).toContain('public')
expect(cacheControlHeaders).toContain(`max-age=${env.CLIENT_CACHE_TTL}`)
})
it('sets Content-Security-Policy response header', async () => {
const originResponse = new Response('hello', {
headers: {
'Content-Security-Policy': 'report-uri: https://endpoint.example.com',
},
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: originResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await res.text()
await waitOnExecutionContext(ctx)
const csp = res.headers.get('Content-Security-Policy')
expect(csp).toMatch(/^default-src 'self'/)
expect(csp).toContain('https://*.filbeam.io')
})
it('fetches the file from calibration service provider', async () => {
const expectedHash =
'3fde6bc0f4d21dd3b033b6100e3fa4023810f699b005b556bd28909b39fd87cf'
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, { retrieveFile })
expect(res.status).toBe(200)
// get the sha256 hash of the content
const content = await res.bytes()
const hash = createHash('sha256').update(content).digest('hex')
expect(hash).toEqual(expectedHash)
await waitOnExecutionContext(ctx)
})
it('stores retrieval results with cache miss and content length set in D1', async () => {
const body = 'file content'
const expectedEgressBytes = Buffer.byteLength(body, 'utf8')
const fakeResponse = new Response(body, {
status: 200,
headers: {
'CF-Cache-Status': 'MISS',
},
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await res.text()
await waitOnExecutionContext(ctx)
expect(res.status).toBe(200)
const readOutput = await env.DB.prepare(
`SELECT id, response_status, egress_bytes, cache_miss, bot_name
FROM retrieval_logs
WHERE data_set_id = ?`,
)
.bind(String(realDataSetId))
.all()
expect(readOutput.results).toStrictEqual([
{
id: 1, // Assuming this is the first log entry
response_status: 200,
egress_bytes: expectedEgressBytes,
cache_miss: 1, // 1 for true, 0 for false
bot_name: null, // No authorization header provided
},
])
})
it('stores retrieval results with cache hit and content length set in D1', async () => {
const body = 'file content'
const expectedEgressBytes = Buffer.byteLength(body, 'utf8')
const fakeResponse = new Response(body, {
status: 200,
headers: {
'CF-Cache-Status': 'HIT',
},
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,
cacheMiss: false,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await res.text()
await waitOnExecutionContext(ctx)
expect(res.status).toBe(200)
const readOutput = await env.DB.prepare(
`SELECT id, response_status, egress_bytes, cache_miss, bot_name
FROM retrieval_logs
WHERE data_set_id = ?`,
)
.bind(String(realDataSetId))
.all()
expect(readOutput.results).toStrictEqual([
{
id: 1, // Assuming this is the first log entry
response_status: 200,
egress_bytes: expectedEgressBytes,
cache_miss: 0, // 1 for true, 0 for false
bot_name: null, // No authorization header provided
},
])
})
it('stores retrieval performance stats in D1', async () => {
const body = 'file content'
const fakeResponse = new Response(body, {
status: 200,
headers: {
'CF-Cache-Status': 'MISS',
},
})
const mockRetrieveFile = async () => {
await sleep(1) // Simulate a delay
return {
response: fakeResponse,
cacheMiss: true,
}
}
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await res.text()
await waitOnExecutionContext(ctx)
expect(res.status).toBe(200)
const readOutput = await env.DB.prepare(
`SELECT
response_status,
fetch_ttfb,
fetch_ttlb,
worker_ttfb
FROM retrieval_logs
WHERE data_set_id = ?`,
)
.bind(String(realDataSetId))
.all()
expect(readOutput.results.length).toBe(1)
const result = readOutput.results[0]
expect(result.response_status).toBe(200)
expect(typeof result.fetch_ttfb).toBe('number')
expect(typeof result.fetch_ttlb).toBe('number')
expect(typeof result.worker_ttfb).toBe('number')
})
it('stores request country code in D1', async () => {
const body = 'file content'
const mockRetrieveFile = async () => {
return {
response: new Response(body, {
status: 200,
}),
cacheMiss: true,
}
}
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid, 'GET', {
'CF-IPCountry': 'US',
})
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await res.text()
await waitOnExecutionContext(ctx)
expect(res.status).toBe(200)
const { results } = await env.DB.prepare(
`SELECT request_country_code
FROM retrieval_logs
WHERE data_set_id = ?`,
)
.bind(String(realDataSetId))
.all()
expect(results).toStrictEqual([
{
request_country_code: 'US',
},
])
})
it('logs 0 egress bytes for empty body', async () => {
const fakeResponse = new Response(null, {
status: 200,
headers: {
'CF-Cache-Status': 'MISS',
},
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.status).toBe(200)
const readOutput = await env.DB.prepare(
'SELECT egress_bytes FROM retrieval_logs WHERE data_set_id = ?',
)
.bind(String(realDataSetId))
.all()
expect(readOutput.results).toStrictEqual([
expect.objectContaining({
egress_bytes: 0,
}),
])
})
it(
'measures egress correctly from real service provider',
{ timeout: 10000 },
async () => {
const tasks = CONTENT_STORED_ON_CALIBRATION.map(
({ dataSetId, pieceCid, serviceProviderId }) => {
return (async () => {
try {
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, { retrieveFile })
expect(res.status).toBe(200)
const content = await res.arrayBuffer()
await waitOnExecutionContext(ctx)
const actualBytes = content.byteLength
const { results } = await env.DB.prepare(
'SELECT egress_bytes FROM retrieval_logs WHERE data_set_id = ?',
)
.bind(String(dataSetId))
.all()
expect(results).toStrictEqual([
expect.objectContaining({
egress_bytes: actualBytes,
}),
])
return { serviceProviderId, success: true }
} catch (err) {
console.warn(
`⚠️ Warning: Fetch or verification failed for serviceProvider ${serviceProviderId}:`,
err,
)
throw err
}
})()
},
)
try {
const res = await Promise.allSettled(tasks)
if (!res.some((r) => r.status === 'fulfilled')) {
throw new Error('All tasks failed')
}
} catch (err) {
const serviceProvidersChecked = CONTENT_STORED_ON_CALIBRATION.map(
(o) => o.serviceProviderId,
)
throw new Error(
`❌ All service providers failed to fetch. Service providers checked: ${serviceProvidersChecked.join(', ')}`,
)
}
},
)
it('charges bots for egress', async () => {
const botToken = Object.keys(botTokens)[0]
/** @type {string} */
const botName = env.BOT_TOKENS[botToken]
console.log({ botToken, botName })
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: new Response('fake'),
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid, 'GET', {
authorization: `Bearer ${botToken}`,
})
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
expect(res.status).toBe(200)
await res.text()
await waitOnExecutionContext(ctx)
const readOutput = await env.DB.prepare(
'SELECT egress_bytes FROM retrieval_logs WHERE data_set_id = ?',
)
.bind(String(realDataSetId))
.all()
expect(readOutput.results).toStrictEqual([
expect.objectContaining({
egress_bytes: 4,
}),
])
})
it('requests payment if withCDN=false', async () => {
const dataSetId = 'test-data-set-no-cdn'
const pieceId = 'root-no-cdn'
const pieceCid =
'baga6ea4seaqaleibb6ud4xeemuzzpsyhl6cxlsymsnfco4cdjka5uzajo2x4ipa'
const serviceProviderId = 'service-provider'
await withDataSetPieces(env, {
serviceProviderId,
pieceCid,
dataSetId,
withCDN: false,
pieceId,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid, 'GET')
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(402)
})
it('reads the provider URL from the database', async () => {
const serviceProviderId = 'service-provider-id'
const payerAddress = '0x1234567890abcdef1234567890abcdef12345608'
const pieceCid = 'bagaTest'
const body = 'file content'
await withDataSetPieces(env, {
serviceProviderId,
pieceCid,
payerAddress,
cdnEgressQuota: 100,
cacheMissEgressQuota: 100,
})
await withApprovedProvider(env, {
id: serviceProviderId,
serviceUrl: 'https://mock-pdp-url.com',
})
const mockRetrieveFile = async () => {
return {
response: new Response(body, {
status: 200,
}),
cacheMiss: true,
}
}
const ctx = createExecutionContext()
const req = withRequest(payerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
// Check if the URL fetched is from the database
expect(await res.text()).toBe(body)
expect(res.status).toBe(200)
await waitOnExecutionContext(ctx)
})
it('throws an error if the providerAddress is not found in the database', async () => {
const serviceProviderId = 'service-provider-id'
const payerAddress = '0x2A06D234246eD18b6C91de8349fF34C22C7268e8'
const pieceCid = 'bagaTest'
await withDataSetPieces(env, {
serviceProviderId,
pieceCid,
payerAddress,
})
const ctx = createExecutionContext()
const req = withRequest(payerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
// Expect an error because no URL was found
expect(res.status).toBe(404)
expect(await res.text()).toBe(
`No approved service provider found for payer '0x2a06d234246ed18b6c91de8349ff34c22c7268e8' and piece_cid 'bagaTest'.`,
)
})
it('returns data set ID in the FB-Data-Set-ID response header', async () => {
const { pieceCid, dataSetId } = CONTENT_STORED_ON_CALIBRATION[0]
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: new Response('hello'),
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
expect(await res.text()).toBe('hello')
expect(res.headers.get('FB-Data-Set-ID')).toBe(String(dataSetId))
await waitOnExecutionContext(ctx)
})
it('stores data set ID in retrieval logs', async () => {
const { pieceCid, dataSetId } = CONTENT_STORED_ON_CALIBRATION[0]
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: new Response('hello'),
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
expect(await res.text()).toBe('hello')
await waitOnExecutionContext(ctx)
expect(res.status).toBe(200)
const { results } = await env.DB.prepare(
`SELECT id, response_status, cache_miss
FROM retrieval_logs
WHERE data_set_id = ?`,
)
.bind(String(dataSetId))
.all()
expect(results).toStrictEqual([
{
id: 1, // Assuming this is the first log entry
response_status: 200,
cache_miss: 1, // 1 for true, 0 for false
},
])
})
it('returns data set ID in the FB-Data-Set-ID response header when the response body is empty', async () => {
const { pieceCid, dataSetId } = CONTENT_STORED_ON_CALIBRATION[0]
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: new Response(null, { status: 404 }),
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.body).toBeNull()
expect(res.headers.get('FB-Data-Set-ID')).toBe(String(dataSetId))
})
it('supports HEAD requests', async () => {
const fakeResponse = new Response('file content', {
status: 200,
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid, 'HEAD')
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
expect(res.status).toBe(200)
await res.text()
await waitOnExecutionContext(ctx)
})
it('rejects retrieval requests for CIDs found in the Bad Bits denylist', async () => {
await withBadBits(env, realPieceCid)
const fakeResponse = new Response('hello')
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,
cacheMiss: true,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.status).toBe(404)
expect(await res.text()).toBe(
'The requested CID was flagged by the Bad Bits Denylist at https://badbits.dwebops.pub',
)
})
it('reject retrieval request if payer is sanctioned', async () => {
const dataSetId = 'test-data-set-payer-sanctioned'
const pieceId = 'root-data-set-payer-sanctioned'
const pieceCid =
'baga6ea4seaqaleibb6ud4xeemuzzpsyhl6cxlsymsnfco4cdjka5uzajo2x4ipa'
const serviceProviderId = 'service-provider-id'
const payerAddress = '0x999999cf1046e68e36E1aA2E0E07105eDDD1f08E'
await withDataSetPieces(env, {
serviceProviderId,
payerAddress,
dataSetId,
withCDN: true,
pieceCid,
pieceId,
})
await withWalletDetails(
env,
payerAddress,
true, // Sanctioned
)
const ctx = createExecutionContext()
const req = withRequest(payerAddress, pieceCid, 'GET')
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(403)
})
it('does not log to retrieval_logs on method not allowed (405)', async () => {
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, realPieceCid, 'POST')
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(405)
expect(await res.text()).toBe('Method Not Allowed')
const result = await env.DB.prepare(
`SELECT response_status FROM retrieval_logs WHERE data_set_id = ? ORDER BY id DESC LIMIT 1`,
)
.bind(realDataSetId)
.first()
expect(result).toBeNull()
})
it('logs to retrieval_logs on unsupported service provider (404)', async () => {
const invalidPieceCid = 'baga6ea4seaq3invalidrootcidfor404loggingtest'
const dataSetId = 'unsupported-serviceProvider-test'
const unsupportedServiceProviderId = 0
await withDataSetPieces(env, {
dataSetId,
serviceProviderId: unsupportedServiceProviderId,
payerAddress: defaultPayerAddress,
withCDN: true,
cdnEgressQuota: 100,
cacheMissEgressQuota: 100,
pieceCid: invalidPieceCid,
pieceId: 'piece-unsupported',
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, invalidPieceCid)
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(404)
expect(await res.text()).toContain('No approved service provider found')
const result = await env.DB.prepare(
'SELECT * FROM retrieval_logs WHERE data_set_id IS NULL AND response_status = 404 and CACHE_MISS IS NULL and egress_bytes IS NULL',
).first()
expect(result).toMatchObject({
bot_name: null,
})
})
it('logs to retrieval_logs on SP error', async () => {
const { pieceCid, dataSetId } = CONTENT_STORED_ON_CALIBRATION[0]
const url = 'https://example.com/piece/123'
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: new Response(null, { status: 510 }),
cacheMiss: true,
url,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.status).toBe(502)
const result = await env.DB.prepare(
'SELECT * FROM retrieval_logs WHERE data_set_id = ? AND response_status = 502 and CACHE_MISS IS NULL and egress_bytes IS NULL',
)
.bind(dataSetId)
.first()
expect(result).toBeDefined()
})
it('does not log to retrieval_logs when payer address is invalid (400)', async () => {
const { count: countBefore } = await env.DB.prepare(
'SELECT COUNT(*) AS count FROM retrieval_logs',
).first()
const invalidAddress = 'not-an-address'
const ctx = createExecutionContext()
const req = withRequest(invalidAddress, realPieceCid)
const res = await worker.fetch(req, env, ctx)
await waitOnExecutionContext(ctx)
expect(res.status).toBe(400)
expect(await res.text()).toContain('Invalid address')
const { count: countAfter } = await env.DB.prepare(
'SELECT COUNT(*) AS count FROM retrieval_logs',
).first()
expect(countAfter).toEqual(countBefore)
})
it('allows full transfer even when exceeding quota (quota goes negative)', async () => {
const payerAddress = '0xaaaa567890abcdef1234567890abcdef12345678'
const pieceCid =
'bafkquotatestexceedquotatestexceedquotatestexceedquotatestexce'
const dataSetId = 'quota-test-dataset-exceed'
const serviceProviderId = 'quota-test-provider-exceed'
// Set up provider and data set with small quota (100 bytes)
await withApprovedProvider(env, {
id: serviceProviderId,
serviceUrl: 'https://test-provider.com',
})
await withDataSetPieces(env, {
dataSetId,
serviceProviderId,
payerAddress,
withCDN: true,
cdnEgressQuota: 100,
cacheMissEgressQuota: 100,
pieceCid,
pieceId: 'piece-quota-test',
})
// Mock a response with more data than quota allows (500 bytes)
const largeContent = new Uint8Array(500).fill(65) // 500 'A's
const fakeResponse = new Response(largeContent, {
status: 200,
headers: { 'CF-Cache-Status': 'MISS', 'Content-Length': '500' },
})
const ctx = createExecutionContext()
const res = await worker.fetch(
withRequest(payerAddress, pieceCid),
{ ...env, ENFORCE_EGRESS_QUOTA: true },
ctx,
{
retrieveFile: async () => ({
response: fakeResponse,
cacheMiss: true,
validate: () => true,
}),
},
)
// Should get full content even when quota is exceeded
// Response should be successful with all 500 bytes
const body = await res.arrayBuffer()
expect(body.byteLength).toBe(500)
await waitOnExecutionContext(ctx)
// Check logs after execution context completes
const { results } = await env.DB.prepare(
'SELECT egress_bytes, response_status FROM retrieval_logs WHERE data_set_id = ?',
)
.bind(dataSetId)
.all()
expect(results).toStrictEqual([
{
egress_bytes: 500,
response_status: 200,
},
])
// Check that both quotas went negative (100 - 500 = -400)
const quotaResult = await env.DB.prepare(
'SELECT cdn_egress_quota, cache_miss_egress_quota FROM data_set_egress_quotas WHERE data_set_id = ?',
)
.bind(dataSetId)
.first()
expect(quotaResult).toStrictEqual({
cdn_egress_quota: -400,
cache_miss_egress_quota: -400,
})
})
it('allows full transfer when within quota', async () => {
const payerAddress = '0xbbbb567890abcdef1234567890abcdef12345678'
const pieceCid =
'bafkquotaokquotaokquotaokquotaokquotaokquotaokquotaokquotaok'
const dataSetId = 'quota-ok-dataset-unique'
const serviceProviderId = 'quota-ok-provider-unique'
// Set up provider and data set with sufficient quota (1000 bytes)
await withApprovedProvider(env, {
id: serviceProviderId,
serviceUrl: 'https://test-provider.com',
})
await withDataSetPieces(env, {
dataSetId,
serviceProviderId,
payerAddress,
withCDN: true,
cdnEgressQuota: 1000,
cacheMissEgressQuota: 1000,
pieceCid,
pieceId: 'piece-quota-ok',
})
// Mock a response that fits within quota (100 bytes)
const content = new Uint8Array(100).fill(65) // 100 'A's
const fakeResponse = new Response(content, {
status: 200,
headers: { 'CF-Cache-Status': 'HIT', 'Content-Length': '100' },
})
const ctx = createExecutionContext()
const res = await worker.fetch(
withRequest(payerAddress, pieceCid),
{ ...env, ENFORCE_EGRESS_QUOTA: true },
ctx,
{
retrieveFile: async () => ({
response: fakeResponse,
cacheMiss: false,
}),
},
)
// Should succeed
expect(res.status).toBe(200)
const body = await res.arrayBuffer()
expect(body.byteLength).toBe(100)
await waitOnExecutionContext(ctx)
// Check that full content was logged
const { results } = await env.DB.prepare(
'SELECT egress_bytes, response_status FROM retrieval_logs WHERE data_set_id = ?',
)
.bind(dataSetId)
.all()
expect(results).toStrictEqual([
{
egress_bytes: 100,
response_status: 200,
},
])
// Check that quotas were decremented correctly (cache hit)
const quotaResult = await env.DB.prepare(
'SELECT cdn_egress_quota, cache_miss_egress_quota FROM data_set_egress_quotas WHERE data_set_id = ?',
)
.bind(dataSetId)
.first()
expect(quotaResult).toStrictEqual({
cdn_egress_quota: 900,
cache_miss_egress_quota: 1000,
})
})
it('responds with 502 and a useful message when SP responds with an error', async () => {
const { pieceCid, dataSetId } = CONTENT_STORED_ON_CALIBRATION[0]
const url = 'https://example.com/piece/123'
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: new Response(null, { status: 500 }),
cacheMiss: true,
url,
})
const ctx = createExecutionContext()
const req = withRequest(defaultPayerAddress, pieceCid)
const res = await worker.fetch(req, env, ctx, {
retrieveFile: mockRetrieveFile,
})
await waitOnExecutionContext(ctx)
expect(res.status).toBe(502)
expect(await res.text()).toMatch(
/^No available service provider found. Attempted: ID=/,
)
expect(res.headers.get('FB-Data-Set-ID')).toBe(String(dataSetId))
const result = await env.DB.prepare(
'SELECT * FROM retrieval_logs WHERE data_set_id = ?',
)
.bind(String(dataSetId))
.first()
expect(result).toMatchObject({ bot_name: null })
})
it('stores bot name in retrieval logs when valid authorization header is provided', async () => {
const body = 'file content'
const fakeResponse = new Response(body, {
status: 200,
})
const mockRetrieveFile = vi.fn().mockResolvedValue({
response: fakeResponse,