@ar.io/sdk 3.11.0-beta.1 → 3.11.0
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.
- package/bundles/web.bundle.min.js +111 -114
- package/lib/cjs/common/ant-versions.js +3 -2
- package/lib/cjs/common/index.js +0 -2
- package/lib/cjs/common/io.js +13 -9
- package/lib/cjs/common/turbo.js +1 -1
- package/lib/cjs/types/index.js +0 -1
- package/lib/cjs/utils/ao.js +2 -1
- package/lib/cjs/version.js +1 -1
- package/lib/esm/common/ant-versions.js +3 -2
- package/lib/esm/common/index.js +0 -2
- package/lib/esm/common/io.js +13 -9
- package/lib/esm/common/turbo.js +1 -1
- package/lib/esm/types/index.js +0 -1
- package/lib/esm/utils/ao.js +2 -1
- package/lib/esm/version.js +1 -1
- package/lib/types/common/ant-versions.d.ts +3 -5
- package/lib/types/common/index.d.ts +0 -1
- package/lib/types/types/index.d.ts +0 -1
- package/lib/types/version.d.ts +1 -1
- package/package.json +1 -1
- package/lib/cjs/common/wayfinder/gateways/network.js +0 -48
- package/lib/cjs/common/wayfinder/gateways/simple-cache.js +0 -35
- package/lib/cjs/common/wayfinder/gateways/static.js +0 -13
- package/lib/cjs/common/wayfinder/index.js +0 -47
- package/lib/cjs/common/wayfinder/routing/strategies/ping.js +0 -72
- package/lib/cjs/common/wayfinder/routing/strategies/ping.test.js +0 -156
- package/lib/cjs/common/wayfinder/routing/strategies/random.js +0 -13
- package/lib/cjs/common/wayfinder/routing/strategies/random.test.js +0 -68
- package/lib/cjs/common/wayfinder/routing/strategies/round-robin.js +0 -42
- package/lib/cjs/common/wayfinder/routing/strategies/round-robin.test.js +0 -78
- package/lib/cjs/common/wayfinder/routing/strategies/static.js +0 -29
- package/lib/cjs/common/wayfinder/routing/strategies/static.test.js +0 -40
- package/lib/cjs/common/wayfinder/verification/strategies/data-root-verifier.js +0 -139
- package/lib/cjs/common/wayfinder/verification/strategies/hash-verifier.js +0 -50
- package/lib/cjs/common/wayfinder/verification/trusted.js +0 -106
- package/lib/cjs/common/wayfinder/wayfinder.js +0 -736
- package/lib/cjs/common/wayfinder/wayfinder.test.js +0 -519
- package/lib/cjs/types/wayfinder.js +0 -3
- package/lib/esm/common/wayfinder/gateways/network.js +0 -44
- package/lib/esm/common/wayfinder/gateways/simple-cache.js +0 -31
- package/lib/esm/common/wayfinder/gateways/static.js +0 -9
- package/lib/esm/common/wayfinder/index.js +0 -31
- package/lib/esm/common/wayfinder/routing/strategies/ping.js +0 -68
- package/lib/esm/common/wayfinder/routing/strategies/ping.test.js +0 -151
- package/lib/esm/common/wayfinder/routing/strategies/random.js +0 -9
- package/lib/esm/common/wayfinder/routing/strategies/random.test.js +0 -63
- package/lib/esm/common/wayfinder/routing/strategies/round-robin.js +0 -38
- package/lib/esm/common/wayfinder/routing/strategies/round-robin.test.js +0 -73
- package/lib/esm/common/wayfinder/routing/strategies/static.js +0 -25
- package/lib/esm/common/wayfinder/routing/strategies/static.test.js +0 -35
- package/lib/esm/common/wayfinder/verification/strategies/data-root-verifier.js +0 -130
- package/lib/esm/common/wayfinder/verification/strategies/hash-verifier.js +0 -46
- package/lib/esm/common/wayfinder/verification/trusted.js +0 -102
- package/lib/esm/common/wayfinder/wayfinder.js +0 -724
- package/lib/esm/common/wayfinder/wayfinder.test.js +0 -514
- package/lib/esm/types/wayfinder.js +0 -2
- package/lib/types/common/wayfinder/gateways/network.d.ts +0 -33
- package/lib/types/common/wayfinder/gateways/simple-cache.d.ts +0 -31
- package/lib/types/common/wayfinder/gateways/static.d.ts +0 -23
- package/lib/types/common/wayfinder/index.d.ts +0 -26
- package/lib/types/common/wayfinder/routing/strategies/ping.d.ts +0 -27
- package/lib/types/common/wayfinder/routing/strategies/ping.test.d.ts +0 -1
- package/lib/types/common/wayfinder/routing/strategies/random.d.ts +0 -21
- package/lib/types/common/wayfinder/routing/strategies/random.test.d.ts +0 -1
- package/lib/types/common/wayfinder/routing/strategies/round-robin.d.ts +0 -29
- package/lib/types/common/wayfinder/routing/strategies/round-robin.test.d.ts +0 -1
- package/lib/types/common/wayfinder/routing/strategies/static.d.ts +0 -29
- package/lib/types/common/wayfinder/routing/strategies/static.test.d.ts +0 -1
- package/lib/types/common/wayfinder/verification/strategies/data-root-verifier.d.ts +0 -31
- package/lib/types/common/wayfinder/verification/strategies/hash-verifier.d.ts +0 -27
- package/lib/types/common/wayfinder/verification/trusted.d.ts +0 -51
- package/lib/types/common/wayfinder/wayfinder.d.ts +0 -299
- package/lib/types/common/wayfinder/wayfinder.test.d.ts +0 -1
- package/lib/types/types/wayfinder.d.ts +0 -66
|
@@ -75,7 +75,8 @@ class ANTVersionsWritable extends ANTVersionsReadable {
|
|
|
75
75
|
super(config);
|
|
76
76
|
this.signer = (0, ao_js_1.createAoSigner)(signer);
|
|
77
77
|
}
|
|
78
|
-
async addVersion(
|
|
78
|
+
async addVersion(params, options) {
|
|
79
|
+
const { version, moduleId, luaSourceId, notes } = params;
|
|
79
80
|
return this.process.send({
|
|
80
81
|
tags: (0, arweave_js_1.pruneTags)([
|
|
81
82
|
{ name: 'Action', value: 'Add-Version' },
|
|
@@ -83,7 +84,7 @@ class ANTVersionsWritable extends ANTVersionsReadable {
|
|
|
83
84
|
{ name: 'Module-Id', value: moduleId },
|
|
84
85
|
{ name: 'Lua-Source-Id', value: luaSourceId },
|
|
85
86
|
{ name: 'Notes', value: notes },
|
|
86
|
-
...(tags ?? []),
|
|
87
|
+
...(options?.tags ?? []),
|
|
87
88
|
]),
|
|
88
89
|
signer: this.signer,
|
|
89
90
|
});
|
package/lib/cjs/common/index.js
CHANGED
package/lib/cjs/common/io.js
CHANGED
|
@@ -457,7 +457,7 @@ class ARIOReadable {
|
|
|
457
457
|
years,
|
|
458
458
|
});
|
|
459
459
|
return {
|
|
460
|
-
tokenCost:
|
|
460
|
+
tokenCost: mARIO.valueOf(),
|
|
461
461
|
wincQty: winc,
|
|
462
462
|
discounts: [],
|
|
463
463
|
};
|
|
@@ -626,12 +626,15 @@ class ARIOReadable {
|
|
|
626
626
|
});
|
|
627
627
|
}
|
|
628
628
|
async resolveArNSName({ name, }) {
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
629
|
+
// derive baseName & undername using last underscore
|
|
630
|
+
const lastUnderscore = name.lastIndexOf('_');
|
|
631
|
+
const baseName = lastUnderscore === -1 ? name : name.slice(lastUnderscore + 1);
|
|
632
|
+
const undername = lastUnderscore === -1 ? '@' : name.slice(0, lastUnderscore);
|
|
633
|
+
// guard against missing or unregistered ARNS record
|
|
634
634
|
const nameData = await this.getArNSRecord({ name: baseName });
|
|
635
|
+
if (nameData === undefined || nameData.processId === undefined) {
|
|
636
|
+
throw new Error(`Base ArNS name ${baseName} not found on ARIO contract (${this.process.processId}).`);
|
|
637
|
+
}
|
|
635
638
|
const ant = ant_js_1.ANT.init({
|
|
636
639
|
process: new ao_process_js_1.AOProcess({
|
|
637
640
|
ao: this.process.ao,
|
|
@@ -650,12 +653,13 @@ class ARIOReadable {
|
|
|
650
653
|
throw new Error(`Invalid record on ANT. Must have ttlSeconds and transactionId. Record: ${JSON.stringify(antRecord)}`);
|
|
651
654
|
}
|
|
652
655
|
return {
|
|
653
|
-
name
|
|
654
|
-
owner
|
|
656
|
+
name,
|
|
657
|
+
owner,
|
|
655
658
|
txId: antRecord.transactionId,
|
|
656
659
|
ttlSeconds: antRecord.ttlSeconds,
|
|
657
660
|
priority: antRecord.priority,
|
|
658
|
-
// NOTE: we may want return the actual index of the record based on sorting
|
|
661
|
+
// NOTE: we may want return the actual index of the record based on sorting
|
|
662
|
+
// in case ANT tries to set duplicate priority values to get around undername limits
|
|
659
663
|
processId: nameData.processId,
|
|
660
664
|
undernameLimit: nameData.undernameLimit,
|
|
661
665
|
type: nameData.type,
|
package/lib/cjs/common/turbo.js
CHANGED
|
@@ -77,7 +77,7 @@ async function signedRequestHeadersFromSigner({ signer, nonce = (0, uuid_1.v4)()
|
|
|
77
77
|
'x-public-key': publicKey,
|
|
78
78
|
'x-nonce': nonce,
|
|
79
79
|
'x-signature': signature,
|
|
80
|
-
'x-signature-type': signatureType,
|
|
80
|
+
'x-signature-type': signatureType.toString(),
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
class TurboArNSPaymentFactory {
|
package/lib/cjs/types/index.js
CHANGED
package/lib/cjs/utils/ao.js
CHANGED
|
@@ -77,8 +77,9 @@ async function spawnANT({ signer, module = constants_js_1.AOS_MODULE_ID, ao = (0
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
if (bootRes === undefined ||
|
|
80
|
-
bootRes.Messages
|
|
80
|
+
bootRes.Messages?.some((m) => m?.Tags?.some((t) => t.value === 'Invalid-Boot-Notice'))) {
|
|
81
81
|
if (bootRes === undefined) {
|
|
82
|
+
// …
|
|
82
83
|
throw new Error('Failed to get boot result');
|
|
83
84
|
}
|
|
84
85
|
const bootError = errorMessageFromOutput(bootRes);
|
package/lib/cjs/version.js
CHANGED
|
@@ -70,7 +70,8 @@ export class ANTVersionsWritable extends ANTVersionsReadable {
|
|
|
70
70
|
super(config);
|
|
71
71
|
this.signer = createAoSigner(signer);
|
|
72
72
|
}
|
|
73
|
-
async addVersion(
|
|
73
|
+
async addVersion(params, options) {
|
|
74
|
+
const { version, moduleId, luaSourceId, notes } = params;
|
|
74
75
|
return this.process.send({
|
|
75
76
|
tags: pruneTags([
|
|
76
77
|
{ name: 'Action', value: 'Add-Version' },
|
|
@@ -78,7 +79,7 @@ export class ANTVersionsWritable extends ANTVersionsReadable {
|
|
|
78
79
|
{ name: 'Module-Id', value: moduleId },
|
|
79
80
|
{ name: 'Lua-Source-Id', value: luaSourceId },
|
|
80
81
|
{ name: 'Notes', value: notes },
|
|
81
|
-
...(tags ?? []),
|
|
82
|
+
...(options?.tags ?? []),
|
|
82
83
|
]),
|
|
83
84
|
signer: this.signer,
|
|
84
85
|
});
|
package/lib/esm/common/index.js
CHANGED
package/lib/esm/common/io.js
CHANGED
|
@@ -453,7 +453,7 @@ export class ARIOReadable {
|
|
|
453
453
|
years,
|
|
454
454
|
});
|
|
455
455
|
return {
|
|
456
|
-
tokenCost:
|
|
456
|
+
tokenCost: mARIO.valueOf(),
|
|
457
457
|
wincQty: winc,
|
|
458
458
|
discounts: [],
|
|
459
459
|
};
|
|
@@ -622,12 +622,15 @@ export class ARIOReadable {
|
|
|
622
622
|
});
|
|
623
623
|
}
|
|
624
624
|
async resolveArNSName({ name, }) {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
625
|
+
// derive baseName & undername using last underscore
|
|
626
|
+
const lastUnderscore = name.lastIndexOf('_');
|
|
627
|
+
const baseName = lastUnderscore === -1 ? name : name.slice(lastUnderscore + 1);
|
|
628
|
+
const undername = lastUnderscore === -1 ? '@' : name.slice(0, lastUnderscore);
|
|
629
|
+
// guard against missing or unregistered ARNS record
|
|
630
630
|
const nameData = await this.getArNSRecord({ name: baseName });
|
|
631
|
+
if (nameData === undefined || nameData.processId === undefined) {
|
|
632
|
+
throw new Error(`Base ArNS name ${baseName} not found on ARIO contract (${this.process.processId}).`);
|
|
633
|
+
}
|
|
631
634
|
const ant = ANT.init({
|
|
632
635
|
process: new AOProcess({
|
|
633
636
|
ao: this.process.ao,
|
|
@@ -646,12 +649,13 @@ export class ARIOReadable {
|
|
|
646
649
|
throw new Error(`Invalid record on ANT. Must have ttlSeconds and transactionId. Record: ${JSON.stringify(antRecord)}`);
|
|
647
650
|
}
|
|
648
651
|
return {
|
|
649
|
-
name
|
|
650
|
-
owner
|
|
652
|
+
name,
|
|
653
|
+
owner,
|
|
651
654
|
txId: antRecord.transactionId,
|
|
652
655
|
ttlSeconds: antRecord.ttlSeconds,
|
|
653
656
|
priority: antRecord.priority,
|
|
654
|
-
// NOTE: we may want return the actual index of the record based on sorting
|
|
657
|
+
// NOTE: we may want return the actual index of the record based on sorting
|
|
658
|
+
// in case ANT tries to set duplicate priority values to get around undername limits
|
|
655
659
|
processId: nameData.processId,
|
|
656
660
|
undernameLimit: nameData.undernameLimit,
|
|
657
661
|
type: nameData.type,
|
package/lib/esm/common/turbo.js
CHANGED
|
@@ -72,7 +72,7 @@ export async function signedRequestHeadersFromSigner({ signer, nonce = uuidv4(),
|
|
|
72
72
|
'x-public-key': publicKey,
|
|
73
73
|
'x-nonce': nonce,
|
|
74
74
|
'x-signature': signature,
|
|
75
|
-
'x-signature-type': signatureType,
|
|
75
|
+
'x-signature-type': signatureType.toString(),
|
|
76
76
|
};
|
|
77
77
|
}
|
|
78
78
|
export class TurboArNSPaymentFactory {
|
package/lib/esm/types/index.js
CHANGED
package/lib/esm/utils/ao.js
CHANGED
|
@@ -66,8 +66,9 @@ export async function spawnANT({ signer, module = AOS_MODULE_ID, ao = connect(),
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
if (bootRes === undefined ||
|
|
69
|
-
bootRes.Messages
|
|
69
|
+
bootRes.Messages?.some((m) => m?.Tags?.some((t) => t.value === 'Invalid-Boot-Notice'))) {
|
|
70
70
|
if (bootRes === undefined) {
|
|
71
|
+
// …
|
|
71
72
|
throw new Error('Failed to get boot result');
|
|
72
73
|
}
|
|
73
74
|
const bootError = errorMessageFromOutput(bootRes);
|
package/lib/esm/version.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AoANTVersionsRead, AoANTVersionsWrite } from '../types/ant.js';
|
|
2
|
-
import { AoMessageResult, WithSigner } from '../types/common.js';
|
|
2
|
+
import { AoMessageResult, WithSigner, WriteOptions } from '../types/common.js';
|
|
3
3
|
import { ProcessConfiguration } from '../types/io.js';
|
|
4
4
|
import { AOProcess } from './contracts/ao-process.js';
|
|
5
5
|
type ANTVersionsNoSigner = ProcessConfiguration;
|
|
@@ -27,13 +27,11 @@ export declare class ANTVersionsReadable implements AoANTVersionsRead {
|
|
|
27
27
|
export declare class ANTVersionsWritable extends ANTVersionsReadable implements AoANTVersionsWrite {
|
|
28
28
|
private signer;
|
|
29
29
|
constructor({ signer, ...config }: WithSigner<ProcessConfiguration>);
|
|
30
|
-
addVersion(
|
|
30
|
+
addVersion(params: {
|
|
31
31
|
version: string;
|
|
32
32
|
moduleId: string;
|
|
33
33
|
luaSourceId?: string;
|
|
34
34
|
notes?: string;
|
|
35
|
-
},
|
|
36
|
-
tags: any;
|
|
37
|
-
}): Promise<AoMessageResult>;
|
|
35
|
+
}, options?: WriteOptions): Promise<AoMessageResult>;
|
|
38
36
|
}
|
|
39
37
|
export {};
|
package/lib/types/version.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NetworkGatewaysProvider = void 0;
|
|
4
|
-
const io_js_1 = require("../../io.js");
|
|
5
|
-
class NetworkGatewaysProvider {
|
|
6
|
-
ario;
|
|
7
|
-
sortBy;
|
|
8
|
-
sortOrder;
|
|
9
|
-
limit;
|
|
10
|
-
filter;
|
|
11
|
-
constructor({ ario = io_js_1.ARIO.mainnet(), sortBy = 'operatorStake', sortOrder = 'desc', limit = 1000, filter = (g) => g.status === 'joined', }) {
|
|
12
|
-
this.ario = ario;
|
|
13
|
-
this.sortBy = sortBy;
|
|
14
|
-
this.sortOrder = sortOrder;
|
|
15
|
-
this.limit = limit;
|
|
16
|
-
this.filter = filter;
|
|
17
|
-
}
|
|
18
|
-
async getGateways() {
|
|
19
|
-
let cursor;
|
|
20
|
-
let attempts = 0;
|
|
21
|
-
const gateways = [];
|
|
22
|
-
do {
|
|
23
|
-
try {
|
|
24
|
-
const { items: newGateways = [], nextCursor } = await this.ario.getGateways({
|
|
25
|
-
limit: 1000,
|
|
26
|
-
cursor,
|
|
27
|
-
sortBy: this.sortBy,
|
|
28
|
-
sortOrder: this.sortOrder,
|
|
29
|
-
});
|
|
30
|
-
gateways.push(...newGateways);
|
|
31
|
-
cursor = nextCursor;
|
|
32
|
-
attempts = 0; // reset attempts if we get a new cursor
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
console.error('Error fetching gateways', {
|
|
36
|
-
cursor,
|
|
37
|
-
attempts,
|
|
38
|
-
error,
|
|
39
|
-
});
|
|
40
|
-
attempts++;
|
|
41
|
-
}
|
|
42
|
-
} while (cursor !== undefined && attempts < 3);
|
|
43
|
-
// filter out any gateways that are not joined
|
|
44
|
-
const filteredGateways = gateways.filter(this.filter).slice(0, this.limit);
|
|
45
|
-
return filteredGateways.map((g) => new URL(`${g.settings.protocol}://${g.settings.fqdn}:${g.settings.port}`));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
exports.NetworkGatewaysProvider = NetworkGatewaysProvider;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SimpleCacheGatewaysProvider = void 0;
|
|
4
|
-
const index_js_1 = require("../../../web/index.js");
|
|
5
|
-
class SimpleCacheGatewaysProvider {
|
|
6
|
-
gatewaysProvider;
|
|
7
|
-
ttlSeconds;
|
|
8
|
-
lastUpdated;
|
|
9
|
-
gatewaysCache;
|
|
10
|
-
logger;
|
|
11
|
-
constructor({ gatewaysProvider, ttlSeconds = 60 * 60, // 1 hour
|
|
12
|
-
logger = index_js_1.Logger.default, }) {
|
|
13
|
-
this.gatewaysCache = [];
|
|
14
|
-
this.gatewaysProvider = gatewaysProvider;
|
|
15
|
-
this.ttlSeconds = ttlSeconds;
|
|
16
|
-
this.logger = logger;
|
|
17
|
-
}
|
|
18
|
-
async getGateways() {
|
|
19
|
-
const now = Date.now();
|
|
20
|
-
if (this.gatewaysCache.length === 0 ||
|
|
21
|
-
now - this.lastUpdated > this.ttlSeconds * 1000) {
|
|
22
|
-
try {
|
|
23
|
-
// preserve the cache if the fetch fails
|
|
24
|
-
const allGateways = await this.gatewaysProvider.getGateways();
|
|
25
|
-
this.gatewaysCache = allGateways;
|
|
26
|
-
this.lastUpdated = now;
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
this.logger.error('Error fetching gateways', error);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return this.gatewaysCache;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.SimpleCacheGatewaysProvider = SimpleCacheGatewaysProvider;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StaticGatewaysProvider = void 0;
|
|
4
|
-
class StaticGatewaysProvider {
|
|
5
|
-
gateways;
|
|
6
|
-
constructor({ gateways }) {
|
|
7
|
-
this.gateways = gateways.map((g) => new URL(g));
|
|
8
|
-
}
|
|
9
|
-
async getGateways() {
|
|
10
|
-
return this.gateways;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
exports.StaticGatewaysProvider = StaticGatewaysProvider;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
/**
|
|
18
|
-
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
19
|
-
*
|
|
20
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
21
|
-
* you may not use this file except in compliance with the License.
|
|
22
|
-
* You may obtain a copy of the License at
|
|
23
|
-
*
|
|
24
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
25
|
-
*
|
|
26
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
27
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
28
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
29
|
-
* See the License for the specific language governing permissions and
|
|
30
|
-
* limitations under the License.
|
|
31
|
-
*/
|
|
32
|
-
__exportStar(require("./wayfinder.js"), exports);
|
|
33
|
-
// routing strategies
|
|
34
|
-
__exportStar(require("./routing/strategies/random.js"), exports);
|
|
35
|
-
__exportStar(require("./routing/strategies/static.js"), exports);
|
|
36
|
-
__exportStar(require("./routing/strategies/ping.js"), exports);
|
|
37
|
-
__exportStar(require("./routing/strategies/round-robin.js"), exports);
|
|
38
|
-
// gateways providers
|
|
39
|
-
__exportStar(require("./gateways/network.js"), exports);
|
|
40
|
-
__exportStar(require("./gateways/simple-cache.js"), exports);
|
|
41
|
-
__exportStar(require("./gateways/static.js"), exports);
|
|
42
|
-
// trusted gateways
|
|
43
|
-
__exportStar(require("./verification/trusted.js"), exports);
|
|
44
|
-
// hash providers
|
|
45
|
-
__exportStar(require("./verification/strategies/data-root-verifier.js"), exports);
|
|
46
|
-
__exportStar(require("./verification/strategies/hash-verifier.js"), exports);
|
|
47
|
-
// TODO: signature verification
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FastestPingRoutingStrategy = void 0;
|
|
4
|
-
class FastestPingRoutingStrategy {
|
|
5
|
-
timeoutMs;
|
|
6
|
-
probePath;
|
|
7
|
-
constructor({ timeoutMs = 500, probePath = '/ar-io/info', // TODO: limit to allowed /ar-io and arweave node endpoints
|
|
8
|
-
} = {}) {
|
|
9
|
-
this.timeoutMs = timeoutMs;
|
|
10
|
-
this.probePath = probePath;
|
|
11
|
-
}
|
|
12
|
-
async selectGateway({ gateways }) {
|
|
13
|
-
if (gateways.length === 0) {
|
|
14
|
-
throw new Error('No gateways provided');
|
|
15
|
-
}
|
|
16
|
-
try {
|
|
17
|
-
const results = await Promise.allSettled(gateways.map(async (gateway) => {
|
|
18
|
-
try {
|
|
19
|
-
const startTime = Date.now();
|
|
20
|
-
const response = await fetch(`${gateway.toString().replace(/\/$/, '')}${this.probePath}`, {
|
|
21
|
-
method: 'HEAD',
|
|
22
|
-
signal: AbortSignal.timeout(this.timeoutMs),
|
|
23
|
-
});
|
|
24
|
-
const endTime = Date.now();
|
|
25
|
-
const durationMs = endTime - startTime;
|
|
26
|
-
return {
|
|
27
|
-
gateway,
|
|
28
|
-
status: response.status,
|
|
29
|
-
durationMs,
|
|
30
|
-
error: null,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
// Handle network errors
|
|
35
|
-
return {
|
|
36
|
-
gateway,
|
|
37
|
-
status: 'rejected',
|
|
38
|
-
durationMs: Infinity,
|
|
39
|
-
error,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
}));
|
|
43
|
-
// Process results
|
|
44
|
-
const processedResults = results.map((result, index) => {
|
|
45
|
-
if (result.status === 'fulfilled') {
|
|
46
|
-
return result.value;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
return {
|
|
50
|
-
gateway: gateways[index],
|
|
51
|
-
status: 'rejected',
|
|
52
|
-
durationMs: Infinity,
|
|
53
|
-
error: result.reason,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
// Filter healthy gateways and sort by latency
|
|
58
|
-
const healthyGateways = processedResults
|
|
59
|
-
.filter((result) => result.status === 200)
|
|
60
|
-
.sort((a, b) => a.durationMs - b.durationMs);
|
|
61
|
-
if (healthyGateways.length > 0) {
|
|
62
|
-
return healthyGateways[0].gateway;
|
|
63
|
-
}
|
|
64
|
-
throw new Error('No healthy gateways found');
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
throw new Error('Failed to ping gateways: ' +
|
|
68
|
-
(error instanceof Error ? error.message : String(error)));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
exports.FastestPingRoutingStrategy = FastestPingRoutingStrategy;
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
/**
|
|
7
|
-
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc.
|
|
8
|
-
*
|
|
9
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
-
* you may not use this file except in compliance with the License.
|
|
11
|
-
* You may obtain a copy of the License at
|
|
12
|
-
*
|
|
13
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
-
* See the License for the specific language governing permissions and
|
|
19
|
-
* limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
22
|
-
const node_test_1 = require("node:test");
|
|
23
|
-
const ping_js_1 = require("./ping.js");
|
|
24
|
-
(0, node_test_1.describe)('FastestPingRoutingStrategy', () => {
|
|
25
|
-
// Original fetch function
|
|
26
|
-
const originalFetch = global.fetch;
|
|
27
|
-
// Mock response options for each gateway
|
|
28
|
-
const mockResponses = new Map();
|
|
29
|
-
(0, node_test_1.beforeEach)(() => {
|
|
30
|
-
// reset mock responses
|
|
31
|
-
mockResponses.clear();
|
|
32
|
-
// mock fetch to simulate network latency and response status
|
|
33
|
-
global.fetch = async (url) => {
|
|
34
|
-
const urlString = url.toString();
|
|
35
|
-
// find the matching gateway
|
|
36
|
-
let matchingGateway = '';
|
|
37
|
-
for (const gateway of mockResponses.keys()) {
|
|
38
|
-
if (urlString.startsWith(gateway)) {
|
|
39
|
-
matchingGateway = gateway;
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (!matchingGateway) {
|
|
44
|
-
return Promise.reject(new Error(`No mock response for URL: ${urlString}`));
|
|
45
|
-
}
|
|
46
|
-
const { status, delayMs } = mockResponses.get(matchingGateway);
|
|
47
|
-
// simulate network delay
|
|
48
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
49
|
-
return new Response(null, { status });
|
|
50
|
-
};
|
|
51
|
-
// mock AbortSignal.timeout
|
|
52
|
-
if (!AbortSignal.timeout) {
|
|
53
|
-
AbortSignal.timeout = (ms) => {
|
|
54
|
-
const controller = new AbortController();
|
|
55
|
-
setTimeout(() => controller.abort(), ms);
|
|
56
|
-
return controller.signal;
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
// restore original fetch after tests
|
|
61
|
-
(0, node_test_1.afterEach)(() => {
|
|
62
|
-
global.fetch = originalFetch;
|
|
63
|
-
});
|
|
64
|
-
(0, node_test_1.it)('selects the gateway with the lowest latency', async () => {
|
|
65
|
-
const gateways = [
|
|
66
|
-
new URL('https://slow.com'),
|
|
67
|
-
new URL('https://fast.com'),
|
|
68
|
-
new URL('https://medium.com'),
|
|
69
|
-
];
|
|
70
|
-
// configure mock responses
|
|
71
|
-
mockResponses.set('https://slow.com', { status: 200, delayMs: 300 });
|
|
72
|
-
mockResponses.set('https://fast.com', { status: 200, delayMs: 50 });
|
|
73
|
-
mockResponses.set('https://medium.com', { status: 200, delayMs: 150 });
|
|
74
|
-
const strategy = new ping_js_1.FastestPingRoutingStrategy({ timeoutMs: 500 });
|
|
75
|
-
// select the gateway with the lowest latency
|
|
76
|
-
const selectedGateway = await strategy.selectGateway({
|
|
77
|
-
gateways,
|
|
78
|
-
});
|
|
79
|
-
strict_1.default.equal(selectedGateway.toString(), 'https://fast.com/', 'Should select the gateway with the lowest latency');
|
|
80
|
-
});
|
|
81
|
-
(0, node_test_1.it)('ignores gateways that return non-200 status codes', async () => {
|
|
82
|
-
const gateways = [
|
|
83
|
-
new URL('https://error.com'),
|
|
84
|
-
new URL('https://success.com'),
|
|
85
|
-
new URL('https://another-error.com'),
|
|
86
|
-
];
|
|
87
|
-
// configure mock responses
|
|
88
|
-
mockResponses.set('https://error.com', { status: 404, delayMs: 50 });
|
|
89
|
-
mockResponses.set('https://success.com', { status: 200, delayMs: 100 });
|
|
90
|
-
mockResponses.set('https://another-error.com', {
|
|
91
|
-
status: 500,
|
|
92
|
-
delayMs: 75,
|
|
93
|
-
});
|
|
94
|
-
const strategy = new ping_js_1.FastestPingRoutingStrategy({ timeoutMs: 500 });
|
|
95
|
-
// select the gateway with the lowest latency
|
|
96
|
-
const selectedGateway = await strategy.selectGateway({
|
|
97
|
-
gateways,
|
|
98
|
-
});
|
|
99
|
-
strict_1.default.equal(selectedGateway.toString(), 'https://success.com/', 'Should select the gateway that returns a 200 status code');
|
|
100
|
-
});
|
|
101
|
-
(0, node_test_1.it)('throws an error when all gateways fail', async () => {
|
|
102
|
-
const gateways = [
|
|
103
|
-
new URL('https://error1.com'),
|
|
104
|
-
new URL('https://error2.com'),
|
|
105
|
-
];
|
|
106
|
-
// configure mock responses
|
|
107
|
-
mockResponses.set('https://error1.com', { status: 404, delayMs: 50 });
|
|
108
|
-
mockResponses.set('https://error2.com', { status: 500, delayMs: 75 });
|
|
109
|
-
const strategy = new ping_js_1.FastestPingRoutingStrategy({ timeoutMs: 500 });
|
|
110
|
-
// select the gateway with the lowest latency
|
|
111
|
-
await strict_1.default.rejects(async () => await strategy.selectGateway({ gateways }), /No healthy gateways found/, 'Should throw an error when all gateways fail');
|
|
112
|
-
});
|
|
113
|
-
(0, node_test_1.it)('handles network errors gracefully', async () => {
|
|
114
|
-
const gateways = [
|
|
115
|
-
new URL('https://network-error.com'),
|
|
116
|
-
new URL('https://success.com'),
|
|
117
|
-
];
|
|
118
|
-
// configure mock responses
|
|
119
|
-
mockResponses.set('https://success.com', { status: 200, delayMs: 100 });
|
|
120
|
-
// override fetch for the network error case
|
|
121
|
-
const originalFetchMock = global.fetch;
|
|
122
|
-
global.fetch = async (url) => {
|
|
123
|
-
if (url.toString().includes('network-error')) {
|
|
124
|
-
throw new Error('Network error');
|
|
125
|
-
}
|
|
126
|
-
return originalFetchMock(url);
|
|
127
|
-
};
|
|
128
|
-
const strategy = new ping_js_1.FastestPingRoutingStrategy({ timeoutMs: 500 });
|
|
129
|
-
// select the gateway with the lowest latency
|
|
130
|
-
const selectedGateway = await strategy.selectGateway({
|
|
131
|
-
gateways,
|
|
132
|
-
});
|
|
133
|
-
strict_1.default.equal(selectedGateway.toString(), 'https://success.com/', 'Should handle network errors and select the working gateway');
|
|
134
|
-
});
|
|
135
|
-
(0, node_test_1.it)('respects the timeout parameter', async () => {
|
|
136
|
-
const gateways = [
|
|
137
|
-
new URL('https://timeout.com'),
|
|
138
|
-
new URL('https://fast.com'),
|
|
139
|
-
];
|
|
140
|
-
// configure mock responses
|
|
141
|
-
mockResponses.set('https://timeout.com', { status: 200, delayMs: 300 });
|
|
142
|
-
mockResponses.set('https://fast.com', { status: 200, delayMs: 50 });
|
|
143
|
-
// set a short timeout
|
|
144
|
-
const strategy = new ping_js_1.FastestPingRoutingStrategy({ timeoutMs: 100 });
|
|
145
|
-
const selectedGateway = await strategy.selectGateway({
|
|
146
|
-
gateways,
|
|
147
|
-
});
|
|
148
|
-
strict_1.default.equal(selectedGateway.toString(), 'https://fast.com/', 'Should respect the timeout and select only gateways that respond within the timeout');
|
|
149
|
-
});
|
|
150
|
-
(0, node_test_1.it)('throws an error when no gateways are provided', async () => {
|
|
151
|
-
const gateways = [];
|
|
152
|
-
const strategy = new ping_js_1.FastestPingRoutingStrategy();
|
|
153
|
-
// select the gateway with the lowest latency
|
|
154
|
-
await strict_1.default.rejects(async () => await strategy.selectGateway({ gateways }), /No gateways provided/, 'Should throw an error when no gateways are provided');
|
|
155
|
-
});
|
|
156
|
-
});
|