diff --git a/History.md b/History.md deleted file mode 100644 index 17e16a9..0000000 --- a/History.md +++ /dev/null @@ -1,160 +0,0 @@ - -3.1.2 / 2017-04-27 -================== - - * [chore] Bump has-binary2 to version 1.0.2 (#70) - * [fix] Fix Blob detection for iOS 8/9 (#69) - -3.1.1 / 2017-04-25 -================== - - * [fix] Ensure globals are functions before running `instanceof` (#68) - -3.1.0 / 2017-04-24 -================== - - * [chore] Bump debug to version 2.6.4 (#67) - * [feat] Move binary detection to the parser (#66) - -3.0.0 / 2017-04-04 -================== - - * [chore] Bump isarray to version 2.0.1 (#65) - * [chore] Use native JSON and drop support for older nodejs versions (#64) - -2.3.2 / 2016-12-30 -================== - - * [perf] Small optimisations (#57) - * [chore] Update zuul config to speed up tests in browser (#58) - * [refactor] Remove useless variable (#55) - * [chore] Bump dependencies (#56) - * [refactor] Remove unused var (#53) - * [refactor] Use strict equality when possible (#52) - -2.3.1 / 2016-10-24 -================== - -* [chore] Revert "Remove deprecated isarray dependency" (#50) - -2.3.0 / 2016-10-21 -================== - - * [perf] Split try catch into separate function (#40) - * [chore] remove browsers setting from .zuul.yml (#34) - * [chore] bump zuul (#37) - * [chore] Bump zuul to 3.11.0 & zuul-ngrok to 4.0.0 (#41) - * [chore] Update zuul browser settings following EOL notices (#42) - * [chore] Restrict files included in npm package (#45) - * [chore] Update zuul browser settings (#44) - * [chore] Remove deprecated isarray dependency (#46) - * [chore] Make the build status badge point towards master (#47) - * [chore] Move benchmark to dev dependencies (#48) - -2.2.6 / 2015-11-25 -================== - - * fix the order of exported events [chylli] - -2.2.5 / 2015-11-21 -================== - - * package: bump debug - * update JSON3 to 3.3.2 - -2.2.4 / 2015-03-03 -================== - - * index: fix off-by-one bound checks - -2.2.3 / 2015-02-03 -================== - - * index: fix potential infinite loop with malicious binary packet - -2.2.2 / 2014-09-04 -================== - - * prevent direct `Buffer` reference that breaks browserify - * binary: reuse `isBuf` helper - -2.2.1 / 2014-06-20 -================== - - * added benchmarking [kevin-roark] - * upgrade component-emitter to 1.1.2 [kevin-roark] - * update protocol version [kevin-roark] - * less indentation and a small optimization [kevin-roark] - -2.2.0 / 2014-05-30 -================== - - * added a BINARY_ACK type [kevin-roark] - -2.1.5 / 2014-05-24 -================== - - * don't iterate keys of `Date` objects [Rase-] - -2.1.4 / 2014-05-17 -================== - - * fix null reconstruction bug [kevin-roark] - -2.1.3 / 2014-04-27 -================== - - * bump zuul version - * updated protocol version - -2.1.2 / 2014-03-06 -================== - - * added support for binary in ACK packets - -2.1.1 / 2014-03-04 -================== - - * removed has-binary-data dependency - * fixed the object check in binary.removeBlobs - -2.1.0 / 2014-03-01 -================== - - * faster and smaller binary parser and protocol [kevin-roark] - -2.0.0 / 2014-02-19 -================== - - * binary support [kevin-roark] - -1.1.2 / 2014-02-11 -================== - - * package: bump `json3` to fix IE6-7 - -1.1.1 / 2014-02-10 -================== - - * package: bump debug to fix browserify issues - -1.1.0 / 2013-12-25 -================== - - * index: use `json3` - -1.0.3 / 2012-12-18 -================== - - * index: added instrumentation through `debug` - * index: make sure decoded `id` is a `Number` - -1.0.2 / 2012-12-18 -================== - - * index: allow for falsy values in `id` and `data` - -1.0.1 / 2012-12-10 -================== - - * Revision 1 diff --git a/index.js b/index.js index ea206ea..f71ff27 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var debug = require('debug')('socket.io-parser'); var Emitter = require('component-emitter'); var hasBin = require('has-binary2'); var binary = require('./binary'); +var isArray = require('isarray'); var isBuf = require('./is-buffer'); /** @@ -272,7 +273,9 @@ function decodeString(str) { type: Number(str.charAt(0)) }; - if (null == exports.types[p.type]) return error(); + if (null == exports.types[p.type]) { + return error('unknown packet type ' + p.type); + } // look up attachments if type binary if (exports.BINARY_EVENT === p.type || exports.BINARY_ACK === p.type) { @@ -318,20 +321,25 @@ function decodeString(str) { // look up json data if (str.charAt(++i)) { - p = tryParse(p, str.substr(i)); + var payload = tryParse(str.substr(i)); + var isPayloadValid = payload !== false && (p.type === exports.ERROR || isArray(payload)); + if (isPayloadValid) { + p.data = payload; + } else { + return error('invalid payload'); + } } debug('decoded %s as %j', str, p); return p; } -function tryParse(p, str) { +function tryParse(str) { try { - p.data = JSON.parse(str); + return JSON.parse(str); } catch(e){ - return error(); + return false; } - return p; } /** @@ -392,9 +400,9 @@ BinaryReconstructor.prototype.finishedReconstruction = function() { this.buffers = []; }; -function error() { +function error(msg) { return { type: exports.ERROR, - data: 'parser error' + data: 'parser error: ' + msg }; } diff --git a/is-buffer.js b/is-buffer.js index 977df88..520ed75 100644 --- a/is-buffer.js +++ b/is-buffer.js @@ -9,5 +9,5 @@ module.exports = isBuf; function isBuf(obj) { return (global.Buffer && global.Buffer.isBuffer(obj)) || - (global.ArrayBuffer && obj instanceof ArrayBuffer); + (global.ArrayBuffer && (obj instanceof ArrayBuffer || ArrayBuffer.isView(obj))); } diff --git a/package.json b/package.json index 2d1284f..bec32d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "socket.io-parser", - "version": "3.1.2", + "version": "3.1.3", "description": "socket.io protocol parser", "repository": { "type": "git", @@ -12,7 +12,7 @@ "is-buffer.js" ], "dependencies": { - "debug": "~2.6.4", + "debug": "~3.1.0", "component-emitter": "1.2.1", "has-binary2": "~1.0.2", "isarray": "2.0.1" diff --git a/test/arraybuffer.js b/test/arraybuffer.js index 0ba4c04..a15c1e7 100644 --- a/test/arraybuffer.js +++ b/test/arraybuffer.js @@ -7,7 +7,7 @@ describe('parser', function() { it('encodes an ArrayBuffer', function() { var packet = { type: parser.BINARY_EVENT, - data: new ArrayBuffer(2), + data: ['a', new ArrayBuffer(2)], id: 0, nsp: '/' }; @@ -17,7 +17,7 @@ describe('parser', function() { it('encodes ArrayBuffers deep in JSON', function() { var packet = { type: parser.BINARY_EVENT, - data: {a: 'hi', b: {why: new ArrayBuffer(3)}, c: {a: 'bye', b: { a: new ArrayBuffer(6)}}}, + data: ['a', {a: 'hi', b: {why: new ArrayBuffer(3)}, c: {a: 'bye', b: { a: new ArrayBuffer(6)}}}], id: 999, nsp: '/deep' }; @@ -27,7 +27,7 @@ describe('parser', function() { it('encodes deep binary JSON with null values', function() { var packet = { type: parser.BINARY_EVENT, - data: {a: 'b', c: 4, e: {g: null}, h: new ArrayBuffer(9)}, + data: ['a', {a: 'b', c: 4, e: {g: null}, h: new ArrayBuffer(9)}], nsp: '/', id: 600 }; diff --git a/test/buffer.js b/test/buffer.js index dd27460..3aba898 100644 --- a/test/buffer.js +++ b/test/buffer.js @@ -8,7 +8,7 @@ describe('parser', function() { it('encodes a Buffer', function() { helpers.test_bin({ type: parser.BINARY_EVENT, - data: new Buffer('abc', 'utf8'), + data: ['a', new Buffer('abc', 'utf8')], id: 23, nsp: '/cool' }); diff --git a/test/parser.js b/test/parser.js index 346ca3b..42179d2 100644 --- a/test/parser.js +++ b/test/parser.js @@ -12,6 +12,8 @@ describe('parser', function(){ expect(parser.EVENT).to.be.a('number'); expect(parser.ACK).to.be.a('number'); expect(parser.ERROR).to.be.a('number'); + expect(parser.BINARY_EVENT).to.be.a('number'); + expect(parser.BINARY_ACK).to.be.a('number'); }); it('encodes connection', function(){ @@ -51,6 +53,14 @@ describe('parser', function(){ }); }); + it('encodes an error', function(){ + helpers.test({ + type: parser.ERROR, + data: 'Unauthorized', + nsp: '/' + }); + }); + it('decodes a bad binary packet', function(){ try { var decoder = new parser.Decoder(); @@ -59,4 +69,13 @@ describe('parser', function(){ expect(e.message).to.match(/Illegal/); } }); + + it('returns an error packet on parsing error', function(done){ + var decoder = new parser.Decoder(); + decoder.on('decoded', function(packet) { + expect(packet).to.eql({ type: 4, data: 'parser error: invalid payload' }); + done(); + }); + decoder.add('442["some","data"'); + }); });