@ar.io/sdk 3.11.0-alpha.4 → 3.11.0-alpha.5

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.
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Wayfinder = exports.createWayfinderClient = exports.resolveWayfinderUrl = exports.txIdRegex = exports.arnsRegex = void 0;
4
4
  const io_js_1 = require("../io.js");
5
+ const logger_js_1 = require("../logger.js");
5
6
  const gateways_js_1 = require("./gateways.js");
6
7
  const random_js_1 = require("./routers/random.js");
7
8
  // known regexes for wayfinder urls
@@ -13,28 +14,46 @@ exports.txIdRegex = /^[a-z0-9]{43}$/;
13
14
  * @param targetGateway - the target gateway to resolve the url against
14
15
  * @returns the resolved url that can be used to make a request
15
16
  */
16
- const resolveWayfinderUrl = ({ originalUrl, targetGateway, }) => {
17
+ const resolveWayfinderUrl = async ({ originalUrl, targetGateway, logger, }) => {
17
18
  if (originalUrl.toString().startsWith('ar://')) {
19
+ logger?.debug(`Applying wayfinder routing protocol to ${originalUrl}`, {
20
+ originalUrl,
21
+ });
22
+ const targetGatewayUrl = new URL(await targetGateway());
23
+ logger?.debug(`Selected target gateway: ${targetGatewayUrl}`, {
24
+ originalUrl,
25
+ targetGateway: targetGatewayUrl,
26
+ });
18
27
  const [, path] = originalUrl.toString().split('ar://');
19
28
  // e.g. ar:///info should route to the info endpoint of the target gateway
20
29
  if (path.startsWith('/')) {
21
- return new URL(path.slice(1), targetGateway);
30
+ logger?.debug(`Routing to ${path.slice(1)} on ${targetGatewayUrl}`, {
31
+ originalUrl,
32
+ targetGateway: targetGatewayUrl,
33
+ });
34
+ return new URL(path.slice(1), targetGatewayUrl);
22
35
  }
23
36
  // TODO: this breaks 43 character named arns names - we should check a a local name cache list before resolving raw transaction ids
24
37
  if (exports.txIdRegex.test(path)) {
25
38
  const [txId, ...rest] = path.split('/');
26
- return new URL(`${txId}${rest.join('/')}`, targetGateway);
39
+ return new URL(`${txId}${rest.join('/')}`, targetGatewayUrl);
27
40
  }
28
41
  if (exports.arnsRegex.test(path)) {
29
42
  // TODO: tests to ensure arns names support query params and paths
30
43
  const [name, ...rest] = path.split('/');
31
- const targetGatewayUrl = new URL(targetGateway);
32
44
  const arnsUrl = `${targetGatewayUrl.protocol}//${name}.${targetGatewayUrl.hostname}${targetGatewayUrl.port ? `:${targetGatewayUrl.port}` : ''}`;
45
+ logger?.debug(`Routing to ${path} on ${arnsUrl}`, {
46
+ originalUrl,
47
+ targetGateway: targetGatewayUrl,
48
+ });
33
49
  return new URL(rest.join('/'), arnsUrl);
34
50
  }
35
51
  // TODO: support .eth addresses
36
52
  // TODO: "gasless" routing via DNS TXT records (e.g. ar://gatewaypie.com -> TXT record lookup for TX ID and redirect to that gateway)
37
53
  }
54
+ logger?.debug('No wayfinder routing protocol applied', {
55
+ originalUrl,
56
+ });
38
57
  // return the original url if it's not a wayfinder url (allows you to use the wayfinder client with non-wayfinder urls)
39
58
  return new URL(originalUrl);
40
59
  };
@@ -52,16 +71,25 @@ exports.resolveWayfinderUrl = resolveWayfinderUrl;
52
71
  * @param resolveUrl - the function to construct the redirect url for ar:// requests
53
72
  * @returns a wrapped http client that supports ar:// protocol
54
73
  */
55
- const createWayfinderClient = ({ httpClient, resolveUrl, }) => {
74
+ const createWayfinderClient = ({ httpClient, resolveUrl, logger, }) => {
56
75
  const wayfinderRedirect = async (fn, rawArgs) => {
57
76
  // TODO: handle if first arg is not a string (i.e. just return the result of the function call)
58
77
  const [originalUrl, ...rest] = rawArgs;
59
78
  // route the request to the target gateway
60
79
  const redirectUrl = await resolveUrl({
61
80
  originalUrl,
81
+ logger,
82
+ });
83
+ logger?.debug(`Redirecting request to ${redirectUrl}`, {
84
+ originalUrl,
85
+ redirectUrl,
62
86
  });
63
87
  // make the request to the target gateway using the redirect url and http client
64
88
  const response = await fn(redirectUrl.toString(), ...rest);
89
+ logger?.debug(`Successfully routed request to ${redirectUrl}`, {
90
+ redirectUrl,
91
+ originalUrl,
92
+ });
65
93
  // TODO: if verifyDataHash is provided, verify the data hash before returning
66
94
  return response;
67
95
  };
@@ -154,21 +182,26 @@ class Wayfinder {
154
182
  router = new random_js_1.RandomGatewayRouter({
155
183
  // optionally use a cache gateways provider to reduce the number of requests to the contract
156
184
  gatewaysProvider: new gateways_js_1.ARIOGatewaysProvider({ ario: io_js_1.ARIO.mainnet() }),
157
- }), httpClient,
185
+ }), httpClient, logger = logger_js_1.Logger.default,
158
186
  // TODO: add verifier interface that provides a verifyDataHash function
159
187
  // TODO: stats provider
160
188
  }) {
161
189
  this.router = router;
162
190
  this.httpClient = httpClient;
163
- this.resolveUrl = async ({ originalUrl }) => (0, exports.resolveWayfinderUrl)({
164
- originalUrl,
165
- targetGateway: await this.router.getTargetGateway(),
166
- });
191
+ this.resolveUrl = async ({ originalUrl, logger }) => {
192
+ return (0, exports.resolveWayfinderUrl)({
193
+ originalUrl,
194
+ targetGateway: async () => await this.router.getTargetGateway(),
195
+ logger,
196
+ });
197
+ };
167
198
  this.request = (0, exports.createWayfinderClient)({
168
199
  httpClient,
169
200
  resolveUrl: this.resolveUrl,
201
+ logger,
170
202
  // TODO: provide the verifyDataHash function from the verifier to the wayfinder client along with verificationSettings
171
203
  });
204
+ logger?.debug(`Wayfinder initialized with ${router.name} routing strategy`);
172
205
  }
