Turn any binary stream into an object stream
MIT License
Turn any binary stream into an object stream, with multiple codec support
'use strict'
var net = require('net')
var nos = require('net-object-stream')
var through2 = require('through2')
var pump = require('pump')
var server = net.createServer(function (original) {
var stream = nos(original)
pump(stream, through2.obj(), stream)
})
server.listen(4200, function () {
var stream = nos(net.connect(4200))
stream.end({ hello: 'world' })
stream.on('data', console.log)
stream.on('end', server.close.bind(server))
})
netObjectStream(stream, [opts])
Wraps stream
into a Duplex object stream, recognized opts:
codec
: an object with a encode
and decode
method, which willnetObjectStream.encoder([opts])
Creates an encoder for the netObjectStream
protocol, recognized opts:
codec
: an object with a encode
and decode
method, which willnetObjectStream.decoder([opts])
Creates a decoder for the netObjectStream
protocol, recognized opts:
codec
: an object with a encode
and decode
method, which willnetObjectStream.parser([opts])
Creates a parser for the netObjectStream
protocol, recognized opts:
codec
: an object with a encode
and decode
method, which willThe parser have a parser.parse(buf)
function to add data to id.
It will emit 'message'
events when it is able to parse an object.
netObjectStream.writeToStream(msg, [opts], stream, [callback])
Write msg
to the given stream
, recognized options:
codec
: an object with a encode
and decode
method, which willThis project is kindly sponsored by nearForm.
MIT