Skip to content

Commit

Permalink
fix(performance): speed up Firefox profile creation (#12320)
Browse files Browse the repository at this point in the history
Co-authored-by: browser-automation-bot <133232582+browser-automation-bot@users.noreply.github.com>
  • Loading branch information
Lightning00Blade and browser-automation-bot committed Apr 24, 2024
1 parent 943ade7 commit c9a5644
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/browsers/src/browser-data/firefox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit c9a5644

Please sign in to comment.