npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@magne4000/json-rpc-peer

v0.16.0

Published

JSON-RPC 2 transport-agnostic library (temporary Typescript fork)

Downloads

6

Readme

json-rpc-peer Build Status

JSON-RPC 2 transport-agnostic library

Install

Installation of the npm package:

> npm install --save json-rpc-peer

Usage

This library provides a high-level peer implementation which should be flexible enough to use in any environments.

// ES5
var Peer = require('json-rpc-peer')['default']

// ES6
import Peer from 'json-rpc-peer'

Construction

var peer = new Peer(function onMessage (message) {
  // Here is the main handler where every incoming
  // notification/request message goes.
  //
  // For a request, this function just has to throw an exception or
  // return a value to send the related response.
  //
  // If the response is asynchronous, just return a promise.
})

The onMessage parameter is optional, it can be omitted if this peer does not handle notifications and requests.

Note: For security concerns, only exceptions which are instance of JsonRpcError will be transmitted to the remote peer, all others will be substituted by an instance of UnknownError.

Connection

The peer is now almost ready, but before being usable, it has to be connected to the transport layer.

The simplest interface, the exec() method, has some limitations (no notifications support) but is often good enough.

It is often used with non-connected protocols such as HTTP:

var readAllSteam = require('read-all-stream')

// For this example we create an HTTP server:
require('http').createServer({
  port: 8081
}, function onRequest (req, res) {
  // Read the whole request body.
  readAllStream(req, function (err, data) {
    // Error handling would be better.
    if (err) return

    // Here `peer` is not used as a stream, it can therefore be used
    // to handle all the connections.
    peer.exec(message).then(function (response) {
      res.end(response)
    })
  })
})

If you have a connected transport, such as WebSocket, you may want to use the stream interface: the peer is a duplex stream and can therefore be connected to other streams via the pipe() method:

// For this example, we create a WebSocket server:
require('websocket-stream').createServer({
  port: 8080
}, function onConnection (stream) {
  // Because a stream can only be used once, it is necessary to create
  // a dedicated peer per connection.
  stream.pipe(new Peer(onMessage)).pipe(stream)
})

Notification

peer.notify('foo', ['bar'])

Request

The request() method returns a promise which will be resolved or rejected when the response will be received.

peer.request('add', [1, 2]).then(function (result) {
  console.log(result)
}).catch(function (error) {
  console.error(error.message)
})

Failure

Sometimes it is known that current pending requests will not get answered (e.g. connection lost), it is therefore necessary to fail them manually.

peer.request('add', [1, 2]).catch(function (reason) {
  console.error(reason)
  // → connection lost
})

peer.failPendingRequests('connection lost');

Low level interface

json-rpc-peer also exports everything from json-rpc-protocol.

// ES5
var peer = require('json-rpc-peer')

var format = peer.format
var parse = peer.parse
var JsonRpcError = peer.JsonRpcError
var InvalidJson = peer.InvalidJson
var InvalidRequest = peer.InvalidRequest
var MethodNotFound = peer.MethodNotFound
var InvalidParameters = peer.InvalidParameters

// ES2015 (formerly known as ES6)
import {
  format,
  parse,
  JsonRpcError,
  InvalidJson,
  InvalidRequest,
  MethodNotFound,
  InvalidParameters
} from 'json-rpc-peer'

Development

# Install dependencies
> yarn

# Run the tests
> yarn test

# Continuously compile
> yarn dev

# Continuously run the tests
> yarn dev-test

# Build for production
> yarn build

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet