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

raptor-rpc

v0.4.5

Published

Raptor RPC is a transport-agnostic RPC server with middleware support and an easy to use api. It will allow you to have a server up and running in a matter of minutes, without limiting you to a specific transport.

Downloads

15

Readme

Raptor RPC

Raptor RPC is a transport-agnostic RPC server with middleware support and an easy to use api. It will allow you to have a server up and running in a matter of minutes, without limiting you to a specific transport.

It works out of the box with the standard transports provided by Node.js and also with some popular frameworks.

Installation

npm install --save raptor-rpc

Usage

// Raptor library
const Raptor = require('raptor-rpc')

// Example libraries
const fetch = require('node-fetch')
const readJsonFile = require('read-json-file')

const app = new Raptor()

app.use(function (req, next) {
  console.log('Incoming request!')
  return next()
})

app.method('ping', function (req) {
  return 'pong'
})

app.method('get-package-name', function (req) {
  return readJsonFile('package.json')
    .then(pkg => pkg.name)
})

app.method('random-name', function (req) {
  return fetch('http://uinames.com/api/')
    .then(res => res.json())
    .then(body => `${body.name} ${body.surname}`)
})

app.serve('http', 1337, function () {
  console.log('Listening on http://localhost:1337/')
})

API

Raptor

.use(fn)

Add middleware to the server.

  • fn: Middleware function with the signature (req, next) => Promise

The middleware function should return a promise of a response. Calling next will return a promise of the next middleware or, if no other middleware exists, the method handler.

.method(name, fn)

Register a method with the server.

  • name: The method name
  • fn: Method handler with the signature (req) => Promise

The method handler should return a promise of what to respond with. It's also acceptable to return the value right away, since the handler is wrapped in a .then call. This also means that any errors thrown will be caught correctly.

.handle(...)

Handle a request, accepts a number of different parameters.

Read more under Transports.

.attach(server)

Attaches Raptor to the server, accepts a number of different parameters.

Read more under Transports.

.serve(type, port[, cb])

Starts a server and accepts connections.

  • type: Which transport to use (dgram, http, net)
  • port: Which port to listen to
  • cb: Function to be called when accepting connections

Returns the server instance (dgram.Socket, http.Server or net.Server).

Request

.id

The jsonrpc id of the request, can be undefined. This variable is read-only.

.method

The method name of the request. This variable is read-only.

.params

The params object as passed from the client. Can be Array or Object.

.param(key, defValue)

Helper function to get a parameter or a default value if it wasn't provided.

  • key: Key to fetch, can be Number or String
  • defValue: Value to return if key wasn't provided, is undefined if not specified.

.require(key, type)

Helper function to require the presence a parameter and optionally check it's type. Will send an Invalid params error (-32602) back to the client, and stop execution, if the parameter is not present. It also returns the value of the parameter.

  • key: Key to require, can be Number or String
  • type: If specified, also require the parameter to be of this type

Valid values for type is specified in the JSON Schema: core definitions and terminology and RFC 4627.

This is implemented by throwing an Error if the key isn't present, or is of the wrong type.

.source

The source of the connection, i.e. the stream that was piped to the connection.

.remote

Info about the other end of the connection. Includes three keys:

  • type: Type of the transport (unknown, dgram, http, net)
  • address: Ip address of the remote
  • port: The remote port

Error handling

If you reject within a middleware or a method handler, that error will be sent back to the client. The description will be err.toString() and the code err.rpcCode. If rpcCode is undefined the code sent will be 0.

You can also include additional data by providing err.rpcData. rpcData can be of any type.

Transports

Express

const app = express()
const raptor = new Raptor()

app.post('/api', raptor.handle)
app.listen(1337)

Dgram

const raptor = new Raptor()
const socket = dgram.createSocket('udp4')

raptor.attach(socket);
socket.bind(1337);

Net

const raptor = new Raptor()
const server = net.createServer({ allowHalfOpen: true })

raptor.attach(server);
server.listen(1337);

Http

const raptor = new Raptor()
const server = http.createServer()

raptor.attach(server);
server.listen(1337);

License

Copyright © 2014 Linus Unnebäck Licensed under the MIT License (MIT)