Skip to content

Commit

Permalink
fix: Resolve problems reported by the TypeScript compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpoulter committed Apr 7, 2024
1 parent 997c8aa commit 954b30a
Show file tree
Hide file tree
Showing 19 changed files with 280 additions and 175 deletions.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

# 7.0.1 (2021-07-01)
* removed `spawnCb`
Expand Down
2 changes: 1 addition & 1 deletion docs/issuer-cerificate.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

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

```sh
NODE_TLS_REJECT_UNAUTHORIZED=0 selenium-standalone install`
Expand Down
2 changes: 1 addition & 1 deletion lib/driver-starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function startDriver(pathToDriver, args) {
});

driverProcess.on('error', (error) => {
throw new Error(error);
throw error;
});

const killChromeDriver = () => {
Expand Down
30 changes: 24 additions & 6 deletions lib/get-selenium-status-url.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
const fs = require('fs');
const { isSelenium4 } = require('./isSelenium4');

/**
* @readonly
* @enum {number}
*/
const PROCESS_TYPES = {
STANDALONE: 0,
GRID_HUB: 1,
GRID_NODE: 2,
DISTRIBUTOR_NODE: 3,
};

/**
* @param {string | undefined} role
* @returns {PROCESS_TYPES}
*/
const parseRole = (role) => {
if (!role || role === 'standalone') return PROCESS_TYPES.STANDALONE;
if (role === 'hub') return PROCESS_TYPES.GRID_HUB;
if (role === 'node') return PROCESS_TYPES.GRID_NODE;
if (role === 'distributor') return PROCESS_TYPES.DISTRIBUTOR_NODE;
};

/**
* @param {PROCESS_TYPES} processType
*/
const getDefaultPort = (processType) => {
switch (processType) {
case PROCESS_TYPES.STANDALONE:
Expand All @@ -25,6 +36,10 @@ const getDefaultPort = (processType) => {
}
};

/**
* @param {string[]} seleniumArgs
* @returns {PROCESS_TYPES}
*/
const getRunningProcessType = (seleniumArgs) => {
const roleArg = Math.max(
seleniumArgs.indexOf('hub'),
Expand All @@ -38,20 +53,20 @@ const getRunningProcessType = (seleniumArgs) => {
};

/**
* @param {string[]} seleniumArgs
* @param {object} opts
* @param {string[]} seleniumArgs - Command line arguments
* @param {{ version?: string }} opts
* @returns {URL}
*
* @throws {Error} The args use the `distributor` process type.
*/
const getSeleniumStatusUrl = (seleniumArgs, opts) => {
const nodeConfigArg = seleniumArgs.indexOf('-nodeConfig');

// args prefix differs for selenium3 and selenium4
// The CLI args prefix is "-" for Selenium v3 and "--" for v4.
let argsPrefix = '-';
if (isSelenium4(opts.version)) {
argsPrefix = '--';
}
const portArg = seleniumArgs.indexOf(`${argsPrefix}port`);
const hostArg = seleniumArgs.indexOf(`${argsPrefix}host`);

let host = 'localhost';
let port;
Expand All @@ -77,10 +92,13 @@ const getSeleniumStatusUrl = (seleniumArgs, opts) => {
}
}

// Overrode port and host if they were specified
// Override the port and host if they were specified
const portArg = seleniumArgs.indexOf(`${argsPrefix}port`);
if (portArg !== -1) {
port = seleniumArgs[portArg + 1];
}

const hostArg = seleniumArgs.indexOf(`${argsPrefix}host`);
if (hostArg !== -1) {
host = seleniumArgs[hostArg + 1];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/install-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async function uncompressGzippedFile(from, gzipFilePath) {
let cbCalled = false;

extractor
.on('entry', (header, stream, callback) => {
.on('entry', (_header, stream, callback) => {
if (fileAlreadyUnarchived) {
if (!cbCalled) {
cbCalled = true;
Expand All @@ -214,7 +214,7 @@ async function uncompressGzippedFile(from, gzipFilePath) {
});
}

async function runInstaller(installerFile, from, to) {
async function runInstaller(installerFile, _from, to) {
const logFile = getTempFileName('installer.log');
const options = [
'/passive', // no user interaction, only show progress bar
Expand Down
10 changes: 3 additions & 7 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = install;
const { createWriteStream } = require('fs');
const { readFile, writeFile } = require('fs').promises;
const path = require('path');
const got = require('got');
const { default: got } = require('got');
const merge = require('lodash.merge');
const mapValues = require('lodash.mapvalues');

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

/** @type {import('got').Options} */
const requestOpts = Object.assign({ timeout: 320000 }, opts.requestOpts);
if (opts.proxy) {
requestOpts.proxy = opts.proxy;
}

opts.progressCb = opts.progressCb || noop;

Expand Down Expand Up @@ -291,7 +287,7 @@ async function install(_opts) {

async function getDownloadStream(downloadUrl) {
let prevTransferred = 0;
const downloadStream = got.stream(downloadUrl, requestOpts);
const downloadStream = got.stream(downloadUrl, { ...requestOpts, isStream: true });
return await new Promise((resolve, reject) => {
downloadStream
.once('response', () => {
Expand All @@ -313,7 +309,7 @@ async function install(_opts) {
downloadStreams.delete(downloadStream);
})
.once('error', (err) => {
if (err.code === 'ERR_NON_2XX_3XX_RESPONSE' && downloadUrl.includes('edge')) {
if ('code' in err && err.code === 'ERR_NON_2XX_3XX_RESPONSE' && downloadUrl.includes('edge')) {
reject(
logError(
'getDownloadStream',
Expand Down
5 changes: 5 additions & 0 deletions lib/log-error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @param {string} fnName
* @param {unknown} error
* @param {string} message
*/
const logError = (fnName, error, message = '') => {
console.error(`\nError in "${fnName}". ${message}\nSee more details below:`);
if (error) {
Expand Down
23 changes: 15 additions & 8 deletions lib/platformDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ function detectBrowserPlatformInternal(wantedArchitecture) {
}
}

/**
* @param {string} version
*/
function isWindows11(version) {
const parts = version.split('.');
if (parts.length > 2) {
Expand Down Expand Up @@ -59,6 +62,9 @@ function detectBrowserPlatformCustom(arh) {
return arh ? detectBrowserPlatformInternal(arh) : detectBrowserPlatformInternal();
}

/**
* @param {string} version
*/
function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) {
let platform;

Expand All @@ -80,6 +86,9 @@ function getChromiumEdgeDriverArchitectureOld(wantedArchitecture, version) {
return platform;
}

/**
* @param {string} version
*/
function getChromeDriverArchitectureOld(wantedArchitecture, version) {
let platform;

Expand Down Expand Up @@ -114,27 +123,25 @@ function getIeDriverArchitectureOld(wanted) {
}

function getFirefoxDriverArchitectureOld(wantedArchitecture) {
const extension = '.tar.gz';

switch (process.platform) {
case 'linux':
return getLinuxFirefoxDriverArchitectureOld(extension, wantedArchitecture);
return getLinuxFirefoxDriverArchitectureOld(wantedArchitecture);
case 'darwin':
return getMacFirefoxDriverArchitectureOld(extension);
return getMacFirefoxDriverArchitectureOld();
case 'win32':
return getWindowsFirefoxDriverArchitectureOld(wantedArchitecture);
default:
throw new Error('No Firefox driver is available for platform "' + process.platform + '"');
}
}

function getLinuxFirefoxDriverArchitectureOld(extension, wantedArchitecture = 'x64') {
function getLinuxFirefoxDriverArchitectureOld(wantedArchitecture = 'x64') {
const arch = wantedArchitecture === 'x64' ? '64' : '32';
return 'linux' + arch + extension;
return 'linux' + arch + '.tar.gz';
}

function getMacFirefoxDriverArchitectureOld(extension) {
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + extension;
function getMacFirefoxDriverArchitectureOld() {
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + '.tar.gz';
}

function getWindowsFirefoxDriverArchitectureOld(wantedArchitecture = '64') {
Expand Down
2 changes: 1 addition & 1 deletion lib/processKiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function killProcessByCmd(processes, type) {
await command(`taskkill /F /IM ${result.name} /T`);

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

console.log(`Killed process: "${processSingle}" system name is "${result.name}"`);
Expand Down
20 changes: 6 additions & 14 deletions lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ async function start(_opts) {

if (
isSelenium4(opts.version) &&
!hasParam(opts.seleniumArgs, 'hub') &&
!hasParam(opts.seleniumArgs, 'node') &&
!hasParam(opts.seleniumArgs, 'standalone') &&
!hasParam(opts.seleniumArgs, 'distributor')
!opts.seleniumArgs.includes('hub') &&
!opts.seleniumArgs.includes('node') &&
!opts.seleniumArgs.includes('standalone') &&
!opts.seleniumArgs.includes('distributor')
) {
opts.seleniumArgs.unshift('standalone');
}
Expand Down Expand Up @@ -190,12 +190,8 @@ async function start(_opts) {
}
await checkPathsExistence(Object.keys(fsPaths).map((name) => fsPaths[name].installPath));

if (
(await isPortReachable((seleniumStatusUrl && seleniumStatusUrl.port) || 4444, { timeout: 100 })) ||
opts.onlyDriver
) {
const seleniumPort = (seleniumStatusUrl && seleniumStatusUrl.port) || 4444;

const seleniumPort = Number(seleniumStatusUrl && seleniumStatusUrl.port) || 4444;
if ((await isPortReachable(seleniumPort, { timeout: 100 })) || opts.onlyDriver) {
if (!('processKiller' in opts) || ('processKiller' in opts && opts.processKiller)) {
if (opts.onlyDriver) {
const drivers = Object.keys(opts.drivers);
Expand Down Expand Up @@ -250,7 +246,3 @@ async function start(_opts) {
return selenium;
}
}

function hasParam(list, param) {
return list.find((p) => p === param);
}
9 changes: 9 additions & 0 deletions lib/validation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = { validateMajorVersionPrefix, getVersionWithZeroedPatchPart };

/**
* Returns the major version number or ''.
* @param {string} possibleMajorPrefix
* @returns {string}
*/
function validateMajorVersionPrefix(possibleMajorPrefix) {
let prefix;

Expand All @@ -9,6 +14,10 @@ function validateMajorVersionPrefix(possibleMajorPrefix) {
return prefix && prefix.length > 0 ? prefix[0] : '';
}

/**
* @param {string} fullVersion
* @return {string}
*/
function getVersionWithZeroedPatchPart(fullVersion) {
if (!/^\d+\.\d+\.\d+$/i.test(fullVersion)) {
// If version longer than just 3 numbers, like '4.0.0-beta-1', do nothing
Expand Down
Loading

0 comments on commit 954b30a

Please sign in to comment.