173
206
  }
174
207
  exports.Wayfinder = Wayfinder;
@@ -17,4 +17,4 @@
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.version = void 0;
19
19
  // AUTOMATICALLY GENERATED FILE - DO NOT TOUCH
20
- exports.version = '3.11.0-alpha.4';
20
+ exports.version = '3.11.0-alpha.5';
@@ -1,4 +1,5 @@
1
1
  import { ARIO } from '../io.js';
2
+ import { Logger } from '../logger.js';
2
3
  import { ARIOGatewaysProvider } from './gateways.js';
3
4
  import { RandomGatewayRouter } from './routers/random.js';
4
5
  // known regexes for wayfinder urls
@@ -10,28 +11,46 @@ export const txIdRegex = /^[a-z0-9]{43}$/;
10
11
  * @param targetGateway - the target gateway to resolve the url against
11
12
  * @returns the resolved url that can be used to make a request
12
13
  */
13
- export const resolveWayfinderUrl = ({ originalUrl, targetGateway, }) => {
14
+ export const resolveWayfinderUrl = async ({ originalUrl, targetGateway, logger, }) => {
14
15
  if (originalUrl.toString().startsWith('ar://')) {
16
+ logger?.debug(`Applying wayfinder routing protocol to ${originalUrl}`, {
17
+ originalUrl,
18
+ });
19
+ const targetGatewayUrl = new URL(await targetGateway());
20
+ logger?.debug(`Selected target gateway: ${targetGatewayUrl}`, {
21
+ originalUrl,
22
+ targetGateway: targetGatewayUrl,
23
+ });
15
24
  const [, path] = originalUrl.toString().split('ar://');
16
25
  // e.g. ar:///info should route to the info endpoint of the target gateway
17
26
  if (path.startsWith('/')) {
18
- return new URL(path.slice(1), targetGateway);
27
+ logger?.debug(`Routing to ${path.slice(1)} on ${targetGatewayUrl}`, {
28
+ originalUrl,
29
+ targetGateway: targetGatewayUrl,
30
+ });
31
+ return new URL(path.slice(1), targetGatewayUrl);
19
32
  }
20
33
  // TODO: this breaks 43 character named arns names - we should check a a local name cache list before resolving raw transaction ids
21
34
  if (txIdRegex.test(path)) {
22
35
  const [txId, ...rest] = path.split('/');
23
- return new URL(`${txId}${rest.join('/')}`, targetGateway);
36
+ return new URL(`${txId}${rest.join('/')}`, targetGatewayUrl);
24
37
  }
25
38
  if (arnsRegex.test(path)) {
26
39
  // TODO: tests to ensure arns names support query params and paths
27
40
  const [name, ...rest] = path.split('/');
28
- const targetGatewayUrl = new URL(targetGateway);
29
41
  const arnsUrl = `${targetGatewayUrl.protocol}//${name}.${targetGatewayUrl.hostname}${targetGatewayUrl.port ? `:${targetGatewayUrl.port}` : ''}`;
42
+ logger?.debug(`Routing to ${path} on ${arnsUrl}`, {
43
+ originalUrl,
44
+ targetGateway: targetGatewayUrl,
45
+ });
30
46
  return new URL(rest.join('/'), arnsUrl);
31
47
  }
32
48
  // TODO: support .eth addresses
33
49
  // TODO: "gasless" routing via DNS TXT records (e.g. ar://gatewaypie.com -> TXT record lookup for TX ID and redirect to that gateway)
34
50
  }
51
+ logger?.debug('No wayfinder routing protocol applied', {
52
+ originalUrl,
53
+ });
35
54
  // return the original url if it's not a wayfinder url (allows you to use the wayfinder client with non-wayfinder urls)
36
55
  return new URL(originalUrl);
37
56
  };
@@ -48,16 +67,25 @@ export const resolveWayfinderUrl = ({ originalUrl, targetGateway, }) => {
48
67
  * @param resolveUrl - the function to construct the redirect url for ar:// requests
49
68
  * @returns a wrapped http client that supports ar:// protocol
50
69
  */
51
- export const createWayfinderClient = ({ httpClient, resolveUrl, }) => {
70
+ export const createWayfinderClient = ({ httpClient, resolveUrl, logger, }) => {
52
71
  const wayfinderRedirect = async (fn, rawArgs) => {
53
72
  // TODO: handle if first arg is not a string (i.e. just return the result of the function call)
54
73
  const [originalUrl, ...rest] = rawArgs;
55
74
  // route the request to the target gateway
56
75
  const redirectUrl = await resolveUrl({
57
76
  originalUrl,
77
+ logger,
78
+ });
79
+ logger?.debug(`Redirecting request to ${redirectUrl}`, {
80
+ originalUrl,
81
+ redirectUrl,
58
82
  });
59
83
  // make the request to the target gateway using the redirect url and http client
60
84
  const response = await fn(redirectUrl.toString(), ...rest);
85
+ logger?.debug(`Successfully routed request to ${redirectUrl}`, {
86
+ redirectUrl,
87
+ originalUrl,
88
+ });
61
89
  // TODO: if verifyDataHash is provided, verify the data hash before returning
62
90
  return response;
63
91
  };
@@ -149,20 +177,25 @@ export class Wayfinder {
149
177
  router = new RandomGatewayRouter({
150
178
  // optionally use a cache gateways provider to reduce the number of requests to the contract
151
179
  gatewaysProvider: new ARIOGatewaysProvider({ ario: ARIO.mainnet() }),
152
- }), httpClient,
180
+ }), httpClient, logger = Logger.default,
153
181
  // TODO: add verifier interface that provides a verifyDataHash function
154
182
  // TODO: stats provider
155
183
  }) {
156
184
  this.router = router;
157
185
  this.httpClient = httpClient;
158
- this.resolveUrl = async ({ originalUrl }) => resolveWayfinderUrl({
159
- originalUrl,
160
- targetGateway: await this.router.getTargetGateway(),
161
- });
186
+ this.resolveUrl = async ({ originalUrl, logger }) => {
187
+ return resolveWayfinderUrl({
188
+ originalUrl,
189
+ targetGateway: async () => await this.router.getTargetGateway(),
190
+ logger,
191
+ });
192
+ };
162
193
  this.request = createWayfinderClient({
163
194
  httpClient,
164
195
  resolveUrl: this.resolveUrl,
196
+ logger,
165
197
  // TODO: provide the verifyDataHash function from the verifier to the wayfinder client along with verificationSettings
166
198
  });
199
+ logger?.debug(`Wayfinder initialized with ${router.name} routing strategy`);
167
200
  }
168
201
  }
