@ar.io/sdk 3.12.0-alpha.1 → 3.12.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.
Files changed (70) hide show
  1. package/bundles/web.bundle.min.js +170 -167
  2. package/lib/cjs/common/index.js +2 -0
  3. package/lib/cjs/common/wayfinder/gateways/network.js +48 -0
  4. package/lib/cjs/common/wayfinder/gateways/simple-cache.js +35 -0
  5. package/lib/cjs/common/wayfinder/gateways/static.js +13 -0
  6. package/lib/cjs/common/wayfinder/index.js +47 -0
  7. package/lib/cjs/common/wayfinder/routing/strategies/ping.js +72 -0
  8. package/lib/cjs/common/wayfinder/routing/strategies/random.js +13 -0
  9. package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +42 -0
  10. package/lib/cjs/common/wayfinder/routing/strategies/static.js +29 -0
  11. package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +139 -0
  12. package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +50 -0
  13. package/lib/cjs/common/wayfinder/verification/trusted.js +106 -0
  14. package/lib/cjs/common/wayfinder/wayfinder.js +735 -0
  15. package/lib/cjs/types/wayfinder.js +3 -0
  16. package/lib/cjs/utils/json.js +0 -1
  17. package/lib/cjs/utils/url.js +3 -1
  18. package/lib/cjs/version.js +1 -1
  19. package/lib/esm/common/index.js +2 -0
  20. package/lib/esm/common/wayfinder/gateways/network.js +44 -0
  21. package/lib/esm/common/wayfinder/gateways/simple-cache.js +31 -0
  22. package/lib/esm/common/wayfinder/gateways/static.js +9 -0
  23. package/lib/esm/common/wayfinder/index.js +31 -0
  24. package/lib/esm/common/wayfinder/routing/strategies/ping.js +68 -0
  25. package/lib/esm/common/wayfinder/routing/strategies/random.js +9 -0
  26. package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +38 -0
  27. package/lib/esm/common/wayfinder/routing/strategies/static.js +25 -0
  28. package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +130 -0
  29. package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +46 -0
  30. package/lib/esm/common/wayfinder/verification/trusted.js +102 -0
  31. package/lib/esm/common/wayfinder/wayfinder.js +723 -0
  32. package/lib/esm/types/wayfinder.js +2 -0
  33. package/lib/esm/utils/json.js +0 -1
  34. package/lib/esm/utils/url.js +3 -1
  35. package/lib/esm/version.js +1 -1
  36. package/lib/types/common/index.d.ts +1 -0
  37. package/lib/types/common/wayfinder/gateways/network.d.ts +33 -0
  38. package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +31 -0
  39. package/lib/types/common/wayfinder/gateways/static.d.ts +23 -0
  40. package/lib/types/common/wayfinder/index.d.ts +26 -0
  41. package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +27 -0
  42. package/lib/types/common/wayfinder/routing/strategies/random.d.ts +21 -0
  43. package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +29 -0
  44. package/lib/types/common/wayfinder/routing/strategies/static.d.ts +29 -0
  45. package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +31 -0
  46. package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +27 -0
  47. package/lib/types/common/wayfinder/verification/trusted.d.ts +51 -0
  48. package/lib/types/common/wayfinder/wayfinder.d.ts +299 -0
  49. package/lib/types/types/wayfinder.d.ts +66 -0
  50. package/lib/types/utils/json.d.ts +1 -1
  51. package/lib/types/version.d.ts +1 -1
  52. package/package.json +3 -2
  53. package/lib/cjs/types/ant.test.js +0 -150
  54. package/lib/cjs/types/token.test.js +0 -83
  55. package/lib/cjs/utils/ant.test.js +0 -111
  56. package/lib/cjs/utils/b64.test.js +0 -72
  57. package/lib/cjs/utils/url.test.js +0 -24
  58. package/lib/cjs/utils/utils.test.js +0 -194
  59. package/lib/esm/types/ant.test.js +0 -148
  60. package/lib/esm/types/token.test.js +0 -81
  61. package/lib/esm/utils/ant.test.js +0 -109
  62. package/lib/esm/utils/b64.test.js +0 -70
  63. package/lib/esm/utils/url.test.js +0 -19
  64. package/lib/esm/utils/utils.test.js +0 -192
  65. package/lib/types/types/ant.test.d.ts +0 -1
  66. package/lib/types/types/token.test.d.ts +0 -1
  67. package/lib/types/utils/ant.test.d.ts +0 -1
  68. package/lib/types/utils/b64.test.d.ts +0 -1
  69. package/lib/types/utils/url.test.d.ts +0 -1
  70. package/lib/types/utils/utils.test.d.ts +0 -1
@@ -0,0 +1,2 @@
1
+ export {};
2
+ // TODO: add an offset provider that returns offsets for data items so we can use them to verify the signatures of a data item within a bundle
@@ -13,7 +13,6 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
16
  export function safeDecode(data) {
18
17
  try {
19
18
  return JSON.parse(data);
@@ -25,7 +25,9 @@ export const urlWithSearchParams = ({ baseUrl, params, }) => {
25
25
  urlObj.searchParams.append(key, v.toString());
26
26
  }
27
27
  }
28
- urlObj.searchParams.set(key, value.toString());
28
+ else {
29
+ urlObj.searchParams.set(key, value.toString());
30
+ }
29
31
  });
30
32
  return urlObj.toString();
31
33
  };
@@ -14,4 +14,4 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
17
- export const version = '3.12.0-alpha.1';
17
+ export const version = '3.12.0-beta.1';
@@ -21,3 +21,4 @@ export * from './ant-versions.js';
21
21
  export * from './faucet.js';
22
22
  export * from './io.js';
23
23
  export * from './contracts/ao-process.js';
24
+ export * from './wayfinder/index.js';
@@ -0,0 +1,33 @@
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
+ import { GatewaysProvider } from '../../../types/wayfinder.js';
18
+ export declare class NetworkGatewaysProvider implements GatewaysProvider {
19
+ private ario;
20
+ private sortBy;
21
+ private sortOrder;
22
+ private limit;
23
+ private filter;
24
+ constructor({ ario, sortBy, sortOrder, limit, filter, }: {
25
+ ario?: AoARIORead;
26
+ sortBy?: 'totalDelegatedStake' | 'operatorStake' | 'startTimestamp';
27
+ sortOrder?: 'asc' | 'desc';
28
+ limit?: number;
29
+ blocklist?: string[];
30
+ filter?: (gateway: AoGatewayWithAddress) => boolean;
31
+ });
32
+ getGateways(): Promise<URL[]>;
33
+ }
@@ -0,0 +1,31 @@
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 { GatewaysProvider } from '../../../types/wayfinder.js';
17
+ import { Logger } from '../../../web/index.js';
18
+ export declare class SimpleCacheGatewaysProvider implements GatewaysProvider {
19
+ private gatewaysProvider;
20
+ private ttlSeconds;
21
+ private lastUpdated;
22
+ private gatewaysCache;
23
+ private logger;
24
+ constructor({ gatewaysProvider, ttlSeconds, // 1 hour
25
+ logger, }: {
26
+ gatewaysProvider: GatewaysProvider;
27
+ ttlSeconds?: number;
28
+ logger?: Logger;
29
+ });
30
+ getGateways(): Promise<URL[]>;
31
+ }
@@ -0,0 +1,23 @@
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 { GatewaysProvider } from '../../../types/wayfinder.js';
17
+ export declare class StaticGatewaysProvider implements GatewaysProvider {
18
+ private gateways;
19
+ constructor({ gateways }: {
20
+ gateways: string[];
21
+ });
22
+ getGateways(): Promise<URL[]>;
23
+ }
@@ -0,0 +1,26 @@
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 './routing/strategies/random.js';
18
+ export * from './routing/strategies/static.js';
19
+ export * from './routing/strategies/ping.js';
20
+ export * from './routing/strategies/round-robin.js';
21
+ export * from './gateways/network.js';
22
+ export * from './gateways/simple-cache.js';
23
+ export * from './gateways/static.js';
24
+ export * from './verification/trusted.js';
25
+ export * from './verification/strategies/data-root-verifier.js';
26
+ export * from './verification/strategies/hash-verifier.js';
@@ -0,0 +1,27 @@
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 { RoutingStrategy } from '../../../../types/wayfinder.js';
17
+ export declare class FastestPingRoutingStrategy implements RoutingStrategy {
18
+ private timeoutMs;
19
+ private probePath;
20
+ constructor({ timeoutMs, probePath, }?: {
21
+ timeoutMs?: number;
22
+ probePath?: string;
23
+ });
24
+ selectGateway({ gateways }: {
25
+ gateways: URL[];
26
+ }): Promise<URL>;
27
+ }
@@ -0,0 +1,21 @@
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 { RoutingStrategy } from '../../../../types/wayfinder.js';
17
+ export declare class RandomRoutingStrategy implements RoutingStrategy {
18
+ selectGateway({ gateways }: {
19
+ gateways: URL[];
20
+ }): Promise<URL>;
21
+ }
@@ -0,0 +1,29 @@
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 { Logger } from '../../../../common/logger.js';
17
+ import { RoutingStrategy } from '../../../../types/wayfinder.js';
18
+ export declare class RoundRobinRoutingStrategy implements RoutingStrategy {
19
+ private gateways;
20
+ private currentIndex;
21
+ private logger;
22
+ constructor({ gateways, logger, }: {
23
+ gateways: URL[];
24
+ logger?: Logger;
25
+ });
26
+ selectGateway({ gateways, }?: {
27
+ gateways?: URL[];
28
+ }): Promise<URL>;
29
+ }
@@ -0,0 +1,29 @@
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 { RoutingStrategy } from '../../../../types/wayfinder.js';
17
+ import { Logger } from '../../../logger.js';
18
+ export declare class StaticRoutingStrategy implements RoutingStrategy {
19
+ readonly name = "static";
20
+ private gateway;
21
+ private logger;
22
+ constructor({ gateway, logger, }: {
23
+ gateway: string;
24
+ logger?: Logger;
25
+ });
26
+ selectGateway({ gateways, }?: {
27
+ gateways?: URL[];
28
+ }): Promise<URL>;
29
+ }
@@ -0,0 +1,31 @@
1
+ import { Readable } from 'node:stream';
2
+ import { DataRootProvider, DataVerificationStrategy } from '../../../../types/wayfinder.js';
3
+ export declare function convertBufferToDataRoot({ buffer, }: {
4
+ buffer: Buffer;
5
+ }): Promise<string>;
6
+ export declare const convertReadableToDataRoot: <T extends AsyncIterable<Uint8Array>>({ iterable, }: {
7
+ iterable: T;
8
+ }) => Promise<string>;
9
+ export declare class DataRootVerificationStrategy implements DataVerificationStrategy {
10
+ private readonly trustedDataRootProvider;
11
+ constructor({ trustedDataRootProvider, }: {
12
+ trustedDataRootProvider: DataRootProvider;
13
+ });
14
+ verifyData({ data, txId, }: {
15
+ data: Buffer | Readable | ReadableStream;
16
+ txId: string;
17
+ }): Promise<void>;
18
+ }
19
+ /**
20
+ * - when you get a signature of a data item, you can only verify the owner
21
+ * - you still need to verify it's going back to the bundle, unpack it, and verify the data item exists at the offset
22
+ * - you need to the location of the chunks for the data item, and prove it's in the chunk and then prove the data root of the bundle, then you have fully verified the data verifier
23
+ * - how to prove the data item is on arweave - verify the merkle hash that the chunks for the data item, fit within the expected tree of the parent bundle
24
+ *
25
+ * Composite verifier - you'll want to be very efficient with streams
26
+ * - hash verifier
27
+ * - parent chunks verifier --> for any range of data within a single transaction, tell me that it's correct
28
+ * - signature verifier
29
+ * - offset verifier
30
+ * - data item verifier
31
+ */
@@ -0,0 +1,27 @@
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 { Readable } from 'node:stream';
17
+ import { DataHashProvider, DataVerificationStrategy } from '../../../../types/wayfinder.js';
18
+ export declare class HashVerificationStrategy implements DataVerificationStrategy {
19
+ private readonly trustedHashProvider;
20
+ constructor({ trustedHashProvider, }: {
21
+ trustedHashProvider: DataHashProvider;
22
+ });
23
+ verifyData({ data, txId, }: {
24
+ data: Buffer | Readable | ReadableStream;
25
+ txId: string;
26
+ }): Promise<void>;
27
+ }
@@ -0,0 +1,51 @@
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 { DataHashProvider, DataRootProvider } from '../../../types/wayfinder.js';
17
+ import { GatewaysProvider } from '../../../types/wayfinder.js';
18
+ export declare class TrustedGatewaysHashProvider implements DataHashProvider, DataRootProvider {
19
+ private gatewaysProvider;
20
+ constructor({ gatewaysProvider, }: {
21
+ gatewaysProvider: GatewaysProvider;
22
+ });
23
+ /**
24
+ * Gets the digest for a given txId from all trusted gateways and ensures they all match.
25
+ * @param txId - The txId to get the digest for.
26
+ * @returns The digest for the given txId.
27
+ */
28
+ getHash({ txId, }: {
29
+ txId: string;
30
+ }): Promise<{
31
+ hash: string;
32
+ algorithm: 'sha256';
33
+ }>;
34
+ /**
35
+ * Get the data root for a given txId from all trusted gateways and ensure they all match.
36
+ * @param txId - The txId to get the data root for.
37
+ * @returns The data root for the given txId.
38
+ */
39
+ getDataRoot({ txId }: {
40
+ txId: string;
41
+ }): Promise<string>;
42
+ }
43
+ /**
44
+ * Note from @djwhitt
45
+ *
46
+ * Calculating data roots this way is fine, but it may not reproduce the original data root.
47
+ * We could also implement a data root verifier that pulls all the chunks, checks that they
48
+ * reproduce the expected data root, and then compares the concatenated chunk data to the
49
+ * original data retrieved. That would take a while, but it should be able to verify any L1
50
+ * data where we can find the chunks.
51
+ */
@@ -0,0 +1,299 @@
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 EventEmitter from 'node:events';
17
+ import { PassThrough, Readable } from 'node:stream';
18
+ import { DataVerificationStrategy, GatewaysProvider, RoutingStrategy } from '../../types/wayfinder.js';
19
+ import { Logger } from '../logger.js';
20
+ type HttpClientArgs = unknown[];
21
+ type HttpClientFunction = (...args: HttpClientArgs) => unknown;
22
+ type WayfinderHttpClient<T extends HttpClientFunction> = T;
23
+ export declare const arnsRegex: RegExp;
24
+ export declare const txIdRegex: RegExp;
25
+ /**
26
+ * Core function that converts a wayfinder url to the proper ar-io gateway URL
27
+ * @param originalUrl - the wayfinder url to resolve
28
+ * @param selectedGateway - the target gateway to resolve the url against
29
+ * @returns the resolved url that can be used to make a request
30
+ */
31
+ export declare const resolveWayfinderUrl: ({ originalUrl, selectedGateway, logger, }: {
32
+ originalUrl: string | URL;
33
+ selectedGateway: URL;
34
+ logger?: Logger;
35
+ }) => URL;
36
+ /**
37
+ * Wayfinder event emitter with verification events
38
+ */
39
+ export type WayfinderEvent = {
40
+ type: 'verification-succeeded';
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-skipped';
59
+ originalUrl: string;
60
+ } | {
61
+ type: 'routing-succeeded';
62
+ originalUrl: string;
63
+ selectedGateway: string;
64
+ redirectUrl: string;
65
+ } | {
66
+ type: 'routing-failed';
67
+ originalUrl: string;
68
+ error: Error;
69
+ } | {
70
+ type: 'identified-transaction-id';
71
+ originalUrl: string;
72
+ selectedGateway: string;
73
+ txId: string;
74
+ };
75
+ export interface WayfinderEventArgs {
76
+ onVerificationPassed?: (payload: Omit<Extract<WayfinderEvent, {
77
+ type: 'verification-succeeded';
78
+ }>, 'type'>) => void;
79
+ onVerificationFailed?: (payload: Omit<Extract<WayfinderEvent, {
80
+ type: 'verification-failed';
81
+ }>, 'type'>) => void;
82
+ onVerificationProgress?: (payload: Omit<Extract<WayfinderEvent, {
83
+ type: 'verification-progress';
84
+ }>, 'type'>) => void;
85
+ }
86
+ export declare class WayfinderEmitter extends EventEmitter {
87
+ constructor({ onVerificationPassed, onVerificationFailed, onVerificationProgress, }?: WayfinderEventArgs);
88
+ emit<E extends WayfinderEvent['type']>(event: E, payload: Omit<Extract<WayfinderEvent, {
89
+ type: E;
90
+ }>, 'type'>): boolean;
91
+ on<E extends WayfinderEvent['type']>(event: E, listener: (payload: Omit<Extract<WayfinderEvent, {
92
+ type: E;
93
+ }>, 'type'>) => void): this;
94
+ }
95
+ export declare function tapAndVerifyStream<T extends Readable | ReadableStream>({ originalStream, contentLength, verifyData, txId, emitter, strict, }: {
96
+ originalStream: T;
97
+ contentLength: number;
98
+ verifyData: DataVerificationStrategy['verifyData'];
99
+ txId: string;
100
+ emitter?: WayfinderEmitter;
101
+ strict?: boolean;
102
+ }): T extends Readable ? PassThrough : T;
103
+ export declare function wrapVerifiedResponse(original: Response, newBody: ReadableStream<Uint8Array>, txId: string): Response;
104
+ /**
105
+ * Creates a wrapped http client that supports ar:// protocol
106
+ *
107
+ * This function leverages a Proxy to intercept calls to the http client
108
+ * and redirects them to the target gateway using the resolveUrl function url.
109
+ * It also supports the http client methods like get(), post(), put(), delete(), etc.
110
+ *
111
+ * Any URLs provided that are not wayfinder urls will be returned as is.
112
+ *
113
+ * @param httpClient - the http client to wrap (e.g. axios, fetch, got, etc.)
114
+ * @param resolveUrl - the function to construct the redirect url for ar:// requests
115
+ * @returns a wrapped http client that supports ar:// protocol
116
+ */
117
+ export declare const createWayfinderClient: <T extends HttpClientFunction>({ httpClient, resolveUrl, verifyData, selectGateway, emitter, logger, strict, }: {
118
+ httpClient: T;
119
+ selectGateway: () => Promise<URL>;
120
+ resolveUrl: (params: {
121
+ originalUrl: string | URL;
122
+ selectedGateway: URL;
123
+ logger?: Logger;
124
+ }) => URL;
125
+ verifyData?: <T_1 extends Readable | ReadableStream | Buffer>({ data, txId, }: {
126
+ data: T_1;
127
+ txId: string;
128
+ }) => Promise<void>;
129
+ logger?: Logger;
130
+ emitter?: WayfinderEmitter;
131
+ strict?: boolean;
132
+ }) => WayfinderHttpClient<T>;
133
+ /**
134
+ * The main class for the wayfinder
135
+ * @param router - the router to use for requests
136
+ * @param httpClient - the http client to use for requests
137
+ * @param blocklist - the blocklist of gateways to avoid
138
+ */
139
+ export declare class Wayfinder<T extends HttpClientFunction> {
140
+ /**
141
+ * The native http client used by wayfinder. By default, the native fetch api is used.
142
+ *
143
+ * @example
144
+ * const wayfinder = new Wayfinder({
145
+ * httpClient: axios,
146
+ * });
147
+ *
148
+ */
149
+ readonly httpClient: T;
150
+ /**
151
+ * The gateways provider is responsible for providing the list of gateways to use for routing requests.
152
+ *
153
+ * @example
154
+ * const wayfinder = new Wayfinder({
155
+ * gatewaysProvider: new SimpleCacheGatewaysProvider({
156
+ * gatewaysProvider: new NetworkGatewaysProvider({ ario: ARIO.mainnet() }),
157
+ * ttlSeconds: 60 * 60 * 24, // 1 day
158
+ * }),
159
+ * });
160
+ */
161
+ readonly gatewaysProvider: GatewaysProvider;
162
+ /**
163
+ * The routing strategy to use when routing requests.
164
+ *
165
+ * @example
166
+ * const wayfinder = new Wayfinder({
167
+ * strategy: new FastestPingStrategy({
168
+ * timeoutMs: 1000,
169
+ * }),
170
+ * });
171
+ */
172
+ readonly routingStrategy: RoutingStrategy;
173
+ /**
174
+ * A helper function that resolves the redirect url for ar:// requests to a target gateway.
175
+ *
176
+ * Note: no verification is done when resolving an ar://<path> url to a wayfinder route.
177
+ * In order to verify the data, you must use the `request` function or request the data and
178
+ * verify it yourself via the `verifyData` function.
179
+ *
180
+ * @example
181
+ * const { resolveUrl } = new Wayfinder();
182
+ *
183
+ * // returns the redirected URL based on the routing strategy and the original url
184
+ * const redirectUrl = await resolveUrl({ originalUrl: 'ar://example' });
185
+ *
186
+ * window.open(redirectUrl.toString(), '_blank');
187
+ */
188
+ readonly resolveUrl: (params: {
189
+ originalUrl: string;
190
+ logger?: Logger;
191
+ }) => Promise<URL>;
192
+ /**
193
+ *
194
+ * A wrapped http client that supports ar:// protocol. If a verification strategy is provided,
195
+ * the request will be verified and events will be emitted as the request is processed.
196
+ *
197
+ * @example
198
+ * const wayfinder = new Wayfinder({
199
+ * verificationStrategy: new HashVerificationStrategy({
200
+ * trustedHashProvider: new TrustedGatewaysHashProvider({
201
+ * gatewaysProvider: new StaticGatewaysProvider({
202
+ * gateways: ['https://permagate.io'],
203
+ * }),
204
+ * }),
205
+ * }),
206
+ * })
207
+ *
208
+ * // request an arns name
209
+ * const response = await wayfinder.request('ar://ardrive')
210
+ *
211
+ * // request a transaction id
212
+ * const response = await wayfinder.request('ar://1234567890')
213
+ *
214
+ * // request a transaction id with a custom http client
215
+ * const response = await wayfinder.request('ar://1234567890')
216
+ *
217
+ * // Set strict mode to true to make verification blocking
218
+ * const wayfinder = new Wayfinder({
219
+ * strict: true,
220
+ * });
221
+ *
222
+ * // This will throw an error if verification fails
223
+ * try {
224
+ * const response = await wayfinder.request('ar://1234567890');
225
+ * } catch (error) {
226
+ * console.error('Verification failed', error);
227
+ * }
228
+ */
229
+ readonly request: WayfinderHttpClient<T>;
230
+ /**
231
+ * The function that verifies the data hash for a given transaction id.
232
+ *
233
+ * @example
234
+ * const wayfinder = new Wayfinder({
235
+ * verifyData: (data, txId) => {
236
+ * // some custom verification logic
237
+ * return true;
238
+ * },
239
+ * });
240
+ */
241
+ readonly verifyData: DataVerificationStrategy['verifyData'];
242
+ /**
243
+ * Whether verification should be strict (blocking) or not.
244
+ * If true, verification failures will cause requests to fail.
245
+ * If false, verification will be performed asynchronously and failures will only emit events.
246
+ */
247
+ readonly strict: boolean;
248
+ /**
249
+ * The event emitter for wayfinder that emits verification events.
250
+ *
251
+ * const wayfinder = new Wayfinder()
252
+ *
253
+ * wayfinder.emitter.on('verification-succeeded', (event) => {
254
+ * console.log('Verification passed!', event);
255
+ * })
256
+ *
257
+ * wayfinder.emitter.on('verification-failed', (event) => {
258
+ * console.log('Verification failed!', event);
259
+ * })
260
+ *
261
+ * or implement the events interface and pass it in, using callback functions
262
+ *
263
+ * const wayfinder = new Wayfinder({
264
+ * events: {
265
+ * onVerificationPassed: (event) => {
266
+ * console.log('Verification passed!', event);
267
+ * },
268
+ * onVerificationFailed: (event) => {
269
+ * console.log('Verification failed!', event);
270
+ * },
271
+ * onVerificationProgress: (event) => {
272
+ * console.log('Verification progress!', event);
273
+ * },
274
+ * }
275
+ * })
276
+ *
277
+ * const response = await wayfind('ar://example');
278
+ */
279
+ readonly emitter: WayfinderEmitter;
280
+ /**
281
+ * The constructor for the wayfinder
282
+ * @param httpClient - the http client to use for requests
283
+ * @param routingStrategy - the routing strategy to use for requests
284
+ * @param verificationStrategy - the verification strategy to use for requests
285
+ * @param gatewaysProvider - the gateways provider to use for routing requests
286
+ * @param logger - the logger to use for logging
287
+ * @param strict - if true, verification will be blocking and will fail requests if verification fails; if false, verification will be non-blocking
288
+ */
289
+ constructor({ httpClient, logger, gatewaysProvider, routingStrategy, verificationStrategy, events, strict, }: {
290
+ httpClient?: T;
291
+ routingStrategy?: RoutingStrategy;
292
+ gatewaysProvider?: GatewaysProvider;
293
+ verificationStrategy?: DataVerificationStrategy;
294
+ logger?: Logger;
295
+ events?: WayfinderEventArgs;
296
+ strict?: boolean;
297
+ });
298
+ }
299
+ export {};