Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@metamask/json-rpc-middleware-stream
Advanced tools
A small toolset for streaming JSON-RPC data and matching requests and responses
@metamask/json-rpc-middleware-stream is a package that provides middleware for JSON-RPC communication over streams. It is particularly useful for building applications that need to handle JSON-RPC requests and responses in a streaming context, such as browser extensions or other environments where communication between different contexts is required.
Creating a JSON-RPC Stream
This feature allows you to create a JSON-RPC stream middleware and integrate it with a JSON-RPC engine. The stream can then be used to send and receive JSON-RPC messages.
const { createStreamMiddleware } = require('@metamask/json-rpc-middleware-stream');
const { createEngineStream } = require('json-rpc-engine');
const { JsonRpcEngine } = require('json-rpc-engine');
const engine = new JsonRpcEngine();
const streamMiddleware = createStreamMiddleware();
engine.push(streamMiddleware.middleware);
const stream = createEngineStream({ engine });
// Now you can use the stream for communication
stream.on('data', (data) => {
console.log('Received data:', data);
});
stream.write({ id: 1, jsonrpc: '2.0', method: 'eth_blockNumber', params: [] });
Handling JSON-RPC Requests
This feature demonstrates how to handle JSON-RPC requests using the middleware. In this example, the middleware responds to the 'eth_blockNumber' method with a mock block number.
const { createStreamMiddleware } = require('@metamask/json-rpc-middleware-stream');
const { JsonRpcEngine } = require('json-rpc-engine');
const engine = new JsonRpcEngine();
const streamMiddleware = createStreamMiddleware();
engine.push(streamMiddleware.middleware);
engine.push((req, res, next, end) => {
if (req.method === 'eth_blockNumber') {
res.result = '0x10'; // Example block number
end();
} else {
next();
}
});
const stream = streamMiddleware.stream;
stream.on('data', (data) => {
console.log('Received data:', data);
});
stream.write({ id: 1, jsonrpc: '2.0', method: 'eth_blockNumber', params: [] });
json-rpc-engine is a framework for building JSON-RPC middleware stacks. It allows you to create a series of middleware functions that can process JSON-RPC requests and responses. Compared to @metamask/json-rpc-middleware-stream, json-rpc-engine focuses more on the middleware stack itself rather than the streaming aspect.
web3-provider-engine is a tool for composing Ethereum provider middleware using a JSON-RPC engine. It is commonly used in Ethereum dApps to handle various provider-related tasks. While it also deals with JSON-RPC, it is more specialized for Ethereum and includes additional features for managing provider connections and caching.
A small toolset for streaming JSON RPC data and matching requests and responses. Made to be used with @metamask/json-rpc-engine
.
yarn add @metamask/json-rpc-middleware-stream
or
npm install @metamask/json-rpc-middleware-stream
This package is part of a monorepo. Instructions for contributing can be found in the monorepo README.
FAQs
A small toolset for streaming JSON-RPC data and matching requests and responses
The npm package @metamask/json-rpc-middleware-stream receives a total of 137,445 weekly downloads. As such, @metamask/json-rpc-middleware-stream popularity was classified as popular.
We found that @metamask/json-rpc-middleware-stream demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.