| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -261,12 +261,22 @@ const readableWebStream = response.body | |||
| 261 | 261 | const readableNodeStream = Readable.fromWeb(readableWebStream) | |
| 262 | 262 | ``` | |
| 263 | 263 | ||
| 264 | - #### Specification Compliance | ||
| 264 | + ## Specification Compliance | ||
| 265 | 265 | ||
| 266 | - This section documents parts of the [Fetch Standard](https://fetch.spec.whatwg.org) that Undici does | ||
| 266 | + This section documents parts of the [HTTP/1.1](https://www.rfc-editor.org/rfc/rfc9110.html) and [Fetch Standard](https://fetch.spec.whatwg.org) that Undici does | ||
| 267 | 267 | not support or does not fully implement. | |
| 268 | 268 | ||
| 269 | - ##### Garbage Collection | ||
| 269 | + #### CORS | ||
| 270 | + | ||
| 271 | + Unlike browsers, Undici does not implement CORS (Cross-Origin Resource Sharing) checks by default. This means: | ||
| 272 | + | ||
| 273 | + - No preflight requests are automatically sent for cross-origin requests | ||
| 274 | + - No validation of `Access-Control-Allow-Origin` headers is performed | ||
| 275 | + - Requests to any origin are allowed regardless of the source | ||
| 276 | + | ||
| 277 | + This behavior is intentional for server-side environments where CORS restrictions are typically unnecessary. If your application requires CORS-like protections, you will need to implement these checks manually. | ||
| 278 | + | ||
| 279 | + #### Garbage Collection | ||
| 270 | 280 | ||
| 271 | 281 | * https://fetch.spec.whatwg.org/#garbage-collection | |
| 272 | 282 | ||
@@ -307,7 +317,7 @@ const headers = await fetch(url, { method: 'HEAD' }) | |||
| 307 | 317 | .then(res => res.headers) | |
| 308 | 318 | ``` | |
| 309 | 319 | ||
| 310 | - ##### Forbidden and Safelisted Header Names | ||
| 320 | + #### Forbidden and Safelisted Header Names | ||
| 311 | 321 | ||
| 312 | 322 | * https://fetch.spec.whatwg.org/#cors-safelisted-response-header-name | |
| 313 | 323 | * https://fetch.spec.whatwg.org/#forbidden-header-name | |
@@ -316,7 +326,7 @@ const headers = await fetch(url, { method: 'HEAD' }) | |||
| 316 | 326 | ||
| 317 | 327 | 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. | |
| 318 | 328 | ||
| 319 | - ### `undici.upgrade([url, options]): Promise` | ||
| 329 | + #### `undici.upgrade([url, options]): Promise` | ||
| 320 | 330 | ||
| 321 | 331 | 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. | |
| 322 | 332 | ||
@@ -378,20 +388,15 @@ Returns: `URL` | |||
| 378 | 388 | * **protocol** `string` (optional) | |
| 379 | 389 | * **search** `string` (optional) | |
| 380 | 390 | ||
| 381 | - ## Specification Compliance | ||
| 382 | - | ||
| 383 | - This section documents parts of the HTTP/1.1 specification that Undici does | ||
| 384 | - not support or does not fully implement. | ||
| 385 | - | ||
| 386 | - ### Expect | ||
| 391 | + #### Expect | ||
| 387 | 392 | ||
| 388 | 393 | Undici does not support the `Expect` request header field. The request | |
| 389 | 394 | body is always immediately sent and the `100 Continue` response will be | |
| 390 | 395 | ignored. | |
| 391 | 396 | ||
| 392 | 397 | Refs: https://tools.ietf.org/html/rfc7231#section-5.1.1 | |
| 393 | 398 | ||
| 394 | - ### Pipelining | ||
| 399 | + #### Pipelining | ||
| 395 | 400 | ||
| 396 | 401 | Undici will only use pipelining if configured with a `pipelining` factor | |
| 397 | 402 | greater than `1`. Also it is important to pass `blocking: false` to the | |
@@ -412,7 +417,7 @@ aborted. | |||
| 412 | 417 | * Refs: https://tools.ietf.org/html/rfc2616#section-8.1.2.2 | |
| 413 | 418 | * Refs: https://tools.ietf.org/html/rfc7230#section-6.3.2 | |
| 414 | 419 | ||
| 415 | - ### Manual Redirect | ||
| 420 | + #### Manual Redirect | ||
| 416 | 421 | ||
| 417 | 422 | Since it is not possible to manually follow an HTTP redirect on the server-side, | |
| 418 | 423 | Undici returns the actual response instead of an `opaqueredirect` filtered one | |
@@ -421,9 +426,9 @@ implementations in Deno and Cloudflare Workers. | |||
| 421 | 426 | ||
| 422 | 427 | Refs: https://fetch.spec.whatwg.org/#atomic-http-redirect-handling | |
| 423 | 428 | ||
| 424 | - ## Workarounds | ||
| 429 | + ### Workarounds | ||
| 425 | 430 | ||
| 426 | - ### Network address family autoselection. | ||
| 431 | + #### Network address family autoselection. | ||
| 427 | 432 | ||
| 428 | 433 | If you experience problem when connecting to a remote server that is resolved by your DNS servers to a IPv6 (AAAA record) | |
| 429 | 434 | first, there are chances that your local router or ISP might have problem connecting to IPv6 networks. In that case | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -75,3 +75,9 @@ See [`Dispatcher.stream(options, factory[, callback])`](/docs/docs/api/Dispatche | |||
| 75 | 75 | ### `Agent.upgrade(options[, callback])` | |
| 76 | 76 | ||
| 77 | 77 | See [`Dispatcher.upgrade(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback). | |
| 78 | + | ||
| 79 | + ### `Agent.stats()` | ||
| 80 | + | ||
| 81 | + Returns an object of stats by origin in the format of `Record<string, TClientStats | TPoolStats>` | ||
| 82 | + | ||
| 83 | + See [`PoolStats`](/docs/docs/api/PoolStats.md) and [`ClientStats`](/docs/docs/api/ClientStats.md). | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + # Class: ClientStats | ||
| 2 | + | ||
| 3 | + Stats for a [Client](/docs/docs/api/Client.md). | ||
| 4 | + | ||
| 5 | + ## `new ClientStats(client)` | ||
| 6 | + | ||
| 7 | + Arguments: | ||
| 8 | + | ||
| 9 | + * **client** `Client` - Client from which to return stats. | ||
| 10 | + | ||
| 11 | + ## Instance Properties | ||
| 12 | + | ||
| 13 | + ### `ClientStats.connected` | ||
| 14 | + | ||
| 15 | + Boolean if socket as open connection by this client. | ||
| 16 | + | ||
| 17 | + ### `ClientStats.pending` | ||
| 18 | + | ||
| 19 | + Number of pending requests of this client. | ||
| 20 | + | ||
| 21 | + ### `ClientStats.running` | ||
| 22 | + | ||
| 23 | + Number of currently active requests across this client. | ||
| 24 | + | ||
| 25 | + ### `ClientStats.size` | ||
| 26 | + | ||
| 27 | + Number of active, pending, or queued requests of this clients. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,7 @@ diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => { | |||
| 19 | 19 | console.log('completed', request.completed) | |
| 20 | 20 | console.log('method', request.method) | |
| 21 | 21 | console.log('path', request.path) | |
| 22 | - console.log('headers') // array of strings, e.g: ['foo', 'bar'] | ||
| 22 | + console.log('headers', request.headers) // array of strings, e.g: ['foo', 'bar'] | ||
| 23 | 23 | request.addHeader('hello', 'world') | |
| 24 | 24 | console.log('headers', request.headers) // e.g. ['foo', 'bar', 'hello', 'world'] | |
| 25 | 25 | }) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,8 @@ Extends: [`AgentOptions`](/docs/docs/api/Agent.md#parameter-agentoptions) | |||
| 20 | 20 | ||
| 21 | 21 | * **ignoreTrailingSlash** `boolean` (optional) - Default: `false` - set the default value for `ignoreTrailingSlash` for interceptors. | |
| 22 | 22 | ||
| 23 | + * **acceptNonStandardSearchParameters** `boolean` (optional) - Default: `false` - set to `true` if the matcher should also accept non standard search parameters such as multi-value items specified with `[]` (e.g. `param[]=1¶m[]=2¶m[]=3`) and multi-value items which values are comma separated (e.g. `param=1,2,3`). | ||
| 24 | + | ||
| 23 | 25 | ### Example - Basic MockAgent instantiation | |
| 24 | 26 | ||
| 25 | 27 | This will instantiate the MockAgent. It will not do anything until registered as the agent to use with requests and mock interceptions are added. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,17 +76,9 @@ class MemoryCacheStore { | |||
| 76 | 76 | const topLevelKey = `${key.origin}:${key.path}` | |
| 77 | 77 | ||
| 78 | 78 | const now = Date.now() | |
| 79 | - const entry = this.#entries.get(topLevelKey)?.find((entry) => ( | ||
| 80 | - entry.deleteAt > now && | ||
| 81 | - entry.method === key.method && | ||
| 82 | - (entry.vary == null || Object.keys(entry.vary).every(headerName => { | ||
| 83 | - if (entry.vary[headerName] === null) { | ||
| 84 | - return key.headers[headerName] === undefined | ||
| 85 | - } | ||
| 79 | + const entries = this.#entries.get(topLevelKey) | ||
| 86 | 80 | ||
| 87 | - return entry.vary[headerName] === key.headers[headerName] | ||
| 88 | - })) | ||
| 89 | - )) | ||
| 81 | + const entry = entries ? findEntry(key, entries, now) : null | ||
| 90 | 82 | ||
| 91 | 83 | return entry == null | |
| 92 | 84 | ? undefined | |
@@ -140,10 +132,17 @@ class MemoryCacheStore { | |||
| 140 | 132 | entries = [] | |
| 141 | 133 | store.#entries.set(topLevelKey, entries) | |
| 142 | 134 | } | |
| 143 | - entries.push(entry) | ||
| 135 | + const previousEntry = findEntry(key, entries, Date.now()) | ||
| 136 | + if (previousEntry) { | ||
| 137 | + const index = entries.indexOf(previousEntry) | ||
| 138 | + entries.splice(index, 1, entry) | ||
| 139 | + store.#size -= previousEntry.size | ||
| 140 | + } else { | ||
| 141 | + entries.push(entry) | ||
| 142 | + store.#count += 1 | ||
| 143 | + } | ||
| 144 | 144 | ||
| 145 | 145 | store.#size += entry.size | |
| 146 | - store.#count += 1 | ||
| 147 | 146 | ||
| 148 | 147 | if (store.#size > store.#maxSize || store.#count > store.#maxCount) { | |
| 149 | 148 | for (const [key, entries] of store.#entries) { | |
@@ -180,4 +179,18 @@ class MemoryCacheStore { | |||
| 180 | 179 | } | |
| 181 | 180 | } | |
| 182 | 181 | ||
| 182 | + function findEntry (key, entries, now) { | ||
| 183 | + return entries.find((entry) => ( | ||
| 184 | + entry.deleteAt > now && | ||
| 185 | + entry.method === key.method && | ||
| 186 | + (entry.vary == null || Object.keys(entry.vary).every(headerName => { | ||
| 187 | + if (entry.vary[headerName] === null) { | ||
| 188 | + return key.headers[headerName] === undefined | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + return entry.vary[headerName] === key.headers[headerName] | ||
| 192 | + })) | ||
| 193 | + )) | ||
| 194 | + } | ||
| 195 | + | ||
| 183 | 196 | module.exports = MemoryCacheStore | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,7 @@ | |||
| 1 | 1 | 'use strict' | |
| 2 | 2 | ||
| 3 | 3 | const { InvalidArgumentError } = require('../core/errors') | |
| 4 | - const { kClients, kRunning, kClose, kDestroy, kDispatch } = require('../core/symbols') | ||
| 4 | + const { kClients, kRunning, kClose, kDestroy, kDispatch, kUrl } = require('../core/symbols') | ||
| 5 | 5 | const DispatcherBase = require('./dispatcher-base') | |
| 6 | 6 | const Pool = require('./pool') | |
| 7 | 7 | const Client = require('./client') | |
@@ -110,6 +110,16 @@ class Agent extends DispatcherBase { | |||
| 110 | 110 | ||
| 111 | 111 | await Promise.all(destroyPromises) | |
| 112 | 112 | } | |
| 113 | + | ||
| 114 | + get stats () { | ||
| 115 | + const allClientStats = {} | ||
| 116 | + for (const client of this[kClients].values()) { | ||
| 117 | + if (client.stats) { | ||
| 118 | + allClientStats[client[kUrl].origin] = client.stats | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + return allClientStats | ||
| 122 | + } | ||
| 113 | 123 | } | |
| 114 | 124 | ||
| 115 | 125 | module.exports = Agent | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -295,11 +295,13 @@ function writeH2 (client, request) { | |||
| 295 | 295 | if (Array.isArray(val)) { | |
| 296 | 296 | for (let i = 0; i < val.length; i++) { | |
| 297 | 297 | if (headers[key]) { | |
| 298 | - headers[key] += `,${val[i]}` | ||
| 298 | + headers[key] += `, ${val[i]}` | ||
| 299 | 299 | } else { | |
| 300 | 300 | headers[key] = val[i] | |
| 301 | 301 | } | |
| 302 | 302 | } | |
| 303 | + } else if (headers[key]) { | ||
| 304 | + headers[key] += `, ${val}` | ||
| 303 | 305 | } else { | |
| 304 | 306 | headers[key] = val | |
| 305 | 307 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ const assert = require('node:assert') | |||
| 4 | 4 | const net = require('node:net') | |
| 5 | 5 | const http = require('node:http') | |
| 6 | 6 | const util = require('../core/util.js') | |
| 7 | + const { ClientStats } = require('../util/stats.js') | ||
| 7 | 8 | const { channels } = require('../core/diagnostics.js') | |
| 8 | 9 | const Request = require('../core/request.js') | |
| 9 | 10 | const DispatcherBase = require('./dispatcher-base') | |
@@ -260,6 +261,10 @@ class Client extends DispatcherBase { | |||
| 260 | 261 | this[kResume](true) | |
| 261 | 262 | } | |
| 262 | 263 | ||
| 264 | + get stats () { | ||
| 265 | + return new ClientStats(this) | ||
| 266 | + } | ||
| 267 | + | ||
| 263 | 268 | get [kPending] () { | |
| 264 | 269 | return this[kQueue].length - this[kPendingIdx] | |
| 265 | 270 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,9 @@ | |||
| 1 | 1 | 'use strict' | |
| 2 | 2 | ||
| 3 | + const { PoolStats } = require('../util/stats.js') | ||
| 3 | 4 | const DispatcherBase = require('./dispatcher-base') | |
| 4 | 5 | const FixedQueue = require('./fixed-queue') | |
| 5 | 6 | const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch } = require('../core/symbols') | |
| 6 | - const PoolStats = require('./pool-stats') | ||
| 7 | 7 | ||
| 8 | 8 | const kClients = Symbol('clients') | |
| 9 | 9 | const kNeedDrain = Symbol('needDrain') | |
@@ -16,7 +16,6 @@ const kOnConnectionError = Symbol('onConnectionError') | |||
| 16 | 16 | const kGetDispatcher = Symbol('get dispatcher') | |
| 17 | 17 | const kAddClient = Symbol('add client') | |
| 18 | 18 | const kRemoveClient = Symbol('remove client') | |
| 19 | - const kStats = Symbol('stats') | ||
| 20 | 19 | ||
| 21 | 20 | class PoolBase extends DispatcherBase { | |
| 22 | 21 | constructor () { | |
@@ -67,8 +66,6 @@ class PoolBase extends DispatcherBase { | |||
| 67 | 66 | this[kOnConnectionError] = (origin, targets, err) => { | |
| 68 | 67 | pool.emit('connectionError', origin, [pool, ...targets], err) | |
| 69 | 68 | } | |
| 70 | - | ||
| 71 | - this[kStats] = new PoolStats(this) | ||
| 72 | 69 | } | |
| 73 | 70 | ||
| 74 | 71 | get [kBusy] () { | |
@@ -108,7 +105,7 @@ class PoolBase extends DispatcherBase { | |||
| 108 | 105 | } | |
| 109 | 106 | ||
| 110 | 107 | get stats () { | |
| 111 | - return this[kStats] | ||
| 108 | + return new PoolStats(this) | ||
| 112 | 109 | } | |
| 113 | 110 | ||
| 114 | 111 | async [kClose] () { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments