@metamask-previews/network-controller 26.0.0-preview-d6fe4594 → 26.0.0-preview-bc80feb8

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.
Files changed (60) hide show
  1. package/CHANGELOG.md +0 -16
  2. package/dist/NetworkController.cjs +0 -4
  3. package/dist/NetworkController.cjs.map +1 -1
  4. package/dist/NetworkController.d.cts +12 -135
  5. package/dist/NetworkController.d.cts.map +1 -1
  6. package/dist/NetworkController.d.mts +12 -135
  7. package/dist/NetworkController.d.mts.map +1 -1
  8. package/dist/NetworkController.mjs +0 -4
  9. package/dist/NetworkController.mjs.map +1 -1
  10. package/dist/create-auto-managed-network-client.cjs +1 -4
  11. package/dist/create-auto-managed-network-client.cjs.map +1 -1
  12. package/dist/create-auto-managed-network-client.d.cts +2 -5
  13. package/dist/create-auto-managed-network-client.d.cts.map +1 -1
  14. package/dist/create-auto-managed-network-client.d.mts +2 -5
  15. package/dist/create-auto-managed-network-client.d.mts.map +1 -1
  16. package/dist/create-auto-managed-network-client.mjs +1 -4
  17. package/dist/create-auto-managed-network-client.mjs.map +1 -1
  18. package/dist/create-network-client.cjs +42 -132
  19. package/dist/create-network-client.cjs.map +1 -1
  20. package/dist/create-network-client.d.cts +2 -5
  21. package/dist/create-network-client.d.cts.map +1 -1
  22. package/dist/create-network-client.d.mts +2 -5
  23. package/dist/create-network-client.d.mts.map +1 -1
  24. package/dist/create-network-client.mjs +42 -132
  25. package/dist/create-network-client.mjs.map +1 -1
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.cts +1 -1
  28. package/dist/index.d.cts.map +1 -1
  29. package/dist/index.d.mts +1 -1
  30. package/dist/index.d.mts.map +1 -1
  31. package/dist/index.mjs.map +1 -1
  32. package/dist/rpc-service/rpc-service-chain.cjs +39 -247
  33. package/dist/rpc-service/rpc-service-chain.cjs.map +1 -1
  34. package/dist/rpc-service/rpc-service-chain.d.cts +25 -106
  35. package/dist/rpc-service/rpc-service-chain.d.cts.map +1 -1
  36. package/dist/rpc-service/rpc-service-chain.d.mts +25 -106
  37. package/dist/rpc-service/rpc-service-chain.d.mts.map +1 -1
  38. package/dist/rpc-service/rpc-service-chain.mjs +39 -247
  39. package/dist/rpc-service/rpc-service-chain.mjs.map +1 -1
  40. package/dist/rpc-service/rpc-service-requestable.cjs.map +1 -1
  41. package/dist/rpc-service/rpc-service-requestable.d.cts +5 -15
  42. package/dist/rpc-service/rpc-service-requestable.d.cts.map +1 -1
  43. package/dist/rpc-service/rpc-service-requestable.d.mts +5 -15
  44. package/dist/rpc-service/rpc-service-requestable.d.mts.map +1 -1
  45. package/dist/rpc-service/rpc-service-requestable.mjs.map +1 -1
  46. package/dist/rpc-service/rpc-service.cjs +33 -65
  47. package/dist/rpc-service/rpc-service.cjs.map +1 -1
  48. package/dist/rpc-service/rpc-service.d.cts +24 -33
  49. package/dist/rpc-service/rpc-service.d.cts.map +1 -1
  50. package/dist/rpc-service/rpc-service.d.mts +24 -33
  51. package/dist/rpc-service/rpc-service.d.mts.map +1 -1
  52. package/dist/rpc-service/rpc-service.mjs +34 -66
  53. package/dist/rpc-service/rpc-service.mjs.map +1 -1
  54. package/dist/rpc-service/shared.cjs.map +1 -1
  55. package/dist/rpc-service/shared.d.cts +3 -25
  56. package/dist/rpc-service/shared.d.cts.map +1 -1
  57. package/dist/rpc-service/shared.d.mts +3 -25
  58. package/dist/rpc-service/shared.d.mts.map +1 -1
  59. package/dist/rpc-service/shared.mjs.map +1 -1
  60. package/package.json +1 -2
@@ -1,9 +1,9 @@
1
- import type { CreateServicePolicyOptions } from "@metamask/controller-utils";
1
+ import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
2
2
  import type { JsonRpcRequest } from "@metamask/utils";
3
3
  import { type Json, type JsonRpcParams, type JsonRpcResponse } from "@metamask/utils";
4
4
  import type { Logger } from "loglevel";
5
5
  import type { AbstractRpcService } from "./abstract-rpc-service.mjs";
6
- import type { FetchOptions } from "./shared.mjs";
6
+ import type { AddToCockatielEventData, FetchOptions } from "./shared.mjs";
7
7
  /**
8
8
  * Options for the RpcService constructor.
9
9
  */
