You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
-238
Original file line number
Diff line number
Diff line change
@@ -114,10 +114,6 @@ Should you need to access the `body` in plain-text after using a mixin, the best
114
114
115
115
For more information about their behavior, please reference the body mixin from the [Fetch Standard](https://fetch.spec.whatwg.org/#body-mixin).
116
116
117
-
## Common API Methods
118
-
119
-
This section documents our most commonly used API methods. Additional APIs are documented in their own files within the [docs](./docs/) folder and are accessible via the navigation list on the left side of the docs site.
See [Dispatcher.request](./docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback) for more details, and [request examples](./examples/README.md) for examples.
You can pass an optional dispatcher to `fetch` as:
208
-
209
-
```js
210
-
import { fetch, Agent } from'undici'
211
-
212
-
constres=awaitfetch('https://example.com', {
213
-
// Mocks are also supported
214
-
dispatcher:newAgent({
215
-
keepAliveTimeout:10,
216
-
keepAliveMaxTimeout:10
217
-
})
218
-
})
219
-
constjson=awaitres.json()
220
-
console.log(json)
221
-
```
222
-
223
-
#### `request.body`
224
-
225
-
A body can be of the following types:
226
-
227
-
- ArrayBuffer
228
-
- ArrayBufferView
229
-
- AsyncIterables
230
-
- Blob
231
-
- Iterables
232
-
- String
233
-
- URLSearchParams
234
-
- FormData
235
-
236
-
In this implementation of fetch, ```request.body``` now accepts ```Async Iterables```. It is not present in the [Fetch Standard.](https://fetch.spec.whatwg.org)
[FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) besides text data and buffers can also utilize streams via [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects:
252
-
253
-
```js
254
-
import { openAsBlob } from'node:fs'
255
-
256
-
constfile=awaitopenAsBlob('./big.csv')
257
-
constbody=newFormData()
258
-
body.set('file', file, 'big.csv')
259
-
260
-
awaitfetch('http://example.com', { method:'POST', body })
261
-
```
262
-
263
-
#### `request.duplex`
264
-
265
-
- half
266
-
267
-
In this implementation of fetch, `request.duplex` must be set if `request.body` is `ReadableStream` or `Async Iterables`, however, fetch requests are currently always full duplex. For more detail refer to the [Fetch Standard.](https://fetch.spec.whatwg.org/#dom-requestinit-duplex).
268
-
269
-
#### `response.body`
270
-
271
-
Nodejs has two kinds of streams: [web streams](https://nodejs.org/dist/latest-v16.x/docs/api/webstreams.html), which follow the API of the WHATWG web standard found in browsers, and an older Node-specific [streams API](https://nodejs.org/api/stream.html). `response.body` returns a readable web stream. If you would prefer to work with a Node stream you can convert a web stream using `.fromWeb()`.
The [Fetch Standard](https://fetch.spec.whatwg.org) allows users to skip consuming the response body by relying on
292
-
[garbage collection](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#garbage_collection) to release connection resources. Undici does not do the same. Therefore, it is important to always either consume or cancel the response body.
293
-
294
-
Garbage collection in Node is less aggressive and deterministic
295
-
(due to the lack of clear idle periods that browsers have through the rendering refresh rate)
296
-
which means that leaving the release of connection resources to the garbage collector can lead
297
-
to excessive connection usage, reduced performance (due to less connection re-use), and even
298
-
stalls or deadlocks when running out of connections.
299
-
300
-
```js
301
-
// Do
302
-
constheaders=awaitfetch(url)
303
-
.then(asyncres=> {
304
-
forawait (constchunkofres.body) {
305
-
// force consumption of body
306
-
}
307
-
returnres.headers
308
-
})
309
-
310
-
// Do not
311
-
constheaders=awaitfetch(url)
312
-
.then(res=>res.headers)
313
-
```
314
-
315
-
However, if you want to get only headers, it might be better to use `HEAD` request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See [MDN - HTTP - HTTP request methods - HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) for more details.
The [Fetch Standard](https://fetch.spec.whatwg.org) requires implementations to exclude certain headers from requests and responses. In browser environments, some headers are forbidden so the user agent remains in full control over them. In Undici, these constraints are removed to give more control to the user.
330
-
331
-
### `undici.upgrade([url, options]): Promise`
332
-
333
-
Upgrade to a different protocol. See [MDN - HTTP - Protocol upgrade mechanism](https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism) for more details.
Returns a promise with the result of the `Dispatcher.upgrade` method.
344
-
345
-
Calls `options.dispatcher.upgrade(options)`.
346
-
347
-
See [Dispatcher.upgrade](./docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback) for more details.
348
-
349
133
### `undici.setGlobalDispatcher(dispatcher)`
350
134
351
135
* dispatcher `Dispatcher`
@@ -358,28 +142,6 @@ Gets the global dispatcher used by Common API Methods.
358
142
359
143
Returns: `Dispatcher`
360
144
361
-
### `undici.setGlobalOrigin(origin)`
362
-
363
-
* origin `string | URL | undefined`
364
-
365
-
Sets the global origin used in `fetch`.
366
-
367
-
If `undefined` is passed, the global origin will be reset. This will cause `Response.redirect`, `new Request()`, and `fetch` to throw an error when a relative path is passed.
0 commit comments