JSPM

pipe-segment-codec

0.1.5
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q49873F
  • License MIT

pipe segment codec

Package Exports

  • pipe-segment-codec

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pipe-segment-codec) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-pipe-segment-codec

See pipe-segment.

This creates a codec segment, an encode/decode transform stream pair. The interfaces are duplex streams for connecting convenience.

Install

npm install pipe-segment-codec

Examples

var through2 = require('through2')
var codecSegment = require('pipe-segment-codec')
// codecSegment is like a double transform stream pipe.
// it returns a segment object, with two duplex streams:
// - segment.encoded
// - segment.decoded
// and two readable error streams
// - segment.encodeErrors
// - segment.decodeErrors

function encode(obj) {
  return JSON.stringify(obj)
}

function decode(str) {
  return JSON.parse(str)
}

var s = codecSegment(encode, decode)

// helper passthrough.
function logthrough(prefix) {
  return through2.obj(function(data, enc, cb) {
    console.log(prefix + ': (' + typeof(data) + ') ' + data.toString())
    cb(null, data)
  })
}

// wire segment interfaces
s.encoded.pipe(logthrough('encoded')).pipe(s.encoded)
s.decoded.pipe(logthrough('decoded'))
s.encodeErrors.pipe(logthrough('encode error'))
s.decodeErrors.pipe(logthrough('decode error'))

s.decoded.write({a: 1, b: 2, c: 3})
s.decoded.write(100)
s.decoded.write([1, 2, 3])
s.encoded.write('{"d":4}')
s.encoded.write('200')
s.encoded.write('[4, 5, 6]')
s.encoded.write('{a: 1}') // invalid json
s.encoded.write('invalid{a: 1}') // invalid json
s.encoded.write('[1, 2, 3, [') // invalid json