@ar.io/wayfinder-core 1.0.7 → 1.1.0
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/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wayfinder.d.ts +127 -65
- package/dist/wayfinder.d.ts.map +1 -1
- package/dist/wayfinder.js +171 -110
- package/package.json +1 -1
package/dist/version.d.ts
CHANGED
|
@@ -14,5 +14,5 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
export declare const WAYFINDER_CORE_VERSION = "v1.0
|
|
17
|
+
export declare const WAYFINDER_CORE_VERSION = "v1.1.0";
|
|
18
18
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
package/dist/wayfinder.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ import { type Tracer } from '@opentelemetry/api';
|
|
|
18
18
|
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
|
19
19
|
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
|
|
20
20
|
import { WayfinderEmitter } from './emitter.js';
|
|
21
|
-
import type { GatewaysProvider, Logger, TelemetrySettings, VerificationStrategy,
|
|
21
|
+
import type { GatewaysProvider, Logger, RoutingStrategy, TelemetrySettings, VerificationStrategy, WayfinderOptions, WayfinderURL, WayfinderURLParams } from './types.js';
|
|
22
22
|
export declare const wayfinderRequestHeaders: ({ traceId, }: {
|
|
23
23
|
traceId?: string;
|
|
24
24
|
}) => {
|
|
@@ -124,70 +124,6 @@ export declare class Wayfinder {
|
|
|
124
124
|
* OpenTelemetry tracer instance
|
|
125
125
|
*/
|
|
126
126
|
protected tracer?: Tracer;
|
|
127
|
-
/**
|
|
128
|
-
* A helper function that resolves a provided url to a target gateway.
|
|
129
|
-
*
|
|
130
|
-
* Note: no verification is done when calling this function.
|
|
131
|
-
* It just generates the redirect url based on the routing strategy.
|
|
132
|
-
* In order to verify the data, you must use the `request` function or request the data and
|
|
133
|
-
* verify it yourself via the `verifyData` function.
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* const { resolveUrl } = new Wayfinder();
|
|
137
|
-
*
|
|
138
|
-
* // returns the redirected URL based on the routing strategy and the original url
|
|
139
|
-
* const redirectUrl = await resolveUrl({
|
|
140
|
-
* originalUrl: 'https://arweave.net/<txId>',
|
|
141
|
-
* });
|
|
142
|
-
*
|
|
143
|
-
* // returns the redirected URL based on the routing strategy and the provided arns name
|
|
144
|
-
* const redirectUrl = await resolveUrl({
|
|
145
|
-
* arnsName: 'ardrive',
|
|
146
|
-
* });
|
|
147
|
-
*
|
|
148
|
-
* // returns the redirected URL based on the routing strategy and the provided wayfinder url
|
|
149
|
-
* const redirectUrl = await resolveUrl({
|
|
150
|
-
* wayfinderUrl: 'ar://1234567890',
|
|
151
|
-
* });
|
|
152
|
-
*
|
|
153
|
-
* // returns the redirected URL based on the routing strategy and the provided txId
|
|
154
|
-
* const redirectUrl = await resolveUrl({
|
|
155
|
-
* txId: '1234567890',
|
|
156
|
-
* });
|
|
157
|
-
*
|
|
158
|
-
* window.open(redirectUrl.toString(), '_blank');
|
|
159
|
-
*/
|
|
160
|
-
readonly resolveUrl: (params: WayfinderURLParams) => Promise<URL>;
|
|
161
|
-
/**
|
|
162
|
-
* A wrapped fetch function that supports ar:// protocol. If a verification strategy is provided,
|
|
163
|
-
* the request will be verified and events will be emitted as the request is processed.
|
|
164
|
-
*
|
|
165
|
-
* @example
|
|
166
|
-
* const wayfinder = new Wayfinder({
|
|
167
|
-
* verificationStrategy: new HashVerificationStrategy({
|
|
168
|
-
* trustedGateways: [new URL('https://permagate.io')],
|
|
169
|
-
* }),
|
|
170
|
-
* })
|
|
171
|
-
*
|
|
172
|
-
* // request an arns name
|
|
173
|
-
* const response = await wayfinder.request('ar://ardrive')
|
|
174
|
-
*
|
|
175
|
-
* // request a transaction id
|
|
176
|
-
* const response = await wayfinder.request('ar://1234567890')
|
|
177
|
-
*
|
|
178
|
-
* // Set strict mode to true to make verification blocking
|
|
179
|
-
* const wayfinder = new Wayfinder({
|
|
180
|
-
* strict: true,
|
|
181
|
-
* });
|
|
182
|
-
*
|
|
183
|
-
* // This will throw an error if verification fails
|
|
184
|
-
* try {
|
|
185
|
-
* const response = await wayfinder.request('ar://1234567890');
|
|
186
|
-
* } catch (error) {
|
|
187
|
-
* console.error('Verification failed', error);
|
|
188
|
-
* }
|
|
189
|
-
*/
|
|
190
|
-
request: WayfinderFetch;
|
|
191
127
|
/**
|
|
192
128
|
* The logger used by this Wayfinder instance
|
|
193
129
|
*/
|
|
@@ -256,5 +192,131 @@ export declare class Wayfinder {
|
|
|
256
192
|
* @param options - Wayfinder configuration options
|
|
257
193
|
*/
|
|
258
194
|
constructor({ logger, gatewaysProvider, verificationSettings, routingSettings, telemetrySettings, }?: WayfinderOptions);
|
|
195
|
+
/**
|
|
196
|
+
* Sets the routing strategy to use for routing requests.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* const wayfinder = new Wayfinder();
|
|
200
|
+
* wayfinder.setRoutingStrategy(new RandomRoutingStrategy());
|
|
201
|
+
*
|
|
202
|
+
* @param strategy - The routing strategy to use
|
|
203
|
+
*/
|
|
204
|
+
setRoutingStrategy(strategy: RoutingStrategy): void;
|
|
205
|
+
/**
|
|
206
|
+
* Sets the verification strategy to use for verifying requests.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const wayfinder = new Wayfinder();
|
|
210
|
+
* wayfinder.setVerificationStrategy(new HashVerificationStrategy({
|
|
211
|
+
* trustedGateways: [new URL('https://permagate.io')],
|
|
212
|
+
* }));
|
|
213
|
+
*
|
|
214
|
+
* @param strategy - The verification strategy to use
|
|
215
|
+
*/
|
|
216
|
+
setVerificationStrategy(strategy: VerificationStrategy): void;
|
|
217
|
+
/**
|
|
218
|
+
* Disables verification for requests.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* const wayfinder = new Wayfinder({
|
|
222
|
+
* verificationSettings: {
|
|
223
|
+
* enabled: true,
|
|
224
|
+
* },
|
|
225
|
+
* });
|
|
226
|
+
*
|
|
227
|
+
* // disable verification
|
|
228
|
+
* wayfinder.disableVerification();
|
|
229
|
+
*
|
|
230
|
+
* // enable verification with strict mode
|
|
231
|
+
* wayfinder.enableVerification({ strict: true });
|
|
232
|
+
*/
|
|
233
|
+
disableVerification(): void;
|
|
234
|
+
/**
|
|
235
|
+
* Enables verification for requests.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* const wayfinder = new Wayfinder({
|
|
239
|
+
* verificationSettings: {
|
|
240
|
+
* enabled: false,
|
|
241
|
+
* },
|
|
242
|
+
* });
|
|
243
|
+
*
|
|
244
|
+
* // enable verification with strict mode
|
|
245
|
+
* wayfinder.enableVerification({ strict: true });
|
|
246
|
+
*
|
|
247
|
+
* // enable verification without strict mode
|
|
248
|
+
* wayfinder.enableVerification();
|
|
249
|
+
* @param strict - Whether to make verification strict
|
|
250
|
+
*/
|
|
251
|
+
enableVerification({ strict, }?: {
|
|
252
|
+
strict?: boolean;
|
|
253
|
+
}): void;
|
|
254
|
+
/**
|
|
255
|
+
* A wrapped fetch function that supports ar:// protocol. If a verification strategy is provided,
|
|
256
|
+
* the request will be verified and events will be emitted as the request is processed.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* const wayfinder = new Wayfinder({
|
|
260
|
+
* verificationStrategy: new HashVerificationStrategy({
|
|
261
|
+
* trustedGateways: [new URL('https://permagate.io')],
|
|
262
|
+
* }),
|
|
263
|
+
* })
|
|
264
|
+
*
|
|
265
|
+
* // request an arns name
|
|
266
|
+
* const response = await wayfinder.request('ar://ardrive')
|
|
267
|
+
*
|
|
268
|
+
* // request a transaction id
|
|
269
|
+
* const response = await wayfinder.request('ar://1234567890')
|
|
270
|
+
*
|
|
271
|
+
* // Set strict mode to true to make verification blocking
|
|
272
|
+
* const wayfinder = new Wayfinder({
|
|
273
|
+
* strict: true,
|
|
274
|
+
* });
|
|
275
|
+
*
|
|
276
|
+
* // This will throw an error if verification fails
|
|
277
|
+
* try {
|
|
278
|
+
* const response = await wayfinder.request('ar://1234567890');
|
|
279
|
+
* } catch (error) {
|
|
280
|
+
* console.error('Verification failed', error);
|
|
281
|
+
* }
|
|
282
|
+
*/
|
|
283
|
+
request(input: URL | RequestInfo, init?: RequestInit & {
|
|
284
|
+
verificationSettings?: NonNullable<WayfinderOptions['verificationSettings']>;
|
|
285
|
+
routingSettings?: NonNullable<WayfinderOptions['routingSettings']>;
|
|
286
|
+
}): Promise<Response>;
|
|
287
|
+
/**
|
|
288
|
+
* A helper function that resolves a provided url to a target gateway using the current routing strategy.
|
|
289
|
+
*
|
|
290
|
+
* Note: no verification is done when calling this function.
|
|
291
|
+
* It just generates the redirect url based on the routing strategy.
|
|
292
|
+
* In order to verify the data, you must use the `request` function or request the data and
|
|
293
|
+
* verify it yourself via the `verifyData` function.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* const { resolveUrl } = new Wayfinder();
|
|
297
|
+
*
|
|
298
|
+
* // returns the redirected URL based on the routing strategy and the original url
|
|
299
|
+
* const redirectUrl = await resolveUrl({
|
|
300
|
+
* originalUrl: 'https://arweave.net/<txId>',
|
|
301
|
+
* });
|
|
302
|
+
*
|
|
303
|
+
* // returns the redirected URL based on the routing strategy and the provided arns name
|
|
304
|
+
* const redirectUrl = await resolveUrl({
|
|
305
|
+
* arnsName: 'ardrive',
|
|
306
|
+
* });
|
|
307
|
+
*
|
|
308
|
+
* // returns the redirected URL based on the routing strategy and the provided wayfinder url
|
|
309
|
+
* const redirectUrl = await resolveUrl({
|
|
310
|
+
* wayfinderUrl: 'ar://1234567890',
|
|
311
|
+
* });
|
|
312
|
+
*
|
|
313
|
+
* // returns the redirected URL based on the routing strategy and the provided txId
|
|
314
|
+
* const redirectUrl = await resolveUrl({
|
|
315
|
+
* txId: '1234567890',
|
|
316
|
+
* });
|
|
317
|
+
*
|
|
318
|
+
* window.open(redirectUrl.toString(), '_blank');
|
|
319
|
+
*/
|
|
320
|
+
resolveUrl(params: WayfinderURLParams): Promise<URL>;
|
|
259
321
|
}
|
|
260
322
|
//# sourceMappingURL=wayfinder.d.ts.map
|
package/dist/wayfinder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wayfinder.d.ts","sourceRoot":"","sources":["../src/wayfinder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,EAAQ,KAAK,MAAM,EAAkB,MAAM,oBAAoB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIhD,OAAO,KAAK,EACV,gBAAgB,EAChB,MAAM,EACN,iBAAiB,EACjB,oBAAoB,EACpB,
|
|
1
|
+
{"version":3,"file":"wayfinder.d.ts","sourceRoot":"","sources":["../src/wayfinder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,OAAO,EAAQ,KAAK,MAAM,EAAkB,MAAM,oBAAoB,CAAC;AACvE,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAIhD,OAAO,KAAK,EACV,gBAAgB,EAChB,MAAM,EACN,eAAe,EACf,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAKpB,eAAO,MAAM,uBAAuB,GAAI,cAErC;IACD,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;;;CAMA,CAAC;AAGF,eAAO,MAAM,SAAS,QAAuB,CAAC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G;IACH,kBAAkB,CAAC,EACjB,MAAc,GACf,GAAE;QACD,MAAM,CAAC,EAAE,OAAO,CAAC;KACb;IAKN;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4BG;IACH,OAAO,CACL,KAAK,EAAE,GAAG,GAAG,WAAW,EACxB,IAAI,CAAC,EAAE,WAAW,GAAG;QACnB,oBAAoB,CAAC,EAAE,WAAW,CAChC,gBAAgB,CAAC,sBAAsB,CAAC,CACzC,CAAC;QACF,eAAe,CAAC,EAAE,WAAW,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,CAAC,CAAC;KACpE,GACA,OAAO,CAAC,QAAQ,CAAC;IAWpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACG,UAAU,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,GAAG,CAAC;CAgD3D"}
|
package/dist/wayfinder.js
CHANGED
|
@@ -449,70 +449,6 @@ export class Wayfinder {
|
|
|
449
449
|
* OpenTelemetry tracer instance
|
|
450
450
|
*/
|
|
451
451
|
tracer;
|
|
452
|
-
/**
|
|
453
|
-
* A helper function that resolves a provided url to a target gateway.
|
|
454
|
-
*
|
|
455
|
-
* Note: no verification is done when calling this function.
|
|
456
|
-
* It just generates the redirect url based on the routing strategy.
|
|
457
|
-
* In order to verify the data, you must use the `request` function or request the data and
|
|
458
|
-
* verify it yourself via the `verifyData` function.
|
|
459
|
-
*
|
|
460
|
-
* @example
|
|
461
|
-
* const { resolveUrl } = new Wayfinder();
|
|
462
|
-
*
|
|
463
|
-
* // returns the redirected URL based on the routing strategy and the original url
|
|
464
|
-
* const redirectUrl = await resolveUrl({
|
|
465
|
-
* originalUrl: 'https://arweave.net/<txId>',
|
|
466
|
-
* });
|
|
467
|
-
*
|
|
468
|
-
* // returns the redirected URL based on the routing strategy and the provided arns name
|
|
469
|
-
* const redirectUrl = await resolveUrl({
|
|
470
|
-
* arnsName: 'ardrive',
|
|
471
|
-
* });
|
|
472
|
-
*
|
|
473
|
-
* // returns the redirected URL based on the routing strategy and the provided wayfinder url
|
|
474
|
-
* const redirectUrl = await resolveUrl({
|
|
475
|
-
* wayfinderUrl: 'ar://1234567890',
|
|
476
|
-
* });
|
|
477
|
-
*
|
|
478
|
-
* // returns the redirected URL based on the routing strategy and the provided txId
|
|
479
|
-
* const redirectUrl = await resolveUrl({
|
|
480
|
-
* txId: '1234567890',
|
|
481
|
-
* });
|
|
482
|
-
*
|
|
483
|
-
* window.open(redirectUrl.toString(), '_blank');
|
|
484
|
-
*/
|
|
485
|
-
resolveUrl;
|
|
486
|
-
/**
|
|
487
|
-
* A wrapped fetch function that supports ar:// protocol. If a verification strategy is provided,
|
|
488
|
-
* the request will be verified and events will be emitted as the request is processed.
|
|
489
|
-
*
|
|
490
|
-
* @example
|
|
491
|
-
* const wayfinder = new Wayfinder({
|
|
492
|
-
* verificationStrategy: new HashVerificationStrategy({
|
|
493
|
-
* trustedGateways: [new URL('https://permagate.io')],
|
|
494
|
-
* }),
|
|
495
|
-
* })
|
|
496
|
-
*
|
|
497
|
-
* // request an arns name
|
|
498
|
-
* const response = await wayfinder.request('ar://ardrive')
|
|
499
|
-
*
|
|
500
|
-
* // request a transaction id
|
|
501
|
-
* const response = await wayfinder.request('ar://1234567890')
|
|
502
|
-
*
|
|
503
|
-
* // Set strict mode to true to make verification blocking
|
|
504
|
-
* const wayfinder = new Wayfinder({
|
|
505
|
-
* strict: true,
|
|
506
|
-
* });
|
|
507
|
-
*
|
|
508
|
-
* // This will throw an error if verification fails
|
|
509
|
-
* try {
|
|
510
|
-
* const response = await wayfinder.request('ar://1234567890');
|
|
511
|
-
* } catch (error) {
|
|
512
|
-
* console.error('Verification failed', error);
|
|
513
|
-
* }
|
|
514
|
-
*/
|
|
515
|
-
request;
|
|
516
452
|
/**
|
|
517
453
|
* The logger used by this Wayfinder instance
|
|
518
454
|
*/
|
|
@@ -630,56 +566,10 @@ export class Wayfinder {
|
|
|
630
566
|
const { tracerProvider, tracer } = initTelemetry(this.telemetrySettings) ?? {};
|
|
631
567
|
this.tracerProvider = tracerProvider;
|
|
632
568
|
this.tracer = tracer;
|
|
633
|
-
this.request = wayfinderFetch({
|
|
634
|
-
logger: this.logger,
|
|
635
|
-
emitter: this.emitter,
|
|
636
|
-
gatewaysProvider: this.gatewaysProvider,
|
|
637
|
-
routingSettings: this.routingSettings,
|
|
638
|
-
verificationSettings: this.verificationSettings,
|
|
639
|
-
tracer: this.tracer,
|
|
640
|
-
});
|
|
641
|
-
this.resolveUrl = async (params) => {
|
|
642
|
-
// create a span for the resolveUrl function
|
|
643
|
-
const resolveUrlSpan = this.tracer?.startSpan('wayfinder.resolveUrl', {
|
|
644
|
-
attributes: {
|
|
645
|
-
...Object.entries(params).reduce((acc, [key, value]) => ({
|
|
646
|
-
...acc,
|
|
647
|
-
[`params.${key}`]: value,
|
|
648
|
-
gatewaysProvider: this.gatewaysProvider.constructor.name,
|
|
649
|
-
'routing.strategy': this.routingSettings.strategy?.constructor.name,
|
|
650
|
-
}), {}),
|
|
651
|
-
},
|
|
652
|
-
});
|
|
653
|
-
// parse url span that uses the resolveUrl as the parent span
|
|
654
|
-
const wayfinderUrl = createWayfinderUrl(params);
|
|
655
|
-
resolveUrlSpan?.setAttribute('wayfinderUrl', wayfinderUrl);
|
|
656
|
-
// extract routing information from the original URL
|
|
657
|
-
const { subdomain, path } = extractRoutingInfo(wayfinderUrl);
|
|
658
|
-
resolveUrlSpan?.setAttribute('subdomain', subdomain);
|
|
659
|
-
resolveUrlSpan?.setAttribute('path', path);
|
|
660
|
-
const gateways = await this.gatewaysProvider.getGateways();
|
|
661
|
-
resolveUrlSpan?.setAttribute('gatewaysCount', gateways.length);
|
|
662
|
-
const selectedGateway = await this.routingSettings.strategy.selectGateway({
|
|
663
|
-
gateways,
|
|
664
|
-
path,
|
|
665
|
-
subdomain,
|
|
666
|
-
});
|
|
667
|
-
resolveUrlSpan?.setAttribute('selectedGateway', selectedGateway.toString());
|
|
668
|
-
const constructedGatewayUrl = constructGatewayUrl({
|
|
669
|
-
selectedGateway,
|
|
670
|
-
subdomain,
|
|
671
|
-
path,
|
|
672
|
-
});
|
|
673
|
-
resolveUrlSpan?.setAttribute('constructedGatewayUrl', constructedGatewayUrl.toString());
|
|
674
|
-
resolveUrlSpan?.end();
|
|
675
|
-
// return the constructed gateway url
|
|
676
|
-
return constructedGatewayUrl;
|
|
677
|
-
};
|
|
678
569
|
// send an event that the wayfinder is initialized
|
|
679
570
|
this.tracer
|
|
680
571
|
?.startSpan('wayfinder.initialized', {
|
|
681
572
|
attributes: {
|
|
682
|
-
gatewaysProvider: this.gatewaysProvider.constructor.name,
|
|
683
573
|
'verification.strategy': this.verificationSettings.strategy?.constructor.name,
|
|
684
574
|
'verification.enabled': this.verificationSettings.enabled,
|
|
685
575
|
'verification.trustedGateways': this.verificationSettings.strategy?.trustedGateways.map((gateway) => gateway.toString()),
|
|
@@ -690,4 +580,175 @@ export class Wayfinder {
|
|
|
690
580
|
})
|
|
691
581
|
.end();
|
|
692
582
|
}
|
|
583
|
+
/**
|
|
584
|
+
* Sets the routing strategy to use for routing requests.
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* const wayfinder = new Wayfinder();
|
|
588
|
+
* wayfinder.setRoutingStrategy(new RandomRoutingStrategy());
|
|
589
|
+
*
|
|
590
|
+
* @param strategy - The routing strategy to use
|
|
591
|
+
*/
|
|
592
|
+
setRoutingStrategy(strategy) {
|
|
593
|
+
this.routingSettings.strategy = strategy;
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Sets the verification strategy to use for verifying requests.
|
|
597
|
+
*
|
|
598
|
+
* @example
|
|
599
|
+
* const wayfinder = new Wayfinder();
|
|
600
|
+
* wayfinder.setVerificationStrategy(new HashVerificationStrategy({
|
|
601
|
+
* trustedGateways: [new URL('https://permagate.io')],
|
|
602
|
+
* }));
|
|
603
|
+
*
|
|
604
|
+
* @param strategy - The verification strategy to use
|
|
605
|
+
*/
|
|
606
|
+
setVerificationStrategy(strategy) {
|
|
607
|
+
this.verificationSettings.strategy = strategy;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Disables verification for requests.
|
|
611
|
+
*
|
|
612
|
+
* @example
|
|
613
|
+
* const wayfinder = new Wayfinder({
|
|
614
|
+
* verificationSettings: {
|
|
615
|
+
* enabled: true,
|
|
616
|
+
* },
|
|
617
|
+
* });
|
|
618
|
+
*
|
|
619
|
+
* // disable verification
|
|
620
|
+
* wayfinder.disableVerification();
|
|
621
|
+
*
|
|
622
|
+
* // enable verification with strict mode
|
|
623
|
+
* wayfinder.enableVerification({ strict: true });
|
|
624
|
+
*/
|
|
625
|
+
disableVerification() {
|
|
626
|
+
this.verificationSettings.enabled = false;
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Enables verification for requests.
|
|
630
|
+
*
|
|
631
|
+
* @example
|
|
632
|
+
* const wayfinder = new Wayfinder({
|
|
633
|
+
* verificationSettings: {
|
|
634
|
+
* enabled: false,
|
|
635
|
+
* },
|
|
636
|
+
* });
|
|
637
|
+
*
|
|
638
|
+
* // enable verification with strict mode
|
|
639
|
+
* wayfinder.enableVerification({ strict: true });
|
|
640
|
+
*
|
|
641
|
+
* // enable verification without strict mode
|
|
642
|
+
* wayfinder.enableVerification();
|
|
643
|
+
* @param strict - Whether to make verification strict
|
|
644
|
+
*/
|
|
645
|
+
enableVerification({ strict = false, } = {}) {
|
|
646
|
+
this.verificationSettings.enabled = true;
|
|
647
|
+
this.verificationSettings.strict = strict;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* A wrapped fetch function that supports ar:// protocol. If a verification strategy is provided,
|
|
651
|
+
* the request will be verified and events will be emitted as the request is processed.
|
|
652
|
+
*
|
|
653
|
+
* @example
|
|
654
|
+
* const wayfinder = new Wayfinder({
|
|
655
|
+
* verificationStrategy: new HashVerificationStrategy({
|
|
656
|
+
* trustedGateways: [new URL('https://permagate.io')],
|
|
657
|
+
* }),
|
|
658
|
+
* })
|
|
659
|
+
*
|
|
660
|
+
* // request an arns name
|
|
661
|
+
* const response = await wayfinder.request('ar://ardrive')
|
|
662
|
+
*
|
|
663
|
+
* // request a transaction id
|
|
664
|
+
* const response = await wayfinder.request('ar://1234567890')
|
|
665
|
+
*
|
|
666
|
+
* // Set strict mode to true to make verification blocking
|
|
667
|
+
* const wayfinder = new Wayfinder({
|
|
668
|
+
* strict: true,
|
|
669
|
+
* });
|
|
670
|
+
*
|
|
671
|
+
* // This will throw an error if verification fails
|
|
672
|
+
* try {
|
|
673
|
+
* const response = await wayfinder.request('ar://1234567890');
|
|
674
|
+
* } catch (error) {
|
|
675
|
+
* console.error('Verification failed', error);
|
|
676
|
+
* }
|
|
677
|
+
*/
|
|
678
|
+
request(input, init) {
|
|
679
|
+
return wayfinderFetch({
|
|
680
|
+
logger: this.logger,
|
|
681
|
+
gatewaysProvider: this.gatewaysProvider,
|
|
682
|
+
emitter: this.emitter,
|
|
683
|
+
routingSettings: this.routingSettings,
|
|
684
|
+
verificationSettings: this.verificationSettings,
|
|
685
|
+
tracer: this.tracer,
|
|
686
|
+
})(input, init);
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* A helper function that resolves a provided url to a target gateway using the current routing strategy.
|
|
690
|
+
*
|
|
691
|
+
* Note: no verification is done when calling this function.
|
|
692
|
+
* It just generates the redirect url based on the routing strategy.
|
|
693
|
+
* In order to verify the data, you must use the `request` function or request the data and
|
|
694
|
+
* verify it yourself via the `verifyData` function.
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* const { resolveUrl } = new Wayfinder();
|
|
698
|
+
*
|
|
699
|
+
* // returns the redirected URL based on the routing strategy and the original url
|
|
700
|
+
* const redirectUrl = await resolveUrl({
|
|
701
|
+
* originalUrl: 'https://arweave.net/<txId>',
|
|
702
|
+
* });
|
|
703
|
+
*
|
|
704
|
+
* // returns the redirected URL based on the routing strategy and the provided arns name
|
|
705
|
+
* const redirectUrl = await resolveUrl({
|
|
706
|
+
* arnsName: 'ardrive',
|
|
707
|
+
* });
|
|
708
|
+
*
|
|
709
|
+
* // returns the redirected URL based on the routing strategy and the provided wayfinder url
|
|
710
|
+
* const redirectUrl = await resolveUrl({
|
|
711
|
+
* wayfinderUrl: 'ar://1234567890',
|
|
712
|
+
* });
|
|
713
|
+
*
|
|
714
|
+
* // returns the redirected URL based on the routing strategy and the provided txId
|
|
715
|
+
* const redirectUrl = await resolveUrl({
|
|
716
|
+
* txId: '1234567890',
|
|
717
|
+
* });
|
|
718
|
+
*
|
|
719
|
+
* window.open(redirectUrl.toString(), '_blank');
|
|
720
|
+
*/
|
|
721
|
+
async resolveUrl(params) {
|
|
722
|
+
// create a span for the resolveUrl function
|
|
723
|
+
const resolveUrlSpan = this.tracer?.startSpan('wayfinder.resolveUrl', {
|
|
724
|
+
attributes: {
|
|
725
|
+
...Object.entries(params).reduce((acc, [key, value]) => ({
|
|
726
|
+
...acc,
|
|
727
|
+
[`params.${key}`]: value,
|
|
728
|
+
'routing.strategy': this.routingSettings.strategy?.constructor.name,
|
|
729
|
+
}), {}),
|
|
730
|
+
},
|
|
731
|
+
});
|
|
732
|
+
// parse url span that uses the resolveUrl as the parent span
|
|
733
|
+
const wayfinderUrl = createWayfinderUrl(params);
|
|
734
|
+
resolveUrlSpan?.setAttribute('wayfinderUrl', wayfinderUrl);
|
|
735
|
+
// extract routing information from the original URL
|
|
736
|
+
const { subdomain, path } = extractRoutingInfo(wayfinderUrl);
|
|
737
|
+
resolveUrlSpan?.setAttribute('subdomain', subdomain);
|
|
738
|
+
resolveUrlSpan?.setAttribute('path', path);
|
|
739
|
+
const selectedGateway = await this.routingSettings.strategy.selectGateway({
|
|
740
|
+
path,
|
|
741
|
+
subdomain,
|
|
742
|
+
});
|
|
743
|
+
resolveUrlSpan?.setAttribute('selectedGateway', selectedGateway.toString());
|
|
744
|
+
const constructedGatewayUrl = constructGatewayUrl({
|
|
745
|
+
selectedGateway,
|
|
746
|
+
subdomain,
|
|
747
|
+
path,
|
|
748
|
+
});
|
|
749
|
+
resolveUrlSpan?.setAttribute('constructedGatewayUrl', constructedGatewayUrl.toString());
|
|
750
|
+
resolveUrlSpan?.end();
|
|
751
|
+
// return the constructed gateway url
|
|
752
|
+
return constructedGatewayUrl;
|
|
753
|
+
}
|
|
693
754
|
}
|