bun-types 1.1.39-canary.20241209T140550 → 1.1.39-canary.20241211T140552

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/ambient.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ declare module "*.txt" {
2
+ var text: string;
3
+ export = text;
4
+ }
5
+
6
+ declare module "*.toml" {
7
+ var contents: any;
8
+ export = contents;
9
+ }
package/bun.d.ts CHANGED
@@ -1605,6 +1605,26 @@ declare module "bun" {
1605
1605
  * https://nodejs.org/api/packages.html#exports
1606
1606
  */
1607
1607
  conditions?: Array<string> | string;
1608
+
1609
+ /**
1610
+ * Controls how environment variables are handled during bundling.
1611
+ *
1612
+ * Can be one of:
1613
+ * - `"inline"`: Injects environment variables into the bundled output by converting `process.env.FOO`
1614
+ * references to string literals containing the actual environment variable values
1615
+ * - `"disable"`: Disables environment variable injection entirely
1616
+ * - A string ending in `*`: Inlines environment variables that match the given prefix.
1617
+ * For example, `"MY_PUBLIC_*"` will only include env vars starting with "MY_PUBLIC_"
1618
+ *
1619
+ * @example
1620
+ * ```ts
1621
+ * Bun.build({
1622
+ * env: "MY_PUBLIC_*",
1623
+ * entrypoints: ["src/index.ts"],
1624
+ * })
1625
+ * ```
1626
+ */
1627
+ env?: "inline" | "disable" | `${string}*`;
1608
1628
  minify?:
1609
1629
  | boolean
1610
1630
  | {
@@ -546,6 +546,113 @@ export type ImportKind =
546
546
 
547
547
  By design, the manifest is a simple JSON object that can easily be serialized or written to disk. It is also compatible with esbuild's [`metafile`](https://esbuild.github.io/api/#metafile) format. -->
548
548
 
549
+ ### `env`
550
+
551
+ Controls how environment variables are handled during bundling. Internally, this uses `define` to inject environment variables into the bundle, but makes it easier to specify the environment variables to inject.
552
+
553
+ #### `env: "inline"`
554
+
555
+ Injects environment variables into the bundled output by converting `process.env.FOO` references to string literals containing the actual environment variable values.
556
+
557
+ {% codetabs group="a" %}
558
+
559
+ ```ts#JavaScript
560
+ await Bun.build({
561
+ entrypoints: ['./index.tsx'],
562
+ outdir: './out',
563
+ env: "inline",
564
+ })
565
+ ```
566
+
567
+ ```bash#CLI
568
+ $ FOO=bar BAZ=123 bun build ./index.tsx --outdir ./out --env inline
569
+ ```
570
+
571
+ {% /codetabs %}
572
+
573
+ For the input below:
574
+
575
+ ```js#input.js
576
+ console.log(process.env.FOO);
577
+ console.log(process.env.BAZ);
578
+ ```
579
+
580
+ The generated bundle will contain the following code:
581
+
582
+ ```js#output.js
583
+ console.log("bar");
584
+ console.log("123");
585
+ ```
586
+
587
+ #### `env: "PUBLIC_*"` (prefix)
588
+
589
+ Inlines environment variables matching the given prefix (the part before the `*` character), replacing `process.env.FOO` with the actual environment variable value. This is useful for selectively inlining environment variables for things like public-facing URLs or client-side tokens, without worrying about injecting private credentials into output bundles.
590
+
591
+ {% codetabs group="a" %}
592
+
593
+ ```ts#JavaScript
594
+ await Bun.build({
595
+ entrypoints: ['./index.tsx'],
596
+ outdir: './out',
597
+
598
+ // Inline all env vars that start with "ACME_PUBLIC_"
599
+ env: "ACME_PUBLIC_*",
600
+ })
601
+ ```
602
+
603
+ ```bash#CLI
604
+ $ FOO=bar BAZ=123 ACME_PUBLIC_URL=https://acme.com bun build ./index.tsx --outdir ./out --env 'ACME_PUBLIC_*'
605
+ ```
606
+
607
+ {% /codetabs %}
608
+
609
+ For example, given the following environment variables:
610
+
611
+ ```bash
612
+ $ FOO=bar BAZ=123 ACME_PUBLIC_URL=https://acme.com
613
+ ```
614
+
615
+ And source code:
616
+
617
+ ```ts#index.tsx
618
+ console.log(process.env.FOO);
619
+ console.log(process.env.ACME_PUBLIC_URL);
620
+ console.log(process.env.BAZ);
621
+ ```
622
+
623
+ The generated bundle will contain the following code:
624
+
625
+ ```js
626
+ console.log(process.env.FOO);
627
+ console.log("https://acme.com");
628
+ console.log(process.env.BAZ);
629
+ ```
630
+
631
+ #### `env: "disable"`
632
+
633
+ Disables environment variable injection entirely.
634
+
635
+ For example, given the following environment variables:
636
+
637
+ ```bash
638
+ $ FOO=bar BAZ=123 ACME_PUBLIC_URL=https://acme.com
639
+ ```
640
+
641
+ And source code:
642
+
643
+ ```ts#index.tsx
644
+ console.log(process.env.FOO);
645
+ console.log(process.env.ACME_PUBLIC_URL);
646
+ console.log(process.env.BAZ);
647
+ ```
648
+
649
+ The generated bundle will contain the following code:
650
+
651
+ ```js
652
+ console.log(process.env.FOO);
653
+ console.log(process.env.BAZ);
654
+ ```
655
+
549
656
  ### `sourcemap`
550
657
 
551
658
  Specifies the type of sourcemap to generate.
@@ -49,7 +49,7 @@ Next, add these preload scripts to your `bunfig.toml` (you can also have everyth
49
49
 
50
50
  ```toml#bunfig.toml
51
51
  [test]
52
- preload = ["happydom.ts", "testing-library.ts"]
52
+ preload = ["./happydom.ts", "./testing-library.ts"]
53
53
  ```
54
54
  ---
55
55
 
@@ -84,4 +84,4 @@ test('Can use Testing Library', () => {
84
84
 
85
85
  ---
86
86
 
87
- Refer to the [Testing Library docs](https://testing-library.com/), [Happy DOM repo](https://github.com/capricorn86/happy-dom) and [Docs > Test runner > DOM](https://bun.sh/docs/test/dom) for complete documentation on writing browser tests with Bun.
87
+ Refer to the [Testing Library docs](https://testing-library.com/), [Happy DOM repo](https://github.com/capricorn86/happy-dom) and [Docs > Test runner > DOM](https://bun.sh/docs/test/dom) for complete documentation on writing browser tests with Bun.
@@ -0,0 +1,199 @@
1
+ {% callout %}
2
+
3
+ This document is for maintainers and contributors to Bun, and describes internal implementation details.
4
+
5
+ {% /callout %}
6
+
7
+ The new bindings generator, introduced to the codebase in Dec 2024, scans for
8
+ `*.bind.ts` to find function and class definition, and generates glue code to
9
+ interop between JavaScript and native code.
10
+
11
+ There are currently other code generators and systems that achieve similar
12
+ purposes. The following will all eventually be completely phased out in favor of
13
+ this one:
14
+
15
+ - "Classes generator", converting `*.classes.ts` for custom classes.
16
+ - "JS2Native", allowing ad-hoc calls from `src/js` to native code.
17
+
18
+ ## Creating JS Functions in Zig
19
+
20
+ Given a file implementing a simple function, such as `add`
21
+
22
+ ```zig#src/bun.js/math.zig
23
+ pub fn add(global: *JSC.JSGlobalObject, a: i32, b: i32) !i32 {
24
+ return std.math.add(i32, a, b) catch {
25
+ // Binding functions can return `error.OutOfMemory` and `error.JSError`.
26
+ // Others like `error.Overflow` from `std.math.add` must be converted.
27
+ // Remember to be descriptive.
28
+ return global.throwPretty("Integer overflow while adding", .{});
29
+ };
30
+ }
31
+
32
+ const gen = bun.gen.math; // "math" being this file's basename
33
+
34
+ const std = @import("std");
35
+ const bun = @import("root").bun;
36
+ const JSC = bun.JSC;
37
+ ```
38
+
39
+ Then describe the API schema using a `.bind.ts` function. The binding file goes next to the Zig file.
40
+
41
+ ```ts#src/bun.js/math.bind.ts
42
+ import { t, fn } from 'bindgen';
43
+
44
+ export const add = fn({
45
+ args: {
46
+ global: t.globalObject,
47
+ a: t.i32,
48
+ b: t.i32.default(1),
49
+ },
50
+ ret: t.i32,
51
+ });
52
+ ```
53
+
54
+ This function declaration is equivalent to:
55
+
56
+ ```ts
57
+ /**
58
+ * Throws if zero arguments are provided.
59
+ * Wraps out of range numbers using modulo.
60
+ */
61
+ declare function add(a: number, b: number = 1): number;
62
+ ```
63
+
64
+ The code generator will provide `bun.gen.math.jsAdd`, which is the native function implementation. To pass to JavaScript, use `bun.gen.math.createAddCallback(global)`
65
+
66
+ ## Strings
67
+
68
+ The type for receiving strings is one of [`t.DOMString`](https://webidl.spec.whatwg.org/#idl-DOMString), [`t.ByteString`](https://webidl.spec.whatwg.org/#idl-ByteString), and [`t.USVString`](https://webidl.spec.whatwg.org/#idl-USVString). These map directly to their WebIDL counterparts, and have slightly different conversion logic. Bindgen will pass BunString to native code in all cases.
69
+
70
+ When in doubt, use DOMString.
71
+
72
+ `t.UTF8String` can be used in place of `t.DOMString`, but will call `bun.String.toUTF8`. The native callback gets `[]const u8` (WTF-8 data) passed to native code, freeing it after the function returns.
73
+
74
+ TLDRs from WebIDL spec:
75
+
76
+ - ByteString can only contain valid latin1 characters. It is not safe to assume bun.String is already in 8-bit format, but it is extremely likely.
77
+ - USVString will not contain invalid surrogate pairs, aka text that can be represented correctly in UTF-8.
78
+ - DOMString is the loosest but also most recommended strategy.
79
+
80
+ ## Function Variants
81
+
82
+ A `variants` can specify multiple variants (also known as overloads).
83
+
84
+ ```ts#src/bun.js/math.bind.ts
85
+ import { t, fn } from 'bindgen';
86
+
87
+ export const action = fn({
88
+ variants: [
89
+ {
90
+ args: {
91
+ a: t.i32,
92
+ },
93
+ ret: t.i32,
94
+ },
95
+ {
96
+ args: {
97
+ a: t.DOMString,
98
+ },
99
+ ret: t.DOMString,
100
+ },
101
+ ]
102
+ });
103
+ ```
104
+
105
+ In Zig, each variant gets a number, based on the order the schema defines.
106
+
107
+ ```
108
+ fn action1(a: i32) i32 {
109
+ return a;
110
+ }
111
+
112
+ fn action2(a: bun.String) bun.String {
113
+ return a;
114
+ }
115
+ ```
116
+
117
+ ## `t.dictionary`
118
+
119
+ A `dictionary` is a definition for a JavaScript object, typically as a function inputs. For function outputs, it is usually a smarter idea to declare a class type to add functions and destructuring.
120
+
121
+ ## Enumerations
122
+
123
+ To use [WebIDL's enumeration](https://webidl.spec.whatwg.org/#idl-enums) type, use either:
124
+
125
+ - `t.stringEnum`: Create and codegen a new enum type.
126
+ - `t.zigEnum`: Derive a bindgen type off of an existing enum in the codebase.
127
+
128
+ An example of `stringEnum` as used in `fmt.zig` / `bun:internal-for-testing`
129
+
130
+ ```ts
131
+ export const Formatter = t.stringEnum(
132
+ "highlight-javascript",
133
+ "escape-powershell",
134
+ );
135
+
136
+ export const fmtString = fn({
137
+ args: {
138
+ global: t.globalObject,
139
+ code: t.UTF8String,
140
+ formatter: Formatter,
141
+ },
142
+ ret: t.DOMString,
143
+ });
144
+ ```
145
+
146
+ WebIDL strongly encourages using kebab case for enumeration values, to be consistent with existing Web APIs.
147
+
148
+ ### Deriving enums from Zig code
149
+
150
+ TODO: zigEnum
151
+
152
+ ## `t.oneOf`
153
+
154
+ A `oneOf` is a union between two or more types. It is represented by `union(enum)` in Zig.
155
+
156
+ TODO:
157
+
158
+ ## Attributes
159
+
160
+ There are set of attributes that can be chained onto `t.*` types. On all types there are:
161
+
162
+ - `.required`, in dictionary parameters only
163
+ - `.optional`, in function arguments only
164
+ - `.default(T)`
165
+
166
+ When a value is optional, it is lowered to a Zig optional.
167
+
168
+ Depending on the type, there are more attributes available. See the type definitions in auto-complete for more details. Note that one of the above three can only be applied, and they must be applied at the end.
169
+
170
+ ### Integer Attributes
171
+
172
+ Integer types allow customizing the overflow behavior with `clamp` or `enforceRange`
173
+
174
+ ```ts
175
+ import { t, fn } from "bindgen";
176
+
177
+ export const add = fn({
178
+ args: {
179
+ global: t.globalObject,
180
+ // enforce in i32 range
181
+ a: t.i32.enforceRange(),
182
+ // clamp to u16 range
183
+ c: t.u16,
184
+ // enforce in arbitrary range, with a default if not provided
185
+ b: t.i32.enforceRange(0, 1000).default(5),
186
+ // clamp to arbitrary range, or null
187
+ d: t.u16.clamp(0, 10).optional,
188
+ },
189
+ ret: t.i32,
190
+ });
191
+ ```
192
+
193
+ ## Callbacks
194
+
195
+ TODO
196
+
197
+ ## Classes
198
+
199
+ TODO
package/globals.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- export {};
2
-
3
1
  type _ReadableStream<T> = typeof globalThis extends {
4
2
  onerror: any;
5
3
  ReadableStream: infer T;
@@ -146,16 +144,6 @@ import type {
146
144
  import type { MessagePort } from "worker_threads";
147
145
  import type { WebSocket as _WebSocket } from "ws";
148
146
 
149
- declare module "*.txt" {
150
- var text: string;
151
- export = text;
152
- }
153
-
154
- declare module "*.toml" {
155
- var contents: any;
156
- export = contents;
157
- }
158
-
159
147
  declare global {
160
148
  var Bun: typeof import("bun");
161
149
 
@@ -1944,10 +1932,10 @@ declare global {
1944
1932
  readonly main: boolean;
1945
1933
 
1946
1934
  /** Alias of `import.meta.dir`. Exists for Node.js compatibility */
1947
- readonly dirname: string;
1935
+ dirname: string;
1948
1936
 
1949
1937
  /** Alias of `import.meta.path`. Exists for Node.js compatibility */
1950
- readonly filename: string;
1938
+ filename: string;
1951
1939
  }
1952
1940
 
1953
1941
  /**
package/index.d.ts CHANGED
@@ -20,3 +20,4 @@
20
20
  /// <reference path="./sqlite.d.ts" />
21
21
  /// <reference path="./wasm.d.ts" />
22
22
  /// <reference path="./deprecated.d.ts" />
23
+ /// <reference path="./ambient.d.ts" />
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.1.39-canary.20241209T140550",
2
+ "version": "1.1.39-canary.20241211T140552",
3
3
  "name": "bun-types",
4
4
  "license": "MIT",
5
5
  "main": "",
package/sqlite.d.ts CHANGED
@@ -1152,7 +1152,7 @@ declare module "bun:sqlite" {
1152
1152
  *
1153
1153
  * @since Bun v1.1.14
1154
1154
  */
1155
- interface Changes {
1155
+ export interface Changes {
1156
1156
  /**
1157
1157
  * The number of rows changed by the last `run` or `exec` call.
1158
1158
  */