@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,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Wayfinder = exports.createWayfinderClient = exports.resolveWayfinderUrl = exports.randomInt = exports.txIdRegex = exports.arnsRegex = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const crypto_1 = require("crypto");
|
|
20
|
+
const io_js_1 = require("../io.js");
|
|
21
|
+
const gateways_js_1 = require("./gateways.js");
|
|
22
|
+
const random_js_1 = require("./routers/random.js");
|
|
23
|
+
// known regexes for wayfinder urls
|
|
24
|
+
exports.arnsRegex = /^[a-z0-9_-]{1,51}$/;
|
|
25
|
+
exports.txIdRegex = /^[a-z0-9]{43}$/;
|
|
26
|
+
/**
|
|
27
|
+
* Cryptographically secure helper for randomness, does not support seeding
|
|
28
|
+
* @param min - the minimum value
|
|
29
|
+
* @param max - the maximum value
|
|
30
|
+
* @returns a random integer between min and max
|
|
31
|
+
*/
|
|
32
|
+
const randomInt = (min, max) => {
|
|
33
|
+
const [rand] = crypto_1.webcrypto.getRandomValues(new Uint32Array(1));
|
|
34
|
+
return min + (rand % (max - min));
|
|
35
|
+
};
|
|
36
|
+
exports.randomInt = randomInt;
|
|
37
|
+
/**
|
|
38
|
+
* Core function to resolve a wayfinder url against a target gateway
|
|
39
|
+
* @param originalUrl - the wayfinder url to resolve
|
|
40
|
+
* @param targetGateway - the target gateway to resolve the url against
|
|
41
|
+
* @returns the resolved url that can be used to make a request
|
|
42
|
+
*/
|
|
43
|
+
const resolveWayfinderUrl = ({ originalUrl, targetGateway, }) => {
|
|
44
|
+
if (originalUrl.toString().startsWith('ar://')) {
|
|
45
|
+
const [, path] = originalUrl.toString().split('ar://');
|
|
46
|
+
// e.g. ar:///info should route to the info endpoint of the target gateway
|
|
47
|
+
if (path.startsWith('/')) {
|
|
48
|
+
return new URL(path.slice(1), targetGateway);
|
|
49
|
+
}
|
|
50
|
+
// TODO: this breaks 43 character named arns names - we should check a a local name cache list before resolving raw transaction ids
|
|
51
|
+
if (exports.txIdRegex.test(path)) {
|
|
52
|
+
const [txId, ...rest] = path.split('/');
|
|
53
|
+
return new URL(`${txId}${rest.join('/')}`, targetGateway);
|
|
54
|
+
}
|
|
55
|
+
if (exports.arnsRegex.test(path)) {
|
|
56
|
+
// TODO: tests to ensure arns names support query params and paths
|
|
57
|
+
const [name, ...rest] = path.split('/');
|
|
58
|
+
const targetGatewayUrl = new URL(targetGateway);
|
|
59
|
+
const arnsUrl = `${targetGatewayUrl.protocol}//${name}.${targetGatewayUrl.hostname}${targetGatewayUrl.port ? `:${targetGatewayUrl.port}` : ''}`;
|
|
60
|
+
return new URL(rest.join('/'), arnsUrl);
|
|
61
|
+
}
|
|
62
|
+
// TODO: support .eth addresses
|
|
63
|
+
// TODO: "gasless" routing via DNS TXT records (e.g. ar://gatewaypie.com -> TXT record lookup for TX ID and redirect to that gateway)
|
|
64
|
+
}
|
|
65
|
+
// return the original url if it's not a wayfinder url (allows you to use the wayfinder client with non-wayfinder urls)
|
|
66
|
+
return new URL(originalUrl);
|
|
67
|
+
};
|
|
68
|
+
exports.resolveWayfinderUrl = resolveWayfinderUrl;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a wrapped http client that supports ar:// protocol
|
|
71
|
+
*
|
|
72
|
+
* This function leverages a Proxy to intercept calls to the http client
|
|
73
|
+
* and redirects them to the target gateway using the resolveUrl function url.
|
|
74
|
+
* It also supports the http client methods like get(), post(), put(), delete(), etc.
|
|
75
|
+
*
|
|
76
|
+
* Any URLs provided that are not wayfinder urls will be returned as is.
|
|
77
|
+
*
|
|
78
|
+
* @param httpClient - the http client to wrap (e.g. axios, fetch, got, etc.)
|
|
79
|
+
* @param resolveUrl - the function to construct the redirect url for ar:// requests
|
|
80
|
+
* @returns a wrapped http client that supports ar:// protocol
|
|
81
|
+
*/
|
|
82
|
+
const createWayfinderClient = ({ httpClient, resolveUrl, }) => {
|
|
83
|
+
const wayfinderRedirect = async (fn, rawArgs) => {
|
|
84
|
+
// TODO: handle if first arg is not a string (i.e. just return the result of the function call)
|
|
85
|
+
const [originalUrl, ...rest] = rawArgs;
|
|
86
|
+
// route the request to the target gateway
|
|
87
|
+
const redirectUrl = await resolveUrl({
|
|
88
|
+
originalUrl,
|
|
89
|
+
});
|
|
90
|
+
// make the request to the target gateway using the redirect url and http client
|
|
91
|
+
const response = await fn(redirectUrl.toString(), ...rest);
|
|
92
|
+
// TODO: if verifyDataHash is provided, verify the data hash before returning
|
|
93
|
+
return response;
|
|
94
|
+
};
|
|
95
|
+
return new Proxy(httpClient, {
|
|
96
|
+
// support direct calls: fetch('ar://…', options)
|
|
97
|
+
// axios() or got()
|
|
98
|
+
apply: (_target, _thisArg, argArray) => wayfinderRedirect(httpClient, argArray),
|
|
99
|
+
// support http clients that use methods like `got.get`, `got.post`, `axios.get`, etc. while still using the wayfinder redirect function
|
|
100
|
+
get: (target, prop, receiver) => {
|
|
101
|
+
const value = Reflect.get(target, prop, receiver);
|
|
102
|
+
if (typeof value === 'function') {
|
|
103
|
+
return (...inner) => wayfinderRedirect(value.bind(target), inner);
|
|
104
|
+
}
|
|
105
|
+
return value; // numbers, objects, symbols pass through untouched
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
exports.createWayfinderClient = createWayfinderClient;
|
|
110
|
+
/**
|
|
111
|
+
* The main class for the wayfinder
|
|
112
|
+
* @param router - the router to use for requests
|
|
113
|
+
* @param httpClient - the http client to use for requests
|
|
114
|
+
* @param blocklist - the blocklist of gateways to avoid
|
|
115
|
+
*/
|
|
116
|
+
class Wayfinder {
|
|
117
|
+
/**
|
|
118
|
+
* The router to use for requests
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* const wayfinder = new Wayfinder({
|
|
122
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
123
|
+
* });
|
|
124
|
+
*/
|
|
125
|
+
router;
|
|
126
|
+
/**
|
|
127
|
+
* The http client to use for requests
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const wayfinder = new Wayfinder({
|
|
131
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
132
|
+
* httpClient: axios,
|
|
133
|
+
* });
|
|
134
|
+
*/
|
|
135
|
+
httpClient;
|
|
136
|
+
/**
|
|
137
|
+
* The function that resolves the redirect url for ar:// requests to a target gateway
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* const wayfinder = new Wayfinder({
|
|
141
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
142
|
+
* httpClient: axios,
|
|
143
|
+
* });
|
|
144
|
+
*
|
|
145
|
+
* const redirectUrl = await wayfinder.resolveUrl({ originalUrl: 'ar://example' });
|
|
146
|
+
*/
|
|
147
|
+
resolveUrl;
|
|
148
|
+
/**
|
|
149
|
+
* A wrapped http client that supports ar:// protocol
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* const { request: wayfind } = new Wayfinder({
|
|
153
|
+
* router: new RandomGatewayRouter({ ario: ARIO.mainnet() }),
|
|
154
|
+
* httpClient: axios,
|
|
155
|
+
* });;
|
|
156
|
+
*
|
|
157
|
+
* TODO: consider a top level function that supports wayfinder routing under the hood
|
|
158
|
+
* const response = await wayfind('ar://', {
|
|
159
|
+
* method: 'POST',
|
|
160
|
+
* data: {
|
|
161
|
+
* name: 'John Doe',
|
|
162
|
+
* },
|
|
163
|
+
* })
|
|
164
|
+
*/
|
|
165
|
+
request;
|
|
166
|
+
// TODO: stats provider
|
|
167
|
+
// TODO: metricsProvider for otel/prom support
|
|
168
|
+
// TODO: private verificationSettings: {
|
|
169
|
+
// trustedGateways: URL[];
|
|
170
|
+
// method: 'local' | 'remote';
|
|
171
|
+
// };
|
|
172
|
+
constructor({
|
|
173
|
+
// TODO: consider changing router to routingStrategy or strategy
|
|
174
|
+
router = new random_js_1.RandomGatewayRouter({
|
|
175
|
+
// optionally use a cache gateways provider to reduce the number of requests to the contract
|
|
176
|
+
gatewaysProvider: new gateways_js_1.ARIOGatewaysProvider({ ario: io_js_1.ARIO.mainnet() }),
|
|
177
|
+
}), httpClient,
|
|
178
|
+
// TODO: add verifier interface that provides a verifyDataHash function
|
|
179
|
+
// TODO: stats provider
|
|
180
|
+
}) {
|
|
181
|
+
this.router = router;
|
|
182
|
+
this.httpClient = httpClient;
|
|
183
|
+
this.resolveUrl = async ({ originalUrl }) => (0, exports.resolveWayfinderUrl)({
|
|
184
|
+
originalUrl,
|
|
185
|
+
targetGateway: await this.router.getTargetGateway(),
|
|
186
|
+
});
|
|
187
|
+
this.request = (0, exports.createWayfinderClient)({
|
|
188
|
+
httpClient,
|
|
189
|
+
resolveUrl: this.resolveUrl,
|
|
190
|
+
// TODO: provide the verifyDataHash function from the verifier to the wayfinder client along with verificationSettings
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.Wayfinder = Wayfinder;
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const axios_1 = __importDefault(require("axios"));
|
|
7
|
+
const got_1 = __importDefault(require("got"));
|
|
8
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
9
|
+
const consumers_1 = require("node:stream/consumers");
|
|
10
|
+
const node_test_1 = require("node:test");
|
|
11
|
+
const random_js_1 = require("./routers/random.js");
|
|
12
|
+
const wayfinder_js_1 = require("./wayfinder.js");
|
|
13
|
+
const stubbedGateway = {
|
|
14
|
+
status: 'joined',
|
|
15
|
+
gatewayAddress: 'arweave',
|
|
16
|
+
operatorStake: 1000,
|
|
17
|
+
totalDelegatedStake: 1000,
|
|
18
|
+
startTimestamp: 1000,
|
|
19
|
+
settings: {
|
|
20
|
+
protocol: 'https',
|
|
21
|
+
fqdn: 'arweave.net',
|
|
22
|
+
port: 443,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
const stubbedGatewaysProvider = {
|
|
26
|
+
getGateways: async () => [stubbedGateway],
|
|
27
|
+
};
|
|
28
|
+
(0, node_test_1.describe)('Wayfinder', () => {
|
|
29
|
+
(0, node_test_1.describe)('http wrapper', () => {
|
|
30
|
+
(0, node_test_1.describe)('fetch', () => {
|
|
31
|
+
let wayfinder;
|
|
32
|
+
(0, node_test_1.before)(() => {
|
|
33
|
+
wayfinder = new wayfinder_js_1.Wayfinder({
|
|
34
|
+
httpClient: fetch,
|
|
35
|
+
router: new random_js_1.RandomGatewayRouter({
|
|
36
|
+
gatewaysProvider: stubbedGatewaysProvider,
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
(0, node_test_1.it)('should fetch the data using the selected gateway', async () => {
|
|
41
|
+
const nativeFetch = await fetch('https://ao.arweave.net');
|
|
42
|
+
const response = await wayfinder.request('ar://ao');
|
|
43
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
44
|
+
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
45
|
+
// assert the arns headers are the same
|
|
46
|
+
const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
|
|
47
|
+
const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
|
|
48
|
+
node_assert_1.default.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
|
|
49
|
+
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
50
|
+
});
|
|
51
|
+
(0, node_test_1.it)('should route a non-ar:// url as a normal fetch', async () => {
|
|
52
|
+
const [nativeFetch, response] = await Promise.all([
|
|
53
|
+
fetch('https://arweave.net/', {
|
|
54
|
+
method: 'HEAD',
|
|
55
|
+
}),
|
|
56
|
+
wayfinder.request('https://arweave.net/', {
|
|
57
|
+
method: 'HEAD',
|
|
58
|
+
}),
|
|
59
|
+
]);
|
|
60
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
61
|
+
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
62
|
+
// TODO: ensure the headers are the same excluding unique headers
|
|
63
|
+
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
64
|
+
});
|
|
65
|
+
for (const api of ['/info', '/metrics', '/block/current']) {
|
|
66
|
+
(0, node_test_1.it)(`supports native arweave node apis ${api}`, async () => {
|
|
67
|
+
const [nativeFetch, response] = await Promise.all([
|
|
68
|
+
fetch(`https://arweave.net${api}`),
|
|
69
|
+
wayfinder.request(`ar:///${api}`),
|
|
70
|
+
]);
|
|
71
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
72
|
+
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
73
|
+
// TODO: ensure the headers are the same excluding unique headers
|
|
74
|
+
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
|
|
78
|
+
(0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
|
|
79
|
+
const [nativeFetch, response] = await Promise.all([
|
|
80
|
+
fetch(`https://arweave.net${api}`),
|
|
81
|
+
wayfinder.request(`ar:///${api}`),
|
|
82
|
+
]);
|
|
83
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
84
|
+
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
85
|
+
// TODO: ensure the headers are the same excluding unique headers
|
|
86
|
+
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
(0, node_test_1.it)('supports a post request to graphql', async () => {
|
|
90
|
+
const response = await wayfinder.request('ar:///graphql', {
|
|
91
|
+
method: 'POST',
|
|
92
|
+
headers: {
|
|
93
|
+
'Content-Type': 'application/json',
|
|
94
|
+
},
|
|
95
|
+
body: JSON.stringify({
|
|
96
|
+
query: `
|
|
97
|
+
query {
|
|
98
|
+
transactions(
|
|
99
|
+
ids: ["xf958qhCNGfDme1FtoiD6DtMfDENDbtxZpjOM_1tsMM"]
|
|
100
|
+
) {
|
|
101
|
+
edges {
|
|
102
|
+
cursor
|
|
103
|
+
node {
|
|
104
|
+
id
|
|
105
|
+
tags {
|
|
106
|
+
name
|
|
107
|
+
value
|
|
108
|
+
}
|
|
109
|
+
block {
|
|
110
|
+
height
|
|
111
|
+
timestamp
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
pageInfo {
|
|
116
|
+
hasNextPage
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
`,
|
|
121
|
+
}),
|
|
122
|
+
});
|
|
123
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
124
|
+
});
|
|
125
|
+
(0, node_test_1.it)('returns the error from the target gateway if the route is not found', async () => {
|
|
126
|
+
const [nativeFetch, response] = await Promise.all([
|
|
127
|
+
fetch('https://arweave.net/not-found'),
|
|
128
|
+
wayfinder.request('https://arweave.net/not-found'),
|
|
129
|
+
]);
|
|
130
|
+
node_assert_1.default.strictEqual(response.status, nativeFetch.status);
|
|
131
|
+
node_assert_1.default.strictEqual(response.statusText, nativeFetch.statusText);
|
|
132
|
+
node_assert_1.default.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
(0, node_test_1.describe)('axios', () => {
|
|
136
|
+
let wayfinder;
|
|
137
|
+
(0, node_test_1.before)(() => {
|
|
138
|
+
wayfinder = new wayfinder_js_1.Wayfinder({
|
|
139
|
+
httpClient: axios_1.default,
|
|
140
|
+
router: new random_js_1.RandomGatewayRouter({
|
|
141
|
+
gatewaysProvider: stubbedGatewaysProvider,
|
|
142
|
+
}),
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
(0, node_test_1.it)('should fetch the data using axios default function against the target gateway', async () => {
|
|
146
|
+
const [nativeAxios, response] = await Promise.all([
|
|
147
|
+
(0, axios_1.default)('https://ao.arweave.net'),
|
|
148
|
+
wayfinder.request('ar://ao'),
|
|
149
|
+
]);
|
|
150
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
151
|
+
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
152
|
+
// assert the arns headers are the same
|
|
153
|
+
const arnsHeaders = Object.entries(response.headers)
|
|
154
|
+
.sort()
|
|
155
|
+
.filter(([key]) => key.startsWith('x-arns-'));
|
|
156
|
+
const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
|
|
157
|
+
node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
|
|
158
|
+
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
159
|
+
});
|
|
160
|
+
(0, node_test_1.it)('should fetch the data using the axios.get method against the target gateway', async () => {
|
|
161
|
+
const [nativeAxios, response] = await Promise.all([
|
|
162
|
+
axios_1.default.get('https://ao.arweave.net'),
|
|
163
|
+
wayfinder.request.get('ar://ao'),
|
|
164
|
+
]);
|
|
165
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
166
|
+
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
167
|
+
// assert the arns headers are the same
|
|
168
|
+
const arnsHeaders = Object.entries(response.headers)
|
|
169
|
+
.sort()
|
|
170
|
+
.filter(([key]) => key.startsWith('x-arns-'));
|
|
171
|
+
const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
|
|
172
|
+
node_assert_1.default.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
|
|
173
|
+
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
174
|
+
});
|
|
175
|
+
(0, node_test_1.it)('should route a non-ar:// url as a normal axios request', async () => {
|
|
176
|
+
const [nativeAxios, response] = await Promise.all([
|
|
177
|
+
(0, axios_1.default)('https://arweave.net/'),
|
|
178
|
+
wayfinder.request('https://arweave.net/'),
|
|
179
|
+
]);
|
|
180
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
181
|
+
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
182
|
+
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
183
|
+
// TODO: ensure the headers are the same excluding unique headers
|
|
184
|
+
});
|
|
185
|
+
for (const api of ['/info', '/metrics', '/block/current']) {
|
|
186
|
+
(0, node_test_1.it)(`supports native arweave node apis ${api}`, async () => {
|
|
187
|
+
const [nativeAxios, response] = await Promise.all([
|
|
188
|
+
(0, axios_1.default)(`https://arweave.net${api}`),
|
|
189
|
+
wayfinder.request(`ar:///${api}`),
|
|
190
|
+
]);
|
|
191
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
192
|
+
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
193
|
+
// TODO: ensure the headers are the same excluding unique headers
|
|
194
|
+
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
|
|
198
|
+
(0, node_test_1.it)(`supports native ario node gateway apis ${api}`, async () => {
|
|
199
|
+
const [nativeAxios, response] = await Promise.all([
|
|
200
|
+
(0, axios_1.default)(`https://arweave.net${api}`),
|
|
201
|
+
wayfinder.request(`ar:///${api}`),
|
|
202
|
+
]);
|
|
203
|
+
node_assert_1.default.strictEqual(response.status, 200);
|
|
204
|
+
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
205
|
+
// TODO: ensure the headers are the same excluding unique headers
|
|
206
|
+
node_assert_1.default.deepStrictEqual(response.data, nativeAxios.data);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
(0, node_test_1.it)('should return the error from the target gateway if the route is not found', async () => {
|
|
210
|
+
const axiosInstance = axios_1.default.create({
|
|
211
|
+
validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
|
|
212
|
+
});
|
|
213
|
+
const wayfinder = new wayfinder_js_1.Wayfinder({
|
|
214
|
+
httpClient: axiosInstance,
|
|
215
|
+
router: new random_js_1.RandomGatewayRouter({
|
|
216
|
+
gatewaysProvider: stubbedGatewaysProvider,
|
|
217
|
+
}),
|
|
218
|
+
});
|
|
219
|
+
const [nativeAxios, response] = await Promise.all([
|
|
220
|
+
axiosInstance('https://arweave.net/not-found'),
|
|
221
|
+
wayfinder.request('https://arweave.net/not-found'),
|
|
222
|
+
]);
|
|
223
|
+
node_assert_1.default.strictEqual(response.status, nativeAxios.status);
|
|
224
|
+
node_assert_1.default.strictEqual(response.data, nativeAxios.data);
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
(0, node_test_1.describe)('got', () => {
|
|
229
|
+
let wayfinder;
|
|
230
|
+
(0, node_test_1.before)(() => {
|
|
231
|
+
wayfinder = new wayfinder_js_1.Wayfinder({
|
|
232
|
+
httpClient: got_1.default,
|
|
233
|
+
router: new random_js_1.RandomGatewayRouter({
|
|
234
|
+
gatewaysProvider: stubbedGatewaysProvider,
|
|
235
|
+
}),
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
(0, node_test_1.it)('should fetch the data using the got default function against the target gateway', async () => {
|
|
239
|
+
const [nativeGot, response] = await Promise.all([
|
|
240
|
+
(0, got_1.default)('https://ao.arweave.net'),
|
|
241
|
+
wayfinder.request('ar://ao'),
|
|
242
|
+
]);
|
|
243
|
+
node_assert_1.default.strictEqual(response.statusCode, 200);
|
|
244
|
+
node_assert_1.default.strictEqual(response.statusCode, nativeGot.statusCode);
|
|
245
|
+
node_assert_1.default.deepStrictEqual(response.body, nativeGot.body);
|
|
246
|
+
});
|
|
247
|
+
(0, node_test_1.it)('should stream the data using got.stream against the selected target gateway', async () => {
|
|
248
|
+
const nativeBuffer = await (0, consumers_1.buffer)(await got_1.default.stream('https://ao.arweave.net', { decompress: false }));
|
|
249
|
+
const wayfinderBuffer = await (0, consumers_1.buffer)(await wayfinder.request.stream('ar://ao', { decompress: false }));
|
|
250
|
+
node_assert_1.default.deepStrictEqual(wayfinderBuffer, nativeBuffer);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
// TODO: streaming support using got.stream and got()
|
|
254
|
+
});
|
package/lib/cjs/types/index.js
CHANGED
package/lib/cjs/version.js
CHANGED
package/lib/esm/common/index.js
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export class ARIOGatewaysProvider {
|
|
2
|
+
ario;
|
|
3
|
+
constructor({ ario }) {
|
|
4
|
+
this.ario = ario;
|
|
5
|
+
}
|
|
6
|
+
async getGateways() {
|
|
7
|
+
let cursor;
|
|
8
|
+
let attempts = 0;
|
|
9
|
+
const gateways = [];
|
|
10
|
+
do {
|
|
11
|
+
try {
|
|
12
|
+
const { items: newGateways, nextCursor } = await this.ario.getGateways({
|
|
13
|
+
limit: 1000,
|
|
14
|
+
cursor,
|
|
15
|
+
});
|
|
16
|
+
gateways.push(...newGateways);
|
|
17
|
+
cursor = nextCursor;
|
|
18
|
+
attempts = 0; // reset attempts if we get a new cursor
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error('Error fetching gateways', {
|
|
22
|
+
cursor,
|
|
23
|
+
attempts,
|
|
24
|
+
error,
|
|
25
|
+
});
|
|
26
|
+
attempts++;
|
|
27
|
+
}
|
|
28
|
+
} while (cursor !== undefined && attempts < 3);
|
|
29
|
+
// filter out any gateways that are not joined
|
|
30
|
+
return gateways.filter((g) => g.status === 'joined');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class StaticGatewaysProvider {
|
|
34
|
+
gateways;
|
|
35
|
+
constructor({ gateways }) {
|
|
36
|
+
this.gateways = gateways;
|
|
37
|
+
}
|
|
38
|
+
async getGateways() {
|
|
39
|
+
return this.gateways;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class SimpleCacheGatewaysProvider {
|
|
43
|
+
gatewaysProvider;
|
|
44
|
+
ttlSeconds;
|
|
45
|
+
lastUpdated;
|
|
46
|
+
gatewaysCache;
|
|
47
|
+
constructor({ gatewaysProvider, ttlSeconds = 5 * 60, // 5 minutes
|
|
48
|
+
}) {
|
|
49
|
+
this.gatewaysCache = [];
|
|
50
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
51
|
+
this.ttlSeconds = ttlSeconds;
|
|
52
|
+
}
|
|
53
|
+
async getGateways() {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
if (this.gatewaysCache.length === 0 ||
|
|
56
|
+
now - this.lastUpdated > this.ttlSeconds * 1000) {
|
|
57
|
+
try {
|
|
58
|
+
// preserve the cache if the fetch fails
|
|
59
|
+
const allGateways = await this.gatewaysProvider.getGateways();
|
|
60
|
+
this.gatewaysCache = allGateways.filter((g) => g.status === 'joined');
|
|
61
|
+
this.lastUpdated = now;
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.error('Error fetching gateways', error);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return this.gatewaysCache;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
export * from './wayfinder.js';
|
|
17
|
+
// routers
|
|
18
|
+
export * from './routers/random.js';
|
|
19
|
+
export * from './routers/priority.js';
|
|
20
|
+
export * from './routers/fixed.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { strict as assert } from 'node:assert';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
import { FixedGatewayRouter } from './fixed.js';
|
|
4
|
+
describe('FixedRouter', () => {
|
|
5
|
+
it('should return the provided gateway', async () => {
|
|
6
|
+
const router = new FixedGatewayRouter({
|
|
7
|
+
gateway: new URL('http://test-gateway.net'),
|
|
8
|
+
});
|
|
9
|
+
const result = await router.getTargetGateway();
|
|
10
|
+
assert.deepStrictEqual(result, new URL('http://test-gateway.net'));
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { randomInt } from '../wayfinder.js';
|
|
2
|
+
// TODO: one of N where N are in the last time window have met certain performance thresholds
|
|
3
|
+
// TODO: look at bitorrent routing protocols for inspiration
|
|
4
|
+
// TODO: router that looks at local stats/metrics and adjusts based on those
|
|
5
|
+
export class PriorityGatewayRouter {
|
|
6
|
+
name = 'priority';
|
|
7
|
+
gatewaysProvider;
|
|
8
|
+
limit;
|
|
9
|
+
sortBy;
|
|
10
|
+
sortOrder;
|
|
11
|
+
blocklist;
|
|
12
|
+
constructor({ gatewaysProvider, limit = 1, sortBy = 'operatorStake', sortOrder = 'desc', blocklist = [], }) {
|
|
13
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
14
|
+
this.limit = limit;
|
|
15
|
+
this.sortBy = sortBy;
|
|
16
|
+
this.sortOrder = sortOrder;
|
|
17
|
+
this.blocklist = blocklist;
|
|
18
|
+
}
|
|
19
|
+
async getTargetGateway() {
|
|
20
|
+
const allGateways = await this.gatewaysProvider.getGateways();
|
|
21
|
+
const gateways = allGateways.filter((gateway) => gateway.status === 'joined' &&
|
|
22
|
+
!this.blocklist.includes(gateway.settings.fqdn));
|
|
23
|
+
const sortedGateways = gateways
|
|
24
|
+
.sort(this.sortOrder === 'asc'
|
|
25
|
+
? (a, b) => a[this.sortBy] - b[this.sortBy]
|
|
26
|
+
: (a, b) => b[this.sortBy] - a[this.sortBy])
|
|
27
|
+
.slice(0, this.limit);
|
|
28
|
+
const targetGateway = sortedGateways[randomInt(0, sortedGateways.length)];
|
|
29
|
+
if (targetGateway === undefined) {
|
|
30
|
+
throw new Error('No target gateway found');
|
|
31
|
+
}
|
|
32
|
+
return new URL(`${targetGateway.settings.protocol}://${targetGateway.settings.fqdn}:${targetGateway.settings.port}`);
|
|
33
|
+
}
|
|
34
|
+
}
|