@@ -17,6 +17,11 @@ export type RpcServiceOptions = {
17
17
  * The URL of the RPC endpoint to hit.
18
18
  */
19
19
  endpointUrl: URL | string;
20
+ /**
21
+ * An RPC service that represents a failover endpoint which will be invoked
22
+ * while the circuit for _this_ service is open.
23
+ */
24
+ failoverService?: AbstractRpcService;
20
25
  /**
21
26
  * A function that can be used to make an HTTP request. If your JavaScript
22
27
  * environment supports `fetch` natively, you'll probably want to pass that;
@@ -94,29 +99,12 @@ export declare class RpcService implements AbstractRpcService {
94
99
  * The URL of the RPC endpoint.
95
100
  */
96
101
  readonly endpointUrl: URL;
97
- /**
98
- * The last error that the retry policy captured (or `undefined` if the last
99
- * execution of the service was successful).
100
- */
101
- lastError: Error | undefined;
102
102
  /**
103
103
  * Constructs a new RpcService object.
104
104
  *
105
105
  * @param options - The options. See {@link RpcServiceOptions}.
106
106
  */
107
107
  constructor(options: RpcServiceOptions);
108
- /**
109
- * Resets the underlying composite Cockatiel policy.
110
- *
111
- * This is useful in a collection of RpcServices where some act as failovers
112
- * for others where you effectively want to invalidate the failovers when the
113
- * primary recovers.
114
- */
115
- resetPolicy(): void;
116
- /**
117
- * @returns The state of the underlying circuit.
118
- */
119
- getCircuitState(): import("@metamask/controller-utils").CircuitState;
120
108
  /**
121
109
  * Listens for when the RPC service retries the request.
122
110
  *
@@ -124,16 +112,21 @@ export declare class RpcService implements AbstractRpcService {
124
112
  * @returns What {@link ServicePolicy.onRetry} returns.
125
113
  * @see {@link createServicePolicy}
126
114
  */
127
- onRetry(listener: Parameters<AbstractRpcService['onRetry']>[0]): import("cockatiel").IDisposable;
115
+ onRetry(listener: AddToCockatielEventData<Parameters<ServicePolicy['onRetry']>[0], {
116
+ endpointUrl: string;
117
+ }>): import("cockatiel").IDisposable;
128
118
  /**
129
119
  * Listens for when the RPC service retries the request too many times in a
130
- * row, causing the underlying circuit to break.
120
+ * row.
131
121
  *
132
122
  * @param listener - The callback to be called when the circuit is broken.
133
123
  * @returns What {@link ServicePolicy.onBreak} returns.
134
124
  * @see {@link createServicePolicy}
135
125
  */
136
- onBreak(listener: Parameters<AbstractRpcService['onBreak']>[0]): import("cockatiel").IDisposable;
126
+ onBreak(listener: AddToCockatielEventData<Parameters<ServicePolicy['onBreak']>[0], {
127
+ endpointUrl: string;
128
+ failoverEndpointUrl?: string;
129
+ }>): import("cockatiel").IDisposable;
137
130
  /**
138
131
  * Listens for when the policy underlying this RPC service detects a slow
139
132
  * request.
@@ -142,17 +135,13 @@ export declare class RpcService implements AbstractRpcService {
142
135
  * @returns What {@link ServicePolicy.onDegraded} returns.
143
136
  * @see {@link createServicePolicy}
144
137
  */
145
- onDegraded(listener: Parameters<AbstractRpcService['onDegraded']>[0]): import("cockatiel").IDisposable;
146
- /**
147
- * Listens for when the policy underlying this RPC service is available.
148
- *
149
- * @param listener - The callback to be called when the request is available.
150
- * @returns What {@link ServicePolicy.onAvailable} returns.
151
- * @see {@link createServicePolicy}
152
- */
153
- onAvailable(listener: Parameters<AbstractRpcService['onAvailable']>[0]): import("cockatiel").IDisposable;
138
+ onDegraded(listener: AddToCockatielEventData<Parameters<ServicePolicy['onDegraded']>[0], {
139
+ endpointUrl: string;
140
+ }>): import("cockatiel").IDisposable;
154
141
  /**
155
- * Makes a request to the RPC endpoint.
142
+ * Makes a request to the RPC endpoint. If the circuit is open because this
143
+ * request has failed too many times, the request is forwarded to a failover
144
+ * service (if provided).
156
145
  *
157
146
  * This overload is specifically designed for `eth_getBlockByNumber`, which
158
147
  * can return a `result` of `null` despite an expected `Result` being
@@ -172,7 +161,9 @@ export declare class RpcService implements AbstractRpcService {
172
161
  method: 'eth_getBlockByNumber';
173
162
  }, fetchOptions?: FetchOptions): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>>;
174
163
  /**
175
- * Makes a request to the RPC endpoint.
164
+ * Makes a request to the RPC endpoint. If the circuit is open because this
165
+ * request has failed too many times, the request is forwarded to a failover
166
+ * service (if provided).
176
167
  *
177
168
  * This overload is designed for all RPC methods except for
178
169
  * `eth_getBlockByNumber`, which are expected to return a `result` of the
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-service.d.mts","sourceRoot":"","sources":["../../src/rpc-service/rpc-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAE3B,mCAAmC;AAQpC,OAAO,KAAK,EAAE,cAAc,EAAE,wBAAwB;AACtD,OAAO,EAIL,KAAK,IAAI,EACT,KAAK,aAAa,EAClB,KAAK,eAAe,EACrB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,MAAM,EAAE,iBAAiB;AAEvC,OAAO,KAAK,EAAE,kBAAkB,EAAE,mCAA+B;AACjE,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAG7C;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;;OAGG;IACH,IAAI,EAAE,OAAO,IAAI,CAAC;IAClB;;OAEG;IACH,WAAW,EAAE,GAAG,GAAG,MAAM,CAAC;IAC1B;;;;OAIG;IACH,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB;;;OAGG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B;;OAEG;IACH,MAAM,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B;;;OAGG;IACH,aAAa,CAAC,EAAE,IAAI,CAAC,0BAA0B,EAAE,mBAAmB,CAAC,CAAC;CACvE,CAAC;AAIF;;;GAGG;AACH,eAAO,MAAM,mBAAmB,IAAI,CAAC;AAErC;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,QAAgC,CAAC;AAE9E;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB;;;GA8C7B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB;;;CAGpB,CAAC;AAEX;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CA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+9,18 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _RpcService_instances, _RpcService_fetch, _RpcService_fetchOptions, _RpcService_logger, _RpcService_policy, _RpcService_getDefaultFetchOptions, _RpcService_getCompleteFetchOptions, _RpcService_executeAndProcessRequest;
12
+ var _RpcService_instances, _RpcService_fetch, _RpcService_fetchOptions, _RpcService_failoverService, _RpcService_logger, _RpcService_policy, _RpcService_getDefaultFetchOptions, _RpcService_getCompleteFetchOptions, _RpcService_processRequest;
13
13
  function $importDefault(module) {
14
14
  if (module?.__esModule) {
15
15
  return module.default;
16
16
  }
17
17
  return module;
18
18
  }
19
- import { BrokenCircuitError, HttpError, createServicePolicy, handleWhen } from "@metamask/controller-utils";
19
+ import { BrokenCircuitError, CircuitState, HttpError, createServicePolicy, handleWhen } from "@metamask/controller-utils";
20
20
  import { JsonRpcError, rpcErrors } from "@metamask/rpc-errors";
21
21
  import { Duration, getErrorMessage, hasProperty } from "@metamask/utils";
22
22
  import $deepmerge from "deepmerge";
23
23
  const deepmerge = $importDefault($deepmerge);
24
- import { projectLogger, createModuleLogger } from "../logger.mjs";
25
- const log = createModuleLogger(projectLogger, 'RpcService');
26
24
  /**
27
25
  * The maximum number of times that a failing service should be re-run before
28
26
  * giving up.
@@ -193,6 +191,11 @@ export class RpcService {
193
191
  * A common set of options that the request options will extend.
194
192
  */
195
193
  _RpcService_fetchOptions.set(this, void 0);
194
+ /**
195
+ * An RPC service that represents a failover endpoint which will be invoked
196
+ * while the circuit for _this_ service is open.
197
+ */
198
+ _RpcService_failoverService.set(this, void 0);
196
199
  /**
197
200
  * A `loglevel` logger.
198
201
  */
@@ -201,13 +204,14 @@ export class RpcService {
201
204
  * The policy that wraps the request.
202
205
  */
203
206
  _RpcService_policy.set(this, void 0);
204
- const { btoa: givenBtoa, endpointUrl, fetch: givenFetch, logger, fetchOptions = {}, policyOptions = {}, } = options;
207
+ const { btoa: givenBtoa, endpointUrl, failoverService, fetch: givenFetch, logger, fetchOptions = {}, policyOptions = {}, } = options;
205
208
  __classPrivateFieldSet(this, _RpcService_fetch, givenFetch, "f");
206
209
  const normalizedUrl = getNormalizedEndpointUrl(endpointUrl);
207
210
  __classPrivateFieldSet(this, _RpcService_fetchOptions, __classPrivateFieldGet(this, _RpcService_instances, "m", _RpcService_getDefaultFetchOptions).call(this, normalizedUrl, fetchOptions, givenBtoa), "f");
208
211
  this.endpointUrl = stripCredentialsFromUrl(normalizedUrl);
212
+ __classPrivateFieldSet(this, _RpcService_failoverService, failoverService, "f");
209
213
  __classPrivateFieldSet(this, _RpcService_logger, logger, "f");
210
- __classPrivateFieldSet(this, _RpcService_policy, createServicePolicy({
214
+ const policy = createServicePolicy({
211
215
  maxRetries: DEFAULT_MAX_RETRIES,
212
216
  maxConsecutiveFailures: DEFAULT_MAX_CONSECUTIVE_FAILURES,
213
217
  ...policyOptions,
@@ -225,23 +229,8 @@ export class RpcService {
225
229
  (hasProperty(error, 'code') &&
226
230
  (error.code === 'ETIMEDOUT' || error.code === 'ECONNRESET')));
227
231
  }),
228
- }), "f");
229
- }
230
- /**
231
- * Resets the underlying composite Cockatiel policy.
232
- *
233
- * This is useful in a collection of RpcServices where some act as failovers
234
- * for others where you effectively want to invalidate the failovers when the
235
- * primary recovers.
236
- */
237
- resetPolicy() {
238
- __classPrivateFieldGet(this, _RpcService_policy, "f").reset();
239
- }
240
- /**
241
- * @returns The state of the underlying circuit.
242
- */
243
- getCircuitState() {
244
- return __classPrivateFieldGet(this, _RpcService_policy, "f").getCircuitState();
232
+ });
233
+ __classPrivateFieldSet(this, _RpcService_policy, policy, "f");
245
234
  }
246
235
  /**
247
236
  * Listens for when the RPC service retries the request.
@@ -257,7 +246,7 @@ export class RpcService {
257
246
  }
258
247
  /**
259
248
  * Listens for when the RPC service retries the request too many times in a
260
- * row, causing the underlying circuit to break.
249
+ * row.
261
250
  *
262
251
  * @param listener - The callback to be called when the circuit is broken.
263
252
  * @returns What {@link ServicePolicy.onBreak} returns.
@@ -265,20 +254,13 @@ export class RpcService {
265
254
  */
266
255
  onBreak(listener) {
267
256
  return __classPrivateFieldGet(this, _RpcService_policy, "f").onBreak((data) => {
268
- // `{ isolated: true }` is a special object that shows up when `isolate`
269
- // is called on the circuit breaker. Usually `isolate` is used to hold the
270
- // circuit open, but we (ab)use this method in `createServicePolicy` to
271
- // reset the circuit breaker policy. When we do this, we don't want to
272
- // call `onBreak` handlers, because then it causes
273
- // `NetworkController:rpcEndpointUnavailable` and
274
- // `NetworkController:rpcEndpointChainUnavailable` to be published. So we
275
- // have to ignore that object here. The consequence is that `isolate`
276
- // doesn't function the way it is intended, at least in the context of an
277
- // RpcService. However, we are making a bet that we won't need to use it
278
- // other than how we are already using it.
279
- if (!('isolated' in data)) {
280
- listener({ ...data, endpointUrl: this.endpointUrl.toString() });
281
- }
257
+ listener({
258
+ ...data,
259
+ endpointUrl: this.endpointUrl.toString(),
260
+ failoverEndpointUrl: __classPrivateFieldGet(this, _RpcService_failoverService, "f")
261
+ ? __classPrivateFieldGet(this, _RpcService_failoverService, "f").endpointUrl.toString()
262
+ : undefined,
263
+ });
282
264
  });
283
265
  }
