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

blue-frog-stream

v0.3.0

Published

parse and reduce the batch processing of JSON-RPC2.0 request/response

Downloads

8

Readme

blue-frog-stream

parse and reduce the batch processing of JSON-RPC2.0 request/respponse.

JSON-RPC 2.0 specifications

see example

example

http server

var rpc      = require('blue-frog-stream')
var response = require('blue-frog-core/response')
var rpcError = require('blue-frog-core/error')
var api      = require('./some-api')

function handler (err, result) {
    var batch = new rpc.response.BatchStream(true)

    if (err) {
        process.nextTick(() => {
            batch.end(response.error(null, rpcError.ParseError(err)))
        })
    }

    else {
        new rpc.request.ParseStream(result)
        .on('error', err => batch.write(InvalidRequest(err)))
        .pipe(through.obj((req, _, done) => {
            if (! api[req.method])
                return done(null, MethodNotFound(err, req)))

            api[req.method](req.params, (err, result) => {
                if (err) return done(null, InvalidParams(err, req))
                if (! req.id) done() // case notification
                else done(null, response(req.id, result))
            }))
        }))
        .pipe(batch)
    }

    return batch
}

var app = http.createServer((req, res) => {
    if (req.method.toUpperCase() === 'POST') helper(req, res, handler)
    else ecstatic(req, res)
})

function helper (req, res, stream) {
    body(req, res, (err, result) => stream(err, result).pipe(res))
}

function InvalidRequest (err) {
    return response.error(null, rpcError.InvalidRequest(err))
}

function MethodNotFound (err, req) {
    return response.error(req.id || null, rpcError.MethodNotFound(err))
}

function InvalidParams (err, req) {
    return response.error(req.id || null, rpcError.InvalidParams(err))
}

http client

var rpc     = require('blue-frog-stream')
var request = require('blue-frog-core/request')

var maap  = mapper()

var batch = rpc.request.BatchStream(true)
var hyp   = hyperquest.post('http://0.0.0.0:3000', {
    'content-type': 'application/json'
})

batch.on('error', onError)
.pipe(hyp).on('error', onError)
.once('response', function (response) {
    body(res, null, function (err, result) {
        if (err) return onError(err)
        new rpc.response.ParseStream(result).on('error', onError)
        .pipe(through.obj(function (res, _, done) {
            maap.value(res, done)
        }))
    })
})

var req1 = request('ID-123-start', 'method', ['params'])
var req2 = request.notification('method2', ['params2'])
var req3 = request('ID-123-finish', 'method')

maap.wrap(req1, function (result, done) {
    console.log(result)
    done()
})
maap.wrap(req3, function (result, done) {
    console.log(result)
    done()
})

batch.write(req1)
batch.write(req2)
batch.end(  req3)


function onError (err) {
    console.log(err)
}

function mapper () {
    return {
        map_: {}
      , wrap: function (req, onSuccess) {
            if (('id' in req) && req.id !== null)
                 return !! (this.map_[req.id] = onSuccess)
            else return false
        }
      , value: function (res, done) {
            if (! this.map_[res.id]) return done()
            else this.map_[res.id](data.result, done)
        }
    }
}

api

var rpc = require('blue-frog-stream')

create a request batch stream. transform stream.

var batch = new rpc.request.BatchStream(doJSONstringify)
  • doJSONstringify : boolean. if this value truthy, then export data is jsonString.
var batch = new rpc.request.BatchStream(true)
batch.on('error' function (err) {
    console.log(err)
   // TypeError: JSON-RPC 2.0 request must be "object"
})
batch.once('data', function (json) {
    console.log(json)
    // [
    // {"jsonrpc":"2.0","method":"sum","id":123,"params":[1,2,3]},
    // {"jsonrpc":"2.0","method":"add","id":null,"params":[4]},
    // {"jsonrpc":"2.0","method":"getResult","id":456}
    // ]
})

batch.write({jsonrpc: "2.0", id: 123, method: "sum", params: [1,2,3]})
batch.write('invalid data')
batch.write({jsonrpc: "2.0", id: null, method: "add", params: [4]})
batch.end(  {jsonrpc: "2.0", id: 456, method: "getResult"})

create a request parse stream. readable stream.

var parse = new rpc.request.ParseStream(jsonRpc2RequestObject)
  • jsonRpc2RequestObject : object or array.
var parse = new rpc.request.ParseStream([
    {jsonrpc: "2.0", id: 123, method: "sum", params: [1,2,3]}
  , {jsonrpc: "2.0", id: null, method: "add", params: [4]}
  , {foo: "bar"}
  , {jsonrpc: "2.0", id: 456, method: "getResult"}
])

parse.on('error', err => {
    console.error(err)
    // Error: this method name "foo" is not allowed
})
.on('data', requestObject => {
    ...
})

create a response batch stream. transform stream.

var batch = new rpc.response.BatchStream(doJSONstringify)
  • doJSONstringify : boolean. if this value truthy, then export data is jsonString.
var batch = new rpc.response.BatchStream(true)
batch.on('error' function (err) {
    console.log(err)
   // TypeError: JSON-RPC 2.0 response must be "object"
})
batch.once('data', function (json) {
    console.log(json)
    // [
    // {"jsonrpc":"2.0","id":123,"result":[1,2,3]},
    // {"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"Invalid params","data":"TypeError: \\"hoge\\" is not \\"number\\""}
    // {"jsonrpc":"2.0","id":456}
    // ]
})

batch.write({jsonrpc: "2.0", id: 123, result: {amount: 6}})
batch.write('invalid data')
batch.write({jsonrpc: "2.0", id: null, error: {code: -32602, message: "Invalid params", data: "TypeError: \"hoge\" is not \"number\""}})
batch.end(  {jsonrpc: "2.0", id: 456, result: {amount: 10}})

create a response parse stream. readable stream.

var parse = new rpc.response.ParseStream(jsonRpc2RequestObject)
  • jsonRpc2RequestObject : object or array.
var parse = new rpc.response.ParseStream([
    {jsonrpc: "2.0", id: 123, result: {amount: 6}}
  , {jsonrpc: "2.0", id: null, error: {code: -32602, message: "Invalid params", data: "TypeError: \"hoge\" is not \"number\""}})
  , {jsonrpc: "2.0", id: 456, result: {amount: 6}}
])

parse.on('error', err => {
    console.log(err)
    // {jsonrpc: "2.0", id: null, error: {code: -32602, message: "Invalid params", data: "TypeError: \"hoge\" is not \"number\""}})
})
.on('data', responseObject => {
    ...
})

see also

blue-frog-core

licence

MIT