Skip to content

Commit 2b55b8e

Browse files
refactor(clientonly): modernize code structure and add comprehensive tests (#4022)
This PR improves `clientonly` start option with better code structure, validation, and comprehensive test coverage. ### Changes **Refactoring:** - Improved parameter handling with explicit function parameter passing instead of closure - Added port validation (1-65535) with proper NaN handling - Removed unnecessary IIFE wrapper (Node.js modules are already scoped) - Extracted `getCommandLineParameter` as a reusable top-level function - Enhanced error reporting with better error messages - Added connection logging for debugging **Testing:** - Added comprehensive e2e tests for parameter validation - Test coverage for missing/incomplete parameters - Test coverage for local address rejection (localhost, 127.0.0.1, ::1, ::ffff:127.0.0.1) - Test coverage for port validation (invalid ranges, non-numeric values) - Test coverage for TLS flag parsing - Integration test with running server ### Testing All tests pass: ```bash npm test -- tests/e2e/clientonly_spec.js # ✓ 18 tests passed
1 parent 6324ec2 commit 2b55b8e

File tree

2 files changed

+333
-123
lines changed

2 files changed

+333
-123
lines changed

clientonly/index.js

Lines changed: 154 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,167 @@
11
"use strict";
22

3-
// Use separate scope to prevent global scope pollution
4-
(function () {
3+
const http = require("node:http");
4+
const https = require("node:https");
5+
6+
/**
7+
* Get command line parameters
8+
* Assumes that a cmdline parameter is defined with `--key [value]`
9+
*
10+
* example: `node clientonly --address localhost --port 8080 --use-tls`
11+
* @param {string} key key to look for at the command line
12+
* @param {string} defaultValue value if no key is given at the command line
13+
* @returns {string} the value of the parameter
14+
*/
15+
function getCommandLineParameter (key, defaultValue = undefined) {
16+
const index = process.argv.indexOf(`--${key}`);
17+
const value = index > -1 ? process.argv[index + 1] : undefined;
18+
return value !== undefined ? String(value) : defaultValue;
19+
}
20+
21+
/**
22+
* Helper function to get server address/hostname from either the commandline or env
23+
* @returns {object} config object containing address, port, and tls properties
24+
*/
25+
function getServerParameters () {
526
const config = {};
627

7-
/**
8-
* Helper function to get server address/hostname from either the commandline or env
9-
*/
10-
function getServerAddress () {
11-
12-
/**
13-
* Get command line parameters
14-
* Assumes that a cmdline parameter is defined with `--key [value]`
15-
* @param {string} key key to look for at the command line
16-
* @param {string} defaultValue value if no key is given at the command line
17-
* @returns {string} the value of the parameter
18-
*/
19-
function getCommandLineParameter (key, defaultValue = undefined) {
20-
const index = process.argv.indexOf(`--${key}`);
21-
const value = index > -1 ? process.argv[index + 1] : undefined;
22-
return value !== undefined ? String(value) : defaultValue;
23-
}
24-
25-
// Prefer command line arguments over environment variables
26-
["address", "port"].forEach((key) => {
27-
config[key] = getCommandLineParameter(key, process.env[key.toUpperCase()]);
28-
});
29-
30-
// determine if "--use-tls"-flag was provided
31-
config.tls = process.argv.indexOf("--use-tls") > 0;
32-
}
33-
34-
/**
35-
* Gets the config from the specified server url
36-
* @param {string} url location where the server is running.
37-
* @returns {Promise} the config
38-
*/
39-
function getServerConfig (url) {
40-
// Return new pending promise
41-
return new Promise((resolve, reject) => {
42-
// Select http or https module, depending on requested url
43-
const lib = url.startsWith("https") ? require("node:https") : require("node:http");
44-
const request = lib.get(url, (response) => {
45-
let configData = "";
46-
47-
// Gather incoming data
48-
response.on("data", function (chunk) {
49-
configData += chunk;
50-
});
51-
// Resolve promise at the end of the HTTP/HTTPS stream
52-
response.on("end", function () {
28+
// Prefer command line arguments over environment variables
29+
config.address = getCommandLineParameter("address", process.env.ADDRESS);
30+
const portValue = getCommandLineParameter("port", process.env.PORT);
31+
config.port = portValue ? parseInt(portValue, 10) : undefined;
32+
33+
// determine if "--use-tls"-flag was provided
34+
config.tls = process.argv.includes("--use-tls");
35+
36+
return config;
37+
}
38+
39+
/**
40+
* Gets the config from the specified server url
41+
* @param {string} url location where the server is running.
42+
* @returns {Promise} the config
43+
*/
44+
function getServerConfig (url) {
45+
// Return new pending promise
46+
return new Promise((resolve, reject) => {
47+
// Select http or https module, depending on requested url
48+
const lib = url.startsWith("https") ? https : http;
49+
const request = lib.get(url, (response) => {
50+
let configData = "";
51+
52+
// Gather incoming data
53+
response.on("data", function (chunk) {
54+
configData += chunk;
55+
});
56+
// Resolve promise at the end of the HTTP/HTTPS stream
57+
response.on("end", function () {
58+
try {
5359
resolve(JSON.parse(configData));
54-
});
60+
} catch (parseError) {
61+
reject(new Error(`Failed to parse server response as JSON: ${parseError.message}`));
62+
}
5563
});
64+
});
5665

57-
request.on("error", function (error) {
58-
reject(new Error(`Unable to read config from server (${url} (${error.message}`));
59-
});
66+
request.on("error", function (error) {
67+
reject(new Error(`Unable to read config from server (${url}) (${error.message})`));
6068
});
69+
});
70+
}
71+
72+
/**
73+
* Print a message to the console in case of errors
74+
* @param {string} message error message to print
75+
* @param {number} code error code for the exit call
76+
*/
77+
function fail (message, code = 1) {
78+
if (message !== undefined && typeof message === "string") {
79+
console.error(message);
80+
} else {
81+
console.error("Usage: 'node clientonly --address 192.168.1.10 --port 8080 [--use-tls]'");
6182
}
62-
63-
/**
64-
* Print a message to the console in case of errors
65-
* @param {string} message error message to print
66-
* @param {number} code error code for the exit call
67-
*/
68-
function fail (message, code = 1) {
69-
if (message !== undefined && typeof message === "string") {
70-
console.log(message);
83+
process.exit(code);
84+
}
85+
86+
/**
87+
* Starts the client by connecting to the server and launching the Electron application
88+
* @param {object} config server configuration
89+
* @param {string} prefix http or https prefix
90+
* @async
91+
*/
92+
async function startClient (config, prefix) {
93+
try {
94+
const serverUrl = `${prefix}${config.address}:${config.port}/config/`;
95+
console.log(`Client: Connecting to server at ${serverUrl}`);
96+
const configReturn = await getServerConfig(serverUrl);
97+
console.log("Client: Successfully retrieved config from server");
98+
99+
// check environment for DISPLAY or WAYLAND_DISPLAY
100+
const elecParams = ["js/electron.js"];
101+
if (process.env.WAYLAND_DISPLAY) {
102+
console.log(`Client: Using WAYLAND_DISPLAY=${process.env.WAYLAND_DISPLAY}`);
103+
elecParams.push("--enable-features=UseOzonePlatform");
104+
elecParams.push("--ozone-platform=wayland");
105+
} else if (process.env.DISPLAY) {
106+
console.log(`Client: Using DISPLAY=${process.env.DISPLAY}`);
71107
} else {
72-
console.log("Usage: 'node clientonly --address 192.168.1.10 --port 8080 [--use-tls]'");
108+
fail("Error: Requires environment variable WAYLAND_DISPLAY or DISPLAY, none is provided.");
73109
}
74-
process.exit(code);
75-
}
76110

77-
getServerAddress();
78-
79-
(config.address && config.port) || fail();
80-
const prefix = config.tls ? "https://" : "http://";
81-
82-
// Only start the client if a non-local server was provided
83-
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) {
84-
getServerConfig(`${prefix}${config.address}:${config.port}/config/`)
85-
.then(function (configReturn) {
86-
// check environment for DISPLAY or WAYLAND_DISPLAY
87-
const elecParams = ["js/electron.js"];
88-
if (process.env.WAYLAND_DISPLAY) {
89-
console.log(`Client: Using WAYLAND_DISPLAY=${process.env.WAYLAND_DISPLAY}`);
90-
elecParams.push("--enable-features=UseOzonePlatform");
91-
elecParams.push("--ozone-platform=wayland");
92-
} else if (process.env.DISPLAY) {
93-
console.log(`Client: Using DISPLAY=${process.env.DISPLAY}`);
94-
} else {
95-
fail("Error: Requires environment variable WAYLAND_DISPLAY or DISPLAY, none is provided.");
96-
}
97-
// Pass along the server config via an environment variable
98-
const env = Object.create(process.env);
99-
env.clientonly = true; // set to pass to electron.js
100-
const options = { env: env };
101-
configReturn.address = config.address;
102-
configReturn.port = config.port;
103-
configReturn.tls = config.tls;
104-
env.config = JSON.stringify(configReturn);
105-
106-
// Spawn electron application
107-
const electron = require("electron");
108-
const child = require("node:child_process").spawn(electron, elecParams, options);
109-
110-
// Pipe all child process output to current stdout
111-
child.stdout.on("data", function (buf) {
112-
process.stdout.write(`Client: ${buf}`);
113-
});
114-
115-
// Pipe all child process errors to current stderr
116-
child.stderr.on("data", function (buf) {
117-
process.stderr.write(`Client: ${buf}`);
118-
});
119-
120-
child.on("error", function (err) {
121-
process.stdout.write(`Client: ${err}`);
122-
});
123-
124-
child.on("close", (code) => {
125-
if (code !== 0) {
126-
console.log(`There something wrong. The clientonly is not running code ${code}`);
127-
}
128-
});
129-
})
130-
.catch(function (reason) {
131-
fail(`Unable to connect to server: (${reason})`);
132-
});
133-
} else {
134-
fail();
111+
// Pass along the server config via an environment variable
112+
const env = { ...process.env };
113+
env.clientonly = true;
114+
const options = { env: env };
115+
configReturn.address = config.address;
116+
configReturn.port = config.port;
117+
configReturn.tls = config.tls;
118+
env.config = JSON.stringify(configReturn);
119+
120+
// Spawn electron application
121+
const electron = require("electron");
122+
const child = require("node:child_process").spawn(electron, elecParams, options);
123+
124+
// Pipe all child process output to current stdout
125+
child.stdout.on("data", function (buf) {
126+
process.stdout.write(`Client: ${buf}`);
127+
});
128+
129+
// Pipe all child process errors to current stderr
130+
child.stderr.on("data", function (buf) {
131+
process.stderr.write(`Client: ${buf}`);
132+
});
133+
134+
child.on("error", function (err) {
135+
process.stderr.write(`Client: ${err}`);
136+
});
137+
138+
child.on("close", (code) => {
139+
if (code !== 0) {
140+
fail(`There is something wrong. The clientonly process exited with code ${code}.`);
141+
}
142+
});
143+
} catch (reason) {
144+
fail(`Unable to connect to server: (${reason})`);
135145
}
136-
}());
146+
}
147+
148+
// Main execution
149+
const config = getServerParameters();
150+
const prefix = config.tls ? "https://" : "http://";
151+
152+
// Validate port
153+
if (config.port !== undefined && (isNaN(config.port) || config.port < 1 || config.port > 65535)) {
154+
fail(`Invalid port number: ${config.port}. Port must be between 1 and 65535.`);
155+
}
156+
157+
// Only start the client if a non-local server was provided and address/port are set
158+
const LOCAL_ADDRESSES = ["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1"];
159+
if (
160+
config.address
161+
&& config.port
162+
&& !LOCAL_ADDRESSES.includes(config.address)
163+
) {
164+
startClient(config, prefix);
165+
} else {
166+
fail();
167+
}

0 commit comments

Comments
 (0)