Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@metamask/json-rpc-middleware-stream

Package Overview
Dependencies
Maintainers
11
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/json-rpc-middleware-stream

A small toolset for streaming JSON-RPC data and matching requests and responses

  • 8.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
171K
decreased by-1.58%
Maintainers
11
Weekly downloads
 
Created

What is @metamask/json-rpc-middleware-stream?

@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.

What are @metamask/json-rpc-middleware-stream's main functionalities?

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: [] });

Other packages similar to @metamask/json-rpc-middleware-stream

Keywords

FAQs

Package last updated on 29 Oct 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc