Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
capnp-stream
Advanced tools
Readme
A Node.js A readable and writebale stream for Cap’n Proto.
With NPM:
npm install --save capnp-stream
Note: capnp binaries need to be available in your system. Check out the official Cap’n Proto install guide for OS-specific instructions.
capnp-stream
allows to consume Cap’n Proto files as a readable stream, that emits
objects.
Parse a Cap’n Proto encoded file
const { createReadStream } = require('fs')
const { join } = require('path')
const capnp = require('capnp')
const { ParseStream } = require('capnp-stream')
// import capnp schema
const schema = capnp.import(join(__dirname, 'person.capnp'))
// initialize stream for a given schema
const capnpStream = new ParseStream(schema.Person)
// print as a JSON every object in the stream
capnpStream.on('data', (d) => {
console.log(JSON.stringify(d, null, 2))
})
// pipe the data file to the capnp-stream instance
createReadStream('someCapnpDataFile')
.pipe(capnpStream)
Encode a incoming objects stream to a Cap’n Proto file:
const { createWriteStream } = require('fs')
const { join } = require('path')
const capnp = require('capnp')
const { SerializeStream } = require('capnp-stream')
// import capnp schema
const schema = capnp.import(join(__dirname, 'person.capnp'))
// initialize stream for a given schema
const capnpStream = new SerializeStream(schema.Person)
someObjectsStream // a stream that emits JavaScript objects that conform the capnp Schema
.pipe(capnpStream) // encodes the javascript objects to a capnp stream
.pipe(createWriteStream(join(__dirname, 'somefile.dat'))) // save the encoded capnp data to a file
skip
and emitEvery
options (sharding)Sometimes you don't want to consume (emit) all the objects in the capnp stream. For instance, this is common when you are processing the stream in parallel multiple times with multiple processes and you want to easily implement a sharding mechanism.
In these situations you can take advantage of the skip
and emitEvery
options that you can pass at construction time:
const capnp = require('capnp')
const { SerializeStream } = require('capnp-stream')
// import capnp schema
const schema = capnp.import(join(__dirname, 'person.capnp'))
// initialize stream for a given schema
const capnpStream = new SerializeStream(schema.Person, {
skip: process.env.SKIP,
emitEvery: process.env.EMIT_EVERY
})
In this example, we pass through these options from environment variables. If you set your environment variables to be SKIP=5
and EMIT_EVERY=2
you will get the following objects emitted (0 based count):
Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.
Licensed under MIT License. © Luciano Mammino.
FAQs
A readable and writebale stream for Cap’n Proto
The npm package capnp-stream receives a total of 6 weekly downloads. As such, capnp-stream popularity was classified as not popular.
We found that capnp-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.