bun-types 1.2.1-canary.20250128T140616 → 1.2.1-canary.20250130T140539
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/docs/api/fetch.md +156 -1
- package/docs/api/http.md +2 -2
- package/docs/api/spawn.md +1 -1
- package/docs/cli/publish.md +1 -1
- package/docs/guides/runtime/cicd.md +1 -1
- package/docs/runtime/debugger.md +3 -3
- package/docs/test/dom.md +1 -1
- package/package.json +1 -1
package/docs/api/fetch.md
CHANGED
|
@@ -111,6 +111,38 @@ const reader = stream.getReader();
|
|
|
111
111
|
const { value, done } = await reader.read();
|
|
112
112
|
```
|
|
113
113
|
|
|
114
|
+
### Streaming request bodies
|
|
115
|
+
|
|
116
|
+
You can also stream data in request bodies using a `ReadableStream`:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const stream = new ReadableStream({
|
|
120
|
+
start(controller) {
|
|
121
|
+
controller.enqueue("Hello");
|
|
122
|
+
controller.enqueue(" ");
|
|
123
|
+
controller.enqueue("World");
|
|
124
|
+
controller.close();
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const response = await fetch("http://example.com", {
|
|
129
|
+
method: "POST",
|
|
130
|
+
body: stream,
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
When using streams with HTTP(S):
|
|
135
|
+
|
|
136
|
+
- The data is streamed directly to the network without buffering the entire body in memory
|
|
137
|
+
- If the connection is lost, the stream will be canceled
|
|
138
|
+
- The `Content-Length` header is not automatically set unless the stream has a known size
|
|
139
|
+
|
|
140
|
+
When using streams with S3:
|
|
141
|
+
|
|
142
|
+
- For PUT/POST requests, Bun automatically uses multipart upload
|
|
143
|
+
- The stream is consumed in chunks and uploaded in parallel
|
|
144
|
+
- Progress can be monitored through the S3 options
|
|
145
|
+
|
|
114
146
|
### Fetching a URL with a timeout
|
|
115
147
|
|
|
116
148
|
To fetch a URL with a timeout, use `AbortSignal.timeout`:
|
|
@@ -180,6 +212,116 @@ await fetch("https://example.com", {
|
|
|
180
212
|
|
|
181
213
|
This is similar to how it works in Node's `net` module.
|
|
182
214
|
|
|
215
|
+
#### Disable TLS validation
|
|
216
|
+
|
|
217
|
+
To disable TLS validation, set `rejectUnauthorized` to `false`:
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
await fetch("https://example.com", {
|
|
221
|
+
tls: {
|
|
222
|
+
rejectUnauthorized: false,
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
This is especially useful to avoid SSL errors when using self-signed certificates, but this disables TLS validation and should be used with caution.
|
|
228
|
+
|
|
229
|
+
### Request options
|
|
230
|
+
|
|
231
|
+
In addition to the standard fetch options, Bun provides several extensions:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const response = await fetch("http://example.com", {
|
|
235
|
+
// Control automatic response decompression (default: true)
|
|
236
|
+
decompress: true,
|
|
237
|
+
|
|
238
|
+
// Disable connection reuse for this request
|
|
239
|
+
keepalive: false,
|
|
240
|
+
|
|
241
|
+
// Debug logging level
|
|
242
|
+
verbose: true, // or "curl" for more detailed output
|
|
243
|
+
});
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### Protocol support
|
|
247
|
+
|
|
248
|
+
Beyond HTTP(S), Bun's fetch supports several additional protocols:
|
|
249
|
+
|
|
250
|
+
#### S3 URLs - `s3://`
|
|
251
|
+
|
|
252
|
+
Bun supports fetching from S3 buckets directly.
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
// Using environment variables for credentials
|
|
256
|
+
const response = await fetch("s3://my-bucket/path/to/object");
|
|
257
|
+
|
|
258
|
+
// Or passing credentials explicitly
|
|
259
|
+
const response = await fetch("s3://my-bucket/path/to/object", {
|
|
260
|
+
s3: {
|
|
261
|
+
accessKeyId: "YOUR_ACCESS_KEY",
|
|
262
|
+
secretAccessKey: "YOUR_SECRET_KEY",
|
|
263
|
+
region: "us-east-1",
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Note: Only PUT and POST methods support request bodies when using S3. For uploads, Bun automatically uses multipart upload for streaming bodies.
|
|
269
|
+
|
|
270
|
+
You can read more about Bun's S3 support in the [S3](https://bun.sh/docs/api/s3) documentation.
|
|
271
|
+
|
|
272
|
+
#### File URLs - `file://`
|
|
273
|
+
|
|
274
|
+
You can fetch local files using the `file:` protocol:
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
const response = await fetch("file:///path/to/file.txt");
|
|
278
|
+
const text = await response.text();
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
On Windows, paths are automatically normalized:
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
// Both work on Windows
|
|
285
|
+
const response = await fetch("file:///C:/path/to/file.txt");
|
|
286
|
+
const response2 = await fetch("file:///c:/path\\to/file.txt");
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
#### Data URLs - `data:`
|
|
290
|
+
|
|
291
|
+
Bun supports the `data:` URL scheme:
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
const response = await fetch("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==");
|
|
295
|
+
const text = await response.text(); // "Hello, World!"
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### Blob URLs - `blob:`
|
|
299
|
+
|
|
300
|
+
You can fetch blobs using URLs created by `URL.createObjectURL()`:
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
const blob = new Blob(["Hello, World!"], { type: "text/plain" });
|
|
304
|
+
const url = URL.createObjectURL(blob);
|
|
305
|
+
const response = await fetch(url);
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### Error handling
|
|
309
|
+
|
|
310
|
+
Bun's fetch implementation includes several specific error cases:
|
|
311
|
+
|
|
312
|
+
- Using a request body with GET/HEAD methods will throw an error (which is expected for the fetch API)
|
|
313
|
+
- Attempting to use both `proxy` and `unix` options together will throw an error
|
|
314
|
+
- TLS certificate validation failures when `rejectUnauthorized` is true (or undefined)
|
|
315
|
+
- S3 operations may throw specific errors related to authentication or permissions
|
|
316
|
+
|
|
317
|
+
### Content-Type handling
|
|
318
|
+
|
|
319
|
+
Bun automatically sets the `Content-Type` header for request bodies when not explicitly provided:
|
|
320
|
+
|
|
321
|
+
- For `Blob` objects, uses the blob's `type`
|
|
322
|
+
- For `FormData`, sets appropriate multipart boundary
|
|
323
|
+
- For JSON objects, sets `application/json`
|
|
324
|
+
|
|
183
325
|
## Debugging
|
|
184
326
|
|
|
185
327
|
To help with debugging, you can pass `verbose: true` to `fetch`:
|
|
@@ -195,7 +337,7 @@ This will print the request and response headers to your terminal:
|
|
|
195
337
|
```sh
|
|
196
338
|
[fetch] > HTTP/1.1 GET http://example.com/
|
|
197
339
|
[fetch] > Connection: keep-alive
|
|
198
|
-
[fetch] > User-Agent: Bun/1.2.1-canary.
|
|
340
|
+
[fetch] > User-Agent: Bun/1.2.1-canary.20250130T140539
|
|
199
341
|
[fetch] > Accept: */*
|
|
200
342
|
[fetch] > Host: example.com
|
|
201
343
|
[fetch] > Accept-Encoding: gzip, deflate, br
|
|
@@ -306,3 +448,16 @@ import { write } from "bun";
|
|
|
306
448
|
|
|
307
449
|
await write("output.txt", response);
|
|
308
450
|
```
|
|
451
|
+
|
|
452
|
+
### Implementation details
|
|
453
|
+
|
|
454
|
+
- Connection pooling is enabled by default but can be disabled per-request with `keepalive: false`. The `"Connection: close"` header can also be used to disable keep-alive.
|
|
455
|
+
- Large file uploads are optimized using the operating system's `sendfile` syscall under specific conditions:
|
|
456
|
+
- The file must be larger than 32KB
|
|
457
|
+
- The request must not be using a proxy
|
|
458
|
+
- On macOS, only regular files (not pipes, sockets, or devices) can use `sendfile`
|
|
459
|
+
- When these conditions aren't met, or when using S3/streaming uploads, Bun falls back to reading the file into memory
|
|
460
|
+
- This optimization is particularly effective for HTTP (not HTTPS) requests where the file can be sent directly from the kernel to the network stack
|
|
461
|
+
- S3 operations automatically handle signing requests and merging authentication headers
|
|
462
|
+
|
|
463
|
+
Note: Many of these features are Bun-specific extensions to the standard fetch API.
|
package/docs/api/http.md
CHANGED
|
@@ -612,8 +612,8 @@ const server = Bun.serve({
|
|
|
612
612
|
// Set 60 second timeout for this request
|
|
613
613
|
server.timeout(req, 60);
|
|
614
614
|
|
|
615
|
-
//
|
|
616
|
-
await
|
|
615
|
+
// If they take longer than 60 seconds to send the body, the request will be aborted
|
|
616
|
+
await req.text();
|
|
617
617
|
|
|
618
618
|
return new Response("Done!");
|
|
619
619
|
},
|
package/docs/api/spawn.md
CHANGED
|
@@ -110,7 +110,7 @@ You can read results from the subprocess via the `stdout` and `stderr` propertie
|
|
|
110
110
|
```ts
|
|
111
111
|
const proc = Bun.spawn(["bun", "--version"]);
|
|
112
112
|
const text = await new Response(proc.stdout).text();
|
|
113
|
-
console.log(text); // => "1.2.1-canary.
|
|
113
|
+
console.log(text); // => "1.2.1-canary.20250130T140539"
|
|
114
114
|
```
|
|
115
115
|
|
|
116
116
|
Configure the output stream by passing one of the following values to `stdout/stderr`:
|
package/docs/cli/publish.md
CHANGED
package/docs/runtime/debugger.md
CHANGED
|
@@ -124,11 +124,11 @@ await fetch("https://example.com", {
|
|
|
124
124
|
This prints the `fetch` request as a single-line `curl` command to let you copy-paste into your terminal to replicate the request.
|
|
125
125
|
|
|
126
126
|
```sh
|
|
127
|
-
[fetch] $ curl --http1.1 "https://example.com/" -X POST -H "content-type: application/json" -H "Connection: keep-alive" -H "User-Agent: Bun/1.2.1-canary.
|
|
127
|
+
[fetch] $ curl --http1.1 "https://example.com/" -X POST -H "content-type: application/json" -H "Connection: keep-alive" -H "User-Agent: Bun/1.2.1-canary.20250130T140539" -H "Accept: */*" -H "Host: example.com" -H "Accept-Encoding: gzip, deflate, br" --compressed -H "Content-Length: 13" --data-raw "{\"foo\":\"bar\"}"
|
|
128
128
|
[fetch] > HTTP/1.1 POST https://example.com/
|
|
129
129
|
[fetch] > content-type: application/json
|
|
130
130
|
[fetch] > Connection: keep-alive
|
|
131
|
-
[fetch] > User-Agent: Bun/1.2.1-canary.
|
|
131
|
+
[fetch] > User-Agent: Bun/1.2.1-canary.20250130T140539
|
|
132
132
|
[fetch] > Accept: */*
|
|
133
133
|
[fetch] > Host: example.com
|
|
134
134
|
[fetch] > Accept-Encoding: gzip, deflate, br
|
|
@@ -170,7 +170,7 @@ This prints the following to the console:
|
|
|
170
170
|
[fetch] > HTTP/1.1 POST https://example.com/
|
|
171
171
|
[fetch] > content-type: application/json
|
|
172
172
|
[fetch] > Connection: keep-alive
|
|
173
|
-
[fetch] > User-Agent: Bun/1.2.1-canary.
|
|
173
|
+
[fetch] > User-Agent: Bun/1.2.1-canary.20250130T140539
|
|
174
174
|
[fetch] > Accept: */*
|
|
175
175
|
[fetch] > Host: example.com
|
|
176
176
|
[fetch] > Accept-Encoding: gzip, deflate, br
|
package/docs/test/dom.md
CHANGED
package/package.json
CHANGED