Skip to content

Commit 4ea4537

Browse files
fix: Replace the deprecated url.parse with new URL (#2735)
1 parent 6087bb4 commit 4ea4537

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

lib/commands/recordscreen.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {SubProcess} from 'teen_process';
44
import {encodeBase64OrUpload} from '../utils';
55
import {WDA_BASE_URL} from 'appium-webdriveragent';
66
import {waitForCondition} from 'asyncbox';
7-
import url from 'node:url';
87
import type {XCUITestDriver} from '../driver';
98
import type {StartRecordingScreenOptions, StopRecordingScreenOptions} from './types';
109
import type {WDASettings} from 'appium-webdriveragent';
@@ -86,7 +85,7 @@ export class ScreenRecorder {
8685
private mainProcess: SubProcess | null;
8786
private timeoutHandler: NodeJS.Timeout | null;
8887

89-
constructor(udid: string, log: any, videoPath: string, opts: ScreenRecorderOptions = {}) {
88+
constructor(udid: string, log: any, videoPath: string, opts: ScreenRecorderOptions) {
9089
this.videoPath = videoPath;
9190
this.log = log;
9291
this.opts = opts;
@@ -144,8 +143,8 @@ export class ScreenRecorder {
144143
if ((videoFps && videoType === 'libx264') || videoTypeHWAccel) {
145144
args.push('-r', String(videoFps));
146145
}
147-
const {protocol, hostname} = url.parse(remoteUrl || '');
148-
args.push('-i', `${protocol}//${hostname}:${remotePort}`);
146+
const parsed = new URL(remoteUrl);
147+
args.push('-i', `${parsed.protocol}//${parsed.hostname}:${remotePort}`);
149148

150149
if (videoFilters || videoScale) {
151150
args.push('-vf', videoFilters || `${scaleFilterHWAccel || 'scale'}=${videoScale}`);
@@ -283,10 +282,9 @@ export async function startRecordingScreen(
283282
suffix: MP4_EXT,
284283
});
285284

286-
const wdaBaseUrl = this.opts.wdaBaseUrl || WDA_BASE_URL;
287285
const screenRecorder = new ScreenRecorder(this.device.udid, this.log, videoPath, {
288286
remotePort: this.opts.mjpegServerPort || DEFAULT_MJPEG_SERVER_PORT,
289-
remoteUrl: wdaBaseUrl,
287+
remoteUrl: this.opts.wdaBaseUrl || WDA_BASE_URL,
290288
videoType,
291289
videoFilters,
292290
videoScale,
@@ -405,8 +403,8 @@ export async function stopRecordingScreen(
405403

406404
interface ScreenRecorderOptions {
407405
hardwareAcceleration?: string;
408-
remotePort?: number;
409-
remoteUrl?: string;
406+
remotePort: number;
407+
remoteUrl: string;
410408
videoFps?: number;
411409
videoType?: string;
412410
videoScale?: string;

lib/driver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import _ from 'lodash';
2020
import {LRUCache} from 'lru-cache';
2121
import EventEmitter from 'node:events';
2222
import path from 'node:path';
23-
import url from 'node:url';
2423
import {
2524
SUPPORTED_EXTENSIONS,
2625
SAFARI_BUNDLE_ID,
@@ -616,8 +615,9 @@ export class XCUITestDriver
616615
}
617616

618617
if (_.isString(caps.webDriverAgentUrl)) {
619-
const {protocol, host} = url.parse(caps.webDriverAgentUrl);
620-
if (_.isEmpty(protocol) || _.isEmpty(host)) {
618+
try {
619+
new URL(caps.webDriverAgentUrl);
620+
} catch {
621621
throw this.log.errorWithException(
622622
`'webDriverAgentUrl' capability is expected to contain a valid WebDriverAgent server URL. ` +
623623
`'${caps.webDriverAgentUrl}' is given instead`,

lib/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import B from 'bluebird';
55
import _ from 'lodash';
66
import os from 'node:os';
77
import path from 'node:path';
8-
import url from 'node:url';
98
import * as semver from 'semver';
109
import {exec} from 'teen_process';
1110
import {log} from './logger';
@@ -381,15 +380,23 @@ export async function removeAllSessionWebSocketHandlers(this: XCUITestDriver): P
381380
}
382381
}
383382

383+
const LOCALHOST_HOSTNAMES = [
384+
'localhost',
385+
'127.0.0.1',
386+
// WHATWG URL normalizes IPv6 hostnames with brackets and hex (e.g. ::ffff:127.0.0.1 -> ::ffff:7f00:1)
387+
'[::1]',
388+
'[::ffff:7f00:1]',
389+
];
390+
384391
/**
385392
* Returns true if the urlString is localhost
386393
* @param urlString
387394
* @returns Return true if the urlString is localhost
388395
*/
389396
export function isLocalHost(urlString: string): boolean {
390397
try {
391-
const hostname = url.parse(urlString).hostname as string;
392-
return ['localhost', '127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(hostname);
398+
const hostname = new URL(urlString).hostname;
399+
return LOCALHOST_HOSTNAMES.includes(hostname);
393400
} catch {
394401
log.warn(`'${urlString}' cannot be parsed as a valid URL`);
395402
}

0 commit comments

Comments
 (0)