msgpack5
Advanced tools
Comparing version 1.6.0 to 2.0.0
@@ -14,3 +14,2 @@ | ||
this._header = opts.header === undefined || opts.header | ||
this._msgpack = opts.msgpack | ||
@@ -35,3 +34,2 @@ } | ||
var buf = null | ||
, header = null | ||
@@ -45,8 +43,2 @@ try { | ||
if (this._header) { | ||
header = new Buffer(4) | ||
header.writeUInt32BE(buf.length, 0) | ||
this.push(header) | ||
} | ||
this.push(buf) | ||
@@ -66,8 +58,2 @@ done() | ||
this._chunks = bl() | ||
if (this._header) { | ||
this._length = -1 | ||
this._transform = decodeWithHeader | ||
} else { | ||
this._transform = decodeWithoutHeader | ||
} | ||
} | ||
@@ -77,3 +63,3 @@ | ||
function decodeWithoutHeader(buf, enc, done) { | ||
Decoder.prototype._transform = function (buf, enc, done) { | ||
if (buf) { | ||
@@ -102,42 +88,3 @@ this._chunks.append(buf) | ||
function decodeWithHeader(buf, enc, done) { | ||
// needed for recursive invocation | ||
if (buf) | ||
this._chunks.append(buf) | ||
// we need to parse the header | ||
if (this._length == -1) { | ||
// no header yet | ||
if (this._chunks.length < 4) | ||
return done() | ||
this._length = this._chunks.readUInt32BE(0) | ||
this._chunks.consume(4) | ||
} | ||
// parse the packet | ||
if (this._chunks.length >= this._length) { | ||
try { | ||
this.push(this._msgpack.decode(this._chunks.slice(0, this._length))) | ||
this._chunks.consume(this._length) | ||
this._length = -1 | ||
} 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 |
{ | ||
"name": "msgpack5", | ||
"version": "1.6.0", | ||
"version": "2.0.0", | ||
"description": "A msgpack v5 implementation for node.js and the browser, with extension points", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -166,15 +166,11 @@ msgpack5 [![Build Status](https://travis-ci.org/mcollina/msgpack5.png)](https://travis-ci.org/mcollina/msgpack5) | ||
<a name="encoder"></a> | ||
### encoder(opts) | ||
### encoder() | ||
Builds a stream in object mode that encodes msgpack. By default it writes | ||
an 4 byte length header containing the message length as a UInt32BE. This | ||
header can be disabled by passing `{ header: false }` as an option. | ||
Builds a stream in object mode that encodes msgpack. | ||
------------------------------------------------------- | ||
<a name="decoder"></a> | ||
### decoder(opts) | ||
### decoder() | ||
Builds a stream in object mode that decodes msgpack. By default it expects | ||
msgpack to have a 4 byte length header containing the packaged length as | ||
a UInt32BE. This header can be disabled by passing `{ header: false }` as an option. | ||
Builds a stream in object mode that decodes msgpack. | ||
@@ -181,0 +177,0 @@ LevelUp Support |
@@ -46,39 +46,6 @@ | ||
test('must send an object through with an header by default', function(t) { | ||
t.plan(2) | ||
test('end-to-end', function(t) { | ||
var pack = msgpack() | ||
, encoder = pack.encoder() | ||
, data = { hello: 'world' } | ||
, encoded = pack.encode(data) | ||
encoder.once('data', function(header) { | ||
t.equal(header.readUInt32BE(0), encoded.length) | ||
encoder.once('data', function(chunk) { | ||
t.equal(chunk.toString('hex'), encoded.toString('hex')) | ||
}) | ||
}) | ||
encoder.end(data) | ||
}) | ||
test('disable header support in encoder', function(t) { | ||
t.plan(1) | ||
var pack = msgpack() | ||
, encoder = pack.encoder({ header: false }) | ||
, data = { hello: 'world' } | ||
, encoded = pack.encode(data) | ||
encoder.once('data', function(chunk) { | ||
t.equal(chunk.toString('hex'), encoded.toString('hex')) | ||
}) | ||
encoder.end(data) | ||
}) | ||
test('end-to-end without headers', function(t) { | ||
var pack = msgpack() | ||
, encoder = pack.encoder({ header: false }) | ||
, decoder = pack.decoder({ header: false }) | ||
, decoder = pack.decoder() | ||
, data = [ | ||
@@ -85,0 +52,0 @@ { hello: 1 } |
407807
8115
218