bun-types 1.2.21-canary.20250822T140708 → 1.2.21-canary.20250823T140535

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.
@@ -0,0 +1,530 @@
1
+ In Bun, YAML is a first-class citizen alongside JSON and TOML.
2
+
3
+ Bun provides built-in support for YAML files through both runtime APIs and bundler integration. You can
4
+
5
+ - Parse YAML strings with `Bun.YAML.parse`
6
+ - import & require YAML files as modules at runtime (including hot reloading & watch mode support)
7
+ - import & require YAML files in frontend apps via bun's bundler
8
+
9
+ ## Conformance
10
+
11
+ Bun's YAML parser currently passes over 90% of the official YAML test suite. While we're actively working on reaching 100% conformance, the current implementation covers the vast majority of real-world use cases. The parser is written in Zig for optimal performance and is continuously being improved.
12
+
13
+ ## Runtime API
14
+
15
+ ### `Bun.YAML.parse()`
16
+
17
+ Parse a YAML string into a JavaScript object.
18
+
19
+ ```ts
20
+ import { YAML } from "bun";
21
+ const text = `
22
+ name: John Doe
23
+ age: 30
24
+ email: john@example.com
25
+ hobbies:
26
+ - reading
27
+ - coding
28
+ - hiking
29
+ `;
30
+
31
+ const data = YAML.parse(text);
32
+ console.log(data);
33
+ // {
34
+ // name: "John Doe",
35
+ // age: 30,
36
+ // email: "john@example.com",
37
+ // hobbies: ["reading", "coding", "hiking"]
38
+ // }
39
+ ```
40
+
41
+ #### Multi-document YAML
42
+
43
+ When parsing YAML with multiple documents (separated by `---`), `Bun.YAML.parse()` returns an array:
44
+
45
+ ```ts
46
+ const multiDoc = `
47
+ ---
48
+ name: Document 1
49
+ ---
50
+ name: Document 2
51
+ ---
52
+ name: Document 3
53
+ `;
54
+
55
+ const docs = Bun.YAML.parse(multiDoc);
56
+ console.log(docs);
57
+ // [
58
+ // { name: "Document 1" },
59
+ // { name: "Document 2" },
60
+ // { name: "Document 3" }
61
+ // ]
62
+ ```
63
+
64
+ #### Supported YAML Features
65
+
66
+ Bun's YAML parser supports the full YAML 1.2 specification, including:
67
+
68
+ - **Scalars**: strings, numbers, booleans, null values
69
+ - **Collections**: sequences (arrays) and mappings (objects)
70
+ - **Anchors and Aliases**: reusable nodes with `&` and `*`
71
+ - **Tags**: type hints like `!!str`, `!!int`, `!!float`, `!!bool`, `!!null`
72
+ - **Multi-line strings**: literal (`|`) and folded (`>`) scalars
73
+ - **Comments**: using `#`
74
+ - **Directives**: `%YAML` and `%TAG`
75
+
76
+ ```ts
77
+ const yaml = `
78
+ # Employee record
79
+ employee: &emp
80
+ name: Jane Smith
81
+ department: Engineering
82
+ skills:
83
+ - JavaScript
84
+ - TypeScript
85
+ - React
86
+
87
+ manager: *emp # Reference to employee
88
+
89
+ config: !!str 123 # Explicit string type
90
+
91
+ description: |
92
+ This is a multi-line
93
+ literal string that preserves
94
+ line breaks and spacing.
95
+
96
+ summary: >
97
+ This is a folded string
98
+ that joins lines with spaces
99
+ unless there are blank lines.
100
+ `;
101
+
102
+ const data = Bun.YAML.parse(yaml);
103
+ ```
104
+
105
+ #### Error Handling
106
+
107
+ `Bun.YAML.parse()` throws a `SyntaxError` if the YAML is invalid:
108
+
109
+ ```ts
110
+ try {
111
+ Bun.YAML.parse("invalid: yaml: content:");
112
+ } catch (error) {
113
+ console.error("Failed to parse YAML:", error.message);
114
+ }
115
+ ```
116
+
117
+ ## Module Import
118
+
119
+ ### ES Modules
120
+
121
+ You can import YAML files directly as ES modules. The YAML content is parsed and made available as both default and named exports:
122
+
123
+ ```yaml#config.yaml
124
+ database:
125
+ host: localhost
126
+ port: 5432
127
+ name: myapp
128
+
129
+ redis:
130
+ host: localhost
131
+ port: 6379
132
+
133
+ features:
134
+ auth: true
135
+ rateLimit: true
136
+ analytics: false
137
+ ```
138
+
139
+ #### Default Import
140
+
141
+ ```ts#app.ts
142
+ import config from "./config.yaml";
143
+
144
+ console.log(config.database.host); // "localhost"
145
+ console.log(config.redis.port); // 6379
146
+ ```
147
+
148
+ #### Named Imports
149
+
150
+ You can destructure top-level YAML properties as named imports:
151
+
152
+ ```ts
153
+ import { database, redis, features } from "./config.yaml";
154
+
155
+ console.log(database.host); // "localhost"
156
+ console.log(redis.port); // 6379
157
+ console.log(features.auth); // true
158
+ ```
159
+
160
+ Or combine both:
161
+
162
+ ```ts
163
+ import config, { database, features } from "./config.yaml";
164
+
165
+ // Use the full config object
166
+ console.log(config);
167
+
168
+ // Or use specific parts
169
+ if (features.rateLimit) {
170
+ setupRateLimiting(database);
171
+ }
172
+ ```
173
+
174
+ ### CommonJS
175
+
176
+ YAML files can also be required in CommonJS:
177
+
178
+ ```js
179
+ const config = require("./config.yaml");
180
+ console.log(config.database.name); // "myapp"
181
+
182
+ // Destructuring also works
183
+ const { database, redis } = require("./config.yaml");
184
+ console.log(database.port); // 5432
185
+ ```
186
+
187
+ ## Hot Reloading with YAML
188
+
189
+ One of the most powerful features of Bun's YAML support is hot reloading. When you run your application with `bun --hot`, changes to YAML files are automatically detected and reloaded without closing connections
190
+
191
+ ### Configuration Hot Reloading
192
+
193
+ ```yaml#config.yaml
194
+ server:
195
+ port: 3000
196
+ host: localhost
197
+
198
+ features:
199
+ debug: true
200
+ verbose: false
201
+ ```
202
+
203
+ ```ts#server.ts
204
+ import { server, features } from "./config.yaml";
205
+
206
+ console.log(`Starting server on ${server.host}:${server.port}`);
207
+
208
+ if (features.debug) {
209
+ console.log("Debug mode enabled");
210
+ }
211
+
212
+ // Your server code here
213
+ Bun.serve({
214
+ port: server.port,
215
+ hostname: server.host,
216
+ fetch(req) {
217
+ if (features.verbose) {
218
+ console.log(`${req.method} ${req.url}`);
219
+ }
220
+ return new Response("Hello World");
221
+ },
222
+ });
223
+ ```
224
+
225
+ Run with hot reloading:
226
+
227
+ ```bash
228
+ bun --hot server.ts
229
+ ```
230
+
231
+ Now when you modify `config.yaml`, the changes are immediately reflected in your running application. This is perfect for:
232
+
233
+ - Adjusting configuration during development
234
+ - Testing different settings without restarts
235
+ - Live debugging with configuration changes
236
+ - Feature flag toggling
237
+
238
+ ## Configuration Management
239
+
240
+ ### Environment-Based Configuration
241
+
242
+ YAML excels at managing configuration across different environments:
243
+
244
+ ```yaml#config.yaml
245
+ defaults: &defaults
246
+ timeout: 5000
247
+ retries: 3
248
+ cache:
249
+ enabled: true
250
+ ttl: 3600
251
+
252
+ development:
253
+ <<: *defaults
254
+ api:
255
+ url: http://localhost:4000
256
+ key: dev_key_12345
257
+ logging:
258
+ level: debug
259
+ pretty: true
260
+
261
+ staging:
262
+ <<: *defaults
263
+ api:
264
+ url: https://staging-api.example.com
265
+ key: ${STAGING_API_KEY}
266
+ logging:
267
+ level: info
268
+ pretty: false
269
+
270
+ production:
271
+ <<: *defaults
272
+ api:
273
+ url: https://api.example.com
274
+ key: ${PROD_API_KEY}
275
+ cache:
276
+ enabled: true
277
+ ttl: 86400
278
+ logging:
279
+ level: error
280
+ pretty: false
281
+ ```
282
+
283
+ ```ts#app.ts
284
+ import configs from "./config.yaml";
285
+
286
+ const env = process.env.NODE_ENV || "development";
287
+ const config = configs[env];
288
+
289
+ // Environment variables in YAML values can be interpolated
290
+ function interpolateEnvVars(obj: any): any {
291
+ if (typeof obj === "string") {
292
+ return obj.replace(/\${(\w+)}/g, (_, key) => process.env[key] || "");
293
+ }
294
+ if (typeof obj === "object") {
295
+ for (const key in obj) {
296
+ obj[key] = interpolateEnvVars(obj[key]);
297
+ }
298
+ }
299
+ return obj;
300
+ }
301
+
302
+ export default interpolateEnvVars(config);
303
+ ```
304
+
305
+ ### Feature Flags Configuration
306
+
307
+ ```yaml#features.yaml
308
+ features:
309
+ newDashboard:
310
+ enabled: true
311
+ rolloutPercentage: 50
312
+ allowedUsers:
313
+ - admin@example.com
314
+ - beta@example.com
315
+
316
+ experimentalAPI:
317
+ enabled: false
318
+ endpoints:
319
+ - /api/v2/experimental
320
+ - /api/v2/beta
321
+
322
+ darkMode:
323
+ enabled: true
324
+ default: auto # auto, light, dark
325
+ ```
326
+
327
+ ```ts#feature-flags.ts
328
+ import { features } from "./features.yaml";
329
+
330
+ export function isFeatureEnabled(
331
+ featureName: string,
332
+ userEmail?: string,
333
+ ): boolean {
334
+ const feature = features[featureName];
335
+
336
+ if (!feature?.enabled) {
337
+ return false;
338
+ }
339
+
340
+ // Check rollout percentage
341
+ if (feature.rolloutPercentage < 100) {
342
+ const hash = hashCode(userEmail || "anonymous");
343
+ if (hash % 100 >= feature.rolloutPercentage) {
344
+ return false;
345
+ }
346
+ }
347
+
348
+ // Check allowed users
349
+ if (feature.allowedUsers && userEmail) {
350
+ return feature.allowedUsers.includes(userEmail);
351
+ }
352
+
353
+ return true;
354
+ }
355
+
356
+ // Use with hot reloading to toggle features in real-time
357
+ if (isFeatureEnabled("newDashboard", user.email)) {
358
+ renderNewDashboard();
359
+ } else {
360
+ renderLegacyDashboard();
361
+ }
362
+ ```
363
+
364
+ ### Database Configuration
365
+
366
+ ```yaml#database.yaml
367
+ connections:
368
+ primary:
369
+ type: postgres
370
+ host: ${DB_HOST:-localhost}
371
+ port: ${DB_PORT:-5432}
372
+ database: ${DB_NAME:-myapp}
373
+ username: ${DB_USER:-postgres}
374
+ password: ${DB_PASS}
375
+ pool:
376
+ min: 2
377
+ max: 10
378
+ idleTimeout: 30000
379
+
380
+ cache:
381
+ type: redis
382
+ host: ${REDIS_HOST:-localhost}
383
+ port: ${REDIS_PORT:-6379}
384
+ password: ${REDIS_PASS}
385
+ db: 0
386
+
387
+ analytics:
388
+ type: clickhouse
389
+ host: ${ANALYTICS_HOST:-localhost}
390
+ port: 8123
391
+ database: analytics
392
+
393
+ migrations:
394
+ autoRun: ${AUTO_MIGRATE:-false}
395
+ directory: ./migrations
396
+
397
+ seeds:
398
+ enabled: ${SEED_DB:-false}
399
+ directory: ./seeds
400
+ ```
401
+
402
+ ```ts#db.ts
403
+ import { connections, migrations } from "./database.yaml";
404
+ import { createConnection } from "./database-driver";
405
+
406
+ // Parse environment variables with defaults
407
+ function parseConfig(config: any) {
408
+ return JSON.parse(
409
+ JSON.stringify(config).replace(
410
+ /\${([^:-]+)(?::([^}]+))?}/g,
411
+ (_, key, defaultValue) => process.env[key] || defaultValue || "",
412
+ ),
413
+ );
414
+ }
415
+
416
+ const dbConfig = parseConfig(connections);
417
+
418
+ export const db = await createConnection(dbConfig.primary);
419
+ export const cache = await createConnection(dbConfig.cache);
420
+ export const analytics = await createConnection(dbConfig.analytics);
421
+
422
+ // Auto-run migrations if configured
423
+ if (parseConfig(migrations).autoRun === "true") {
424
+ await runMigrations(db, migrations.directory);
425
+ }
426
+ ```
427
+
428
+ ### Bundler Integration
429
+
430
+ When you import YAML files in your application and bundle it with Bun, the YAML is parsed at build time and included as a JavaScript module:
431
+
432
+ ```bash
433
+ bun build app.ts --outdir=dist
434
+ ```
435
+
436
+ This means:
437
+
438
+ - Zero runtime YAML parsing overhead in production
439
+ - Smaller bundle sizes (no YAML parser needed)
440
+ - Type safety with TypeScript
441
+ - Tree-shaking support for unused configuration
442
+
443
+ ### Dynamic Imports
444
+
445
+ YAML files can be dynamically imported, useful for loading configuration on demand:
446
+
447
+ ```ts#Load configuration based on environment
448
+ const env = process.env.NODE_ENV || "development";
449
+ const config = await import(`./configs/${env}.yaml`);
450
+
451
+ // Load user-specific settings
452
+ async function loadUserSettings(userId: string) {
453
+ try {
454
+ const settings = await import(`./users/${userId}/settings.yaml`);
455
+ return settings.default;
456
+ } catch {
457
+ return await import("./users/default-settings.yaml");
458
+ }
459
+ }
460
+ ```
461
+
462
+ ## Use Cases
463
+
464
+ ### Testing and Fixtures
465
+
466
+ YAML works well for test fixtures and seed data:
467
+
468
+ ```yaml#fixtures.yaml
469
+ users:
470
+ - id: 1
471
+ name: Alice
472
+ email: alice@example.com
473
+ role: admin
474
+ - id: 2
475
+ name: Bob
476
+ email: bob@example.com
477
+ role: user
478
+
479
+ products:
480
+ - sku: PROD-001
481
+ name: Widget
482
+ price: 19.99
483
+ stock: 100
484
+ ```
485
+
486
+ ```ts
487
+ import fixtures from "./fixtures.yaml";
488
+ import { db } from "./database";
489
+
490
+ async function seed() {
491
+ await db.user.createMany({ data: fixtures.users });
492
+ await db.product.createMany({ data: fixtures.products });
493
+ }
494
+ ```
495
+
496
+ ### API Definitions
497
+
498
+ YAML is commonly used for API specifications like OpenAPI:
499
+
500
+ ```yaml#api.yaml
501
+ openapi: 3.0.0
502
+ info:
503
+ title: My API
504
+ version: 1.0.0
505
+
506
+ paths:
507
+ /users:
508
+ get:
509
+ summary: List users
510
+ responses:
511
+ 200:
512
+ description: Success
513
+ ```
514
+
515
+ ```ts#api.ts
516
+ import apiSpec from "./api.yaml";
517
+ import { generateRoutes } from "./router";
518
+
519
+ const routes = generateRoutes(apiSpec);
520
+ ```
521
+
522
+ ## Performance
523
+
524
+ Bun's YAML parser is implemented in Zig for optimal performance:
525
+
526
+ - **Fast parsing**: Native implementation provides excellent parse speed
527
+ - **Build-time optimization**: When importing YAML files, parsing happens at build time, resulting in zero runtime overhead
528
+ - **Memory efficient**: Streaming parser design minimizes memory usage
529
+ - **Hot reload support**: changes to YAML files trigger instant reloads without server restarts when used with `bun --hot` or Bun's [frontend dev server](/docs/bundler/fullstack)
530
+ - **Error recovery**: Detailed error messages with line and column information
@@ -408,16 +408,119 @@ $ bun build --compile --asset-naming="[name].[ext]" ./index.ts
408
408
 
