Skip to content

Commit b4cf110

Browse files
committed
[fix] Add compatibility with bufferutil@>1 and utf-8-validate@>2
1 parent 98f0d21 commit b4cf110

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ run-coverage:
2424
$(TESTS)
2525

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

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

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

3535
benchmark:
3636
@node bench/sender.benchmark.js
3737
@node bench/parser.benchmark.js
3838

3939
autobahn:
40-
@NODE_PATH=lib node test/autobahn.js
40+
@node test/autobahn.js
4141

4242
autobahn-server:
43-
@NODE_PATH=lib node test/autobahn-server.js
43+
@node test/autobahn-server.js
4444

4545
.PHONY: test coverage

lib/BufferUtil.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
* MIT Licensed
77
*/
88

9+
var bufferUtil;
10+
911
try {
10-
module.exports = require('bufferutil');
12+
bufferUtil = require('bufferutil');
1113
} catch (e) {
12-
module.exports = require('./BufferUtil.fallback');
14+
bufferUtil = require('./BufferUtil.fallback');
1315
}
16+
17+
module.exports = bufferUtil.BufferUtil || bufferUtil;

lib/Receiver.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66

77
var util = require('util')
8-
, Validation = require('./Validation').Validation
8+
, isValidUTF8 = require('./Validation')
99
, ErrorCodes = require('./ErrorCodes')
1010
, BufferPool = require('./BufferPool')
11-
, bufferUtil = require('./BufferUtil').BufferUtil
11+
, bufferUtil = require('./BufferUtil')
1212
, PerMessageDeflate = require('./PerMessageDeflate');
1313

1414
/**
@@ -529,7 +529,7 @@ var opcodes = {
529529
var messageBuffer = Buffer.concat(self.currentMessage);
530530
self.currentMessage = [];
531531
self.currentMessageLength = 0;
532-
if (!Validation.isValidUTF8(messageBuffer)) {
532+
if (!isValidUTF8(messageBuffer)) {
533533
self.error('invalid utf8 sequence', 1007);
534534
return;
535535
}
@@ -686,7 +686,7 @@ var opcodes = {
686686
var message = '';
687687
if (data && data.length > 2) {
688688
var messageBuffer = data.slice(2);
689-
if (!Validation.isValidUTF8(messageBuffer)) {
689+
if (!isValidUTF8(messageBuffer)) {
690690
self.error('invalid utf8 sequence', 1007);
691691
return;
692692
}

lib/Sender.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var events = require('events')
99
, crypto = require('crypto')
1010
, EventEmitter = events.EventEmitter
1111
, ErrorCodes = require('./ErrorCodes')
12-
, bufferUtil = require('./BufferUtil').BufferUtil
12+
, bufferUtil = require('./BufferUtil')
1313
, PerMessageDeflate = require('./PerMessageDeflate');
1414

1515
/**

lib/Validation.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
* MIT Licensed
77
*/
88

9+
var isValidUTF8;
10+
911
try {
10-
module.exports = require('utf-8-validate');
12+
isValidUTF8 = require('utf-8-validate');
1113
} catch (e) {
12-
module.exports = require('./Validation.fallback');
14+
isValidUTF8 = require('./Validation.fallback');
1315
}
16+
17+
module.exports = typeof isValidUTF8 === 'object'
18+
? isValidUTF8.Validation.isValidUTF8
19+
: isValidUTF8;

test/Validation.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
var Validation = require('../lib/Validation').Validation;
1+
var isValidUTF8 = require('../lib/Validation');
22
require('should');
33

44
describe('Validation', function() {
55
describe('isValidUTF8', function() {
66
it('should return true for a valid utf8 string', function() {
77
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.');
8-
Validation.isValidUTF8(validBuffer).should.be.ok;
8+
isValidUTF8(validBuffer).should.be.ok;
99
});
1010
it('should return false for an erroneous string', function() {
1111
var invalidBuffer = new Buffer([0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0xed, 0xa0, 0x80, 0x65, 0x64, 0x69, 0x74, 0x65, 0x64]);
12-
Validation.isValidUTF8(invalidBuffer).should.not.be.ok;
12+
isValidUTF8(invalidBuffer).should.not.be.ok;
1313
});
1414
it('should return true for valid cases from the autobahn test suite', function() {
15-
Validation.isValidUTF8(new Buffer('\xf0\x90\x80\x80')).should.be.ok;
16-
Validation.isValidUTF8(new Buffer([0xf0, 0x90, 0x80, 0x80])).should.be.ok;
15+
isValidUTF8(new Buffer('\xf0\x90\x80\x80')).should.be.ok;
16+
isValidUTF8(new Buffer([0xf0, 0x90, 0x80, 0x80])).should.be.ok;
1717
});
1818
it('should return false for erroneous autobahn strings', function() {
19-
Validation.isValidUTF8(new Buffer([0xce, 0xba, 0xe1, 0xbd])).should.not.be.ok;
19+
isValidUTF8(new Buffer([0xce, 0xba, 0xe1, 0xbd])).should.not.be.ok;
2020
});
2121
});
2222
});
23-

0 commit comments

Comments
 (0)