@ar.io/sdk 3.11.0-alpha.11 → 3.11.0-alpha.13

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 (61) hide show
  1. package/bundles/web.bundle.min.js +120 -123
  2. package/lib/cjs/common/contracts/ao-process.js +2 -1
  3. package/lib/cjs/common/index.js +0 -2
  4. package/lib/cjs/types/index.js +0 -1
  5. package/lib/cjs/version.js +1 -1
  6. package/lib/esm/common/contracts/ao-process.js +2 -1
  7. package/lib/esm/common/index.js +0 -2
  8. package/lib/esm/types/index.js +0 -1
  9. package/lib/esm/version.js +1 -1
  10. package/lib/types/common/index.d.ts +0 -1
  11. package/lib/types/types/index.d.ts +0 -1
  12. package/lib/types/version.d.ts +1 -1
  13. package/package.json +1 -1
  14. package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +0 -106
  15. package/lib/cjs/common/wayfinder/gateways.js +0 -86
  16. package/lib/cjs/common/wayfinder/index.js +0 -44
  17. package/lib/cjs/common/wayfinder/routers/priority.js +0 -29
  18. package/lib/cjs/common/wayfinder/routers/priority.test.js +0 -155
  19. package/lib/cjs/common/wayfinder/routers/random.js +0 -23
  20. package/lib/cjs/common/wayfinder/routers/random.test.js +0 -25
  21. package/lib/cjs/common/wayfinder/routers/simple-cache.js +0 -25
  22. package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +0 -41
  23. package/lib/cjs/common/wayfinder/routers/static.js +0 -14
  24. package/lib/cjs/common/wayfinder/routers/static.test.js +0 -14
  25. package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +0 -139
  26. package/lib/cjs/common/wayfinder/verification/hash-verifier.js +0 -50
  27. package/lib/cjs/common/wayfinder/wayfinder.js +0 -596
  28. package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -500
  29. package/lib/cjs/types/wayfinder.js +0 -3
  30. package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +0 -102
  31. package/lib/esm/common/wayfinder/gateways.js +0 -80
  32. package/lib/esm/common/wayfinder/index.js +0 -28
  33. package/lib/esm/common/wayfinder/routers/priority.js +0 -25
  34. package/lib/esm/common/wayfinder/routers/priority.test.js +0 -153
  35. package/lib/esm/common/wayfinder/routers/random.js +0 -19
  36. package/lib/esm/common/wayfinder/routers/random.test.js +0 -23
  37. package/lib/esm/common/wayfinder/routers/simple-cache.js +0 -21
  38. package/lib/esm/common/wayfinder/routers/simple-cache.test.js +0 -39
  39. package/lib/esm/common/wayfinder/routers/static.js +0 -10
  40. package/lib/esm/common/wayfinder/routers/static.test.js +0 -12
  41. package/lib/esm/common/wayfinder/verification/data-root-verifier.js +0 -130
  42. package/lib/esm/common/wayfinder/verification/hash-verifier.js +0 -46
  43. package/lib/esm/common/wayfinder/wayfinder.js +0 -584
  44. package/lib/esm/common/wayfinder/wayfinder.test.js +0 -495
  45. package/lib/esm/types/wayfinder.js +0 -2
  46. package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +0 -51
  47. package/lib/types/common/wayfinder/gateways.d.ts +0 -53
  48. package/lib/types/common/wayfinder/index.d.ts +0 -23
  49. package/lib/types/common/wayfinder/routers/priority.d.ts +0 -29
  50. package/lib/types/common/wayfinder/routers/priority.test.d.ts +0 -1
  51. package/lib/types/common/wayfinder/routers/random.d.ts +0 -27
  52. package/lib/types/common/wayfinder/routers/random.test.d.ts +0 -1
  53. package/lib/types/common/wayfinder/routers/simple-cache.d.ts +0 -28
  54. package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +0 -1
  55. package/lib/types/common/wayfinder/routers/static.d.ts +0 -24
  56. package/lib/types/common/wayfinder/routers/static.test.d.ts +0 -1
  57. package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +0 -31
  58. package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +0 -27
  59. package/lib/types/common/wayfinder/wayfinder.d.ts +0 -265
  60. package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
  61. package/lib/types/types/wayfinder.d.ts +0 -62
