@ar.io/sdk 3.11.0-alpha.7 → 3.11.0-alpha.8

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 (28) hide show
  1. package/bundles/web.bundle.min.js +119 -116
  2. package/lib/cjs/common/wayfinder/gateways/trusted-gateways.js +106 -0
  3. package/lib/cjs/common/wayfinder/index.js +6 -0
  4. package/lib/cjs/common/wayfinder/verification/data-root-verifier.js +139 -0
  5. package/lib/cjs/common/wayfinder/verification/hash-verifier.js +50 -0
  6. package/lib/cjs/common/wayfinder/wayfinder.js +407 -18
  7. package/lib/cjs/common/wayfinder/wayfinder.test.js +262 -3
  8. package/lib/cjs/types/wayfinder.js +1 -0
  9. package/lib/cjs/utils/hash.js +56 -0
  10. package/lib/cjs/version.js +1 -1
  11. package/lib/esm/common/wayfinder/gateways/trusted-gateways.js +102 -0
  12. package/lib/esm/common/wayfinder/index.js +6 -0
  13. package/lib/esm/common/wayfinder/verification/data-root-verifier.js +130 -0
  14. package/lib/esm/common/wayfinder/verification/hash-verifier.js +46 -0
  15. package/lib/esm/common/wayfinder/wayfinder.js +401 -18
  16. package/lib/esm/common/wayfinder/wayfinder.test.js +263 -4
  17. package/lib/esm/types/wayfinder.js +1 -0
  18. package/lib/esm/utils/hash.js +50 -0
  19. package/lib/esm/version.js +1 -1
  20. package/lib/types/common/wayfinder/gateways/trusted-gateways.d.ts +51 -0
  21. package/lib/types/common/wayfinder/index.d.ts +3 -0
  22. package/lib/types/common/wayfinder/verification/data-root-verifier.d.ts +31 -0
  23. package/lib/types/common/wayfinder/verification/hash-verifier.d.ts +27 -0
  24. package/lib/types/common/wayfinder/wayfinder.d.ts +148 -10
  25. package/lib/types/types/wayfinder.d.ts +43 -0
  26. package/lib/types/utils/hash.d.ts +4 -0
  27. package/lib/types/version.d.ts +1 -1
  28. package/package.json +1 -1
@@ -13,9 +13,11 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import { WayfinderRouter } from '../../types/wayfinder.js';
16
+ import EventEmitter from 'node:events';
17
+ import { PassThrough, Readable } from 'node:stream';
18
+ import { DataHashProvider, DataVerifier, WayfinderRouter } from '../../types/wayfinder.js';
17
19
  import { Logger } from '../logger.js';
18
- type HttpClientArgs = [string | URL, ...unknown[]];
20
+ type HttpClientArgs = unknown[];
19
21
  type HttpClientFunction = (...args: HttpClientArgs) => unknown;
20
22
  type WayfinderHttpClient<T extends HttpClientFunction> = T;
21
23
  export declare const arnsRegex: RegExp;
@@ -31,6 +33,69 @@ export declare const resolveWayfinderUrl: ({ originalUrl, targetGateway, logger,
31
33
  targetGateway: () => Promise<string | URL>;
32
34
  logger?: Logger;
33
35
  }) => Promise<URL>;
36
+ /**
37
+ * Wayfinder event emitter with verification events
38
+ */
39
+ export type WayfinderEvent = {
40
+ type: 'verification-passed';
41
+ txId: string;
42
+ } | {
43
+ type: 'verification-failed';
44
+ txId: string;
45
+ error: Error;
46
+ } | {
47
+ type: 'verification-skipped';
48
+ originalUrl: string;
49
+ } | {
50
+ type: 'verification-progress';
51
+ txId: string;
52
+ processedBytes: number;
53
+ totalBytes: number;
54
+ } | {
55
+ type: 'routing-started';
56
+ originalUrl: string;
57
+ } | {
58
+ type: 'routing-succeeded';
59
+ originalUrl: string;
60
+ targetGateway: string;
61
+ } | {
62
+ type: 'routing-failed';
63
+ originalUrl: string;
64
+ error: Error;
65
+ } | {
66
+ type: 'identified-transaction-id';
67
+ originalUrl: string;
68
+ targetGateway: string;
69
+ txId: string;
70
+ };
71
+ export interface WayfinderEventArgs {
72
+ onVerificationPassed?: (payload: Omit<Extract<WayfinderEvent, {
73
+ type: 'verification-passed';
74
+ }>, 'type'>) => void;
75
+ onVerificationFailed?: (payload: Omit<Extract<WayfinderEvent, {
76
+ type: 'verification-failed';
77
+ }>, 'type'>) => void;
78
+ onVerificationProgress?: (payload: Omit<Extract<WayfinderEvent, {
79
+ type: 'verification-progress';
80
+ }>, 'type'>) => void;
81
+ }
82
+ export declare class WayfinderEmitter extends EventEmitter {
83
+ constructor({ onVerificationPassed, onVerificationFailed, onVerificationProgress, }?: WayfinderEventArgs);
84
+ emit<E extends WayfinderEvent['type']>(event: E, payload: Omit<Extract<WayfinderEvent, {
85
+ type: E;
86
+ }>, 'type'>): boolean;
87
+ on<E extends WayfinderEvent['type']>(event: E, listener: (payload: Omit<Extract<WayfinderEvent, {
88
+ type: E;
89
+ }>, 'type'>) => void): this;
90
+ }
91
+ export declare function tapAndVerifyStream<T extends Readable | ReadableStream>({ originalStream, contentLength, verifyData, txId, emitter, }: {
92
+ originalStream: T;
93
+ contentLength: number;
94
+ verifyData: DataVerifier['verifyData'];
95
+ txId: string;
96
+ emitter?: WayfinderEmitter;
97
+ }): T extends Readable ? PassThrough : T;
98
+ export declare function wrapVerifiedResponse(original: Response, newBody: ReadableStream<Uint8Array>, txId: string): Response;
34
99
  /**
35
100
  * Creates a wrapped http client that supports ar:// protocol
36
101
  *
@@ -44,13 +109,18 @@ export declare const resolveWayfinderUrl: ({ originalUrl, targetGateway, logger,
44
109
  * @param resolveUrl - the function to construct the redirect url for ar:// requests
45
110
  * @returns a wrapped http client that supports ar:// protocol
46
111
  */
