appium-xcuitest-driver
Version:
Appium driver for iOS using XCUITest for backend
98 lines • 3.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPIDsListeningOnPort = getPIDsListeningOnPort;
exports.encodeBase64OrUpload = encodeBase64OrUpload;
exports.isLocalHost = isLocalHost;
const node_net_1 = require("node:net");
const support_1 = require("appium/support");
const asyncbox_1 = require("asyncbox");
const lodash_1 = __importDefault(require("lodash"));
const teen_process_1 = require("teen_process");
const logger_1 = require("../logger");
/**
* Get the IDs of processes listening on the particular system port.
* It is also possible to apply additional filtering based on the
* process command line.
*/
async function getPIDsListeningOnPort(port, filteringFunc = null) {
const result = [];
try {
// This only works since Mac OS X El Capitan
const { stdout } = await (0, teen_process_1.exec)('lsof', ['-ti', `tcp:${port}`]);
result.push(...stdout.trim().split(/\n+/));
}
catch {
return result;
}
if (!lodash_1.default.isFunction(filteringFunc)) {
return result;
}
return await (0, asyncbox_1.asyncfilter)(result, async (x) => {
const { stdout } = await (0, teen_process_1.exec)('ps', ['-p', x, '-o', 'command']);
return await filteringFunc(stdout);
});
}
/**
* Encodes the given local file to base64 and returns the resulting string
* or uploads it to a remote server using http/https or ftp protocols
* if `remotePath` is set
*/
async function encodeBase64OrUpload(localPath, remotePath = null, uploadOptions = {}) {
if (!(await support_1.fs.exists(localPath))) {
throw logger_1.log.errorWithException(`The file at '${localPath}' does not exist or is not accessible`);
}
if (lodash_1.default.isEmpty(remotePath)) {
const { size } = await support_1.fs.stat(localPath);
logger_1.log.debug(`The size of the file is ${support_1.util.toReadableSizeString(size)}`);
return (await support_1.util.toInMemoryBase64(localPath)).toString();
}
const { user, pass, method, headers, fileFieldName, formFields } = uploadOptions;
const options = {
method: method ?? 'PUT',
headers,
fileFieldName,
formFields,
};
if (user && pass) {
options.auth = { user, pass };
}
await support_1.net.uploadFile(localPath, remotePath, options);
return '';
}
const LOCALHOST_HOSTNAMES = [
'localhost',
'127.0.0.1',
// IPv6 loopback in the form WHATWG URL uses in `hostname` (no `[`/`]` delimiters), e.g. `::1`,
// and IPv4-mapped 127.0.0.1 as serialized by the URL parser (e.g. `::ffff:7f00:1`).
'::1',
'::ffff:7f00:1',
];
/** Returns true if the given URL host resolves to localhost. */
function isLocalHost(urlString) {
const hostname = canonicalHostnameForLocalhost(urlString);
return Boolean(hostname && LOCALHOST_HOSTNAMES.includes(hostname));
}
/**
* `URL#hostname` for IPv6 may be unbracketed (`::1`) or bracketed (`[::1]`) depending on the
* runtime. If the host is bracketed and the inner value is IPv6, strip brackets so we can compare
* against unbracketed literals in {@link LOCALHOST_HOSTNAMES}.
*/
function canonicalHostnameForLocalhost(urlString) {
try {
const { hostname } = new URL(urlString);
if (hostname.startsWith('[') && hostname.endsWith(']') && hostname.length > 2) {
const inner = hostname.slice(1, -1);
if ((0, node_net_1.isIPv6)(inner)) {
return inner;
}
}
return hostname;
}
catch {
return null;
}
}
//# sourceMappingURL=network.js.map