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

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 (81) hide show
  1. package/README.md +2 -0
  2. package/bundles/web.bundle.min.js +258 -170
  3. package/lib/cjs/cli/utils.js +2 -0
  4. package/lib/cjs/common/ant-versions.js +3 -2
  5. package/lib/cjs/common/contracts/ao-process.js +3 -1
  6. package/lib/cjs/common/index.js +0 -2
  7. package/lib/cjs/common/io.js +17 -9
  8. package/lib/cjs/common/turbo.js +1 -1
  9. package/lib/cjs/types/index.js +0 -1
  10. package/lib/cjs/utils/ao.js +8 -3
  11. package/lib/cjs/utils/processes.js +3 -1
  12. package/lib/cjs/version.js +1 -1
  13. package/lib/esm/cli/utils.js +2 -0
  14. package/lib/esm/common/ant-versions.js +3 -2
  15. package/lib/esm/common/contracts/ao-process.js +3 -1
  16. package/lib/esm/common/index.js +0 -2
  17. package/lib/esm/common/io.js +17 -9
  18. package/lib/esm/common/turbo.js +1 -1
  19. package/lib/esm/types/index.js +0 -1
  20. package/lib/esm/utils/ao.js +8 -3
  21. package/lib/esm/utils/processes.js +3 -1
  22. package/lib/esm/version.js +1 -1
  23. package/lib/types/common/ant-versions.d.ts +3 -5
  24. package/lib/types/common/index.d.ts +0 -1
  25. package/lib/types/types/index.d.ts +0 -1
  26. package/lib/types/version.d.ts +1 -1
  27. package/package.json +2 -2
  28. package/lib/cjs/common/wayfinder/gateways/network.js +0 -48
  29. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +0 -35
  30. package/lib/cjs/common/wayfinder/gateways/static.js +0 -13
  31. package/lib/cjs/common/wayfinder/index.js +0 -47
  32. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +0 -72
  33. package/lib/cjs/common/wayfinder/routing/strategies/ping.test.js +0 -156
  34. package/lib/cjs/common/wayfinder/routing/strategies/random.js +0 -13
  35. package/lib/cjs/common/wayfinder/routing/strategies/random.test.js +0 -68
  36. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +0 -42
  37. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.test.js +0 -78
  38. package/lib/cjs/common/wayfinder/routing/strategies/static.js +0 -29
  39. package/lib/cjs/common/wayfinder/routing/strategies/static.test.js +0 -40
  40. package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +0 -139
  41. package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +0 -50
  42. package/lib/cjs/common/wayfinder/verification/trusted.js +0 -106
  43. package/lib/cjs/common/wayfinder/wayfinder.js +0 -736
  44. package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -519
  45. package/lib/cjs/types/wayfinder.js +0 -3
  46. package/lib/esm/common/wayfinder/gateways/network.js +0 -44
  47. package/lib/esm/common/wayfinder/gateways/simple-cache.js +0 -31
  48. package/lib/esm/common/wayfinder/gateways/static.js +0 -9
  49. package/lib/esm/common/wayfinder/index.js +0 -31
  50. package/lib/esm/common/wayfinder/routing/strategies/ping.js +0 -68
  51. package/lib/esm/common/wayfinder/routing/strategies/ping.test.js +0 -151
  52. package/lib/esm/common/wayfinder/routing/strategies/random.js +0 -9
  53. package/lib/esm/common/wayfinder/routing/strategies/random.test.js +0 -63
  54. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +0 -38
  55. package/lib/esm/common/wayfinder/routing/strategies/round-robin.test.js +0 -73
  56. package/lib/esm/common/wayfinder/routing/strategies/static.js +0 -25
  57. package/lib/esm/common/wayfinder/routing/strategies/static.test.js +0 -35
  58. package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +0 -130
  59. package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +0 -46
  60. package/lib/esm/common/wayfinder/verification/trusted.js +0 -102
  61. package/lib/esm/common/wayfinder/wayfinder.js +0 -724
  62. package/lib/esm/common/wayfinder/wayfinder.test.js +0 -514
  63. package/lib/esm/types/wayfinder.js +0 -2
  64. package/lib/types/common/wayfinder/gateways/network.d.ts +0 -33
  65. package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +0 -31
  66. package/lib/types/common/wayfinder/gateways/static.d.ts +0 -23
  67. package/lib/types/common/wayfinder/index.d.ts +0 -26
  68. package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +0 -27
  69. package/lib/types/common/wayfinder/routing/strategies/ping.test.d.ts +0 -1
  70. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +0 -21
  71. package/lib/types/common/wayfinder/routing/strategies/random.test.d.ts +0 -1
  72. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +0 -29
  73. package/lib/types/common/wayfinder/routing/strategies/round-robin.test.d.ts +0 -1
  74. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +0 -29
  75. package/lib/types/common/wayfinder/routing/strategies/static.test.d.ts +0 -1
  76. package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +0 -31
  77. package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +0 -27
  78. package/lib/types/common/wayfinder/verification/trusted.d.ts +0 -51
  79. package/lib/types/common/wayfinder/wayfinder.d.ts +0 -299
  80. package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
  81. package/lib/types/types/wayfinder.d.ts +0 -66