@@ -98,7 +98,8 @@ class AOProcess {
98
98
  tags,
99
99
  processId: this.processId,
100
100
  });
101
- throw new Error(`Process ${this.processId} does not support provided action.`);
101
+ throw new Error(result.message ||
102
+ `Process ${this.processId} did not return a valid response. Response: ${JSON.stringify(result)}`);
102
103
  }
103
104
  const messageData = result.Messages?.[0]?.Data;
104
105
  // return undefined if no data is returned
@@ -38,5 +38,3 @@ __exportStar(require("./faucet.js"), exports);
38
38
  // ao
39
39
  __exportStar(require("./io.js"), exports);
40
40
  __exportStar(require("./contracts/ao-process.js"), exports);
41
- // wayfinder
42
- __exportStar(require("./wayfinder/index.js"), exports);
@@ -35,4 +35,3 @@ __exportStar(require("./common.js"), exports);
35
35
  __exportStar(require("./faucet.js"), exports);
36
36
  __exportStar(require("./io.js"), exports);
37
37
  __exportStar(require("./token.js"), exports);
38
- __exportStar(require("./wayfinder.js"), exports);
@@ -17,4 +17,4 @@
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.version = void 0;
19
19
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
20
- exports.version = '3.11.0-alpha.11';
20
+ exports.version = '3.11.0-alpha.13';
@@ -95,7 +95,8 @@ export class AOProcess {
95
95
  tags,
96
96
  processId: this.processId,
97
97
  });
98
- throw new Error(`Process ${this.processId} does not support provided action.`);
98
+ throw new Error(result.message ||
99
+ `Process ${this.processId} did not return a valid response. Response: ${JSON.stringify(result)}`);
99
100
  }
100
101
  const messageData = result.Messages?.[0]?.Data;
101
102
  // return undefined if no data is returned
@@ -22,5 +22,3 @@ export * from './faucet.js';
22
22
  // ao
23
23
  export * from './io.js';
24
24
  export * from './contracts/ao-process.js';
25
- // wayfinder
26
- export * from './wayfinder/index.js';
@@ -19,4 +19,3 @@ export * from './common.js';
19
19
  export * from './faucet.js';
20
20
  export * from './io.js';
21
21
  export * from './token.js';
22
- export * from './wayfinder.js';
@@ -14,4 +14,4 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
17
- export const version = '3.11.0-alpha.11';
17
+ export const version = '3.11.0-alpha.13';
@@ -21,4 +21,3 @@ export * from './ant-versions.js';
21
21
  export * from './faucet.js';
22
22
  export * from './io.js';
23
23
  export * from './contracts/ao-process.js';
24
- export * from './wayfinder/index.js';
@@ -19,4 +19,3 @@ export * from './common.js';
19
19
  export * from './faucet.js';
20
20
  export * from './io.js';
21
21
  export * from './token.js';
