| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,7 @@ CLAUDE.md | |||
| 95 | 95 | ||
| 96 | 96 | # Ignore .pi | |
| 97 | 97 | .pi | |
| 98 | + AGENTS.md | ||
| 98 | 99 | ||
| 99 | 100 | # Ignore .githuman | |
| 100 | 101 | .githuman | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,6 +96,10 @@ Create a commit which includes all of the updated files in lib/llhttp. | |||
| 96 | 96 | ||
| 97 | 97 | ### Steps: | |
| 98 | 98 | ||
| 99 | + `npm run test:wpt` and `node test/web-platform-tests/wpt-runner.mjs setup` will initialize the WPT submodule automatically when it is missing. | ||
| 100 | + | ||
| 101 | + If you want to prepare the checkout explicitly, run: | ||
| 102 | + | ||
| 99 | 103 | ```bash | |
| 100 | 104 | git submodule update --init --recursive | |
| 101 | 105 | ``` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -154,6 +154,57 @@ const { statusCode, body } = await request('https://api.example.com/data'); | |||
| 154 | 154 | const data = await body.json(); | |
| 155 | 155 | ``` | |
| 156 | 156 | ||
| 157 | + ### Keep `fetch` and `FormData` together | ||
| 158 | + | ||
| 159 | + When you send a `FormData` body, keep `fetch` and `FormData` from the same | ||
| 160 | + implementation. | ||
| 161 | + | ||
| 162 | + Use one of these patterns: | ||
| 163 | + | ||
| 164 | + ```js | ||
| 165 | + // Built-in globals | ||
| 166 | + const body = new FormData() | ||
| 167 | + body.set('name', 'some') | ||
| 168 | + await fetch('https://example.com', { | ||
| 169 | + method: 'POST', | ||
| 170 | + body | ||
| 171 | + }) | ||
| 172 | + ``` | ||
| 173 | + | ||
| 174 | + ```js | ||
| 175 | + // undici module imports | ||
| 176 | + import { fetch, FormData } from 'undici' | ||
| 177 | + | ||
| 178 | + const body = new FormData() | ||
| 179 | + body.set('name', 'some') | ||
| 180 | + await fetch('https://example.com', { | ||
| 181 | + method: 'POST', | ||
| 182 | + body | ||
| 183 | + }) | ||
| 184 | + ``` | ||
| 185 | + | ||
| 186 | + If you want the installed `undici` package to provide the globals, call | ||
| 187 | + `install()` first: | ||
| 188 | + | ||
| 189 | + ```js | ||
| 190 | + import { install } from 'undici' | ||
| 191 | + | ||
| 192 | + install() | ||
| 193 | + | ||
| 194 | + const body = new FormData() | ||
| 195 | + body.set('name', 'some') | ||
| 196 | + await fetch('https://example.com', { | ||
| 197 | + method: 'POST', | ||
| 198 | + body | ||
| 199 | + }) | ||
| 200 | + ``` | ||
| 201 | + | ||
| 202 | + `install()` replaces the global `fetch`, `Headers`, `Response`, `Request`, and | ||
| 203 | + `FormData` implementations with undici's versions, so they all match. | ||
| 204 | + | ||
| 205 | + Avoid mixing a global `FormData` with `undici.fetch()`, or `undici.FormData` | ||
| 206 | + with the built-in global `fetch()`. | ||
| 207 | + | ||
| 157 | 208 | ### Version Compatibility | |
| 158 | 209 | ||
| 159 | 210 | You can check which version of undici is bundled with your Node.js version: | |
@@ -263,6 +314,11 @@ The `install()` function adds the following classes to `globalThis`: | |||
| 263 | 314 | - `CloseEvent`, `ErrorEvent`, `MessageEvent` - WebSocket events | |
| 264 | 315 | - `EventSource` - Server-sent events client | |
| 265 | 316 | ||
| 317 | + When you call `install()`, these globals come from the same undici | ||
| 318 | + implementation. For example, global `fetch` and global `FormData` will both be | ||
| 319 | + undici's versions, which is the recommended setup if you want to use undici | ||
| 320 | + through globals. | ||
| 321 | + | ||
| 266 | 322 | This is useful for: | |
| 267 | 323 | - Polyfilling environments that don't have fetch | |
| 268 | 324 | - Ensuring consistent fetch behavior across different Node.js versions | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -182,22 +182,24 @@ diagnosticsChannel.channel('undici:websocket:open').subscribe(({ | |||
| 182 | 182 | console.log(websocket) // the WebSocket instance | |
| 183 | 183 | ||
| 184 | 184 | // Handshake response details | |
| 185 | - console.log(handshakeResponse.status) // 101 for successful WebSocket upgrade | ||
| 186 | - console.log(handshakeResponse.statusText) // 'Switching Protocols' | ||
| 185 | + console.log(handshakeResponse.status) // 101 for HTTP/1.1, 200 for HTTP/2 extended CONNECT | ||
| 186 | + console.log(handshakeResponse.statusText) // 'Switching Protocols' for HTTP/1.1, commonly 'OK' for HTTP/2 in Node.js | ||
| 187 | 187 | console.log(handshakeResponse.headers) // Object containing response headers | |
| 188 | 188 | }) | |
| 189 | 189 | ``` | |
| 190 | 190 | ||
| 191 | 191 | ### Handshake Response Object | |
| 192 | 192 | ||
| 193 | - The `handshakeResponse` object contains the HTTP response that upgraded the connection to WebSocket: | ||
| 193 | + The `handshakeResponse` object contains the HTTP response that established the WebSocket connection: | ||
| 194 | 194 | ||
| 195 | - - `status` (number): The HTTP status code (101 for successful WebSocket upgrade) | ||
| 196 | - - `statusText` (string): The HTTP status message ('Switching Protocols' for successful upgrade) | ||
| 195 | + - `status` (number): The HTTP status code (`101` for HTTP/1.1 upgrade, `200` for HTTP/2 extended CONNECT) | ||
| 196 | + - `statusText` (string): The HTTP status message (`'Switching Protocols'` for HTTP/1.1, commonly `'OK'` for HTTP/2 in Node.js) | ||
| 197 | 197 | - `headers` (object): The HTTP response headers from the server, including: | |
| 198 | + - `sec-websocket-accept` and other WebSocket-related headers | ||
| 198 | 199 | - `upgrade: 'websocket'` | |
| 199 | 200 | - `connection: 'upgrade'` | |
| 200 | - - `sec-websocket-accept` and other WebSocket-related headers | ||
| 201 | + | ||
| 202 | + The `upgrade` and `connection` headers are only present for HTTP/1.1 handshakes. | ||
| 201 | 203 | ||
| 202 | 204 | This information is particularly useful for debugging and monitoring WebSocket connections, as it provides access to the initial HTTP handshake response that established the WebSocket connection. | |
| 203 | 205 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -364,7 +364,7 @@ client.dispatch({ | |||
| 364 | 364 | ||
| 365 | 365 | ### `Dispatcher.pipeline(options, handler)` | |
| 366 | 366 | ||
| 367 | - For easy use with [stream.pipeline](https://nodejs.org/api/stream.html#stream_stream_pipeline_source_transforms_destination_callback). The `handler` argument should return a `Readable` from which the result will be read. Usually it should just return the `body` argument unless some kind of transformation needs to be performed based on e.g. `headers` or `statusCode`. The `handler` should validate the response and save any required state. If there is an error, it should be thrown. The function returns a `Duplex` which writes to the request and reads from the response. | ||
| 367 | + For easy use with [stream.pipeline](https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-options). The `handler` argument should return a `Readable` from which the result will be read. Usually it should just return the `body` argument unless some kind of transformation needs to be performed based on e.g. `headers` or `statusCode`. The `handler` should validate the response and save any required state. If there is an error, it should be thrown. The function returns a `Duplex` which writes to the request and reads from the response. | ||
| 368 | 368 | ||
| 369 | 369 | Arguments: | |
| 370 | 370 | ||
@@ -963,7 +963,7 @@ const { Client, interceptors } = require("undici"); | |||
| 963 | 963 | const { redirect } = interceptors; | |
| 964 | 964 | ||
| 965 | 965 | const client = new Client("http://service.example").compose( | |
| 966 | - redirect({ maxRedirections: 3, throwOnMaxRedirects: true }) | ||
| 966 | + redirect({ maxRedirections: 3, throwOnMaxRedirect: true }) | ||
| 967 | 967 | ); | |
| 968 | 968 | client.request({ path: "/" }) | |
| 969 | 969 | ``` | |
@@ -1036,10 +1036,10 @@ The `dns` interceptor enables you to cache DNS lookups for a given duration, per | |||
| 1036 | 1036 | - `dualStack` - Whether to resolve both IPv4 and IPv6 addresses. Default: `true`. | |
| 1037 | 1037 | - It will also attempt a happy-eyeballs-like approach to connect to the available addresses in case of a connection failure. | |
| 1038 | 1038 | - `affinity` - Whether to use IPv4 or IPv6 addresses. Default: `4`. | |
| 1039 | - - It can be either `'4` or `6`. | ||
| 1039 | + - It can be either `4` or `6`. | ||
| 1040 | 1040 | - It will only take effect if `dualStack` is `false`. | |
| 1041 | 1041 | - `lookup: (hostname: string, options: LookupOptions, callback: (err: NodeJS.ErrnoException | null, addresses: DNSInterceptorRecord[]) => void) => void` - Custom lookup function. Default: `dns.lookup`. | |
| 1042 | - - For more info see [dns.lookup](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback). | ||
| 1042 | + - For more info see [dns.lookup](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback). | ||
| 1043 | 1043 | - `pick: (origin: URL, records: DNSInterceptorRecords, affinity: 4 | 6) => DNSInterceptorRecord` - Custom pick function. Default: `RoundRobin`. | |
| 1044 | 1044 | - The function should return a single record from the records array. | |
| 1045 | 1045 | - By default a simplified version of Round Robin is used. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,14 @@ This API is implemented as per the standard, you can find documentation on [MDN] | |||
| 10 | 10 | ||
| 11 | 11 | If any parameters are passed to the FormData constructor other than `undefined`, an error will be thrown. Other parameters are ignored. | |
| 12 | 12 | ||
| 13 | + When you use `FormData` as a request body, keep `fetch` and `FormData` from the | ||
| 14 | + same implementation. Use the built-in global `FormData` with the built-in | ||
| 15 | + global `fetch()`, and use `undici`'s `FormData` with `undici.fetch()`. | ||
| 16 | + | ||
| 17 | + If you want the installed `undici` package to provide the globals, call | ||
| 18 | + [`install()`](/docs/api/GlobalInstallation.md) so `fetch`, `Headers`, | ||
| 19 | + `Response`, `Request`, and `FormData` are installed together as a matching set. | ||
| 20 | + | ||
| 13 | 21 | ## Response | |
| 14 | 22 | ||
| 15 | 23 | This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Response) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,6 +43,54 @@ The `install()` function adds the following classes to `globalThis`: | |||
| 43 | 43 | | `MessageEvent` | WebSocket message event | | |
| 44 | 44 | | `EventSource` | Server-sent events client | | |
| 45 | 45 | ||
| 46 | + ## Using `FormData` with `fetch` | ||
| 47 | + | ||
| 48 | + If you send a `FormData` body, use matching implementations for `fetch` and | ||
| 49 | + `FormData`. | ||
| 50 | + | ||
| 51 | + These two patterns are safe: | ||
| 52 | + | ||
| 53 | + ```js | ||
| 54 | + // Built-in globals from Node.js | ||
| 55 | + const body = new FormData() | ||
| 56 | + await fetch('https://example.com', { | ||
| 57 | + method: 'POST', | ||
| 58 | + body | ||
| 59 | + }) | ||
| 60 | + ``` | ||
| 61 | + | ||
| 62 | + ```js | ||
| 63 | + // Globals installed from the undici package | ||
| 64 | + import { install } from 'undici' | ||
| 65 | + | ||
| 66 | + install() | ||
| 67 | + | ||
| 68 | + const body = new FormData() | ||
| 69 | + await fetch('https://example.com', { | ||
| 70 | + method: 'POST', | ||
| 71 | + body | ||
| 72 | + }) | ||
| 73 | + ``` | ||
| 74 | + | ||
| 75 | + After `install()`, `fetch`, `Headers`, `Response`, `Request`, and `FormData` | ||
| 76 | + all come from the installed `undici` package, so they work as a matching set. | ||
| 77 | + | ||
| 78 | + If you do not want to install globals, import both from `undici` instead: | ||
| 79 | + | ||
| 80 | + ```js | ||
| 81 | + import { fetch, FormData } from 'undici' | ||
| 82 | + | ||
| 83 | + const body = new FormData() | ||
| 84 | + await fetch('https://example.com', { | ||
| 85 | + method: 'POST', | ||
| 86 | + body | ||
| 87 | + }) | ||
| 88 | + ``` | ||
| 89 | + | ||
| 90 | + Avoid mixing a global `FormData` with `undici.fetch()`, or `undici.FormData` | ||
| 91 | + with the built-in global `fetch()`. Keeping them paired avoids surprising | ||
| 92 | + multipart behavior across Node.js and undici versions. | ||
| 93 | + | ||
| 46 | 94 | ## Use Cases | |
| 47 | 95 | ||
| 48 | 96 | Global installation is useful for: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,15 +2,14 @@ | |||
| 2 | 2 | ||
| 3 | 3 | A class that handles redirection logic for HTTP requests. | |
| 4 | 4 | ||
| 5 | - ## `new RedirectHandler(dispatch, maxRedirections, opts, handler, redirectionLimitReached)` | ||
| 5 | + ## `new RedirectHandler(dispatch, maxRedirections, opts, handler)` | ||
| 6 | 6 | ||
| 7 | 7 | Arguments: | |
| 8 | 8 | ||
| 9 | 9 | - **dispatch** `function` - The dispatch function to be called after every retry. | |
| 10 | 10 | - **maxRedirections** `number` - Maximum number of redirections allowed. | |
| 11 | 11 | - **opts** `object` - Options for handling redirection. | |
| 12 | 12 | - **handler** `object` - An object containing handlers for different stages of the request lifecycle. | |
| 13 | - - **redirectionLimitReached** `boolean` (default: `false`) - A flag that the implementer can provide to enable or disable the feature. If set to `false`, it indicates that the caller doesn't want to use the feature and prefers the old behavior. | ||
| 14 | 13 | ||
| 15 | 14 | Returns: `RedirectHandler` | |
| 16 | 15 | ||
@@ -20,7 +19,6 @@ Returns: `RedirectHandler` | |||
| 20 | 19 | - **maxRedirections** `number` (required) - Maximum number of redirections allowed. | |
| 21 | 20 | - **opts** `object` (required) - Options for handling redirection. | |
| 22 | 21 | - **handler** `object` (required) - Handlers for different stages of the request lifecycle. | |
| 23 | - - **redirectionLimitReached** `boolean` (default: `false`) - A flag that the implementer can provide to enable or disable the feature. If set to `false`, it indicates that the caller doesn't want to use the feature and prefers the old behavior. | ||
| 24 | 22 | ||
| 25 | 23 | ### Properties | |
| 26 | 24 | ||
@@ -30,7 +28,6 @@ Returns: `RedirectHandler` | |||
| 30 | 28 | - **maxRedirections** `number` - Maximum number of redirections allowed. | |
| 31 | 29 | - **handler** `object` - Handlers for different stages of the request lifecycle. | |
| 32 | 30 | - **history** `Array` - An array representing the history of URLs during redirection. | |
| 33 | - - **redirectionLimitReached** `boolean` - Indicates whether the redirection limit has been reached. | ||
| 34 | 31 | ||
| 35 | 32 | ### Methods | |
| 36 | 33 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,93 @@ When you install undici from npm, you get the full library with all of its | |||
| 19 | 19 | additional APIs, and potentially a newer release than what your Node.js version | |
| 20 | 20 | bundles. | |
| 21 | 21 | ||
| 22 | + ## Keep `fetch` and `FormData` from the same implementation | ||
| 23 | + | ||
| 24 | + When you send a `FormData` body, keep `fetch` and `FormData` together from the | ||
| 25 | + same implementation. | ||
| 26 | + | ||
| 27 | + Use one of these patterns: | ||
| 28 | + | ||
| 29 | + ### Built-in globals | ||
| 30 | + | ||
| 31 | + ```js | ||
| 32 | + const body = new FormData() | ||
| 33 | + body.set('name', 'some') | ||
| 34 | + body.set('someOtherProperty', '8000') | ||
| 35 | + | ||
| 36 | + await fetch('https://example.com', { | ||
| 37 | + method: 'POST', | ||
| 38 | + body | ||
| 39 | + }) | ||
| 40 | + ``` | ||
| 41 | + | ||
| 42 | + ### `undici` module imports | ||
| 43 | + | ||
| 44 | + ```js | ||
| 45 | + import { fetch, FormData } from 'undici' | ||
| 46 | + | ||
| 47 | + const body = new FormData() | ||
| 48 | + body.set('name', 'some') | ||
| 49 | + body.set('someOtherProperty', '8000') | ||
| 50 | + | ||
| 51 | + await fetch('https://example.com', { | ||
| 52 | + method: 'POST', | ||
| 53 | + body | ||
| 54 | + }) | ||
| 55 | + ``` | ||
| 56 | + | ||
| 57 | + ### `undici.install()` globals | ||
| 58 | + | ||
| 59 | + If you want the installed `undici` package to provide the globals, call | ||
| 60 | + [`install()`](/docs/api/GlobalInstallation.md): | ||
| 61 | + | ||
| 62 | + ```js | ||
| 63 | + import { install } from 'undici' | ||
| 64 | + | ||
| 65 | + install() | ||
| 66 | + | ||
| 67 | + const body = new FormData() | ||
| 68 | + body.set('name', 'some') | ||
| 69 | + body.set('someOtherProperty', '8000') | ||
| 70 | + | ||
| 71 | + await fetch('https://example.com', { | ||
| 72 | + method: 'POST', | ||
| 73 | + body | ||
| 74 | + }) | ||
| 75 | + ``` | ||
| 76 | + | ||
| 77 | + `install()` replaces the global `fetch`, `Headers`, `Response`, `Request`, and | ||
| 78 | + `FormData` implementations with undici's versions, and also installs undici's | ||
| 79 | + `WebSocket`, `CloseEvent`, `ErrorEvent`, `MessageEvent`, and `EventSource` | ||
| 80 | + globals. | ||
| 81 | + | ||
| 82 | + Avoid mixing implementations in the same request, for example: | ||
| 83 | + | ||
| 84 | + ```js | ||
| 85 | + import { fetch } from 'undici' | ||
| 86 | + | ||
| 87 | + const body = new FormData() | ||
| 88 | + | ||
| 89 | + await fetch('https://example.com', { | ||
| 90 | + method: 'POST', | ||
| 91 | + body | ||
| 92 | + }) | ||
| 93 | + ``` | ||
| 94 | + | ||
| 95 | + ```js | ||
| 96 | + import { FormData } from 'undici' | ||
| 97 | + | ||
| 98 | + const body = new FormData() | ||
| 99 | + | ||
| 100 | + await fetch('https://example.com', { | ||
| 101 | + method: 'POST', | ||
| 102 | + body | ||
| 103 | + }) | ||
| 104 | + ``` | ||
| 105 | + | ||
| 106 | + Those combinations may behave differently across Node.js and undici versions. | ||
| 107 | + Using matching pairs keeps multipart handling predictable. | ||
| 108 | + | ||
| 22 | 109 | ## When you do NOT need to install undici | |
| 23 | 110 | ||
| 24 | 111 | If all of the following are true, you can rely on the built-in globals and skip | |
@@ -119,12 +206,12 @@ You can always check the exact bundled version at runtime with | |||
| 119 | 206 | `process.versions.undici`. | |
| 120 | 207 | ||
| 121 | 208 | Installing undici from npm does not replace the built-in globals. If you want | |
| 122 | - your installed version to override the global `fetch`, use | ||
| 123 | - [`setGlobalDispatcher`](/docs/api/GlobalInstallation.md) or import `fetch` | ||
| 209 | + your installed version to replace the global `fetch` and related classes, use | ||
| 210 | + [`install()`](/docs/api/GlobalInstallation.md). Otherwise, import `fetch` | ||
| 124 | 211 | directly from `'undici'`: | |
| 125 | 212 | ||
| 126 | 213 | ```js | |
| 127 | - import { fetch } from 'undici'; // uses your installed version, not the built-in | ||
| 214 | + import { fetch } from 'undici' // uses your installed version, not the built-in | ||
| 128 | 215 | ``` | |
| 129 | 216 | ||
| 130 | 217 | ## Further reading | |
| Back | FazBrowse Home | New Git URL |
0 commit comments