@ar.io/sdk 3.12.2 → 3.13.0-beta.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 +28 -1
- package/bundles/web.bundle.min.js +142 -147
- package/lib/cjs/cli/commands/arnsPurchaseCommands.js +10 -0
- package/lib/cjs/cli/options.js +5 -0
- package/lib/cjs/cli/utils.js +4 -0
- package/lib/cjs/common/index.js +2 -0
- package/lib/cjs/common/io.js +4 -0
- package/lib/cjs/common/turbo.js +2 -1
- package/lib/cjs/common/wayfinder/gateways/network.js +48 -0
- package/lib/cjs/common/wayfinder/gateways/simple-cache.js +35 -0
- package/lib/cjs/common/wayfinder/gateways/static.js +13 -0
- package/lib/cjs/common/wayfinder/index.js +48 -0
- package/lib/cjs/common/wayfinder/routing/strategies/ping.js +72 -0
- package/lib/cjs/common/wayfinder/routing/strategies/preferred-with-fallback.js +50 -0
- package/lib/cjs/common/wayfinder/routing/strategies/random.js +13 -0
- package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +42 -0
- package/lib/cjs/common/wayfinder/routing/strategies/static.js +29 -0
- package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +110 -0
- package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +27 -0
- package/lib/cjs/common/wayfinder/verification/trusted.js +125 -0
- package/lib/cjs/common/wayfinder/wayfinder.js +508 -0
- package/lib/cjs/types/wayfinder.js +3 -0
- package/lib/cjs/utils/hash.js +83 -31
- package/lib/cjs/version.js +1 -1
- package/lib/esm/cli/commands/arnsPurchaseCommands.js +11 -1
- package/lib/esm/cli/options.js +5 -0
- package/lib/esm/cli/utils.js +3 -0
- package/lib/esm/common/index.js +2 -0
- package/lib/esm/common/io.js +4 -0
- package/lib/esm/common/turbo.js +2 -1
- package/lib/esm/common/wayfinder/gateways/network.js +44 -0
- package/lib/esm/common/wayfinder/gateways/simple-cache.js +31 -0
- package/lib/esm/common/wayfinder/gateways/static.js +9 -0
- package/lib/esm/common/wayfinder/index.js +32 -0
- package/lib/esm/common/wayfinder/routing/strategies/ping.js +68 -0
- package/lib/esm/common/wayfinder/routing/strategies/preferred-with-fallback.js +46 -0
- package/lib/esm/common/wayfinder/routing/strategies/random.js +9 -0
- package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +38 -0
- package/lib/esm/common/wayfinder/routing/strategies/static.js +25 -0
- package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +102 -0
- package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +23 -0
- package/lib/esm/common/wayfinder/verification/trusted.js +121 -0
- package/lib/esm/common/wayfinder/wayfinder.js +499 -0
- package/lib/esm/types/wayfinder.js +2 -0
- package/lib/esm/utils/hash.js +74 -27
- package/lib/esm/version.js +1 -1
- package/lib/types/cli/options.d.ts +4 -0
- package/lib/types/cli/utils.d.ts +3 -0
- package/lib/types/common/index.d.ts +1 -0
- package/lib/types/common/turbo.d.ts +2 -1
- package/lib/types/common/wayfinder/gateways/network.d.ts +33 -0
- package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +31 -0
- package/lib/types/common/wayfinder/gateways/static.d.ts +23 -0
- package/lib/types/common/wayfinder/index.d.ts +27 -0
- package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +27 -0
- package/lib/types/common/wayfinder/routing/strategies/preferred-with-fallback.d.ts +31 -0
- package/lib/types/common/wayfinder/routing/strategies/random.d.ts +21 -0
- package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +29 -0
- package/lib/types/common/wayfinder/routing/strategies/static.d.ts +29 -0
- package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +27 -0
- package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +26 -0
- package/lib/types/common/wayfinder/verification/trusted.d.ts +51 -0
- package/lib/types/common/wayfinder/wayfinder.d.ts +257 -0
- package/lib/types/types/io.d.ts +4 -0
- package/lib/types/types/wayfinder.d.ts +66 -0
- package/lib/types/utils/hash.d.ts +8 -4
- package/lib/types/version.d.ts +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TrustedGatewaysHashProvider = void 0;
|
|
4
|
+
const wayfinder_js_1 = require("../wayfinder.js");
|
|
5
|
+
const arioGatewayHeaders = {
|
|
6
|
+
digest: 'x-ar-io-digest',
|
|
7
|
+
verified: 'x-ar-io-verified',
|
|
8
|
+
txId: 'x-arns-resolved-tx-id',
|
|
9
|
+
processId: 'x-arns-resolved-process-id',
|
|
10
|
+
};
|
|
11
|
+
class TrustedGatewaysHashProvider {
|
|
12
|
+
gatewaysProvider;
|
|
13
|
+
constructor({ gatewaysProvider,
|
|
14
|
+
// TODO: add threshold for allowed hash difference (i.e. by count or ratio of total gateways checked)
|
|
15
|
+
}) {
|
|
16
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Gets the digest for a given txId from all trusted gateways and ensures they all match.
|
|
20
|
+
* @param txId - The txId to get the digest for.
|
|
21
|
+
* @returns The digest for the given txId.
|
|
22
|
+
*/
|
|
23
|
+
async getHash({ txId, }) {
|
|
24
|
+
// get the hash from every gateway, and ensure they all match
|
|
25
|
+
const hashSet = new Set();
|
|
26
|
+
const hashResults = [];
|
|
27
|
+
const gateways = await this.gatewaysProvider.getGateways();
|
|
28
|
+
const hashes = await Promise.all(gateways.map(async (gateway) => {
|
|
29
|
+
const sandbox = (0, wayfinder_js_1.sandboxFromId)(txId);
|
|
30
|
+
const urlWithSandbox = `${gateway.protocol}//${sandbox}.${gateway.hostname}/${txId}`;
|
|
31
|
+
let txIdHash;
|
|
32
|
+
/**
|
|
33
|
+
* This is a problem because we're not able to verify the hash of the data item if the gateway doesn't have the data in its cache.
|
|
34
|
+
* We should add the ability to send a HEAD request to trigger a GET request to hydrate the cache on the trusted gateway via a header.
|
|
35
|
+
* For now, we'll just do a GET request to hydrate the cache if the HEAD request doesn't contain the digest.
|
|
36
|
+
*/
|
|
37
|
+
for (const method of ['GET', 'GET']) {
|
|
38
|
+
const response = await fetch(urlWithSandbox, {
|
|
39
|
+
method,
|
|
40
|
+
redirect: 'follow',
|
|
41
|
+
mode: 'cors',
|
|
42
|
+
headers: {
|
|
43
|
+
'Cache-Control': 'no-cache',
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
// skip if the request failed or the digest is not present
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const fetchedTxIdHash = response.headers.get(arioGatewayHeaders.digest);
|
|
51
|
+
if (fetchedTxIdHash !== null && fetchedTxIdHash !== undefined) {
|
|
52
|
+
txIdHash = fetchedTxIdHash;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (txIdHash === undefined) {
|
|
57
|
+
// skip this gateway if we didn't get a hash
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
hashResults.push({
|
|
61
|
+
gateway: gateway.hostname,
|
|
62
|
+
txIdHash,
|
|
63
|
+
});
|
|
64
|
+
return txIdHash;
|
|
65
|
+
}));
|
|
66
|
+
for (const hash of hashes) {
|
|
67
|
+
if (hash !== undefined) {
|
|
68
|
+
hashSet.add(hash);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (hashSet.size === 0) {
|
|
72
|
+
throw new Error(`No trusted gateways returned a hash for txId ${txId}`);
|
|
73
|
+
}
|
|
74
|
+
if (hashSet.size > 1) {
|
|
75
|
+
throw new Error(`Failed to get consistent hash from all trusted gateways. ${JSON.stringify(hashResults)}`);
|
|
76
|
+
}
|
|
77
|
+
return { hash: hashResults[0].txIdHash, algorithm: 'sha256' };
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Get the data root for a given txId from all trusted gateways and ensure they all match.
|
|
81
|
+
* @param txId - The txId to get the data root for.
|
|
82
|
+
* @returns The data root for the given txId.
|
|
83
|
+
*/
|
|
84
|
+
async getDataRoot({ txId }) {
|
|
85
|
+
const dataRootSet = new Set();
|
|
86
|
+
const dataRootResults = [];
|
|
87
|
+
const gateways = await this.gatewaysProvider.getGateways();
|
|
88
|
+
const dataRoots = await Promise.all(gateways.map(async (gateway) => {
|
|
89
|
+
const response = await fetch(`${gateway.toString()}tx/${txId}/data_root`);
|
|
90
|
+
if (!response.ok) {
|
|
91
|
+
// skip this gateway
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const dataRoot = await response.text();
|
|
95
|
+
dataRootResults.push({
|
|
96
|
+
gateway: gateway.hostname,
|
|
97
|
+
dataRoot,
|
|
98
|
+
});
|
|
99
|
+
return dataRoot;
|
|
100
|
+
}));
|
|
101
|
+
for (const dataRoot of dataRoots) {
|
|
102
|
+
if (dataRoot !== undefined) {
|
|
103
|
+
dataRootSet.add(dataRoot);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (dataRootSet.size > 1) {
|
|
107
|
+
throw new Error(`Failed to get consistent data root from all trusted gateways. ${JSON.stringify(dataRootResults)}`);
|
|
108
|
+
}
|
|
109
|
+
return dataRootSet.values().next().value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.TrustedGatewaysHashProvider = TrustedGatewaysHashProvider;
|
|
113
|
+
// client could check hashes of data items, match expected hash
|
|
114
|
+
// if the gateway has the hash and they've verified it, you can trust the data item and offset
|
|
115
|
+
// you would be only trusting the gateway that it is a valid bundle
|
|
116
|
+
// you can request the offset from the gateway to verify the id
|
|
117
|
+
/**
|
|
118
|
+
* Note from @djwhitt
|
|
119
|
+
*
|
|
120
|
+
* Calculating data roots this way is fine, but it may not reproduce the original data root.
|
|
121
|
+
* We could also implement a data root verifier that pulls all the chunks, checks that they
|
|
122
|
+
* reproduce the expected data root, and then compares the concatenated chunk data to the
|
|
123
|
+
* original data retrieved. That would take a while, but it should be able to verify any L1
|
|
124
|
+
* data where we can find the chunks.
|
|
125
|
+
*/
|
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Wayfinder = exports.createWayfinderClient = exports.WayfinderEmitter = exports.resolveWayfinderUrl = exports.txIdRegex = exports.arnsRegex = void 0;
|
|
4
|
+
exports.tapAndVerifyReadableStream = tapAndVerifyReadableStream;
|
|
5
|
+
exports.sandboxFromId = sandboxFromId;
|
|
6
|
+
/**
|
|
7
|
+
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
const eventemitter3_1 = require("eventemitter3");
|
|
22
|
+
const rfc4648_1 = require("rfc4648");
|
|
23
|
+
const io_js_1 = require("../io.js");
|
|
24
|
+
const logger_js_1 = require("../logger.js");
|
|
25
|
+
const network_js_1 = require("./gateways/network.js");
|
|
26
|
+
const simple_cache_js_1 = require("./gateways/simple-cache.js");
|
|
27
|
+
const static_js_1 = require("./gateways/static.js");
|
|
28
|
+
const ping_js_1 = require("./routing/strategies/ping.js");
|
|
29
|
+
const hash_verifier_js_1 = require("./verification/strategies/hash-verifier.js");
|
|
30
|
+
const trusted_js_1 = require("./verification/trusted.js");
|
|
31
|
+
// known regexes for wayfinder urls
|
|
32
|
+
exports.arnsRegex = /^[a-z0-9_-]{1,51}$/;
|
|
33
|
+
exports.txIdRegex = /^[A-Za-z0-9_-]{43}$/;
|
|
34
|
+
/**
|
|
35
|
+
* Core function that converts a wayfinder url to the proper ar-io gateway URL
|
|
36
|
+
* @param originalUrl - the wayfinder url to resolve
|
|
37
|
+
* @param selectedGateway - the target gateway to resolve the url against
|
|
38
|
+
* @returns the resolved url that can be used to make a request
|
|
39
|
+
*/
|
|
40
|
+
const resolveWayfinderUrl = ({ originalUrl, selectedGateway, logger, }) => {
|
|
41
|
+
if (originalUrl.toString().startsWith('ar://')) {
|
|
42
|
+
logger?.debug(`Applying wayfinder routing protocol to ${originalUrl}`, {
|
|
43
|
+
originalUrl,
|
|
44
|
+
});
|
|
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
|
+
logger?.debug(`Routing to ${path.slice(1)} on ${selectedGateway}`, {
|
|
49
|
+
originalUrl,
|
|
50
|
+
selectedGateway,
|
|
51
|
+
});
|
|
52
|
+
return new URL(path.slice(1), selectedGateway);
|
|
53
|
+
}
|
|
54
|
+
// Split path to get the first part (name/txId) and remaining path components
|
|
55
|
+
const [firstPart, ...rest] = path.split('/');
|
|
56
|
+
// TODO: this breaks 43 character named arns names - we should check a a local name cache list before resolving raw transaction ids
|
|
57
|
+
if (exports.txIdRegex.test(firstPart)) {
|
|
58
|
+
const sandbox = sandboxFromId(firstPart);
|
|
59
|
+
return new URL(`${firstPart}${rest.length > 0 ? '/' + rest.join('/') : ''}`, `${selectedGateway.protocol}//${sandbox}.${selectedGateway.hostname}`);
|
|
60
|
+
}
|
|
61
|
+
if (exports.arnsRegex.test(firstPart)) {
|
|
62
|
+
// TODO: tests to ensure arns names support query params and paths
|
|
63
|
+
const arnsUrl = `${selectedGateway.protocol}//${firstPart}.${selectedGateway.hostname}${selectedGateway.port ? `:${selectedGateway.port}` : ''}`;
|
|
64
|
+
logger?.debug(`Routing to ${path} on ${arnsUrl}`, {
|
|
65
|
+
originalUrl,
|
|
66
|
+
selectedGateway,
|
|
67
|
+
});
|
|
68
|
+
return new URL(rest.length > 0 ? rest.join('/') : '', arnsUrl);
|
|
69
|
+
}
|
|
70
|
+
// TODO: support .eth addresses
|
|
71
|
+
// TODO: "gasless" routing via DNS TXT records
|
|
72
|
+
}
|
|
73
|
+
logger?.debug('No wayfinder routing protocol applied', {
|
|
74
|
+
originalUrl,
|
|
75
|
+
});
|
|
76
|
+
// return the original url if it's not a wayfinder url
|
|
77
|
+
return new URL(originalUrl);
|
|
78
|
+
};
|
|
79
|
+
exports.resolveWayfinderUrl = resolveWayfinderUrl;
|
|
80
|
+
class WayfinderEmitter extends eventemitter3_1.EventEmitter {
|
|
81
|
+
constructor({ onVerificationSucceeded, onVerificationFailed, onVerificationProgress, } = {}) {
|
|
82
|
+
super();
|
|
83
|
+
if (onVerificationSucceeded) {
|
|
84
|
+
this.on('verification-succeeded', onVerificationSucceeded);
|
|
85
|
+
}
|
|
86
|
+
if (onVerificationFailed) {
|
|
87
|
+
this.on('verification-failed', onVerificationFailed);
|
|
88
|
+
}
|
|
89
|
+
if (onVerificationProgress) {
|
|
90
|
+
this.on('verification-progress', onVerificationProgress);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.WayfinderEmitter = WayfinderEmitter;
|
|
95
|
+
function tapAndVerifyReadableStream({ originalStream, contentLength, verifyData, txId, emitter, strict = false, }) {
|
|
96
|
+
if (originalStream instanceof ReadableStream &&
|
|
97
|
+
typeof originalStream.tee === 'function') {
|
|
98
|
+
/**
|
|
99
|
+
* NOTE: tee requires the streams both streams to be consumed, so we need to make sure we consume the client branch
|
|
100
|
+
* by the caller. This means when `request` is called, the client stream must be consumed by the caller via await request.text()
|
|
101
|
+
* for verification to complete.
|
|
102
|
+
*
|
|
103
|
+
* It is feasible to make the verification stream not to depend on the client branch being consumed, should the DX not be obvious.
|
|
104
|
+
*/
|
|
105
|
+
const [verifyBranch, clientBranch] = originalStream.tee();
|
|
106
|
+
// setup our promise to verify the data
|
|
107
|
+
const verificationPromise = verifyData({
|
|
108
|
+
data: verifyBranch,
|
|
109
|
+
txId,
|
|
110
|
+
});
|
|
111
|
+
let bytesProcessed = 0;
|
|
112
|
+
const reader = clientBranch.getReader();
|
|
113
|
+
const clientStreamWithVerification = new ReadableStream({
|
|
114
|
+
async pull(controller) {
|
|
115
|
+
const { done, value } = await reader.read();
|
|
116
|
+
if (done) {
|
|
117
|
+
if (strict) {
|
|
118
|
+
// in strict mode, we wait for verification to complete before closing the controller
|
|
119
|
+
try {
|
|
120
|
+
await verificationPromise;
|
|
121
|
+
emitter?.emit('verification-succeeded', { txId });
|
|
122
|
+
controller.close();
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
// emit the verification failed event
|
|
126
|
+
emitter?.emit('verification-failed', err);
|
|
127
|
+
// In strict mode, we report the error to the client stream
|
|
128
|
+
controller.error(new Error('Verification failed', { cause: err }));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
// trigger the verification promise and emit events for the result
|
|
133
|
+
verificationPromise
|
|
134
|
+
.then(() => {
|
|
135
|
+
emitter?.emit('verification-succeeded', { txId });
|
|
136
|
+
})
|
|
137
|
+
.catch((error) => {
|
|
138
|
+
emitter?.emit('verification-failed', error);
|
|
139
|
+
});
|
|
140
|
+
// in non-strict mode, we close the controller immediately and handle verification asynchronously
|
|
141
|
+
controller.close();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
bytesProcessed += value.length;
|
|
146
|
+
emitter?.emit('verification-progress', {
|
|
147
|
+
txId,
|
|
148
|
+
totalBytes: contentLength,
|
|
149
|
+
processedBytes: bytesProcessed,
|
|
150
|
+
});
|
|
151
|
+
controller.enqueue(value);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
cancel(reason) {
|
|
155
|
+
// cancel the reader regardless of verification status
|
|
156
|
+
reader.cancel(reason);
|
|
157
|
+
// emit the verification cancellation event
|
|
158
|
+
emitter?.emit('verification-failed', {
|
|
159
|
+
txId,
|
|
160
|
+
error: new Error('Verification cancelled', {
|
|
161
|
+
cause: {
|
|
162
|
+
reason,
|
|
163
|
+
},
|
|
164
|
+
}),
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
return clientStreamWithVerification;
|
|
169
|
+
}
|
|
170
|
+
throw new Error('Unsupported body type for cloning');
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Gets the sandbox hash for a given transaction id
|
|
174
|
+
*/
|
|
175
|
+
function sandboxFromId(id) {
|
|
176
|
+
return rfc4648_1.base32
|
|
177
|
+
.stringify(Buffer.from(id, 'base64'), { pad: false })
|
|
178
|
+
.toLowerCase();
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Creates a wrapped fetch function that supports ar:// protocol
|
|
182
|
+
*
|
|
183
|
+
* This function leverages a Proxy to intercept calls to fetch
|
|
184
|
+
* and redirects them to the target gateway using the resolveUrl function.
|
|
185
|
+
*
|
|
186
|
+
* Any URLs provided that are not wayfinder urls will be passed directly to fetch.
|
|
187
|
+
*
|
|
188
|
+
* @param resolveUrl - the function to construct the redirect url for ar:// requests
|
|
189
|
+
* @returns a wrapped fetch function that supports ar:// protocol and always returns Response
|
|
190
|
+
*/
|
|
191
|
+
const createWayfinderClient = ({ resolveUrl, verifyData, selectGateway, emitter = new WayfinderEmitter(), logger, strict = false, }) => {
|
|
192
|
+
return async (url, init) => {
|
|
193
|
+
if (typeof url !== 'string') {
|
|
194
|
+
logger?.debug('URL is not a string, skipping routing', {
|
|
195
|
+
url,
|
|
196
|
+
});
|
|
197
|
+
emitter?.emit('routing-skipped', {
|
|
198
|
+
originalUrl: JSON.stringify(url),
|
|
199
|
+
});
|
|
200
|
+
return fetch(url, init);
|
|
201
|
+
}
|
|
202
|
+
emitter?.emit('routing-started', {
|
|
203
|
+
originalUrl: url.toString(),
|
|
204
|
+
});
|
|
205
|
+
const maxRetries = 3;
|
|
206
|
+
const retryDelay = 1000;
|
|
207
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
208
|
+
try {
|
|
209
|
+
// select the target gateway
|
|
210
|
+
const selectedGateway = await selectGateway();
|
|
211
|
+
logger?.debug('Selected gateway', {
|
|
212
|
+
originalUrl: url,
|
|
213
|
+
selectedGateway: selectedGateway.toString(),
|
|
214
|
+
});
|
|
215
|
+
// route the request to the target gateway
|
|
216
|
+
const redirectUrl = resolveUrl({
|
|
217
|
+
originalUrl: url,
|
|
218
|
+
selectedGateway,
|
|
219
|
+
logger,
|
|
220
|
+
});
|
|
221
|
+
emitter?.emit('routing-succeeded', {
|
|
222
|
+
originalUrl: url,
|
|
223
|
+
selectedGateway: selectedGateway.toString(),
|
|
224
|
+
redirectUrl: redirectUrl.toString(),
|
|
225
|
+
});
|
|
226
|
+
logger?.debug(`Redirecting request`, {
|
|
227
|
+
originalUrl: url,
|
|
228
|
+
redirectUrl: redirectUrl.toString(),
|
|
229
|
+
});
|
|
230
|
+
// make the request to the target gateway using the redirect url
|
|
231
|
+
const response = await fetch(redirectUrl.toString(), {
|
|
232
|
+
// enforce CORS given we're likely going to a different origin, but always allow the client to override
|
|
233
|
+
redirect: 'follow',
|
|
234
|
+
mode: 'cors',
|
|
235
|
+
...init,
|
|
236
|
+
});
|
|
237
|
+
logger?.debug(`Successfully routed request to gateway`, {
|
|
238
|
+
redirectUrl: redirectUrl.toString(),
|
|
239
|
+
originalUrl: url.toString(),
|
|
240
|
+
});
|
|
241
|
+
// only verify data if the redirect url is different from the original url
|
|
242
|
+
if (redirectUrl.toString() !== url.toString()) {
|
|
243
|
+
if (verifyData) {
|
|
244
|
+
const headers = response.headers;
|
|
245
|
+
// transaction id is either in the response headers or the path of the request as the first parameter
|
|
246
|
+
const txId = headers.get('x-arns-resolved-id') ??
|
|
247
|
+
redirectUrl.pathname.split('/')[1];
|
|
248
|
+
const contentLength = +(headers.get('content-length') ?? 0);
|
|
249
|
+
if (!exports.txIdRegex.test(txId)) {
|
|
250
|
+
// no transaction id found, skip verification
|
|
251
|
+
logger?.debug('No transaction id found, skipping verification', {
|
|
252
|
+
redirectUrl: redirectUrl.toString(),
|
|
253
|
+
originalUrl: url,
|
|
254
|
+
});
|
|
255
|
+
emitter?.emit('verification-skipped', {
|
|
256
|
+
originalUrl: url,
|
|
257
|
+
});
|
|
258
|
+
return response;
|
|
259
|
+
}
|
|
260
|
+
emitter?.emit('identified-transaction-id', {
|
|
261
|
+
originalUrl: url,
|
|
262
|
+
selectedGateway: redirectUrl.toString(),
|
|
263
|
+
txId,
|
|
264
|
+
});
|
|
265
|
+
// Check if the response has a body
|
|
266
|
+
if (response.body) {
|
|
267
|
+
const newClientStream = tapAndVerifyReadableStream({
|
|
268
|
+
originalStream: response.body,
|
|
269
|
+
contentLength,
|
|
270
|
+
verifyData,
|
|
271
|
+
txId,
|
|
272
|
+
emitter,
|
|
273
|
+
strict,
|
|
274
|
+
});
|
|
275
|
+
return new Response(newClientStream, {
|
|
276
|
+
status: response.status,
|
|
277
|
+
statusText: response.statusText,
|
|
278
|
+
headers: response.headers,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
// No response body to verify, skip verification
|
|
283
|
+
logger?.debug('No response body to verify', {
|
|
284
|
+
redirectUrl: redirectUrl.toString(),
|
|
285
|
+
originalUrl: url,
|
|
286
|
+
txId,
|
|
287
|
+
});
|
|
288
|
+
return response;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return response;
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
logger?.debug('Failed to route request', {
|
|
296
|
+
error: error.message,
|
|
297
|
+
stack: error.stack,
|
|
298
|
+
originalUrl: url,
|
|
299
|
+
attempt: i + 1,
|
|
300
|
+
maxRetries,
|
|
301
|
+
});
|
|
302
|
+
if (i < maxRetries - 1) {
|
|
303
|
+
await new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
throw new Error('Failed to route request after max retries', {
|
|
308
|
+
cause: {
|
|
309
|
+
originalUrl: url,
|
|
310
|
+
maxRetries,
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
};
|
|
315
|
+
exports.createWayfinderClient = createWayfinderClient;
|
|
316
|
+
/**
|
|
317
|
+
* The main class for the wayfinder
|
|
318
|
+
*/
|
|
319
|
+
class Wayfinder {
|
|
320
|
+
/**
|
|
321
|
+
* The gateways provider is responsible for providing the list of gateways to use for routing requests.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* const wayfinder = new Wayfinder({
|
|
325
|
+
* gatewaysProvider: new SimpleCacheGatewaysProvider({
|
|
326
|
+
* gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
|
|
327
|
+
* ttlSeconds: 60 * 60 * 24, // 1 day
|
|
328
|
+
* }),
|
|
329
|
+
* });
|
|
330
|
+
*/
|
|
331
|
+
gatewaysProvider;
|
|
332
|
+
/**
|
|
333
|
+
* The routing strategy to use when routing requests.
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* const wayfinder = new Wayfinder({
|
|
337
|
+
* strategy: new FastestPingStrategy({
|
|
338
|
+
* timeoutMs: 1000,
|
|
339
|
+
* }),
|
|
340
|
+
* });
|
|
341
|
+
*/
|
|
342
|
+
routingStrategy;
|
|
343
|
+
/**
|
|
344
|
+
* A helper function that resolves the redirect url for ar:// requests to a target gateway.
|
|
345
|
+
*
|
|
346
|
+
* Note: no verification is done when resolving an ar://<path> url to a wayfinder route.
|
|
347
|
+
* In order to verify the data, you must use the `request` function or request the data and
|
|
348
|
+
* verify it yourself via the `verifyData` function.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* const { resolveUrl } = new Wayfinder();
|
|
352
|
+
*
|
|
353
|
+
* // returns the redirected URL based on the routing strategy and the original url
|
|
354
|
+
* const redirectUrl = await resolveUrl({ originalUrl: 'ar://example' });
|
|
355
|
+
*
|
|
356
|
+
* window.open(redirectUrl.toString(), '_blank');
|
|
357
|
+
*/
|
|
358
|
+
resolveUrl;
|
|
359
|
+
/**
|
|
360
|
+
* A wrapped fetch function that supports ar:// protocol. If a verification strategy is provided,
|
|
361
|
+
* the request will be verified and events will be emitted as the request is processed.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* const wayfinder = new Wayfinder({
|
|
365
|
+
* verificationStrategy: new HashVerificationStrategy({
|
|
366
|
+
* trustedHashProvider: new TrustedGatewaysHashProvider({
|
|
367
|
+
* gatewaysProvider: new StaticGatewaysProvider({
|
|
368
|
+
* gateways: ['https://permagate.io'],
|
|
369
|
+
* }),
|
|
370
|
+
* }),
|
|
371
|
+
* }),
|
|
372
|
+
* })
|
|
373
|
+
*
|
|
374
|
+
* // request an arns name
|
|
375
|
+
* const response = await wayfinder.request('ar://ardrive')
|
|
376
|
+
*
|
|
377
|
+
* // request a transaction id
|
|
378
|
+
* const response = await wayfinder.request('ar://1234567890')
|
|
379
|
+
*
|
|
380
|
+
* // Set strict mode to true to make verification blocking
|
|
381
|
+
* const wayfinder = new Wayfinder({
|
|
382
|
+
* strict: true,
|
|
383
|
+
* });
|
|
384
|
+
*
|
|
385
|
+
* // This will throw an error if verification fails
|
|
386
|
+
* try {
|
|
387
|
+
* const response = await wayfinder.request('ar://1234567890');
|
|
388
|
+
* } catch (error) {
|
|
389
|
+
* console.error('Verification failed', error);
|
|
390
|
+
* }
|
|
391
|
+
*/
|
|
392
|
+
request;
|
|
393
|
+
/**
|
|
394
|
+
* The function that verifies the data hash for a given transaction id.
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* const wayfinder = new Wayfinder({
|
|
398
|
+
* verifyData: (data, txId) => {
|
|
399
|
+
* // some custom verification logic
|
|
400
|
+
* return true;
|
|
401
|
+
* },
|
|
402
|
+
* });
|
|
403
|
+
*/
|
|
404
|
+
verifyData;
|
|
405
|
+
/**
|
|
406
|
+
* Whether verification should be strict (blocking) or not.
|
|
407
|
+
* If true, verification failures will cause requests to fail.
|
|
408
|
+
* If false, verification will be performed asynchronously and failures will only emit events.
|
|
409
|
+
*/
|
|
410
|
+
strict;
|
|
411
|
+
/**
|
|
412
|
+
* The event emitter for wayfinder that emits verification events.
|
|
413
|
+
*
|
|
414
|
+
* const wayfinder = new Wayfinder()
|
|
415
|
+
*
|
|
416
|
+
* wayfinder.emitter.on('verification-succeeded', (event) => {
|
|
417
|
+
* console.log('Verification passed!', event);
|
|
418
|
+
* })
|
|
419
|
+
*
|
|
420
|
+
* wayfinder.emitter.on('verification-failed', (event) => {
|
|
421
|
+
* console.log('Verification failed!', event);
|
|
422
|
+
* })
|
|
423
|
+
*
|
|
424
|
+
* or implement the events interface and pass it in, using callback functions
|
|
425
|
+
*
|
|
426
|
+
* const wayfinder = new Wayfinder({
|
|
427
|
+
* events: {
|
|
428
|
+
* onVerificationPassed: (event) => {
|
|
429
|
+
* console.log('Verification passed!', event);
|
|
430
|
+
* },
|
|
431
|
+
* onVerificationFailed: (event) => {
|
|
432
|
+
* console.log('Verification failed!', event);
|
|
433
|
+
* },
|
|
434
|
+
* onVerificationProgress: (event) => {
|
|
435
|
+
* console.log('Verification progress!', event);
|
|
436
|
+
* },
|
|
437
|
+
* }
|
|
438
|
+
* })
|
|
439
|
+
*
|
|
440
|
+
* const response = await wayfind('ar://example');
|
|
441
|
+
*/
|
|
442
|
+
emitter;
|
|
443
|
+
/**
|
|
444
|
+
* The constructor for the wayfinder
|
|
445
|
+
* @param routingStrategy - the routing strategy to use for requests
|
|
446
|
+
* @param verificationStrategy - the verification strategy to use for requests
|
|
447
|
+
* @param gatewaysProvider - the gateways provider to use for routing requests
|
|
448
|
+
* @param logger - the logger to use for logging
|
|
449
|
+
* @param strict - if true, verification will be blocking and will fail requests if verification fails; if false, verification will be non-blocking
|
|
450
|
+
*/
|
|
451
|
+
constructor({ logger = logger_js_1.Logger.default, gatewaysProvider = new simple_cache_js_1.SimpleCacheGatewaysProvider({
|
|
452
|
+
gatewaysProvider: new network_js_1.NetworkGatewaysProvider({
|
|
453
|
+
ario: io_js_1.ARIO.mainnet(),
|
|
454
|
+
}),
|
|
455
|
+
ttlSeconds: 60 * 60, // 1 hour
|
|
456
|
+
}), routingStrategy = new ping_js_1.FastestPingRoutingStrategy({
|
|
457
|
+
timeoutMs: 1000,
|
|
458
|
+
}), verificationStrategy = new hash_verifier_js_1.HashVerificationStrategy({
|
|
459
|
+
trustedHashProvider: new trusted_js_1.TrustedGatewaysHashProvider({
|
|
460
|
+
gatewaysProvider: new static_js_1.StaticGatewaysProvider({
|
|
461
|
+
gateways: ['https://permagate.io'],
|
|
462
|
+
}),
|
|
463
|
+
}),
|
|
464
|
+
}), events = {
|
|
465
|
+
onVerificationSucceeded: (event) => {
|
|
466
|
+
logger.debug('Verification passed!', event);
|
|
467
|
+
},
|
|
468
|
+
onVerificationFailed: (event) => {
|
|
469
|
+
logger.error('Verification failed!', event);
|
|
470
|
+
},
|
|
471
|
+
onVerificationProgress: (event) => {
|
|
472
|
+
logger.debug('Verification progress!', event);
|
|
473
|
+
},
|
|
474
|
+
}, strict = false, } = {}) {
|
|
475
|
+
this.routingStrategy = routingStrategy;
|
|
476
|
+
this.gatewaysProvider = gatewaysProvider;
|
|
477
|
+
this.emitter = new WayfinderEmitter(events);
|
|
478
|
+
this.verifyData =
|
|
479
|
+
verificationStrategy.verifyData.bind(verificationStrategy);
|
|
480
|
+
this.strict = strict;
|
|
481
|
+
// top level function to easily resolve wayfinder urls using the routing strategy and gateways provider
|
|
482
|
+
this.resolveUrl = async ({ originalUrl, logger }) => {
|
|
483
|
+
const selectedGateway = await this.routingStrategy.selectGateway({
|
|
484
|
+
gateways: await this.gatewaysProvider.getGateways(),
|
|
485
|
+
});
|
|
486
|
+
return (0, exports.resolveWayfinderUrl)({
|
|
487
|
+
originalUrl,
|
|
488
|
+
selectedGateway,
|
|
489
|
+
logger,
|
|
490
|
+
});
|
|
491
|
+
};
|
|
492
|
+
// create a wayfinder client with the routing strategy and gateways provider
|
|
493
|
+
this.request = (0, exports.createWayfinderClient)({
|
|
494
|
+
selectGateway: async () => {
|
|
495
|
+
return this.routingStrategy.selectGateway({
|
|
496
|
+
gateways: await this.gatewaysProvider.getGateways(),
|
|
497
|
+
});
|
|
498
|
+
},
|
|
499
|
+
resolveUrl: exports.resolveWayfinderUrl,
|
|
500
|
+
verifyData: this.verifyData,
|
|
501
|
+
emitter: this.emitter,
|
|
502
|
+
logger,
|
|
503
|
+
strict,
|
|
504
|
+
});
|
|
505
|
+
logger?.debug(`Wayfinder initialized with ${routingStrategy.constructor.name} routing strategy`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
exports.Wayfinder = Wayfinder;
|