22
- export * from './wayfinder.js';
@@ -13,4 +13,4 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export declare const version = "3.11.0-alpha.10";
16
+ export declare const version = "3.11.0-alpha.12";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ar.io/sdk",
3
- "version": "3.11.0-alpha.11",
3
+ "version": "3.11.0-alpha.13",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ar-io/ar-io-sdk.git"
@@ -1,106 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TrustedGatewaysHashProvider = void 0;
4
- const arioGatewayHeaders = {
5
- digest: 'x-ar-io-digest',
6
- verified: 'x-ar-io-verified',
7
- txId: 'x-arns-resolved-tx-id',
8
- processId: 'x-arns-resolved-process-id',
9
- };
10
- class TrustedGatewaysHashProvider {
11
- gatewaysProvider;
12
- constructor({ gatewaysProvider,
13
- // TODO: add threshold for allowed hash difference (i.e. by count or ratio of total gateways checked)
14
- }) {
15
- this.gatewaysProvider = gatewaysProvider;
16
- }
17
- /**
18
- * Gets the digest for a given txId from all trusted gateways and ensures they all match.
19
- * @param txId - The txId to get the digest for.
20
- * @returns The digest for the given txId.
21
- */
22
- async getHash({ txId, }) {
23
- // get the hash from every gateway, and ensure they all match
24
- const hashSet = new Set();
25
- const hashResults = [];
26
- const gateways = await this.gatewaysProvider.getGateways();
27
- const hashes = await Promise.all(gateways.map(async (gateway) => {
28
- const response = await fetch(`${gateway.toString()}${txId}`, {
29
- method: 'HEAD',
30
- redirect: 'follow',
31
- });
32
- if (!response.ok) {
33
- // skip this gateway
34
- return undefined;
35
- }
36
- const txIdHash = response.headers.get(arioGatewayHeaders.digest);
37
- if (txIdHash === null || txIdHash === undefined) {
38
- // skip this gateway
39
- return undefined;
40
- }
41
- hashResults.push({
42
- gateway: gateway.hostname,
43
- txIdHash,
44
- });
45
- return txIdHash;
46
- }));
47
- for (const hash of hashes) {
48
- if (hash !== undefined) {
49
- hashSet.add(hash);
50
- }
51
- }
52
- if (hashSet.size === 0) {
53
- throw new Error(`No trusted gateways returned a hash for txId ${txId}`);
54
- }
55
- if (hashSet.size > 1) {
56
- throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
57
- }
58
- return { hash: hashResults[0].txIdHash, algorithm: 'sha256' };
59
- }
60
- /**
61
- * Get the data root for a given txId from all trusted gateways and ensure they all match.
62
- * @param txId - The txId to get the data root for.
63
- * @returns The data root for the given txId.
64
- */
65
- async getDataRoot({ txId }) {
66
- const dataRootSet = new Set();
67
- const dataRootResults = [];
68
- const gateways = await this.gatewaysProvider.getGateways();
69
- const dataRoots = await Promise.all(gateways.map(async (gateway) => {
70
- const response = await fetch(`${gateway.toString()}tx/${txId}/data_root`);
71
- if (!response.ok) {
72
- // skip this gateway
73
- return undefined;
74
- }
75
- const dataRoot = await response.text();
76
- dataRootResults.push({
77
- gateway: gateway.hostname,
78
- dataRoot,
79
- });
80
- return dataRoot;
81
- }));
82
- for (const dataRoot of dataRoots) {
83
- if (dataRoot !== undefined) {
84
- dataRootSet.add(dataRoot);
85
- }
86
- }
87
- if (dataRootSet.size > 1) {
88
- throw new Error(`Failed to get consistent data root from all trusted gateways. ${JSON.stringify(dataRootResults)}`);
89
- }
90
- return dataRootSet.values().next().value;
91
- }
92
- }
93
- exports.TrustedGatewaysHashProvider = TrustedGatewaysHashProvider;
94
- // client could check hashes of data items, match expected hash
95
- // if the gateway has the hash and they've verified it, you can trust the data item and offset
96
- // you would be only trusting the gateway that it is a valid bundle
97
- // you can request the offset from the gateway to verify the id
98
- /**
99
- * Note from @djwhitt
100
- *
101
- * Calculating data roots this way is fine, but it may not reproduce the original data root.
102
- * We could also implement a data root verifier that pulls all the chunks, checks that they
103
- * reproduce the expected data root, and then compares the concatenated chunk data to the
104
- * original data retrieved. That would take a while, but it should be able to verify any L1
105
- * data where we can find the chunks.
106
- */
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SimpleCacheGatewaysProvider = exports.StaticGatewaysProvider = exports.NetworkGatewaysProvider = void 0;
4
- class NetworkGatewaysProvider {
5
- ario;
6
- sortBy;
7
- sortOrder;
8
- limit;
9
- filter;
10
- constructor({ ario, sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
11
- this.ario = ario;
12
- this.sortBy = sortBy;
13
- this.sortOrder = sortOrder;
14
- this.limit = limit;
15
- this.filter = filter;
16
- }
17
- async getGateways() {
18
- let cursor;
19
- let attempts = 0;
20
- const gateways = [];
21
- do {
22
- try {
23
- const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
24
- limit: 1000,
25
- cursor,
26
- sortBy: this.sortBy,
27
- sortOrder: this.sortOrder,
28
- });
29
- gateways.push(...newGateways);
30
- cursor = nextCursor;
31
- attempts = 0; // reset attempts if we get a new cursor
32
- }
33
- catch (error) {
34
- console.error('Error fetching gateways', {
35
- cursor,
36
- attempts,
37
- error,
38
- });
39
- attempts++;
40
- }
41
- } while (cursor !== undefined && attempts < 3);
42
- // filter out any gateways that are not joined
43
- const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
44
- return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
45
- }
46
- }
47
- exports.NetworkGatewaysProvider = NetworkGatewaysProvider;
48
- class StaticGatewaysProvider {
49
- gateways;
50
- constructor({ gateways }) {
51
- this.gateways = gateways.map((g) => new URL(g));
52
- }
53
- async getGateways() {
54
- return this.gateways;
55
- }
56
- }
57
- exports.StaticGatewaysProvider = StaticGatewaysProvider;
58
- class SimpleCacheGatewaysProvider {
59
- gatewaysProvider;
60
- ttlSeconds;
61
- lastUpdated;
62
- gatewaysCache;
63
- constructor({ gatewaysProvider, ttlSeconds = 5 * 60, // 5 minutes
64
- }) {
65
- this.gatewaysCache = [];
66
- this.gatewaysProvider = gatewaysProvider;
67
- this.ttlSeconds = ttlSeconds;
68
- }
69
- async getGateways() {
70
- const now = Date.now();
71
- if (this.gatewaysCache.length === 0 ||
72
- now - this.lastUpdated > this.ttlSeconds * 1000) {
73
- try {
74
- // preserve the cache if the fetch fails
75
- const allGateways = await this.gatewaysProvider.getGateways();
76
- this.gatewaysCache = allGateways;
77
- this.lastUpdated = now;
78
- }
79
- catch (error) {
80
- console.error('Error fetching gateways', error);
81
- }
82
- }
83
- return this.gatewaysCache;
84
- }
85
- }
86
- exports.SimpleCacheGatewaysProvider = SimpleCacheGatewaysProvider;
@@ -1,44 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- /**
18
- * Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
19
- *
20
- * Licensed under the Apache License, Version 2.0 (the "License");
21
- * you may not use this file except in compliance with the License.
22
- * You may obtain a copy of the License at
23
- *
24
- * http://www.apache.org/licenses/LICENSE-2.0
25
- *
26
- * Unless required by applicable law or agreed to in writing, software
27
- * distributed under the License is distributed on an "AS IS" BASIS,
28
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
- * See the License for the specific language governing permissions and
30
- * limitations under the License.
31
- */
32
- __exportStar(require("./wayfinder.js"), exports);
33
- // routers
34
- __exportStar(require("./routers/random.js"), exports);
35
- __exportStar(require("./routers/priority.js"), exports);
36
- __exportStar(require("./routers/static.js"), exports);
37
- // gateways providers
38
- __exportStar(require("./gateways.js"), exports);
39
- // trusted gateways
40
- __exportStar(require("./gateways/trusted-gateways.js"), exports);
41
- // hash providers
42
- __exportStar(require("./verification/data-root-verifier.js"), exports);
43
- __exportStar(require("./verification/hash-verifier.js"), exports);
44
- // TODO: signature verification
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PriorityGatewayRouter = void 0;
4
- const random_js_1 = require("../../../utils/random.js");
5
- const gateways_js_1 = require("../gateways.js");
6
- // TODO: one of N where N are in the last time window have met certain performance thresholds
7
- // TODO: look at bitorrent routing protocols for inspiration
8
- // TODO: router that looks at local stats/metrics and adjusts based on those
9
- class PriorityGatewayRouter {
10
- name = 'priority';
11
- gatewaysProvider;
12
- constructor({ ario, sortBy, sortOrder, limit, }) {
13
- this.gatewaysProvider = new gateways_js_1.NetworkGatewaysProvider({
14
- ario,
15
- sortBy,
16
- sortOrder,
17
- limit,
18
- });
19
- }
20
- async getTargetGateway() {
21
- const gateways = await this.gatewaysProvider.getGateways();
22
- const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
23
- if (targetGateway === undefined) {
24
- throw new Error('No target gateway found');
25
- }
26
- return targetGateway;
27
- }
28
- }
29
- exports.PriorityGatewayRouter = PriorityGatewayRouter;
@@ -1,155 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const priority_js_1 = require("./priority.js");
6
- (0, node_test_1.describe)('PriorityRouter', () => {
7
- const mockGateways = [
8
- {
9
- settings: {
10
- fqdn: 'gateway1.net',
11
- port: 443,
12
- protocol: 'https',
13
- allowDelegatedStaking: false,
14
- delegateRewardShareRatio: 0.5,
15
- allowedDelegates: [],
16
- minDelegatedStake: 0,
17
- autoStake: false,
18
- properties: '',
19
- label: '',
20
- note: '',
21
- },
22
- gatewayAddress: 'addr1',
23
- observerAddress: 'addr1',
24
- totalDelegatedStake: 1000,
25
- startTimestamp: 0,
26
- endTimestamp: 0,
27
- operatorStake: 100,
28
- status: 'joined',
29
- weights: {
30
- normalizedCompositeWeight: 0.5,
31
- stakeWeight: 0.5,
32
- tenureWeight: 0.5,
33
- gatewayPerformanceRatio: 0.5,
34
- observerPerformanceRatio: 0.5,
35
- compositeWeight: 0.5,
36
- gatewayRewardRatioWeight: 0.5,
37
- observerRewardRatioWeight: 0.5,
38
- },
39
- stats: {
40
- passedConsecutiveEpochs: 10,
41
- failedConsecutiveEpochs: 5,
42
- totalEpochCount: 15,
43
- passedEpochCount: 10,
44
- failedEpochCount: 5,
45
- observedEpochCount: 15,
46
- prescribedEpochCount: 20,
47
- },
48
- },
49
- {
50
- settings: {
51
- fqdn: 'gateway1.net',
52
- port: 443,
53
- protocol: 'https',
54
- allowDelegatedStaking: false,
55
- delegateRewardShareRatio: 0.5,
56
- allowedDelegates: [],
57
- minDelegatedStake: 0,
58
- autoStake: false,
59
- properties: '',
60
- label: '',
61
- note: '',
62
- },
63
- gatewayAddress: 'addr1',
64
- observerAddress: 'addr1',
65
- totalDelegatedStake: 2000,
66
- startTimestamp: 0,
67
- endTimestamp: 0,
68
- operatorStake: 2000,
69
- status: 'joined',
70
- weights: {
71
- normalizedCompositeWeight: 0.5,
72
- stakeWeight: 0.5,
73
- tenureWeight: 0.5,
74
- gatewayPerformanceRatio: 0.5,
75
- observerPerformanceRatio: 0.5,
76
- compositeWeight: 0.5,
77
- gatewayRewardRatioWeight: 0.5,
78
- observerRewardRatioWeight: 0.5,
79
- },
80
- stats: {
81
- passedConsecutiveEpochs: 10,
82
- failedConsecutiveEpochs: 5,
83
- totalEpochCount: 15,
84
- passedEpochCount: 10,
85
- failedEpochCount: 5,
86
- observedEpochCount: 15,
87
- prescribedEpochCount: 20,
88
- },
89
- },
90
- {
91
- settings: {
92
- fqdn: 'gateway2.net',
93
- port: 443,
94
- protocol: 'https',
95
- allowDelegatedStaking: false,
96
- delegateRewardShareRatio: 0.5,
97
- allowedDelegates: [],
98
- minDelegatedStake: 0,
99
- autoStake: false,
100
- properties: '',
101
- label: '',
102
- note: '',
103
- },
104
- gatewayAddress: 'addr2',
105
- observerAddress: 'addr2',
106
- totalDelegatedStake: 0,
107
- startTimestamp: 0,
108
- endTimestamp: 0,
109
- operatorStake: 0,
110
- status: 'leaving',
111
- weights: {
112
- normalizedCompositeWeight: 0.5,
113
- stakeWeight: 0.5,
114
- tenureWeight: 0.5,
115
- gatewayPerformanceRatio: 0.5,
116
- observerPerformanceRatio: 0.5,
117
- compositeWeight: 0.5,
118
- gatewayRewardRatioWeight: 0.5,
119
- observerRewardRatioWeight: 0.5,
120
- },
121
- stats: {
122
- passedConsecutiveEpochs: 10,
123
- failedConsecutiveEpochs: 5,
124
- totalEpochCount: 15,
125
- passedEpochCount: 10,
126
- failedEpochCount: 5,
127
- observedEpochCount: 15,
128
- prescribedEpochCount: 20,
129
- },
130
- },
131
- ];
132
- const mockArIOClient = {
133
- getGateways: async () => ({ items: mockGateways }),
134
- };
135
- (0, node_test_1.it)('should prioritize gateway with highest success rate when using successRate weight', async () => {
136
- const router = new priority_js_1.PriorityGatewayRouter({
137
- ario: mockArIOClient,
138
- sortBy: 'operatorStake',
139
- sortOrder: 'desc',
140
- limit: 1,
141
- });
142
- const result = await router.getTargetGateway();
143
- node_assert_1.strict.deepStrictEqual(result, new URL('http://gateway1.net'));
144
- });
145
- (0, node_test_1.it)('should prioritize gateway with lowest latency when using latency weight', async () => {
146
- const router = new priority_js_1.PriorityGatewayRouter({
147
- ario: mockArIOClient,
148
- sortBy: 'operatorStake',
149
- sortOrder: 'desc',
150
- limit: 1,
151
- });
152
- const result = await router.getTargetGateway();
153
- node_assert_1.strict.deepStrictEqual(result, new URL('http://gateway2.net'));
154
- });
155
- });
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RandomGatewayRouter = void 0;
4
- const random_js_1 = require("../../../utils/random.js");
5
- class RandomGatewayRouter {
6
- name = 'random';
7
- gatewaysProvider;
8
- blocklist;
9
- constructor({ gatewaysProvider, blocklist = [], }) {
10
- this.gatewaysProvider = gatewaysProvider;
11
- this.blocklist = blocklist;
12
- }
13
- async getTargetGateway() {
14
- const allGateways = await this.gatewaysProvider.getGateways();
15
- const gateways = allGateways.filter((g) => !this.blocklist.includes(g.hostname));
16
- const targetGateway = gateways[(0, random_js_1.randomInt)(0, gateways.length)];
17
- if (targetGateway === undefined) {
18
- throw new Error('No target gateway found');
19
- }
20
- return targetGateway;
21
- }
22
- }
23
- exports.RandomGatewayRouter = RandomGatewayRouter;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const random_js_1 = require("./random.js");
6
- (0, node_test_1.describe)('RandomRouter', () => {
7
- const mockGateways = [
8
- new URL('https://gateway1.net'),
9
- new URL('https://gateway2.net'),
10
- new URL('https://gateway3.net'),
11
- ];
12
- const mockGatewaysProvider = {
13
- getGateways: async () => mockGateways,
14
- };
15
- (0, node_test_1.it)('should only return joined gateways', async () => {
16
- const router = new random_js_1.RandomGatewayRouter({
17
- gatewaysProvider: mockGatewaysProvider,
18
- });
19
- // random gateway should be one of the mock gateways
20
- for (let i = 0; i < 10; i++) {
21
- const result = await router.getTargetGateway();
22
- node_assert_1.strict.ok(mockGateways.includes(result));
23
- }
24
- });
25
- });
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SimpleCacheRouter = void 0;
4
- class SimpleCacheRouter {
5
- name;
6
- lastUpdatedTimestamp;
7
- ttlSeconds;
8
- targetGateway;
9
- router;
10
- constructor({ router, ttlSeconds = 5 * 60, // 5 minutes
11
- }) {
12
- this.router = router;
13
- this.ttlSeconds = ttlSeconds;
14
- }
15
- async getTargetGateway() {
16
- if (this.targetGateway === undefined ||
17
- this.lastUpdatedTimestamp + this.ttlSeconds * 1000 < Date.now()) {
18
- this.targetGateway = await this.router.getTargetGateway();
19
- this.lastUpdatedTimestamp = Date.now();
20
- }
21
- return this.targetGateway;
22
- }
23
- }
24
- exports.SimpleCacheRouter = SimpleCacheRouter;
25
- // TODO: a router that accepts ario and a router, and adds read through promise cache to ario.getGateways to avoid calling the ARIO contract on every request
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const simple_cache_js_1 = require("./simple-cache.js");
6
- (0, node_test_1.describe)('SimpleCacheRouter', () => {
7
- (0, node_test_1.it)('should cache gateway for TTL period even if underlying gateway changes', async () => {
8
- const gateway1 = new URL('https://gateway1.net');
9
- const gateway2 = new URL('https://gateway2.net');
10
- let currentGateway = gateway1;
11
- const mockRouter = {
12
- name: 'mock',
13
- getTargetGateway: async () => currentGateway,
14
- };
15
- const router = new simple_cache_js_1.SimpleCacheRouter({
16
- router: mockRouter,
17
- ttlSeconds: 300, // 5 minutes
18
- });
19
- // Get initial gateway which should be cached
20
- const initial = await router.getTargetGateway();
21
- node_assert_1.strict.deepStrictEqual(initial, gateway1);
22
- // Change the underlying gateway
23
- currentGateway = gateway2;
24
- // Should still return cached gateway1 for multiple calls
25
- for (let i = 0; i < 5; i++) {
26
- const result = await router.getTargetGateway();
27
- node_assert_1.strict.deepStrictEqual(result, gateway1);
28
- }
29
- // Advance time past TTL
30
- const originalNow = Date.now;
31
- try {
32
- Date.now = () => originalNow() + 300 * 1000 + 1;
33
- // Should now return the new gateway2
34
- const result = await router.getTargetGateway();
35
- node_assert_1.strict.deepStrictEqual(result, gateway2);
36
- }
37
- finally {
38
- Date.now = originalNow;
39
- }
40
- });
41
- });
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StaticGatewayRouter = void 0;
4
- class StaticGatewayRouter {
5
- name = 'static';
6
- gateway;
7
- constructor({ gateway }) {
8
- this.gateway = new URL(gateway);
9
- }
10
- async getTargetGateway() {
11
- return this.gateway;
12
- }
13
- }
14
- exports.StaticGatewayRouter = StaticGatewayRouter;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_assert_1 = require("node:assert");
4
- const node_test_1 = require("node:test");
5
- const static_js_1 = require("./static.js");
6
- (0, node_test_1.describe)('StaticGatewayRouter', () => {
7
- (0, node_test_1.it)('should return the provided gateway', async () => {
8
- const router = new static_js_1.StaticGatewayRouter({
9
- gateway: 'http://test-gateway.net',
10
- });
11
- const result = await router.getTargetGateway();
12
- node_assert_1.strict.deepStrictEqual(result, new URL('http://test-gateway.net'));
13
- });
14
- });