@ar.io/sdk 3.11.0-alpha.7 → 3.11.0-alpha.9
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.
- package/README.md +52 -0
- package/bundles/web.bundle.min.js +119 -116
- package/lib/cjs/cli/cli.js +137 -122
- package/lib/cjs/cli/commands/readCommands.js +6 -0
- package/lib/cjs/common/ant.js +5 -5
- package/lib/cjs/common/io.js +37 -0
- package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
- package/lib/cjs/common/wayfinder/index.js +6 -0
- package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +139 -0
- package/lib/cjs/common/wayfinder/verification/hash-verifier.js +50 -0
- package/lib/cjs/common/wayfinder/wayfinder.js +407 -18
- package/lib/cjs/common/wayfinder/wayfinder.test.js +262 -3
- package/lib/cjs/types/wayfinder.js +1 -0
- package/lib/cjs/utils/hash.js +56 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/cli/cli.js +138 -123
- package/lib/esm/cli/commands/readCommands.js +5 -0
- package/lib/esm/common/ant.js +5 -5
- package/lib/esm/common/io.js +37 -0
- package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
- package/lib/esm/common/wayfinder/index.js +6 -0
- package/lib/esm/common/wayfinder/verification/data-root-verifier.js +130 -0
- package/lib/esm/common/wayfinder/verification/hash-verifier.js +46 -0
- package/lib/esm/common/wayfinder/wayfinder.js +401 -18
- package/lib/esm/common/wayfinder/wayfinder.test.js +263 -4
- package/lib/esm/types/wayfinder.js +1 -0
- package/lib/esm/utils/hash.js +50 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/cli/commands/readCommands.d.ts +1 -0
- package/lib/types/common/io.d.ts +5 -2
- package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
- package/lib/types/common/wayfinder/index.d.ts +3 -0
- package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +31 -0
- package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +27 -0
- package/lib/types/common/wayfinder/wayfinder.d.ts +148 -10
- package/lib/types/types/io.d.ts +16 -1
- package/lib/types/types/wayfinder.d.ts +43 -0
- package/lib/types/utils/hash.d.ts +4 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
|
@@ -6,18 +6,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const axios_1 = __importDefault(require("axios"));
|
|
7
7
|
const got_1 = __importDefault(require("got"));
|
|
8
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");
|
|
9
11
|
const consumers_1 = require("node:stream/consumers");
|
|
10
12
|
const node_test_1 = require("node:test");
|
|
11
13
|
const logger_js_1 = require("../../common/logger.js");
|
|
12
14
|
const random_js_1 = require("./routers/random.js");
|
|
15
|
+
const static_js_1 = require("./routers/static.js");
|
|
13
16
|
const wayfinder_js_1 = require("./wayfinder.js");
|
|
17
|
+
// TODO: replace with locally running gateway
|
|
14
18
|
const gatewayUrl = 'permagate.io';
|
|
15
19
|
const stubbedGatewaysProvider = {
|
|
16
|
-
getGateways: async () => [new URL(`
|
|
20
|
+
getGateways: async () => [new URL(`http://${gatewayUrl}`)],
|
|
17
21
|
};
|
|
18
|
-
logger_js_1.Logger.default.setLogLevel('
|
|
22
|
+
logger_js_1.Logger.default.setLogLevel('none');
|
|
19
23
|
(0, node_test_1.describe)('Wayfinder', () => {
|
|
20
|
-
|
|
24
|
+
node_test_1.describe.skip('http wrapper', () => {
|
|
21
25
|
(0, node_test_1.describe)('fetch', () => {
|
|
22
26
|
let wayfinder;
|
|
23
27
|
(0, node_test_1.before)(() => {
|
|
@@ -238,4 +242,259 @@ logger_js_1.Logger.default.setLogLevel('debug');
|
|
|
238
242
|
});
|
|
239
243
|
});
|
|
240
244
|
});
|
|
245
|
+
(0, node_test_1.describe)('events', () => {
|
|
246
|
+
(0, node_test_1.it)('should emit events on the wayfinder event emitter', async () => {
|
|
247
|
+
const wayfinder = new wayfinder_js_1.Wayfinder({
|
|
248
|
+
httpClient: fetch,
|
|
249
|
+
router: new static_js_1.StaticGatewayRouter({
|
|
250
|
+
gateway: `http://${gatewayUrl}`,
|
|
251
|
+
}),
|
|
252
|
+
verifier: {
|
|
253
|
+
// @ts-expect-error
|
|
254
|
+
verifyData: async (params) => {
|
|
255
|
+
return;
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
const events = [];
|
|
260
|
+
wayfinder.emitter.on('verification-failed', (event) => {
|
|
261
|
+
events.push({ type: 'verification-failed', ...event });
|
|
262
|
+
});
|
|
263
|
+
wayfinder.emitter.on('verification-progress', (event) => {
|
|
264
|
+
events.push({ type: 'verification-progress', ...event });
|
|
265
|
+
});
|
|
266
|
+
wayfinder.emitter.on('verification-passed', (event) => {
|
|
267
|
+
events.push({ type: 'verification-passed', ...event });
|
|
268
|
+
});
|
|
269
|
+
// request data and assert the event is emitted
|
|
270
|
+
const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
|
|
271
|
+
redirect: 'follow',
|
|
272
|
+
});
|
|
273
|
+
// read the full response body to ensure the stream is fully consumed
|
|
274
|
+
await response.text();
|
|
275
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
276
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-passed'), 'Should emit at least one verification-passed');
|
|
277
|
+
});
|
|
278
|
+
(0, node_test_1.it)('should execute callbacks provided to the wayfinder constructor', async () => {
|
|
279
|
+
let verificationFailed = false;
|
|
280
|
+
let verificationProgress = false;
|
|
281
|
+
let verificationPassed = false;
|
|
282
|
+
const wayfinder = new wayfinder_js_1.Wayfinder({
|
|
283
|
+
httpClient: fetch,
|
|
284
|
+
router: new static_js_1.StaticGatewayRouter({
|
|
285
|
+
gateway: `http://${gatewayUrl}`,
|
|
286
|
+
}),
|
|
287
|
+
events: {
|
|
288
|
+
onVerificationFailed: () => {
|
|
289
|
+
verificationFailed = true;
|
|
290
|
+
},
|
|
291
|
+
onVerificationProgress: () => {
|
|
292
|
+
verificationProgress = true;
|
|
293
|
+
},
|
|
294
|
+
onVerificationPassed: () => {
|
|
295
|
+
verificationPassed = true;
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
const response = await wayfinder.request('ar://c7wkwt6TKgcWJUfgvpJ5q5qi4DIZyJ1_TqhjXgURh0U', {
|
|
300
|
+
redirect: 'follow',
|
|
301
|
+
});
|
|
302
|
+
// read the full response body to ensure the stream is fully consumed
|
|
303
|
+
await response.text();
|
|
304
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
305
|
+
node_assert_1.default.ok(verificationFailed === false, 'Should not emit verification-failed');
|
|
306
|
+
node_assert_1.default.ok(verificationProgress, 'Should emit verification-progress');
|
|
307
|
+
node_assert_1.default.ok(verificationPassed, 'Should emit verification-passed');
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
node_test_1.describe.skip('tapAndVerifyRequest', () => {
|
|
311
|
+
(0, node_test_1.describe)('Readable', () => {
|
|
312
|
+
(0, node_test_1.it)('should duplicate the stream, verify the first and return the second if verification passes', async () => {
|
|
313
|
+
// create a simple readable
|
|
314
|
+
const chunks = [
|
|
315
|
+
Buffer.from('foo'),
|
|
316
|
+
Buffer.from('bar'),
|
|
317
|
+
Buffer.from('baz'),
|
|
318
|
+
];
|
|
319
|
+
const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
|
|
320
|
+
// a stream that will emit chunks
|
|
321
|
+
const originalStream = node_stream_1.Readable.from(chunks);
|
|
322
|
+
let seen = Buffer.alloc(0);
|
|
323
|
+
const verifyData = async ({ data,
|
|
324
|
+
// @ts-expect-error
|
|
325
|
+
txId, }) => {
|
|
326
|
+
return new Promise((resolve, reject) => {
|
|
327
|
+
data.on('data', (chunk) => {
|
|
328
|
+
seen = Buffer.concat([seen, chunk]);
|
|
329
|
+
});
|
|
330
|
+
data.on('end', () => {
|
|
331
|
+
// Should have seen exactly the full payload
|
|
332
|
+
node_assert_1.default.strictEqual(seen.length, contentLength);
|
|
333
|
+
resolve();
|
|
334
|
+
});
|
|
335
|
+
data.on('error', reject);
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
const txId = 'test-tx-1';
|
|
339
|
+
const emitter = new node_events_1.EventEmitter();
|
|
340
|
+
const events = [];
|
|
341
|
+
emitter.on('verification-progress', (e) => {
|
|
342
|
+
console.log('verification-progress', e);
|
|
343
|
+
events.push({ type: 'verification-progress', ...e });
|
|
344
|
+
});
|
|
345
|
+
emitter.on('verification-passed', (e) => events.push({ type: 'verification-passed', ...e }));
|
|
346
|
+
// tap with verification
|
|
347
|
+
const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
|
|
348
|
+
originalStream,
|
|
349
|
+
contentLength,
|
|
350
|
+
verifyData,
|
|
351
|
+
txId,
|
|
352
|
+
emitter,
|
|
353
|
+
});
|
|
354
|
+
// read the stream
|
|
355
|
+
const out = [];
|
|
356
|
+
for await (const chunk of tapped) {
|
|
357
|
+
out.push(chunk);
|
|
358
|
+
}
|
|
359
|
+
// assert the stream is the same
|
|
360
|
+
node_assert_1.default.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
|
|
361
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
|
|
362
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-passed' && e.txId === txId), 'Should emit at least one verification-passed');
|
|
363
|
+
});
|
|
364
|
+
(0, node_test_1.it)('should throw an error on the client stream if verification fails', async () => {
|
|
365
|
+
const chunks = [
|
|
366
|
+
Buffer.from('foo'),
|
|
367
|
+
Buffer.from('bar'),
|
|
368
|
+
Buffer.from('baz'),
|
|
369
|
+
];
|
|
370
|
+
const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
|
|
371
|
+
// a stream that will emit chunks
|
|
372
|
+
const originalStream = node_stream_1.Readable.from(chunks);
|
|
373
|
+
const verifyData = async ({
|
|
374
|
+
// @ts-expect-error
|
|
375
|
+
data, txId, }) => {
|
|
376
|
+
throw new Error('Verification failed for txId: ' + txId);
|
|
377
|
+
};
|
|
378
|
+
const txId = 'test-tx-1';
|
|
379
|
+
const emitter = new node_events_1.EventEmitter();
|
|
380
|
+
const events = [];
|
|
381
|
+
emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
|
|
382
|
+
emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
|
|
383
|
+
// tap with verification
|
|
384
|
+
const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
|
|
385
|
+
originalStream,
|
|
386
|
+
contentLength,
|
|
387
|
+
verifyData,
|
|
388
|
+
txId,
|
|
389
|
+
emitter,
|
|
390
|
+
});
|
|
391
|
+
// read the stream
|
|
392
|
+
try {
|
|
393
|
+
const out = [];
|
|
394
|
+
for await (const chunk of tapped) {
|
|
395
|
+
out.push(chunk);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
catch (error) {
|
|
399
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
|
|
400
|
+
// stream should be closed
|
|
401
|
+
node_assert_1.default.ok(tapped.closed);
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
(0, node_test_1.describe)('ReadableStream', () => {
|
|
406
|
+
(0, node_test_1.it)('should duplicate the ReadableStream, verify the first and return the second if verification passes', async () => {
|
|
407
|
+
// create a simple readable
|
|
408
|
+
const chunks = [
|
|
409
|
+
Buffer.from('foo'),
|
|
410
|
+
Buffer.from('bar'),
|
|
411
|
+
Buffer.from('baz'),
|
|
412
|
+
];
|
|
413
|
+
const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
|
|
414
|
+
// a stream that will emit chunks
|
|
415
|
+
const originalStream = ReadableStream.from(chunks);
|
|
416
|
+
let seen = Buffer.alloc(0);
|
|
417
|
+
const verifyData = async ({ data,
|
|
418
|
+
// @ts-expect-error
|
|
419
|
+
txId, }) => {
|
|
420
|
+
return new Promise(async (resolve, reject) => {
|
|
421
|
+
const reader = data.getReader();
|
|
422
|
+
while (true) {
|
|
423
|
+
try {
|
|
424
|
+
const { done, value } = await reader.read();
|
|
425
|
+
if (done) {
|
|
426
|
+
resolve();
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
seen = Buffer.concat([seen, value]);
|
|
430
|
+
}
|
|
431
|
+
catch (error) {
|
|
432
|
+
reject(error);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
};
|
|
437
|
+
const txId = 'test-tx-1';
|
|
438
|
+
const emitter = new node_events_1.EventEmitter();
|
|
439
|
+
const events = [];
|
|
440
|
+
emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
|
|
441
|
+
emitter.on('verification-passed', (e) => events.push({ type: 'verification-passed', ...e }));
|
|
442
|
+
// tap with verification
|
|
443
|
+
const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
|
|
444
|
+
originalStream,
|
|
445
|
+
contentLength,
|
|
446
|
+
verifyData,
|
|
447
|
+
txId,
|
|
448
|
+
emitter,
|
|
449
|
+
});
|
|
450
|
+
// read the stream
|
|
451
|
+
const out = [];
|
|
452
|
+
for await (const chunk of tapped) {
|
|
453
|
+
out.push(chunk);
|
|
454
|
+
}
|
|
455
|
+
// assert the stream is the same
|
|
456
|
+
node_assert_1.default.strictEqual(Buffer.concat(out).toString(), Buffer.concat(chunks).toString(), 'The tapped stream should emit exactly the original data');
|
|
457
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-progress'), 'Should emit at least one verification-progress');
|
|
458
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-passed' && e.txId === txId), 'Should emit at least one verification-passed');
|
|
459
|
+
});
|
|
460
|
+
(0, node_test_1.it)('should throw an error on the client stream if verification fails', async () => {
|
|
461
|
+
const chunks = [
|
|
462
|
+
Buffer.from('foo'),
|
|
463
|
+
Buffer.from('bar'),
|
|
464
|
+
Buffer.from('baz'),
|
|
465
|
+
];
|
|
466
|
+
const contentLength = chunks.reduce((sum, c) => sum + c.length, 0);
|
|
467
|
+
// a stream that will emit chunks
|
|
468
|
+
const originalStream = ReadableStream.from(chunks);
|
|
469
|
+
const verifyData = async ({
|
|
470
|
+
// @ts-expect-error
|
|
471
|
+
data, txId, }) => {
|
|
472
|
+
throw new Error('Verification failed for txId: ' + txId);
|
|
473
|
+
};
|
|
474
|
+
const txId = 'test-tx-1';
|
|
475
|
+
const emitter = new node_events_1.EventEmitter();
|
|
476
|
+
const events = [];
|
|
477
|
+
emitter.on('verification-progress', (e) => events.push({ type: 'verification-progress', ...e }));
|
|
478
|
+
emitter.on('verification-failed', (e) => events.push({ type: 'verification-failed', ...e }));
|
|
479
|
+
// tap with verification
|
|
480
|
+
const tapped = (0, wayfinder_js_1.tapAndVerifyStream)({
|
|
481
|
+
originalStream,
|
|
482
|
+
contentLength,
|
|
483
|
+
verifyData,
|
|
484
|
+
txId,
|
|
485
|
+
emitter,
|
|
486
|
+
});
|
|
487
|
+
// read the stream
|
|
488
|
+
try {
|
|
489
|
+
const out = [];
|
|
490
|
+
for await (const chunk of tapped) {
|
|
491
|
+
out.push(chunk);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
catch (error) {
|
|
495
|
+
node_assert_1.default.ok(events.find((e) => e.type === 'verification-failed' && e.txId === txId), 'Should emit at least one verification-failed');
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
});
|
|
241
500
|
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hashBufferToB64Url = exports.hashReadableStreamToB64Url = exports.hashReadableToB64Url = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const crypto_1 = require("crypto");
|
|
20
|
+
const base64_js_1 = require("./base64.js");
|
|
21
|
+
const hashReadableToB64Url = (stream, algorithm = 'sha256') => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
const hash = (0, crypto_1.createHash)(algorithm);
|
|
24
|
+
stream.on('data', (chunk) => hash.update(chunk));
|
|
25
|
+
stream.on('end', () => resolve((0, base64_js_1.toB64Url)(hash.digest())));
|
|
26
|
+
stream.on('error', (err) => reject(err));
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
exports.hashReadableToB64Url = hashReadableToB64Url;
|
|
30
|
+
const hashReadableStreamToB64Url = (stream, algorithm = 'sha256') => {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const hash = (0, crypto_1.createHash)(algorithm);
|
|
33
|
+
const reader = stream.getReader();
|
|
34
|
+
const read = async () => {
|
|
35
|
+
try {
|
|
36
|
+
const { done, value } = await reader.read();
|
|
37
|
+
if (done) {
|
|
38
|
+
resolve((0, base64_js_1.toB64Url)(hash.digest()));
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
hash.update(value);
|
|
42
|
+
read();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
reject(err);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
read().catch(reject);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
exports.hashReadableStreamToB64Url = hashReadableStreamToB64Url;
|
|
53
|
+
const hashBufferToB64Url = (buffer, algorithm = 'sha256') => {
|
|
54
|
+
return (0, base64_js_1.toB64Url)((0, crypto_1.createHash)(algorithm).update(buffer).digest());
|
|
55
|
+
};
|
|
56
|
+
exports.hashBufferToB64Url = hashBufferToB64Url;
|
package/lib/cjs/version.js
CHANGED