@metamask-previews/json-rpc-engine 10.5.0-preview-4f4c98e → 10.5.0-preview-eb263b0ee
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 -1
- package/dist/JsonRpcEngine.cjs +352 -0
- package/dist/JsonRpcEngine.cjs.map +1 -0
- package/dist/{JsonRpcEngine.d.ts → JsonRpcEngine.d.cts} +3 -3
- package/dist/JsonRpcEngine.d.cts.map +1 -0
- package/dist/JsonRpcEngine.d.mts +132 -0
- package/dist/JsonRpcEngine.d.mts.map +1 -0
- package/dist/{JsonRpcEngine.js → JsonRpcEngine.mjs} +5 -5
- package/dist/JsonRpcEngine.mjs.map +1 -0
- package/dist/asV2Middleware.cjs +57 -0
- package/dist/asV2Middleware.cjs.map +1 -0
- package/dist/{asV2Middleware.d.ts → asV2Middleware.d.cts} +6 -6
- package/dist/asV2Middleware.d.cts.map +1 -0
- package/dist/asV2Middleware.d.mts +20 -0
- package/dist/asV2Middleware.d.mts.map +1 -0
- package/dist/{asV2Middleware.js → asV2Middleware.mjs} +5 -5
- package/dist/asV2Middleware.mjs.map +1 -0
- package/dist/createAsyncMiddleware.cjs +71 -0
- package/dist/createAsyncMiddleware.cjs.map +1 -0
- package/dist/{createAsyncMiddleware.d.ts → createAsyncMiddleware.d.cts} +3 -3
- package/dist/createAsyncMiddleware.d.cts.map +1 -0
- package/dist/createAsyncMiddleware.d.mts +28 -0
- package/dist/createAsyncMiddleware.d.mts.map +1 -0
- package/dist/{createAsyncMiddleware.js → createAsyncMiddleware.mjs} +1 -1
- package/dist/createAsyncMiddleware.mjs.map +1 -0
- package/dist/createMethodMiddleware.cjs +56 -0
- package/dist/createMethodMiddleware.cjs.map +1 -0
- package/dist/{createMethodMiddleware.d.ts → createMethodMiddleware.d.cts} +6 -6
- package/dist/createMethodMiddleware.d.cts.map +1 -0
- package/dist/createMethodMiddleware.d.mts +86 -0
- package/dist/createMethodMiddleware.d.mts.map +1 -0
- package/dist/{createMethodMiddleware.js → createMethodMiddleware.mjs} +3 -3
- package/dist/createMethodMiddleware.mjs.map +1 -0
- package/dist/createOriginMiddleware.cjs +18 -0
- package/dist/createOriginMiddleware.cjs.map +1 -0
- package/dist/{createOriginMiddleware.d.ts → createOriginMiddleware.d.cts} +3 -3
- package/dist/createOriginMiddleware.d.cts.map +1 -0
- package/dist/createOriginMiddleware.d.mts +11 -0
- package/dist/createOriginMiddleware.d.mts.map +1 -0
- package/dist/{createOriginMiddleware.js → createOriginMiddleware.mjs} +1 -1
- package/dist/createOriginMiddleware.mjs.map +1 -0
- package/dist/createScaffoldMiddleware.cjs +31 -0
- package/dist/createScaffoldMiddleware.cjs.map +1 -0
- package/dist/{createScaffoldMiddleware.d.ts → createScaffoldMiddleware.d.cts} +3 -3
- package/dist/createScaffoldMiddleware.d.cts.map +1 -0
- package/dist/createScaffoldMiddleware.d.mts +18 -0
- package/dist/createScaffoldMiddleware.d.mts.map +1 -0
- package/dist/{createScaffoldMiddleware.js → createScaffoldMiddleware.mjs} +1 -1
- package/dist/createScaffoldMiddleware.mjs.map +1 -0
- package/dist/getUniqueId.cjs +20 -0
- package/dist/getUniqueId.cjs.map +1 -0
- package/dist/{getUniqueId.d.ts → getUniqueId.d.cts} +1 -1
- package/dist/getUniqueId.d.cts.map +1 -0
- package/dist/getUniqueId.d.mts +9 -0
- package/dist/getUniqueId.d.mts.map +1 -0
- package/dist/{getUniqueId.js → getUniqueId.mjs} +1 -1
- package/dist/getUniqueId.mjs.map +1 -0
- package/dist/idRemapMiddleware.cjs +29 -0
- package/dist/idRemapMiddleware.cjs.map +1 -0
- package/dist/{idRemapMiddleware.d.ts → idRemapMiddleware.d.cts} +3 -3
- package/dist/idRemapMiddleware.d.cts.map +1 -0
- package/dist/idRemapMiddleware.d.mts +14 -0
- package/dist/idRemapMiddleware.d.mts.map +1 -0
- package/dist/{idRemapMiddleware.js → idRemapMiddleware.mjs} +2 -2
- package/dist/idRemapMiddleware.mjs.map +1 -0
- package/dist/index.cjs +22 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +10 -0
- package/dist/index.mjs.map +1 -0
- package/dist/mergeMiddleware.cjs +18 -0
- package/dist/mergeMiddleware.cjs.map +1 -0
- package/dist/{mergeMiddleware.d.ts → mergeMiddleware.d.cts} +3 -3
- package/dist/mergeMiddleware.d.cts.map +1 -0
- package/dist/mergeMiddleware.d.mts +11 -0
- package/dist/mergeMiddleware.d.mts.map +1 -0
- package/dist/{mergeMiddleware.js → mergeMiddleware.mjs} +2 -2
- package/dist/mergeMiddleware.mjs.map +1 -0
- package/dist/v2/JsonRpcEngineV2.cjs +220 -0
- package/dist/v2/JsonRpcEngineV2.cjs.map +1 -0
- package/dist/v2/{JsonRpcEngineV2.d.ts → JsonRpcEngineV2.d.cts} +5 -5
- package/dist/v2/JsonRpcEngineV2.d.cts.map +1 -0
- package/dist/v2/JsonRpcEngineV2.d.mts +136 -0
- package/dist/v2/JsonRpcEngineV2.d.mts.map +1 -0
- package/dist/v2/{JsonRpcEngineV2.js → JsonRpcEngineV2.mjs} +13 -9
- package/dist/v2/JsonRpcEngineV2.mjs.map +1 -0
- package/dist/v2/JsonRpcServer.cjs +178 -0
- package/dist/v2/JsonRpcServer.cjs.map +1 -0
- package/dist/v2/{JsonRpcServer.d.ts → JsonRpcServer.d.cts} +4 -4
- package/dist/v2/JsonRpcServer.d.cts.map +1 -0
- package/dist/v2/JsonRpcServer.d.mts +104 -0
- package/dist/v2/JsonRpcServer.d.mts.map +1 -0
- package/dist/v2/{JsonRpcServer.js → JsonRpcServer.mjs} +5 -5
- package/dist/v2/JsonRpcServer.mjs.map +1 -0
- package/dist/v2/MiddlewareContext.cjs +108 -0
- package/dist/v2/MiddlewareContext.cjs.map +1 -0
- package/dist/v2/{MiddlewareContext.d.ts → MiddlewareContext.d.cts} +2 -2
- package/dist/v2/MiddlewareContext.d.cts.map +1 -0
- package/dist/v2/MiddlewareContext.d.mts +108 -0
- package/dist/v2/MiddlewareContext.d.mts.map +1 -0
- package/dist/v2/{MiddlewareContext.js → MiddlewareContext.mjs} +2 -2
- package/dist/v2/MiddlewareContext.mjs.map +1 -0
- package/dist/v2/asLegacyMiddleware.cjs +45 -0
- package/dist/v2/asLegacyMiddleware.cjs.map +1 -0
- package/dist/v2/{asLegacyMiddleware.d.ts → asLegacyMiddleware.d.cts} +5 -5
- package/dist/v2/asLegacyMiddleware.d.cts.map +1 -0
- package/dist/v2/asLegacyMiddleware.d.mts +19 -0
- package/dist/v2/asLegacyMiddleware.d.mts.map +1 -0
- package/dist/v2/{asLegacyMiddleware.js → asLegacyMiddleware.mjs} +4 -4
- package/dist/v2/asLegacyMiddleware.mjs.map +1 -0
- package/dist/v2/compatibility-utils.cjs +167 -0
- package/dist/v2/compatibility-utils.cjs.map +1 -0
- package/dist/v2/{compatibility-utils.d.ts → compatibility-utils.d.cts} +5 -5
- 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.js → compatibility-utils.mjs} +6 -6
- package/dist/v2/compatibility-utils.mjs.map +1 -0
- package/dist/v2/createMethodMiddleware.cjs +44 -0
- package/dist/v2/createMethodMiddleware.cjs.map +1 -0
- package/dist/v2/{createMethodMiddleware.d.ts → createMethodMiddleware.d.cts} +5 -5
- package/dist/v2/createMethodMiddleware.d.cts.map +1 -0
- package/dist/v2/createMethodMiddleware.d.mts +71 -0
- package/dist/v2/createMethodMiddleware.d.mts.map +1 -0
- package/dist/v2/{createMethodMiddleware.js → createMethodMiddleware.mjs} +3 -2
- package/dist/v2/createMethodMiddleware.mjs.map +1 -0
- package/dist/v2/createOriginMiddleware.cjs +18 -0
- package/dist/v2/createOriginMiddleware.cjs.map +1 -0
- package/dist/v2/{createOriginMiddleware.d.ts → createOriginMiddleware.d.cts} +4 -4
- package/dist/v2/createOriginMiddleware.d.cts.map +1 -0
- package/dist/v2/createOriginMiddleware.d.mts +13 -0
- package/dist/v2/createOriginMiddleware.d.mts.map +1 -0
- package/dist/v2/{createOriginMiddleware.js → createOriginMiddleware.mjs} +2 -1
- package/dist/v2/createOriginMiddleware.mjs.map +1 -0
- package/dist/v2/createScaffoldMiddleware.cjs +25 -0
- package/dist/v2/createScaffoldMiddleware.cjs.map +1 -0
- package/dist/v2/{createScaffoldMiddleware.d.ts → createScaffoldMiddleware.d.cts} +4 -4
- package/dist/v2/createScaffoldMiddleware.d.cts.map +1 -0
- package/dist/v2/createScaffoldMiddleware.d.mts +30 -0
- package/dist/v2/createScaffoldMiddleware.d.mts.map +1 -0
- package/dist/v2/{createScaffoldMiddleware.js → createScaffoldMiddleware.mjs} +1 -1
- package/dist/v2/createScaffoldMiddleware.mjs.map +1 -0
- package/dist/v2/index.cjs +26 -0
- package/dist/v2/index.cjs.map +1 -0
- package/dist/v2/index.d.cts +14 -0
- package/dist/v2/index.d.cts.map +1 -0
- package/dist/v2/index.d.mts +14 -0
- package/dist/v2/index.d.mts.map +1 -0
- package/dist/v2/index.mjs +10 -0
- package/dist/v2/index.mjs.map +1 -0
- package/dist/v2/utils.cjs +122 -0
- package/dist/v2/utils.cjs.map +1 -0
- package/dist/v2/{utils.d.ts → utils.d.cts} +5 -5
- package/dist/v2/utils.d.cts.map +1 -0
- package/dist/v2/utils.d.mts +85 -0
- package/dist/v2/utils.d.mts.map +1 -0
- package/dist/v2/{utils.js → utils.mjs} +3 -3
- package/dist/v2/utils.mjs.map +1 -0
- package/package.json +25 -15
- package/dist/JsonRpcEngine.d.ts.map +0 -1
- package/dist/JsonRpcEngine.js.map +0 -1
- package/dist/asV2Middleware.d.ts.map +0 -1
- package/dist/asV2Middleware.js.map +0 -1
- package/dist/createAsyncMiddleware.d.ts.map +0 -1
- package/dist/createAsyncMiddleware.js.map +0 -1
- package/dist/createMethodMiddleware.d.ts.map +0 -1
- package/dist/createMethodMiddleware.js.map +0 -1
- package/dist/createOriginMiddleware.d.ts.map +0 -1
- package/dist/createOriginMiddleware.js.map +0 -1
- package/dist/createScaffoldMiddleware.d.ts.map +0 -1
- package/dist/createScaffoldMiddleware.js.map +0 -1
- package/dist/getUniqueId.d.ts.map +0 -1
- package/dist/getUniqueId.js.map +0 -1
- package/dist/idRemapMiddleware.d.ts.map +0 -1
- package/dist/idRemapMiddleware.js.map +0 -1
- package/dist/index.d.ts +0 -13
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -10
- package/dist/index.js.map +0 -1
- package/dist/mergeMiddleware.d.ts.map +0 -1
- package/dist/mergeMiddleware.js.map +0 -1
- package/dist/v2/JsonRpcEngineV2.d.ts.map +0 -1
- package/dist/v2/JsonRpcEngineV2.js.map +0 -1
- package/dist/v2/JsonRpcServer.d.ts.map +0 -1
- package/dist/v2/JsonRpcServer.js.map +0 -1
- package/dist/v2/MiddlewareContext.d.ts.map +0 -1
- package/dist/v2/MiddlewareContext.js.map +0 -1
- package/dist/v2/asLegacyMiddleware.d.ts.map +0 -1
- package/dist/v2/asLegacyMiddleware.js.map +0 -1
- package/dist/v2/compatibility-utils.d.ts.map +0 -1
- package/dist/v2/compatibility-utils.js.map +0 -1
- package/dist/v2/createMethodMiddleware.d.ts.map +0 -1
- package/dist/v2/createMethodMiddleware.js.map +0 -1
- package/dist/v2/createOriginMiddleware.d.ts.map +0 -1
- package/dist/v2/createOriginMiddleware.js.map +0 -1
- package/dist/v2/createScaffoldMiddleware.d.ts.map +0 -1
- package/dist/v2/createScaffoldMiddleware.js.map +0 -1
- package/dist/v2/index.d.ts +0 -14
- package/dist/v2/index.d.ts.map +0 -1
- package/dist/v2/index.js +0 -10
- package/dist/v2/index.js.map +0 -1
- package/dist/v2/utils.d.ts.map +0 -1
- package/dist/v2/utils.js.map +0 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _a, _JsonRpcServer_engine, _JsonRpcServer_onError, _JsonRpcServer_coerceRequest;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.JsonRpcServer = void 0;
|
|
16
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
17
|
+
const utils_1 = require("@metamask/utils");
|
|
18
|
+
const getUniqueId_js_1 = require("../getUniqueId.cjs");
|
|
19
|
+
const JsonRpcEngineV2_js_1 = require("./JsonRpcEngineV2.cjs");
|
|
20
|
+
const jsonrpc = '2.0';
|
|
21
|
+
/**
|
|
22
|
+
* A JSON-RPC server that handles requests and notifications.
|
|
23
|
+
*
|
|
24
|
+
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
25
|
+
* yet permissive JSON-RPC 2.0 server.
|
|
26
|
+
*
|
|
27
|
+
* Note that the server will accept both requests and notifications via {@link handle},
|
|
28
|
+
* even if the underlying engine is only able to handle one or the other.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const server = new JsonRpcServer({
|
|
33
|
+
* engine,
|
|
34
|
+
* onError,
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* const response = await server.handle(request);
|
|
38
|
+
* if ('result' in response) {
|
|
39
|
+
* // Handle result
|
|
40
|
+
* } else {
|
|
41
|
+
* // Handle error
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
class JsonRpcServer {
|
|
46
|
+
/**
|
|
47
|
+
* Construct a new JSON-RPC server.
|
|
48
|
+
*
|
|
49
|
+
* @param options - The options for the server.
|
|
50
|
+
* @param options.onError - The callback to handle errors thrown by the
|
|
51
|
+
* engine. Errors always result in a failed response object, containing a
|
|
52
|
+
* JSON-RPC 2.0 serialized version of the original error. If you need to
|
|
53
|
+
* access the original error, use the `onError` callback. If the `onError`
|
|
54
|
+
* callback itself throws or rejects, the error is silently ignored.
|
|
55
|
+
* @param options.engine - The engine to use. Mutually exclusive with
|
|
56
|
+
* `middleware`.
|
|
57
|
+
* @param options.middleware - The middleware to use. Mutually exclusive with
|
|
58
|
+
* `engine`.
|
|
59
|
+
*/
|
|
60
|
+
constructor(options) {
|
|
61
|
+
_JsonRpcServer_engine.set(this, void 0);
|
|
62
|
+
_JsonRpcServer_onError.set(this, void 0);
|
|
63
|
+
__classPrivateFieldSet(this, _JsonRpcServer_onError, options.onError, "f");
|
|
64
|
+
if ((0, utils_1.hasProperty)(options, 'engine')) {
|
|
65
|
+
// @ts-expect-error - hasProperty fails to narrow the type.
|
|
66
|
+
__classPrivateFieldSet(this, _JsonRpcServer_engine, options.engine, "f");
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.
|
|
70
|
+
__classPrivateFieldSet(this, _JsonRpcServer_engine, JsonRpcEngineV2_js_1.JsonRpcEngineV2.create({ middleware: options.middleware }), "f");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async handle(rawRequest, options) {
|
|
74
|
+
// If rawRequest is not a notification, the originalId will be attached
|
|
75
|
+
// to the response. We attach our own, trusted id in #coerceRequest()
|
|
76
|
+
// while the request is being handled.
|
|
77
|
+
const [originalId, isRequest] = getOriginalId(rawRequest);
|
|
78
|
+
try {
|
|
79
|
+
const request = __classPrivateFieldGet(_a, _a, "m", _JsonRpcServer_coerceRequest).call(_a, rawRequest, isRequest);
|
|
80
|
+
// @ts-expect-error - The request may not be of the type expected by the engine,
|
|
81
|
+
// and we intentionally allow this to happen.
|
|
82
|
+
const result = await __classPrivateFieldGet(this, _JsonRpcServer_engine, "f").handle(request, options);
|
|
83
|
+
if (result !== undefined) {
|
|
84
|
+
return {
|
|
85
|
+
jsonrpc,
|
|
86
|
+
// @ts-expect-error - Reassign the original id, regardless of its type.
|
|
87
|
+
id: originalId,
|
|
88
|
+
result,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
try {
|
|
94
|
+
const maybePromise = __classPrivateFieldGet(this, _JsonRpcServer_onError, "f")?.call(this, error);
|
|
95
|
+
if (maybePromise instanceof Promise) {
|
|
96
|
+
maybePromise.catch(() => {
|
|
97
|
+
// Prevent unhandled promise rejection.
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// onError must not prevent handle() from honoring its "never throws" contract.
|
|
103
|
+
}
|
|
104
|
+
if (isRequest) {
|
|
105
|
+
return {
|
|
106
|
+
jsonrpc,
|
|
107
|
+
// @ts-expect-error - Reassign the original id, regardless of its type.
|
|
108
|
+
id: originalId,
|
|
109
|
+
error: (0, rpc_errors_1.serializeError)(error, {
|
|
110
|
+
shouldIncludeStack: false,
|
|
111
|
+
shouldPreserveMessage: true,
|
|
112
|
+
}),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.JsonRpcServer = JsonRpcServer;
|
|
120
|
+
_a = JsonRpcServer, _JsonRpcServer_engine = new WeakMap(), _JsonRpcServer_onError = new WeakMap(), _JsonRpcServer_coerceRequest = function _JsonRpcServer_coerceRequest(rawRequest, isRequest) {
|
|
121
|
+
if (!isMinimalRequest(rawRequest)) {
|
|
122
|
+
throw rpc_errors_1.rpcErrors.invalidRequest({
|
|
123
|
+
data: {
|
|
124
|
+
request: rawRequest,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const request = {
|
|
129
|
+
jsonrpc,
|
|
130
|
+
method: rawRequest.method,
|
|
131
|
+
};
|
|
132
|
+
if ((0, utils_1.hasProperty)(rawRequest, 'params')) {
|
|
133
|
+
request.params = rawRequest.params;
|
|
134
|
+
}
|
|
135
|
+
if (isRequest) {
|
|
136
|
+
request.id = (0, getUniqueId_js_1.getUniqueId)();
|
|
137
|
+
}
|
|
138
|
+
return request;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Check if an unvalidated request is a minimal request.
|
|
142
|
+
*
|
|
143
|
+
* @param rawRequest - The raw request to check.
|
|
144
|
+
* @returns `true` if the request is a {@link MinimalRequest}, `false` otherwise.
|
|
145
|
+
*/
|
|
146
|
+
function isMinimalRequest(rawRequest) {
|
|
147
|
+
return ((0, utils_1.isObject)(rawRequest) &&
|
|
148
|
+
(0, utils_1.hasProperty)(rawRequest, 'method') &&
|
|
149
|
+
typeof rawRequest.method === 'string' &&
|
|
150
|
+
hasValidParams(rawRequest));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Check if a request has valid params, i.e. an array or object.
|
|
154
|
+
* The contents of the params are not inspected.
|
|
155
|
+
*
|
|
156
|
+
* @param rawRequest - The request to check.
|
|
157
|
+
* @returns `true` if the request has valid params, `false` otherwise.
|
|
158
|
+
*/
|
|
159
|
+
function hasValidParams(rawRequest) {
|
|
160
|
+
if ((0, utils_1.hasProperty)(rawRequest, 'params')) {
|
|
161
|
+
return Array.isArray(rawRequest.params) || (0, utils_1.isObject)(rawRequest.params);
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get the original id from a request.
|
|
167
|
+
*
|
|
168
|
+
* @param rawRequest - The request to get the original id from.
|
|
169
|
+
* @returns The original id and a boolean indicating if the request is a request
|
|
170
|
+
* (as opposed to a notification).
|
|
171
|
+
*/
|
|
172
|
+
function getOriginalId(rawRequest) {
|
|
173
|
+
if ((0, utils_1.isObject)(rawRequest) && (0, utils_1.hasProperty)(rawRequest, 'id')) {
|
|
174
|
+
return [rawRequest.id, true];
|
|
175
|
+
}
|
|
176
|
+
return [undefined, false];
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=JsonRpcServer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.cjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qDAAiE;AAQjE,2CAAwD;AAExD,uDAAgD;AAQhD,8DAAuD;AAgBvD,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,aAAa;IAUxB;;;;;;;;;;;;;OAaG;IACH,YAAY,OAA4B;QArB/B,wCAGP;QAEO,yCAA+B;QAiBtC,uBAAA,IAAI,0BAAY,OAAO,CAAC,OAAO,MAAA,CAAC;QAEhC,IAAI,IAAA,mBAAW,EAAC,OAAO,EAAE,QAAQ,CAAC,EAAE,CAAC;YACnC,2DAA2D;YAC3D,uBAAA,IAAI,yBAAW,OAAO,CAAC,MAAM,MAAA,CAAC;QAChC,CAAC;aAAM,CAAC;YACN,kGAAkG;YAClG,uBAAA,IAAI,yBAAW,oCAAe,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,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E,IAAI,CAAC,EAAE,CAAC;QAC1D,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,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 { getUniqueId } from '../getUniqueId.js';\nimport type {\n HandleOptions,\n JsonRpcMiddleware,\n MergedContextOf,\n MiddlewareConstraint,\n RequestOf,\n} from './JsonRpcEngineV2.js';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2.js';\nimport type { JsonRpcCall } from './utils.js';\n\ntype OnError = (error: unknown) => void;\n\ntype Options<Middleware extends MiddlewareConstraint> = {\n onError?: OnError;\n} & (\n | {\n engine: ReturnType<typeof JsonRpcEngineV2.create<Middleware>>;\n }\n | {\n middleware: NonEmptyArray<Middleware>;\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 * Note that the server will accept both requests and notifications via {@link handle},\n * even if the underlying engine is only able to handle one or the other.\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 Middleware extends MiddlewareConstraint = JsonRpcMiddleware,\n> {\n readonly #engine: JsonRpcEngineV2<\n RequestOf<Middleware>,\n MergedContextOf<Middleware>\n >;\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. If the `onError`\n * callback itself throws or rejects, the error is silently ignored.\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<Middleware>) {\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 // @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.\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 * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine can only handle notifications.\n *\n * @param request - The request to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n * @returns The JSON-RPC response.\n */\n async handle(\n request: JsonRpcRequest,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine cannot handle notifications.\n *\n * @param notification - The notification to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n */\n async handle(\n notification: JsonRpcNotification,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 an `id` property, it will be\n * treated as a request, otherwise it will be treated as a notification.\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 * **WARNING**: The request will fail if its coerced type (i.e. request or\n * response) is not of the type expected by the underlying engine.\n *\n * @param rawRequest - The raw request to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(\n rawRequest: unknown,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): Promise<JsonRpcResponse | void>;\n\n async handle(\n rawRequest: unknown,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 = JsonRpcServer.#coerceRequest(rawRequest, isRequest);\n // @ts-expect-error - The request may not be of the type expected by the engine,\n // and we intentionally allow this to happen.\n const result = await this.#engine.handle(request, options);\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 try {\n const maybePromise: unknown = this.#onError?.(error);\n if (maybePromise instanceof Promise) {\n maybePromise.catch(() => {\n // Prevent unhandled promise rejection.\n });\n }\n } catch {\n // onError must not prevent handle() from honoring its \"never throws\" contract.\n }\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 static #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 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 return [undefined, false];\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, NonEmptyArray } from
|
|
2
|
-
import type { HandleOptions, JsonRpcMiddleware, MergedContextOf, MiddlewareConstraint } from
|
|
3
|
-
import { JsonRpcEngineV2 } from
|
|
1
|
+
import type { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, NonEmptyArray } from "@metamask/utils";
|
|
2
|
+
import type { HandleOptions, JsonRpcMiddleware, MergedContextOf, MiddlewareConstraint } from "./JsonRpcEngineV2.cjs";
|
|
3
|
+
import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.cjs";
|
|
4
4
|
type OnError = (error: unknown) => void;
|
|
5
5
|
type Options<Middleware extends MiddlewareConstraint> = {
|
|
6
6
|
onError?: OnError;
|
|
@@ -101,4 +101,4 @@ export declare class JsonRpcServer<Middleware extends MiddlewareConstraint = Jso
|
|
|
101
101
|
handle(rawRequest: unknown, options?: HandleOptions<MergedContextOf<Middleware>>): Promise<JsonRpcResponse | void>;
|
|
102
102
|
}
|
|
103
103
|
export {};
|
|
104
|
-
//# sourceMappingURL=JsonRpcServer.d.
|
|
104
|
+
//# sourceMappingURL=JsonRpcServer.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.d.cts","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EAEnB,cAAc,EACd,eAAe,EACf,aAAa,EACd,wBAAwB;AAIzB,OAAO,KAAK,EACV,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,oBAAoB,EAErB,8BAA6B;AAC9B,OAAO,EAAE,eAAe,EAAE,8BAA6B;AAGvD,KAAK,OAAO,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;AAExC,KAAK,OAAO,CAAC,UAAU,SAAS,oBAAoB,IAAI;IACtD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,GAAG,CACA;IACE,MAAM,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;CAC/D,GACD;IACE,UAAU,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;CACvC,CACJ,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,aAAa,CACxB,UAAU,SAAS,oBAAoB,GAAG,iBAAiB;;IAS3D;;;;;;;;;;;;;OAaG;gBACS,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC;IAYxC;;;;;;;;;;;;;OAaG;IACG,MAAM,CACV,OAAO,EAAE,cAAc,EACvB,OAAO,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,GACnD,OAAO,CAAC,eAAe,CAAC;IAE3B;;;;;;;;;;;;OAYG;IACG,MAAM,CACV,YAAY,EAAE,mBAAmB,EACjC,OAAO,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,GACnD,OAAO,CAAC,IAAI,CAAC;IAEhB;;;;;;;;;;;;;;;;;;OAkBG;IACG,MAAM,CACV,UAAU,EAAE,OAAO,EACnB,OAAO,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,GACnD,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC;CA4EnC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, NonEmptyArray } from "@metamask/utils";
|
|
2
|
+
import type { HandleOptions, JsonRpcMiddleware, MergedContextOf, MiddlewareConstraint } from "./JsonRpcEngineV2.mjs";
|
|
3
|
+
import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
|
|
4
|
+
type OnError = (error: unknown) => void;
|
|
5
|
+
type Options<Middleware extends MiddlewareConstraint> = {
|
|
6
|
+
onError?: OnError;
|
|
7
|
+
} & ({
|
|
8
|
+
engine: ReturnType<typeof JsonRpcEngineV2.create<Middleware>>;
|
|
9
|
+
} | {
|
|
10
|
+
middleware: NonEmptyArray<Middleware>;
|
|
11
|
+
});
|
|
12
|
+
/**
|
|
13
|
+
* A JSON-RPC server that handles requests and notifications.
|
|
14
|
+
*
|
|
15
|
+
* Essentially wraps a {@link JsonRpcEngineV2} in order to create a conformant
|
|
16
|
+
* yet permissive JSON-RPC 2.0 server.
|
|
17
|
+
*
|
|
18
|
+
* Note that the server will accept both requests and notifications via {@link handle},
|
|
19
|
+
* even if the underlying engine is only able to handle one or the other.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const server = new JsonRpcServer({
|
|
24
|
+
* engine,
|
|
25
|
+
* onError,
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* const response = await server.handle(request);
|
|
29
|
+
* if ('result' in response) {
|
|
30
|
+
* // Handle result
|
|
31
|
+
* } else {
|
|
32
|
+
* // Handle error
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class JsonRpcServer<Middleware extends MiddlewareConstraint = JsonRpcMiddleware> {
|
|
37
|
+
#private;
|
|
38
|
+
/**
|
|
39
|
+
* Construct a new JSON-RPC server.
|
|
40
|
+
*
|
|
41
|
+
* @param options - The options for the server.
|
|
42
|
+
* @param options.onError - The callback to handle errors thrown by the
|
|
43
|
+
* engine. Errors always result in a failed response object, containing a
|
|
44
|
+
* JSON-RPC 2.0 serialized version of the original error. If you need to
|
|
45
|
+
* access the original error, use the `onError` callback. If the `onError`
|
|
46
|
+
* callback itself throws or rejects, the error is silently ignored.
|
|
47
|
+
* @param options.engine - The engine to use. Mutually exclusive with
|
|
48
|
+
* `middleware`.
|
|
49
|
+
* @param options.middleware - The middleware to use. Mutually exclusive with
|
|
50
|
+
* `engine`.
|
|
51
|
+
*/
|
|
52
|
+
constructor(options: Options<Middleware>);
|
|
53
|
+
/**
|
|
54
|
+
* Handle a JSON-RPC request.
|
|
55
|
+
*
|
|
56
|
+
* This method never throws. For requests, a response is always returned.
|
|
57
|
+
* All errors are passed to the engine's `onError` callback.
|
|
58
|
+
*
|
|
59
|
+
* **WARNING**: This method is unaware of the request type of the underlying
|
|
60
|
+
* engine. The request will fail if the engine can only handle notifications.
|
|
61
|
+
*
|
|
62
|
+
* @param request - The request to handle.
|
|
63
|
+
* @param options - The options for the handle operation.
|
|
64
|
+
* @param options.context - The context to pass to the middleware.
|
|
65
|
+
* @returns The JSON-RPC response.
|
|
66
|
+
*/
|
|
67
|
+
handle(request: JsonRpcRequest, options?: HandleOptions<MergedContextOf<Middleware>>): Promise<JsonRpcResponse>;
|
|
68
|
+
/**
|
|
69
|
+
* Handle a JSON-RPC notification.
|
|
70
|
+
*
|
|
71
|
+
* This method never throws. For notifications, `undefined` is always returned.
|
|
72
|
+
* All errors are passed to the engine's `onError` callback.
|
|
73
|
+
*
|
|
74
|
+
* **WARNING**: This method is unaware of the request type of the underlying
|
|
75
|
+
* engine. The request will fail if the engine cannot handle notifications.
|
|
76
|
+
*
|
|
77
|
+
* @param notification - The notification to handle.
|
|
78
|
+
* @param options - The options for the handle operation.
|
|
79
|
+
* @param options.context - The context to pass to the middleware.
|
|
80
|
+
*/
|
|
81
|
+
handle(notification: JsonRpcNotification, options?: HandleOptions<MergedContextOf<Middleware>>): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Handle an alleged JSON-RPC request or notification. Permits any plain
|
|
84
|
+
* object with `{ method: string }`, so long as any present JSON-RPC 2.0
|
|
85
|
+
* properties are valid. If the object has an `id` property, it will be
|
|
86
|
+
* treated as a request, otherwise it will be treated as a notification.
|
|
87
|
+
*
|
|
88
|
+
* This method never throws. All errors are passed to the engine's
|
|
89
|
+
* `onError` callback. A JSON-RPC response is always returned for requests,
|
|
90
|
+
* and `undefined` is returned for notifications.
|
|
91
|
+
*
|
|
92
|
+
* **WARNING**: The request will fail if its coerced type (i.e. request or
|
|
93
|
+
* response) is not of the type expected by the underlying engine.
|
|
94
|
+
*
|
|
95
|
+
* @param rawRequest - The raw request to handle.
|
|
96
|
+
* @param options - The options for the handle operation.
|
|
97
|
+
* @param options.context - The context to pass to the middleware.
|
|
98
|
+
* @returns The JSON-RPC response, or `undefined` if the request is a
|
|
99
|
+
* notification.
|
|
100
|
+
*/
|
|
101
|
+
handle(rawRequest: unknown, options?: HandleOptions<MergedContextOf<Middleware>>): Promise<JsonRpcResponse | void>;
|
|
102
|
+
}
|
|
103
|
+
export {};
|
|
104
|
+
//# sourceMappingURL=JsonRpcServer.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.d.mts","sourceRoot":"","sources":["../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A4EnC"}
|
|
@@ -10,10 +10,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _a, _JsonRpcServer_engine, _JsonRpcServer_onError, _JsonRpcServer_coerceRequest;
|
|
13
|
-
import { rpcErrors, serializeError } from
|
|
14
|
-
import { hasProperty, isObject } from
|
|
15
|
-
import { getUniqueId } from
|
|
16
|
-
import { JsonRpcEngineV2 } from
|
|
13
|
+
import { rpcErrors, serializeError } from "@metamask/rpc-errors";
|
|
14
|
+
import { hasProperty, isObject } from "@metamask/utils";
|
|
15
|
+
import { getUniqueId } from "../getUniqueId.mjs";
|
|
16
|
+
import { JsonRpcEngineV2 } from "./JsonRpcEngineV2.mjs";
|
|
17
17
|
const jsonrpc = '2.0';
|
|
18
18
|
/**
|
|
19
19
|
* A JSON-RPC server that handles requests and notifications.
|
|
@@ -171,4 +171,4 @@ function getOriginalId(rawRequest) {
|
|
|
171
171
|
}
|
|
172
172
|
return [undefined, false];
|
|
173
173
|
}
|
|
174
|
-
//# sourceMappingURL=JsonRpcServer.
|
|
174
|
+
//# sourceMappingURL=JsonRpcServer.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonRpcServer.mjs","sourceRoot":"","sources":["../../src/v2/JsonRpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,6BAA6B;AAQjE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,wBAAwB;AAExD,OAAO,EAAE,WAAW,EAAE,2BAA0B;AAQhD,OAAO,EAAE,eAAe,EAAE,8BAA6B;AAgBvD,MAAM,OAAO,GAAG,KAAc,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAa;IAUxB;;;;;;;;;;;;;OAaG;IACH,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AAE,CAAC;QACtC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACzE,CAAC;IACD,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,CAAC;QAC1D,OAAO,CAAC,UAAU,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,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 { getUniqueId } from '../getUniqueId.js';\nimport type {\n HandleOptions,\n JsonRpcMiddleware,\n MergedContextOf,\n MiddlewareConstraint,\n RequestOf,\n} from './JsonRpcEngineV2.js';\nimport { JsonRpcEngineV2 } from './JsonRpcEngineV2.js';\nimport type { JsonRpcCall } from './utils.js';\n\ntype OnError = (error: unknown) => void;\n\ntype Options<Middleware extends MiddlewareConstraint> = {\n onError?: OnError;\n} & (\n | {\n engine: ReturnType<typeof JsonRpcEngineV2.create<Middleware>>;\n }\n | {\n middleware: NonEmptyArray<Middleware>;\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 * Note that the server will accept both requests and notifications via {@link handle},\n * even if the underlying engine is only able to handle one or the other.\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 Middleware extends MiddlewareConstraint = JsonRpcMiddleware,\n> {\n readonly #engine: JsonRpcEngineV2<\n RequestOf<Middleware>,\n MergedContextOf<Middleware>\n >;\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. If the `onError`\n * callback itself throws or rejects, the error is silently ignored.\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<Middleware>) {\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 // @ts-expect-error - TypeScript complains that engine is of the wrong type, but clearly it's not.\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 * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine can only handle notifications.\n *\n * @param request - The request to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n * @returns The JSON-RPC response.\n */\n async handle(\n request: JsonRpcRequest,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 * **WARNING**: This method is unaware of the request type of the underlying\n * engine. The request will fail if the engine cannot handle notifications.\n *\n * @param notification - The notification to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n */\n async handle(\n notification: JsonRpcNotification,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 an `id` property, it will be\n * treated as a request, otherwise it will be treated as a notification.\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 * **WARNING**: The request will fail if its coerced type (i.e. request or\n * response) is not of the type expected by the underlying engine.\n *\n * @param rawRequest - The raw request to handle.\n * @param options - The options for the handle operation.\n * @param options.context - The context to pass to the middleware.\n * @returns The JSON-RPC response, or `undefined` if the request is a\n * notification.\n */\n async handle(\n rawRequest: unknown,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): Promise<JsonRpcResponse | void>;\n\n async handle(\n rawRequest: unknown,\n options?: HandleOptions<MergedContextOf<Middleware>>,\n ): 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 = JsonRpcServer.#coerceRequest(rawRequest, isRequest);\n // @ts-expect-error - The request may not be of the type expected by the engine,\n // and we intentionally allow this to happen.\n const result = await this.#engine.handle(request, options);\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 try {\n const maybePromise: unknown = this.#onError?.(error);\n if (maybePromise instanceof Promise) {\n maybePromise.catch(() => {\n // Prevent unhandled promise rejection.\n });\n }\n } catch {\n // onError must not prevent handle() from honoring its \"never throws\" contract.\n }\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 static #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 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 return [undefined, false];\n}\n"]}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.MiddlewareContext = void 0;
|
|
5
|
+
const utils_js_1 = require("./utils.cjs");
|
|
6
|
+
const MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');
|
|
7
|
+
/**
|
|
8
|
+
* A context object for middleware that attempts to protect against accidental
|
|
9
|
+
* modifications. Its interface is frozen.
|
|
10
|
+
*
|
|
11
|
+
* Map keys may not be directly overridden with {@link set}. Instead, use
|
|
12
|
+
* {@link delete} to remove a key and then {@link set} to add a new value.
|
|
13
|
+
*
|
|
14
|
+
* The override protections are circumvented when using e.g. `Reflect.set`, so
|
|
15
|
+
* don't do that.
|
|
16
|
+
*
|
|
17
|
+
* @template KeyValues - The type of the keys and values in the context.
|
|
18
|
+
* @example
|
|
19
|
+
* // By default, the context permits any PropertyKey as a key.
|
|
20
|
+
* const context = new MiddlewareContext();
|
|
21
|
+
* context.set('foo', 'bar');
|
|
22
|
+
* context.get('foo'); // 'bar'
|
|
23
|
+
* context.get('fizz'); // undefined
|
|
24
|
+
* @example
|
|
25
|
+
* // By specifying an object type, the context permits only the keys of the object.
|
|
26
|
+
* type Context = MiddlewareContext<{ foo: string }>;
|
|
27
|
+
* const context = new Context([['foo', 'bar']]);
|
|
28
|
+
* context.get('foo'); // 'bar'
|
|
29
|
+
* context.get('fizz'); // Type error
|
|
30
|
+
*/
|
|
31
|
+
class MiddlewareContext extends Map {
|
|
32
|
+
/**
|
|
33
|
+
* Check if a value is a {@link MiddlewareContext} instance.
|
|
34
|
+
* Works across different package versions in the same realm.
|
|
35
|
+
*
|
|
36
|
+
* @param value - The value to check.
|
|
37
|
+
* @returns Whether the value is a {@link MiddlewareContext} instance.
|
|
38
|
+
*/
|
|
39
|
+
static isInstance(value) {
|
|
40
|
+
return (0, utils_js_1.isInstance)(value, MiddlewareContextSymbol);
|
|
41
|
+
}
|
|
42
|
+
constructor(entries) {
|
|
43
|
+
super(entries && isIterable(entries)
|
|
44
|
+
? entries
|
|
45
|
+
: entriesFromKeyValues(entries ?? {}));
|
|
46
|
+
// This is a computed property name, and it doesn't seem possible to make it
|
|
47
|
+
// hash private using `#`.
|
|
48
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
49
|
+
this[_a] = true;
|
|
50
|
+
Object.freeze(this);
|
|
51
|
+
}
|
|
52
|
+
get(key) {
|
|
53
|
+
return super.get(key);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get a value from the context. Throws if the key is not found.
|
|
57
|
+
*
|
|
58
|
+
* @param key - The key to get the value for.
|
|
59
|
+
* @returns The value.
|
|
60
|
+
*/
|
|
61
|
+
assertGet(key) {
|
|
62
|
+
if (!super.has(key)) {
|
|
63
|
+
throw new Error(`Context key "${String(key)}" not found`);
|
|
64
|
+
}
|
|
65
|
+
return super.get(key);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Set a value in the context. Throws if the key already exists.
|
|
69
|
+
* {@link delete} an existing key before setting it to a new value.
|
|
70
|
+
*
|
|
71
|
+
* @throws If the key already exists.
|
|
72
|
+
* @param key - The key to set the value for.
|
|
73
|
+
* @param value - The value to set.
|
|
74
|
+
* @returns The context.
|
|
75
|
+
*/
|
|
76
|
+
set(key, value) {
|
|
77
|
+
if (super.has(key)) {
|
|
78
|
+
throw new Error(`MiddlewareContext key "${String(key)}" already exists`);
|
|
79
|
+
}
|
|
80
|
+
super.set(key, value);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.MiddlewareContext = MiddlewareContext;
|
|
85
|
+
_a = MiddlewareContextSymbol;
|
|
86
|
+
/**
|
|
87
|
+
* {@link Iterable} type guard.
|
|
88
|
+
*
|
|
89
|
+
* @param value - The value to check.
|
|
90
|
+
* @returns Whether the value is an {@link Iterable}.
|
|
91
|
+
*/
|
|
92
|
+
function isIterable(value) {
|
|
93
|
+
return Symbol.iterator in value;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Like Object.entries(), but includes symbol-keyed properties.
|
|
97
|
+
*
|
|
98
|
+
* @template KeyValues - The type of the keys and values in the object.
|
|
99
|
+
* @param keyValues - The object to convert.
|
|
100
|
+
* @returns The array of entries, including symbol-keyed properties.
|
|
101
|
+
*/
|
|
102
|
+
function entriesFromKeyValues(keyValues) {
|
|
103
|
+
return Reflect.ownKeys(keyValues).map((key) => [
|
|
104
|
+
key,
|
|
105
|
+
keyValues[key],
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=MiddlewareContext.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareContext.cjs","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":";;;;AAAA,0CAAwC;AAGxC,MAAM,uBAAuB,GAAG,MAAM,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAa,iBAEX,SAAQ,GAAgD;IAMxD;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,KAAc;QAC9B,OAAO,IAAA,qBAAU,EAAC,KAAK,EAAE,uBAAuB,CAAC,CAAC;IACpD,CAAC;IAED,YACE,OAEa;QAEb,KAAK,CACH,OAAO,IAAI,UAAU,CAAC,OAAO,CAAC;YAC5B,CAAC,CAAC,OAAO;YACT,CAAC,CAAC,oBAAoB,CAAC,OAAO,IAAI,EAAE,CAAC,CACxC,CAAC;QAzBJ,4EAA4E;QAC5E,0BAA0B;QAC1B,gDAAgD;QAC/B,QAAyB,GAAG,IAAI,CAAC;QAuBhD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC;IAED,GAAG,CAA8B,GAAQ;QACvC,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAA+B,CAAC;IACtD,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAA8B,GAAQ;QAC7C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,gBAAgB,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC5D,CAAC;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,CAAmB,CAAC;IAC1C,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAA8B,GAAQ,EAAE,KAAqB;QAC9D,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;QAC3E,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAjED,8CAiEC;KA3DmB,uBAAuB;AA6D3C;;;;;GAKG;AACH,SAAS,UAAU,CACjB,KAAuD;IAEvD,OAAO,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC;AAClC,CAAC;AAED;;;;;;GAMG;AACH,SAAS,oBAAoB,CAC3B,SAAoB;IAEpB,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,GAAoB,EAAE,EAAE,CAAC;QAC9D,GAAG;QACH,SAAS,CAAC,GAAG,CAAC;KACf,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { isInstance } from './utils.js';\nimport type { UnionToIntersection } from './utils.js';\n\nconst MiddlewareContextSymbol = Symbol.for('json-rpc-engine#MiddlewareContext');\n\n/**\n * A 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 // This is a computed property name, and it doesn't seem possible to make it\n // hash private using `#`.\n // eslint-disable-next-line no-restricted-syntax\n private readonly [MiddlewareContextSymbol] = true;\n\n /**\n * Check if a value is a {@link MiddlewareContext} instance.\n * Works across different package versions in the same realm.\n *\n * @param value - The value to check.\n * @returns Whether the value is a {@link MiddlewareContext} instance.\n */\n static isInstance(value: unknown): value is MiddlewareContext {\n return isInstance(value, MiddlewareContextSymbol);\n }\n\n constructor(\n entries?:\n | Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]>\n | KeyValues,\n ) {\n super(\n entries && isIterable(entries)\n ? entries\n : entriesFromKeyValues(entries ?? {}),\n );\n Object.freeze(this);\n }\n\n get<Key extends keyof KeyValues>(key: Key): KeyValues[Key] | undefined {\n return super.get(key) as KeyValues[Key] | 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<Key extends keyof KeyValues>(key: Key): KeyValues[Key] {\n if (!super.has(key)) {\n throw new Error(`Context key \"${String(key)}\" not found`);\n }\n return super.get(key) as KeyValues[Key];\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<Key extends keyof KeyValues>(key: Key, value: KeyValues[Key]): 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 * {@link Iterable} type guard.\n *\n * @param value - The value to check.\n * @returns Whether the value is an {@link Iterable}.\n */\nfunction isIterable(\n value: Iterable<unknown> | Record<PropertyKey, unknown>,\n): value is Iterable<unknown> {\n return Symbol.iterator in value;\n}\n\n/**\n * Like Object.entries(), but includes symbol-keyed properties.\n *\n * @template KeyValues - The type of the keys and values in the object.\n * @param keyValues - The object to convert.\n * @returns The array of entries, including symbol-keyed properties.\n */\nfunction entriesFromKeyValues<KeyValues extends Record<PropertyKey, unknown>>(\n keyValues: KeyValues,\n): [keyof KeyValues, KeyValues[keyof KeyValues]][] {\n return Reflect.ownKeys(keyValues).map((key: keyof KeyValues) => [\n key,\n keyValues[key],\n ]);\n}\n\n/**\n * Infer the KeyValues type from a {@link MiddlewareContext}.\n */\nexport type InferKeyValues<Type> =\n Type extends MiddlewareContext<infer KeyValues> ? KeyValues : 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<Type> = Type extends infer Object\n ? { [Key in keyof Object]: Object[Key] }\n : 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<Type extends Record<PropertyKey, unknown>> = {\n [Key in keyof Type]-?: [Type[Key]] extends [never] ? Key : never;\n}[keyof Type] extends never\n ? Type\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/**\n * A constraint for {@link MiddlewareContext} generic parameters.\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"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { UnionToIntersection } from
|
|
1
|
+
import type { UnionToIntersection } from "./utils.cjs";
|
|
2
2
|
declare const MiddlewareContextSymbol: unique symbol;
|
|
3
3
|
/**
|
|
4
4
|
* A context object for middleware that attempts to protect against accidental
|
|
@@ -105,4 +105,4 @@ export type ContextConstraint = MiddlewareContext<any>;
|
|
|
105
105
|
*/
|
|
106
106
|
export type EmptyContext = MiddlewareContext<{}>;
|
|
107
107
|
export {};
|
|
108
|
-
//# sourceMappingURL=MiddlewareContext.d.
|
|
108
|
+
//# sourceMappingURL=MiddlewareContext.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MiddlewareContext.d.cts","sourceRoot":"","sources":["../../src/v2/MiddlewareContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAmB;AAEtD,QAAA,MAAM,uBAAuB,eAAkD,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,iBAAiB,CAC5B,SAAS,SAAS,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAC7E,SA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G,GAAG,GAAG,KAAK;CACjE,CAAC,MAAM,IAAI,CAAC,SAAS,KAAK,GACvB,IAAI,GACJ,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;AAER;;GAEG;AAGH,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;AAEvD;;GAEG;AAGH,MAAM,MAAM,YAAY,GAAG,iBAAiB,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { UnionToIntersection } from "./utils.mjs";
|
|
2
|
+
declare const MiddlewareContextSymbol: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* A context object for middleware that attempts to protect against accidental
|
|
5
|
+
* modifications. Its interface is frozen.
|
|
6
|
+
*
|
|
7
|
+
* Map keys may not be directly overridden with {@link set}. Instead, use
|
|
8
|
+
* {@link delete} to remove a key and then {@link set} to add a new value.
|
|
9
|
+
*
|
|
10
|
+
* The override protections are circumvented when using e.g. `Reflect.set`, so
|
|
11
|
+
* don't do that.
|
|
12
|
+
*
|
|
13
|
+
* @template KeyValues - The type of the keys and values in the context.
|
|
14
|
+
* @example
|
|
15
|
+
* // By default, the context permits any PropertyKey as a key.
|
|
16
|
+
* const context = new MiddlewareContext();
|
|
17
|
+
* context.set('foo', 'bar');
|
|
18
|
+
* context.get('foo'); // 'bar'
|
|
19
|
+
* context.get('fizz'); // undefined
|
|
20
|
+
* @example
|
|
21
|
+
* // By specifying an object type, the context permits only the keys of the object.
|
|
22
|
+
* type Context = MiddlewareContext<{ foo: string }>;
|
|
23
|
+
* const context = new Context([['foo', 'bar']]);
|
|
24
|
+
* context.get('foo'); // 'bar'
|
|
25
|
+
* context.get('fizz'); // Type error
|
|
26
|
+
*/
|
|
27
|
+
export declare class MiddlewareContext<KeyValues extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Map<keyof KeyValues, KeyValues[keyof KeyValues]> {
|
|
28
|
+
private readonly [MiddlewareContextSymbol];
|
|
29
|
+
/**
|
|
30
|
+
* Check if a value is a {@link MiddlewareContext} instance.
|
|
31
|
+
* Works across different package versions in the same realm.
|
|
32
|
+
*
|
|
33
|
+
* @param value - The value to check.
|
|
34
|
+
* @returns Whether the value is a {@link MiddlewareContext} instance.
|
|
35
|
+
*/
|
|
36
|
+
static isInstance(value: unknown): value is MiddlewareContext;
|
|
37
|
+
constructor(entries?: Iterable<readonly [keyof KeyValues, KeyValues[keyof KeyValues]]> | KeyValues);
|
|
38
|
+
get<Key extends keyof KeyValues>(key: Key): KeyValues[Key] | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Get a value from the context. Throws if the key is not found.
|
|
41
|
+
*
|
|
42
|
+
* @param key - The key to get the value for.
|
|
43
|
+
* @returns The value.
|
|
44
|
+
*/
|
|
45
|
+
assertGet<Key extends keyof KeyValues>(key: Key): KeyValues[Key];
|
|
46
|
+
/**
|
|
47
|
+
* Set a value in the context. Throws if the key already exists.
|
|
48
|
+
* {@link delete} an existing key before setting it to a new value.
|
|
49
|
+
*
|
|
50
|
+
* @throws If the key already exists.
|
|
51
|
+
* @param key - The key to set the value for.
|
|
52
|
+
* @param value - The value to set.
|
|
53
|
+
* @returns The context.
|
|
54
|
+
*/
|
|
55
|
+
set<Key extends keyof KeyValues>(key: Key, value: KeyValues[Key]): this;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Infer the KeyValues type from a {@link MiddlewareContext}.
|
|
59
|
+
*/
|
|
60
|
+
export type InferKeyValues<Type> = Type extends MiddlewareContext<infer KeyValues> ? KeyValues : never;
|
|
61
|
+
/**
|
|
62
|
+
* Simplifies an object type by "merging" its properties.
|
|
63
|
+
*
|
|
64
|
+
* - Expands intersections into a single object type.
|
|
65
|
+
* - Forces mapped/conditional results to resolve into a readable shape.
|
|
66
|
+
* - No runtime effect; purely a type-level normalization.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* type A = { a: string } & { b: number };
|
|
70
|
+
* type B = Simplify<A>; // { a: string; b: number }
|
|
71
|
+
*/
|
|
72
|
+
type Simplify<Type> = Type extends infer Object ? {
|
|
73
|
+
[Key in keyof Object]: Object[Key];
|
|
74
|
+
} : never;
|
|
75
|
+
/**
|
|
76
|
+
* Rejects record types that contain any `never`-valued property.
|
|
77
|
+
*
|
|
78
|
+
* If any property of `T` resolves to `never`, the result is `never`; otherwise it returns `T` unchanged.
|
|
79
|
+
* Useful as a guard to ensure computed/merged record types didn't collapse any fields to `never`.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* type A = ExcludeNever<{ a: string; b: never }>; // never
|
|
83
|
+
* type B = ExcludeNever<{ a: string; b: number }>; // { a: string; b: number }
|
|
84
|
+
*/
|
|
85
|
+
type ExcludeNever<Type extends Record<PropertyKey, unknown>> = {
|
|
86
|
+
[Key in keyof Type]-?: [Type[Key]] extends [never] ? Key : never;
|
|
87
|
+
}[keyof Type] extends never ? Type : never;
|
|
88
|
+
/**
|
|
89
|
+
* Merge a union of {@link MiddlewareContext}s into a single {@link MiddlewareContext}
|
|
90
|
+
* supertype.
|
|
91
|
+
*
|
|
92
|
+
* @param Contexts - The union of {@link MiddlewareContext}s to merge.
|
|
93
|
+
* @returns The merged {@link MiddlewareContext} supertype.
|
|
94
|
+
* @example
|
|
95
|
+
* type A = MiddlewareContext<{ a: string }> | MiddlewareContext<{ b: number }>;
|
|
96
|
+
* type B = MergeContexts<A>; // MiddlewareContext<{ a: string, b: number }>
|
|
97
|
+
*/
|
|
98
|
+
export type MergeContexts<Contexts extends ContextConstraint> = ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>> extends never ? never : MiddlewareContext<ExcludeNever<Simplify<UnionToIntersection<InferKeyValues<Contexts>>>>>;
|
|
99
|
+
/**
|
|
100
|
+
* A constraint for {@link MiddlewareContext} generic parameters.
|
|
101
|
+
*/
|
|
102
|
+
export type ContextConstraint = MiddlewareContext<any>;
|
|
103
|
+
/**
|
|
104
|
+
* The empty context type, i.e. `MiddlewareContext<{}>`.
|
|
105
|
+
*/
|
|
106
|
+
export type EmptyContext = MiddlewareContext<{}>;
|
|
107
|
+
export {};
|
|
108
|
+
//# sourceMappingURL=MiddlewareContext.d.mts.map
|