diff --git a/.jshintrc b/.jshintrc index 4a9bbe28..98d8766c 100644 --- a/.jshintrc +++ b/.jshintrc @@ -25,7 +25,7 @@ // "single" : require single quotes // "double" : require double quotes "undef" : true, // true: Require all non-global variables to be declared (prevents global leaks) - "unused" : true, // true: Require all defined variables be used + "unused" : "vars", // vars: Require all defined variables be used, ignore function params "strict" : false, // true: Requires all functions run in ES5 Strict Mode "maxparams" : false, // {int} Max number of formal params allowed per function "maxdepth" : false, // {int} Max depth of nested blocks (within functions) @@ -82,5 +82,7 @@ "yui" : false, // Yahoo User Interface // Custom Globals - "globals" : {} // additional predefined global variables + "globals" : { // additional predefined global variables + "WebSocket": true + } } diff --git a/gulpfile.js b/gulpfile.js index f6385456..b515b928 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -5,7 +5,7 @@ var gulp = require('gulp'); var jshint = require('gulp-jshint'); gulp.task('lint', function() { - return gulp.src(['gulpfile.js', 'lib/**/*.js', 'test/unit/*.js']) + return gulp.src(['gulpfile.js', 'lib/**/*.js', 'test/**/*.js']) .pipe(jshint('.jshintrc')) .pipe(jshint.reporter('jshint-stylish', {verbose: true})) .pipe(jshint.reporter('fail')); diff --git a/test/scripts/autobahn-test-client.js b/test/scripts/autobahn-test-client.js index 6def3e11..a448d86e 100755 --- a/test/scripts/autobahn-test-client.js +++ b/test/scripts/autobahn-test-client.js @@ -2,14 +2,14 @@ /************************************************************************ * Copyright 2010-2011 Worlize Inc. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -34,14 +34,14 @@ process.argv.forEach(function(value) { } }); -args.protocol = args.secure ? 'wss:' : 'ws:' +args.protocol = args.secure ? 'wss:' : 'ws:'; -console.log("WebSocket-Node: Echo test client for running against the Autobahn test suite"); -console.log("Usage: ./libwebsockets-test-client.js --host=127.0.0.1 --port=9000 [--secure]"); -console.log(""); +console.log('WebSocket-Node: Echo test client for running against the Autobahn test suite'); +console.log('Usage: ./libwebsockets-test-client.js --host=127.0.0.1 --port=9000 [--secure]'); +console.log(''); -console.log("Starting test run."); +console.log('Starting test run.'); getCaseCount(function(caseCount) { var currentCase = 1; @@ -54,9 +54,9 @@ getCaseCount(function(caseCount) { } else { process.nextTick(function() { - console.log("Test suite complete, generating report."); + console.log('Test suite complete, generating report.'); updateReport(function() { - console.log("Report generated."); + console.log('Report generated.'); }); }); } @@ -66,7 +66,7 @@ getCaseCount(function(caseCount) { function runTestCase(caseIndex, caseCount, callback) { - console.log("Running test " + caseIndex + " of " + caseCount); + console.log('Running test ' + caseIndex + ' of ' + caseCount); var echoClient = new WebSocketClient({ maxReceivedFrameSize: 64*1024*1024, // 64MiB maxReceivedMessageSize: 64*1024*1024, // 64MiB @@ -76,12 +76,12 @@ function runTestCase(caseIndex, caseCount, callback) { }); echoClient.on('connectFailed', function(error) { - console.log("Connect Error: " + error.toString()); + console.log('Connect Error: ' + error.toString()); }); echoClient.on('connect', function(connection) { connection.on('error', function(error) { - console.log("Connection Error: " + error.toString()); + console.log('Connection Error: ' + error.toString()); }); connection.on('close', function() { callback(); @@ -98,10 +98,9 @@ function runTestCase(caseIndex, caseCount, callback) { var qs = querystring.stringify({ case: caseIndex, - agent: "WebSocket-Node Client v" + wsVersion + agent: 'WebSocket-Node Client v' + wsVersion }); - var url = "ws://" + args.host + ":" + args.port + "/runCase?" + qs; - echoClient.connect("ws://" + args.host + ":" + args.port + "/runCase?" + qs, []); + echoClient.connect('ws://' + args.host + ':' + args.port + '/runCase?' + qs, []); } function getCaseCount(callback) { @@ -113,11 +112,11 @@ function getCaseCount(callback) { }); connection.on('message', function(message) { if (message.type === 'utf8') { - console.log("Got case count: " + message.utf8Data); + console.log('Got case count: ' + message.utf8Data); caseCount = parseInt(message.utf8Data, 10); } else if (message.type === 'binary') { - throw new Error("Unexpected binary message when retrieving case count"); + throw new Error('Unexpected binary message when retrieving case count'); } }); }); @@ -127,7 +126,7 @@ function getCaseCount(callback) { function updateReport(callback) { var client = new WebSocketClient(); var qs = querystring.stringify({ - agent: "WebSocket-Node Client v" + wsVersion + agent: 'WebSocket-Node Client v' + wsVersion }); client.on('connect', function(connection) { connection.on('close', callback); diff --git a/test/scripts/echo-server.js b/test/scripts/echo-server.js index c88fe394..1d14959b 100755 --- a/test/scripts/echo-server.js +++ b/test/scripts/echo-server.js @@ -2,14 +2,14 @@ /************************************************************************ * Copyright 2010-2011 Worlize Inc. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -17,8 +17,6 @@ var WebSocketServer = require('../../lib/WebSocketServer'); var http = require('http'); -var url = require('url'); -var fs = require('fs'); var args = { /* defaults */ port: '8080', @@ -37,19 +35,19 @@ process.argv.forEach(function(value) { var port = parseInt(args.port, 10); var debug = args.debug; -console.log("WebSocket-Node: echo-server"); -console.log("Usage: ./echo-server.js [--port=8080] [--debug]"); +console.log('WebSocket-Node: echo-server'); +console.log('Usage: ./echo-server.js [--port=8080] [--debug]'); var server = http.createServer(function(request, response) { - if (debug) console.log((new Date()) + " Received request for " + request.url); + if (debug) { console.log((new Date()) + ' Received request for ' + request.url); } response.writeHead(404); response.end(); }); server.listen(port, function() { - console.log((new Date()) + " Server is listening on port " + port); + console.log((new Date()) + ' Server is listening on port ' + port); }); -wsServer = new WebSocketServer({ +var wsServer = new WebSocketServer({ httpServer: server, autoAcceptConnections: true, maxReceivedFrameSize: 64*1024*1024, // 64MiB @@ -60,11 +58,11 @@ wsServer = new WebSocketServer({ }); wsServer.on('connect', function(connection) { - if (debug) console.log((new Date()) + " Connection accepted" + - " - Protocol Version " + connection.webSocketVersion); + if (debug) { console.log((new Date()) + ' Connection accepted' + + ' - Protocol Version ' + connection.webSocketVersion); } function sendCallback(err) { if (err) { - console.error("send() error: " + err); + console.error('send() error: ' + err); connection.drop(); setTimeout(function() { process.exit(100); @@ -73,16 +71,16 @@ wsServer.on('connect', function(connection) { } connection.on('message', function(message) { if (message.type === 'utf8') { - if (debug) console.log("Received utf-8 message of " + message.utf8Data.length + " characters."); + if (debug) { console.log('Received utf-8 message of ' + message.utf8Data.length + ' characters.'); } connection.sendUTF(message.utf8Data, sendCallback); } else if (message.type === 'binary') { - if (debug) console.log("Received Binary Message of " + message.binaryData.length + " bytes"); + if (debug) { console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); } connection.sendBytes(message.binaryData, sendCallback); } }); connection.on('close', function(reasonCode, description) { - if (debug) console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected."); + if (debug) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); } connection._debug.printOutput(); }); }); diff --git a/test/scripts/fragmentation-test-client.js b/test/scripts/fragmentation-test-client.js index 9ed874ed..90e47f16 100755 --- a/test/scripts/fragmentation-test-client.js +++ b/test/scripts/fragmentation-test-client.js @@ -2,14 +2,14 @@ /************************************************************************ * Copyright 2010-2011 Worlize Inc. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -17,13 +17,13 @@ var WebSocketClient = require('../../lib/WebSocketClient'); -console.log("WebSocket-Node: Test client for parsing fragmented messages."); +console.log('WebSocket-Node: Test client for parsing fragmented messages.'); var args = { /* defaults */ secure: false, - port: "8080", - host: "127.0.0.1", - "no-defragment": false, + port: '8080', + host: '127.0.0.1', + 'no-defragment': false, binary: false }; @@ -36,15 +36,15 @@ process.argv.forEach(function(value) { } }); -args.protocol = args.secure ? 'wss:' : 'ws:' +args.protocol = args.secure ? 'wss:' : 'ws:'; if (args.help) { - console.log("Usage: ./fragmentation-test-client.js [--host=127.0.0.1] [--port=8080] [--no-defragment] [--binary]"); - console.log(""); + console.log('Usage: ./fragmentation-test-client.js [--host=127.0.0.1] [--port=8080] [--no-defragment] [--binary]'); + console.log(''); return; } else { - console.log("Use --help for usage information."); + console.log('Use --help for usage information.'); } @@ -55,7 +55,7 @@ var client = new WebSocketClient({ }); client.on('connectFailed', function(error) { - console.log("Client Error: " + error.toString()) + console.log('Client Error: ' + error.toString()); }); @@ -65,36 +65,36 @@ var startTime; var byteCounter; client.on('connect', function(connection) { - console.log("Connected"); + console.log('Connected'); startTime = new Date(); byteCounter = 0; connection.on('error', function(error) { - console.log("Connection Error: " + error.toString()); + console.log('Connection Error: ' + error.toString()); }); connection.on('close', function() { - console.log("Connection Closed"); + console.log('Connection Closed'); }); connection.on('message', function(message) { if (message.type === 'utf8') { - console.log("Received utf-8 message of " + message.utf8Data.length + " characters."); + console.log('Received utf-8 message of ' + message.utf8Data.length + ' characters.'); logThroughput(message.utf8Data.length); requestData(); } else { - console.log("Received binary message of " + message.binaryData.length + " bytes."); + console.log('Received binary message of ' + message.binaryData.length + ' bytes.'); logThroughput(message.binaryData.length); requestData(); } }); connection.on('frame', function(frame) { - console.log("Frame: 0x" + frame.opcode.toString(16) + "; " + frame.length + " bytes; Flags: " + renderFlags(frame)) + console.log('Frame: 0x' + frame.opcode.toString(16) + '; ' + frame.length + ' bytes; Flags: ' + renderFlags(frame)); messageSize += frame.length; if (frame.fin) { - console.log("Total message size: " + messageSize + " bytes."); + console.log('Total message size: ' + messageSize + ' bytes.'); logThroughput(messageSize); messageSize = 0; requestData(); @@ -106,14 +106,14 @@ client.on('connect', function(connection) { var duration = (new Date()).valueOf() - startTime.valueOf(); if (duration > 1000) { var kiloBytesPerSecond = Math.round((byteCounter / 1024) / (duration/1000)); - console.log(" Throughput: " + kiloBytesPerSecond + " KBps"); + console.log(' Throughput: ' + kiloBytesPerSecond + ' KBps'); startTime = new Date(); byteCounter = 0; } - }; + } function sendUTFCallback(err) { - if (err) console.error("sendUTF() error: " + err); + if (err) { console.error('sendUTF() error: ' + err); } } function requestData() { @@ -144,7 +144,7 @@ client.on('connect', function(connection) { flags.push('[MASK]'); } if (flags.length === 0) { - return "---"; + return '---'; } return flags.join(' '); } @@ -153,11 +153,11 @@ client.on('connect', function(connection) { }); if (args['no-defragment']) { - console.log("Not automatically re-assembling fragmented messages."); + console.log('Not automatically re-assembling fragmented messages.'); } else { - console.log("Maximum aggregate message size: " + client.config.maxReceivedMessageSize + " bytes."); + console.log('Maximum aggregate message size: ' + client.config.maxReceivedMessageSize + ' bytes.'); } -console.log("Connecting"); +console.log('Connecting'); client.connect(args.protocol + '//' + args.host + ':' + args.port + '/', 'fragmentation-test'); diff --git a/test/scripts/fragmentation-test-server.js b/test/scripts/fragmentation-test-server.js index a01e589f..6338801f 100755 --- a/test/scripts/fragmentation-test-server.js +++ b/test/scripts/fragmentation-test-server.js @@ -2,14 +2,14 @@ /************************************************************************ * Copyright 2010-2011 Worlize Inc. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -19,15 +19,14 @@ var WebSocketServer = require('../../lib/WebSocketServer'); var WebSocketRouter = require('../../lib/WebSocketRouter'); var http = require('http'); -var url = require('url'); var fs = require('fs'); -console.log("WebSocket-Node: Test server to spit out fragmented messages."); +console.log('WebSocket-Node: Test server to spit out fragmented messages.'); var args = { - "no-fragmentation": false, - "fragment": "16384", - "port": "8080" + 'no-fragmentation': false, + 'fragment': '16384', + 'port': '8080' }; /* Parse command line options */ @@ -42,17 +41,17 @@ process.argv.forEach(function(value) { args.protocol = 'ws:'; if (args.help) { - console.log("Usage: ./fragmentation-test-server.js [--port=8080] [--fragment=n] [--no-fragmentation]"); - console.log(""); + console.log('Usage: ./fragmentation-test-server.js [--port=8080] [--fragment=n] [--no-fragmentation]'); + console.log(''); return; } else { - console.log("Use --help for usage information.") + console.log('Use --help for usage information.'); } var server = http.createServer(function(request, response) { - console.log((new Date()) + " Received request for " + request.url); - if (request.url == "/") { + console.log((new Date()) + ' Received request for ' + request.url); + if (request.url === '/') { fs.readFile('fragmentation-test-page.html', 'utf8', function(err, data) { if (err) { response.writeHead(404); @@ -72,10 +71,10 @@ var server = http.createServer(function(request, response) { } }); server.listen(args.port, function() { - console.log((new Date()) + " Server is listening on port " + args.port); + console.log((new Date()) + ' Server is listening on port ' + args.port); }); -wsServer = new WebSocketServer({ +var wsServer = new WebSocketServer({ httpServer: server, fragmentOutgoingMessages: !args['no-fragmentation'], fragmentationThreshold: parseInt(args['fragment'], 10) @@ -85,40 +84,41 @@ var router = new WebSocketRouter(); router.attachServer(wsServer); -var lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat."; +var lorem = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'; router.mount('*', 'fragmentation-test', function(request) { var connection = request.accept(request.origin); - console.log((new Date()) + " connection accepted from " + connection.remoteAddress); + console.log((new Date()) + ' connection accepted from ' + connection.remoteAddress); connection.on('message', function(message) { - if (message.type === 'utf8') { - function sendCallback(err) { - if (err) console.error("send() error: " + err); - } + function sendCallback(err) { + if (err) { console.error('send() error: ' + err); } + } + if (message.type === 'utf8') { var length = 0; var match = /sendMessage\|(\d+)/.exec(message.utf8Data); + var requestedLength; if (match) { - var requestedLength = parseInt(match[1], 10); + requestedLength = parseInt(match[1], 10); var longLorem = ''; while (length < requestedLength) { - longLorem += (" " + lorem); + longLorem += (' ' + lorem); length = Buffer.byteLength(longLorem); } longLorem = longLorem.slice(0,requestedLength); length = Buffer.byteLength(longLorem); if (length > 0) { connection.sendUTF(longLorem, sendCallback); - console.log((new Date()) + " sent " + length + " byte utf-8 message to " + connection.remoteAddress); + console.log((new Date()) + ' sent ' + length + ' byte utf-8 message to ' + connection.remoteAddress); } return; } - var match = /sendBinaryMessage\|(\d+)/.exec(message.utf8Data); + match = /sendBinaryMessage\|(\d+)/.exec(message.utf8Data); if (match) { - var requestedLength = parseInt(match[1], 10); + requestedLength = parseInt(match[1], 10); // Generate random binary data. var buffer = new Buffer(requestedLength); @@ -127,25 +127,25 @@ router.mount('*', 'fragmentation-test', function(request) { } connection.sendBytes(buffer, sendCallback); - console.log((new Date()) + " sent " + buffer.length + " byte binary message to " + connection.remoteAddress); + console.log((new Date()) + ' sent ' + buffer.length + ' byte binary message to ' + connection.remoteAddress); return; } } }); connection.on('close', function(reasonCode, description) { - console.log((new Date()) + " peer " + connection.remoteAddress + " disconnected."); + console.log((new Date()) + ' peer ' + connection.remoteAddress + ' disconnected.'); }); connection.on('error', function(error) { - console.log("Connection error for peer " + connection.remoteAddress + ": " + error); + console.log('Connection error for peer ' + connection.remoteAddress + ': ' + error); }); }); -console.log("Point your WebSocket Protocol Version 8 compliant browser at http://localhost:" + args.port + "/"); +console.log('Point your WebSocket Protocol Version 8 compliant browser at http://localhost:' + args.port + '/'); if (args['no-fragmentation']) { - console.log("Fragmentation disabled."); + console.log('Fragmentation disabled.'); } else { - console.log("Fragmenting messages at " + wsServer.config.fragmentationThreshold + " bytes"); + console.log('Fragmenting messages at ' + wsServer.config.fragmentationThreshold + ' bytes'); } diff --git a/test/scripts/libwebsockets-test-client.js b/test/scripts/libwebsockets-test-client.js index 62091f2a..8f37dae1 100755 --- a/test/scripts/libwebsockets-test-client.js +++ b/test/scripts/libwebsockets-test-client.js @@ -2,14 +2,14 @@ /************************************************************************ * Copyright 2010-2011 Worlize Inc. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -31,13 +31,13 @@ process.argv.forEach(function(value) { } }); -args.protocol = args.secure ? 'wss:' : 'ws:' +args.protocol = args.secure ? 'wss:' : 'ws:'; args.version = parseInt(args.version, 10); if (!args.host || !args.port) { - console.log("WebSocket-Node: Test client for Andy Green's libwebsockets-test-server"); - console.log("Usage: ./libwebsockets-test-client.js --host=127.0.0.1 --port=8080 [--version=8|13] [--secure]"); - console.log(""); + console.log('WebSocket-Node: Test client for Andy Green\'s libwebsockets-test-server'); + console.log('Usage: ./libwebsockets-test-client.js --host=127.0.0.1 --port=8080 [--version=8|13] [--secure]'); + console.log(''); return; } @@ -46,19 +46,19 @@ var mirrorClient = new WebSocketClient({ }); mirrorClient.on('connectFailed', function(error) { - console.log("Connect Error: " + error.toString()); + console.log('Connect Error: ' + error.toString()); }); mirrorClient.on('connect', function(connection) { - console.log("lws-mirror-protocol connected"); + console.log('lws-mirror-protocol connected'); connection.on('error', function(error) { - console.log("Connection Error: " + error.toString()); + console.log('Connection Error: ' + error.toString()); }); connection.on('close', function() { - console.log("lws-mirror-protocol Connection Closed"); + console.log('lws-mirror-protocol Connection Closed'); }); function sendCallback(err) { - if (err) console.error("send() error: " + err); + if (err) { console.error('send() error: ' + err); } } function spamCircles() { if (connection.connected) { @@ -67,7 +67,7 @@ mirrorClient.on('connect', function(connection) { var x = Math.round(Math.random() * 502); var y = Math.round(Math.random() * 306); var radius = Math.round(Math.random() * 30); - connection.send("c #" + color.toString(16) + " " + x + " " + y + " " + radius + ";", sendCallback); + connection.send('c #' + color.toString(16) + ' ' + x + ' ' + y + ' ' + radius + ';', sendCallback); setTimeout(spamCircles, 10); } } @@ -82,19 +82,19 @@ var incrementClient = new WebSocketClient({ }); incrementClient.on('connectFailed', function(error) { - console.log("Connect Error: " + error.toString()); + console.log('Connect Error: ' + error.toString()); }); incrementClient.on('connect', function(connection) { - console.log("dumb-increment-protocol connected"); + console.log('dumb-increment-protocol connected'); connection.on('error', function(error) { - console.log("Connection Error: " + error.toString()); + console.log('Connection Error: ' + error.toString()); }); connection.on('close', function() { - console.log("dumb-increment-protocol Connection Closed"); - }) + console.log('dumb-increment-protocol Connection Closed'); + }); connection.on('message', function(message) { - console.log("Number: '" + message.utf8Data + "'"); + console.log('Number: \'' + message.utf8Data + '\''); }); }); diff --git a/test/scripts/libwebsockets-test-server.js b/test/scripts/libwebsockets-test-server.js index 6ee97019..52bed7e0 100755 --- a/test/scripts/libwebsockets-test-server.js +++ b/test/scripts/libwebsockets-test-server.js @@ -2,14 +2,14 @@ /************************************************************************ * Copyright 2010-2011 Worlize Inc. * - * Licensed under the Apache License, Version 2.0 (the "License"); + * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, + * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. @@ -19,9 +19,7 @@ var WebSocketServer = require('../../lib/WebSocketServer'); var WebSocketRouter = require('../../lib/WebSocketRouter'); var http = require('http'); -var url = require('url'); var fs = require('fs'); -var os = require('os'); var args = { /* defaults */ secure: false @@ -36,27 +34,27 @@ process.argv.forEach(function(value) { } }); -args.protocol = args.secure ? 'wss:' : 'ws:' +args.protocol = args.secure ? 'wss:' : 'ws:'; if (!args.port) { - console.log("WebSocket-Node: Test Server implementing Andy Green's") - console.log("libwebsockets-test-server protocols."); - console.log("Usage: ./libwebsockets-test-server.js --port=8080 [--secure]"); - console.log(""); + console.log('WebSocket-Node: Test Server implementing Andy Green\'s'); + console.log('libwebsockets-test-server protocols.'); + console.log('Usage: ./libwebsockets-test-server.js --port=8080 [--secure]'); + console.log(''); return; } if (args.secure) { - console.log("WebSocket-Node: Test Server implementing Andy Green's") - console.log("libwebsockets-test-server protocols."); - console.log("ERROR: TLS is not yet supported."); - console.log(""); + console.log('WebSocket-Node: Test Server implementing Andy Green\'s'); + console.log('libwebsockets-test-server protocols.'); + console.log('ERROR: TLS is not yet supported.'); + console.log(''); return; } var server = http.createServer(function(request, response) { - console.log((new Date()) + " Received request for " + request.url); - if (request.url == "/") { + console.log((new Date()) + ' Received request for ' + request.url); + if (request.url === '/') { fs.readFile('libwebsockets-test.html', 'utf8', function(err, data) { if (err) { response.writeHead(404); @@ -76,10 +74,10 @@ var server = http.createServer(function(request, response) { } }); server.listen(args.port, function() { - console.log((new Date()) + " Server is listening on port " + args.port); + console.log((new Date()) + ' Server is listening on port ' + args.port); }); -wsServer = new WebSocketServer({ +var wsServer = new WebSocketServer({ httpServer: server }); @@ -92,14 +90,14 @@ var mirrorConnections = []; var mirrorHistory = []; function sendCallback(err) { - if (err) console.error("send() error: " + err); + if (err) { console.error('send() error: ' + err); } } router.mount('*', 'lws-mirror-protocol', function(request) { var cookies = [ { - name: "TestCookie", - value: "CookieValue" + Math.floor(Math.random()*1000), + name: 'TestCookie', + value: 'CookieValue' + Math.floor(Math.random()*1000), path: '/', secure: false, maxage: 5000, @@ -110,14 +108,14 @@ router.mount('*', 'lws-mirror-protocol', function(request) { // Should do origin verification here. You have to pass the accepted // origin into the accept method of the request. var connection = request.accept(request.origin, cookies); - console.log((new Date()) + " lws-mirror-protocol connection accepted from " + connection.remoteAddress + - " - Protocol Version " + connection.webSocketVersion); + console.log((new Date()) + ' lws-mirror-protocol connection accepted from ' + connection.remoteAddress + + ' - Protocol Version ' + connection.webSocketVersion); if (mirrorHistory.length > 0) { var historyString = mirrorHistory.join(''); - console.log((new Date()) + " sending mirror protocol history to client; " + connection.remoteAddress + " : " + Buffer.byteLength(historyString) + " bytes"); + console.log((new Date()) + ' sending mirror protocol history to client; ' + connection.remoteAddress + ' : ' + Buffer.byteLength(historyString) + ' bytes'); connection.send(historyString, sendCallback); } @@ -146,13 +144,13 @@ router.mount('*', 'lws-mirror-protocol', function(request) { connection.on('close', function(closeReason, description) { var index = mirrorConnections.indexOf(connection); if (index !== -1) { - console.log((new Date()) + " lws-mirror-protocol peer " + connection.remoteAddress + " disconnected, code: " + closeReason + "."); + console.log((new Date()) + ' lws-mirror-protocol peer ' + connection.remoteAddress + ' disconnected, code: ' + closeReason + '.'); mirrorConnections.splice(index, 1); } }); connection.on('error', function(error) { - console.log("Connection error for peer " + connection.remoteAddress + ": " + error); + console.log('Connection error for peer ' + connection.remoteAddress + ': ' + error); }); }); @@ -160,8 +158,8 @@ router.mount('*', 'dumb-increment-protocol', function(request) { // Should do origin verification here. You have to pass the accepted // origin into the accept method of the request. var connection = request.accept(request.origin); - console.log((new Date()) + " dumb-increment-protocol connection accepted from " + connection.remoteAddress + - " - Protocol Version " + connection.webSocketVersion); + console.log((new Date()) + ' dumb-increment-protocol connection accepted from ' + connection.remoteAddress + + ' - Protocol Version ' + connection.webSocketVersion); var number = 0; connection.timerInterval = setInterval(function() { @@ -173,16 +171,16 @@ router.mount('*', 'dumb-increment-protocol', function(request) { connection.on('message', function(message) { if (message.type === 'utf8') { if (message.utf8Data === 'reset\n') { - console.log((new Date()) + " increment reset received"); + console.log((new Date()) + ' increment reset received'); number = 0; } } }); connection.on('close', function(closeReason, description) { - console.log((new Date()) + " dumb-increment-protocol peer " + connection.remoteAddress + " disconnected, code: " + closeReason + "."); + console.log((new Date()) + ' dumb-increment-protocol peer ' + connection.remoteAddress + ' disconnected, code: ' + closeReason + '.'); }); }); -console.log("WebSocket-Node: Test Server implementing Andy Green's") -console.log("libwebsockets-test-server protocols."); -console.log("Point your WebSocket Protocol Version 8 complant browser to http://localhost:" + args.port + "/"); +console.log('WebSocket-Node: Test Server implementing Andy Green\'s'); +console.log('libwebsockets-test-server protocols.'); +console.log('Point your WebSocket Protocol Version 8 complant browser to http://localhost:' + args.port + '/'); diff --git a/test/scripts/memoryleak-client.js b/test/scripts/memoryleak-client.js index 20d2295c..04bc37a8 100644 --- a/test/scripts/memoryleak-client.js +++ b/test/scripts/memoryleak-client.js @@ -1,4 +1,4 @@ -var WebSocketClient = require('../../lib/websocket').client +var WebSocketClient = require('../../lib/websocket').client; var connectionAmount = process.argv[2]; var activeCount = 0; @@ -7,7 +7,7 @@ var deviceList = []; connectDevices(); function logActiveCount() { - console.log("---activecount---: " + activeCount); + console.log('---activecount---: ' + activeCount); } setInterval(logActiveCount, 500); @@ -19,7 +19,7 @@ function connectDevices() { } function connect( i ){ - // console.log( "--- Connecting: " + i ); + // console.log( '--- Connecting: ' + i ); var client = new WebSocketClient({ tlsOptions: { rejectUnauthorized: false @@ -33,7 +33,7 @@ function connect( i ){ }); client.on('connect', function(connection) { - console.log(i + " - connect"); + console.log(i + ' - connect'); activeCount ++; client.connection = connection; flake( i ); @@ -41,11 +41,11 @@ function connect( i ){ maybeScheduleSend(i); connection.on('error', function(error) { - console.log(i + " - " + error.toString()); + console.log(i + ' - ' + error.toString()); }); connection.on('close', function(reasonCode, closeDescription) { - console.log(i + " - close (%d) %s", reasonCode, closeDescription); + console.log(i + ' - close (%d) %s', reasonCode, closeDescription); activeCount --; if (client._flakeTimeout) { clearTimeout(client._flakeTimeout); @@ -56,12 +56,12 @@ function connect( i ){ connection.on('message', function(message) { if ( message.type === 'utf8' ) { - console.log(i + " received: '" + message.utf8Data + "'"); + console.log(i + ' received: \'' + message.utf8Data + '\''); } }); }); - client.connect("wss://localhost:8080"); + client.connect('wss://localhost:8080'); } function disconnect( i ){ @@ -75,13 +75,13 @@ function disconnect( i ){ function maybeScheduleSend(i) { var client = deviceList[i]; var random = Math.round(Math.random() * 100); - console.log(i + " - scheduling send. Random: " + random); + console.log(i + ' - scheduling send. Random: ' + random); if (random < 50) { setTimeout(function() { - console.log(i + " - send timeout. Connected? " + client.connection.connected); + console.log(i + ' - send timeout. Connected? ' + client.connection.connected); if (client && client.connection.connected) { - console.log(i + " - Sending test data! random: " + random); - client.connection.send( (new Array(random)).join("TestData") ); + console.log(i + ' - Sending test data! random: ' + random); + client.connection.send( (new Array(random)).join('TestData') ); } }, random); } @@ -92,6 +92,5 @@ function flake(i) { var timeBeforeDisconnect = Math.round(Math.random() * 2000); client._flakeTimeout = setTimeout( function() { disconnect(i); - var timeBeforeReconnect = 0; }, timeBeforeDisconnect); } diff --git a/test/scripts/memoryleak-server.js b/test/scripts/memoryleak-server.js index f4e330fa..2b078415 100644 --- a/test/scripts/memoryleak-server.js +++ b/test/scripts/memoryleak-server.js @@ -1,4 +1,4 @@ -process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; // var heapdump = require('heapdump'); // var memwatch = require('memwatch'); @@ -9,8 +9,8 @@ var https = require('https'); var activeCount = 0; var config = { - key: fs.readFileSync( "privatekey.pem" ), - cert: fs.readFileSync( "certificate.pem" ) + key: fs.readFileSync( 'privatekey.pem' ), + cert: fs.readFileSync( 'certificate.pem' ) }; var server = https.createServer( config ); @@ -19,14 +19,14 @@ server.listen(8080, function() { console.log((new Date()) + ' Server is listening on port 8080 (wss)'); }); -wsServer = new WebSocketServer({ +var wsServer = new WebSocketServer({ httpServer: server, autoAcceptConnections: false }); wsServer.on('request', function(request) { activeCount++; - console.log("Opened from: %j\n---activeCount---: %d", request.remoteAddresses, activeCount); + console.log('Opened from: %j\n---activeCount---: %d', request.remoteAddresses, activeCount); var connection = request.accept(null, request.origin); console.log((new Date()) + ' Connection accepted.'); connection.on('message', function(message) { @@ -41,19 +41,19 @@ wsServer.on('request', function(request) { }); connection.on('close', function(reasonCode, description) { activeCount--; - console.log("Closed. (" + reasonCode + ") " + description + - "\n---activeCount---: " + activeCount); + console.log('Closed. (' + reasonCode + ') ' + description + + '\n---activeCount---: ' + activeCount); // connection._debug.printOutput(); }); connection.on('error', function(error) { - console.log("Connection error: " + error); + console.log('Connection error: ' + error); }); }); // setInterval( function(){ // // global.gc(); // var filename = './heapdump/'+ new Date().getTime() + '_' + activeCount + '.heapsnapshot'; -// console.log("Triggering heapdump to write to %s", filename); +// console.log('Triggering heapdump to write to %s', filename); // heapdump.writeSnapshot( filename ); // }, 10000 ); // memwatch.on('leak', function(info) { console.log(info); }); diff --git a/test/shared/start-echo-server.js b/test/shared/start-echo-server.js index 50478ae6..9dbd9808 100644 --- a/test/shared/start-echo-server.js +++ b/test/shared/start-echo-server.js @@ -15,14 +15,14 @@ function startEchoServer(outputStream, callback) { var echoServer = require('child_process').spawn('node', [ path ]); - var state = "starting"; + var state = 'starting'; var processProxy = { kill: function(signal) { state = 'exiting'; echoServer.kill(signal); } - } + }; if (outputStream) { echoServer.stdout.pipe(outputStream); @@ -43,7 +43,7 @@ function startEchoServer(outputStream, callback) { echoServer = null; if (state !== 'exiting') { state = 'exited'; - callback(new Error("Echo Server exited unexpectedly with code " + code)); + callback(new Error('Echo Server exited unexpectedly with code ' + code)); process.exit(1); } }); diff --git a/test/unit/dropBeforeAccept.js b/test/unit/dropBeforeAccept.js index 69010743..c13a7e6d 100644 --- a/test/unit/dropBeforeAccept.js +++ b/test/unit/dropBeforeAccept.js @@ -37,7 +37,7 @@ test('Drop TCP Connection Before server accepts the request', function(t) { }); connection.on('error', function(error) { - t.fail("No error events should be received on the connection"); + t.fail('No error events should be received on the connection'); stopServer(); }); diff --git a/test/unit/w3cwebsocket.js b/test/unit/w3cwebsocket.js index 4873f4fa..8d997817 100755 --- a/test/unit/w3cwebsocket.js +++ b/test/unit/w3cwebsocket.js @@ -7,30 +7,30 @@ var startEchoServer = require('../shared/start-echo-server'); test('W3CWebSockets', function(t) { var counter = 0; - var message = "This is a test message."; + var message = 'This is a test message.'; startEchoServer(function(err, echoServer) { - if (err) { return t.fail("Unable to start echo server: " + err); } + if (err) { return t.fail('Unable to start echo server: ' + err); } - ws = new WebSocket('ws://localhost:8080/'); + var ws = new WebSocket('ws://localhost:8080/'); ws.onopen = function() { - t.equal(++counter, 1, "onopen should be called first"); + t.equal(++counter, 1, 'onopen should be called first'); ws.send(message); }; ws.onerror = function(event) { - t.fail("No errors are expected: " + event); + t.fail('No errors are expected: ' + event); }; ws.onmessage = function(event) { - t.equal(++counter, 2, "onmessage should be called second"); + t.equal(++counter, 2, 'onmessage should be called second'); - t.equal(event.data, message, "Received message data should match sent message data."); + t.equal(event.data, message, 'Received message data should match sent message data.'); ws.close(); }; ws.onclose = function(event) { - t.equal(++counter, 3, "onclose should be called last"); + t.equal(++counter, 3, 'onclose should be called last'); echoServer.kill();