Skip to content

Commit

Permalink
[fix] Add compatibility with bufferutil@>1 and utf-8-validate@>2
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Mar 11, 2017
1 parent 98f0d21 commit b4cf110
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ run-coverage:
$(TESTS)

test:
@$(MAKE) NODE_TLS_REJECT_UNAUTHORIZED=0 NODE_PATH=lib TESTS="$(ALL_TESTS)" run-tests
@$(MAKE) NODE_TLS_REJECT_UNAUTHORIZED=0 TESTS="$(ALL_TESTS)" run-tests

integrationtest:
@$(MAKE) NODE_TLS_REJECT_UNAUTHORIZED=0 NODE_PATH=lib TESTS="$(ALL_INTEGRATION)" run-integrationtests
@$(MAKE) NODE_TLS_REJECT_UNAUTHORIZED=0 TESTS="$(ALL_INTEGRATION)" run-integrationtests

coverage:
@$(MAKE) NODE_TLS_REJECT_UNAUTHORIZED=0 NODE_PATH=lib TESTS="$(ALL_TESTS)" run-coverage
@$(MAKE) NODE_TLS_REJECT_UNAUTHORIZED=0 TESTS="$(ALL_TESTS)" run-coverage

benchmark:
@node bench/sender.benchmark.js
@node bench/parser.benchmark.js

autobahn:
@NODE_PATH=lib node test/autobahn.js
@node test/autobahn.js

autobahn-server:
@NODE_PATH=lib node test/autobahn-server.js
@node test/autobahn-server.js

.PHONY: test coverage
8 changes: 6 additions & 2 deletions lib/BufferUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
* MIT Licensed
*/

var bufferUtil;

try {
module.exports = require('bufferutil');
bufferUtil = require('bufferutil');
} catch (e) {
module.exports = require('./BufferUtil.fallback');
bufferUtil = require('./BufferUtil.fallback');
}

module.exports = bufferUtil.BufferUtil || bufferUtil;
8 changes: 4 additions & 4 deletions lib/Receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/

var util = require('util')
, Validation = require('./Validation').Validation
, isValidUTF8 = require('./Validation')
, ErrorCodes = require('./ErrorCodes')
, BufferPool = require('./BufferPool')
, bufferUtil = require('./BufferUtil').BufferUtil
, bufferUtil = require('./BufferUtil')
, PerMessageDeflate = require('./PerMessageDeflate');

/**
Expand Down Expand Up @@ -529,7 +529,7 @@ var opcodes = {
var messageBuffer = Buffer.concat(self.currentMessage);
self.currentMessage = [];
self.currentMessageLength = 0;
if (!Validation.isValidUTF8(messageBuffer)) {
if (!isValidUTF8(messageBuffer)) {
self.error('invalid utf8 sequence', 1007);
return;
}
Expand Down Expand Up @@ -686,7 +686,7 @@ var opcodes = {
var message = '';
if (data && data.length > 2) {
var messageBuffer = data.slice(2);
if (!Validation.isValidUTF8(messageBuffer)) {
if (!isValidUTF8(messageBuffer)) {
self.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 @@ var events = require('events')
, crypto = require('crypto')
, EventEmitter = events.EventEmitter
, ErrorCodes = require('./ErrorCodes')
, bufferUtil = require('./BufferUtil').BufferUtil
, bufferUtil = require('./BufferUtil')
, PerMessageDeflate = require('./PerMessageDeflate');

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/Validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
* MIT Licensed
*/

var isValidUTF8;

try {
module.exports = require('utf-8-validate');
isValidUTF8 = require('utf-8-validate');
} catch (e) {
module.exports = require('./Validation.fallback');
isValidUTF8 = require('./Validation.fallback');
}

module.exports = typeof isValidUTF8 === 'object'
? isValidUTF8.Validation.isValidUTF8
: isValidUTF8;
13 changes: 6 additions & 7 deletions test/Validation.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
var Validation = require('../lib/Validation').Validation;
var isValidUTF8 = require('../lib/Validation');
require('should');

describe('Validation', function() {
describe('isValidUTF8', function() {
it('should return true for a valid utf8 string', function() {
var validBuffer = new Buffer('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque gravida mattis rhoncus. Donec iaculis, metus quis varius accumsan, erat mauris condimentum diam, et egestas erat enim ut ligula. Praesent sollicitudin tellus eget dolor euismod euismod. Nullam ac augue nec neque varius luctus. Curabitur elit mi, consequat ultricies adipiscing mollis, scelerisque in erat. Phasellus facilisis fermentum ullamcorper. Nulla et sem eu arcu pharetra pellentesque. Praesent consectetur tempor justo, vel iaculis dui ullamcorper sit amet. Integer tristique viverra ullamcorper. Vivamus laoreet, nulla eget suscipit eleifend, lacus lectus feugiat libero, non fermentum erat nisi at risus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut pulvinar dignissim tellus, eu dignissim lorem vulputate quis. Morbi ut pulvinar augue.');
Validation.isValidUTF8(validBuffer).should.be.ok;
isValidUTF8(validBuffer).should.be.ok;
});
it('should return false for an erroneous string', function() {
var invalidBuffer = new Buffer([0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0xed, 0xa0, 0x80, 0x65, 0x64, 0x69, 0x74, 0x65, 0x64]);
Validation.isValidUTF8(invalidBuffer).should.not.be.ok;
isValidUTF8(invalidBuffer).should.not.be.ok;
});
it('should return true for valid cases from the autobahn test suite', function() {
Validation.isValidUTF8(new Buffer('\xf0\x90\x80\x80')).should.be.ok;
Validation.isValidUTF8(new Buffer([0xf0, 0x90, 0x80, 0x80])).should.be.ok;
isValidUTF8(new Buffer('\xf0\x90\x80\x80')).should.be.ok;
isValidUTF8(new Buffer([0xf0, 0x90, 0x80, 0x80])).should.be.ok;
});
it('should return false for erroneous autobahn strings', function() {
Validation.isValidUTF8(new Buffer([0xce, 0xba, 0xe1, 0xbd])).should.not.be.ok;
isValidUTF8(new Buffer([0xce, 0xba, 0xe1, 0xbd])).should.not.be.ok;
});
});
});

0 comments on commit b4cf110

Please sign in to comment.