@ar.io/sdk 3.11.0-alpha.8 → 3.11.0-beta.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.
- package/README.md +52 -0
- package/bundles/web.bundle.min.js +106 -106
- package/lib/cjs/cli/cli.js +137 -122
- package/lib/cjs/cli/commands/readCommands.js +6 -0
- package/lib/cjs/cli/utils.js +4 -1
- package/lib/cjs/common/ant.js +5 -5
- package/lib/cjs/common/contracts/ao-process.js +2 -1
- package/lib/cjs/common/io.js +37 -0
- package/lib/cjs/common/wayfinder/{gateways.js → gateways/network.js} +3 -41
- package/lib/cjs/common/wayfinder/gateways/simple-cache.js +35 -0
- package/lib/cjs/common/wayfinder/gateways/static.js +13 -0
- package/lib/cjs/common/wayfinder/index.js +11 -8
- package/lib/cjs/common/wayfinder/routing/strategies/ping.js +72 -0
- package/lib/cjs/common/wayfinder/routing/strategies/ping.test.js +156 -0
- package/lib/cjs/common/wayfinder/routing/strategies/random.js +13 -0
- package/lib/cjs/common/wayfinder/routing/strategies/random.test.js +68 -0
- package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +42 -0
- package/lib/cjs/common/wayfinder/routing/strategies/round-robin.test.js +78 -0
- package/lib/cjs/common/wayfinder/routing/strategies/static.js +29 -0
- package/lib/cjs/common/wayfinder/routing/strategies/static.test.js +40 -0
- package/lib/cjs/common/wayfinder/verification/{data-root-verifier.js → strategies/data-root-verifier.js} +4 -4
- package/lib/cjs/common/wayfinder/verification/{hash-verifier.js → strategies/hash-verifier.js} +4 -4
- package/lib/cjs/common/wayfinder/{gateways/trusted-gateways.js → verification/trusted.js} +1 -1
- package/lib/cjs/common/wayfinder/wayfinder.js +397 -257
- package/lib/cjs/common/wayfinder/wayfinder.test.js +227 -208
- 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/cli/utils.js +4 -1
- package/lib/esm/common/ant.js +5 -5
- package/lib/esm/common/contracts/ao-process.js +2 -1
- package/lib/esm/common/io.js +37 -0
- package/lib/esm/common/wayfinder/{gateways.js → gateways/network.js} +2 -38
- package/lib/esm/common/wayfinder/gateways/simple-cache.js +31 -0
- package/lib/esm/common/wayfinder/gateways/static.js +9 -0
- package/lib/esm/common/wayfinder/index.js +11 -8
- package/lib/esm/common/wayfinder/routing/strategies/ping.js +68 -0
- package/lib/esm/common/wayfinder/routing/strategies/ping.test.js +151 -0
- package/lib/esm/common/wayfinder/routing/strategies/random.js +9 -0
- package/lib/esm/common/wayfinder/routing/strategies/random.test.js +63 -0
- package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +38 -0
- package/lib/esm/common/wayfinder/routing/strategies/round-robin.test.js +73 -0
- package/lib/esm/common/wayfinder/routing/strategies/static.js +25 -0
- package/lib/esm/common/wayfinder/routing/strategies/static.test.js +35 -0
- package/lib/esm/common/wayfinder/verification/{data-root-verifier.js → strategies/data-root-verifier.js} +2 -2
- package/lib/esm/common/wayfinder/verification/{hash-verifier.js → strategies/hash-verifier.js} +2 -2
- package/lib/esm/common/wayfinder/{gateways/trusted-gateways.js → verification/trusted.js} +1 -1
- package/lib/esm/common/wayfinder/wayfinder.js +395 -255
- package/lib/esm/common/wayfinder/wayfinder.test.js +227 -208
- 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.d.ts → gateways/network.d.ts} +3 -23
- package/lib/types/common/wayfinder/{routers/random.d.ts → gateways/simple-cache.d.ts} +12 -8
- package/lib/types/common/wayfinder/{routers → gateways}/static.d.ts +6 -7
- package/lib/types/common/wayfinder/index.d.ts +10 -7
- package/lib/types/common/wayfinder/{routers/simple-cache.d.ts → routing/strategies/ping.d.ts} +10 -11
- package/lib/types/common/wayfinder/routing/strategies/random.d.ts +21 -0
- package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +29 -0
- package/lib/types/common/wayfinder/routing/strategies/static.d.ts +29 -0
- package/lib/types/common/wayfinder/verification/{data-root-verifier.d.ts → strategies/data-root-verifier.d.ts} +2 -2
- package/lib/types/common/wayfinder/verification/{hash-verifier.d.ts → strategies/hash-verifier.d.ts} +2 -2
- package/lib/types/common/wayfinder/{gateways/trusted-gateways.d.ts → verification/trusted.d.ts} +1 -1
- package/lib/types/common/wayfinder/wayfinder.d.ts +111 -77
- package/lib/types/types/io.d.ts +16 -1
- package/lib/types/types/wayfinder.d.ts +8 -4
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
- package/lib/cjs/common/wayfinder/routers/priority.js +0 -29
- package/lib/cjs/common/wayfinder/routers/priority.test.js +0 -155
- package/lib/cjs/common/wayfinder/routers/random.js +0 -23
- package/lib/cjs/common/wayfinder/routers/random.test.js +0 -25
- package/lib/cjs/common/wayfinder/routers/simple-cache.js +0 -25
- package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +0 -41
- package/lib/cjs/common/wayfinder/routers/static.js +0 -14
- package/lib/cjs/common/wayfinder/routers/static.test.js +0 -14
- package/lib/esm/common/wayfinder/routers/priority.js +0 -25
- package/lib/esm/common/wayfinder/routers/priority.test.js +0 -153
- package/lib/esm/common/wayfinder/routers/random.js +0 -19
- package/lib/esm/common/wayfinder/routers/random.test.js +0 -23
- package/lib/esm/common/wayfinder/routers/simple-cache.js +0 -21
- package/lib/esm/common/wayfinder/routers/simple-cache.test.js +0 -39
- package/lib/esm/common/wayfinder/routers/static.js +0 -10
- package/lib/esm/common/wayfinder/routers/static.test.js +0 -12
- package/lib/types/common/wayfinder/routers/priority.d.ts +0 -29
- /package/lib/types/common/wayfinder/{routers/priority.test.d.ts → routing/strategies/ping.test.d.ts} +0 -0
- /package/lib/types/common/wayfinder/{routers → routing/strategies}/random.test.d.ts +0 -0
- /package/lib/types/common/wayfinder/{routers/simple-cache.test.d.ts → routing/strategies/round-robin.test.d.ts} +0 -0
- /package/lib/types/common/wayfinder/{routers → routing/strategies}/static.test.d.ts +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
});
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
import Arweave from 'arweave';
|
|
17
17
|
import { MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, buildLayers, generateLeaves, } from 'arweave/node/lib/merkle.js';
|
|
18
18
|
import { Readable } from 'node:stream';
|
|
19
|
-
import { toB64Url } from '
|
|
19
|
+
import { toB64Url } from '../../../../utils/base64.js';
|
|
20
20
|
export async function convertBufferToDataRoot({ buffer, }) {
|
|
21
21
|
const chunks = [];
|
|
22
22
|
let cursor = 0;
|
|
@@ -83,7 +83,7 @@ export const convertReadableToDataRoot = async ({ iterable, }) => {
|
|
|
83
83
|
const root = await buildLayers(leaves);
|
|
84
84
|
return toB64Url(Buffer.from(root.id));
|
|
85
85
|
};
|
|
86
|
-
export class
|
|
86
|
+
export class DataRootVerificationStrategy {
|
|
87
87
|
trustedDataRootProvider;
|
|
88
88
|
constructor({ trustedDataRootProvider, }) {
|
|
89
89
|
this.trustedDataRootProvider = trustedDataRootProvider;
|
package/lib/esm/common/wayfinder/verification/{hash-verifier.js → strategies/hash-verifier.js}
RENAMED
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
import { Readable } from 'node:stream';
|
|
17
|
-
import { hashBufferToB64Url, hashReadableStreamToB64Url, hashReadableToB64Url, } from '
|
|
18
|
-
export class
|
|
17
|
+
import { hashBufferToB64Url, hashReadableStreamToB64Url, hashReadableToB64Url, } from '../../../../utils/hash.js';
|
|
18
|
+
export class HashVerificationStrategy {
|
|
19
19
|
trustedHashProvider;
|
|
20
20
|
constructor({ trustedHashProvider, }) {
|
|
21
21
|
this.trustedHashProvider = trustedHashProvider;
|
|
@@ -47,7 +47,7 @@ export class TrustedGatewaysHashProvider {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
if (hashSet.size === 0) {
|
|
50
|
-
throw new Error(`No trusted gateways
|
|
50
|
+
throw new Error(`No trusted gateways returned a hash for txId ${txId}`);
|
|
51
51
|
}
|
|
52
52
|
if (hashSet.size > 1) {
|
|
53
53
|
throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
|