@metamask-previews/eth-json-rpc-middleware 21.0.0-preview-5a558267 → 21.0.0-preview-f8faf75c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -3
- package/dist/block-cache.cjs +32 -41
- package/dist/block-cache.cjs.map +1 -1
- package/dist/block-cache.d.cts +3 -12
- package/dist/block-cache.d.cts.map +1 -1
- package/dist/block-cache.d.mts +3 -12
- package/dist/block-cache.d.mts.map +1 -1
- package/dist/block-cache.mjs +32 -41
- package/dist/block-cache.mjs.map +1 -1
- package/dist/block-ref-rewrite.cjs +8 -12
- package/dist/block-ref-rewrite.cjs.map +1 -1
- package/dist/block-ref-rewrite.d.cts +3 -3
- package/dist/block-ref-rewrite.d.cts.map +1 -1
- package/dist/block-ref-rewrite.d.mts +3 -3
- package/dist/block-ref-rewrite.d.mts.map +1 -1
- package/dist/block-ref-rewrite.mjs +8 -12
- package/dist/block-ref-rewrite.mjs.map +1 -1
- package/dist/block-ref.cjs +10 -17
- package/dist/block-ref.cjs.map +1 -1
- package/dist/block-ref.d.cts +3 -12
- package/dist/block-ref.d.cts.map +1 -1
- package/dist/block-ref.d.mts +3 -12
- package/dist/block-ref.d.mts.map +1 -1
- package/dist/block-ref.mjs +10 -17
- package/dist/block-ref.mjs.map +1 -1
- package/dist/block-tracker-inspector.cjs +25 -28
- package/dist/block-tracker-inspector.cjs.map +1 -1
- package/dist/block-tracker-inspector.d.cts +3 -3
- package/dist/block-tracker-inspector.d.cts.map +1 -1
- package/dist/block-tracker-inspector.d.mts +3 -3
- package/dist/block-tracker-inspector.d.mts.map +1 -1
- package/dist/block-tracker-inspector.mjs +25 -28
- package/dist/block-tracker-inspector.mjs.map +1 -1
- package/dist/fetch.cjs +12 -8
- package/dist/fetch.cjs.map +1 -1
- package/dist/fetch.d.cts +3 -5
- package/dist/fetch.d.cts.map +1 -1
- package/dist/fetch.d.mts +3 -5
- package/dist/fetch.d.mts.map +1 -1
- package/dist/fetch.mjs +12 -8
- package/dist/fetch.mjs.map +1 -1
- package/dist/inflight-cache.cjs +45 -61
- package/dist/inflight-cache.cjs.map +1 -1
- package/dist/inflight-cache.d.cts +3 -12
- package/dist/inflight-cache.d.cts.map +1 -1
- package/dist/inflight-cache.d.mts +3 -12
- package/dist/inflight-cache.d.mts.map +1 -1
- package/dist/inflight-cache.mjs +45 -61
- package/dist/inflight-cache.mjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.cjs +9 -20
- package/dist/methods/wallet-request-execution-permissions.cjs.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.cts +3 -14
- package/dist/methods/wallet-request-execution-permissions.d.cts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.d.mts +3 -14
- package/dist/methods/wallet-request-execution-permissions.d.mts.map +1 -1
- package/dist/methods/wallet-request-execution-permissions.mjs +7 -18
- package/dist/methods/wallet-request-execution-permissions.mjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.cjs +11 -23
- package/dist/methods/wallet-revoke-execution-permission.cjs.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.cts +5 -19
- package/dist/methods/wallet-revoke-execution-permission.d.cts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.d.mts +5 -19
- package/dist/methods/wallet-revoke-execution-permission.d.mts.map +1 -1
- package/dist/methods/wallet-revoke-execution-permission.mjs +8 -20
- package/dist/methods/wallet-revoke-execution-permission.mjs.map +1 -1
- package/dist/providerAsMiddleware.cjs +1 -18
- package/dist/providerAsMiddleware.cjs.map +1 -1
- package/dist/providerAsMiddleware.d.cts +3 -18
- package/dist/providerAsMiddleware.d.cts.map +1 -1
- package/dist/providerAsMiddleware.d.mts +3 -18
- package/dist/providerAsMiddleware.d.mts.map +1 -1
- package/dist/providerAsMiddleware.mjs +0 -16
- package/dist/providerAsMiddleware.mjs.map +1 -1
- package/dist/retryOnEmpty.cjs +11 -8
- package/dist/retryOnEmpty.cjs.map +1 -1
- package/dist/retryOnEmpty.d.cts +3 -3
- package/dist/retryOnEmpty.d.cts.map +1 -1
- package/dist/retryOnEmpty.d.mts +3 -3
- package/dist/retryOnEmpty.d.mts.map +1 -1
- package/dist/retryOnEmpty.mjs +11 -8
- package/dist/retryOnEmpty.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +5 -0
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +5 -0
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/error.cjs +0 -7
- package/dist/utils/error.cjs.map +1 -1
- package/dist/utils/error.d.cts +0 -7
- package/dist/utils/error.d.cts.map +1 -1
- package/dist/utils/error.d.mts +0 -7
- package/dist/utils/error.d.mts.map +1 -1
- package/dist/utils/error.mjs +0 -7
- package/dist/utils/error.mjs.map +1 -1
- package/dist/utils/normalize.cjs +2 -7
- package/dist/utils/normalize.cjs.map +1 -1
- package/dist/utils/normalize.d.cts.map +1 -1
- package/dist/utils/normalize.d.mts.map +1 -1
- package/dist/utils/normalize.mjs +2 -7
- package/dist/utils/normalize.mjs.map +1 -1
- package/dist/utils/validation.cjs +2 -34
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts +3 -29
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts +3 -29
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +2 -34
- package/dist/utils/validation.mjs.map +1 -1
- package/dist/wallet.cjs +92 -199
- package/dist/wallet.cjs.map +1 -1
- package/dist/wallet.d.cts +13 -40
- package/dist/wallet.d.cts.map +1 -1
- package/dist/wallet.d.mts +13 -40
- package/dist/wallet.d.mts.map +1 -1
- package/dist/wallet.mjs +93 -200
- package/dist/wallet.mjs.map +1 -1
- package/package.json +1 -4
package/dist/fetch.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.cts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"fetch.d.cts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AAGnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAkB,wBAAwB;AAE3E,OAAO,KAAK,EAAE,sBAAsB,EAAE,oBAAgB;AAWtD;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,UAAU,EACV,OAAY,GACb,EAAE;IACD,UAAU,EAAE,sBAAsB,CAAC;IACnC,OAAO,CAAC,EAAE;QACR,mBAAmB,CAAC,EAAE,MAAM,CAAC;KAC9B,CAAC;CACH,GAAG,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAqCzC"}
|
package/dist/fetch.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { JsonRpcMiddleware
|
|
2
|
-
import type { Json,
|
|
1
|
+
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import type { Json, JsonRpcParams } from "@metamask/utils";
|
|
3
3
|
import type { AbstractRpcServiceLike } from "./types.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* Creates middleware for sending a JSON-RPC request through the given RPC
|
|
@@ -18,7 +18,5 @@ export declare function createFetchMiddleware({ rpcService, options, }: {
|
|
|
18
18
|
options?: {
|
|
19
19
|
originHttpHeaderKey?: string;
|
|
20
20
|
};
|
|
21
|
-
}): JsonRpcMiddleware<
|
|
22
|
-
origin: string;
|
|
23
|
-
}>>;
|
|
21
|
+
}): JsonRpcMiddleware<JsonRpcParams, Json>;
|
|
24
22
|
//# sourceMappingURL=fetch.d.mts.map
|
package/dist/fetch.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.mts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"fetch.d.mts","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kCAAkC;AAGnE,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAkB,wBAAwB;AAE3E,OAAO,KAAK,EAAE,sBAAsB,EAAE,oBAAgB;AAWtD;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CAAC,EACpC,UAAU,EACV,OAAY,GACb,EAAE;IACD,UAAU,EAAE,sBAAsB,CAAC;IACnC,OAAO,CAAC,EAAE;QACR,mBAAmB,CAAC,EAAE,MAAM,CAAC;KAC9B,CAAC;CACH,GAAG,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAqCzC"}
|
package/dist/fetch.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
|
|
1
2
|
import { rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
-
import { klona } from "klona";
|
|
3
3
|
/**
|
|
4
4
|
* Creates middleware for sending a JSON-RPC request through the given RPC
|
|
5
5
|
* service.
|
|
@@ -13,14 +13,18 @@ import { klona } from "klona";
|
|
|
13
13
|
* @returns The fetch middleware.
|
|
14
14
|
*/
|
|
15
15
|
export function createFetchMiddleware({ rpcService, options = {}, }) {
|
|
16
|
-
return async (
|
|
17
|
-
const origin = context.get('origin');
|
|
16
|
+
return createAsyncMiddleware(async (req, res) => {
|
|
18
17
|
const headers = 'originHttpHeaderKey' in options &&
|
|
19
18
|
options.originHttpHeaderKey !== undefined &&
|
|
20
|
-
origin !== undefined
|
|
21
|
-
? { [options.originHttpHeaderKey]: origin }
|
|
19
|
+
req.origin !== undefined
|
|
20
|
+
? { [options.originHttpHeaderKey]: req.origin }
|
|
22
21
|
: {};
|
|
23
|
-
const jsonRpcResponse = await rpcService.request(
|
|
22
|
+
const jsonRpcResponse = await rpcService.request({
|
|
23
|
+
id: req.id,
|
|
24
|
+
jsonrpc: req.jsonrpc,
|
|
25
|
+
method: req.method,
|
|
26
|
+
params: req.params,
|
|
27
|
+
}, {
|
|
24
28
|
headers,
|
|
25
29
|
});
|
|
26
30
|
// NOTE: We intentionally do not test to see if `jsonRpcResponse.error` is
|
|
@@ -34,7 +38,7 @@ export function createFetchMiddleware({ rpcService, options = {}, }) {
|
|
|
34
38
|
}
|
|
35
39
|
// Discard the `id` and `jsonrpc` fields in the response body
|
|
36
40
|
// (the JSON-RPC engine will fill those in)
|
|
37
|
-
|
|
38
|
-
};
|
|
41
|
+
res.result = jsonRpcResponse.result;
|
|
42
|
+
});
|
|
39
43
|
}
|
|
40
44
|
//# sourceMappingURL=fetch.mjs.map
|
package/dist/fetch.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.mjs","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.mjs","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AAClE,OAAO,EAAE,SAAS,EAAE,6BAA6B;AAcjD;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,qBAAqB,CAAC,EACpC,UAAU,EACV,OAAO,GAAG,EAAE,GAMb;IACC,OAAO,qBAAqB,CAC1B,KAAK,EAAE,GAA4C,EAAE,GAAG,EAAE,EAAE;QAC1D,MAAM,OAAO,GACX,qBAAqB,IAAI,OAAO;YAChC,OAAO,CAAC,mBAAmB,KAAK,SAAS;YACzC,GAAG,CAAC,MAAM,KAAK,SAAS;YACtB,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,mBAAmB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE;YAC/C,CAAC,CAAC,EAAE,CAAC;QAET,MAAM,eAAe,GAAG,MAAM,UAAU,CAAC,OAAO,CAC9C;YACE,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,MAAM,EAAE,GAAG,CAAC,MAAM;SACnB,EACD;YACE,OAAO;SACR,CACF,CAAC;QAEF,0EAA0E;QAC1E,4CAA4C;QAC5C,sEAAsE;QACtE,uEAAuE;QACvE,IAAI,OAAO,IAAI,eAAe,EAAE;YAC9B,MAAM,SAAS,CAAC,QAAQ,CAAC;gBACvB,IAAI,EAAE,eAAe,CAAC,KAAK;aAC5B,CAAC,CAAC;SACJ;QAED,6DAA6D;QAC7D,2CAA2C;QAC3C,GAAG,CAAC,MAAM,GAAG,eAAe,CAAC,MAAM,CAAC;IACtC,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';\nimport { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport type { AbstractRpcServiceLike } from './types';\n\n/**\n * Like a JSON-RPC request, but includes an optional `origin` property.\n * This will be included in the request as a header if specified.\n */\ntype JsonRpcRequestWithOrigin<Params extends JsonRpcParams> =\n JsonRpcRequest<Params> & {\n origin?: string;\n };\n\n/**\n * Creates middleware for sending a JSON-RPC request through the given RPC\n * service.\n *\n * @param args - The arguments to this function.\n * @param args.rpcService - The RPC service to use.\n * @param args.options - Options.\n * @param args.options.originHttpHeaderKey - If provided, the origin field for\n * each JSON-RPC request will be attached to each outgoing fetch request under\n * this header.\n * @returns The fetch middleware.\n */\nexport function createFetchMiddleware({\n rpcService,\n options = {},\n}: {\n rpcService: AbstractRpcServiceLike;\n options?: {\n originHttpHeaderKey?: string;\n };\n}): JsonRpcMiddleware<JsonRpcParams, Json> {\n return createAsyncMiddleware(\n async (req: JsonRpcRequestWithOrigin<JsonRpcParams>, res) => {\n const headers =\n 'originHttpHeaderKey' in options &&\n options.originHttpHeaderKey !== undefined &&\n req.origin !== undefined\n ? { [options.originHttpHeaderKey]: req.origin }\n : {};\n\n const jsonRpcResponse = await rpcService.request(\n {\n id: req.id,\n jsonrpc: req.jsonrpc,\n method: req.method,\n params: req.params,\n },\n {\n headers,\n },\n );\n\n // NOTE: We intentionally do not test to see if `jsonRpcResponse.error` is\n // strictly a JSON-RPC error response as per\n // <https://www.jsonrpc.org/specification#error_object> to account for\n // Ganache returning error objects with extra properties such as `name`\n if ('error' in jsonRpcResponse) {\n throw rpcErrors.internal({\n data: jsonRpcResponse.error,\n });\n }\n\n // Discard the `id` and `jsonrpc` fields in the response body\n // (the JSON-RPC engine will fill those in)\n res.result = jsonRpcResponse.result;\n },\n );\n}\n"]}
|
package/dist/inflight-cache.cjs
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createInflightCacheMiddleware = void 0;
|
|
4
|
-
const
|
|
4
|
+
const json_rpc_engine_1 = require("@metamask/json-rpc-engine");
|
|
5
|
+
const full_1 = require("klona/full");
|
|
5
6
|
const logging_utils_1 = require("./logging-utils.cjs");
|
|
6
7
|
const cache_1 = require("./utils/cache.cjs");
|
|
7
8
|
const log = (0, logging_utils_1.createModuleLogger)(logging_utils_1.projectLogger, 'inflight-cache');
|
|
8
|
-
/**
|
|
9
|
-
* Creates a middleware that caches inflight requests.
|
|
10
|
-
* If a request is already in flight, the middleware will wait for the request to complete
|
|
11
|
-
* and then return the result.
|
|
12
|
-
*
|
|
13
|
-
* @returns A middleware that caches inflight requests.
|
|
14
|
-
*/
|
|
15
9
|
function createInflightCacheMiddleware() {
|
|
16
10
|
const inflightRequests = {};
|
|
17
|
-
return async (
|
|
18
|
-
if
|
|
11
|
+
return (0, json_rpc_engine_1.createAsyncMiddleware)(async (req, res, next) => {
|
|
12
|
+
// allow cach to be skipped if so specified
|
|
13
|
+
if (req.skipCache) {
|
|
19
14
|
return next();
|
|
20
15
|
}
|
|
21
|
-
|
|
16
|
+
// get cacheId, if cacheable
|
|
17
|
+
const cacheId = (0, cache_1.cacheIdentifierForRequest)(req);
|
|
18
|
+
// if not cacheable, skip
|
|
22
19
|
if (!cacheId) {
|
|
23
|
-
log('Request is not cacheable, proceeding. req = %o',
|
|
20
|
+
log('Request is not cacheable, proceeding. req = %o', req);
|
|
24
21
|
return next();
|
|
25
22
|
}
|
|
26
23
|
// check for matching requests
|
|
@@ -29,68 +26,55 @@ function createInflightCacheMiddleware() {
|
|
|
29
26
|
if (activeRequestHandlers) {
|
|
30
27
|
// setup the response listener and wait for it to be called
|
|
31
28
|
// it will handle copying the result and request fields
|
|
32
|
-
log('Running %i handler(s) for request %o', activeRequestHandlers.length,
|
|
33
|
-
|
|
29
|
+
log('Running %i handler(s) for request %o', activeRequestHandlers.length, req);
|
|
30
|
+
await createActiveRequestHandler(res, activeRequestHandlers);
|
|
31
|
+
return undefined;
|
|
34
32
|
}
|
|
35
33
|
// setup response handler array for subsequent requests
|
|
36
34
|
activeRequestHandlers = [];
|
|
37
35
|
inflightRequests[cacheId] = activeRequestHandlers;
|
|
38
36
|
// allow request to be handled normally
|
|
39
|
-
log('Carrying original request forward %o',
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
* Creates a new request handler for the active request.
|
|
57
|
-
*
|
|
58
|
-
* @param activeRequestHandlers - The active request handlers.
|
|
59
|
-
* @returns A promise that resolves to the result of the request.
|
|
60
|
-
*/
|
|
61
|
-
function createActiveRequestHandler(activeRequestHandlers) {
|
|
62
|
-
const { resolve, promise, reject } = (0, utils_1.createDeferredPromise)();
|
|
63
|
-
activeRequestHandlers.push({
|
|
64
|
-
onSuccess: (result) => resolve(result),
|
|
65
|
-
onError: (error) => reject(error),
|
|
37
|
+
log('Carrying original request forward %o', req);
|
|
38
|
+
await next();
|
|
39
|
+
// clear inflight requests
|
|
40
|
+
delete inflightRequests[cacheId];
|
|
41
|
+
// schedule activeRequestHandlers to be handled
|
|
42
|
+
log('Running %i collected handler(s) for request %o', activeRequestHandlers.length, req);
|
|
43
|
+
handleActiveRequest(res, activeRequestHandlers);
|
|
44
|
+
// complete
|
|
45
|
+
return undefined;
|
|
46
|
+
});
|
|
47
|
+
async function createActiveRequestHandler(res, activeRequestHandlers) {
|
|
48
|
+
const { resolve, promise } = deferredPromise();
|
|
49
|
+
activeRequestHandlers.push((handledRes) => {
|
|
50
|
+
// append a copy of the result and error to the response
|
|
51
|
+
res.result = (0, full_1.klona)(handledRes.result);
|
|
52
|
+
res.error = (0, full_1.klona)(handledRes.error);
|
|
53
|
+
resolve();
|
|
66
54
|
});
|
|
67
55
|
return promise;
|
|
68
56
|
}
|
|
69
|
-
|
|
70
|
-
* Handles successful requests.
|
|
71
|
-
*
|
|
72
|
-
* @param result - The result of the request.
|
|
73
|
-
* @param activeRequestHandlers - The active request handlers.
|
|
74
|
-
*/
|
|
75
|
-
function handleSuccess(result, activeRequestHandlers) {
|
|
57
|
+
function handleActiveRequest(res, activeRequestHandlers) {
|
|
76
58
|
// use setTimeout so we can resolve our original request first
|
|
77
59
|
setTimeout(() => {
|
|
78
|
-
activeRequestHandlers.forEach((
|
|
79
|
-
|
|
60
|
+
activeRequestHandlers.forEach((handler) => {
|
|
61
|
+
try {
|
|
62
|
+
handler(res);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
// catch error so all requests are handled correctly
|
|
66
|
+
console.error(err);
|
|
67
|
+
}
|
|
80
68
|
});
|
|
81
69
|
});
|
|
82
70
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Handles failed requests.
|
|
85
|
-
*
|
|
86
|
-
* @param error - The error of the request.
|
|
87
|
-
* @param activeRequestHandlers - The active request handlers.
|
|
88
|
-
*/
|
|
89
|
-
function handleError(error, activeRequestHandlers) {
|
|
90
|
-
activeRequestHandlers.forEach(({ onError }) => {
|
|
91
|
-
onError(error);
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
71
|
}
|
|
95
72
|
exports.createInflightCacheMiddleware = createInflightCacheMiddleware;
|
|
73
|
+
function deferredPromise() {
|
|
74
|
+
let resolve;
|
|
75
|
+
const promise = new Promise((_resolve) => {
|
|
76
|
+
resolve = _resolve;
|
|
77
|
+
});
|
|
78
|
+
return { resolve, promise };
|
|
79
|
+
}
|
|
96
80
|
//# sourceMappingURL=inflight-cache.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.cjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"inflight-cache.cjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":";;;AAAA,+DAAkE;AAMlE,qCAAmC;AAEnC,uDAAoE;AAEpE,6CAA0D;AAO1D,MAAM,GAAG,GAAG,IAAA,kCAAkB,EAAC,6BAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,SAAgB,6BAA6B;IAI3C,MAAM,gBAAgB,GAAoB,EAAE,CAAC;IAE7C,OAAO,IAAA,uCAAqB,EAC1B,KAAK,EAAE,GAAyC,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC7D,2CAA2C;QAC3C,IAAI,GAAG,CAAC,SAAS,EAAE;YACjB,OAAO,IAAI,EAAE,CAAC;SACf;QACD,4BAA4B;QAC5B,MAAM,OAAO,GAAkB,IAAA,iCAAyB,EAAC,GAAG,CAAC,CAAC;QAC9D,yBAAyB;QACzB,IAAI,CAAC,OAAO,EAAE;YACZ,GAAG,CAAC,gDAAgD,EAAE,GAAG,CAAC,CAAC;YAC3D,OAAO,IAAI,EAAE,CAAC;SACf;QACD,8BAA8B;QAC9B,IAAI,qBAAqB,GAAsB,gBAAgB,CAAC,O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G,EAAE;YACd,qBAAqB,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBACxC,IAAI;oBACF,OAAO,CAAC,GAAG,CAAC,CAAC;iBACd;gBAAC,OAAO,GAAG,EAAE;oBACZ,oDAAoD;oBACpD,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;iBACpB;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AApFD,sEAoFC;AAED,SAAS,eAAe;IACtB,IAAI,OAAY,CAAC;IACjB,MAAM,OAAO,GAAkB,IAAI,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QACtD,OAAO,GAAG,QAAQ,CAAC;IACrB,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n JsonRpcParams,\n Json,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { klona } from 'klona/full';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { JsonRpcRequestToCache, JsonRpcCacheMiddleware } from './types';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandlers = (handledRes: PendingJsonRpcResponse) => void;\ntype InflightRequest = {\n [cacheId: string]: RequestHandlers[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\nexport function createInflightCacheMiddleware(): JsonRpcCacheMiddleware<\n JsonRpcParams,\n Json\n> {\n const inflightRequests: InflightRequest = {};\n\n return createAsyncMiddleware(\n async (req: JsonRpcRequestToCache<JsonRpcParams>, res, next) => {\n // allow cach to be skipped if so specified\n if (req.skipCache) {\n return next();\n }\n // get cacheId, if cacheable\n const cacheId: string | null = cacheIdentifierForRequest(req);\n // if not cacheable, skip\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', req);\n return next();\n }\n // check for matching requests\n let activeRequestHandlers: RequestHandlers[] = 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 req,\n );\n await createActiveRequestHandler(res, activeRequestHandlers);\n return undefined;\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', req);\n await next();\n\n // clear inflight requests\n delete inflightRequests[cacheId];\n // schedule activeRequestHandlers to be handled\n log(\n 'Running %i collected handler(s) for request %o',\n activeRequestHandlers.length,\n req,\n );\n handleActiveRequest(res, activeRequestHandlers);\n // complete\n return undefined;\n },\n );\n\n async function createActiveRequestHandler(\n res: PendingJsonRpcResponse,\n activeRequestHandlers: RequestHandlers[],\n ): Promise<void> {\n const { resolve, promise } = deferredPromise();\n activeRequestHandlers.push((handledRes: PendingJsonRpcResponse) => {\n // append a copy of the result and error to the response\n res.result = klona(handledRes.result);\n res.error = klona(handledRes.error);\n resolve();\n });\n return promise;\n }\n\n function handleActiveRequest(\n res: PendingJsonRpcResponse,\n activeRequestHandlers: RequestHandlers[],\n ): void {\n // use setTimeout so we can resolve our original request first\n setTimeout(() => {\n activeRequestHandlers.forEach((handler) => {\n try {\n handler(res);\n } catch (err) {\n // catch error so all requests are handled correctly\n console.error(err);\n }\n });\n });\n }\n}\n\nfunction deferredPromise() {\n let resolve: any;\n const promise: Promise<void> = new Promise((_resolve) => {\n resolve = _resolve;\n });\n return { resolve, promise };\n}\n"]}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
* Creates a middleware that caches inflight requests.
|
|
5
|
-
* If a request is already in flight, the middleware will wait for the request to complete
|
|
6
|
-
* and then return the result.
|
|
7
|
-
*
|
|
8
|
-
* @returns A middleware that caches inflight requests.
|
|
9
|
-
*/
|
|
10
|
-
export declare function createInflightCacheMiddleware(): JsonRpcMiddleware<JsonRpcRequest, Json, MiddlewareContext<{
|
|
11
|
-
skipCache: boolean;
|
|
12
|
-
}>>;
|
|
1
|
+
import type { JsonRpcParams, Json } from "@metamask/utils";
|
|
2
|
+
import type { JsonRpcCacheMiddleware } from "./types.cjs";
|
|
3
|
+
export declare function createInflightCacheMiddleware(): JsonRpcCacheMiddleware<JsonRpcParams, Json>;
|
|
13
4
|
//# sourceMappingURL=inflight-cache.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.d.cts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"inflight-cache.d.cts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,IAAI,EAEL,wBAAwB;AAIzB,OAAO,KAAK,EAAyB,sBAAsB,EAAE,oBAAgB;AAU7E,wBAAgB,6BAA6B,IAAI,sBAAsB,CACrE,aAAa,EACb,IAAI,CACL,CAiFA"}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
* Creates a middleware that caches inflight requests.
|
|
5
|
-
* If a request is already in flight, the middleware will wait for the request to complete
|
|
6
|
-
* and then return the result.
|
|
7
|
-
*
|
|
8
|
-
* @returns A middleware that caches inflight requests.
|
|
9
|
-
*/
|
|
10
|
-
export declare function createInflightCacheMiddleware(): JsonRpcMiddleware<JsonRpcRequest, Json, MiddlewareContext<{
|
|
11
|
-
skipCache: boolean;
|
|
12
|
-
}>>;
|
|
1
|
+
import type { JsonRpcParams, Json } from "@metamask/utils";
|
|
2
|
+
import type { JsonRpcCacheMiddleware } from "./types.mjs";
|
|
3
|
+
export declare function createInflightCacheMiddleware(): JsonRpcCacheMiddleware<JsonRpcParams, Json>;
|
|
13
4
|
//# sourceMappingURL=inflight-cache.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.d.mts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"inflight-cache.d.mts","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,IAAI,EAEL,wBAAwB;AAIzB,OAAO,KAAK,EAAyB,sBAAsB,EAAE,oBAAgB;AAU7E,wBAAgB,6BAA6B,IAAI,sBAAsB,CACrE,aAAa,EACb,IAAI,CACL,CAiFA"}
|
package/dist/inflight-cache.mjs
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createAsyncMiddleware } from "@metamask/json-rpc-engine";
|
|
2
|
+
import { klona } from "klona/full";
|
|
2
3
|
import { projectLogger, createModuleLogger } from "./logging-utils.mjs";
|
|
3
4
|
import { cacheIdentifierForRequest } from "./utils/cache.mjs";
|
|
4
5
|
const log = createModuleLogger(projectLogger, 'inflight-cache');
|
|
5
|
-
/**
|
|
6
|
-
* Creates a middleware that caches inflight requests.
|
|
7
|
-
* If a request is already in flight, the middleware will wait for the request to complete
|
|
8
|
-
* and then return the result.
|
|
9
|
-
*
|
|
10
|
-
* @returns A middleware that caches inflight requests.
|
|
11
|
-
*/
|
|
12
6
|
export function createInflightCacheMiddleware() {
|
|
13
7
|
const inflightRequests = {};
|
|
14
|
-
return async (
|
|
15
|
-
if
|
|
8
|
+
return createAsyncMiddleware(async (req, res, next) => {
|
|
9
|
+
// allow cach to be skipped if so specified
|
|
10
|
+
if (req.skipCache) {
|
|
16
11
|
return next();
|
|
17
12
|
}
|
|
18
|
-
|
|
13
|
+
// get cacheId, if cacheable
|
|
14
|
+
const cacheId = cacheIdentifierForRequest(req);
|
|
15
|
+
// if not cacheable, skip
|
|
19
16
|
if (!cacheId) {
|
|
20
|
-
log('Request is not cacheable, proceeding. req = %o',
|
|
17
|
+
log('Request is not cacheable, proceeding. req = %o', req);
|
|
21
18
|
return next();
|
|
22
19
|
}
|
|
23
20
|
// check for matching requests
|
|
@@ -26,67 +23,54 @@ export function createInflightCacheMiddleware() {
|
|
|
26
23
|
if (activeRequestHandlers) {
|
|
27
24
|
// setup the response listener and wait for it to be called
|
|
28
25
|
// it will handle copying the result and request fields
|
|
29
|
-
log('Running %i handler(s) for request %o', activeRequestHandlers.length,
|
|
30
|
-
|
|
26
|
+
log('Running %i handler(s) for request %o', activeRequestHandlers.length, req);
|
|
27
|
+
await createActiveRequestHandler(res, activeRequestHandlers);
|
|
28
|
+
return undefined;
|
|
31
29
|
}
|
|
32
30
|
// setup response handler array for subsequent requests
|
|
33
31
|
activeRequestHandlers = [];
|
|
34
32
|
inflightRequests[cacheId] = activeRequestHandlers;
|
|
35
33
|
// allow request to be handled normally
|
|
36
|
-
log('Carrying original request forward %o',
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* Creates a new request handler for the active request.
|
|
54
|
-
*
|
|
55
|
-
* @param activeRequestHandlers - The active request handlers.
|
|
56
|
-
* @returns A promise that resolves to the result of the request.
|
|
57
|
-
*/
|
|
58
|
-
function createActiveRequestHandler(activeRequestHandlers) {
|
|
59
|
-
const { resolve, promise, reject } = createDeferredPromise();
|
|
60
|
-
activeRequestHandlers.push({
|
|
61
|
-
onSuccess: (result) => resolve(result),
|
|
62
|
-
onError: (error) => reject(error),
|
|
34
|
+
log('Carrying original request forward %o', req);
|
|
35
|
+
await next();
|
|
36
|
+
// clear inflight requests
|
|
37
|
+
delete inflightRequests[cacheId];
|
|
38
|
+
// schedule activeRequestHandlers to be handled
|
|
39
|
+
log('Running %i collected handler(s) for request %o', activeRequestHandlers.length, req);
|
|
40
|
+
handleActiveRequest(res, activeRequestHandlers);
|
|
41
|
+
// complete
|
|
42
|
+
return undefined;
|
|
43
|
+
});
|
|
44
|
+
async function createActiveRequestHandler(res, activeRequestHandlers) {
|
|
45
|
+
const { resolve, promise } = deferredPromise();
|
|
46
|
+
activeRequestHandlers.push((handledRes) => {
|
|
47
|
+
// append a copy of the result and error to the response
|
|
48
|
+
res.result = klona(handledRes.result);
|
|
49
|
+
res.error = klona(handledRes.error);
|
|
50
|
+
resolve();
|
|
63
51
|
});
|
|
64
52
|
return promise;
|
|
65
53
|
}
|
|
66
|
-
|
|
67
|
-
* Handles successful requests.
|
|
68
|
-
*
|
|
69
|
-
* @param result - The result of the request.
|
|
70
|
-
* @param activeRequestHandlers - The active request handlers.
|
|
71
|
-
*/
|
|
72
|
-
function handleSuccess(result, activeRequestHandlers) {
|
|
54
|
+
function handleActiveRequest(res, activeRequestHandlers) {
|
|
73
55
|
// use setTimeout so we can resolve our original request first
|
|
74
56
|
setTimeout(() => {
|
|
75
|
-
activeRequestHandlers.forEach((
|
|
76
|
-
|
|
57
|
+
activeRequestHandlers.forEach((handler) => {
|
|
58
|
+
try {
|
|
59
|
+
handler(res);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
// catch error so all requests are handled correctly
|
|
63
|
+
console.error(err);
|
|
64
|
+
}
|
|
77
65
|
});
|
|
78
66
|
});
|
|
79
67
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
activeRequestHandlers.forEach(({ onError }) => {
|
|
88
|
-
onError(error);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
68
|
+
}
|
|
69
|
+
function deferredPromise() {
|
|
70
|
+
let resolve;
|
|
71
|
+
const promise = new Promise((_resolve) => {
|
|
72
|
+
resolve = _resolve;
|
|
73
|
+
});
|
|
74
|
+
return { resolve, promise };
|
|
91
75
|
}
|
|
92
76
|
//# sourceMappingURL=inflight-cache.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inflight-cache.mjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"inflight-cache.mjs","sourceRoot":"","sources":["../src/inflight-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,kCAAkC;AAMlE,OAAO,EAAE,KAAK,EAAE,mBAAmB;AAEnC,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,4BAAwB;AAEpE,OAAO,EAAE,yBAAyB,EAAE,0BAAsB;AAO1D,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE,MAAM,UAAU,6BAA6B;IAI3C,MAAM,gBAAgB,GAAoB,EAAE,CAAC;IAE7C,OAAO,qBAAqB,CAC1B,KAAK,EAAE,GAAyC,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC7D,2CAA2C;QAC3C,IAAI,GAAG,CAAC,SAAS,EAAE;YACjB,OAAO,IAAI,EAAE,CAAC;SACf;QACD,4BAA4B;QAC5B,MAAM,OAAO,GAAkB,yBAAyB,CAAC,GAAG,CAAC,CAAC;QAC9D,yBAAyB;QACzB,IAAI,CAAC,OAAO,EAAE;YACZ,GAAG,CAAC,gDAAgD,EAAE,GAAG,CAAC,CAAC;YAC3D,OAAO,IAAI,EAAE,CAAC;SACf;QACD,8BAA8B;QAC9B,IAAI,qBAAqB,GAAsB,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACzE,sDAAsD;QACtD,IAAI,qBAAqB,EAAE;YACzB,2DAA2D;YAC3D,uDAAuD;YACvD,GAAG,CACD,sCAAsC,EACtC,qBAAqB,CAAC,MAAM,EAC5B,GAAG,CACJ,CAAC;YACF,MAAM,0BAA0B,CAAC,GAAG,EAAE,qBAAqB,CAAC,CAAC;YAC7D,OAAO,SAAS,CAAC;SAClB;QACD,uDAAuD;QACvD,qBAAqB,GAAG,EA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ACtB,IAAI,OAAY,CAAC;IACjB,MAAM,OAAO,GAAkB,IAAI,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QACtD,OAAO,GAAG,QAAQ,CAAC;IACrB,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n JsonRpcParams,\n Json,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { klona } from 'klona/full';\n\nimport { projectLogger, createModuleLogger } from './logging-utils';\nimport type { JsonRpcRequestToCache, JsonRpcCacheMiddleware } from './types';\nimport { cacheIdentifierForRequest } from './utils/cache';\n\ntype RequestHandlers = (handledRes: PendingJsonRpcResponse) => void;\ntype InflightRequest = {\n [cacheId: string]: RequestHandlers[];\n};\n\nconst log = createModuleLogger(projectLogger, 'inflight-cache');\n\nexport function createInflightCacheMiddleware(): JsonRpcCacheMiddleware<\n JsonRpcParams,\n Json\n> {\n const inflightRequests: InflightRequest = {};\n\n return createAsyncMiddleware(\n async (req: JsonRpcRequestToCache<JsonRpcParams>, res, next) => {\n // allow cach to be skipped if so specified\n if (req.skipCache) {\n return next();\n }\n // get cacheId, if cacheable\n const cacheId: string | null = cacheIdentifierForRequest(req);\n // if not cacheable, skip\n if (!cacheId) {\n log('Request is not cacheable, proceeding. req = %o', req);\n return next();\n }\n // check for matching requests\n let activeRequestHandlers: RequestHandlers[] = 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 req,\n );\n await createActiveRequestHandler(res, activeRequestHandlers);\n return undefined;\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', req);\n await next();\n\n // clear inflight requests\n delete inflightRequests[cacheId];\n // schedule activeRequestHandlers to be handled\n log(\n 'Running %i collected handler(s) for request %o',\n activeRequestHandlers.length,\n req,\n );\n handleActiveRequest(res, activeRequestHandlers);\n // complete\n return undefined;\n },\n );\n\n async function createActiveRequestHandler(\n res: PendingJsonRpcResponse,\n activeRequestHandlers: RequestHandlers[],\n ): Promise<void> {\n const { resolve, promise } = deferredPromise();\n activeRequestHandlers.push((handledRes: PendingJsonRpcResponse) => {\n // append a copy of the result and error to the response\n res.result = klona(handledRes.result);\n res.error = klona(handledRes.error);\n resolve();\n });\n return promise;\n }\n\n function handleActiveRequest(\n res: PendingJsonRpcResponse,\n activeRequestHandlers: RequestHandlers[],\n ): void {\n // use setTimeout so we can resolve our original request first\n setTimeout(() => {\n activeRequestHandlers.forEach((handler) => {\n try {\n handler(res);\n } catch (err) {\n // catch error so all requests are handled correctly\n console.error(err);\n }\n });\n });\n }\n}\n\nfunction deferredPromise() {\n let resolve: any;\n const promise: Promise<void> = new Promise((_resolve) => {\n resolve = _resolve;\n });\n return { resolve, promise };\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.walletRequestExecutionPermissions = exports.RequestExecutionPermissionsStruct = void 0;
|
|
4
4
|
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
5
|
const superstruct_1 = require("@metamask/superstruct");
|
|
6
6
|
const utils_1 = require("@metamask/utils");
|
|
@@ -29,24 +29,13 @@ const PermissionRequestStruct = (0, superstruct_1.object)({
|
|
|
29
29
|
rules: (0, superstruct_1.optional)((0, superstruct_1.union)([(0, superstruct_1.array)(RuleStruct), (0, superstruct_1.literal)(null)])),
|
|
30
30
|
});
|
|
31
31
|
exports.RequestExecutionPermissionsStruct = (0, superstruct_1.array)(PermissionRequestStruct);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* `wallet_requestExecutionPermissions` JSON-RPC method.
|
|
40
|
-
*/
|
|
41
|
-
function createWalletRequestExecutionPermissionsHandler({ processRequestExecutionPermissions, }) {
|
|
42
|
-
return async ({ request }) => {
|
|
43
|
-
if (!processRequestExecutionPermissions) {
|
|
44
|
-
throw rpc_errors_1.rpcErrors.methodNotSupported('wallet_requestExecutionPermissions - no middleware configured');
|
|
45
|
-
}
|
|
46
|
-
const { params } = request;
|
|
47
|
-
(0, validation_1.validateParams)(params, exports.RequestExecutionPermissionsStruct);
|
|
48
|
-
return await processRequestExecutionPermissions(params, request);
|
|
49
|
-
};
|
|
32
|
+
async function walletRequestExecutionPermissions(req, res, { processRequestExecutionPermissions, }) {
|
|
33
|
+
if (!processRequestExecutionPermissions) {
|
|
34
|
+
throw rpc_errors_1.rpcErrors.methodNotSupported('wallet_requestExecutionPermissions - no middleware configured');
|
|
35
|
+
}
|
|
36
|
+
const { params } = req;
|
|
37
|
+
(0, validation_1.validateParams)(params, exports.RequestExecutionPermissionsStruct);
|
|
38
|
+
res.result = await processRequestExecutionPermissions(params, req);
|
|
50
39
|
}
|
|
51
|
-
exports.
|
|
40
|
+
exports.walletRequestExecutionPermissions = walletRequestExecutionPermissions;
|
|
52
41
|
//# sourceMappingURL=wallet-request-execution-permissions.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-request-execution-permissions.cjs","sourceRoot":"","sources":["../../src/methods/wallet-request-execution-permissions.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"wallet-request-execution-permissions.cjs","sourceRoot":"","sources":["../../src/methods/wallet-request-execution-permissions.ts"],"names":[],"mappings":";;;AAAA,qDAAiD;AAEjD,uDAU+B;AAC/B,2CAOyB;AAEzB,wDAAqD;AAErD,MAAM,gBAAgB,GAAG,IAAA,oBAAM,EAAC;IAC9B,IAAI,EAAE,IAAA,oBAAM,GAAE;IACd,mBAAmB,EAAE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AC,CAAC;IAE1D,GAAG,CAAC,MAAM,GAAG,MAAM,kCAAkC,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AACrE,CAAC;AApBD,8EAoBC","sourcesContent":["import { rpcErrors } from '@metamask/rpc-errors';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n array,\n boolean,\n literal,\n object,\n optional,\n record,\n string,\n union,\n unknown,\n} from '@metamask/superstruct';\nimport {\n HexChecksumAddressStruct,\n type Hex,\n type Json,\n type JsonRpcRequest,\n type PendingJsonRpcResponse,\n StrictHexStruct,\n} from '@metamask/utils';\n\nimport { validateParams } from '../utils/validation';\n\nconst PermissionStruct = object({\n type: string(),\n isAdjustmentAllowed: boolean(),\n data: record(string(), unknown()),\n});\n\nconst RuleStruct = object({\n type: string(),\n isAdjustmentAllowed: boolean(),\n data: record(string(), unknown()),\n});\n\nconst AccountSignerStruct = object({\n type: literal('account'),\n data: object({\n address: HexChecksumAddressStruct,\n }),\n});\n\nconst PermissionRequestStruct = object({\n chainId: StrictHexStruct,\n address: optional(HexChecksumAddressStruct),\n signer: AccountSignerStruct,\n permission: PermissionStruct,\n rules: optional(union([array(RuleStruct), literal(null)])),\n});\n\nexport const RequestExecutionPermissionsStruct = array(PermissionRequestStruct);\n\n// RequestExecutionPermissions API types\nexport type RequestExecutionPermissionsRequestParams = Infer<\n typeof RequestExecutionPermissionsStruct\n>;\n\nexport type RequestExecutionPermissionsResult = Json &\n (Infer<typeof PermissionRequestStruct> & {\n context: Hex;\n })[];\n\nexport type ProcessRequestExecutionPermissionsHook = (\n request: RequestExecutionPermissionsRequestParams,\n req: JsonRpcRequest,\n) => Promise<RequestExecutionPermissionsResult>;\n\nexport async function walletRequestExecutionPermissions(\n req: JsonRpcRequest,\n res: PendingJsonRpcResponse,\n {\n processRequestExecutionPermissions,\n }: {\n processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;\n },\n): Promise<void> {\n if (!processRequestExecutionPermissions) {\n throw rpcErrors.methodNotSupported(\n 'wallet_requestExecutionPermissions - no middleware configured',\n );\n }\n\n const { params } = req;\n\n validateParams(params, RequestExecutionPermissionsStruct);\n\n res.result = await processRequestExecutionPermissions(params, req);\n}\n"]}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine/v2";
|
|
2
1
|
import type { Infer } from "@metamask/superstruct";
|
|
3
|
-
import { type Hex, type Json, type JsonRpcRequest } from "@metamask/utils";
|
|
4
|
-
import type { WalletMiddlewareContext } from "../wallet.cjs";
|
|
2
|
+
import { type Hex, type Json, type JsonRpcRequest, type PendingJsonRpcResponse } from "@metamask/utils";
|
|
5
3
|
declare const PermissionRequestStruct: import("@metamask/superstruct").Struct<{
|
|
6
4
|
chainId: `0x${string}`;
|
|
7
5
|
signer: {
|
|
@@ -126,17 +124,8 @@ export type RequestExecutionPermissionsResult = Json & (Infer<typeof PermissionR
|
|
|
126
124
|
context: Hex;
|
|
127
125
|
})[];
|
|
128
126
|
export type ProcessRequestExecutionPermissionsHook = (request: RequestExecutionPermissionsRequestParams, req: JsonRpcRequest) => Promise<RequestExecutionPermissionsResult>;
|
|
129
|
-
|
|
130
|
-
* Creates a handler for the `wallet_requestExecutionPermissions` JSON-RPC method.
|
|
131
|
-
*
|
|
132
|
-
* @param options - The options for the handler.
|
|
133
|
-
* @param options.processRequestExecutionPermissions - The function to process the
|
|
134
|
-
* request execution permissions request.
|
|
135
|
-
* @returns A JSON-RPC middleware function that handles the
|
|
136
|
-
* `wallet_requestExecutionPermissions` JSON-RPC method.
|
|
137
|
-
*/
|
|
138
|
-
export declare function createWalletRequestExecutionPermissionsHandler({ processRequestExecutionPermissions, }: {
|
|
127
|
+
export declare function walletRequestExecutionPermissions(req: JsonRpcRequest, res: PendingJsonRpcResponse, { processRequestExecutionPermissions, }: {
|
|
139
128
|
processRequestExecutionPermissions?: ProcessRequestExecutionPermissionsHook;
|
|
140
|
-
}):
|
|
129
|
+
}): Promise<void>;
|
|
141
130
|
export {};
|
|
142
131
|
//# sourceMappingURL=wallet-request-execution-permissions.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wallet-request-execution-permissions.d.cts","sourceRoot":"","sources":["../../src/methods/wallet-request-execution-permissions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wallet-request-execution-permissions.d.cts","sourceRoot":"","sources":["../../src/methods/wallet-request-execution-permissions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,8BAA8B;AAYnD,OAAO,EAEL,KAAK,GAAG,EACR,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,sBAAsB,EAE5B,wBAAwB;AAuBzB,QAAA,MAAM,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAM3B,CAAC;AAEH,eAAO,MAAM,iCAAiC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAAiC,CAAC;AAGhF,MAAM,MAAM,wCAAwC,GAAG,KAAK,CAC1D,OAAO,iCAAiC,CACzC,CAAC;AAEF,MAAM,MAAM,iCAAiC,GAAG,IAAI,GAClD,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,GAAG;IACvC,OAAO,EAAE,GAAG,CAAC;CACd,CAAC,EAAE,CAAC;AAEP,MAAM,MAAM,sCAAsC,GAAG,CACnD,OAAO,EAAE,wCAAwC,EACjD,GAAG,EAAE,cAAc,KAChB,OAAO,CAAC,iCAAiC,CAAC,CAAC;AAEhD,wBAAsB,iCAAiC,CACrD,GAAG,EAAE,cAAc,EACnB,GAAG,EAAE,sBAAsB,EAC3B,EACE,kCAAkC,GACnC,EAAE;IACD,kCAAkC,CAAC,EAAE,sCAAsC,CAAC;CAC7E,GACA,OAAO,CAAC,IAAI,CAAC,CAYf"}
|