From ef2156e41bd08919a64a799e6b9bcdc5a3f99e3a Mon Sep 17 00:00:00 2001 From: Chris Swithinbank Date: Tue, 5 Mar 2024 11:00:06 +0100 Subject: [PATCH] Fix temporary file conflicts --- packages/db/src/core/load-file.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/db/src/core/load-file.ts b/packages/db/src/core/load-file.ts index 7150d3183c..b6500118f0 100644 --- a/packages/db/src/core/load-file.ts +++ b/packages/db/src/core/load-file.ts @@ -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); +}