@ar.io/sdk 3.10.0 → 3.11.0-alpha.2

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 (99) hide show
  1. package/README.md +66 -125
  2. package/bundles/web.bundle.min.js +130 -125
  3. package/lib/cjs/cli/options.js +12 -0
  4. package/lib/cjs/cli/utils.js +32 -10
  5. package/lib/cjs/cli/wayfinder.js +34 -0
  6. package/lib/cjs/common/ant-versions.js +5 -5
  7. package/lib/cjs/common/index.js +2 -0
  8. package/lib/cjs/common/io.js +64 -1
  9. package/lib/cjs/common/turbo.js +208 -0
  10. package/lib/cjs/common/wayfinder/gateways.js +75 -0
  11. package/lib/cjs/common/wayfinder/index.js +38 -0
  12. package/lib/cjs/common/wayfinder/routers/fixed.js +14 -0
  13. package/lib/cjs/common/wayfinder/routers/fixed.test.js +14 -0
  14. package/lib/cjs/common/wayfinder/routers/priority.js +38 -0
  15. package/lib/cjs/common/wayfinder/routers/priority.test.js +155 -0
  16. package/lib/cjs/common/wayfinder/routers/random.js +23 -0
  17. package/lib/cjs/common/wayfinder/routers/random.test.js +104 -0
  18. package/lib/cjs/common/wayfinder/routers/simple-cache.js +25 -0
  19. package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +41 -0
  20. package/lib/cjs/common/wayfinder/wayfinder.js +174 -0
  21. package/lib/cjs/common/wayfinder/wayfinder.test.js +253 -0
  22. package/lib/cjs/types/ant.js +12 -1
  23. package/lib/cjs/types/index.js +1 -0
  24. package/lib/cjs/types/io.js +1 -1
  25. package/lib/cjs/types/wayfinder.js +2 -0
  26. package/lib/cjs/utils/ao.js +5 -0
  27. package/lib/cjs/utils/random.js +30 -0
  28. package/lib/cjs/utils/url.js +28 -0
  29. package/lib/cjs/utils/url.test.js +24 -0
  30. package/lib/cjs/utils/utils.test.js +8 -8
  31. package/lib/cjs/version.js +1 -1
  32. package/lib/cjs/web/index.js +3 -1
  33. package/lib/esm/cli/options.js +12 -0
  34. package/lib/esm/cli/utils.js +31 -10
  35. package/lib/esm/cli/wayfinder.js +32 -0
  36. package/lib/esm/common/ant-versions.js +5 -5
  37. package/lib/esm/common/index.js +2 -0
  38. package/lib/esm/common/io.js +64 -1
  39. package/lib/esm/common/turbo.js +200 -0
  40. package/lib/esm/common/wayfinder/gateways.js +69 -0
  41. package/lib/esm/common/wayfinder/index.js +22 -0
  42. package/lib/esm/common/wayfinder/routers/fixed.js +10 -0
  43. package/lib/esm/common/wayfinder/routers/fixed.test.js +12 -0
  44. package/lib/esm/common/wayfinder/routers/priority.js +34 -0
  45. package/lib/esm/common/wayfinder/routers/priority.test.js +153 -0
  46. package/lib/esm/common/wayfinder/routers/random.js +19 -0
  47. package/lib/esm/common/wayfinder/routers/random.test.js +102 -0
  48. package/lib/esm/common/wayfinder/routers/simple-cache.js +21 -0
  49. package/lib/esm/common/wayfinder/routers/simple-cache.test.js +39 -0
  50. package/lib/esm/common/wayfinder/wayfinder.js +168 -0
  51. package/lib/esm/common/wayfinder/wayfinder.test.js +248 -0
  52. package/lib/esm/types/ant.js +11 -0
  53. package/lib/esm/types/index.js +1 -0
  54. package/lib/esm/types/io.js +1 -1
  55. package/lib/esm/types/wayfinder.js +1 -0
  56. package/lib/esm/utils/ao.js +5 -0
  57. package/lib/esm/utils/random.js +26 -0
  58. package/lib/esm/utils/url.js +24 -0
  59. package/lib/esm/utils/url.test.js +19 -0
  60. package/lib/esm/utils/utils.test.js +1 -1
  61. package/lib/esm/version.js +1 -1
  62. package/lib/esm/web/index.js +1 -1
  63. package/lib/types/cli/commands/antCommands.d.ts +3 -3
  64. package/lib/types/cli/commands/arnsPurchaseCommands.d.ts +1 -1
  65. package/lib/types/cli/commands/gatewayWriteCommands.d.ts +9 -9
  66. package/lib/types/cli/commands/readCommands.d.ts +1 -0
  67. package/lib/types/cli/commands/transfer.d.ts +3 -3
  68. package/lib/types/cli/options.d.ts +9 -0
  69. package/lib/types/cli/types.d.ts +3 -0
  70. package/lib/types/cli/utils.d.ts +4 -0
  71. package/lib/types/cli/wayfinder.d.ts +1 -0
  72. package/lib/types/common/ant-versions.d.ts +3 -6
  73. package/lib/types/common/index.d.ts +1 -0
  74. package/lib/types/common/io.d.ts +9 -7
  75. package/lib/types/common/turbo.d.ts +62 -0
  76. package/lib/types/common/wayfinder/gateways.d.ts +44 -0
  77. package/lib/types/common/wayfinder/index.d.ts +20 -0
  78. package/lib/types/common/wayfinder/routers/fixed.d.ts +24 -0
  79. package/lib/types/common/wayfinder/routers/fixed.test.d.ts +1 -0
  80. package/lib/types/common/wayfinder/routers/priority.d.ts +33 -0
  81. package/lib/types/common/wayfinder/routers/priority.test.d.ts +1 -0
  82. package/lib/types/common/wayfinder/routers/random.d.ts +27 -0
  83. package/lib/types/common/wayfinder/routers/random.test.d.ts +1 -0
  84. package/lib/types/common/wayfinder/routers/simple-cache.d.ts +28 -0
  85. package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +1 -0
  86. package/lib/types/common/wayfinder/wayfinder.d.ts +120 -0
  87. package/lib/types/common/wayfinder/wayfinder.test.d.ts +1 -0
  88. package/lib/types/types/ant.d.ts +53 -1
  89. package/lib/types/types/common.d.ts +7 -2
  90. package/lib/types/types/index.d.ts +1 -0
  91. package/lib/types/types/io.d.ts +7 -5
  92. package/lib/types/types/wayfinder.d.ts +19 -0
  93. package/lib/types/utils/ao.d.ts +1 -12
  94. package/lib/types/utils/random.d.ts +7 -0
  95. package/lib/types/utils/url.d.ts +19 -0
  96. package/lib/types/utils/url.test.d.ts +1 -0
  97. package/lib/types/version.d.ts +1 -1
  98. package/lib/types/web/index.d.ts +1 -1
  99. package/package.json +5 -3
