@ar.io/wayfinder-core 0.0.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.
Files changed (51) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +318 -0
  3. package/dist/gateways/network.d.ts +39 -0
  4. package/dist/gateways/network.d.ts.map +1 -0
  5. package/dist/gateways/network.js +62 -0
  6. package/dist/gateways/simple-cache.d.ts +51 -0
  7. package/dist/gateways/simple-cache.d.ts.map +1 -0
  8. package/dist/gateways/simple-cache.js +66 -0
  9. package/dist/gateways/static.d.ts +26 -0
  10. package/dist/gateways/static.d.ts.map +1 -0
  11. package/dist/gateways/static.js +9 -0
  12. package/dist/index.d.ts +29 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +32 -0
  15. package/dist/routing/ping.d.ts +19 -0
  16. package/dist/routing/ping.d.ts.map +1 -0
  17. package/dist/routing/ping.js +77 -0
  18. package/dist/routing/preferred-with-fallback.d.ts +34 -0
  19. package/dist/routing/preferred-with-fallback.d.ts.map +1 -0
  20. package/dist/routing/preferred-with-fallback.js +41 -0
  21. package/dist/routing/random.d.ts +24 -0
  22. package/dist/routing/random.d.ts.map +1 -0
  23. package/dist/routing/random.js +9 -0
  24. package/dist/routing/round-robin.d.ts +50 -0
  25. package/dist/routing/round-robin.d.ts.map +1 -0
  26. package/dist/routing/round-robin.js +41 -0
  27. package/dist/routing/static.d.ts +49 -0
  28. package/dist/routing/static.d.ts.map +1 -0
  29. package/dist/routing/static.js +37 -0
  30. package/dist/utils/ario.d.ts +28 -0
  31. package/dist/utils/ario.d.ts.map +1 -0
  32. package/dist/utils/ario.js +27 -0
  33. package/dist/utils/base64.d.ts +21 -0
  34. package/dist/utils/base64.d.ts.map +1 -0
  35. package/dist/utils/base64.js +51 -0
  36. package/dist/utils/hash.d.ts +10 -0
  37. package/dist/utils/hash.d.ts.map +1 -0
  38. package/dist/utils/hash.js +81 -0
  39. package/dist/utils/random.d.ts +25 -0
  40. package/dist/utils/random.d.ts.map +1 -0
  41. package/dist/utils/random.js +26 -0
  42. package/dist/verification/data-root-verifier.d.ts +28 -0
  43. package/dist/verification/data-root-verifier.d.ts.map +1 -0
  44. package/dist/verification/data-root-verifier.js +129 -0
  45. package/dist/verification/hash-verifier.d.ts +28 -0
  46. package/dist/verification/hash-verifier.d.ts.map +1 -0
  47. package/dist/verification/hash-verifier.js +108 -0
  48. package/dist/wayfinder.d.ts +301 -0
  49. package/dist/wayfinder.d.ts.map +1 -0
  50. package/dist/wayfinder.js +498 -0
  51. package/package.json +44 -0
