@metamask-previews/eth-json-rpc-middleware 21.0.0-preview-21a5ddac → 21.0.0-preview-4cd9da8d
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 +12 -6
- package/dist/inflight-cache.cjs +1 -1
- package/dist/inflight-cache.cjs.map +1 -1
- package/dist/inflight-cache.d.cts.map +1 -1
- package/dist/inflight-cache.d.mts.map +1 -1
- package/dist/inflight-cache.mjs +1 -1
- package/dist/inflight-cache.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,21 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Add new function `providerAsMiddlewareV2` for converting an `InternalProvider` into a `JsonRpcEngine` v2-compatible middleware ([#7138](https://github.com/MetaMask/core/pull/7138))
|
|
13
|
+
|
|
10
14
|
### Changed
|
|
11
15
|
|
|
12
|
-
- **BREAKING:** Migrate to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
|
|
13
|
-
- Migrates all middleware from `JsonRpcEngine` to `JsonRpcEngineV2`.
|
|
14
|
-
- Signatures of various middleware dependencies, e.g. `processTransaction` of `createWalletMiddleware`, have changed
|
|
15
|
-
and must be updated by consumers.
|
|
16
|
-
- Be advised that request objects are now deeply frozen, and cannot be mutated.
|
|
16
|
+
- **BREAKING:** Migrate all middleware from `JsonRpcEngine` to `JsonRpcEngineV2` ([#7065](https://github.com/MetaMask/core/pull/7065))
|
|
17
17
|
- To continue using this package with the legacy `JsonRpcEngine`, use the `asLegacyMiddleware` backwards compatibility function.
|
|
18
|
+
- **BREAKING:** Change the signatures of hooks for `createWalletMiddleware` ([#7065](https://github.com/MetaMask/core/pull/7065))
|
|
19
|
+
- To wit:
|
|
20
|
+
- `getAccounts` takes an origin argument (`string`) instead of a `JsonRpcRequest`
|
|
21
|
+
- `processDecryptMessage` and `processEncryptionPublicKey` take a `MessageRequest` from `@metamask/message-manager` instead of `JsonRpcRequest`
|
|
22
|
+
- `processPersonalMessage`, `processTransaction`, `processSignTransaction`, `processTypedMessage`, `processTypedMessageV3` and `processTypedMessageV4` take a `context` as the third argument, before any other arguments
|
|
23
|
+
- Be advised that request objects are now deeply frozen, and cannot be mutated.
|
|
18
24
|
- **BREAKING:** Use `InternalProvider` instead of `SafeEventEmitterProvider` ([#6796](https://github.com/MetaMask/core/pull/6796))
|
|
19
25
|
- Wherever a `SafeEventEmitterProvider` was expected, an `InternalProvider` is now expected instead.
|
|
20
26
|
- **BREAKING:** Stop retrying `undefined` results for methods that include a block tag parameter ([#7001](https://github.com/MetaMask/core/pull/7001))
|
|
21
27
|
- The `retryOnEmpty` middleware will now throw an error if it encounters an `undefined` result when dispatching
|
|
22
28
|
a request with a later block number than the originally requested block number.
|
|
23
29
|
- In practice, this should happen rarely if ever.
|
|
24
|
-
- Migrate all uses of `interface` to `type` ([#6885](https://github.com/MetaMask/core/pull/6885))
|
|
30
|
+
- **BREAKING:** Migrate all uses of `interface` to `type` ([#6885](https://github.com/MetaMask/core/pull/6885))
|
|
25
31
|
|
|
26
32
|
## [21.0.0]
|
|
27
33
|
|
package/dist/inflight-cache.cjs
CHANGED
|
@@ -45,7 +45,7 @@ function createInflightCacheMiddleware() {
|
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
47
47
|
log('Running %i collected handler(s) for failed request %o', activeRequestHandlers.length, request);
|
|
48
|
-
runRequestHandlers({ error
|
|
48
|
+
runRequestHandlers({ error }, activeRequestHandlers);
|
|
49
49
|
throw error;
|
|
50
50
|
}
|
|
51
51
|
finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.cjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":";;;AAIA,2CAIyB;AAEzB,uDAAoE;AACp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CAAC;AACL,CAAC","sourcesContent":["import type {\n JsonRpcMiddleware,\n MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport {\n type Json,\n type JsonRpcRequest,\n createDeferredPromise,\n} from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandler = [\n (result: Readonly<Json>) => void,\n (error: unknown) => void,\n];\n\ntype InflightRequest = {\n [cacheId: string]: RequestHandler[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\n/**\n * Creates a middleware that caches inflight requests.\n * If a request is already in flight, the middleware will wait for the request to complete\n * and then return the result.\n *\n * @returns A middleware that caches inflight requests.\n */\nexport function createInflightCacheMiddleware(): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache: boolean }>\n> {\n const inflightRequests: InflightRequest = {};\n\n return async ({ request, context, next }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const cacheId: string | null = cacheIdentifierForRequest(request);\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', request);\n return next();\n }\n\n // check for matching requests\n let activeRequestHandlers: RequestHandler[] = inflightRequests[cacheId];\n // if found, wait for the active request to be handled\n if (activeRequestHandlers) {\n // setup the response listener and wait for it to be called\n // it will handle copying the result and request fields\n log(\n 'Running %i handler(s) for request %o',\n activeRequestHandlers.length,\n request,\n );\n return await createActiveRequestHandler(activeRequestHandlers);\n }\n\n // setup response handler array for subsequent requests\n activeRequestHandlers = [];\n inflightRequests[cacheId] = activeRequestHandlers;\n // allow request to be handled normally\n log('Carrying original request forward %o', request);\n try {\n const result = (await next()) as Readonly<Json>;\n log(\n 'Running %i collected handler(s) for successful request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ result }, activeRequestHandlers);\n return result;\n } catch (error) {\n log(\n 'Running %i collected handler(s) for failed request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ error }, activeRequestHandlers);\n throw error;\n } finally {\n delete inflightRequests[cacheId];\n }\n };\n}\n\n/**\n * Creates a new request handler for the active request.\n *\n * @param activeRequestHandlers - The active request handlers.\n * @returns A promise that resolves to the result of the request.\n */\nfunction createActiveRequestHandler(\n activeRequestHandlers: RequestHandler[],\n): Promise<Readonly<Json>> {\n const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();\n activeRequestHandlers.push([\n (result: Readonly<Json>) => resolve(result),\n (error: unknown) => reject(error),\n ]);\n return promise;\n}\n\n/**\n * Runs the request handlers for the given result or error.\n *\n * @param resultOrError - The result or error of the request.\n * @param activeRequestHandlers - The active request handlers.\n */\nfunction runRequestHandlers(\n resultOrError: { result: Readonly<Json> } | { error: unknown },\n activeRequestHandlers: RequestHandler[],\n): void {\n // use setTimeout so we can handle the original request first\n setTimeout(() => {\n activeRequestHandlers.forEach(([onSuccess, onError]) => {\n if ('result' in resultOrError) {\n onSuccess(resultOrError.result);\n } else {\n onError(resultOrError.error);\n }\n });\n });\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.d.cts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EAEpB,wBAAwB;
|
|
1
|
+
{"version":3,"file":"inflight-cache.d.cts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EAEpB,wBAAwB;AAgBzB;;;;;;GAMG;AACH,wBAAgB,6BAA6B,IAAI,iBAAiB,CAChE,cAAc,EACd,IAAI,EACJ,iBAAiB,CAAC;IAAE,SAAS,EAAE,OAAO,CAAA;CAAE,CAAC,CAC1C,CAsDA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.d.mts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EAEpB,wBAAwB;
|
|
1
|
+
{"version":3,"file":"inflight-cache.d.mts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,iBAAiB,EAClB,qCAAqC;AACtC,OAAO,EACL,KAAK,IAAI,EACT,KAAK,cAAc,EAEpB,wBAAwB;AAgBzB;;;;;;GAMG;AACH,wBAAgB,6BAA6B,IAAI,iBAAiB,CAChE,cAAc,EACd,IAAI,EACJ,iBAAiB,CAAC;IAAE,SAAS,EAAE,OAAO,CAAA;CAAE,CAAC,CAC1C,CAsDA"}
|
package/dist/inflight-cache.mjs
CHANGED
|
@@ -42,7 +42,7 @@ export function createInflightCacheMiddleware() {
|
|
|
42
42
|
}
|
|
43
43
|
catch (error) {
|
|
44
44
|
log('Running %i collected handler(s) for failed request %o', activeRequestHandlers.length, request);
|
|
45
|
-
runRequestHandlers({ error
|
|
45
|
+
runRequestHandlers({ error }, activeRequestHandlers);
|
|
46
46
|
throw error;
|
|
47
47
|
}
|
|
48
48
|
finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.mjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,qBAAqB,EACtB,wBAAwB;AAEzB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AACpE,OAAO,EAAE,yBAAyB,EAAE,0BAAsB;
|
|
1
|
+
{"version":3,"file":"inflight-cache.mjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,qBAAqB,EACtB,wBAAwB;AA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engine/v2';\nimport {\n type Json,\n type JsonRpcRequest,\n createDeferredPromise,\n} from '@metamask/utils';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandler = [\n (result: Readonly<Json>) => void,\n (error: unknown) => void,\n];\n\ntype InflightRequest = {\n [cacheId: string]: RequestHandler[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\n/**\n * Creates a middleware that caches inflight requests.\n * If a request is already in flight, the middleware will wait for the request to complete\n * and then return the result.\n *\n * @returns A middleware that caches inflight requests.\n */\nexport function createInflightCacheMiddleware(): JsonRpcMiddleware<\n JsonRpcRequest,\n Json,\n MiddlewareContext<{ skipCache: boolean }>\n> {\n const inflightRequests: InflightRequest = {};\n\n return async ({ request, context, next }) => {\n if (context.get('skipCache')) {\n return next();\n }\n\n const cacheId: string | null = cacheIdentifierForRequest(request);\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', request);\n return next();\n }\n\n // check for matching requests\n let activeRequestHandlers: RequestHandler[] = inflightRequests[cacheId];\n // if found, wait for the active request to be handled\n if (activeRequestHandlers) {\n // setup the response listener and wait for it to be called\n // it will handle copying the result and request fields\n log(\n 'Running %i handler(s) for request %o',\n activeRequestHandlers.length,\n request,\n );\n return await createActiveRequestHandler(activeRequestHandlers);\n }\n\n // setup response handler array for subsequent requests\n activeRequestHandlers = [];\n inflightRequests[cacheId] = activeRequestHandlers;\n // allow request to be handled normally\n log('Carrying original request forward %o', request);\n try {\n const result = (await next()) as Readonly<Json>;\n log(\n 'Running %i collected handler(s) for successful request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ result }, activeRequestHandlers);\n return result;\n } catch (error) {\n log(\n 'Running %i collected handler(s) for failed request %o',\n activeRequestHandlers.length,\n request,\n );\n runRequestHandlers({ error }, activeRequestHandlers);\n throw error;\n } finally {\n delete inflightRequests[cacheId];\n }\n };\n}\n\n/**\n * Creates a new request handler for the active request.\n *\n * @param activeRequestHandlers - The active request handlers.\n * @returns A promise that resolves to the result of the request.\n */\nfunction createActiveRequestHandler(\n activeRequestHandlers: RequestHandler[],\n): Promise<Readonly<Json>> {\n const { resolve, promise, reject } = createDeferredPromise<Readonly<Json>>();\n activeRequestHandlers.push([\n (result: Readonly<Json>) => resolve(result),\n (error: unknown) => reject(error),\n ]);\n return promise;\n}\n\n/**\n * Runs the request handlers for the given result or error.\n *\n * @param resultOrError - The result or error of the request.\n * @param activeRequestHandlers - The active request handlers.\n */\nfunction runRequestHandlers(\n resultOrError: { result: Readonly<Json> } | { error: unknown },\n activeRequestHandlers: RequestHandler[],\n): void {\n // use setTimeout so we can handle the original request first\n setTimeout(() => {\n activeRequestHandlers.forEach(([onSuccess, onError]) => {\n if ('result' in resultOrError) {\n onSuccess(resultOrError.result);\n } else {\n onError(resultOrError.error);\n }\n });\n });\n}\n"]}
|