bun-types 1.2.6-canary.20250325T140646 → 1.2.7
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/README.md +1 -3
- package/bun.d.ts +7186 -7604
- package/bun.ns.d.ts +7 -0
- package/deprecated.d.ts +53 -58
- package/devserver.d.ts +177 -183
- package/docs/api/cookie.md +449 -0
- package/docs/api/fetch.md +1 -1
- package/docs/api/http.md +78 -0
- package/docs/api/spawn.md +1 -1
- package/docs/cli/publish.md +1 -1
- package/docs/guides/ecosystem/nuxt.md +1 -1
- package/docs/guides/install/add-peer.md +2 -2
- package/docs/guides/install/from-npm-install-to-bun-install.md +1 -1
- package/docs/guides/runtime/typescript.md +7 -6
- package/docs/guides/test/run-tests.md +3 -3
- package/docs/guides/test/snapshot.md +3 -3
- package/docs/guides/test/update-snapshots.md +1 -1
- package/docs/guides/util/version.md +1 -1
- package/docs/installation.md +4 -4
- package/docs/runtime/debugger.md +3 -3
- package/docs/runtime/nodejs-apis.md +1 -1
- package/docs/test/dom.md +1 -1
- package/docs/typescript.md +7 -6
- package/extensions.d.ts +31 -0
- package/fetch.d.ts +62 -159
- package/ffi.d.ts +1084 -1108
- package/globals.d.ts +1721 -1949
- package/html-rewriter.d.ts +146 -151
- package/index.d.ts +13 -11
- package/jsc.d.ts +216 -230
- package/overrides.d.ts +106 -63
- package/package.json +39 -31
- package/s3.d.ts +825 -0
- package/sqlite.d.ts +1143 -1172
- package/test.d.ts +2130 -2241
- package/wasm.d.ts +190 -288
- package/ambient.d.ts +0 -31
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
Bun provides native APIs for working with HTTP cookies through `Bun.Cookie` and `Bun.CookieMap`. These APIs offer fast, easy-to-use methods for parsing, generating, and manipulating cookies in HTTP requests and responses.
|
|
2
|
+
|
|
3
|
+
## CookieMap class
|
|
4
|
+
|
|
5
|
+
`Bun.CookieMap` provides a Map-like interface for working with collections of cookies. It implements the `Iterable` interface, allowing you to use it with `for...of` loops and other iteration methods.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// Empty cookie map
|
|
9
|
+
const cookies = new Bun.CookieMap();
|
|
10
|
+
|
|
11
|
+
// From a cookie string
|
|
12
|
+
const cookies1 = new Bun.CookieMap("name=value; foo=bar");
|
|
13
|
+
|
|
14
|
+
// From an object
|
|
15
|
+
const cookies2 = new Bun.CookieMap({
|
|
16
|
+
session: "abc123",
|
|
17
|
+
theme: "dark",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// From an array of name/value pairs
|
|
21
|
+
const cookies3 = new Bun.CookieMap([
|
|
22
|
+
["session", "abc123"],
|
|
23
|
+
["theme", "dark"],
|
|
24
|
+
]);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### In HTTP servers
|
|
28
|
+
|
|
29
|
+
In Bun's HTTP server, the `cookies` property on the request object (in `routes`) is an instance of `CookieMap`:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const server = Bun.serve({
|
|
33
|
+
routes: {
|
|
34
|
+
"/": req => {
|
|
35
|
+
// Access request cookies
|
|
36
|
+
const cookies = req.cookies;
|
|
37
|
+
|
|
38
|
+
// Get a specific cookie
|
|
39
|
+
const sessionCookie = cookies.get("session");
|
|
40
|
+
if (sessionCookie != null) {
|
|
41
|
+
console.log(sessionCookie);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Check if a cookie exists
|
|
45
|
+
if (cookies.has("theme")) {
|
|
46
|
+
// ...
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Set a cookie, it will be automatically applied to the response
|
|
50
|
+
cookies.set("visited", "true");
|
|
51
|
+
|
|
52
|
+
return new Response("Hello");
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
console.log("Server listening at: " + server.url);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Methods
|
|
61
|
+
|
|
62
|
+
#### `get(name: string): string | null`
|
|
63
|
+
|
|
64
|
+
Retrieves a cookie by name. Returns `null` if the cookie doesn't exist.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
// Get by name
|
|
68
|
+
const cookie = cookies.get("session");
|
|
69
|
+
|
|
70
|
+
if (cookie != null) {
|
|
71
|
+
console.log(cookie);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### `has(name: string): boolean`
|
|
76
|
+
|
|
77
|
+
Checks if a cookie with the given name exists.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// Check if cookie exists
|
|
81
|
+
if (cookies.has("session")) {
|
|
82
|
+
// Cookie exists
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### `set(name: string, value: string): void`
|
|
87
|
+
|
|
88
|
+
#### `set(options: CookieInit): void`
|
|
89
|
+
|
|
90
|
+
#### `set(cookie: Cookie): void`
|
|
91
|
+
|
|
92
|
+
Adds or updates a cookie in the map. Cookies default to `{ path: "/", sameSite: "lax" }`.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// Set by name and value
|
|
96
|
+
cookies.set("session", "abc123");
|
|
97
|
+
|
|
98
|
+
// Set using options object
|
|
99
|
+
cookies.set({
|
|
100
|
+
name: "theme",
|
|
101
|
+
value: "dark",
|
|
102
|
+
maxAge: 3600,
|
|
103
|
+
secure: true,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// Set using Cookie instance
|
|
107
|
+
const cookie = new Bun.Cookie("visited", "true");
|
|
108
|
+
cookies.set(cookie);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### `delete(name: string): void`
|
|
112
|
+
|
|
113
|
+
#### `delete(options: CookieStoreDeleteOptions): void`
|
|
114
|
+
|
|
115
|
+
Removes a cookie from the map. When applied to a Response, this adds a cookie with an empty string value and an expiry date in the past. A cookie will only delete successfully on the browser if the domain and path is the same as it was when the cookie was created.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
// Delete by name using default domain and path.
|
|
119
|
+
cookies.delete("session");
|
|
120
|
+
|
|
121
|
+
// Delete with domain/path options.
|
|
122
|
+
cookies.delete({
|
|
123
|
+
name: "session",
|
|
124
|
+
domain: "example.com",
|
|
125
|
+
path: "/admin",
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### `toJSON(): Record<string, string>`
|
|
130
|
+
|
|
131
|
+
Converts the cookie map to a serializable format.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
const json = cookies.toJSON();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### `toSetCookieHeaders(): string[]`
|
|
138
|
+
|
|
139
|
+
Returns an array of values for Set-Cookie headers that can be used to apply all cookie changes.
|
|
140
|
+
|
|
141
|
+
When using `Bun.serve()`, you don't need to call this method explicitly. Any changes made to the `req.cookies` map are automatically applied to the response headers. This method is primarily useful when working with other HTTP server implementations.
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import { createServer } from "node:http";
|
|
145
|
+
import { CookieMap } from "bun";
|
|
146
|
+
|
|
147
|
+
const server = createServer((req, res) => {
|
|
148
|
+
const cookieHeader = req.headers.cookie || "";
|
|
149
|
+
const cookies = new CookieMap(cookieHeader);
|
|
150
|
+
|
|
151
|
+
cookies.set("view-count", Number(cookies.get("view-count") || "0") + 1);
|
|
152
|
+
cookies.delete("session");
|
|
153
|
+
|
|
154
|
+
res.writeHead(200, {
|
|
155
|
+
"Content-Type": "text/plain",
|
|
156
|
+
"Set-Cookie": cookies.toSetCookieHeaders(),
|
|
157
|
+
});
|
|
158
|
+
res.end(`Found ${cookies.size} cookies`);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
server.listen(3000, () => {
|
|
162
|
+
console.log("Server running at http://localhost:3000/");
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Iteration
|
|
167
|
+
|
|
168
|
+
`CookieMap` provides several methods for iteration:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
// Iterate over [name, cookie] entries
|
|
172
|
+
for (const [name, value] of cookies) {
|
|
173
|
+
console.log(`${name}: ${value}`);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Using entries()
|
|
177
|
+
for (const [name, value] of cookies.entries()) {
|
|
178
|
+
console.log(`${name}: ${value}`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Using keys()
|
|
182
|
+
for (const name of cookies.keys()) {
|
|
183
|
+
console.log(name);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Using values()
|
|
187
|
+
for (const value of cookies.values()) {
|
|
188
|
+
console.log(value);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Using forEach
|
|
192
|
+
cookies.forEach((value, name) => {
|
|
193
|
+
console.log(`${name}: ${value}`);
|
|
194
|
+
});
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Properties
|
|
198
|
+
|
|
199
|
+
#### `size: number`
|
|
200
|
+
|
|
201
|
+
Returns the number of cookies in the map.
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
console.log(cookies.size); // Number of cookies
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Cookie class
|
|
208
|
+
|
|
209
|
+
`Bun.Cookie` represents an HTTP cookie with its name, value, and attributes.
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import { Cookie } from "bun";
|
|
213
|
+
|
|
214
|
+
// Create a basic cookie
|
|
215
|
+
const cookie = new Bun.Cookie("name", "value");
|
|
216
|
+
|
|
217
|
+
// Create a cookie with options
|
|
218
|
+
const secureSessionCookie = new Bun.Cookie("session", "abc123", {
|
|
219
|
+
domain: "example.com",
|
|
220
|
+
path: "/admin",
|
|
221
|
+
expires: new Date(Date.now() + 86400000), // 1 day
|
|
222
|
+
httpOnly: true,
|
|
223
|
+
secure: true,
|
|
224
|
+
sameSite: "strict",
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Parse from a cookie string
|
|
228
|
+
const parsedCookie = new Bun.Cookie("name=value; Path=/; HttpOnly");
|
|
229
|
+
|
|
230
|
+
// Create from an options object
|
|
231
|
+
const objCookie = new Bun.Cookie({
|
|
232
|
+
name: "theme",
|
|
233
|
+
value: "dark",
|
|
234
|
+
maxAge: 3600,
|
|
235
|
+
secure: true,
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Constructors
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
// Basic constructor with name/value
|
|
243
|
+
new Bun.Cookie(name: string, value: string);
|
|
244
|
+
|
|
245
|
+
// Constructor with name, value, and options
|
|
246
|
+
new Bun.Cookie(name: string, value: string, options: CookieInit);
|
|
247
|
+
|
|
248
|
+
// Constructor from cookie string
|
|
249
|
+
new Bun.Cookie(cookieString: string);
|
|
250
|
+
|
|
251
|
+
// Constructor from cookie object
|
|
252
|
+
new Bun.Cookie(options: CookieInit);
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Properties
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
cookie.name; // string - Cookie name
|
|
259
|
+
cookie.value; // string - Cookie value
|
|
260
|
+
cookie.domain; // string | null - Domain scope (null if not specified)
|
|
261
|
+
cookie.path; // string - URL path scope (defaults to "/")
|
|
262
|
+
cookie.expires; // number | undefined - Expiration timestamp (ms since epoch)
|
|
263
|
+
cookie.secure; // boolean - Require HTTPS
|
|
264
|
+
cookie.sameSite; // "strict" | "lax" | "none" - SameSite setting
|
|
265
|
+
cookie.partitioned; // boolean - Whether the cookie is partitioned (CHIPS)
|
|
266
|
+
cookie.maxAge; // number | undefined - Max age in seconds
|
|
267
|
+
cookie.httpOnly; // boolean - Accessible only via HTTP (not JavaScript)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### Methods
|
|
271
|
+
|
|
272
|
+
#### `isExpired(): boolean`
|
|
273
|
+
|
|
274
|
+
Checks if the cookie has expired.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
// Expired cookie (Date in the past)
|
|
278
|
+
const expiredCookie = new Bun.Cookie("name", "value", {
|
|
279
|
+
expires: new Date(Date.now() - 1000),
|
|
280
|
+
});
|
|
281
|
+
console.log(expiredCookie.isExpired()); // true
|
|
282
|
+
|
|
283
|
+
// Valid cookie (Using maxAge instead of expires)
|
|
284
|
+
const validCookie = new Bun.Cookie("name", "value", {
|
|
285
|
+
maxAge: 3600, // 1 hour in seconds
|
|
286
|
+
});
|
|
287
|
+
console.log(validCookie.isExpired()); // false
|
|
288
|
+
|
|
289
|
+
// Session cookie (no expiration)
|
|
290
|
+
const sessionCookie = new Bun.Cookie("name", "value");
|
|
291
|
+
console.log(sessionCookie.isExpired()); // false
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
#### `serialize(): string`
|
|
295
|
+
|
|
296
|
+
#### `toString(): string`
|
|
297
|
+
|
|
298
|
+
Returns a string representation of the cookie suitable for a `Set-Cookie` header.
|
|
299
|
+
|
|
300
|
+
```ts
|
|
301
|
+
const cookie = new Bun.Cookie("session", "abc123", {
|
|
302
|
+
domain: "example.com",
|
|
303
|
+
path: "/admin",
|
|
304
|
+
expires: new Date(Date.now() + 86400000),
|
|
305
|
+
secure: true,
|
|
306
|
+
httpOnly: true,
|
|
307
|
+
sameSite: "strict",
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
console.log(cookie.serialize());
|
|
311
|
+
// => "session=abc123; Domain=example.com; Path=/admin; Expires=Sun, 19 Mar 2025 15:03:26 GMT; Secure; HttpOnly; SameSite=strict"
|
|
312
|
+
console.log(cookie.toString());
|
|
313
|
+
// => "session=abc123; Domain=example.com; Path=/admin; Expires=Sun, 19 Mar 2025 15:03:26 GMT; Secure; HttpOnly; SameSite=strict"
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
#### `toJSON(): CookieInit`
|
|
317
|
+
|
|
318
|
+
Converts the cookie to a plain object suitable for JSON serialization.
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
const cookie = new Bun.Cookie("session", "abc123", {
|
|
322
|
+
secure: true,
|
|
323
|
+
httpOnly: true,
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const json = cookie.toJSON();
|
|
327
|
+
// => {
|
|
328
|
+
// name: "session",
|
|
329
|
+
// value: "abc123",
|
|
330
|
+
// path: "/",
|
|
331
|
+
// secure: true,
|
|
332
|
+
// httpOnly: true,
|
|
333
|
+
// sameSite: "lax",
|
|
334
|
+
// partitioned: false
|
|
335
|
+
// }
|
|
336
|
+
|
|
337
|
+
// Works with JSON.stringify
|
|
338
|
+
const jsonString = JSON.stringify(cookie);
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Static methods
|
|
342
|
+
|
|
343
|
+
#### `Cookie.parse(cookieString: string): Cookie`
|
|
344
|
+
|
|
345
|
+
Parses a cookie string into a `Cookie` instance.
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
const cookie = Bun.Cookie.parse("name=value; Path=/; Secure; SameSite=Lax");
|
|
349
|
+
|
|
350
|
+
console.log(cookie.name); // "name"
|
|
351
|
+
console.log(cookie.value); // "value"
|
|
352
|
+
console.log(cookie.path); // "/"
|
|
353
|
+
console.log(cookie.secure); // true
|
|
354
|
+
console.log(cookie.sameSite); // "lax"
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
#### `Cookie.from(name: string, value: string, options?: CookieInit): Cookie`
|
|
358
|
+
|
|
359
|
+
Factory method to create a cookie.
|
|
360
|
+
|
|
361
|
+
```ts
|
|
362
|
+
const cookie = Bun.Cookie.from("session", "abc123", {
|
|
363
|
+
httpOnly: true,
|
|
364
|
+
secure: true,
|
|
365
|
+
maxAge: 3600,
|
|
366
|
+
});
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## Types
|
|
370
|
+
|
|
371
|
+
```ts
|
|
372
|
+
interface CookieInit {
|
|
373
|
+
name?: string;
|
|
374
|
+
value?: string;
|
|
375
|
+
domain?: string;
|
|
376
|
+
/** Defaults to '/'. To allow the browser to set the path, use an empty string. */
|
|
377
|
+
path?: string;
|
|
378
|
+
expires?: number | Date | string;
|
|
379
|
+
secure?: boolean;
|
|
380
|
+
/** Defaults to `lax`. */
|
|
381
|
+
sameSite?: CookieSameSite;
|
|
382
|
+
httpOnly?: boolean;
|
|
383
|
+
partitioned?: boolean;
|
|
384
|
+
maxAge?: number;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
interface CookieStoreDeleteOptions {
|
|
388
|
+
name: string;
|
|
389
|
+
domain?: string | null;
|
|
390
|
+
path?: string;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
interface CookieStoreGetOptions {
|
|
394
|
+
name?: string;
|
|
395
|
+
url?: string;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
type CookieSameSite = "strict" | "lax" | "none";
|
|
399
|
+
|
|
400
|
+
class Cookie {
|
|
401
|
+
constructor(name: string, value: string, options?: CookieInit);
|
|
402
|
+
constructor(cookieString: string);
|
|
403
|
+
constructor(cookieObject?: CookieInit);
|
|
404
|
+
|
|
405
|
+
readonly name: string;
|
|
406
|
+
value: string;
|
|
407
|
+
domain?: string;
|
|
408
|
+
path: string;
|
|
409
|
+
expires?: Date;
|
|
410
|
+
secure: boolean;
|
|
411
|
+
sameSite: CookieSameSite;
|
|
412
|
+
partitioned: boolean;
|
|
413
|
+
maxAge?: number;
|
|
414
|
+
httpOnly: boolean;
|
|
415
|
+
|
|
416
|
+
isExpired(): boolean;
|
|
417
|
+
|
|
418
|
+
serialize(): string;
|
|
419
|
+
toString(): string;
|
|
420
|
+
toJSON(): CookieInit;
|
|
421
|
+
|
|
422
|
+
static parse(cookieString: string): Cookie;
|
|
423
|
+
static from(name: string, value: string, options?: CookieInit): Cookie;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
class CookieMap implements Iterable<[string, string]> {
|
|
427
|
+
constructor(init?: string[][] | Record<string, string> | string);
|
|
428
|
+
|
|
429
|
+
get(name: string): string | null;
|
|
430
|
+
|
|
431
|
+
toSetCookieHeaders(): string[];
|
|
432
|
+
|
|
433
|
+
has(name: string): boolean;
|
|
434
|
+
set(name: string, value: string, options?: CookieInit): void;
|
|
435
|
+
set(options: CookieInit): void;
|
|
436
|
+
delete(name: string): void;
|
|
437
|
+
delete(options: CookieStoreDeleteOptions): void;
|
|
438
|
+
delete(name: string, options: Omit<CookieStoreDeleteOptions, "name">): void;
|
|
439
|
+
toJSON(): Record<string, string>;
|
|
440
|
+
|
|
441
|
+
readonly size: number;
|
|
442
|
+
|
|
443
|
+
entries(): IterableIterator<[string, string]>;
|
|
444
|
+
keys(): IterableIterator<string>;
|
|
445
|
+
values(): IterableIterator<string>;
|
|
446
|
+
forEach(callback: (value: string, key: string, map: CookieMap) => void): void;
|
|
447
|
+
[Symbol.iterator](): IterableIterator<[string, string]>;
|
|
448
|
+
}
|
|
449
|
+
```
|
package/docs/api/fetch.md
CHANGED
|
@@ -337,7 +337,7 @@ This will print the request and response headers to your terminal:
|
|
|
337
337
|
```sh
|
|
338
338
|
[fetch] > HTTP/1.1 GET http://example.com/
|
|
339
339
|
[fetch] > Connection: keep-alive
|
|
340
|
-
[fetch] > User-Agent: Bun/1.2.
|
|
340
|
+
[fetch] > User-Agent: Bun/1.2.7
|
|
341
341
|
[fetch] > Accept: */*
|
|
342
342
|
[fetch] > Host: example.com
|
|
343
343
|
[fetch] > Accept-Encoding: gzip, deflate, br
|
package/docs/api/http.md
CHANGED
|
@@ -61,6 +61,7 @@ Routes in `Bun.serve()` receive a `BunRequest` (which extends [`Request`](https:
|
|
|
61
61
|
// Simplified for brevity
|
|
62
62
|
interface BunRequest<T extends string> extends Request {
|
|
63
63
|
params: Record<T, string>;
|
|
64
|
+
readonly cookies: CookieMap;
|
|
64
65
|
}
|
|
65
66
|
```
|
|
66
67
|
|
|
@@ -934,6 +935,83 @@ const server = Bun.serve({
|
|
|
934
935
|
|
|
935
936
|
Returns `null` for closed requests or Unix domain sockets.
|
|
936
937
|
|
|
938
|
+
## Working with Cookies
|
|
939
|
+
|
|
940
|
+
Bun provides a built-in API for working with cookies in HTTP requests and responses. The `BunRequest` object includes a `cookies` property that provides a `CookieMap` for easily accessing and manipulating cookies. When using `routes`, `Bun.serve()` automatically tracks `request.cookies.set` and applies them to the response.
|
|
941
|
+
|
|
942
|
+
### Reading cookies
|
|
943
|
+
|
|
944
|
+
Read cookies from incoming requests using the `cookies` property on the `BunRequest` object:
|
|
945
|
+
|
|
946
|
+
```ts
|
|
947
|
+
Bun.serve({
|
|
948
|
+
routes: {
|
|
949
|
+
"/profile": req => {
|
|
950
|
+
// Access cookies from the request
|
|
951
|
+
const userId = req.cookies.get("user_id");
|
|
952
|
+
const theme = req.cookies.get("theme") || "light";
|
|
953
|
+
|
|
954
|
+
return Response.json({
|
|
955
|
+
userId,
|
|
956
|
+
theme,
|
|
957
|
+
message: "Profile page",
|
|
958
|
+
});
|
|
959
|
+
},
|
|
960
|
+
},
|
|
961
|
+
});
|
|
962
|
+
```
|
|
963
|
+
|
|
964
|
+
### Setting cookies
|
|
965
|
+
|
|
966
|
+
To set cookies, use the `set` method on the `CookieMap` from the `BunRequest` object.
|
|
967
|
+
|
|
968
|
+
```ts
|
|
969
|
+
Bun.serve({
|
|
970
|
+
routes: {
|
|
971
|
+
"/login": req => {
|
|
972
|
+
const cookies = req.cookies;
|
|
973
|
+
|
|
974
|
+
// Set a cookie with various options
|
|
975
|
+
cookies.set("user_id", "12345", {
|
|
976
|
+
maxAge: 60 * 60 * 24 * 7, // 1 week
|
|
977
|
+
httpOnly: true,
|
|
978
|
+
secure: true,
|
|
979
|
+
path: "/",
|
|
980
|
+
});
|
|
981
|
+
|
|
982
|
+
// Add a theme preference cookie
|
|
983
|
+
cookies.set("theme", "dark");
|
|
984
|
+
|
|
985
|
+
// Modified cookies from the request are automatically applied to the response
|
|
986
|
+
return new Response("Login successful");
|
|
987
|
+
},
|
|
988
|
+
},
|
|
989
|
+
});
|
|
990
|
+
```
|
|
991
|
+
|
|
992
|
+
`Bun.serve()` automatically tracks modified cookies from the request and applies them to the response.
|
|
993
|
+
|
|
994
|
+
### Deleting cookies
|
|
995
|
+
|
|
996
|
+
To delete a cookie, use the `delete` method on the `request.cookies` (`CookieMap`) object:
|
|
997
|
+
|
|
998
|
+
```ts
|
|
999
|
+
Bun.serve({
|
|
1000
|
+
routes: {
|
|
1001
|
+
"/logout": req => {
|
|
1002
|
+
// Delete the user_id cookie
|
|
1003
|
+
req.cookies.delete("user_id", {
|
|
1004
|
+
path: "/",
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
return new Response("Logged out successfully");
|
|
1008
|
+
},
|
|
1009
|
+
},
|
|
1010
|
+
});
|
|
1011
|
+
```
|
|
1012
|
+
|
|
1013
|
+
Deleted cookies become a `Set-Cookie` header on the response with the `maxAge` set to `0` and an empty `value`.
|
|
1014
|
+
|
|
937
1015
|
## Server Metrics
|
|
938
1016
|
|
|
939
1017
|
### server.pendingRequests and server.pendingWebSockets
|
package/docs/api/spawn.md
CHANGED
|
@@ -120,7 +120,7 @@ You can read results from the subprocess via the `stdout` and `stderr` propertie
|
|
|
120
120
|
```ts
|
|
121
121
|
const proc = Bun.spawn(["bun", "--version"]);
|
|
122
122
|
const text = await new Response(proc.stdout).text();
|
|
123
|
-
console.log(text); // => "1.2.
|
|
123
|
+
console.log(text); // => "1.2.7"
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
Configure the output stream by passing one of the following values to `stdout/stderr`:
|
package/docs/cli/publish.md
CHANGED
|
@@ -9,7 +9,7 @@ $ bunx nuxi init my-nuxt-app
|
|
|
9
9
|
✔ Which package manager would you like to use?
|
|
10
10
|
bun
|
|
11
11
|
◐ Installing dependencies...
|
|
12
|
-
bun install v1.2.
|
|
12
|
+
bun install v1.2.7 (16b4bf34)
|
|
13
13
|
+ @nuxt/devtools@0.8.2
|
|
14
14
|
+ nuxt@3.7.0
|
|
15
15
|
785 packages installed [2.67s]
|
|
@@ -16,7 +16,7 @@ This will add the package to `peerDependencies` in `package.json`.
|
|
|
16
16
|
```json-diff
|
|
17
17
|
{
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
+ "@types/bun": "^1.2.
|
|
19
|
+
+ "@types/bun": "^1.2.7"
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
```
|
|
@@ -28,7 +28,7 @@ Running `bun install` will install peer dependencies by default, unless marked o
|
|
|
28
28
|
```json-diff
|
|
29
29
|
{
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@types/bun": "^1.2.
|
|
31
|
+
"@types/bun": "^1.2.7"
|
|
32
32
|
},
|
|
33
33
|
"peerDependenciesMeta": {
|
|
34
34
|
+ "@types/bun": {
|
|
@@ -97,7 +97,7 @@ $ bun update
|
|
|
97
97
|
$ bun update @types/bun --latest
|
|
98
98
|
|
|
99
99
|
# Update a dependency to a specific version
|
|
100
|
-
$ bun update @types/bun@1.2.
|
|
100
|
+
$ bun update @types/bun@1.2.7
|
|
101
101
|
|
|
102
102
|
# Update all dependencies to the latest versions
|
|
103
103
|
$ bun update --latest
|
|
@@ -15,8 +15,8 @@ Below is the full set of recommended `compilerOptions` for a Bun project. With t
|
|
|
15
15
|
```jsonc
|
|
16
16
|
{
|
|
17
17
|
"compilerOptions": {
|
|
18
|
-
//
|
|
19
|
-
"lib": ["ESNext"
|
|
18
|
+
// Environment setup & latest features
|
|
19
|
+
"lib": ["ESNext"],
|
|
20
20
|
"target": "ESNext",
|
|
21
21
|
"module": "ESNext",
|
|
22
22
|
"moduleDetection": "force",
|
|
@@ -33,11 +33,12 @@ Below is the full set of recommended `compilerOptions` for a Bun project. With t
|
|
|
33
33
|
"strict": true,
|
|
34
34
|
"skipLibCheck": true,
|
|
35
35
|
"noFallthroughCasesInSwitch": true,
|
|
36
|
+
"noUncheckedIndexedAccess": true,
|
|
36
37
|
|
|
37
|
-
// Some stricter flags
|
|
38
|
-
"noUnusedLocals":
|
|
39
|
-
"noUnusedParameters":
|
|
40
|
-
"noPropertyAccessFromIndexSignature":
|
|
38
|
+
// Some stricter flags (disabled by default)
|
|
39
|
+
"noUnusedLocals": false,
|
|
40
|
+
"noUnusedParameters": false,
|
|
41
|
+
"noPropertyAccessFromIndexSignature": false,
|
|
41
42
|
},
|
|
42
43
|
}
|
|
43
44
|
```
|
|
@@ -21,7 +21,7 @@ Here's what the output of a typical test run looks like. In this case, there are
|
|
|
21
21
|
|
|
22
22
|
```sh
|
|
23
23
|
$ bun test
|
|
24
|
-
bun test v1.2.
|
|
24
|
+
bun test v1.2.7 (9c68abdb)
|
|
25
25
|
|
|
26
26
|
test.test.js:
|
|
27
27
|
✓ add [0.87ms]
|
|
@@ -47,7 +47,7 @@ To only run certain test files, pass a positional argument to `bun test`. The ru
|
|
|
47
47
|
|
|
48
48
|
```sh
|
|
49
49
|
$ bun test test3
|
|
50
|
-
bun test v1.2.
|
|
50
|
+
bun test v1.2.7 (9c68abdb)
|
|
51
51
|
|
|
52
52
|
test3.test.js:
|
|
53
53
|
✓ add [1.40ms]
|
|
@@ -85,7 +85,7 @@ Adding `-t add` will only run tests with "add" in the name. This works with test
|
|
|
85
85
|
|
|
86
86
|
```sh
|
|
87
87
|
$ bun test -t add
|
|
88
|
-
bun test v1.2.
|
|
88
|
+
bun test v1.2.7 (9c68abdb)
|
|
89
89
|
|
|
90
90
|
test.test.js:
|
|
91
91
|
✓ add [1.79ms]
|
|
@@ -18,7 +18,7 @@ The first time this test is executed, Bun will evaluate the value passed into `e
|
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
20
|
$ bun test test/snap
|
|
21
|
-
bun test v1.2.
|
|
21
|
+
bun test v1.2.7 (9c68abdb)
|
|
22
22
|
|
|
23
23
|
test/snap.test.ts:
|
|
24
24
|
✓ snapshot [1.48ms]
|
|
@@ -61,7 +61,7 @@ Later, when this test file is executed again, Bun will read the snapshot file an
|
|
|
61
61
|
|
|
62
62
|
```sh
|
|
63
63
|
$ bun test
|
|
64
|
-
bun test v1.2.
|
|
64
|
+
bun test v1.2.7 (9c68abdb)
|
|
65
65
|
|
|
66
66
|
test/snap.test.ts:
|
|
67
67
|
✓ snapshot [1.05ms]
|
|
@@ -78,7 +78,7 @@ To update snapshots, use the `--update-snapshots` flag.
|
|
|
78
78
|
|
|
79
79
|
```sh
|
|
80
80
|
$ bun test --update-snapshots
|
|
81
|
-
bun test v1.2.
|
|
81
|
+
bun test v1.2.7 (9c68abdb)
|
|
82
82
|
|
|
83
83
|
test/snap.test.ts:
|
|
84
84
|
✓ snapshot [0.86ms]
|