284
266
  /**
@@ -294,26 +276,23 @@ export class RpcService {
294
276
  listener({ ...(data ?? {}), endpointUrl: this.endpointUrl.toString() });
295
277
  });
296
278
  }
297
- /**
298
- * Listens for when the policy underlying this RPC service is available.
299
- *
300
- * @param listener - The callback to be called when the request is available.
301
- * @returns What {@link ServicePolicy.onAvailable} returns.
302
- * @see {@link createServicePolicy}
303
- */
304
- onAvailable(listener) {
305
- return __classPrivateFieldGet(this, _RpcService_policy, "f").onAvailable(() => {
306
- listener({ endpointUrl: this.endpointUrl.toString() });
307
- });
308
- }
309
279
  async request(
310
280
  // The request object may be frozen and must not be mutated.
311
281
  jsonRpcRequest, fetchOptions = {}) {
312
282
  const completeFetchOptions = __classPrivateFieldGet(this, _RpcService_instances, "m", _RpcService_getCompleteFetchOptions).call(this, jsonRpcRequest, fetchOptions);
313
- return await __classPrivateFieldGet(this, _RpcService_instances, "m", _RpcService_executeAndProcessRequest).call(this, completeFetchOptions);
283
+ try {
284
+ return await __classPrivateFieldGet(this, _RpcService_instances, "m", _RpcService_processRequest).call(this, completeFetchOptions);
285
+ }
286
+ catch (error) {
287
+ if (__classPrivateFieldGet(this, _RpcService_policy, "f").circuitBreakerPolicy.state === CircuitState.Open &&
288
+ __classPrivateFieldGet(this, _RpcService_failoverService, "f") !== undefined) {
289
+ return await __classPrivateFieldGet(this, _RpcService_failoverService, "f").request(jsonRpcRequest, completeFetchOptions);
290
+ }
291
+ throw error;
292
+ }
314
293
  }
315
294
  }
316
- _RpcService_fetch = new WeakMap(), _RpcService_fetchOptions = new WeakMap(), _RpcService_logger = new WeakMap(), _RpcService_policy = new WeakMap(), _RpcService_instances = new WeakSet(), _RpcService_getDefaultFetchOptions = function _RpcService_getDefaultFetchOptions(endpointUrl, fetchOptions, givenBtoa) {
295
+ _RpcService_fetch = new WeakMap(), _RpcService_fetchOptions = new WeakMap(), _RpcService_failoverService = new WeakMap(), _RpcService_logger = new WeakMap(), _RpcService_policy = new WeakMap(), _RpcService_instances = new WeakSet(), _RpcService_getDefaultFetchOptions = function _RpcService_getDefaultFetchOptions(endpointUrl, fetchOptions, givenBtoa) {
317
296
  if (endpointUrl.username && endpointUrl.password) {
318
297
  const authString = `${endpointUrl.username}:${endpointUrl.password}`;
319
298
  const encodedCredentials = givenBtoa(authString);
@@ -339,7 +318,7 @@ _RpcService_fetch = new WeakMap(), _RpcService_fetchOptions = new WeakMap(), _Rp
339
318
  params,
340
319
  });
341
320
  return { ...mergedOptions, body };
342
- }, _RpcService_executeAndProcessRequest =
321
+ }, _RpcService_processRequest =
343
322
  /**
344
323
  * Makes the request using the Cockatiel policy that this service creates.
345
324
  *
@@ -352,29 +331,18 @@ _RpcService_fetch = new WeakMap(), _RpcService_fetchOptions = new WeakMap(), _Rp
352
331
  * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.
353
332
  * @throws A "parse" JSON-RPC error (code -32700) if the response is not valid JSON.
354
333
  */
