Files
httptoolkit/patches/server/0002-bun-compat-tmp-import.patch
T
shihaam 6768b0a5b2
Build & Release binary / build (push) Successful in 26m20s
build works now
2026-08-22 16:14:56 +05:00

34 lines
1.4 KiB
Diff

Bun's bundler mis-compiles `import * as tmp from 'tmp'`.
tmp's entrypoint ends with `Object.defineProperty(module.exports, 'tmpdir', {...})`.
When bun builds an ESM namespace object for that CJS module it emits a reference
to `exports_tmp` without ever declaring the binding, so the compiled binary dies
on startup with `ReferenceError: exports_tmp is not defined`.
Importing it as CJS instead keeps bun on its `__commonJS` path, which handles the
`defineProperty` pattern correctly. Only the `tmp.file` value and the `Options`
type are used here, so the switch is behaviour-preserving.
diff --git a/src/util/fs.ts b/src/util/fs.ts
index 21c99ef..0d2224f 100644
--- a/src/util/fs.ts
+++ b/src/util/fs.ts
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
-import * as tmp from 'tmp';
+import type { Options as TmpOptions } from 'tmp';
+const tmp: typeof import('tmp') = require('tmp');
import { lookpath } from 'lookpath';
import { isErrorLike } from '@httptoolkit/util';
@@ -62,7 +63,7 @@ export const resolveCommandPath = (path: string): Promise<string | undefined> =>
export const commandExists = (path: string): Promise<boolean> =>
resolveCommandPath(path).then((result) => result !== undefined);
-export const createTmp = (options: tmp.Options = {}) => new Promise<{
+export const createTmp = (options: TmpOptions = {}) => new Promise<{
path: string,
fd: number,
cleanupCallback: () => void