appium-geckodriver
Version:
Appium driver for Gecko-based browsers and web views
138 lines • 4.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatCapsForServer = formatCapsForServer;
exports.downloadToFile = downloadToFile;
exports.mkdirp = mkdirp;
exports.extractFileFromTarGz = extractFileFromTarGz;
exports.extractFileFromZip = extractFileFromZip;
const lodash_1 = __importDefault(require("lodash"));
const support_1 = require("appium/support");
const tar_stream_1 = __importDefault(require("tar-stream"));
const node_zlib_1 = __importDefault(require("node:zlib"));
const bluebird_1 = __importDefault(require("bluebird"));
const node_path_1 = __importDefault(require("node:path"));
const GECKO_CAP_PREFIXES = ['moz:'];
// https://www.w3.org/TR/webdriver/#capabilities
const STANDARD_CAPS = [
'browserVersion',
'platformName',
'acceptInsecureCerts',
'pageLoadStrategy',
'proxy',
'setWindowRect',
'timeouts',
'unhandledPromptBehavior',
'webSocketUrl',
];
/**
*
* @param {import('@appium/types').StringRecord} caps
* @returns {import('@appium/types').StringRecord}
*/
function formatCapsForServer(caps) {
const result = {};
if (caps.browserName) {
result.browserName = 'firefox';
}
for (const [name, value] of lodash_1.default.toPairs(caps)) {
if (GECKO_CAP_PREFIXES.some((prefix) => name.startsWith(prefix)) || STANDARD_CAPS.includes(name)) {
result[name] = value;
}
}
if (result.platformName) {
// Geckodriver only supports lowercase platform names
result.platformName = lodash_1.default.toLower(result.platformName);
}
return result;
}
/**
*
* @param {string} srcUrl
* @param {string} dstPath
* @returns {Promise<void>}
*/
async function downloadToFile(srcUrl, dstPath) {
await support_1.net.downloadFile(srcUrl, dstPath);
}
/**
*
* @param {string} p
* @returns {Promise<void>}
*/
async function mkdirp(p) {
await support_1.fs.mkdirp(p);
}
/**
*
* @param {string} srcAcrhive
* @param {string} fileToExtract
* @param {string} dstPath
* @returns {Promise<void>}
*/
async function extractFileFromTarGz(srcAcrhive, fileToExtract, dstPath) {
const chunks = [];
const extract = tar_stream_1.default.extract();
const extractPromise = new bluebird_1.default((resolve, reject) => {
extract.on('entry', (header, stream, next) => {
if (header.name === fileToExtract) {
stream.on('data', (chunk) => {
chunks.push(chunk);
});
}
stream.on('end', function () {
next();
});
stream.resume();
});
extract.once('error', reject);
extract.once('finish', async () => {
if (chunks.length) {
try {
await support_1.fs.writeFile(dstPath, Buffer.concat(chunks));
}
catch (e) {
return reject(e);
}
}
else {
return reject(new Error(`The file '${fileToExtract}' could not be found in the '${srcAcrhive}' archive`));
}
resolve();
});
});
support_1.fs.createReadStream(srcAcrhive)
.pipe(node_zlib_1.default.createGunzip())
.pipe(extract);
await extractPromise;
}
/**
*
* @param {string} srcAcrhive
* @param {string} fileToExtract
* @param {string} dstPath
* @returns {Promise<void>}
*/
async function extractFileFromZip(srcAcrhive, fileToExtract, dstPath) {
let didFindEntry = false;
await support_1.zip.readEntries(srcAcrhive, async ({ entry, extractEntryTo }) => {
if (didFindEntry || entry.fileName !== fileToExtract) {
return;
}
didFindEntry = true;
const tmpRoot = await support_1.tempDir.openDir();
try {
await extractEntryTo(tmpRoot);
await support_1.fs.mv(node_path_1.default.resolve(tmpRoot, entry.fileName), dstPath);
}
finally {
await support_1.fs.rimraf(tmpRoot);
}
});
if (!didFindEntry) {
throw new Error(`The file '${fileToExtract}' could not be found in the '${srcAcrhive}' archive`);
}
}
//# sourceMappingURL=utils.js.map