@ar.io/sdk 3.10.0-alpha.5 → 3.10.0-alpha.6
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/bundles/web.bundle.min.js +60 -60
- package/lib/cjs/common/index.js +2 -0
- package/lib/cjs/common/wayfinder/gateways.js +75 -0
- package/lib/cjs/common/wayfinder/index.js +36 -0
- package/lib/cjs/common/wayfinder/routers/fixed.js +14 -0
- package/lib/cjs/common/wayfinder/routers/fixed.test.js +14 -0
- package/lib/cjs/common/wayfinder/routers/priority.js +38 -0
- package/lib/cjs/common/wayfinder/routers/priority.test.js +155 -0
- package/lib/cjs/common/wayfinder/routers/random.js +23 -0
- package/lib/cjs/common/wayfinder/routers/random.test.js +104 -0
- package/lib/cjs/common/wayfinder/routers/simple-cache.js +25 -0
- package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +41 -0
- package/lib/cjs/common/wayfinder/wayfinder.js +194 -0
- package/lib/cjs/common/wayfinder/wayfinder.test.js +254 -0
- package/lib/cjs/types/index.js +1 -0
- package/lib/cjs/types/wayfinder.js +2 -0
- package/lib/cjs/version.js +1 -1
- package/lib/esm/common/index.js +2 -0
- package/lib/esm/common/wayfinder/gateways.js +69 -0
- package/lib/esm/common/wayfinder/index.js +20 -0
- package/lib/esm/common/wayfinder/routers/fixed.js +10 -0
- package/lib/esm/common/wayfinder/routers/fixed.test.js +12 -0
- package/lib/esm/common/wayfinder/routers/priority.js +34 -0
- package/lib/esm/common/wayfinder/routers/priority.test.js +153 -0
- package/lib/esm/common/wayfinder/routers/random.js +19 -0
- package/lib/esm/common/wayfinder/routers/random.test.js +102 -0
- package/lib/esm/common/wayfinder/routers/simple-cache.js +21 -0
- package/lib/esm/common/wayfinder/routers/simple-cache.test.js +39 -0
- package/lib/esm/common/wayfinder/wayfinder.js +187 -0
- package/lib/esm/common/wayfinder/wayfinder.test.js +249 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/wayfinder.js +1 -0
- package/lib/esm/version.js +1 -1
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/io.d.ts +0 -1
- package/lib/types/common/wayfinder/gateways.d.ts +44 -0
- package/lib/types/common/wayfinder/index.d.ts +19 -0
- package/lib/types/common/wayfinder/routers/fixed.d.ts +25 -0
- package/lib/types/common/wayfinder/routers/fixed.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/priority.d.ts +34 -0
- package/lib/types/common/wayfinder/routers/priority.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/random.d.ts +28 -0
- package/lib/types/common/wayfinder/routers/random.test.d.ts +1 -0
- package/lib/types/common/wayfinder/routers/simple-cache.d.ts +29 -0
- package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +1 -0
- package/lib/types/common/wayfinder/wayfinder.d.ts +106 -0
- package/lib/types/common/wayfinder/wayfinder.test.d.ts +1 -0
- package/lib/types/types/index.d.ts +1 -0
- package/lib/types/types/io.d.ts +1 -0
- package/lib/types/types/token.d.ts +1 -0
- package/lib/types/types/wayfinder.d.ts +20 -0
- package/lib/types/utils/base64.d.ts +1 -0
- package/lib/types/version.d.ts +1 -1
- package/package.json +4 -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 '../wayfinder.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,187 @@
|
|
|
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 { webcrypto } from 'crypto';
|
|
17
|
+
import { ARIO } from '../io.js';
|
|
18
|
+
import { ARIOGatewaysProvider } from './gateways.js';
|
|
19
|
+
import { RandomGatewayRouter } from './routers/random.js';
|
|
20
|
+
// known regexes for wayfinder urls
|
|
21
|
+
export const arnsRegex = /^[a-z0-9_-]{1,51}$/;
|
|
22
|
+
export const txIdRegex = /^[a-z0-9]{43}$/;
|
|
23
|
+
/**
|
|
24
|
+
* Cryptographically secure helper for randomness, does not support seeding
|
|
25
|
+
* @param min - the minimum value
|
|
26
|
+
* @param max - the maximum value
|
|
27
|
+
* @returns a random integer between min and max
|
|
28
|
+
*/
|
|
29
|
+
export const randomInt = (min, max) => {
|
|
30
|
+
const [rand] = webcrypto.getRandomValues(new Uint32Array(1));
|
|
31
|
+
return min + (rand % (max - min));
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Core function to resolve a wayfinder url against a target gateway
|
|
35
|
+
* @param originalUrl - the wayfinder url to resolve
|
|
36
|
+
* @param targetGateway - the target gateway to resolve the url against
|
|
37
|
+
* @returns the resolved url that can be used to make a request
|
|
38
|
+
*/
|
|
39
|
+
export const resolveWayfinderUrl = ({ originalUrl, targetGateway, }) => {
|
|
40
|
+
if (originalUrl.toString().startsWith('ar://')) {
|
|
41
|
+
const [, path] = originalUrl.toString().split('ar://');
|
|
42
|
+
// e.g. ar:///info should route to the info endpoint of the target gateway
|
|
43
|
+
if (path.startsWith('/')) {
|
|
44
|
+
return new URL(path.slice(1), targetGateway);
|
|
45
|
+
}
|
|
46
|
+
// TODO: this breaks 43 character named arns names - we should check a a local name cache list before resolving raw transaction ids
|
|
47
|
+
if (txIdRegex.test(path)) {
|
|
48
|
+
const [txId, ...rest] = path.split('/');
|
|
49
|
+
return new URL(`${txId}${rest.join('/')}`, targetGateway);
|
|
50
|
+
}
|
|
51
|
+
if (arnsRegex.test(path)) {
|
|
52
|
+
// TODO: tests to ensure arns names support query params and paths
|
|
53
|
+
const [name, ...rest] = path.split('/');
|
|
54
|
+
const targetGatewayUrl = new URL(targetGateway);
|
|
55
|
+
const arnsUrl = `${targetGatewayUrl.protocol}//${name}.${targetGatewayUrl.hostname}${targetGatewayUrl.port ? `:${targetGatewayUrl.port}` : ''}`;
|
|
56
|
+
return new URL(rest.join('/'), arnsUrl);
|
|
57
|
+
}
|
|
58
|
+
// TODO: support .eth addresses
|
|
59
|
+
// TODO: "gasless" routing via DNS TXT records (e.g. ar://gatewaypie.com -> TXT record lookup for TX ID and redirect to that gateway)
|
|
60
|
+
}
|
|
61
|
+
// return the original url if it's not a wayfinder url (allows you to use the wayfinder client with non-wayfinder urls)
|
|
62
|
+
return new URL(originalUrl);
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Creates a wrapped http client that supports ar:// protocol
|
|
66
|
+
*
|
|
67
|
+
* This function leverages a Proxy to intercept calls to the http client
|
|
68
|
+
* and redirects them to the target gateway using the resolveUrl function url.
|
|
69
|
+
* It also supports the http client methods like get(), post(), put(), delete(), etc.
|
|
70
|
+
*
|
|
71
|
+
* Any URLs provided that are not wayfinder urls will be returned as is.
|
|
72
|
+
*
|
|
73
|
+
* @param httpClient - the http client to wrap (e.g. axios, fetch, got, etc.)
|
|
74
|
+
* @param resolveUrl - the function to construct the redirect url for ar:// requests
|
|
75
|
+
* @returns a wrapped http client that supports ar:// protocol
|
|
76
|
+
*/
|
|
77
|
+
export const createWayfinderClient = ({ httpClient, resolveUrl, }) => {
|
|
78
|
+
const wayfinderRedirect = async (fn, rawArgs) => {
|
|
79
|
+
// TODO: handle if first arg is not a string (i.e. just return the result of the function call)
|
|
80
|
+
const [originalUrl, ...rest] = rawArgs;
|
|
81
|
+
// route the request to the target gateway
|
|
82
|
+
const redirectUrl = await resolveUrl({
|
|
83
|
+
originalUrl,
|
|
84
|
+
});
|
|
85
|
+
// make the request to the target gateway using the redirect url and http client
|
|
86
|
+
const response = await fn(redirectUrl.toString(), ...rest);
|
|
87
|
+
// TODO: if verifyDataHash is provided, verify the data hash before returning
|
|
88
|
+
return response;
|
|
89
|
+
};
|
|
90
|
+
return new Proxy(httpClient, {
|
|
91
|
+
// support direct calls: fetch('ar://…', options)
|
|
92
|
+
// axios() or got()
|
|
93
|
+
apply: (_target, _thisArg, argArray) => wayfinderRedirect(httpClient, argArray),
|
|
94
|
+
// support http clients that use methods like `got.get`, `got.post`, `axios.get`, etc. while still using the wayfinder redirect function
|
|
95
|
+
get: (target, prop, receiver) => {
|
|
96
|
+
const value = Reflect.get(target, prop, receiver);
|
|
97
|
+
if (typeof value === 'function') {
|
|
98
|
+
return (...inner) => wayfinderRedirect(value.bind(target), inner);
|
|
99
|
+
}
|
|
100
|
+
return value; // numbers, objects, symbols pass through untouched
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* The main class for the wayfinder
|
|
106
|
+
* @param router - the router to use for requests
|
|
107
|
+
* @param httpClient - the http client to use for requests
|
|
108
|
+
* @param blocklist - the blocklist of gateways to avoid
|
|
109
|
+
*/
|
|
110
|
+
export class Wayfinder {
|
|
111
|
+
/**
|
|
112
|
+
* The router to use for requests
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* const wayfinder = new Wayfinder({
|
|
116
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
117
|
+
* });
|
|
118
|
+
*/
|
|
119
|
+
router;
|
|
120
|
+
/**
|
|
121
|
+
* The http client to use for requests
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* const wayfinder = new Wayfinder({
|
|
125
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
126
|
+
* httpClient: axios,
|
|
127
|
+
* });
|
|
128
|
+
*/
|
|
129
|
+
httpClient;
|
|
130
|
+
/**
|
|
131
|
+
* The function that resolves the redirect url for ar:// requests to a target gateway
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* const wayfinder = new Wayfinder({
|
|
135
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
136
|
+
* httpClient: axios,
|
|
137
|
+
* });
|
|
138
|
+
*
|
|
139
|
+
* const redirectUrl = await wayfinder.resolveUrl({ originalUrl: 'ar://example' });
|
|
140
|
+
*/
|
|
141
|
+
resolveUrl;
|
|
142
|
+
/**
|
|
143
|
+
* A wrapped http client that supports ar:// protocol
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* const { request: wayfind } = new Wayfinder({
|
|
147
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
148
|
+
* httpClient: axios,
|
|
149
|
+
* });;
|
|
150
|
+
*
|
|
151
|
+
* TODO: consider a top level function that supports wayfinder routing under the hood
|
|
152
|
+
* const response = await wayfind('ar://', {
|
|
153
|
+
* method: 'POST',
|
|
154
|
+
* data: {
|
|
155
|
+
* name: 'John Doe',
|
|
156
|
+
* },
|
|
157
|
+
* })
|
|
158
|
+
*/
|
|
159
|
+
request;
|
|
160
|
+
// TODO: stats provider
|
|
161
|
+
// TODO: metricsProvider for otel/prom support
|
|
162
|
+
// TODO: private verificationSettings: {
|
|
163
|
+
// trustedGateways: URL[];
|
|
164
|
+
// method: 'local' | 'remote';
|
|
165
|
+
// };
|
|
166
|
+
constructor({
|
|
167
|
+
// TODO: consider changing router to routingStrategy or strategy
|
|
168
|
+
router = new RandomGatewayRouter({
|
|
169
|
+
// optionally use a cache gateways provider to reduce the number of requests to the contract
|
|
170
|
+
gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() }),
|
|
171
|
+
}), httpClient,
|
|
172
|
+
// TODO: add verifier interface that provides a verifyDataHash function
|
|
173
|
+
// TODO: stats provider
|
|
174
|
+
}) {
|
|
175
|
+
this.router = router;
|
|
176
|
+
this.httpClient = httpClient;
|
|
177
|
+
this.resolveUrl = async ({ originalUrl }) => resolveWayfinderUrl({
|
|
178
|
+
originalUrl,
|
|
179
|
+
targetGateway: await this.router.getTargetGateway(),
|
|
180
|
+
});
|
|
181
|
+
this.request = createWayfinderClient({
|
|
182
|
+
httpClient,
|
|
183
|
+
resolveUrl: this.resolveUrl,
|
|
184
|
+
// TODO: provide the verifyDataHash function from the verifier to the wayfinder client along with verificationSettings
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|