json-rpc-middleware-stream
Advanced tools
Comparing version 3.0.0 to 4.0.0
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
@@ -10,9 +9,16 @@ | ||
## [4.0.0] - 2022-10-03 | ||
### Changed | ||
- BREAKING: Add Node 12 as minimum required version [#15](https://github.com/MetaMask/json-rpc-middleware-stream/pull/15) | ||
- Retry pending requests when notification to reconnect is received ([#27](https://github.com/MetaMask/json-rpc-middleware-stream/pull/27)) | ||
### Security | ||
- Add `@lavamoat/allow-scripts` to make dependency install scripts opt-in ([#25](https://github.com/MetaMask/json-rpc-middleware-stream/pull/25)) | ||
## [3.0.0] - 2020-12-08 | ||
### Added | ||
- TypeScript typings ([#11](https://github.com/MetaMask/json-rpc-middleware-stream/pull/11)) | ||
[Unreleased]:https://github.com/MetaMask/json-rpc-middleware-stream/compare/v3.0.0...HEAD | ||
[3.0.0]:https://github.com/MetaMask/json-rpc-middleware-stream/compare/v2.1.1...v3.0.0 | ||
[Unreleased]: https://github.com/MetaMask/json-rpc-middleware-stream/compare/v4.0.0...HEAD | ||
[4.0.0]: https://github.com/MetaMask/json-rpc-middleware-stream/compare/v3.0.0...v4.0.0 | ||
[3.0.0]: https://github.com/MetaMask/json-rpc-middleware-stream/releases/tag/v3.0.0 |
@@ -16,3 +16,3 @@ "use strict"; | ||
const { engine } = opts; | ||
const stream = new readable_stream_1.Duplex({ objectMode: true, read, write }); | ||
const stream = new readable_stream_1.Duplex({ objectMode: true, read: () => undefined, write }); | ||
// forward notifications | ||
@@ -25,5 +25,9 @@ if (engine.on) { | ||
return stream; | ||
function read() { | ||
return undefined; | ||
} | ||
/** | ||
* Write a JSON-RPC request to the stream. | ||
* | ||
* @param req - The JSON-rpc request. | ||
* @param _encoding - The stream encoding, not used. | ||
* @param cb - The stream write callback. | ||
*/ | ||
function write(req, _encoding, cb) { | ||
@@ -30,0 +34,0 @@ engine.handle(req, (_err, res) => { |
import SafeEventEmitter from '@metamask/safe-event-emitter'; | ||
import { Duplex } from 'readable-stream'; | ||
import { JsonRpcMiddleware } from 'json-rpc-engine'; | ||
interface Options { | ||
retryOnMessage?: string; | ||
} | ||
/** | ||
@@ -10,5 +13,6 @@ * Creates a JsonRpcEngine middleware with an associated Duplex stream and | ||
* | ||
* @param options - Configuration options for middleware. | ||
* @returns The event emitter, middleware, and stream. | ||
*/ | ||
export default function createStreamMiddleware(): { | ||
export default function createStreamMiddleware(options?: Options): { | ||
events: SafeEventEmitter; | ||
@@ -18,1 +22,2 @@ middleware: JsonRpcMiddleware<unknown, unknown>; | ||
}; | ||
export {}; |
@@ -14,9 +14,10 @@ "use strict"; | ||
* | ||
* @param options - Configuration options for middleware. | ||
* @returns The event emitter, middleware, and stream. | ||
*/ | ||
function createStreamMiddleware() { | ||
const idMap = {}; | ||
function createStreamMiddleware(options = {}) { | ||
const idMap = {}; // TODO: replace with actual Map | ||
const stream = new readable_stream_1.Duplex({ | ||
objectMode: true, | ||
read: readNoop, | ||
read: () => undefined, | ||
write: processMessage, | ||
@@ -27,3 +28,3 @@ }); | ||
// write req to stream | ||
stream.push(req); | ||
sendToStream(req); | ||
// register request on id map | ||
@@ -33,7 +34,20 @@ idMap[req.id] = { req, res, next, end }; | ||
return { events, middleware, stream }; | ||
function readNoop() { | ||
return false; | ||
/** | ||
* Forwards JSON-RPC request to the stream. | ||
* | ||
* @param req - The JSON-RPC request object. | ||
*/ | ||
function sendToStream(req) { | ||
// TODO: limiting retries could be implemented here | ||
stream.push(req); | ||
} | ||
/** | ||
* Writes a JSON-RPC object to the stream. | ||
* | ||
* @param res - The JSON-RPC response object. | ||
* @param _encoding - The stream encoding, not used. | ||
* @param cb - The stream write callback. | ||
*/ | ||
function processMessage(res, _encoding, cb) { | ||
let err; | ||
let err = null; | ||
try { | ||
@@ -54,2 +68,7 @@ const isNotification = !res.id; | ||
} | ||
/** | ||
* Processes a JSON-RPC response. | ||
* | ||
* @param res - The response to process. | ||
*/ | ||
function processResponse(res) { | ||
@@ -67,7 +86,24 @@ const context = idMap[res.id]; | ||
} | ||
function processNotification(res) { | ||
events.emit('notification', res); | ||
/** | ||
* Processes a JSON-RPC notification. | ||
* | ||
* @param notif - The notification to process. | ||
*/ | ||
function processNotification(notif) { | ||
if ((options === null || options === void 0 ? void 0 : options.retryOnMessage) && notif.method === options.retryOnMessage) { | ||
retryStuckRequests(); | ||
} | ||
events.emit('notification', notif); | ||
} | ||
/** | ||
* Retry pending requests. | ||
*/ | ||
function retryStuckRequests() { | ||
Object.values(idMap).forEach(({ req }) => { | ||
// TODO: limiting retries could be implemented here | ||
sendToStream(req); | ||
}); | ||
} | ||
} | ||
exports.default = createStreamMiddleware; | ||
//# sourceMappingURL=createStreamMiddleware.js.map |
{ | ||
"name": "json-rpc-middleware-stream", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "A small toolset for streaming JSON-RPC data and matching requests and responses.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/MetaMask/json-rpc-middleware-stream.git" | ||
}, | ||
"license": "ISC", | ||
@@ -11,7 +15,12 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc --project .", | ||
"test": "yarn build && node test/index.js", | ||
"lint": "eslint . --ext ts,js,json", | ||
"lint:fix": "eslint . --ext ts,js,json --fix", | ||
"prepublishOnly": "yarn test" | ||
"build": "tsc --project tsconfig.build.json", | ||
"build:clean": "rimraf dist && yarn build", | ||
"lint": "yarn lint:eslint && yarn lint:misc --check", | ||
"lint:eslint": "eslint . --cache --ext js,ts", | ||
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", | ||
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern", | ||
"prepublishOnly": "yarn build:clean && yarn lint && yarn test", | ||
"setup": "yarn install && yarn allow-scripts", | ||
"test": "jest && jest-it-up", | ||
"test:watch": "jest --watch" | ||
}, | ||
@@ -23,22 +32,44 @@ "dependencies": { | ||
"devDependencies": { | ||
"@metamask/eslint-config": "^4.1.0", | ||
"@lavamoat/allow-scripts": "^1.0.5", | ||
"@metamask/auto-changelog": "^2.3.0", | ||
"@metamask/eslint-config": "^9.0.0", | ||
"@metamask/eslint-config-jest": "^9.0.0", | ||
"@metamask/eslint-config-nodejs": "^9.0.0", | ||
"@metamask/eslint-config-typescript": "^9.0.1", | ||
"@types/jest": "^26.0.13", | ||
"@types/node": "^17.0.23", | ||
"@types/readable-stream": "^2.3.9", | ||
"@typescript-eslint/eslint-plugin": "^4.9.0", | ||
"@typescript-eslint/parser": "^4.9.0", | ||
"eslint": "^7.14.0", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-json": "^2.1.1", | ||
"@typescript-eslint/eslint-plugin": "^4.21.0", | ||
"@typescript-eslint/parser": "^4.21.0", | ||
"eslint": "^7.23.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jest": "^24.3.4", | ||
"eslint-plugin-jsdoc": "^36.1.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.3.1", | ||
"extension-port-stream": "^2.0.1", | ||
"jest": "^27.5.1", | ||
"jest-it-up": "^2.0.2", | ||
"json-rpc-engine": "^6.1.0", | ||
"tape": "^5.0.0", | ||
"typescript": "^4.1.2" | ||
"prettier": "^2.2.1", | ||
"prettier-plugin-packagejson": "^2.2.17", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^27.1.4", | ||
"ts-node": "^10.7.0", | ||
"typescript": "^4.2.4", | ||
"webextension-polyfill-ts": "^0.26.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/MetaMask/json-rpc-middleware-stream.git" | ||
"engines": { | ||
"node": ">=14.0.0" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/MetaMask/json-rpc-middleware-stream/issues" | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"homepage": "https://github.com/MetaMask/json-rpc-middleware-stream#readme" | ||
"lavamoat": { | ||
"allowScripts": { | ||
"@lavamoat/preinstall-always-fail": false | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
19452
189
1
29
2
1