@ar.io/sdk 3.11.0-beta.1 → 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/bundles/web.bundle.min.js +111 -114
  2. package/lib/cjs/common/ant-versions.js +3 -2
  3. package/lib/cjs/common/index.js +0 -2
  4. package/lib/cjs/common/io.js +13 -9
  5. package/lib/cjs/common/turbo.js +1 -1
  6. package/lib/cjs/types/index.js +0 -1
  7. package/lib/cjs/utils/ao.js +2 -1
  8. package/lib/cjs/version.js +1 -1
  9. package/lib/esm/common/ant-versions.js +3 -2
  10. package/lib/esm/common/index.js +0 -2
  11. package/lib/esm/common/io.js +13 -9
  12. package/lib/esm/common/turbo.js +1 -1
  13. package/lib/esm/types/index.js +0 -1
  14. package/lib/esm/utils/ao.js +2 -1
  15. package/lib/esm/version.js +1 -1
  16. package/lib/types/common/ant-versions.d.ts +3 -5
  17. package/lib/types/common/index.d.ts +0 -1
  18. package/lib/types/types/index.d.ts +0 -1
  19. package/lib/types/version.d.ts +1 -1
  20. package/package.json +1 -1
  21. package/lib/cjs/common/wayfinder/gateways/network.js +0 -48
  22. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +0 -35
  23. package/lib/cjs/common/wayfinder/gateways/static.js +0 -13
  24. package/lib/cjs/common/wayfinder/index.js +0 -47
  25. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +0 -72
  26. package/lib/cjs/common/wayfinder/routing/strategies/ping.test.js +0 -156
  27. package/lib/cjs/common/wayfinder/routing/strategies/random.js +0 -13
  28. package/lib/cjs/common/wayfinder/routing/strategies/random.test.js +0 -68
  29. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +0 -42
  30. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.test.js +0 -78
  31. package/lib/cjs/common/wayfinder/routing/strategies/static.js +0 -29
  32. package/lib/cjs/common/wayfinder/routing/strategies/static.test.js +0 -40
  33. package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +0 -139
  34. package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +0 -50
  35. package/lib/cjs/common/wayfinder/verification/trusted.js +0 -106
  36. package/lib/cjs/common/wayfinder/wayfinder.js +0 -736
  37. package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -519
  38. package/lib/cjs/types/wayfinder.js +0 -3
  39. package/lib/esm/common/wayfinder/gateways/network.js +0 -44
  40. package/lib/esm/common/wayfinder/gateways/simple-cache.js +0 -31
  41. package/lib/esm/common/wayfinder/gateways/static.js +0 -9
  42. package/lib/esm/common/wayfinder/index.js +0 -31
  43. package/lib/esm/common/wayfinder/routing/strategies/ping.js +0 -68
  44. package/lib/esm/common/wayfinder/routing/strategies/ping.test.js +0 -151
  45. package/lib/esm/common/wayfinder/routing/strategies/random.js +0 -9
  46. package/lib/esm/common/wayfinder/routing/strategies/random.test.js +0 -63
  47. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +0 -38
  48. package/lib/esm/common/wayfinder/routing/strategies/round-robin.test.js +0 -73
  49. package/lib/esm/common/wayfinder/routing/strategies/static.js +0 -25
  50. package/lib/esm/common/wayfinder/routing/strategies/static.test.js +0 -35
  51. package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +0 -130
  52. package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +0 -46
  53. package/lib/esm/common/wayfinder/verification/trusted.js +0 -102
  54. package/lib/esm/common/wayfinder/wayfinder.js +0 -724
  55. package/lib/esm/common/wayfinder/wayfinder.test.js +0 -514
  56. package/lib/esm/types/wayfinder.js +0 -2
  57. package/lib/types/common/wayfinder/gateways/network.d.ts +0 -33
  58. package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +0 -31
  59. package/lib/types/common/wayfinder/gateways/static.d.ts +0 -23
  60. package/lib/types/common/wayfinder/index.d.ts +0 -26
  61. package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +0 -27
  62. package/lib/types/common/wayfinder/routing/strategies/ping.test.d.ts +0 -1
  63. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +0 -21
  64. package/lib/types/common/wayfinder/routing/strategies/random.test.d.ts +0 -1
  65. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +0 -29
  66. package/lib/types/common/wayfinder/routing/strategies/round-robin.test.d.ts +0 -1
  67. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +0 -29
  68. package/lib/types/common/wayfinder/routing/strategies/static.test.d.ts +0 -1
  69. package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +0 -31
  70. package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +0 -27
  71. package/lib/types/common/wayfinder/verification/trusted.d.ts +0 -51
  72. package/lib/types/common/wayfinder/wayfinder.d.ts +0 -299
  73. package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
  74. package/lib/types/types/wayfinder.d.ts +0 -66
