Skip to content

Commit fc92a7e

Browse files
theturtle32claude
andcommitted
Phase 1: Complete test suite modernization with ES6 patterns
- Refactor all unit tests (5 files) - var → const/let conversions - Refactor test infrastructure (2 files) - modern arrow functions and template literals - Refactor all test scripts (8 files) - comprehensive ES6 modernization - Fix linting issues in core library files: - Remove unused util import in WebSocketConnection.js - Fix const mutation in WebSocketRequest.js cookie parsing - Add JSHint global directive for globalThis in browser.js Test suite changes: - test/unit: dropBeforeAccept.js, regressions.js, request.js, w3cwebsocket.js, websocketFrame.js - test/shared: start-echo-server.js, test-server.js - test/scripts: autobahn-test-client.js, echo-server.js, fragmentation-test-client.js, fragmentation-test-server.js, libwebsockets-test-client.js, libwebsockets-test-server.js, memoryleak-client.js, memoryleak-server.js All 26 tests pass ✅ Linting passes without errors ✅ Maintains Node.js 4.x+ compatibility ✅ 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3061c00 commit fc92a7e

18 files changed

+281
-285
lines changed

lib/WebSocketConnection.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
***********************************************************************/
1616

17-
const util = require('util');
1817
const utils = require('./utils');
1918
const EventEmitter = require('events').EventEmitter;
2019
const WebSocketFrame = require('./WebSocketFrame');

lib/WebSocketRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ WebSocketRequest.prototype.parseCookies = function(str) {
238238
}
239239

240240
const key = pair.substr(0, eq_idx).trim();
241-
let val = pair.substr(++eq_idx, pair.length).trim();
241+
let val = pair.substr(eq_idx + 1, pair.length).trim();
242242

