mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix(performance): speed up Firefox profile creation (#12320)
Co-authored-by: browser-automation-bot <133232582+browser-automation-bot@users.noreply.github.com>
This commit is contained in:
parent
943ade77b7
commit
c9a5644c65
@ -406,20 +406,30 @@ function defaultProfilePreferences(
|
||||
* @param profilePath - Firefox profile to write the preferences to.
|
||||
*/
|
||||
async function writePreferences(options: ProfileOptions): Promise<void> {
|
||||
const prefsPath = path.join(options.path, 'prefs.js');
|
||||
const lines = Object.entries(options.preferences).map(([key, value]) => {
|
||||
return `user_pref(${JSON.stringify(key)}, ${JSON.stringify(value)});`;
|
||||
});
|
||||
|
||||
await fs.promises.writeFile(
|
||||
path.join(options.path, 'user.js'),
|
||||
lines.join('\n')
|
||||
);
|
||||
|
||||
// Create a backup of the preferences file if it already exitsts.
|
||||
const prefsPath = path.join(options.path, 'prefs.js');
|
||||
if (fs.existsSync(prefsPath)) {
|
||||
const prefsBackupPath = path.join(options.path, 'prefs.js.puppeteer');
|
||||
await fs.promises.copyFile(prefsPath, prefsBackupPath);
|
||||
// Use allSettled to prevent corruption
|
||||
const result = await Promise.allSettled([
|
||||
fs.promises.writeFile(path.join(options.path, 'user.js'), lines.join('\n')),
|
||||
// Create a backup of the preferences file if it already exitsts.
|
||||
fs.promises.access(prefsPath, fs.constants.F_OK).then(
|
||||
async () => {
|
||||
await fs.promises.copyFile(
|
||||
prefsPath,
|
||||
path.join(options.path, 'prefs.js.puppeteer')
|
||||
);
|
||||
},
|
||||
// Swallow only if file does not exist
|
||||
() => {}
|
||||
),
|
||||
]);
|
||||
for (const command of result) {
|
||||
if (command.status === 'rejected') {
|
||||
throw command.reason;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user