Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 102 additions & 6 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5289,7 +5289,12 @@ declare module "bun" {
options: udp.ConnectSocketOptions<DataBinaryType>,
): Promise<udp.ConnectedSocket<DataBinaryType>>;

namespace SpawnOptions {
/**
* @deprecated use {@link Bun.Spawn} instead
*/
export import SpawnOptions = Spawn;

namespace Spawn {
/**
* Option for stdout/stderr
*/
Expand Down Expand Up @@ -5320,14 +5325,35 @@ declare module "bun" {
| Response
| Request;

interface OptionsObject<In extends Writable, Out extends Readable, Err extends Readable> {
/**
* @deprecated use BaseOptions or the specific options for the specific {@link spawn} or {@link spawnSync} usage
*/
type OptionsObject<In extends Writable, Out extends Readable, Err extends Readable> = BaseOptions<In, Out, Err>;

interface BaseOptions<In extends Writable, Out extends Readable, Err extends Readable> {
/**
* The current working directory of the process
*
* Defaults to `process.cwd()`
*/
cwd?: string;

/**
* Run the child in a separate process group, detached from the parent.
*
* - POSIX: calls `setsid()` so the child starts a new session and becomes
* the process group leader. It can outlive the parent and receive
* signals independently of the parent’s terminal/process group.
* - Windows: sets `UV_PROCESS_DETACHED`, allowing the child to outlive
* the parent and receive signals independently.
*
* Note: stdio may keep the parent process alive. Pass `stdio: ["ignore",
* "ignore", "ignore"]` to the spawn constructor to prevent this.
*
* @default false
*/
detached?: boolean;

/**
* The environment variables of the process
*
Expand Down Expand Up @@ -5430,6 +5456,48 @@ declare module "bun" {
error?: ErrorLike,
): void | Promise<void>;

/**
* Called exactly once when the IPC channel between the parent and this
* subprocess is closed. After this runs, no further IPC messages will be
* delivered.
*
* When it fires:
* - The child called `process.disconnect()` or the parent called
* `subprocess.disconnect()`.
* - The child exited for any reason (normal exit or due to a signal like
* `SIGILL`, `SIGKILL`, etc.).
* - The child replaced itself with a program that does not support Bun
* IPC.
*
* Notes:
* - This callback indicates that the pipe is closed; it is not an error
* by itself. Use {@link onExit} or {@link Subprocess.exited} to
* determine why the process ended.
* - It may occur before or after {@link onExit} depending on timing; do
* not rely on ordering. Typically, if you or the child call
* `disconnect()` first, this fires before {@link onExit}; if the
* process exits without an explicit disconnect, either may happen
* first.
* - Only runs when {@link ipc} is enabled and runs at most once per
* subprocess.
* - If the child becomes a zombie (exited but not yet reaped), the IPC is
* already closed, and this callback will fire (or may already have
* fired).
Comment thread
alii marked this conversation as resolved.
Resolved
*
* @example
*
* ```ts
* const subprocess = spawn({
* cmd: ["echo", "hello"],
* ipc: (message) => console.log(message),
* onDisconnect: () => {
* console.log("IPC channel disconnected");
* },
* });
* ```
*/
onDisconnect?(): void | Promise<void>;

Comment thread
alii marked this conversation as resolved.
Resolved
/**
* When specified, Bun will open an IPC channel to the subprocess. The passed callback is called for
* incoming messages, and `subprocess.send` can send messages to the subprocess. Messages are serialized
Expand Down Expand Up @@ -5549,6 +5617,34 @@ declare module "bun" {
maxBuffer?: number;
}

interface SpawnSyncOptions<In extends Writable, Out extends Readable, Err extends Readable>
extends BaseOptions<In, Out, Err> {}

interface SpawnOptions<In extends Writable, Out extends Readable, Err extends Readable>
extends BaseOptions<In, Out, Err> {
/**
* If true, stdout and stderr pipes will not automatically start reading
* data. Reading will only begin when you access the `stdout` or `stderr`
* properties.
*
* This can improve performance when you don't need to read output
* immediately.
Comment thread
alii marked this conversation as resolved.
Resolved
*
* @default false
*
* @example
* ```ts
* const subprocess = Bun.spawn({
* cmd: ["echo", "hello"],
* lazy: true, // Don't start reading stdout until accessed
* });
* // stdout reading hasn't started yet
* await subprocess.stdout.text(); // Now reading starts
* ```
*/
lazy?: boolean;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

type ReadableToIO<X extends Readable> = X extends "pipe" | undefined
? ReadableStream<Uint8Array<ArrayBuffer>>
: X extends BunFile | ArrayBufferView | number
Expand Down Expand Up @@ -5806,7 +5902,7 @@ declare module "bun" {
const Out extends SpawnOptions.Readable = "pipe",
const Err extends SpawnOptions.Readable = "inherit",
>(
options: SpawnOptions.OptionsObject<In, Out, Err> & {
options: SpawnOptions.SpawnOptions<In, Out, Err> & {
/**
* The command to run
*
Expand Down Expand Up @@ -5856,7 +5952,7 @@ declare module "bun" {
* ```
*/
cmds: string[],
options?: SpawnOptions.OptionsObject<In, Out, Err>,
options?: SpawnOptions.SpawnOptions<In, Out, Err>,
): Subprocess<In, Out, Err>;

/**
Expand All @@ -5878,7 +5974,7 @@ declare module "bun" {
const Out extends SpawnOptions.Readable = "pipe",
const Err extends SpawnOptions.Readable = "pipe",
>(
options: SpawnOptions.OptionsObject<In, Out, Err> & {
options: SpawnOptions.SpawnSyncOptions<In, Out, Err> & {
/**
* The command to run
*
Expand Down Expand Up @@ -5929,7 +6025,7 @@ declare module "bun" {
* ```
*/
cmds: string[],
options?: SpawnOptions.OptionsObject<In, Out, Err>,
options?: SpawnOptions.SpawnSyncOptions<In, Out, Err>,
): SyncSubprocess<Out, Err>;

/** Utility type for any process from {@link Bun.spawn()} with both stdout and stderr set to `"pipe"` */
Expand Down
4 changes: 2 additions & 2 deletions packages/bun-types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ declare module "bun:test" {
*/
each<T extends Readonly<[any, ...any[]]>>(table: readonly T[]): Describe<[...T]>;
each<T extends any[]>(table: readonly T[]): Describe<[...T]>;
each<T>(table: T[]): Describe<[T]>;
each<const T>(table: T[]): Describe<[T]>;
}
/**
* Describes a group of related tests.
Expand Down Expand Up @@ -552,7 +552,7 @@ declare module "bun:test" {
*/
each<T extends Readonly<[unknown, ...unknown[]]>>(table: readonly T[]): Test<T>;
each<T extends unknown[]>(table: readonly T[]): Test<T>;
each<T>(table: T[]): Test<[T]>;
each<const T>(table: T[]): Test<[T]>;
}
/**
* Runs a test.
Expand Down
42 changes: 42 additions & 0 deletions test/integration/bun-types/fixture/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,45 @@ tsd.expectAssignable<NullSubprocess>(Bun.spawn([], { stdio: ["ignore", "inherit"
tsd.expectAssignable<NullSubprocess>(Bun.spawn([], { stdio: [null, null, null] }));

tsd.expectAssignable<SyncSubprocess<Bun.SpawnOptions.Readable, Bun.SpawnOptions.Readable>>(Bun.spawnSync([], {}));

// Lazy option types (async only)
{
// valid: lazy usable with async spawn
const p1 = Bun.spawn(["echo", "hello"], {
stdout: "pipe",
stderr: "pipe",
lazy: true,
});
tsd.expectType(p1.stdout).is<ReadableStream<Uint8Array<ArrayBuffer>>>();
}

{
// valid: lazy false is also allowed
const p2 = Bun.spawn(["echo", "hello"], {
stdout: "pipe",
stderr: "pipe",
lazy: false,
});
tsd.expectType(p2.stderr).is<ReadableStream<Uint8Array<ArrayBuffer>>>();
}

{
// invalid: lazy is not supported in spawnSync
Bun.spawnSync(["echo", "hello"], {
stdout: "pipe",
stderr: "pipe",
// @ts-expect-error lazy applies only to async spawn
lazy: true,
});
}

{
// invalid: lazy is not supported in spawnSync (object overload)
// @ts-expect-error lazy applies only to async spawn
Bun.spawnSync({
cmd: ["echo", "hello"],
stdout: "pipe",
stderr: "pipe",
lazy: true,
});
}
4 changes: 4 additions & 0 deletions test/integration/bun-types/fixture/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ describe("bun:test", () => {
});
});

test.each([1, 2, 3])("test.each", a => {
expectType<1 | 2 | 3>(a);
});

// inference should work when data is passed directly in
test.each([
["a", true, 5],
Expand Down
Loading