@metamask/json-rpc-engine
Advanced tools
Comparing version 7.2.0 to 7.3.0
@@ -9,2 +9,11 @@ # Changelog | ||
## [7.3.0] | ||
### Added | ||
- Migrate `@metamask/json-rpc-engine` into the core monorepo ([#1895](https://github.com/MetaMask/core/pull/1895)) | ||
### Changed | ||
- Bump `@metamask/utils` from `^8.1.0` to `^8.2.0` ([#1895](https://github.com/MetaMask/core/pull/1895)) | ||
- Bump `@metamask/rpc-errors` from `^6.0.0` to `^6.1.0` ([#1882](https://github.com/MetaMask/core/pull/1882)) | ||
- Bump `@metamask/auto-changelog` from `3.4.2` to `3.4.3` ([#1997](https://github.com/MetaMask/core/pull/1997)) | ||
## [7.2.0] | ||
@@ -87,11 +96,12 @@ ### Added | ||
[Unreleased]: https://github.com/MetaMask/json-rpc-engine/compare/v7.2.0...HEAD | ||
[7.2.0]: https://github.com/MetaMask/json-rpc-engine/compare/v7.1.1...v7.2.0 | ||
[7.1.1]: https://github.com/MetaMask/json-rpc-engine/compare/v7.1.0...v7.1.1 | ||
[7.1.0]: https://github.com/MetaMask/json-rpc-engine/compare/v7.0.0...v7.1.0 | ||
[7.0.0]: https://github.com/MetaMask/json-rpc-engine/compare/v6.1.0...v7.0.0 | ||
[6.1.0]: https://github.com/MetaMask/json-rpc-engine/compare/v6.0.0...v6.1.0 | ||
[6.0.0]: https://github.com/MetaMask/json-rpc-engine/compare/v5.4.0...v6.0.0 | ||
[5.4.0]: https://github.com/MetaMask/json-rpc-engine/compare/v5.3.0...v5.4.0 | ||
[5.3.0]: https://github.com/MetaMask/json-rpc-engine/compare/v5.2.0...v5.3.0 | ||
[5.2.0]: https://github.com/MetaMask/json-rpc-engine/releases/tag/v5.2.0 | ||
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/json-rpc-engine@7.3.0...HEAD | ||
[7.3.0]: https://github.com/MetaMask/core/compare/@metamask/json-rpc-engine@7.2.0...@metamask/json-rpc-engine@7.3.0 | ||
[7.2.0]: https://github.com/MetaMask/core/compare/@metamask/json-rpc-engine@7.1.1...@metamask/json-rpc-engine@7.2.0 | ||
[7.1.1]: https://github.com/MetaMask/core/compare/@metamask/json-rpc-engine@7.1.0...@metamask/json-rpc-engine@7.1.1 | ||
[7.1.0]: https://github.com/MetaMask/core/compare/@metamask/json-rpc-engine@7.0.0...@metamask/json-rpc-engine@7.1.0 | ||
[7.0.0]: https://github.com/MetaMask/core/compare/json-rpc-engine@6.1.0...@metamask/json-rpc-engine@7.0.0 | ||
[6.1.0]: https://github.com/MetaMask/core/compare/json-rpc-engine@6.0.0...json-rpc-engine@6.1.0 | ||
[6.0.0]: https://github.com/MetaMask/core/compare/json-rpc-engine@5.4.0...json-rpc-engine@6.0.0 | ||
[5.4.0]: https://github.com/MetaMask/core/compare/json-rpc-engine@5.3.0...json-rpc-engine@5.4.0 | ||
[5.3.0]: https://github.com/MetaMask/core/compare/json-rpc-engine@5.2.0...json-rpc-engine@5.3.0 | ||
[5.2.0]: https://github.com/MetaMask/core/releases/tag/json-rpc-engine@5.2.0 |
@@ -27,1 +27,2 @@ import type { Json, JsonRpcParams, JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils'; | ||
export declare function createAsyncMiddleware<Params extends JsonRpcParams, Result extends Json>(asyncMiddleware: AsyncJsonrpcMiddleware<Params, Result>): JsonRpcMiddleware<Params, Result>; | ||
//# sourceMappingURL=createAsyncMiddleware.d.ts.map |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -27,3 +36,3 @@ exports.createAsyncMiddleware = void 0; | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
return async (request, response, next, end) => { | ||
return (request, response, next, end) => __awaiter(this, void 0, void 0, function* () { | ||
// nextPromise is the key to the implementation | ||
@@ -39,3 +48,3 @@ // it is resolved by the return handler passed to the | ||
// This will be called by the consumer's async middleware. | ||
const asyncNext = async () => { | ||
const asyncNext = () => __awaiter(this, void 0, void 0, function* () { | ||
nextWasCalled = true; | ||
@@ -50,7 +59,7 @@ // We pass a return handler to next(). When it is called by the engine, | ||
return nextPromise; | ||
}; | ||
}); | ||
try { | ||
await asyncMiddleware(request, response, asyncNext); | ||
yield asyncMiddleware(request, response, asyncNext); | ||
if (nextWasCalled) { | ||
await nextPromise; // we must wait until the return handler is called | ||
yield nextPromise; // we must wait until the return handler is called | ||
returnHandlerCallback(null); | ||
@@ -70,5 +79,5 @@ } | ||
} | ||
}; | ||
}); | ||
} | ||
exports.createAsyncMiddleware = createAsyncMiddleware; | ||
//# sourceMappingURL=createAsyncMiddleware.js.map |
@@ -17,1 +17,2 @@ import type { Json, JsonRpcParams } from '@metamask/utils'; | ||
export {}; | ||
//# sourceMappingURL=createScaffoldMiddleware.d.ts.map |
@@ -9,1 +9,2 @@ /** | ||
export declare function getUniqueId(): number; | ||
//# sourceMappingURL=getUniqueId.d.ts.map |
@@ -13,1 +13,2 @@ import type { Json, JsonRpcParams } from '@metamask/utils'; | ||
export declare function createIdRemapMiddleware(): JsonRpcMiddleware<JsonRpcParams, Json>; | ||
//# sourceMappingURL=idRemapMiddleware.d.ts.map |
@@ -7,1 +7,2 @@ export * from './createAsyncMiddleware'; | ||
export * from './mergeMiddleware'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -114,1 +114,2 @@ import SafeEventEmitter from '@metamask/safe-event-emitter'; | ||
export {}; | ||
//# sourceMappingURL=JsonRpcEngine.d.ts.map |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
@@ -104,13 +113,13 @@ if (kind === "m") throw new TypeError("Private method is not writable"); | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
return async (req, res, next, end) => { | ||
return (req, res, next, end) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const [middlewareError, isComplete, returnHandlers] = await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runAllMiddleware).call(JsonRpcEngine, req, res, __classPrivateFieldGet(this, _JsonRpcEngine_middleware, "f")); | ||
const [middlewareError, isComplete, returnHandlers] = yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runAllMiddleware).call(JsonRpcEngine, req, res, __classPrivateFieldGet(this, _JsonRpcEngine_middleware, "f")); | ||
if (isComplete) { | ||
await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runReturnHandlers).call(JsonRpcEngine, returnHandlers); | ||
yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runReturnHandlers).call(JsonRpcEngine, returnHandlers); | ||
return end(middlewareError); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
return next(async (handlerCallback) => { | ||
return next((handlerCallback) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runReturnHandlers).call(JsonRpcEngine, returnHandlers); | ||
yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runReturnHandlers).call(JsonRpcEngine, returnHandlers); | ||
} | ||
@@ -121,3 +130,3 @@ catch (error) { | ||
return handlerCallback(); | ||
}); | ||
})); | ||
} | ||
@@ -127,3 +136,3 @@ catch (error) { | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -139,16 +148,18 @@ /** | ||
// eslint-disable-next-line no-restricted-syntax | ||
async _promiseHandle(request) { | ||
return new Promise((resolve, reject) => { | ||
__classPrivateFieldGet(this, _JsonRpcEngine_instances, "m", _JsonRpcEngine_handle).call(this, request, (error, res) => { | ||
// For notifications, the response will be `undefined`, and any caught | ||
// errors are unexpected and should be surfaced to the caller. | ||
if (error && res === undefined) { | ||
reject(error); | ||
} | ||
else { | ||
// Excepting notifications, there will always be a response, and it will | ||
// always have any error that is caught and propagated. | ||
resolve(res); | ||
} | ||
}).catch(reject); | ||
_promiseHandle(request) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve, reject) => { | ||
__classPrivateFieldGet(this, _JsonRpcEngine_instances, "m", _JsonRpcEngine_handle).call(this, request, (error, res) => { | ||
// For notifications, the response will be `undefined`, and any caught | ||
// errors are unexpected and should be surfaced to the caller. | ||
if (error && res === undefined) { | ||
reject(error); | ||
} | ||
else { | ||
// Excepting notifications, there will always be a response, and it will | ||
// always have any error that is caught and propagated. | ||
resolve(res); | ||
} | ||
}).catch(reject); | ||
}); | ||
}); | ||
@@ -162,180 +173,173 @@ } | ||
} | ||
}, _JsonRpcEngine_handleBatch = | ||
/** | ||
* Handles a batch of JSON-RPC requests, either in `async` or callback | ||
* fashion. | ||
* | ||
* @param requests - The request objects to process. | ||
* @param callback - The completion callback. | ||
* @returns The array of responses, or nothing if a callback was specified. | ||
*/ | ||
async function _JsonRpcEngine_handleBatch(requests, callback) { | ||
// The order here is important | ||
try { | ||
// If the batch is an empty array, the response array must contain a single object | ||
if (requests.length === 0) { | ||
const response = [ | ||
{ | ||
id: null, | ||
jsonrpc: '2.0', | ||
error: new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.invalidRequest, 'Request batch must contain plain objects. Received an empty array'), | ||
}, | ||
]; | ||
}, _JsonRpcEngine_handleBatch = function _JsonRpcEngine_handleBatch(requests, callback) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// The order here is important | ||
try { | ||
// If the batch is an empty array, the response array must contain a single object | ||
if (requests.length === 0) { | ||
const response = [ | ||
{ | ||
id: null, | ||
jsonrpc: '2.0', | ||
error: new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.invalidRequest, 'Request batch must contain plain objects. Received an empty array'), | ||
}, | ||
]; | ||
if (callback) { | ||
return callback(null, response); | ||
} | ||
return response; | ||
} | ||
// 2. Wait for all requests to finish, or throw on some kind of fatal | ||
// error | ||
const responses = (yield Promise.all( | ||
// 1. Begin executing each request in the order received | ||
requests.map(this._promiseHandle.bind(this)))).filter( | ||
// Filter out any notification responses. | ||
(response) => response !== undefined); | ||
// 3. Return batch response | ||
if (callback) { | ||
return callback(null, response); | ||
return callback(null, responses); | ||
} | ||
return response; | ||
return responses; | ||
} | ||
// 2. Wait for all requests to finish, or throw on some kind of fatal | ||
// error | ||
const responses = (await Promise.all( | ||
// 1. Begin executing each request in the order received | ||
requests.map(this._promiseHandle.bind(this)))).filter( | ||
// Filter out any notification responses. | ||
(response) => response !== undefined); | ||
// 3. Return batch response | ||
if (callback) { | ||
return callback(null, responses); | ||
catch (error) { | ||
if (callback) { | ||
return callback(error); | ||
} | ||
throw error; | ||
} | ||
return responses; | ||
} | ||
catch (error) { | ||
if (callback) { | ||
return callback(error); | ||
}); | ||
}, _JsonRpcEngine_handle = function _JsonRpcEngine_handle(callerReq, callback) { | ||
var _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!callerReq || | ||
Array.isArray(callerReq) || | ||
typeof callerReq !== 'object') { | ||
const error = new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.invalidRequest, `Requests must be plain objects. Received: ${typeof callerReq}`, { request: callerReq }); | ||
return callback(error, { id: null, jsonrpc: '2.0', error }); | ||
} | ||
throw error; | ||
} | ||
}, _JsonRpcEngine_handle = | ||
/** | ||
* Ensures that the request / notification object is valid, processes it, and | ||
* passes any error and response object to the given callback. | ||
* | ||
* Does not reject. | ||
* | ||
* @param callerReq - The request object from the caller. | ||
* @param callback - The callback function. | ||
* @returns Nothing. | ||
*/ | ||
async function _JsonRpcEngine_handle(callerReq, callback) { | ||
if (!callerReq || | ||
Array.isArray(callerReq) || | ||
typeof callerReq !== 'object') { | ||
const error = new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.invalidRequest, `Requests must be plain objects. Received: ${typeof callerReq}`, { request: callerReq }); | ||
return callback(error, { id: null, jsonrpc: '2.0', error }); | ||
} | ||
if (typeof callerReq.method !== 'string') { | ||
const error = new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.invalidRequest, `Must specify a string method. Received: ${typeof callerReq.method}`, { request: callerReq }); | ||
if (typeof callerReq.method !== 'string') { | ||
const error = new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.invalidRequest, `Must specify a string method. Received: ${typeof callerReq.method}`, { request: callerReq }); | ||
if (__classPrivateFieldGet(this, _JsonRpcEngine_notificationHandler, "f") && !(0, utils_1.isJsonRpcRequest)(callerReq)) { | ||
// Do not reply to notifications, even if they are malformed. | ||
return callback(null); | ||
} | ||
return callback(error, { | ||
// Typecast: This could be a notification, but we want to access the | ||
// `id` even if it doesn't exist. | ||
id: (_b = callerReq.id) !== null && _b !== void 0 ? _b : null, | ||
jsonrpc: '2.0', | ||
error, | ||
}); | ||
} | ||
// Handle notifications. | ||
// We can't use isJsonRpcNotification here because that narrows callerReq to | ||
// "never" after the if clause for unknown reasons. | ||
if (__classPrivateFieldGet(this, _JsonRpcEngine_notificationHandler, "f") && !(0, utils_1.isJsonRpcRequest)(callerReq)) { | ||
// Do not reply to notifications, even if they are malformed. | ||
try { | ||
yield __classPrivateFieldGet(this, _JsonRpcEngine_notificationHandler, "f").call(this, callerReq); | ||
} | ||
catch (error) { | ||
return callback(error); | ||
} | ||
return callback(null); | ||
} | ||
return callback(error, { | ||
// Typecast: This could be a notification, but we want to access the | ||
// `id` even if it doesn't exist. | ||
id: callerReq.id ?? null, | ||
jsonrpc: '2.0', | ||
error, | ||
}); | ||
} | ||
// Handle notifications. | ||
// We can't use isJsonRpcNotification here because that narrows callerReq to | ||
// "never" after the if clause for unknown reasons. | ||
if (__classPrivateFieldGet(this, _JsonRpcEngine_notificationHandler, "f") && !(0, utils_1.isJsonRpcRequest)(callerReq)) { | ||
let error = null; | ||
// Handle requests. | ||
// Typecast: Permit missing id's for backwards compatibility. | ||
const req = Object.assign({}, callerReq); | ||
const res = { | ||
id: req.id, | ||
jsonrpc: req.jsonrpc, | ||
}; | ||
try { | ||
await __classPrivateFieldGet(this, _JsonRpcEngine_notificationHandler, "f").call(this, callerReq); | ||
yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_processRequest).call(JsonRpcEngine, req, res, __classPrivateFieldGet(this, _JsonRpcEngine_middleware, "f")); | ||
} | ||
catch (error) { | ||
return callback(error); | ||
catch (_error) { | ||
// A request handler error, a re-thrown middleware error, or something | ||
// unexpected. | ||
error = _error; | ||
} | ||
return callback(null); | ||
} | ||
let error = null; | ||
// Handle requests. | ||
// Typecast: Permit missing id's for backwards compatibility. | ||
const req = { ...callerReq }; | ||
const res = { | ||
id: req.id, | ||
jsonrpc: req.jsonrpc, | ||
}; | ||
try { | ||
await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_processRequest).call(JsonRpcEngine, req, res, __classPrivateFieldGet(this, _JsonRpcEngine_middleware, "f")); | ||
} | ||
catch (_error) { | ||
// A request handler error, a re-thrown middleware error, or something | ||
// unexpected. | ||
error = _error; | ||
} | ||
if (error) { | ||
// Ensure no result is present on an errored response | ||
delete res.result; | ||
if (!res.error) { | ||
res.error = (0, rpc_errors_1.serializeError)(error); | ||
if (error) { | ||
// Ensure no result is present on an errored response | ||
delete res.result; | ||
if (!res.error) { | ||
res.error = (0, rpc_errors_1.serializeError)(error); | ||
} | ||
} | ||
} | ||
return callback(error, res); | ||
}, _JsonRpcEngine_processRequest = async function _JsonRpcEngine_processRequest(req, res, middlewares) { | ||
const [error, isComplete, returnHandlers] = await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runAllMiddleware).call(JsonRpcEngine, req, res, middlewares); | ||
// Throw if "end" was not called, or if the response has neither a result | ||
// nor an error. | ||
__classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_checkForCompletion).call(JsonRpcEngine, req, res, isComplete); | ||
// The return handlers should run even if an error was encountered during | ||
// middleware processing. | ||
await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runReturnHandlers).call(JsonRpcEngine, returnHandlers); | ||
// Now we re-throw the middleware processing error, if any, to catch it | ||
// further up the call chain. | ||
if (error) { | ||
// eslint-disable-next-line @typescript-eslint/no-throw-literal | ||
throw error; | ||
} | ||
}, _JsonRpcEngine_runAllMiddleware = async function _JsonRpcEngine_runAllMiddleware(req, res, middlewares) { | ||
const returnHandlers = []; | ||
let error = null; | ||
let isComplete = false; | ||
// Go down stack of middleware, call and collect optional returnHandlers | ||
for (const middleware of middlewares) { | ||
[error, isComplete] = await __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runMiddleware).call(JsonRpcEngine, req, res, middleware, returnHandlers); | ||
if (isComplete) { | ||
break; | ||
return callback(error, res); | ||
}); | ||
}, _JsonRpcEngine_processRequest = function _JsonRpcEngine_processRequest(req, res, middlewares) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const [error, isComplete, returnHandlers] = yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runAllMiddleware).call(JsonRpcEngine, req, res, middlewares); | ||
// Throw if "end" was not called, or if the response has neither a result | ||
// nor an error. | ||
__classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_checkForCompletion).call(JsonRpcEngine, req, res, isComplete); | ||
// The return handlers should run even if an error was encountered during | ||
// middleware processing. | ||
yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runReturnHandlers).call(JsonRpcEngine, returnHandlers); | ||
// Now we re-throw the middleware processing error, if any, to catch it | ||
// further up the call chain. | ||
if (error) { | ||
// eslint-disable-next-line @typescript-eslint/no-throw-literal | ||
throw error; | ||
} | ||
} | ||
return [error, isComplete, returnHandlers.reverse()]; | ||
}, _JsonRpcEngine_runMiddleware = async function _JsonRpcEngine_runMiddleware(request, response, middleware, returnHandlers) { | ||
return new Promise((resolve) => { | ||
const end = (error) => { | ||
const parsedError = error || response.error; | ||
if (parsedError) { | ||
response.error = (0, rpc_errors_1.serializeError)(parsedError); | ||
}); | ||
}, _JsonRpcEngine_runAllMiddleware = function _JsonRpcEngine_runAllMiddleware(req, res, middlewares) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const returnHandlers = []; | ||
let error = null; | ||
let isComplete = false; | ||
// Go down stack of middleware, call and collect optional returnHandlers | ||
for (const middleware of middlewares) { | ||
[error, isComplete] = yield __classPrivateFieldGet(JsonRpcEngine, _a, "m", _JsonRpcEngine_runMiddleware).call(JsonRpcEngine, req, res, middleware, returnHandlers); | ||
if (isComplete) { | ||
break; | ||
} | ||
// True indicates that the request should end | ||
resolve([parsedError, true]); | ||
}; | ||
const next = (returnHandler) => { | ||
if (response.error) { | ||
end(response.error); | ||
} | ||
else { | ||
if (returnHandler) { | ||
if (typeof returnHandler !== 'function') { | ||
end(new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.internal, `JsonRpcEngine: "next" return handlers must be functions. ` + | ||
`Received "${typeof returnHandler}" for request:\n${jsonify(request)}`, { request: request })); | ||
} | ||
return [error, isComplete, returnHandlers.reverse()]; | ||
}); | ||
}, _JsonRpcEngine_runMiddleware = function _JsonRpcEngine_runMiddleware(request, response, middleware, returnHandlers) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
const end = (error) => { | ||
const parsedError = error || response.error; | ||
if (parsedError) { | ||
response.error = (0, rpc_errors_1.serializeError)(parsedError); | ||
} | ||
// True indicates that the request should end | ||
resolve([parsedError, true]); | ||
}; | ||
const next = (returnHandler) => { | ||
if (response.error) { | ||
end(response.error); | ||
} | ||
else { | ||
if (returnHandler) { | ||
if (typeof returnHandler !== 'function') { | ||
end(new rpc_errors_1.JsonRpcError(rpc_errors_1.errorCodes.rpc.internal, `JsonRpcEngine: "next" return handlers must be functions. ` + | ||
`Received "${typeof returnHandler}" for request:\n${jsonify(request)}`, { request: request })); | ||
} | ||
returnHandlers.push(returnHandler); | ||
} | ||
returnHandlers.push(returnHandler); | ||
// False indicates that the request should not end | ||
resolve([null, false]); | ||
} | ||
// False indicates that the request should not end | ||
resolve([null, false]); | ||
}; | ||
try { | ||
middleware(request, response, next, end); | ||
} | ||
}; | ||
try { | ||
middleware(request, response, next, end); | ||
catch (error) { | ||
end(error); | ||
} | ||
}); | ||
}); | ||
}, _JsonRpcEngine_runReturnHandlers = function _JsonRpcEngine_runReturnHandlers(handlers) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (const handler of handlers) { | ||
yield new Promise((resolve, reject) => { | ||
handler((error) => (error ? reject(error) : resolve())); | ||
}); | ||
} | ||
catch (error) { | ||
end(error); | ||
} | ||
}); | ||
}, _JsonRpcEngine_runReturnHandlers = async function _JsonRpcEngine_runReturnHandlers(handlers) { | ||
for (const handler of handlers) { | ||
await new Promise((resolve, reject) => { | ||
handler((error) => (error ? reject(error) : resolve())); | ||
}); | ||
} | ||
}, _JsonRpcEngine_checkForCompletion = function _JsonRpcEngine_checkForCompletion(request, response, isComplete) { | ||
@@ -342,0 +346,0 @@ if (!(0, utils_1.hasProperty)(response, 'result') && !(0, utils_1.hasProperty)(response, 'error')) { |
@@ -10,1 +10,2 @@ import type { Json, JsonRpcParams } from '@metamask/utils'; | ||
export declare function mergeMiddleware(middlewareStack: JsonRpcMiddleware<JsonRpcParams, Json>[]): JsonRpcMiddleware<JsonRpcParams, Json>; | ||
//# sourceMappingURL=mergeMiddleware.d.ts.map |
{ | ||
"name": "@metamask/json-rpc-engine", | ||
"version": "7.2.0", | ||
"description": "A tool for processing JSON-RPC messages.", | ||
"homepage": "https://github.com/MetaMask/json-rpc-engine#readme", | ||
"version": "7.3.0", | ||
"description": "A tool for processing JSON-RPC messages", | ||
"keywords": [ | ||
"MetaMask", | ||
"Ethereum" | ||
], | ||
"homepage": "https://github.com/MetaMask/core/tree/main/packages/json-rpc-engine#readme", | ||
"bugs": { | ||
"url": "https://github.com/MetaMask/json-rpc-engine/issues" | ||
"url": "https://github.com/MetaMask/core/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/MetaMask/json-rpc-engine.git" | ||
"url": "https://github.com/MetaMask/core.git" | ||
}, | ||
"license": "ISC", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"directories": { | ||
@@ -20,3 +24,3 @@ "test": "test" | ||
"files": [ | ||
"dist" | ||
"dist/" | ||
], | ||
@@ -27,2 +31,3 @@ "scripts": { | ||
"build:docs": "typedoc", | ||
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/json-rpc-engine --tag-prefix-before-package-rename json-rpc-engine@ --version-before-package-rename 6.1.0", | ||
"lint": "yarn lint:eslint && yarn lint:misc --check && yarn lint:dependencies && yarn lint:changelog", | ||
@@ -34,43 +39,26 @@ "lint:changelog": "auto-changelog validate", | ||
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern", | ||
"prepack": "./scripts/prepack.sh", | ||
"test": "jest && jest-it-up", | ||
"publish:preview": "yarn npm publish --tag preview", | ||
"test": "jest --reporters=jest-silent-reporter", | ||
"test:clean": "jest --clearCache", | ||
"test:verbose": "jest --verbose", | ||
"test:watch": "jest --watch" | ||
}, | ||
"dependencies": { | ||
"@metamask/rpc-errors": "^6.0.0", | ||
"@metamask/rpc-errors": "^6.1.0", | ||
"@metamask/safe-event-emitter": "^3.0.0", | ||
"@metamask/utils": "^8.1.0" | ||
"@metamask/utils": "^8.2.0" | ||
}, | ||
"devDependencies": { | ||
"@lavamoat/allow-scripts": "^2.3.1", | ||
"@metamask/auto-changelog": "^3.1.0", | ||
"@metamask/eslint-config": "^12.2.0", | ||
"@metamask/eslint-config-jest": "^12.1.0", | ||
"@metamask/eslint-config-nodejs": "^12.1.0", | ||
"@metamask/eslint-config-typescript": "^12.1.0", | ||
"@types/jest": "^29.5.0", | ||
"@types/node": "^18.15.11", | ||
"@typescript-eslint/eslint-plugin": "^5.43.0", | ||
"@typescript-eslint/parser": "^5.43.0", | ||
"@metamask/auto-changelog": "^3.4.3", | ||
"@types/jest": "^27.4.1", | ||
"deepmerge": "^4.2.2", | ||
"depcheck": "^1.4.3", | ||
"eslint": "^8.27.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-import-resolver-typescript": "^2.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-jest": "^27.1.5", | ||
"eslint-plugin-jsdoc": "^39.6.2", | ||
"eslint-plugin-n": "^15.7.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-promise": "^6.1.1", | ||
"jest": "^29.5.0", | ||
"jest-it-up": "^2.2.0", | ||
"prettier": "^2.8.7", | ||
"prettier-plugin-packagejson": "^2.4.3", | ||
"rimraf": "^5.0.0", | ||
"ts-jest": "^29.1.0", | ||
"ts-node": "^10.9.1", | ||
"typedoc": "^0.24.1", | ||
"jest": "^27.5.1", | ||
"jest-it-up": "^2.0.2", | ||
"ts-jest": "^27.1.4", | ||
"typedoc": "^0.24.8", | ||
"typescript": "~4.8.4" | ||
}, | ||
"packageManager": "yarn@3.5.0", | ||
"packageManager": "yarn@3.3.0", | ||
"engines": { | ||
@@ -77,0 +65,0 @@ "node": ">=16.0.0" |
@@ -5,2 +5,10 @@ # `@metamask/json-rpc-engine` | ||
## Installation | ||
`yarn add @metamask/json-rpc-engine` | ||
or | ||
`npm install @metamask/json-rpc-engine` | ||
## Usage | ||
@@ -195,12 +203,4 @@ | ||
## Running tests | ||
## Contributing | ||
Build the project if not already built: | ||
```bash | ||
yarn build | ||
``` | ||
```bash | ||
yarn test | ||
``` | ||
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme). |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94391
10
32
739
Updated@metamask/rpc-errors@^6.1.0
Updated@metamask/utils@^8.2.0