@metamask-previews/network-controller 26.0.0-preview-e3eb8eca → 26.0.0-preview-e0caebb
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/CHANGELOG.md +0 -19
- package/dist/NetworkController.cjs +10 -37
- package/dist/NetworkController.cjs.map +1 -1
- package/dist/NetworkController.d.cts +11 -146
- package/dist/NetworkController.d.cts.map +1 -1
- package/dist/NetworkController.d.mts +11 -146
- package/dist/NetworkController.d.mts.map +1 -1
- package/dist/NetworkController.mjs +10 -37
- package/dist/NetworkController.mjs.map +1 -1
- package/dist/constants.cjs +10 -18
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +10 -18
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +10 -18
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +10 -18
- package/dist/constants.mjs.map +1 -1
- package/dist/create-auto-managed-network-client.cjs +1 -4
- package/dist/create-auto-managed-network-client.cjs.map +1 -1
- package/dist/create-auto-managed-network-client.d.cts +2 -5
- package/dist/create-auto-managed-network-client.d.cts.map +1 -1
- package/dist/create-auto-managed-network-client.d.mts +2 -5
- package/dist/create-auto-managed-network-client.d.mts.map +1 -1
- package/dist/create-auto-managed-network-client.mjs +1 -4
- package/dist/create-auto-managed-network-client.mjs.map +1 -1
- package/dist/create-network-client.cjs +42 -115
- package/dist/create-network-client.cjs.map +1 -1
- package/dist/create-network-client.d.cts +2 -5
- package/dist/create-network-client.d.cts.map +1 -1
- package/dist/create-network-client.d.mts +2 -5
- package/dist/create-network-client.d.mts.map +1 -1
- package/dist/create-network-client.mjs +42 -115
- package/dist/create-network-client.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/rpc-service/rpc-service-chain.cjs +39 -255
- package/dist/rpc-service/rpc-service-chain.cjs.map +1 -1
- package/dist/rpc-service/rpc-service-chain.d.cts +25 -117
- package/dist/rpc-service/rpc-service-chain.d.cts.map +1 -1
- package/dist/rpc-service/rpc-service-chain.d.mts +25 -117
- package/dist/rpc-service/rpc-service-chain.d.mts.map +1 -1
- package/dist/rpc-service/rpc-service-chain.mjs +39 -255
- package/dist/rpc-service/rpc-service-chain.mjs.map +1 -1
- package/dist/rpc-service/rpc-service-requestable.cjs.map +1 -1
- package/dist/rpc-service/rpc-service-requestable.d.cts +5 -15
- package/dist/rpc-service/rpc-service-requestable.d.cts.map +1 -1
- package/dist/rpc-service/rpc-service-requestable.d.mts +5 -15
- package/dist/rpc-service/rpc-service-requestable.d.mts.map +1 -1
- package/dist/rpc-service/rpc-service-requestable.mjs.map +1 -1
- package/dist/rpc-service/rpc-service.cjs +33 -63
- package/dist/rpc-service/rpc-service.cjs.map +1 -1
- package/dist/rpc-service/rpc-service.d.cts +24 -35
- package/dist/rpc-service/rpc-service.d.cts.map +1 -1
- package/dist/rpc-service/rpc-service.d.mts +24 -35
- package/dist/rpc-service/rpc-service.d.mts.map +1 -1
- package/dist/rpc-service/rpc-service.mjs +34 -64
- package/dist/rpc-service/rpc-service.mjs.map +1 -1
- package/dist/rpc-service/shared.cjs.map +1 -1
- package/dist/rpc-service/shared.d.cts +3 -30
- package/dist/rpc-service/shared.d.cts.map +1 -1
- package/dist/rpc-service/shared.d.mts +3 -30
- package/dist/rpc-service/shared.d.mts.map +1 -1
- package/dist/rpc-service/shared.mjs.map +1 -1
- package/package.json +1 -2
|
@@ -1,33 +1,21 @@
|
|
|
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
|
+
};
|
|
1
6
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
7
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
8
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
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");
|
|
5
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
11
|
};
|
|
7
|
-
var
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
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");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _RpcServiceChain_onAvailableEventEmitter, _RpcServiceChain_onBreakEventEmitter, _RpcServiceChain_onDegradedEventEmitter, _RpcServiceChain_primaryService, _RpcServiceChain_services, _RpcServiceChain_status;
|
|
13
|
-
import { CircuitState, CockatielEventEmitter } from "@metamask/controller-utils";
|
|
12
|
+
var _RpcServiceChain_instances, _RpcServiceChain_services, _RpcServiceChain_buildRpcServiceChain;
|
|
14
13
|
import { RpcService } from "./rpc-service.mjs";
|
|
15
|
-
import { projectLogger, createModuleLogger } from "../logger.mjs";
|
|
16
|
-
const log = createModuleLogger(projectLogger, 'RpcServiceChain');
|
|
17
14
|
/**
|
|
18
|
-
*
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
Degraded: 'degraded',
|
|
23
|
-
Unknown: 'unknown',
|
|
24
|
-
Unavailable: 'unavailable',
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* This class constructs and manages requests to a chain of RpcService objects
|
|
28
|
-
* which represent RPC endpoints with which to access a particular network. The
|
|
29
|
-
* first service in the chain is intended to be the primary way of hitting the
|
|
30
|
-
* network and the remaining services are used as failovers.
|
|
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
|
|
17
|
+
* primary way of reaching the network and the remaining objects are used as
|
|
18
|
+
* failovers.
|
|
31
19
|
*/
|
|
32
20
|
export class RpcServiceChain {
|
|
33
21
|
/**
|
|
@@ -38,77 +26,18 @@ export class RpcServiceChain {
|
|
|
38
26
|
* {@link RpcServiceOptions}.
|
|
39
27
|
*/
|
|
40
28
|
constructor(rpcServiceConfigurations) {
|
|
41
|
-
|
|
42
|
-
* The event emitter for the `onBreak` event.
|
|
43
|
-
*/
|
|
44
|
-
_RpcServiceChain_onAvailableEventEmitter.set(this, void 0);
|
|
45
|
-
/**
|
|
46
|
-
* The event emitter for the `onBreak` event.
|
|
47
|
-
*/
|
|
48
|
-
_RpcServiceChain_onBreakEventEmitter.set(this, void 0);
|
|
49
|
-
/**
|
|
50
|
-
* The event emitter for the `onBreak` event.
|
|
51
|
-
*/
|
|
52
|
-
_RpcServiceChain_onDegradedEventEmitter.set(this, void 0);
|
|
53
|
-
/**
|
|
54
|
-
* The first RPC service that requests will be sent to.
|
|
55
|
-
*/
|
|
56
|
-
_RpcServiceChain_primaryService.set(this, void 0);
|
|
57
|
-
/**
|
|
58
|
-
* The RPC services in the chain.
|
|
59
|
-
*/
|
|
29
|
+
_RpcServiceChain_instances.add(this);
|
|
60
30
|
_RpcServiceChain_services.set(this, void 0);
|
|
61
|
-
|
|
62
|
-
* The status of the RPC service chain.
|
|
63
|
-
*/
|
|
64
|
-
_RpcServiceChain_status.set(this, void 0);
|
|
65
|
-
__classPrivateFieldSet(this, _RpcServiceChain_services, rpcServiceConfigurations.map((rpcServiceConfiguration) => new RpcService(rpcServiceConfiguration)), "f");
|
|
66
|
-
__classPrivateFieldSet(this, _RpcServiceChain_primaryService, __classPrivateFieldGet(this, _RpcServiceChain_services, "f")[0], "f");
|
|
67
|
-
__classPrivateFieldSet(this, _RpcServiceChain_status, STATUSES.Unknown, "f");
|
|
68
|
-
__classPrivateFieldSet(this, _RpcServiceChain_onBreakEventEmitter, new CockatielEventEmitter(), "f");
|
|
69
|
-
__classPrivateFieldSet(this, _RpcServiceChain_onDegradedEventEmitter, new CockatielEventEmitter(), "f");
|
|
70
|
-
for (const service of __classPrivateFieldGet(this, _RpcServiceChain_services, "f")) {
|
|
71
|
-
service.onDegraded((data) => {
|
|
72
|
-
if (__classPrivateFieldGet(this, _RpcServiceChain_status, "f") !== STATUSES.Degraded) {
|
|
73
|
-
log('Updating status to "degraded"', data);
|
|
74
|
-
__classPrivateFieldSet(this, _RpcServiceChain_status, STATUSES.Degraded, "f");
|
|
75
|
-
__classPrivateFieldGet(this, _RpcServiceChain_onDegradedEventEmitter, "f").emit({
|
|
76
|
-
...data,
|
|
77
|
-
primaryEndpointUrl: __classPrivateFieldGet(this, _RpcServiceChain_primaryService, "f").endpointUrl.toString(),
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
__classPrivateFieldSet(this, _RpcServiceChain_onAvailableEventEmitter, new CockatielEventEmitter(), "f");
|
|
83
|
-
for (const service of __classPrivateFieldGet(this, _RpcServiceChain_services, "f")) {
|
|
84
|
-
service.onAvailable((data) => {
|
|
85
|
-
if (__classPrivateFieldGet(this, _RpcServiceChain_status, "f") !== STATUSES.Available) {
|
|
86
|
-
log('Updating status to "available"', data);
|
|
87
|
-
__classPrivateFieldSet(this, _RpcServiceChain_status, STATUSES.Available, "f");
|
|
88
|
-
__classPrivateFieldGet(this, _RpcServiceChain_onAvailableEventEmitter, "f").emit({
|
|
89
|
-
...data,
|
|
90
|
-
primaryEndpointUrl: __classPrivateFieldGet(this, _RpcServiceChain_primaryService, "f").endpointUrl.toString(),
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
31
|
+
__classPrivateFieldSet(this, _RpcServiceChain_services, __classPrivateFieldGet(this, _RpcServiceChain_instances, "m", _RpcServiceChain_buildRpcServiceChain).call(this, rpcServiceConfigurations), "f");
|
|
95
32
|
}
|
|
96
33
|
/**
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* This is mainly useful for tests.
|
|
34
|
+
* Listens for when any of the RPC services retry a request.
|
|
100
35
|
*
|
|
101
|
-
* @param listener - The callback to be called.
|
|
102
|
-
* @returns
|
|
103
|
-
* the event listener.
|
|
36
|
+
* @param listener - The callback to be called when the retry occurs.
|
|
37
|
+
* @returns What {@link RpcService.onRetry} returns.
|
|
104
38
|
*/
|
|
105
|
-
|
|
106
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onRetry(
|
|
107
|
-
listener({
|
|
108
|
-
...data,
|
|
109
|
-
primaryEndpointUrl: __classPrivateFieldGet(this, _RpcServiceChain_primaryService, "f").endpointUrl.toString(),
|
|
110
|
-
});
|
|
111
|
-
}));
|
|
39
|
+
onRetry(listener) {
|
|
40
|
+
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onRetry(listener));
|
|
112
41
|
return {
|
|
113
42
|
dispose() {
|
|
114
43
|
disposables.forEach((disposable) => disposable.dispose());
|
|
@@ -116,39 +45,14 @@ export class RpcServiceChain {
|
|
|
116
45
|
};
|
|
117
46
|
}
|
|
118
47
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* RPC services in the chain, and all services' underlying circuits have
|
|
122
|
-
* broken.
|
|
48
|
+
* Listens for when any of the RPC services retry the request too many times
|
|
49
|
+
* in a row.
|
|
123
50
|
*
|
|
124
|
-
* The callback
|
|
125
|
-
*
|
|
126
|
-
* granularity.
|
|
127
|
-
*
|
|
128
|
-
* @param listener - The callback to be called.
|
|
129
|
-
* @returns An object with a `dispose` method which can be used to unregister
|
|
130
|
-
* the callback.
|
|
51
|
+
* @param listener - The callback to be called when the retry occurs.
|
|
52
|
+
* @returns What {@link RpcService.onBreak} returns.
|
|
131
53
|
*/
|
|
132
54
|
onBreak(listener) {
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Calls the provided callback each time when, for *any* of the RPC services
|
|
137
|
-
* in this chain, the maximum number of failed consecutive attempts to receive
|
|
138
|
-
* a 2xx response has been reached and the underlying circuit has broken. A
|
|
139
|
-
* more granular version of `onBreak`.
|
|
140
|
-
*
|
|
141
|
-
* @param listener - The callback to be called.
|
|
142
|
-
* @returns An object with a `dispose` method which can be used to unregister
|
|
143
|
-
* the callback.
|
|
144
|
-
*/
|
|
145
|
-
onServiceBreak(listener) {
|
|
146
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onBreak((data) => {
|
|
147
|
-
listener({
|
|
148
|
-
...data,
|
|
149
|
-
primaryEndpointUrl: __classPrivateFieldGet(this, _RpcServiceChain_primaryService, "f").endpointUrl.toString(),
|
|
150
|
-
});
|
|
151
|
-
}));
|
|
55
|
+
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onBreak(listener));
|
|
152
56
|
return {
|
|
153
57
|
dispose() {
|
|
154
58
|
disposables.forEach((disposable) => disposable.dispose());
|
|
@@ -156,153 +60,33 @@ export class RpcServiceChain {
|
|
|
156
60
|
};
|
|
157
61
|
}
|
|
158
62
|
/**
|
|
159
|
-
*
|
|
160
|
-
* all requests to RPC services in this chain have responded successfully in a
|
|
161
|
-
* timely fashion, and then one of the two conditions apply:
|
|
162
|
-
*
|
|
163
|
-
* 1. When a retriable error is encountered making a request to an RPC
|
|
164
|
-
* service, and the request is retried until a set maximum is reached.
|
|
165
|
-
* 2. When a RPC service responds successfully, but the request takes longer
|
|
166
|
-
* than a set number of seconds to complete.
|
|
167
|
-
*
|
|
168
|
-
* Note that the callback will be called even if there are local connectivity
|
|
169
|
-
* issues which prevent requests from being initiated. This is intentional.
|
|
63
|
+
* Listens for when any of the RPC services send a slow request.
|
|
170
64
|
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* will not be called if a failover service falls into a degraded state, then
|
|
174
|
-
* the primary comes back online, but it is slow). Use `onServiceDegraded` if
|
|
175
|
-
* you'd like a lower level of granularity.
|
|
176
|
-
*
|
|
177
|
-
* @param listener - The callback to be called.
|
|
178
|
-
* @returns An object with a `dispose` method which can be used to unregister
|
|
179
|
-
* the callback.
|
|
65
|
+
* @param listener - The callback to be called when the retry occurs.
|
|
66
|
+
* @returns What {@link RpcService.onRetry} returns.
|
|
180
67
|
*/
|
|
181
68
|
onDegraded(listener) {
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Calls the provided callback each time one of the two conditions apply:
|
|
186
|
-
*
|
|
187
|
-
* 1. When a retriable error is encountered making a request to an RPC
|
|
188
|
-
* service, and the request is retried until a set maximum is reached.
|
|
189
|
-
* 2. When a RPC service responds successfully, but the request takes longer
|
|
190
|
-
* than a set number of seconds to complete.
|
|
191
|
-
*
|
|
192
|
-
* Note that the callback will be called even if there are local connectivity
|
|
193
|
-
* issues which prevent requests from being initiated. This is intentional.
|
|
194
|
-
*
|
|
195
|
-
* This is a more granular version of `onDegraded`. The callback will be
|
|
196
|
-
* called for each slow request to an RPC service. It may also be called again
|
|
197
|
-
* if a failover service falls into a degraded state, then the primary comes
|
|
198
|
-
* back online, but it is slow.
|
|
199
|
-
*
|
|
200
|
-
* @param listener - The callback to be called.
|
|
201
|
-
* @returns An object with a `dispose` method which can be used to unregister
|
|
202
|
-
* the callback.
|
|
203
|
-
*/
|
|
204
|
-
onServiceDegraded(listener) {
|
|
205
|
-
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onDegraded((data) => {
|
|
206
|
-
listener({
|
|
207
|
-
...data,
|
|
208
|
-
primaryEndpointUrl: __classPrivateFieldGet(this, _RpcServiceChain_primaryService, "f").endpointUrl.toString(),
|
|
209
|
-
});
|
|
210
|
-
}));
|
|
69
|
+
const disposables = __classPrivateFieldGet(this, _RpcServiceChain_services, "f").map((service) => service.onDegraded(listener));
|
|
211
70
|
return {
|
|
212
71
|
dispose() {
|
|
213
72
|
disposables.forEach((disposable) => disposable.dispose());
|
|
214
73
|
},
|
|
215
74
|
};
|
|
216
75
|
}
|
|
217
|
-
/**
|
|
218
|
-
* Calls the provided callback in one of the following two conditions:
|
|
219
|
-
*
|
|
220
|
-
* 1. The first time that a 2xx request is made to any of the RPC services in
|
|
221
|
-
* this chain.
|
|
222
|
-
* 2. When requests to any the failover RPC services in this chain were
|
|
223
|
-
* failing such that they were degraded or their underyling circuits broke,
|
|
224
|
-
* but the first request to the primary succeeds again.
|
|
225
|
-
*
|
|
226
|
-
* Note this callback will only be called if the RPC service chain as a whole
|
|
227
|
-
* is in an "available" state.
|
|
228
|
-
*
|
|
229
|
-
* @param listener - The callback to be called.
|
|
230
|
-
* @returns An object with a `dispose` method which can be used to unregister
|
|
231
|
-
* the callback.
|
|
232
|
-
*/
|
|
233
|
-
onAvailable(listener) {
|
|
234
|
-
return __classPrivateFieldGet(this, _RpcServiceChain_onAvailableEventEmitter, "f").addListener(listener);
|
|
235
|
-
}
|
|
236
76
|
async request(jsonRpcRequest, fetchOptions = {}) {
|
|
237
|
-
|
|
238
|
-
// need arises. This is a bit confusing, so keep reading for more on how
|
|
239
|
-
// this works.
|
|
240
|
-
let availableServiceIndex;
|
|
241
|
-
let response;
|
|
242
|
-
for (const [i, service] of __classPrivateFieldGet(this, _RpcServiceChain_services, "f").entries()) {
|
|
243
|
-
log(`Trying service #${i + 1}...`);
|
|
244
|
-
const previousCircuitState = service.getCircuitState();
|
|
245
|
-
try {
|
|
246
|
-
// Try making the request through the service.
|
|
247
|
-
response = await service.request(jsonRpcRequest, fetchOptions);
|
|
248
|
-
log('Service successfully received request.');
|
|
249
|
-
availableServiceIndex = i;
|
|
250
|
-
break;
|
|
251
|
-
}
|
|
252
|
-
catch (error) {
|
|
253
|
-
// Oops, that didn't work.
|
|
254
|
-
// Capture this error so that we can handle it later.
|
|
255
|
-
const lastFailureReason = service.getLastInnerFailureReason();
|
|
256
|
-
const isCircuitOpen = service.getCircuitState() === CircuitState.Open;
|
|
257
|
-
log('Service failed!', error, lastFailureReason);
|
|
258
|
-
log('Circuit state', service.getCircuitState(), 'Previous circuit state', previousCircuitState, 'state', __classPrivateFieldGet(this, _RpcServiceChain_status, "f"));
|
|
259
|
-
if (isCircuitOpen) {
|
|
260
|
-
if (i < __classPrivateFieldGet(this, _RpcServiceChain_services, "f").length - 1) {
|
|
261
|
-
log("This service's circuit is open. Proceeding to next service...");
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
if (previousCircuitState !== CircuitState.Open &&
|
|
265
|
-
__classPrivateFieldGet(this, _RpcServiceChain_status, "f") !== STATUSES.Unavailable &&
|
|
266
|
-
lastFailureReason !== undefined) {
|
|
267
|
-
// If the service's circuit just broke and it's the last one in the
|
|
268
|
-
// chain, then trigger the onBreak event. (But if for some reason we
|
|
269
|
-
// have already done this, then don't do it.)
|
|
270
|
-
log('This service\'s circuit just opened and it is the last service. Updating status to "unavailable" and triggering onBreak.');
|
|
271
|
-
__classPrivateFieldSet(this, _RpcServiceChain_status, STATUSES.Unavailable, "f");
|
|
272
|
-
__classPrivateFieldGet(this, _RpcServiceChain_onBreakEventEmitter, "f").emit({
|
|
273
|
-
...lastFailureReason,
|
|
274
|
-
primaryEndpointUrl: __classPrivateFieldGet(this, _RpcServiceChain_primaryService, "f").endpointUrl.toString(),
|
|
275
|
-
endpointUrl: service.endpointUrl.toString(),
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
// The service failed, and we throw whatever the error is. The calling
|
|
280
|
-
// code can try again if it so desires.
|
|
281
|
-
log(`${isCircuitOpen ? "This service's circuit is open, but for some reason it wasn't handled above. " : "This service's circuit is closed. "}Re-throwing error.`);
|
|
282
|
-
throw error;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
if (response) {
|
|
286
|
-
// If one of the services returned a successful response, assume that we
|
|
287
|
-
// won't need to hit any of the failover services following it and reset
|
|
288
|
-
// all of the policies of the following services. In particularly this
|
|
289
|
-
// means that if any of the failover services' circuits was open when
|
|
290
|
-
// requests were diverted back to the available service, that circuit will
|
|
291
|
-
// now be reset so that if we start hitting it again we don't get a
|
|
292
|
-
// "circuit broken" error.
|
|
293
|
-
if (availableServiceIndex !== undefined) {
|
|
294
|
-
for (const [i, service] of [...__classPrivateFieldGet(this, _RpcServiceChain_services, "f").entries()].slice(availableServiceIndex + 1)) {
|
|
295
|
-
log(`Resetting policy for service #${i + 1}.`);
|
|
296
|
-
service.resetPolicy();
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
return response;
|
|
300
|
-
}
|
|
301
|
-
// The only way we can end up here is if there are no services to loop over.
|
|
302
|
-
// That is not possible due to the types on the constructor, but TypeScript
|
|
303
|
-
// doesn't know this, so we have to appease it.
|
|
304
|
-
throw new Error('Nothing to return');
|
|
77
|
+
return __classPrivateFieldGet(this, _RpcServiceChain_services, "f")[0].request(jsonRpcRequest, fetchOptions);
|
|
305
78
|
}
|
|
306
79
|
}
|
|
307
|
-
|
|
80
|
+
_RpcServiceChain_services = new WeakMap(), _RpcServiceChain_instances = new WeakSet(), _RpcServiceChain_buildRpcServiceChain = function _RpcServiceChain_buildRpcServiceChain(rpcServiceConfigurations) {
|
|
81
|
+
return [...rpcServiceConfigurations]
|
|
82
|
+
.reverse()
|
|
83
|
+
.reduce((workingServices, serviceConfiguration, index) => {
|
|
84
|
+
const failoverService = index > 0 ? workingServices[0] : undefined;
|
|
85
|
+
const service = new RpcService({
|
|
86
|
+
...serviceConfiguration,
|
|
87
|
+
failoverService,
|
|
88
|
+
});
|
|
89
|
+
return [service, ...workingServices];
|
|
90
|
+
}, []);
|
|
91
|
+
};
|
|
308
92
|
//# sourceMappingURL=rpc-service-chain.mjs.map
|
|
@@ -1 +1 @@
|
|
|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AC,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,uBAAA,IAAI,iCAAU,CAAC,OAAO,EAAE,CAAC,CAAC,KAAK,CAC5D,qBAAqB,GAAG,CAAC,CAC1B,EAAE,CAAC;oBACF,GAAG,CAAC,iCAAiC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;oBAC/C,OAAO,CAAC,WAAW,EAAE,CAAC;gBACxB,CAAC;YACH,CAAC;YAED,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,4EAA4E;QAC5E,2EAA2E;QAC3E,+CAA+C;QAC/C,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACvC,CAAC;CACF","sourcesContent":["import {\n CircuitState,\n CockatielEventEmitter,\n} from '@metamask/controller-utils';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport { RpcService } from './rpc-service';\nimport type { RpcServiceOptions } from './rpc-service';\nimport type {\n CockatielEventToEventEmitterWithData,\n CockatielEventToEventListenerWithData,\n ExtendCockatielEventData,\n ExtractCockatielEventData,\n FetchOptions,\n} from './shared';\nimport { projectLogger, createModuleLogger } from '../logger';\n\nconst log = createModuleLogger(projectLogger, 'RpcServiceChain');\n\n/**\n * Statuses that the RPC service chain can be in.\n */\nconst STATUSES = {\n Available: 'available',\n Degraded: 'degraded',\n Unknown: 'unknown',\n Unavailable: 'unavailable',\n} as const;\n\n/**\n * Statuses that the RPC service chain can be in.\n */\ntype Status = (typeof STATUSES)[keyof typeof STATUSES];\n\ntype RpcServiceConfiguration = Omit<RpcServiceOptions, 'failoverService'>;\n\n/**\n * This class constructs and manages requests to a chain of RpcService objects\n * which represent RPC endpoints with which to access a particular network. The\n * first service in the chain is intended to be the primary way of hitting the\n * network and the remaining services are used as failovers.\n */\nexport class RpcServiceChain {\n /**\n * The event emitter for the `onBreak` event.\n */\n readonly #onAvailableEventEmitter: CockatielEventToEventEmitterWithData<\n RpcService['onAvailable'],\n { primaryEndpointUrl: string }\n >;\n\n /**\n * The event emitter for the `onBreak` event.\n */\n readonly #onBreakEventEmitter: CockatielEventToEventEmitterWithData<\n RpcService['onBreak'],\n { primaryEndpointUrl: string }\n >;\n\n /**\n * The event emitter for the `onBreak` event.\n */\n readonly #onDegradedEventEmitter: CockatielEventToEventEmitterWithData<\n RpcService['onDegraded'],\n { primaryEndpointUrl: string }\n >;\n\n /**\n * The first RPC service that requests will be sent to.\n */\n readonly #primaryService: RpcService;\n\n /**\n * The RPC services in the chain.\n */\n readonly #services: RpcService[];\n\n /**\n * The status of the RPC service chain.\n */\n #status: Status;\n\n /**\n * Constructs a new RpcServiceChain object.\n *\n * @param rpcServiceConfigurations - The options for the RPC services\n * that you want to construct. Each object in this array is the same as\n * {@link RpcServiceOptions}.\n */\n constructor(\n rpcServiceConfigurations: [\n RpcServiceConfiguration,\n ...RpcServiceConfiguration[],\n ],\n ) {\n this.#services = rpcServiceConfigurations.map(\n (rpcServiceConfiguration) => new RpcService(rpcServiceConfiguration),\n );\n this.#primaryService = this.#services[0];\n\n this.#status = STATUSES.Unknown;\n this.#onBreakEventEmitter = new CockatielEventEmitter<\n ExtendCockatielEventData<\n ExtractCockatielEventData<RpcService['onBreak']>,\n { primaryEndpointUrl: string }\n >\n >();\n\n this.#onDegradedEventEmitter = new CockatielEventEmitter();\n for (const service of this.#services) {\n service.onDegraded((data) => {\n if (this.#status !== STATUSES.Degraded) {\n log('Updating status to \"degraded\"', data);\n this.#status = STATUSES.Degraded;\n this.#onDegradedEventEmitter.emit({\n ...data,\n primaryEndpointUrl: this.#primaryService.endpointUrl.toString(),\n });\n }\n });\n }\n\n this.#onAvailableEventEmitter = new CockatielEventEmitter();\n for (const service of this.#services) {\n service.onAvailable((data) => {\n if (this.#status !== STATUSES.Available) {\n log('Updating status to \"available\"', data);\n this.#status = STATUSES.Available;\n this.#onAvailableEventEmitter.emit({\n ...data,\n primaryEndpointUrl: this.#primaryService.endpointUrl.toString(),\n });\n }\n });\n }\n }\n\n /**\n * Calls the provided callback when any of the RPC services is retried.\n *\n * This is mainly useful for tests.\n *\n * @param listener - The callback to be called.\n * @returns An object with a `dispose` method which can be used to unregister\n * the event listener.\n */\n onServiceRetry(\n listener: CockatielEventToEventListenerWithData<\n RpcService['onRetry'],\n { primaryEndpointUrl: string }\n >,\n ) {\n const disposables = this.#services.map((service) =>\n service.onRetry((data) => {\n listener({\n ...data,\n primaryEndpointUrl: this.#primaryService.endpointUrl.toString(),\n });\n }),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Calls the provided callback only when the maximum number of failed\n * consecutive attempts to receive a 2xx response has been reached for all\n * RPC services in the chain, and all services' underlying circuits have\n * broken.\n *\n * The callback will not be called if a service's circuit breaks but its\n * failover does not. Use `onServiceBreak` if you'd like a lower level of\n * granularity.\n *\n * @param listener - The callback to be called.\n * @returns An object with a `dispose` method which can be used to unregister\n * the callback.\n */\n onBreak(\n listener: CockatielEventToEventListenerWithData<\n RpcService['onBreak'],\n { primaryEndpointUrl: string }\n >,\n ) {\n return this.#onBreakEventEmitter.addListener(listener);\n }\n\n /**\n * Calls the provided callback each time when, for *any* of the RPC services\n * in this chain, the maximum number of failed consecutive attempts to receive\n * a 2xx response has been reached and the underlying circuit has broken. A\n * more granular version of `onBreak`.\n *\n * @param listener - The callback to be called.\n * @returns An object with a `dispose` method which can be used to unregister\n * the callback.\n */\n onServiceBreak(\n listener: CockatielEventToEventListenerWithData<\n RpcService['onBreak'],\n { primaryEndpointUrl: string }\n >,\n ) {\n const disposables = this.#services.map((service) =>\n service.onBreak((data) => {\n listener({\n ...data,\n primaryEndpointUrl: this.#primaryService.endpointUrl.toString(),\n });\n }),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Calls the provided callback if no requests have been initiated yet or\n * all requests to RPC services in this chain have responded successfully in a\n * timely fashion, and then one of the two conditions apply:\n *\n * 1. When a retriable error is encountered making a request to an RPC\n * service, and the request is retried until a set maximum is reached.\n * 2. When a RPC service responds successfully, but the request takes longer\n * than a set number of seconds to complete.\n *\n * Note that the callback will be called even if there are local connectivity\n * issues which prevent requests from being initiated. This is intentional.\n *\n * Also note this callback will only be called if the RPC service chain as a\n * whole is in a \"degraded\" state, and will then only be called once (e.g., it\n * will not be called if a failover service falls into a degraded state, then\n * the primary comes back online, but it is slow). Use `onServiceDegraded` if\n * you'd like a lower level of granularity.\n *\n * @param listener - The callback to be called.\n * @returns An object with a `dispose` method which can be used to unregister\n * the callback.\n */\n onDegraded(\n listener: CockatielEventToEventListenerWithData<\n RpcService['onDegraded'],\n { primaryEndpointUrl: string }\n >,\n ) {\n return this.#onDegradedEventEmitter.addListener(listener);\n }\n\n /**\n * Calls the provided callback each time one of the two conditions apply:\n *\n * 1. When a retriable error is encountered making a request to an RPC\n * service, and the request is retried until a set maximum is reached.\n * 2. When a RPC service responds successfully, but the request takes longer\n * than a set number of seconds to complete.\n *\n * Note that the callback will be called even if there are local connectivity\n * issues which prevent requests from being initiated. This is intentional.\n *\n * This is a more granular version of `onDegraded`. The callback will be\n * called for each slow request to an RPC service. It may also be called again\n * if a failover service falls into a degraded state, then the primary comes\n * back online, but it is slow.\n *\n * @param listener - The callback to be called.\n * @returns An object with a `dispose` method which can be used to unregister\n * the callback.\n */\n onServiceDegraded(\n listener: CockatielEventToEventListenerWithData<\n RpcService['onDegraded'],\n { primaryEndpointUrl: string }\n >,\n ) {\n const disposables = this.#services.map((service) =>\n service.onDegraded((data) => {\n listener({\n ...data,\n primaryEndpointUrl: this.#primaryService.endpointUrl.toString(),\n });\n }),\n );\n\n return {\n dispose() {\n disposables.forEach((disposable) => disposable.dispose());\n },\n };\n }\n\n /**\n * Calls the provided callback in one of the following two conditions:\n *\n * 1. The first time that a 2xx request is made to any of the RPC services in\n * this chain.\n * 2. When requests to any the failover RPC services in this chain were\n * failing such that they were degraded or their underyling circuits broke,\n * but the first request to the primary succeeds again.\n *\n * Note this callback will only be called if the RPC service chain as a whole\n * is in an \"available\" state.\n *\n * @param listener - The callback to be called.\n * @returns An object with a `dispose` method which can be used to unregister\n * the callback.\n */\n onAvailable(\n listener: CockatielEventToEventListenerWithData<\n RpcService['onAvailable'],\n { primaryEndpointUrl: string }\n >,\n ) {\n return this.#onAvailableEventEmitter.addListener(listener);\n }\n\n /**\n * Uses the RPC services in the chain to make a request, using each service\n * after the first as a fallback to the previous one as necessary.\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 401 error if the response status is 401.\n * @throws A \"rate limiting\" error if the response HTTP status is 429.\n * @throws A \"resource unavailable\" error if the response status is 402, 404, or any 5xx.\n * @throws A generic HTTP client error (-32100) for any other 4xx status codes.\n * @throws A \"parse\" error if the response is not valid JSON.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>> & {\n method: 'eth_getBlockByNumber';\n },\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;\n\n /**\n * Uses the RPC services in the chain to make a request, using each service\n * after the first as a fallback to the previous one as necessary.\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 401 error if the response status is 401.\n * @throws A \"rate limiting\" error if the response HTTP status is 429.\n * @throws A \"resource unavailable\" error if the response status is 402, 404, or any 5xx.\n * @throws A generic HTTP client error (-32100) for any other 4xx status codes.\n * @throws A \"parse\" error if the response is not valid JSON.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result>>;\n\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions: FetchOptions = {},\n ): Promise<JsonRpcResponse<Result | null>> {\n // Start with the primary (first) service and switch to failovers as the\n // need arises. This is a bit confusing, so keep reading for more on how\n // this works.\n\n let availableServiceIndex: number | undefined;\n let response: JsonRpcResponse<Result> | undefined;\n\n for (const [i, service] of this.#services.entries()) {\n log(`Trying service #${i + 1}...`);\n const previousCircuitState = service.getCircuitState();\n\n try {\n // Try making the request through the service.\n response = await service.request<Params, Result>(\n jsonRpcRequest,\n fetchOptions,\n );\n log('Service successfully received request.');\n availableServiceIndex = i;\n break;\n } catch (error) {\n // Oops, that didn't work.\n // Capture this error so that we can handle it later.\n\n const lastFailureReason = service.getLastInnerFailureReason();\n const isCircuitOpen = service.getCircuitState() === CircuitState.Open;\n\n log('Service failed!', error, lastFailureReason);\n log(\n 'Circuit state',\n service.getCircuitState(),\n 'Previous circuit state',\n previousCircuitState,\n 'state',\n this.#status,\n );\n\n if (isCircuitOpen) {\n if (i < this.#services.length - 1) {\n log(\n \"This service's circuit is open. Proceeding to next service...\",\n );\n continue;\n }\n\n if (\n previousCircuitState !== CircuitState.Open &&\n this.#status !== STATUSES.Unavailable &&\n lastFailureReason !== undefined\n ) {\n // If the service's circuit just broke and it's the last one in the\n // chain, then trigger the onBreak event. (But if for some reason we\n // have already done this, then don't do it.)\n log(\n 'This service\\'s circuit just opened and it is the last service. Updating status to \"unavailable\" and triggering onBreak.',\n );\n this.#status = STATUSES.Unavailable;\n this.#onBreakEventEmitter.emit({\n ...lastFailureReason,\n primaryEndpointUrl: this.#primaryService.endpointUrl.toString(),\n endpointUrl: service.endpointUrl.toString(),\n });\n }\n }\n\n // The service failed, and we throw whatever the error is. The calling\n // code can try again if it so desires.\n log(\n `${isCircuitOpen ? \"This service's circuit is open, but for some reason it wasn't handled above. \" : \"This service's circuit is closed. \"}Re-throwing error.`,\n );\n throw error;\n }\n }\n\n if (response) {\n // If one of the services returned a successful response, assume that we\n // won't need to hit any of the failover services following it and reset\n // all of the policies of the following services. In particularly this\n // means that if any of the failover services' circuits was open when\n // requests were diverted back to the available service, that circuit will\n // now be reset so that if we start hitting it again we don't get a\n // \"circuit broken\" error.\n if (availableServiceIndex !== undefined) {\n for (const [i, service] of [...this.#services.entries()].slice(\n availableServiceIndex + 1,\n )) {\n log(`Resetting policy for service #${i + 1}.`);\n service.resetPolicy();\n }\n }\n\n return response;\n }\n\n // The only way we can end up here is if there are no services to loop over.\n // That is not possible due to the types on the constructor, but TypeScript\n // doesn't know this, so we have to appease it.\n throw new Error('Nothing to return');\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"rpc-service-chain.mjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-chain.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,OAAO,EAAE,UAAU,EAAE,0BAAsB;AAK3C;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IAG1B;;;;;;OAMG;IACH,YACE,wBAAsE;;QAV/D,4CAAwB;QAY/B,uBAAA,IAAI,6BAAa,uBAAA,IAAI,yEAAsB,MAA1B,IAAI,EAAuB,wBAAwB,CAAC,MAAA,CAAC;IACxE,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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AC,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 { RpcService } from './rpc-service';\nimport type { RpcServiceOptions } from './rpc-service';\nimport type { RpcServiceRequestable } from './rpc-service-requestable';\nimport type { FetchOptions } from './shared';\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\n * primary way of reaching the network and the remaining objects are used as\n * failovers.\n */\nexport class RpcServiceChain implements RpcServiceRequestable {\n readonly #services: RpcService[];\n\n /**\n * Constructs a new RpcServiceChain object.\n *\n * @param rpcServiceConfigurations - The options for the RPC services\n * that you want to construct. Each object in this array is the same as\n * {@link RpcServiceOptions}.\n */\n constructor(\n rpcServiceConfigurations: Omit<RpcServiceOptions, 'failoverService'>[],\n ) {\n this.#services = this.#buildRpcServiceChain(rpcServiceConfigurations);\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 401 error if the response status is 401.\n * @throws A \"rate limiting\" error if the response HTTP status is 429.\n * @throws A \"resource unavailable\" error if the response status is 402, 404, or any 5xx.\n * @throws A generic HTTP client error (-32100) for any other 4xx status codes.\n * @throws A \"parse\" error if the response is not valid JSON.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>> & {\n method: 'eth_getBlockByNumber';\n },\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 401 error if the response status is 401.\n * @throws A \"rate limiting\" error if the response HTTP status is 429.\n * @throws A \"resource unavailable\" error if the response status is 402, 404, or any 5xx.\n * @throws A generic HTTP client error (-32100) for any other 4xx status codes.\n * @throws A \"parse\" error if the response is not valid JSON.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result>>;\n\n async request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<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 rpcServiceConfigurations - The options for the RPC services that\n * you want to construct. Each object in this array is the same as\n * {@link RpcServiceOptions}.\n * @returns The constructed chain of RPC services.\n */\n #buildRpcServiceChain(\n rpcServiceConfigurations: Omit<RpcServiceOptions, 'failoverService'>[],\n ): RpcService[] {\n return [...rpcServiceConfigurations]\n .reverse()\n .reduce((workingServices: RpcService[], serviceConfiguration, index) => {\n const failoverService = index > 0 ? workingServices[0] : undefined;\n const service = new RpcService({\n ...serviceConfiguration,\n failoverService,\n });\n return [service, ...workingServices];\n }, []);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-requestable.cjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"","sourcesContent":["import type { ServicePolicy } from '@metamask/controller-utils';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"rpc-service-requestable.cjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"","sourcesContent":["import type { ServicePolicy } from '@metamask/controller-utils';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { AddToCockatielEventData, FetchOptions } from './shared';\n\n/**\n * The interface for a service class responsible for making a request to a\n * target, whether that is a single RPC endpoint or an RPC endpoint in an RPC\n * service chain.\n */\nexport type RpcServiceRequestable = {\n /**\n * Listens for when the RPC service retries the request.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link ServicePolicy.onRetry} returns.\n * @see {@link createServicePolicy}\n */\n onRetry(\n listener: AddToCockatielEventData<\n Parameters<ServicePolicy['onRetry']>[0],\n { endpointUrl: string }\n >,\n ): ReturnType<ServicePolicy['onRetry']>;\n\n /**\n * Listens for when the RPC service retries the request too many times in a\n * row.\n *\n * @param listener - The callback to be called when the circuit is broken.\n * @returns What {@link ServicePolicy.onBreak} returns.\n * @see {@link createServicePolicy}\n */\n onBreak(\n listener: AddToCockatielEventData<\n Parameters<ServicePolicy['onBreak']>[0],\n { endpointUrl: string }\n >,\n ): ReturnType<ServicePolicy['onBreak']>;\n\n /**\n * Listens for when the policy underlying this RPC service detects a slow\n * request.\n *\n * @param listener - The callback to be called when the request is slow.\n * @returns What {@link ServicePolicy.onDegraded} returns.\n * @see {@link createServicePolicy}\n */\n onDegraded(\n listener: AddToCockatielEventData<\n Parameters<ServicePolicy['onDegraded']>[0],\n { endpointUrl: string }\n >,\n ): ReturnType<ServicePolicy['onDegraded']>;\n\n /**\n * Makes a request to the target.\n */\n request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result | null>>;\n};\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ServicePolicy } from "@metamask/controller-utils";
|
|
2
2
|
import type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcResponse } from "@metamask/utils";
|
|
3
|
-
import type {
|
|
3
|
+
import type { AddToCockatielEventData, FetchOptions } from "./shared.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* The interface for a service class responsible for making a request to a
|
|
6
6
|
* target, whether that is a single RPC endpoint or an RPC endpoint in an RPC
|
|
@@ -14,7 +14,7 @@ export type RpcServiceRequestable = {
|
|
|
14
14
|
* @returns What {@link ServicePolicy.onRetry} returns.
|
|
15
15
|
* @see {@link createServicePolicy}
|
|
16
16
|
*/
|
|
17
|
-
onRetry(listener:
|
|
17
|
+
onRetry(listener: AddToCockatielEventData<Parameters<ServicePolicy['onRetry']>[0], {
|
|
18
18
|
endpointUrl: string;
|
|
19
19
|
}>): ReturnType<ServicePolicy['onRetry']>;
|
|
20
20
|
/**
|
|
@@ -25,9 +25,9 @@ export type RpcServiceRequestable = {
|
|
|
25
25
|
* @returns What {@link ServicePolicy.onBreak} returns.
|
|
26
26
|
* @see {@link createServicePolicy}
|
|
27
27
|
*/
|
|
28
|
-
onBreak(listener:
|
|
28
|
+
onBreak(listener: AddToCockatielEventData<Parameters<ServicePolicy['onBreak']>[0], {
|
|
29
29
|
endpointUrl: string;
|
|
30
|
-
}
|
|
30
|
+
}>): ReturnType<ServicePolicy['onBreak']>;
|
|
31
31
|
/**
|
|
32
32
|
* Listens for when the policy underlying this RPC service detects a slow
|
|
33
33
|
* request.
|
|
@@ -36,19 +36,9 @@ export type RpcServiceRequestable = {
|
|
|
36
36
|
* @returns What {@link ServicePolicy.onDegraded} returns.
|
|
37
37
|
* @see {@link createServicePolicy}
|
|
38
38
|
*/
|
|
39
|
-
onDegraded(listener:
|
|
39
|
+
onDegraded(listener: AddToCockatielEventData<Parameters<ServicePolicy['onDegraded']>[0], {
|
|
40
40
|
endpointUrl: string;
|
|
41
41
|
}>): ReturnType<ServicePolicy['onDegraded']>;
|
|
42
|
-
/**
|
|
43
|
-
* Listens for when the policy underlying this RPC service is available.
|
|
44
|
-
*
|
|
45
|
-
* @param listener - The callback to be called when the request is available.
|
|
46
|
-
* @returns What {@link ServicePolicy.onDegraded} returns.
|
|
47
|
-
* @see {@link createServicePolicy}
|
|
48
|
-
*/
|
|
49
|
-
onAvailable(listener: CockatielEventToEventListenerWithData<ServicePolicy['onAvailable'], {
|
|
50
|
-
endpointUrl: string;
|
|
51
|
-
}>): ReturnType<ServicePolicy['onAvailable']>;
|
|
52
42
|
/**
|
|
53
43
|
* Makes a request to the target.
|
|
54
44
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-requestable.d.cts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAEzB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"rpc-service-requestable.d.cts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,uBAAuB,EAAE,YAAY,EAAE,qBAAiB;AAEtE;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;OAMG;IACH,OAAO,CACL,QAAQ,EAAE,uBAAuB,CAC/B,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EACvC;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CACxB,GACA,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;IAExC;;;;;;;OAOG;IACH,OAAO,CACL,QAAQ,EAAE,uBAAuB,CAC/B,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EACvC;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CACxB,GACA,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;IAExC;;;;;;;OAOG;IACH,UAAU,CACR,QAAQ,EAAE,uBAAuB,CAC/B,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1C;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CACxB,GACA,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC;IAE3C;;OAEG;IACH,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EACvD,cAAc,EAAE,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,EAChD,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,eAAe,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC;CAC5C,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ServicePolicy } from "@metamask/controller-utils";
|
|
2
2
|
import type { Json, JsonRpcParams, JsonRpcRequest, JsonRpcResponse } from "@metamask/utils";
|
|
3
|
-
import type {
|
|
3
|
+
import type { AddToCockatielEventData, FetchOptions } from "./shared.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* The interface for a service class responsible for making a request to a
|
|
6
6
|
* target, whether that is a single RPC endpoint or an RPC endpoint in an RPC
|
|
@@ -14,7 +14,7 @@ export type RpcServiceRequestable = {
|
|
|
14
14
|
* @returns What {@link ServicePolicy.onRetry} returns.
|
|
15
15
|
* @see {@link createServicePolicy}
|
|
16
16
|
*/
|
|
17
|
-
onRetry(listener:
|
|
17
|
+
onRetry(listener: AddToCockatielEventData<Parameters<ServicePolicy['onRetry']>[0], {
|
|
18
18
|
endpointUrl: string;
|
|
19
19
|
}>): ReturnType<ServicePolicy['onRetry']>;
|
|
20
20
|
/**
|
|
@@ -25,9 +25,9 @@ export type RpcServiceRequestable = {
|
|
|
25
25
|
* @returns What {@link ServicePolicy.onBreak} returns.
|
|
26
26
|
* @see {@link createServicePolicy}
|
|
27
27
|
*/
|
|
28
|
-
onBreak(listener:
|
|
28
|
+
onBreak(listener: AddToCockatielEventData<Parameters<ServicePolicy['onBreak']>[0], {
|
|
29
29
|
endpointUrl: string;
|
|
30
|
-
}
|
|
30
|
+
}>): ReturnType<ServicePolicy['onBreak']>;
|
|
31
31
|
/**
|
|
32
32
|
* Listens for when the policy underlying this RPC service detects a slow
|
|
33
33
|
* request.
|
|
@@ -36,19 +36,9 @@ export type RpcServiceRequestable = {
|
|
|
36
36
|
* @returns What {@link ServicePolicy.onDegraded} returns.
|
|
37
37
|
* @see {@link createServicePolicy}
|
|
38
38
|
*/
|
|
39
|
-
onDegraded(listener:
|
|
39
|
+
onDegraded(listener: AddToCockatielEventData<Parameters<ServicePolicy['onDegraded']>[0], {
|
|
40
40
|
endpointUrl: string;
|
|
41
41
|
}>): ReturnType<ServicePolicy['onDegraded']>;
|
|
42
|
-
/**
|
|
43
|
-
* Listens for when the policy underlying this RPC service is available.
|
|
44
|
-
*
|
|
45
|
-
* @param listener - The callback to be called when the request is available.
|
|
46
|
-
* @returns What {@link ServicePolicy.onDegraded} returns.
|
|
47
|
-
* @see {@link createServicePolicy}
|
|
48
|
-
*/
|
|
49
|
-
onAvailable(listener: CockatielEventToEventListenerWithData<ServicePolicy['onAvailable'], {
|
|
50
|
-
endpointUrl: string;
|
|
51
|
-
}>): ReturnType<ServicePolicy['onAvailable']>;
|
|
52
42
|
/**
|
|
53
43
|
* Makes a request to the target.
|
|
54
44
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-requestable.d.mts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAEzB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"rpc-service-requestable.d.mts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,mCAAmC;AAChE,OAAO,KAAK,EACV,IAAI,EACJ,aAAa,EACb,cAAc,EACd,eAAe,EAChB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,uBAAuB,EAAE,YAAY,EAAE,qBAAiB;AAEtE;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;;;;;OAMG;IACH,OAAO,CACL,QAAQ,EAAE,uBAAuB,CAC/B,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EACvC;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CACxB,GACA,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;IAExC;;;;;;;OAOG;IACH,OAAO,CACL,QAAQ,EAAE,uBAAuB,CAC/B,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,EACvC;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CACxB,GACA,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;IAExC;;;;;;;OAOG;IACH,UAAU,CACR,QAAQ,EAAE,uBAAuB,CAC/B,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,EAC1C;QAAE,WAAW,EAAE,MAAM,CAAA;KAAE,CACxB,GACA,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC;IAE3C;;OAEG;IACH,OAAO,CAAC,MAAM,SAAS,aAAa,EAAE,MAAM,SAAS,IAAI,EACvD,cAAc,EAAE,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,EAChD,YAAY,CAAC,EAAE,YAAY,GAC1B,OAAO,CAAC,eAAe,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC,CAAC;CAC5C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-service-requestable.mjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"","sourcesContent":["import type { ServicePolicy } from '@metamask/controller-utils';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport type {
|
|
1
|
+
{"version":3,"file":"rpc-service-requestable.mjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service-requestable.ts"],"names":[],"mappings":"","sourcesContent":["import type { ServicePolicy } from '@metamask/controller-utils';\nimport type {\n Json,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n} from '@metamask/utils';\n\nimport type { AddToCockatielEventData, FetchOptions } from './shared';\n\n/**\n * The interface for a service class responsible for making a request to a\n * target, whether that is a single RPC endpoint or an RPC endpoint in an RPC\n * service chain.\n */\nexport type RpcServiceRequestable = {\n /**\n * Listens for when the RPC service retries the request.\n *\n * @param listener - The callback to be called when the retry occurs.\n * @returns What {@link ServicePolicy.onRetry} returns.\n * @see {@link createServicePolicy}\n */\n onRetry(\n listener: AddToCockatielEventData<\n Parameters<ServicePolicy['onRetry']>[0],\n { endpointUrl: string }\n >,\n ): ReturnType<ServicePolicy['onRetry']>;\n\n /**\n * Listens for when the RPC service retries the request too many times in a\n * row.\n *\n * @param listener - The callback to be called when the circuit is broken.\n * @returns What {@link ServicePolicy.onBreak} returns.\n * @see {@link createServicePolicy}\n */\n onBreak(\n listener: AddToCockatielEventData<\n Parameters<ServicePolicy['onBreak']>[0],\n { endpointUrl: string }\n >,\n ): ReturnType<ServicePolicy['onBreak']>;\n\n /**\n * Listens for when the policy underlying this RPC service detects a slow\n * request.\n *\n * @param listener - The callback to be called when the request is slow.\n * @returns What {@link ServicePolicy.onDegraded} returns.\n * @see {@link createServicePolicy}\n */\n onDegraded(\n listener: AddToCockatielEventData<\n Parameters<ServicePolicy['onDegraded']>[0],\n { endpointUrl: string }\n >,\n ): ReturnType<ServicePolicy['onDegraded']>;\n\n /**\n * Makes a request to the target.\n */\n request<Params extends JsonRpcParams, Result extends Json>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions?: FetchOptions,\n ): Promise<JsonRpcResponse<Result | null>>;\n};\n"]}
|