Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for a stream option. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var notepack = require('notepack');

var encoded = notepack.encode({ foo: 'bar'}); // <Buffer 81 a3 66 6f 6f a3 62 61 72>
var decoded = notepack.decode(encoded); // { foo: 'bar' }
var stream = notepack.createStream().on('data', ...); // decode continuous streams of buffers
```

## Performance
Expand Down
39 changes: 39 additions & 0 deletions lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ function Decoder(buffer) {
this.buffer = buffer;
}

Decoder.prototype.expect = function (length) {
var remain = this.buffer.length - this.offset;
if (remain < length) {
throw new Error('Expected ' + length + ' bytes, found ' + remain + '.');
}
}

Decoder.prototype.array = function (length) {
var value = new Array(length);
for (var i = 0; i < length; i++) {
Expand All @@ -23,12 +30,14 @@ Decoder.prototype.map = function (length) {
};

Decoder.prototype.str = function (length) {
this.expect(length);
var value = this.buffer.toString('utf8', this.offset, this.offset + length);
this.offset += length;
return value;
};

Decoder.prototype.bin = function (length) {
this.expect(length);
var value = this.buffer.slice(this.offset, this.offset + length);
this.offset += length;
return value;
Expand Down Expand Up @@ -73,85 +82,102 @@ Decoder.prototype.parse = function () {

// bin
case 0xc4:
this.expect(1);
length = this.buffer.readUInt8(this.offset);
this.offset += 1;
return this.bin(length);
case 0xc5:
this.expect(2);
length = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return this.bin(length);
case 0xc6:
this.expect(4);
length = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return this.bin(length);

// ext
case 0xc7:
this.expect(2);
length = this.buffer.readUInt8(this.offset);
type = this.buffer.readInt8(this.offset + 1);
this.offset += 2;
return [type, this.bin(length)];
case 0xc8:
this.expect(3);
length = this.buffer.readUInt16BE(this.offset);
type = this.buffer.readInt8(this.offset + 2);
this.offset += 3;
return [type, this.bin(length)];
case 0xc9:
this.expect(5);
length = this.buffer.readUInt32BE(this.offset);
type = this.buffer.readInt8(this.offset + 4);
this.offset += 5;
return [type, this.bin(length)];

// float
case 0xca:
this.expect(4);
value = this.buffer.readFloatBE(this.offset);
this.offset += 4;
return value;
case 0xcb:
this.expect(8);
value = this.buffer.readDoubleBE(this.offset);
this.offset += 8;
return value;

// uint
case 0xcc:
this.expect(1);
value = this.buffer.readUInt8(this.offset);
this.offset += 1;
return value;
case 0xcd:
this.expect(2);
value = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return value;
case 0xce:
this.expect(4);
value = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return value;
case 0xcf:
this.expect(8);
hi = this.buffer.readUInt32BE(this.offset) * Math.pow(2, 32);
lo = this.buffer.readUInt32BE(this.offset + 4);
this.offset += 8;
return hi + lo;

// int
case 0xd0:
this.expect(1);
value = this.buffer.readInt8(this.offset);
this.offset += 1;
return value;
case 0xd1:
this.expect(2);
value = this.buffer.readInt16BE(this.offset);
this.offset += 2;
return value;
case 0xd2:
this.expect(4);
value = this.buffer.readInt32BE(this.offset);
this.offset += 4;
return value;
case 0xd3:
this.expect(8);
hi = this.buffer.readInt32BE(this.offset) * Math.pow(2, 32);
lo = this.buffer.readUInt32BE(this.offset + 4);
this.offset += 8;
return hi + lo;

// fixext
case 0xd4:
this.expect(1);
type = this.buffer.readInt8(this.offset);
this.offset += 1;
if (type === 0x00) {
Expand All @@ -160,58 +186,70 @@ Decoder.prototype.parse = function () {
}
return [type, this.bin(1)];
case 0xd5:
this.expect(1);
type = this.buffer.readInt8(this.offset);
this.offset += 1;
return [type, this.bin(2)];
case 0xd6:
this.expect(1);
type = this.buffer.readInt8(this.offset);
this.offset += 1;
return [type, this.bin(4)];
case 0xd7:
this.expect(1);
type = this.buffer.readInt8(this.offset);
this.offset += 1;
if (type === 0x00) {
this.expect(8);
hi = this.buffer.readInt32BE(this.offset) * Math.pow(2, 32);
lo = this.buffer.readUInt32BE(this.offset + 4);
this.offset += 8;
return new Date(hi + lo);
}
return [type, this.bin(8)];
case 0xd8:
this.expect(1);
type = this.buffer.readInt8(this.offset);
this.offset += 1;
return [type, this.bin(16)];

// str
case 0xd9:
this.expect(1);
length = this.buffer.readUInt8(this.offset);
this.offset += 1;
return this.str(length);
case 0xda:
this.expect(2);
length = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return this.str(length);
case 0xdb:
this.expect(4);
length = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return this.str(length);

// array
case 0xdc:
this.expect(2);
length = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return this.array(length);
case 0xdd:
this.expect(4);
length = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return this.array(length);

// map
case 0xde:
this.expect(2);
length = this.buffer.readUInt16BE(this.offset);
this.offset += 2;
return this.map(length);
case 0xdf:
this.expect(4);
length = this.buffer.readUInt32BE(this.offset);
this.offset += 4;
return this.map(length);
Expand All @@ -229,4 +267,5 @@ function decode(buffer) {
return value;
}

decode.Decoder = Decoder;
module.exports = decode;
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exports.encode = require('./encode');
exports.decode = require('./decode');
exports.createStream = require('./stream');
29 changes: 29 additions & 0 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

var stream = require('stream');
var Decoder = require('./decode').Decoder;

function createStream () {
var cache = new Buffer('');

var ret = stream.Writable();
ret._write = function (chunk, encoding, next) {
cache = Buffer.concat([cache, chunk]);

try {
while (true) {
var d = new Decoder(cache);
ret.emit('data', d.parse());
cache = cache.slice(d.offset);
if (!cache.length) {
break;
}
}
} catch (e) {
}
next();
};
return ret;
}

module.exports = createStream;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notepack",
"version": "0.0.2",
"version": "0.0.3",
"description": "A fast Node.js implementation of the latest MessagePack spec",
"main": "lib/index.js",
"repository": {
Expand Down