47
- export declare const createWayfinderClient: <T extends HttpClientFunction>({ httpClient, resolveUrl, logger, }: {
112
+ export declare const createWayfinderClient: <T extends HttpClientFunction>({ httpClient, resolveUrl, verifyData, emitter, logger, }: {
48
113
  httpClient: T;
49
114
  resolveUrl: (params: {
50
115
  originalUrl: string | URL;
51
116
  logger?: Logger;
52
117
  }) => Promise<URL>;
118
+ verifyData?: <T_1 extends Readable | ReadableStream | Buffer>({ data, txId, }: {
119
+ data: T_1;
120
+ txId: string;
121
+ }) => Promise<void>;
53
122
  logger?: Logger;
123
+ emitter?: WayfinderEmitter;
54
124
  }) => WayfinderHttpClient<T>;
55
125
  /**
56
126
  * The main class for the wayfinder
@@ -65,21 +135,31 @@ export declare class Wayfinder<T extends HttpClientFunction> {
65
135
  * @example
66
136
  * const wayfinder = new Wayfinder({
67
137
  * router: new RandomGatewayRouter({
68
- * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
138
+ * gatewaysProvider: new SimpleCacheGatewaysProvider({
139
+ * gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
140
+ * ttlSeconds: 60 * 60 * 24, // 1 day
141
+ * }),
69
142
  * }),
70
143
  * });
144
+ *
145
+ * // Returns a target gateway based on the routing strategy
146
+ * const targetGateway = await wayfinder.router.getTargetGateway();
71
147
  */
72
148
  readonly router: WayfinderRouter;
73
149
  /**
74
- * The http client to use for requests
150
+ * The native http client used by wayfinder
75
151
  *
76
152
  * @example
77
153
  * const wayfinder = new Wayfinder({
78
154
  * router: new RandomGatewayRouter({
79
- * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
155
+ * gatewaysProvider: new SimpleCacheGatewaysProvider({
156
+ * gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
157
+ * ttlSeconds: 60 * 60 * 24, // 1 day
158
+ * }),
80
159
  * }),
81
160
  * httpClient: axios,
82
161
  * });
162
+ *
83
163
  */
84
164
  readonly httpClient: T;
85
165
  /**
@@ -88,11 +168,15 @@ export declare class Wayfinder<T extends HttpClientFunction> {
88
168
  * @example
89
169
  * const wayfinder = new Wayfinder({
90
170
  * router: new RandomGatewayRouter({
91
- * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
171
+ * gatewaysProvider: new SimpleCacheGatewaysProvider({
172
+ * gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
173
+ * ttlSeconds: 60 * 60 * 24, // 1 day
174
+ * }),
92
175
  * }),
93
176
  * httpClient: axios,
94
177
  * });
95
178
  *
179
+ * // returns the redirected URL based on the routing strategy and the original url
96
180
  * const redirectUrl = await wayfinder.resolveUrl({ originalUrl: 'ar://example' });
97
181
  */
98
182
  readonly resolveUrl: (params: {
@@ -105,7 +189,10 @@ export declare class Wayfinder<T extends HttpClientFunction> {
105
189
  * @example
106
190
  * const { request: wayfind } = new Wayfinder({
107
191
  * router: new RandomGatewayRouter({
108
- * gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() })
192
+ * gatewaysProvider: new SimpleCacheGatewaysProvider({
193
+ * gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
194
+ * ttlSeconds: 60 * 60 * 24, // 1 day
195
+ * }),
109
196
  * }),
110
197
  * httpClient: axios,
111
198
  * });;
@@ -118,10 +205,61 @@ export declare class Wayfinder<T extends HttpClientFunction> {
118
205
  * })
119
206
  */
120
207
  readonly request: WayfinderHttpClient<T>;
121
- constructor({ router, httpClient, logger, }: {
122
- router: WayfinderRouter;
208
+ readonly verifyData: DataVerifier['verifyData'];
209
+ /**
210
+ * The event emitter for wayfinder that emits verification events.
211
+ *
212
+ * const wayfinder = new Wayfinder()
213
+ *
214
+ * wayfinder.emitter.on('verification-passed', (event) => {
215
+ * console.log('Verification passed!', event);
216
+ * })
217
+ *
218
+ * wayfinder.emitter.on('verification-failed', (event) => {
219
+ * console.log('Verification failed!', event);
220
+ * })
221
+ *
222
+ * or implement the events interface and pass it in, using callback functions
223
+ *
224
+ * const wayfinder = new Wayfinder({
225
+ * events: {
226
+ * onVerificationPassed: (event) => {
227
+ * console.log('Verification passed!', event);
228
+ * },
229
+ * onVerificationFailed: (event) => {
230
+ * console.log('Verification failed!', event);
231
+ * },
232
+ * onVerificationProgress: (event) => {
233
+ * console.log('Verification progress!', event);
234
+ * },
235
+ * }
236
+ * })
237
+ *
238
+ * const response = await wayfind('ar://example');
239
+ */
240
+ readonly emitter: WayfinderEmitter;
241
+ constructor({ httpClient, router, logger, verifier, events, }: {
123
242
  httpClient: T;
243
+ router?: WayfinderRouter;
124
244
  logger?: Logger;
245
+ verifier?: DataVerifier;
246
+ hashProvider?: DataHashProvider;
247
+ events?: WayfinderEventArgs;
125
248
  });
126
249
  }
127
250
  export {};
251
+ /**
252
+ *
253
+ * type | complexity | performance | security
254
+ * ---------|------------|-------------|---------
255
+ * hash | low | high | low
256
+ * ---------|------------|-------------|---------
257
+ * data root | medium | medium | low | only L1
258
+ * ---------|------------|-------------|---------
259
+ * signature | medium | medium | medium
260
+ * ---------|------------|-------------|---------
261
+ * composite | high | low | high
262
+ * ---------|------------|-------------|---------
263
+ *
264
+ *
265
+ */
@@ -13,7 +13,50 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
+ import { Readable } from 'stream';
16
17
  export interface WayfinderRouter {
17
18
  readonly name: string;
18
19
  getTargetGateway: () => Promise<URL>;
19
20
  }
21
+ export interface DataVerifier {
22
+ /**
23
+ * Verifies the provided data for a given txId
24
+ *
25
+ * Depending on the implementation, the hash can be the computed data root of a transaction, the digest of the data, or some other hash of the data.
26
+ *
27
+ * The interface is intended to be vague in order to support various degrees of verification.
28
+ *
29
+ * @param data - The data to verify
30
+ * @param txId - The txId of the data
31
+ * @returns the hash of the data
32
+ */
33
+ verifyData: ({ data, txId, }: {
34
+ data: Buffer | Readable | ReadableStream;
35
+ txId: string;
36
+ }) => Promise<void>;
37
+ }
38
+ export interface DataHashProvider {
39
+ /**
40
+ * Returns a hash for the provided txId using the specified algorithm.
41
+ *
42
+ * @param txId - The txId of the data
43
+ * @returns the hash of the data
44
+ */
45
+ getHash: ({ txId, }: {
46
+ txId: string;
47
+ }) => Promise<{
48
+ hash: string;
49
+ algorithm: 'sha256';
50
+ }>;
51
+ }
52
+ export interface DataRootProvider {
53
+ /**
54
+ * Returns the data root for the provided txId
55
+ *
56
+ * @param txId - The txId of the data
57
+ * @returns the data root of the data
58
+ */
59
+ getDataRoot: ({ txId }: {
60
+ txId: string;
61
+ }) => Promise<string>;
62
+ }
@@ -0,0 +1,4 @@
1
+ import { Readable } from 'stream';
2
+ export declare const hashReadableToB64Url: (stream: Readable, algorithm?: string) => Promise<string>;
3
+ export declare const hashReadableStreamToB64Url: (stream: ReadableStream, algorithm?: string) => Promise<string>;
4
+ export declare const hashBufferToB64Url: (buffer: Buffer, algorithm?: string) => string;
@@ -13,4 +13,4 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- export declare const version = "3.11.0-alpha.6";
16
+ export declare const version = "3.11.0-alpha.7";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ar.io/sdk",
3
- "version": "3.11.0-alpha.7",
3
+ "version": "3.11.0-alpha.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ar-io/ar-io-sdk.git"