Skip to content

Commit 954b30a

Browse files
committed
fix: Resolve problems reported by the TypeScript compiler
1 parent 997c8aa commit 954b30a

19 files changed

+280
-175
lines changed

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# 7.1.0 (2021-07-01)
1717
* support Apple M1 for chrome and gecko drivers #558
18-
* prevent selenium process from hagning if spawned programmatically without stdout/stderr handlers
18+
* prevent selenium process from hanging if spawned programmatically without stdout/stderr handlers
1919

2020
# 7.0.1 (2021-07-01)
2121
* removed `spawnCb`

docs/issuer-cerificate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
### `Error: unable to get local issuer certificate`
99

10-
This error might happen when you are behind a specific proxy. Then you need to set some environement variables:
10+
This error might happen when you are behind a specific proxy. Then you need to set some environment variables:
1111

1212
```sh
1313
NODE_TLS_REJECT_UNAUTHORIZED=0 selenium-standalone install`

lib/driver-starter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function startDriver(pathToDriver, args) {
2828
});
2929

3030
driverProcess.on('error', (error) => {
31-
throw new Error(error);
31+
throw error;
3232
});
3333

3434
const killChromeDriver = () => {

lib/get-selenium-status-url.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
const fs = require('fs');
22
const { isSelenium4 } = require('./isSelenium4');
33

4+
/**
5+
* @readonly
6+
* @enum {number}
7+
*/
48
const PROCESS_TYPES = {
59
STANDALONE: 0,
610
GRID_HUB: 1,
711
GRID_NODE: 2,
812
DISTRIBUTOR_NODE: 3,
913
};
1014

15+
/**
16+
* @param {string | undefined} role
17+
* @returns {PROCESS_TYPES}
18+
*/
1119
const parseRole = (role) => {
1220
if (!role || role === 'standalone') return PROCESS_TYPES.STANDALONE;
1321
if (role === 'hub') return PROCESS_TYPES.GRID_HUB;
1422
if (role === 'node') return PROCESS_TYPES.GRID_NODE;
1523
if (role === 'distributor') return PROCESS_TYPES.DISTRIBUTOR_NODE;
1624
};
1725

26+
/**
27+
* @param {PROCESS_TYPES} processType
28+
*/
1829
const getDefaultPort = (processType) => {
1930
switch (processType) {
2031
case PROCESS_TYPES.STANDALONE:
@@ -25,6 +36,10 @@ const getDefaultPort = (processType) => {
2536
}
2637
};
2738

39+
/**
40+
* @param {string[]} seleniumArgs
41+
* @returns {PROCESS_TYPES}
42+
*/
2843
const getRunningProcessType = (seleniumArgs) => {
2944
const roleArg = Math.max(
3045
seleniumArgs.indexOf('hub'),
@@ -38,20 +53,20 @@ const getRunningProcessType = (seleniumArgs) => {
3853
};
3954

4055
/**
41-
* @param {string[]} seleniumArgs
42-
* @param {object} opts
56+
* @param {string[]} seleniumArgs - Command line arguments
57+
* @param {{ version?: string }} opts
4358
* @returns {URL}
59+
*
60+
* @throws {Error} The args use the `distributor` process type.
4461
*/
4562
const getSeleniumStatusUrl = (seleniumArgs, opts) => {
4663
const nodeConfigArg = seleniumArgs.indexOf('-nodeConfig');
4764

48-
// args prefix differs for selenium3 and selenium4
65+
// The CLI args prefix is "-" for Selenium v3 and "--" for v4.
4966
let argsPrefix = '-';
5067
if (isSelenium4(opts.version)) {
5168
argsPrefix = '--';
5269
}
53-
const portArg = seleniumArgs.indexOf(`${argsPrefix}port`);
54-
const hostArg = seleniumArgs.indexOf(`${argsPrefix}host`);
5570

5671
let host = 'localhost';
5772
let port;
@@ -77,10 +92,13 @@ const getSeleniumStatusUrl = (seleniumArgs, opts) => {
7792
}
7893
}
7994

80-
// Overrode port and host if they were specified
95+
// Override the port and host if they were specified
96+
const portArg = seleniumArgs.indexOf(`${argsPrefix}port`);
8197
if (portArg !== -1) {
8298
port = seleniumArgs[portArg + 1];
8399
}
100+
101+
const hostArg = seleniumArgs.indexOf(`${argsPrefix}host`);
84102
if (hostArg !== -1) {
85103
host = seleniumArgs[hostArg + 1];
86104
}

lib/install-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ async function uncompressGzippedFile(from, gzipFilePath) {
187187
let cbCalled = false;
188188

189189
extractor
190-
.on('entry', (header, stream, callback) => {
190+
.on('entry', (_header, stream, callback) => {
191191
if (fileAlreadyUnarchived) {
192192
if (!cbCalled) {
193193
cbCalled = true;
@@ -214,7 +214,7 @@ async function uncompressGzippedFile(from, gzipFilePath) {
214214
});
215215
}
216216

217-
async function runInstaller(installerFile, from, to) {
217+
async function runInstaller(installerFile, _from, to) {
218218
const logFile = getTempFileName('installer.log');
219219
const options = [
220220
'/passive', // no user interaction, only show progress bar

lib/install.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = install;
44
const { createWriteStream } = require('fs');
55
const { readFile, writeFile } = require('fs').promises;
66
const path = require('path');
7-
const got = require('got');
7+
const { default: got } = require('got');
88
const merge = require('lodash.merge');
99
const mapValues = require('lodash.mapvalues');
1010

@@ -49,7 +49,6 @@ const downloadStreams = new Map();
4949
* logger?: Logger
5050
* onlyDriver?: keyof import('./start').Drivers
5151
* progressCb?: ProgressCallback
52-
* proxy?: string
5352
* requestOpts?: import('got').Options
5453
* singleDriverInstall?: keyof import('./start').Drivers
5554
* version?: string
@@ -98,9 +97,6 @@ async function install(_opts) {
9897

9998
/** @type {import('got').Options} */
10099
const requestOpts = Object.assign({ timeout: 320000 }, opts.requestOpts);
101-
if (opts.proxy) {
102-
requestOpts.proxy = opts.proxy;
103-
}
104100

105101
opts.progressCb = opts.progressCb || noop;
106102

@@ -291,7 +287,7 @@ async function install(_opts) {
291287

292288
async function getDownloadStream(downloadUrl) {
293289
let prevTransferred = 0;
294-
const downloadStream = got.stream(downloadUrl, requestOpts);
290+
const downloadStream = got.stream(downloadUrl, { ...requestOpts, isStream: true });
295291
return await new Promise((resolve, reject) => {
296292
downloadStream
297293
.once('response', () => {
@@ -313,7 +309,7 @@ async function install(_opts) {
313309
downloadStreams.delete(downloadStream);
314310
})
315311
.once('error', (err) => {
316-
if (err.code === 'ERR_NON_2XX_3XX_RESPONSE' && downloadUrl.includes('edge')) {
312+
if ('code' in err && err.code === 'ERR_NON_2XX_3XX_RESPONSE' && downloadUrl.includes('edge')) {
317313
reject(
318314
logError(
319315
'getDownloadStream',

lib/log-error.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @param {string} fnName
3+
* @param {unknown} error
4+
* @param {string} message
5+
*/
16
const logError = (fnName, error, message = '') => {
27
console.error(`\nError in "${fnName}". ${message}\nSee more details below:`);
38
if (error) {

lib/platformDetection.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ function detectBrowserPlatformInternal(wantedArchitecture) {
2525
}
2626
}
2727

28+
/**
29+
* @param {string} version
30+
*/
2831
function isWindows11(version) {
2932
const parts = version.split('.');
3033
if (parts.length > 2) {
@@ -59,6 +62,9 @@ function detectBrowserPlatformCustom(arh) {
5962
return arh ? detectBrowserPlatformInternal(arh) : detectBrowserPlatformInternal();
6063
}
6164

65+
/**
66+
* @param {string} version
67+
*/
6268
function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) {
6369
let platform;
6470

@@ -80,6 +86,9 @@ function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) {
8086
return platform;
8187
}
8288

89+
/**
90+
* @param {string} version
91+
*/
8392
function getChromeDriverArchitectureOld(wantedArchitecture, version) {
8493
let platform;
8594

@@ -114,27 +123,25 @@ function getIeDriverArchitectureOld(wanted) {
114123
}
115124

116125
function getFirefoxDriverArchitectureOld(wantedArchitecture) {
117-
const extension = '.tar.gz';
118-
119126
switch (process.platform) {
120127
case 'linux':
121-
return getLinuxFirefoxDriverArchitectureOld(extension, wantedArchitecture);
128+
return getLinuxFirefoxDriverArchitectureOld(wantedArchitecture);
122129
case 'darwin':
123-
return getMacFirefoxDriverArchitectureOld(extension);
130+
return getMacFirefoxDriverArchitectureOld();
124131
case 'win32':
125132
return getWindowsFirefoxDriverArchitectureOld(wantedArchitecture);
126133
default:
127134
throw new Error('No Firefox driver is available for platform "' + process.platform + '"');
128135
}
129136
}
130137

131-
function getLinuxFirefoxDriverArchitectureOld(extension, wantedArchitecture = 'x64') {
138+
function getLinuxFirefoxDriverArchitectureOld(wantedArchitecture = 'x64') {
132139
const arch = wantedArchitecture === 'x64' ? '64' : '32';
133-
return 'linux' + arch + extension;
140+
return 'linux' + arch + '.tar.gz';
134141
}
135142

136-
function getMacFirefoxDriverArchitectureOld(extension) {
137-
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + extension;
143+
function getMacFirefoxDriverArchitectureOld() {
144+
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + '.tar.gz';
138145
}
139146

140147
function getWindowsFirefoxDriverArchitectureOld(wantedArchitecture = '64') {

lib/processKiller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async function killProcessByCmd(processes, type) {
5959
await command(`taskkill /F /IM ${result.name} /T`);
6060

6161
console.log(`Killed process: "${processSingle}" system name is "${result.name}"`);
62-
} else if (!process.name.includes('node')) {
62+
} else if (!result.name.includes('node')) {
6363
await command(`pkill -f ${result.name}`);
6464

6565
console.log(`Killed process: "${processSingle}" system name is "${result.name}"`);

lib/start.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ async function start(_opts) {
6868

6969
if (
7070
isSelenium4(opts.version) &&
71-
!hasParam(opts.seleniumArgs, 'hub') &&
72-
!hasParam(opts.seleniumArgs, 'node') &&
73-
!hasParam(opts.seleniumArgs, 'standalone') &&
74-
!hasParam(opts.seleniumArgs, 'distributor')
71+
!opts.seleniumArgs.includes('hub') &&
72+
!opts.seleniumArgs.includes('node') &&
73+
!opts.seleniumArgs.includes('standalone') &&
74+
!opts.seleniumArgs.includes('distributor')
7575
) {
7676
opts.seleniumArgs.unshift('standalone');
7777
}
@@ -190,12 +190,8 @@ async function start(_opts) {
190190
}
191191
await checkPathsExistence(Object.keys(fsPaths).map((name) => fsPaths[name].installPath));
192192

193-
if (
194-
(await isPortReachable((seleniumStatusUrl && seleniumStatusUrl.port) || 4444, { timeout: 100 })) ||
195-
opts.onlyDriver
196-
) {
197-
const seleniumPort = (seleniumStatusUrl && seleniumStatusUrl.port) || 4444;
198-
193+
const seleniumPort = Number(seleniumStatusUrl && seleniumStatusUrl.port) || 4444;
194+
if ((await isPortReachable(seleniumPort, { timeout: 100 })) || opts.onlyDriver) {
199195
if (!('processKiller' in opts) || ('processKiller' in opts && opts.processKiller)) {
200196
if (opts.onlyDriver) {
201197
const drivers = Object.keys(opts.drivers);
@@ -250,7 +246,3 @@ async function start(_opts) {
250246
return selenium;
251247
}
252248
}
253-
254-
function hasParam(list, param) {
255-
return list.find((p) => p === param);
256-
}

0 commit comments

Comments
 (0)