409
409
  To trim down the size of the executable a little, pass `--minify` to `bun build --compile`. This uses Bun's minifier to reduce the code size. Overall though, Bun's binary is still way too big and we need to make it smaller.
410
410
 
411
+ ## Using Bun.build() API
412
+
413
+ You can also generate standalone executables using the `Bun.build()` JavaScript API. This is useful when you need programmatic control over the build process.
414
+
415
+ ### Basic usage
416
+
417
+ ```js
418
+ await Bun.build({
419
+ entrypoints: ["./app.ts"],
420
+ outdir: "./dist",
421
+ compile: {
422
+ target: "bun-windows-x64",
423
+ outfile: "myapp.exe",
424
+ },
425
+ });
426
+ ```
427
+
428
+ ### Windows metadata with Bun.build()
429
+
430
+ When targeting Windows, you can specify metadata through the `windows` object:
431
+
432
+ ```js
433
+ await Bun.build({
434
+ entrypoints: ["./app.ts"],
435
+ outdir: "./dist",
436
+ compile: {
437
+ target: "bun-windows-x64",
438
+ outfile: "myapp.exe",
439
+ windows: {
440
+ title: "My Application",
441
+ publisher: "My Company Inc",
442
+ version: "1.2.3.4",
443
+ description: "A powerful application built with Bun",
444
+ copyright: "© 2024 My Company Inc",
445
+ hideConsole: false, // Set to true for GUI applications
446
+ icon: "./icon.ico", // Path to icon file
447
+ },
448
+ },
449
+ });
450
+ ```
451
+
452
+ ### Cross-compilation with Bun.build()
453
+
454
+ You can cross-compile for different platforms:
455
+
456
+ ```js
457
+ // Build for multiple platforms
458
+ const platforms = [
459
+ { target: "bun-windows-x64", outfile: "app-windows.exe" },
460
+ { target: "bun-linux-x64", outfile: "app-linux" },
461
+ { target: "bun-darwin-arm64", outfile: "app-macos" },
462
+ ];
463
+
464
+ for (const platform of platforms) {
465
+ await Bun.build({
466
+ entrypoints: ["./app.ts"],
467
+ outdir: "./dist",
468
+ compile: platform,
469
+ });
470
+ }
471
+ ```
472
+
411
473
  ## Windows-specific flags