@@ -0,0 +1,301 @@
1
+ /**
2
+ * WayFinder
3
+ * Copyright (C) 2022-2025 Permanent Data Solutions, Inc. All Rights Reserved.
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Affero General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU Affero General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Affero General Public License
16
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+ import { EventEmitter } from 'eventemitter3';
19
+ import { DataVerificationStrategy, GatewaysProvider, RoutingStrategy } from '../types/wayfinder.js';
20
+ type WayfinderHttpClient = typeof fetch;
21
+ /**
22
+ * Simple logger interface that Wayfinder will use
23
+ * This allows users to provide their own logger implementation
24
+ */
25
+ export interface Logger {
26
+ debug: (message: string, ...args: any[]) => void;
27
+ info: (message: string, ...args: any[]) => void;
28
+ warn: (message: string, ...args: any[]) => void;
29
+ error: (message: string, ...args: any[]) => void;
30
+ }
31
+ /**
32
+ * Default console logger implementation
33
+ */
34
+ export declare const defaultLogger: Logger;
35
+ export declare const arnsRegex: RegExp;
36
+ export declare const txIdRegex: RegExp;
37
+ /**
38
+ * Core function that converts a wayfinder url to the proper ar-io gateway URL
39
+ * @param originalUrl - the wayfinder url to resolve
40
+ * @param selectedGateway - the target gateway to resolve the url against
41
+ * @returns the resolved url that can be used to make a request
42
+ */
43
+ export declare const resolveWayfinderUrl: ({ originalUrl, selectedGateway, logger, }: {
44
+ originalUrl: string;
45
+ selectedGateway: URL;
46
+ logger?: Logger;
47
+ }) => URL;
48
+ /**
49
+ * Wayfinder event emitter with verification events
50
+ */
51
+ export type WayfinderEvent = {
52
+ 'verification-succeeded': {
53
+ txId: string;
54
+ };
55
+ 'verification-failed': Error;
56
+ 'verification-skipped': {
57
+ originalUrl: string;
58
+ };
59
+ 'verification-progress': {
60
+ txId: string;
61
+ processedBytes: number;
62
+ totalBytes: number;
63
+ };
64
+ 'routing-started': {
65
+ originalUrl: string;
66
+ };
67
+ 'routing-skipped': {
68
+ originalUrl: string;
69
+ };
70
+ 'routing-succeeded': {
71
+ originalUrl: string;
72
+ selectedGateway: string;
73
+ redirectUrl: string;
74
+ };
75
+ 'routing-failed': Error;
76
+ 'identified-transaction-id': {
77
+ originalUrl: string;
78
+ selectedGateway: string;
79
+ txId: string;
80
+ };
81
+ };
82
+ export interface WayfinderEventArgs {
83
+ onVerificationSucceeded?: (payload: WayfinderEvent['verification-succeeded']) => void;
84
+ onVerificationFailed?: (payload: WayfinderEvent['verification-failed']) => void;
85
+ onVerificationProgress?: (payload: WayfinderEvent['verification-progress']) => void;
86
+ }
87
+ export declare class WayfinderEmitter extends EventEmitter<WayfinderEvent> {
88
+ constructor({ onVerificationSucceeded, onVerificationFailed, onVerificationProgress, }?: WayfinderEventArgs);
89
+ }
90
+ export declare function tapAndVerifyReadableStream({ originalStream, contentLength, verifyData, txId, emitter, strict, }: {
91
+ originalStream: ReadableStream;
92
+ contentLength: number;
93
+ verifyData: DataVerificationStrategy['verifyData'];
94
+ txId: string;
95
+ emitter?: WayfinderEmitter;
96
+ strict?: boolean;
97
+ }): ReadableStream;
98
+ /**
99
+ * Gets the sandbox hash for a given transaction id
100
+ */
101
+ export declare function sandboxFromId(id: string): string;
102
+ /**
103
+ * Creates a wrapped fetch function that supports ar:// protocol
104
+ *
105
+ * This function leverages a Proxy to intercept calls to fetch
106
+ * and redirects them to the target gateway using the resolveUrl function.
107
+ *
108
+ * Any URLs provided that are not wayfinder urls will be passed directly to fetch.
109
+ *
110
+ * @param resolveUrl - the function to construct the redirect url for ar:// requests
111
+ * @returns a wrapped fetch function that supports ar:// protocol and always returns Response
112
+ */
113
+ export declare const createWayfinderClient: ({ resolveUrl, verifyData, selectGateway, emitter, logger, strict, }: {
114
+ selectGateway: () => Promise<URL>;
115
+ resolveUrl: (params: {
116
+ originalUrl: string;
117
+ selectedGateway: URL;
118
+ logger?: Logger;
119
+ }) => URL;
120
+ verifyData?: DataVerificationStrategy["verifyData"];
121
+ logger?: Logger;
122
+ emitter?: WayfinderEmitter;
123
+ strict?: boolean;
124
+ }) => (input: URL | RequestInfo, init?: RequestInit) => Promise<Response>;
125
+ /**
126
+ * Configuration options for the Wayfinder
127
+ */
128
+ export interface WayfinderOptions {
129
+ /**
130
+ * Logger to use for logging
131
+ * @default defaultLogger (standard console logger)
132
+ */
133
+ logger?: Logger;
134
+ /**
135
+ * The gateways provider to use for routing requests
136
+ */
137
+ gatewaysProvider: GatewaysProvider;
138
+ /**
139
+ * The routing strategy to use for selecting gateways
140
+ * @default FastestPingRoutingStrategy with timeoutMs=1000
141
+ */
142
+ routingStrategy?: RoutingStrategy;
143
+ /**
144
+ * The verification strategy to use for verifying data
145
+ * @default HashVerificationStrategy with TrustedGatewaysHashProvider
146
+ */
147
+ verificationStrategy?: DataVerificationStrategy;
148
+ /**
149
+ * Event handlers for verification events
150
+ */
151
+ events?: WayfinderEventArgs;
152
+ /**
153
+ * Whether verification should be strict (blocking)
154
+ * If true, verification failures will cause requests to fail
155
+ * If false, verification will be performed asynchronously
156
+ * @default false
157
+ */
158
+ strict?: boolean;
159
+ }
160
+ /**
161
+ * The main class for the wayfinder
162
+ */
163
+ export declare class Wayfinder {
164
+ /**
165
+ * The gateways provider is responsible for providing the list of gateways to use for routing requests.
166
+ *
167
+ * @example
168
+ * const wayfinder = new Wayfinder({
169
+ * gatewaysProvider: new SimpleCacheGatewaysProvider({
170
+ * gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
171
+ * ttlSeconds: 60 * 60 * 24, // 1 day
172
+ * }),
173
+ * });
174
+ */
175
+ readonly gatewaysProvider: GatewaysProvider;
176
+ /**
177
+ * The routing strategy to use when routing requests.
178
+ *
179
+ * @example
180
+ * const wayfinder = new Wayfinder({
181
+ * strategy: new FastestPingStrategy({
182
+ * timeoutMs: 1000,
183
+ * }),
184
+ * });
185
+ */
186
+ readonly routingStrategy: RoutingStrategy;
187
+ /**
188
+ * A helper function that resolves the redirect url for ar:// requests to a target gateway.
189
+ *
190
+ * Note: no verification is done when resolving an ar://<path> url to a wayfinder route.
191
+ * In order to verify the data, you must use the `request` function or request the data and
192
+ * verify it yourself via the `verifyData` function.
193
+ *
194
+ * @example
195
+ * const { resolveUrl } = new Wayfinder();
196
+ *
197
+ * // returns the redirected URL based on the routing strategy and the original url
198
+ * const redirectUrl = await resolveUrl({ originalUrl: 'ar://example' });
199
+ *
200
+ * window.open(redirectUrl.toString(), '_blank');
201
+ */
202
+ readonly resolveUrl: (params: {
203
+ originalUrl: string;
204
+ logger?: Logger;
205
+ }) => Promise<URL>;
206
+ /**
207
+ * A wrapped fetch function that supports ar:// protocol. If a verification strategy is provided,
208
+ * the request will be verified and events will be emitted as the request is processed.
209
+ *
210
+ * @example
211
+ * const wayfinder = new Wayfinder({
212
+ * verificationStrategy: new HashVerificationStrategy({
213
+ * trustedHashProvider: new TrustedGatewaysHashProvider({
214
+ * gatewaysProvider: new StaticGatewaysProvider({
215
+ * gateways: ['https://permagate.io'],
216
+ * }),
217
+ * }),
218
+ * }),
219
+ * })
220
+ *
221
+ * // request an arns name
222
+ * const response = await wayfinder.request('ar://ardrive')
223
+ *
224
+ * // request a transaction id
225
+ * const response = await wayfinder.request('ar://1234567890')
226
+ *
227
+ * // Set strict mode to true to make verification blocking
228
+ * const wayfinder = new Wayfinder({
229
+ * strict: true,
230
+ * });
231
+ *
232
+ * // This will throw an error if verification fails
233
+ * try {
234
+ * const response = await wayfinder.request('ar://1234567890');
235
+ * } catch (error) {
236
+ * console.error('Verification failed', error);
237
+ * }
238
+ */
239
+ readonly request: WayfinderHttpClient;
240
+ /**
241
+ * The function that verifies the data hash for a given transaction id.
242
+ *
243
+ * @example
244
+ * const wayfinder = new Wayfinder({
245
+ * verifyData: (data, txId) => {
246
+ * // some custom verification logic
247
+ * return true;
248
+ * },
249
+ * });
250
+ */
251
+ readonly verifyData: DataVerificationStrategy['verifyData'];
252
+ /**
253
+ * Whether verification should be strict (blocking) or not.
254
+ * If true, verification failures will cause requests to fail.
255
+ * If false, verification will be performed asynchronously and failures will only emit events.
256
+ */
257
+ readonly strict: boolean;
258
+ /**
259
+ * The logger used by this Wayfinder instance
260
+ */
261
+ readonly logger: Logger;
262
+ /**
263
+ * The event emitter for wayfinder that emits verification events.
264
+ *
265
+ * const wayfinder = new Wayfinder()
266
+ *
267
+ * wayfinder.emitter.on('verification-succeeded', (event) => {
268
+ * console.log('Verification passed!', event);
269
+ * })
270
+ *
271
+ * wayfinder.emitter.on('verification-failed', (event) => {
272
+ * console.log('Verification failed!', event);
273
+ * })
274
+ *
275
+ * or implement the events interface and pass it in, using callback functions
276
+ *
277
+ * const wayfinder = new Wayfinder({
278
+ * events: {
279
+ * onVerificationPassed: (event) => {
280
+ * console.log('Verification passed!', event);
281
+ * },
282
+ * onVerificationFailed: (event) => {
283
+ * console.log('Verification failed!', event);
284
+ * },
285
+ * onVerificationProgress: (event) => {
286
+ * console.log('Verification progress!', event);
287
+ * },
288
+ * }
289
+ * })
290
+ *
291
+ * const response = await wayfinder.request('ar://example');
292
+ */
293
+ readonly emitter: WayfinderEmitter;
294
+ /**
295
+ * The constructor for the wayfinder
296
+ * @param options - Wayfinder configuration options
297
+ */
298
+ constructor({ logger, gatewaysProvider, routingStrategy, verificationStrategy, events, strict, }: WayfinderOptions);
299
+ }
300
+ export {};
301
+ //# sourceMappingURL=wayfinder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wayfinder.d.ts","sourceRoot":"","sources":["../src/wayfinder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAG7C,OAAO,EACL,wBAAwB,EACxB,gBAAgB,EAChB,eAAe,EAChB,MAAM,uBAAuB,CAAC;AAK/B,KAAK,mBAAmB,GAAG,OAAO,KAAK,CAAC;AAExC;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IACjD,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAChD,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAChD,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAClD;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,MAK3B,CAAC;AAGF,eAAO,MAAM,SAAS,QAAuB,CAAC;AAC9C,eAAO,MAAM,SAAS,QAAwB,CAAC;AAE/C;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,GAAI,2CAIjC;IACD,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,GAAG,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,KAAG,GAgDH,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,wBAAw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