jscribe

Effortlessly send, receive, and validate JSON over streams

Usage no npm install needed!

<script type="module">
  import jscribe from 'https://cdn.skypack.dev/jscribe';
</script>

README

jscribe

Small library for sending and receiving JSON over streams. Data validation à la JSON schema included.

Especially useful with TCP, TLS, and IPC sockets.

Install

Requires Node 14.x or later.

npm i jscribe

Usage

Here's a full example with a TCP server + client.

Register stream with callback:

const jscribe = require('jscribe')

const stream = getReadableStream()

const opts = {
  destroyOnError: false,
  maxBufferSize: 4096,
  once: false,
  schema: null // JSON schema for message validation
}

// called when there's an error or
// each time a message is received
const cb = (err, msg) => { ... }

jscribe(stream, opts, cb)

// or

jscribe(stream, cb)

Send a message:

const jscribe = require('jscribe')

const stream = getWritableStream()
const msg = { foo: 1, bar: 'baz' }

jscribe.send(stream, msg)

Receive a message:

const jscribe = require('jscribe')

const stream = getReadableStream()
const opts = { timeout: 3e3, ... } // includes opts for jscribe()

jscribe.receive(stream, opts)
  .then(msg => { ... })
  .catch(err => { ... })

Documentation

To generate the docs:

npm run docs

Then open out/index.html in your browser.

Tests

npm test

Linting

npm run lint

Contributing

Go for it! You're welcome to open an issue or create a pull request.

Make sure linting and tests pass before requesting review on a PR!