@metamask-previews/network-controller 22.1.1-preview-ee77a6d9 → 22.1.1-preview-33c13522
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/package.json +1 -1
- package/dist/rpc-service/rpc-service-chain.cjs +0 -109
- package/dist/rpc-service/rpc-service-chain.cjs.map +0 -1
- package/dist/rpc-service/rpc-service-chain.d.cts +0 -116
- package/dist/rpc-service/rpc-service-chain.d.cts.map +0 -1
- package/dist/rpc-service/rpc-service-chain.d.mts +0 -116
- package/dist/rpc-service/rpc-service-chain.d.mts.map +0 -1
- package/dist/rpc-service/rpc-service-chain.mjs +0 -105
- package/dist/rpc-service/rpc-service-chain.mjs.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/network-controller",
|
|
3
|
-
"version": "22.1.1-preview-
|
|
3
|
+
"version": "22.1.1-preview-33c13522",
|
|
4
4
|
"description": "Provides an interface to the currently selected network via a MetaMask-compatible provider object",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
|
-
var _RpcServiceChain_instances, _RpcServiceChain_services, _RpcServiceChain_buildRpcServiceChain;
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.RpcServiceChain = void 0;
|
|
16
|
-
const rpc_service_1 = require("./rpc-service.cjs");
|
|
17
|
-
/**
|
|
18
|
-
* This class constructs a chain of RpcService objects which represent a
|
|
19
|
-
* particular network. The first object in the chain is intended to be the primary
|
|
20
|
-
* way of reaching the network and the remaining objects are used as failovers.
|
|
21
|
-
*/
|
|
22
|
-
class RpcServiceChain {
|
|
23
|
-
/**
|
|
24
|
-
* Constructs a new RpcServiceChain object.
|
|
25
|
-
*
|
|
26
|
-
* @param args - The arguments.
|
|
27
|
-
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
28
|
-
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
29
|
-
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
30
|
-
* via `node-fetch`).
|
|
31
|
-
* @param args.btoa - A function that can be used to convert a binary string
|
|
32
|
-
* into base-64. Used to encode authorization credentials.
|
|
33
|
-
* @param args.serviceConfigurations - The options for the RPC services that
|
|
34
|
-
* you want to construct. This class takes a set of configuration objects and
|
|
35
|
-
* not literal `RpcService`s to account for the possibility that we may want
|
|
36
|
-
* to send request headers to official Infura endpoints and not failovers.
|
|
37
|
-
*/
|
|
38
|
-
constructor({ fetch: givenFetch, btoa: givenBtoa, serviceConfigurations, }) {
|
|
39
|
-
_RpcServiceChain_instances.add(this);
|
|
40
|
-
_RpcServiceChain_services.set(this, void 0);
|
|
41
|
-
__classPrivateFieldSet(this, _RpcServiceChain_services, __classPrivateFieldGet(this, _RpcServiceChain_instances, "m", _RpcServiceChain_buildRpcServiceChain).call(this, {
|
|
42
|
-
serviceConfigurations,
|
|
43
|
-
fetch: givenFetch,
|
|
44
|
-
btoa: givenBtoa,
|
|
45
|
-
}), "f");
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Listens for when any of the RPC services retry a request.
|
|
49
|
-
*
|
|
50
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
51
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
52
|
-
*/
|
|
53
|
-
onRetry(listener) {
|
|
54
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onRetry(listener));
|
|
55
|
-
return {
|
|
56
|
-
dispose() {
|
|
57
|
-
disposables.forEach((disposable) => disposable.dispose());
|
|
58
|
-
},
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Listens for when any of the RPC services retry the request too many times
|
|
63
|
-
* in a row.
|
|
64
|
-
*
|
|
65
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
66
|
-
* @returns What {@link RpcService.onBreak} returns.
|
|
67
|
-
*/
|
|
68
|
-
onBreak(listener) {
|
|
69
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onBreak(listener));
|
|
70
|
-
return {
|
|
71
|
-
dispose() {
|
|
72
|
-
disposables.forEach((disposable) => disposable.dispose());
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Listens for when any of the RPC services send a slow request.
|
|
78
|
-
*
|
|
79
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
80
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
81
|
-
*/
|
|
82
|
-
onDegraded(listener) {
|
|
83
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onDegraded(listener));
|
|
84
|
-
return {
|
|
85
|
-
dispose() {
|
|
86
|
-
disposables.forEach((disposable) => disposable.dispose());
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
async request(jsonRpcRequest, fetchOptions = {}) {
|
|
91
|
-
return __classPrivateFieldGet(this, _RpcServiceChain_services, "f")[0].request(jsonRpcRequest, fetchOptions);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
exports.RpcServiceChain = RpcServiceChain;
|
|
95
|
-
_RpcServiceChain_services = new WeakMap(), _RpcServiceChain_instances = new WeakSet(), _RpcServiceChain_buildRpcServiceChain = function _RpcServiceChain_buildRpcServiceChain({ serviceConfigurations, fetch: givenFetch, btoa: givenBtoa, }) {
|
|
96
|
-
return [...serviceConfigurations]
|
|
97
|
-
.reverse()
|
|
98
|
-
.reduce((workingServices, serviceConfiguration, index) => {
|
|
99
|
-
const failoverService = index > 0 ? workingServices[0] : undefined;
|
|
100
|
-
const service = new rpc_service_1.RpcService({
|
|
101
|
-
fetch: givenFetch,
|
|
102
|
-
btoa: givenBtoa,
|
|
103
|
-
...serviceConfiguration,
|
|
104
|
-
failoverService,
|
|
105
|
-
});
|
|
106
|
-
return [service, ...workingServices];
|
|
107
|
-
}, []);
|
|
108
|
-
};
|
|
109
|
-
//# sourceMappingURL=rpc-service-chain.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-chain.cjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-chain.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAQA,mDAA2C;AAkB3C;;;;GAIG;AACH,MAAa,eAAe;IAG1B;;;;;;;;;;;;;;OAcG;IACH,YAAY,EACV,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,EACf,qBAAqB,GAKtB;;QAzBQ,4CAAwB;QA0B/B,uBAAA,IAAI,6BAAa,uBAAA,IAAI,yEAAsB,MAA1B,IAAI,EAAuB;YAC1C,qBAAqB;YACrB,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,SAAS;SAChB,CAAC,MAAA,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,QAA8C;QACpD,MAAM,WAAW,GAAG,uBAAA,IAAI,iCAAU,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAC1B,CAAC;QAEF,OAAO;YACL,OAAO;gBACL,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,OAAO,CAAC,QAA8C;QACpD,MAAM,WAAW,GAAG,uBAAA,IAAI,iCAAU,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAC1B,CAAC;QAEF,OAAO;YACL,OAAO;gBACL,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,QAAiD;QAC1D,MAAM,WAAW,GAAG,uBAAA,IAAI,iCAAU,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAC7B,CAAC;QAEF,OAAO;YACL,OAAO;gBACL,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;IAgDD,KAAK,CAAC,OAAO,CACX,cAAsC,EACtC,eAA6B,EAAE;QAE/B,OAAO,uBAAA,IAAI,iCAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;IACjE,CAAC;CAqCF;AAjLD,0CAiLC;8KAtBuB,EACpB,qBAAqB,EACrB,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAKhB;IACC,OAAO,CAAC,GAAG,qBAAqB,CAAC;SAC9B,OAAO,EAAE;SACT,MAAM,CAAC,CAAC,eAA6B,EAAE,oBAAoB,EAAE,KAAK,EAAE,EAAE;QACrE,MAAM,eAAe,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACnE,MAAM,OAAO,GAAG,IAAI,wBAAU,CAAC;YAC7B,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,SAAS;YACf,GAAG,oBAAoB;YACvB,eAAe;SAChB,CAAC,CAAC;QACH,OAAO,CAAC,OAAO,EAAE,GAAG,eAAe,CAAC,CAAC;IACvC,CAAC,EAAE,EAAE,CAAC,CAAC;AACX,CAAC","sourcesContent":["import type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { AbstractRpcService } from './abstract-rpc-service';\nimport { RpcService } from './rpc-service';\nimport type { FetchOptions } from './shared';\n\n/**\n * The subset of options accepted by the RpcServiceChain constructor which\n * represent a single endpoint.\n */\ntype RpcServiceConfiguration = {\n /**\n * The URL of the endpoint.\n */\n endpointUrl: URL | string;\n /**\n * The options to pass to `fetch` when making the request to the endpoint.\n */\n fetchOptions?: FetchOptions;\n};\n\n/**\n * This class constructs a chain of RpcService objects which represent a\n * particular network. The first object in the chain is intended to be the primary\n * way of reaching the network and the remaining objects are used as failovers.\n */\nexport class RpcServiceChain implements AbstractRpcService {\n readonly #services: RpcService[];\n\n /**\n * Constructs a new RpcServiceChain object.\n *\n * @param args - The arguments.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * If your JavaScript environment supports `fetch` natively, you'll probably\n * want to pass that; otherwise you can pass an equivalent (such as `fetch`\n * via `node-fetch`).\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64. Used to encode authorization credentials.\n * @param args.serviceConfigurations - The options for the RPC services that\n * you want to construct. This class takes a set of configuration objects and\n * not literal `RpcService`s to account for the possibility that we may want\n * to send request headers to official Infura endpoints and not failovers.\n */\n constructor({\n fetch: givenFetch,\n btoa: givenBtoa,\n serviceConfigurations,\n }: {\n fetch: typeof fetch;\n btoa: typeof btoa;\n serviceConfigurations: RpcServiceConfiguration[];\n }) {\n this.#services = this.#buildRpcServiceChain({\n serviceConfigurations,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n }\n\n /**\n * Listens for when any of the RPC services retry a request.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link RpcService.onRetry} returns.\n */\n onRetry(listener: Parameters<RpcService['onRetry']>[0]) {\n const disposables = this.#services.map((service) =>\n service.onRetry(listener),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Listens for when any of the RPC services retry the request too many times\n * in a row.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link RpcService.onBreak} returns.\n */\n onBreak(listener: Parameters<RpcService['onBreak']>[0]) {\n const disposables = this.#services.map((service) =>\n service.onBreak(listener),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Listens for when any of the RPC services send a slow request.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link RpcService.onRetry} returns.\n */\n onDegraded(listener: Parameters<RpcService['onDegraded']>[0]) {\n const disposables = this.#services.map((service) =>\n service.onDegraded(listener),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Makes a request to the first RPC service in the chain. If this service is\n * down, then the request is forwarded to the next service in the chain, etc.\n *\n * This overload is specifically designed for `eth_getBlockByNumber`, which\n * can return a `result` of `null` despite an expected `Result` being\n * provided.\n *\n * @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.\n * @param fetchOptions - An options bag for {@link fetch} which further\n * specifies the request.\n * @returns The decoded JSON-RPC response from the endpoint.\n * @throws A \"method not found\" error if the response status is 405.\n * @throws A rate limiting error if the response HTTP status is 429.\n * @throws A timeout error if the response HTTP status is 503 or 504.\n * @throws A generic error if the response HTTP status is not 2xx but also not\n * 405, 429, 503, or 504.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: JsonRpcRequest<Params> & { method: 'eth_getBlockByNumber' },\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;\n\n /**\n * Makes a request to the first RPC service in the chain. If this service is\n * down, then the request is forwarded to the next service in the chain, etc.\n *\n * This overload is designed for all RPC methods except for\n * `eth_getBlockByNumber`, which are expected to return a `result` of the\n * expected `Result`.\n *\n * @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.\n * @param fetchOptions - An options bag for {@link fetch} which further\n * specifies the request.\n * @returns The decoded JSON-RPC response from the endpoint.\n * @throws A \"method not found\" error if the response status is 405.\n * @throws A rate limiting error if the response HTTP status is 429.\n * @throws A timeout error if the response HTTP status is 503 or 504.\n * @throws A generic error if the response HTTP status is not 2xx but also not\n * 405, 429, 503, or 504.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: JsonRpcRequest<Params>,\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result>>;\n\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: JsonRpcRequest<Params>,\n fetchOptions: FetchOptions = {},\n ): Promise<JsonRpcResponse<Result | null>> {\n return this.#services[0].request(jsonRpcRequest, fetchOptions);\n }\n\n /**\n * Constructs the chain of RPC services. The second RPC service is\n * configured as the failover for the first, the third service is\n * configured as the failover for the second, etc.\n *\n * @param args - The arguments.\n * @param args.serviceConfigurations - The options for the RPC services that\n * you want to construct.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64. Used to encode authorization credentials.\n * @returns The constructed chain of RPC services.\n */\n #buildRpcServiceChain({\n serviceConfigurations,\n fetch: givenFetch,\n btoa: givenBtoa,\n }: {\n serviceConfigurations: RpcServiceConfiguration[];\n fetch: typeof fetch;\n btoa: typeof btoa;\n }): RpcService[] {\n return [...serviceConfigurations]\n .reverse()\n .reduce((workingServices: RpcService[], serviceConfiguration, index) => {\n const failoverService = index > 0 ? workingServices[0] : undefined;\n const service = new RpcService({\n fetch: givenFetch,\n btoa: givenBtoa,\n ...serviceConfiguration,\n failoverService,\n });\n return [service, ...workingServices];\n }, []);\n }\n}\n"]}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcResponse } from "@metamask/utils";
|
|
2
|
-
import type { AbstractRpcService } from "./abstract-rpc-service.cjs";
|
|
3
|
-
import { RpcService } from "./rpc-service.cjs";
|
|
4
|
-
import type { FetchOptions } from "./shared.cjs";
|
|
5
|
-
/**
|
|
6
|
-
* The subset of options accepted by the RpcServiceChain constructor which
|
|
7
|
-
* represent a single endpoint.
|
|
8
|
-
*/
|
|
9
|
-
type RpcServiceConfiguration = {
|
|
10
|
-
/**
|
|
11
|
-
* The URL of the endpoint.
|
|
12
|
-
*/
|
|
13
|
-
endpointUrl: URL | string;
|
|
14
|
-
/**
|
|
15
|
-
* The options to pass to `fetch` when making the request to the endpoint.
|
|
16
|
-
*/
|
|
17
|
-
fetchOptions?: FetchOptions;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* This class constructs a chain of RpcService objects which represent a
|
|
21
|
-
* particular network. The first object in the chain is intended to be the primary
|
|
22
|
-
* way of reaching the network and the remaining objects are used as failovers.
|
|
23
|
-
*/
|
|
24
|
-
export declare class RpcServiceChain implements AbstractRpcService {
|
|
25
|
-
#private;
|
|
26
|
-
/**
|
|
27
|
-
* Constructs a new RpcServiceChain object.
|
|
28
|
-
*
|
|
29
|
-
* @param args - The arguments.
|
|
30
|
-
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
31
|
-
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
32
|
-
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
33
|
-
* via `node-fetch`).
|
|
34
|
-
* @param args.btoa - A function that can be used to convert a binary string
|
|
35
|
-
* into base-64. Used to encode authorization credentials.
|
|
36
|
-
* @param args.serviceConfigurations - The options for the RPC services that
|
|
37
|
-
* you want to construct. This class takes a set of configuration objects and
|
|
38
|
-
* not literal `RpcService`s to account for the possibility that we may want
|
|
39
|
-
* to send request headers to official Infura endpoints and not failovers.
|
|
40
|
-
*/
|
|
41
|
-
constructor({ fetch: givenFetch, btoa: givenBtoa, serviceConfigurations, }: {
|
|
42
|
-
fetch: typeof fetch;
|
|
43
|
-
btoa: typeof btoa;
|
|
44
|
-
serviceConfigurations: RpcServiceConfiguration[];
|
|
45
|
-
});
|
|
46
|
-
/**
|
|
47
|
-
* Listens for when any of the RPC services retry a request.
|
|
48
|
-
*
|
|
49
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
50
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
51
|
-
*/
|
|
52
|
-
onRetry(listener: Parameters<RpcService['onRetry']>[0]): {
|
|
53
|
-
dispose(): void;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Listens for when any of the RPC services retry the request too many times
|
|
57
|
-
* in a row.
|
|
58
|
-
*
|
|
59
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
60
|
-
* @returns What {@link RpcService.onBreak} returns.
|
|
61
|
-
*/
|
|
62
|
-
onBreak(listener: Parameters<RpcService['onBreak']>[0]): {
|
|
63
|
-
dispose(): void;
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Listens for when any of the RPC services send a slow request.
|
|
67
|
-
*
|
|
68
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
69
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
70
|
-
*/
|
|
71
|
-
onDegraded(listener: Parameters<RpcService['onDegraded']>[0]): {
|
|
72
|
-
dispose(): void;
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* Makes a request to the first RPC service in the chain. If this service is
|
|
76
|
-
* down, then the request is forwarded to the next service in the chain, etc.
|
|
77
|
-
*
|
|
78
|
-
* This overload is specifically designed for `eth_getBlockByNumber`, which
|
|
79
|
-
* can return a `result` of `null` despite an expected `Result` being
|
|
80
|
-
* provided.
|
|
81
|
-
*
|
|
82
|
-
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
|
|
83
|
-
* @param fetchOptions - An options bag for {@link fetch} which further
|
|
84
|
-
* specifies the request.
|
|
85
|
-
* @returns The decoded JSON-RPC response from the endpoint.
|
|
86
|
-
* @throws A "method not found" error if the response status is 405.
|
|
87
|
-
* @throws A rate limiting error if the response HTTP status is 429.
|
|
88
|
-
* @throws A timeout error if the response HTTP status is 503 or 504.
|
|
89
|
-
* @throws A generic error if the response HTTP status is not 2xx but also not
|
|
90
|
-
* 405, 429, 503, or 504.
|
|
91
|
-
*/
|
|
92
|
-
request<Params extends JsonRpcParams, Result extends Json>(jsonRpcRequest: JsonRpcRequest<Params> & {
|
|
93
|
-
method: 'eth_getBlockByNumber';
|
|
94
|
-
}, fetchOptions?: FetchOptions): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;
|
|
95
|
-
/**
|
|
96
|
-
* Makes a request to the first RPC service in the chain. If this service is
|
|
97
|
-
* down, then the request is forwarded to the next service in the chain, etc.
|
|
98
|
-
*
|
|
99
|
-
* This overload is designed for all RPC methods except for
|
|
100
|
-
* `eth_getBlockByNumber`, which are expected to return a `result` of the
|
|
101
|
-
* expected `Result`.
|
|
102
|
-
*
|
|
103
|
-
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
|
|
104
|
-
* @param fetchOptions - An options bag for {@link fetch} which further
|
|
105
|
-
* specifies the request.
|
|
106
|
-
* @returns The decoded JSON-RPC response from the endpoint.
|
|
107
|
-
* @throws A "method not found" error if the response status is 405.
|
|
108
|
-
* @throws A rate limiting error if the response HTTP status is 429.
|
|
109
|
-
* @throws A timeout error if the response HTTP status is 503 or 504.
|
|
110
|
-
* @throws A generic error if the response HTTP status is not 2xx but also not
|
|
111
|
-
* 405, 429, 503, or 504.
|
|
112
|
-
*/
|
|
113
|
-
request<Params extends JsonRpcParams, Result extends Json>(jsonRpcRequest: JsonRpcRequest<Params>, fetchOptions?: FetchOptions): Promise<JsonRpcResponse<Result>>;
|
|
114
|
-
}
|
|
115
|
-
export {};
|
|
116
|
-
//# sourceMappingURL=rpc-service-chain.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-chain.d.cts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-chain.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,kBAAkB,EAAE,mCAA+B;AACjE,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAE7C;;;GAGG;AACH,KAAK,uBAAuB,GAAG;IAC7B;;OAEG;IACH,WAAW,EAAE,GAAG,GAAG,MAAM,CAAC;IAC1B;;OAEG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC;AAEF;;;;GAIG;AACH,qBAAa,eAAgB,YAAW,kBAAkB;;IAGxD;;;;;;;;;;;;;;OAcG;gBACS,EACV,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,EACf,qBAAqB,GACtB,EAAE;QACD,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,IAAI,EAAE,OAAO,IAAI,CAAC;QAClB,qBAAqB,EAAE,uBAAuB,EAAE,CAAC;KAClD;IAQD;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;;;IAYtD;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;;;IAYtD;;;;;OAKG;IACH,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;;;IAY5D;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GAAG;QAAE,MAAM,EAAE,sBAAsB,CAAA;KAAE,EAC3E,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IAE3D;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,EACtC,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;CA4CpC"}
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcResponse } from "@metamask/utils";
|
|
2
|
-
import type { AbstractRpcService } from "./abstract-rpc-service.mjs";
|
|
3
|
-
import { RpcService } from "./rpc-service.mjs";
|
|
4
|
-
import type { FetchOptions } from "./shared.mjs";
|
|
5
|
-
/**
|
|
6
|
-
* The subset of options accepted by the RpcServiceChain constructor which
|
|
7
|
-
* represent a single endpoint.
|
|
8
|
-
*/
|
|
9
|
-
type RpcServiceConfiguration = {
|
|
10
|
-
/**
|
|
11
|
-
* The URL of the endpoint.
|
|
12
|
-
*/
|
|
13
|
-
endpointUrl: URL | string;
|
|
14
|
-
/**
|
|
15
|
-
* The options to pass to `fetch` when making the request to the endpoint.
|
|
16
|
-
*/
|
|
17
|
-
fetchOptions?: FetchOptions;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* This class constructs a chain of RpcService objects which represent a
|
|
21
|
-
* particular network. The first object in the chain is intended to be the primary
|
|
22
|
-
* way of reaching the network and the remaining objects are used as failovers.
|
|
23
|
-
*/
|
|
24
|
-
export declare class RpcServiceChain implements AbstractRpcService {
|
|
25
|
-
#private;
|
|
26
|
-
/**
|
|
27
|
-
* Constructs a new RpcServiceChain object.
|
|
28
|
-
*
|
|
29
|
-
* @param args - The arguments.
|
|
30
|
-
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
31
|
-
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
32
|
-
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
33
|
-
* via `node-fetch`).
|
|
34
|
-
* @param args.btoa - A function that can be used to convert a binary string
|
|
35
|
-
* into base-64. Used to encode authorization credentials.
|
|
36
|
-
* @param args.serviceConfigurations - The options for the RPC services that
|
|
37
|
-
* you want to construct. This class takes a set of configuration objects and
|
|
38
|
-
* not literal `RpcService`s to account for the possibility that we may want
|
|
39
|
-
* to send request headers to official Infura endpoints and not failovers.
|
|
40
|
-
*/
|
|
41
|
-
constructor({ fetch: givenFetch, btoa: givenBtoa, serviceConfigurations, }: {
|
|
42
|
-
fetch: typeof fetch;
|
|
43
|
-
btoa: typeof btoa;
|
|
44
|
-
serviceConfigurations: RpcServiceConfiguration[];
|
|
45
|
-
});
|
|
46
|
-
/**
|
|
47
|
-
* Listens for when any of the RPC services retry a request.
|
|
48
|
-
*
|
|
49
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
50
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
51
|
-
*/
|
|
52
|
-
onRetry(listener: Parameters<RpcService['onRetry']>[0]): {
|
|
53
|
-
dispose(): void;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Listens for when any of the RPC services retry the request too many times
|
|
57
|
-
* in a row.
|
|
58
|
-
*
|
|
59
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
60
|
-
* @returns What {@link RpcService.onBreak} returns.
|
|
61
|
-
*/
|
|
62
|
-
onBreak(listener: Parameters<RpcService['onBreak']>[0]): {
|
|
63
|
-
dispose(): void;
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Listens for when any of the RPC services send a slow request.
|
|
67
|
-
*
|
|
68
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
69
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
70
|
-
*/
|
|
71
|
-
onDegraded(listener: Parameters<RpcService['onDegraded']>[0]): {
|
|
72
|
-
dispose(): void;
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* Makes a request to the first RPC service in the chain. If this service is
|
|
76
|
-
* down, then the request is forwarded to the next service in the chain, etc.
|
|
77
|
-
*
|
|
78
|
-
* This overload is specifically designed for `eth_getBlockByNumber`, which
|
|
79
|
-
* can return a `result` of `null` despite an expected `Result` being
|
|
80
|
-
* provided.
|
|
81
|
-
*
|
|
82
|
-
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
|
|
83
|
-
* @param fetchOptions - An options bag for {@link fetch} which further
|
|
84
|
-
* specifies the request.
|
|
85
|
-
* @returns The decoded JSON-RPC response from the endpoint.
|
|
86
|
-
* @throws A "method not found" error if the response status is 405.
|
|
87
|
-
* @throws A rate limiting error if the response HTTP status is 429.
|
|
88
|
-
* @throws A timeout error if the response HTTP status is 503 or 504.
|
|
89
|
-
* @throws A generic error if the response HTTP status is not 2xx but also not
|
|
90
|
-
* 405, 429, 503, or 504.
|
|
91
|
-
*/
|
|
92
|
-
request<Params extends JsonRpcParams, Result extends Json>(jsonRpcRequest: JsonRpcRequest<Params> & {
|
|
93
|
-
method: 'eth_getBlockByNumber';
|
|
94
|
-
}, fetchOptions?: FetchOptions): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;
|
|
95
|
-
/**
|
|
96
|
-
* Makes a request to the first RPC service in the chain. If this service is
|
|
97
|
-
* down, then the request is forwarded to the next service in the chain, etc.
|
|
98
|
-
*
|
|
99
|
-
* This overload is designed for all RPC methods except for
|
|
100
|
-
* `eth_getBlockByNumber`, which are expected to return a `result` of the
|
|
101
|
-
* expected `Result`.
|
|
102
|
-
*
|
|
103
|
-
* @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.
|
|
104
|
-
* @param fetchOptions - An options bag for {@link fetch} which further
|
|
105
|
-
* specifies the request.
|
|
106
|
-
* @returns The decoded JSON-RPC response from the endpoint.
|
|
107
|
-
* @throws A "method not found" error if the response status is 405.
|
|
108
|
-
* @throws A rate limiting error if the response HTTP status is 429.
|
|
109
|
-
* @throws A timeout error if the response HTTP status is 503 or 504.
|
|
110
|
-
* @throws A generic error if the response HTTP status is not 2xx but also not
|
|
111
|
-
* 405, 429, 503, or 504.
|
|
112
|
-
*/
|
|
113
|
-
request<Params extends JsonRpcParams, Result extends Json>(jsonRpcRequest: JsonRpcRequest<Params>, fetchOptions?: FetchOptions): Promise<JsonRpcResponse<Result>>;
|
|
114
|
-
}
|
|
115
|
-
export {};
|
|
116
|
-
//# sourceMappingURL=rpc-service-chain.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-chain.d.mts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-chain.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,kBAAkB,EAAE,mCAA+B;AACjE,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAE7C;;;GAGG;AACH,KAAK,uBAAuB,GAAG;IAC7B;;OAEG;IACH,WAAW,EAAE,GAAG,GAAG,MAAM,CAAC;IAC1B;;OAEG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC;AAEF;;;;GAIG;AACH,qBAAa,eAAgB,YAAW,kBAAkB;;IAGxD;;;;;;;;;;;;;;OAcG;gBACS,EACV,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,EACf,qBAAqB,GACtB,EAAE;QACD,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,IAAI,EAAE,OAAO,IAAI,CAAC;QAClB,qBAAqB,EAAE,uBAAuB,EAAE,CAAC;KAClD;IAQD;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;;;IAYtD;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;;;IAYtD;;;;;OAKG;IACH,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;;;IAY5D;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,GAAG;QAAE,MAAM,EAAE,sBAAsB,CAAA;KAAE,EAC3E,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;IAE3D;;;;;;;;;;;;;;;;;OAiBG;IACG,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EAC7D,cAAc,EAAE,cAAc,CAAC,MAAM,CAAC,EACtC,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;CA4CpC"}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
-
};
|
|
12
|
-
var _RpcServiceChain_instances, _RpcServiceChain_services, _RpcServiceChain_buildRpcServiceChain;
|
|
13
|
-
import { RpcService } from "./rpc-service.mjs";
|
|
14
|
-
/**
|
|
15
|
-
* This class constructs a chain of RpcService objects which represent a
|
|
16
|
-
* particular network. The first object in the chain is intended to be the primary
|
|
17
|
-
* way of reaching the network and the remaining objects are used as failovers.
|
|
18
|
-
*/
|
|
19
|
-
export class RpcServiceChain {
|
|
20
|
-
/**
|
|
21
|
-
* Constructs a new RpcServiceChain object.
|
|
22
|
-
*
|
|
23
|
-
* @param args - The arguments.
|
|
24
|
-
* @param args.fetch - A function that can be used to make an HTTP request.
|
|
25
|
-
* If your JavaScript environment supports `fetch` natively, you'll probably
|
|
26
|
-
* want to pass that; otherwise you can pass an equivalent (such as `fetch`
|
|
27
|
-
* via `node-fetch`).
|
|
28
|
-
* @param args.btoa - A function that can be used to convert a binary string
|
|
29
|
-
* into base-64. Used to encode authorization credentials.
|
|
30
|
-
* @param args.serviceConfigurations - The options for the RPC services that
|
|
31
|
-
* you want to construct. This class takes a set of configuration objects and
|
|
32
|
-
* not literal `RpcService`s to account for the possibility that we may want
|
|
33
|
-
* to send request headers to official Infura endpoints and not failovers.
|
|
34
|
-
*/
|
|
35
|
-
constructor({ fetch: givenFetch, btoa: givenBtoa, serviceConfigurations, }) {
|
|
36
|
-
_RpcServiceChain_instances.add(this);
|
|
37
|
-
_RpcServiceChain_services.set(this, void 0);
|
|
38
|
-
__classPrivateFieldSet(this, _RpcServiceChain_services, __classPrivateFieldGet(this, _RpcServiceChain_instances, "m", _RpcServiceChain_buildRpcServiceChain).call(this, {
|
|
39
|
-
serviceConfigurations,
|
|
40
|
-
fetch: givenFetch,
|
|
41
|
-
btoa: givenBtoa,
|
|
42
|
-
}), "f");
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Listens for when any of the RPC services retry a request.
|
|
46
|
-
*
|
|
47
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
48
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
49
|
-
*/
|
|
50
|
-
onRetry(listener) {
|
|
51
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onRetry(listener));
|
|
52
|
-
return {
|
|
53
|
-
dispose() {
|
|
54
|
-
disposables.forEach((disposable) => disposable.dispose());
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Listens for when any of the RPC services retry the request too many times
|
|
60
|
-
* in a row.
|
|
61
|
-
*
|
|
62
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
63
|
-
* @returns What {@link RpcService.onBreak} returns.
|
|
64
|
-
*/
|
|
65
|
-
onBreak(listener) {
|
|
66
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onBreak(listener));
|
|
67
|
-
return {
|
|
68
|
-
dispose() {
|
|
69
|
-
disposables.forEach((disposable) => disposable.dispose());
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Listens for when any of the RPC services send a slow request.
|
|
75
|
-
*
|
|
76
|
-
* @param listener - The callback to be called when the retry occurs.
|
|
77
|
-
* @returns What {@link RpcService.onRetry} returns.
|
|
78
|
-
*/
|
|
79
|
-
onDegraded(listener) {
|
|
80
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onDegraded(listener));
|
|
81
|
-
return {
|
|
82
|
-
dispose() {
|
|
83
|
-
disposables.forEach((disposable) => disposable.dispose());
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
async request(jsonRpcRequest, fetchOptions = {}) {
|
|
88
|
-
return __classPrivateFieldGet(this, _RpcServiceChain_services, "f")[0].request(jsonRpcRequest, fetchOptions);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
_RpcServiceChain_services = new WeakMap(), _RpcServiceChain_instances = new WeakSet(), _RpcServiceChain_buildRpcServiceChain = function _RpcServiceChain_buildRpcServiceChain({ serviceConfigurations, fetch: givenFetch, btoa: givenBtoa, }) {
|
|
92
|
-
return [...serviceConfigurations]
|
|
93
|
-
.reverse()
|
|
94
|
-
.reduce((workingServices, serviceConfiguration, index) => {
|
|
95
|
-
const failoverService = index > 0 ? workingServices[0] : undefined;
|
|
96
|
-
const service = new RpcService({
|
|
97
|
-
fetch: givenFetch,
|
|
98
|
-
btoa: givenBtoa,
|
|
99
|
-
...serviceConfiguration,
|
|
100
|
-
failoverService,
|
|
101
|
-
});
|
|
102
|
-
return [service, ...workingServices];
|
|
103
|
-
}, []);
|
|
104
|
-
};
|
|
105
|
-
//# sourceMappingURL=rpc-service-chain.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-chain.mjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-chain.ts"],"names":[],"mappings":";;;;;;;;;;;;AAQA,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAkB3C;;;;GAIG;AACH,MAAM,OAAO,eAAe;IAG1B;;;;;;;;;;;;;;OAcG;IACH,YAAY,EACV,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,EACf,qBAAqB,GAKtB;;QAzBQ,4CAAwB;QA0B/B,uBAAA,IAAI,6BAAa,uBAAA,IAAI,yEAAsB,MAA1B,IAAI,EAAuB;YAC1C,qBAAqB;YACrB,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,SAAS;SAChB,CAAC,MAAA,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,QAA8C;QACpD,MAAM,WAAW,GAAG,uBAAA,IAAI,iCAAU,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAC1B,CAAC;QAEF,OAAO;YACL,OAAO;gBACL,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,OAAO,CAAC,QAA8C;QACpD,MAAM,WAAW,GAAG,uBAAA,IAAI,iCAAU,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAC1B,CAAC;QAEF,OAAO;YACL,OAAO;gBACL,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,UAAU,CAAC,QAAiD;QAC1D,MAAM,WAAW,GAAG,uBAAA,IAAI,iCAAU,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CACjD,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAC7B,CAAC;QAEF,OAAO;YACL,OAAO;gBACL,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;IAgDD,KAAK,CAAC,OAAO,CACX,cAAsC,EACtC,eAA6B,EAAE;QAE/B,OAAO,uBAAA,IAAI,iCAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;IACjE,CAAC;CAqCF;8KAtBuB,EACpB,qBAAqB,EACrB,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,SAAS,GAKhB;IACC,OAAO,CAAC,GAAG,qBAAqB,CAAC;SAC9B,OAAO,EAAE;SACT,MAAM,CAAC,CAAC,eAA6B,EAAE,oBAAoB,EAAE,KAAK,EAAE,EAAE;QACrE,MAAM,eAAe,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACnE,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC;YAC7B,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,SAAS;YACf,GAAG,oBAAoB;YACvB,eAAe;SAChB,CAAC,CAAC;QACH,OAAO,CAAC,OAAO,EAAE,GAAG,eAAe,CAAC,CAAC;IACvC,CAAC,EAAE,EAAE,CAAC,CAAC;AACX,CAAC","sourcesContent":["import type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { AbstractRpcService } from './abstract-rpc-service';\nimport { RpcService } from './rpc-service';\nimport type { FetchOptions } from './shared';\n\n/**\n * The subset of options accepted by the RpcServiceChain constructor which\n * represent a single endpoint.\n */\ntype RpcServiceConfiguration = {\n /**\n * The URL of the endpoint.\n */\n endpointUrl: URL | string;\n /**\n * The options to pass to `fetch` when making the request to the endpoint.\n */\n fetchOptions?: FetchOptions;\n};\n\n/**\n * This class constructs a chain of RpcService objects which represent a\n * particular network. The first object in the chain is intended to be the primary\n * way of reaching the network and the remaining objects are used as failovers.\n */\nexport class RpcServiceChain implements AbstractRpcService {\n readonly #services: RpcService[];\n\n /**\n * Constructs a new RpcServiceChain object.\n *\n * @param args - The arguments.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * If your JavaScript environment supports `fetch` natively, you'll probably\n * want to pass that; otherwise you can pass an equivalent (such as `fetch`\n * via `node-fetch`).\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64. Used to encode authorization credentials.\n * @param args.serviceConfigurations - The options for the RPC services that\n * you want to construct. This class takes a set of configuration objects and\n * not literal `RpcService`s to account for the possibility that we may want\n * to send request headers to official Infura endpoints and not failovers.\n */\n constructor({\n fetch: givenFetch,\n btoa: givenBtoa,\n serviceConfigurations,\n }: {\n fetch: typeof fetch;\n btoa: typeof btoa;\n serviceConfigurations: RpcServiceConfiguration[];\n }) {\n this.#services = this.#buildRpcServiceChain({\n serviceConfigurations,\n fetch: givenFetch,\n btoa: givenBtoa,\n });\n }\n\n /**\n * Listens for when any of the RPC services retry a request.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link RpcService.onRetry} returns.\n */\n onRetry(listener: Parameters<RpcService['onRetry']>[0]) {\n const disposables = this.#services.map((service) =>\n service.onRetry(listener),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Listens for when any of the RPC services retry the request too many times\n * in a row.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link RpcService.onBreak} returns.\n */\n onBreak(listener: Parameters<RpcService['onBreak']>[0]) {\n const disposables = this.#services.map((service) =>\n service.onBreak(listener),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Listens for when any of the RPC services send a slow request.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link RpcService.onRetry} returns.\n */\n onDegraded(listener: Parameters<RpcService['onDegraded']>[0]) {\n const disposables = this.#services.map((service) =>\n service.onDegraded(listener),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Makes a request to the first RPC service in the chain. If this service is\n * down, then the request is forwarded to the next service in the chain, etc.\n *\n * This overload is specifically designed for `eth_getBlockByNumber`, which\n * can return a `result` of `null` despite an expected `Result` being\n * provided.\n *\n * @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.\n * @param fetchOptions - An options bag for {@link fetch} which further\n * specifies the request.\n * @returns The decoded JSON-RPC response from the endpoint.\n * @throws A \"method not found\" error if the response status is 405.\n * @throws A rate limiting error if the response HTTP status is 429.\n * @throws A timeout error if the response HTTP status is 503 or 504.\n * @throws A generic error if the response HTTP status is not 2xx but also not\n * 405, 429, 503, or 504.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: JsonRpcRequest<Params> & { method: 'eth_getBlockByNumber' },\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;\n\n /**\n * Makes a request to the first RPC service in the chain. If this service is\n * down, then the request is forwarded to the next service in the chain, etc.\n *\n * This overload is designed for all RPC methods except for\n * `eth_getBlockByNumber`, which are expected to return a `result` of the\n * expected `Result`.\n *\n * @param jsonRpcRequest - The JSON-RPC request to send to the endpoint.\n * @param fetchOptions - An options bag for {@link fetch} which further\n * specifies the request.\n * @returns The decoded JSON-RPC response from the endpoint.\n * @throws A \"method not found\" error if the response status is 405.\n * @throws A rate limiting error if the response HTTP status is 429.\n * @throws A timeout error if the response HTTP status is 503 or 504.\n * @throws A generic error if the response HTTP status is not 2xx but also not\n * 405, 429, 503, or 504.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: JsonRpcRequest<Params>,\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result>>;\n\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: JsonRpcRequest<Params>,\n fetchOptions: FetchOptions = {},\n ): Promise<JsonRpcResponse<Result | null>> {\n return this.#services[0].request(jsonRpcRequest, fetchOptions);\n }\n\n /**\n * Constructs the chain of RPC services. The second RPC service is\n * configured as the failover for the first, the third service is\n * configured as the failover for the second, etc.\n *\n * @param args - The arguments.\n * @param args.serviceConfigurations - The options for the RPC services that\n * you want to construct.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * @param args.btoa - A function that can be used to convert a binary string\n * into base-64. Used to encode authorization credentials.\n * @returns The constructed chain of RPC services.\n */\n #buildRpcServiceChain({\n serviceConfigurations,\n fetch: givenFetch,\n btoa: givenBtoa,\n }: {\n serviceConfigurations: RpcServiceConfiguration[];\n fetch: typeof fetch;\n btoa: typeof btoa;\n }): RpcService[] {\n return [...serviceConfigurations]\n .reverse()\n .reduce((workingServices: RpcService[], serviceConfiguration, index) => {\n const failoverService = index > 0 ? workingServices[0] : undefined;\n const service = new RpcService({\n fetch: givenFetch,\n btoa: givenBtoa,\n ...serviceConfiguration,\n failoverService,\n });\n return [service, ...workingServices];\n }, []);\n }\n}\n"]}
|