Skip to content

Commit

Permalink
[major] Remove ErrorCodes module
Browse files Browse the repository at this point in the history
Move the `isValidErrorCode` function to the Validation module and
rename it to `isValidStatusCode`.
  • Loading branch information
lpinca committed Jan 5, 2018
1 parent 695c5ea commit 63ce954
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 43 deletions.
28 changes: 0 additions & 28 deletions lib/ErrorCodes.js

This file was deleted.

9 changes: 4 additions & 5 deletions lib/Receiver.js
Expand Up @@ -9,9 +9,8 @@
const safeBuffer = require('safe-buffer');

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

const Buffer = safeBuffer.Buffer;
Expand Down Expand Up @@ -420,7 +419,7 @@ class Receiver {
} else {
const buf = toBuffer(fragments, messageLength);

if (!isValidUTF8(buf)) {
if (!validation.isValidUTF8(buf)) {
this.error(
new Error('Invalid WebSocket frame: invalid UTF-8 sequence'),
1007
Expand Down Expand Up @@ -455,7 +454,7 @@ class Receiver {
} else {
const code = data.readUInt16BE(0, true);

if (!ErrorCodes.isValidErrorCode(code)) {
if (!validation.isValidStatusCode(code)) {
this.error(
new RangeError(
`Invalid WebSocket frame: invalid status code ${code}`
Expand All @@ -467,7 +466,7 @@ class Receiver {

const buf = data.slice(2);

if (!isValidUTF8(buf)) {
if (!validation.isValidUTF8(buf)) {
this.error(
new Error('Invalid WebSocket frame: invalid UTF-8 sequence'),
1007
Expand Down
4 changes: 2 additions & 2 deletions lib/Sender.js
Expand Up @@ -11,7 +11,7 @@ const crypto = require('crypto');

const PerMessageDeflate = require('./PerMessageDeflate');
const bufferUtil = require('./BufferUtil');
const ErrorCodes = require('./ErrorCodes');
const validation = require('./Validation');
const constants = require('./Constants');

const Buffer = safeBuffer.Buffer;
Expand Down Expand Up @@ -117,7 +117,7 @@ class Sender {

if (code === undefined) {
buf = constants.EMPTY_BUFFER;
} else if (typeof code !== 'number' || !ErrorCodes.isValidErrorCode(code)) {
} else if (typeof code !== 'number' || !validation.isValidStatusCode(code)) {
throw new TypeError('First argument must be a valid error code number');
} else if (data === undefined || data === '') {
buf = Buffer.allocUnsafe(2);
Expand Down
22 changes: 20 additions & 2 deletions lib/Validation.js
Expand Up @@ -9,9 +9,27 @@
try {
const isValidUTF8 = require('utf-8-validate');

module.exports = typeof isValidUTF8 === 'object'
exports.isValidUTF8 = typeof isValidUTF8 === 'object'
? isValidUTF8.Validation.isValidUTF8 // utf-8-validate@<3.0.0
: isValidUTF8;
} catch (e) /* istanbul ignore next */ {
module.exports = () => true;
exports.isValidUTF8 = () => true;
}

/**
* Checks if a status code is allowed in a close frame.
*
* @param {Number} code The status code
* @return {Boolean} `true` if the status code is valid, else `false`
* @public
*/
exports.isValidStatusCode = (code) => {
return (
(code >= 1000 &&
code <= 1013 &&
code !== 1004 &&
code !== 1005 &&
code !== 1006) ||
(code >= 3000 && code <= 4999)
);
};
12 changes: 6 additions & 6 deletions test/Validation.test.js
Expand Up @@ -3,7 +3,7 @@
const safeBuffer = require('safe-buffer');
const assert = require('assert');

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

const Buffer = safeBuffer.Buffer;

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

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

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

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

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

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

0 comments on commit 63ce954

Please sign in to comment.