bun-types 1.2.11-canary.20250417T140614 → 1.2.11-canary.20250419T140546

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
@@ -27,7 +27,7 @@ declare module "bun" {
27
27
  | ReadableStreamDefaultReadValueResult<T>
28
28
  | ReadableStreamDefaultReadDoneResult;
29
29
  type ReadableStreamReader<T> = ReadableStreamDefaultReader<T>;
30
- type Transferable = ArrayBuffer | import("worker_threads").MessagePort;
30
+ type Transferable = ArrayBuffer | MessagePort;
31
31
  type MessageEventSource = Bun.__internal.UseLibDomIfAvailable<"MessageEventSource", undefined>;
32
32
  type Encoding = "utf-8" | "windows-1252" | "utf-16";
33
33
  type UncaughtExceptionOrigin = "uncaughtException" | "unhandledRejection";
@@ -46,9 +46,6 @@ declare module "bun" {
46
46
  type EventListenerOrEventListenerObject = EventListener | EventListenerObject;
47
47
  type BlobOrStringOrBuffer = string | NodeJS.TypedArray | ArrayBufferLike | Blob;
48
48
 
49
- /**
50
- * @private
51
- */
52
49
  namespace __internal {
53
50
  type LibDomIsLoaded = typeof globalThis extends { onabort: any } ? true : false;
54
51
 
@@ -62,8 +59,6 @@ declare module "bun" {
62
59
  * Unfortunately some symbols cannot be defined when both Bun types and lib.dom.d.ts types are loaded,
63
60
  * and since we can't redeclare the symbol in a way that satisfies both, we need to fallback
64
61
  * to the type that lib.dom.d.ts provides.
65
- *
66
- * @internal
67
62
  */
68
63
  type UseLibDomIfAvailable<GlobalThisKeyName extends PropertyKey, Otherwise> =
69
64
  // `onabort` is defined in lib.dom.d.ts, so we can check to see if lib dom is loaded by checking if `onabort` is defined
@@ -74,6 +69,7 @@ declare module "bun" {
74
69
  : Otherwise; // Lib dom not loaded anyway, so no conflict. We can safely use our own definition
75
70
  }
76
71
 
72
+ /** @deprecated This type is unused in Bun's types and might be removed in the near future */
77
73
  type Platform =
78
74
  | "aix"
79
75
  | "android"
@@ -87,16 +83,21 @@ declare module "bun" {
87
83
  | "cygwin"
88
84
  | "netbsd";
89
85
 
86
+ /** @deprecated This type is unused in Bun's types and might be removed in the near future */
90
87
  type Architecture = "arm" | "arm64" | "ia32" | "mips" | "mipsel" | "ppc" | "ppc64" | "s390" | "s390x" | "x64";
91
88
 
89
+ /** @deprecated This type is unused in Bun's types and might be removed in the near future */
92
90
  type UncaughtExceptionListener = (error: Error, origin: UncaughtExceptionOrigin) => void;
93
91
 
94
92
  /**
95
93
  * Most of the time the unhandledRejection will be an Error, but this should not be relied upon
96
94
  * as *anything* can be thrown/rejected, it is therefore unsafe to assume that the value is an Error.
95
+ *
96
+ * @deprecated This type is unused in Bun's types and might be removed in the near future
97
97
  */
98
98
  type UnhandledRejectionListener = (reason: unknown, promise: Promise<unknown>) => void;
99
99
 
100
+ /** @deprecated This type is unused in Bun's types and might be removed in the near future */
100
101
  type MultipleResolveListener = (type: MultipleResolveType, promise: Promise<unknown>, value: unknown) => void;
101
102
 
102
103
  interface ErrorEventInit extends EventInit {
@@ -522,18 +523,44 @@ declare module "bun" {
522
523
  * The raw arguments passed to the process, including flags passed to Bun. If you want to easily read flags passed to your script, consider using `process.argv` instead.
523
524
  */
524
525
  const argv: string[];
525
- const origin: string;
526
+
527
+ interface WhichOptions {
528
+ /**
529
+ * Overrides the PATH environment variable
530
+ */
531
+ PATH?: string;
532
+
533
+ /**
534
+ * When given a relative path, use this path to join it.
535
+ */
536
+ cwd?: string;
537
+ }
526
538
 
527
539
  /**
528
540
  * Find the path to an executable, similar to typing which in your terminal. Reads the `PATH` environment variable unless overridden with `options.PATH`.
529
541
  *
530
542
  * @category Utilities
531
543
  *
532
- * @param command The name of the executable or script
533
- * @param options.PATH Overrides the PATH environment variable
534
- * @param options.cwd When given a relative path, use this path to join it.
544
+ * @param command The name of the executable or script to find
545
+ * @param options Options for the search
535
546
  */
536
- function which(command: string, options?: { PATH?: string; cwd?: string }): string | null;
547
+ function which(command: string, options?: WhichOptions): string | null;
548
+
549
+ interface StringWidthOptions {
550
+ /**
551
+ * If `true`, count ANSI escape codes as part of the string width. If `false`, ANSI escape codes are ignored when calculating the string width.
552
+ *
553
+ * @default false
554
+ */
555
+ countAnsiEscapeCodes?: boolean;
556
+
557
+ /**
558
+ * When it's ambiugous and `true`, count emoji as 1 characters wide. If `false`, emoji are counted as 2 character wide.
559
+ *
560
+ * @default true
561
+ */
562
+ ambiguousIsNarrow?: boolean;
563
+ }
537
564
 
538
565
  /**
539
566
  * Get the column count of a string as it would be displayed in a terminal.
@@ -565,23 +592,13 @@ declare module "bun" {
565
592
  * The string to measure
566
593
  */
567
594
  input: string,
568
- options?: {
569
- /**
570
- * If `true`, count ANSI escape codes as part of the string width. If `false`, ANSI escape codes are ignored when calculating the string width.
571
- *
572
- * @default false
573
- */
574
- countAnsiEscapeCodes?: boolean;
575
- /**
576
- * When it's ambiugous and `true`, count emoji as 1 characters wide. If `false`, emoji are counted as 2 character wide.
577
- *
578
- * @default true
579
- */
580
- ambiguousIsNarrow?: boolean;
581
- },
595
+ options?: StringWidthOptions,
582
596
  ): number;
583
597
 
584
- const TOML: {
598
+ /**
599
+ * TOML related APIs
600
+ */
601
+ namespace TOML {
585
602
  /**
586
603
  * Parse a TOML string into a JavaScript object.
587
604
  *
@@ -590,8 +607,8 @@ declare module "bun" {
590
607
  * @param input The TOML string to parse
591
608
  * @returns A JavaScript object
592
609
  */
593
- parse(input: string): object;
594
- };
610
+ export function parse(input: string): object;
611
+ }
595
612
 
596
613
  /**
597
614
  * Synchronously resolve a `moduleId` as though it were imported from `parent`
@@ -618,13 +635,17 @@ declare module "bun" {
618
635
  *
619
636
  * @param destination The file or file path to write to
620
637
  * @param input The data to copy into `destination`.
638
+ * @param options Options for the write
639
+ *
621
640
  * @returns A promise that resolves with the number of bytes written.
622
641
  */
623
642
  function write(
624
643
  destination: BunFile | S3File | PathLike,
625
644
  input: Blob | NodeJS.TypedArray | ArrayBufferLike | string | BlobPart[],
626
645
  options?: {
627
- /** If writing to a PathLike, set the permissions of the file. */
646
+ /**
647
+ * If writing to a PathLike, set the permissions of the file.
648
+ */
628
649
  mode?: number;
629
650
  /**
630
651
  * If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
@@ -645,6 +666,8 @@ declare module "bun" {
645
666
  * overwritten. If `input`'s size is less than `destination`'s size,
646
667
  * `destination` will be truncated.
647
668
  * @param input - `Response` object
669
+ * @param options Options for the write
670
+ *
648
671
  * @returns A promise that resolves with the number of bytes written.
649
672
  */
650
673
  function write(
@@ -853,7 +876,8 @@ declare module "bun" {
853
876
  * @param multipartBoundaryExcludingDashes Optional boundary to use for multipart form data. If none is provided, assumes it is a URLEncoded form.
854
877
  * @returns A promise that resolves with the data encoded into a {@link FormData} object.
855
878
  *
856
- * @example Multipart form data example
879
+ * @example
880
+ * **Multipart form data example**
857
881
  * ```ts
858
882
  * // without dashes
859
883
  * const boundary = "WebKitFormBoundary" + Math.random().toString(16).slice(2);
@@ -863,7 +887,7 @@ declare module "bun" {
863
887
  * formData.get("foo"); // "bar"
864
888
  * ```
865
889
  *
866
- * @example URL-encoded form data example
890
+ * **URL-encoded form data example**
867
891
  * ```ts
868
892
  * const stream = new Response("hello=123").body;
869
893
  * const formData = await Bun.readableStreamToFormData(stream);
@@ -941,14 +965,13 @@ declare module "bun" {
941
965
  */
942
966
  function pathToFileURL(path: string): URL;
943
967
 
944
- interface Peek {
945
- <T = undefined>(promise: T | Promise<T>): Promise<T> | T;
946
- status<T = undefined>(promise: T | Promise<T>): "pending" | "fulfilled" | "rejected";
947
- }
948
968
  /**
949
969
  * Extract the value from the Promise in the same tick of the event loop
950
970
  */
951
- const peek: Peek;
971
+ function peek<T = undefined>(promise: T | Promise<T>): Promise<T> | T;
972
+ namespace peek {
973
+ function status<T = undefined>(promise: T | Promise<T>): "pending" | "fulfilled" | "rejected";
974
+ }
952
975
 
953
976
  /**
954
977
  * Convert a {@link URL} to a filesystem path.
@@ -1001,7 +1024,8 @@ declare module "bun" {
1001
1024
  end(): ArrayBuffer | Uint8Array;
1002
1025
  }
1003
1026
 
1004
- const dns: {
1027
+ /** DNS Related APIs */
1028
+ namespace dns {
1005
1029
  /**
1006
1030
  * Lookup the IP address for a hostname
1007
1031
  *
@@ -1010,29 +1034,27 @@ declare module "bun" {
1010
1034
  * @param hostname The hostname to lookup
1011
1035
  * @param options Options for the lookup
1012
1036
  *
1013
- * ## Example
1014
- *
1037
+ * @example
1038
+ * ## Basic usage
1015
1039
  * ```js
1016
1040
  * const [{ address }] = await Bun.dns.lookup('example.com');
1017
1041
  * ```
1018
1042
  *
1019
- * ### Filter results to IPv4:
1020
- *
1043
+ * ## Filter results to IPv4
1021
1044
  * ```js
1022
1045
  * import { dns } from 'bun';
1023
1046
  * const [{ address }] = await dns.lookup('example.com', {family: 4});
1024
1047
  * console.log(address); // "123.122.22.126"
1025
1048
  * ```
1026
1049
  *
1027
- * ### Filter results to IPv6:
1028
- *
1050
+ * ## Filter results to IPv6
1029
1051
  * ```js
1030
1052
  * import { dns } from 'bun';
1031
1053
  * const [{ address }] = await dns.lookup('example.com', {family: 6});
1032
1054
  * console.log(address); // "2001:db8::1"
1033
1055
  * ```
1034
1056
  *
1035
- * #### DNS resolver client
1057
+ * ## DNS resolver client
1036
1058
  *
1037
1059
  * Bun supports three DNS resolvers:
1038
1060
  * - `c-ares` - Uses the c-ares library to perform DNS resolution. This is the default on Linux.
@@ -1046,7 +1068,7 @@ declare module "bun" {
1046
1068
  * console.log(address); // "19.42.52.62"
1047
1069
  * ```
1048
1070
  */
1049
- lookup(
1071
+ function lookup(
1050
1072
  hostname: string,
1051
1073
  options?: {
1052
1074
  /**
@@ -1115,12 +1137,12 @@ declare module "bun" {
1115
1137
  * await fetch('https://example.com');
1116
1138
  * ```
1117
1139
  */
1118
- prefetch(hostname: string): void;
1140
+ function prefetch(hostname: string): void;
1119
1141
 
1120
1142
  /**
1121
1143
  * **Experimental API**
1122
1144
  */
1123
- getCacheStats(): {
1145
+ function getCacheStats(): {
1124
1146
  /**
1125
1147
  * The number of times a cached DNS entry that was already resolved was used.
1126
1148
  */
@@ -1132,10 +1154,10 @@ declare module "bun" {
1132
1154
  totalCount: number;
1133
1155
  };
1134
1156
 
1135
- ADDRCONFIG: number;
1136
- ALL: number;
1137
- V4MAPPED: number;
1138
- };
1157
+ const ADDRCONFIG: number;
1158
+ const ALL: number;
1159
+ const V4MAPPED: number;
1160
+ }
1139
1161
 
1140
1162
  interface DNSLookup {
1141
1163
  /**
@@ -1184,7 +1206,7 @@ declare module "bun" {
1184
1206
  * ```
1185
1207
  */
1186
1208
  interface BunFile extends Blob {
1187
- /**.p
1209
+ /**
1188
1210
  * Offset any operation on the file starting at `begin` and ending at `end`. `end` is relative to 0
1189
1211
  *
1190
1212
  * Similar to [`TypedArray.subarray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray). Does not copy the file, open the file, or modify the file.
@@ -1197,7 +1219,6 @@ declare module "bun" {
1197
1219
  */
1198
1220
  slice(begin?: number, end?: number, contentType?: string): BunFile;
1199
1221
 
1200
- /** */
1201
1222
  /**
1202
1223
  * Offset any operation on the file starting at `begin`
1203
1224
  *
@@ -1211,6 +1232,8 @@ declare module "bun" {
1211
1232
  slice(begin?: number, contentType?: string): BunFile;
1212
1233
 
1213
1234
  /**
1235
+ * Slice the file from the beginning to the end, optionally with a new MIME type.
1236
+ *
1214
1237
  * @param contentType - MIME type for the new BunFile
1215
1238
  */
1216
1239
  slice(contentType?: string): BunFile;
@@ -1271,7 +1294,7 @@ declare module "bun" {
1271
1294
  unlink(): Promise<void>;
1272
1295
 
1273
1296
  /**
1274
- * Deletes the file. ( same as unlink )
1297
+ * Deletes the file (same as unlink)
1275
1298
  */
1276
1299
  delete(): Promise<void>;
1277
1300
 
@@ -1695,14 +1718,14 @@ declare module "bun" {
1695
1718
  *
1696
1719
  * @category Security
1697
1720
  */
1698
- var CSRF: {
1721
+ namespace CSRF {
1699
1722
  /**
1700
1723
  * Generate a CSRF token.
1701
1724
  * @param secret The secret to use for the token. If not provided, a random default secret will be generated in memory and used.
1702
1725
  * @param options The options for the token.
1703
1726
  * @returns The generated token.
1704
1727
  */
1705
- generate(secret?: string, options?: CSRFGenerateOptions): string;
1728
+ function generate(secret?: string, options?: CSRFGenerateOptions): string;
1706
1729
 
1707
1730
  /**
1708
1731
  * Verify a CSRF token.
@@ -1710,8 +1733,8 @@ declare module "bun" {
1710
1733
  * @param options The options for the token.
1711
1734
  * @returns True if the token is valid, false otherwise.
1712
1735
  */
1713
- verify(token: string, options?: CSRFVerifyOptions): boolean;
1714
- };
1736
+ function verify(token: string, options?: CSRFVerifyOptions): boolean;
1737
+ }
1715
1738
 
1716
1739
  /**
1717
1740
  * This lets you use macros as regular imports
@@ -2231,8 +2254,8 @@ declare module "bun" {
2231
2254
  * Standard Library. Thanks to \@jedisct1 and other Zig contributors for their
2232
2255
  * work on this.
2233
2256
  *
2234
- * ### Example with argon2
2235
- *
2257
+ * @example
2258
+ * **Example with argon2**
2236
2259
  * ```ts
2237
2260
  * import {password} from "bun";
2238
2261
  *
@@ -2241,7 +2264,7 @@ declare module "bun" {
2241
2264
  * console.log(verify); // true
2242
2265
  * ```
2243
2266
  *
2244
- * ### Example with bcrypt
2267
+ * **Example with bcrypt**
2245
2268
  * ```ts
2246
2269
  * import {password} from "bun";
2247
2270
  *
@@ -2296,14 +2319,16 @@ declare module "bun" {
2296
2319
  *
2297
2320
  * @returns A promise that resolves to the hashed password
2298
2321
  *
2299
- * ## Example with argon2
2322
+ * @example
2323
+ * **Example with argon2**
2300
2324
  * ```ts
2301
2325
  * import {password} from "bun";
2302
2326
  * const hash = await password.hash("hello world");
2303
2327
  * console.log(hash); // $argon2id$v=1...
2304
2328
  * const verify = await password.verify("hello world", hash);
2305
2329
  * ```
2306
- * ## Example with bcrypt
2330
+ *
2331
+ * **Example with bcrypt**
2307
2332
  * ```ts
2308
2333
  * import {password} from "bun";
2309
2334
  * const hash = await password.hash("hello world", "bcrypt");
@@ -2336,8 +2361,8 @@ declare module "bun" {
2336
2361
  * Standard Library. Thanks to \@jedisct1 and other Zig contributors for their
2337
2362
  * work on this.
2338
2363
  *
2339
- * ### Example with argon2
2340
- *
2364
+ * @example
2365
+ * **Example with argon2**
2341
2366
  * ```ts
2342
2367
  * import {password} from "bun";
2343
2368
  *
@@ -2346,7 +2371,7 @@ declare module "bun" {
2346
2371
  * console.log(verify); // true
2347
2372
  * ```
2348
2373
  *
2349
- * ### Example with bcrypt
2374
+ * **Example with bcrypt**
2350
2375
  * ```ts
2351
2376
  * import {password} from "bun";
2352
2377
  *
@@ -2381,8 +2406,8 @@ declare module "bun" {
2381
2406
  * Standard Library. Thanks to \@jedisct1 and other Zig contributors for their
2382
2407
  * work on this.
2383
2408
  *
2384
- * ### Example with argon2
2385
- *
2409
+ * @example
2410
+ * **Example with argon2**
2386
2411
  * ```ts
2387
2412
  * import {password} from "bun";
2388
2413
  *
@@ -2391,7 +2416,7 @@ declare module "bun" {
2391
2416
  * console.log(verify); // true
2392
2417
  * ```
2393
2418
  *
2394
- * ### Example with bcrypt
2419
+ * **Example with bcrypt**
2395
2420
  * ```ts
2396
2421
  * import {password} from "bun";
2397
2422
  *
@@ -2447,227 +2472,251 @@ declare module "bun" {
2447
2472
  /**
2448
2473
  * Bundles JavaScript, TypeScript, CSS, HTML and other supported files into optimized outputs.
2449
2474
  *
2450
- * @param {Object} config - Build configuration options
2451
- * @returns {Promise<BuildOutput>} Promise that resolves to build output containing generated artifacts and build status
2475
+ * @param config - Build configuration options
2476
+ * @returns Promise that resolves to build output containing generated artifacts and build status
2452
2477
  * @throws {AggregateError} When build fails and config.throw is true (default in Bun 1.2+)
2453
2478
  *
2454
2479
  * @category Bundler
2455
2480
  *
2456
- * @example Basic usage - Bundle a single entrypoint and check results
2457
- * ```ts
2481
+ * @example
2482
+ * Basic usage - Bundle a single entrypoint and check results
2483
+ *```ts
2458
2484
  * const result = await Bun.build({
2459
2485
  * entrypoints: ['./src/index.tsx'],
2460
2486
  * outdir: './dist'
2461
2487
  * });
2462
2488
  *
2463
- * if (!result.success) {
2464
- * console.error('Build failed:', result.logs);
2465
- * process.exit(1);
2466
- * }
2467
- * ```
2468
- * *
2469
- * * @example Set up multiple entrypoints with code splitting enabled
2470
- * ```ts
2471
- * await Bun.build({
2472
- * entrypoints: ['./src/app.tsx', './src/admin.tsx'],
2473
- * outdir: './dist',
2474
- * splitting: true,
2475
- * sourcemap: "external"
2476
- * });
2477
- * ```
2478
- * *
2479
- * * @example Configure minification and optimization settings
2480
- * ```ts
2481
- * await Bun.build({
2482
- * entrypoints: ['./src/index.tsx'],
2483
- * outdir: './dist',
2484
- * minify: {
2485
- * whitespace: true,
2486
- * identifiers: true,
2487
- * syntax: true
2488
- * },
2489
- * drop: ['console', 'debugger']
2490
- * });
2491
- * ```
2492
- * *
2493
- * * @example Set up custom loaders and mark packages as external
2494
- * ```ts
2495
- * await Bun.build({
2496
- * entrypoints: ['./src/index.tsx'],
2497
- * outdir: './dist',
2498
- * loader: {
2499
- * '.png': 'dataurl',
2500
- * '.svg': 'file',
2501
- * '.txt': 'text',
2502
- * '.json': 'json'
2503
- * },
2504
- * external: ['react', 'react-dom']
2505
- * });
2506
- * ```
2507
- * *
2508
- * * @example Configure environment variable handling with different modes
2509
- * ```ts
2510
- * // Inline all environment variables
2511
- * await Bun.build({
2512
- * entrypoints: ['./src/index.tsx'],
2513
- * outdir: './dist',
2514
- * env: 'inline'
2515
- * });
2516
-
2517
- * // Only include specific env vars
2518
- * await Bun.build({
2519
- * entrypoints: ['./src/index.tsx'],
2520
- * outdir: './dist',
2521
- * env: 'PUBLIC_*'
2522
- * });
2523
- * ```
2524
- * *
2525
- * * @example Set up custom naming patterns for all output types
2526
- * ```ts
2527
- * await Bun.build({
2528
- * entrypoints: ['./src/index.tsx'],
2529
- * outdir: './dist',
2530
- * naming: {
2531
- * entry: '[dir]/[name]-[hash].[ext]',
2532
- * chunk: 'chunks/[name]-[hash].[ext]',
2533
- * asset: 'assets/[name]-[hash].[ext]'
2534
- * }
2535
- * });
2536
- * ```
2537
- * @example Work with build artifacts in different formats
2538
- * ```ts
2539
- * const result = await Bun.build({
2540
- * entrypoints: ['./src/index.tsx']
2541
- * });
2542
-
2543
- * for (const artifact of result.outputs) {
2544
- * const text = await artifact.text();
2545
- * const buffer = await artifact.arrayBuffer();
2546
- * const bytes = await artifact.bytes();
2547
-
2548
- * new Response(artifact);
2549
- * await Bun.write(artifact.path, artifact);
2550
- * }
2551
- * ```
2552
- * @example Implement comprehensive error handling with position info
2553
- * ```ts
2554
- * try {
2555
- * const result = await Bun.build({
2556
- * entrypoints: ['./src/index.tsx'],
2557
- * });
2558
- * } catch (e) {
2559
- * const error = e as AggregateError;
2560
- * console.error('Build failed:');
2561
- * for (const msg of error.errors) {
2562
- * if ('position' in msg) {
2563
- * console.error(
2564
- * `${msg.message} at ${msg.position?.file}:${msg.position?.line}:${msg.position?.column}`
2565
- * );
2566
- * } else {
2567
- * console.error(msg.message);
2568
- * }
2569
- * }
2570
- * }
2571
- * ```
2572
- * @example Set up Node.js target with specific configurations
2573
- * ```ts
2574
- * await Bun.build({
2575
- * entrypoints: ['./src/server.ts'],
2576
- * outdir: './dist',
2577
- * target: 'node',
2578
- * format: 'cjs',
2579
- * sourcemap: 'external',
2580
- * minify: false,
2581
- * packages: 'external'
2582
- * });
2583
- * ```
2584
- * *
2585
- * * @example Configure experimental CSS bundling with multiple themes
2586
- * ```ts
2587
- * await Bun.build({
2588
- * entrypoints: [
2589
- * './src/styles.css',
2590
- * './src/themes/dark.css',
2591
- * './src/themes/light.css'
2592
- * ],
2593
- * outdir: './dist/css',
2594
- * });
2595
- * ```
2596
- * @example Define compile-time constants and version information
2597
- * ```ts
2598
- * await Bun.build({
2599
- * entrypoints: ['./src/index.tsx'],
2600
- * outdir: './dist',
2601
- * define: {
2602
- * 'process.env.NODE_ENV': JSON.stringify('production'),
2603
- * 'CONSTANTS.VERSION': JSON.stringify('1.0.0'),
2604
- * 'CONSTANTS.BUILD_TIME': JSON.stringify(new Date().toISOString())
2605
- * }
2606
- * });
2607
- * ```
2608
- * @example Create a custom plugin for handling special file types
2609
- * ```ts
2610
- * await Bun.build({
2611
- * entrypoints: ['./src/index.tsx'],
2612
- * outdir: './dist',
2613
- * plugins: [
2614
- * {
2615
- * name: 'my-plugin',
2616
- * setup(build) {
2617
- * build.onLoad({ filter: /\.custom$/ }, async (args) => {
2618
- * const content = await Bun.file(args.path).text();
2619
- * return {
2620
- * contents: `export default ${JSON.stringify(content)}`,
2621
- * loader: 'js'
2622
- * };
2623
- * });
2624
- * }
2625
- * }
2626
- * ]
2627
- * });
2628
- * ```
2629
- * @example Enable bytecode generation for faster startup
2630
- * ```ts
2631
- * await Bun.build({
2632
- * entrypoints: ['./src/server.ts'],
2633
- * outdir: './dist',
2634
- * target: 'bun',
2635
- * format: 'cjs',
2636
- * bytecode: true
2637
- * });
2638
- * ```
2639
- * @example Add custom banner and footer to output files
2640
- * ```ts
2641
- * await Bun.build({
2642
- * entrypoints: ['./src/index.tsx'],
2643
- * outdir: './dist',
2644
- * banner: '"use client";\n// Built with Bun',
2645
- * footer: '// Generated on ' + new Date().toISOString()
2646
- * });
2647
- * ```
2648
- * @example Configure CDN public path for asset loading
2649
- * ```ts
2650
- * await Bun.build({
2651
- * entrypoints: ['./src/index.tsx'],
2652
- * outdir: './dist',
2653
- * publicPath: 'https://cdn.example.com/assets/',
2654
- * loader: {
2655
- * '.png': 'file',
2656
- * '.svg': 'file'
2657
- * }
2658
- * });
2659
- * ```
2660
- * @example Set up package export conditions for different environments
2661
- * ```ts
2662
- * await Bun.build({
2663
- * entrypoints: ['./src/index.tsx'],
2664
- * outdir: './dist',
2665
- * conditions: ['production', 'browser', 'module'],
2666
- * packages: 'external'
2667
- * });
2668
- * ```
2489
+ * if (!result.success) {
2490
+ * console.error('Build failed:', result.logs);
2491
+ * process.exit(1);
2492
+ * }
2493
+ *```
2494
+ *
2495
+ * @example
2496
+ * Set up multiple entrypoints with code splitting enabled
2497
+ *```ts
2498
+ * await Bun.build({
2499
+ * entrypoints: ['./src/app.tsx', './src/admin.tsx'],
2500
+ * outdir: './dist',
2501
+ * splitting: true,
2502
+ * sourcemap: "external"
2503
+ * });
2504
+ *```
2505
+ *
2506
+ * @example
2507
+ * Configure minification and optimization settings
2508
+ *```ts
2509
+ * await Bun.build({
2510
+ * entrypoints: ['./src/index.tsx'],
2511
+ * outdir: './dist',
2512
+ * minify: {
2513
+ * whitespace: true,
2514
+ * identifiers: true,
2515
+ * syntax: true
2516
+ * },
2517
+ * drop: ['console', 'debugger']
2518
+ * });
2519
+ *```
2520
+ *
2521
+ * @example
2522
+ * Set up custom loaders and mark packages as external
2523
+ *```ts
2524
+ * await Bun.build({
2525
+ * entrypoints: ['./src/index.tsx'],
2526
+ * outdir: './dist',
2527
+ * loader: {
2528
+ * '.png': 'dataurl',
2529
+ * '.svg': 'file',
2530
+ * '.txt': 'text',
2531
+ * '.json': 'json'
2532
+ * },
2533
+ * external: ['react', 'react-dom']
2534
+ * });
2535
+ *```
2536
+ *
2537
+ * @example
2538
+ * Configure environment variable handling with different modes
2539
+ *```ts
2540
+ * // Inline all environment variables
2541
+ * await Bun.build({
2542
+ * entrypoints: ['./src/index.tsx'],
2543
+ * outdir: './dist',
2544
+ * env: 'inline'
2545
+ * });
2546
+ *
2547
+ * // Only include specific env vars
2548
+ * await Bun.build({
2549
+ * entrypoints: ['./src/index.tsx'],
2550
+ * outdir: './dist',
2551
+ * env: 'PUBLIC_*'
2552
+ * });
2553
+ *```
2554
+ *
2555
+ * @example
2556
+ * Set up custom naming patterns for all output types
2557
+ *```ts
2558
+ * await Bun.build({
2559
+ * entrypoints: ['./src/index.tsx'],
2560
+ * outdir: './dist',
2561
+ * naming: {
2562
+ * entry: '[dir]/[name]-[hash].[ext]',
2563
+ * chunk: 'chunks/[name]-[hash].[ext]',
2564
+ * asset: 'assets/[name]-[hash].[ext]'
2565
+ * }
2566
+ * });
2567
+ *```
2568
+ *
2569
+ * @example
2570
+ * Work with build artifacts in different formats
2571
+ *```ts
2572
+ * const result = await Bun.build({
2573
+ * entrypoints: ['./src/index.tsx']
2574
+ * });
2575
+ * for (const artifact of result.outputs) {
2576
+ * const text = await artifact.text();
2577
+ * const buffer = await artifact.arrayBuffer();
2578
+ * const bytes = await artifact.bytes();
2579
+ * new Response(artifact);
2580
+ * await Bun.write(artifact.path, artifact);
2581
+ * }
2582
+ *```
2583
+ *
2584
+ * @example
2585
+ * Implement comprehensive error handling with position info
2586
+ *```ts
2587
+ * try {
2588
+ * const result = await Bun.build({
2589
+ * entrypoints: ['./src/index.tsx'],
2590
+ * });
2591
+ * } catch (e) {
2592
+ * const error = e as AggregateError;
2593
+ * console.error('Build failed:');
2594
+ * for (const msg of error.errors) {
2595
+ * if ('position' in msg) {
2596
+ * console.error(
2597
+ * `${msg.message} at ${msg.position?.file}:${msg.position?.line}:${msg.position?.column}`
2598
+ * );
2599
+ * } else {
2600
+ * console.error(msg.message);
2601
+ * }
2602
+ * }
2603
+ * }
2604
+ *```
2605
+ *
2606
+ * @example
2607
+ * Set up Node.js target with specific configurations
2608
+ *```ts
2609
+ * await Bun.build({
2610
+ * entrypoints: ['./src/server.ts'],
2611
+ * outdir: './dist',
2612
+ * target: 'node',
2613
+ * format: 'cjs',
2614
+ * sourcemap: 'external',
2615
+ * minify: false,
2616
+ * packages: 'external'
2617
+ * });
2618
+ *```
2619
+ *
2620
+ * @example
2621
+ * Configure experimental CSS bundling with multiple themes
2622
+ *```ts
2623
+ * await Bun.build({
2624
+ * entrypoints: [
2625
+ * './src/styles.css',
2626
+ * './src/themes/dark.css',
2627
+ * './src/themes/light.css'
2628
+ * ],
2629
+ * outdir: './dist/css',
2630
+ * });
2631
+ *```
2632
+ *
2633
+ * @example
2634
+ * Define compile-time constants and version information
2635
+ *```ts
2636
+ * await Bun.build({
2637
+ * entrypoints: ['./src/index.tsx'],
2638
+ * outdir: './dist',
2639
+ * define: {
2640
+ * 'process.env.NODE_ENV': JSON.stringify('production'),
2641
+ * 'CONSTANTS.VERSION': JSON.stringify('1.0.0'),
2642
+ * 'CONSTANTS.BUILD_TIME': JSON.stringify(new Date().toISOString())
2643
+ * }
2644
+ * });
2645
+ *```
2646
+ *
2647
+ * @example
2648
+ * Create a custom plugin for handling special file types
2649
+ *```ts
2650
+ * await Bun.build({
2651
+ * entrypoints: ['./src/index.tsx'],
2652
+ * outdir: './dist',
2653
+ * plugins: [
2654
+ * {
2655
+ * name: 'my-plugin',
2656
+ * setup(build) {
2657
+ * build.onLoad({ filter: /\.custom$/ }, async (args) => {
2658
+ * const content = await Bun.file(args.path).text();
2659
+ * return {
2660
+ * contents: `export default ${JSON.stringify(content)}`,
2661
+ * loader: 'js'
2662
+ * };
2663
+ * });
2664
+ * }
2665
+ * }
2666
+ * ]
2667
+ * });
2668
+ *```
2669
+ *
2670
+ * @example
2671
+ * Enable bytecode generation for faster startup
2672
+ *```ts
2673
+ * await Bun.build({
2674
+ * entrypoints: ['./src/server.ts'],
2675
+ * outdir: './dist',
2676
+ * target: 'bun',
2677
+ * format: 'cjs',
2678
+ * bytecode: true
2679
+ * });
2680
+ *```
2681
+ *
2682
+ * @example
2683
+ * Add custom banner and footer to output files
2684
+ *```ts
2685
+ * await Bun.build({
2686
+ * entrypoints: ['./src/index.tsx'],
2687
+ * outdir: './dist',
2688
+ * banner: '"use client";\n// Built with Bun',
2689
+ * footer: '// Generated on ' + new Date().toISOString()
2690
+ * });
2691
+ *```
2692
+ *
2693
+ * @example
2694
+ * Configure CDN public path for asset loading
2695
+ *```ts
2696
+ * await Bun.build({
2697
+ * entrypoints: ['./src/index.tsx'],
2698
+ * outdir: './dist',
2699
+ * publicPath: 'https://cdn.example.com/assets/',
2700
+ * loader: {
2701
+ * '.png': 'file',
2702
+ * '.svg': 'file'
2703
+ * }
2704
+ * });
2705
+ *```
2706
+ *
2707
+ * @example
2708
+ * Set up package export conditions for different environments
2709
+ *```ts
2710
+ * await Bun.build({
2711
+ * entrypoints: ['./src/index.tsx'],
2712
+ * outdir: './dist',
2713
+ * conditions: ['production', 'browser', 'module'],
2714
+ * packages: 'external'
2715
+ * });
2716
+ *```
2669
2717
  */
2670
2718
  function build(config: BuildConfig): Promise<BuildOutput>;
2719
+
2671
2720
  /**
2672
2721
  * A status that represents the outcome of a sent message.
2673
2722
  *
@@ -2715,6 +2764,7 @@ declare module "bun" {
2715
2764
  * Powered by [uWebSockets](https://github.com/uNetworking/uWebSockets).
2716
2765
  *
2717
2766
  * @example
2767
+ * ```ts
2718
2768
  * Bun.serve({
2719
2769
  * websocket: {
2720
2770
  * open(ws) {
@@ -2729,6 +2779,7 @@ declare module "bun" {
2729
2779
  * },
2730
2780
  * }
2731
2781
  * });
2782
+ * ```
2732
2783
  *
2733
2784
  * @category HTTP & Networking
2734
2785
  */
@@ -3214,13 +3265,15 @@ declare module "bun" {
3214
3265
  /**
3215
3266
  * Uniquely identify a server instance with an ID
3216
3267
  *
3217
- * ### When bun is started with the `--hot` flag
3268
+ * ---
3269
+ *
3270
+ * **When bun is started with the `--hot` flag**:
3218
3271
  *
3219
3272
  * This string will be used to hot reload the server without interrupting
3220
3273
  * pending requests or websockets. If not provided, a value will be
3221
3274
  * generated. To disable hot reloading, set this value to `null`.
3222
3275
  *
3223
- * ### When bun is not started with the `--hot` flag
3276
+ * **When bun is not started with the `--hot` flag**:
3224
3277
  *
3225
3278
  * This string will currently do nothing. But in the future it could be useful for logs or metrics.
3226
3279
  */
@@ -4520,14 +4573,20 @@ declare module "bun" {
4520
4573
  */
4521
4574
  function mmap(path: PathLike, opts?: MMapOptions): Uint8Array;
4522
4575
 
4523
- /** Write to stdout */
4576
+ /**
4577
+ * Write to stdout
4578
+ */
4524
4579
  const stdout: BunFile;
4525
- /** Write to stderr */
4580
+
4581
+ /**
4582
+ * Write to stderr
4583
+ */
4526
4584
  const stderr: BunFile;
4585
+
4527
4586
  /**
4528
4587
  * Read from stdin
4529
4588
  *
4530
- * This is read-only
4589
+ * This is a read-only BunFile
4531
4590
  */
4532
4591
  const stdin: BunFile;
4533
4592
 
@@ -4645,20 +4704,20 @@ declare module "bun" {
4645
4704
  /**
4646
4705
  * Bun.semver provides a fast way to parse and compare version numbers.
4647
4706
  */
4648
- var semver: {
4707
+ namespace semver {
4649
4708
  /**
4650
4709
  * Test if the version satisfies the range. Stringifies both arguments. Returns `true` or `false`.
4651
4710
  */
4652
- satisfies: (version: StringLike, range: StringLike) => boolean;
4711
+ function satisfies(version: StringLike, range: StringLike): boolean;
4653
4712
 
4654
4713
  /**
4655
4714
  * Returns 0 if the versions are equal, 1 if `v1` is greater, or -1 if `v2` is greater.
4656
4715
  * Throws an error if either version is invalid.
4657
4716
  */
4658
- order: (v1: StringLike, v2: StringLike) => -1 | 0 | 1;
4659
- };
4717
+ function order(v1: StringLike, v2: StringLike): -1 | 0 | 1;
4718
+ }
4660
4719
 
4661
- interface Unsafe {
4720
+ namespace unsafe {
4662
4721
  /**
4663
4722
  * Cast bytes to a `String` without copying. This is the fastest way to get a `String` from a `Uint8Array` or `ArrayBuffer`.
4664
4723
  *
@@ -4666,7 +4725,7 @@ declare module "bun" {
4666
4725
  *
4667
4726
  * **The input buffer must not be garbage collected**. That means you will need to hold on to it for the duration of the string's lifetime.
4668
4727
  */
4669
- arrayBufferToString(buffer: Uint8Array | ArrayBufferLike): string;
4728
+ function arrayBufferToString(buffer: Uint8Array | ArrayBufferLike): string;
4670
4729
 
4671
4730
  /**
4672
4731
  * Cast bytes to a `String` without copying. This is the fastest way to get a `String` from a `Uint16Array`
@@ -4676,7 +4735,7 @@ declare module "bun" {
4676
4735
  * **The input buffer must not be garbage collected**. That means you will need to hold on to it for the duration of the string's lifetime.
4677
4736
  */
4678
4737
 
4679
- arrayBufferToString(buffer: Uint16Array): string;
4738
+ function arrayBufferToString(buffer: Uint16Array): string;
4680
4739
 
4681
4740
  /**
4682
4741
  * Force the garbage collector to run extremely often,
@@ -4693,14 +4752,13 @@ declare module "bun" {
4693
4752
  * @param level
4694
4753
  * @returns The previous level
4695
4754
  */
4696
- gcAggressionLevel(level?: 0 | 1 | 2): 0 | 1 | 2;
4755
+ function gcAggressionLevel(level?: 0 | 1 | 2): 0 | 1 | 2;
4697
4756
 
4698
4757
  /**
4699
4758
  * Dump the mimalloc heap to the console
4700
4759
  */
4701
- mimallocDump(): void;
4760
+ function mimallocDump(): void;
4702
4761
  }
4703
- const unsafe: Unsafe;
4704
4762
 
4705
4763
  type DigestEncoding = "utf8" | "ucs2" | "utf16le" | "latin1" | "ascii" | "base64" | "base64url" | "hex";
4706
4764
 
@@ -4792,6 +4850,8 @@ declare module "bun" {
4792
4850
 
4793
4851
  /**
4794
4852
  * The next time JavaScriptCore is idle, clear unused memory and attempt to reduce the heap size.
4853
+ *
4854
+ * @deprecated
4795
4855
  */
4796
4856
  function shrink(): void;
4797
4857
 
@@ -5498,7 +5558,6 @@ declare module "bun" {
5498
5558
  * @param specifier The module specifier to register the callback for
5499
5559
  * @param callback The function to run when the module is imported or required
5500
5560
  *
5501
- * ### Example
5502
5561
  * @example
5503
5562
  * ```ts
5504
5563
  * Bun.plugin({
@@ -5709,13 +5768,18 @@ declare module "bun" {
5709
5768
  */
5710
5769
  shutdown(halfClose?: boolean): void;
5711
5770
 
5712
- // -1 = detached
5713
- // 0 = closed
5714
- // 1 = open
5715
- // -2 = closing
5716
- // 2 = everything else
5717
- // positive = open
5718
- readonly readyState: "open" | "closing" | "closed";
5771
+ /**
5772
+ * The ready state of the socket.
5773
+ *
5774
+ * You can assume that a positive value means the socket is open and usable
5775
+ *
5776
+ * - `-2` = Shutdown
5777
+ * - `-1` = Detached
5778
+ * - `0` = Closed
5779
+ * - `1` = Established
5780
+ * - `2` = Else
5781
+ */
5782
+ readonly readyState: -2 | -1 | 0 | 1 | 2;
5719
5783
 
5720
5784
  /**
5721
5785
  * Allow Bun's process to exit even if this socket is still open
@@ -7064,13 +7128,13 @@ declare module "bun" {
7064
7128
  /**
7065
7129
  * The current version of Bun
7066
7130
  * @example
7067
- * "0.2.0"
7131
+ * "1.2.0"
7068
7132
  */
7069
7133
  const version: string;
7070
7134
 
7071
7135
  /**
7072
7136
  * The current version of Bun with the shortened commit sha of the build
7073
- * @example "v1.1.30 (d09df1af)"
7137
+ * @example "v1.2.0 (a1b2c3d4)"
7074
7138
  */
7075
7139
  const version_with_sha: string;
7076
7140
 
@@ -7366,34 +7430,133 @@ declare module "bun" {
7366
7430
 
7367
7431
  type CookieSameSite = "strict" | "lax" | "none";
7368
7432
 
7433
+ /**
7434
+ * A class for working with a single cookie
7435
+ *
7436
+ * @example
7437
+ * ```js
7438
+ * const cookie = new Bun.Cookie("name", "value");
7439
+ * console.log(cookie.toString()); // "name=value; Path=/; SameSite=Lax"
7440
+ * ```
7441
+ */
7369
7442
  class Cookie {
7443
+ /**
7444
+ * Create a new cookie
7445
+ * @param name - The name of the cookie
7446
+ * @param value - The value of the cookie
7447
+ * @param options - Optional cookie attributes
7448
+ */
7370
7449
  constructor(name: string, value: string, options?: CookieInit);
7450
+
7451
+ /**
7452
+ * Create a new cookie from a cookie string
7453
+ * @param cookieString - The cookie string
7454
+ */
7371
7455
  constructor(cookieString: string);
7456
+
7457
+ /**
7458
+ * Create a new cookie from a cookie object
7459
+ * @param cookieObject - The cookie object
7460
+ */
7372
7461
  constructor(cookieObject?: CookieInit);
7373
7462
 
7463
+ /**
7464
+ * The name of the cookie
7465
+ */
7374
7466
  readonly name: string;
7467
+
7468
+ /**
7469
+ * The value of the cookie
7470
+ */
7375
7471
  value: string;
7472
+
7473
+ /**
7474
+ * The domain of the cookie
7475
+ */
7376
7476
  domain?: string;
7477
+
7478
+ /**
7479
+ * The path of the cookie
7480
+ */
7377
7481
  path: string;
7482
+
7483
+ /**
7484
+ * The expiration date of the cookie
7485
+ */
7378
7486
  expires?: Date;
7487
+
7488
+ /**
7489
+ * Whether the cookie is secure
7490
+ */
7379
7491
  secure: boolean;
7492
+
7493
+ /**
7494
+ * The same-site attribute of the cookie
7495
+ */
7380
7496
  sameSite: CookieSameSite;
7497
+
7498
+ /**
7499
+ * Whether the cookie is partitioned
7500
+ */
7381
7501
  partitioned: boolean;
7502
+
7503
+ /**
7504
+ * The maximum age of the cookie in seconds
7505
+ */
7382
7506
  maxAge?: number;
7507
+
7508
+ /**
7509
+ * Whether the cookie is HTTP-only
7510
+ */
7383
7511
  httpOnly: boolean;
7384
7512
 
7513
+ /**
7514
+ * Whether the cookie is expired
7515
+ */
7385
7516
  isExpired(): boolean;
7386
7517
 
7518
+ /**
7519
+ * Serialize the cookie to a string
7520
+ *
7521
+ * @example
7522
+ * ```ts
7523
+ * const cookie = Bun.Cookie.from("session", "abc123", {
7524
+ * domain: "example.com",
7525
+ * path: "/",
7526
+ * secure: true,
7527
+ * httpOnly: true
7528
+ * }).serialize(); // "session=abc123; Domain=example.com; Path=/; Secure; HttpOnly; SameSite=Lax"
7529
+ * ```
7530
+ */
7387
7531
  serialize(): string;
7532
+
7533
+ /**
7534
+ * Serialize the cookie to a string
7535
+ *
7536
+ * Alias of {@link Cookie.serialize}
7537
+ */
7388
7538
  toString(): string;
7539
+
7540
+ /**
7541
+ * Serialize the cookie to a JSON object
7542
+ */
7389
7543
  toJSON(): CookieInit;
7390
7544
 
7545
+ /**
7546
+ * Parse a cookie string into a Cookie object
7547
+ * @param cookieString - The cookie string
7548
+ */
7391
7549
  static parse(cookieString: string): Cookie;
7550
+
7551
+ /**
7552
+ * Create a new cookie from a name and value and optional options
7553
+ */
7392
7554
  static from(name: string, value: string, options?: CookieInit): Cookie;
7393
7555
  }
7394
7556
 
7395
7557
  /**
7396
7558
  * A Map-like interface for working with collections of cookies.
7559
+ *
7397
7560
  * Implements the `Iterable` interface, allowing use with `for...of` loops.
7398
7561
  */
7399
7562
  class CookieMap implements Iterable<[string, string]> {