Wrap a duplex stream with JSONStream parse and stringify.
const json = require('wrap-json-stream')
, through2 = require('through2')
const wrapped = json(through2.obj(function(obj, _, next){
obj.y = obj.x*2
next(null, obj)
}))
wrapped.on('data', (data) => console.log(data.toString()))
wrapped.write('{"x":1}\n{"x":2}')
{"x":1,"y":2}
{"x":2,"y":4}
Options:
- fwd: array of events to forward from the inner stream to the outer
- inherit: array of properties to inherit
const inner = someStream()
inner.name = 'my-stream'
const outer = json(inner, { inherit: ['name'], fwd: ['sync'] })
outer.on('sync', () => console.log(outer.name))
inner.emit('sync')
With npm do:
npm install wrap-json-stream
MIT © Vincent Weevers