@metamask-previews/json-rpc-engine 10.1.1-preview-63ea58af → 10.1.1-preview-0458fe94
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 +11 -0
- package/README.md +647 -124
- package/dist/JsonRpcEngine.cjs +11 -13
- package/dist/JsonRpcEngine.cjs.map +1 -1
- package/dist/JsonRpcEngine.d.cts +18 -0
- package/dist/JsonRpcEngine.d.cts.map +1 -1
- package/dist/JsonRpcEngine.d.mts +18 -0
- package/dist/JsonRpcEngine.d.mts.map +1 -1
- package/dist/JsonRpcEngine.mjs +11 -13
- package/dist/JsonRpcEngine.mjs.map +1 -1
- package/dist/asV2Middleware.cjs +48 -0
- package/dist/asV2Middleware.cjs.map +1 -0
- package/dist/asV2Middleware.d.cts +11 -0
- package/dist/asV2Middleware.d.cts.map +1 -0
- package/dist/asV2Middleware.d.mts +11 -0
- package/dist/asV2Middleware.d.mts.map +1 -0
- package/dist/asV2Middleware.mjs +44 -0
- package/dist/asV2Middleware.mjs.map +1 -0
- package/dist/createAsyncMiddleware.cjs +1 -0
- package/dist/createAsyncMiddleware.cjs.map +1 -1
- package/dist/createAsyncMiddleware.d.cts +1 -0
- package/dist/createAsyncMiddleware.d.cts.map +1 -1
- package/dist/createAsyncMiddleware.d.mts +1 -0
- package/dist/createAsyncMiddleware.d.mts.map +1 -1
- package/dist/createAsyncMiddleware.mjs +1 -0
- package/dist/createAsyncMiddleware.mjs.map +1 -1
- package/dist/createScaffoldMiddleware.cjs +1 -0
- package/dist/createScaffoldMiddleware.cjs.map +1 -1
- package/dist/createScaffoldMiddleware.d.cts +1 -0
- package/dist/createScaffoldMiddleware.d.cts.map +1 -1
- package/dist/createScaffoldMiddleware.d.mts +1 -0
- package/dist/createScaffoldMiddleware.d.mts.map +1 -1
- package/dist/createScaffoldMiddleware.mjs +1 -0
- package/dist/createScaffoldMiddleware.mjs.map +1 -1
- package/dist/idRemapMiddleware.cjs +1 -0
- package/dist/idRemapMiddleware.cjs.map +1 -1
- package/dist/idRemapMiddleware.d.cts +1 -0
- package/dist/idRemapMiddleware.d.cts.map +1 -1
- package/dist/idRemapMiddleware.d.mts +1 -0
- package/dist/idRemapMiddleware.d.mts.map +1 -1
- package/dist/idRemapMiddleware.mjs +1 -0
- package/dist/idRemapMiddleware.mjs.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/mergeMiddleware.cjs +1 -0
- package/dist/mergeMiddleware.cjs.map +1 -1
- package/dist/mergeMiddleware.d.cts +1 -0
- package/dist/mergeMiddleware.d.cts.map +1 -1
- package/dist/mergeMiddleware.d.mts +1 -0
- package/dist/mergeMiddleware.d.mts.map +1 -1
- package/dist/mergeMiddleware.mjs +1 -0
- package/dist/mergeMiddleware.mjs.map +1 -1
- package/dist/v2/JsonRpcEngineV2.cjs +213 -0
- package/dist/v2/JsonRpcEngineV2.cjs.map +1 -0
- package/dist/v2/JsonRpcEngineV2.d.cts +122 -0
- package/dist/v2/JsonRpcEngineV2.d.cts.map +1 -0
- package/dist/v2/JsonRpcEngineV2.d.mts +122 -0
- package/dist/v2/JsonRpcEngineV2.d.mts.map +1 -0
- package/dist/v2/JsonRpcEngineV2.mjs +213 -0
- package/dist/v2/JsonRpcEngineV2.mjs.map +1 -0
- package/dist/v2/JsonRpcServer.cjs +162 -0
- package/dist/v2/JsonRpcServer.cjs.map +1 -0
- package/dist/v2/JsonRpcServer.d.cts +85 -0
- package/dist/v2/JsonRpcServer.d.cts.map +1 -0
- package/dist/v2/JsonRpcServer.d.mts +85 -0
- package/dist/v2/JsonRpcServer.d.mts.map +1 -0
- package/dist/v2/JsonRpcServer.mjs +158 -0
- package/dist/v2/JsonRpcServer.mjs.map +1 -0
- package/dist/v2/MiddlewareContext.cjs +66 -0
- package/dist/v2/MiddlewareContext.cjs.map +1 -0
- package/dist/v2/MiddlewareContext.d.cts +95 -0
- package/dist/v2/MiddlewareContext.d.cts.map +1 -0
- package/dist/v2/MiddlewareContext.d.mts +95 -0
- package/dist/v2/MiddlewareContext.d.mts.map +1 -0
- package/dist/v2/MiddlewareContext.mjs +62 -0
- package/dist/v2/MiddlewareContext.mjs.map +1 -0
- package/dist/v2/asLegacyMiddleware.cjs +39 -0
- package/dist/v2/asLegacyMiddleware.cjs.map +1 -0
- package/dist/v2/asLegacyMiddleware.d.cts +11 -0
- package/dist/v2/asLegacyMiddleware.d.cts.map +1 -0
- package/dist/v2/asLegacyMiddleware.d.mts +11 -0
- package/dist/v2/asLegacyMiddleware.d.mts.map +1 -0
- package/dist/v2/asLegacyMiddleware.mjs +35 -0
- package/dist/v2/asLegacyMiddleware.mjs.map +1 -0
- package/dist/v2/compatibility-utils.cjs +151 -0
- package/dist/v2/compatibility-utils.cjs.map +1 -0
- package/dist/v2/compatibility-utils.d.cts +75 -0
- package/dist/v2/compatibility-utils.d.cts.map +1 -0
- package/dist/v2/compatibility-utils.d.mts +75 -0
- package/dist/v2/compatibility-utils.d.mts.map +1 -0
- package/dist/v2/compatibility-utils.mjs +142 -0
- package/dist/v2/compatibility-utils.mjs.map +1 -0
- package/dist/v2/index.cjs +29 -0
- package/dist/v2/index.cjs.map +1 -0
- package/dist/v2/index.d.cts +8 -0
- package/dist/v2/index.d.cts.map +1 -0
- package/dist/v2/index.d.mts +8 -0
- package/dist/v2/index.d.mts.map +1 -0
- package/dist/v2/index.mjs +6 -0
- package/dist/v2/index.mjs.map +1 -0
- package/dist/v2/utils.cjs +41 -0
- package/dist/v2/utils.cjs.map +1 -0
- package/dist/v2/utils.d.cts +35 -0
- package/dist/v2/utils.d.cts.map +1 -0
- package/dist/v2/utils.d.mts +35 -0
- package/dist/v2/utils.d.mts.map +1 -0
- package/dist/v2/utils.mjs +34 -0
- package/dist/v2/utils.mjs.map +1 -0
- package/package.json +17 -3
- package/v2.js +3 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _JsonRpcServer_instances, _JsonRpcServer_engine, _JsonRpcServer_onError, _JsonRpcServer_coerceRequest;
|
|
13
|
+
import { rpcErrors, serializeError } from "@metamask/rpc-errors";
|
|
14
|
+
import { hasProperty, isObject } from "@metamask/utils";
|
|
15
|
+
import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
|
|
16
|
+
import { getUniqueId } from "../getUniqueId.mjs";
|
|
17
|
+
const jsonrpc = '2.0';
|
|
18
|
+
/**
|
|
19
|
+
* A JSON-RPC server that handles requests and notifications.
|
|
20
|
+
*
|
|
21
|
+
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
22
|
+
* yet permissive JSON-RPC 2.0 server.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const server = new JsonRpcServer({
|
|
27
|
+
* engine,
|
|
28
|
+
* onError,
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* const response = await server.handle(request);
|
|
32
|
+
* if ('result' in response) {
|
|
33
|
+
* // Handle result
|
|
34
|
+
* } else {
|
|
35
|
+
* // Handle error
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class JsonRpcServer {
|
|
40
|
+
/**
|
|
41
|
+
* Construct a new JSON-RPC server.
|
|
42
|
+
*
|
|
43
|
+
* @param options - The options for the server.
|
|
44
|
+
* @param options.onError - The callback to handle errors thrown by the
|
|
45
|
+
* engine. Errors always result in a failed response object, containing a
|
|
46
|
+
* JSON-RPC 2.0 serialized version of the original error. If you need to
|
|
47
|
+
* access the original error, use the `onError` callback.
|
|
48
|
+
* @param options.engine - The engine to use. Mutually exclusive with
|
|
49
|
+
* `middleware`.
|
|
50
|
+
* @param options.middleware - The middleware to use. Mutually exclusive with
|
|
51
|
+
* `engine`.
|
|
52
|
+
*/
|
|
53
|
+
constructor(options) {
|
|
54
|
+
_JsonRpcServer_instances.add(this);
|
|
55
|
+
_JsonRpcServer_engine.set(this, void 0);
|
|
56
|
+
_JsonRpcServer_onError.set(this, void 0);
|
|
57
|
+
__classPrivateFieldSet(this, _JsonRpcServer_onError, options.onError, "f");
|
|
58
|
+
if (hasProperty(options, 'engine')) {
|
|
59
|
+
// @ts-expect-error - hasProperty fails to narrow the type.
|
|
60
|
+
__classPrivateFieldSet(this, _JsonRpcServer_engine, options.engine, "f");
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
__classPrivateFieldSet(this, _JsonRpcServer_engine, JsonRpcEngineV2.create({ middleware: options.middleware }), "f");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async handle(rawRequest) {
|
|
67
|
+
// If rawRequest is not a notification, the originalId will be attached
|
|
68
|
+
// to the response. We attach our own, trusted id in #coerceRequest()
|
|
69
|
+
// while the request is being handled.
|
|
70
|
+
const [originalId, isRequest] = getOriginalId(rawRequest);
|
|
71
|
+
try {
|
|
72
|
+
const request = __classPrivateFieldGet(this, _JsonRpcServer_instances, "m", _JsonRpcServer_coerceRequest).call(this, rawRequest, isRequest);
|
|
73
|
+
const result = await __classPrivateFieldGet(this, _JsonRpcServer_engine, "f").handle(request);
|
|
74
|
+
if (result !== undefined) {
|
|
75
|
+
return {
|
|
76
|
+
jsonrpc,
|
|
77
|
+
// @ts-expect-error - Reassign the original id, regardless of its type.
|
|
78
|
+
id: originalId,
|
|
79
|
+
result,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
__classPrivateFieldGet(this, _JsonRpcServer_onError, "f")?.call(this, error);
|
|
85
|
+
if (isRequest) {
|
|
86
|
+
return {
|
|
87
|
+
jsonrpc,
|
|
88
|
+
// @ts-expect-error - Reassign the original id, regardless of its type.
|
|
89
|
+
id: originalId,
|
|
90
|
+
error: serializeError(error, {
|
|
91
|
+
shouldIncludeStack: false,
|
|
92
|
+
shouldPreserveMessage: true,
|
|
93
|
+
}),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
_JsonRpcServer_engine = new WeakMap(), _JsonRpcServer_onError = new WeakMap(), _JsonRpcServer_instances = new WeakSet(), _JsonRpcServer_coerceRequest = function _JsonRpcServer_coerceRequest(rawRequest, isRequest) {
|
|
101
|
+
if (!isMinimalRequest(rawRequest)) {
|
|
102
|
+
throw rpcErrors.invalidRequest({
|
|
103
|
+
data: {
|
|
104
|
+
request: rawRequest,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const request = {
|
|
109
|
+
jsonrpc,
|
|
110
|
+
method: rawRequest.method,
|
|
111
|
+
};
|
|
112
|
+
if (hasProperty(rawRequest, 'params')) {
|
|
113
|
+
request.params = rawRequest.params;
|
|
114
|
+
}
|
|
115
|
+
if (isRequest) {
|
|
116
|
+
request.id = getUniqueId();
|
|
117
|
+
}
|
|
118
|
+
return request;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Check if an unvalidated request is a minimal request.
|
|
122
|
+
*
|
|
123
|
+
* @param rawRequest - The raw request to check.
|
|
124
|
+
* @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.
|
|
125
|
+
*/
|
|
126
|
+
function isMinimalRequest(rawRequest) {
|
|
127
|
+
return (isObject(rawRequest) &&
|
|
128
|
+
hasProperty(rawRequest, 'method') &&
|
|
129
|
+
typeof rawRequest.method === 'string' &&
|
|
130
|
+
hasValidParams(rawRequest));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Check if a request has valid params, i.e. an array or object.
|
|
134
|
+
* The contents of the params are not inspected.
|
|
135
|
+
*
|
|
136
|
+
* @param rawRequest - The request to check.
|
|
137
|
+
* @returns `true` if the request has valid params, `false` otherwise.
|
|
138
|
+
*/
|
|
139
|
+
function hasValidParams(rawRequest) {
|
|
140
|
+
if (hasProperty(rawRequest, 'params')) {
|
|
141
|
+
return Array.isArray(rawRequest.params) || isObject(rawRequest.params);
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get the original id from a request.
|
|
147
|
+
*
|
|
148
|
+
* @param rawRequest - The request to get the original id from.
|
|
149
|
+
* @returns The original id and a boolean indicating if the request is a request
|
|
150
|
+
* (as opposed to a notification).
|
|
151
|
+
*/
|
|
152
|
+
function getOriginalId(rawRequest) {
|
|
153
|
+
if (isObject(rawRequest) && hasProperty(rawRequest, 'id')) {
|
|
154
|
+
return [rawRequest.id, true];
|
|
155
|
+
}
|
|
156
|
+
return [undefined, false];
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=JsonRpcServer.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.mjs","sourceRoot":"","sources":["../../src/v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U,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KACxE;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAS,aAAa,CAAC,UAAmB;IACxC,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,WAAW,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE;QACzD,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;KAC9B;IAED,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { rpcErrors, serializeError } from '@metamask/rpc-errors';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n JsonRpcResponse,\n NonEmptyArray,\n} from '@metamask/utils';\nimport { hasProperty, isObject } from '@metamask/utils';\n\nimport type { JsonRpcMiddleware } from './JsonRpcEngineV2';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2';\nimport type { JsonRpcCall } from './utils';\nimport { getUniqueId } from '../getUniqueId';\n\ntype OnError = (error: unknown) => void;\n\ntype Options = {\n onError?: OnError;\n} & (\n | {\n engine: JsonRpcEngineV2;\n }\n | {\n middleware: NonEmptyArray<JsonRpcMiddleware>;\n }\n);\n\nconst jsonrpc = '2.0' as const;\n\n/**\n * A JSON-RPC server that handles requests and notifications.\n *\n * Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant\n * yet permissive JSON-RPC 2.0 server.\n *\n * @example\n * ```ts\n * const server = new JsonRpcServer({\n * engine,\n * onError,\n * });\n *\n * const response = await server.handle(request);\n * if ('result' in response) {\n * // Handle result\n * } else {\n * // Handle error\n * }\n * ```\n */\nexport class JsonRpcServer {\n readonly #engine: JsonRpcEngineV2;\n\n readonly #onError?: OnError | undefined;\n\n /**\n * Construct a new JSON-RPC server.\n *\n * @param options - The options for the server.\n * @param options.onError - The callback to handle errors thrown by the\n * engine. Errors always result in a failed response object, containing a\n * JSON-RPC 2.0 serialized version of the original error. If you need to\n * access the original error, use the `onError` callback.\n * @param options.engine - The engine to use. Mutually exclusive with\n * `middleware`.\n * @param options.middleware - The middleware to use. Mutually exclusive with\n * `engine`.\n */\n constructor(options: Options) {\n this.#onError = options.onError;\n\n if (hasProperty(options, 'engine')) {\n // @ts-expect-error - hasProperty fails to narrow the type.\n this.#engine = options.engine;\n } else {\n this.#engine = JsonRpcEngineV2.create({ middleware: options.middleware });\n }\n }\n\n /**\n * Handle a JSON-RPC request.\n *\n * This method never throws. For requests, a response is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * @param request - The request to handle.\n * @returns The JSON-RPC response.\n */\n async handle(request: JsonRpcRequest): Promise<JsonRpcResponse>;\n\n /**\n * Handle a JSON-RPC notification.\n *\n * This method never throws. For notifications, `undefined` is always returned.\n * All errors are passed to the engine's `onError` callback.\n *\n * @param notification - The notification to handle.\n */\n async handle(notification: JsonRpcNotification): Promise<void>;\n\n /**\n * Handle an alleged JSON-RPC request or notification. Permits any plain\n * object with `{ method: string }`, so long as any present JSON-RPC 2.0\n * properties are valid. If the object has no `id`, it will be treated as\n * a notification and vice versa.\n *\n * This method never throws. All errors are passed to the engine's\n * `onError` callback. A JSON-RPC response is always returned for requests,\n * and `undefined` is returned for notifications.\n *\n * @param rawRequest - The raw request to handle.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void>;\n\n async handle(rawRequest: unknown): Promise<JsonRpcResponse | void> {\n // If rawRequest is not a notification, the originalId will be attached\n // to the response. We attach our own, trusted id in #coerceRequest()\n // while the request is being handled.\n const [originalId, isRequest] = getOriginalId(rawRequest);\n\n try {\n const request = this.#coerceRequest(rawRequest, isRequest);\n const result = await this.#engine.handle(request);\n\n if (result !== undefined) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n result,\n };\n }\n } catch (error) {\n this.#onError?.(error);\n\n if (isRequest) {\n return {\n jsonrpc,\n // @ts-expect-error - Reassign the original id, regardless of its type.\n id: originalId,\n error: serializeError(error, {\n shouldIncludeStack: false,\n shouldPreserveMessage: true,\n }),\n };\n }\n }\n return undefined;\n }\n\n #coerceRequest(rawRequest: unknown, isRequest: boolean): JsonRpcCall {\n if (!isMinimalRequest(rawRequest)) {\n throw rpcErrors.invalidRequest({\n data: {\n request: rawRequest,\n },\n });\n }\n\n const request: JsonRpcCall = {\n jsonrpc,\n method: rawRequest.method,\n };\n\n if (hasProperty(rawRequest, 'params')) {\n request.params = rawRequest.params as JsonRpcParams;\n }\n\n if (isRequest) {\n (request as JsonRpcRequest).id = getUniqueId();\n }\n\n return request;\n }\n}\n\n/**\n * The most minimally conformant request object that we will accept.\n */\ntype MinimalRequest = {\n method: string;\n params?: JsonRpcParams;\n} & Record<string, unknown>;\n\n/**\n * Check if an unvalidated request is a minimal request.\n *\n * @param rawRequest - The raw request to check.\n * @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.\n */\nfunction isMinimalRequest(rawRequest: unknown): rawRequest is MinimalRequest {\n return (\n isObject(rawRequest) &&\n hasProperty(rawRequest, 'method') &&\n typeof rawRequest.method === 'string' &&\n hasValidParams(rawRequest)\n );\n}\n\n/**\n * Check if a request has valid params, i.e. an array or object.\n * The contents of the params are not inspected.\n *\n * @param rawRequest - The request to check.\n * @returns `true` if the request has valid params, `false` otherwise.\n */\nfunction hasValidParams(\n rawRequest: Record<string, unknown>,\n): rawRequest is { params?: JsonRpcParams } {\n if (hasProperty(rawRequest, 'params')) {\n return Array.isArray(rawRequest.params) || isObject(rawRequest.params);\n }\n\n return true;\n}\n\n/**\n * Get the original id from a request.\n *\n * @param rawRequest - The request to get the original id from.\n * @returns The original id and a boolean indicating if the request is a request\n * (as opposed to a notification).\n */\nfunction getOriginalId(rawRequest: unknown): [unknown, boolean] {\n if (isObject(rawRequest) && hasProperty(rawRequest, 'id')) {\n return [rawRequest.id, true];\n }\n\n return [undefined, false];\n}\n"]}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MiddlewareContext = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* An context object for middleware that attempts to protect against accidental
|
|
6
|
+
* modifications. Its interface is frozen.
|
|
7
|
+
*
|
|
8
|
+
* Map keys may not be directly overridden with {@link set}. Instead, use
|
|
9
|
+
* {@link delete} to remove a key and then {@link set} to add a new value.
|
|
10
|
+
*
|
|
11
|
+
* The override protections are circumvented when using e.g. `Reflect.set`, so
|
|
12
|
+
* don't do that.
|
|
13
|
+
*
|
|
14
|
+
* @template KeyValues - The type of the keys and values in the context.
|
|
15
|
+
* @example
|
|
16
|
+
* // By default, the context permits any PropertyKey as a key.
|
|
17
|
+
* const context = new MiddlewareContext();
|
|
18
|
+
* context.set('foo', 'bar');
|
|
19
|
+
* context.get('foo'); // 'bar'
|
|
20
|
+
* context.get('fizz'); // undefined
|
|
21
|
+
* @example
|
|
22
|
+
* // By specifying an object type, the context permits only the keys of the object.
|
|
23
|
+
* type Context = MiddlewareContext<{ foo: string }>;
|
|
24
|
+
* const context = new Context([['foo', 'bar']]);
|
|
25
|
+
* context.get('foo'); // 'bar'
|
|
26
|
+
* context.get('fizz'); // Type error
|
|
27
|
+
*/
|
|
28
|
+
class MiddlewareContext extends Map {
|
|
29
|
+
constructor(entries) {
|
|
30
|
+
super(entries);
|
|
31
|
+
Object.freeze(this);
|
|
32
|
+
}
|
|
33
|
+
get(key) {
|
|
34
|
+
return super.get(key);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get a value from the context. Throws if the key is not found.
|
|
38
|
+
*
|
|
39
|
+
* @param key - The key to get the value for.
|
|
40
|
+
* @returns The value.
|
|
41
|
+
*/
|
|
42
|
+
assertGet(key) {
|
|
43
|
+
if (!super.has(key)) {
|
|
44
|
+
throw new Error(`Context key "${String(key)}" not found`);
|
|
45
|
+
}
|
|
46
|
+
return super.get(key);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Set a value in the context. Throws if the key already exists.
|
|
50
|
+
* {@link delete} an existing key before setting it to a new value.
|
|
51
|
+
*
|
|
52
|
+
* @throws If the key already exists.
|
|
53
|
+
* @param key - The key to set the value for.
|
|
54
|
+
* @param value - The value to set.
|
|
55
|
+
* @returns The context.
|
|
56
|
+
*/
|
|
57
|
+
set(key, value) {
|
|
58
|
+
if (super.has(key)) {
|
|
59
|
+
throw new Error(`MiddlewareContext key "${String(key)}" already exists`);
|
|
60
|
+
}
|
|
61
|
+
super.set(key, value);
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.MiddlewareContext = MiddlewareContext;
|
|
66
|
+
//# sourceMappingURL=MiddlewareContext.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareContext.cjs","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,iBAEX,SAAQ,GAAgD;IACxD,YACE,OAA0E;QAE1E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,GAAG,CAA4B,GAAM;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAA6B,CAAC;IACpD,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAA4B,GAAM;QACzC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;SAC3D;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAiB,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAA4B,GAAM,EAAE,KAAmB;QACxD,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;SAC1E;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA3CD,8CA2CC","sourcesContent":["import type { UnionToIntersection } from './utils';\n\n/**\n * An context object for middleware that attempts to protect against accidental\n * modifications. Its interface is frozen.\n *\n * Map keys may not be directly overridden with {@link set}. Instead, use\n * {@link delete} to remove a key and then {@link set} to add a new value.\n *\n * The override protections are circumvented when using e.g. `Reflect.set`, so\n * don't do that.\n *\n * @template KeyValues - The type of the keys and values in the context.\n * @example\n * // By default, the context permits any PropertyKey as a key.\n * const context = new MiddlewareContext();\n * context.set('foo', 'bar');\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // undefined\n * @example\n * // By specifying an object type, the context permits only the keys of the object.\n * type Context = MiddlewareContext<{ foo: string }>;\n * const context = new Context([['foo', 'bar']]);\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // Type error\n */\nexport class MiddlewareContext<\n KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,\n> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {\n constructor(\n entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>,\n ) {\n super(entries);\n Object.freeze(this);\n }\n\n get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined {\n return super.get(key) as KeyValues[K] | undefined;\n }\n\n /**\n * Get a value from the context. Throws if the key is not found.\n *\n * @param key - The key to get the value for.\n * @returns The value.\n */\n assertGet<K extends keyof KeyValues>(key: K): KeyValues[K] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[K];\n }\n\n /**\n * Set a value in the context. Throws if the key already exists.\n * {@link delete} an existing key before setting it to a new value.\n *\n * @throws If the key already exists.\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The context.\n */\n set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this {\n if (super.has(key)) {\n throw new Error(`MiddlewareContext key \"${String(key)}\" already exists`);\n }\n super.set(key, value);\n return this;\n }\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\ntype InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;\n\n/**\n * Simplifies an object type by \"merging\" its properties.\n *\n * - Expands intersections into a single object type.\n * - Forces mapped/conditional results to resolve into a readable shape.\n * - No runtime effect; purely a type-level normalization.\n *\n * @example\n * type A = { a: string } & { b: number };\n * type B = Simplify<A>; // { a: string; b: number }\n */\ntype Simplify<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;\n\n/**\n * Rejects record types that contain any `never`-valued property.\n *\n * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.\n * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.\n *\n * @example\n * type A = ExcludeNever<{ a: string; b: never }>; // never\n * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }\n */\ntype ExcludeNever<T extends Record<PropertyKey, unknown>> = {\n [K in keyof T]-?: [T[K]] extends [never] ? K : never;\n}[keyof T] extends never\n ? T\n : never;\n\n/**\n * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}\n * supertype.\n *\n * @param Contexts - The union of {@link MiddlewareContext}s to merge.\n * @returns The merged {@link MiddlewareContext} supertype.\n * @example\n * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;\n * type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>\n */\nexport type MergeContexts<Contexts extends ContextConstraint> =\n ExcludeNever<\n Simplify<UnionToIntersection<InferKeyValues<Contexts>>>\n > extends never\n ? never\n : MiddlewareContext<\n ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>\n >;\n\n// Non-polluting `any` constraint.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ContextConstraint = MiddlewareContext<any>;\n\n/**\n * The empty context type, i.e. `MiddlewareContext<{}>`.\n */\n// The empty object type is literally an empty object in this context.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type EmptyContext = MiddlewareContext<{}>;\n"]}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { UnionToIntersection } from "./utils.cjs";
|
|
2
|
+
/**
|
|
3
|
+
* An context object for middleware that attempts to protect against accidental
|
|
4
|
+
* modifications. Its interface is frozen.
|
|
5
|
+
*
|
|
6
|
+
* Map keys may not be directly overridden with {@link set}. Instead, use
|
|
7
|
+
* {@link delete} to remove a key and then {@link set} to add a new value.
|
|
8
|
+
*
|
|
9
|
+
* The override protections are circumvented when using e.g. `Reflect.set`, so
|
|
10
|
+
* don't do that.
|
|
11
|
+
*
|
|
12
|
+
* @template KeyValues - The type of the keys and values in the context.
|
|
13
|
+
* @example
|
|
14
|
+
* // By default, the context permits any PropertyKey as a key.
|
|
15
|
+
* const context = new MiddlewareContext();
|
|
16
|
+
* context.set('foo', 'bar');
|
|
17
|
+
* context.get('foo'); // 'bar'
|
|
18
|
+
* context.get('fizz'); // undefined
|
|
19
|
+
* @example
|
|
20
|
+
* // By specifying an object type, the context permits only the keys of the object.
|
|
21
|
+
* type Context = MiddlewareContext<{ foo: string }>;
|
|
22
|
+
* const context = new Context([['foo', 'bar']]);
|
|
23
|
+
* context.get('foo'); // 'bar'
|
|
24
|
+
* context.get('fizz'); // Type error
|
|
25
|
+
*/
|
|
26
|
+
export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {
|
|
27
|
+
constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>);
|
|
28
|
+
get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Get a value from the context. Throws if the key is not found.
|
|
31
|
+
*
|
|
32
|
+
* @param key - The key to get the value for.
|
|
33
|
+
* @returns The value.
|
|
34
|
+
*/
|
|
35
|
+
assertGet<K extends keyof KeyValues>(key: K): KeyValues[K];
|
|
36
|
+
/**
|
|
37
|
+
* Set a value in the context. Throws if the key already exists.
|
|
38
|
+
* {@link delete} an existing key before setting it to a new value.
|
|
39
|
+
*
|
|
40
|
+
* @throws If the key already exists.
|
|
41
|
+
* @param key - The key to set the value for.
|
|
42
|
+
* @param value - The value to set.
|
|
43
|
+
* @returns The context.
|
|
44
|
+
*/
|
|
45
|
+
set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Infer the KeyValues type from a {@link MiddlewareContext}.
|
|
49
|
+
*/
|
|
50
|
+
type InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;
|
|
51
|
+
/**
|
|
52
|
+
* Simplifies an object type by "merging" its properties.
|
|
53
|
+
*
|
|
54
|
+
* - Expands intersections into a single object type.
|
|
55
|
+
* - Forces mapped/conditional results to resolve into a readable shape.
|
|
56
|
+
* - No runtime effect; purely a type-level normalization.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* type A = { a: string } & { b: number };
|
|
60
|
+
* type B = Simplify<A>; // { a: string; b: number }
|
|
61
|
+
*/
|
|
62
|
+
type Simplify<T> = T extends infer O ? {
|
|
63
|
+
[K in keyof O]: O[K];
|
|
64
|
+
} : never;
|
|
65
|
+
/**
|
|
66
|
+
* Rejects record types that contain any `never`-valued property.
|
|
67
|
+
*
|
|
68
|
+
* If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.
|
|
69
|
+
* Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* type A = ExcludeNever<{ a: string; b: never }>; // never
|
|
73
|
+
* type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }
|
|
74
|
+
*/
|
|
75
|
+
type ExcludeNever<T extends Record<PropertyKey, unknown>> = {
|
|
76
|
+
[K in keyof T]-?: [T[K]] extends [never] ? K : never;
|
|
77
|
+
}[keyof T] extends never ? T : never;
|
|
78
|
+
/**
|
|
79
|
+
* Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}
|
|
80
|
+
* supertype.
|
|
81
|
+
*
|
|
82
|
+
* @param Contexts - The union of {@link MiddlewareContext}s to merge.
|
|
83
|
+
* @returns The merged {@link MiddlewareContext} supertype.
|
|
84
|
+
* @example
|
|
85
|
+
* type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;
|
|
86
|
+
* type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>
|
|
87
|
+
*/
|
|
88
|
+
export type MergeContexts<Contexts extends ContextConstraint> = ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>> extends never ? never : MiddlewareContext<ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>>;
|
|
89
|
+
export type ContextConstraint = MiddlewareContext<any>;
|
|
90
|
+
/**
|
|
91
|
+
* The empty context type, i.e. `MiddlewareContext<{}>`.
|
|
92
|
+
*/
|
|
93
|
+
export type EmptyContext = MiddlewareContext<{}>;
|
|
94
|
+
export {};
|
|
95
|
+
//# sourceMappingURL=MiddlewareContext.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareContext.d.cts","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAEnD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,iBAAiB,CAC5B,SAAS,SAAS,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAC7E,SAAQ,GAAG,CAAC,MAAM,SAAS,EA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AC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CACxD,SAAS,KAAK,GACX,KAAK,GACL,iBAAiB,CACf,YAAY,CAAC,QAAQ,CAAC,mBAAmB,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CACtE,CAAC;AAIR,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;AAEvD;;GAEG;AAGH,MAAM,MAAM,YAAY,GAAG,iBAAiB,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { UnionToIntersection } from "./utils.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* An context object for middleware that attempts to protect against accidental
|
|
4
|
+
* modifications. Its interface is frozen.
|
|
5
|
+
*
|
|
6
|
+
* Map keys may not be directly overridden with {@link set}. Instead, use
|
|
7
|
+
* {@link delete} to remove a key and then {@link set} to add a new value.
|
|
8
|
+
*
|
|
9
|
+
* The override protections are circumvented when using e.g. `Reflect.set`, so
|
|
10
|
+
* don't do that.
|
|
11
|
+
*
|
|
12
|
+
* @template KeyValues - The type of the keys and values in the context.
|
|
13
|
+
* @example
|
|
14
|
+
* // By default, the context permits any PropertyKey as a key.
|
|
15
|
+
* const context = new MiddlewareContext();
|
|
16
|
+
* context.set('foo', 'bar');
|
|
17
|
+
* context.get('foo'); // 'bar'
|
|
18
|
+
* context.get('fizz'); // undefined
|
|
19
|
+
* @example
|
|
20
|
+
* // By specifying an object type, the context permits only the keys of the object.
|
|
21
|
+
* type Context = MiddlewareContext<{ foo: string }>;
|
|
22
|
+
* const context = new Context([['foo', 'bar']]);
|
|
23
|
+
* context.get('foo'); // 'bar'
|
|
24
|
+
* context.get('fizz'); // Type error
|
|
25
|
+
*/
|
|
26
|
+
export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {
|
|
27
|
+
constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>);
|
|
28
|
+
get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Get a value from the context. Throws if the key is not found.
|
|
31
|
+
*
|
|
32
|
+
* @param key - The key to get the value for.
|
|
33
|
+
* @returns The value.
|
|
34
|
+
*/
|
|
35
|
+
assertGet<K extends keyof KeyValues>(key: K): KeyValues[K];
|
|
36
|
+
/**
|
|
37
|
+
* Set a value in the context. Throws if the key already exists.
|
|
38
|
+
* {@link delete} an existing key before setting it to a new value.
|
|
39
|
+
*
|
|
40
|
+
* @throws If the key already exists.
|
|
41
|
+
* @param key - The key to set the value for.
|
|
42
|
+
* @param value - The value to set.
|
|
43
|
+
* @returns The context.
|
|
44
|
+
*/
|
|
45
|
+
set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Infer the KeyValues type from a {@link MiddlewareContext}.
|
|
49
|
+
*/
|
|
50
|
+
type InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;
|
|
51
|
+
/**
|
|
52
|
+
* Simplifies an object type by "merging" its properties.
|
|
53
|
+
*
|
|
54
|
+
* - Expands intersections into a single object type.
|
|
55
|
+
* - Forces mapped/conditional results to resolve into a readable shape.
|
|
56
|
+
* - No runtime effect; purely a type-level normalization.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* type A = { a: string } & { b: number };
|
|
60
|
+
* type B = Simplify<A>; // { a: string; b: number }
|
|
61
|
+
*/
|
|
62
|
+
type Simplify<T> = T extends infer O ? {
|
|
63
|
+
[K in keyof O]: O[K];
|
|
64
|
+
} : never;
|
|
65
|
+
/**
|
|
66
|
+
* Rejects record types that contain any `never`-valued property.
|
|
67
|
+
*
|
|
68
|
+
* If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.
|
|
69
|
+
* Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* type A = ExcludeNever<{ a: string; b: never }>; // never
|
|
73
|
+
* type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }
|
|
74
|
+
*/
|
|
75
|
+
type ExcludeNever<T extends Record<PropertyKey, unknown>> = {
|
|
76
|
+
[K in keyof T]-?: [T[K]] extends [never] ? K : never;
|
|
77
|
+
}[keyof T] extends never ? T : never;
|
|
78
|
+
/**
|
|
79
|
+
* Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}
|
|
80
|
+
* supertype.
|
|
81
|
+
*
|
|
82
|
+
* @param Contexts - The union of {@link MiddlewareContext}s to merge.
|
|
83
|
+
* @returns The merged {@link MiddlewareContext} supertype.
|
|
84
|
+
* @example
|
|
85
|
+
* type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;
|
|
86
|
+
* type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>
|
|
87
|
+
*/
|
|
88
|
+
export type MergeContexts<Contexts extends ContextConstraint> = ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>> extends never ? never : MiddlewareContext<ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>>;
|
|
89
|
+
export type ContextConstraint = MiddlewareContext<any>;
|
|
90
|
+
/**
|
|
91
|
+
* The empty context type, i.e. `MiddlewareContext<{}>`.
|
|
92
|
+
*/
|
|
93
|
+
export type EmptyContext = MiddlewareContext<{}>;
|
|
94
|
+
export {};
|
|
95
|
+
//# sourceMappingURL=MiddlewareContext.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareContext.d.mts","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAEnD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,iBAAiB,CAC5B,SAAS,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KAAK,CAAC;AAEV;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa,CAAC,QAAQ,SAAS,iBAAiB,IAC1D,YAAY,CACV,QAAQ,CAAC,mBAAmB,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CACxD,SAAS,KAAK,GACX,KAAK,GACL,iBAAiB,CACf,YAAY,CAAC,QAAQ,CAAC,mBAAmB,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CACtE,CAAC;AAIR,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;AAEvD;;GAEG;AAGH,MAAM,MAAM,YAAY,GAAG,iBAAiB,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An context object for middleware that attempts to protect against accidental
|
|
3
|
+
* modifications. Its interface is frozen.
|
|
4
|
+
*
|
|
5
|
+
* Map keys may not be directly overridden with {@link set}. Instead, use
|
|
6
|
+
* {@link delete} to remove a key and then {@link set} to add a new value.
|
|
7
|
+
*
|
|
8
|
+
* The override protections are circumvented when using e.g. `Reflect.set`, so
|
|
9
|
+
* don't do that.
|
|
10
|
+
*
|
|
11
|
+
* @template KeyValues - The type of the keys and values in the context.
|
|
12
|
+
* @example
|
|
13
|
+
* // By default, the context permits any PropertyKey as a key.
|
|
14
|
+
* const context = new MiddlewareContext();
|
|
15
|
+
* context.set('foo', 'bar');
|
|
16
|
+
* context.get('foo'); // 'bar'
|
|
17
|
+
* context.get('fizz'); // undefined
|
|
18
|
+
* @example
|
|
19
|
+
* // By specifying an object type, the context permits only the keys of the object.
|
|
20
|
+
* type Context = MiddlewareContext<{ foo: string }>;
|
|
21
|
+
* const context = new Context([['foo', 'bar']]);
|
|
22
|
+
* context.get('foo'); // 'bar'
|
|
23
|
+
* context.get('fizz'); // Type error
|
|
24
|
+
*/
|
|
25
|
+
export class MiddlewareContext extends Map {
|
|
26
|
+
constructor(entries) {
|
|
27
|
+
super(entries);
|
|
28
|
+
Object.freeze(this);
|
|
29
|
+
}
|
|
30
|
+
get(key) {
|
|
31
|
+
return super.get(key);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get a value from the context. Throws if the key is not found.
|
|
35
|
+
*
|
|
36
|
+
* @param key - The key to get the value for.
|
|
37
|
+
* @returns The value.
|
|
38
|
+
*/
|
|
39
|
+
assertGet(key) {
|
|
40
|
+
if (!super.has(key)) {
|
|
41
|
+
throw new Error(`Context key "${String(key)}" not found`);
|
|
42
|
+
}
|
|
43
|
+
return super.get(key);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Set a value in the context. Throws if the key already exists.
|
|
47
|
+
* {@link delete} an existing key before setting it to a new value.
|
|
48
|
+
*
|
|
49
|
+
* @throws If the key already exists.
|
|
50
|
+
* @param key - The key to set the value for.
|
|
51
|
+
* @param value - The value to set.
|
|
52
|
+
* @returns The context.
|
|
53
|
+
*/
|
|
54
|
+
set(key, value) {
|
|
55
|
+
if (super.has(key)) {
|
|
56
|
+
throw new Error(`MiddlewareContext key "${String(key)}" already exists`);
|
|
57
|
+
}
|
|
58
|
+
super.set(key, value);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=MiddlewareContext.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareContext.mjs","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,iBAEX,SAAQ,GAAgD;IACxD,YACE,OAA0E;QAE1E,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,GAAG,CAA4B,GAAM;QACnC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAA6B,CAAC;IACpD,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAA4B,GAAM;QACzC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;SAC3D;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAiB,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAA4B,GAAM,EAAE,KAAmB;QACxD,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YAClB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;SAC1E;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import type { UnionToIntersection } from './utils';\n\n/**\n * An context object for middleware that attempts to protect against accidental\n * modifications. Its interface is frozen.\n *\n * Map keys may not be directly overridden with {@link set}. Instead, use\n * {@link delete} to remove a key and then {@link set} to add a new value.\n *\n * The override protections are circumvented when using e.g. `Reflect.set`, so\n * don't do that.\n *\n * @template KeyValues - The type of the keys and values in the context.\n * @example\n * // By default, the context permits any PropertyKey as a key.\n * const context = new MiddlewareContext();\n * context.set('foo', 'bar');\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // undefined\n * @example\n * // By specifying an object type, the context permits only the keys of the object.\n * type Context = MiddlewareContext<{ foo: string }>;\n * const context = new Context([['foo', 'bar']]);\n * context.get('foo'); // 'bar'\n * context.get('fizz'); // Type error\n */\nexport class MiddlewareContext<\n KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>,\n> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {\n constructor(\n entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>,\n ) {\n super(entries);\n Object.freeze(this);\n }\n\n get<K extends keyof KeyValues>(key: K): KeyValues[K] | undefined {\n return super.get(key) as KeyValues[K] | undefined;\n }\n\n /**\n * Get a value from the context. Throws if the key is not found.\n *\n * @param key - The key to get the value for.\n * @returns The value.\n */\n assertGet<K extends keyof KeyValues>(key: K): KeyValues[K] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[K];\n }\n\n /**\n * Set a value in the context. Throws if the key already exists.\n * {@link delete} an existing key before setting it to a new value.\n *\n * @throws If the key already exists.\n * @param key - The key to set the value for.\n * @param value - The value to set.\n * @returns The context.\n */\n set<K extends keyof KeyValues>(key: K, value: KeyValues[K]): this {\n if (super.has(key)) {\n throw new Error(`MiddlewareContext key \"${String(key)}\" already exists`);\n }\n super.set(key, value);\n return this;\n }\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\ntype InferKeyValues<T> = T extends MiddlewareContext<infer U> ? U : never;\n\n/**\n * Simplifies an object type by \"merging\" its properties.\n *\n * - Expands intersections into a single object type.\n * - Forces mapped/conditional results to resolve into a readable shape.\n * - No runtime effect; purely a type-level normalization.\n *\n * @example\n * type A = { a: string } & { b: number };\n * type B = Simplify<A>; // { a: string; b: number }\n */\ntype Simplify<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;\n\n/**\n * Rejects record types that contain any `never`-valued property.\n *\n * If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.\n * Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.\n *\n * @example\n * type A = ExcludeNever<{ a: string; b: never }>; // never\n * type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }\n */\ntype ExcludeNever<T extends Record<PropertyKey, unknown>> = {\n [K in keyof T]-?: [T[K]] extends [never] ? K : never;\n}[keyof T] extends never\n ? T\n : never;\n\n/**\n * Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}\n * supertype.\n *\n * @param Contexts - The union of {@link MiddlewareContext}s to merge.\n * @returns The merged {@link MiddlewareContext} supertype.\n * @example\n * type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;\n * type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>\n */\nexport type MergeContexts<Contexts extends ContextConstraint> =\n ExcludeNever<\n Simplify<UnionToIntersection<InferKeyValues<Contexts>>>\n > extends never\n ? never\n : MiddlewareContext<\n ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>\n >;\n\n// Non-polluting `any` constraint.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type ContextConstraint = MiddlewareContext<any>;\n\n/**\n * The empty context type, i.e. `MiddlewareContext<{}>`.\n */\n// The empty object type is literally an empty object in this context.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport type EmptyContext = MiddlewareContext<{}>;\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.asLegacyMiddleware = void 0;
|
|
4
|
+
const compatibility_utils_1 = require("./compatibility-utils.cjs");
|
|
5
|
+
const __1 = require("../index.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Convert a {@link JsonRpcEngineV2} into a legacy middleware.
|
|
8
|
+
*
|
|
9
|
+
* @param engine - The engine to convert.
|
|
10
|
+
* @returns The legacy middleware.
|
|
11
|
+
*/
|
|
12
|
+
function asLegacyMiddleware(engine) {
|
|
13
|
+
const middleware = engine.asMiddleware();
|
|
14
|
+
return (0, __1.createAsyncMiddleware)(async (req, res, next) => {
|
|
15
|
+
const request = (0, compatibility_utils_1.fromLegacyRequest)(req);
|
|
16
|
+
const context = (0, compatibility_utils_1.makeContext)(req);
|
|
17
|
+
let modifiedRequest;
|
|
18
|
+
const result = await middleware({
|
|
19
|
+
request,
|
|
20
|
+
context,
|
|
21
|
+
next: (finalRequest) => {
|
|
22
|
+
modifiedRequest = finalRequest;
|
|
23
|
+
return Promise.resolve(undefined);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
if (modifiedRequest !== undefined && modifiedRequest !== request) {
|
|
27
|
+
Object.assign(req, (0, compatibility_utils_1.deepClone)(modifiedRequest));
|
|
28
|
+
}
|
|
29
|
+
(0, compatibility_utils_1.propagateToRequest)(req, context);
|
|
30
|
+
if (result !== undefined) {
|
|
31
|
+
// Unclear why the `as unknown` is needed here, but the cast is safe.
|
|
32
|
+
res.result = (0, compatibility_utils_1.deepClone)(result);
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
return next();
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
exports.asLegacyMiddleware = asLegacyMiddleware;
|
|
39
|
+
//# sourceMappingURL=asLegacyMiddleware.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asLegacyMiddleware.cjs","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":";;;AAEA,mEAK+B;AAE/B,oCAA2C;AAG3C;;;;;GAKG;AACH,SAAgB,kBAAkB,CAIhC,MAAgC;IAEhC,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;IACzC,OAAO,IAAA,yBAAqB,EAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACpD,MAAM,OAAO,GAAG,IAAA,uCAAiB,EAAC,GAAc,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,IAAA,iCAAW,EAAC,GAAG,CAAC,CAAC;QACjC,IAAI,eAAoC,CAAC;QAEzC,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC;YAC9B,OAAO;YACP,OAAO;YACP,IAAI,EAAE,CAAC,YAAY,EAAE,EAAE;gBACrB,eAAe,GAAG,YAAY,CAAC;gBAC/B,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACpC,CAAC;SACF,CAAC,CAAC;QAEH,IAAI,eAAe,KAAK,SAAS,IAAI,eAAe,KAAK,OAAO,EAAE;YAChE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,IAAA,+BAAS,EAAC,eAAe,CAAC,CAAC,CAAC;SAChD;QACD,IAAA,wCAAkB,EAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QAEjC,IAAI,MAAM,KAAK,SAAS,EAAE;YACxB,qEAAqE;YACrE,GAAG,CAAC,MAAM,GAAG,IAAA,+BAAS,EAAC,MAAM,CAAyC,CAAC;YACvE,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC,CAAC;AACL,CAAC;AAjCD,gDAiCC","sourcesContent":["import type { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';\n\nimport {\n deepClone,\n fromLegacyRequest,\n makeContext,\n propagateToRequest,\n} from './compatibility-utils';\nimport type { JsonRpcEngineV2, ResultConstraint } from './JsonRpcEngineV2';\nimport { createAsyncMiddleware } from '..';\nimport type { JsonRpcMiddleware as LegacyMiddleware } from '..';\n\n/**\n * Convert a {@link JsonRpcEngineV2} into a legacy middleware.\n *\n * @param engine - The engine to convert.\n * @returns The legacy middleware.\n */\nexport function asLegacyMiddleware<\n Params extends JsonRpcParams,\n Request extends JsonRpcRequest<Params>,\n>(\n engine: JsonRpcEngineV2<Request>,\n): LegacyMiddleware<Params, ResultConstraint<Request>> {\n const middleware = engine.asMiddleware();\n return createAsyncMiddleware(async (req, res, next) => {\n const request = fromLegacyRequest(req as Request);\n const context = makeContext(req);\n let modifiedRequest: Request | undefined;\n\n const result = await middleware({\n request,\n context,\n next: (finalRequest) => {\n modifiedRequest = finalRequest;\n return Promise.resolve(undefined);\n },\n });\n\n if (modifiedRequest !== undefined && modifiedRequest !== request) {\n Object.assign(req, deepClone(modifiedRequest));\n }\n propagateToRequest(req, context);\n\n if (result !== undefined) {\n // Unclear why the `as unknown` is needed here, but the cast is safe.\n res.result = deepClone(result) as unknown as ResultConstraint<Request>;\n return undefined;\n }\n return next();\n });\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
|
|
2
|
+
import type { JsonRpcEngineV2, ResultConstraint } from "./JsonRpcEngineV2.cjs";
|
|
3
|
+
import type { JsonRpcMiddleware as LegacyMiddleware } from "../index.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Convert a {@link JsonRpcEngineV2} into a legacy middleware.
|
|
6
|
+
*
|
|
7
|
+
* @param engine - The engine to convert.
|
|
8
|
+
* @returns The legacy middleware.
|
|
9
|
+
*/
|
|
10
|
+
export declare function asLegacyMiddleware<Params extends JsonRpcParams, Request extends JsonRpcRequest<Params>>(engine: JsonRpcEngineV2<Request>): LegacyMiddleware<Params, ResultConstraint<Request>>;
|
|
11
|
+
//# sourceMappingURL=asLegacyMiddleware.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asLegacyMiddleware.d.cts","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAQrE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,8BAA0B;AAE3E,OAAO,KAAK,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,qBAAW;AAEhE;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,GAC/B,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CA4BrD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonRpcParams, JsonRpcRequest } from "@metamask/utils";
|
|
2
|
+
import type { JsonRpcEngineV2, ResultConstraint } from "./JsonRpcEngineV2.mjs";
|
|
3
|
+
import type { JsonRpcMiddleware as LegacyMiddleware } from "../index.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Convert a {@link JsonRpcEngineV2} into a legacy middleware.
|
|
6
|
+
*
|
|
7
|
+
* @param engine - The engine to convert.
|
|
8
|
+
* @returns The legacy middleware.
|
|
9
|
+
*/
|
|
10
|
+
export declare function asLegacyMiddleware<Params extends JsonRpcParams, Request extends JsonRpcRequest<Params>>(engine: JsonRpcEngineV2<Request>): LegacyMiddleware<Params, ResultConstraint<Request>>;
|
|
11
|
+
//# sourceMappingURL=asLegacyMiddleware.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asLegacyMiddleware.d.mts","sourceRoot":"","sources":["../../src/v2/asLegacyMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,wBAAwB;AAQrE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,8BAA0B;AAE3E,OAAO,KAAK,EAAE,iBAAiB,IAAI,gBAAgB,EAAE,qBAAW;AAEhE;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,SAAS,aAAa,EAC5B,OAAO,SAAS,cAAc,CAAC,MAAM,CAAC,EAEtC,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,GAC/B,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,CA4BrD"}
|