You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.4 KiB
85 lines
1.4 KiB
'use strict' |
|
|
|
var Transform = require('readable-stream').Transform |
|
var inherits = require('inherits') |
|
var bl = require('bl') |
|
|
|
function Base (opts) { |
|
opts = opts || {} |
|
|
|
opts.objectMode = true |
|
opts.highWaterMark = 16 |
|
|
|
Transform.call(this, opts) |
|
|
|
this._msgpack = opts.msgpack |
|
} |
|
|
|
inherits(Base, Transform) |
|
|
|
function Encoder (opts) { |
|
if (!(this instanceof Encoder)) { |
|
opts = opts || {} |
|
opts.msgpack = this |
|
return new Encoder(opts) |
|
} |
|
|
|
Base.call(this, opts) |
|
} |
|
|
|
inherits(Encoder, Base) |
|
|
|
Encoder.prototype._transform = function (obj, enc, done) { |
|
var buf = null |
|
|
|
try { |
|
buf = this._msgpack.encode(obj).slice(0) |
|
} catch (err) { |
|
this.emit('error', err) |
|
return done() |
|
} |
|
|
|
this.push(buf) |
|
done() |
|
} |
|
|
|
function Decoder (opts) { |
|
if (!(this instanceof Decoder)) { |
|
opts = opts || {} |
|
opts.msgpack = this |
|
return new Decoder(opts) |
|
} |
|
|
|
Base.call(this, opts) |
|
|
|
this._chunks = bl() |
|
} |
|
|
|
inherits(Decoder, Base) |
|
|
|
Decoder.prototype._transform = function (buf, enc, done) { |
|
if (buf) { |
|
this._chunks.append(buf) |
|
} |
|
|
|
try { |
|
var result = this._msgpack.decode(this._chunks) |
|
this.push(result) |
|
} catch (err) { |
|
if (err instanceof this._msgpack.IncompleteBufferError) { |
|
done() |
|
} else { |
|
this.emit('error', err) |
|
} |
|
return |
|
} |
|
|
|
if (this._chunks.length > 0) { |
|
this._transform(null, enc, done) |
|
} else { |
|
done() |
|
} |
|
} |
|
|
|
module.exports.decoder = Decoder |
|
module.exports.encoder = Encoder
|
|
|