bun-types 1.3.4 → 1.3.5-canary.20251218T140753

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bun.d.ts CHANGED
@@ -1740,9 +1740,9 @@ declare module "bun" {
1740
1740
  * @default "esm"
1741
1741
  */
1742
1742
  format?: /**
1743
- * ECMAScript Module format
1744
- */
1745
- | "esm"
1743
+ * ECMAScript Module format
1744
+ */
1745
+ | "esm"
1746
1746
  /**
1747
1747
  * CommonJS format
1748
1748
  * **Experimental**
@@ -1891,6 +1891,24 @@ declare module "bun" {
1891
1891
  */
1892
1892
  drop?: string[];
1893
1893
 
1894
+ /**
1895
+ * Enable feature flags for dead-code elimination via `import { feature } from "bun:bundle"`.
1896
+ *
1897
+ * When `feature("FLAG_NAME")` is called, it returns `true` if FLAG_NAME is in this array,
1898
+ * or `false` otherwise. This enables static dead-code elimination at bundle time.
1899
+ *
1900
+ * Equivalent to the CLI `--feature` flag.
1901
+ *
1902
+ * @example
1903
+ * ```ts
1904
+ * await Bun.build({
1905
+ * entrypoints: ['./src/index.ts'],
1906
+ * features: ['FEATURE_A', 'FEATURE_B'],
1907
+ * });
1908
+ * ```
1909
+ */
1910
+ features?: string[];
1911
+
1894
1912
  /**
1895
1913
  * - When set to `true`, the returned promise rejects with an AggregateError when a build failure happens.
1896
1914
  * - When set to `false`, returns a {@link BuildOutput} with `{success: false}`
@@ -3316,10 +3334,10 @@ declare module "bun" {
3316
3334
  function color(
3317
3335
  input: ColorInput,
3318
3336
  outputFormat?: /**
3319
- * True color ANSI color string, for use in terminals
3320
- * @example \x1b[38;2;100;200;200m
3321
- */
3322
- | "ansi"
3337
+ * True color ANSI color string, for use in terminals
3338
+ * @example \x1b[38;2;100;200;200m
3339
+ */
3340
+ | "ansi"
3323
3341
  | "ansi-16"
3324
3342
  | "ansi-16m"
3325
3343
  /**
@@ -3887,6 +3905,9 @@ declare module "bun" {
3887
3905
  static readonly byteLength: 32;
3888
3906
  }
3889
3907
 
3908
+ /** Extends the standard web formats with `brotli` and `zstd` support. */
3909
+ type CompressionFormat = "gzip" | "deflate" | "deflate-raw" | "brotli" | "zstd";
3910
+
3890
3911
  /** Compression options for `Bun.deflateSync` and `Bun.gzipSync` */
3891
3912
  interface ZlibCompressionOptions {
3892
3913
  /**
@@ -5650,17 +5671,11 @@ declare module "bun" {
5650
5671
  maxBuffer?: number;
5651
5672
  }
5652
5673
 
5653
- interface SpawnSyncOptions<In extends Writable, Out extends Readable, Err extends Readable> extends BaseOptions<
5654
- In,
5655
- Out,
5656
- Err
5657
- > {}
5658
-
5659
- interface SpawnOptions<In extends Writable, Out extends Readable, Err extends Readable> extends BaseOptions<
5660
- In,
5661
- Out,
5662
- Err
5663
- > {
5674
+ interface SpawnSyncOptions<In extends Writable, Out extends Readable, Err extends Readable>
5675
+ extends BaseOptions<In, Out, Err> {}
5676
+
5677
+ interface SpawnOptions<In extends Writable, Out extends Readable, Err extends Readable>
5678
+ extends BaseOptions<In, Out, Err> {
5664
5679
  /**
5665
5680
  * If true, stdout and stderr pipes will not automatically start reading
5666
5681
  * data. Reading will only begin when you access the `stdout` or `stderr`
@@ -5682,6 +5697,44 @@ declare module "bun" {
5682
5697
  * ```
5683
5698
  */
5684
5699
  lazy?: boolean;
5700
+
5701
+ /**
5702
+ * Spawn the subprocess with a pseudo-terminal (PTY) attached.
5703
+ *
5704
+ * When this option is provided:
5705
+ * - `stdin`, `stdout`, and `stderr` are all connected to the terminal
5706
+ * - The subprocess sees itself running in a real terminal (`isTTY = true`)
5707
+ * - Access the terminal via `subprocess.terminal`
5708
+ * - `subprocess.stdin`, `subprocess.stdout`, `subprocess.stderr` return `null`
5709
+ *
5710
+ * Only available on POSIX systems (Linux, macOS).
5711
+ *
5712
+ * @example
5713
+ * ```ts
5714
+ * const proc = Bun.spawn(["bash"], {
5715
+ * terminal: {
5716
+ * cols: 80,
5717
+ * rows: 24,
5718
+ * data: (term, data) => console.log(data.toString()),
5719
+ * },
5720
+ * });
5721
+ *
5722
+ * proc.terminal.write("echo hello\n");
5723
+ * await proc.exited;
5724
+ * proc.terminal.close();
5725
+ * ```
5726
+ *
5727
+ * You can also pass an existing `Terminal` object for reuse across multiple spawns:
5728
+ * ```ts
5729
+ * const terminal = new Bun.Terminal({ ... });
5730
+ * const proc1 = Bun.spawn(["echo", "first"], { terminal });
5731
+ * await proc1.exited;
5732
+ * const proc2 = Bun.spawn(["echo", "second"], { terminal });
5733
+ * await proc2.exited;
5734
+ * terminal.close();
5735
+ * ```
5736
+ */
5737
+ terminal?: TerminalOptions | Terminal;
5685
5738
  }
5686
5739
 
5687
5740
  type ReadableToIO<X extends Readable> = X extends "pipe" | undefined
@@ -5796,6 +5849,24 @@ declare module "bun" {
5796
5849
  readonly stdout: SpawnOptions.ReadableToIO<Out>;
5797
5850
  readonly stderr: SpawnOptions.ReadableToIO<Err>;
5798
5851
 
5852
+ /**
5853
+ * The terminal attached to this subprocess, if spawned with the `terminal` option.
5854
+ * Returns `undefined` if no terminal was attached.
5855
+ *
5856
+ * When a terminal is attached, `stdin`, `stdout`, and `stderr` return `null`.
5857
+ * Use `terminal.write()` and the `data` callback instead.
5858
+ *
5859
+ * @example
5860
+ * ```ts
5861
+ * const proc = Bun.spawn(["bash"], {
5862
+ * terminal: { data: (term, data) => console.log(data.toString()) },
5863
+ * });
5864
+ *
5865
+ * proc.terminal?.write("echo hello\n");
5866
+ * ```
5867
+ */
5868
+ readonly terminal: Terminal | undefined;
5869
+
5799
5870
  /**
5800
5871
  * Access extra file descriptors passed to the `stdio` option in the options object.
5801
5872
  */
@@ -6087,6 +6158,154 @@ declare module "bun" {
6087
6158
  "ignore" | "inherit" | null | undefined
6088
6159
  >;
6089
6160
 
6161
+ /**
6162
+ * Options for creating a pseudo-terminal (PTY).
6163
+ */
6164
+ interface TerminalOptions {
6165
+ /**
6166
+ * Number of columns for the terminal.
6167
+ * @default 80
6168
+ */
6169
+ cols?: number;
6170
+ /**
6171
+ * Number of rows for the terminal.
6172
+ * @default 24
6173
+ */
6174
+ rows?: number;
6175
+ /**
6176
+ * Terminal name (e.g., "xterm-256color").
6177
+ * @default "xterm-256color"
6178
+ */
6179
+ name?: string;
6180
+ /**
6181
+ * Callback invoked when data is received from the terminal.
6182
+ * @param terminal The terminal instance
6183
+ * @param data The data received as a Uint8Array
6184
+ */
6185
+ data?: (terminal: Terminal, data: Uint8Array<ArrayBuffer>) => void;
6186
+ /**
6187
+ * Callback invoked when the PTY stream closes (EOF or read error).
6188
+ * Note: exitCode is a PTY lifecycle status (0=clean EOF, 1=error), NOT the subprocess exit code.
6189
+ * Use Subprocess.exited or onExit callback for actual process exit information.
6190
+ * @param terminal The terminal instance
6191
+ * @param exitCode PTY lifecycle status (0 for EOF, 1 for error)
6192
+ * @param signal Reserved for future signal reporting, currently null
6193
+ */
6194
+ exit?: (terminal: Terminal, exitCode: number, signal: string | null) => void;
6195
+ /**
6196
+ * Callback invoked when the terminal is ready to receive more data.
6197
+ * @param terminal The terminal instance
6198
+ */
6199
+ drain?: (terminal: Terminal) => void;
6200
+ }
6201
+
6202
+ /**
6203
+ * A pseudo-terminal (PTY) that can be used to spawn interactive terminal programs.
6204
+ *
6205
+ * @example
6206
+ * ```ts
6207
+ * await using terminal = new Bun.Terminal({
6208
+ * cols: 80,
6209
+ * rows: 24,
6210
+ * data(term, data) {
6211
+ * console.log("Received:", new TextDecoder().decode(data));
6212
+ * },
6213
+ * });
6214
+ *
6215
+ * // Spawn a shell connected to the PTY
6216
+ * const proc = Bun.spawn(["bash"], { terminal });
6217
+ *
6218
+ * // Write to the terminal
6219
+ * terminal.write("echo hello\n");
6220
+ *
6221
+ * // Wait for process to exit
6222
+ * await proc.exited;
6223
+ *
6224
+ * // Terminal is closed automatically by `await using`
6225
+ * ```
6226
+ */
6227
+ class Terminal implements AsyncDisposable {
6228
+ constructor(options: TerminalOptions);
6229
+
6230
+ /**
6231
+ * Whether the terminal is closed.
6232
+ */
6233
+ readonly closed: boolean;
6234
+
6235
+ /**
6236
+ * Write data to the terminal.
6237
+ * @param data The data to write (string or BufferSource)
6238
+ * @returns The number of bytes written
6239
+ */
6240
+ write(data: string | BufferSource): number;
6241
+
6242
+ /**
6243
+ * Resize the terminal.
6244
+ * @param cols New number of columns
6245
+ * @param rows New number of rows
6246
+ */
6247
+ resize(cols: number, rows: number): void;
6248
+
6249
+ /**
6250
+ * Set raw mode on the terminal.
6251
+ * In raw mode, input is passed directly without processing.
6252
+ * @param enabled Whether to enable raw mode
6253
+ */
6254
+ setRawMode(enabled: boolean): void;
6255
+
6256
+ /**
6257
+ * Reference the terminal to keep the event loop alive.
6258
+ */
6259
+ ref(): void;
6260
+
6261
+ /**
6262
+ * Unreference the terminal to allow the event loop to exit.
6263
+ */
6264
+ unref(): void;
6265
+
6266
+ /**
6267
+ * Close the terminal.
6268
+ */
6269
+ close(): void;
6270
+
6271
+ /**
6272
+ * Async dispose for use with `await using`.
6273
+ */
6274
+ [Symbol.asyncDispose](): Promise<void>;
6275
+
6276
+ /**
6277
+ * Terminal input flags (c_iflag from termios).
6278
+ * Controls input processing behavior like ICRNL, IXON, etc.
6279
+ * Returns 0 if terminal is closed.
6280
+ * Setting returns true on success, false on failure.
6281
+ */
6282
+ inputFlags: number;
6283
+
6284
+ /**
6285
+ * Terminal output flags (c_oflag from termios).
6286
+ * Controls output processing behavior like OPOST, ONLCR, etc.
6287
+ * Returns 0 if terminal is closed.
6288
+ * Setting returns true on success, false on failure.
6289
+ */
6290
+ outputFlags: number;
6291
+
6292
+ /**
6293
+ * Terminal local flags (c_lflag from termios).
6294
+ * Controls local processing like ICANON, ECHO, ISIG, etc.
6295
+ * Returns 0 if terminal is closed.
6296
+ * Setting returns true on success, false on failure.
6297
+ */
6298
+ localFlags: number;
6299
+
6300
+ /**
6301
+ * Terminal control flags (c_cflag from termios).
6302
+ * Controls hardware characteristics like CSIZE, PARENB, etc.
6303
+ * Returns 0 if terminal is closed.
6304
+ * Setting returns true on success, false on failure.
6305
+ */
6306
+ controlFlags: number;
6307
+ }
6308
+
6090
6309
  // Blocked on https://github.com/oven-sh/bun/issues/8329
6091
6310
  // /**
6092
6311
  // *
package/bundle.d.ts ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The `bun:bundle` module provides compile-time utilities for dead-code elimination.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { feature } from "bun:bundle";
7
+ *
8
+ * if (feature("SUPER_SECRET")) {
9
+ * console.log("Secret feature enabled!");
10
+ * } else {
11
+ * console.log("Normal mode");
12
+ * }
13
+ * ```
14
+ *
15
+ * Enable feature flags via CLI:
16
+ * ```bash
17
+ * # During build
18
+ * bun build --feature=SUPER_SECRET index.ts
19
+ *
20
+ * # At runtime
21
+ * bun run --feature=SUPER_SECRET index.ts
22
+ *
23
+ * # In tests
24
+ * bun test --feature=SUPER_SECRET
25
+ * ```
26
+ *
27
+ * @module bun:bundle
28
+ */
29
+ declare module "bun:bundle" {
30
+ /**
31
+ * Registry for type-safe feature flags.
32
+ *
33
+ * Augment this interface to get autocomplete and type checking for your feature flags:
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * // env.d.ts
38
+ * declare module "bun:bundle" {
39
+ * interface Registry {
40
+ * features: "DEBUG" | "PREMIUM" | "BETA";
41
+ * }
42
+ * }
43
+ * ```
44
+ *
45
+ * Now `feature()` only accepts `"DEBUG"`, `"PREMIUM"`, or `"BETA"`:
46
+ * ```ts
47
+ * feature("DEBUG"); // OK
48
+ * feature("TYPO"); // Type error
49
+ * ```
50
+ */
51
+ interface Registry {}
52
+
53
+ /**
54
+ * Check if a feature flag is enabled at compile time.
55
+ *
56
+ * This function is replaced with a boolean literal (`true` or `false`) at bundle time,
57
+ * enabling dead-code elimination. The bundler will remove unreachable branches.
58
+ *
59
+ * @param flag - The name of the feature flag to check
60
+ * @returns `true` if the flag was passed via `--feature=FLAG`, `false` otherwise
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import { feature } from "bun:bundle";
65
+ *
66
+ * // With --feature=DEBUG, this becomes: if (true) { ... }
67
+ * // Without --feature=DEBUG, this becomes: if (false) { ... }
68
+ * if (feature("DEBUG")) {
69
+ * console.log("Debug mode enabled");
70
+ * }
71
+ * ```
72
+ */
73
+ function feature(flag: Registry extends { features: infer Features extends string } ? Features : string): boolean;
74
+ }
@@ -65,6 +65,7 @@ In Bun's CLI, simple boolean flags like `--minify` do not accept an argument. Ot
65
65
  | `--chunk-names` | `--chunk-naming` | Renamed for consistency with naming in JS API |
66
66
  | `--color` | n/a | Always enabled |
67
67
  | `--drop` | `--drop` | |
68
+ | n/a | `--feature` | Bun-specific. Enable feature flags for compile-time dead-code elimination via `import { feature } from "bun:bundle"` |
68
69
  | `--entry-names` | `--entry-naming` | Renamed for consistency with naming in JS API |
69
70
  | `--global-name` | n/a | Not applicable, Bun does not support `iife` output at this time |
70
71
  | `--ignore-annotations` | `--ignore-dce-annotations` | |