Skip to content

Commit

Permalink
[minor] Add support for bufferutil@2 and utf-8-validate@3
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Feb 3, 2017
1 parent a8d21d4 commit 466e210
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 40 deletions.
66 changes: 46 additions & 20 deletions lib/BufferUtil.fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,51 @@

'use strict';

exports.BufferUtil = {
merge: function (mergedBuffer, buffers) {
var offset = 0;
for (var i = 0, l = buffers.length; i < l; ++i) {
var buf = buffers[i];
buf.copy(mergedBuffer, offset);
offset += buf.length;
}
},
mask: function (source, mask, output, offset, length) {
for (var i = 0; i < length; i++) {
output[offset + i] = source[i] ^ mask[i & 3];
}
},
unmask: function (data, mask) {
// required until https://github.com/nodejs/node/issues/9006 is resolved
var length = data.length;
for (var i = 0; i < length; i++) {
data[i] ^= mask[i & 3];
}
/**
* Merges an array of buffers into a target buffer.
*
* @param {Buffer} target The target buffer
* @param {Buffer[]} buffers The array of buffers to merge
* @public
*/
const merge = (target, buffers) => {
var offset = 0;
for (var i = 0; i < buffers.length; i++) {
const buf = buffers[i];
buf.copy(target, offset);
offset += buf.length;
}
};

/**
* Masks a buffer using the given mask.
*
* @param {Buffer} source The buffer to mask
* @param {Buffer} mask The mask to use
* @param {Buffer} output The buffer where to store the result
* @param {Number} offset The offset at which to start writing
* @param {Number} length The number of bytes to mask.
* @public
*/
const mask = (source, mask, output, offset, length) => {
for (var i = 0; i < length; i++) {
output[offset + i] = source[i] ^ mask[i & 3];
}
};

/**
* Unmasks a buffer using the given mask.
*
* @param {Buffer} buffer The buffer to unmask
* @param {Buffer} mask The mask to use
* @public
*/
const unmask = (buffer, mask) => {
// Required until https://github.com/nodejs/node/issues/9006 is resolved.
const length = buffer.length;
for (var i = 0; i < length; i++) {
buffer[i] ^= mask[i & 3];
}
};

module.exports = { merge, mask, unmask };
4 changes: 3 additions & 1 deletion lib/BufferUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

try {
module.exports = require('bufferutil');
const bufferUtil = require('bufferutil');

module.exports = bufferUtil.BufferUtil || bufferUtil;
} catch (e) {
module.exports = require('./BufferUtil.fallback');
}
8 changes: 4 additions & 4 deletions lib/Receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
'use strict';

const PerMessageDeflate = require('./PerMessageDeflate');
const bufferUtil = require('./BufferUtil').BufferUtil;
const Validation = require('./Validation').Validation;
const isValidUTF8 = require('./Validation');
const bufferUtil = require('./BufferUtil');
const ErrorCodes = require('./ErrorCodes');

const EMPTY_BUFFER = Buffer.alloc(0);
Expand Down Expand Up @@ -360,7 +360,7 @@ class Receiver {
if (this.opcode === 2) {
this.onmessage(buf, { masked: this.masked, binary: true });
} else {
if (!Validation.isValidUTF8(buf)) {
if (!isValidUTF8(buf)) {
this.error(new Error('invalid utf8 sequence'), 1007);
return;
}
Expand Down Expand Up @@ -396,7 +396,7 @@ class Receiver {

const buf = data.slice(2);

if (!Validation.isValidUTF8(buf)) {
if (!isValidUTF8(buf)) {
this.error(new Error('invalid utf8 sequence'), 1007);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const crypto = require('crypto');

const PerMessageDeflate = require('./PerMessageDeflate');
const bufferUtil = require('./BufferUtil').BufferUtil;
const bufferUtil = require('./BufferUtil');
const ErrorCodes = require('./ErrorCodes');

/**
Expand Down
6 changes: 1 addition & 5 deletions lib/Validation.fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@

'use strict';

exports.Validation = {
isValidUTF8: function (buffer) {
return true;
}
};
module.exports = () => true;
6 changes: 5 additions & 1 deletion lib/Validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
'use strict';

try {
module.exports = require('utf-8-validate');
const isValidUTF8 = require('utf-8-validate');

module.exports = typeof isValidUTF8 === 'object'
? isValidUTF8.Validation.isValidUTF8 // utf-8-validate@<3.0.0
: isValidUTF8;
} catch (e) {
module.exports = require('./Validation.fallback');
}
14 changes: 6 additions & 8 deletions test/Validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

const assert = require('assert');

const validation = require('../lib/Validation');

const Validation = validation.Validation;
const isValidUTF8 = require('../lib/Validation');

describe('Validation', function () {
describe('isValidUTF8', function () {
Expand All @@ -27,7 +25,7 @@ describe('Validation', function () {
'vulputate quis. Morbi ut pulvinar augue.'
);

assert.ok(Validation.isValidUTF8(validBuffer));
assert.ok(isValidUTF8(validBuffer));
});

it('should return false for an erroneous string', function () {
Expand All @@ -37,16 +35,16 @@ describe('Validation', function () {
0x65, 0x64, 0x69, 0x74, 0x65, 0x64
]);

assert.ok(!Validation.isValidUTF8(invalidBuffer));
assert.ok(!isValidUTF8(invalidBuffer));
});

it('should return true for valid cases from the autobahn test suite', function () {
assert.ok(Validation.isValidUTF8(Buffer.from([0xf0, 0x90, 0x80, 0x80])));
assert.ok(Validation.isValidUTF8(Buffer.from('\xf0\x90\x80\x80')));
assert.ok(isValidUTF8(Buffer.from([0xf0, 0x90, 0x80, 0x80])));
assert.ok(isValidUTF8(Buffer.from('\xf0\x90\x80\x80')));
});

it('should return false for erroneous autobahn strings', function () {
assert.ok(!Validation.isValidUTF8(Buffer.from([0xce, 0xba, 0xe1, 0xbd])));
assert.ok(!isValidUTF8(Buffer.from([0xce, 0xba, 0xe1, 0xbd])));
});
});
});

0 comments on commit 466e210

Please sign in to comment.