@@ -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.11.0-alpha.4';
17
+ export const version = '3.11.0-alpha.5';
@@ -14,6 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  import { WayfinderRouter } from '../../types/wayfinder.js';
17
+ import { Logger } from '../logger.js';
17
18
  type HttpClientArgs = [string | URL, ...unknown[]];
18
19
  type HttpClientFunction = (...args: HttpClientArgs) => unknown;
19
20
  type WayfinderHttpClient<T extends HttpClientFunction> = T;
@@ -25,10 +26,11 @@ export declare const txIdRegex: RegExp;
25
26
  * @param targetGateway - the target gateway to resolve the url against
26
27
  * @returns the resolved url that can be used to make a request
27
28
  */
28
- export declare const resolveWayfinderUrl: ({ originalUrl, targetGateway, }: {
29
+ export declare const resolveWayfinderUrl: ({ originalUrl, targetGateway, logger, }: {
29
30
  originalUrl: string | URL;
30
- targetGateway: string | URL;
31
- }) => URL;
31
+ targetGateway: () => Promise<string | URL>;
32
+ logger?: Logger;
33
+ }) => Promise<URL>;
32
34
  /**
33
35
  * Creates a wrapped http client that supports ar:// protocol
34
36
  *
@@ -42,11 +44,13 @@ export declare const resolveWayfinderUrl: ({ originalUrl, targetGateway, }: {
42
44
  * @param resolveUrl - the function to construct the redirect url for ar:// requests
43
45
  * @returns a wrapped http client that supports ar:// protocol
44
46
  */
