Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: socketio/socket.io-parser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3.1.3
Choose a base ref
...
head repository: socketio/socket.io-parser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3.2.0
Choose a head ref
  • 5 commits
  • 6 files changed
  • 1 contributor

Commits on Feb 28, 2018

  1. [fix] Properly detect typed arrays (#85)

    ArrayBuffer.isView method is not defined in IE10.
    darrachequesne authored Feb 28, 2018

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    6b356eb View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f115039 View commit details
  3. [revert] Move binary detection to the parser

    So that we can skip the binary check.
    darrachequesne committed Feb 28, 2018

    Verified

    This commit was signed with the committer’s verified signature.
    earl-warren Earl Warren
    Copy the full SHA
    dc4f475 View commit details
  4. [fix] Properly handle JSON.stringify errors (#84)

    JSON.stringify method throws when passed a circular object.
    darrachequesne committed Feb 28, 2018
    Copy the full SHA
    92c530d View commit details
  5. [chore] Release 3.2.0

    darrachequesne committed Feb 28, 2018
    Copy the full SHA
    6e40018 View commit details
Showing with 79 additions and 18 deletions.
  1. +17 −4 .travis.yml
  2. +17 −8 index.js
  3. +13 −2 is-buffer.js
  4. +1 −2 package.json
  5. +13 −0 test/arraybuffer.js
  6. +18 −2 test/parser.js
21 changes: 17 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -2,12 +2,25 @@ language: node_js
sudo: false
node_js:
- '4'
- 'node'
- '6'
- '8'
- node
notifications:
irc: irc.freenode.org##socket.io
irc: 'irc.freenode.org##socket.io'
git:
depth: 1
matrix:
include:
- node_js: 'node'
env: BROWSERS=1
- node_js: node
env: BROWSERS=1
cache:
directories:
- node_modules
env:
global:
- secure: >-
Ea4P/R9UlWzDlHSP5ynmLiD/YgLjecIvCviOcRTle9mV3P1j2k94Ay1LVu1Jw4whlNmWLq2Z/p8M63L92ODPMlarPsuME8HlP4zGr41whFhRbFdda4k3zrHfUhZBlnhY1MVWXTtVm/l7DOzpBrNh+wKecxZB3yyyEaA+PSG3qcQ=
- secure: >-
JmPf38qx5Rb6K+WYOMwb5YmESkDmVJ6tgggiJIuyRfHsgQVOO7XBwZuspIKGTSFolUIMaqwQe79Kd+Ehs2ZZ/0lUyF2/6xW3FqFnASUusYJcZdfRjypmBFWs6BRdtEORM8HL0dgBx4O4u/e4ZvtygumbPahjQbMDaqN+MvlpjD0=
- secure: >-
c3pnLhy3VDJqMl16ABA+8vt3I623aNa2wkLceLXb2V1Dc6eiZeulDH2ekwmdVo/r2WwGIKP3Y6B0mq/xP4W0hg4uT+xWh0AmFHclVyM/yp/AqfXrDUv17Vm0vB7OIgp332OiAlK6Dr13YDbWW8iZxmID41O2+2qohLGPn5JMncg=
25 changes: 17 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@

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');
@@ -114,6 +113,8 @@ exports.Decoder = Decoder;

function Encoder() {}

var ERROR_PACKET = exports.ERROR + '"encode error"';

/**
* Encode a packet as a single string if non-binary, or as a
* buffer sequence, depending on packet type.
@@ -125,16 +126,11 @@ function Encoder() {}
*/

Encoder.prototype.encode = function(obj, callback){
if ((obj.type === exports.EVENT || obj.type === exports.ACK) && hasBin(obj.data)) {
obj.type = obj.type === exports.EVENT ? exports.BINARY_EVENT : exports.BINARY_ACK;
}

debug('encoding packet %j', obj);

if (exports.BINARY_EVENT === obj.type || exports.BINARY_ACK === obj.type) {
encodeAsBinary(obj, callback);
}
else {
} else {
var encoding = encodeAsString(obj);
callback([encoding]);
}
@@ -171,13 +167,26 @@ function encodeAsString(obj) {

// json data
if (null != obj.data) {
str += JSON.stringify(obj.data);
var payload = tryStringify(obj.data);
if (payload !== false) {
str += payload;
} else {
return ERROR_PACKET;
}
}

debug('encoded %j as %s', obj, str);
return str;
}

function tryStringify(str) {
try {
return JSON.stringify(str);
} catch(e){
return false;
}
}

/**
* Encode packet as 'buffer sequence' by removing blobs, and
* deconstructing packet into object with placeholders and
15 changes: 13 additions & 2 deletions is-buffer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@

module.exports = isBuf;

var withNativeBuffer = typeof global.Buffer === 'function' && typeof global.Buffer.isBuffer === 'function';
var withNativeArrayBuffer = typeof global.ArrayBuffer === 'function';

var isView = (function () {
if (withNativeArrayBuffer && typeof global.ArrayBuffer.isView === 'function') {
return global.ArrayBuffer.isView;
} else {
return function (obj) { return obj.buffer instanceof global.ArrayBuffer; };
}
})();

/**
* Returns true if obj is a buffer or an arraybuffer.
*
* @api private
*/

function isBuf(obj) {
return (global.Buffer && global.Buffer.isBuffer(obj)) ||
(global.ArrayBuffer && (obj instanceof ArrayBuffer || ArrayBuffer.isView(obj)));
return (withNativeBuffer && global.Buffer.isBuffer(obj)) ||
(withNativeArrayBuffer && (obj instanceof global.ArrayBuffer || isView(obj)));
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "socket.io-parser",
"version": "3.1.3",
"version": "3.2.0",
"description": "socket.io protocol parser",
"repository": {
"type": "git",
@@ -14,7 +14,6 @@
"dependencies": {
"debug": "~3.1.0",
"component-emitter": "1.2.1",
"has-binary2": "~1.0.2",
"isarray": "2.0.1"
},
"devDependencies": {
13 changes: 13 additions & 0 deletions test/arraybuffer.js
Original file line number Diff line number Diff line change
@@ -14,6 +14,19 @@ describe('parser', function() {
helpers.test_bin(packet);
});

it('encodes a TypedArray', function() {
var array = new Uint8Array(5);
for (var i = 0; i < array.length; i++) array[i] = i;

var packet = {
type: parser.BINARY_EVENT,
data: ['a', array],
id: 0,
nsp: '/'
};
helpers.test_bin(packet);
});

it('encodes ArrayBuffers deep in JSON', function() {
var packet = {
type: parser.BINARY_EVENT,
20 changes: 18 additions & 2 deletions test/parser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var parser = require('../index.js');
var expect = require('expect.js');
var helpers = require('./helpers.js');
var encode = parser.encode;
var decode = parser.decode;

describe('parser', function(){

@@ -61,6 +59,24 @@ describe('parser', function(){
});
});

it('properly handles circular objects', function() {
var a = {};
a.b = a;

var data = {
type: parser.EVENT,
data: a,
id: 1,
nsp: '/'
}

var encoder = new parser.Encoder();

encoder.encode(data, function(encodedPackets) {
expect(encodedPackets[0]).to.be('4"encode error"');
});
});

it('decodes a bad binary packet', function(){
try {
var decoder = new parser.Decoder();