243243
// quoted values
244244
if ('"' === val[0]) {

lib/browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* global globalThis */
12
let _globalThis;
23
if (typeof globalThis === 'object') {
34
_globalThis = globalThis;

test/scripts/autobahn-test-client.js

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
* limitations under the License.
1616
***********************************************************************/
1717

18-
var WebSocketClient = require('../../lib/WebSocketClient');
19-
var wsVersion = require('../../lib/websocket').version;
20-
var querystring = require('querystring');
18+
const WebSocketClient = require('../../lib/WebSocketClient');
19+
const wsVersion = require('../../lib/websocket').version;
20+
const querystring = require('querystring');
2121

22-
var args = { /* defaults */
22+
const args = { /* defaults */
2323
secure: false,
2424
port: '9000',
2525
host: 'localhost'
2626
};
2727

2828
/* Parse command line options */
29-
var pattern = /^--(.*?)(?:=(.*))?$/;
30-
process.argv.forEach(function(value) {
31-
var match = pattern.exec(value);
29+
const pattern = /^--(.*?)(?:=(.*))?$/;
30+
process.argv.forEach((value) => {
31+
const match = pattern.exec(value);
3232
if (match) {
3333
args[match[1]] = match[2] ? match[2] : true;
3434
}
@@ -43,19 +43,19 @@ console.log('');
4343

4444
console.log('Starting test run.');
4545

46-
getCaseCount(function(caseCount) {
47-
var currentCase = 1;
46+
getCaseCount((caseCount) => {
47+
let currentCase = 1;
4848
runNextTestCase();
4949

5050
function runNextTestCase() {
51-
runTestCase(currentCase++, caseCount, function() {
51+
runTestCase(currentCase++, caseCount, () => {
5252
if (currentCase <= caseCount) {
5353
process.nextTick(runNextTestCase);
5454
}
5555
else {
56-
process.nextTick(function() {
56+
process.nextTick(() => {
5757
console.log('Test suite complete, generating report.');
58-
updateReport(function() {
58+
updateReport(() => {
5959
console.log('Report generated.');
6060
});
6161
});
@@ -66,27 +66,27 @@ getCaseCount(function(caseCount) {
6666

6767

6868
function runTestCase(caseIndex, caseCount, callback) {
69-
console.log('Running test ' + caseIndex + ' of ' + caseCount);
70-
var echoClient = new WebSocketClient({
69+
console.log(`Running test ${caseIndex} of ${caseCount}`);
70+
const echoClient = new WebSocketClient({
7171
maxReceivedFrameSize: 64*1024*1024, // 64MiB
7272
maxReceivedMessageSize: 64*1024*1024, // 64MiB
7373
fragmentOutgoingMessages: false,
7474
keepalive: false,
7575
disableNagleAlgorithm: false
7676
});
7777

78-
echoClient.on('connectFailed', function(error) {
79-
console.log('Connect Error: ' + error.toString());
78+
echoClient.on('connectFailed', (error) => {
79+
console.log(`Connect Error: ${error.toString()}`);
8080
});
8181

82-
echoClient.on('connect', function(connection) {
83-
connection.on('error', function(error) {
84-
console.log('Connection Error: ' + error.toString());
82+
echoClient.on('connect', (connection) => {
83+
connection.on('error', (error) => {
84+
console.log(`Connection Error: ${error.toString()}`);
8585
});
86-
connection.on('close', function() {
86+
connection.on('close', () => {
8787
callback();
8888
});
89-
connection.on('message', function(message) {
89+
connection.on('message', (message) => {
9090
if (message.type === 'utf8') {
9191
connection.sendUTF(message.utf8Data);
9292
}
@@ -96,40 +96,40 @@ function runTestCase(caseIndex, caseCount, callback) {
9696
});
9797
});
9898

99-
var qs = querystring.stringify({
99+
const qs = querystring.stringify({
100100
case: caseIndex,
101-
agent: 'WebSocket-Node Client v' + wsVersion
101+
agent: `WebSocket-Node Client v${wsVersion}`
102102
});
103-
echoClient.connect('ws://' + args.host + ':' + args.port + '/runCase?' + qs, []);
103+
echoClient.connect(`ws://${args.host}:${args.port}/runCase?${qs}`, []);
104104
}
105105

106106
function getCaseCount(callback) {
107-
var client = new WebSocketClient();
108-
var caseCount = NaN;
109-
client.on('connect', function(connection) {
110-
connection.on('close', function() {
107+
const client = new WebSocketClient();
108+
let caseCount = NaN;
109+
client.on('connect', (connection) => {
110+
connection.on('close', () => {
111111
callback(caseCount);
112112
});
113-
connection.on('message', function(message) {
113+
connection.on('message', (message) => {
114114
if (message.type === 'utf8') {
115-
console.log('Got case count: ' + message.utf8Data);
115+
console.log(`Got case count: ${message.utf8Data}`);
116116
caseCount = parseInt(message.utf8Data, 10);
117117
}
118118
else if (message.type === 'binary') {
119119
throw new Error('Unexpected binary message when retrieving case count');
120120
}
121121
});
122122
});
123-
client.connect('ws://' + args.host + ':' + args.port + '/getCaseCount', []);
123+
client.connect(`ws://${args.host}:${args.port}/getCaseCount`, []);
124124
}
125125

126126
function updateReport(callback) {
127-
var client = new WebSocketClient();
128-
var qs = querystring.stringify({
129-
agent: 'WebSocket-Node Client v' + wsVersion
127+
const client = new WebSocketClient();
128+
const qs = querystring.stringify({
129+
agent: `WebSocket-Node Client v${wsVersion}`
130130
});
131-
client.on('connect', function(connection) {
131+
client.on('connect', (connection) => {
132132
connection.on('close', callback);
133133
});
134-
client.connect('ws://localhost:9000/updateReports?' + qs);
134+
client.connect(`ws://localhost:9000/updateReports?${qs}`);
135135
}

test/scripts/echo-server.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,39 @@
1515
* limitations under the License.
1616
***********************************************************************/
1717

18-
var WebSocketServer = require('../../lib/WebSocketServer');
19-
var http = require('http');
18+
const WebSocketServer = require('../../lib/WebSocketServer');
19+
const http = require('http');
2020

21-
var args = { /* defaults */
21+
const args = { /* defaults */
2222
port: '8080',
2323
debug: false
2424
};
2525

2626
/* Parse command line options */
27-
var pattern = /^--(.*?)(?:=(.*))?$/;
27+
const pattern = /^--(.*?)(?:=(.*))?$/;
2828
process.argv.forEach(function(value) {
29-
var match = pattern.exec(value);
29+
const match = pattern.exec(value);
3030
if (match) {
3131
args[match[1]] = match[2] ? match[2] : true;
3232
}
3333
});
3434

35-
var port = parseInt(args.port, 10);
36-
var debug = args.debug;
35+
const port = parseInt(args.port, 10);
36+
const debug = args.debug;
3737

3838
console.log('WebSocket-Node: echo-server');
3939
console.log('Usage: ./echo-server.js [--port=8080] [--debug]');
4040

41-
var server = http.createServer(function(request, response) {
42-
if (debug) { console.log((new Date()) + ' Received request for ' + request.url); }
41+
const server = http.createServer(function(request, response) {
42+
if (debug) { console.log(`${new Date()} Received request for ${request.url}`); }
4343
response.writeHead(404);
4444
response.end();
4545
});
4646
server.listen(port, function() {
47-
console.log((new Date()) + ' Server is listening on port ' + port);
47+
console.log(`${new Date()} Server is listening on port ${port}`);
4848
});
4949

50-
var wsServer = new WebSocketServer({
50+
const wsServer = new WebSocketServer({
5151
httpServer: server,
5252
autoAcceptConnections: true,
5353
maxReceivedFrameSize: 64*1024*1024, // 64MiB
@@ -58,11 +58,10 @@ var wsServer = new WebSocketServer({
5858
});
5959

6060
wsServer.on('connect', function(connection) {
61-
if (debug) { console.log((new Date()) + ' Connection accepted' +
62-
' - Protocol Version ' + connection.webSocketVersion); }
61+
if (debug) { console.log(`${new Date()} Connection accepted - Protocol Version ${connection.webSocketVersion}`); }
6362
function sendCallback(err) {
6463
if (err) {
65-
console.error('send() error: ' + err);
64+
console.error(`send() error: ${err}`);
6665
connection.drop();
6766
setTimeout(function() {
6867
process.exit(100);
@@ -71,16 +70,16 @@ wsServer.on('connect', function(connection) {
7170
}
7271
connection.on('message', function(message) {
7372
if (message.type === 'utf8') {
74-
if (debug) { console.log('Received utf-8 message of ' + message.utf8Data.length + ' characters.'); }
73+
if (debug) { console.log(`Received utf-8 message of ${message.utf8Data.length} characters.`); }
7574
connection.sendUTF(message.utf8Data, sendCallback);
7675
}
7776
else if (message.type === 'binary') {
78-
if (debug) { console.log('Received Binary Message of ' + message.binaryData.length + ' bytes'); }
77+
if (debug) { console.log(`Received Binary Message of ${message.binaryData.length} bytes`); }
7978
connection.sendBytes(message.binaryData, sendCallback);
8079
}
8180
});
8281
connection.on('close', function(reasonCode, description) {
83-
if (debug) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }
82+
if (debug) { console.log(`${new Date()} Peer ${connection.remoteAddress} disconnected.`); }
8483
connection._debug.printOutput();
8584
});
8685
});

test/scripts/fragmentation-test-client.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
* limitations under the License.
1616
***********************************************************************/
1717

18-
var WebSocketClient = require('../../lib/WebSocketClient');
18+
const WebSocketClient = require('../../lib/WebSocketClient');
1919

2020
console.log('WebSocket-Node: Test client for parsing fragmented messages.');
2121

22-
var args = { /* defaults */
22+
const args = { /* defaults */
2323
secure: false,
2424
port: '8080',
2525
host: '127.0.0.1',
@@ -28,9 +28,9 @@ var args = { /* defaults */
2828
};
2929

3030
/* Parse command line options */
31-
var pattern = /^--(.*?)(?:=(.*))?$/;
32-
process.argv.forEach(function(value) {
33-
var match = pattern.exec(value);
31+
const pattern = /^--(.*?)(?:=(.*))?$/;
32+
process.argv.forEach((value) => {
33+
const match = pattern.exec(value);
3434
if (match) {
3535
args[match[1]] = match[2] ? match[2] : true;
3636
}
@@ -48,53 +48,53 @@ else {
4848
}
4949

5050

51-
var client = new WebSocketClient({
51+
const client = new WebSocketClient({
5252
maxReceivedMessageSize: 128*1024*1024, // 128 MiB
5353
maxReceivedFrameSize: 1*1024*1024, // 1 MiB
5454
assembleFragments: !args['no-defragment']
5555
});
5656

57-
client.on('connectFailed', function(error) {
58-
console.log('Client Error: ' + error.toString());
57+
client.on('connectFailed', (error) => {
58+
console.log(`Client Error: ${error.toString()}`);
5959
});
6060

6161

62-
var requestedLength = 100;
63-
var messageSize = 0;
64-
var startTime;
65-
var byteCounter;
62+
let requestedLength = 100;
63+
let messageSize = 0;
64+
let startTime;
65+
let byteCounter;
6666

67-
client.on('connect', function(connection) {
67+
client.on('connect', (connection) => {
6868
console.log('Connected');
6969
startTime = new Date();
7070
byteCounter = 0;
7171

72-
connection.on('error', function(error) {
73-
console.log('Connection Error: ' + error.toString());
72+
connection.on('error', (error) => {
73+
console.log(`Connection Error: ${error.toString()}`);
7474
});
7575

76-
connection.on('close', function() {
76+
connection.on('close', () => {
7777
console.log('Connection Closed');
7878
});
7979

80-
connection.on('message', function(message) {
80+
connection.on('message', (message) => {
8181
if (message.type === 'utf8') {
82-
console.log('Received utf-8 message of ' + message.utf8Data.length + ' characters.');
82+
console.log(`Received utf-8 message of ${message.utf8Data.length} characters.`);
8383
logThroughput(message.utf8Data.length);
8484
requestData();
8585
}
8686
else {
87-
console.log('Received binary message of ' + message.binaryData.length + ' bytes.');
87+
console.log(`Received binary message of ${message.binaryData.length} bytes.`);
8888
logThroughput(message.binaryData.length);
8989
requestData();
9090
}
9191
});
9292

93-
connection.on('frame', function(frame) {
94-
console.log('Frame: 0x' + frame.opcode.toString(16) + '; ' + frame.length + ' bytes; Flags: ' + renderFlags(frame));
93+
connection.on('frame', (frame) => {
94+
console.log(`Frame: 0x${frame.opcode.toString(16)}; ${frame.length} bytes; Flags: ${renderFlags(frame)}`);
9595
messageSize += frame.length;
9696
if (frame.fin) {
97-
console.log('Total message size: ' + messageSize + ' bytes.');
97+
console.log(`Total message size: ${messageSize} bytes.`);
9898
logThroughput(messageSize);
9999
messageSize = 0;
100100
requestData();
@@ -103,10 +103,10 @@ client.on('connect', function(connection) {
103103

104104
function logThroughput(numBytes) {
105105
byteCounter += numBytes;
106-
var duration = (new Date()).valueOf() - startTime.valueOf();
106+
const duration = (new Date()).valueOf() - startTime.valueOf();
107107
if (duration > 1000) {
108-
var kiloBytesPerSecond = Math.round((byteCounter / 1024) / (duration/1000));
109-
console.log(' Throughput: ' + kiloBytesPerSecond + ' KBps');
108+
const kiloBytesPerSecond = Math.round((byteCounter / 1024) / (duration/1000));
109+
console.log(` Throughput: ${kiloBytesPerSecond} KBps`);
110110
startTime = new Date();
111111
byteCounter = 0;
112112
}
@@ -118,16 +118,16 @@ client.on('connect', function(connection) {
118118

119119
function requestData() {
120120
if (args.binary) {
121-
connection.sendUTF('sendBinaryMessage|' + requestedLength, sendUTFCallback);
121+
connection.sendUTF(`sendBinaryMessage|${requestedLength}`, sendUTFCallback);
122122
}
123123
else {
124-
connection.sendUTF('sendMessage|' + requestedLength, sendUTFCallback);
124+
connection.sendUTF(`sendMessage|${requestedLength}`, sendUTFCallback);
125125
}
126126
requestedLength += Math.ceil(Math.random() * 1024);
127127
}
128128

129129
function renderFlags(frame) {
130-
var flags = [];
130+
const flags = [];
131131
if (frame.fin) {
132132
flags.push('[FIN]');
133133
}
@@ -156,8 +156,8 @@ if (args['no-defragment']) {
156156
console.log('Not automatically re-assembling fragmented messages.');
157157
}
158158
else {
159-
console.log('Maximum aggregate message size: ' + client.config.maxReceivedMessageSize + ' bytes.');
159+
console.log(`Maximum aggregate message size: ${client.config.maxReceivedMessageSize} bytes.`);
160160
}
161161
console.log('Connecting');
162162

163-
client.connect(args.protocol + '//' + args.host + ':' + args.port + '/', 'fragmentation-test');
163+
client.connect(`${args.protocol}//${args.host}:${args.port}/`, 'fragmentation-test');

0 commit comments

Comments
 (0)