{"id":21660234,"url":"https://github.com/MetaMask/json-rpc-engine","last_synced_at":"2025-07-17T23:31:04.703Z","repository":{"id":37549977,"uuid":"69557939","full_name":"MetaMask/json-rpc-engine","owner":"MetaMask","description":"A tool for processing JSON RPC","archived":true,"fork":false,"pushed_at":"2023-11-09T00:34:27.000Z","size":3192,"stargazers_count":160,"open_issues_count":5,"forks_count":63,"subscribers_count":75,"default_branch":"main","last_synced_at":"2025-06-15T16:00:52.178Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MetaMask.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":"metamask","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-09-29T10:40:15.000Z","updated_at":"2025-01-05T16:55:08.000Z","dependencies_parsed_at":"2024-06-18T12:24:27.362Z","dependency_job_id":"a696d33b-4c00-4493-ba45-284ce5a684b7","html_url":"https://github.com/MetaMask/json-rpc-engine","commit_stats":{"total_commits":179,"total_committers":16,"mean_commits":11.1875,"dds":0.5865921787709497,"last_synced_commit":"cb6714ca804ab73f5f14d00557fc63e7e011bf24"},"previous_names":["kumavis/json-rpc-engine"],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/MetaMask/json-rpc-engine","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fjson-rpc-engine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fjson-rpc-engine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fjson-rpc-engine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fjson-rpc-engine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MetaMask","download_url":"https://codeload.github.com/MetaMask/json-rpc-engine/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MetaMask%2Fjson-rpc-engine/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265678474,"owners_count":23810114,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-25T09:32:31.953Z","updated_at":"2025-07-17T23:31:04.166Z","avatar_url":"https://github.com/MetaMask.png","language":"TypeScript","readme":"# `@metamask/json-rpc-engine`\n\n\u003ctable\u003e\u003ctr\u003e\u003ctd\u003e\u003cp align=\"center\"\u003e\u003cb\u003e⚠️ PLEASE READ ⚠️\u003c/b\u003e\u003c/p\u003e\u003cp align=\"center\"\u003eThis package has been migrated to our \u003ca href=\"https://github.com/MetaMask/core\"\u003e\u003ccode\u003ecore\u003c/code\u003e\u003c/a\u003e monorepo, and this repository has been archived. Please note that all future development and feature releases will take place in the \u003ca href=\"https://github.com/MetaMask/core\"\u003e\u003ccode\u003ecore\u003c/code\u003e\u003c/a\u003e repository.\u003c/p\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/table\u003e\n\nA tool for processing JSON-RPC requests and responses.\n\n## Usage\n\n```js\nconst { JsonRpcEngine } = require('@metamask/json-rpc-engine');\n\nconst engine = new JsonRpcEngine();\n```\n\nBuild a stack of JSON-RPC processors by pushing middleware to the engine.\n\n```js\nengine.push(function (req, res, next, end) {\n  res.result = 42;\n  end();\n});\n```\n\nRequests are handled asynchronously, stepping down the stack until complete.\n\n```js\nconst request = { id: 1, jsonrpc: '2.0', method: 'hello' };\n\nengine.handle(request, function (err, response) {\n  // Do something with response.result, or handle response.error\n});\n\n// There is also a Promise signature\nconst response = await engine.handle(request);\n```\n\nMiddleware have direct access to the request and response objects.\nThey can let processing continue down the stack with `next()`, or complete the request with `end()`.\n\n```js\nengine.push(function (req, res, next, end) {\n  if (req.skipCache) return next();\n  res.result = getResultFromCache(req);\n  end();\n});\n```\n\nBy passing a _return handler_ to the `next` function, you can get a peek at the result before it returns.\n\n```js\nengine.push(function (req, res, next, end) {\n  next(function (cb) {\n    insertIntoCache(res, cb);\n  });\n});\n```\n\nIf you specify a `notificationHandler` when constructing the engine, JSON-RPC notifications passed to `handle()` will be handed off directly to this function without touching the middleware stack:\n\n```js\nconst engine = new JsonRpcEngine({ notificationHandler });\n\n// A notification is defined as a JSON-RPC request without an `id` property.\nconst notification = { jsonrpc: '2.0', method: 'hello' };\n\nconst response = await engine.handle(notification);\nconsole.log(typeof response); // 'undefined'\n```\n\nEngines can be nested by converting them to middleware using `JsonRpcEngine.asMiddleware()`:\n\n```js\nconst engine = new JsonRpcEngine();\nconst subengine = new JsonRpcEngine();\nengine.push(subengine.asMiddleware());\n```\n\n### `async` Middleware\n\nIf you require your middleware function to be `async`, use `createAsyncMiddleware`:\n\n```js\nconst { createAsyncMiddleware } = require('@metamask/json-rpc-engine');\n\nlet engine = new RpcEngine();\nengine.push(\n  createAsyncMiddleware(async (req, res, next) =\u003e {\n    res.result = 42;\n    next();\n  }),\n);\n```\n\n`async` middleware do not take an `end` callback.\nInstead, the request ends if the middleware returns without calling `next()`:\n\n```js\nengine.push(\n  createAsyncMiddleware(async (req, res, next) =\u003e {\n    res.result = 42;\n    /* The request will end when this returns */\n  }),\n);\n```\n\nThe `next` callback of `async` middleware also don't take return handlers.\nInstead, you can `await next()`.\nWhen the execution of the middleware resumes, you can work with the response again.\n\n```js\nengine.push(\n  createAsyncMiddleware(async (req, res, next) =\u003e {\n    res.result = 42;\n    await next();\n    /* Your return handler logic goes here */\n    addToMetrics(res);\n  }),\n);\n```\n\nYou can freely mix callback-based and `async` middleware:\n\n```js\nengine.push(function (req, res, next, end) {\n  if (!isCached(req)) {\n    return next((cb) =\u003e {\n      insertIntoCache(res, cb);\n    });\n  }\n  res.result = getResultFromCache(req);\n  end();\n});\n\nengine.push(\n  createAsyncMiddleware(async (req, res, next) =\u003e {\n    res.result = 42;\n    await next();\n    addToMetrics(res);\n  }),\n);\n```\n\n### Teardown\n\nIf your middleware has teardown to perform, you can assign a method `destroy()` to your middleware function(s),\nand calling `JsonRpcEngine.destroy()` will call this method on each middleware that has it.\nA destroyed engine can no longer be used.\n\n```js\nconst middleware = (req, res, next, end) =\u003e {\n  /* do something */\n};\nmiddleware.destroy = () =\u003e {\n  /* perform teardown */\n};\n\nconst engine = new JsonRpcEngine();\nengine.push(middleware);\n\n/* perform work */\n\n// This will call middleware.destroy() and destroy the engine itself.\nengine.destroy();\n\n// Calling any public method on the middleware other than `destroy()` itself\n// will throw an error.\nengine.handle(req);\n```\n\n### Gotchas\n\nHandle errors via `end(err)`, _NOT_ `next(err)`.\n\n```js\n/* INCORRECT */\nengine.push(function (req, res, next, end) {\n  next(new Error());\n});\n\n/* CORRECT */\nengine.push(function (req, res, next, end) {\n  end(new Error());\n});\n```\n\nHowever, `next()` will detect errors on the response object, and cause\n`end(res.error)` to be called.\n\n```js\nengine.push(function (req, res, next, end) {\n  res.error = new Error();\n  next(); /* This will cause end(res.error) to be called. */\n});\n```\n\n## Running tests\n\nBuild the project if not already built:\n\n```bash\nyarn build\n```\n\n```bash\nyarn test\n```\n","funding_links":["https://opencollective.com/metamask"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMetaMask%2Fjson-rpc-engine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMetaMask%2Fjson-rpc-engine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMetaMask%2Fjson-rpc-engine/lists"}