@ar.io/sdk 3.12.0-beta.2 → 3.12.0-beta.3

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.
@@ -35,6 +35,7 @@ __exportStar(require("./routing/strategies/random.js"), exports);
35
35
  __exportStar(require("./routing/strategies/static.js"), exports);
36
36
  __exportStar(require("./routing/strategies/ping.js"), exports);
37
37
  __exportStar(require("./routing/strategies/round-robin.js"), exports);
38
+ __exportStar(require("./routing/strategies/preferred-with-fallback.js"), exports);
38
39
  // gateways providers
39
40
  __exportStar(require("./gateways/network.js"), exports);
40
41
  __exportStar(require("./gateways/simple-cache.js"), exports);
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PreferredWithFallbackRoutingStrategy = void 0;
4
+ const logger_js_1 = require("../../../logger.js");
5
+ const ping_js_1 = require("./ping.js");
6
+ class PreferredWithFallbackRoutingStrategy {
7
+ name = 'preferred-with-fallback';
8
+ preferredGateway;
9
+ fallbackStrategy;
10
+ logger;
11
+ constructor({ preferredGateway, fallbackStrategy = new ping_js_1.FastestPingRoutingStrategy(), logger = logger_js_1.Logger.default, }) {
12
+ try {
13
+ this.preferredGateway = new URL(preferredGateway);
14
+ }
15
+ catch (error) {
16
+ throw new Error(`Invalid URL provided for preferred gateway: ${preferredGateway}`);
17
+ }
18
+ this.fallbackStrategy = fallbackStrategy;
19
+ this.logger = logger;
20
+ }
21
+ async selectGateway({ gateways = [] }) {
22
+ this.logger.debug('Attempting to connect to preferred gateway', {
23
+ preferredGateway: this.preferredGateway.toString(),
24
+ });
25
+ try {
26
+ // Check if the preferred gateway is responsive
27
+ const response = await fetch(this.preferredGateway.toString(), {
28
+ method: 'HEAD',
29
+ signal: AbortSignal.timeout(1000),
30
+ });
31
+ if (response.ok) {
32
+ this.logger.debug('Successfully connected to preferred gateway', {
33
+ preferredGateway: this.preferredGateway.toString(),
34
+ });
35
+ return this.preferredGateway;
36
+ }
37
+ throw new Error(`Preferred gateway responded with status: ${response.status}`);
38
+ }
39
+ catch (error) {
40
+ this.logger.warn('Failed to connect to preferred gateway, falling back to alternative strategy', {
41
+ preferredGateway: this.preferredGateway.toString(),
42
+ error: error instanceof Error ? error.message : String(error),
43
+ fallbackStrategy: this.fallbackStrategy.constructor.name,
44
+ });
45
+ // Fall back to the provided routing strategy
46
+ return this.fallbackStrategy.selectGateway({ gateways });
47
+ }
48
+ }
49
+ }
50
+ exports.PreferredWithFallbackRoutingStrategy = PreferredWithFallbackRoutingStrategy;
@@ -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.12.0-beta.2';
20
+ exports.version = '3.12.0-beta.3';
@@ -19,6 +19,7 @@ export * from './routing/strategies/random.js';
19
19
  export * from './routing/strategies/static.js';
20
20
  export * from './routing/strategies/ping.js';
21
21
  export * from './routing/strategies/round-robin.js';
22
+ export * from './routing/strategies/preferred-with-fallback.js';
22
23
  // gateways providers
23
24
  export * from './gateways/network.js';
24
25
  export * from './gateways/simple-cache.js';
@@ -0,0 +1,46 @@
1
+ import { Logger } from '../../../logger.js';
2
+ import { FastestPingRoutingStrategy } from './ping.js';
3
+ export class PreferredWithFallbackRoutingStrategy {
4
+ name = 'preferred-with-fallback';
5
+ preferredGateway;
6
+ fallbackStrategy;
7
+ logger;
8
+ constructor({ preferredGateway, fallbackStrategy = new FastestPingRoutingStrategy(), logger = Logger.default, }) {
9
+ try {
10
+ this.preferredGateway = new URL(preferredGateway);
11
+ }
12
+ catch (error) {
13
+ throw new Error(`Invalid URL provided for preferred gateway: ${preferredGateway}`);
14
+ }
15
+ this.fallbackStrategy = fallbackStrategy;
16
+ this.logger = logger;
17
+ }
18
+ async selectGateway({ gateways = [] }) {
19
+ this.logger.debug('Attempting to connect to preferred gateway', {
20
+ preferredGateway: this.preferredGateway.toString(),
21
+ });
22
+ try {
23
+ // Check if the preferred gateway is responsive
24
+ const response = await fetch(this.preferredGateway.toString(), {
25
+ method: 'HEAD',
26
+ signal: AbortSignal.timeout(1000),
27
+ });
28
+ if (response.ok) {
29
+ this.logger.debug('Successfully connected to preferred gateway', {
30
+ preferredGateway: this.preferredGateway.toString(),
31
+ });
32
+ return this.preferredGateway;
33
+ }
34
+ throw new Error(`Preferred gateway responded with status: ${response.status}`);
35
+ }
36
+ catch (error) {
37
+ this.logger.warn('Failed to connect to preferred gateway, falling back to alternative strategy', {
38
+ preferredGateway: this.preferredGateway.toString(),
39
+ error: error instanceof Error ? error.message : String(error),
40
+ fallbackStrategy: this.fallbackStrategy.constructor.name,
41
+ });
42
+ // Fall back to the provided routing strategy
43
+ return this.fallbackStrategy.selectGateway({ gateways });
44
+ }
45
+ }
46
+ }
@@ -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-beta.2';
17
+ export const version = '3.12.0-beta.3';
@@ -18,6 +18,7 @@ export * from './routing/strategies/random.js';
18
18
  export * from './routing/strategies/static.js';
19
19
  export * from './routing/strategies/ping.js';
20
20
  export * from './routing/strategies/round-robin.js';
21
+ export * from './routing/strategies/preferred-with-fallback.js';
21
22
  export * from './gateways/network.js';
22
23
  export * from './gateways/simple-cache.js';
23
24
  export * from './gateways/static.js';
@@ -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 { RoutingStrategy } from '../../../../types/wayfinder.js';
17
+ import { Logger } from '../../../logger.js';
18
+ export declare class PreferredWithFallbackRoutingStrategy implements RoutingStrategy {
19
+ readonly name = "preferred-with-fallback";
20
+ private preferredGateway;
21
+ private fallbackStrategy;
22
+ private logger;
23
+ constructor({ preferredGateway, fallbackStrategy, logger, }: {
24
+ preferredGateway: string;
25
+ fallbackStrategy?: RoutingStrategy;
26
+ logger?: Logger;
27
+ });
28
+ selectGateway({ gateways }: {
29
+ gateways: URL[];
30
+ }): Promise<URL>;
31
+ }
@@ -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.12.0-beta.1";
16
+ export declare const version = "3.12.0-beta.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ar.io/sdk",
3
- "version": "3.12.0-beta.2",
3
+ "version": "3.12.0-beta.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ar-io/ar-io-sdk.git"