412
474
 
413
- When compiling a standalone executable on Windows, there are two platform-specific options that can be used to customize metadata on the generated `.exe` file:
475
+ When compiling a standalone executable for Windows, there are several platform-specific options that can be used to customize the generated `.exe` file:
476
+
477
+ ### Visual customization
478
+
479
+ - `--windows-icon=path/to/icon.ico` - Set the executable file icon
480
+ - `--windows-hide-console` - Disable the background terminal window (useful for GUI applications)
481
+
482
+ ### Metadata customization
483
+
484
+ You can embed version information and other metadata into your Windows executable:
485
+
486
+ - `--windows-title <STR>` - Set the product name (appears in file properties)
487
+ - `--windows-publisher <STR>` - Set the company name
488
+ - `--windows-version <STR>` - Set the version number (e.g. "1.2.3.4")
489
+ - `--windows-description <STR>` - Set the file description
490
+ - `--windows-copyright <STR>` - Set the copyright information
491
+
492
+ #### Example with all metadata flags:
493
+
494
+ ```sh
495
+ bun build --compile ./app.ts \
496
+ --outfile myapp.exe \
497
+ --windows-title "My Application" \
498
+ --windows-publisher "My Company Inc" \
499
+ --windows-version "1.2.3.4" \
500
+ --windows-description "A powerful application built with Bun" \
501
+ --windows-copyright "© 2024 My Company Inc"
502
+ ```
503
+
504
+ This metadata will be visible in Windows Explorer when viewing the file properties:
505
+
506
+ 1. Right-click the executable in Windows Explorer
507
+ 2. Select "Properties"
508
+ 3. Go to the "Details" tab
509
+
510
+ #### Version string format
511
+
512
+ The `--windows-version` flag accepts version strings in the following formats:
513
+
514
+ - `"1"` - Will be normalized to "1.0.0.0"
515
+ - `"1.2"` - Will be normalized to "1.2.0.0"
516
+ - `"1.2.3"` - Will be normalized to "1.2.3.0"
517
+ - `"1.2.3.4"` - Full version format
414
518
 
