Skip to content

Commit

Permalink
Fix temporary file conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Mar 5, 2024
1 parent 985d3a0 commit ef2156e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/db/src/core/load-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

0 comments on commit ef2156e

Please sign in to comment.