|
12 | 12 | // EMITTED <Report>.routes.ts files unmodified and mount them. |
13 | 13 |
|
14 | 14 | import Fastify, { type FastifyInstance } from "fastify"; |
| 15 | +import { randomUUID } from "node:crypto"; |
15 | 16 | import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
16 | 17 | import { dirname, join } from "node:path"; |
17 | 18 | import { fileURLToPath, pathToFileURL } from "node:url"; |
@@ -161,17 +162,19 @@ export async function startSqliteReportServer( |
161 | 162 | async function openEngine(engine: SqliteReportEngine, tmp: string): Promise<EngineClient> { |
162 | 163 | if (engine === "d1") { |
163 | 164 | const local = await startLocalD1(); |
164 | | - (globalThis as { __reportD1?: unknown }).__reportD1 = local.d1; |
| 165 | + // One key per server, so two servers in flight never read each other's binding. |
| 166 | + const key = `__reportD1_${randomUUID().replaceAll("-", "")}`; |
| 167 | + (globalThis as Record<string, unknown>)[key] = local.d1; |
165 | 168 | writeFileSync(join(tmp, "db.ts"), ` |
166 | 169 | import { drizzle } from "drizzle-orm/d1"; |
167 | | -export const db = drizzle((globalThis as { __reportD1?: never }).__reportD1 as never); |
| 170 | +export const db = drizzle((globalThis as Record<string, never>)[${JSON.stringify(key)}]); |
168 | 171 | `, "utf8"); |
169 | 172 | return { |
170 | 173 | run: async (sql, params = []) => { |
171 | 174 | await local.d1.prepare(sql).bind(...params).all(); |
172 | 175 | }, |
173 | 176 | close: async () => { |
174 | | - delete (globalThis as { __reportD1?: unknown }).__reportD1; |
| 177 | + delete (globalThis as Record<string, unknown>)[key]; |
175 | 178 | await local.dispose(); |
176 | 179 | }, |
177 | 180 | }; |
|
0 commit comments