355
- async function _RpcService_executeAndProcessRequest(fetchOptions) {
334
+ async function _RpcService_processRequest(fetchOptions) {
356
335
  let response;
357
336
  try {
358
- log(`[${this.endpointUrl}] Circuit state`, __classPrivateFieldGet(this, _RpcService_policy, "f").getCircuitState());
359
- const jsonDecodedResponse = await __classPrivateFieldGet(this, _RpcService_policy, "f").execute(async (context) => {
360
- log('REQUEST INITIATED:', this.endpointUrl.toString(), '::', fetchOptions,
361
- // @ts-expect-error This property _is_ here, the type of
362
- // ServicePolicy is just wrong.
363
- `(attempt ${context.attempt + 1})`);
337
+ return await __classPrivateFieldGet(this, _RpcService_policy, "f").execute(async () => {
364
338
  response = await __classPrivateFieldGet(this, _RpcService_fetch, "f").call(this, this.endpointUrl, fetchOptions);
365
339
  if (!response.ok) {
366
340
  throw new HttpError(response.status);
367
341
  }
368
- log('REQUEST SUCCESSFUL:', this.endpointUrl.toString(), response.status);
369
342
  return await response.json();
370
343
  });
371
- this.lastError = undefined;
372
- return jsonDecodedResponse;
373
344
  }
374
345
  catch (error) {
375
- log('REQUEST ERROR:', this.endpointUrl.toString(), error);
376
- this.lastError =
377
- error instanceof Error ? error : new Error(getErrorMessage(error));
378
346
  if (error instanceof HttpError) {
379
347
  const status = error.httpStatus;
380
348
  if (status === 401) {
@@ -1 +1 @@
1
- {"version":3,"file":"rpc-service.mjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAIA,OAAO,EACL,kBAAkB,EAClB,SAAS,EACT,mBAAmB,EACnB,UAAU,EACX,mCAAmC;AACpC,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,6BAA6B;AAE/D,OAA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AAI,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,MAAM,SAAS,CAAC,KAAK,CAAC;gBACpB,OAAO,EAAE,mCAAmC;aAC7C,CAAC,CAAC;QACL,CAAC;aAAM,IAAI,KAAK,YAAY,kBAAkB,EAAE,CAAC;YAC/C,uBAAA,IAAI,0BAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAC1B,MAAM,4BAA4B,GAChC,uBAAA,IAAI,0BAAQ,CAAC,+BAA+B,EAAE,CAAC;YACjD,MAAM,qCAAqC,GAAG,IAAI,CAAC,YAAY,CAC7D,SAAS,EACT,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAC7B,CAAC,MAAM,CACN,CAAC,4BAA4B,IAAI,uBAAA,IAAI,0BAAQ,CAAC,oBAAoB,CAAC;gBACjE,QAAQ,CAAC,MAAM,CAClB,CAAC;YACF,MAAM,SAAS,CAAC,mBAAmB,CAAC;gBAClC,OAAO,EAAE,sDAAsD,qCAAqC,oDAAoD;aACzJ,CAAC,CAAC;QACL,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n BrokenCircuitError,\n HttpError,\n createServicePolicy,\n handleWhen,\n} from '@metamask/controller-utils';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport {\n Duration,\n getErrorMessage,\n hasProperty,\n type Json,\n type JsonRpcParams,\n type JsonRpcResponse,\n} from '@metamask/utils';\nimport deepmerge from 'deepmerge';\nimport type { Logger } from 'loglevel';\n\nimport type { AbstractRpcService } from './abstract-rpc-service';\nimport type { FetchOptions } from './shared';\nimport { projectLogger, createModuleLogger } from '../logger';\n\n/**\n * Options for the RpcService constructor.\n */\nexport type RpcServiceOptions = {\n /**\n * A function that can be used to convert a binary string into a\n * base64-encoded ASCII string. Used to encode authorization credentials.\n */\n btoa: typeof btoa;\n /**\n * The URL of the RPC endpoint to hit.\n */\n endpointUrl: URL | string;\n /**\n * A function that can be used to make an HTTP request. If your JavaScript\n * environment supports `fetch` natively, you'll probably want to pass that;\n * otherwise you can pass an equivalent (such as `fetch` via `node-fetch`).\n */\n fetch: typeof fetch;\n /**\n * A common set of options that will be used to make every request. Can be\n * overridden on the request level (e.g. to add headers).\n */\n fetchOptions?: FetchOptions;\n /**\n * A `loglevel` logger.\n */\n logger?: Pick<Logger, 'warn'>;\n /**\n * Options to pass to `createServicePolicy`. Note that `retryFilterPolicy` is\n * not accepted, as it is overwritten. See {@link createServicePolicy}.\n */\n policyOptions?: Omit<CreateServicePolicyOptions, 'retryFilterPolicy'>;\n};\n\nconst log = createModuleLogger(projectLogger, 'RpcService');\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 4;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries. This is set to a value such that if given a\n * service that continually fails, the policy needs to be executed 3 times\n * before further retries are paused.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The list of error messages that represent a failure to connect to the network.\n *\n * This list was derived from Sindre Sorhus's `is-network-error` package:\n * <https://github.com/sindresorhus/is-network-error/blob/7bbfa8be9482ce1427a21fbff60e3ee1650dd091/index.js>\n */\nexport const CONNECTION_ERRORS = [\n // Chrome\n {\n constructorName: 'TypeError',\n pattern: /network error/u,\n },\n // Chrome\n {\n constructorName: 'TypeError',\n pattern: /Failed to fetch/u,\n },\n // Firefox\n {\n constructorName: 'TypeError',\n pattern: /NetworkError when attempting to fetch resource\\./u,\n },\n // Safari 16\n {\n constructorName: 'TypeError',\n pattern: /The Internet connection appears to be offline\\./u,\n },\n // Safari 17+\n {\n constructorName: 'TypeError',\n pattern: /Load failed/u,\n },\n // `cross-fetch`\n {\n constructorName: 'TypeError',\n pattern: /Network request failed/u,\n },\n // `node-fetch`\n {\n constructorName: 'FetchError',\n pattern: /request to (.+) failed/u,\n },\n // Undici (Node.js)\n {\n constructorName: 'TypeError',\n pattern: /fetch failed/u,\n },\n // Undici (Node.js)\n {\n constructorName: 'TypeError',\n pattern: /terminated/u,\n },\n];\n\n/**\n * Custom JSON-RPC error codes for specific cases.\n *\n * These should be moved to `@metamask/rpc-errors` eventually.\n */\nexport const CUSTOM_RPC_ERRORS = {\n unauthorized: -32006,\n httpClientError: -32080,\n} as const;\n\n/**\n * Determines whether the given error represents a failure to reach the network\n * after request parameters have been validated.\n *\n * This is somewhat difficult to verify because JavaScript engines (and in\n * some cases libraries) produce slightly different error messages for this\n * particular scenario, and we need to account for this.\n *\n * @param error - The error.\n * @returns True if the error indicates that the network cannot be connected to,\n * and false otherwise.\n */\nexport function isConnectionError(error: unknown) {\n if (!(typeof error === 'object' && error !== null && 'message' in error)) {\n return false;\n }\n\n const { message } = error;\n\n return (\n typeof message === 'string' &&\n !isNockError(message) &&\n CONNECTION_ERRORS.some(({ constructorName, pattern }) => {\n return (\n error.constructor.name === constructorName && pattern.test(message)\n );\n })\n );\n}\n\n/**\n * Determines whether the given error message refers to a Nock error.\n *\n * It's important that if we failed to mock a request in a test, the resulting\n * error does not cause the request to be retried so that we can see it right\n * away.\n *\n * @param message - The error message to test.\n * @returns True if the message indicates a missing Nock mock, false otherwise.\n */\nfunction isNockError(message: string) {\n return message.includes('Nock:');\n}\n\n/**\n * Determine whether the given error message indicates a failure to parse JSON.\n *\n * This is different in tests vs. implementation code because it may manifest as\n * a FetchError or a SyntaxError.\n *\n * @param error - The error object to test.\n * @returns True if the error indicates a JSON parse error, false otherwise.\n */\nfunction isJsonParseError(error: unknown) {\n return (\n error instanceof SyntaxError ||\n /invalid json/iu.test(getErrorMessage(error))\n );\n}\n\n/**\n * Guarantees a URL, even given a string. This is useful for checking components\n * of that URL.\n *\n * @param endpointUrlOrUrlString - Either a URL object or a string that\n * represents the URL of an endpoint.\n * @returns A URL object.\n */\nfunction getNormalizedEndpointUrl(endpointUrlOrUrlString: URL | string): URL {\n return endpointUrlOrUrlString instanceof URL\n ? endpointUrlOrUrlString\n : new URL(endpointUrlOrUrlString);\n}\n\n/**\n * Strips username and password from a URL.\n *\n * @param url - The URL to strip credentials from.\n * @returns A new URL object with credentials removed.\n */\nfunction stripCredentialsFromUrl(url: URL): URL {\n const strippedUrl = new URL(url.toString());\n strippedUrl.username = '';\n strippedUrl.password = '';\n return strippedUrl;\n}\n\n/**\n * This class is responsible for making a request to an endpoint that implements\n * the JSON-RPC protocol. It is designed to gracefully handle network and server\n * failures, retrying requests using exponential backoff. It also offers a hook\n * which can used to respond to slow requests.\n */\nexport class RpcService implements AbstractRpcService {\n /**\n * The URL of the RPC endpoint.\n */\n readonly endpointUrl: URL;\n\n /**\n * The last error that the retry policy captured (or `undefined` if the last\n * execution of the service was successful).\n */\n lastError: Error | undefined;\n\n /**\n * The function used to make an HTTP request.\n */\n readonly #fetch: typeof fetch;\n\n /**\n * A common set of options that the request options will extend.\n */\n readonly #fetchOptions: FetchOptions;\n\n /**\n * A `loglevel` logger.\n */\n readonly #logger: RpcServiceOptions['logger'];\n\n /**\n * The policy that wraps the request.\n */\n readonly #policy: ServicePolicy;\n\n /**\n * Constructs a new RpcService object.\n *\n * @param options - The options. See {@link RpcServiceOptions}.\n */\n constructor(options: RpcServiceOptions) {\n const {\n btoa: givenBtoa,\n endpointUrl,\n fetch: givenFetch,\n logger,\n fetchOptions = {},\n policyOptions = {},\n } = options;\n\n this.#fetch = givenFetch;\n const normalizedUrl = getNormalizedEndpointUrl(endpointUrl);\n this.#fetchOptions = this.#getDefaultFetchOptions(\n normalizedUrl,\n fetchOptions,\n givenBtoa,\n );\n this.endpointUrl = stripCredentialsFromUrl(normalizedUrl);\n this.#logger = logger;\n\n this.#policy = createServicePolicy({\n maxRetries: DEFAULT_MAX_RETRIES,\n maxConsecutiveFailures: DEFAULT_MAX_CONSECUTIVE_FAILURES,\n ...policyOptions,\n retryFilterPolicy: handleWhen((error) => {\n return (\n // Ignore errors where the request failed to establish\n isConnectionError(error) ||\n // Ignore server sent HTML error pages or truncated JSON responses\n isJsonParseError(error) ||\n // Ignore server overload errors\n ('httpStatus' in error &&\n (error.httpStatus === 502 ||\n error.httpStatus === 503 ||\n error.httpStatus === 504)) ||\n (hasProperty(error, 'code') &&\n (error.code === 'ETIMEDOUT' || error.code === 'ECONNRESET'))\n );\n }),\n });\n }\n\n /**\n * Resets the underlying composite Cockatiel policy.\n *\n * This is useful in a collection of RpcServices where some act as failovers\n * for others where you effectively want to invalidate the failovers when the\n * primary recovers.\n */\n resetPolicy() {\n this.#policy.reset();\n }\n\n /**\n * @returns The state of the underlying circuit.\n */\n getCircuitState() {\n return this.#policy.getCircuitState();\n }\n\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(listener: Parameters<AbstractRpcService['onRetry']>[0]) {\n return this.#policy.onRetry((data) => {\n listener({ ...data, endpointUrl: this.endpointUrl.toString() });\n });\n }\n\n /**\n * Listens for when the RPC service retries the request too many times in a\n * row, causing the underlying circuit to break.\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(listener: Parameters<AbstractRpcService['onBreak']>[0]) {\n return this.#policy.onBreak((data) => {\n // `{ isolated: true }` is a special object that shows up when `isolate`\n // is called on the circuit breaker. Usually `isolate` is used to hold the\n // circuit open, but we (ab)use this method in `createServicePolicy` to\n // reset the circuit breaker policy. When we do this, we don't want to\n // call `onBreak` handlers, because then it causes\n // `NetworkController:rpcEndpointUnavailable` and\n // `NetworkController:rpcEndpointChainUnavailable` to be published. So we\n // have to ignore that object here. The consequence is that `isolate`\n // doesn't function the way it is intended, at least in the context of an\n // RpcService. However, we are making a bet that we won't need to use it\n // other than how we are already using it.\n if (!('isolated' in data)) {\n listener({ ...data, endpointUrl: this.endpointUrl.toString() });\n }\n });\n }\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(listener: Parameters<AbstractRpcService['onDegraded']>[0]) {\n return this.#policy.onDegraded((data) => {\n listener({ ...(data ?? {}), endpointUrl: this.endpointUrl.toString() });\n });\n }\n\n /**\n * Listens for when the policy underlying this RPC service is available.\n *\n * @param listener - The callback to be called when the request is available.\n * @returns What {@link ServicePolicy.onAvailable} returns.\n * @see {@link createServicePolicy}\n */\n onAvailable(listener: Parameters<AbstractRpcService['onAvailable']>[0]) {\n return this.#policy.onAvailable(() => {\n listener({ endpointUrl: this.endpointUrl.toString() });\n });\n }\n\n /**\n * Makes a request to the RPC endpoint.\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 An \"authorized\" JSON-RPC error (code -32006) if the response HTTP status is 401.\n * @throws A \"rate limiting\" JSON-RPC error (code -32005) if the response HTTP status is 429.\n * @throws A \"resource unavailable\" JSON-RPC error (code -32002) if the response HTTP status is 402, 404, or any 5xx.\n * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.\n * @throws A \"parse\" JSON-RPC error (code -32700) if the response is not valid JSON.\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 RPC endpoint.\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 An \"authorized\" JSON-RPC error (code -32006) if the response HTTP status is 401.\n * @throws A \"rate limiting\" JSON-RPC error (code -32005) if the response HTTP status is 429.\n * @throws A \"resource unavailable\" JSON-RPC error (code -32002) if the response HTTP status is 402, 404, or any 5xx.\n * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.\n * @throws A \"parse\" JSON-RPC error (code -32700) if the response is not valid JSON.\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 // The request object may be frozen and must not be mutated.\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions: FetchOptions = {},\n ): Promise<JsonRpcResponse<Result | null>> {\n const completeFetchOptions = this.#getCompleteFetchOptions(\n jsonRpcRequest,\n fetchOptions,\n );\n return await this.#executeAndProcessRequest<Result>(completeFetchOptions);\n }\n\n /**\n * Constructs a default set of options to `fetch`.\n *\n * If a username and password are present in the URL, they are extracted to an\n * Authorization header.\n *\n * @param endpointUrl - The endpoint URL.\n * @param fetchOptions - The options to `fetch`.\n * @param givenBtoa - An implementation of `btoa`.\n * @returns The default fetch options.\n */\n #getDefaultFetchOptions(\n endpointUrl: URL,\n fetchOptions: FetchOptions,\n givenBtoa: (stringToEncode: string) => string,\n ): FetchOptions {\n if (endpointUrl.username && endpointUrl.password) {\n const authString = `${endpointUrl.username}:${endpointUrl.password}`;\n const encodedCredentials = givenBtoa(authString);\n return deepmerge(fetchOptions, {\n headers: { Authorization: `Basic ${encodedCredentials}` },\n });\n }\n\n return fetchOptions;\n }\n\n /**\n * Constructs a final set of options to pass to `fetch`. Note that the method\n * defaults to `post`, and the JSON-RPC request is automatically JSON-encoded.\n *\n * @param jsonRpcRequest - The JSON-RPC request.\n * @param fetchOptions - Custom `fetch` options.\n * @returns The complete set of `fetch` options.\n */\n #getCompleteFetchOptions<Params extends JsonRpcParams>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions: FetchOptions,\n ): FetchOptions {\n const defaultOptions = {\n method: 'POST',\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n };\n const mergedOptions = deepmerge(\n defaultOptions,\n deepmerge(this.#fetchOptions, fetchOptions),\n );\n\n const { id, jsonrpc, method, params } = jsonRpcRequest;\n const body = JSON.stringify({\n id,\n jsonrpc,\n method,\n params,\n });\n\n return { ...mergedOptions, body };\n }\n\n /**\n * Makes the request using the Cockatiel policy that this service creates.\n *\n * @param fetchOptions - The options for `fetch`; will be combined with the\n * fetch options passed to the constructor\n * @returns The decoded JSON-RPC response from the endpoint.\n * @throws An \"authorized\" JSON-RPC error (code -32006) if the response HTTP status is 401.\n * @throws A \"rate limiting\" JSON-RPC error (code -32005) if the response HTTP status is 429.\n * @throws A \"resource unavailable\" JSON-RPC error (code -32002) if the response HTTP status is 402, 404, or any 5xx.\n * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.\n * @throws A \"parse\" JSON-RPC error (code -32700) if the response is not valid JSON.\n */\n async #executeAndProcessRequest<Result extends Json>(\n fetchOptions: FetchOptions,\n ): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>> {\n let response: Response | undefined;\n try {\n log(\n `[${this.endpointUrl}] Circuit state`,\n this.#policy.getCircuitState(),\n );\n const jsonDecodedResponse = await this.#policy.execute(\n async (context) => {\n log(\n 'REQUEST INITIATED:',\n this.endpointUrl.toString(),\n '::',\n fetchOptions,\n // @ts-expect-error This property _is_ here, the type of\n // ServicePolicy is just wrong.\n `(attempt ${context.attempt + 1})`,\n );\n response = await this.#fetch(this.endpointUrl, fetchOptions);\n if (!response.ok) {\n throw new HttpError(response.status);\n }\n log(\n 'REQUEST SUCCESSFUL:',\n this.endpointUrl.toString(),\n response.status,\n );\n return await response.json();\n },\n );\n this.lastError = undefined;\n return jsonDecodedResponse;\n } catch (error) {\n log('REQUEST ERROR:', this.endpointUrl.toString(), error);\n\n this.lastError =\n error instanceof Error ? error : new Error(getErrorMessage(error));\n\n if (error instanceof HttpError) {\n const status = error.httpStatus;\n if (status === 401) {\n throw new JsonRpcError(\n CUSTOM_RPC_ERRORS.unauthorized,\n 'Unauthorized.',\n {\n httpStatus: status,\n },\n );\n }\n if (status === 429) {\n throw rpcErrors.limitExceeded({\n message: 'Request is being rate limited.',\n data: {\n httpStatus: status,\n },\n });\n }\n if (status >= 500 || status === 402 || status === 404) {\n throw rpcErrors.resourceUnavailable({\n message: 'RPC endpoint not found or unavailable.',\n data: {\n httpStatus: status,\n },\n });\n }\n\n // Handle all other 4xx errors as generic HTTP client errors\n throw new JsonRpcError(\n CUSTOM_RPC_ERRORS.httpClientError,\n 'RPC endpoint returned HTTP client error.',\n {\n httpStatus: status,\n },\n );\n } else if (isJsonParseError(error)) {\n throw rpcErrors.parse({\n message: 'RPC endpoint did not return JSON.',\n });\n } else if (error instanceof BrokenCircuitError) {\n this.#logger?.warn(error);\n const remainingCircuitOpenDuration =\n this.#policy.getRemainingCircuitOpenDuration();\n const formattedRemainingCircuitOpenDuration = Intl.NumberFormat(\n undefined,\n { maximumFractionDigits: 2 },\n ).format(\n (remainingCircuitOpenDuration ?? this.#policy.circuitBreakDuration) /\n Duration.Minute,\n );\n throw rpcErrors.resourceUnavailable({\n message: `RPC endpoint returned too many errors, retrying in ${formattedRemainingCircuitOpenDuration} minutes. Consider using a different RPC endpoint.`,\n });\n }\n throw error;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"rpc-service.mjs","sourceRoot":"","sources":["../../src/rpc-service/rpc-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAIA,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,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,CAAC,CAAC;YACL,CAAC;YACD,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;gBACtD,MAAM,SAAS,CAAC,mBAAmB,CAAC;oBAClC,OAAO,EAAE,wCAAwC;oBACjD,IAAI,EAAE;wBACJ,UAAU,EAAE,MAAM;qBACnB;iBACF,CAAC,CAAC;YACL,CAAC;YAED,4DAA4D;YAC5D,MAAM,IAAI,YAAY,CACpB,iBAAiB,CAAC,eAAe,EACjC,0CAA0C,EAC1C;gBACE,UAAU,EAAE,MAAM;aACnB,CACF,CAAC;QACJ,CAAC;aAAM,IAAI,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,MAAM,SAAS,CAAC,KAAK,CAAC;gBACpB,OAAO,EAAE,mCAAmC;aAC7C,CAAC,CAAC;QACL,CAAC;aAAM,IAAI,KAAK,YAAY,kBAAkB,EAAE,CAAC;YAC/C,uBAAA,IAAI,0BAAQ,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAC1B,MAAM,4BAA4B,GAChC,uBAAA,IAAI,0BAAQ,CAAC,+BAA+B,EAAE,CAAC;YACjD,MAAM,qCAAqC,GAAG,IAAI,CAAC,YAAY,CAC7D,SAAS,EACT,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAC7B,CAAC,MAAM,CACN,CAAC,4BAA4B,IAAI,uBAAA,IAAI,0BAAQ,CAAC,oBAAoB,CAAC;gBACjE,QAAQ,CAAC,MAAM,CAClB,CAAC;YACF,MAAM,SAAS,CAAC,mBAAmB,CAAC;gBAClC,OAAO,EAAE,sDAAsD,qCAAqC,oDAAoD;aACzJ,CAAC,CAAC;QACL,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n BrokenCircuitError,\n CircuitState,\n HttpError,\n createServicePolicy,\n handleWhen,\n} from '@metamask/controller-utils';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport type { JsonRpcRequest } from '@metamask/utils';\nimport {\n Duration,\n getErrorMessage,\n hasProperty,\n type Json,\n type JsonRpcParams,\n type JsonRpcResponse,\n} from '@metamask/utils';\nimport deepmerge from 'deepmerge';\nimport type { Logger } from 'loglevel';\n\nimport type { AbstractRpcService } from './abstract-rpc-service';\nimport type { AddToCockatielEventData, FetchOptions } from './shared';\n\n/**\n * Options for the RpcService constructor.\n */\nexport type RpcServiceOptions = {\n /**\n * A function that can be used to convert a binary string into a\n * base64-encoded ASCII string. Used to encode authorization credentials.\n */\n btoa: typeof btoa;\n /**\n * The URL of the RPC endpoint to hit.\n */\n endpointUrl: URL | string;\n /**\n * An RPC service that represents a failover endpoint which will be invoked\n * while the circuit for _this_ service is open.\n */\n failoverService?: AbstractRpcService;\n /**\n * A function that can be used to make an HTTP request. If your JavaScript\n * environment supports `fetch` natively, you'll probably want to pass that;\n * otherwise you can pass an equivalent (such as `fetch` via `node-fetch`).\n */\n fetch: typeof fetch;\n /**\n * A common set of options that will be used to make every request. Can be\n * overridden on the request level (e.g. to add headers).\n */\n fetchOptions?: FetchOptions;\n /**\n * A `loglevel` logger.\n */\n logger?: Pick<Logger, 'warn'>;\n /**\n * Options to pass to `createServicePolicy`. Note that `retryFilterPolicy` is\n * not accepted, as it is overwritten. See {@link createServicePolicy}.\n */\n policyOptions?: Omit<CreateServicePolicyOptions, 'retryFilterPolicy'>;\n};\n\n/**\n * The maximum number of times that a failing service should be re-run before\n * giving up.\n */\nexport const DEFAULT_MAX_RETRIES = 4;\n\n/**\n * The maximum number of times that the service is allowed to fail before\n * pausing further retries. This is set to a value such that if given a\n * service that continually fails, the policy needs to be executed 3 times\n * before further retries are paused.\n */\nexport const DEFAULT_MAX_CONSECUTIVE_FAILURES = (1 + DEFAULT_MAX_RETRIES) * 3;\n\n/**\n * The list of error messages that represent a failure to connect to the network.\n *\n * This list was derived from Sindre Sorhus's `is-network-error` package:\n * <https://github.com/sindresorhus/is-network-error/blob/7bbfa8be9482ce1427a21fbff60e3ee1650dd091/index.js>\n */\nexport const CONNECTION_ERRORS = [\n // Chrome\n {\n constructorName: 'TypeError',\n pattern: /network error/u,\n },\n // Chrome\n {\n constructorName: 'TypeError',\n pattern: /Failed to fetch/u,\n },\n // Firefox\n {\n constructorName: 'TypeError',\n pattern: /NetworkError when attempting to fetch resource\\./u,\n },\n // Safari 16\n {\n constructorName: 'TypeError',\n pattern: /The Internet connection appears to be offline\\./u,\n },\n // Safari 17+\n {\n constructorName: 'TypeError',\n pattern: /Load failed/u,\n },\n // `cross-fetch`\n {\n constructorName: 'TypeError',\n pattern: /Network request failed/u,\n },\n // `node-fetch`\n {\n constructorName: 'FetchError',\n pattern: /request to (.+) failed/u,\n },\n // Undici (Node.js)\n {\n constructorName: 'TypeError',\n pattern: /fetch failed/u,\n },\n // Undici (Node.js)\n {\n constructorName: 'TypeError',\n pattern: /terminated/u,\n },\n];\n\n/**\n * Custom JSON-RPC error codes for specific cases.\n *\n * These should be moved to `@metamask/rpc-errors` eventually.\n */\nexport const CUSTOM_RPC_ERRORS = {\n unauthorized: -32006,\n httpClientError: -32080,\n} as const;\n\n/**\n * Determines whether the given error represents a failure to reach the network\n * after request parameters have been validated.\n *\n * This is somewhat difficult to verify because JavaScript engines (and in\n * some cases libraries) produce slightly different error messages for this\n * particular scenario, and we need to account for this.\n *\n * @param error - The error.\n * @returns True if the error indicates that the network cannot be connected to,\n * and false otherwise.\n */\nexport function isConnectionError(error: unknown) {\n if (!(typeof error === 'object' && error !== null && 'message' in error)) {\n return false;\n }\n\n const { message } = error;\n\n return (\n typeof message === 'string' &&\n !isNockError(message) &&\n CONNECTION_ERRORS.some(({ constructorName, pattern }) => {\n return (\n error.constructor.name === constructorName && pattern.test(message)\n );\n })\n );\n}\n\n/**\n * Determines whether the given error message refers to a Nock error.\n *\n * It's important that if we failed to mock a request in a test, the resulting\n * error does not cause the request to be retried so that we can see it right\n * away.\n *\n * @param message - The error message to test.\n * @returns True if the message indicates a missing Nock mock, false otherwise.\n */\nfunction isNockError(message: string) {\n return message.includes('Nock:');\n}\n\n/**\n * Determine whether the given error message indicates a failure to parse JSON.\n *\n * This is different in tests vs. implementation code because it may manifest as\n * a FetchError or a SyntaxError.\n *\n * @param error - The error object to test.\n * @returns True if the error indicates a JSON parse error, false otherwise.\n */\nfunction isJsonParseError(error: unknown) {\n return (\n error instanceof SyntaxError ||\n /invalid json/iu.test(getErrorMessage(error))\n );\n}\n\n/**\n * Guarantees a URL, even given a string. This is useful for checking components\n * of that URL.\n *\n * @param endpointUrlOrUrlString - Either a URL object or a string that\n * represents the URL of an endpoint.\n * @returns A URL object.\n */\nfunction getNormalizedEndpointUrl(endpointUrlOrUrlString: URL | string): URL {\n return endpointUrlOrUrlString instanceof URL\n ? endpointUrlOrUrlString\n : new URL(endpointUrlOrUrlString);\n}\n\n/**\n * Strips username and password from a URL.\n *\n * @param url - The URL to strip credentials from.\n * @returns A new URL object with credentials removed.\n */\nfunction stripCredentialsFromUrl(url: URL): URL {\n const strippedUrl = new URL(url.toString());\n strippedUrl.username = '';\n strippedUrl.password = '';\n return strippedUrl;\n}\n\n/**\n * This class is responsible for making a request to an endpoint that implements\n * the JSON-RPC protocol. It is designed to gracefully handle network and server\n * failures, retrying requests using exponential backoff. It also offers a hook\n * which can used to respond to slow requests.\n */\nexport class RpcService implements AbstractRpcService {\n /**\n * The function used to make an HTTP request.\n */\n readonly #fetch: typeof fetch;\n\n /**\n * The URL of the RPC endpoint.\n */\n readonly endpointUrl: URL;\n\n /**\n * A common set of options that the request options will extend.\n */\n readonly #fetchOptions: FetchOptions;\n\n /**\n * An RPC service that represents a failover endpoint which will be invoked\n * while the circuit for _this_ service is open.\n */\n readonly #failoverService: RpcServiceOptions['failoverService'];\n\n /**\n * A `loglevel` logger.\n */\n readonly #logger: RpcServiceOptions['logger'];\n\n /**\n * The policy that wraps the request.\n */\n readonly #policy: ServicePolicy;\n\n /**\n * Constructs a new RpcService object.\n *\n * @param options - The options. See {@link RpcServiceOptions}.\n */\n constructor(options: RpcServiceOptions) {\n const {\n btoa: givenBtoa,\n endpointUrl,\n failoverService,\n fetch: givenFetch,\n logger,\n fetchOptions = {},\n policyOptions = {},\n } = options;\n\n this.#fetch = givenFetch;\n const normalizedUrl = getNormalizedEndpointUrl(endpointUrl);\n this.#fetchOptions = this.#getDefaultFetchOptions(\n normalizedUrl,\n fetchOptions,\n givenBtoa,\n );\n this.endpointUrl = stripCredentialsFromUrl(normalizedUrl);\n this.#failoverService = failoverService;\n this.#logger = logger;\n\n const policy = createServicePolicy({\n maxRetries: DEFAULT_MAX_RETRIES,\n maxConsecutiveFailures: DEFAULT_MAX_CONSECUTIVE_FAILURES,\n ...policyOptions,\n retryFilterPolicy: handleWhen((error) => {\n return (\n // Ignore errors where the request failed to establish\n isConnectionError(error) ||\n // Ignore server sent HTML error pages or truncated JSON responses\n isJsonParseError(error) ||\n // Ignore server overload errors\n ('httpStatus' in error &&\n (error.httpStatus === 502 ||\n error.httpStatus === 503 ||\n error.httpStatus === 504)) ||\n (hasProperty(error, 'code') &&\n (error.code === 'ETIMEDOUT' || error.code === 'ECONNRESET'))\n );\n }),\n });\n this.#policy = policy;\n }\n\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 ) {\n return this.#policy.onRetry((data) => {\n listener({ ...data, endpointUrl: this.endpointUrl.toString() });\n });\n }\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; failoverEndpointUrl?: string }\n >,\n ) {\n return this.#policy.onBreak((data) => {\n listener({\n ...data,\n endpointUrl: this.endpointUrl.toString(),\n failoverEndpointUrl: this.#failoverService\n ? this.#failoverService.endpointUrl.toString()\n : undefined,\n });\n });\n }\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 ) {\n return this.#policy.onDegraded((data) => {\n listener({ ...(data ?? {}), endpointUrl: this.endpointUrl.toString() });\n });\n }\n\n /**\n * Makes a request to the RPC endpoint. If the circuit is open because this\n * request has failed too many times, the request is forwarded to a failover\n * service (if provided).\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 An \"authorized\" JSON-RPC error (code -32006) if the response HTTP status is 401.\n * @throws A \"rate limiting\" JSON-RPC error (code -32005) if the response HTTP status is 429.\n * @throws A \"resource unavailable\" JSON-RPC error (code -32002) if the response HTTP status is 402, 404, or any 5xx.\n * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.\n * @throws A \"parse\" JSON-RPC error (code -32700) if the response is not valid JSON.\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 RPC endpoint. If the circuit is open because this\n * request has failed too many times, the request is forwarded to a failover\n * service (if provided).\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 An \"authorized\" JSON-RPC error (code -32006) if the response HTTP status is 401.\n * @throws A \"rate limiting\" JSON-RPC error (code -32005) if the response HTTP status is 429.\n * @throws A \"resource unavailable\" JSON-RPC error (code -32002) if the response HTTP status is 402, 404, or any 5xx.\n * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.\n * @throws A \"parse\" JSON-RPC error (code -32700) if the response is not valid JSON.\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 // The request object may be frozen and must not be mutated.\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions: FetchOptions = {},\n ): Promise<JsonRpcResponse<Result | null>> {\n const completeFetchOptions = this.#getCompleteFetchOptions(\n jsonRpcRequest,\n fetchOptions,\n );\n\n try {\n return await this.#processRequest<Result>(completeFetchOptions);\n } catch (error) {\n if (\n this.#policy.circuitBreakerPolicy.state === CircuitState.Open &&\n this.#failoverService !== undefined\n ) {\n return await this.#failoverService.request(\n jsonRpcRequest,\n completeFetchOptions,\n );\n }\n throw error;\n }\n }\n\n /**\n * Constructs a default set of options to `fetch`.\n *\n * If a username and password are present in the URL, they are extracted to an\n * Authorization header.\n *\n * @param endpointUrl - The endpoint URL.\n * @param fetchOptions - The options to `fetch`.\n * @param givenBtoa - An implementation of `btoa`.\n * @returns The default fetch options.\n */\n #getDefaultFetchOptions(\n endpointUrl: URL,\n fetchOptions: FetchOptions,\n givenBtoa: (stringToEncode: string) => string,\n ): FetchOptions {\n if (endpointUrl.username && endpointUrl.password) {\n const authString = `${endpointUrl.username}:${endpointUrl.password}`;\n const encodedCredentials = givenBtoa(authString);\n return deepmerge(fetchOptions, {\n headers: { Authorization: `Basic ${encodedCredentials}` },\n });\n }\n\n return fetchOptions;\n }\n\n /**\n * Constructs a final set of options to pass to `fetch`. Note that the method\n * defaults to `post`, and the JSON-RPC request is automatically JSON-encoded.\n *\n * @param jsonRpcRequest - The JSON-RPC request.\n * @param fetchOptions - Custom `fetch` options.\n * @returns The complete set of `fetch` options.\n */\n #getCompleteFetchOptions<Params extends JsonRpcParams>(\n jsonRpcRequest: Readonly<JsonRpcRequest<Params>>,\n fetchOptions: FetchOptions,\n ): FetchOptions {\n const defaultOptions = {\n method: 'POST',\n headers: {\n Accept: 'application/json',\n 'Content-Type': 'application/json',\n },\n };\n const mergedOptions = deepmerge(\n defaultOptions,\n deepmerge(this.#fetchOptions, fetchOptions),\n );\n\n const { id, jsonrpc, method, params } = jsonRpcRequest;\n const body = JSON.stringify({\n id,\n jsonrpc,\n method,\n params,\n });\n\n return { ...mergedOptions, body };\n }\n\n /**\n * Makes the request using the Cockatiel policy that this service creates.\n *\n * @param fetchOptions - The options for `fetch`; will be combined with the\n * fetch options passed to the constructor\n * @returns The decoded JSON-RPC response from the endpoint.\n * @throws An \"authorized\" JSON-RPC error (code -32006) if the response HTTP status is 401.\n * @throws A \"rate limiting\" JSON-RPC error (code -32005) if the response HTTP status is 429.\n * @throws A \"resource unavailable\" JSON-RPC error (code -32002) if the response HTTP status is 402, 404, or any 5xx.\n * @throws A generic HTTP client JSON-RPC error (code -32050) for any other 4xx HTTP status codes.\n * @throws A \"parse\" JSON-RPC error (code -32700) if the response is not valid JSON.\n */\n async #processRequest<Result extends Json>(\n fetchOptions: FetchOptions,\n ): Promise<JsonRpcResponse<Result> | JsonRpcResponse<null>> {\n let response: Response | undefined;\n try {\n return await this.#policy.execute(async () => {\n response = await this.#fetch(this.endpointUrl, fetchOptions);\n if (!response.ok) {\n throw new HttpError(response.status);\n }\n return await response.json();\n });\n } catch (error) {\n if (error instanceof HttpError) {\n const status = error.httpStatus;\n if (status === 401) {\n throw new JsonRpcError(\n CUSTOM_RPC_ERRORS.unauthorized,\n 'Unauthorized.',\n {\n httpStatus: status,\n },\n );\n }\n if (status === 429) {\n throw rpcErrors.limitExceeded({\n message: 'Request is being rate limited.',\n data: {\n httpStatus: status,\n },\n });\n }\n if (status >= 500 || status === 402 || status === 404) {\n throw rpcErrors.resourceUnavailable({\n message: 'RPC endpoint not found or unavailable.',\n data: {\n httpStatus: status,\n },\n });\n }\n\n // Handle all other 4xx errors as generic HTTP client errors\n throw new JsonRpcError(\n CUSTOM_RPC_ERRORS.httpClientError,\n 'RPC endpoint returned HTTP client error.',\n {\n httpStatus: status,\n },\n );\n } else if (isJsonParseError(error)) {\n throw rpcErrors.parse({\n message: 'RPC endpoint did not return JSON.',\n });\n } else if (error instanceof BrokenCircuitError) {\n this.#logger?.warn(error);\n const remainingCircuitOpenDuration =\n this.#policy.getRemainingCircuitOpenDuration();\n const formattedRemainingCircuitOpenDuration = Intl.NumberFormat(\n undefined,\n { maximumFractionDigits: 2 },\n ).format(\n (remainingCircuitOpenDuration ?? this.#policy.circuitBreakDuration) /\n Duration.Minute,\n );\n throw rpcErrors.resourceUnavailable({\n message: `RPC endpoint returned too many errors, retrying in ${formattedRemainingCircuitOpenDuration} minutes. Consider using a different RPC endpoint.`,\n });\n }\n throw error;\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"shared.cjs","sourceRoot":"","sources":["../../src/rpc-service/shared.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n CockatielEvent,\n CockatielEventEmitter,\n} from '@metamask/controller-utils';\n\n/**\n * Equivalent to the built-in `FetchOptions` type, but renamed for clarity.\n */\nexport type FetchOptions = RequestInit;\n\n/**\n * Converts a Cockatiel event type to an event emitter type.\n */\nexport type CockatielEventToEventEmitter<Event> =\n Event extends CockatielEvent<infer EventData>\n ? CockatielEventEmitter<EventData>\n : never;\n\n/**\n * Obtains the event data type from a Cockatiel event or event listener type.\n */\nexport type ExtractCockatielEventData<CockatielEventOrEventListener> =\n CockatielEventOrEventListener extends CockatielEvent<infer Data>\n ? Data\n : CockatielEventOrEventListener extends (data: infer Data) => void\n ? Data\n : never;\n\n/**\n * Extends the data that a Cockatiel event listener is called with additional\n * data.\n */\nexport type ExtendCockatielEventData<OriginalData, AdditionalData> =\n OriginalData extends void ? AdditionalData : OriginalData & AdditionalData;\n\n/**\n * Removes keys from the data that a Cockatiel event listner is called with.\n */\nexport type ExcludeCockatielEventData<\n OriginalData,\n Keys extends PropertyKey,\n> = OriginalData extends void ? void : Omit<OriginalData, Keys>;\n\n/**\n * Converts a Cockatiel event type to an event listener type, but adding the\n * requested data.\n */\nexport type CockatielEventToEventListenerWithData<Event, Data> = (\n data: ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>,\n) => void;\n\n/**\n * Converts a Cockatiel event listener type to an event emitter type.\n */\nexport type CockatielEventToEventEmitterWithData<Event, Data> =\n CockatielEventEmitter<\n ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>\n >;\n"]}
1
+ {"version":3,"file":"shared.cjs","sourceRoot":"","sources":["../../src/rpc-service/shared.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Equivalent to the built-in `FetchOptions` type, but renamed for clarity.\n */\nexport type FetchOptions = RequestInit;\n\n/**\n * Extends an event listener that Cockatiel uses so that when it is called, more\n * data can be supplied in the event object.\n */\nexport type AddToCockatielEventData<EventListener, AdditionalData> =\n EventListener extends (data: infer Data) => void\n ? (data: Data extends void ? AdditionalData : Data & AdditionalData) => void\n : never;\n"]}
@@ -1,32 +1,10 @@
1
- import type { CockatielEvent, CockatielEventEmitter } from "@metamask/controller-utils";
2
1
  /**
3
2
  * Equivalent to the built-in `FetchOptions` type, but renamed for clarity.
4
3
  */
5
4
  export type FetchOptions = RequestInit;
6
5
  /**
7
- * Converts a Cockatiel event type to an event emitter type.
6
+ * Extends an event listener that Cockatiel uses so that when it is called, more
7
+ * data can be supplied in the event object.
8
8
  */
9
- export type CockatielEventToEventEmitter<Event> = Event extends CockatielEvent<infer EventData> ? CockatielEventEmitter<EventData> : never;
10
- /**
11
- * Obtains the event data type from a Cockatiel event or event listener type.
12
- */
13
- export type ExtractCockatielEventData<CockatielEventOrEventListener> = CockatielEventOrEventListener extends CockatielEvent<infer Data> ? Data : CockatielEventOrEventListener extends (data: infer Data) => void ? Data : never;
14
- /**
15
- * Extends the data that a Cockatiel event listener is called with additional
16
- * data.
17
- */
18
- export type ExtendCockatielEventData<OriginalData, AdditionalData> = OriginalData extends void ? AdditionalData : OriginalData & AdditionalData;
19
- /**
20
- * Removes keys from the data that a Cockatiel event listner is called with.
21
- */
22
- export type ExcludeCockatielEventData<OriginalData, Keys extends PropertyKey> = OriginalData extends void ? void : Omit<OriginalData, Keys>;
23
- /**
24
- * Converts a Cockatiel event type to an event listener type, but adding the
25
- * requested data.
26
- */
27
- export type CockatielEventToEventListenerWithData<Event, Data> = (data: ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>) => void;
28
- /**
29
- * Converts a Cockatiel event listener type to an event emitter type.
30
- */
31
- export type CockatielEventToEventEmitterWithData<Event, Data> = CockatielEventEmitter<ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>>;
9
+ export type AddToCockatielEventData<EventListener, AdditionalData> = EventListener extends (data: infer Data) => void ? (data: Data extends void ? AdditionalData : Data & AdditionalData) => void : never;
32
10
  //# sourceMappingURL=shared.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared.d.cts","sourceRoot":"","sources":["../../src/rpc-service/shared.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,qBAAqB,EACtB,mCAAmC;AAEpC;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC;AAEvC;;GAEG;AACH,MAAM,MAAM,4BAA4B,CAAC,KAAK,IAC5C,KAAK,SAAS,cAAc,CAAC,MAAM,SAAS,CAAC,GACzC,qBAAqB,CAAC,SAA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 +1,10 @@
1
- import type { CockatielEvent, CockatielEventEmitter } from "@metamask/controller-utils";
2
1
  /**
3
2
  * Equivalent to the built-in `FetchOptions` type, but renamed for clarity.
4
3
  */
5
4
  export type FetchOptions = RequestInit;
6
5
  /**
7
- * Converts a Cockatiel event type to an event emitter type.
6
+ * Extends an event listener that Cockatiel uses so that when it is called, more
7
+ * data can be supplied in the event object.
8
8
  */
9
- export type CockatielEventToEventEmitter<Event> = Event extends CockatielEvent<infer EventData> ? CockatielEventEmitter<EventData> : never;
10
- /**
11
- * Obtains the event data type from a Cockatiel event or event listener type.
12
- */
13
- export type ExtractCockatielEventData<CockatielEventOrEventListener> = CockatielEventOrEventListener extends CockatielEvent<infer Data> ? Data : CockatielEventOrEventListener extends (data: infer Data) => void ? Data : never;
14
- /**
15
- * Extends the data that a Cockatiel event listener is called with additional
16
- * data.
17
- */
18
- export type ExtendCockatielEventData<OriginalData, AdditionalData> = OriginalData extends void ? AdditionalData : OriginalData & AdditionalData;
19
- /**
20
- * Removes keys from the data that a Cockatiel event listner is called with.
21
- */
22
- export type ExcludeCockatielEventData<OriginalData, Keys extends PropertyKey> = OriginalData extends void ? void : Omit<OriginalData, Keys>;
23
- /**
24
- * Converts a Cockatiel event type to an event listener type, but adding the
25
- * requested data.
26
- */
27
- export type CockatielEventToEventListenerWithData<Event, Data> = (data: ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>) => void;
28
- /**
29
- * Converts a Cockatiel event listener type to an event emitter type.
30
- */
31
- export type CockatielEventToEventEmitterWithData<Event, Data> = CockatielEventEmitter<ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>>;
9
+ export type AddToCockatielEventData<EventListener, AdditionalData> = EventListener extends (data: infer Data) => void ? (data: Data extends void ? AdditionalData : Data & AdditionalData) => void : never;
32
10
  //# sourceMappingURL=shared.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared.d.mts","sourceRoot":"","sources":["../../src/rpc-service/shared.ts"],"names":[],"mappings":"AAA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;AACH,MAAM,MAAM,uBAAuB,CAAC,aAAa,EAAE,cAAc,IAC/D,aAAa,SAAS,CAAC,IAAI,EAAE,MAAM,IAAI,KAAK,IAAI,GAC5C,CAAC,IAAI,EAAE,IAAI,SAAS,IAAI,GAAG,cAAc,GAAG,IAAI,GAAG,cAAc,KAAK,IAAI,GAC1E,KAAK,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"shared.mjs","sourceRoot":"","sources":["../../src/rpc-service/shared.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n CockatielEvent,\n CockatielEventEmitter,\n} from '@metamask/controller-utils';\n\n/**\n * Equivalent to the built-in `FetchOptions` type, but renamed for clarity.\n */\nexport type FetchOptions = RequestInit;\n\n/**\n * Converts a Cockatiel event type to an event emitter type.\n */\nexport type CockatielEventToEventEmitter<Event> =\n Event extends CockatielEvent<infer EventData>\n ? CockatielEventEmitter<EventData>\n : never;\n\n/**\n * Obtains the event data type from a Cockatiel event or event listener type.\n */\nexport type ExtractCockatielEventData<CockatielEventOrEventListener> =\n CockatielEventOrEventListener extends CockatielEvent<infer Data>\n ? Data\n : CockatielEventOrEventListener extends (data: infer Data) => void\n ? Data\n : never;\n\n/**\n * Extends the data that a Cockatiel event listener is called with additional\n * data.\n */\nexport type ExtendCockatielEventData<OriginalData, AdditionalData> =\n OriginalData extends void ? AdditionalData : OriginalData & AdditionalData;\n\n/**\n * Removes keys from the data that a Cockatiel event listner is called with.\n */\nexport type ExcludeCockatielEventData<\n OriginalData,\n Keys extends PropertyKey,\n> = OriginalData extends void ? void : Omit<OriginalData, Keys>;\n\n/**\n * Converts a Cockatiel event type to an event listener type, but adding the\n * requested data.\n */\nexport type CockatielEventToEventListenerWithData<Event, Data> = (\n data: ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>,\n) => void;\n\n/**\n * Converts a Cockatiel event listener type to an event emitter type.\n */\nexport type CockatielEventToEventEmitterWithData<Event, Data> =\n CockatielEventEmitter<\n ExtendCockatielEventData<ExtractCockatielEventData<Event>, Data>\n >;\n"]}
1
+ {"version":3,"file":"shared.mjs","sourceRoot":"","sources":["../../src/rpc-service/shared.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Equivalent to the built-in `FetchOptions` type, but renamed for clarity.\n */\nexport type FetchOptions = RequestInit;\n\n/**\n * Extends an event listener that Cockatiel uses so that when it is called, more\n * data can be supplied in the event object.\n */\nexport type AddToCockatielEventData<EventListener, AdditionalData> =\n EventListener extends (data: infer Data) => void\n ? (data: Data extends void ? AdditionalData : Data & AdditionalData) => void\n : never;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/network-controller",
3
- "version": "26.0.0-preview-d6fe4594",
3
+ "version": "26.0.0-preview-bc80feb8",
4
4
  "description": "Provides an interface to the currently selected network via a MetaMask-compatible provider object",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -78,7 +78,6 @@
78
78
  "@types/jest-when": "^2.7.3",
79
79
  "@types/lodash": "^4.14.191",
80
80
  "@types/node-fetch": "^2.6.12",
81
- "cockatiel": "^3.1.2",
82
81
  "deep-freeze-strict": "^1.1.1",
83
82
  "deepmerge": "^4.2.2",
84
83
  "jest": "^27.5.1",