fix(db): preserve child process spawn overloads

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-07-27 15:32:13 +00:00
1 parent bcec510631
commit 14df3c37e9
2 files changed
+37 -6

No files matched your search

@@ -1,8 +1,13 @@
import childProcess from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { ensureLinuxSharedLibraryAliases, mergeEmbeddedPostgresSpawnEnv } from "./embedded-postgres-native.js";
import {
ensureLinuxSharedLibraryAliases,
mergeEmbeddedPostgresSpawnEnv,
prepareEmbeddedPostgresNativeRuntime,
} from "./embedded-postgres-native.js";
describe("embedded Postgres native runtime", () => {
const tempDirs: string[] = [];
@@ -60,4 +65,16 @@ describe("embedded Postgres spawn environment", () => {
const options = { env: { ONLY: "value" } };
expect(mergeEmbeddedPostgresSpawnEnv("node", options)).toBe(options);
});
it("preserves the spawn options-only overload after installing the patch", async () => {
await prepareEmbeddedPostgresNativeRuntime();
const exitCode = await new Promise<number | null>((resolve, reject) => {
const child = childProcess.spawn(process.execPath, { stdio: "ignore" });
child.once("error", reject);
child.once("close", resolve);
});
expect(exitCode).toBe(0);
});
});
+19 -5
View File
@@ -18,12 +18,26 @@ export function mergeEmbeddedPostgresSpawnEnv(
function installEmbeddedPostgresSpawnPatch(): void {
if (embeddedPostgresSpawnPatchInstalled) return;
const originalSpawn = childProcess.spawn;
childProcess.spawn = ((command: string, args?: readonly string[], options?: object) =>
originalSpawn(
childProcess.spawn = ((
command: string,
argsOrOptions?: readonly string[] | childProcess.SpawnOptions,
options?: childProcess.SpawnOptions,
) => {
if (Array.isArray(argsOrOptions)) {
return originalSpawn(
command,
argsOrOptions,
mergeEmbeddedPostgresSpawnEnv(command, options) as childProcess.SpawnOptions,
);
}
return originalSpawn(
command,
args,
mergeEmbeddedPostgresSpawnEnv(command, options) as never,
)) as typeof childProcess.spawn;
mergeEmbeddedPostgresSpawnEnv(
command,
argsOrOptions as childProcess.SpawnOptions | undefined,
) as childProcess.SpawnOptions,
);
}) as typeof childProcess.spawn;
syncBuiltinESMExports();
embeddedPostgresSpawnPatchInstalled = true;
}