Permalink
Browse files

Call unpackBuffer() callback only once, include remainingData

Fixes #27
  • Loading branch information...
1 parent 33e451f commit 13e1a4d201b1642d726a35610baebbf58e26e4f5 @Sannis committed Jul 5, 2012
Showing with 59 additions and 62 deletions.
  1. +15 −0 lib/ubjson-parser.js
  2. +16 −4 lib/ubjson.js
  3. +28 −58 test/test-errors.js
View
@@ -30,6 +30,17 @@ function UbjsonParser(acc) {
// Special type for string parsing
var STRINGEND = -1;
+ // We should stop stream parsing if top-level acc() get error in argument
+ var doneParsing = false;
+ var topLevelAcc = acc;
+ acc = function (value) {
+ if (value instanceof Error) {
+ doneParsing = true;
+ }
+
+ topLevelAcc(value);
+ };
+
// Return a function for unpacking an array
var unpackArray = function(nvals, oldAcc) {
var arr = [];
@@ -272,6 +283,10 @@ function UbjsonParser(acc) {
// Parse a single value, calling acc() as values are accumulated
return function(v) {
+ if (doneParsing === true) {
+ return strtok.DONE;
+ }
+
if (v === undefined) {
return strtok.UINT8;
}
View
@@ -59,11 +59,23 @@ util.inherits(BufferStream, EventEmitter);
function unpackBuffer(buffer, callback) {
var stream = new BufferStream(buffer);
- var ubjsonStrtokParser = new UbjsonParser(function (v) {
- if (v instanceof Error) {
- callback(v);
+ var ubjsonStrtokParser = new UbjsonParser(function (value) {
+ if (value instanceof Error) {
+ var remainingData = new Buffer(buffer.length);
+ var remainingDataLength = 0;
+
+ stream.on('data', function (data) {
+ data.copy(remainingData, remainingDataLength);
+ remainingDataLength += data.length;
+ });
+
+ stream.on('end', function () {
+ value.remainingData = remainingData.slice(0, remainingDataLength);
+
+ callback(value);
+ });
} else {
- callback(null, v);
+ callback(null, value);
}
});
View
@@ -35,15 +35,17 @@ exports.PackUnsupportedTypes = function (test) {
};
exports.UnpackUnsupportedTypes = function (test) {
- test.expect(2);
+ test.expect(3);
// X type is not defined in UBJSON
- var bufferWithUnsupportedType = new Buffer("X", "binary");
+ var bufferWithUnsupportedType = new Buffer("XXXXX", "binary");
UBJSON.unpackBuffer(bufferWithUnsupportedType, function (error, value) {
test.ok(error instanceof Error);
test.ok(typeof value === 'undefined');
+ test.equal(error.remainingData.toString("binary"), "XXXX");
+
test.done();
});
};
@@ -140,7 +142,7 @@ exports.UnpackMalformedUnknownLengthNestedArraysWithImpossibleElement = function
// see https://github.com/Sannis/node-ubjson/issues/21
exports.UnpackMalformedObjectWithArrayKey = function (test) {
- test.expect(5);
+ test.expect(4);
// [o][2]
// [B][1][B][2]
@@ -150,28 +152,20 @@ exports.UnpackMalformedObjectWithArrayKey = function (test) {
// [B][5]
var ubjsonBuffer = new Buffer("o\x02B\x01B\x02a\x01B\x03B\x05", "binary");
- var message = 0;
-
UBJSON.unpackBuffer(ubjsonBuffer, function (error, value) {
- message += 1;
-
- if (message === 1) {
- test.ok(error instanceof Error);
- test.ok(typeof value === 'undefined');
+ test.ok(error instanceof Error);
+ test.ok(typeof value === 'undefined');
- test.ok(error.message.match(/\{"1":2\}/));
- } else {
- test.equal(error, null);
- test.equal(value, 5);
+ test.ok(error.message.match(/\{"1":2\}/));
+ test.equal(error.remainingData.toString("binary"), "B\x05");
- test.done();
- }
+ test.done();
});
};
// see https://github.com/Sannis/node-ubjson/issues/21
exports.UnpackMalformedObjectWithObjectKey = function (test) {
- test.expect(5);
+ test.expect(4);
// [o][2]
// [B][1][B][2]
@@ -181,22 +175,14 @@ exports.UnpackMalformedObjectWithObjectKey = function (test) {
// [B][5]
var ubjsonBuffer = new Buffer("o\x02B\x01B\x02o\x01B\x03B\x06B\x05", "binary");
- var message = 0;
-
UBJSON.unpackBuffer(ubjsonBuffer, function (error, value) {
- message += 1;
-
- if (message === 1) {
- test.ok(error instanceof Error);
- test.ok(typeof value === 'undefined');
+ test.ok(error instanceof Error);
+ test.ok(typeof value === 'undefined');
- test.ok(error.message.match(/\{"1":2\}/));
- } else {
- test.equal(error, null);
- test.equal(value, 5);
+ test.ok(error.message.match(/\{"1":2\}/));
+ test.equal(error.remainingData.toString("binary"), "B\x05");
- test.done();
- }
+ test.done();
});
};
@@ -283,7 +269,7 @@ exports.UnpackMalformedUnknownLengthObjectDeeper = function (test) {
// see https://github.com/Sannis/node-ubjson/issues/21
exports.UnpackMalformedUnknownLengthObjectWithArrayKey = function (test) {
- test.expect(5);
+ test.expect(4);
// [o][255]
// [B][1][B][2]
@@ -293,28 +279,20 @@ exports.UnpackMalformedUnknownLengthObjectWithArrayKey = function (test) {
// [B][5]
var ubjsonBuffer = new Buffer("o\xFFB\x01B\x02a\x01B\x03B\x05", "binary");
- var message = 0;
-
UBJSON.unpackBuffer(ubjsonBuffer, function (error, value) {
- message += 1;
-
- if (message === 1) {
- test.ok(error instanceof Error);
- test.ok(typeof value === 'undefined');
+ test.ok(error instanceof Error);
+ test.ok(typeof value === 'undefined');
- test.ok(error.message.match(/\{"1":2\}/));
- } else {
- test.equal(error, null);
- test.equal(value, 5);
+ test.ok(error.message.match(/\{"1":2\}/));
+ test.equal(error.remainingData.toString("binary"), "B\x05");
- test.done();
- }
+ test.done();
});
};
// see https://github.com/Sannis/node-ubjson/issues/21
exports.UnpackMalformedUnknownLengthObjectWithObjectKey = function (test) {
- test.expect(5);
+ test.expect(4);
// [o][255]
// [B][1][B][2]
@@ -324,22 +302,14 @@ exports.UnpackMalformedUnknownLengthObjectWithObjectKey = function (test) {
// [B][5]
var ubjsonBuffer = new Buffer("o\xFFB\x01B\x02o\x01B\x03B\x06B\x05", "binary");
- var message = 0;
-
UBJSON.unpackBuffer(ubjsonBuffer, function (error, value) {
- message += 1;
-
- if (message === 1) {
- test.ok(error instanceof Error);
- test.ok(typeof value === 'undefined');
+ test.ok(error instanceof Error);
+ test.ok(typeof value === 'undefined');
- test.ok(error.message.match(/\{"1":2\}/));
- } else {
- test.equal(error, null);
- test.equal(value, 5);
+ test.ok(error.message.match(/\{"1":2\}/));
+ test.equal(error.remainingData.toString("binary"), "B\x05");
- test.done();
- }
+ test.done();
});
};

0 comments on commit 13e1a4d

Please sign in to comment.