0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-24 23:21:57 -05:00

Fix temporary file conflicts

This commit is contained in:
Chris Swithinbank 2024-03-05 11:00:06 +01:00
parent 985d3a0ec0
commit ef2156e41b
No known key found for this signature in database
GPG key ID: 52DB15DC07051619

View file

@ -182,7 +182,7 @@ export async function importBundledFile({
root: URL;
}): Promise<{ default?: unknown }> {
// Write it to disk, load it with native Node ESM, then delete the file.
const tmpFileUrl = new URL(`./db.timestamp-${Date.now()}.mjs`, root);
const tmpFileUrl = tempUrl(root);
await writeFile(tmpFileUrl, code, { encoding: 'utf8' });
try {
return await import(/* @vite-ignore */ tmpFileUrl.pathname);
@ -194,3 +194,9 @@ export async function importBundledFile({
}
}
}
/** Get a unique temporary file URL. Combines timestamp and a random ID to avoid conflicts. */
function tempUrl(root: URL) {
const uuid = Math.random().toFixed(10).slice(2);
return new URL(`./db.timestamp-${Date.now()}-${uuid}.mjs`, root);
}