Node updated. Some todos.
This commit is contained in:
50
Scripts/node_modules/@puppeteer/browsers/lib/cjs/Cache.js
generated
vendored
50
Scripts/node_modules/@puppeteer/browsers/lib/cjs/Cache.js
generated
vendored
@ -12,8 +12,10 @@ exports.Cache = exports.InstalledBrowser = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const os_1 = __importDefault(require("os"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const debug_1 = __importDefault(require("debug"));
|
||||
const browser_data_js_1 = require("./browser-data/browser-data.js");
|
||||
const detectPlatform_js_1 = require("./detectPlatform.js");
|
||||
const debugCache = (0, debug_1.default)('puppeteer:browsers:cache');
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
@ -44,6 +46,12 @@ class InstalledBrowser {
|
||||
get path() {
|
||||
return this.#cache.installationDir(this.browser, this.platform, this.buildId);
|
||||
}
|
||||
readMetadata() {
|
||||
return this.#cache.readMetadata(this.browser);
|
||||
}
|
||||
writeMetadata(metadata) {
|
||||
this.#cache.writeMetadata(this.browser, metadata);
|
||||
}
|
||||
}
|
||||
exports.InstalledBrowser = InstalledBrowser;
|
||||
/**
|
||||
@ -74,6 +82,35 @@ class Cache {
|
||||
browserRoot(browser) {
|
||||
return path_1.default.join(this.#rootDir, browser);
|
||||
}
|
||||
metadataFile(browser) {
|
||||
return path_1.default.join(this.browserRoot(browser), '.metadata');
|
||||
}
|
||||
readMetadata(browser) {
|
||||
const metatadaPath = this.metadataFile(browser);
|
||||
if (!fs_1.default.existsSync(metatadaPath)) {
|
||||
return { aliases: {} };
|
||||
}
|
||||
// TODO: add type-safe parsing.
|
||||
const data = JSON.parse(fs_1.default.readFileSync(metatadaPath, 'utf8'));
|
||||
if (typeof data !== 'object') {
|
||||
throw new Error('.metadata is not an object');
|
||||
}
|
||||
return data;
|
||||
}
|
||||
writeMetadata(browser, metadata) {
|
||||
const metatadaPath = this.metadataFile(browser);
|
||||
fs_1.default.mkdirSync(path_1.default.dirname(metatadaPath), { recursive: true });
|
||||
fs_1.default.writeFileSync(metatadaPath, JSON.stringify(metadata, null, 2));
|
||||
}
|
||||
resolveAlias(browser, alias) {
|
||||
const metadata = this.readMetadata(browser);
|
||||
if (alias === 'latest') {
|
||||
return Object.values(metadata.aliases || {})
|
||||
.sort((0, browser_data_js_1.getVersionComparator)(browser))
|
||||
.at(-1);
|
||||
}
|
||||
return metadata.aliases[alias];
|
||||
}
|
||||
installationDir(browser, platform, buildId) {
|
||||
return path_1.default.join(this.browserRoot(browser), `${platform}-${buildId}`);
|
||||
}
|
||||
@ -86,6 +123,12 @@ class Cache {
|
||||
});
|
||||
}
|
||||
uninstall(browser, platform, buildId) {
|
||||
const metadata = this.readMetadata(browser);
|
||||
for (const alias of Object.keys(metadata.aliases)) {
|
||||
if (metadata.aliases[alias] === buildId) {
|
||||
delete metadata.aliases[alias];
|
||||
}
|
||||
}
|
||||
fs_1.default.rmSync(this.installationDir(browser, platform, buildId), {
|
||||
force: true,
|
||||
recursive: true,
|
||||
@ -121,6 +164,13 @@ class Cache {
|
||||
if (!options.platform) {
|
||||
throw new Error(`Cannot download a binary for the provided platform: ${os_1.default.platform()} (${os_1.default.arch()})`);
|
||||
}
|
||||
try {
|
||||
options.buildId =
|
||||
this.resolveAlias(options.browser, options.buildId) ?? options.buildId;
|
||||
}
|
||||
catch {
|
||||
debugCache('could not read .metadata file for the browser');
|
||||
}
|
||||
const installationDir = this.installationDir(options.browser, options.platform, options.buildId);
|
||||
return path_1.default.join(installationDir, browser_data_js_1.executablePathByBrowser[options.browser](options.platform, options.buildId));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user