-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfallback.spec.js
531 lines (425 loc) · 16.5 KB
/
fallback.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
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
import assert from 'node:assert/strict'
import { describe, mock, test } from 'node:test'
import { Saturn } from '#src/index.js'
import { concatChunks, generateNodes, getMockServer, HTTP_STATUS_GONE, HTTP_STATUS_TIMEOUT, mockFlatFileOriginHandler, mockJWT, mockNodesHandlers, mockOrchHandler, mockOriginHandler, MSW_SERVER_OPTS } from './test-utils.js'
const TEST_DEFAULT_ORCH = 'https://orchestrator.strn.pl.test/nodes'
const TEST_NODES_LIST_KEY = 'saturn-nodes'
const TEST_AUTH = 'https://auth.test/'
const TEST_ORIGIN_DOMAIN = 'l1s.saturn.test'
const TEST_CUSTOMER_ORIGIN = 'customer.test/ipfs/bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'
const CLIENT_KEY = 'key'
const options = {
cdnURL: TEST_ORIGIN_DOMAIN,
orchURL: TEST_DEFAULT_ORCH,
authURL: TEST_AUTH,
experimental: true,
clientKey: CLIENT_KEY,
clientId: 'test'
}
describe('Client Fallback', () => {
test('Nodes are loaded from the orchestrator if no storage is passed', async (t) => {
const handlers = [
mockOrchHandler(2, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(2, TEST_ORIGIN_DOMAIN)
// No Storage is injected
const saturn = new Saturn({ ...options })
const mockOpts = { orchURL: TEST_DEFAULT_ORCH }
await saturn._loadNodes(mockOpts)
// Assert that the loaded nodes are the expected ones.
assert.deepEqual(saturn.nodes, expectedNodes)
server.close()
})
test('Storage is invoked correctly when supplied', async (t) => {
const handlers = [
mockOrchHandler(2, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(2, TEST_ORIGIN_DOMAIN)
const mockStorage = {
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}
// Mocking storage object
const storage = async () => mockStorage
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const saturn = new Saturn({ storage, ...options })
// Mocking options
const mockOpts = { orchURL: TEST_DEFAULT_ORCH }
await saturn._loadNodes(mockOpts)
// Assert that all the storage methods were called twice.
assert.strictEqual(mockStorage.set.mock.calls.length, 2)
assert.strictEqual(mockStorage.get.mock.calls.length, 2)
// Assert that the set method was invoked with the correct params.
assert.deepStrictEqual(mockStorage.set.mock.calls[0].arguments, [TEST_NODES_LIST_KEY, expectedNodes])
assert.deepEqual(saturn.nodes, expectedNodes)
server.close()
mock.reset()
})
test('Storage is loaded first when the orch is slower', async (t) => {
const handlers = [
mockOrchHandler(2, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN, 500)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(4, TEST_ORIGIN_DOMAIN)
// Mocking storage object
const mockStorage = {
get: async (key) => { return Promise.resolve(expectedNodes.slice(2, 4)) },
set: async (key, value) => { return null }
}
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })
// Mocking options
const mockOpts = { orchURL: TEST_DEFAULT_ORCH }
await saturn._loadNodes(mockOpts)
// Assert that all the storage methods were called twice.
assert.strictEqual(mockStorage.set.mock.calls.length, 2)
assert.strictEqual(mockStorage.get.mock.calls.length, 2)
// Assert that the set method was invoked with the correct params.
assert.deepStrictEqual(mockStorage.set.mock.calls[0].arguments, [TEST_NODES_LIST_KEY, expectedNodes.slice(0, 2)])
assert.deepEqual(saturn.nodes, expectedNodes.slice(0, 2))
server.close()
mock.reset()
})
test('Content Fallback fetches a cid properly', async (t) => {
const handlers = [
mockOrchHandler(2, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
mockOriginHandler(TEST_ORIGIN_DOMAIN, 0, true),
...mockNodesHandlers(2, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(2, TEST_ORIGIN_DOMAIN)
// Mocking storage object
const mockStorage = {
get: async (key) => { return Promise.resolve(expectedNodes.slice(2, 4)) },
set: async (key, value) => { return null }
}
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })
const cid = saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4')
const buffer = await concatChunks(cid)
const actualContent = String.fromCharCode(...buffer)
const expectedContent = 'hello world\n'
assert.strictEqual(actualContent, expectedContent)
server.close()
mock.reset()
})
test('Content Fallback fetches a cid properly with race', async (t) => {
const handlers = [
mockOrchHandler(5, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
mockOriginHandler(TEST_ORIGIN_DOMAIN, 0, true),
...mockNodesHandlers(5, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(3, TEST_ORIGIN_DOMAIN)
// Mocking storage object
const mockStorage = {
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })
const cid = saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4', { raceNodes: true })
const buffer = await concatChunks(cid)
const actualContent = String.fromCharCode(...buffer)
const expectedContent = 'hello world\n'
assert.strictEqual(actualContent, expectedContent)
server.close()
mock.reset()
})
test('Requests to the same cid go to the same node', async (t) => {
const handlers = [
mockOrchHandler(5, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
mockOriginHandler(TEST_ORIGIN_DOMAIN, 0, true),
...mockNodesHandlers(5, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(3, TEST_ORIGIN_DOMAIN)
// Mocking storage object
const mockStorage = {
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })
await saturn.loadNodesPromise
const hashring = saturn.hashring
const cid = 'bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'
const initialNode = hashring.get(cid)
const testRequestCount = 5
for (let i = 0; i < testRequestCount; i++) {
const node = hashring.get(cid)
assert.strictEqual(node, initialNode)
}
server.close()
mock.reset()
})
test('Content Fallback with race fetches from consecutive nodes on failure', async (t) => {
const handlers = [
mockOrchHandler(5, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
mockOriginHandler(TEST_ORIGIN_DOMAIN, 0, true),
...mockNodesHandlers(5, TEST_ORIGIN_DOMAIN, 2)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(5, TEST_ORIGIN_DOMAIN)
// Mocking storage object
const mockStorage = {
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const storage = async () => mockStorage
const saturn = new Saturn({ storage, ...options })
const cid = saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4', { raceNodes: true })
const buffer = await concatChunks(cid)
const actualContent = String.fromCharCode(...buffer)
const expectedContent = 'hello world\n'
assert.strictEqual(actualContent, expectedContent)
server.close()
mock.reset()
})
test('should fetch content from the first node successfully', async () => {
const handlers = [
mockOrchHandler(2, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
...mockNodesHandlers(2, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const saturn = new Saturn({ ...options })
const fetchContentMock = mock.fn(async function * (cidPath, opts) {
yield Buffer.from('chunk1')
yield Buffer.from('chunk2')
})
saturn.fetchContent = fetchContentMock
const content = await saturn.fetchContentWithFallback('some-cid-path')
const buffer = await concatChunks(content)
const expectedContent = new Uint8Array([...Buffer.from('chunk1'), ...Buffer.from('chunk2')])
assert.deepEqual(buffer, expectedContent)
assert.strictEqual(fetchContentMock.mock.calls.length, 1)
server.close()
mock.reset()
})
test('should try all nodes and fail if all nodes fail', async () => {
const numNodes = 3
const handlers = [
mockOrchHandler(numNodes, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
...mockNodesHandlers(numNodes, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const saturn = new Saturn({ ...options })
const fetchContentMock = mock.fn(async function * (cidPath, opts) { throw new Error('Fetch error') }) // eslint-disable-line
saturn.fetchContent = fetchContentMock
let error
try {
for await (const _ of saturn.fetchContentWithFallback('some-cid-path')) { // eslint-disable-line
// This loop body shouldn't be reached.
}
} catch (e) {
error = e
}
assert(error)
assert.strictEqual(error.message, 'All attempts to fetch content have failed. Last error: Fetch error')
assert.strictEqual(fetchContentMock.mock.calls.length, numNodes + 1)
mock.reset()
server.close()
})
test('should hit origin if failed to fetch', async (t) => {
const numNodes = 3
const handlers = [
mockOrchHandler(numNodes, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
mockOriginHandler(TEST_ORIGIN_DOMAIN, 0, true),
mockFlatFileOriginHandler(TEST_CUSTOMER_ORIGIN, 0, false),
...mockNodesHandlers(numNodes, TEST_ORIGIN_DOMAIN, numNodes, HTTP_STATUS_TIMEOUT)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const expectedNodes = generateNodes(5, TEST_ORIGIN_DOMAIN)
// Mocking storage object
const mockStorage = {
get: async (key) => expectedNodes,
set: async (key, value) => { return null }
}
t.mock.method(mockStorage, 'get')
t.mock.method(mockStorage, 'set')
const storage = async () => mockStorage
const saturn = new Saturn({ storage, customerFallbackURL: TEST_CUSTOMER_ORIGIN, ...options })
const cid = saturn.fetchContentWithFallback(TEST_CUSTOMER_ORIGIN, { raceNodes: true })
const buffer = await concatChunks(cid)
const actualContent = String.fromCharCode(...buffer)
const jsonContent = JSON.parse(actualContent)
const expectedContent = JSON.parse('{ "hello": "world" }')
assert.deepEqual(jsonContent, expectedContent)
mock.reset()
server.close()
})
test('Should abort fallback on 410s', async () => {
const numNodes = 3
const handlers = [
mockOrchHandler(numNodes, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
...mockNodesHandlers(numNodes, TEST_ORIGIN_DOMAIN, 3, HTTP_STATUS_GONE)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const saturn = new Saturn({ ...options })
await saturn.loadNodesPromise
let error
try {
for await (const _ of saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4')) { // eslint-disable-line
// This loop body shouldn't be reached.
}
} catch (e) {
error = e
}
const logs = saturn.logs
assert(error)
assert.strictEqual(logs.length, 1)
mock.reset()
server.close()
})
test('Should abort fallback on specific errors', async () => {
const numNodes = 3
const handlers = [
mockOrchHandler(numNodes, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
...mockNodesHandlers(numNodes, TEST_ORIGIN_DOMAIN, 3, HTTP_STATUS_GONE)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const saturn = new Saturn({ ...options })
await saturn.loadNodesPromise
let callCount = 0
const fetchContentMock = mock.fn(async function * (cidPath, opts) {
callCount++
yield ''
throw new Error('file does not exist')
})
saturn.fetchContent = fetchContentMock
let error
try {
for await (const _ of saturn.fetchContentWithFallback('bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4')) { // eslint-disable-line
}
} catch (e) {
error = e
}
assert(error)
assert.strictEqual(callCount, 1)
mock.reset()
server.close()
})
test('Handles fallback with chunk overlap correctly', async () => {
const numNodes = 3
const handlers = [
mockOrchHandler(numNodes, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
...mockNodesHandlers(numNodes, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const saturn = new Saturn({ ...options })
let callCount = 0
const fetchContentMock = mock.fn(async function * (cidPath, opts) {
callCount++
if (callCount === 1) {
throw new Error('First call error')
}
if (callCount === 2) {
yield Buffer.from('chunk1-overlap')
yield Buffer.from('chunk2')
}
})
saturn.fetchContent = fetchContentMock
const content = saturn.fetchContentWithFallback('some-cid-path')
const buffer = await concatChunks(content)
const expectedContent = new Uint8Array([
...Buffer.from('chunk1-overlap'),
...Buffer.from('chunk2')
])
assert.deepEqual(buffer, expectedContent)
assert.strictEqual(fetchContentMock.mock.calls.length, 2)
server.close()
mock.reset()
})
test('should handle byte chunk overlaps correctly', async () => {
const numNodes = 3
const handlers = [
mockOrchHandler(numNodes, TEST_DEFAULT_ORCH, TEST_ORIGIN_DOMAIN),
mockJWT(TEST_AUTH),
...mockNodesHandlers(numNodes, TEST_ORIGIN_DOMAIN)
]
const server = getMockServer(handlers)
server.listen(MSW_SERVER_OPTS)
const saturn = new Saturn({ ...options })
let callCount = 0
let fetchContentMock = mock.fn(async function * (cidPath, opts) {
callCount++
if (callCount === 1) {
yield Buffer.from('chunk1-overlap')
throw new Error('First call error')
}
if (callCount === 2) {
yield Buffer.from('chunk1-overlap')
yield Buffer.from('chunk2')
}
})
saturn.fetchContent = fetchContentMock
const expectedContent = new Uint8Array([
...Buffer.from('chunk1-overlap'),
...Buffer.from('chunk2')
])
let content = saturn.fetchContentWithFallback('some-cid-path')
let buffer = await concatChunks(content)
assert.deepEqual(buffer, expectedContent)
assert.strictEqual(fetchContentMock.mock.calls.length, 2)
callCount = 0
fetchContentMock = mock.fn(async function * (cidPath, opts) {
callCount++
if (callCount === 1) {
yield Buffer.from('chunk1-')
throw new Error('First call error')
}
if (callCount === 2) {
yield Buffer.from('chunk1')
yield Buffer.from('-overlap')
throw new Error('Second call error')
}
if (callCount === 3) {
yield Buffer.from('chunk1-overlap')
yield Buffer.from('chunk2')
}
})
saturn.fetchContent = fetchContentMock
content = await saturn.fetchContentWithFallback('some-cid-path')
buffer = await concatChunks(content)
assert.deepEqual(buffer, expectedContent)
assert.strictEqual(fetchContentMock.mock.calls.length, 3)
server.close()
mock.reset()
})
})