@@ -1,519 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const axios_1 = __importDefault(require("axios"));
7
- const got_1 = __importDefault(require("got"));
8
- const node_assert_1 = __importDefault(require("node:assert"));
9
- const node_events_1 = require("node:events");
10
- const node_stream_1 = require("node:stream");
11
- const consumers_1 = require("node:stream/consumers");
12
- const node_test_1 = require("node:test");
13
- const logger_js_1 = require("../../common/logger.js");
14
- const random_js_1 = require("./routing/strategies/random.js");
15
- const static_js_1 = require("./routing/strategies/static.js");
16
- const wayfinder_js_1 = require("./wayfinder.js");
17
- // TODO: replace with locally running gateway
18
- const gatewayUrl = 'permagate.io';
19
- const stubbedGatewaysProvider = {
20
- getGateways: async () => [new URL(`http://${gatewayUrl}`)],
21
- };
22
- logger_js_1.Logger.default.setLogLevel('none');
23
- (0, node_test_1.describe)('Wayfinder', () => {
24
- (0, node_test_1.describe)('http wrapper', () => {
25
- (0, node_test_1.describe)('fetch', () => {
26
- let wayfinder;
27
- (0, node_test_1.before)(() => {
28
- wayfinder = new wayfinder_js_1.Wayfinder({
29
- httpClient: fetch,
30
- routingStrategy: new random_js_1.RandomRoutingStrategy(),
31
- gatewaysProvider: stubbedGatewaysProvider,
32
- });
33
- });
34
- (0, node_test_1.it)('should fetch the data using the selected gateway', async () => {
35
- const nativeFetch = await fetch(`https://ao.${gatewayUrl}`);
36
- const response = await wayfinder.request('ar://ao');
37
- node_assert_1.default.strictEqual(response.status, 200);
38
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
39
- // assert the arns headers are the same
40
- const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
41
- const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
42
- node_assert_1.default.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
43
- });
44
- (0, node_test_1.it)('should fetch a tx id using the selected gateway', async () => {
45
- const nativeFetch = await fetch(`https://${gatewayUrl}/KKmRbIfrc7wiLcG0zvY1etlO0NBx1926dSCksxCIN3A`,
46
- // follow redirects
47
- { redirect: 'follow' });
48
- const response = await wayfinder.request('ar://KKmRbIfrc7wiLcG0zvY1etlO0NBx1926dSCksxCIN3A', { redirect: 'follow' });
49
- node_assert_1.default.strictEqual(response.status, 200);
50
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
51
- });
52
- (0, node_test_1.it)('should route a non-ar:// url as a normal fetch', async () => {
53
- const [nativeFetch, response] = await Promise.all([
54
- fetch(`https://${gatewayUrl}/`, {
55
- method: 'HEAD',
56
- redirect: 'follow',
57
- }),
58
- wayfinder.request(`https://${gatewayUrl}/`, {
59
- method: 'HEAD',
60
- redirect: 'follow',
61
- }),
62
- ]);
63
- node_assert_1.default.strictEqual(response.status, 200);
64
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
65
- // TODO: ensure the headers are the same excluding unique headers
66
- });
67
- for (const api of ['/info', '/block/current']) {
68
- node_test_1.it.skip(`supports native arweave node apis ${api}`, async () => {
69
- const [nativeFetch, response] = await Promise.all([
70
- fetch(`https://${gatewayUrl}${api}`, {
71
- redirect: 'follow',
72
- }),
73
- wayfinder.request(`ar://${api}`, {
74
- redirect: 'follow',
75
- }),
76
- ]);
77
- node_assert_1.default.strictEqual(response.status, 200);
78
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
79
- // TODO: ensure the headers are the same excluding unique headers
80
- });
81
- }
82
- for (const api of ['/ar-io/info']) {
83
- (0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
84
- const [nativeFetch, response] = await Promise.all([
85
- fetch(`https://${gatewayUrl}${api}`),
86
- wayfinder.request(`ar:///${api}`),
87
- ]);
88
- node_assert_1.default.strictEqual(response.status, 200);
89
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
90
- // TODO: ensure the headers are the same excluding unique headers
91
- });
92
- }
93
- (0, node_test_1.it)('supports a post request to graphql', async () => {
94
- const response = await wayfinder.request('ar:///graphql', {
95
- method: 'POST',
96
- redirect: 'follow',
97
- headers: {
98
- 'Content-Type': 'application/json',
99
- },
100
- body: JSON.stringify({
101
- query: `
102
- query {
103
- transactions(
104
- ids: ["xf958qhCNGfDme1FtoiD6DtMfDENDbtxZpjOM_1tsMM"]
105
- ) {
106
- edges {
107
- cursor
108
- node {
109
- id
110
- tags {
111
- name
112
- value
113
- }
114
- block {
115
- height
116
- timestamp
117
- }
118
- }
119
- }
120
- pageInfo {
121
- hasNextPage
122
- }
123
- }
124
- }
125
- `,
126
- }),
127
- });
128
- node_assert_1.default.strictEqual(response.status, 200);
129
- });
130
- (0, node_test_1.it)('returns the error from the target gateway if the route is not found', async () => {
131
- const [nativeFetch, response] = await Promise.all([
132
- fetch(`https://${gatewayUrl}/ar-io/not-found`, {
133
- redirect: 'follow',
134
- }),
135
- wayfinder.request('ar:///ar-io/not-found', {
136
- redirect: 'follow',
137
- }),
138
- ]);
139
- node_assert_1.default.strictEqual(response.status, nativeFetch.status);
140
- node_assert_1.default.strictEqual(response.statusText, nativeFetch.statusText);
141
- });
142
- });
143
- (0, node_test_1.describe)('axios', () => {
144
- let wayfinder;
145
- (0, node_test_1.before)(() => {
146
- wayfinder = new wayfinder_js_1.Wayfinder({
147
- httpClient: axios_1.default,
148
- routingStrategy: new random_js_1.RandomRoutingStrategy(),
149
- gatewaysProvider: stubbedGatewaysProvider,
150
- });
151
- });
152
- (0, node_test_1.it)('should fetch the data using axios default function against the target gateway', async () => {
153
- const [nativeAxios, response] = await Promise.all([
154
- (0, axios_1.default)(`https://ao.${gatewayUrl}`),
155
- wayfinder.request('ar://ao', {
156
- maxRedirects: 5,
157
- }),
158
- ]);
159
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
160
- // assert the arns headers are the same
161
- const arnsHeaders = Object.entries(response.headers)
162
- .sort()
163
- .filter(([key]) => key.startsWith('x-arns-'));
164
- const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
165
- node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
166
- });
167
- (0, node_test_1.it)('should fetch the data using the axios.get method against the target gateway', async () => {
168
- const [nativeAxios, response] = await Promise.all([
169
- axios_1.default.get(`https://ao.${gatewayUrl}`),
170
- wayfinder.request.get('ar://ao'),
171
- ]);
172
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
173
- // assert the arns headers are the same
174
- const arnsHeaders = Object.entries(response.headers)
175
- .sort()
176
- .filter(([key]) => key.startsWith('x-arns-'));
177
- const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
178
- node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
179
- });
180
- (0, node_test_1.it)('should route a non-ar:// url as a normal axios request', async () => {
181
- const [nativeAxios, response] = await Promise.all([
182
- (0, axios_1.default)(`https://${gatewayUrl}/`),
183
- wayfinder.request(`https://${gatewayUrl}/`),
184
- ]);
185
- node_assert_1.default.strictEqual(response.status, 200);
186
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
187
- // TODO: ensure the headers are the same excluding unique headers
188
- });
189
- for (const api of ['/info', '/block/current']) {
190
- node_test_1.it.skip(`supports native arweave node apis ${api}`, async () => {
191
- const [nativeAxios, response] = await Promise.all([
192
- (0, axios_1.default)(`https://${gatewayUrl}${api}`),
193
- wayfinder.request(`ar://${api}`),
194
- ]);
195
- node_assert_1.default.strictEqual(response.status, 200);
196
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
197
- // TODO: ensure the headers are the same excluding unique headers
198
- });
199
- }
200
- for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
201
- (0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
202
- const [nativeAxios, response] = await Promise.all([
203
- (0, axios_1.default)(`https://${gatewayUrl}${api}`),
204
- wayfinder.request(`ar://${api}`),
205
- ]);
206
- node_assert_1.default.strictEqual(response.status, 200);
207
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
208
- // TODO: ensure the headers are the same excluding unique headers
209
- });
210
- }
211
- (0, node_test_1.it)('should return the error from the target gateway if the route is not found', async () => {
212
- const axiosInstance = axios_1.default.create({
213
- validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
214
- });
215
- const wayfinder = new wayfinder_js_1.Wayfinder({
216
- httpClient: axiosInstance,
217
- routingStrategy: new random_js_1.RandomRoutingStrategy(),
218
- });
219
- const [nativeAxios, response] = await Promise.all([
220
- axiosInstance(`https://${gatewayUrl}/ar-io/not-found`),
221
- wayfinder.request('ar:///ar-io/not-found'),
222
- ]);
223
- node_assert_1.default.strictEqual(response.status, nativeAxios.status);
224
- });
225
- });
226
- (0, node_test_1.describe)('got', () => {
227
- let wayfinder;
228
- (0, node_test_1.before)(() => {
229
- wayfinder = new wayfinder_js_1.Wayfinder({
230
- httpClient: got_1.default,
231
- routingStrategy: new random_js_1.RandomRoutingStrategy(),
232
- gatewaysProvider: stubbedGatewaysProvider,
233
- });
234
- });
235
- (0, node_test_1.it)('should fetch the data using the got default function against the target gateway', async () => {
236
- const [nativeGot, response] = await Promise.all([
237
- (0, got_1.default)(`https://ao.${gatewayUrl}`),
238
- wayfinder.request('ar://ao'),
239
- ]);
240
- node_assert_1.default.strictEqual(response.statusCode, nativeGot.statusCode);
241
- node_assert_1.default.deepStrictEqual(response.body, nativeGot.body);
242
- });
243
- (0, node_test_1.it)('should stream the data using got.stream against the selected target gateway', async () => {
244
- const nativeBuffer = await (0, consumers_1.buffer)(await got_1.default.stream(`https://ao.${gatewayUrl}`, {
245
- decompress: false,
246
- followRedirect: true,
247
- }));
248
- const wayfinderBuffer = await (0, consumers_1.buffer)(await wayfinder.request.stream('ar://ao', {
249
- decompress: false,
250
- followRedirect: true,
251
- }));
252
- node_assert_1.default.deepStrictEqual(wayfinderBuffer, nativeBuffer);
253
- });
254
- });
255
- });
256
- (0, node_test_1.describe)('events', () => {
257
- (0, node_test_1.it)('should emit events on the wayfinder event emitter', async () => {
258
- const wayfinder = new wayfinder_js_1.Wayfinder({
259
- httpClient: fetch,
260
- routingStrategy: new static_js_1.StaticRoutingStrategy({
261
- gateway: `http://${gatewayUrl}`,
262
- }),
263
- verificationStrategy: {
264
- // @ts-expect-error
265
- verifyData: async (params) => {
266
- return;
267
- },
268
- },
269
- });
270
- const events = [];
271
- wayfinder.emitter.on('verification-failed', (event) => {
272
- events.push({ type: 'verification-failed', ...event });
273
- });
274
- wayfinder.emitter.on('verification-progress', (event) => {
275
- events.push({ type: 'verification-progress', ...event });
276
- });
277
- wayfinder.emitter.on('verification-succeeded', (event) => {
278
- events.push({ type: 'verification-succeeded', ...event });
279
- });
280
- // request data and assert the event is emitted
281
- const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
282
- redirect: 'follow',
283
- });
284
- // read the full response body to ensure the stream is fully consumed
285
- await response.text();
286
- node_assert_1.default.strictEqual(response.status, 200);
287
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-succeeded'), 'Should emit at least one verification-succeeded');
288
- });
289
- (0, node_test_1.it)('should execute callbacks provided to the wayfinder constructor', async () => {
290
- let verificationFailed = false;
291
- let verificationProgress = false;
292
- let verificationPassed = false;
293
- const wayfinder = new wayfinder_js_1.Wayfinder({
294
- httpClient: fetch,
295
- routingStrategy: new static_js_1.StaticRoutingStrategy({
296
- gateway: `http://${gatewayUrl}`,
297
- }),
298
- events: {
299
- onVerificationFailed: () => {
300
- verificationFailed = true;
301
- },
302
- onVerificationProgress: () => {
303
- verificationProgress = true;
304
- },
305
- onVerificationPassed: () => {
306
- verificationPassed = true;
307
- },
308
- },
309
- strict: true,
310
- });
311
- const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
312
- redirect: 'follow',
313
- });
314
- // read the full response body to ensure the stream is fully consumed
315
- await response.text();
316
- node_assert_1.default.strictEqual(response.status, 200);
317
- node_assert_1.default.ok(verificationFailed === false, 'Should not emit verification-failed');
318
- node_assert_1.default.ok(verificationProgress, 'Should emit verification-progress');
319
- node_assert_1.default.ok(verificationPassed, 'Should emit verification-succeeded');
320
- });
321
- });
322
- (0, node_test_1.describe)('tapAndVerifyRequest', () => {
323
- (0, node_test_1.describe)('Readable', () => {
324
- (0, node_test_1.describe)('strict mode enabled', () => {
325
- (0, node_test_1.it)('should duplicate the stream, verify the first and return the second if verification passes', async () => {
326
- // create a simple readable
327
- const chunks = [
328
- Buffer.from('foo'),
329
- Buffer.from('bar'),
330
- Buffer.from('baz'),
331
- ];
332
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
333
- // a stream that will emit chunks
334
- const originalStream = node_stream_1.Readable.from(chunks);
335
- let seen = Buffer.alloc(0);
336
- const verifyData = async ({ data,
337
- // @ts-expect-error
338
- txId, }) => {
339
- return new Promise((resolve, reject) => {
340
- data.on('data', (chunk) => {
341
- seen = Buffer.concat([seen, chunk]);
342
- });
343
- data.on('end', () => {
344
- // Should have seen exactly the full payload
345
- node_assert_1.default.strictEqual(seen.length, contentLength);
346
- resolve();
347
- });
348
- data.on('error', reject);
349
- });
350
- };
351
- const txId = 'test-tx-1';
352
- const emitter = new node_events_1.EventEmitter();
353
- const events = [];
354
- emitter.on('verification-progress', (e) => {
355
- events.push({ type: 'verification-progress', ...e });
356
- });
357
- emitter.on('verification-succeeded', (e) => events.push({ type: 'verification-succeeded', ...e }));
358
- // tap with verification
359
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
360
- originalStream,
361
- contentLength,
362
- verifyData,
363
- txId,
364
- emitter,
365
- strict: true,
366
- });
367
- // read the stream
368
- const out = [];
369
- for await (const chunk of tapped) {
370
- out.push(chunk);
371
- }
372
- // assert the stream is the same
373
- node_assert_1.default.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
374
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
375
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-succeeded' && e.txId === txId), 'Should emit at least one verification-succeeded');
376
- });
377
- (0, node_test_1.it)('should throw an error on the client stream if verification fails', async () => {
378
- const chunks = [
379
- Buffer.from('foo'),
380
- Buffer.from('bar'),
381
- Buffer.from('baz'),
382
- ];
383
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
384
- // a stream that will emit chunks
385
- const originalStream = node_stream_1.Readable.from(chunks);
386
- const verifyData = async ({
387
- // @ts-expect-error
388
- data, txId, }) => {
389
- throw new Error('Verification failed for txId: ' + txId);
390
- };
391
- const txId = 'test-tx-1';
392
- const emitter = new node_events_1.EventEmitter();
393
- const events = [];
394
- emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
395
- emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
396
- // tap with verification (using strict mode)
397
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
398
- originalStream,
399
- contentLength,
400
- verifyData,
401
- txId,
402
- emitter,
403
- strict: true,
404
- });
405
- // read the stream
406
- try {
407
- const out = [];
408
- for await (const chunk of tapped) {
409
- out.push(chunk);
410
- }
411
- }
412
- catch (error) {
413
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
414
- // stream should be closed
415
- node_assert_1.default.ok(tapped.closed);
416
- }
417
- });
418
- });
419
- });
420
- (0, node_test_1.describe)('ReadableStream', () => {
421
- (0, node_test_1.describe)('strict mode enabled', () => {
422
- (0, node_test_1.it)('should duplicate the ReadableStream, verify the first and return the second if verification passes', async () => {
423
- // create a simple readable
424
- const chunks = [
425
- Buffer.from('foo'),
426
- Buffer.from('bar'),
427
- Buffer.from('baz'),
428
- ];
429
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
430
- // a stream that will emit chunks
431
- const originalStream = ReadableStream.from(chunks);
432
- let seen = Buffer.alloc(0);
433
- const verifyData = async ({ data,
434
- // @ts-expect-error
435
- txId, }) => {
436
- return new Promise(async (resolve, reject) => {
437
- const reader = data.getReader();
438
- while (true) {
439
- try {
440
- const { done, value } = await reader.read();
441
- if (done) {
442
- resolve();
443
- break;
444
- }
445
- seen = Buffer.concat([seen, value]);
446
- }
447
- catch (error) {
448
- reject(error);
449
- }
450
- }
451
- });
452
- };
453
- const txId = 'test-tx-1';
454
- const emitter = new node_events_1.EventEmitter();
455
- const events = [];
456
- emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
457
- emitter.on('verification-succeeded', (e) => events.push({ type: 'verification-succeeded', ...e }));
458
- // tap with verification
459
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
460
- originalStream,
461
- contentLength,
462
- verifyData,
463
- txId,
464
- emitter,
465
- strict: true,
466
- });
467
- // read the stream
468
- const out = [];
469
- for await (const chunk of tapped) {
470
- out.push(chunk);
471
- }
472
- // assert the stream is the same
473
- node_assert_1.default.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
474
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
475
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-succeeded' && e.txId === txId), 'Should emit at least one verification-succeeded');
476
- });
477
- (0, node_test_1.it)('should throw an error on the client stream if verification fails', async () => {
478
- const chunks = [
479
- Buffer.from('foo'),
480
- Buffer.from('bar'),
481
- Buffer.from('baz'),
482
- ];
483
- const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
484
- // a stream that will emit chunks
485
- const originalStream = ReadableStream.from(chunks);
486
- const verifyData = async ({
487
- // @ts-expect-error
488
- data, txId, }) => {
489
- throw new Error('Verification failed for txId: ' + txId);
490
- };
491
- const txId = 'test-tx-1';
492
- const emitter = new node_events_1.EventEmitter();
493
- const events = [];
494
- emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
495
- emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
496
- // tap with verification (using strict mode)
497
- const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
498
- originalStream,
499
- contentLength,
500
- verifyData,
501
- txId,
502
- emitter,
503
- strict: true,
504
- });
505
- // read the stream
506
- try {
507
- const out = [];
508
- for await (const chunk of tapped) {
509
- out.push(chunk);
510
- }
511
- }
512
- catch (error) {
513
- node_assert_1.default.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
514
- }
515
- });
516
- });
517
- });
518
- });
519
- });
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // TODO: add an offset provider that returns offsets for data items so we can use them to verify the signatures of a data item within a bundle
@@ -1,44 +0,0 @@
1
- import { ARIO } from '../../io.js';
2
- export class NetworkGatewaysProvider {
3
- ario;
4
- sortBy;
5
- sortOrder;
6
- limit;
7
- filter;
8
- constructor({ ario = ARIO.mainnet(), sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
9
- this.ario = ario;
10
- this.sortBy = sortBy;
11
- this.sortOrder = sortOrder;
12
- this.limit = limit;
13
- this.filter = filter;
14
- }
15
- async getGateways() {
16
- let cursor;
17
- let attempts = 0;
18
- const gateways = [];
19
- do {
20
- try {
21
- const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
22
- limit: 1000,
23
- cursor,
24
- sortBy: this.sortBy,
25
- sortOrder: this.sortOrder,
26
- });
27
- gateways.push(...newGateways);
28
- cursor = nextCursor;
29
- attempts = 0; // reset attempts if we get a new cursor
30
- }
31
- catch (error) {
32
- console.error('Error fetching gateways', {
33
- cursor,
34
- attempts,
35
- error,
36
- });
37
- attempts++;
38
- }
39
- } while (cursor !== undefined && attempts < 3);
40
- // filter out any gateways that are not joined
41
- const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
42
- return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
43
- }
44
- }
@@ -1,31 +0,0 @@
1
- import { Logger } from '../../../web/index.js';
2
- export class SimpleCacheGatewaysProvider {
3
- gatewaysProvider;
4
- ttlSeconds;
5
- lastUpdated;
6
- gatewaysCache;
7
- logger;
8
- constructor({ gatewaysProvider, ttlSeconds = 60 * 60, // 1 hour
9
- logger = Logger.default, }) {
10
- this.gatewaysCache = [];
11
- this.gatewaysProvider = gatewaysProvider;
12
- this.ttlSeconds = ttlSeconds;
13
- this.logger = logger;
14
- }
15
- async getGateways() {
16
- const now = Date.now();
17
- if (this.gatewaysCache.length === 0 ||
18
- now - this.lastUpdated > this.ttlSeconds * 1000) {
19
- try {
20
- // preserve the cache if the fetch fails
21
- const allGateways = await this.gatewaysProvider.getGateways();
22
- this.gatewaysCache = allGateways;
23
- this.lastUpdated = now;
24
- }
25
- catch (error) {
26
- this.logger.error('Error fetching gateways', error);
27
- }
28
- }
29
- return this.gatewaysCache;
30
- }
31
- }
@@ -1,9 +0,0 @@
1
- export class StaticGatewaysProvider {
2
- gateways;
3
- constructor({ gateways }) {
4
- this.gateways = gateways.map((g) => new URL(g));
5
- }
6
- async getGateways() {
7
- return this.gateways;
8
- }
9
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- export * from './wayfinder.js';
17
- // routing strategies
18
- export * from './routing/strategies/random.js';
19
- export * from './routing/strategies/static.js';
20
- export * from './routing/strategies/ping.js';
21
- export * from './routing/strategies/round-robin.js';
22
- // gateways providers
23
- export * from './gateways/network.js';
24
- export * from './gateways/simple-cache.js';
25
- export * from './gateways/static.js';
26
- // trusted gateways
27
- export * from './verification/trusted.js';
28
- // hash providers
29
- export * from './verification/strategies/data-root-verifier.js';
30
- export * from './verification/strategies/hash-verifier.js';
31
- // TODO: signature verification