Skip to content
Closed
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
8 changes: 4 additions & 4 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Module dependencies.
*/

var keys = require('./keys');
var utils = require('./utils');
var hasBinary = require('has-binary');
var sliceBuffer = require('arraybuffer.slice');
var after = require('after');
var utf8 = require('wtf-8');
var utf8 = require('utf8');

var base64encoder;
if (global.ArrayBuffer) {
Expand Down Expand Up @@ -56,7 +56,7 @@ var packets = exports.packets = {
, noop: 6
};

var packetslist = keys(packets);
var packetslist = utils.keys(packets);

/**
* Premade error packet.
Expand Down Expand Up @@ -117,7 +117,7 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {

// data fragment is optional
if (undefined !== packet.data) {
encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
encoded += utf8encode ? utf8.encode(utils.sanitizeString(String(packet.data))) : String(packet.data);
}

return callback('' + encoded);
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Module dependencies.
*/

var utf8 = require('wtf-8');
var utf8 = require('utf8');
var after = require('after');
var keys = require('./keys');
var utils = require('./utils');

/**
* Current protocol version.
Expand All @@ -25,7 +25,7 @@ var packets = exports.packets = {
, noop: 6
};

var packetslist = keys(packets);
var packetslist = utils.keys(packets);

/**
* Premade error packet.
Expand Down Expand Up @@ -72,7 +72,7 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {

// data fragment is optional
if (undefined !== packet.data) {
encoded += utf8encode ? utf8.encode(String(packet.data)) : String(packet.data);
encoded += utf8encode ? utf8.encode(utils.sanitizeString(String(packet.data))) : String(packet.data);
}

return callback('' + encoded);
Expand Down
19 changes: 0 additions & 19 deletions lib/keys.js

This file was deleted.

38 changes: 38 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

/**
* Gets the keys for an object.
*
* @return {Array} keys
* @api private
*/

var keys = Object.keys || function keys (obj) {
var arr = [];
var has = Object.prototype.hasOwnProperty;

for (var i in obj) {
if (has.call(obj, i)) {
arr.push(i);
}
}
return arr;
};

// from https://gist.github.com/mathiasbynens/bbe7f870208abcfec860
var loneSurrogatesRegex = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g;

/**
* Sanitize a WTF-8 string, replacing lone surrogates with
* U+FFFD 'REPLACEMENT CHARACTER'
*
* @return {String} str
* @api private
*/
var sanitizeString = function (str) {
return str.replace(loneSurrogatesRegex, '\uFFFD');
};

module.exports = {
keys: keys,
sanitizeString: sanitizeString
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"base64-arraybuffer": "0.1.5",
"blob": "0.0.4",
"has-binary": "0.1.6",
"wtf-8": "1.0.0"
"utf8": "2.1.0"
},
"scripts": {
"test": "make test"
Expand Down
9 changes: 9 additions & 0 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ module.exports = function(parser) {
expect(data).to.match(/^[0-9]$/);
});
});

it('should encode a string message with lone surrogates replaced', function(done) {
var data = '\uDC00 a\uDC00 \uDBFF\uDC00 \uDBFFb \uDBFF';
encode({ type: 'message', data: data }, null, true, function(encoded) {
expect(decode(encoded, null, true)).to.eql({ type: 'message', data: '\uFFFD \uFFFD \uDBFF\uDC00 \uFFFDb \uFFFD' });
done();
});
});

});

describe('decoding error handing', function () {
Expand Down