@metamask-previews/eth-json-rpc-provider 6.0.1-preview-8bfa290fb → 7.0.0-preview-0a30e47
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 +16 -3
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/{internal-provider.d.cts → internal-provider.d.ts} +5 -5
- package/dist/internal-provider.d.ts.map +1 -0
- package/dist/internal-provider.js +120 -0
- package/dist/internal-provider.js.map +1 -0
- package/dist/mock-internal-provider.d.ts +103 -0
- package/dist/mock-internal-provider.d.ts.map +1 -0
- package/dist/mock-internal-provider.js +157 -0
- package/dist/mock-internal-provider.js.map +1 -0
- package/dist/{provider-from-middleware.d.cts → provider-from-middleware.d.ts} +5 -5
- package/dist/provider-from-middleware.d.ts.map +1 -0
- package/dist/{provider-from-middleware.mjs → provider-from-middleware.js} +4 -4
- package/dist/provider-from-middleware.js.map +1 -0
- package/package.json +15 -17
- package/dist/index.cjs +0 -23
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -9
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts +0 -9
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -5
- package/dist/index.mjs.map +0 -1
- package/dist/internal-provider.cjs +0 -137
- package/dist/internal-provider.cjs.map +0 -1
- package/dist/internal-provider.d.cts.map +0 -1
- package/dist/internal-provider.d.mts +0 -74
- package/dist/internal-provider.d.mts.map +0 -1
- package/dist/internal-provider.mjs +0 -132
- package/dist/internal-provider.mjs.map +0 -1
- package/dist/provider-from-middleware.cjs +0 -40
- package/dist/provider-from-middleware.cjs.map +0 -1
- package/dist/provider-from-middleware.d.cts.map +0 -1
- package/dist/provider-from-middleware.d.mts +0 -20
- package/dist/provider-from-middleware.d.mts.map +0 -1
- package/dist/provider-from-middleware.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,10 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [7.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add `MockInternalProvider`, which can be substituted for `InternalProvider` in tests, while providing the ability to assert requests and inject canned responses ([#10099](https://github.com/MetaMask/core/pull/10099))
|
|
15
|
+
- Also add `MockInternalProviderStub` type
|
|
16
|
+
|
|
10
17
|
### Changed
|
|
11
18
|
|
|
19
|
+
- **BREAKING:** Drop CommonJS support ([#9536](https://github.com/MetaMask/core/pull/9536))
|
|
20
|
+
- This package is now ESM-only, but can still be used in CommonJS projects via `require(esm)` in modern Node.js versions (22+), or dynamic imports in older Node.js versions.
|
|
21
|
+
- **BREAKING:** Bump minimum Node.js version to 22 ([#9976](https://github.com/MetaMask/core/pull/9976))
|
|
22
|
+
- **BREAKING:** Bump TypeScript target to ES2022 ([#10019](https://github.com/MetaMask/core/pull/10019))
|
|
23
|
+
- This package now ships ES2022 code, requiring a compatible modern environment or bundler configuration to consume.
|
|
12
24
|
- Bump `@metamask/utils` from `^11.9.0` to `^11.12.0` ([#9074](https://github.com/MetaMask/core/pull/9074), [#10076](https://github.com/MetaMask/core/pull/10076))
|
|
13
|
-
- Bump `@metamask/json-rpc-engine` from `^10.2.4` to `^
|
|
25
|
+
- Bump `@metamask/json-rpc-engine` from `^10.2.4` to `^11.0.0` ([#8661](https://github.com/MetaMask/core/pull/8661), [#8746](https://github.com/MetaMask/core/pull/8746), [#8753](https://github.com/MetaMask/core/pull/8753), [#10160](https://github.com/MetaMask/core/pull/10160))
|
|
14
26
|
|
|
15
27
|
## [6.0.1]
|
|
16
28
|
|
|
@@ -104,7 +116,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
104
116
|
["Are the Types Wrong?"](https://arethetypeswrong.github.io/) tool as
|
|
105
117
|
["masquerading as CJS"](https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseCJS.md).
|
|
106
118
|
All of the ATTW checks now pass.
|
|
107
|
-
- Remove chunk files ([#4648](https://github.com/MetaMask/core/pull/4648))
|
|
119
|
+
- Remove chunk files. ([#4648](https://github.com/MetaMask/core/pull/4648))
|
|
108
120
|
- Previously, the build tool we used to generate JavaScript files extracted
|
|
109
121
|
common code to "chunk" files. While this was intended to make this package
|
|
110
122
|
more tree-shakeable, it also made debugging more difficult for our
|
|
@@ -247,7 +259,8 @@ Release `v2.0.0` is identical to `v1.0.1` aside from Node.js version requirement
|
|
|
247
259
|
|
|
248
260
|
- Initial release, including `providerFromEngine` and `providerFromMiddleware`.
|
|
249
261
|
|
|
250
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/eth-json-rpc-provider@
|
|
262
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/eth-json-rpc-provider@7.0.0...HEAD
|
|
263
|
+
[7.0.0]: https://github.com/MetaMask/core/compare/@metamask/eth-json-rpc-provider@6.0.1...@metamask/eth-json-rpc-provider@7.0.0
|
|
251
264
|
[6.0.1]: https://github.com/MetaMask/core/compare/@metamask/eth-json-rpc-provider@6.0.0...@metamask/eth-json-rpc-provider@6.0.1
|
|
252
265
|
[6.0.0]: https://github.com/MetaMask/core/compare/@metamask/eth-json-rpc-provider@5.0.1...@metamask/eth-json-rpc-provider@6.0.0
|
|
253
266
|
[5.0.1]: https://github.com/MetaMask/core/compare/@metamask/eth-json-rpc-provider@5.0.0...@metamask/eth-json-rpc-provider@5.0.1
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InternalProvider } from './internal-provider.js';
|
|
2
|
+
export { MockInternalProvider } from './mock-internal-provider.js';
|
|
3
|
+
export type { MockInternalProviderStub } from './mock-internal-provider.js';
|
|
4
|
+
export * from './provider-from-middleware.js';
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated Use {@link InternalProvider} instead.
|
|
7
|
+
*/
|
|
8
|
+
type SafeEventEmitterProvider = InternalProvider;
|
|
9
|
+
declare const SafeEventEmitterProvider: typeof InternalProvider;
|
|
10
|
+
export { InternalProvider, SafeEventEmitterProvider };
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,YAAY,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAE5E,cAAc,+BAA+B,CAAC;AAE9C;;GAEG;AACH,KAAK,wBAAwB,GAAG,gBAAgB,CAAC;AACjD,QAAA,MAAM,wBAAwB,yBAAmB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { InternalProvider } from './internal-provider.js';
|
|
2
|
+
export { MockInternalProvider } from './mock-internal-provider.js';
|
|
3
|
+
export * from './provider-from-middleware.js';
|
|
4
|
+
const SafeEventEmitterProvider = InternalProvider;
|
|
5
|
+
export { InternalProvider, SafeEventEmitterProvider };
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAGnE,cAAc,+BAA+B,CAAC;AAM9C,MAAM,wBAAwB,GAAG,gBAAgB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,CAAC","sourcesContent":["import { InternalProvider } from './internal-provider.js';\n\nexport { MockInternalProvider } from './mock-internal-provider.js';\nexport type { MockInternalProviderStub } from './mock-internal-provider.js';\n\nexport * from './provider-from-middleware.js';\n\n/**\n * @deprecated Use {@link InternalProvider} instead.\n */\ntype SafeEventEmitterProvider = InternalProvider;\nconst SafeEventEmitterProvider = InternalProvider;\n\nexport { InternalProvider, SafeEventEmitterProvider };\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { JsonRpcEngine } from
|
|
2
|
-
import { JsonRpcEngineV2 } from
|
|
3
|
-
import type { HandleOptions, ContextConstraint, MiddlewareContext } from
|
|
4
|
-
import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from
|
|
1
|
+
import type { JsonRpcEngine } from '@metamask/json-rpc-engine';
|
|
2
|
+
import { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';
|
|
3
|
+
import type { HandleOptions, ContextConstraint, MiddlewareContext } from '@metamask/json-rpc-engine/v2';
|
|
4
|
+
import type { Json, JsonRpcId, JsonRpcParams, JsonRpcRequest, JsonRpcVersion2 } from '@metamask/utils';
|
|
5
5
|
/**
|
|
6
6
|
* A JSON-RPC request conforming to the EIP-1193 specification.
|
|
7
7
|
*/
|
|
@@ -71,4 +71,4 @@ export declare class InternalProvider<Context extends ContextConstraint = Middle
|
|
|
71
71
|
*/
|
|
72
72
|
export declare function convertEip1193RequestToJsonRpcRequest(eip1193Request: Eip1193Request): JsonRpcRequest;
|
|
73
73
|
export {};
|
|
74
|
-
//# sourceMappingURL=internal-provider.d.
|
|
74
|
+
//# sourceMappingURL=internal-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-provider.d.ts","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;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,MAAM,CAAC,YAG5B,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,GAAG,KAAK,IAAI,KACpD,IAAI,CAOL;CAkCH;AAED;;;;;GAKG;AACH,wBAAgB,qCAAqC,CACnD,cAAc,EAAE,cAAc,GAC7B,cAAc,CAehB"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { asV2Middleware } from '@metamask/json-rpc-engine';
|
|
2
|
+
import { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
/**
|
|
5
|
+
* An Ethereum provider.
|
|
6
|
+
*
|
|
7
|
+
* This provider loosely follows conventions that pre-date EIP-1193.
|
|
8
|
+
* It is not compliant with any Ethereum provider standard.
|
|
9
|
+
*/
|
|
10
|
+
export class InternalProvider {
|
|
11
|
+
#engine;
|
|
12
|
+
/**
|
|
13
|
+
* Construct a InternalProvider from a JSON-RPC server or legacy engine.
|
|
14
|
+
*
|
|
15
|
+
* @param options - Options.
|
|
16
|
+
* @param options.engine - The JSON-RPC engine used to process requests.
|
|
17
|
+
*/
|
|
18
|
+
constructor({ engine }) {
|
|
19
|
+
this.#engine =
|
|
20
|
+
'push' in engine
|
|
21
|
+
? JsonRpcEngineV2.create({
|
|
22
|
+
middleware: [asV2Middleware(engine)],
|
|
23
|
+
})
|
|
24
|
+
: engine;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Send a provider request asynchronously.
|
|
28
|
+
*
|
|
29
|
+
* @param eip1193Request - The request to send.
|
|
30
|
+
* @param options - The options for the request operation.
|
|
31
|
+
* @param options.context - The context to include with the request.
|
|
32
|
+
* @returns The JSON-RPC response.
|
|
33
|
+
*/
|
|
34
|
+
async request(eip1193Request, options) {
|
|
35
|
+
const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
|
|
36
|
+
return (await this.#handle(jsonRpcRequest, options)).result;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Send a provider request asynchronously.
|
|
40
|
+
*
|
|
41
|
+
* This method serves the same purpose as `request`. It only exists for
|
|
42
|
+
* legacy reasons.
|
|
43
|
+
*
|
|
44
|
+
* @param eip1193Request - The request to send.
|
|
45
|
+
* @param callback - A function that is called upon the success or failure of the request.
|
|
46
|
+
* @deprecated Use {@link request} instead. This method is retained solely for backwards
|
|
47
|
+
* compatibility with certain libraries.
|
|
48
|
+
*/
|
|
49
|
+
sendAsync = (eip1193Request,
|
|
50
|
+
// Non-polluting `any` that acts like a constraint.
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
callback) => {
|
|
53
|
+
const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
|
|
54
|
+
this.#handleWithCallback(jsonRpcRequest, callback);
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Send a provider request asynchronously.
|
|
58
|
+
*
|
|
59
|
+
* This method serves the same purpose as `request`. It only exists for
|
|
60
|
+
* legacy reasons.
|
|
61
|
+
*
|
|
62
|
+
* @param eip1193Request - The request to send.
|
|
63
|
+
* @param callback - A function that is called upon the success or failure of the request.
|
|
64
|
+
* @deprecated Use {@link request} instead. This method is retained solely for backwards
|
|
65
|
+
* compatibility with certain libraries.
|
|
66
|
+
*/
|
|
67
|
+
send = (eip1193Request,
|
|
68
|
+
// TODO: Replace `any` with type
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
|
+
callback) => {
|
|
71
|
+
if (typeof callback !== 'function') {
|
|
72
|
+
throw new Error('Must provide callback to "send" method.');
|
|
73
|
+
}
|
|
74
|
+
const jsonRpcRequest = convertEip1193RequestToJsonRpcRequest(eip1193Request);
|
|
75
|
+
this.#handleWithCallback(jsonRpcRequest, callback);
|
|
76
|
+
};
|
|
77
|
+
#handle = async (jsonRpcRequest, options) => {
|
|
78
|
+
const { id, jsonrpc } = jsonRpcRequest;
|
|
79
|
+
// The `result` typecast is unsafe, but we need it to preserve the provider's
|
|
80
|
+
// public interface, which allows you to unsafely typecast results.
|
|
81
|
+
const result = (await this.#engine.handle(jsonRpcRequest, options));
|
|
82
|
+
return {
|
|
83
|
+
id,
|
|
84
|
+
jsonrpc,
|
|
85
|
+
result,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
#handleWithCallback = (jsonRpcRequest, callback) => {
|
|
89
|
+
/* eslint-disable promise/no-callback-in-promise */
|
|
90
|
+
this.#handle(jsonRpcRequest)
|
|
91
|
+
// A resolution will always be a successful response
|
|
92
|
+
.then((response) => callback(null, response))
|
|
93
|
+
.catch((error) => {
|
|
94
|
+
callback(error);
|
|
95
|
+
});
|
|
96
|
+
/* eslint-enable promise/no-callback-in-promise */
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Convert an EIP-1193 request to a JSON-RPC request.
|
|
101
|
+
*
|
|
102
|
+
* @param eip1193Request - The EIP-1193 request to convert.
|
|
103
|
+
* @returns The JSON-RPC request.
|
|
104
|
+
*/
|
|
105
|
+
export function convertEip1193RequestToJsonRpcRequest(eip1193Request) {
|
|
106
|
+
const { id = nanoid(), jsonrpc = '2.0', method, params } = eip1193Request;
|
|
107
|
+
return params
|
|
108
|
+
? {
|
|
109
|
+
id,
|
|
110
|
+
jsonrpc,
|
|
111
|
+
method,
|
|
112
|
+
params,
|
|
113
|
+
}
|
|
114
|
+
: {
|
|
115
|
+
id,
|
|
116
|
+
jsonrpc,
|
|
117
|
+
method,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=internal-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"internal-provider.js","sourceRoot":"","sources":["../src/internal-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAc/D,OAAO,EAAE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EACC,EAAE;QACnC,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,cAAc,CAAC;QACvC,6EAA6E;QAC7E,mEAAmE;QACnE,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CACvC,cAAc,EACd,OAAO,CACR,CAAsB,CAAC;QAExB,OAAO;YACL,EAAE;YACF,OAAO;YACP,MAAM;SACP,CAAC;IACJ,CAAC,CAAC;IAEO,mBAAmB,GAAG,CAC7B,cAA8B,EAC9B,QAAyD,EACnD,EAAE;QACR,mDAAmD;QACnD,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC;YAC1B,oDAAoD;aACnD,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;aAC5C,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACf,QAAQ,CAAC,KAAK,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;QACL,kDAAkD;IACpD,CAAC,CAAC;CACH;AAED;;;;;GAKG;AACH,MAAM,UAAU,qCAAqC,CACnD,cAA8B;IAE9B,MAAM,EAAE,EAAE,GAAG,MAAM,EAAE,EAAE,OAAO,GAAG,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,CAAC;IAE1E,OAAO,MAAM;QACX,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;YACN,MAAM;SACP;QACH,CAAC,CAAC;YACE,EAAE;YACF,OAAO;YACP,MAAM;SACP,CAAC;AACR,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcEngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n HandleOptions,\n ContextConstraint,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcSuccess,\n JsonRpcRequest,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams = JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\ntype Options<\n Request extends JsonRpcRequest = JsonRpcRequest,\n Context extends ContextConstraint = MiddlewareContext,\n> = {\n engine: JsonRpcEngine | JsonRpcEngineV2<Request, Context>;\n};\n\n/**\n * An Ethereum provider.\n *\n * This provider loosely follows conventions that pre-date EIP-1193.\n * It is not compliant with any Ethereum provider standard.\n */\nexport class InternalProvider<\n Context extends ContextConstraint = MiddlewareContext,\n> {\n readonly #engine: JsonRpcEngineV2<JsonRpcRequest, Context>;\n\n /**\n * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n *\n * @param options - Options.\n * @param options.engine - The JSON-RPC engine used to process requests.\n */\n constructor({ engine }: Options<JsonRpcRequest, Context>) {\n this.#engine =\n 'push' in engine\n ? JsonRpcEngineV2.create({\n middleware: [asV2Middleware<JsonRpcParams, JsonRpcRequest>(engine)],\n })\n : engine;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * @param eip1193Request - The request to send.\n * @param options - The options for the request operation.\n * @param options.context - The context to include with the request.\n * @returns The JSON-RPC response.\n */\n async request<Params extends JsonRpcParams, Result extends Json>(\n eip1193Request: Eip1193Request<Params>,\n options?: HandleOptions<Context>,\n ): Promise<Result> {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n return (await this.#handle<Result>(jsonRpcRequest, options)).result;\n }\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Use {@link request} instead. This method is retained solely for backwards\n * compatibility with certain libraries.\n */\n sendAsync = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // Non-polluting `any` that acts like a constraint.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ): void => {\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#handleWithCallback(jsonRpcRequest, callback);\n };\n\n /**\n * Send a provider request asynchronously.\n *\n * This method serves the same purpose as `request`. It only exists for\n * legacy reasons.\n *\n * @param eip1193Request - The request to send.\n * @param callback - A function that is called upon the success or failure of the request.\n * @deprecated Use {@link request} instead. This method is retained solely for backwards\n * compatibility with certain libraries.\n */\n send = <Params extends JsonRpcParams>(\n eip1193Request: Eip1193Request<Params>,\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ): void => {\n if (typeof callback !== 'function') {\n throw new Error('Must provide callback to \"send\" method.');\n }\n const jsonRpcRequest =\n convertEip1193RequestToJsonRpcRequest(eip1193Request);\n this.#handleWithCallback(jsonRpcRequest, callback);\n };\n\n readonly #handle = async <Result extends Json>(\n jsonRpcRequest: JsonRpcRequest,\n options?: HandleOptions<Context>,\n ): Promise<JsonRpcSuccess<Result>> => {\n const { id, jsonrpc } = jsonRpcRequest;\n // The `result` typecast is unsafe, but we need it to preserve the provider's\n // public interface, which allows you to unsafely typecast results.\n const result = (await this.#engine.handle(\n jsonRpcRequest,\n options,\n )) as unknown as Result;\n\n return {\n id,\n jsonrpc,\n result,\n };\n };\n\n readonly #handleWithCallback = (\n jsonRpcRequest: JsonRpcRequest,\n callback: (error: unknown, providerRes?: unknown) => void,\n ): void => {\n /* eslint-disable promise/no-callback-in-promise */\n this.#handle(jsonRpcRequest)\n // A resolution will always be a successful response\n .then((response) => callback(null, response))\n .catch((error) => {\n callback(error);\n });\n /* eslint-enable promise/no-callback-in-promise */\n };\n}\n\n/**\n * Convert an EIP-1193 request to a JSON-RPC request.\n *\n * @param eip1193Request - The EIP-1193 request to convert.\n * @returns The JSON-RPC request.\n */\nexport function convertEip1193RequestToJsonRpcRequest(\n eip1193Request: Eip1193Request,\n): JsonRpcRequest {\n const { id = nanoid(), jsonrpc = '2.0', method, params } = eip1193Request;\n\n return params\n ? {\n id,\n jsonrpc,\n method,\n params,\n }\n : {\n id,\n jsonrpc,\n method,\n };\n}\n"]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { MiddlewareContext } from '@metamask/json-rpc-engine/v2';
|
|
2
|
+
import type { Json, JsonRpcId, JsonRpcParams, JsonRpcVersion2 } from '@metamask/utils';
|
|
3
|
+
import { InternalProvider } from './internal-provider.js';
|
|
4
|
+
/**
|
|
5
|
+
* A JSON-RPC request conforming to the EIP-1193 specification.
|
|
6
|
+
*/
|
|
7
|
+
type Eip1193Request<Params extends JsonRpcParams> = {
|
|
8
|
+
id?: JsonRpcId;
|
|
9
|
+
jsonrpc?: JsonRpcVersion2;
|
|
10
|
+
method: string;
|
|
11
|
+
params?: Params;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Represents the type of the `response` property in a fake provider stub.
|
|
15
|
+
*/
|
|
16
|
+
export type MockInternalProviderResponse = {
|
|
17
|
+
result: any;
|
|
18
|
+
} | {
|
|
19
|
+
error: string;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* An object that allows specifying the behavior of a specific invocation of
|
|
23
|
+
* `request`. The `method` always identifies the stub, but the behavior
|
|
24
|
+
* may be specified multiple ways: `request` can either return a promise or
|
|
25
|
+
* throw an error, and if it returns a promise, that promise can either be
|
|
26
|
+
* resolved with a response object or reject with an error.
|
|
27
|
+
*
|
|
28
|
+
* @property request - Looks for a request matching these specifications.
|
|
29
|
+
* @property request.method - The RPC method to which this stub will be matched.
|
|
30
|
+
* @property request.params - The params to which this stub will be matched.
|
|
31
|
+
* @property response - Instructs `request` to return a promise that resolves
|
|
32
|
+
* with a response object.
|
|
33
|
+
* @property response.result - Specifies a successful response, with this as the
|
|
34
|
+
* `result`.
|
|
35
|
+
* @property response.error - Specifies an error response, with this as the
|
|
36
|
+
* `error`.
|
|
37
|
+
* @property error - Instructs `request` to return a promise that rejects with
|
|
38
|
+
* this error.
|
|
39
|
+
* @property implementation - Allows overriding `request` entirely. Useful if
|
|
40
|
+
* you want it to throw an error.
|
|
41
|
+
* @property delay - The amount of time that will pass after the callback is
|
|
42
|
+
* called with the response.
|
|
43
|
+
* @property discardAfterMatching - Usually after the stub matches a request, it
|
|
44
|
+
* is discarded, but setting this to true prevents that from happening. True by
|
|
45
|
+
* default.
|
|
46
|
+
* @property beforeCompleting - Sometimes it is useful to do something after the
|
|
47
|
+
* request is kicked off but before it ends (or, in terms of a `fetch` promise,
|
|
48
|
+
* when the promise is initiated but before it is resolved). You can pass an
|
|
49
|
+
* (async) function for this option to do this.
|
|
50
|
+
*/
|
|
51
|
+
export type MockInternalProviderStub = {
|
|
52
|
+
request: {
|
|
53
|
+
method: string;
|
|
54
|
+
params?: any[];
|
|
55
|
+
};
|
|
56
|
+
delay?: number;
|
|
57
|
+
discardAfterMatching?: boolean;
|
|
58
|
+
beforeCompleting?: () => void | Promise<void>;
|
|
59
|
+
} & ({
|
|
60
|
+
response: MockInternalProviderResponse;
|
|
61
|
+
} | {
|
|
62
|
+
error: unknown;
|
|
63
|
+
} | {
|
|
64
|
+
implementation: () => void;
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* The set of options that the MockInternalProvider constructor takes.
|
|
68
|
+
*
|
|
69
|
+
* @property stubs - A set of objects that allow specifying the behavior
|
|
70
|
+
* of specific invocations of `request` matching a `method`.
|
|
71
|
+
*/
|
|
72
|
+
type MockInternalProviderEngineOptions = {
|
|
73
|
+
stubs?: MockInternalProviderStub[];
|
|
74
|
+
};
|
|
75
|
+
type Context = MiddlewareContext<{
|
|
76
|
+
origin: string;
|
|
77
|
+
skipCache: boolean;
|
|
78
|
+
} & Record<string, unknown>>;
|
|
79
|
+
/**
|
|
80
|
+
* An implementation of the provider that NetworkController exposes, which is
|
|
81
|
+
* actually an instance of InternalProvider (from the
|
|
82
|
+
* `@metamask/eth-json-rpc-provider` package). Hence it supports the same
|
|
83
|
+
* interface as InternalProvider, except that fake responses for any RPC
|
|
84
|
+
* methods that are accessed can be supplied via an API that is more succinct
|
|
85
|
+
* than using Jest's mocking API.
|
|
86
|
+
*/
|
|
87
|
+
export declare class MockInternalProvider extends InternalProvider<Context> {
|
|
88
|
+
#private;
|
|
89
|
+
calledStubs: MockInternalProviderStub[];
|
|
90
|
+
/**
|
|
91
|
+
* Makes a new instance of the fake provider.
|
|
92
|
+
*
|
|
93
|
+
* @param options - The options.
|
|
94
|
+
* @param options.stubs - A set of objects that allow specifying the behavior
|
|
95
|
+
* of specific invocations of `request` matching a `method`.
|
|
96
|
+
*/
|
|
97
|
+
constructor({ stubs }?: MockInternalProviderEngineOptions);
|
|
98
|
+
request: <Params extends JsonRpcParams, Result extends Json>(payload: Eip1193Request<Params>) => Promise<Result>;
|
|
99
|
+
sendAsync: <Params extends JsonRpcParams>(payload: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
|
|
100
|
+
send: <Params extends JsonRpcParams>(req: Eip1193Request<Params>, callback: (error: unknown, providerRes?: any) => void) => void;
|
|
101
|
+
}
|
|
102
|
+
export {};
|
|
103
|
+
//# sourceMappingURL=mock-internal-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-internal-provider.d.ts","sourceRoot":"","sources":["../src/mock-internal-provider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,iBAAiB,EAElB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EACV,IAAI,EACJ,SAAS,EACT,aAAa,EAGb,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAK1D;;GAEG;AACH,KAAK,cAAc,CAAC,MAAM,SAAS,aAAa,IAAI;IAClD,EAAE,CAAC,E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SAAS,IAAI,WACjD,cAAc,CAAC,MAAM,CAAC,KAC9B,OAAO,CAAC,MAAM,CAAC,CAYhB;IAEF,SAAS,GAAI,MAAM,SAAS,aAAa,WAC9B,cAAc,CAAC,MAAM,CAAC,YAGrB,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,GAAG,KAAK,IAAI,KACpD,IAAI,CAEL;IAEF,IAAI,GAAI,MAAM,SAAS,aAAa,OAC7B,cAAc,CAAC,MAAM,CAAC,YAGjB,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,GAAG,KAAK,IAAI,KACpD,IAAI,CAEL;CAwGH"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';
|
|
2
|
+
import { hasProperty } from '@metamask/utils';
|
|
3
|
+
import { isEqual } from 'lodash-es';
|
|
4
|
+
import { InternalProvider } from './internal-provider.js';
|
|
5
|
+
// Store this in case it gets stubbed later
|
|
6
|
+
const originalSetTimeout = setTimeout;
|
|
7
|
+
/**
|
|
8
|
+
* An implementation of the provider that NetworkController exposes, which is
|
|
9
|
+
* actually an instance of InternalProvider (from the
|
|
10
|
+
* `@metamask/eth-json-rpc-provider` package). Hence it supports the same
|
|
11
|
+
* interface as InternalProvider, except that fake responses for any RPC
|
|
12
|
+
* methods that are accessed can be supplied via an API that is more succinct
|
|
13
|
+
* than using Jest's mocking API.
|
|
14
|
+
*/
|
|
15
|
+
// NOTE: We shouldn't need to extend from the "real" provider here, but
|
|
16
|
+
// we'd need a `InternalProvider` _interface_ and that doesn't exist (at
|
|
17
|
+
// least not yet).
|
|
18
|
+
export class MockInternalProvider extends InternalProvider {
|
|
19
|
+
calledStubs;
|
|
20
|
+
#originalStubs;
|
|
21
|
+
#stubs;
|
|
22
|
+
/**
|
|
23
|
+
* Makes a new instance of the fake provider.
|
|
24
|
+
*
|
|
25
|
+
* @param options - The options.
|
|
26
|
+
* @param options.stubs - A set of objects that allow specifying the behavior
|
|
27
|
+
* of specific invocations of `request` matching a `method`.
|
|
28
|
+
*/
|
|
29
|
+
constructor({ stubs = [] } = {}) {
|
|
30
|
+
super({
|
|
31
|
+
engine: JsonRpcEngineV2.create({
|
|
32
|
+
middleware: [
|
|
33
|
+
() => {
|
|
34
|
+
throw new Error('MockInternalProvider received unstubbed method call');
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
this.#originalStubs = stubs;
|
|
40
|
+
this.#stubs = this.#originalStubs.slice();
|
|
41
|
+
this.calledStubs = [];
|
|
42
|
+
}
|
|
43
|
+
request = (payload) => {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
this.#handleSend(payload, (error, providerRes) => {
|
|
46
|
+
if (error) {
|
|
47
|
+
// Error is `unknown`.
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
49
|
+
reject(error);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
resolve(providerRes.result);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
sendAsync = (payload,
|
|
58
|
+
// This is copied from the implementation for InternalProvider.
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
callback) => {
|
|
61
|
+
return this.#handleSend(payload, callback);
|
|
62
|
+
};
|
|
63
|
+
send = (req,
|
|
64
|
+
// This is copied from the implementation for InternalProvider.
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
66
|
+
callback) => {
|
|
67
|
+
return this.#handleSend(req, callback);
|
|
68
|
+
};
|
|
69
|
+
#handleSend(req,
|
|
70
|
+
// This is copied from the implementation for InternalProvider.
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
callback) {
|
|
73
|
+
if (Array.isArray(req)) {
|
|
74
|
+
throw new Error("Arrays aren't supported");
|
|
75
|
+
}
|
|
76
|
+
const index = this.#stubs.findIndex((stub) => {
|
|
77
|
+
return (stub.request.method === req.method &&
|
|
78
|
+
(!hasProperty(stub.request, 'params') ||
|
|
79
|
+
isEqual(stub.request.params, req.params)));
|
|
80
|
+
});
|
|
81
|
+
if (index === -1) {
|
|
82
|
+
const matchingCalledStubs = this.calledStubs.filter((stub) => {
|
|
83
|
+
return (stub.request.method === req.method &&
|
|
84
|
+
(!hasProperty(stub.request, 'params') ||
|
|
85
|
+
isEqual(stub.request.params, req.params)));
|
|
86
|
+
});
|
|
87
|
+
let message = `Could not find any stubs matching: ${JSON.stringify(req)}`;
|
|
88
|
+
if (matchingCalledStubs.length > 0) {
|
|
89
|
+
message += `\n\nIt appears the following stubs were defined, but have been called already:\n\n${JSON.stringify(matchingCalledStubs)}`;
|
|
90
|
+
}
|
|
91
|
+
throw new Error(message);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// We just verified that we can find this index.
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
96
|
+
const stub = this.#stubs[index];
|
|
97
|
+
if (stub.discardAfterMatching !== false) {
|
|
98
|
+
this.#stubs.splice(index, 1);
|
|
99
|
+
}
|
|
100
|
+
if (stub.delay) {
|
|
101
|
+
originalSetTimeout(() => {
|
|
102
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
104
|
+
this.#handleRequest(stub, callback);
|
|
105
|
+
}, stub.delay);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
110
|
+
this.#handleRequest(stub, callback);
|
|
111
|
+
}
|
|
112
|
+
this.calledStubs.push({ ...stub });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async #handleRequest(stub, callback) {
|
|
116
|
+
if (stub.beforeCompleting) {
|
|
117
|
+
await stub.beforeCompleting();
|
|
118
|
+
}
|
|
119
|
+
// We need to narrow the type.
|
|
120
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
121
|
+
if ('implementation' in stub) {
|
|
122
|
+
stub.implementation();
|
|
123
|
+
// We need to narrow the type.
|
|
124
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
125
|
+
}
|
|
126
|
+
else if ('response' in stub) {
|
|
127
|
+
// We need to narrow the type.
|
|
128
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
129
|
+
if ('result' in stub.response) {
|
|
130
|
+
return callback(null, {
|
|
131
|
+
jsonrpc: '2.0',
|
|
132
|
+
id: 1,
|
|
133
|
+
result: stub.response.result,
|
|
134
|
+
});
|
|
135
|
+
// We need to narrow the type.
|
|
136
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
137
|
+
}
|
|
138
|
+
else if ('error' in stub.response) {
|
|
139
|
+
return callback(null, {
|
|
140
|
+
jsonrpc: '2.0',
|
|
141
|
+
id: 1,
|
|
142
|
+
error: {
|
|
143
|
+
code: -999,
|
|
144
|
+
message: stub.response.error,
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// We need to narrow the type.
|
|
149
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
150
|
+
}
|
|
151
|
+
else if ('error' in stub) {
|
|
152
|
+
return callback(stub.error);
|
|
153
|
+
}
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=mock-internal-provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-internal-provider.js","sourceRoot":"","sources":["../src/mock-internal-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAM/D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAS9C,OAAO,EAAE,OAAO,EAAE,MAAM,W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YACtB,8BAA8B;YAC9B,gDAAgD;QAClD,CAAC;aAAM,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;YAC9B,8BAA8B;YAC9B,gDAAgD;YAChD,IAAI,QAAQ,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;gBAC9B,OAAO,QAAQ,CAAC,IAAI,EAAE;oBACpB,OAAO,EAAE,KAAK;oBACd,EAAE,EAAE,CAAC;oBACL,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM;iBAC7B,CAAC,CAAC;gBACH,8BAA8B;gBAC9B,gDAAgD;YAClD,CAAC;iBAAM,IAAI,OAAO,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACpC,OAAO,QAAQ,CAAC,IAAI,EAAE;oBACpB,OAAO,EAAE,KAAK;oBACd,EAAE,EAAE,CAAC;oBACL,KAAK,EAAE;wBACL,IAAI,EAAE,CAAC,GAAG;wBACV,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK;qBAC7B;iBACF,CAAC,CAAC;YACL,CAAC;YACD,8BAA8B;YAC9B,gDAAgD;QAClD,CAAC;aAAM,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;YAC3B,OAAO,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC9B,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,CAAC;CACF","sourcesContent":["import { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n JsonRpcMiddleware,\n MiddlewareContext,\n ResultConstraint,\n} from '@metamask/json-rpc-engine/v2';\nimport { hasProperty } from '@metamask/utils';\nimport type {\n Json,\n JsonRpcId,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n JsonRpcVersion2,\n} from '@metamask/utils';\nimport { isEqual } from 'lodash-es';\n\nimport { InternalProvider } from './internal-provider.js';\n\n// Store this in case it gets stubbed later\nconst originalSetTimeout = setTimeout;\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams> = {\n id?: JsonRpcId;\n jsonrpc?: JsonRpcVersion2;\n method: string;\n params?: Params;\n};\n\n/**\n * Represents the type of the `response` property in a fake provider stub.\n */\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type MockInternalProviderResponse = { result: any } | { error: string };\n\n/**\n * An object that allows specifying the behavior of a specific invocation of\n * `request`. The `method` always identifies the stub, but the behavior\n * may be specified multiple ways: `request` can either return a promise or\n * throw an error, and if it returns a promise, that promise can either be\n * resolved with a response object or reject with an error.\n *\n * @property request - Looks for a request matching these specifications.\n * @property request.method - The RPC method to which this stub will be matched.\n * @property request.params - The params to which this stub will be matched.\n * @property response - Instructs `request` to return a promise that resolves\n * with a response object.\n * @property response.result - Specifies a successful response, with this as the\n * `result`.\n * @property response.error - Specifies an error response, with this as the\n * `error`.\n * @property error - Instructs `request` to return a promise that rejects with\n * this error.\n * @property implementation - Allows overriding `request` entirely. Useful if\n * you want it to throw an error.\n * @property delay - The amount of time that will pass after the callback is\n * called with the response.\n * @property discardAfterMatching - Usually after the stub matches a request, it\n * is discarded, but setting this to true prevents that from happening. True by\n * default.\n * @property beforeCompleting - Sometimes it is useful to do something after the\n * request is kicked off but before it ends (or, in terms of a `fetch` promise,\n * when the promise is initiated but before it is resolved). You can pass an\n * (async) function for this option to do this.\n */\nexport type MockInternalProviderStub = {\n request: {\n method: string;\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params?: any[];\n };\n delay?: number;\n discardAfterMatching?: boolean;\n beforeCompleting?: () => void | Promise<void>;\n} & (\n | {\n response: MockInternalProviderResponse;\n }\n | {\n error: unknown;\n }\n | {\n implementation: () => void;\n }\n);\n\n/**\n * The set of options that the MockInternalProvider constructor takes.\n *\n * @property stubs - A set of objects that allow specifying the behavior\n * of specific invocations of `request` matching a `method`.\n */\ntype MockInternalProviderEngineOptions = {\n stubs?: MockInternalProviderStub[];\n};\n\ntype Context = MiddlewareContext<\n { origin: string; skipCache: boolean } & Record<string, unknown>\n>;\ntype Middleware = JsonRpcMiddleware<\n JsonRpcRequest,\n ResultConstraint<JsonRpcRequest>,\n Context\n>;\n\n/**\n * An implementation of the provider that NetworkController exposes, which is\n * actually an instance of InternalProvider (from the\n * `@metamask/eth-json-rpc-provider` package). Hence it supports the same\n * interface as InternalProvider, except that fake responses for any RPC\n * methods that are accessed can be supplied via an API that is more succinct\n * than using Jest's mocking API.\n */\n// NOTE: We shouldn't need to extend from the \"real\" provider here, but\n// we'd need a `InternalProvider` _interface_ and that doesn't exist (at\n// least not yet).\nexport class MockInternalProvider extends InternalProvider<Context> {\n calledStubs: MockInternalProviderStub[];\n\n readonly #originalStubs: MockInternalProviderStub[];\n\n readonly #stubs: MockInternalProviderStub[];\n\n /**\n * Makes a new instance of the fake provider.\n *\n * @param options - The options.\n * @param options.stubs - A set of objects that allow specifying the behavior\n * of specific invocations of `request` matching a `method`.\n */\n constructor({ stubs = [] }: MockInternalProviderEngineOptions = {}) {\n super({\n engine: JsonRpcEngineV2.create<Middleware>({\n middleware: [\n (): never => {\n throw new Error(\n 'MockInternalProvider received unstubbed method call',\n );\n },\n ],\n }),\n });\n this.#originalStubs = stubs;\n this.#stubs = this.#originalStubs.slice();\n this.calledStubs = [];\n }\n\n request = <Params extends JsonRpcParams, Result extends Json>(\n payload: Eip1193Request<Params>,\n ): Promise<Result> => {\n return new Promise((resolve, reject) => {\n this.#handleSend(payload, (error, providerRes) => {\n if (error) {\n // Error is `unknown`.\n // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors\n reject(error);\n } else {\n resolve(providerRes.result);\n }\n });\n });\n };\n\n sendAsync = <Params extends JsonRpcParams>(\n payload: Eip1193Request<Params>,\n // This is copied from the implementation for InternalProvider.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ): void => {\n return this.#handleSend(payload, callback);\n };\n\n send = <Params extends JsonRpcParams>(\n req: Eip1193Request<Params>,\n // This is copied from the implementation for InternalProvider.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ): void => {\n return this.#handleSend(req, callback);\n };\n\n #handleSend<Params extends JsonRpcParams>(\n req: Eip1193Request<Params>,\n // This is copied from the implementation for InternalProvider.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n callback: (error: unknown, providerRes?: any) => void,\n ): void {\n if (Array.isArray(req)) {\n throw new Error(\"Arrays aren't supported\");\n }\n\n const index = this.#stubs.findIndex((stub) => {\n return (\n stub.request.method === req.method &&\n (!hasProperty(stub.request, 'params') ||\n isEqual(stub.request.params, req.params))\n );\n });\n\n if (index === -1) {\n const matchingCalledStubs = this.calledStubs.filter((stub) => {\n return (\n stub.request.method === req.method &&\n (!hasProperty(stub.request, 'params') ||\n isEqual(stub.request.params, req.params))\n );\n });\n let message = `Could not find any stubs matching: ${JSON.stringify(req)}`;\n if (matchingCalledStubs.length > 0) {\n message += `\\n\\nIt appears the following stubs were defined, but have been called already:\\n\\n${JSON.stringify(\n matchingCalledStubs,\n )}`;\n }\n\n throw new Error(message);\n } else {\n // We just verified that we can find this index.\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const stub = this.#stubs[index]!;\n\n if (stub.discardAfterMatching !== false) {\n this.#stubs.splice(index, 1);\n }\n\n if (stub.delay) {\n originalSetTimeout(() => {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#handleRequest(stub, callback);\n }, stub.delay);\n } else {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.#handleRequest(stub, callback);\n }\n\n this.calledStubs.push({ ...stub });\n }\n }\n\n async #handleRequest(\n stub: MockInternalProviderStub,\n callback: (error: unknown, response?: JsonRpcResponse) => void,\n ): Promise<void> {\n if (stub.beforeCompleting) {\n await stub.beforeCompleting();\n }\n\n // We need to narrow the type.\n // eslint-disable-next-line no-restricted-syntax\n if ('implementation' in stub) {\n stub.implementation();\n // We need to narrow the type.\n // eslint-disable-next-line no-restricted-syntax\n } else if ('response' in stub) {\n // We need to narrow the type.\n // eslint-disable-next-line no-restricted-syntax\n if ('result' in stub.response) {\n return callback(null, {\n jsonrpc: '2.0',\n id: 1,\n result: stub.response.result,\n });\n // We need to narrow the type.\n // eslint-disable-next-line no-restricted-syntax\n } else if ('error' in stub.response) {\n return callback(null, {\n jsonrpc: '2.0',\n id: 1,\n error: {\n code: -999,\n message: stub.response.error,\n },\n });\n }\n // We need to narrow the type.\n // eslint-disable-next-line no-restricted-syntax\n } else if ('error' in stub) {\n return callback(stub.error);\n }\n\n return undefined;\n }\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from
|
|
2
|
-
import type { ContextConstraint, JsonRpcMiddleware, ResultConstraint } from
|
|
3
|
-
import type { Json, JsonRpcParams, JsonRpcRequest } from
|
|
4
|
-
import { InternalProvider } from
|
|
1
|
+
import type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';
|
|
2
|
+
import type { ContextConstraint, JsonRpcMiddleware, ResultConstraint } from '@metamask/json-rpc-engine/v2';
|
|
3
|
+
import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
|
|
4
|
+
import { InternalProvider } from './internal-provider.js';
|
|
5
5
|
/**
|
|
6
6
|
* Construct an Ethereum provider from the given middleware.
|
|
7
7
|
*
|
|
@@ -17,4 +17,4 @@ export declare function providerFromMiddleware<Params extends JsonRpcParams, Res
|
|
|
17
17
|
* @returns An Ethereum provider.
|
|
18
18
|
*/
|
|
19
19
|
export declare function providerFromMiddlewareV2<Request extends JsonRpcRequest, Middleware extends JsonRpcMiddleware<Request, ResultConstraint<Request>, ContextConstraint>>(middleware: Middleware): InternalProvider;
|
|
20
|
-
//# sourceMappingURL=provider-from-middleware.d.
|
|
20
|
+
//# sourceMappingURL=provider-from-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-from-middleware.d.ts","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AAE9F,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAE3E,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,SAAS,aAAa,EAC5B,MAAM,SAAS,IAAI,EACnB,UAAU,EAAE,uBAAuB,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CAQvE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,SAAS,cAAc,EAC9B,UAAU,SAAS,iBAAiB,CAClC,OAAO,EACP,gBAAgB,CAAC,OAAO,CAAC,EACzB,iBAAiB,CAClB,EACD,UAAU,EAAE,UAAU,GAAG,gBAAgB,CAS1C"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { asV2Middleware } from
|
|
2
|
-
import { JsonRpcEngineV2 } from
|
|
3
|
-
import { InternalProvider } from
|
|
1
|
+
import { asV2Middleware } from '@metamask/json-rpc-engine';
|
|
2
|
+
import { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';
|
|
3
|
+
import { InternalProvider } from './internal-provider.js';
|
|
4
4
|
/**
|
|
5
5
|
* Construct an Ethereum provider from the given middleware.
|
|
6
6
|
*
|
|
@@ -32,4 +32,4 @@ export function providerFromMiddlewareV2(middleware) {
|
|
|
32
32
|
}),
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
//# sourceMappingURL=provider-from-middleware.
|
|
35
|
+
//# sourceMappingURL=provider-from-middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-from-middleware.js","sourceRoot":"","sources":["../src/provider-from-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAQ/D,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAGpC,UAAmD;IACnD,OAAO,wBAAwB;IAC7B,sGAAsG;IACtG,mGAAmG;IACnG,sGAAsG;IACtG,oEAAoE;IACpE,cAAc,CAAC,UAAU,CAAsC,CAChE,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAOtC,UAAsB;IACtB,OAAO,IAAI,gBAAgB,CAAC;QAC1B,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC;YAC7B,4FAA4F;YAC5F,oGAAoG;YACpG,0DAA0D;YAC1D,UAAU,EAAE,CAAC,UAA+C,CAAC;SAC9D,CAAC;KACH,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { asV2Middleware } from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as LegacyJsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n ContextConstraint,\n JsonRpcMiddleware,\n ResultConstraint,\n} from '@metamask/json-rpc-engine/v2';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport { InternalProvider } from './internal-provider.js';\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n * @deprecated Use `JsonRpcEngineV2` middleware and {@link providerFromMiddlewareV2} instead.\n */\nexport function providerFromMiddleware<\n Params extends JsonRpcParams,\n Result extends Json,\n>(middleware: LegacyJsonRpcMiddleware<Params, Result>): InternalProvider {\n return providerFromMiddlewareV2(\n // This function is generic on the Params and Result types to match the legacy JsonRpcMiddleware type.\n // However, since the V2 JsonRpcMiddleware type is not generic on the Params, we need to elide this\n // parameter by upcasting the request type to JsonRpcRequest, or we get an error due to contravariance\n // since JsonRpcRequest<Params> is not assignable to JsonRpcRequest.\n asV2Middleware(middleware) as JsonRpcMiddleware<JsonRpcRequest>,\n );\n}\n\n/**\n * Construct an Ethereum provider from the given middleware.\n *\n * @param middleware - The middleware to construct a provider from.\n * @returns An Ethereum provider.\n */\nexport function providerFromMiddlewareV2<\n Request extends JsonRpcRequest,\n Middleware extends JsonRpcMiddleware<\n Request,\n ResultConstraint<Request>,\n ContextConstraint\n >,\n>(middleware: Middleware): InternalProvider {\n return new InternalProvider({\n engine: JsonRpcEngineV2.create({\n // This function is generic in order to accept middleware functions with narrower types than\n // the plain JsonRpcMiddleware<JsonRpcRequest> type. However, since InternalProvider is non-generic,\n // we need to upcast the middleware to avoid a type error.\n middleware: [middleware as JsonRpcMiddleware<JsonRpcRequest>],\n }),\n });\n}\n"]}
|