@ar.io/sdk 3.10.0-alpha.6 → 3.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -125
- package/bundles/web.bundle.min.js +147 -126
- package/lib/cjs/cli/commands/antCommands.js +3 -4
- package/lib/cjs/cli/commands/arnsPurchaseCommands.js +5 -6
- package/lib/cjs/cli/commands/gatewayWriteCommands.js +11 -12
- package/lib/cjs/cli/commands/readCommands.js +21 -22
- package/lib/cjs/cli/commands/transfer.js +6 -7
- package/lib/cjs/cli/options.js +0 -12
- package/lib/cjs/cli/utils.js +52 -74
- package/lib/cjs/common/ant-versions.js +5 -5
- package/lib/cjs/common/faucet.js +2 -2
- package/lib/cjs/common/index.js +0 -2
- package/lib/cjs/common/io.js +5 -64
- package/lib/cjs/types/ant.js +2 -13
- package/lib/cjs/types/index.js +0 -1
- package/lib/cjs/types/io.js +5 -5
- package/lib/cjs/utils/ao.js +9 -14
- package/lib/cjs/utils/arweave.js +4 -4
- package/lib/cjs/utils/base64.js +4 -5
- package/lib/cjs/utils/json.js +1 -2
- package/lib/cjs/utils/schema.js +1 -2
- package/lib/cjs/version.js +1 -1
- package/lib/cjs/web/index.js +1 -3
- package/lib/esm/cli/options.js +0 -12
- package/lib/esm/cli/utils.js +10 -31
- package/lib/esm/common/ant-versions.js +5 -5
- package/lib/esm/common/index.js +0 -2
- package/lib/esm/common/io.js +5 -64
- package/lib/esm/types/ant.js +0 -11
- package/lib/esm/types/index.js +0 -1
- package/lib/esm/types/io.js +1 -1
- package/lib/esm/utils/ao.js +0 -5
- package/lib/esm/version.js +1 -1
- package/lib/esm/web/index.js +1 -1
- package/lib/types/cli/commands/antCommands.d.ts +3 -3
- package/lib/types/cli/commands/arnsPurchaseCommands.d.ts +1 -1
- package/lib/types/cli/commands/gatewayWriteCommands.d.ts +9 -9
- package/lib/types/cli/commands/readCommands.d.ts +3 -4
- package/lib/types/cli/commands/transfer.d.ts +3 -3
- package/lib/types/cli/options.d.ts +0 -9
- package/lib/types/cli/types.d.ts +0 -3
- package/lib/types/cli/utils.d.ts +0 -4
- package/lib/types/common/ant-versions.d.ts +6 -3
- package/lib/types/common/http.d.ts +0 -1
- package/lib/types/common/index.d.ts +0 -1
- package/lib/types/common/io.d.ts +9 -15
- package/lib/types/types/ant.d.ts +1 -53
- package/lib/types/types/common.d.ts +2 -8
- package/lib/types/types/index.d.ts +0 -1
- package/lib/types/types/io.d.ts +7 -9
- package/lib/types/types/token.d.ts +0 -2
- package/lib/types/utils/ao.d.ts +12 -1
- package/lib/types/utils/base64.d.ts +0 -2
- package/lib/types/version.d.ts +1 -1
- package/lib/types/web/index.d.ts +1 -1
- package/package.json +3 -5
- package/lib/cjs/common/turbo.js +0 -208
- package/lib/cjs/common/wayfinder/gateways.js +0 -75
- package/lib/cjs/common/wayfinder/index.js +0 -36
- package/lib/cjs/common/wayfinder/routers/fixed.js +0 -14
- package/lib/cjs/common/wayfinder/routers/fixed.test.js +0 -14
- package/lib/cjs/common/wayfinder/routers/priority.js +0 -38
- package/lib/cjs/common/wayfinder/routers/priority.test.js +0 -155
- package/lib/cjs/common/wayfinder/routers/random.js +0 -23
- package/lib/cjs/common/wayfinder/routers/random.test.js +0 -104
- package/lib/cjs/common/wayfinder/routers/simple-cache.js +0 -25
- package/lib/cjs/common/wayfinder/routers/simple-cache.test.js +0 -41
- package/lib/cjs/common/wayfinder/wayfinder.js +0 -194
- package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -254
- package/lib/cjs/types/wayfinder.js +0 -2
- package/lib/cjs/utils/url.js +0 -28
- package/lib/cjs/utils/url.test.js +0 -24
- package/lib/esm/common/turbo.js +0 -200
- package/lib/esm/common/wayfinder/gateways.js +0 -69
- package/lib/esm/common/wayfinder/index.js +0 -20
- package/lib/esm/common/wayfinder/routers/fixed.js +0 -10
- package/lib/esm/common/wayfinder/routers/fixed.test.js +0 -12
- package/lib/esm/common/wayfinder/routers/priority.js +0 -34
- package/lib/esm/common/wayfinder/routers/priority.test.js +0 -153
- package/lib/esm/common/wayfinder/routers/random.js +0 -19
- package/lib/esm/common/wayfinder/routers/random.test.js +0 -102
- package/lib/esm/common/wayfinder/routers/simple-cache.js +0 -21
- package/lib/esm/common/wayfinder/routers/simple-cache.test.js +0 -39
- package/lib/esm/common/wayfinder/wayfinder.js +0 -187
- package/lib/esm/common/wayfinder/wayfinder.test.js +0 -249
- package/lib/esm/types/wayfinder.js +0 -1
- package/lib/esm/utils/url.js +0 -24
- package/lib/esm/utils/url.test.js +0 -19
- package/lib/types/common/turbo.d.ts +0 -62
- package/lib/types/common/wayfinder/gateways.d.ts +0 -44
- package/lib/types/common/wayfinder/index.d.ts +0 -19
- package/lib/types/common/wayfinder/routers/fixed.d.ts +0 -25
- package/lib/types/common/wayfinder/routers/fixed.test.d.ts +0 -1
- package/lib/types/common/wayfinder/routers/priority.d.ts +0 -34
- package/lib/types/common/wayfinder/routers/priority.test.d.ts +0 -1
- package/lib/types/common/wayfinder/routers/random.d.ts +0 -28
- package/lib/types/common/wayfinder/routers/random.test.d.ts +0 -1
- package/lib/types/common/wayfinder/routers/simple-cache.d.ts +0 -29
- package/lib/types/common/wayfinder/routers/simple-cache.test.d.ts +0 -1
- package/lib/types/common/wayfinder/wayfinder.d.ts +0 -106
- package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
- package/lib/types/types/wayfinder.d.ts +0 -20
- package/lib/types/utils/url.d.ts +0 -19
- package/lib/types/utils/url.test.d.ts +0 -1
|
@@ -1,187 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,249 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import got from 'got';
|
|
3
|
-
import assert from 'node:assert';
|
|
4
|
-
import { buffer } from 'node:stream/consumers';
|
|
5
|
-
import { before, describe, it } from 'node:test';
|
|
6
|
-
import { RandomGatewayRouter } from './routers/random.js';
|
|
7
|
-
import { Wayfinder } from './wayfinder.js';
|
|
8
|
-
const stubbedGateway = {
|
|
9
|
-
status: 'joined',
|
|
10
|
-
gatewayAddress: 'arweave',
|
|
11
|
-
operatorStake: 1000,
|
|
12
|
-
totalDelegatedStake: 1000,
|
|
13
|
-
startTimestamp: 1000,
|
|
14
|
-
settings: {
|
|
15
|
-
protocol: 'https',
|
|
16
|
-
fqdn: 'arweave.net',
|
|
17
|
-
port: 443,
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
const stubbedGatewaysProvider = {
|
|
21
|
-
getGateways: async () => [stubbedGateway],
|
|
22
|
-
};
|
|
23
|
-
describe('Wayfinder', () => {
|
|
24
|
-
describe('http wrapper', () => {
|
|
25
|
-
describe('fetch', () => {
|
|
26
|
-
let wayfinder;
|
|
27
|
-
before(() => {
|
|
28
|
-
wayfinder = new Wayfinder({
|
|
29
|
-
httpClient: fetch,
|
|
30
|
-
router: new RandomGatewayRouter({
|
|
31
|
-
gatewaysProvider: stubbedGatewaysProvider,
|
|
32
|
-
}),
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
it('should fetch the data using the selected gateway', async () => {
|
|
36
|
-
const nativeFetch = await fetch('https://ao.arweave.net');
|
|
37
|
-
const response = await wayfinder.request('ar://ao');
|
|
38
|
-
assert.strictEqual(response.status, 200);
|
|
39
|
-
assert.strictEqual(response.status, nativeFetch.status);
|
|
40
|
-
// assert the arns headers are the same
|
|
41
|
-
const arnsHeaders = Array.from(response.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
|
|
42
|
-
const nativeFetchHeaders = Array.from(nativeFetch.headers.entries()).filter(([key]) => key.startsWith('x-arns-'));
|
|
43
|
-
assert.deepStrictEqual(arnsHeaders, nativeFetchHeaders);
|
|
44
|
-
assert.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
45
|
-
});
|
|
46
|
-
it('should route a non-ar:// url as a normal fetch', async () => {
|
|
47
|
-
const [nativeFetch, response] = await Promise.all([
|
|
48
|
-
fetch('https://arweave.net/', {
|
|
49
|
-
method: 'HEAD',
|
|
50
|
-
}),
|
|
51
|
-
wayfinder.request('https://arweave.net/', {
|
|
52
|
-
method: 'HEAD',
|
|
53
|
-
}),
|
|
54
|
-
]);
|
|
55
|
-
assert.strictEqual(response.status, 200);
|
|
56
|
-
assert.strictEqual(response.status, nativeFetch.status);
|
|
57
|
-
// TODO: ensure the headers are the same excluding unique headers
|
|
58
|
-
assert.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
59
|
-
});
|
|
60
|
-
for (const api of ['/info', '/metrics', '/block/current']) {
|
|
61
|
-
it(`supports native arweave node apis ${api}`, async () => {
|
|
62
|
-
const [nativeFetch, response] = await Promise.all([
|
|
63
|
-
fetch(`https://arweave.net${api}`),
|
|
64
|
-
wayfinder.request(`ar:///${api}`),
|
|
65
|
-
]);
|
|
66
|
-
assert.strictEqual(response.status, 200);
|
|
67
|
-
assert.strictEqual(response.status, nativeFetch.status);
|
|
68
|
-
// TODO: ensure the headers are the same excluding unique headers
|
|
69
|
-
assert.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
|
|
73
|
-
it(`supports native ario node gateway apis ${api}`, async () => {
|
|
74
|
-
const [nativeFetch, response] = await Promise.all([
|
|
75
|
-
fetch(`https://arweave.net${api}`),
|
|
76
|
-
wayfinder.request(`ar:///${api}`),
|
|
77
|
-
]);
|
|
78
|
-
assert.strictEqual(response.status, 200);
|
|
79
|
-
assert.strictEqual(response.status, nativeFetch.status);
|
|
80
|
-
// TODO: ensure the headers are the same excluding unique headers
|
|
81
|
-
assert.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
it('supports a post request to graphql', async () => {
|
|
85
|
-
const response = await wayfinder.request('ar:///graphql', {
|
|
86
|
-
method: 'POST',
|
|
87
|
-
headers: {
|
|
88
|
-
'Content-Type': 'application/json',
|
|
89
|
-
},
|
|
90
|
-
body: JSON.stringify({
|
|
91
|
-
query: `
|
|
92
|
-
query {
|
|
93
|
-
transactions(
|
|
94
|
-
ids: ["xf958qhCNGfDme1FtoiD6DtMfDENDbtxZpjOM_1tsMM"]
|
|
95
|
-
) {
|
|
96
|
-
edges {
|
|
97
|
-
cursor
|
|
98
|
-
node {
|
|
99
|
-
id
|
|
100
|
-
tags {
|
|
101
|
-
name
|
|
102
|
-
value
|
|
103
|
-
}
|
|
104
|
-
block {
|
|
105
|
-
height
|
|
106
|
-
timestamp
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
pageInfo {
|
|
111
|
-
hasNextPage
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
`,
|
|
116
|
-
}),
|
|
117
|
-
});
|
|
118
|
-
assert.strictEqual(response.status, 200);
|
|
119
|
-
});
|
|
120
|
-
it('returns the error from the target gateway if the route is not found', async () => {
|
|
121
|
-
const [nativeFetch, response] = await Promise.all([
|
|
122
|
-
fetch('https://arweave.net/not-found'),
|
|
123
|
-
wayfinder.request('https://arweave.net/not-found'),
|
|
124
|
-
]);
|
|
125
|
-
assert.strictEqual(response.status, nativeFetch.status);
|
|
126
|
-
assert.strictEqual(response.statusText, nativeFetch.statusText);
|
|
127
|
-
assert.deepStrictEqual(await response.text(), await nativeFetch.text());
|
|
128
|
-
});
|
|
129
|
-
});
|
|
130
|
-
describe('axios', () => {
|
|
131
|
-
let wayfinder;
|
|
132
|
-
before(() => {
|
|
133
|
-
wayfinder = new Wayfinder({
|
|
134
|
-
httpClient: axios,
|
|
135
|
-
router: new RandomGatewayRouter({
|
|
136
|
-
gatewaysProvider: stubbedGatewaysProvider,
|
|
137
|
-
}),
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
it('should fetch the data using axios default function against the target gateway', async () => {
|
|
141
|
-
const [nativeAxios, response] = await Promise.all([
|
|
142
|
-
axios('https://ao.arweave.net'),
|
|
143
|
-
wayfinder.request('ar://ao'),
|
|
144
|
-
]);
|
|
145
|
-
assert.strictEqual(response.status, 200);
|
|
146
|
-
assert.strictEqual(response.status, nativeAxios.status);
|
|
147
|
-
// assert the arns headers are the same
|
|
148
|
-
const arnsHeaders = Object.entries(response.headers)
|
|
149
|
-
.sort()
|
|
150
|
-
.filter(([key]) => key.startsWith('x-arns-'));
|
|
151
|
-
const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
|
|
152
|
-
assert.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
|
|
153
|
-
assert.deepStrictEqual(response.data, nativeAxios.data);
|
|
154
|
-
});
|
|
155
|
-
it('should fetch the data using the axios.get method against the target gateway', async () => {
|
|
156
|
-
const [nativeAxios, response] = await Promise.all([
|
|
157
|
-
axios.get('https://ao.arweave.net'),
|
|
158
|
-
wayfinder.request.get('ar://ao'),
|
|
159
|
-
]);
|
|
160
|
-
assert.strictEqual(response.status, 200);
|
|
161
|
-
assert.strictEqual(response.status, nativeAxios.status);
|
|
162
|
-
// assert the arns headers are the same
|
|
163
|
-
const arnsHeaders = Object.entries(response.headers)
|
|
164
|
-
.sort()
|
|
165
|
-
.filter(([key]) => key.startsWith('x-arns-'));
|
|
166
|
-
const nativeAxiosHeaders = Object.entries(nativeAxios.headers).filter(([key]) => key.startsWith('x-arns-'));
|
|
167
|
-
assert.deepStrictEqual(arnsHeaders.sort(), nativeAxiosHeaders.sort());
|
|
168
|
-
assert.deepStrictEqual(response.data, nativeAxios.data);
|
|
169
|
-
});
|
|
170
|
-
it('should route a non-ar:// url as a normal axios request', async () => {
|
|
171
|
-
const [nativeAxios, response] = await Promise.all([
|
|
172
|
-
axios('https://arweave.net/'),
|
|
173
|
-
wayfinder.request('https://arweave.net/'),
|
|
174
|
-
]);
|
|
175
|
-
assert.strictEqual(response.status, 200);
|
|
176
|
-
assert.strictEqual(response.status, nativeAxios.status);
|
|
177
|
-
assert.deepStrictEqual(response.data, nativeAxios.data);
|
|
178
|
-
// TODO: ensure the headers are the same excluding unique headers
|
|
179
|
-
});
|
|
180
|
-
for (const api of ['/info', '/metrics', '/block/current']) {
|
|
181
|
-
it(`supports native arweave node apis ${api}`, async () => {
|
|
182
|
-
const [nativeAxios, response] = await Promise.all([
|
|
183
|
-
axios(`https://arweave.net${api}`),
|
|
184
|
-
wayfinder.request(`ar:///${api}`),
|
|
185
|
-
]);
|
|
186
|
-
assert.strictEqual(response.status, 200);
|
|
187
|
-
assert.strictEqual(response.status, nativeAxios.status);
|
|
188
|
-
// TODO: ensure the headers are the same excluding unique headers
|
|
189
|
-
assert.deepStrictEqual(response.data, nativeAxios.data);
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
for (const api of ['/ar-io/info', '/ar-io/__gateway_metrics']) {
|
|
193
|
-
it(`supports native ario node gateway apis ${api}`, async () => {
|
|
194
|
-
const [nativeAxios, response] = await Promise.all([
|
|
195
|
-
axios(`https://arweave.net${api}`),
|
|
196
|
-
wayfinder.request(`ar:///${api}`),
|
|
197
|
-
]);
|
|
198
|
-
assert.strictEqual(response.status, 200);
|
|
199
|
-
assert.strictEqual(response.status, nativeAxios.status);
|
|
200
|
-
// TODO: ensure the headers are the same excluding unique headers
|
|
201
|
-
assert.deepStrictEqual(response.data, nativeAxios.data);
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
it('should return the error from the target gateway if the route is not found', async () => {
|
|
205
|
-
const axiosInstance = axios.create({
|
|
206
|
-
validateStatus: () => true, // don't throw so we can compare axios result with wrapped axios result
|
|
207
|
-
});
|
|
208
|
-
const wayfinder = new Wayfinder({
|
|
209
|
-
httpClient: axiosInstance,
|
|
210
|
-
router: new RandomGatewayRouter({
|
|
211
|
-
gatewaysProvider: stubbedGatewaysProvider,
|
|
212
|
-
}),
|
|
213
|
-
});
|
|
214
|
-
const [nativeAxios, response] = await Promise.all([
|
|
215
|
-
axiosInstance('https://arweave.net/not-found'),
|
|
216
|
-
wayfinder.request('https://arweave.net/not-found'),
|
|
217
|
-
]);
|
|
218
|
-
assert.strictEqual(response.status, nativeAxios.status);
|
|
219
|
-
assert.strictEqual(response.data, nativeAxios.data);
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
});
|
|
223
|
-
describe('got', () => {
|
|
224
|
-
let wayfinder;
|
|
225
|
-
before(() => {
|
|
226
|
-
wayfinder = new Wayfinder({
|
|
227
|
-
httpClient: got,
|
|
228
|
-
router: new RandomGatewayRouter({
|
|
229
|
-
gatewaysProvider: stubbedGatewaysProvider,
|
|
230
|
-
}),
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
it('should fetch the data using the got default function against the target gateway', async () => {
|
|
234
|
-
const [nativeGot, response] = await Promise.all([
|
|
235
|
-
got('https://ao.arweave.net'),
|
|
236
|
-
wayfinder.request('ar://ao'),
|
|
237
|
-
]);
|
|
238
|
-
assert.strictEqual(response.statusCode, 200);
|
|
239
|
-
assert.strictEqual(response.statusCode, nativeGot.statusCode);
|
|
240
|
-
assert.deepStrictEqual(response.body, nativeGot.body);
|
|
241
|
-
});
|
|
242
|
-
it('should stream the data using got.stream against the selected target gateway', async () => {
|
|
243
|
-
const nativeBuffer = await buffer(await got.stream('https://ao.arweave.net', { decompress: false }));
|
|
244
|
-
const wayfinderBuffer = await buffer(await wayfinder.request.stream('ar://ao', { decompress: false }));
|
|
245
|
-
assert.deepStrictEqual(wayfinderBuffer, nativeBuffer);
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
// TODO: streaming support using got.stream and got()
|
|
249
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/esm/utils/url.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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 const urlWithSearchParams = ({ baseUrl, params, }) => {
|
|
17
|
-
const urlObj = new URL(baseUrl);
|
|
18
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
19
|
-
if (value === undefined || value === null)
|
|
20
|
-
return;
|
|
21
|
-
urlObj.searchParams.set(key, value.toString());
|
|
22
|
-
});
|
|
23
|
-
return urlObj.toString();
|
|
24
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
|
-
import { test } from 'node:test';
|
|
3
|
-
import { urlWithSearchParams } from './url.js';
|
|
4
|
-
test('urlWithSearchParams prunes undefined values but keeps other falsey values', () => {
|
|
5
|
-
const result = urlWithSearchParams({
|
|
6
|
-
baseUrl: 'https://example.com',
|
|
7
|
-
params: {
|
|
8
|
-
number: 1,
|
|
9
|
-
string: 'string',
|
|
10
|
-
boolean: true,
|
|
11
|
-
empty: '',
|
|
12
|
-
zero: 0,
|
|
13
|
-
false: false,
|
|
14
|
-
null: null,
|
|
15
|
-
undef: undefined,
|
|
16
|
-
},
|
|
17
|
-
});
|
|
18
|
-
assert.strictEqual(result, 'https://example.com/?number=1&string=string&boolean=true&empty=&zero=0&false=false');
|
|
19
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { AxiosInstance, RawAxiosRequestHeaders } from 'axios';
|
|
2
|
-
import { AoMessageResult, TransactionId, TurboArNSSigner } from '../types/common.js';
|
|
3
|
-
import { AoTokenCostParams } from '../types/io.js';
|
|
4
|
-
import { mARIOToken } from '../types/token.js';
|
|
5
|
-
import { ILogger } from './logger.js';
|
|
6
|
-
export interface TurboUnauthenticatedConfig {
|
|
7
|
-
paymentUrl?: string;
|
|
8
|
-
logger?: ILogger;
|
|
9
|
-
axios?: AxiosInstance;
|
|
10
|
-
}
|
|
11
|
-
export interface TurboAuthenticatedConfig extends TurboUnauthenticatedConfig {
|
|
12
|
-
signer: TurboArNSSigner;
|
|
13
|
-
}
|
|
14
|
-
export declare function signedRequestHeadersFromSigner({ signer, nonce, }: {
|
|
15
|
-
signer: TurboArNSSigner;
|
|
16
|
-
nonce?: string;
|
|
17
|
-
}): Promise<RawAxiosRequestHeaders>;
|
|
18
|
-
export type ArNSPurchaseReceipt = AoTokenCostParams & {
|
|
19
|
-
wincQty: string;
|
|
20
|
-
mARIOQty: string;
|
|
21
|
-
usdArRate: number;
|
|
22
|
-
createdDate: string;
|
|
23
|
-
};
|
|
24
|
-
export interface ArNSPaymentProvider {
|
|
25
|
-
/** Returns the cost of the action in the Payment Provider's native currency (winc for Turbo) */
|
|
26
|
-
getPrice(params: AoTokenCostParams): Promise<number>;
|
|
27
|
-
getArNSPriceDetails(params: AoTokenCostParams): Promise<{
|
|
28
|
-
winc: string;
|
|
29
|
-
mARIO: mARIOToken;
|
|
30
|
-
}>;
|
|
31
|
-
}
|
|
32
|
-
export interface ArNSAuthenticatedPaymentProvider extends ArNSPaymentProvider {
|
|
33
|
-
initiateArNSPurchase(params: AoTokenCostParams & {
|
|
34
|
-
processId?: TransactionId;
|
|
35
|
-
}): Promise<AoMessageResult<ArNSPurchaseReceipt>>;
|
|
36
|
-
}
|
|
37
|
-
export declare class TurboArNSPaymentFactory {
|
|
38
|
-
static init(): TurboArNSPaymentProviderUnauthenticated;
|
|
39
|
-
static init({ paymentUrl, axios, logger, }: TurboUnauthenticatedConfig & {
|
|
40
|
-
signer?: TurboArNSSigner;
|
|
41
|
-
}): TurboArNSPaymentProviderUnauthenticated;
|
|
42
|
-
static init({ signer, paymentUrl, axios, logger, }: TurboAuthenticatedConfig): TurboArNSPaymentProviderAuthenticated;
|
|
43
|
-
}
|
|
44
|
-
export declare class TurboArNSPaymentProviderUnauthenticated implements ArNSPaymentProvider {
|
|
45
|
-
protected readonly paymentUrl: string;
|
|
46
|
-
protected readonly axios: AxiosInstance;
|
|
47
|
-
protected readonly logger: ILogger;
|
|
48
|
-
constructor({ paymentUrl, axios, logger, }: TurboUnauthenticatedConfig);
|
|
49
|
-
getArNSPriceDetails({ intent, name, quantity, type, years, }: AoTokenCostParams): Promise<{
|
|
50
|
-
winc: string;
|
|
51
|
-
mARIO: mARIOToken;
|
|
52
|
-
}>;
|
|
53
|
-
getPrice(params: AoTokenCostParams): Promise<number>;
|
|
54
|
-
}
|
|
55
|
-
export declare class TurboArNSPaymentProviderAuthenticated extends TurboArNSPaymentProviderUnauthenticated implements ArNSAuthenticatedPaymentProvider {
|
|
56
|
-
private readonly signer;
|
|
57
|
-
constructor({ signer, ...restConfig }: TurboAuthenticatedConfig);
|
|
58
|
-
initiateArNSPurchase({ intent, name, quantity, type, processId, years, }: AoTokenCostParams & {
|
|
59
|
-
processId?: TransactionId;
|
|
60
|
-
}): Promise<AoMessageResult<ArNSPurchaseReceipt>>;
|
|
61
|
-
}
|
|
62
|
-
export declare function isTurboArNSSigner(signer: unknown): signer is TurboArNSSigner;
|
|
@@ -1,44 +0,0 @@
|
|
|
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 { AoARIORead, AoGatewayWithAddress } from '../../types/io.js';
|
|
17
|
-
export interface GatewaysProvider {
|
|
18
|
-
getGateways(): Promise<AoGatewayWithAddress[]>;
|
|
19
|
-
}
|
|
20
|
-
export declare class ARIOGatewaysProvider implements GatewaysProvider {
|
|
21
|
-
private ario;
|
|
22
|
-
constructor({ ario }: {
|
|
23
|
-
ario: AoARIORead;
|
|
24
|
-
});
|
|
25
|
-
getGateways(): Promise<AoGatewayWithAddress[]>;
|
|
26
|
-
}
|
|
27
|
-
export declare class StaticGatewaysProvider implements GatewaysProvider {
|
|
28
|
-
private gateways;
|
|
29
|
-
constructor({ gateways }: {
|
|
30
|
-
gateways: AoGatewayWithAddress[];
|
|
31
|
-
});
|
|
32
|
-
getGateways(): Promise<AoGatewayWithAddress[]>;
|
|
33
|
-
}
|
|
34
|
-
export declare class SimpleCacheGatewaysProvider implements GatewaysProvider {
|
|
35
|
-
private gatewaysProvider;
|
|
36
|
-
private ttlSeconds;
|
|
37
|
-
private lastUpdated;
|
|
38
|
-
private gatewaysCache;
|
|
39
|
-
constructor({ gatewaysProvider, ttlSeconds, }: {
|
|
40
|
-
gatewaysProvider: GatewaysProvider;
|
|
41
|
-
ttlSeconds?: number;
|
|
42
|
-
});
|
|
43
|
-
getGateways(): Promise<AoGatewayWithAddress[]>;
|
|
44
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
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
|
-
export * from './routers/random.js';
|
|
18
|
-
export * from './routers/priority.js';
|
|
19
|
-
export * from './routers/fixed.js';
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
import { WayfinderRouter } from '../../../types/wayfinder.js';
|
|
18
|
-
export declare class FixedGatewayRouter implements WayfinderRouter {
|
|
19
|
-
readonly name = "fixed";
|
|
20
|
-
private gateway;
|
|
21
|
-
constructor({ gateway }: {
|
|
22
|
-
gateway: URL;
|
|
23
|
-
});
|
|
24
|
-
getTargetGateway(): Promise<URL>;
|
|
25
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
import { WayfinderRouter } from '../../../types/wayfinder.js';
|
|
18
|
-
import { GatewaysProvider } from '../gateways.js';
|
|
19
|
-
export declare class PriorityGatewayRouter implements WayfinderRouter {
|
|
20
|
-
readonly name = "priority";
|
|
21
|
-
private gatewaysProvider;
|
|
22
|
-
private limit;
|
|
23
|
-
private sortBy;
|
|
24
|
-
private sortOrder;
|
|
25
|
-
private blocklist;
|
|
26
|
-
constructor({ gatewaysProvider, limit, sortBy, sortOrder, blocklist, }: {
|
|
27
|
-
gatewaysProvider: GatewaysProvider;
|
|
28
|
-
limit?: number;
|
|
29
|
-
sortBy?: 'totalDelegatedStake' | 'operatorStake' | 'startTimestamp';
|
|
30
|
-
sortOrder?: 'asc' | 'desc';
|
|
31
|
-
blocklist?: string[];
|
|
32
|
-
});
|
|
33
|
-
getTargetGateway(): Promise<URL>;
|
|
34
|
-
}
|