45
- export declare const createWayfinderClient: <T extends HttpClientFunction>({ httpClient, resolveUrl, }: {
47
+ export declare const createWayfinderClient: <T extends HttpClientFunction>({ httpClient, resolveUrl, logger, }: {
46
48
  httpClient: T;
47
49
  resolveUrl: (params: {
48
50
  originalUrl: string | URL;
51
+ logger?: Logger;
49
52
  }) => Promise<URL>;
53
+ logger?: Logger;
50
54
  }) => WayfinderHttpClient<T>;
51
55
  /**
52
56
  * The main class for the wayfinder
@@ -93,6 +97,7 @@ export declare class Wayfinder<T extends HttpClientFunction> {
93
97
  */
94
98
  readonly resolveUrl: (params: {
95
99
  originalUrl: string;
100
+ logger?: Logger;
96
101
  }) => Promise<URL>;
97
102
  /**
98
103
  * A wrapped http client that supports ar:// protocol
@@ -113,9 +118,10 @@ export declare class Wayfinder<T extends HttpClientFunction> {
113
118
  * })
114
119
  */
115
120
  readonly request: WayfinderHttpClient<T>;
116
- constructor({ router, httpClient, }: {
121
+ constructor({ router, httpClient, logger, }: {
117
122
  router: WayfinderRouter;
118
123
  httpClient: T;
124
+ logger?: Logger;
119
125
  });
120
126
  }
121
127
  export {};
@@ -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.3";
16
+ export declare const version = "3.11.0-alpha.4";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ar.io/sdk",
3
- "version": "3.11.0-alpha.4",
3
+ "version": "3.11.0-alpha.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ar-io/ar-io-sdk.git"