Skip to content

Commit 636a4fe

Browse files
committed
Making compiled modules optional
Eliminating wscript Using explicit 'install.js' script to call node-gyp for building the native modules
1 parent 98344eb commit 636a4fe

10 files changed

+91
-42
lines changed

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
all:
2+
node-gyp configure build
3+
4+
clean:
5+
node-gyp clean
6+
7+
autobahn:
8+
@NODE_PATH=lib node test/autobahn-test-client.js --host=127.0.0.1 --port=9000
9+
10+
autobahn-server:
11+
@NODE_PATH=lib node test/echo-server.js

binding.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
'targets': [
33
{
44
'target_name': 'validation',
5+
'cflags': [ '-O3' ],
56
'sources': [ 'src/validation.cc' ]
67
},
78
{
89
'target_name': 'xor',
10+
'cflags': [ '-O3' ],
911
'sources': [ 'src/xor.cpp' ]
1012
}
1113
]

install.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var spawn = require('child_process').spawn
2+
, exec = require('child_process').exec
3+
, fs = require('fs')
4+
, version = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).version
5+
, verbose = process.env['npm_package_config_verbose'] != null ? process.env['npm_package_config_verbose'] === 'true' : false;
6+
7+
console.log('[websocket v%s] Attempting to compile native extensions.', version);
8+
9+
var gyp = exec('node-gyp rebuild', {cwd: __dirname});
10+
gyp.stdout.on('data', function(data) {
11+
if (verbose) process.stdout.write(data);
12+
});
13+
gyp.stderr.on('data', function(data) {
14+
if (verbose) process.stdout.write(data);
15+
});
16+
gyp.on('exit', function(code) {
17+
if (code !== 0) {
18+
console.log("[websocket v%s]", version);
19+
console.log(' Native code compile failed!!');
20+
console.log(' On Windows, native extensions require Visual Studio and Python.');
21+
console.log(' On Unix, native extensions require Python, make and a C++ compiler.');
22+
console.log(' Start npm with --websocket:verbose to show compilation output (if any).');
23+
}
24+
else {
25+
console.log('[websocket v%s] Native extension compilation successful!', version);
26+
}
27+
process.exit();
28+
});

lib/Validation.fallback.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*!
2+
* UTF-8 Validation Fallback Code originally from:
3+
* ws: a node.js websocket client
4+
* Copyright(c) 2011 Einar Otto Stangvik <[email protected]>
5+
* MIT Licensed
6+
*/
7+
8+
module.exports.Validation = {
9+
isValidUTF8: function(buffer) {
10+
return true;
11+
}
12+
};

lib/Validation.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* Copyright(c) 2011 Einar Otto Stangvik <[email protected]>
55
* MIT Licensed
66
*/
7-
8-
/**
9-
* Node version 0.4 and 0.6 compatibility
10-
*/
117

128
try {
13-
module.exports = require('../build/Release/validation');
9+
module.exports = require('../build/Release/validation');
10+
} catch (e) { try {
11+
module.exports = require('../build/default/validation');
1412
} catch (e) { try {
15-
module.exports = require('../build/default/validation');
13+
module.exports = require('./Validation.fallback');
14+
console.warn("Warning: Native modules not compiled. UTF-8 validation disabled.")
1615
} catch (e) {
17-
throw e;
18-
}}
16+
console.error("validation.node seems not to have been built. Run npm install.")
17+
throw e;
18+
}}}

lib/WebSocketFrame.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ WebSocketFrame.prototype.addData = function(bufferList, fragmentationType) {
111111
if (bufferList.length >= 4) {
112112
bufferList.joinInto(this.maskBytes, 0, 0, 4);
113113
bufferList.advance(4);
114-
this.maskPos = 0;
115114
this.parseState = WAITING_FOR_PAYLOAD;
116115
}
117116
}
@@ -137,7 +136,7 @@ WebSocketFrame.prototype.addData = function(bufferList, fragmentationType) {
137136
this.binaryPayload = bufferList.take(this.length);
138137
bufferList.advance(this.length);
139138
if (this.mask) {
140-
this.maskPos = xor(this.binaryPayload, this.maskBytes, 0);
139+
xor(this.binaryPayload, this.maskBytes, 0);
141140
}
142141

143142
if (this.opcode === 0x08) { // WebSocketOpcode.CONNECTION_CLOSE
@@ -259,14 +258,13 @@ WebSocketFrame.prototype.toBuffer = function(nullMask) {
259258
maskKey = 0x00000000;
260259
}
261260
ctio.wuint32(maskKey, 'big', this.maskBytes, 0);
262-
this.maskPos = 0;
263261

264262
// write the mask key
265263
this.maskBytes.copy(output, outputPos);
266264
outputPos += 4;
267265

268266
data.copy(output, outputPos);
269-
this.maskPos = xor(output.slice(outputPos), this.maskBytes, this.maskPos);
267+
xor(output.slice(outputPos), this.maskBytes, 0);
270268
}
271269
else {
272270
data.copy(output, outputPos);

lib/xor.fallback.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
xor: function (payload, maskBytes, maskPos) {
3+
var end = payload.length;
4+
if (typeof(maskPos) !== 'number') {
5+
maskPos = 0;
6+
}
7+
for (var i=0; i < end; i++) {
8+
payload[i] = payload[i] ^ maskBytes[maskPos];
9+
maskPos = (maskPos + 1) & 3;
10+
}
11+
return maskPos;
12+
}
13+
};

lib/xor.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
* Version: 1.0
66
*/
77

8-
/**
9-
* Buffer xor module
10-
* Node version 0.4 and 0.6 compatibility
11-
*/
12-
138
try {
14-
module.exports = require('../build/Release/xor');
9+
module.exports = require('../build/Release/xor');
1510
} catch (e) { try {
16-
module.exports = require('../build/default/xor');
11+
module.exports = require('../build/default/xor');
12+
} catch(e) { try {
13+
module.exports = require('./xor.fallback');
14+
console.warn("Warning: Native modules not compiled. XOR performance will be degraded.")
1715
} catch (e) {
18-
throw e;
19-
}}
16+
console.error("xor.node seems not to have been built. Run npm install.")
17+
throw e;
18+
}}}

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "websocket",
33
"description": "Websocket Client & Server Library implementing the WebSocket protocol as specified in RFC 6455.",
44
"keywords": ["websocket", "socket", "networking", "comet", "push"],
5-
"author": "Brian McKelvey <[email protected]>",
5+
"author": "Brian McKelvey <[email protected]> (https://www.worlize.com/)",
66
"version": "1.0.6",
77
"repository": {
88
"type": "git",
@@ -11,6 +11,12 @@
1111
"engines": {
1212
"node": ">=0.6.13"
1313
},
14+
"config": {
15+
"verbose" : false
16+
},
17+
"scripts": {
18+
"install": "node install.js"
19+
},
1420
"main": "index",
1521
"directories": {
1622
"lib": "./lib"

wscript

-20
This file was deleted.

0 commit comments

Comments
 (0)