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:
Nikolay Vitkov 2024-04-24 12:57:02 +02:00 committed by GitHub
parent 943ade77b7
commit c9a5644c65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
}
}
}