bun-types 1.0.25 → 1.0.26
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 +4601 -0
- package/deprecated.d.ts +74 -0
- package/fetch.d.ts +53 -0
- package/ffi.d.ts +1030 -0
- package/globals.d.ts +2000 -0
- package/html-rewriter.d.ts +126 -0
- package/index.d.ts +20 -0
- package/jsc.d.ts +214 -0
- package/overrides.d.ts +64 -0
- package/package.json +28 -15
- package/sqlite.d.ts +831 -0
- package/test.d.ts +1903 -0
- package/wasm.d.ts +270 -0
- package/tsconfig.json +0 -22
- package/types.d.ts +0 -11950
package/bun.d.ts
ADDED
|
@@ -0,0 +1,4601 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bun.js runtime APIs
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import {file} from 'bun';
|
|
8
|
+
*
|
|
9
|
+
* // Log the file to the console
|
|
10
|
+
* const input = await file('/path/to/file.txt').text();
|
|
11
|
+
* console.log(input);
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* This module aliases `globalThis.Bun`.
|
|
15
|
+
*/
|
|
16
|
+
declare module "bun" {
|
|
17
|
+
import type { Encoding as CryptoEncoding } from "crypto";
|
|
18
|
+
|
|
19
|
+
interface Env {
|
|
20
|
+
NODE_ENV?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Can be used to change the default timezone at runtime
|
|
23
|
+
*/
|
|
24
|
+
TZ?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The environment variables of the process
|
|
29
|
+
*
|
|
30
|
+
* Defaults to `process.env` as it was when the current Bun process launched.
|
|
31
|
+
*
|
|
32
|
+
* Changes to `process.env` at runtime won't automatically be reflected in the default value. For that, you can pass `process.env` explicitly.
|
|
33
|
+
*/
|
|
34
|
+
const env: NodeJS.ProcessEnv;
|
|
35
|
+
/**
|
|
36
|
+
* 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.
|
|
37
|
+
*/
|
|
38
|
+
const argv: string[];
|
|
39
|
+
const origin: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Find the path to an executable, similar to typing which in your terminal. Reads the `PATH` environment variable unless overridden with `options.PATH`.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} command The name of the executable or script
|
|
45
|
+
* @param {string} options.PATH Overrides the PATH environment variable
|
|
46
|
+
* @param {string} options.cwd Limits the search to a particular directory in which to searc
|
|
47
|
+
*/
|
|
48
|
+
function which(command: string, options?: { PATH?: string; cwd?: string }): string | null;
|
|
49
|
+
|
|
50
|
+
export type ShellFunction = (input: Uint8Array) => Uint8Array;
|
|
51
|
+
|
|
52
|
+
export type ShellExpression =
|
|
53
|
+
| string
|
|
54
|
+
| { raw: string }
|
|
55
|
+
| Subprocess
|
|
56
|
+
| SpawnOptions.Readable
|
|
57
|
+
| SpawnOptions.Writable
|
|
58
|
+
| ReadableStream;
|
|
59
|
+
|
|
60
|
+
class ShellPromise extends Promise<ShellOutput> {
|
|
61
|
+
get stdin(): WritableStream;
|
|
62
|
+
/**
|
|
63
|
+
* Change the current working directory of the shell.
|
|
64
|
+
* @param newCwd - The new working directory
|
|
65
|
+
*/
|
|
66
|
+
cwd(newCwd: string): this;
|
|
67
|
+
/**
|
|
68
|
+
* Set environment variables for the shell.
|
|
69
|
+
* @param newEnv - The new environment variables
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* await $`echo $FOO`.env({ ...process.env, FOO: "LOL!" })
|
|
74
|
+
* expect(stdout.toString()).toBe("LOL!");
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
env(newEnv: Record<string, string> | undefined): this;
|
|
78
|
+
/**
|
|
79
|
+
* By default, the shell will write to the current process's stdout and stderr, as well as buffering that output.
|
|
80
|
+
*
|
|
81
|
+
* This configures the shell to only buffer the output.
|
|
82
|
+
*/
|
|
83
|
+
quiet(): this;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Read from stdout as a string, line by line
|
|
87
|
+
*
|
|
88
|
+
* Automatically calls {@link quiet} to disable echoing to stdout.
|
|
89
|
+
*/
|
|
90
|
+
lines(): AsyncIterable<string>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Read from stdout as a string
|
|
94
|
+
*
|
|
95
|
+
* Automatically calls {@link quiet} to disable echoing to stdout.
|
|
96
|
+
* @param encoding - The encoding to use when decoding the output
|
|
97
|
+
* @returns A promise that resolves with stdout as a string
|
|
98
|
+
* @example
|
|
99
|
+
*
|
|
100
|
+
* ## Read as UTF-8 string
|
|
101
|
+
*
|
|
102
|
+
* ```ts
|
|
103
|
+
* const output = await $`echo hello`.text();
|
|
104
|
+
* console.log(output); // "hello\n"
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* ## Read as base64 string
|
|
108
|
+
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* const output = await $`echo ${atob("hello")}`.text("base64");
|
|
111
|
+
* console.log(output); // "hello\n"
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
*/
|
|
115
|
+
text(encoding?: BufferEncoding): Promise<string>;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Read from stdout as a JSON object
|
|
119
|
+
*
|
|
120
|
+
* Automatically calls {@link quiet}
|
|
121
|
+
*
|
|
122
|
+
* @returns A promise that resolves with stdout as a JSON object
|
|
123
|
+
* @example
|
|
124
|
+
*
|
|
125
|
+
* ```ts
|
|
126
|
+
* const output = await $`echo '{"hello": 123}'`.json();
|
|
127
|
+
* console.log(output); // { hello: 123 }
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
*/
|
|
131
|
+
json(): Promise<any>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Read from stdout as an ArrayBuffer
|
|
135
|
+
*
|
|
136
|
+
* Automatically calls {@link quiet}
|
|
137
|
+
* @returns A promise that resolves with stdout as an ArrayBuffer
|
|
138
|
+
* @example
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* const output = await $`echo hello`.arrayBuffer();
|
|
142
|
+
* console.log(output); // ArrayBuffer { byteLength: 6 }
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Read from stdout as a Blob
|
|
149
|
+
*
|
|
150
|
+
* Automatically calls {@link quiet}
|
|
151
|
+
* @returns A promise that resolves with stdout as a Blob
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const output = await $`echo hello`.blob();
|
|
155
|
+
* console.log(output); // Blob { size: 6, type: "" }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
blob(): Promise<Blob>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface ShellConstructor {
|
|
162
|
+
new (): Shell;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface Shell {
|
|
166
|
+
(strings: TemplateStringsArray, ...expressions: ShellExpression[]): ShellPromise;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Perform bash-like brace expansion on the given pattern.
|
|
170
|
+
* @param pattern - Brace pattern to expand
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```js
|
|
174
|
+
* const result = braces('index.{js,jsx,ts,tsx}');
|
|
175
|
+
* console.log(result) // ['index.js', 'index.jsx', 'index.ts', 'index.tsx']
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
braces(pattern: string): string[];
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Escape strings for input into shell commands.
|
|
182
|
+
* @param input
|
|
183
|
+
*/
|
|
184
|
+
escape(input: string): string;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
*
|
|
188
|
+
* Change the default environment variables for shells created by this instance.
|
|
189
|
+
*
|
|
190
|
+
* @param newEnv Default environment variables to use for shells created by this instance.
|
|
191
|
+
* @default process.env
|
|
192
|
+
*
|
|
193
|
+
* ## Example
|
|
194
|
+
*
|
|
195
|
+
* ```js
|
|
196
|
+
* import {$} from 'bun';
|
|
197
|
+
* $.env({ BUN: "bun" });
|
|
198
|
+
* await $`echo $BUN`;
|
|
199
|
+
* // "bun"
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
env(newEnv?: Record<string, string | undefined>): this;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
*
|
|
206
|
+
* @param newCwd Default working directory to use for shells created by this instance.
|
|
207
|
+
*/
|
|
208
|
+
cwd(newCwd?: string): this;
|
|
209
|
+
|
|
210
|
+
readonly ShellPromise: typeof ShellPromise;
|
|
211
|
+
readonly Shell: ShellConstructor;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export interface ShellOutput {
|
|
215
|
+
readonly stdout: Buffer;
|
|
216
|
+
readonly stderr: Buffer;
|
|
217
|
+
readonly exitCode: number;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export const $: Shell;
|
|
221
|
+
|
|
222
|
+
interface TOML {
|
|
223
|
+
/**
|
|
224
|
+
* Parse a TOML string into a JavaScript object.
|
|
225
|
+
*
|
|
226
|
+
* @param {string} command The name of the executable or script
|
|
227
|
+
* @param {string} options.PATH Overrides the PATH environment variable
|
|
228
|
+
* @param {string} options.cwd Limits the search to a particular directory in which to searc
|
|
229
|
+
*/
|
|
230
|
+
parse(input: string): object;
|
|
231
|
+
}
|
|
232
|
+
const TOML: TOML;
|
|
233
|
+
|
|
234
|
+
type Serve<WebSocketDataType = undefined> =
|
|
235
|
+
| ServeOptions
|
|
236
|
+
| TLSServeOptions
|
|
237
|
+
| UnixServeOptions
|
|
238
|
+
| UnixTLSServeOptions
|
|
239
|
+
| WebSocketServeOptions<WebSocketDataType>
|
|
240
|
+
| TLSWebSocketServeOptions<WebSocketDataType>
|
|
241
|
+
| UnixWebSocketServeOptions<WebSocketDataType>
|
|
242
|
+
| UnixTLSWebSocketServeOptions<WebSocketDataType>;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Start a fast HTTP server.
|
|
246
|
+
*
|
|
247
|
+
* @param options Server options (port defaults to $PORT || 3000)
|
|
248
|
+
*
|
|
249
|
+
* -----
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* Bun.serve({
|
|
255
|
+
* fetch(req: Request): Response | Promise<Response> {
|
|
256
|
+
* return new Response("Hello World!");
|
|
257
|
+
* },
|
|
258
|
+
*
|
|
259
|
+
* // Optional port number - the default value is 3000
|
|
260
|
+
* port: process.env.PORT || 3000,
|
|
261
|
+
* });
|
|
262
|
+
* ```
|
|
263
|
+
* -----
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
*
|
|
267
|
+
* Send a file
|
|
268
|
+
*
|
|
269
|
+
* ```ts
|
|
270
|
+
* Bun.serve({
|
|
271
|
+
* fetch(req: Request): Response | Promise<Response> {
|
|
272
|
+
* return new Response(Bun.file("./package.json"));
|
|
273
|
+
* },
|
|
274
|
+
*
|
|
275
|
+
* // Optional port number - the default value is 3000
|
|
276
|
+
* port: process.env.PORT || 3000,
|
|
277
|
+
* });
|
|
278
|
+
* ```
|
|
279
|
+
*/
|
|
280
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
281
|
+
function serve<T>(options: Serve<T>): Server;
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Synchronously resolve a `moduleId` as though it were imported from `parent`
|
|
285
|
+
*
|
|
286
|
+
* On failure, throws a `ResolveMessage`
|
|
287
|
+
*/
|
|
288
|
+
// tslint:disable-next-line:unified-signatures
|
|
289
|
+
function resolveSync(moduleId: string, parent: string): string;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Resolve a `moduleId` as though it were imported from `parent`
|
|
293
|
+
*
|
|
294
|
+
* On failure, throws a `ResolveMessage`
|
|
295
|
+
*
|
|
296
|
+
* For now, use the sync version. There is zero performance benefit to using this async version. It exists for future-proofing.
|
|
297
|
+
*/
|
|
298
|
+
// tslint:disable-next-line:unified-signatures
|
|
299
|
+
function resolve(moduleId: string, parent: string): Promise<string>;
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Use the fastest syscalls available to copy from `input` into `destination`.
|
|
303
|
+
*
|
|
304
|
+
* If `destination` exists, it must be a regular file or symlink to a file. If `destination`'s directory does not exist, it will be created by default.
|
|
305
|
+
*
|
|
306
|
+
* @param destination The file or file path to write to
|
|
307
|
+
* @param input The data to copy into `destination`.
|
|
308
|
+
* @returns A promise that resolves with the number of bytes written.
|
|
309
|
+
*/
|
|
310
|
+
// tslint:disable-next-line:unified-signatures
|
|
311
|
+
function write(
|
|
312
|
+
destination: BunFile | Bun.PathLike,
|
|
313
|
+
input: Blob | NodeJS.TypedArray | ArrayBufferLike | string | Bun.BlobPart[],
|
|
314
|
+
options?: {
|
|
315
|
+
/** If writing to a PathLike, set the permissions of the file. */
|
|
316
|
+
mode?: number;
|
|
317
|
+
/**
|
|
318
|
+
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
|
|
319
|
+
*
|
|
320
|
+
* If `false`, this will throw an error if the directory doesn't exist.
|
|
321
|
+
*
|
|
322
|
+
* @default true
|
|
323
|
+
*/
|
|
324
|
+
createPath?: boolean;
|
|
325
|
+
},
|
|
326
|
+
): Promise<number>;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Persist a {@link Response} body to disk.
|
|
330
|
+
*
|
|
331
|
+
* @param destination The file to write to. If the file doesn't exist,
|
|
332
|
+
* it will be created and if the file does exist, it will be
|
|
333
|
+
* overwritten. If `input`'s size is less than `destination`'s size,
|
|
334
|
+
* `destination` will be truncated.
|
|
335
|
+
* @param input - `Response` object
|
|
336
|
+
* @returns A promise that resolves with the number of bytes written.
|
|
337
|
+
*/
|
|
338
|
+
function write(
|
|
339
|
+
destination: BunFile,
|
|
340
|
+
input: Response,
|
|
341
|
+
options?: {
|
|
342
|
+
/**
|
|
343
|
+
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
|
|
344
|
+
*
|
|
345
|
+
* If `false`, this will throw an error if the directory doesn't exist.
|
|
346
|
+
*
|
|
347
|
+
* @default true
|
|
348
|
+
*/
|
|
349
|
+
createPath?: boolean;
|
|
350
|
+
},
|
|
351
|
+
): Promise<number>;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Persist a {@link Response} body to disk.
|
|
355
|
+
*
|
|
356
|
+
* @param destinationPath The file path to write to. If the file doesn't
|
|
357
|
+
* exist, it will be created and if the file does exist, it will be
|
|
358
|
+
* overwritten. If `input`'s size is less than `destination`'s size,
|
|
359
|
+
* `destination` will be truncated.
|
|
360
|
+
* @param input - `Response` object
|
|
361
|
+
* @returns A promise that resolves with the number of bytes written.
|
|
362
|
+
*/
|
|
363
|
+
// tslint:disable-next-line:unified-signatures
|
|
364
|
+
function write(
|
|
365
|
+
destinationPath: Bun.PathLike,
|
|
366
|
+
input: Response,
|
|
367
|
+
options?: {
|
|
368
|
+
/**
|
|
369
|
+
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
|
|
370
|
+
*
|
|
371
|
+
* If `false`, this will throw an error if the directory doesn't exist.
|
|
372
|
+
*
|
|
373
|
+
* @default true
|
|
374
|
+
*/
|
|
375
|
+
createPath?: boolean;
|
|
376
|
+
},
|
|
377
|
+
): Promise<number>;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Use the fastest syscalls available to copy from `input` into `destination`.
|
|
381
|
+
*
|
|
382
|
+
* If `destination` exists, it must be a regular file or symlink to a file.
|
|
383
|
+
*
|
|
384
|
+
* On Linux, this uses `copy_file_range`.
|
|
385
|
+
*
|
|
386
|
+
* On macOS, when the destination doesn't already exist, this uses
|
|
387
|
+
* [`clonefile()`](https://www.manpagez.com/man/2/clonefile/) and falls
|
|
388
|
+
* back to [`fcopyfile()`](https://www.manpagez.com/man/2/fcopyfile/)
|
|
389
|
+
*
|
|
390
|
+
* @param destination The file to write to. If the file doesn't exist,
|
|
391
|
+
* it will be created and if the file does exist, it will be
|
|
392
|
+
* overwritten. If `input`'s size is less than `destination`'s size,
|
|
393
|
+
* `destination` will be truncated.
|
|
394
|
+
* @param input The file to copy from.
|
|
395
|
+
* @returns A promise that resolves with the number of bytes written.
|
|
396
|
+
*/
|
|
397
|
+
// tslint:disable-next-line:unified-signatures
|
|
398
|
+
function write(
|
|
399
|
+
destination: BunFile,
|
|
400
|
+
input: BunFile,
|
|
401
|
+
options?: {
|
|
402
|
+
/**
|
|
403
|
+
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
|
|
404
|
+
*
|
|
405
|
+
* If `false`, this will throw an error if the directory doesn't exist.
|
|
406
|
+
*
|
|
407
|
+
* @default true
|
|
408
|
+
*/
|
|
409
|
+
createPath?: boolean;
|
|
410
|
+
},
|
|
411
|
+
): Promise<number>;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Use the fastest syscalls available to copy from `input` into `destination`.
|
|
415
|
+
*
|
|
416
|
+
* If `destination` exists, it must be a regular file or symlink to a file.
|
|
417
|
+
*
|
|
418
|
+
* On Linux, this uses `copy_file_range`.
|
|
419
|
+
*
|
|
420
|
+
* On macOS, when the destination doesn't already exist, this uses
|
|
421
|
+
* [`clonefile()`](https://www.manpagez.com/man/2/clonefile/) and falls
|
|
422
|
+
* back to [`fcopyfile()`](https://www.manpagez.com/man/2/fcopyfile/)
|
|
423
|
+
*
|
|
424
|
+
* @param destinationPath The file path to write to. If the file doesn't
|
|
425
|
+
* exist, it will be created and if the file does exist, it will be
|
|
426
|
+
* overwritten. If `input`'s size is less than `destination`'s size,
|
|
427
|
+
* `destination` will be truncated.
|
|
428
|
+
* @param input The file to copy from.
|
|
429
|
+
* @returns A promise that resolves with the number of bytes written.
|
|
430
|
+
*/
|
|
431
|
+
// tslint:disable-next-line:unified-signatures
|
|
432
|
+
function write(
|
|
433
|
+
destinationPath: Bun.PathLike,
|
|
434
|
+
input: BunFile,
|
|
435
|
+
options?: {
|
|
436
|
+
/**
|
|
437
|
+
* If `true`, create the parent directory if it doesn't exist. By default, this is `true`.
|
|
438
|
+
*
|
|
439
|
+
* If `false`, this will throw an error if the directory doesn't exist.
|
|
440
|
+
*
|
|
441
|
+
* @default true
|
|
442
|
+
*/
|
|
443
|
+
createPath?: boolean;
|
|
444
|
+
},
|
|
445
|
+
): Promise<number>;
|
|
446
|
+
|
|
447
|
+
interface SystemError extends Error {
|
|
448
|
+
errno?: number | undefined;
|
|
449
|
+
code?: string | undefined;
|
|
450
|
+
path?: string | undefined;
|
|
451
|
+
syscall?: string | undefined;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Concatenate an array of typed arrays into a single `ArrayBuffer`. This is a fast path.
|
|
456
|
+
*
|
|
457
|
+
* You can do this manually if you'd like, but this function will generally
|
|
458
|
+
* be a little faster.
|
|
459
|
+
*
|
|
460
|
+
* If you want a `Uint8Array` instead, consider `Buffer.concat`.
|
|
461
|
+
*
|
|
462
|
+
* @param buffers An array of typed arrays to concatenate.
|
|
463
|
+
* @returns An `ArrayBuffer` with the data from all the buffers.
|
|
464
|
+
*
|
|
465
|
+
* Here is similar code to do it manually, except about 30% slower:
|
|
466
|
+
* ```js
|
|
467
|
+
* var chunks = [...];
|
|
468
|
+
* var size = 0;
|
|
469
|
+
* for (const chunk of chunks) {
|
|
470
|
+
* size += chunk.byteLength;
|
|
471
|
+
* }
|
|
472
|
+
* var buffer = new ArrayBuffer(size);
|
|
473
|
+
* var view = new Uint8Array(buffer);
|
|
474
|
+
* var offset = 0;
|
|
475
|
+
* for (const chunk of chunks) {
|
|
476
|
+
* view.set(chunk, offset);
|
|
477
|
+
* offset += chunk.byteLength;
|
|
478
|
+
* }
|
|
479
|
+
* return buffer;
|
|
480
|
+
* ```
|
|
481
|
+
*
|
|
482
|
+
* This function is faster because it uses uninitialized memory when copying. Since the entire
|
|
483
|
+
* length of the buffer is known, it is safe to use uninitialized memory.
|
|
484
|
+
*/
|
|
485
|
+
function concatArrayBuffers(buffers: Array<ArrayBufferView | ArrayBufferLike>): ArrayBuffer;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Consume all data from a {@link ReadableStream} until it closes or errors.
|
|
489
|
+
*
|
|
490
|
+
* Concatenate the chunks into a single {@link ArrayBuffer}.
|
|
491
|
+
*
|
|
492
|
+
* Each chunk must be a TypedArray or an ArrayBuffer. If you need to support
|
|
493
|
+
* chunks of different types, consider {@link readableStreamToBlob}
|
|
494
|
+
*
|
|
495
|
+
* @param stream The stream to consume.
|
|
496
|
+
* @returns A promise that resolves with the concatenated chunks or the concatenated chunks as an `ArrayBuffer`.
|
|
497
|
+
*/
|
|
498
|
+
function readableStreamToArrayBuffer(
|
|
499
|
+
stream: ReadableStream<ArrayBufferView | ArrayBufferLike>,
|
|
500
|
+
): Promise<ArrayBuffer> | ArrayBuffer;
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Consume all data from a {@link ReadableStream} until it closes or errors.
|
|
504
|
+
*
|
|
505
|
+
* Concatenate the chunks into a single {@link Blob}.
|
|
506
|
+
*
|
|
507
|
+
* @param stream The stream to consume.
|
|
508
|
+
* @returns A promise that resolves with the concatenated chunks as a {@link Blob}.
|
|
509
|
+
*/
|
|
510
|
+
function readableStreamToBlob(stream: ReadableStream): Promise<Blob>;
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Consume all data from a {@link ReadableStream} until it closes or errors.
|
|
514
|
+
*
|
|
515
|
+
* Reads the multi-part or URL-encoded form data into a {@link FormData} object
|
|
516
|
+
*
|
|
517
|
+
* @param stream The stream to consume.
|
|
518
|
+
* @param multipartBoundaryExcludingDashes Optional boundary to use for multipart form data. If none is provided, assumes it is a URLEncoded form.
|
|
519
|
+
* @returns A promise that resolves with the data encoded into a {@link FormData} object.
|
|
520
|
+
*
|
|
521
|
+
* ## Multipart form data example
|
|
522
|
+
*
|
|
523
|
+
* ```ts
|
|
524
|
+
* // without dashes
|
|
525
|
+
* const boundary = "WebKitFormBoundary" + Math.random().toString(16).slice(2);
|
|
526
|
+
*
|
|
527
|
+
* const myStream = getStreamFromSomewhere() // ...
|
|
528
|
+
* const formData = await Bun.readableStreamToFormData(stream, boundary);
|
|
529
|
+
* formData.get("foo"); // "bar"
|
|
530
|
+
* ```
|
|
531
|
+
* ## URL-encoded form data example
|
|
532
|
+
*
|
|
533
|
+
* ```ts
|
|
534
|
+
* const stream = new Response("hello=123").body;
|
|
535
|
+
* const formData = await Bun.readableStreamToFormData(stream);
|
|
536
|
+
* formData.get("hello"); // "123"
|
|
537
|
+
* ```
|
|
538
|
+
*/
|
|
539
|
+
function readableStreamToFormData(
|
|
540
|
+
stream: ReadableStream<string | NodeJS.TypedArray | ArrayBufferView>,
|
|
541
|
+
multipartBoundaryExcludingDashes?: string | NodeJS.TypedArray | ArrayBufferView,
|
|
542
|
+
): Promise<FormData>;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Consume all data from a {@link ReadableStream} until it closes or errors.
|
|
546
|
+
*
|
|
547
|
+
* Concatenate the chunks into a single string. Chunks must be a TypedArray or an ArrayBuffer. If you need to support chunks of different types, consider {@link readableStreamToBlob}.
|
|
548
|
+
*
|
|
549
|
+
* @param stream The stream to consume.
|
|
550
|
+
* @returns A promise that resolves with the concatenated chunks as a {@link String}.
|
|
551
|
+
*/
|
|
552
|
+
function readableStreamToText(stream: ReadableStream): Promise<string>;
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Consume all data from a {@link ReadableStream} until it closes or errors.
|
|
556
|
+
*
|
|
557
|
+
* Concatenate the chunks into a single string and parse as JSON. Chunks must be a TypedArray or an ArrayBuffer. If you need to support chunks of different types, consider {@link readableStreamToBlob}.
|
|
558
|
+
*
|
|
559
|
+
* @param stream The stream to consume.
|
|
560
|
+
* @returns A promise that resolves with the concatenated chunks as a {@link String}.
|
|
561
|
+
*/
|
|
562
|
+
function readableStreamToJSON(stream: ReadableStream): Promise<any>;
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Consume all data from a {@link ReadableStream} until it closes or errors.
|
|
566
|
+
*
|
|
567
|
+
* @param stream The stream to consume
|
|
568
|
+
* @returns A promise that resolves with the chunks as an array
|
|
569
|
+
*/
|
|
570
|
+
function readableStreamToArray<T>(stream: ReadableStream<T>): Promise<T[]> | T[];
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Escape the following characters in a string:
|
|
574
|
+
*
|
|
575
|
+
* - `"` becomes `"""`
|
|
576
|
+
* - `&` becomes `"&"`
|
|
577
|
+
* - `'` becomes `"'"`
|
|
578
|
+
* - `<` becomes `"<"`
|
|
579
|
+
* - `>` becomes `">"`
|
|
580
|
+
*
|
|
581
|
+
* This function is optimized for large input. On an M1X, it processes 480 MB/s -
|
|
582
|
+
* 20 GB/s, depending on how much data is being escaped and whether there is non-ascii
|
|
583
|
+
* text.
|
|
584
|
+
*
|
|
585
|
+
* Non-string types will be converted to a string before escaping.
|
|
586
|
+
*/
|
|
587
|
+
function escapeHTML(input: string | object | number | boolean): string;
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Convert a filesystem path to a file:// URL.
|
|
591
|
+
*
|
|
592
|
+
* @param path The path to convert.
|
|
593
|
+
* @returns A {@link URL} with the file:// scheme.
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* ```js
|
|
597
|
+
* const url = Bun.pathToFileURL("/foo/bar.txt");
|
|
598
|
+
* console.log(url.href); // "file:///foo/bar.txt"
|
|
599
|
+
* ```
|
|
600
|
+
*
|
|
601
|
+
* Internally, this function uses WebKit's URL API to
|
|
602
|
+
* convert the path to a file:// URL.
|
|
603
|
+
*/
|
|
604
|
+
function pathToFileURL(path: string): URL;
|
|
605
|
+
|
|
606
|
+
interface Peek {
|
|
607
|
+
<T = undefined>(promise: T | Promise<T>): Promise<T> | T;
|
|
608
|
+
status<T = undefined>(promise: T | Promise<T>): "pending" | "fulfilled" | "rejected";
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Extract the value from the Promise in the same tick of the event loop
|
|
612
|
+
*/
|
|
613
|
+
const peek: Peek;
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Convert a {@link URL} to a filesystem path.
|
|
617
|
+
* @param url The URL to convert.
|
|
618
|
+
* @returns A filesystem path.
|
|
619
|
+
* @throws If the URL is not a URL.
|
|
620
|
+
* @example
|
|
621
|
+
* ```js
|
|
622
|
+
* const path = Bun.fileURLToPath(new URL("file:///foo/bar.txt"));
|
|
623
|
+
* console.log(path); // "/foo/bar.txt"
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
function fileURLToPath(url: URL): string;
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Fast incremental writer that becomes an `ArrayBuffer` on end().
|
|
630
|
+
*/
|
|
631
|
+
class ArrayBufferSink {
|
|
632
|
+
constructor();
|
|
633
|
+
|
|
634
|
+
start(options?: {
|
|
635
|
+
asUint8Array?: boolean;
|
|
636
|
+
/**
|
|
637
|
+
* Preallocate an internal buffer of this size
|
|
638
|
+
* This can significantly improve performance when the chunk size is small
|
|
639
|
+
*/
|
|
640
|
+
highWaterMark?: number;
|
|
641
|
+
/**
|
|
642
|
+
* On {@link ArrayBufferSink.flush}, return the written data as a `Uint8Array`.
|
|
643
|
+
* Writes will restart from the beginning of the buffer.
|
|
644
|
+
*/
|
|
645
|
+
stream?: boolean;
|
|
646
|
+
}): void;
|
|
647
|
+
|
|
648
|
+
write(chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number;
|
|
649
|
+
/**
|
|
650
|
+
* Flush the internal buffer
|
|
651
|
+
*
|
|
652
|
+
* If {@link ArrayBufferSink.start} was passed a `stream` option, this will return a `ArrayBuffer`
|
|
653
|
+
* If {@link ArrayBufferSink.start} was passed a `stream` option and `asUint8Array`, this will return a `Uint8Array`
|
|
654
|
+
* Otherwise, this will return the number of bytes written since the last flush
|
|
655
|
+
*
|
|
656
|
+
* This API might change later to separate Uint8ArraySink and ArrayBufferSink
|
|
657
|
+
*/
|
|
658
|
+
flush(): number | Uint8Array | ArrayBuffer;
|
|
659
|
+
end(): ArrayBuffer | Uint8Array;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const dns: {
|
|
663
|
+
/**
|
|
664
|
+
* Lookup the IP address for a hostname
|
|
665
|
+
*
|
|
666
|
+
* Uses non-blocking APIs by default
|
|
667
|
+
*
|
|
668
|
+
* @param hostname The hostname to lookup
|
|
669
|
+
* @param options Options for the lookup
|
|
670
|
+
*
|
|
671
|
+
* ## Example
|
|
672
|
+
*
|
|
673
|
+
* ```js
|
|
674
|
+
* const [{ address }] = await Bun.dns.lookup('example.com');
|
|
675
|
+
* ```
|
|
676
|
+
*
|
|
677
|
+
* ### Filter results to IPv4:
|
|
678
|
+
*
|
|
679
|
+
* ```js
|
|
680
|
+
* import { dns } from 'bun';
|
|
681
|
+
* const [{ address }] = await dns.lookup('example.com', {family: 4});
|
|
682
|
+
* console.log(address); // "123.122.22.126"
|
|
683
|
+
* ```
|
|
684
|
+
*
|
|
685
|
+
* ### Filter results to IPv6:
|
|
686
|
+
*
|
|
687
|
+
* ```js
|
|
688
|
+
* import { dns } from 'bun';
|
|
689
|
+
* const [{ address }] = await dns.lookup('example.com', {family: 6});
|
|
690
|
+
* console.log(address); // "2001:db8::1"
|
|
691
|
+
* ```
|
|
692
|
+
*
|
|
693
|
+
* #### DNS resolver client
|
|
694
|
+
*
|
|
695
|
+
* Bun supports three DNS resolvers:
|
|
696
|
+
* - `c-ares` - Uses the c-ares library to perform DNS resolution. This is the default on Linux.
|
|
697
|
+
* - `system` - Uses the system's non-blocking DNS resolver API if available, falls back to `getaddrinfo`. This is the default on macOS and the same as `getaddrinfo` on Linux.
|
|
698
|
+
* - `getaddrinfo` - Uses the posix standard `getaddrinfo` function. Will cause performance issues under concurrent loads.
|
|
699
|
+
*
|
|
700
|
+
* To customize the DNS resolver, pass a `backend` option to `dns.lookup`:
|
|
701
|
+
* ```js
|
|
702
|
+
* import { dns } from 'bun';
|
|
703
|
+
* const [{ address }] = await dns.lookup('example.com', {backend: 'getaddrinfo'});
|
|
704
|
+
* console.log(address); // "19.42.52.62"
|
|
705
|
+
* ```
|
|
706
|
+
*/
|
|
707
|
+
lookup(
|
|
708
|
+
hostname: string,
|
|
709
|
+
options?: {
|
|
710
|
+
/**
|
|
711
|
+
* Limit results to either IPv4, IPv6, or both
|
|
712
|
+
*/
|
|
713
|
+
family?: 4 | 6 | 0 | "IPv4" | "IPv6" | "any";
|
|
714
|
+
/**
|
|
715
|
+
* Limit results to either UDP or TCP
|
|
716
|
+
*/
|
|
717
|
+
socketType?: "udp" | "tcp";
|
|
718
|
+
flags?: number;
|
|
719
|
+
port?: number;
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* The DNS resolver implementation to use
|
|
723
|
+
*
|
|
724
|
+
* Defaults to `"c-ares"` on Linux and `"system"` on macOS. This default
|
|
725
|
+
* may change in a future version of Bun if c-ares is not reliable
|
|
726
|
+
* enough.
|
|
727
|
+
*
|
|
728
|
+
* On macOS, `system` uses the builtin macOS [non-blocking DNS
|
|
729
|
+
* resolution
|
|
730
|
+
* API](https://opensource.apple.com/source/Libinfo/Libinfo-222.1/lookup.subproj/netdb_async.h.auto.html).
|
|
731
|
+
*
|
|
732
|
+
* On Linux, `system` is the same as `getaddrinfo`.
|
|
733
|
+
*
|
|
734
|
+
* `c-ares` is more performant on Linux in some high concurrency
|
|
735
|
+
* situations, but it lacks support support for mDNS (`*.local`,
|
|
736
|
+
* `*.localhost` domains) along with some other advanced features. If
|
|
737
|
+
* you run into issues using `c-ares`, you should try `system`. If the
|
|
738
|
+
* hostname ends with `.local` or `.localhost`, Bun will automatically
|
|
739
|
+
* use `system` instead of `c-ares`.
|
|
740
|
+
*
|
|
741
|
+
* [`getaddrinfo`](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html)
|
|
742
|
+
* is the POSIX standard function for blocking DNS resolution. Bun runs
|
|
743
|
+
* it in Bun's thread pool, which is limited to `cpus / 2`. That means
|
|
744
|
+
* if you run a lot of concurrent DNS lookups, concurrent IO will
|
|
745
|
+
* potentially pause until the DNS lookups are done.
|
|
746
|
+
*
|
|
747
|
+
* On macOS, it shouldn't be necessary to use "`getaddrinfo`" because
|
|
748
|
+
* `"system"` uses the same API underneath (except non-blocking).
|
|
749
|
+
*
|
|
750
|
+
* On Windows, libuv's non-blocking DNS resolver is used by default, and
|
|
751
|
+
* when specifying backends "system", "libc", or "getaddrinfo". The c-ares
|
|
752
|
+
* backend isn't currently supported on Windows.
|
|
753
|
+
*/
|
|
754
|
+
backend?: "libc" | "c-ares" | "system" | "getaddrinfo";
|
|
755
|
+
},
|
|
756
|
+
): Promise<DNSLookup[]>;
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
interface DNSLookup {
|
|
760
|
+
/**
|
|
761
|
+
* The IP address of the host as a string in IPv4 or IPv6 format.
|
|
762
|
+
*
|
|
763
|
+
* @example "127.0.0.1"
|
|
764
|
+
* @example "192.168.0.1"
|
|
765
|
+
* @example "2001:4860:4860::8888"
|
|
766
|
+
*/
|
|
767
|
+
address: string;
|
|
768
|
+
family: 4 | 6;
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* Time to live in seconds
|
|
772
|
+
*
|
|
773
|
+
* Only supported when using the `c-ares` DNS resolver via "backend" option
|
|
774
|
+
* to {@link dns.lookup}. Otherwise, it's 0.
|
|
775
|
+
*/
|
|
776
|
+
ttl: number;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Fast incremental writer for files and pipes.
|
|
781
|
+
*
|
|
782
|
+
* This uses the same interface as {@link ArrayBufferSink}, but writes to a file or pipe.
|
|
783
|
+
*/
|
|
784
|
+
interface FileSink {
|
|
785
|
+
/**
|
|
786
|
+
* Write a chunk of data to the file.
|
|
787
|
+
*
|
|
788
|
+
* If the file descriptor is not writable yet, the data is buffered.
|
|
789
|
+
*/
|
|
790
|
+
write(chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number;
|
|
791
|
+
/**
|
|
792
|
+
* Flush the internal buffer, committing the data to disk or the pipe.
|
|
793
|
+
*/
|
|
794
|
+
flush(): number | Promise<number>;
|
|
795
|
+
/**
|
|
796
|
+
* Close the file descriptor. This also flushes the internal buffer.
|
|
797
|
+
*/
|
|
798
|
+
end(error?: Error): number | Promise<number>;
|
|
799
|
+
|
|
800
|
+
start(options?: {
|
|
801
|
+
/**
|
|
802
|
+
* Preallocate an internal buffer of this size
|
|
803
|
+
* This can significantly improve performance when the chunk size is small
|
|
804
|
+
*/
|
|
805
|
+
highWaterMark?: number;
|
|
806
|
+
}): void;
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* For FIFOs & pipes, this lets you decide whether Bun's process should
|
|
810
|
+
* remain alive until the pipe is closed.
|
|
811
|
+
*
|
|
812
|
+
* By default, it is automatically managed. While the stream is open, the
|
|
813
|
+
* process remains alive and once the other end hangs up or the stream
|
|
814
|
+
* closes, the process exits.
|
|
815
|
+
*
|
|
816
|
+
* If you previously called {@link unref}, you can call this again to re-enable automatic management.
|
|
817
|
+
*
|
|
818
|
+
* Internally, it will reference count the number of times this is called. By default, that number is 1
|
|
819
|
+
*
|
|
820
|
+
* If the file is not a FIFO or pipe, {@link ref} and {@link unref} do
|
|
821
|
+
* nothing. If the pipe is already closed, this does nothing.
|
|
822
|
+
*/
|
|
823
|
+
ref(): void;
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* For FIFOs & pipes, this lets you decide whether Bun's process should
|
|
827
|
+
* remain alive until the pipe is closed.
|
|
828
|
+
*
|
|
829
|
+
* If you want to allow Bun's process to terminate while the stream is open,
|
|
830
|
+
* call this.
|
|
831
|
+
*
|
|
832
|
+
* If the file is not a FIFO or pipe, {@link ref} and {@link unref} do
|
|
833
|
+
* nothing. If the pipe is already closed, this does nothing.
|
|
834
|
+
*/
|
|
835
|
+
unref(): void;
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
interface FileBlob extends BunFile {}
|
|
839
|
+
/**
|
|
840
|
+
* [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
|
|
841
|
+
*
|
|
842
|
+
* This Blob is lazy. That means it won't do any work until you read from it.
|
|
843
|
+
*
|
|
844
|
+
* - `size` will not be valid until the contents of the file are read at least once.
|
|
845
|
+
* - `type` is auto-set based on the file extension when possible
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
* ```js
|
|
849
|
+
* const file = Bun.file("./hello.json");
|
|
850
|
+
* console.log(file.type); // "application/json"
|
|
851
|
+
* console.log(await file.text()); // '{"hello":"world"}'
|
|
852
|
+
* ```
|
|
853
|
+
*
|
|
854
|
+
* @example
|
|
855
|
+
* ```js
|
|
856
|
+
* await Bun.write(
|
|
857
|
+
* Bun.file("./hello.txt"),
|
|
858
|
+
* "Hello, world!"
|
|
859
|
+
* );
|
|
860
|
+
* ```
|
|
861
|
+
*/
|
|
862
|
+
interface BunFile extends Blob {
|
|
863
|
+
/**
|
|
864
|
+
* Offset any operation on the file starting at `begin` and ending at `end`. `end` is relative to 0
|
|
865
|
+
*
|
|
866
|
+
* 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.
|
|
867
|
+
*
|
|
868
|
+
* If `begin` > 0, {@link Bun.write()} will be slower on macOS
|
|
869
|
+
*
|
|
870
|
+
* @param begin - start offset in bytes
|
|
871
|
+
* @param end - absolute offset in bytes (relative to 0)
|
|
872
|
+
* @param contentType - MIME type for the new BunFile
|
|
873
|
+
*/
|
|
874
|
+
slice(begin?: number, end?: number, contentType?: string): BunFile;
|
|
875
|
+
|
|
876
|
+
/** */
|
|
877
|
+
/**
|
|
878
|
+
* Offset any operation on the file starting at `begin`
|
|
879
|
+
*
|
|
880
|
+
* 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.
|
|
881
|
+
*
|
|
882
|
+
* If `begin` > 0, {@link Bun.write()} will be slower on macOS
|
|
883
|
+
*
|
|
884
|
+
* @param begin - start offset in bytes
|
|
885
|
+
* @param contentType - MIME type for the new BunFile
|
|
886
|
+
*/
|
|
887
|
+
slice(begin?: number, contentType?: string): BunFile;
|
|
888
|
+
|
|
889
|
+
/**
|
|
890
|
+
* @param contentType - MIME type for the new BunFile
|
|
891
|
+
*/
|
|
892
|
+
slice(contentType?: string): BunFile;
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Incremental writer for files and pipes.
|
|
896
|
+
*/
|
|
897
|
+
writer(options?: { highWaterMark?: number }): FileSink;
|
|
898
|
+
|
|
899
|
+
readonly readable: ReadableStream;
|
|
900
|
+
|
|
901
|
+
// TODO: writable: WritableStream;
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* A UNIX timestamp indicating when the file was last modified.
|
|
905
|
+
*/
|
|
906
|
+
lastModified: number;
|
|
907
|
+
/**
|
|
908
|
+
* The name or path of the file, as specified in the constructor.
|
|
909
|
+
*/
|
|
910
|
+
readonly name?: string;
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Does the file exist?
|
|
914
|
+
*
|
|
915
|
+
* This returns true for regular files and FIFOs. It returns false for
|
|
916
|
+
* directories. Note that a race condition can occur where the file is
|
|
917
|
+
* deleted or renamed after this is called but before you open it.
|
|
918
|
+
*
|
|
919
|
+
* This does a system call to check if the file exists, which can be
|
|
920
|
+
* slow.
|
|
921
|
+
*
|
|
922
|
+
* If using this in an HTTP server, it's faster to instead use `return new
|
|
923
|
+
* Response(Bun.file(path))` and then an `error` handler to handle
|
|
924
|
+
* exceptions.
|
|
925
|
+
*
|
|
926
|
+
* Instead of checking for a file's existence and then performing the
|
|
927
|
+
* operation, it is faster to just perform the operation and handle the
|
|
928
|
+
* error.
|
|
929
|
+
*
|
|
930
|
+
* For empty Blob, this always returns true.
|
|
931
|
+
*/
|
|
932
|
+
exists(): Promise<boolean>;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* This lets you use macros as regular imports
|
|
937
|
+
* @example
|
|
938
|
+
* ```
|
|
939
|
+
* {
|
|
940
|
+
* "react-relay": {
|
|
941
|
+
* "graphql": "bun-macro-relay/bun-macro-relay.tsx"
|
|
942
|
+
* }
|
|
943
|
+
* }
|
|
944
|
+
* ```
|
|
945
|
+
*/
|
|
946
|
+
type MacroMap = Record<string, Record<string, string>>;
|
|
947
|
+
|
|
948
|
+
/**
|
|
949
|
+
* Hash a string or array buffer using Wyhash
|
|
950
|
+
*
|
|
951
|
+
* This is not a cryptographic hash function.
|
|
952
|
+
* @param data The data to hash.
|
|
953
|
+
* @param seed The seed to use.
|
|
954
|
+
*/
|
|
955
|
+
const hash: ((
|
|
956
|
+
data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
|
|
957
|
+
seed?: number | bigint,
|
|
958
|
+
) => number | bigint) &
|
|
959
|
+
Hash;
|
|
960
|
+
|
|
961
|
+
interface Hash {
|
|
962
|
+
wyhash: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, seed?: bigint) => bigint;
|
|
963
|
+
adler32: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer) => number;
|
|
964
|
+
crc32: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer) => number;
|
|
965
|
+
cityHash32: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer) => number;
|
|
966
|
+
cityHash64: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, seed?: bigint) => bigint;
|
|
967
|
+
murmur32v3: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, seed?: number) => number;
|
|
968
|
+
murmur32v2: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, seed?: number) => number;
|
|
969
|
+
murmur64v2: (data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, seed?: bigint) => bigint;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
type JavaScriptLoader = "jsx" | "js" | "ts" | "tsx";
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* Fast deep-equality check two objects.
|
|
976
|
+
*
|
|
977
|
+
* This also powers expect().toEqual in `bun:test`
|
|
978
|
+
*/
|
|
979
|
+
function deepEquals(
|
|
980
|
+
a: any,
|
|
981
|
+
b: any,
|
|
982
|
+
/** @default false */
|
|
983
|
+
strict?: boolean,
|
|
984
|
+
): boolean;
|
|
985
|
+
|
|
986
|
+
/**
|
|
987
|
+
* Returns true if all properties in the subset exist in the
|
|
988
|
+
* other and have equal values.
|
|
989
|
+
*
|
|
990
|
+
* This also powers expect().toMatchObject in `bun:test`
|
|
991
|
+
*/
|
|
992
|
+
function deepMatch(subset: unknown, a: unknown): boolean;
|
|
993
|
+
|
|
994
|
+
/**
|
|
995
|
+
* tsconfig.json options supported by Bun
|
|
996
|
+
*/
|
|
997
|
+
interface TSConfig {
|
|
998
|
+
extends?: string;
|
|
999
|
+
compilerOptions?: {
|
|
1000
|
+
paths?: Record<string, string[]>;
|
|
1001
|
+
baseUrl?: string;
|
|
1002
|
+
/** "preserve" is not supported yet */
|
|
1003
|
+
jsx?: "preserve" | "react" | "react-jsx" | "react-jsxdev";
|
|
1004
|
+
jsxFactory?: string;
|
|
1005
|
+
jsxFragmentFactory?: string;
|
|
1006
|
+
jsxImportSource?: string;
|
|
1007
|
+
useDefineForClassFields?: boolean;
|
|
1008
|
+
importsNotUsedAsValues?: "remove" | "preserve" | "error";
|
|
1009
|
+
/** moduleSuffixes is not supported yet */
|
|
1010
|
+
moduleSuffixes?: any;
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
interface TranspilerOptions {
|
|
1015
|
+
/**
|
|
1016
|
+
* Replace key with value. Value must be a JSON string.
|
|
1017
|
+
* @example
|
|
1018
|
+
* ```
|
|
1019
|
+
* { "process.env.NODE_ENV": "\"production\"" }
|
|
1020
|
+
* ```
|
|
1021
|
+
*/
|
|
1022
|
+
define?: Record<string, string>;
|
|
1023
|
+
|
|
1024
|
+
/** What is the default loader used for this transpiler? */
|
|
1025
|
+
loader?: JavaScriptLoader;
|
|
1026
|
+
|
|
1027
|
+
/** What platform are we targeting? This may affect how import and/or require is used */
|
|
1028
|
+
/** @example "browser" */
|
|
1029
|
+
target?: Target;
|
|
1030
|
+
|
|
1031
|
+
/**
|
|
1032
|
+
* TSConfig.json file as stringified JSON or an object
|
|
1033
|
+
* Use this to set a custom JSX factory, fragment, or import source
|
|
1034
|
+
* For example, if you want to use Preact instead of React. Or if you want to use Emotion.
|
|
1035
|
+
*/
|
|
1036
|
+
tsconfig?: string | TSConfig;
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* Replace an import statement with a macro.
|
|
1040
|
+
*
|
|
1041
|
+
* This will remove the import statement from the final output
|
|
1042
|
+
* and replace any function calls or template strings with the result returned by the macro
|
|
1043
|
+
*
|
|
1044
|
+
* @example
|
|
1045
|
+
* ```json
|
|
1046
|
+
* {
|
|
1047
|
+
* "react-relay": {
|
|
1048
|
+
* "graphql": "bun-macro-relay"
|
|
1049
|
+
* }
|
|
1050
|
+
* }
|
|
1051
|
+
* ```
|
|
1052
|
+
*
|
|
1053
|
+
* Code that calls `graphql` will be replaced with the result of the macro.
|
|
1054
|
+
*
|
|
1055
|
+
* ```js
|
|
1056
|
+
* import {graphql} from "react-relay";
|
|
1057
|
+
*
|
|
1058
|
+
* // Input:
|
|
1059
|
+
* const query = graphql`
|
|
1060
|
+
* query {
|
|
1061
|
+
* ... on User {
|
|
1062
|
+
* id
|
|
1063
|
+
* }
|
|
1064
|
+
* }
|
|
1065
|
+
* }`;
|
|
1066
|
+
* ```
|
|
1067
|
+
*
|
|
1068
|
+
* Will be replaced with:
|
|
1069
|
+
*
|
|
1070
|
+
* ```js
|
|
1071
|
+
* import UserQuery from "./UserQuery.graphql";
|
|
1072
|
+
* const query = UserQuery;
|
|
1073
|
+
* ```
|
|
1074
|
+
*/
|
|
1075
|
+
macro?: MacroMap;
|
|
1076
|
+
|
|
1077
|
+
autoImportJSX?: boolean;
|
|
1078
|
+
allowBunRuntime?: boolean;
|
|
1079
|
+
exports?: {
|
|
1080
|
+
eliminate?: string[];
|
|
1081
|
+
replace?: Record<string, string>;
|
|
1082
|
+
};
|
|
1083
|
+
treeShaking?: boolean;
|
|
1084
|
+
trimUnusedImports?: boolean;
|
|
1085
|
+
jsxOptimizationInline?: boolean;
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* **Experimental**
|
|
1089
|
+
*
|
|
1090
|
+
* Minify whitespace and comments from the output.
|
|
1091
|
+
*/
|
|
1092
|
+
minifyWhitespace?: boolean;
|
|
1093
|
+
/**
|
|
1094
|
+
* **Experimental**
|
|
1095
|
+
*
|
|
1096
|
+
* Enabled by default, use this to disable dead code elimination.
|
|
1097
|
+
*
|
|
1098
|
+
* Some other transpiler options may still do some specific dead code elimination.
|
|
1099
|
+
*/
|
|
1100
|
+
deadCodeElimination?: boolean;
|
|
1101
|
+
|
|
1102
|
+
/**
|
|
1103
|
+
* This does two things (and possibly more in the future):
|
|
1104
|
+
* 1. `const` declarations to primitive types (excluding Object/Array) at the top of a scope before any `let` or `var` declarations will be inlined into their usages.
|
|
1105
|
+
* 2. `let` and `const` declarations only used once are inlined into their usages.
|
|
1106
|
+
*
|
|
1107
|
+
* JavaScript engines typically do these optimizations internally, however
|
|
1108
|
+
* it might only happen much later in the compilation pipeline, after code
|
|
1109
|
+
* has been executed many many times.
|
|
1110
|
+
*
|
|
1111
|
+
* This will typically shrink the output size of code, but it might increase
|
|
1112
|
+
* it in some cases. Do your own benchmarks!
|
|
1113
|
+
*/
|
|
1114
|
+
inline?: boolean;
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* @default "warn"
|
|
1118
|
+
*/
|
|
1119
|
+
logLevel?: "verbose" | "debug" | "info" | "warn" | "error";
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* Quickly transpile TypeScript, JSX, or JS to modern JavaScript.
|
|
1124
|
+
*
|
|
1125
|
+
* @example
|
|
1126
|
+
* ```js
|
|
1127
|
+
* const transpiler = new Bun.Transpiler();
|
|
1128
|
+
* transpiler.transformSync(`
|
|
1129
|
+
* const App = () => <div>Hello World</div>;
|
|
1130
|
+
* export default App;
|
|
1131
|
+
* `);
|
|
1132
|
+
* // This outputs:
|
|
1133
|
+
* const output = `
|
|
1134
|
+
* const App = () => jsx("div", {
|
|
1135
|
+
* children: "Hello World"
|
|
1136
|
+
* }, undefined, false, undefined, this);
|
|
1137
|
+
* export default App;
|
|
1138
|
+
* `
|
|
1139
|
+
* ```
|
|
1140
|
+
*/
|
|
1141
|
+
|
|
1142
|
+
class Transpiler {
|
|
1143
|
+
constructor(options?: TranspilerOptions);
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Transpile code from TypeScript or JSX into valid JavaScript.
|
|
1147
|
+
* This function does not resolve imports.
|
|
1148
|
+
* @param code The code to transpile
|
|
1149
|
+
*/
|
|
1150
|
+
transform(code: Bun.StringOrBuffer, loader?: JavaScriptLoader): Promise<string>;
|
|
1151
|
+
/**
|
|
1152
|
+
* Transpile code from TypeScript or JSX into valid JavaScript.
|
|
1153
|
+
* This function does not resolve imports.
|
|
1154
|
+
* @param code The code to transpile
|
|
1155
|
+
*/
|
|
1156
|
+
transformSync(code: Bun.StringOrBuffer, loader: JavaScriptLoader, ctx: object): string;
|
|
1157
|
+
/**
|
|
1158
|
+
* Transpile code from TypeScript or JSX into valid JavaScript.
|
|
1159
|
+
* This function does not resolve imports.
|
|
1160
|
+
* @param code The code to transpile
|
|
1161
|
+
* @param ctx An object to pass to macros
|
|
1162
|
+
*/
|
|
1163
|
+
transformSync(code: Bun.StringOrBuffer, ctx: object): string;
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* Transpile code from TypeScript or JSX into valid JavaScript.
|
|
1167
|
+
* This function does not resolve imports.
|
|
1168
|
+
* @param code The code to transpile
|
|
1169
|
+
*/
|
|
1170
|
+
transformSync(code: Bun.StringOrBuffer, loader?: JavaScriptLoader): string;
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
1173
|
+
* Get a list of import paths and paths from a TypeScript, JSX, TSX, or JavaScript file.
|
|
1174
|
+
* @param code The code to scan
|
|
1175
|
+
* @example
|
|
1176
|
+
* ```js
|
|
1177
|
+
* const {imports, exports} = transpiler.scan(`
|
|
1178
|
+
* import {foo} from "baz";
|
|
1179
|
+
* const hello = "hi!";
|
|
1180
|
+
* `);
|
|
1181
|
+
*
|
|
1182
|
+
* console.log(imports); // ["baz"]
|
|
1183
|
+
* console.log(exports); // ["hello"]
|
|
1184
|
+
* ```
|
|
1185
|
+
*/
|
|
1186
|
+
scan(code: Bun.StringOrBuffer): { exports: string[]; imports: Import[] };
|
|
1187
|
+
|
|
1188
|
+
/**
|
|
1189
|
+
* Get a list of import paths from a TypeScript, JSX, TSX, or JavaScript file.
|
|
1190
|
+
* @param code The code to scan
|
|
1191
|
+
* @example
|
|
1192
|
+
* ```js
|
|
1193
|
+
* const imports = transpiler.scanImports(`
|
|
1194
|
+
* import {foo} from "baz";
|
|
1195
|
+
* import type {FooType} from "bar";
|
|
1196
|
+
* import type {DogeType} from "wolf";
|
|
1197
|
+
* `);
|
|
1198
|
+
*
|
|
1199
|
+
* console.log(imports); // ["baz"]
|
|
1200
|
+
* ```
|
|
1201
|
+
* This is a fast path which performs less work than `scan`.
|
|
1202
|
+
*/
|
|
1203
|
+
scanImports(code: Bun.StringOrBuffer): Import[];
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
type ImportKind =
|
|
1207
|
+
| "import-statement"
|
|
1208
|
+
| "require-call"
|
|
1209
|
+
| "require-resolve"
|
|
1210
|
+
| "dynamic-import"
|
|
1211
|
+
| "import-rule"
|
|
1212
|
+
| "url-token"
|
|
1213
|
+
| "internal"
|
|
1214
|
+
| "entry-point";
|
|
1215
|
+
|
|
1216
|
+
interface Import {
|
|
1217
|
+
path: string;
|
|
1218
|
+
kind: ImportKind;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
type ModuleFormat = "esm"; // later: "cjs", "iife"
|
|
1222
|
+
|
|
1223
|
+
interface BuildConfig {
|
|
1224
|
+
entrypoints: string[]; // list of file path
|
|
1225
|
+
outdir?: string; // output directory
|
|
1226
|
+
target?: Target; // default: "browser"
|
|
1227
|
+
format?: ModuleFormat; // later: "cjs", "iife"
|
|
1228
|
+
naming?:
|
|
1229
|
+
| string
|
|
1230
|
+
| {
|
|
1231
|
+
chunk?: string;
|
|
1232
|
+
entry?: string;
|
|
1233
|
+
asset?: string;
|
|
1234
|
+
}; // | string;
|
|
1235
|
+
root?: string; // project root
|
|
1236
|
+
splitting?: boolean; // default true, enable code splitting
|
|
1237
|
+
plugins?: BunPlugin[];
|
|
1238
|
+
// manifest?: boolean; // whether to return manifest
|
|
1239
|
+
external?: string[];
|
|
1240
|
+
publicPath?: string;
|
|
1241
|
+
define?: Record<string, string>;
|
|
1242
|
+
// origin?: string; // e.g. http://mydomain.com
|
|
1243
|
+
loader?: { [k in string]: Loader };
|
|
1244
|
+
sourcemap?: "none" | "inline" | "external"; // default: "none"
|
|
1245
|
+
minify?:
|
|
1246
|
+
| boolean
|
|
1247
|
+
| {
|
|
1248
|
+
whitespace?: boolean;
|
|
1249
|
+
syntax?: boolean;
|
|
1250
|
+
identifiers?: boolean;
|
|
1251
|
+
};
|
|
1252
|
+
// treeshaking?: boolean;
|
|
1253
|
+
|
|
1254
|
+
// jsx?:
|
|
1255
|
+
// | "automatic"
|
|
1256
|
+
// | "classic"
|
|
1257
|
+
// | /* later: "preserve" */ {
|
|
1258
|
+
// runtime?: "automatic" | "classic"; // later: "preserve"
|
|
1259
|
+
// /** Only works when runtime=classic */
|
|
1260
|
+
// factory?: string; // default: "React.createElement"
|
|
1261
|
+
// /** Only works when runtime=classic */
|
|
1262
|
+
// fragment?: string; // default: "React.Fragment"
|
|
1263
|
+
// /** Only works when runtime=automatic */
|
|
1264
|
+
// importSource?: string; // default: "react"
|
|
1265
|
+
// };
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
namespace Password {
|
|
1269
|
+
type AlgorithmLabel = "bcrypt" | "argon2id" | "argon2d" | "argon2i";
|
|
1270
|
+
|
|
1271
|
+
interface Argon2Algorithm {
|
|
1272
|
+
algorithm: "argon2id" | "argon2d" | "argon2i";
|
|
1273
|
+
/**
|
|
1274
|
+
* Memory cost, which defines the memory usage, given in kibibytes.
|
|
1275
|
+
*/
|
|
1276
|
+
memoryCost?: number;
|
|
1277
|
+
/**
|
|
1278
|
+
* Defines the amount of computation realized and therefore the execution
|
|
1279
|
+
* time, given in number of iterations.
|
|
1280
|
+
*/
|
|
1281
|
+
timeCost?: number;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
interface BCryptAlgorithm {
|
|
1285
|
+
algorithm: "bcrypt";
|
|
1286
|
+
/**
|
|
1287
|
+
* A number between 4 and 31. The default is 10.
|
|
1288
|
+
*/
|
|
1289
|
+
cost?: number;
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
/**
|
|
1294
|
+
* Hash and verify passwords using argon2 or bcrypt. The default is argon2.
|
|
1295
|
+
* Password hashing functions are necessarily slow, and this object will
|
|
1296
|
+
* automatically run in a worker thread.
|
|
1297
|
+
*
|
|
1298
|
+
* The underlying implementation of these functions are provided by the Zig
|
|
1299
|
+
* Standard Library. Thanks to @jedisct1 and other Zig constributors for their
|
|
1300
|
+
* work on this.
|
|
1301
|
+
*
|
|
1302
|
+
* ### Example with argon2
|
|
1303
|
+
*
|
|
1304
|
+
* ```ts
|
|
1305
|
+
* import {password} from "bun";
|
|
1306
|
+
*
|
|
1307
|
+
* const hash = await password.hash("hello world");
|
|
1308
|
+
* const verify = await password.verify("hello world", hash);
|
|
1309
|
+
* console.log(verify); // true
|
|
1310
|
+
* ```
|
|
1311
|
+
*
|
|
1312
|
+
* ### Example with bcrypt
|
|
1313
|
+
* ```ts
|
|
1314
|
+
* import {password} from "bun";
|
|
1315
|
+
*
|
|
1316
|
+
* const hash = await password.hash("hello world", "bcrypt");
|
|
1317
|
+
* // algorithm is optional, will be inferred from the hash if not specified
|
|
1318
|
+
* const verify = await password.verify("hello world", hash, "bcrypt");
|
|
1319
|
+
*
|
|
1320
|
+
* console.log(verify); // true
|
|
1321
|
+
* ```
|
|
1322
|
+
*/
|
|
1323
|
+
const password: {
|
|
1324
|
+
/**
|
|
1325
|
+
* Verify a password against a previously hashed password.
|
|
1326
|
+
*
|
|
1327
|
+
* @returns true if the password matches, false otherwise
|
|
1328
|
+
*
|
|
1329
|
+
* @example
|
|
1330
|
+
* ```ts
|
|
1331
|
+
* import {password} from "bun";
|
|
1332
|
+
* await password.verify("hey", "$argon2id$v=19$m=65536,t=2,p=1$ddbcyBcbAcagei7wSkZFiouX6TqnUQHmTyS5mxGCzeM$+3OIaFatZ3n6LtMhUlfWbgJyNp7h8/oIsLK+LzZO+WI");
|
|
1333
|
+
* // true
|
|
1334
|
+
* ```
|
|
1335
|
+
*
|
|
1336
|
+
* @throws If the algorithm is specified and does not match the hash
|
|
1337
|
+
* @throws If the algorithm is invalid
|
|
1338
|
+
* @throws if the hash is invalid
|
|
1339
|
+
*/
|
|
1340
|
+
verify(
|
|
1341
|
+
/**
|
|
1342
|
+
* The password to verify.
|
|
1343
|
+
*
|
|
1344
|
+
* If empty, always returns false
|
|
1345
|
+
*/
|
|
1346
|
+
password: Bun.StringOrBuffer,
|
|
1347
|
+
/**
|
|
1348
|
+
* Previously hashed password.
|
|
1349
|
+
* If empty, always returns false
|
|
1350
|
+
*/
|
|
1351
|
+
hash: Bun.StringOrBuffer,
|
|
1352
|
+
/**
|
|
1353
|
+
* If not specified, the algorithm will be inferred from the hash.
|
|
1354
|
+
*
|
|
1355
|
+
* If specified and the algorithm does not match the hash, this function
|
|
1356
|
+
* throws an error.
|
|
1357
|
+
*/
|
|
1358
|
+
algorithm?: Password.AlgorithmLabel,
|
|
1359
|
+
): Promise<boolean>;
|
|
1360
|
+
/**
|
|
1361
|
+
* Asynchronously hash a password using argon2 or bcrypt. The default is argon2.
|
|
1362
|
+
*
|
|
1363
|
+
* @returns A promise that resolves to the hashed password
|
|
1364
|
+
*
|
|
1365
|
+
* ## Example with argon2
|
|
1366
|
+
* ```ts
|
|
1367
|
+
* import {password} from "bun";
|
|
1368
|
+
* const hash = await password.hash("hello world");
|
|
1369
|
+
* console.log(hash); // $argon2id$v=1...
|
|
1370
|
+
* const verify = await password.verify("hello world", hash);
|
|
1371
|
+
* ```
|
|
1372
|
+
* ## Example with bcrypt
|
|
1373
|
+
* ```ts
|
|
1374
|
+
* import {password} from "bun";
|
|
1375
|
+
* const hash = await password.hash("hello world", "bcrypt");
|
|
1376
|
+
* console.log(hash); // $2b$10$...
|
|
1377
|
+
* const verify = await password.verify("hello world", hash);
|
|
1378
|
+
* ```
|
|
1379
|
+
*/
|
|
1380
|
+
hash(
|
|
1381
|
+
/**
|
|
1382
|
+
* The password to hash
|
|
1383
|
+
*
|
|
1384
|
+
* If empty, this function throws an error. It is usually a programming
|
|
1385
|
+
* mistake to hash an empty password.
|
|
1386
|
+
*/
|
|
1387
|
+
password: Bun.StringOrBuffer,
|
|
1388
|
+
/**
|
|
1389
|
+
* @default "argon2id"
|
|
1390
|
+
*
|
|
1391
|
+
* When using bcrypt, passwords exceeding 72 characters will be SHA512'd before
|
|
1392
|
+
*/
|
|
1393
|
+
algorithm?: Password.AlgorithmLabel | Password.Argon2Algorithm | Password.BCryptAlgorithm,
|
|
1394
|
+
): Promise<string>;
|
|
1395
|
+
|
|
1396
|
+
/**
|
|
1397
|
+
* Synchronously hash and verify passwords using argon2 or bcrypt. The default is argon2.
|
|
1398
|
+
* Warning: password hashing is slow, consider using {@link Bun.password.verify}
|
|
1399
|
+
* instead which runs in a worker thread.
|
|
1400
|
+
*
|
|
1401
|
+
* The underlying implementation of these functions are provided by the Zig
|
|
1402
|
+
* Standard Library. Thanks to @jedisct1 and other Zig constributors for their
|
|
1403
|
+
* work on this.
|
|
1404
|
+
*
|
|
1405
|
+
* ### Example with argon2
|
|
1406
|
+
*
|
|
1407
|
+
* ```ts
|
|
1408
|
+
* import {password} from "bun";
|
|
1409
|
+
*
|
|
1410
|
+
* const hash = await password.hashSync("hello world");
|
|
1411
|
+
* const verify = await password.verifySync("hello world", hash);
|
|
1412
|
+
* console.log(verify); // true
|
|
1413
|
+
* ```
|
|
1414
|
+
*
|
|
1415
|
+
* ### Example with bcrypt
|
|
1416
|
+
* ```ts
|
|
1417
|
+
* import {password} from "bun";
|
|
1418
|
+
*
|
|
1419
|
+
* const hash = await password.hashSync("hello world", "bcrypt");
|
|
1420
|
+
* // algorithm is optional, will be inferred from the hash if not specified
|
|
1421
|
+
* const verify = await password.verifySync("hello world", hash, "bcrypt");
|
|
1422
|
+
*
|
|
1423
|
+
* console.log(verify); // true
|
|
1424
|
+
* ```
|
|
1425
|
+
*/
|
|
1426
|
+
verifySync(
|
|
1427
|
+
password: Bun.StringOrBuffer,
|
|
1428
|
+
hash: Bun.StringOrBuffer,
|
|
1429
|
+
/**
|
|
1430
|
+
* If not specified, the algorithm will be inferred from the hash.
|
|
1431
|
+
*/
|
|
1432
|
+
algorithm?: Password.AlgorithmLabel,
|
|
1433
|
+
): boolean;
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* Synchronously hash and verify passwords using argon2 or bcrypt. The default is argon2.
|
|
1437
|
+
* Warning: password hashing is slow, consider using {@link Bun.password.hash}
|
|
1438
|
+
* instead which runs in a worker thread.
|
|
1439
|
+
*
|
|
1440
|
+
* The underlying implementation of these functions are provided by the Zig
|
|
1441
|
+
* Standard Library. Thanks to @jedisct1 and other Zig constributors for their
|
|
1442
|
+
* work on this.
|
|
1443
|
+
*
|
|
1444
|
+
* ### Example with argon2
|
|
1445
|
+
*
|
|
1446
|
+
* ```ts
|
|
1447
|
+
* import {password} from "bun";
|
|
1448
|
+
*
|
|
1449
|
+
* const hash = await password.hashSync("hello world");
|
|
1450
|
+
* const verify = await password.verifySync("hello world", hash);
|
|
1451
|
+
* console.log(verify); // true
|
|
1452
|
+
* ```
|
|
1453
|
+
*
|
|
1454
|
+
* ### Example with bcrypt
|
|
1455
|
+
* ```ts
|
|
1456
|
+
* import {password} from "bun";
|
|
1457
|
+
*
|
|
1458
|
+
* const hash = await password.hashSync("hello world", "bcrypt");
|
|
1459
|
+
* // algorithm is optional, will be inferred from the hash if not specified
|
|
1460
|
+
* const verify = await password.verifySync("hello world", hash, "bcrypt");
|
|
1461
|
+
*
|
|
1462
|
+
* console.log(verify); // true
|
|
1463
|
+
* ```
|
|
1464
|
+
*/
|
|
1465
|
+
hashSync(
|
|
1466
|
+
/**
|
|
1467
|
+
* The password to hash
|
|
1468
|
+
*
|
|
1469
|
+
* If empty, this function throws an error. It is usually a programming
|
|
1470
|
+
* mistake to hash an empty password.
|
|
1471
|
+
*/
|
|
1472
|
+
password: Bun.StringOrBuffer,
|
|
1473
|
+
/**
|
|
1474
|
+
* @default "argon2id"
|
|
1475
|
+
*
|
|
1476
|
+
* When using bcrypt, passwords exceeding 72 characters will be SHA256'd before
|
|
1477
|
+
*/
|
|
1478
|
+
algorithm?: Password.AlgorithmLabel | Password.Argon2Algorithm | Password.BCryptAlgorithm,
|
|
1479
|
+
): string;
|
|
1480
|
+
};
|
|
1481
|
+
|
|
1482
|
+
interface BuildArtifact extends Blob {
|
|
1483
|
+
path: string;
|
|
1484
|
+
loader: Loader;
|
|
1485
|
+
hash: string | null;
|
|
1486
|
+
kind: "entry-point" | "chunk" | "asset" | "sourcemap";
|
|
1487
|
+
sourcemap: BuildArtifact | null;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
interface BuildOutput {
|
|
1491
|
+
outputs: BuildArtifact[];
|
|
1492
|
+
success: boolean;
|
|
1493
|
+
logs: Array<BuildMessage | ResolveMessage>;
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
function build(config: BuildConfig): Promise<BuildOutput>;
|
|
1497
|
+
|
|
1498
|
+
/**
|
|
1499
|
+
* A status that represents the outcome of a sent message.
|
|
1500
|
+
*
|
|
1501
|
+
* - if **0**, the message was **dropped**.
|
|
1502
|
+
* - if **-1**, there is **backpressure** of messages.
|
|
1503
|
+
* - if **>0**, it represents the **number of bytes sent**.
|
|
1504
|
+
*
|
|
1505
|
+
* @example
|
|
1506
|
+
* ```js
|
|
1507
|
+
* const status = ws.send("Hello!");
|
|
1508
|
+
* if (status === 0) {
|
|
1509
|
+
* console.log("Message was dropped");
|
|
1510
|
+
* } else if (status === -1) {
|
|
1511
|
+
* console.log("Backpressure was applied");
|
|
1512
|
+
* } else {
|
|
1513
|
+
* console.log(`Success! Sent ${status} bytes`);
|
|
1514
|
+
* }
|
|
1515
|
+
* ```
|
|
1516
|
+
*/
|
|
1517
|
+
type ServerWebSocketSendStatus = number;
|
|
1518
|
+
|
|
1519
|
+
/**
|
|
1520
|
+
* A state that represents if a WebSocket is connected.
|
|
1521
|
+
*
|
|
1522
|
+
* - `WebSocket.CONNECTING` is `0`, the connection is pending.
|
|
1523
|
+
* - `WebSocket.OPEN` is `1`, the connection is established and `send()` is possible.
|
|
1524
|
+
* - `WebSocket.CLOSING` is `2`, the connection is closing.
|
|
1525
|
+
* - `WebSocket.CLOSED` is `3`, the connection is closed or couldn't be opened.
|
|
1526
|
+
*
|
|
1527
|
+
* @link https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
|
|
1528
|
+
*/
|
|
1529
|
+
type WebSocketReadyState = 0 | 1 | 2 | 3;
|
|
1530
|
+
|
|
1531
|
+
/**
|
|
1532
|
+
* A fast WebSocket designed for servers.
|
|
1533
|
+
*
|
|
1534
|
+
* Features:
|
|
1535
|
+
* - **Message compression** - Messages can be compressed
|
|
1536
|
+
* - **Backpressure** - If the client is not ready to receive data, the server will tell you.
|
|
1537
|
+
* - **Dropped messages** - If the client cannot receive data, the server will tell you.
|
|
1538
|
+
* - **Topics** - Messages can be {@link ServerWebSocket.publish}ed to a specific topic and the client can {@link ServerWebSocket.subscribe} to topics
|
|
1539
|
+
*
|
|
1540
|
+
* This is slightly different than the browser {@link WebSocket} which Bun supports for clients.
|
|
1541
|
+
*
|
|
1542
|
+
* Powered by [uWebSockets](https://github.com/uNetworking/uWebSockets).
|
|
1543
|
+
*
|
|
1544
|
+
* @example
|
|
1545
|
+
* import { serve } from "bun";
|
|
1546
|
+
*
|
|
1547
|
+
* serve({
|
|
1548
|
+
* websocket: {
|
|
1549
|
+
* open(ws) {
|
|
1550
|
+
* console.log("Connected", ws.remoteAddress);
|
|
1551
|
+
* },
|
|
1552
|
+
* message(ws, data) {
|
|
1553
|
+
* console.log("Received", data);
|
|
1554
|
+
* ws.send(data);
|
|
1555
|
+
* },
|
|
1556
|
+
* close(ws, code, reason) {
|
|
1557
|
+
* console.log("Disconnected", code, reason);
|
|
1558
|
+
* },
|
|
1559
|
+
* }
|
|
1560
|
+
* });
|
|
1561
|
+
*/
|
|
1562
|
+
interface ServerWebSocket<T = undefined> {
|
|
1563
|
+
/**
|
|
1564
|
+
* Sends a message to the client.
|
|
1565
|
+
*
|
|
1566
|
+
* @param data The data to send.
|
|
1567
|
+
* @param compress Should the data be compressed? If the client does not support compression, this is ignored.
|
|
1568
|
+
* @example
|
|
1569
|
+
* ws.send("Hello!");
|
|
1570
|
+
* ws.send("Compress this.", true);
|
|
1571
|
+
* ws.send(new Uint8Array([1, 2, 3, 4]));
|
|
1572
|
+
*/
|
|
1573
|
+
send(data: string | Bun.BufferSource, compress?: boolean): ServerWebSocketSendStatus;
|
|
1574
|
+
|
|
1575
|
+
/**
|
|
1576
|
+
* Sends a text message to the client.
|
|
1577
|
+
*
|
|
1578
|
+
* @param data The data to send.
|
|
1579
|
+
* @param compress Should the data be compressed? If the client does not support compression, this is ignored.
|
|
1580
|
+
* @example
|
|
1581
|
+
* ws.send("Hello!");
|
|
1582
|
+
* ws.send("Compress this.", true);
|
|
1583
|
+
*/
|
|
1584
|
+
sendText(data: string, compress?: boolean): ServerWebSocketSendStatus;
|
|
1585
|
+
|
|
1586
|
+
/**
|
|
1587
|
+
* Sends a binary message to the client.
|
|
1588
|
+
*
|
|
1589
|
+
* @param data The data to send.
|
|
1590
|
+
* @param compress Should the data be compressed? If the client does not support compression, this is ignored.
|
|
1591
|
+
* @example
|
|
1592
|
+
* ws.send(new TextEncoder().encode("Hello!"));
|
|
1593
|
+
* ws.send(new Uint8Array([1, 2, 3, 4]), true);
|
|
1594
|
+
*/
|
|
1595
|
+
sendBinary(data: Bun.BufferSource, compress?: boolean): ServerWebSocketSendStatus;
|
|
1596
|
+
|
|
1597
|
+
/**
|
|
1598
|
+
* Closes the connection.
|
|
1599
|
+
*
|
|
1600
|
+
* Here is a list of close codes:
|
|
1601
|
+
* - `1000` means "normal closure" **(default)**
|
|
1602
|
+
* - `1009` means a message was too big and was rejected
|
|
1603
|
+
* - `1011` means the server encountered an error
|
|
1604
|
+
* - `1012` means the server is restarting
|
|
1605
|
+
* - `1013` means the server is too busy or the client is rate-limited
|
|
1606
|
+
* - `4000` through `4999` are reserved for applications (you can use it!)
|
|
1607
|
+
*
|
|
1608
|
+
* To close the connection abruptly, use `terminate()`.
|
|
1609
|
+
*
|
|
1610
|
+
* @param code The close code to send
|
|
1611
|
+
* @param reason The close reason to send
|
|
1612
|
+
*/
|
|
1613
|
+
close(code?: number, reason?: string): void;
|
|
1614
|
+
|
|
1615
|
+
/**
|
|
1616
|
+
* Abruptly close the connection.
|
|
1617
|
+
*
|
|
1618
|
+
* To gracefully close the connection, use `close()`.
|
|
1619
|
+
*/
|
|
1620
|
+
terminate(): void;
|
|
1621
|
+
|
|
1622
|
+
/**
|
|
1623
|
+
* Sends a ping.
|
|
1624
|
+
*
|
|
1625
|
+
* @param data The data to send
|
|
1626
|
+
*/
|
|
1627
|
+
ping(data?: string | Bun.BufferSource): ServerWebSocketSendStatus;
|
|
1628
|
+
|
|
1629
|
+
/**
|
|
1630
|
+
* Sends a pong.
|
|
1631
|
+
*
|
|
1632
|
+
* @param data The data to send
|
|
1633
|
+
*/
|
|
1634
|
+
pong(data?: string | Bun.BufferSource): ServerWebSocketSendStatus;
|
|
1635
|
+
|
|
1636
|
+
/**
|
|
1637
|
+
* Sends a message to subscribers of the topic.
|
|
1638
|
+
*
|
|
1639
|
+
* @param topic The topic name.
|
|
1640
|
+
* @param data The data to send.
|
|
1641
|
+
* @param compress Should the data be compressed? If the client does not support compression, this is ignored.
|
|
1642
|
+
* @example
|
|
1643
|
+
* ws.publish("chat", "Hello!");
|
|
1644
|
+
* ws.publish("chat", "Compress this.", true);
|
|
1645
|
+
* ws.publish("chat", new Uint8Array([1, 2, 3, 4]));
|
|
1646
|
+
*/
|
|
1647
|
+
publish(topic: string, data: string | Bun.BufferSource, compress?: boolean): ServerWebSocketSendStatus;
|
|
1648
|
+
|
|
1649
|
+
/**
|
|
1650
|
+
* Sends a text message to subscribers of the topic.
|
|
1651
|
+
*
|
|
1652
|
+
* @param topic The topic name.
|
|
1653
|
+
* @param data The data to send.
|
|
1654
|
+
* @param compress Should the data be compressed? If the client does not support compression, this is ignored.
|
|
1655
|
+
* @example
|
|
1656
|
+
* ws.publish("chat", "Hello!");
|
|
1657
|
+
* ws.publish("chat", "Compress this.", true);
|
|
1658
|
+
*/
|
|
1659
|
+
publishText(topic: string, data: string, compress?: boolean): ServerWebSocketSendStatus;
|
|
1660
|
+
|
|
1661
|
+
/**
|
|
1662
|
+
* Sends a binary message to subscribers of the topic.
|
|
1663
|
+
*
|
|
1664
|
+
* @param topic The topic name.
|
|
1665
|
+
* @param data The data to send.
|
|
1666
|
+
* @param compress Should the data be compressed? If the client does not support compression, this is ignored.
|
|
1667
|
+
* @example
|
|
1668
|
+
* ws.publish("chat", new TextEncoder().encode("Hello!"));
|
|
1669
|
+
* ws.publish("chat", new Uint8Array([1, 2, 3, 4]), true);
|
|
1670
|
+
*/
|
|
1671
|
+
publishBinary(topic: string, data: Bun.BufferSource, compress?: boolean): ServerWebSocketSendStatus;
|
|
1672
|
+
|
|
1673
|
+
/**
|
|
1674
|
+
* Subscribes a client to the topic.
|
|
1675
|
+
*
|
|
1676
|
+
* @param topic The topic name.
|
|
1677
|
+
* @example
|
|
1678
|
+
* ws.subscribe("chat");
|
|
1679
|
+
*/
|
|
1680
|
+
subscribe(topic: string): void;
|
|
1681
|
+
|
|
1682
|
+
/**
|
|
1683
|
+
* Unsubscribes a client to the topic.
|
|
1684
|
+
*
|
|
1685
|
+
* @param topic The topic name.
|
|
1686
|
+
* @example
|
|
1687
|
+
* ws.unsubscribe("chat");
|
|
1688
|
+
*/
|
|
1689
|
+
unsubscribe(topic: string): void;
|
|
1690
|
+
|
|
1691
|
+
/**
|
|
1692
|
+
* Is the client subscribed to a topic?
|
|
1693
|
+
*
|
|
1694
|
+
* @param topic The topic name.
|
|
1695
|
+
* @example
|
|
1696
|
+
* ws.subscribe("chat");
|
|
1697
|
+
* console.log(ws.isSubscribed("chat")); // true
|
|
1698
|
+
*/
|
|
1699
|
+
isSubscribed(topic: string): boolean;
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* Batches `send()` and `publish()` operations, which makes it faster to send data.
|
|
1703
|
+
*
|
|
1704
|
+
* The `message`, `open`, and `drain` callbacks are automatically corked, so
|
|
1705
|
+
* you only need to call this if you are sending messages outside of those
|
|
1706
|
+
* callbacks or in async functions.
|
|
1707
|
+
*
|
|
1708
|
+
* @param callback The callback to run.
|
|
1709
|
+
* @example
|
|
1710
|
+
* ws.cork((ctx) => {
|
|
1711
|
+
* ctx.send("These messages");
|
|
1712
|
+
* ctx.sendText("are sent");
|
|
1713
|
+
* ctx.sendBinary(new TextEncoder().encode("together!"));
|
|
1714
|
+
* });
|
|
1715
|
+
*/
|
|
1716
|
+
cork<T = unknown>(callback: (ws: ServerWebSocket<T>) => T): T;
|
|
1717
|
+
|
|
1718
|
+
/**
|
|
1719
|
+
* The IP address of the client.
|
|
1720
|
+
*
|
|
1721
|
+
* @example
|
|
1722
|
+
* console.log(socket.remoteAddress); // "127.0.0.1"
|
|
1723
|
+
*/
|
|
1724
|
+
readonly remoteAddress: string;
|
|
1725
|
+
|
|
1726
|
+
/**
|
|
1727
|
+
* The ready state of the client.
|
|
1728
|
+
*
|
|
1729
|
+
* - if `0`, the client is connecting.
|
|
1730
|
+
* - if `1`, the client is connected.
|
|
1731
|
+
* - if `2`, the client is closing.
|
|
1732
|
+
* - if `3`, the client is closed.
|
|
1733
|
+
*
|
|
1734
|
+
* @example
|
|
1735
|
+
* console.log(socket.readyState); // 1
|
|
1736
|
+
*/
|
|
1737
|
+
readonly readyState: WebSocketReadyState;
|
|
1738
|
+
|
|
1739
|
+
/**
|
|
1740
|
+
* Sets how binary data is returned in events.
|
|
1741
|
+
*
|
|
1742
|
+
* - if `nodebuffer`, binary data is returned as `Buffer` objects. **(default)**
|
|
1743
|
+
* - if `arraybuffer`, binary data is returned as `ArrayBuffer` objects.
|
|
1744
|
+
* - if `uint8array`, binary data is returned as `Uint8Array` objects.
|
|
1745
|
+
*
|
|
1746
|
+
* @example
|
|
1747
|
+
* let ws: WebSocket;
|
|
1748
|
+
* ws.binaryType = "uint8array";
|
|
1749
|
+
* ws.addEventListener("message", ({ data }) => {
|
|
1750
|
+
* console.log(data instanceof Uint8Array); // true
|
|
1751
|
+
* });
|
|
1752
|
+
*/
|
|
1753
|
+
binaryType?: "nodebuffer" | "arraybuffer" | "uint8array";
|
|
1754
|
+
|
|
1755
|
+
/**
|
|
1756
|
+
* Custom data that you can assign to a client, can be read and written at any time.
|
|
1757
|
+
*
|
|
1758
|
+
* @example
|
|
1759
|
+
* import { serve } from "bun";
|
|
1760
|
+
*
|
|
1761
|
+
* serve({
|
|
1762
|
+
* fetch(request, server) {
|
|
1763
|
+
* const data = {
|
|
1764
|
+
* accessToken: request.headers.get("Authorization"),
|
|
1765
|
+
* };
|
|
1766
|
+
* if (server.upgrade(request, { data })) {
|
|
1767
|
+
* return;
|
|
1768
|
+
* }
|
|
1769
|
+
* return new Response();
|
|
1770
|
+
* },
|
|
1771
|
+
* websocket: {
|
|
1772
|
+
* open(ws) {
|
|
1773
|
+
* console.log(ws.data.accessToken);
|
|
1774
|
+
* }
|
|
1775
|
+
* }
|
|
1776
|
+
* });
|
|
1777
|
+
*/
|
|
1778
|
+
data: T;
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
/**
|
|
1782
|
+
* Compression options for WebSocket messages.
|
|
1783
|
+
*/
|
|
1784
|
+
type WebSocketCompressor =
|
|
1785
|
+
| "disable"
|
|
1786
|
+
| "shared"
|
|
1787
|
+
| "dedicated"
|
|
1788
|
+
| "3KB"
|
|
1789
|
+
| "4KB"
|
|
1790
|
+
| "8KB"
|
|
1791
|
+
| "16KB"
|
|
1792
|
+
| "32KB"
|
|
1793
|
+
| "64KB"
|
|
1794
|
+
| "128KB"
|
|
1795
|
+
| "256KB";
|
|
1796
|
+
|
|
1797
|
+
/**
|
|
1798
|
+
* Create a server-side {@link ServerWebSocket} handler for use with {@link Bun.serve}
|
|
1799
|
+
*
|
|
1800
|
+
* @example
|
|
1801
|
+
* ```ts
|
|
1802
|
+
* import { websocket, serve } from "bun";
|
|
1803
|
+
*
|
|
1804
|
+
* serve<{name: string}>({
|
|
1805
|
+
* port: 3000,
|
|
1806
|
+
* websocket: {
|
|
1807
|
+
* open: (ws) => {
|
|
1808
|
+
* console.log("Client connected");
|
|
1809
|
+
* },
|
|
1810
|
+
* message: (ws, message) => {
|
|
1811
|
+
* console.log(`${ws.data.name}: ${message}`);
|
|
1812
|
+
* },
|
|
1813
|
+
* close: (ws) => {
|
|
1814
|
+
* console.log("Client disconnected");
|
|
1815
|
+
* },
|
|
1816
|
+
* },
|
|
1817
|
+
*
|
|
1818
|
+
* fetch(req, server) {
|
|
1819
|
+
* const url = new URL(req.url);
|
|
1820
|
+
* if (url.pathname === "/chat") {
|
|
1821
|
+
* const upgraded = server.upgrade(req, {
|
|
1822
|
+
* data: {
|
|
1823
|
+
* name: new URL(req.url).searchParams.get("name"),
|
|
1824
|
+
* },
|
|
1825
|
+
* });
|
|
1826
|
+
* if (!upgraded) {
|
|
1827
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
1828
|
+
* }
|
|
1829
|
+
* return;
|
|
1830
|
+
* }
|
|
1831
|
+
* return new Response("Hello World");
|
|
1832
|
+
* },
|
|
1833
|
+
* });
|
|
1834
|
+
*/
|
|
1835
|
+
interface WebSocketHandler<T = undefined> {
|
|
1836
|
+
/**
|
|
1837
|
+
* Called when the server receives an incoming message.
|
|
1838
|
+
*
|
|
1839
|
+
* If the message is not a `string`, its type is based on the value of `binaryType`.
|
|
1840
|
+
* - if `nodebuffer`, then the message is a `Buffer`.
|
|
1841
|
+
* - if `arraybuffer`, then the message is an `ArrayBuffer`.
|
|
1842
|
+
* - if `uint8array`, then the message is a `Uint8Array`.
|
|
1843
|
+
*
|
|
1844
|
+
* @param ws The websocket that sent the message
|
|
1845
|
+
* @param message The message received
|
|
1846
|
+
*/
|
|
1847
|
+
message(ws: ServerWebSocket<T>, message: string | Buffer): void | Promise<void>;
|
|
1848
|
+
|
|
1849
|
+
/**
|
|
1850
|
+
* Called when a connection is opened.
|
|
1851
|
+
*
|
|
1852
|
+
* @param ws The websocket that was opened
|
|
1853
|
+
*/
|
|
1854
|
+
open?(ws: ServerWebSocket<T>): void | Promise<void>;
|
|
1855
|
+
|
|
1856
|
+
/**
|
|
1857
|
+
* Called when a connection was previously under backpressure,
|
|
1858
|
+
* meaning it had too many queued messages, but is now ready to receive more data.
|
|
1859
|
+
*
|
|
1860
|
+
* @param ws The websocket that is ready for more data
|
|
1861
|
+
*/
|
|
1862
|
+
drain?(ws: ServerWebSocket<T>): void | Promise<void>;
|
|
1863
|
+
|
|
1864
|
+
/**
|
|
1865
|
+
* Called when a connection is closed.
|
|
1866
|
+
*
|
|
1867
|
+
* @param ws The websocket that was closed
|
|
1868
|
+
* @param code The close code
|
|
1869
|
+
* @param message The close message
|
|
1870
|
+
*/
|
|
1871
|
+
close?(ws: ServerWebSocket<T>, code: number, reason: string): void | Promise<void>;
|
|
1872
|
+
|
|
1873
|
+
/**
|
|
1874
|
+
* Called when a ping is sent.
|
|
1875
|
+
*
|
|
1876
|
+
* @param ws The websocket that received the ping
|
|
1877
|
+
* @param data The data sent with the ping
|
|
1878
|
+
*/
|
|
1879
|
+
ping?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>;
|
|
1880
|
+
|
|
1881
|
+
/**
|
|
1882
|
+
* Called when a pong is received.
|
|
1883
|
+
*
|
|
1884
|
+
* @param ws The websocket that received the ping
|
|
1885
|
+
* @param data The data sent with the ping
|
|
1886
|
+
*/
|
|
1887
|
+
pong?(ws: ServerWebSocket<T>, data: Buffer): void | Promise<void>;
|
|
1888
|
+
|
|
1889
|
+
/**
|
|
1890
|
+
* Sets the maximum size of messages in bytes.
|
|
1891
|
+
*
|
|
1892
|
+
* Default is 16 MB, or `1024 * 1024 * 16` in bytes.
|
|
1893
|
+
*/
|
|
1894
|
+
maxPayloadLength?: number;
|
|
1895
|
+
|
|
1896
|
+
/**
|
|
1897
|
+
* Sets the maximum number of bytes that can be buffered on a single connection.
|
|
1898
|
+
*
|
|
1899
|
+
* Default is 16 MB, or `1024 * 1024 * 16` in bytes.
|
|
1900
|
+
*/
|
|
1901
|
+
backpressureLimit?: number;
|
|
1902
|
+
|
|
1903
|
+
/**
|
|
1904
|
+
* Sets if the connection should be closed if `backpressureLimit` is reached.
|
|
1905
|
+
*
|
|
1906
|
+
* Default is `false`.
|
|
1907
|
+
*/
|
|
1908
|
+
closeOnBackpressureLimit?: boolean;
|
|
1909
|
+
|
|
1910
|
+
/**
|
|
1911
|
+
* Sets the the number of seconds to wait before timing out a connection
|
|
1912
|
+
* due to no messages or pings.
|
|
1913
|
+
*
|
|
1914
|
+
* Default is 2 minutes, or `120` in seconds.
|
|
1915
|
+
*/
|
|
1916
|
+
idleTimeout?: number;
|
|
1917
|
+
|
|
1918
|
+
/**
|
|
1919
|
+
* Should `ws.publish()` also send a message to `ws` (itself), if it is subscribed?
|
|
1920
|
+
*
|
|
1921
|
+
* Default is `false`.
|
|
1922
|
+
*/
|
|
1923
|
+
publishToSelf?: boolean;
|
|
1924
|
+
|
|
1925
|
+
/**
|
|
1926
|
+
* Should the server automatically send and respond to pings to clients?
|
|
1927
|
+
*
|
|
1928
|
+
* Default is `true`.
|
|
1929
|
+
*/
|
|
1930
|
+
sendPings?: boolean;
|
|
1931
|
+
|
|
1932
|
+
/**
|
|
1933
|
+
* Sets the compression level for messages, for clients that supports it. By default, compression is disabled.
|
|
1934
|
+
*
|
|
1935
|
+
* Default is `false`.
|
|
1936
|
+
*/
|
|
1937
|
+
perMessageDeflate?:
|
|
1938
|
+
| boolean
|
|
1939
|
+
| {
|
|
1940
|
+
/**
|
|
1941
|
+
* Sets the compression level.
|
|
1942
|
+
*/
|
|
1943
|
+
compress?: WebSocketCompressor | boolean;
|
|
1944
|
+
/**
|
|
1945
|
+
* Sets the decompression level.
|
|
1946
|
+
*/
|
|
1947
|
+
decompress?: WebSocketCompressor | boolean;
|
|
1948
|
+
};
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
interface GenericServeOptions {
|
|
1952
|
+
/**
|
|
1953
|
+
* What URI should be used to make {@link Request.url} absolute?
|
|
1954
|
+
*
|
|
1955
|
+
* By default, looks at {@link hostname}, {@link port}, and whether or not SSL is enabled to generate one
|
|
1956
|
+
*
|
|
1957
|
+
* @example
|
|
1958
|
+
* ```js
|
|
1959
|
+
* "http://my-app.com"
|
|
1960
|
+
* ```
|
|
1961
|
+
*
|
|
1962
|
+
* @example
|
|
1963
|
+
* ```js
|
|
1964
|
+
* "https://wongmjane.com/"
|
|
1965
|
+
* ```
|
|
1966
|
+
*
|
|
1967
|
+
* This should be the public, absolute URL – include the protocol and {@link hostname}. If the port isn't 80 or 443, then include the {@link port} too.
|
|
1968
|
+
*
|
|
1969
|
+
* @example
|
|
1970
|
+
* "http://localhost:3000"
|
|
1971
|
+
*/
|
|
1972
|
+
// baseURI?: string;
|
|
1973
|
+
|
|
1974
|
+
/**
|
|
1975
|
+
* What is the maximum size of a request body? (in bytes)
|
|
1976
|
+
* @default 1024 * 1024 * 128 // 128MB
|
|
1977
|
+
*/
|
|
1978
|
+
maxRequestBodySize?: number;
|
|
1979
|
+
|
|
1980
|
+
/**
|
|
1981
|
+
* Render contextual errors? This enables bun's error page
|
|
1982
|
+
* @default process.env.NODE_ENV !== 'production'
|
|
1983
|
+
*/
|
|
1984
|
+
development?: boolean;
|
|
1985
|
+
|
|
1986
|
+
error?: (this: Server, request: ErrorLike) => Response | Promise<Response> | undefined | Promise<undefined>;
|
|
1987
|
+
|
|
1988
|
+
/**
|
|
1989
|
+
* Uniquely identify a server instance with an ID
|
|
1990
|
+
*
|
|
1991
|
+
* ### When bun is started with the `--hot` flag
|
|
1992
|
+
*
|
|
1993
|
+
* This string will be used to hot reload the server without interrupting
|
|
1994
|
+
* pending requests or websockets. If not provided, a value will be
|
|
1995
|
+
* generated. To disable hot reloading, set this value to `null`.
|
|
1996
|
+
*
|
|
1997
|
+
* ### When bun is not started with the `--hot` flag
|
|
1998
|
+
*
|
|
1999
|
+
* This string will currently do nothing. But in the future it could be useful for logs or metrics.
|
|
2000
|
+
*/
|
|
2001
|
+
id?: string | null;
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
interface ServeOptions extends GenericServeOptions {
|
|
2005
|
+
/**
|
|
2006
|
+
* What port should the server listen on?
|
|
2007
|
+
* @default process.env.PORT || "3000"
|
|
2008
|
+
*/
|
|
2009
|
+
port?: string | number;
|
|
2010
|
+
|
|
2011
|
+
/**
|
|
2012
|
+
* If the `SO_REUSEPORT` flag should be set.
|
|
2013
|
+
*
|
|
2014
|
+
* This allows multiple processes to bind to the same port, which is useful for load balancing.
|
|
2015
|
+
*
|
|
2016
|
+
* @default false
|
|
2017
|
+
*/
|
|
2018
|
+
reusePort?: boolean;
|
|
2019
|
+
|
|
2020
|
+
/**
|
|
2021
|
+
* What hostname should the server listen on?
|
|
2022
|
+
*
|
|
2023
|
+
* @default
|
|
2024
|
+
* ```js
|
|
2025
|
+
* "0.0.0.0" // listen on all interfaces
|
|
2026
|
+
* ```
|
|
2027
|
+
* @example
|
|
2028
|
+
* ```js
|
|
2029
|
+
* "127.0.0.1" // Only listen locally
|
|
2030
|
+
* ```
|
|
2031
|
+
* @example
|
|
2032
|
+
* ```js
|
|
2033
|
+
* "remix.run" // Only listen on remix.run
|
|
2034
|
+
* ````
|
|
2035
|
+
*
|
|
2036
|
+
* note: hostname should not include a {@link port}
|
|
2037
|
+
*/
|
|
2038
|
+
hostname?: string;
|
|
2039
|
+
|
|
2040
|
+
/**
|
|
2041
|
+
* If set, the HTTP server will listen on a unix socket instead of a port.
|
|
2042
|
+
* (Cannot be used with hostname+port)
|
|
2043
|
+
*/
|
|
2044
|
+
unix?: never;
|
|
2045
|
+
|
|
2046
|
+
/**
|
|
2047
|
+
* Handle HTTP requests
|
|
2048
|
+
*
|
|
2049
|
+
* Respond to {@link Request} objects with a {@link Response} object.
|
|
2050
|
+
*/
|
|
2051
|
+
fetch(this: Server, request: Request, server: Server): Response | Promise<Response>;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
interface UnixServeOptions extends GenericServeOptions {
|
|
2055
|
+
/**
|
|
2056
|
+
* If set, the HTTP server will listen on a unix socket instead of a port.
|
|
2057
|
+
* (Cannot be used with hostname+port)
|
|
2058
|
+
*/
|
|
2059
|
+
unix: string;
|
|
2060
|
+
/**
|
|
2061
|
+
* Handle HTTP requests
|
|
2062
|
+
*
|
|
2063
|
+
* Respond to {@link Request} objects with a {@link Response} object.
|
|
2064
|
+
*/
|
|
2065
|
+
fetch(this: Server, request: Request, server: Server): Response | Promise<Response>;
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
interface WebSocketServeOptions<WebSocketDataType = undefined> extends GenericServeOptions {
|
|
2069
|
+
/**
|
|
2070
|
+
* What port should the server listen on?
|
|
2071
|
+
* @default process.env.PORT || "3000"
|
|
2072
|
+
*/
|
|
2073
|
+
port?: string | number;
|
|
2074
|
+
|
|
2075
|
+
/**
|
|
2076
|
+
* What hostname should the server listen on?
|
|
2077
|
+
*
|
|
2078
|
+
* @default
|
|
2079
|
+
* ```js
|
|
2080
|
+
* "0.0.0.0" // listen on all interfaces
|
|
2081
|
+
* ```
|
|
2082
|
+
* @example
|
|
2083
|
+
* ```js
|
|
2084
|
+
* "127.0.0.1" // Only listen locally
|
|
2085
|
+
* ```
|
|
2086
|
+
* @example
|
|
2087
|
+
* ```js
|
|
2088
|
+
* "remix.run" // Only listen on remix.run
|
|
2089
|
+
* ````
|
|
2090
|
+
*
|
|
2091
|
+
* note: hostname should not include a {@link port}
|
|
2092
|
+
*/
|
|
2093
|
+
hostname?: string;
|
|
2094
|
+
|
|
2095
|
+
/**
|
|
2096
|
+
* Enable websockets with {@link Bun.serve}
|
|
2097
|
+
*
|
|
2098
|
+
* For simpler type safety, see {@link Bun.websocket}
|
|
2099
|
+
*
|
|
2100
|
+
* @example
|
|
2101
|
+
* ```js
|
|
2102
|
+
* import { serve } from "bun";
|
|
2103
|
+
* serve({
|
|
2104
|
+
* websocket: {
|
|
2105
|
+
* open: (ws) => {
|
|
2106
|
+
* console.log("Client connected");
|
|
2107
|
+
* },
|
|
2108
|
+
* message: (ws, message) => {
|
|
2109
|
+
* console.log("Client sent message", message);
|
|
2110
|
+
* },
|
|
2111
|
+
* close: (ws) => {
|
|
2112
|
+
* console.log("Client disconnected");
|
|
2113
|
+
* },
|
|
2114
|
+
* },
|
|
2115
|
+
* fetch(req, server) {
|
|
2116
|
+
* const url = new URL(req.url);
|
|
2117
|
+
* if (url.pathname === "/chat") {
|
|
2118
|
+
* const upgraded = server.upgrade(req);
|
|
2119
|
+
* if (!upgraded) {
|
|
2120
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
2121
|
+
* }
|
|
2122
|
+
* }
|
|
2123
|
+
* return new Response("Hello World");
|
|
2124
|
+
* },
|
|
2125
|
+
* });
|
|
2126
|
+
* ```
|
|
2127
|
+
* Upgrade a {@link Request} to a {@link ServerWebSocket} via {@link Server.upgrade}
|
|
2128
|
+
*
|
|
2129
|
+
* Pass `data` in @{link Server.upgrade} to attach data to the {@link ServerWebSocket.data} property
|
|
2130
|
+
*/
|
|
2131
|
+
websocket: WebSocketHandler<WebSocketDataType>;
|
|
2132
|
+
|
|
2133
|
+
/**
|
|
2134
|
+
* Handle HTTP requests or upgrade them to a {@link ServerWebSocket}
|
|
2135
|
+
*
|
|
2136
|
+
* Respond to {@link Request} objects with a {@link Response} object.
|
|
2137
|
+
*/
|
|
2138
|
+
fetch(
|
|
2139
|
+
this: Server,
|
|
2140
|
+
request: Request,
|
|
2141
|
+
server: Server,
|
|
2142
|
+
): Response | undefined | void | Promise<Response | undefined | void>;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
interface UnixWebSocketServeOptions<WebSocketDataType = undefined> extends GenericServeOptions {
|
|
2146
|
+
/**
|
|
2147
|
+
* If set, the HTTP server will listen on a unix socket instead of a port.
|
|
2148
|
+
* (Cannot be used with hostname+port)
|
|
2149
|
+
*/
|
|
2150
|
+
unix: string;
|
|
2151
|
+
|
|
2152
|
+
/**
|
|
2153
|
+
* Enable websockets with {@link Bun.serve}
|
|
2154
|
+
*
|
|
2155
|
+
* For simpler type safety, see {@link Bun.websocket}
|
|
2156
|
+
*
|
|
2157
|
+
* @example
|
|
2158
|
+
* ```js
|
|
2159
|
+
* import { serve } from "bun";
|
|
2160
|
+
* serve({
|
|
2161
|
+
* websocket: {
|
|
2162
|
+
* open: (ws) => {
|
|
2163
|
+
* console.log("Client connected");
|
|
2164
|
+
* },
|
|
2165
|
+
* message: (ws, message) => {
|
|
2166
|
+
* console.log("Client sent message", message);
|
|
2167
|
+
* },
|
|
2168
|
+
* close: (ws) => {
|
|
2169
|
+
* console.log("Client disconnected");
|
|
2170
|
+
* },
|
|
2171
|
+
* },
|
|
2172
|
+
* fetch(req, server) {
|
|
2173
|
+
* const url = new URL(req.url);
|
|
2174
|
+
* if (url.pathname === "/chat") {
|
|
2175
|
+
* const upgraded = server.upgrade(req);
|
|
2176
|
+
* if (!upgraded) {
|
|
2177
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
2178
|
+
* }
|
|
2179
|
+
* }
|
|
2180
|
+
* return new Response("Hello World");
|
|
2181
|
+
* },
|
|
2182
|
+
* });
|
|
2183
|
+
* ```
|
|
2184
|
+
* Upgrade a {@link Request} to a {@link ServerWebSocket} via {@link Server.upgrade}
|
|
2185
|
+
*
|
|
2186
|
+
* Pass `data` in @{link Server.upgrade} to attach data to the {@link ServerWebSocket.data} property
|
|
2187
|
+
*/
|
|
2188
|
+
websocket: WebSocketHandler<WebSocketDataType>;
|
|
2189
|
+
|
|
2190
|
+
/**
|
|
2191
|
+
* Handle HTTP requests or upgrade them to a {@link ServerWebSocket}
|
|
2192
|
+
*
|
|
2193
|
+
* Respond to {@link Request} objects with a {@link Response} object.
|
|
2194
|
+
*/
|
|
2195
|
+
fetch(this: Server, request: Request, server: Server): Response | undefined | Promise<Response | undefined>;
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
interface TLSWebSocketServeOptions<WebSocketDataType = undefined>
|
|
2199
|
+
extends WebSocketServeOptions<WebSocketDataType>,
|
|
2200
|
+
TLSOptions {
|
|
2201
|
+
unix?: never;
|
|
2202
|
+
tls?: TLSOptions;
|
|
2203
|
+
}
|
|
2204
|
+
interface UnixTLSWebSocketServeOptions<WebSocketDataType = undefined>
|
|
2205
|
+
extends UnixWebSocketServeOptions<WebSocketDataType>,
|
|
2206
|
+
TLSOptions {
|
|
2207
|
+
/**
|
|
2208
|
+
* If set, the HTTP server will listen on a unix socket instead of a port.
|
|
2209
|
+
* (Cannot be used with hostname+port)
|
|
2210
|
+
*/
|
|
2211
|
+
unix: string;
|
|
2212
|
+
tls?: TLSOptions;
|
|
2213
|
+
}
|
|
2214
|
+
interface ErrorLike extends Error {
|
|
2215
|
+
code?: string;
|
|
2216
|
+
errno?: number;
|
|
2217
|
+
syscall?: string;
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
interface TLSOptions {
|
|
2221
|
+
/**
|
|
2222
|
+
* Passphrase for the TLS key
|
|
2223
|
+
*/
|
|
2224
|
+
passphrase?: string;
|
|
2225
|
+
|
|
2226
|
+
/**
|
|
2227
|
+
* File path to a .pem file custom Diffie Helman parameters
|
|
2228
|
+
*/
|
|
2229
|
+
dhParamsFile?: string;
|
|
2230
|
+
|
|
2231
|
+
/**
|
|
2232
|
+
* Explicitly set a server name
|
|
2233
|
+
*/
|
|
2234
|
+
serverName?: string;
|
|
2235
|
+
|
|
2236
|
+
/**
|
|
2237
|
+
* This sets `OPENSSL_RELEASE_BUFFERS` to 1.
|
|
2238
|
+
* It reduces overall performance but saves some memory.
|
|
2239
|
+
* @default false
|
|
2240
|
+
*/
|
|
2241
|
+
lowMemoryMode?: boolean;
|
|
2242
|
+
|
|
2243
|
+
/**
|
|
2244
|
+
* Optionally override the trusted CA certificates. Default is to trust
|
|
2245
|
+
* the well-known CAs curated by Mozilla. Mozilla's CAs are completely
|
|
2246
|
+
* replaced when CAs are explicitly specified using this option.
|
|
2247
|
+
*/
|
|
2248
|
+
ca?: string | Buffer | BunFile | Array<string | Buffer | BunFile> | undefined;
|
|
2249
|
+
/**
|
|
2250
|
+
* Cert chains in PEM format. One cert chain should be provided per
|
|
2251
|
+
* private key. Each cert chain should consist of the PEM formatted
|
|
2252
|
+
* certificate for a provided private key, followed by the PEM
|
|
2253
|
+
* formatted intermediate certificates (if any), in order, and not
|
|
2254
|
+
* including the root CA (the root CA must be pre-known to the peer,
|
|
2255
|
+
* see ca). When providing multiple cert chains, they do not have to
|
|
2256
|
+
* be in the same order as their private keys in key. If the
|
|
2257
|
+
* intermediate certificates are not provided, the peer will not be
|
|
2258
|
+
* able to validate the certificate, and the handshake will fail.
|
|
2259
|
+
*/
|
|
2260
|
+
cert?: string | Buffer | BunFile | Array<string | Buffer | BunFile> | undefined;
|
|
2261
|
+
/**
|
|
2262
|
+
* Private keys in PEM format. PEM allows the option of private keys
|
|
2263
|
+
* being encrypted. Encrypted keys will be decrypted with
|
|
2264
|
+
* options.passphrase. Multiple keys using different algorithms can be
|
|
2265
|
+
* provided either as an array of unencrypted key strings or buffers,
|
|
2266
|
+
* or an array of objects in the form {pem: <string|buffer>[,
|
|
2267
|
+
* passphrase: <string>]}. The object form can only occur in an array.
|
|
2268
|
+
* object.passphrase is optional. Encrypted keys will be decrypted with
|
|
2269
|
+
* object.passphrase if provided, or options.passphrase if it is not.
|
|
2270
|
+
*/
|
|
2271
|
+
key?: string | Buffer | BunFile | Array<string | Buffer | BunFile> | undefined;
|
|
2272
|
+
/**
|
|
2273
|
+
* Optionally affect the OpenSSL protocol behavior, which is not
|
|
2274
|
+
* usually necessary. This should be used carefully if at all! Value is
|
|
2275
|
+
* a numeric bitmask of the SSL_OP_* options from OpenSSL Options
|
|
2276
|
+
*/
|
|
2277
|
+
secureOptions?: number | undefined; // Value is a numeric bitmask of the `SSL_OP_*` options
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
interface TLSServeOptions extends ServeOptions, TLSOptions {
|
|
2281
|
+
/**
|
|
2282
|
+
* The keys are [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication) hostnames.
|
|
2283
|
+
* The values are SSL options objects.
|
|
2284
|
+
*/
|
|
2285
|
+
serverNames?: Record<string, TLSOptions>;
|
|
2286
|
+
|
|
2287
|
+
tls?: TLSOptions;
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
interface UnixTLSServeOptions extends UnixServeOptions, TLSOptions {
|
|
2291
|
+
/**
|
|
2292
|
+
* The keys are [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication) hostnames.
|
|
2293
|
+
* The values are SSL options objects.
|
|
2294
|
+
*/
|
|
2295
|
+
serverNames?: Record<string, TLSOptions>;
|
|
2296
|
+
|
|
2297
|
+
tls?: TLSOptions;
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
interface SocketAddress {
|
|
2301
|
+
/**
|
|
2302
|
+
* The IP address of the client.
|
|
2303
|
+
*/
|
|
2304
|
+
address: string;
|
|
2305
|
+
/**
|
|
2306
|
+
* The port of the client.
|
|
2307
|
+
*/
|
|
2308
|
+
port: number;
|
|
2309
|
+
/**
|
|
2310
|
+
* The IP family ("IPv4" or "IPv6").
|
|
2311
|
+
*/
|
|
2312
|
+
family: "IPv4" | "IPv6";
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
/**
|
|
2316
|
+
* HTTP & HTTPS Server
|
|
2317
|
+
*
|
|
2318
|
+
* To start the server, see {@link serve}
|
|
2319
|
+
*
|
|
2320
|
+
* For performance, Bun pre-allocates most of the data for 2048 concurrent requests.
|
|
2321
|
+
* That means starting a new server allocates about 500 KB of memory. Try to
|
|
2322
|
+
* avoid starting and stopping the server often (unless it's a new instance of bun).
|
|
2323
|
+
*
|
|
2324
|
+
* Powered by a fork of [uWebSockets](https://github.com/uNetworking/uWebSockets). Thank you @alexhultman.
|
|
2325
|
+
*/
|
|
2326
|
+
interface Server {
|
|
2327
|
+
/**
|
|
2328
|
+
* Stop listening to prevent new connections from being accepted.
|
|
2329
|
+
*
|
|
2330
|
+
* By default, it does not cancel in-flight requests or websockets. That means it may take some time before all network activity stops.
|
|
2331
|
+
*
|
|
2332
|
+
* @param closeActiveConnections Immediately terminate in-flight requests, websockets, and stop accepting new connections.
|
|
2333
|
+
* @default false
|
|
2334
|
+
*/
|
|
2335
|
+
stop(closeActiveConnections?: boolean): void;
|
|
2336
|
+
|
|
2337
|
+
/**
|
|
2338
|
+
* Update the `fetch` and `error` handlers without restarting the server.
|
|
2339
|
+
*
|
|
2340
|
+
* This is useful if you want to change the behavior of your server without
|
|
2341
|
+
* restarting it or for hot reloading.
|
|
2342
|
+
*
|
|
2343
|
+
* @example
|
|
2344
|
+
*
|
|
2345
|
+
* ```js
|
|
2346
|
+
* // create the server
|
|
2347
|
+
* const server = Bun.serve({
|
|
2348
|
+
* fetch(request) {
|
|
2349
|
+
* return new Response("Hello World v1")
|
|
2350
|
+
* }
|
|
2351
|
+
* });
|
|
2352
|
+
*
|
|
2353
|
+
* // Update the server to return a different response
|
|
2354
|
+
* server.reload({
|
|
2355
|
+
* fetch(request) {
|
|
2356
|
+
* return new Response("Hello World v2")
|
|
2357
|
+
* }
|
|
2358
|
+
* });
|
|
2359
|
+
* ```
|
|
2360
|
+
*
|
|
2361
|
+
* Passing other options such as `port` or `hostname` won't do anything.
|
|
2362
|
+
*/
|
|
2363
|
+
reload(options: Serve): void;
|
|
2364
|
+
|
|
2365
|
+
/**
|
|
2366
|
+
* Mock the fetch handler for a running server.
|
|
2367
|
+
*
|
|
2368
|
+
* This feature is not fully implemented yet. It doesn't normalize URLs
|
|
2369
|
+
* consistently in all cases and it doesn't yet call the `error` handler
|
|
2370
|
+
* consistently. This needs to be fixed
|
|
2371
|
+
*/
|
|
2372
|
+
fetch(request: Request | string): Response | Promise<Response>;
|
|
2373
|
+
|
|
2374
|
+
/**
|
|
2375
|
+
* Upgrade a {@link Request} to a {@link ServerWebSocket}
|
|
2376
|
+
*
|
|
2377
|
+
* @param request The {@link Request} to upgrade
|
|
2378
|
+
* @param options Pass headers or attach data to the {@link ServerWebSocket}
|
|
2379
|
+
*
|
|
2380
|
+
* @returns `true` if the upgrade was successful and `false` if it failed
|
|
2381
|
+
*
|
|
2382
|
+
* @example
|
|
2383
|
+
* ```js
|
|
2384
|
+
* import { serve } from "bun";
|
|
2385
|
+
* serve({
|
|
2386
|
+
* websocket: {
|
|
2387
|
+
* open: (ws) => {
|
|
2388
|
+
* console.log("Client connected");
|
|
2389
|
+
* },
|
|
2390
|
+
* message: (ws, message) => {
|
|
2391
|
+
* console.log("Client sent message", message);
|
|
2392
|
+
* },
|
|
2393
|
+
* close: (ws) => {
|
|
2394
|
+
* console.log("Client disconnected");
|
|
2395
|
+
* },
|
|
2396
|
+
* },
|
|
2397
|
+
* fetch(req, server) {
|
|
2398
|
+
* const url = new URL(req.url);
|
|
2399
|
+
* if (url.pathname === "/chat") {
|
|
2400
|
+
* const upgraded = server.upgrade(req);
|
|
2401
|
+
* if (!upgraded) {
|
|
2402
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
2403
|
+
* }
|
|
2404
|
+
* }
|
|
2405
|
+
* return new Response("Hello World");
|
|
2406
|
+
* },
|
|
2407
|
+
* });
|
|
2408
|
+
* ```
|
|
2409
|
+
* What you pass to `data` is available on the {@link ServerWebSocket.data} property
|
|
2410
|
+
*/
|
|
2411
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
2412
|
+
upgrade<T = undefined>(
|
|
2413
|
+
request: Request,
|
|
2414
|
+
options?: {
|
|
2415
|
+
/**
|
|
2416
|
+
* Send any additional headers while upgrading, like cookies
|
|
2417
|
+
*/
|
|
2418
|
+
headers?: Bun.HeadersInit;
|
|
2419
|
+
/**
|
|
2420
|
+
* This value is passed to the {@link ServerWebSocket.data} property
|
|
2421
|
+
*/
|
|
2422
|
+
data?: T;
|
|
2423
|
+
},
|
|
2424
|
+
): boolean;
|
|
2425
|
+
|
|
2426
|
+
/**
|
|
2427
|
+
* Send a message to all connected {@link ServerWebSocket} subscribed to a topic
|
|
2428
|
+
*
|
|
2429
|
+
* @param topic The topic to publish to
|
|
2430
|
+
* @param data The data to send
|
|
2431
|
+
* @param compress Should the data be compressed? Ignored if the client does not support compression.
|
|
2432
|
+
*
|
|
2433
|
+
* @returns 0 if the message was dropped, -1 if backpressure was applied, or the number of bytes sent.
|
|
2434
|
+
*
|
|
2435
|
+
* @example
|
|
2436
|
+
*
|
|
2437
|
+
* ```js
|
|
2438
|
+
* server.publish("chat", "Hello World");
|
|
2439
|
+
* ```
|
|
2440
|
+
*
|
|
2441
|
+
* @example
|
|
2442
|
+
* ```js
|
|
2443
|
+
* server.publish("chat", new Uint8Array([1, 2, 3, 4]));
|
|
2444
|
+
* ```
|
|
2445
|
+
*
|
|
2446
|
+
* @example
|
|
2447
|
+
* ```js
|
|
2448
|
+
* server.publish("chat", new ArrayBuffer(4), true);
|
|
2449
|
+
* ```
|
|
2450
|
+
*
|
|
2451
|
+
* @example
|
|
2452
|
+
* ```js
|
|
2453
|
+
* server.publish("chat", new DataView(new ArrayBuffer(4)));
|
|
2454
|
+
* ```
|
|
2455
|
+
*/
|
|
2456
|
+
publish(
|
|
2457
|
+
topic: string,
|
|
2458
|
+
data: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
|
|
2459
|
+
compress?: boolean,
|
|
2460
|
+
): ServerWebSocketSendStatus;
|
|
2461
|
+
|
|
2462
|
+
/**
|
|
2463
|
+
* Returns the client IP address and port of the given Request. If the request was closed or is a unix socket, returns null.
|
|
2464
|
+
*
|
|
2465
|
+
* @example
|
|
2466
|
+
* ```js
|
|
2467
|
+
* export default {
|
|
2468
|
+
* async fetch(request, server) {
|
|
2469
|
+
* return new Response(server.requestIP(request));
|
|
2470
|
+
* }
|
|
2471
|
+
* }
|
|
2472
|
+
* ```
|
|
2473
|
+
*/
|
|
2474
|
+
requestIP(request: Request): SocketAddress | null;
|
|
2475
|
+
|
|
2476
|
+
/**
|
|
2477
|
+
* Undo a call to {@link Server.unref}
|
|
2478
|
+
*
|
|
2479
|
+
* If the Server has already been stopped, this does nothing.
|
|
2480
|
+
*
|
|
2481
|
+
* If {@link Server.ref} is called multiple times, this does nothing. Think of it as a boolean toggle.
|
|
2482
|
+
*/
|
|
2483
|
+
ref(): void;
|
|
2484
|
+
|
|
2485
|
+
/**
|
|
2486
|
+
* Don't keep the process alive if this server is the only thing left.
|
|
2487
|
+
* Active connections may continue to keep the process alive.
|
|
2488
|
+
*
|
|
2489
|
+
* By default, the server is ref'd.
|
|
2490
|
+
*
|
|
2491
|
+
* To prevent new connections from being accepted, use {@link Server.stop}
|
|
2492
|
+
*/
|
|
2493
|
+
unref(): void;
|
|
2494
|
+
|
|
2495
|
+
/**
|
|
2496
|
+
* How many requests are in-flight right now?
|
|
2497
|
+
*/
|
|
2498
|
+
readonly pendingRequests: number;
|
|
2499
|
+
|
|
2500
|
+
/**
|
|
2501
|
+
* How many {@link ServerWebSocket}s are in-flight right now?
|
|
2502
|
+
*/
|
|
2503
|
+
readonly pendingWebSockets: number;
|
|
2504
|
+
|
|
2505
|
+
readonly url: URL;
|
|
2506
|
+
|
|
2507
|
+
readonly port: number;
|
|
2508
|
+
/**
|
|
2509
|
+
* The hostname the server is listening on. Does not include the port
|
|
2510
|
+
* @example
|
|
2511
|
+
* ```js
|
|
2512
|
+
* "localhost"
|
|
2513
|
+
* ```
|
|
2514
|
+
*/
|
|
2515
|
+
readonly hostname: string;
|
|
2516
|
+
/**
|
|
2517
|
+
* Is the server running in development mode?
|
|
2518
|
+
*
|
|
2519
|
+
* In development mode, `Bun.serve()` returns rendered error messages with
|
|
2520
|
+
* stack traces instead of a generic 500 error. This makes debugging easier,
|
|
2521
|
+
* but development mode shouldn't be used in production or you will risk
|
|
2522
|
+
* leaking sensitive information.
|
|
2523
|
+
*/
|
|
2524
|
+
readonly development: boolean;
|
|
2525
|
+
|
|
2526
|
+
/**
|
|
2527
|
+
* An identifier of the server instance
|
|
2528
|
+
*
|
|
2529
|
+
* When bun is started with the `--hot` flag, this ID is used to hot reload the server without interrupting pending requests or websockets.
|
|
2530
|
+
*
|
|
2531
|
+
* When bun is not started with the `--hot` flag, this ID is currently unused.
|
|
2532
|
+
*/
|
|
2533
|
+
readonly id: string;
|
|
2534
|
+
}
|
|
2535
|
+
|
|
2536
|
+
/**
|
|
2537
|
+
* [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
|
|
2538
|
+
*
|
|
2539
|
+
* This Blob is lazy. That means it won't do any work until you read from it.
|
|
2540
|
+
*
|
|
2541
|
+
* - `size` will not be valid until the contents of the file are read at least once.
|
|
2542
|
+
* - `type` is auto-set based on the file extension when possible
|
|
2543
|
+
*
|
|
2544
|
+
* @example
|
|
2545
|
+
* ```js
|
|
2546
|
+
* const file = Bun.file("./hello.json");
|
|
2547
|
+
* console.log(file.type); // "application/json"
|
|
2548
|
+
* console.log(await file.json()); // { hello: "world" }
|
|
2549
|
+
* ```
|
|
2550
|
+
*
|
|
2551
|
+
* @example
|
|
2552
|
+
* ```js
|
|
2553
|
+
* await Bun.write(
|
|
2554
|
+
* Bun.file("./hello.txt"),
|
|
2555
|
+
* "Hello, world!"
|
|
2556
|
+
* );
|
|
2557
|
+
* ```
|
|
2558
|
+
* @param path The path to the file (lazily loaded)
|
|
2559
|
+
*/
|
|
2560
|
+
// tslint:disable-next-line:unified-signatures
|
|
2561
|
+
function file(path: string | URL, options?: BlobPropertyBag): BunFile;
|
|
2562
|
+
|
|
2563
|
+
/**
|
|
2564
|
+
* `Blob` that leverages the fastest system calls available to operate on files.
|
|
2565
|
+
*
|
|
2566
|
+
* This Blob is lazy. It won't do any work until you read from it. Errors propagate as promise rejections.
|
|
2567
|
+
*
|
|
2568
|
+
* `Blob.size` will not be valid until the contents of the file are read at least once.
|
|
2569
|
+
* `Blob.type` will have a default set based on the file extension
|
|
2570
|
+
*
|
|
2571
|
+
* @example
|
|
2572
|
+
* ```js
|
|
2573
|
+
* const file = Bun.file(new TextEncoder.encode("./hello.json"));
|
|
2574
|
+
* console.log(file.type); // "application/json"
|
|
2575
|
+
* ```
|
|
2576
|
+
*
|
|
2577
|
+
* @param path The path to the file as a byte buffer (the buffer is copied)
|
|
2578
|
+
*/
|
|
2579
|
+
// tslint:disable-next-line:unified-signatures
|
|
2580
|
+
function file(path: ArrayBufferLike | Uint8Array, options?: BlobPropertyBag): BunFile;
|
|
2581
|
+
|
|
2582
|
+
/**
|
|
2583
|
+
* [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
|
|
2584
|
+
*
|
|
2585
|
+
* This Blob is lazy. That means it won't do any work until you read from it.
|
|
2586
|
+
*
|
|
2587
|
+
* - `size` will not be valid until the contents of the file are read at least once.
|
|
2588
|
+
*
|
|
2589
|
+
* @example
|
|
2590
|
+
* ```js
|
|
2591
|
+
* const file = Bun.file(fd);
|
|
2592
|
+
* ```
|
|
2593
|
+
*
|
|
2594
|
+
* @param fileDescriptor The file descriptor of the file
|
|
2595
|
+
*/
|
|
2596
|
+
// tslint:disable-next-line:unified-signatures
|
|
2597
|
+
function file(fileDescriptor: number, options?: BlobPropertyBag): BunFile;
|
|
2598
|
+
|
|
2599
|
+
/**
|
|
2600
|
+
* Allocate a new [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) without zeroing the bytes.
|
|
2601
|
+
*
|
|
2602
|
+
* This can be 3.5x faster than `new Uint8Array(size)`, but if you send uninitialized memory to your users (even unintentionally), it can potentially leak anything recently in memory.
|
|
2603
|
+
*/
|
|
2604
|
+
function allocUnsafe(size: number): Uint8Array;
|
|
2605
|
+
|
|
2606
|
+
interface BunInspectOptions {
|
|
2607
|
+
colors?: boolean;
|
|
2608
|
+
depth?: number;
|
|
2609
|
+
sorted?: boolean;
|
|
2610
|
+
}
|
|
2611
|
+
|
|
2612
|
+
/**
|
|
2613
|
+
* Pretty-print an object the same as {@link console.log} to a `string`
|
|
2614
|
+
*
|
|
2615
|
+
* Supports JSX
|
|
2616
|
+
*
|
|
2617
|
+
* @param args
|
|
2618
|
+
*/
|
|
2619
|
+
function inspect(arg: any, options?: BunInspectOptions): string;
|
|
2620
|
+
namespace inspect {
|
|
2621
|
+
/**
|
|
2622
|
+
* That can be used to declare custom inspect functions.
|
|
2623
|
+
*/
|
|
2624
|
+
const custom: typeof import("util").inspect.custom;
|
|
2625
|
+
}
|
|
2626
|
+
|
|
2627
|
+
interface MMapOptions {
|
|
2628
|
+
/**
|
|
2629
|
+
* Sets MAP_SYNC flag on Linux. Ignored on macOS due to lack of support.
|
|
2630
|
+
*/
|
|
2631
|
+
sync?: boolean;
|
|
2632
|
+
/**
|
|
2633
|
+
* Allow other processes to see results instantly?
|
|
2634
|
+
* This enables MAP_SHARED. If false, it enables MAP_PRIVATE.
|
|
2635
|
+
* @default true
|
|
2636
|
+
*/
|
|
2637
|
+
shared?: boolean;
|
|
2638
|
+
}
|
|
2639
|
+
/**
|
|
2640
|
+
* Open a file as a live-updating `Uint8Array` without copying memory
|
|
2641
|
+
* - Writing to the array writes to the file.
|
|
2642
|
+
* - Reading from the array reads from the file.
|
|
2643
|
+
*
|
|
2644
|
+
* This uses the [`mmap()`](https://man7.org/linux/man-pages/man2/mmap.2.html) syscall under the hood.
|
|
2645
|
+
*
|
|
2646
|
+
* ---
|
|
2647
|
+
*
|
|
2648
|
+
* This API inherently has some rough edges:
|
|
2649
|
+
* - It does not support empty files. It will throw a `SystemError` with `EINVAL`
|
|
2650
|
+
* - Usage on shared/networked filesystems is discouraged. It will be very slow.
|
|
2651
|
+
* - If you delete or truncate the file, that will crash bun. This is called a segmentation fault.
|
|
2652
|
+
*
|
|
2653
|
+
* ---
|
|
2654
|
+
*
|
|
2655
|
+
* To close the file, set the array to `null` and it will be garbage collected eventually.
|
|
2656
|
+
*/
|
|
2657
|
+
function mmap(path: Bun.PathLike, opts?: MMapOptions): Uint8Array;
|
|
2658
|
+
|
|
2659
|
+
/** Write to stdout */
|
|
2660
|
+
const stdout: BunFile;
|
|
2661
|
+
/** Write to stderr */
|
|
2662
|
+
const stderr: BunFile;
|
|
2663
|
+
/**
|
|
2664
|
+
* Read from stdin
|
|
2665
|
+
*
|
|
2666
|
+
* This is read-only
|
|
2667
|
+
*/
|
|
2668
|
+
const stdin: BunFile;
|
|
2669
|
+
|
|
2670
|
+
type StringLike = string | { toString(): string };
|
|
2671
|
+
|
|
2672
|
+
interface Semver {
|
|
2673
|
+
/**
|
|
2674
|
+
* Test if the version satisfies the range. Stringifies both arguments. Returns `true` or `false`.
|
|
2675
|
+
*/
|
|
2676
|
+
satisfies(version: StringLike, range: StringLike): boolean;
|
|
2677
|
+
|
|
2678
|
+
/**
|
|
2679
|
+
* Returns 0 if the versions are equal, 1 if `v1` is greater, or -1 if `v2` is greater.
|
|
2680
|
+
* Throws an error if either version is invalid.
|
|
2681
|
+
*/
|
|
2682
|
+
order(v1: StringLike, v2: StringLike): -1 | 0 | 1;
|
|
2683
|
+
}
|
|
2684
|
+
var semver: Semver;
|
|
2685
|
+
|
|
2686
|
+
interface Unsafe {
|
|
2687
|
+
/**
|
|
2688
|
+
* Cast bytes to a `String` without copying. This is the fastest way to get a `String` from a `Uint8Array` or `ArrayBuffer`.
|
|
2689
|
+
*
|
|
2690
|
+
* **Only use this for ASCII strings**. If there are non-ascii characters, your application may crash and/or very confusing bugs will happen such as `"foo" !== "foo"`.
|
|
2691
|
+
*
|
|
2692
|
+
* **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.
|
|
2693
|
+
*/
|
|
2694
|
+
arrayBufferToString(buffer: Uint8Array | ArrayBufferLike): string;
|
|
2695
|
+
|
|
2696
|
+
/**
|
|
2697
|
+
* Cast bytes to a `String` without copying. This is the fastest way to get a `String` from a `Uint16Array`
|
|
2698
|
+
*
|
|
2699
|
+
* **The input must be a UTF-16 encoded string**. This API does no validation whatsoever.
|
|
2700
|
+
*
|
|
2701
|
+
* **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.
|
|
2702
|
+
*/
|
|
2703
|
+
// tslint:disable-next-line:unified-signatures
|
|
2704
|
+
arrayBufferToString(buffer: Uint16Array): string;
|
|
2705
|
+
|
|
2706
|
+
/** Mock bun's segfault handler. You probably don't want to use this */
|
|
2707
|
+
segfault(): void;
|
|
2708
|
+
|
|
2709
|
+
/**
|
|
2710
|
+
* Force the garbage collector to run extremely often,
|
|
2711
|
+
* especially inside `bun:test`.
|
|
2712
|
+
*
|
|
2713
|
+
* - `0`: default, disable
|
|
2714
|
+
* - `1`: asynchronously call the garbage collector more often
|
|
2715
|
+
* - `2`: synchronously call the garbage collector more often.
|
|
2716
|
+
*
|
|
2717
|
+
* This is a global setting. It's useful for debugging seemingly random crashes.
|
|
2718
|
+
*
|
|
2719
|
+
* `BUN_GARBAGE_COLLECTOR_LEVEL` environment variable is also supported.
|
|
2720
|
+
*
|
|
2721
|
+
* @param level
|
|
2722
|
+
* @returns The previous level
|
|
2723
|
+
*/
|
|
2724
|
+
gcAggressionLevel(level?: 0 | 1 | 2): 0 | 1 | 2;
|
|
2725
|
+
}
|
|
2726
|
+
const unsafe: Unsafe;
|
|
2727
|
+
|
|
2728
|
+
type DigestEncoding = "hex" | "base64";
|
|
2729
|
+
|
|
2730
|
+
/**
|
|
2731
|
+
* Are ANSI colors enabled for stdin and stdout?
|
|
2732
|
+
*
|
|
2733
|
+
* Used for {@link console.log}
|
|
2734
|
+
*/
|
|
2735
|
+
const enableANSIColors: boolean;
|
|
2736
|
+
|
|
2737
|
+
/**
|
|
2738
|
+
* What script launched bun?
|
|
2739
|
+
*
|
|
2740
|
+
* Absolute file path
|
|
2741
|
+
*
|
|
2742
|
+
* @example "/never-gonna-give-you-up.js"
|
|
2743
|
+
*/
|
|
2744
|
+
const main: string;
|
|
2745
|
+
|
|
2746
|
+
/**
|
|
2747
|
+
* Manually trigger the garbage collector
|
|
2748
|
+
*
|
|
2749
|
+
* This does two things:
|
|
2750
|
+
* 1. It tells JavaScriptCore to run the garbage collector
|
|
2751
|
+
* 2. It tells [mimalloc](https://github.com/microsoft/mimalloc) to clean up fragmented memory. Mimalloc manages the heap not used in JavaScriptCore.
|
|
2752
|
+
*
|
|
2753
|
+
* @param force Synchronously run the garbage collector
|
|
2754
|
+
*/
|
|
2755
|
+
function gc(force: boolean): void;
|
|
2756
|
+
|
|
2757
|
+
/**
|
|
2758
|
+
* JavaScriptCore engine's internal heap snapshot
|
|
2759
|
+
*
|
|
2760
|
+
* I don't know how to make this something Chrome or Safari can read.
|
|
2761
|
+
*
|
|
2762
|
+
* If you have any ideas, please file an issue https://github.com/oven-sh/bun
|
|
2763
|
+
*/
|
|
2764
|
+
interface HeapSnapshot {
|
|
2765
|
+
/** 2 */
|
|
2766
|
+
version: number;
|
|
2767
|
+
|
|
2768
|
+
/** "Inspector" */
|
|
2769
|
+
type: string;
|
|
2770
|
+
|
|
2771
|
+
nodes: number[];
|
|
2772
|
+
|
|
2773
|
+
nodeClassNames: string[];
|
|
2774
|
+
edges: number[];
|
|
2775
|
+
edgeTypes: string[];
|
|
2776
|
+
edgeNames: string[];
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
/**
|
|
2780
|
+
* Nanoseconds since Bun.js was started as an integer.
|
|
2781
|
+
*
|
|
2782
|
+
* This uses a high-resolution monotonic system timer.
|
|
2783
|
+
*
|
|
2784
|
+
* After 14 weeks of consecutive uptime, this function
|
|
2785
|
+
* wraps
|
|
2786
|
+
*/
|
|
2787
|
+
function nanoseconds(): number;
|
|
2788
|
+
|
|
2789
|
+
/**
|
|
2790
|
+
* Generate a heap snapshot for seeing where the heap is being used
|
|
2791
|
+
*/
|
|
2792
|
+
function generateHeapSnapshot(): HeapSnapshot;
|
|
2793
|
+
|
|
2794
|
+
/**
|
|
2795
|
+
* The next time JavaScriptCore is idle, clear unused memory and attempt to reduce the heap size.
|
|
2796
|
+
*/
|
|
2797
|
+
function shrink(): void;
|
|
2798
|
+
|
|
2799
|
+
/**
|
|
2800
|
+
* Open a file in your local editor. Auto-detects via `$VISUAL` || `$EDITOR`
|
|
2801
|
+
*
|
|
2802
|
+
* @param path path to open
|
|
2803
|
+
*/
|
|
2804
|
+
function openInEditor(path: string, options?: EditorOptions): void;
|
|
2805
|
+
|
|
2806
|
+
interface EditorOptions {
|
|
2807
|
+
editor?: "vscode" | "subl";
|
|
2808
|
+
line?: number;
|
|
2809
|
+
column?: number;
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
/**
|
|
2813
|
+
* This class only exists in types
|
|
2814
|
+
*/
|
|
2815
|
+
abstract class CryptoHashInterface<T> {
|
|
2816
|
+
/**
|
|
2817
|
+
* Update the hash with data
|
|
2818
|
+
*
|
|
2819
|
+
* @param data
|
|
2820
|
+
*/
|
|
2821
|
+
update(data: Bun.BlobOrStringOrBuffer): T;
|
|
2822
|
+
|
|
2823
|
+
/**
|
|
2824
|
+
* Finalize the hash
|
|
2825
|
+
*
|
|
2826
|
+
* @param encoding `DigestEncoding` to return the hash in. If none is provided, it will return a `Uint8Array`.
|
|
2827
|
+
*/
|
|
2828
|
+
digest(encoding: DigestEncoding): string;
|
|
2829
|
+
|
|
2830
|
+
/**
|
|
2831
|
+
* Finalize the hash
|
|
2832
|
+
*
|
|
2833
|
+
* @param hashInto `TypedArray` to write the hash into. Faster than creating a new one each time
|
|
2834
|
+
*/
|
|
2835
|
+
digest(hashInto?: NodeJS.TypedArray): NodeJS.TypedArray;
|
|
2836
|
+
|
|
2837
|
+
/**
|
|
2838
|
+
* Run the hash over the given data
|
|
2839
|
+
*
|
|
2840
|
+
* @param input `string`, `Uint8Array`, or `ArrayBuffer` to hash. `Uint8Array` or `ArrayBuffer` is faster.
|
|
2841
|
+
*
|
|
2842
|
+
* @param hashInto `TypedArray` to write the hash into. Faster than creating a new one each time
|
|
2843
|
+
*/
|
|
2844
|
+
static hash(input: Bun.BlobOrStringOrBuffer, hashInto?: NodeJS.TypedArray): NodeJS.TypedArray;
|
|
2845
|
+
|
|
2846
|
+
/**
|
|
2847
|
+
* Run the hash over the given data
|
|
2848
|
+
*
|
|
2849
|
+
* @param input `string`, `Uint8Array`, or `ArrayBuffer` to hash. `Uint8Array` or `ArrayBuffer` is faster.
|
|
2850
|
+
*
|
|
2851
|
+
* @param encoding `DigestEncoding` to return the hash in
|
|
2852
|
+
*/
|
|
2853
|
+
static hash(input: Bun.BlobOrStringOrBuffer, encoding: DigestEncoding): string;
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
type SupportedCryptoAlgorithms =
|
|
2857
|
+
| "blake2b256"
|
|
2858
|
+
| "md4"
|
|
2859
|
+
| "md5"
|
|
2860
|
+
| "ripemd160"
|
|
2861
|
+
| "sha1"
|
|
2862
|
+
| "sha224"
|
|
2863
|
+
| "sha256"
|
|
2864
|
+
| "sha384"
|
|
2865
|
+
| "sha512"
|
|
2866
|
+
| "sha512-256";
|
|
2867
|
+
/**
|
|
2868
|
+
* Hardware-accelerated cryptographic hash functions
|
|
2869
|
+
*
|
|
2870
|
+
* Used for `crypto.createHash()`
|
|
2871
|
+
*/
|
|
2872
|
+
class CryptoHasher {
|
|
2873
|
+
/**
|
|
2874
|
+
* The algorithm chosen to hash the data
|
|
2875
|
+
*/
|
|
2876
|
+
readonly algorithm: SupportedCryptoAlgorithms;
|
|
2877
|
+
|
|
2878
|
+
/**
|
|
2879
|
+
* The length of the output hash in bytes
|
|
2880
|
+
*/
|
|
2881
|
+
readonly byteLength: number;
|
|
2882
|
+
|
|
2883
|
+
/**
|
|
2884
|
+
* Create a new hasher
|
|
2885
|
+
*
|
|
2886
|
+
* @param algorithm The algorithm to use. See {@link algorithms} for a list of supported algorithms
|
|
2887
|
+
*/
|
|
2888
|
+
constructor(algorithm: SupportedCryptoAlgorithms);
|
|
2889
|
+
|
|
2890
|
+
/**
|
|
2891
|
+
* Update the hash with data
|
|
2892
|
+
*
|
|
2893
|
+
* @param input
|
|
2894
|
+
*/
|
|
2895
|
+
update(input: Bun.BlobOrStringOrBuffer, inputEncoding?: CryptoEncoding): CryptoHasher;
|
|
2896
|
+
|
|
2897
|
+
/**
|
|
2898
|
+
* Perform a deep copy of the hasher
|
|
2899
|
+
*/
|
|
2900
|
+
copy(): CryptoHasher;
|
|
2901
|
+
|
|
2902
|
+
/**
|
|
2903
|
+
* Finalize the hash. Resets the CryptoHasher so it can be reused.
|
|
2904
|
+
*
|
|
2905
|
+
* @param encoding `DigestEncoding` to return the hash in. If none is provided, it will return a `Uint8Array`.
|
|
2906
|
+
*/
|
|
2907
|
+
digest(encoding: DigestEncoding): string;
|
|
2908
|
+
|
|
2909
|
+
/**
|
|
2910
|
+
* Finalize the hash
|
|
2911
|
+
*
|
|
2912
|
+
* @param hashInto `TypedArray` to write the hash into. Faster than creating a new one each time
|
|
2913
|
+
*/
|
|
2914
|
+
digest(hashInto?: NodeJS.TypedArray): NodeJS.TypedArray;
|
|
2915
|
+
|
|
2916
|
+
/**
|
|
2917
|
+
* Run the hash over the given data
|
|
2918
|
+
*
|
|
2919
|
+
* @param input `string`, `Uint8Array`, or `ArrayBuffer` to hash. `Uint8Array` or `ArrayBuffer` is faster.
|
|
2920
|
+
*
|
|
2921
|
+
* @param hashInto `TypedArray` to write the hash into. Faster than creating a new one each time
|
|
2922
|
+
*/
|
|
2923
|
+
static hash(
|
|
2924
|
+
algorithm: SupportedCryptoAlgorithms,
|
|
2925
|
+
input: Bun.BlobOrStringOrBuffer,
|
|
2926
|
+
hashInto?: NodeJS.TypedArray,
|
|
2927
|
+
): NodeJS.TypedArray;
|
|
2928
|
+
|
|
2929
|
+
/**
|
|
2930
|
+
* Run the hash over the given data
|
|
2931
|
+
*
|
|
2932
|
+
* @param input `string`, `Uint8Array`, or `ArrayBuffer` to hash. `Uint8Array` or `ArrayBuffer` is faster.
|
|
2933
|
+
*
|
|
2934
|
+
* @param encoding `DigestEncoding` to return the hash in
|
|
2935
|
+
*/
|
|
2936
|
+
static hash(
|
|
2937
|
+
algorithm: SupportedCryptoAlgorithms,
|
|
2938
|
+
input: Bun.BlobOrStringOrBuffer,
|
|
2939
|
+
encoding: DigestEncoding,
|
|
2940
|
+
): string;
|
|
2941
|
+
|
|
2942
|
+
/**
|
|
2943
|
+
* List of supported hash algorithms
|
|
2944
|
+
*
|
|
2945
|
+
* These are hardware accelerated with BoringSSL
|
|
2946
|
+
*/
|
|
2947
|
+
static readonly algorithms: SupportedCryptoAlgorithms[];
|
|
2948
|
+
}
|
|
2949
|
+
|
|
2950
|
+
/**
|
|
2951
|
+
* Resolve a `Promise` after milliseconds. This is like
|
|
2952
|
+
* {@link setTimeout} except it returns a `Promise`.
|
|
2953
|
+
*
|
|
2954
|
+
* @param ms milliseconds to delay resolving the promise. This is a minimum
|
|
2955
|
+
* number. It may take longer. If a {@link Date} is passed, it will sleep until the
|
|
2956
|
+
* {@link Date} is reached.
|
|
2957
|
+
*
|
|
2958
|
+
* @example
|
|
2959
|
+
* ## Sleep for 1 second
|
|
2960
|
+
* ```ts
|
|
2961
|
+
* import { sleep } from "bun";
|
|
2962
|
+
*
|
|
2963
|
+
* await sleep(1000);
|
|
2964
|
+
* ```
|
|
2965
|
+
* ## Sleep for 10 milliseconds
|
|
2966
|
+
* ```ts
|
|
2967
|
+
* await Bun.sleep(10);
|
|
2968
|
+
* ```
|
|
2969
|
+
* ## Sleep until `Date`
|
|
2970
|
+
*
|
|
2971
|
+
* ```ts
|
|
2972
|
+
* const target = new Date();
|
|
2973
|
+
* target.setSeconds(target.getSeconds() + 1);
|
|
2974
|
+
* await Bun.sleep(target);
|
|
2975
|
+
* ```
|
|
2976
|
+
* Internally, `Bun.sleep` is the equivalent of
|
|
2977
|
+
* ```ts
|
|
2978
|
+
* await new Promise((resolve) => setTimeout(resolve, ms));
|
|
2979
|
+
* ```
|
|
2980
|
+
* As always, you can use `Bun.sleep` or the imported `sleep` function interchangeably.
|
|
2981
|
+
*/
|
|
2982
|
+
function sleep(ms: number | Date): Promise<void>;
|
|
2983
|
+
|
|
2984
|
+
/**
|
|
2985
|
+
* Sleep the thread for a given number of milliseconds
|
|
2986
|
+
*
|
|
2987
|
+
* This is a blocking function.
|
|
2988
|
+
*
|
|
2989
|
+
* Internally, it calls [nanosleep(2)](https://man7.org/linux/man-pages/man2/nanosleep.2.html)
|
|
2990
|
+
*/
|
|
2991
|
+
function sleepSync(ms: number): void;
|
|
2992
|
+
|
|
2993
|
+
/**
|
|
2994
|
+
* Hash `input` using [SHA-2 512/256](https://en.wikipedia.org/wiki/SHA-2#Comparison_of_SHA_functions)
|
|
2995
|
+
*
|
|
2996
|
+
* @param input `string`, `Uint8Array`, or `ArrayBuffer` to hash. `Uint8Array` or `ArrayBuffer` will be faster
|
|
2997
|
+
* @param hashInto optional `Uint8Array` to write the hash to. 32 bytes minimum.
|
|
2998
|
+
*
|
|
2999
|
+
* This hashing function balances speed with cryptographic strength. This does not encrypt or decrypt data.
|
|
3000
|
+
*
|
|
3001
|
+
* The implementation uses [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go)
|
|
3002
|
+
*
|
|
3003
|
+
* The equivalent `openssl` command is:
|
|
3004
|
+
*
|
|
3005
|
+
* ```bash
|
|
3006
|
+
* # You will need OpenSSL 3 or later
|
|
3007
|
+
* openssl sha512-256 /path/to/file
|
|
3008
|
+
* ```
|
|
3009
|
+
*/
|
|
3010
|
+
function sha(input: Bun.StringOrBuffer, hashInto?: NodeJS.TypedArray): NodeJS.TypedArray;
|
|
3011
|
+
|
|
3012
|
+
/**
|
|
3013
|
+
* Hash `input` using [SHA-2 512/256](https://en.wikipedia.org/wiki/SHA-2#Comparison_of_SHA_functions)
|
|
3014
|
+
*
|
|
3015
|
+
* @param input `string`, `Uint8Array`, or `ArrayBuffer` to hash. `Uint8Array` or `ArrayBuffer` will be faster
|
|
3016
|
+
* @param encoding `DigestEncoding` to return the hash in
|
|
3017
|
+
*
|
|
3018
|
+
* This hashing function balances speed with cryptographic strength. This does not encrypt or decrypt data.
|
|
3019
|
+
*
|
|
3020
|
+
* The implementation uses [BoringSSL](https://boringssl.googlesource.com/boringssl) (used in Chromium & Go)
|
|
3021
|
+
*
|
|
3022
|
+
* The equivalent `openssl` command is:
|
|
3023
|
+
*
|
|
3024
|
+
* ```bash
|
|
3025
|
+
* # You will need OpenSSL 3 or later
|
|
3026
|
+
* openssl sha512-256 /path/to/file
|
|
3027
|
+
* ```
|
|
3028
|
+
*/
|
|
3029
|
+
function sha(input: Bun.StringOrBuffer, encoding: DigestEncoding): string;
|
|
3030
|
+
|
|
3031
|
+
/**
|
|
3032
|
+
* This is not the default because it's not cryptographically secure and it's slower than {@link SHA512}
|
|
3033
|
+
*
|
|
3034
|
+
* Consider using the ugly-named {@link SHA512_256} instead
|
|
3035
|
+
*/
|
|
3036
|
+
class SHA1 extends CryptoHashInterface<SHA1> {
|
|
3037
|
+
constructor();
|
|
3038
|
+
|
|
3039
|
+
/**
|
|
3040
|
+
* The number of bytes the hash will produce
|
|
3041
|
+
*/
|
|
3042
|
+
static readonly byteLength: 20;
|
|
3043
|
+
}
|
|
3044
|
+
class MD5 extends CryptoHashInterface<MD5> {
|
|
3045
|
+
constructor();
|
|
3046
|
+
|
|
3047
|
+
/**
|
|
3048
|
+
* The number of bytes the hash will produce
|
|
3049
|
+
*/
|
|
3050
|
+
static readonly byteLength: 16;
|
|
3051
|
+
}
|
|
3052
|
+
class MD4 extends CryptoHashInterface<MD4> {
|
|
3053
|
+
constructor();
|
|
3054
|
+
|
|
3055
|
+
/**
|
|
3056
|
+
* The number of bytes the hash will produce
|
|
3057
|
+
*/
|
|
3058
|
+
static readonly byteLength: 16;
|
|
3059
|
+
}
|
|
3060
|
+
class SHA224 extends CryptoHashInterface<SHA224> {
|
|
3061
|
+
constructor();
|
|
3062
|
+
|
|
3063
|
+
/**
|
|
3064
|
+
* The number of bytes the hash will produce
|
|
3065
|
+
*/
|
|
3066
|
+
static readonly byteLength: 28;
|
|
3067
|
+
}
|
|
3068
|
+
class SHA512 extends CryptoHashInterface<SHA512> {
|
|
3069
|
+
constructor();
|
|
3070
|
+
|
|
3071
|
+
/**
|
|
3072
|
+
* The number of bytes the hash will produce
|
|
3073
|
+
*/
|
|
3074
|
+
static readonly byteLength: 64;
|
|
3075
|
+
}
|
|
3076
|
+
class SHA384 extends CryptoHashInterface<SHA384> {
|
|
3077
|
+
constructor();
|
|
3078
|
+
|
|
3079
|
+
/**
|
|
3080
|
+
* The number of bytes the hash will produce
|
|
3081
|
+
*/
|
|
3082
|
+
static readonly byteLength: 48;
|
|
3083
|
+
}
|
|
3084
|
+
class SHA256 extends CryptoHashInterface<SHA256> {
|
|
3085
|
+
constructor();
|
|
3086
|
+
|
|
3087
|
+
/**
|
|
3088
|
+
* The number of bytes the hash will produce
|
|
3089
|
+
*/
|
|
3090
|
+
static readonly byteLength: 32;
|
|
3091
|
+
}
|
|
3092
|
+
/**
|
|
3093
|
+
* See also {@link sha}
|
|
3094
|
+
*/
|
|
3095
|
+
class SHA512_256 extends CryptoHashInterface<SHA512_256> {
|
|
3096
|
+
constructor();
|
|
3097
|
+
|
|
3098
|
+
/**
|
|
3099
|
+
* The number of bytes the hash will produce
|
|
3100
|
+
*/
|
|
3101
|
+
static readonly byteLength: 32;
|
|
3102
|
+
}
|
|
3103
|
+
|
|
3104
|
+
/** Compression options for `Bun.deflateSync` and `Bun.gzipSync` */
|
|
3105
|
+
interface ZlibCompressionOptions {
|
|
3106
|
+
/**
|
|
3107
|
+
* The compression level to use. Must be between `-1` and `9`.
|
|
3108
|
+
* - A value of `-1` uses the default compression level (Currently `6`)
|
|
3109
|
+
* - A value of `0` gives no compression
|
|
3110
|
+
* - A value of `1` gives least compression, fastest speed
|
|
3111
|
+
* - A value of `9` gives best compression, slowest speed
|
|
3112
|
+
*/
|
|
3113
|
+
level?: -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
3114
|
+
/**
|
|
3115
|
+
* How much memory should be allocated for the internal compression state.
|
|
3116
|
+
*
|
|
3117
|
+
* A value of `1` uses minimum memory but is slow and reduces compression ratio.
|
|
3118
|
+
*
|
|
3119
|
+
* A value of `9` uses maximum memory for optimal speed. The default is `8`.
|
|
3120
|
+
*/
|
|
3121
|
+
memLevel?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
3122
|
+
/**
|
|
3123
|
+
* The base 2 logarithm of the window size (the size of the history buffer).
|
|
3124
|
+
*
|
|
3125
|
+
* Larger values of this parameter result in better compression at the expense of memory usage.
|
|
3126
|
+
*
|
|
3127
|
+
* The following value ranges are supported:
|
|
3128
|
+
* - `9..15`: The output will have a zlib header and footer (Deflate)
|
|
3129
|
+
* - `-9..-15`: The output will **not** have a zlib header or footer (Raw Deflate)
|
|
3130
|
+
* - `25..31` (16+`9..15`): The output will have a gzip header and footer (gzip)
|
|
3131
|
+
*
|
|
3132
|
+
* The gzip header will have no file name, no extra data, no comment, no modification time (set to zero) and no header CRC.
|
|
3133
|
+
*/
|
|
3134
|
+
windowBits?:
|
|
3135
|
+
| -9
|
|
3136
|
+
| -10
|
|
3137
|
+
| -11
|
|
3138
|
+
| -12
|
|
3139
|
+
| -13
|
|
3140
|
+
| -14
|
|
3141
|
+
| -15
|
|
3142
|
+
| 9
|
|
3143
|
+
| 10
|
|
3144
|
+
| 11
|
|
3145
|
+
| 12
|
|
3146
|
+
| 13
|
|
3147
|
+
| 14
|
|
3148
|
+
| 15
|
|
3149
|
+
| 25
|
|
3150
|
+
| 26
|
|
3151
|
+
| 27
|
|
3152
|
+
| 28
|
|
3153
|
+
| 29
|
|
3154
|
+
| 30
|
|
3155
|
+
| 31;
|
|
3156
|
+
/**
|
|
3157
|
+
* Tunes the compression algorithm.
|
|
3158
|
+
*
|
|
3159
|
+
* - `Z_DEFAULT_STRATEGY`: For normal data **(Default)**
|
|
3160
|
+
* - `Z_FILTERED`: For data produced by a filter or predictor
|
|
3161
|
+
* - `Z_HUFFMAN_ONLY`: Force Huffman encoding only (no string match)
|
|
3162
|
+
* - `Z_RLE`: Limit match distances to one (run-length encoding)
|
|
3163
|
+
* - `Z_FIXED` prevents the use of dynamic Huffman codes
|
|
3164
|
+
*
|
|
3165
|
+
* `Z_RLE` is designed to be almost as fast as `Z_HUFFMAN_ONLY`, but give better compression for PNG image data.
|
|
3166
|
+
*
|
|
3167
|
+
* `Z_FILTERED` forces more Huffman coding and less string matching, it is
|
|
3168
|
+
* somewhat intermediate between `Z_DEFAULT_STRATEGY` and `Z_HUFFMAN_ONLY`.
|
|
3169
|
+
* Filtered data consists mostly of small values with a somewhat random distribution.
|
|
3170
|
+
*/
|
|
3171
|
+
strategy?: number;
|
|
3172
|
+
}
|
|
3173
|
+
|
|
3174
|
+
/**
|
|
3175
|
+
* Compresses a chunk of data with `zlib` DEFLATE algorithm.
|
|
3176
|
+
* @param data The buffer of data to compress
|
|
3177
|
+
* @param options Compression options to use
|
|
3178
|
+
* @returns The output buffer with the compressed data
|
|
3179
|
+
*/
|
|
3180
|
+
function deflateSync(data: Uint8Array | string | ArrayBuffer, options?: ZlibCompressionOptions): Uint8Array;
|
|
3181
|
+
/**
|
|
3182
|
+
* Compresses a chunk of data with `zlib` GZIP algorithm.
|
|
3183
|
+
* @param data The buffer of data to compress
|
|
3184
|
+
* @param options Compression options to use
|
|
3185
|
+
* @returns The output buffer with the compressed data
|
|
3186
|
+
*/
|
|
3187
|
+
function gzipSync(data: Uint8Array | string | ArrayBuffer, options?: ZlibCompressionOptions): Uint8Array;
|
|
3188
|
+
/**
|
|
3189
|
+
* Decompresses a chunk of data with `zlib` INFLATE algorithm.
|
|
3190
|
+
* @param data The buffer of data to decompress
|
|
3191
|
+
* @returns The output buffer with the decompressed data
|
|
3192
|
+
*/
|
|
3193
|
+
function inflateSync(data: Uint8Array | string | ArrayBuffer): Uint8Array;
|
|
3194
|
+
/**
|
|
3195
|
+
* Decompresses a chunk of data with `zlib` GUNZIP algorithm.
|
|
3196
|
+
* @param data The buffer of data to decompress
|
|
3197
|
+
* @returns The output buffer with the decompressed data
|
|
3198
|
+
*/
|
|
3199
|
+
function gunzipSync(data: Uint8Array | string | ArrayBuffer): Uint8Array;
|
|
3200
|
+
|
|
3201
|
+
type Target =
|
|
3202
|
+
/**
|
|
3203
|
+
* For generating bundles that are intended to be run by the Bun runtime. In many cases,
|
|
3204
|
+
* it isn't necessary to bundle server-side code; you can directly execute the source code
|
|
3205
|
+
* without modification. However, bundling your server code can reduce startup times and
|
|
3206
|
+
* improve running performance.
|
|
3207
|
+
*
|
|
3208
|
+
* All bundles generated with `target: "bun"` are marked with a special `// @bun` pragma, which
|
|
3209
|
+
* indicates to the Bun runtime that there's no need to re-transpile the file before execution.
|
|
3210
|
+
*/
|
|
3211
|
+
| "bun"
|
|
3212
|
+
/**
|
|
3213
|
+
* The plugin will be applied to Node.js builds
|
|
3214
|
+
*/
|
|
3215
|
+
| "node"
|
|
3216
|
+
/**
|
|
3217
|
+
* The plugin will be applied to browser builds
|
|
3218
|
+
*/
|
|
3219
|
+
| "browser";
|
|
3220
|
+
|
|
3221
|
+
/** https://bun.sh/docs/bundler/loaders */
|
|
3222
|
+
type Loader = "js" | "jsx" | "ts" | "tsx" | "json" | "toml" | "file" | "napi" | "wasm" | "text";
|
|
3223
|
+
|
|
3224
|
+
interface PluginConstraints {
|
|
3225
|
+
/**
|
|
3226
|
+
* Only apply the plugin when the import specifier matches this regular expression
|
|
3227
|
+
*
|
|
3228
|
+
* @example
|
|
3229
|
+
* ```ts
|
|
3230
|
+
* // Only apply the plugin when the import specifier matches the regex
|
|
3231
|
+
* Bun.plugin({
|
|
3232
|
+
* setup(builder) {
|
|
3233
|
+
* builder.onLoad({ filter: /node_modules\/underscore/ }, (args) => {
|
|
3234
|
+
* return { contents: "throw new Error('Please use lodash instead of underscore.')" };
|
|
3235
|
+
* });
|
|
3236
|
+
* }
|
|
3237
|
+
* })
|
|
3238
|
+
* ```
|
|
3239
|
+
*/
|
|
3240
|
+
filter: RegExp;
|
|
3241
|
+
|
|
3242
|
+
/**
|
|
3243
|
+
* Only apply the plugin when the import specifier has a namespace matching
|
|
3244
|
+
* this string
|
|
3245
|
+
*
|
|
3246
|
+
* Namespaces are prefixes in import specifiers. For example, `"bun:ffi"`
|
|
3247
|
+
* has the namespace `"bun"`.
|
|
3248
|
+
*
|
|
3249
|
+
* The default namespace is `"file"` and it can be omitted from import
|
|
3250
|
+
* specifiers.
|
|
3251
|
+
*/
|
|
3252
|
+
namespace?: string;
|
|
3253
|
+
}
|
|
3254
|
+
|
|
3255
|
+
interface OnLoadResultSourceCode {
|
|
3256
|
+
/**
|
|
3257
|
+
* The source code of the module
|
|
3258
|
+
*/
|
|
3259
|
+
contents: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer;
|
|
3260
|
+
/**
|
|
3261
|
+
* The loader to use for this file
|
|
3262
|
+
*
|
|
3263
|
+
* "css" will be added in a future version of Bun.
|
|
3264
|
+
*/
|
|
3265
|
+
loader?: Loader;
|
|
3266
|
+
}
|
|
3267
|
+
|
|
3268
|
+
interface OnLoadResultObject {
|
|
3269
|
+
/**
|
|
3270
|
+
* The object to use as the module
|
|
3271
|
+
* @example
|
|
3272
|
+
* ```ts
|
|
3273
|
+
* // In your loader
|
|
3274
|
+
* builder.onLoad({ filter: /^hello:world$/ }, (args) => {
|
|
3275
|
+
* return { exports: { foo: "bar" }, loader: "object" };
|
|
3276
|
+
* });
|
|
3277
|
+
*
|
|
3278
|
+
* // In your script
|
|
3279
|
+
* import {foo} from "hello:world";
|
|
3280
|
+
* console.log(foo); // "bar"
|
|
3281
|
+
* ```
|
|
3282
|
+
*/
|
|
3283
|
+
exports: Record<string, unknown>;
|
|
3284
|
+
/**
|
|
3285
|
+
* The loader to use for this file
|
|
3286
|
+
*/
|
|
3287
|
+
loader: "object";
|
|
3288
|
+
}
|
|
3289
|
+
|
|
3290
|
+
interface OnLoadArgs {
|
|
3291
|
+
/**
|
|
3292
|
+
* The resolved import specifier of the module being loaded
|
|
3293
|
+
* @example
|
|
3294
|
+
* ```ts
|
|
3295
|
+
* builder.onLoad({ filter: /^hello:world$/ }, (args) => {
|
|
3296
|
+
* console.log(args.path); // "hello:world"
|
|
3297
|
+
* return { exports: { foo: "bar" }, loader: "object" };
|
|
3298
|
+
* });
|
|
3299
|
+
* ```
|
|
3300
|
+
*/
|
|
3301
|
+
path: string;
|
|
3302
|
+
/**
|
|
3303
|
+
* The namespace of the module being loaded
|
|
3304
|
+
*/
|
|
3305
|
+
namespace: string;
|
|
3306
|
+
/**
|
|
3307
|
+
* The default loader for this file extension
|
|
3308
|
+
*/
|
|
3309
|
+
loader: Loader;
|
|
3310
|
+
}
|
|
3311
|
+
|
|
3312
|
+
type OnLoadResult = OnLoadResultSourceCode | OnLoadResultObject | undefined;
|
|
3313
|
+
type OnLoadCallback = (args: OnLoadArgs) => OnLoadResult | Promise<OnLoadResult>;
|
|
3314
|
+
|
|
3315
|
+
interface OnResolveArgs {
|
|
3316
|
+
/**
|
|
3317
|
+
* The import specifier of the module being loaded
|
|
3318
|
+
*/
|
|
3319
|
+
path: string;
|
|
3320
|
+
/**
|
|
3321
|
+
* The module that imported the module being resolved
|
|
3322
|
+
*/
|
|
3323
|
+
importer: string;
|
|
3324
|
+
/**
|
|
3325
|
+
* The namespace of the importer.
|
|
3326
|
+
*/
|
|
3327
|
+
namespace: string;
|
|
3328
|
+
/**
|
|
3329
|
+
* The kind of import this resolve is for.
|
|
3330
|
+
*/
|
|
3331
|
+
kind: ImportKind;
|
|
3332
|
+
// resolveDir: string;
|
|
3333
|
+
// pluginData: any;
|
|
3334
|
+
}
|
|
3335
|
+
|
|
3336
|
+
interface OnResolveResult {
|
|
3337
|
+
/**
|
|
3338
|
+
* The destination of the import
|
|
3339
|
+
*/
|
|
3340
|
+
path: string;
|
|
3341
|
+
/**
|
|
3342
|
+
* The namespace of the destination
|
|
3343
|
+
* It will be concatenated with `path` to form the final import specifier
|
|
3344
|
+
* @example
|
|
3345
|
+
* ```ts
|
|
3346
|
+
* "foo" // "foo:bar"
|
|
3347
|
+
* ```
|
|
3348
|
+
*/
|
|
3349
|
+
namespace?: string;
|
|
3350
|
+
external?: boolean;
|
|
3351
|
+
}
|
|
3352
|
+
|
|
3353
|
+
type OnResolveCallback = (
|
|
3354
|
+
args: OnResolveArgs,
|
|
3355
|
+
) => OnResolveResult | Promise<OnResolveResult | undefined | null> | undefined | null;
|
|
3356
|
+
|
|
3357
|
+
interface PluginBuilder {
|
|
3358
|
+
/**
|
|
3359
|
+
* Register a callback to load imports with a specific import specifier
|
|
3360
|
+
* @param constraints The constraints to apply the plugin to
|
|
3361
|
+
* @param callback The callback to handle the import
|
|
3362
|
+
* @example
|
|
3363
|
+
* ```ts
|
|
3364
|
+
* Bun.plugin({
|
|
3365
|
+
* setup(builder) {
|
|
3366
|
+
* builder.onLoad({ filter: /^hello:world$/ }, (args) => {
|
|
3367
|
+
* return { exports: { foo: "bar" }, loader: "object" };
|
|
3368
|
+
* });
|
|
3369
|
+
* },
|
|
3370
|
+
* });
|
|
3371
|
+
* ```
|
|
3372
|
+
*/
|
|
3373
|
+
onLoad(constraints: PluginConstraints, callback: OnLoadCallback): void;
|
|
3374
|
+
/**
|
|
3375
|
+
* Register a callback to resolve imports matching a filter and/or namespace
|
|
3376
|
+
* @param constraints The constraints to apply the plugin to
|
|
3377
|
+
* @param callback The callback to handle the import
|
|
3378
|
+
* @example
|
|
3379
|
+
* ```ts
|
|
3380
|
+
* Bun.plugin({
|
|
3381
|
+
* setup(builder) {
|
|
3382
|
+
* builder.onResolve({ filter: /^wat$/ }, (args) => {
|
|
3383
|
+
* return { path: "/tmp/woah.js" };
|
|
3384
|
+
* });
|
|
3385
|
+
* },
|
|
3386
|
+
* });
|
|
3387
|
+
* ```
|
|
3388
|
+
*/
|
|
3389
|
+
onResolve(constraints: PluginConstraints, callback: OnResolveCallback): void;
|
|
3390
|
+
/**
|
|
3391
|
+
* The config object passed to `Bun.build` as is. Can be mutated.
|
|
3392
|
+
*/
|
|
3393
|
+
config: BuildConfig & { plugins: BunPlugin[] };
|
|
3394
|
+
|
|
3395
|
+
/**
|
|
3396
|
+
* Create a lazy-loaded virtual module that can be `import`ed or `require`d from other modules
|
|
3397
|
+
*
|
|
3398
|
+
* @param specifier The module specifier to register the callback for
|
|
3399
|
+
* @param callback The function to run when the module is imported or required
|
|
3400
|
+
*
|
|
3401
|
+
* ### Example
|
|
3402
|
+
* @example
|
|
3403
|
+
* ```ts
|
|
3404
|
+
* Bun.plugin({
|
|
3405
|
+
* setup(builder) {
|
|
3406
|
+
* builder.module("hello:world", () => {
|
|
3407
|
+
* return { exports: { foo: "bar" }, loader: "object" };
|
|
3408
|
+
* });
|
|
3409
|
+
* },
|
|
3410
|
+
* });
|
|
3411
|
+
*
|
|
3412
|
+
* // sometime later
|
|
3413
|
+
* const { foo } = await import("hello:world");
|
|
3414
|
+
* console.log(foo); // "bar"
|
|
3415
|
+
*
|
|
3416
|
+
* // or
|
|
3417
|
+
* const { foo } = require("hello:world");
|
|
3418
|
+
* console.log(foo); // "bar"
|
|
3419
|
+
* ```
|
|
3420
|
+
*/
|
|
3421
|
+
module(specifier: string, callback: () => OnLoadResult | Promise<OnLoadResult>): void;
|
|
3422
|
+
}
|
|
3423
|
+
|
|
3424
|
+
interface BunPlugin {
|
|
3425
|
+
/**
|
|
3426
|
+
* Human-readable name of the plugin
|
|
3427
|
+
*
|
|
3428
|
+
* In a future version of Bun, this will be used in error messages.
|
|
3429
|
+
*/
|
|
3430
|
+
name?: string;
|
|
3431
|
+
|
|
3432
|
+
/**
|
|
3433
|
+
* The target JavaScript environment the plugin should be applied to.
|
|
3434
|
+
* - `bun`: The default environment when using `bun run` or `bun` to load a script
|
|
3435
|
+
* - `browser`: The plugin will be applied to browser builds
|
|
3436
|
+
* - `node`: The plugin will be applied to Node.js builds
|
|
3437
|
+
*
|
|
3438
|
+
* If in Bun's runtime, the default target is `bun`.
|
|
3439
|
+
*
|
|
3440
|
+
* If unspecified, it is assumed that the plugin is compatible with the default target.
|
|
3441
|
+
*/
|
|
3442
|
+
target?: Target;
|
|
3443
|
+
/**
|
|
3444
|
+
* A function that will be called when the plugin is loaded.
|
|
3445
|
+
*
|
|
3446
|
+
* This function may be called in the same tick that it is registered, or it may be called later. It could potentially be called multiple times for different targets.
|
|
3447
|
+
*/
|
|
3448
|
+
setup(
|
|
3449
|
+
/**
|
|
3450
|
+
* A builder object that can be used to register plugin hooks
|
|
3451
|
+
* @example
|
|
3452
|
+
* ```ts
|
|
3453
|
+
* builder.onLoad({ filter: /\.yaml$/ }, ({ path }) => ({
|
|
3454
|
+
* loader: "object",
|
|
3455
|
+
* exports: require("js-yaml").load(fs.readFileSync(path, "utf8")),
|
|
3456
|
+
* }));
|
|
3457
|
+
* ```
|
|
3458
|
+
*/
|
|
3459
|
+
build: PluginBuilder,
|
|
3460
|
+
): void | Promise<void>;
|
|
3461
|
+
}
|
|
3462
|
+
|
|
3463
|
+
/**
|
|
3464
|
+
* Extend Bun's module resolution and loading behavior
|
|
3465
|
+
*
|
|
3466
|
+
* Plugins are applied in the order they are defined.
|
|
3467
|
+
*
|
|
3468
|
+
* Today, there are two kinds of hooks:
|
|
3469
|
+
* - `onLoad` lets you return source code or an object that will become the module's exports
|
|
3470
|
+
* - `onResolve` lets you redirect a module specifier to another module specifier. It does not chain.
|
|
3471
|
+
*
|
|
3472
|
+
* Plugin hooks must define a `filter` RegExp and will only be matched if the
|
|
3473
|
+
* import specifier contains a "." or a ":".
|
|
3474
|
+
*
|
|
3475
|
+
* ES Module resolution semantics mean that plugins may be initialized _after_
|
|
3476
|
+
* a module is resolved. You might need to load plugins at the very beginning
|
|
3477
|
+
* of the application and then use a dynamic import to load the rest of the
|
|
3478
|
+
* application. A future version of Bun may also support specifying plugins
|
|
3479
|
+
* via `bunfig.toml`.
|
|
3480
|
+
*
|
|
3481
|
+
* @example
|
|
3482
|
+
* A YAML loader plugin
|
|
3483
|
+
*
|
|
3484
|
+
* ```js
|
|
3485
|
+
* Bun.plugin({
|
|
3486
|
+
* setup(builder) {
|
|
3487
|
+
* builder.onLoad({ filter: /\.yaml$/ }, ({path}) => ({
|
|
3488
|
+
* loader: "object",
|
|
3489
|
+
* exports: require("js-yaml").load(fs.readFileSync(path, "utf8"))
|
|
3490
|
+
* }));
|
|
3491
|
+
* });
|
|
3492
|
+
*
|
|
3493
|
+
* // You can use require()
|
|
3494
|
+
* const {foo} = require("./file.yaml");
|
|
3495
|
+
*
|
|
3496
|
+
* // Or import
|
|
3497
|
+
* await import("./file.yaml");
|
|
3498
|
+
*
|
|
3499
|
+
* ```
|
|
3500
|
+
*/
|
|
3501
|
+
interface BunRegisterPlugin {
|
|
3502
|
+
<T extends BunPlugin>(options: T): ReturnType<T["setup"]>;
|
|
3503
|
+
|
|
3504
|
+
/**
|
|
3505
|
+
* Deactivate all plugins
|
|
3506
|
+
*
|
|
3507
|
+
* This prevents registered plugins from being applied to future builds.
|
|
3508
|
+
*/
|
|
3509
|
+
clearAll(): void;
|
|
3510
|
+
}
|
|
3511
|
+
|
|
3512
|
+
const plugin: BunRegisterPlugin;
|
|
3513
|
+
|
|
3514
|
+
/**
|
|
3515
|
+
* Is the current global scope the main thread?
|
|
3516
|
+
*/
|
|
3517
|
+
const isMainThread: boolean;
|
|
3518
|
+
|
|
3519
|
+
interface Socket<Data = undefined> {
|
|
3520
|
+
/**
|
|
3521
|
+
* Write `data` to the socket
|
|
3522
|
+
*
|
|
3523
|
+
* @param data The data to write to the socket
|
|
3524
|
+
* @param byteOffset The offset in the buffer to start writing from (defaults to 0)
|
|
3525
|
+
* @param byteLength The number of bytes to write (defaults to the length of the buffer)
|
|
3526
|
+
*
|
|
3527
|
+
* When passed a string, `byteOffset` and `byteLength` refer to the UTF-8 offset, not the string character offset.
|
|
3528
|
+
*
|
|
3529
|
+
* This is unbuffered as of Bun v0.2.2. That means individual write() calls
|
|
3530
|
+
* will be slow. In the future, Bun will buffer writes and flush them at the
|
|
3531
|
+
* end of the tick, when the event loop is idle, or sooner if the buffer is full.
|
|
3532
|
+
*/
|
|
3533
|
+
write(data: string | Bun.BufferSource, byteOffset?: number, byteLength?: number): number;
|
|
3534
|
+
|
|
3535
|
+
/**
|
|
3536
|
+
* The data context for the socket.
|
|
3537
|
+
*/
|
|
3538
|
+
data: Data;
|
|
3539
|
+
|
|
3540
|
+
/**
|
|
3541
|
+
* Like {@link Socket.write} except it includes a TCP FIN packet
|
|
3542
|
+
*
|
|
3543
|
+
* Use it to send your last message and close the connection.
|
|
3544
|
+
*/
|
|
3545
|
+
end(data?: string | Bun.BufferSource, byteOffset?: number, byteLength?: number): number;
|
|
3546
|
+
|
|
3547
|
+
/**
|
|
3548
|
+
* Close the socket immediately
|
|
3549
|
+
*/
|
|
3550
|
+
end(): void;
|
|
3551
|
+
|
|
3552
|
+
/**
|
|
3553
|
+
* Keep Bun's process alive at least until this socket is closed
|
|
3554
|
+
*
|
|
3555
|
+
* After the socket has closed, the socket is unref'd, the process may exit,
|
|
3556
|
+
* and this becomes a no-op
|
|
3557
|
+
*/
|
|
3558
|
+
ref(): void;
|
|
3559
|
+
|
|
3560
|
+
/**
|
|
3561
|
+
* Set a timeout until the socket automatically closes.
|
|
3562
|
+
*
|
|
3563
|
+
* To reset the timeout, call this function again.
|
|
3564
|
+
*
|
|
3565
|
+
* When a timeout happens, the `timeout` callback is called and the socket is closed.
|
|
3566
|
+
*/
|
|
3567
|
+
timeout(seconds: number): void;
|
|
3568
|
+
|
|
3569
|
+
/**
|
|
3570
|
+
* Shutdown writes to a socket
|
|
3571
|
+
*
|
|
3572
|
+
* This makes the socket a half-closed socket. It can still receive data.
|
|
3573
|
+
*
|
|
3574
|
+
* This calls [shutdown(2)](https://man7.org/linux/man-pages/man2/shutdown.2.html) internally
|
|
3575
|
+
*/
|
|
3576
|
+
shutdown(halfClose?: boolean): void;
|
|
3577
|
+
|
|
3578
|
+
readonly readyState: "open" | "closing" | "closed";
|
|
3579
|
+
|
|
3580
|
+
/**
|
|
3581
|
+
* Allow Bun's process to exit even if this socket is still open
|
|
3582
|
+
*
|
|
3583
|
+
* After the socket has closed, this function does nothing.
|
|
3584
|
+
*/
|
|
3585
|
+
unref(): void;
|
|
3586
|
+
|
|
3587
|
+
/**
|
|
3588
|
+
* Flush any buffered data to the socket
|
|
3589
|
+
*/
|
|
3590
|
+
flush(): void;
|
|
3591
|
+
|
|
3592
|
+
/**
|
|
3593
|
+
* Reset the socket's callbacks. This is useful with `bun --hot` to facilitate hot reloading.
|
|
3594
|
+
*
|
|
3595
|
+
* This will apply to all sockets from the same {@link Listener}. it is per socket only for {@link Bun.connect}.
|
|
3596
|
+
*/
|
|
3597
|
+
reload(handler: SocketHandler): void;
|
|
3598
|
+
|
|
3599
|
+
/**
|
|
3600
|
+
* Get the server that created this socket
|
|
3601
|
+
*
|
|
3602
|
+
* This will return undefined if the socket was created by {@link Bun.connect} or if the listener has already closed.
|
|
3603
|
+
*/
|
|
3604
|
+
readonly listener?: SocketListener;
|
|
3605
|
+
|
|
3606
|
+
/**
|
|
3607
|
+
* Remote IP address connected to the socket
|
|
3608
|
+
*/
|
|
3609
|
+
readonly remoteAddress: string;
|
|
3610
|
+
|
|
3611
|
+
/**
|
|
3612
|
+
* local port connected to the socket
|
|
3613
|
+
*/
|
|
3614
|
+
readonly localPort: number;
|
|
3615
|
+
}
|
|
3616
|
+
|
|
3617
|
+
interface SocketListener<Data = undefined> {
|
|
3618
|
+
stop(closeActiveConnections?: boolean): void;
|
|
3619
|
+
ref(): void;
|
|
3620
|
+
unref(): void;
|
|
3621
|
+
reload(options: Pick<Partial<SocketOptions>, "socket">): void;
|
|
3622
|
+
data: Data;
|
|
3623
|
+
}
|
|
3624
|
+
interface TCPSocketListener<Data = unknown> extends SocketListener<Data> {
|
|
3625
|
+
readonly port: number;
|
|
3626
|
+
readonly hostname: string;
|
|
3627
|
+
}
|
|
3628
|
+
interface UnixSocketListener<Data> extends SocketListener<Data> {
|
|
3629
|
+
readonly unix: string;
|
|
3630
|
+
}
|
|
3631
|
+
|
|
3632
|
+
interface TCPSocket extends Socket {}
|
|
3633
|
+
interface TLSSocket extends Socket {}
|
|
3634
|
+
|
|
3635
|
+
interface BinaryTypeList {
|
|
3636
|
+
arraybuffer: ArrayBuffer;
|
|
3637
|
+
buffer: Buffer;
|
|
3638
|
+
uint8array: Uint8Array;
|
|
3639
|
+
// TODO: DataView
|
|
3640
|
+
// dataview: DataView;
|
|
3641
|
+
}
|
|
3642
|
+
type BinaryType = keyof BinaryTypeList;
|
|
3643
|
+
|
|
3644
|
+
interface SocketHandler<Data = unknown, DataBinaryType extends BinaryType = "buffer"> {
|
|
3645
|
+
/**
|
|
3646
|
+
* Is called when the socket connects, or in case of TLS if no handshake is provided
|
|
3647
|
+
* this will be called only after handshake
|
|
3648
|
+
* @param socket
|
|
3649
|
+
*/
|
|
3650
|
+
open?(socket: Socket<Data>): void | Promise<void>;
|
|
3651
|
+
close?(socket: Socket<Data>): void | Promise<void>;
|
|
3652
|
+
error?(socket: Socket<Data>, error: Error): void | Promise<void>;
|
|
3653
|
+
data?(socket: Socket<Data>, data: BinaryTypeList[DataBinaryType]): void | Promise<void>;
|
|
3654
|
+
drain?(socket: Socket<Data>): void | Promise<void>;
|
|
3655
|
+
|
|
3656
|
+
/**
|
|
3657
|
+
* When handshake is completed, this functions is called.
|
|
3658
|
+
* @param socket
|
|
3659
|
+
* @param success Indicates if the server authorized despite the authorizationError.
|
|
3660
|
+
* @param authorizationError Certificate Authorization Error or null.
|
|
3661
|
+
*/
|
|
3662
|
+
handshake?(socket: Socket<Data>, success: boolean, authorizationError: Error | null): void;
|
|
3663
|
+
|
|
3664
|
+
/**
|
|
3665
|
+
* When the socket has been shutdown from the other end, this function is
|
|
3666
|
+
* called. This is a TCP FIN packet.
|
|
3667
|
+
*/
|
|
3668
|
+
end?(socket: Socket<Data>): void | Promise<void>;
|
|
3669
|
+
|
|
3670
|
+
/**
|
|
3671
|
+
* When the socket fails to be created, this function is called.
|
|
3672
|
+
*
|
|
3673
|
+
* The promise returned by `Bun.connect` rejects **after** this function is
|
|
3674
|
+
* called.
|
|
3675
|
+
*
|
|
3676
|
+
* When `connectError` is specified, the rejected promise will not be
|
|
3677
|
+
* added to the promise rejection queue (so it won't be reported as an
|
|
3678
|
+
* unhandled promise rejection, since connectError handles it).
|
|
3679
|
+
*
|
|
3680
|
+
* When `connectError` is not specified, the rejected promise will be added
|
|
3681
|
+
* to the promise rejection queue.
|
|
3682
|
+
*/
|
|
3683
|
+
connectError?(socket: Socket<Data>, error: Error): void | Promise<void>;
|
|
3684
|
+
|
|
3685
|
+
/**
|
|
3686
|
+
* Called when a message times out.
|
|
3687
|
+
*/
|
|
3688
|
+
timeout?(socket: Socket<Data>): void | Promise<void>;
|
|
3689
|
+
/**
|
|
3690
|
+
* Choose what `ArrayBufferView` is returned in the {@link SocketHandler.data} callback.
|
|
3691
|
+
*
|
|
3692
|
+
* @default "buffer"
|
|
3693
|
+
*
|
|
3694
|
+
* @remarks
|
|
3695
|
+
* This lets you select the desired binary type for the `data` callback.
|
|
3696
|
+
* It's a small performance optimization to let you avoid creating extra
|
|
3697
|
+
* ArrayBufferView objects when possible.
|
|
3698
|
+
*
|
|
3699
|
+
* Bun originally defaulted to `Uint8Array` but when dealing with network
|
|
3700
|
+
* data, it's more useful to be able to directly read from the bytes which
|
|
3701
|
+
* `Buffer` allows.
|
|
3702
|
+
*/
|
|
3703
|
+
binaryType?: BinaryType;
|
|
3704
|
+
}
|
|
3705
|
+
|
|
3706
|
+
interface SocketOptions<Data = unknown> {
|
|
3707
|
+
socket: SocketHandler<Data>;
|
|
3708
|
+
data?: Data;
|
|
3709
|
+
}
|
|
3710
|
+
// interface TCPSocketOptions<Data = undefined> extends SocketOptions<Data> {
|
|
3711
|
+
// hostname: string;
|
|
3712
|
+
// port: number;
|
|
3713
|
+
// }
|
|
3714
|
+
|
|
3715
|
+
interface TCPSocketListenOptions<Data = undefined> extends SocketOptions<Data> {
|
|
3716
|
+
hostname: string;
|
|
3717
|
+
port: number;
|
|
3718
|
+
tls?: TLSOptions;
|
|
3719
|
+
}
|
|
3720
|
+
|
|
3721
|
+
interface TCPSocketConnectOptions<Data = undefined> extends SocketOptions<Data> {
|
|
3722
|
+
hostname: string;
|
|
3723
|
+
port: number;
|
|
3724
|
+
tls?: boolean;
|
|
3725
|
+
}
|
|
3726
|
+
|
|
3727
|
+
interface UnixSocketOptions<Data = undefined> extends SocketOptions<Data> {
|
|
3728
|
+
unix: string;
|
|
3729
|
+
}
|
|
3730
|
+
|
|
3731
|
+
/**
|
|
3732
|
+
* Create a TCP client that connects to a server
|
|
3733
|
+
*
|
|
3734
|
+
* @param options The options to use when creating the client
|
|
3735
|
+
* @param options.socket The socket handler to use
|
|
3736
|
+
* @param options.data The per-instance data context
|
|
3737
|
+
* @param options.hostname The hostname to connect to
|
|
3738
|
+
* @param options.port The port to connect to
|
|
3739
|
+
* @param options.tls The TLS configuration object
|
|
3740
|
+
* @param options.unix The unix socket to connect to
|
|
3741
|
+
*/
|
|
3742
|
+
function connect<Data = undefined>(options: TCPSocketConnectOptions<Data>): Promise<Socket<Data>>;
|
|
3743
|
+
function connect<Data = undefined>(options: UnixSocketOptions<Data>): Promise<Socket<Data>>;
|
|
3744
|
+
|
|
3745
|
+
/**
|
|
3746
|
+
* Create a TCP server that listens on a port
|
|
3747
|
+
*
|
|
3748
|
+
* @param options The options to use when creating the server
|
|
3749
|
+
* @param options.socket The socket handler to use
|
|
3750
|
+
* @param options.data The per-instance data context
|
|
3751
|
+
* @param options.hostname The hostname to connect to
|
|
3752
|
+
* @param options.port The port to connect to
|
|
3753
|
+
* @param options.tls The TLS configuration object
|
|
3754
|
+
* @param options.unix The unix socket to connect to
|
|
3755
|
+
*/
|
|
3756
|
+
function listen<Data = undefined>(options: TCPSocketListenOptions<Data>): TCPSocketListener<Data>;
|
|
3757
|
+
function listen<Data = undefined>(options: UnixSocketOptions<Data>): UnixSocketListener<Data>;
|
|
3758
|
+
|
|
3759
|
+
namespace SpawnOptions {
|
|
3760
|
+
/**
|
|
3761
|
+
* Option for stdout/stderr
|
|
3762
|
+
*/
|
|
3763
|
+
type Readable =
|
|
3764
|
+
| "pipe"
|
|
3765
|
+
| "inherit"
|
|
3766
|
+
| "ignore"
|
|
3767
|
+
| null // equivalent to "ignore"
|
|
3768
|
+
| undefined // to use default
|
|
3769
|
+
| BunFile
|
|
3770
|
+
| ArrayBufferView
|
|
3771
|
+
| number;
|
|
3772
|
+
|
|
3773
|
+
/**
|
|
3774
|
+
* Option for stdin
|
|
3775
|
+
*/
|
|
3776
|
+
type Writable =
|
|
3777
|
+
| "pipe"
|
|
3778
|
+
| "inherit"
|
|
3779
|
+
| "ignore"
|
|
3780
|
+
| null // equivalent to "ignore"
|
|
3781
|
+
| undefined // to use default
|
|
3782
|
+
| BunFile
|
|
3783
|
+
| ArrayBufferView
|
|
3784
|
+
| number
|
|
3785
|
+
| ReadableStream
|
|
3786
|
+
| Blob
|
|
3787
|
+
| Response
|
|
3788
|
+
| Request;
|
|
3789
|
+
|
|
3790
|
+
interface OptionsObject<
|
|
3791
|
+
In extends Writable = Writable,
|
|
3792
|
+
Out extends Readable = Readable,
|
|
3793
|
+
Err extends Readable = Readable,
|
|
3794
|
+
> {
|
|
3795
|
+
/**
|
|
3796
|
+
* The current working directory of the process
|
|
3797
|
+
*
|
|
3798
|
+
* Defaults to `process.cwd()`
|
|
3799
|
+
*/
|
|
3800
|
+
cwd?: string;
|
|
3801
|
+
|
|
3802
|
+
/**
|
|
3803
|
+
* The environment variables of the process
|
|
3804
|
+
*
|
|
3805
|
+
* Defaults to `process.env` as it was when the current Bun process launched.
|
|
3806
|
+
*
|
|
3807
|
+
* Changes to `process.env` at runtime won't automatically be reflected in the default value. For that, you can pass `process.env` explicitly.
|
|
3808
|
+
*/
|
|
3809
|
+
env?: Record<string, string | undefined>;
|
|
3810
|
+
|
|
3811
|
+
/**
|
|
3812
|
+
* The standard file descriptors of the process, in the form [stdin, stdout, stderr].
|
|
3813
|
+
* This overrides the `stdin`, `stdout`, and `stderr` properties.
|
|
3814
|
+
*
|
|
3815
|
+
* For stdin you may pass:
|
|
3816
|
+
*
|
|
3817
|
+
* - `"ignore"`, `null`, `undefined`: The process will have no standard input (default)
|
|
3818
|
+
* - `"pipe"`: The process will have a new {@link FileSink} for standard input
|
|
3819
|
+
* - `"inherit"`: The process will inherit the standard input of the current process
|
|
3820
|
+
* - `ArrayBufferView`, `Blob`, `Bun.file()`, `Response`, `Request`: The process will read from buffer/stream.
|
|
3821
|
+
* - `number`: The process will read from the file descriptor
|
|
3822
|
+
*
|
|
3823
|
+
* For stdout and stdin you may pass:
|
|
3824
|
+
*
|
|
3825
|
+
* - `"pipe"`, `undefined`: The process will have a {@link ReadableStream} for standard output/error
|
|
3826
|
+
* - `"ignore"`, `null`: The process will have no standard output/error
|
|
3827
|
+
* - `"inherit"`: The process will inherit the standard output/error of the current process
|
|
3828
|
+
* - `ArrayBufferView`: The process write to the preallocated buffer. Not implemented.
|
|
3829
|
+
* - `number`: The process will write to the file descriptor
|
|
3830
|
+
*
|
|
3831
|
+
* @default ["ignore", "pipe", "inherit"] for `spawn`
|
|
3832
|
+
* ["ignore", "pipe", "pipe"] for `spawnSync`
|
|
3833
|
+
*/
|
|
3834
|
+
stdio?: [In, Out, Err];
|
|
3835
|
+
/**
|
|
3836
|
+
* The file descriptor for the standard input. It may be:
|
|
3837
|
+
*
|
|
3838
|
+
* - `"ignore"`, `null`, `undefined`: The process will have no standard input
|
|
3839
|
+
* - `"pipe"`: The process will have a new {@link FileSink} for standard input
|
|
3840
|
+
* - `"inherit"`: The process will inherit the standard input of the current process
|
|
3841
|
+
* - `ArrayBufferView`, `Blob`: The process will read from the buffer
|
|
3842
|
+
* - `number`: The process will read from the file descriptor
|
|
3843
|
+
*
|
|
3844
|
+
* @default "ignore"
|
|
3845
|
+
*/
|
|
3846
|
+
stdin?: In;
|
|
3847
|
+
/**
|
|
3848
|
+
* The file descriptor for the standard output. It may be:
|
|
3849
|
+
*
|
|
3850
|
+
* - `"pipe"`, `undefined`: The process will have a {@link ReadableStream} for standard output/error
|
|
3851
|
+
* - `"ignore"`, `null`: The process will have no standard output/error
|
|
3852
|
+
* - `"inherit"`: The process will inherit the standard output/error of the current process
|
|
3853
|
+
* - `ArrayBufferView`: The process write to the preallocated buffer. Not implemented.
|
|
3854
|
+
* - `number`: The process will write to the file descriptor
|
|
3855
|
+
*
|
|
3856
|
+
* @default "pipe"
|
|
3857
|
+
*/
|
|
3858
|
+
stdout?: Out;
|
|
3859
|
+
/**
|
|
3860
|
+
* The file descriptor for the standard error. It may be:
|
|
3861
|
+
*
|
|
3862
|
+
* - `"pipe"`, `undefined`: The process will have a {@link ReadableStream} for standard output/error
|
|
3863
|
+
* - `"ignore"`, `null`: The process will have no standard output/error
|
|
3864
|
+
* - `"inherit"`: The process will inherit the standard output/error of the current process
|
|
3865
|
+
* - `ArrayBufferView`: The process write to the preallocated buffer. Not implemented.
|
|
3866
|
+
* - `number`: The process will write to the file descriptor
|
|
3867
|
+
*
|
|
3868
|
+
* @default "inherit" for `spawn`
|
|
3869
|
+
* "pipe" for `spawnSync`
|
|
3870
|
+
*/
|
|
3871
|
+
stderr?: Err;
|
|
3872
|
+
|
|
3873
|
+
/**
|
|
3874
|
+
* Callback that runs when the {@link Subprocess} exits
|
|
3875
|
+
*
|
|
3876
|
+
* This is called even if the process exits with a non-zero exit code.
|
|
3877
|
+
*
|
|
3878
|
+
* Warning: this may run before the `Bun.spawn` function returns.
|
|
3879
|
+
*
|
|
3880
|
+
* A simple alternative is `await subprocess.exited`.
|
|
3881
|
+
*
|
|
3882
|
+
* @example
|
|
3883
|
+
*
|
|
3884
|
+
* ```ts
|
|
3885
|
+
* const subprocess = spawn({
|
|
3886
|
+
* cmd: ["echo", "hello"],
|
|
3887
|
+
* onExit: (subprocess, code) => {
|
|
3888
|
+
* console.log(`Process exited with code ${code}`);
|
|
3889
|
+
* },
|
|
3890
|
+
* });
|
|
3891
|
+
* ```
|
|
3892
|
+
*/
|
|
3893
|
+
onExit?(
|
|
3894
|
+
subprocess: Subprocess<In, Out, Err>,
|
|
3895
|
+
exitCode: number | null,
|
|
3896
|
+
signalCode: number | null,
|
|
3897
|
+
/**
|
|
3898
|
+
* If an error occurred in the call to waitpid2, this will be the error.
|
|
3899
|
+
*/
|
|
3900
|
+
error?: ErrorLike,
|
|
3901
|
+
): void | Promise<void>;
|
|
3902
|
+
|
|
3903
|
+
/**
|
|
3904
|
+
* When specified, Bun will open an IPC channel to the subprocess. The passed callback is called for
|
|
3905
|
+
* incoming messages, and `subprocess.send` can send messages to the subprocess. Messages are serialized
|
|
3906
|
+
* using the JSC serialize API, which allows for the same types that `postMessage`/`structuredClone` supports.
|
|
3907
|
+
*
|
|
3908
|
+
* The subprocess can send and recieve messages by using `process.send` and `process.on("message")`,
|
|
3909
|
+
* respectively. This is the same API as what Node.js exposes when `child_process.fork()` is used.
|
|
3910
|
+
*
|
|
3911
|
+
* Currently, this is only compatible with processes that are other `bun` instances.
|
|
3912
|
+
*/
|
|
3913
|
+
ipc?(
|
|
3914
|
+
message: any,
|
|
3915
|
+
/**
|
|
3916
|
+
* The {@link Subprocess} that sent the message
|
|
3917
|
+
*/
|
|
3918
|
+
subprocess: Subprocess<In, Out, Err>,
|
|
3919
|
+
): void;
|
|
3920
|
+
|
|
3921
|
+
/**
|
|
3922
|
+
* If true, the subprocess will have a hidden window.
|
|
3923
|
+
*/
|
|
3924
|
+
// windowsHide?: boolean;
|
|
3925
|
+
}
|
|
3926
|
+
|
|
3927
|
+
type OptionsToSubprocess<Opts extends OptionsObject> = Opts extends OptionsObject<infer In, infer Out, infer Err>
|
|
3928
|
+
? Subprocess<
|
|
3929
|
+
// "Writable extends In" means "if In === Writable",
|
|
3930
|
+
// aka if true that means the user didn't specify anything
|
|
3931
|
+
Writable extends In ? "ignore" : In,
|
|
3932
|
+
Readable extends Out ? "pipe" : Out,
|
|
3933
|
+
Readable extends Err ? "inherit" : Err
|
|
3934
|
+
>
|
|
3935
|
+
: Subprocess<Writable, Readable, Readable>;
|
|
3936
|
+
|
|
3937
|
+
type OptionsToSyncSubprocess<Opts extends OptionsObject> = Opts extends OptionsObject<any, infer Out, infer Err>
|
|
3938
|
+
? SyncSubprocess<Readable extends Out ? "pipe" : Out, Readable extends Err ? "pipe" : Err>
|
|
3939
|
+
: SyncSubprocess<Readable, Readable>;
|
|
3940
|
+
|
|
3941
|
+
type ReadableIO = ReadableStream<Uint8Array> | number | undefined;
|
|
3942
|
+
|
|
3943
|
+
type ReadableToIO<X extends Readable> = X extends "pipe" | undefined
|
|
3944
|
+
? ReadableStream<Uint8Array>
|
|
3945
|
+
: X extends BunFile | ArrayBufferView | number
|
|
3946
|
+
? number
|
|
3947
|
+
: undefined;
|
|
3948
|
+
|
|
3949
|
+
type ReadableToSyncIO<X extends Readable> = X extends "pipe" | undefined ? Buffer : undefined;
|
|
3950
|
+
|
|
3951
|
+
type WritableIO = FileSink | number | undefined;
|
|
3952
|
+
|
|
3953
|
+
type WritableToIO<X extends Writable> = X extends "pipe"
|
|
3954
|
+
? FileSink
|
|
3955
|
+
: X extends BunFile | ArrayBufferView | Blob | Request | Response | number
|
|
3956
|
+
? number
|
|
3957
|
+
: undefined;
|
|
3958
|
+
}
|
|
3959
|
+
|
|
3960
|
+
interface ResourceUsage {
|
|
3961
|
+
/**
|
|
3962
|
+
* The number of voluntary and involuntary context switches that the process made.
|
|
3963
|
+
*/
|
|
3964
|
+
contextSwitches: {
|
|
3965
|
+
/**
|
|
3966
|
+
* Voluntary context switches (context switches that the process initiated).
|
|
3967
|
+
*/
|
|
3968
|
+
voluntary: number;
|
|
3969
|
+
/**
|
|
3970
|
+
* Involuntary context switches (context switches initiated by the system scheduler).
|
|
3971
|
+
*/
|
|
3972
|
+
involuntary: number;
|
|
3973
|
+
};
|
|
3974
|
+
|
|
3975
|
+
/**
|
|
3976
|
+
* The amount of CPU time used by the process, in nanoseconds.
|
|
3977
|
+
*/
|
|
3978
|
+
cpuTime: {
|
|
3979
|
+
/**
|
|
3980
|
+
* User CPU time used by the process, in nanoseconds.
|
|
3981
|
+
*/
|
|
3982
|
+
user: number;
|
|
3983
|
+
/**
|
|
3984
|
+
* System CPU time used by the process, in nanoseconds.
|
|
3985
|
+
*/
|
|
3986
|
+
system: number;
|
|
3987
|
+
/**
|
|
3988
|
+
* Total CPU time used by the process, in nanoseconds.
|
|
3989
|
+
*/
|
|
3990
|
+
total: number;
|
|
3991
|
+
};
|
|
3992
|
+
/**
|
|
3993
|
+
* The maximum amount of resident set size (in bytes) used by the process during its lifetime.
|
|
3994
|
+
*/
|
|
3995
|
+
maxRSS: number;
|
|
3996
|
+
|
|
3997
|
+
/**
|
|
3998
|
+
* IPC messages sent and received by the process.
|
|
3999
|
+
*/
|
|
4000
|
+
messages: {
|
|
4001
|
+
/**
|
|
4002
|
+
* The number of IPC messages sent.
|
|
4003
|
+
*/
|
|
4004
|
+
sent: number;
|
|
4005
|
+
/**
|
|
4006
|
+
* The number of IPC messages received.
|
|
4007
|
+
*/
|
|
4008
|
+
received: number;
|
|
4009
|
+
};
|
|
4010
|
+
/**
|
|
4011
|
+
* The number of IO operations done by the process.
|
|
4012
|
+
*/
|
|
4013
|
+
ops: {
|
|
4014
|
+
/**
|
|
4015
|
+
* The number of input operations via the file system.
|
|
4016
|
+
*/
|
|
4017
|
+
in: number;
|
|
4018
|
+
/**
|
|
4019
|
+
* The number of output operations via the file system.
|
|
4020
|
+
*/
|
|
4021
|
+
out: number;
|
|
4022
|
+
};
|
|
4023
|
+
/**
|
|
4024
|
+
* The amount of shared memory that the process used.
|
|
4025
|
+
*/
|
|
4026
|
+
shmSize: number;
|
|
4027
|
+
/**
|
|
4028
|
+
* The number of signals delivered to the process.
|
|
4029
|
+
*/
|
|
4030
|
+
signalCount: number;
|
|
4031
|
+
/**
|
|
4032
|
+
* The number of times the process was swapped out of main memory.
|
|
4033
|
+
*/
|
|
4034
|
+
swapCount: number;
|
|
4035
|
+
}
|
|
4036
|
+
|
|
4037
|
+
/**
|
|
4038
|
+
* A process created by {@link Bun.spawn}.
|
|
4039
|
+
*
|
|
4040
|
+
* This type accepts 3 optional type parameters which correspond to the `stdio` array from the options object. Instead of specifying these, you should use one of the following utility types instead:
|
|
4041
|
+
* - {@link ReadableSubprocess} (any, pipe, pipe)
|
|
4042
|
+
* - {@link WritableSubprocess} (pipe, any, any)
|
|
4043
|
+
* - {@link PipedSubprocess} (pipe, pipe, pipe)
|
|
4044
|
+
* - {@link NullSubprocess} (ignore, ignore, ignore)
|
|
4045
|
+
*/
|
|
4046
|
+
interface Subprocess<
|
|
4047
|
+
In extends SpawnOptions.Writable = SpawnOptions.Writable,
|
|
4048
|
+
Out extends SpawnOptions.Readable = SpawnOptions.Readable,
|
|
4049
|
+
Err extends SpawnOptions.Readable = SpawnOptions.Readable,
|
|
4050
|
+
> {
|
|
4051
|
+
readonly stdin: SpawnOptions.WritableToIO<In>;
|
|
4052
|
+
readonly stdout: SpawnOptions.ReadableToIO<Out>;
|
|
4053
|
+
readonly stderr: SpawnOptions.ReadableToIO<Err>;
|
|
4054
|
+
|
|
4055
|
+
/**
|
|
4056
|
+
* This returns the same value as {@link Subprocess.stdout}
|
|
4057
|
+
*
|
|
4058
|
+
* It exists for compatibility with {@link ReadableStream.pipeThrough}
|
|
4059
|
+
*/
|
|
4060
|
+
readonly readable: SpawnOptions.ReadableToIO<Out>;
|
|
4061
|
+
|
|
4062
|
+
/**
|
|
4063
|
+
* The process ID of the child process
|
|
4064
|
+
* @example
|
|
4065
|
+
* ```ts
|
|
4066
|
+
* const { pid } = Bun.spawn({ cmd: ["echo", "hello"] });
|
|
4067
|
+
* console.log(pid); // 1234
|
|
4068
|
+
* ```
|
|
4069
|
+
*/
|
|
4070
|
+
readonly pid: number;
|
|
4071
|
+
/**
|
|
4072
|
+
* The exit code of the process
|
|
4073
|
+
*
|
|
4074
|
+
* The promise will resolve when the process exits
|
|
4075
|
+
*/
|
|
4076
|
+
readonly exited: Promise<number>;
|
|
4077
|
+
|
|
4078
|
+
/**
|
|
4079
|
+
* Synchronously get the exit code of the process
|
|
4080
|
+
*
|
|
4081
|
+
* If the process hasn't exited yet, this will return `null`
|
|
4082
|
+
*/
|
|
4083
|
+
readonly exitCode: number | null;
|
|
4084
|
+
|
|
4085
|
+
/**
|
|
4086
|
+
* Synchronously get the signal code of the process
|
|
4087
|
+
*
|
|
4088
|
+
* If the process never sent a signal code, this will return `null`
|
|
4089
|
+
*
|
|
4090
|
+
* To receive signal code changes, use the `onExit` callback.
|
|
4091
|
+
*
|
|
4092
|
+
* If the signal code is unknown, it will return the original signal code
|
|
4093
|
+
* number, but that case should essentially never happen.
|
|
4094
|
+
*/
|
|
4095
|
+
readonly signalCode: NodeJS.Signals | null;
|
|
4096
|
+
|
|
4097
|
+
/**
|
|
4098
|
+
* Has the process exited?
|
|
4099
|
+
*/
|
|
4100
|
+
readonly killed: boolean;
|
|
4101
|
+
|
|
4102
|
+
/**
|
|
4103
|
+
* Kill the process
|
|
4104
|
+
* @param exitCode The exitCode to send to the process
|
|
4105
|
+
*/
|
|
4106
|
+
kill(exitCode?: number): void;
|
|
4107
|
+
|
|
4108
|
+
/**
|
|
4109
|
+
* This method will tell Bun to wait for this process to exit after you already
|
|
4110
|
+
* called `unref()`.
|
|
4111
|
+
*
|
|
4112
|
+
* Before shutting down, Bun will wait for all subprocesses to exit by default
|
|
4113
|
+
*/
|
|
4114
|
+
ref(): void;
|
|
4115
|
+
|
|
4116
|
+
/**
|
|
4117
|
+
* Before shutting down, Bun will wait for all subprocesses to exit by default
|
|
4118
|
+
*
|
|
4119
|
+
* This method will tell Bun to not wait for this process to exit before shutting down.
|
|
4120
|
+
*/
|
|
4121
|
+
unref(): void;
|
|
4122
|
+
|
|
4123
|
+
/**
|
|
4124
|
+
* Send a message to the subprocess. This is only supported if the subprocess
|
|
4125
|
+
* was created with the `ipc` option, and is another instance of `bun`.
|
|
4126
|
+
*
|
|
4127
|
+
* Messages are serialized using the JSC serialize API, which allows for the same types that `postMessage`/`structuredClone` supports.
|
|
4128
|
+
*/
|
|
4129
|
+
send(message: any): void;
|
|
4130
|
+
|
|
4131
|
+
/**
|
|
4132
|
+
* Disconnect the IPC channel to the subprocess. This is only supported if the subprocess
|
|
4133
|
+
* was created with the `ipc` option.
|
|
4134
|
+
*/
|
|
4135
|
+
disconnect(): void;
|
|
4136
|
+
|
|
4137
|
+
/**
|
|
4138
|
+
* Get the resource usage information of the process (max RSS, CPU time, etc)
|
|
4139
|
+
*
|
|
4140
|
+
* Only available after the process has exited
|
|
4141
|
+
*
|
|
4142
|
+
* If the process hasn't exited yet, this will return `undefined`
|
|
4143
|
+
*/
|
|
4144
|
+
resourceUsage(): ResourceUsage | undefined;
|
|
4145
|
+
}
|
|
4146
|
+
|
|
4147
|
+
/**
|
|
4148
|
+
* A process created by {@link Bun.spawnSync}.
|
|
4149
|
+
*
|
|
4150
|
+
* This type accepts 2 optional type parameters which correspond to the `stdout` and `stderr` options. Instead of specifying these, you should use one of the following utility types instead:
|
|
4151
|
+
* - {@link ReadableSyncSubprocess} (pipe, pipe)
|
|
4152
|
+
* - {@link NullSyncSubprocess} (ignore, ignore)
|
|
4153
|
+
*/
|
|
4154
|
+
interface SyncSubprocess<
|
|
4155
|
+
Out extends SpawnOptions.Readable = SpawnOptions.Readable,
|
|
4156
|
+
Err extends SpawnOptions.Readable = SpawnOptions.Readable,
|
|
4157
|
+
> {
|
|
4158
|
+
stdout: SpawnOptions.ReadableToSyncIO<Out>;
|
|
4159
|
+
stderr: SpawnOptions.ReadableToSyncIO<Err>;
|
|
4160
|
+
exitCode: number;
|
|
4161
|
+
success: boolean;
|
|
4162
|
+
/**
|
|
4163
|
+
* Get the resource usage information of the process (max RSS, CPU time, etc)
|
|
4164
|
+
*/
|
|
4165
|
+
resourceUsage: ResourceUsage;
|
|
4166
|
+
}
|
|
4167
|
+
|
|
4168
|
+
/**
|
|
4169
|
+
* Spawn a new process
|
|
4170
|
+
*
|
|
4171
|
+
* ```js
|
|
4172
|
+
* const subprocess = Bun.spawn({
|
|
4173
|
+
* cmd: ["echo", "hello"],
|
|
4174
|
+
* stdout: "pipe",
|
|
4175
|
+
* });
|
|
4176
|
+
* const text = await readableStreamToText(subprocess.stdout);
|
|
4177
|
+
* console.log(text); // "hello\n"
|
|
4178
|
+
* ```
|
|
4179
|
+
*
|
|
4180
|
+
* Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
|
|
4181
|
+
*/
|
|
4182
|
+
function spawn<Opts extends SpawnOptions.OptionsObject>(
|
|
4183
|
+
options: Opts & {
|
|
4184
|
+
/**
|
|
4185
|
+
* The command to run
|
|
4186
|
+
*
|
|
4187
|
+
* The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
|
|
4188
|
+
*
|
|
4189
|
+
* If you explicitly set `PATH` in `env`, that `PATH` will be used to resolve the executable instead of the default `PATH`.
|
|
4190
|
+
*
|
|
4191
|
+
* To check if the command exists before running it, use `Bun.which(bin)`.
|
|
4192
|
+
*
|
|
4193
|
+
* @example
|
|
4194
|
+
* ```ts
|
|
4195
|
+
* const subprocess = Bun.spawn(["echo", "hello"]);
|
|
4196
|
+
* ```
|
|
4197
|
+
*/
|
|
4198
|
+
cmd: string[]; // to support dynamically constructed commands
|
|
4199
|
+
},
|
|
4200
|
+
): SpawnOptions.OptionsToSubprocess<Opts>;
|
|
4201
|
+
|
|
4202
|
+
/**
|
|
4203
|
+
* Spawn a new process
|
|
4204
|
+
*
|
|
4205
|
+
* ```js
|
|
4206
|
+
* const {stdout} = Bun.spawn(["echo", "hello"]);
|
|
4207
|
+
* const text = await readableStreamToText(stdout);
|
|
4208
|
+
* console.log(text); // "hello\n"
|
|
4209
|
+
* ```
|
|
4210
|
+
*
|
|
4211
|
+
* Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
|
|
4212
|
+
*/
|
|
4213
|
+
function spawn<Opts extends SpawnOptions.OptionsObject>(
|
|
4214
|
+
/**
|
|
4215
|
+
* The command to run
|
|
4216
|
+
*
|
|
4217
|
+
* The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
|
|
4218
|
+
*
|
|
4219
|
+
* If you explicitly set `PATH` in `env`, that `PATH` will be used to resolve the executable instead of the default `PATH`.
|
|
4220
|
+
*
|
|
4221
|
+
* To check if the command exists before running it, use `Bun.which(bin)`.
|
|
4222
|
+
*
|
|
4223
|
+
* @example
|
|
4224
|
+
* ```ts
|
|
4225
|
+
* const subprocess = Bun.spawn(["echo", "hello"]);
|
|
4226
|
+
* ```
|
|
4227
|
+
*/
|
|
4228
|
+
cmds: string[],
|
|
4229
|
+
options?: Opts,
|
|
4230
|
+
): SpawnOptions.OptionsToSubprocess<Opts>;
|
|
4231
|
+
|
|
4232
|
+
/**
|
|
4233
|
+
* Spawn a new process
|
|
4234
|
+
*
|
|
4235
|
+
* ```js
|
|
4236
|
+
* const {stdout} = Bun.spawnSync({
|
|
4237
|
+
* cmd: ["echo", "hello"],
|
|
4238
|
+
* });
|
|
4239
|
+
* console.log(stdout.toString()); // "hello\n"
|
|
4240
|
+
* ```
|
|
4241
|
+
*
|
|
4242
|
+
* Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
|
|
4243
|
+
*/
|
|
4244
|
+
function spawnSync<Opts extends SpawnOptions.OptionsObject>(
|
|
4245
|
+
options: Opts & {
|
|
4246
|
+
/**
|
|
4247
|
+
* The command to run
|
|
4248
|
+
*
|
|
4249
|
+
* The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
|
|
4250
|
+
*
|
|
4251
|
+
* If you explicitly set `PATH` in `env`, that `PATH` will be used to resolve the executable instead of the default `PATH`.
|
|
4252
|
+
*
|
|
4253
|
+
* To check if the command exists before running it, use `Bun.which(bin)`.
|
|
4254
|
+
*
|
|
4255
|
+
* @example
|
|
4256
|
+
* ```ts
|
|
4257
|
+
* const subprocess = Bun.spawnSync({ cmd: ["echo", "hello"] });
|
|
4258
|
+
* ```
|
|
4259
|
+
*/
|
|
4260
|
+
cmd: string[];
|
|
4261
|
+
},
|
|
4262
|
+
): SpawnOptions.OptionsToSyncSubprocess<Opts>;
|
|
4263
|
+
|
|
4264
|
+
/**
|
|
4265
|
+
* Synchronously spawn a new process
|
|
4266
|
+
*
|
|
4267
|
+
* ```js
|
|
4268
|
+
* const {stdout} = Bun.spawnSync(["echo", "hello"]);
|
|
4269
|
+
* console.log(stdout.toString()); // "hello\n"
|
|
4270
|
+
* ```
|
|
4271
|
+
*
|
|
4272
|
+
* Internally, this uses [posix_spawn(2)](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/posix_spawn.2.html)
|
|
4273
|
+
*/
|
|
4274
|
+
function spawnSync<Opts extends SpawnOptions.OptionsObject>(
|
|
4275
|
+
/**
|
|
4276
|
+
* The command to run
|
|
4277
|
+
*
|
|
4278
|
+
* The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
|
|
4279
|
+
*
|
|
4280
|
+
* If you explicitly set `PATH` in `env`, that `PATH` will be used to resolve the executable instead of the default `PATH`.
|
|
4281
|
+
*
|
|
4282
|
+
* To check if the command exists before running it, use `Bun.which(bin)`.
|
|
4283
|
+
*
|
|
4284
|
+
* @example
|
|
4285
|
+
* ```ts
|
|
4286
|
+
* const subprocess = Bun.spawnSync(["echo", "hello"]);
|
|
4287
|
+
* ```
|
|
4288
|
+
*/
|
|
4289
|
+
cmds: string[],
|
|
4290
|
+
options?: Opts,
|
|
4291
|
+
): SpawnOptions.OptionsToSyncSubprocess<Opts>;
|
|
4292
|
+
|
|
4293
|
+
/** Utility type for any process from {@link Bun.spawn()} with both stdout and stderr set to `"pipe"` */
|
|
4294
|
+
type ReadableSubprocess = Subprocess<any, "pipe", "pipe">;
|
|
4295
|
+
/** Utility type for any process from {@link Bun.spawn()} with stdin set to `"pipe"` */
|
|
4296
|
+
type WritableSubprocess = Subprocess<"pipe", any, any>;
|
|
4297
|
+
/** Utility type for any process from {@link Bun.spawn()} with stdin, stdout, stderr all set to `"pipe"`. A combination of {@link ReadableSubprocess} and {@link WritableSubprocess} */
|
|
4298
|
+
type PipedSubprocess = Subprocess<"pipe", "pipe", "pipe">;
|
|
4299
|
+
/** Utility type for any process from {@link Bun.spawn()} with stdin, stdout, stderr all set to `null` or similar. */
|
|
4300
|
+
type NullSubprocess = Subprocess<
|
|
4301
|
+
"ignore" | "inherit" | null | undefined,
|
|
4302
|
+
"ignore" | "inherit" | null | undefined,
|
|
4303
|
+
"ignore" | "inherit" | null | undefined
|
|
4304
|
+
>;
|
|
4305
|
+
/** Utility type for any process from {@link Bun.spawnSync()} with both stdout and stderr set to `"pipe"` */
|
|
4306
|
+
type ReadableSyncSubprocess = SyncSubprocess<"pipe", "pipe">;
|
|
4307
|
+
/** Utility type for any process from {@link Bun.spawnSync()} with both stdout and stderr set to `null` or similar */
|
|
4308
|
+
type NullSyncSubprocess = SyncSubprocess<
|
|
4309
|
+
"ignore" | "inherit" | null | undefined,
|
|
4310
|
+
"ignore" | "inherit" | null | undefined
|
|
4311
|
+
>;
|
|
4312
|
+
|
|
4313
|
+
// Blocked on https://github.com/oven-sh/bun/issues/8329
|
|
4314
|
+
// /**
|
|
4315
|
+
// *
|
|
4316
|
+
// * Count the visible width of a string, as it would be displayed in a terminal.
|
|
4317
|
+
// *
|
|
4318
|
+
// * By default, strips ANSI escape codes before measuring the string. This is
|
|
4319
|
+
// * because ANSI escape codes are not visible characters. If passed a non-string,
|
|
4320
|
+
// * it will return 0.
|
|
4321
|
+
// *
|
|
4322
|
+
// * @param str The string to measure
|
|
4323
|
+
// * @param options
|
|
4324
|
+
// */
|
|
4325
|
+
// function stringWidth(
|
|
4326
|
+
// str: string,
|
|
4327
|
+
// options?: {
|
|
4328
|
+
// /**
|
|
4329
|
+
// * Whether to include ANSI escape codes in the width calculation
|
|
4330
|
+
// *
|
|
4331
|
+
// * Slightly faster if set to `false`, but less accurate if the string contains ANSI escape codes.
|
|
4332
|
+
// * @default false
|
|
4333
|
+
// */
|
|
4334
|
+
// countAnsiEscapeCodes?: boolean;
|
|
4335
|
+
// },
|
|
4336
|
+
// ): number;
|
|
4337
|
+
|
|
4338
|
+
class FileSystemRouter {
|
|
4339
|
+
/**
|
|
4340
|
+
* Create a new {@link FileSystemRouter}.
|
|
4341
|
+
*
|
|
4342
|
+
* @example
|
|
4343
|
+
* ```ts
|
|
4344
|
+
* const router = new FileSystemRouter({
|
|
4345
|
+
* dir: process.cwd() + "/pages",
|
|
4346
|
+
* style: "nextjs",
|
|
4347
|
+
* });
|
|
4348
|
+
*
|
|
4349
|
+
* const {params} = router.match("/blog/2020/01/01/hello-world");
|
|
4350
|
+
* console.log(params); // {year: "2020", month: "01", day: "01", slug: "hello-world"}
|
|
4351
|
+
* ```
|
|
4352
|
+
* @param options The options to use when creating the router
|
|
4353
|
+
* @param options.dir The root directory containing the files to route
|
|
4354
|
+
* @param options.style The style of router to use (only "nextjs" supported
|
|
4355
|
+
* for now)
|
|
4356
|
+
*/
|
|
4357
|
+
constructor(options: {
|
|
4358
|
+
/**
|
|
4359
|
+
* The root directory containing the files to route
|
|
4360
|
+
*
|
|
4361
|
+
* There is no default value for this option.
|
|
4362
|
+
*
|
|
4363
|
+
* @example
|
|
4364
|
+
* ```ts
|
|
4365
|
+
* const router = new FileSystemRouter({
|
|
4366
|
+
* dir:
|
|
4367
|
+
*/
|
|
4368
|
+
dir: string;
|
|
4369
|
+
style: "nextjs";
|
|
4370
|
+
|
|
4371
|
+
/** The base path to use when routing */
|
|
4372
|
+
assetPrefix?: string;
|
|
4373
|
+
origin?: string;
|
|
4374
|
+
/** Limit the pages to those with particular file extensions. */
|
|
4375
|
+
fileExtensions?: string[];
|
|
4376
|
+
});
|
|
4377
|
+
|
|
4378
|
+
// todo: URL
|
|
4379
|
+
match(input: string | Request | Response): MatchedRoute | null;
|
|
4380
|
+
|
|
4381
|
+
readonly assetPrefix: string;
|
|
4382
|
+
readonly origin: string;
|
|
4383
|
+
readonly style: string;
|
|
4384
|
+
readonly routes: Record<string, string>;
|
|
4385
|
+
|
|
4386
|
+
reload(): void;
|
|
4387
|
+
}
|
|
4388
|
+
|
|
4389
|
+
interface MatchedRoute {
|
|
4390
|
+
/**
|
|
4391
|
+
* A map of the parameters from the route
|
|
4392
|
+
*
|
|
4393
|
+
* @example
|
|
4394
|
+
* ```ts
|
|
4395
|
+
* const router = new FileSystemRouter({
|
|
4396
|
+
* dir: "/path/to/files",
|
|
4397
|
+
* style: "nextjs",
|
|
4398
|
+
* });
|
|
4399
|
+
* const {params} = router.match("/blog/2020/01/01/hello-world");
|
|
4400
|
+
* console.log(params.year); // "2020"
|
|
4401
|
+
* console.log(params.month); // "01"
|
|
4402
|
+
* console.log(params.day); // "01"
|
|
4403
|
+
* console.log(params.slug); // "hello-world"
|
|
4404
|
+
* ```
|
|
4405
|
+
*/
|
|
4406
|
+
readonly params: Record<string, string>;
|
|
4407
|
+
readonly filePath: string;
|
|
4408
|
+
readonly pathname: string;
|
|
4409
|
+
readonly query: Record<string, string>;
|
|
4410
|
+
readonly name: string;
|
|
4411
|
+
readonly kind: "exact" | "catch-all" | "optional-catch-all" | "dynamic";
|
|
4412
|
+
readonly src: string;
|
|
4413
|
+
}
|
|
4414
|
+
|
|
4415
|
+
/**
|
|
4416
|
+
* The current version of Bun
|
|
4417
|
+
* @example
|
|
4418
|
+
* "0.2.0"
|
|
4419
|
+
*/
|
|
4420
|
+
const version: string;
|
|
4421
|
+
|
|
4422
|
+
/**
|
|
4423
|
+
* The git sha at the time the currently-running version of Bun was compiled
|
|
4424
|
+
* @example
|
|
4425
|
+
* "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
|
|
4426
|
+
*/
|
|
4427
|
+
const revision: string;
|
|
4428
|
+
|
|
4429
|
+
/**
|
|
4430
|
+
* Find the index of a newline character in potentially ill-formed UTF-8 text.
|
|
4431
|
+
*
|
|
4432
|
+
* This is sort of like readline() except without the IO.
|
|
4433
|
+
*/
|
|
4434
|
+
function indexOfLine(buffer: ArrayBufferView | ArrayBufferLike, offset?: number): number;
|
|
4435
|
+
|
|
4436
|
+
/**
|
|
4437
|
+
* Provides a higher level API for command-line argument parsing than interacting
|
|
4438
|
+
* with `process.argv` directly. Takes a specification for the expected arguments
|
|
4439
|
+
* and returns a structured object with the parsed options and positionals.
|
|
4440
|
+
*
|
|
4441
|
+
* ```js
|
|
4442
|
+
* const args = ['-f', '--bar', 'b'];
|
|
4443
|
+
* const options = {
|
|
4444
|
+
* foo: {
|
|
4445
|
+
* type: 'boolean',
|
|
4446
|
+
* short: 'f',
|
|
4447
|
+
* },
|
|
4448
|
+
* bar: {
|
|
4449
|
+
* type: 'string',
|
|
4450
|
+
* },
|
|
4451
|
+
* };
|
|
4452
|
+
* const {
|
|
4453
|
+
* values,
|
|
4454
|
+
* positionals,
|
|
4455
|
+
* } = Bun.parseArgs({ args, options });
|
|
4456
|
+
* console.log(values, positionals);
|
|
4457
|
+
* // Prints: { foo: true, bar: 'b' } []
|
|
4458
|
+
* ```
|
|
4459
|
+
* @param config Used to provide arguments for parsing and to configure the parser.
|
|
4460
|
+
* @return The parsed command line arguments
|
|
4461
|
+
*/
|
|
4462
|
+
const parseArgs: typeof import("util").parseArgs;
|
|
4463
|
+
|
|
4464
|
+
interface GlobScanOptions {
|
|
4465
|
+
/**
|
|
4466
|
+
* The root directory to start matching from. Defaults to `process.cwd()`
|
|
4467
|
+
*/
|
|
4468
|
+
cwd?: string;
|
|
4469
|
+
|
|
4470
|
+
/**
|
|
4471
|
+
* Allow patterns to match entries that begin with a period (`.`).
|
|
4472
|
+
*
|
|
4473
|
+
* @default false
|
|
4474
|
+
*/
|
|
4475
|
+
dot?: boolean;
|
|
4476
|
+
|
|
4477
|
+
/**
|
|
4478
|
+
* Return the absolute path for entries.
|
|
4479
|
+
*
|
|
4480
|
+
* @default false
|
|
4481
|
+
*/
|
|
4482
|
+
absolute?: boolean;
|
|
4483
|
+
|
|
4484
|
+
/**
|
|
4485
|
+
* Indicates whether to traverse descendants of symbolic link directories.
|
|
4486
|
+
*
|
|
4487
|
+
* @default false
|
|
4488
|
+
*/
|
|
4489
|
+
followSymlinks?: boolean;
|
|
4490
|
+
|
|
4491
|
+
/**
|
|
4492
|
+
* Throw an error when symbolic link is broken
|
|
4493
|
+
*
|
|
4494
|
+
* @default false
|
|
4495
|
+
*/
|
|
4496
|
+
throwErrorOnBrokenSymlink?: boolean;
|
|
4497
|
+
|
|
4498
|
+
/**
|
|
4499
|
+
* Return only files.
|
|
4500
|
+
*
|
|
4501
|
+
* @default true
|
|
4502
|
+
*/
|
|
4503
|
+
onlyFiles?: boolean;
|
|
4504
|
+
}
|
|
4505
|
+
|
|
4506
|
+
/**
|
|
4507
|
+
* Match files using [glob patterns](https://en.wikipedia.org/wiki/Glob_(programming)).
|
|
4508
|
+
*
|
|
4509
|
+
* The supported pattern syntax for is:
|
|
4510
|
+
*
|
|
4511
|
+
* - `?`
|
|
4512
|
+
* Matches any single character.
|
|
4513
|
+
* - `*`
|
|
4514
|
+
* Matches zero or more characters, except for path separators ('/' or '\').
|
|
4515
|
+
* - `**`
|
|
4516
|
+
* Matches zero or more characters, including path separators.
|
|
4517
|
+
* Must match a complete path segment, i.e. followed by a path separator or
|
|
4518
|
+
* at the end of the pattern.
|
|
4519
|
+
* - `[ab]`
|
|
4520
|
+
* Matches one of the characters contained in the brackets.
|
|
4521
|
+
* Character ranges (e.g. "[a-z]") are also supported.
|
|
4522
|
+
* Use "[!ab]" or "[^ab]" to match any character *except* those contained
|
|
4523
|
+
* in the brackets.
|
|
4524
|
+
* - `{a,b}`
|
|
4525
|
+
* Match one of the patterns contained in the braces.
|
|
4526
|
+
* Any of the wildcards listed above can be used in the sub patterns.
|
|
4527
|
+
* Braces may be nested up to 10 levels deep.
|
|
4528
|
+
* - `!`
|
|
4529
|
+
* Negates the result when at the start of the pattern.
|
|
4530
|
+
* Multiple "!" characters negate the pattern multiple times.
|
|
4531
|
+
* - `\`
|
|
4532
|
+
* Used to escape any of the special characters above.
|
|
4533
|
+
*
|
|
4534
|
+
* @example
|
|
4535
|
+
* ```js
|
|
4536
|
+
* const glob = new Glob("*.{ts,tsx}");
|
|
4537
|
+
* const scannedFiles = await Array.fromAsync(glob.scan({ cwd: './src' }))
|
|
4538
|
+
* ```
|
|
4539
|
+
*/
|
|
4540
|
+
export class Glob {
|
|
4541
|
+
constructor(pattern: string);
|
|
4542
|
+
|
|
4543
|
+
/**
|
|
4544
|
+
* Scan a root directory recursively for files that match this glob pattern. Returns an async iterator.
|
|
4545
|
+
*
|
|
4546
|
+
* @throws {ENOTDIR} Given root cwd path must be a directory
|
|
4547
|
+
*
|
|
4548
|
+
* @example
|
|
4549
|
+
* ```js
|
|
4550
|
+
* const glob = new Glob("*.{ts,tsx}");
|
|
4551
|
+
* const scannedFiles = await Array.fromAsync(glob.scan({ cwd: './src' }))
|
|
4552
|
+
* ```
|
|
4553
|
+
*
|
|
4554
|
+
* @example
|
|
4555
|
+
* ```js
|
|
4556
|
+
* const glob = new Glob("*.{ts,tsx}");
|
|
4557
|
+
* for await (const path of glob.scan()) {
|
|
4558
|
+
* // do something
|
|
4559
|
+
* }
|
|
4560
|
+
* ```
|
|
4561
|
+
*/
|
|
4562
|
+
scan(optionsOrCwd?: string | GlobScanOptions): AsyncIterableIterator<string>;
|
|
4563
|
+
|
|
4564
|
+
/**
|
|
4565
|
+
* Synchronously scan a root directory recursively for files that match this glob pattern. Returns an iterator.
|
|
4566
|
+
*
|
|
4567
|
+
* @throws {ENOTDIR} Given root cwd path must be a directory
|
|
4568
|
+
*
|
|
4569
|
+
* @example
|
|
4570
|
+
* ```js
|
|
4571
|
+
* const glob = new Glob("*.{ts,tsx}");
|
|
4572
|
+
* const scannedFiles = Array.from(glob.scan({ cwd: './src' }))
|
|
4573
|
+
* ```
|
|
4574
|
+
*
|
|
4575
|
+
* @example
|
|
4576
|
+
* ```js
|
|
4577
|
+
* const glob = new Glob("*.{ts,tsx}");
|
|
4578
|
+
* for (const path of glob.scan()) {
|
|
4579
|
+
* // do something
|
|
4580
|
+
* }
|
|
4581
|
+
* ```
|
|
4582
|
+
*/
|
|
4583
|
+
scanSync(optionsOrCwd?: string | GlobScanOptions): IterableIterator<string>;
|
|
4584
|
+
|
|
4585
|
+
/**
|
|
4586
|
+
* Match the glob against a string
|
|
4587
|
+
*
|
|
4588
|
+
* @example
|
|
4589
|
+
* ```js
|
|
4590
|
+
* const glob = new Glob("*.{ts,tsx}");
|
|
4591
|
+
* expect(glob.match('foo.ts')).toBeTrue();
|
|
4592
|
+
* ```
|
|
4593
|
+
*/
|
|
4594
|
+
match(str: string): boolean;
|
|
4595
|
+
}
|
|
4596
|
+
}
|
|
4597
|
+
|
|
4598
|
+
// extends lib.dom.d.ts
|
|
4599
|
+
interface BufferEncodingOption {
|
|
4600
|
+
encoding?: BufferEncoding;
|
|
4601
|
+
}
|