@@ -1,68 +0,0 @@
1
- export class FastestPingRoutingStrategy {
2
- timeoutMs;
3
- probePath;
4
- constructor({ timeoutMs = 500, probePath = '/ar-io/info', // TODO: limit to allowed /ar-io and arweave node endpoints
5
- } = {}) {
6
- this.timeoutMs = timeoutMs;
7
- this.probePath = probePath;
8
- }
9
- async selectGateway({ gateways }) {
10
- if (gateways.length === 0) {
11
- throw new Error('No gateways provided');
12
- }
13
- try {
14
- const results = await Promise.allSettled(gateways.map(async (gateway) => {
15
- try {
16
- const startTime = Date.now();
17
- const response = await fetch(`${gateway.toString().replace(/\/$/, '')}${this.probePath}`, {
18
- method: 'HEAD',
19
- signal: AbortSignal.timeout(this.timeoutMs),
20
- });
21
- const endTime = Date.now();
22
- const durationMs = endTime - startTime;
23
- return {
24
- gateway,
25
- status: response.status,
26
- durationMs,
27
- error: null,
28
- };
29
- }
30
- catch (error) {
31
- // Handle network errors
32
- return {
33
- gateway,
34
- status: 'rejected',
35
- durationMs: Infinity,
36
- error,
37
- };
38
- }
39
- }));
40
- // Process results
41
- const processedResults = results.map((result, index) => {
42
- if (result.status === 'fulfilled') {
43
- return result.value;
44
- }
45
- else {
46
- return {
47
- gateway: gateways[index],
48
- status: 'rejected',
49
- durationMs: Infinity,
50
- error: result.reason,
51
- };
52
- }
53
- });
54
- // Filter healthy gateways and sort by latency
55
- const healthyGateways = processedResults
56
- .filter((result) => result.status === 200)
57
- .sort((a, b) => a.durationMs - b.durationMs);
58
- if (healthyGateways.length > 0) {
59
- return healthyGateways[0].gateway;
60
- }
61
- throw new Error('No healthy gateways found');
62
- }
63
- catch (error) {
64
- throw new Error('Failed to ping gateways: ' +
65
- (error instanceof Error ? error.message : String(error)));
66
- }
67
- }
68
- }
@@ -1,151 +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
- import assert from 'node:assert/strict';
17
- import { afterEach, beforeEach, describe, it } from 'node:test';
18
- import { FastestPingRoutingStrategy } from './ping.js';
19
- describe('FastestPingRoutingStrategy', () => {
20
- // Original fetch function
21
- const originalFetch = global.fetch;
22
- // Mock response options for each gateway
23
- const mockResponses = new Map();
24
- beforeEach(() => {
25
- // reset mock responses
26
- mockResponses.clear();
27
- // mock fetch to simulate network latency and response status
28
- global.fetch = async (url) => {
29
- const urlString = url.toString();
30
- // find the matching gateway
31
- let matchingGateway = '';
32
- for (const gateway of mockResponses.keys()) {
33
- if (urlString.startsWith(gateway)) {
34
- matchingGateway = gateway;
35
- break;
36
- }
37
- }
38
- if (!matchingGateway) {
39
- return Promise.reject(new Error(`No mock response for URL: ${urlString}`));
40
- }
41
- const { status, delayMs } = mockResponses.get(matchingGateway);
42
- // simulate network delay
43
- await new Promise((resolve) => setTimeout(resolve, delayMs));
44
- return new Response(null, { status });
45
- };
46
- // mock AbortSignal.timeout
47
- if (!AbortSignal.timeout) {
48
- AbortSignal.timeout = (ms) => {
49
- const controller = new AbortController();
50
- setTimeout(() => controller.abort(), ms);
51
- return controller.signal;
52
- };
53
- }
54
- });
55
- // restore original fetch after tests
56
- afterEach(() => {
57
- global.fetch = originalFetch;
58
- });
59
- it('selects the gateway with the lowest latency', async () => {
60
- const gateways = [
61
- new URL('https://slow.com'),
62
- new URL('https://fast.com'),
63
- new URL('https://medium.com'),
64
- ];
65
- // configure mock responses
66
- mockResponses.set('https://slow.com', { status: 200, delayMs: 300 });
67
- mockResponses.set('https://fast.com', { status: 200, delayMs: 50 });
68
- mockResponses.set('https://medium.com', { status: 200, delayMs: 150 });
69
- const strategy = new FastestPingRoutingStrategy({ timeoutMs: 500 });
70
- // select the gateway with the lowest latency
71
- const selectedGateway = await strategy.selectGateway({
72
- gateways,
73
- });
74
- assert.equal(selectedGateway.toString(), 'https://fast.com/', 'Should select the gateway with the lowest latency');
75
- });
76
- it('ignores gateways that return non-200 status codes', async () => {
77
- const gateways = [
78
- new URL('https://error.com'),
79
- new URL('https://success.com'),
80
- new URL('https://another-error.com'),
81
- ];
82
- // configure mock responses
83
- mockResponses.set('https://error.com', { status: 404, delayMs: 50 });
84
- mockResponses.set('https://success.com', { status: 200, delayMs: 100 });
85
- mockResponses.set('https://another-error.com', {
86
- status: 500,
87
- delayMs: 75,
88
- });
89
- const strategy = new FastestPingRoutingStrategy({ timeoutMs: 500 });
90
- // select the gateway with the lowest latency
91
- const selectedGateway = await strategy.selectGateway({
92
- gateways,
93
- });
94
- assert.equal(selectedGateway.toString(), 'https://success.com/', 'Should select the gateway that returns a 200 status code');
95
- });
96
- it('throws an error when all gateways fail', async () => {
97
- const gateways = [
98
- new URL('https://error1.com'),
99
- new URL('https://error2.com'),
100
- ];
101
- // configure mock responses
102
- mockResponses.set('https://error1.com', { status: 404, delayMs: 50 });
103
- mockResponses.set('https://error2.com', { status: 500, delayMs: 75 });
104
- const strategy = new FastestPingRoutingStrategy({ timeoutMs: 500 });
105
- // select the gateway with the lowest latency
106
- await assert.rejects(async () => await strategy.selectGateway({ gateways }), /No healthy gateways found/, 'Should throw an error when all gateways fail');
107
- });
108
- it('handles network errors gracefully', async () => {
109
- const gateways = [
110
- new URL('https://network-error.com'),
111
- new URL('https://success.com'),
112
- ];
113
- // configure mock responses
114
- mockResponses.set('https://success.com', { status: 200, delayMs: 100 });
115
- // override fetch for the network error case
116
- const originalFetchMock = global.fetch;
117
- global.fetch = async (url) => {
118
- if (url.toString().includes('network-error')) {
119
- throw new Error('Network error');
120
- }
121
- return originalFetchMock(url);
122
- };
123
- const strategy = new FastestPingRoutingStrategy({ timeoutMs: 500 });
124
- // select the gateway with the lowest latency
125
- const selectedGateway = await strategy.selectGateway({
126
- gateways,
127
- });
128
- assert.equal(selectedGateway.toString(), 'https://success.com/', 'Should handle network errors and select the working gateway');
129
- });
130
- it('respects the timeout parameter', async () => {
131
- const gateways = [
132
- new URL('https://timeout.com'),
133
- new URL('https://fast.com'),
134
- ];
135
- // configure mock responses
136
- mockResponses.set('https://timeout.com', { status: 200, delayMs: 300 });
137
- mockResponses.set('https://fast.com', { status: 200, delayMs: 50 });
138
- // set a short timeout
139
- const strategy = new FastestPingRoutingStrategy({ timeoutMs: 100 });
140
- const selectedGateway = await strategy.selectGateway({
141
- gateways,
142
- });
143
- assert.equal(selectedGateway.toString(), 'https://fast.com/', 'Should respect the timeout and select only gateways that respond within the timeout');
144
- });
145
- it('throws an error when no gateways are provided', async () => {
146
- const gateways = [];
147
- const strategy = new FastestPingRoutingStrategy();
148
- // select the gateway with the lowest latency
149
- await assert.rejects(async () => await strategy.selectGateway({ gateways }), /No gateways provided/, 'Should throw an error when no gateways are provided');
150
- });
151
- });
@@ -1,9 +0,0 @@
1
- import { randomInt } from '../../../../utils/random.js';
2
- export class RandomRoutingStrategy {
3
- async selectGateway({ gateways }) {
4
- if (gateways.length === 0) {
5
- throw new Error('No gateways available');
6
- }
7
- return gateways[randomInt(0, gateways.length)];
8
- }
9
- }
@@ -1,63 +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
- import assert from 'node:assert/strict';
17
- import { describe, it } from 'node:test';
18
- import { RandomRoutingStrategy } from './random.js';
19
- describe('RandomRoutingStrategy', () => {
20
- it('selects a gateway from the provided list', async () => {
21
- // Arrange
22
- const gateways = [
23
- new URL('https://example1.com'),
24
- new URL('https://example2.com'),
25
- new URL('https://example3.com'),
26
- ];
27
- const strategy = new RandomRoutingStrategy();
28
- const selectedGateway = await strategy.selectGateway({ gateways });
29
- assert.ok(gateways.includes(selectedGateway), 'The selected gateway should be one of the gateways provided');
30
- });
31
- it('throws error when no gateways are provided', async () => {
32
- const gateways = [];
33
- const strategy = new RandomRoutingStrategy();
34
- await assert.rejects(async () => await strategy.selectGateway({ gateways }), /No gateways available/, 'Should throw an error when no gateways are provided');
35
- });
36
- it('should distribute gateway selection somewhat randomly', async () => {
37
- const gateways = [
38
- new URL('https://example1.com'),
39
- new URL('https://example2.com'),
40
- new URL('https://example3.com'),
41
- new URL('https://example4.com'),
42
- new URL('https://example5.com'),
43
- ];
44
- const strategy = new RandomRoutingStrategy();
45
- const selections = new Map();
46
- // select gateways multiple times
47
- const iterations = 100;
48
- for (let i = 0; i < iterations; i++) {
49
- const gateway = await strategy.selectGateway({ gateways });
50
- const key = gateway.toString();
51
- selections.set(key, (selections.get(key) || 0) + 1);
52
- }
53
- // each gateway should be selected at least once
54
- for (const gateway of gateways) {
55
- const key = gateway.toString();
56
- assert.ok(selections.has(key), `Gateway ${key} should be selected at least once`);
57
- }
58
- // no gateway should be selected more than 50% of the time
59
- for (const [key, count] of selections.entries()) {
60
- assert.ok(count < iterations * 0.5, `Gateway ${key} was selected ${count} times, which is more than 50% of iterations`);
61
- }
62
- });
63
- });
@@ -1,38 +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
- import { Logger } from '../../../../common/logger.js';
17
- export class RoundRobinRoutingStrategy {
18
- gateways;
19
- currentIndex;
20
- logger;
21
- constructor({ gateways, logger = Logger.default, }) {
22
- this.gateways = gateways;
23
- this.currentIndex = 0;
24
- this.logger = logger;
25
- }
26
- // provided gateways are ignored
27
- async selectGateway({ gateways = [], } = {}) {
28
- if (gateways.length > 0) {
29
- this.logger.warn('RoundRobinRoutingStrategy does not accept provided gateways. Ignoring provided gateways...', {
30
- providedGateways: gateways.length,
31
- internalGateways: this.gateways,
32
- });
33
- }
34
- const gateway = this.gateways[this.currentIndex];
35
- this.currentIndex = (this.currentIndex + 1) % this.gateways.length;
36
- return gateway;
37
- }
38
- }
@@ -1,73 +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
- import assert from 'node:assert/strict';
17
- import { describe, it } from 'node:test';
18
- import { Logger } from '../../../../common/logger.js';
19
- import { RoundRobinRoutingStrategy } from './round-robin.js';
20
- Logger.default.setLogLevel('none');
21
- describe('RoundRobinRoutingStrategy', () => {
22
- it('selects gateways in order and cycles back to the beginning', async () => {
23
- const gateways = [
24
- new URL('https://example1.com'),
25
- new URL('https://example2.com'),
26
- new URL('https://example3.com'),
27
- ];
28
- const strategy = new RoundRobinRoutingStrategy({ gateways });
29
- const selection1 = await strategy.selectGateway();
30
- assert.equal(selection1.toString(), gateways[0].toString(), 'Should select the first gateway first');
31
- const selection2 = await strategy.selectGateway();
32
- assert.equal(selection2.toString(), gateways[1].toString(), 'Should select the second gateway second');
33
- const selection3 = await strategy.selectGateway();
34
- assert.equal(selection3.toString(), gateways[2].toString(), 'Should select the third gateway third');
35
- // should cycle back to the first gateway
36
- const selection4 = await strategy.selectGateway();
37
- assert.equal(selection4.toString(), gateways[0].toString(), 'Should cycle back to the first gateway');
38
- });
39
- it('uses the internal list even when a different list is provided', async () => {
40
- const initialGateways = [
41
- new URL('https://example1.com'),
42
- new URL('https://example2.com'),
43
- ];
44
- const newGateways = [
45
- new URL('https://example3.com'),
46
- new URL('https://example4.com'),
47
- ];
48
- const strategy = new RoundRobinRoutingStrategy({
49
- gateways: initialGateways,
50
- });
51
- const selection1 = await strategy.selectGateway({
52
- gateways: newGateways,
53
- });
54
- assert.equal(selection1.toString(), initialGateways[0].toString(), 'Should use the internal list even when a different list is provided');
55
- const selection2 = await strategy.selectGateway({
56
- // @ts-ignore
57
- gateways: newGateways,
58
- });
59
- assert.equal(selection2.toString(), initialGateways[1].toString(), 'Should use the internal list even when a different list is provided');
60
- });
61
- it('handles a single gateway by returning it repeatedly', async () => {
62
- const gateways = [new URL('https://example1.com')];
63
- const strategy = new RoundRobinRoutingStrategy({ gateways });
64
- const selection1 = await strategy.selectGateway({
65
- gateways: [new URL('https://example2.com')],
66
- });
67
- assert.equal(selection1.toString(), gateways[0].toString(), 'Should return the single gateway');
68
- const selection2 = await strategy.selectGateway({
69
- gateways: [new URL('https://example2.com')],
70
- });
71
- assert.equal(selection2.toString(), gateways[0].toString(), 'Should return the single gateway again');
72
- });
73
- });
@@ -1,25 +0,0 @@
1
- import { Logger } from '../../../logger.js';
2
- export class StaticRoutingStrategy {
3
- name = 'static';
4
- gateway;
5
- logger;
6
- constructor({ gateway, logger = Logger.default, }) {
7
- try {
8
- this.gateway = new URL(gateway);
9
- }
10
- catch (error) {
11
- throw new Error(`Invalid URL provided for static gateway: ${gateway}`);
12
- }
13
- this.logger = logger;
14
- }
15
- // provided gateways are ignored
16
- async selectGateway({ gateways = [], } = {}) {
17
- if (gateways.length > 0) {
18
- this.logger.warn('StaticRoutingStrategy does not accept provided gateways. Ignoring provided gateways...', {
19
- providedGateways: gateways.length,
20
- internalGateway: this.gateway,
21
- });
22
- }
23
- return this.gateway;
24
- }
25
- }
@@ -1,35 +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
- import assert from 'node:assert/strict';
17
- import { describe, it } from 'node:test';
18
- import { StaticRoutingStrategy } from './static.js';
19
- describe('StaticRoutingStrategy', () => {
20
- it('returns the configured gateway regardless of the gateways parameter', async () => {
21
- const staticGateway = 'https://static-example.com/';
22
- const strategy = new StaticRoutingStrategy({
23
- gateway: staticGateway,
24
- });
25
- const result1 = await strategy.selectGateway();
26
- const result2 = await strategy.selectGateway();
27
- const result3 = await strategy.selectGateway();
28
- assert.equal(result1.toString(), staticGateway, 'Should return the static gateway');
29
- assert.equal(result2.toString(), staticGateway, 'Should return the static gateway');
30
- assert.equal(result3.toString(), staticGateway, 'Should return the static gateway even when no gateways are provided');
31
- });
32
- it('throws an error when an invalid URL is provided', () => {
33
- assert.throws(() => new StaticRoutingStrategy({ gateway: 'not-a-valid-url' }), /Invalid URL provided for static gateway/, 'Should throw an error when an invalid URL is provided');
34
- });
35
- });
@@ -1,130 +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
- import Arweave from 'arweave';
17
- import { MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, buildLayers, generateLeaves, } from 'arweave/node/lib/merkle.js';
18
- import { Readable } from 'node:stream';
19
- import { toB64Url } from '../../../../utils/base64.js';
20
- export async function convertBufferToDataRoot({ buffer, }) {
21
- const chunks = [];
22
- let cursor = 0;
23
- let offset = 0;
24
- while (offset < buffer.byteLength) {
25
- let chunkSize = Math.min(MAX_CHUNK_SIZE, buffer.byteLength - offset);
26
- const remainder = buffer.byteLength - offset - chunkSize;
27
- if (remainder > 0 && remainder < MIN_CHUNK_SIZE) {
28
- chunkSize = Math.ceil((buffer.byteLength - offset) / 2);
29
- }
30
- // subarray does not exist on web Buffer type
31
- const slice = buffer.subarray(offset, offset + chunkSize);
32
- const hash = await crypto.subtle.digest('SHA-256', slice);
33
- const hashArray = new Uint8Array(hash);
34
- chunks.push({
35
- dataHash: hashArray,
36
- minByteRange: cursor,
37
- maxByteRange: cursor + chunkSize,
38
- });
39
- cursor += chunkSize;
40
- offset += chunkSize;
41
- }
42
- const leaves = await generateLeaves(chunks);
43
- const result = await buildLayers(leaves);
44
- return Buffer.from(result.id).toString('base64url');
45
- }
46
- export const convertReadableToDataRoot = async ({ iterable, }) => {
47
- const chunks = [];
48
- let leftover = new Uint8Array(0);
49
- let cursor = 0;
50
- for await (const data of iterable) {
51
- const inputChunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
52
- const combined = new Uint8Array(leftover.length + inputChunk.length);
53
- combined.set(leftover, 0);
54
- combined.set(inputChunk, leftover.length);
55
- let startIndex = 0;
56
- while (combined.length - startIndex >= MAX_CHUNK_SIZE) {
57
- let chunkSize = MAX_CHUNK_SIZE;
58
- const remainderAfterThis = combined.length - startIndex - MAX_CHUNK_SIZE;
59
- if (remainderAfterThis > 0 && remainderAfterThis < MIN_CHUNK_SIZE) {
60
- chunkSize = Math.ceil((combined.length - startIndex) / 2);
61
- }
62
- const chunkData = combined.slice(startIndex, startIndex + chunkSize);
63
- const dataHash = await Arweave.crypto.hash(chunkData);
64
- chunks.push({
65
- dataHash,
66
- minByteRange: cursor,
67
- maxByteRange: cursor + chunkSize,
68
- });
69
- cursor += chunkSize;
70
- startIndex += chunkSize;
71
- }
72
- leftover = combined.slice(startIndex);
73
- }
74
- if (leftover.length > 0) {
75
- const dataHash = await Arweave.crypto.hash(leftover);
76
- chunks.push({
77
- dataHash,
78
- minByteRange: cursor,
79
- maxByteRange: cursor + leftover.length,
80
- });
81
- }
82
- const leaves = await generateLeaves(chunks);
83
- const root = await buildLayers(leaves);
84
- return toB64Url(Buffer.from(root.id));
85
- };
86
- export class DataRootVerificationStrategy {
87
- trustedDataRootProvider;
88
- constructor({ trustedDataRootProvider, }) {
89
- this.trustedDataRootProvider = trustedDataRootProvider;
90
- }
91
- async verifyData({ data, txId, }) {
92
- const trustedDataRootPromise = this.trustedDataRootProvider.getDataRoot({
93
- txId,
94
- });
95
- let computedDataRoot;
96
- if (Buffer.isBuffer(data)) {
97
- computedDataRoot = await convertBufferToDataRoot({ buffer: data });
98
- }
99
- else if (data instanceof Readable || data instanceof ReadableStream) {
100
- computedDataRoot = await convertReadableToDataRoot({ iterable: data });
101
- }
102
- if (computedDataRoot === undefined) {
103
- throw new Error('Data root could not be computed');
104
- }
105
- const trustedDataRoot = await trustedDataRootPromise;
106
- if (computedDataRoot !== trustedDataRoot) {
107
- throw new Error('Data root does not match', {
108
- cause: { computedDataRoot, trustedDataRoot },
109
- });
110
- }
111
- }
112
- }
113
- // some data item options
114
- // compute and verify data root, use offsets from server and verify the signature that the data item at the offset matches the signature
115
- // does not give you assurance of valid bundle, but gives verification that the data item itself is valid
116
- // reading from offsets is the only way for the client to compute and verify the signature
117
- /**
118
- * - when you get a signature of a data item, you can only verify the owner
119
- * - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
120
- * - you need to the location of the chunks for the data item, and prove it's in the chunk and then prove the data root of the bundle, then you have fully verified the data verifier
121
- * - how to prove the data item is on arweave - verify the merkle hash that the chunks for the data item, fit within the expected tree of the parent bundle
122
- *
123
- * Composite verifier - you'll want to be very efficient with streams
124
- * - hash verifier
125
- * - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
126
- * - signature verifier
127
- * - offset verifier
128
- * - data item verifier
129
- */
130
- // introduce a composite verifier that determines where/how to lookup the hash
@@ -1,46 +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
- import { Readable } from 'node:stream';
17
- import { hashBufferToB64Url, hashReadableStreamToB64Url, hashReadableToB64Url, } from '../../../../utils/hash.js';
18
- export class HashVerificationStrategy {
19
- trustedHashProvider;
20
- constructor({ trustedHashProvider, }) {
21
- this.trustedHashProvider = trustedHashProvider;
22
- }
23
- async verifyData({ data, txId, }) {
24
- const hashPromise = this.trustedHashProvider.getHash({ txId });
25
- let computedHash;
26
- if (Buffer.isBuffer(data)) {
27
- computedHash = hashBufferToB64Url(data);
28
- }
29
- else if (data instanceof Readable) {
30
- computedHash = await hashReadableToB64Url(data);
31
- }
32
- else if (data instanceof ReadableStream) {
33
- computedHash = await hashReadableStreamToB64Url(data);
34
- }
35
- // await on the hash promise and compare to get a little concurrency when computing hashes over larger data
36
- const { hash } = await hashPromise;
37
- if (computedHash === undefined) {
38
- throw new Error('Hash could not be computed');
39
- }
40
- if (computedHash !== hash) {
41
- throw new Error('Hash does not match', {
42
- cause: { computedHash, trustedHash: hash },
43
- });
44
- }
45
- }
46
- }