415
- - `--windows-icon=path/to/icon.ico` to customize the executable file icon.
416
- - `--windows-hide-console` to disable the background terminal, which can be used for applications that do not need a TTY.
519
+ Each version component must be a number between 0 and 65535.
417
520
 
418
521
  {% callout %}
419
522
 
420
- These flags currently cannot be used when cross-compiling because they depend on Windows APIs.
523
+ These flags currently cannot be used when cross-compiling because they depend on Windows APIs. They are only available when building on Windows itself.
421
524
 
422
525
  {% /callout %}
423
526
 
package/docs/cli/pm.md CHANGED
@@ -213,7 +213,7 @@ To display current package version and help:
213
213
 
214
214
  ```bash
215
215
  $ bun pm version
216
- bun pm version v1.2.21-canary.20250822T140708 (ca7428e9)
216
+ bun pm version v1.2.21-canary.20250823T140535 (ca7428e9)
217
217
  Current package version: v1.0.0
218
218
 
219
219
  Increment:
@@ -7,7 +7,7 @@ Use `bun publish` to publish a package to the npm registry.
7
7
  $ bun publish
8
8
 
9
9
  ## Output
10
- bun publish v1.2.21-canary.20250822T140708 (ca7428e9)
10
+ bun publish v1.2.21-canary.20250823T140535 (ca7428e9)
11
11
 
12
12
  packed 203B package.json
13
13
  packed 224B README.md
@@ -9,7 +9,7 @@ $ bunx nuxi init my-nuxt-app
9
9
  ✔ Which package manager would you like to use?
10
10
  bun
11
11
  ◐ Installing dependencies...
12
- bun install v1.2.21-canary.20250822T140708 (16b4bf34)
12
+ bun install v1.2.21-canary.20250823T140535 (16b4bf34)
13
13
  + @nuxt/devtools@0.8.2
14
14
  + nuxt@3.7.0
15
15
  785 packages installed [2.67s]
@@ -15,7 +15,7 @@ This will add the package to `peerDependencies` in `package.json`.
15
15
  ```json-diff
16
16
  {
17
17
  "peerDependencies": {
18
- + "@types/bun": "^1.2.21-canary.20250822T140708"
18
+ + "@types/bun": "^1.2.21-canary.20250823T140535"
19
19
  }
20
20
  }
21
21
  ```
@@ -27,7 +27,7 @@ Running `bun install` will install peer dependencies by default, unless marked o
27
27
  ```json-diff
28
28
  {
29
29
  "peerDependencies": {
30
- "@types/bun": "^1.2.21-canary.20250822T140708"
30
+ "@types/bun": "^1.2.21-canary.20250823T140535"
31
31
  },
32
32
  "peerDependenciesMeta": {
33
33
  + "@types/bun": {
@@ -97,7 +97,7 @@ $ bun update
97
97
  $ bun update @types/bun --latest
98
98
 
99
99
  # Update a dependency to a specific version
100
- $ bun update @types/bun@1.2.21-canary.20250822T140708
100
+ $ bun update @types/bun@1.2.21-canary.20250823T140535
101
101
 
102
102
  # Update all dependencies to the latest versions
103
103
  $ bun update --latest