@@ -0,0 +1,153 @@
1
+ import { strict as assert } from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { PriorityGatewayRouter } from './priority.js';
4
+ describe('PriorityRouter', () => {
5
+ const mockGateways = [
6
+ {
7
+ settings: {
8
+ fqdn: 'gateway1.net',
9
+ port: 443,
10
+ protocol: 'https',
11
+ allowDelegatedStaking: false,
12
+ delegateRewardShareRatio: 0.5,
13
+ allowedDelegates: [],
14
+ minDelegatedStake: 0,
15
+ autoStake: false,
16
+ properties: '',
17
+ label: '',
18
+ note: '',
19
+ },
20
+ gatewayAddress: 'addr1',
21
+ observerAddress: 'addr1',
22
+ totalDelegatedStake: 1000,
23
+ startTimestamp: 0,
24
+ endTimestamp: 0,
25
+ operatorStake: 100,
26
+ status: 'joined',
27
+ weights: {
28
+ normalizedCompositeWeight: 0.5,
29
+ stakeWeight: 0.5,
30
+ tenureWeight: 0.5,
31
+ gatewayPerformanceRatio: 0.5,
32
+ observerPerformanceRatio: 0.5,
33
+ compositeWeight: 0.5,
34
+ gatewayRewardRatioWeight: 0.5,
35
+ observerRewardRatioWeight: 0.5,
36
+ },
37
+ stats: {
38
+ passedConsecutiveEpochs: 10,
39
+ failedConsecutiveEpochs: 5,
40
+ totalEpochCount: 15,
41
+ passedEpochCount: 10,
42
+ failedEpochCount: 5,
43
+ observedEpochCount: 15,
44
+ prescribedEpochCount: 20,
45
+ },
46
+ },
47
+ {
48
+ settings: {
49
+ fqdn: 'gateway1.net',
50
+ port: 443,
51
+ protocol: 'https',
52
+ allowDelegatedStaking: false,
53
+ delegateRewardShareRatio: 0.5,
54
+ allowedDelegates: [],
55
+ minDelegatedStake: 0,
56
+ autoStake: false,
57
+ properties: '',
58
+ label: '',
59
+ note: '',
60
+ },
61
+ gatewayAddress: 'addr1',
62
+ observerAddress: 'addr1',
63
+ totalDelegatedStake: 2000,
64
+ startTimestamp: 0,
65
+ endTimestamp: 0,
66
+ operatorStake: 2000,
67
+ status: 'joined',
68
+ weights: {
69
+ normalizedCompositeWeight: 0.5,
70
+ stakeWeight: 0.5,
71
+ tenureWeight: 0.5,
72
+ gatewayPerformanceRatio: 0.5,
73
+ observerPerformanceRatio: 0.5,
74
+ compositeWeight: 0.5,
75
+ gatewayRewardRatioWeight: 0.5,
76
+ observerRewardRatioWeight: 0.5,
77
+ },
78
+ stats: {
79
+ passedConsecutiveEpochs: 10,
80
+ failedConsecutiveEpochs: 5,
81
+ totalEpochCount: 15,
82
+ passedEpochCount: 10,
83
+ failedEpochCount: 5,
84
+ observedEpochCount: 15,
85
+ prescribedEpochCount: 20,
86
+ },
87
+ },
88
+ {
89
+ settings: {
90
+ fqdn: 'gateway2.net',
91
+ port: 443,
92
+ protocol: 'https',
93
+ allowDelegatedStaking: false,
94
+ delegateRewardShareRatio: 0.5,
95
+ allowedDelegates: [],
96
+ minDelegatedStake: 0,
97
+ autoStake: false,
98
+ properties: '',
99
+ label: '',
100
+ note: '',
101
+ },
102
+ gatewayAddress: 'addr2',
103
+ observerAddress: 'addr2',
104
+ totalDelegatedStake: 0,
105
+ startTimestamp: 0,
106
+ endTimestamp: 0,
107
+ operatorStake: 0,
108
+ status: 'leaving',
109
+ weights: {
110
+ normalizedCompositeWeight: 0.5,
111
+ stakeWeight: 0.5,
112
+ tenureWeight: 0.5,
113
+ gatewayPerformanceRatio: 0.5,
114
+ observerPerformanceRatio: 0.5,
115
+ compositeWeight: 0.5,
116
+ gatewayRewardRatioWeight: 0.5,
117
+ observerRewardRatioWeight: 0.5,
118
+ },
119
+ stats: {
120
+ passedConsecutiveEpochs: 10,
121
+ failedConsecutiveEpochs: 5,
122
+ totalEpochCount: 15,
123
+ passedEpochCount: 10,
124
+ failedEpochCount: 5,
125
+ observedEpochCount: 15,
126
+ prescribedEpochCount: 20,
127
+ },
128
+ },
129
+ ];
130
+ const mockGatewaysProvider = {
131
+ getGateways: async () => mockGateways,
132
+ };
133
+ it('should prioritize gateway with highest success rate when using successRate weight', async () => {
134
+ const router = new PriorityGatewayRouter({
135
+ gatewaysProvider: mockGatewaysProvider,
136
+ sortBy: 'totalDelegatedStake',
137
+ sortOrder: 'desc',
138
+ limit: 1,
139
+ });
140
+ const result = await router.getTargetGateway();
141
+ assert.deepStrictEqual(result, new URL('http://gateway1.net'));
142
+ });
143
+ it('should prioritize gateway with lowest latency when using latency weight', async () => {
144
+ const router = new PriorityGatewayRouter({
145
+ gatewaysProvider: mockGatewaysProvider,
146
+ sortBy: 'operatorStake',
147
+ sortOrder: 'desc',
148
+ limit: 1,
149
+ });
150
+ const result = await router.getTargetGateway();
151
+ assert.deepStrictEqual(result, new URL('http://gateway2.net'));
152
+ });
153
+ });
@@ -0,0 +1,19 @@
1
+ import { randomInt } from '../../../utils/random.js';
2
+ export class RandomGatewayRouter {
3
+ name = 'random';
4
+ gatewaysProvider;
5
+ blocklist;
6
+ constructor({ gatewaysProvider, blocklist = [], }) {
7
+ this.gatewaysProvider = gatewaysProvider;
8
+ this.blocklist = blocklist;
9
+ }
10
+ async getTargetGateway() {
11
+ const allGateways = await this.gatewaysProvider.getGateways();
12
+ const gateways = allGateways.filter((g) => g.status === 'joined' && !this.blocklist.includes(g.settings.fqdn));
13
+ const targetGateway = gateways[randomInt(0, gateways.length)];
14
+ if (targetGateway === undefined) {
15
+ throw new Error('No target gateway found');
16
+ }
17
+ return new URL(`${targetGateway.settings.protocol}://${targetGateway.settings.fqdn}:${targetGateway.settings.port}`);
18
+ }
19
+ }
@@ -0,0 +1,102 @@
1
+ import { strict as assert } from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { RandomGatewayRouter } from './random.js';
4
+ describe('RandomRouter', () => {
5
+ const mockGateways = [
6
+ {
7
+ settings: {
8
+ fqdn: 'gateway1.net',
9
+ port: 443,
10
+ protocol: 'https',
11
+ allowDelegatedStaking: false,
12
+ delegateRewardShareRatio: 0.5,
13
+ allowedDelegates: [],
14
+ minDelegatedStake: 0,
15
+ autoStake: false,
16
+ properties: '',
17
+ label: '',
18
+ note: '',
19
+ },
20
+ gatewayAddress: 'addr1',
21
+ observerAddress: 'addr1',
22
+ totalDelegatedStake: 1000,
23
+ startTimestamp: 0,
24
+ endTimestamp: 0,
25
+ operatorStake: 100,
26
+ status: 'joined',
27
+ weights: {
28
+ normalizedCompositeWeight: 0.5,
29
+ stakeWeight: 0.5,
30
+ tenureWeight: 0.5,
31
+ gatewayPerformanceRatio: 0.5,
32
+ observerPerformanceRatio: 0.5,
33
+ compositeWeight: 0.5,
34
+ gatewayRewardRatioWeight: 0.5,
35
+ observerRewardRatioWeight: 0.5,
36
+ },
37
+ stats: {
38
+ passedConsecutiveEpochs: 10,
39
+ failedConsecutiveEpochs: 5,
40
+ totalEpochCount: 15,
41
+ passedEpochCount: 10,
42
+ failedEpochCount: 5,
43
+ observedEpochCount: 15,
44
+ prescribedEpochCount: 20,
45
+ },
46
+ },
47
+ {
48
+ settings: {
49
+ fqdn: 'gateway2.net',
50
+ port: 443,
51
+ protocol: 'https',
52
+ allowDelegatedStaking: false,
53
+ delegateRewardShareRatio: 0.5,
54
+ allowedDelegates: [],
55
+ minDelegatedStake: 0,
56
+ autoStake: false,
57
+ properties: '',
58
+ label: '',
59
+ note: '',
60
+ },
61
+ gatewayAddress: 'addr2',
62
+ observerAddress: 'addr2',
63
+ totalDelegatedStake: 0,
64
+ startTimestamp: 0,
65
+ endTimestamp: 0,
66
+ operatorStake: 0,
67
+ status: 'leaving',
68
+ weights: {
69
+ normalizedCompositeWeight: 0.5,
70
+ stakeWeight: 0.5,
71
+ tenureWeight: 0.5,
72
+ gatewayPerformanceRatio: 0.5,
73
+ observerPerformanceRatio: 0.5,
74
+ compositeWeight: 0.5,
75
+ gatewayRewardRatioWeight: 0.5,
76
+ observerRewardRatioWeight: 0.5,
77
+ },
78
+ stats: {
79
+ passedConsecutiveEpochs: 10,
80
+ failedConsecutiveEpochs: 5,
81
+ totalEpochCount: 15,
82
+ passedEpochCount: 10,
83
+ failedEpochCount: 5,
84
+ observedEpochCount: 15,
85
+ prescribedEpochCount: 20,
86
+ },
87
+ },
88
+ ];
89
+ const mockGatewaysProvider = {
90
+ getGateways: async () => mockGateways,
91
+ };
92
+ it('should only return joined gateways', async () => {
93
+ const router = new RandomGatewayRouter({
94
+ gatewaysProvider: mockGatewaysProvider,
95
+ });
96
+ // Run multiple times to ensure we only get joined gateways
97
+ for (let i = 0; i < 10; i++) {
98
+ const result = await router.getTargetGateway();
99
+ assert.deepStrictEqual(result, new URL('https://gateway1.net'));
100
+ }
101
+ });
102
+ });
@@ -0,0 +1,21 @@
1
+ export class SimpleCacheRouter {
2
+ name;
3
+ lastUpdatedTimestamp;
4
+ ttlSeconds;
5
+ targetGateway;
6
+ router;
7
+ constructor({ router, ttlSeconds = 5 * 60, // 5 minutes
8
+ }) {
9
+ this.router = router;
10
+ this.ttlSeconds = ttlSeconds;
11
+ }
12
+ async getTargetGateway() {
13
+ if (this.targetGateway === undefined ||
14
+ this.lastUpdatedTimestamp + this.ttlSeconds * 1000 < Date.now()) {
15
+ this.targetGateway = await this.router.getTargetGateway();
16
+ this.lastUpdatedTimestamp = Date.now();
17
+ }
18
+ return this.targetGateway;
19
+ }
20
+ }
21
+ // 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
@@ -0,0 +1,39 @@
1
+ import { strict as assert } from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { SimpleCacheRouter } from './simple-cache.js';
4
+ describe('SimpleCacheRouter', () => {
5
+ it('should cache gateway for TTL period even if underlying gateway changes', async () => {
6
+ const gateway1 = new URL('https://gateway1.net');
7
+ const gateway2 = new URL('https://gateway2.net');
8
+ let currentGateway = gateway1;
9
+ const mockRouter = {
10
+ name: 'mock',
11
+ getTargetGateway: async () => currentGateway,
12
+ };
13
+ const router = new SimpleCacheRouter({
14
+ router: mockRouter,
15
+ ttlSeconds: 300, // 5 minutes
16
+ });
17
+ // Get initial gateway which should be cached
18
+ const initial = await router.getTargetGateway();
19
+ assert.deepStrictEqual(initial, gateway1);
20
+ // Change the underlying gateway
21
+ currentGateway = gateway2;
22
+ // Should still return cached gateway1 for multiple calls
23
+ for (let i = 0; i < 5; i++) {
24
+ const result = await router.getTargetGateway();
25
+ assert.deepStrictEqual(result, gateway1);
26
+ }
27
+ // Advance time past TTL
28
+ const originalNow = Date.now;
29
+ try {
30
+ Date.now = () => originalNow() + 300 * 1000 + 1;
31
+ // Should now return the new gateway2
32
+ const result = await router.getTargetGateway();
33
+ assert.deepStrictEqual(result, gateway2);
34
+ }
35
+ finally {
36
+ Date.now = originalNow;
37
+ }
38
+ });
39
+ });
@@ -0,0 +1,168 @@
1
+ import { ARIO } from '../io.js';
2
+ import { ARIOGatewaysProvider } from './gateways.js';
3
+ import { RandomGatewayRouter } from './routers/random.js';
4
+ // known regexes for wayfinder urls
5
+ export const arnsRegex = /^[a-z0-9_-]{1,51}$/;
6
+ export const txIdRegex = /^[a-z0-9]{43}$/;
7
+ /**
8
+ * Core function to resolve a wayfinder url against a target gateway
9
+ * @param originalUrl - the wayfinder url to resolve
10
+ * @param targetGateway - the target gateway to resolve the url against
11
+ * @returns the resolved url that can be used to make a request
12
+ */
13
+ export const resolveWayfinderUrl = ({ originalUrl, targetGateway, }) => {
14
+ if (originalUrl.toString().startsWith('ar://')) {
15
+ const [, path] = originalUrl.toString().split('ar://');
16
+ // e.g. ar:///info should route to the info endpoint of the target gateway
17
+ if (path.startsWith('/')) {
18
+ return new URL(path.slice(1), targetGateway);
19
+ }
20
+ // TODO: this breaks 43 character named arns names - we should check a a local name cache list before resolving raw transaction ids
21
+ if (txIdRegex.test(path)) {
22
+ const [txId, ...rest] = path.split('/');
23
+ return new URL(`${txId}${rest.join('/')}`, targetGateway);
24
+ }
25
+ if (arnsRegex.test(path)) {
26
+ // TODO: tests to ensure arns names support query params and paths
27
+ const [name, ...rest] = path.split('/');
28
+ const targetGatewayUrl = new URL(targetGateway);
29
+ const arnsUrl = `${targetGatewayUrl.protocol}//${name}.${targetGatewayUrl.hostname}${targetGatewayUrl.port ? `:${targetGatewayUrl.port}` : ''}`;
30
+ return new URL(rest.join('/'), arnsUrl);
31
+ }
32
+ // TODO: support .eth addresses
33
+ // TODO: "gasless" routing via DNS TXT records (e.g. ar://gatewaypie.com -> TXT record lookup for TX ID and redirect to that gateway)
34
+ }
35
+ // return the original url if it's not a wayfinder url (allows you to use the wayfinder client with non-wayfinder urls)
36
+ return new URL(originalUrl);
37
+ };
38
+ /**
39
+ * Creates a wrapped http client that supports ar:// protocol
40
+ *
41
+ * This function leverages a Proxy to intercept calls to the http client
42
+ * and redirects them to the target gateway using the resolveUrl function url.
43
+ * It also supports the http client methods like get(), post(), put(), delete(), etc.
44
+ *
45
+ * Any URLs provided that are not wayfinder urls will be returned as is.
46
+ *
47
+ * @param httpClient - the http client to wrap (e.g. axios, fetch, got, etc.)
48
+ * @param resolveUrl - the function to construct the redirect url for ar:// requests
49
+ * @returns a wrapped http client that supports ar:// protocol
50
+ */
51
+ export const createWayfinderClient = ({ httpClient, resolveUrl, }) => {
52
+ const wayfinderRedirect = async (fn, rawArgs) => {
53
+ // TODO: handle if first arg is not a string (i.e. just return the result of the function call)
54
+ const [originalUrl, ...rest] = rawArgs;
55
+ // route the request to the target gateway
56
+ const redirectUrl = await resolveUrl({
57
+ originalUrl,
58
+ });
59
+ // make the request to the target gateway using the redirect url and http client
60
+ const response = await fn(redirectUrl.toString(), ...rest);
61
+ // TODO: if verifyDataHash is provided, verify the data hash before returning
62
+ return response;
63
+ };
64
+ return new Proxy(httpClient, {
65
+ // support direct calls: fetch('ar://…', options)
66
+ // axios() or got()
67
+ apply: (_target, _thisArg, argArray) => wayfinderRedirect(httpClient, argArray),
68
+ // support http clients that use methods like `got.get`, `got.post`, `axios.get`, etc. while still using the wayfinder redirect function
69
+ get: (target, prop, receiver) => {
70
+ const value = Reflect.get(target, prop, receiver);
71
+ if (typeof value === 'function') {
72
+ return (...inner) => wayfinderRedirect(value.bind(target), inner);
73
+ }
74
+ return value; // numbers, objects, symbols pass through untouched
75
+ },
76
+ });
77
+ };
78
+ /**
79
+ * The main class for the wayfinder
80
+ * @param router - the router to use for requests
81
+ * @param httpClient - the http client to use for requests
82
+ * @param blocklist - the blocklist of gateways to avoid
83
+ */
84
+ export class Wayfinder {
85
+ /**
86
+ * The router to use for requests
87
+ *
88
+ * @example
89
+ * const wayfinder = new Wayfinder({
90
+ * router: new RandomGatewayRouter({
91
+ * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
92
+ * }),
93
+ * });
94
+ */
95
+ router;
96
+ /**
97
+ * The http client to use for requests
98
+ *
99
+ * @example
100
+ * const wayfinder = new Wayfinder({
101
+ * router: new RandomGatewayRouter({
102
+ * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
103
+ * }),
104
+ * httpClient: axios,
105
+ * });
106
+ */
107
+ httpClient;
108
+ /**
109
+ * The function that resolves the redirect url for ar:// requests to a target gateway
110
+ *
111
+ * @example
112
+ * const wayfinder = new Wayfinder({
113
+ * router: new RandomGatewayRouter({
114
+ * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
115
+ * }),
116
+ * httpClient: axios,
117
+ * });
118
+ *
119
+ * const redirectUrl = await wayfinder.resolveUrl({ originalUrl: 'ar://example' });
120
+ */
121
+ resolveUrl;
122
+ /**
123
+ * A wrapped http client that supports ar:// protocol
124
+ *
125
+ * @example
126
+ * const { request: wayfind } = new Wayfinder({
127
+ * router: new RandomGatewayRouter({
128
+ * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
129
+ * }),
130
+ * httpClient: axios,
131
+ * });;
132
+ *
133
+ * const response = await wayfind('ar://example', {
134
+ * method: 'POST',
135
+ * data: {
136
+ * name: 'John Doe',
137
+ * },
138
+ * })
139
+ */
140
+ request;
141
+ // TODO: stats provider
142
+ // TODO: metricsProvider for otel/prom support
143
+ // TODO: private verificationSettings: {
144
+ // trustedGateways: URL[];
145
+ // method: 'local' | 'remote';
146
+ // };
147
+ constructor({
148
+ // TODO: consider changing router to routingStrategy or strategy
149
+ router = new RandomGatewayRouter({
150
+ // optionally use a cache gateways provider to reduce the number of requests to the contract
151
+ gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() }),
152
+ }), httpClient,
153
+ // TODO: add verifier interface that provides a verifyDataHash function
154
+ // TODO: stats provider
155
+ }) {
156
+ this.router = router;
157
+ this.httpClient = httpClient;
158
+ this.resolveUrl = async ({ originalUrl }) => resolveWayfinderUrl({
159
+ originalUrl,
160
+ targetGateway: await this.router.getTargetGateway(),
161
+ });
162
+ this.request = createWayfinderClient({
163
+ httpClient,
164
+ resolveUrl: this.resolveUrl,
165
+ // TODO: provide the verifyDataHash function from the verifier to the wayfinder client along with verificationSettings
166
+ });
167
+ }
168
+ }