https://github.com/cy-suite/undici

https://github.com/cy-suite/undici

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (7.6%) to scientific vocabulary
Last synced: 6 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: cy-suite
  • License: mit
  • Language: JavaScript
  • Default Branch: main
  • Size: 9.45 MB
Statistics
  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • Open Issues: 14
  • Releases: 0
Created over 1 year ago · Last pushed about 1 year ago
Metadata Files
Readme Contributing License Code of conduct Security Governance

README.md

undici

Node CI neostandard javascript style npm version codecov

An HTTP/1.1 client, written from scratch for Node.js.

Undici means eleven in Italian. 1.1 -> 11 -> Eleven -> Undici. It is also a Stranger Things reference.

How to get involved

Have a question about using Undici? Open a Q&A Discussion or join our official OpenJS Slack channel.

Looking to contribute? Start by reading the contributing guide

Install

npm i undici

Benchmarks

The benchmark is a simple getting data example using a 50 TCP connections with a pipelining depth of 10 running on Node 22.11.0.

┌────────────────────────┬─────────┬────────────────────┬────────────┬─────────────────────────┐ │ Tests │ Samples │ Result │ Tolerance │ Difference with slowest │ ├────────────────────────┼─────────┼────────────────────┼────────────┼─────────────────────────┤ │ 'axios' │ 15 │ '5708.26 req/sec' │ '± 2.91 %' │ '-' │ │ 'http - no keepalive' │ 10 │ '5809.80 req/sec' │ '± 2.30 %' │ '+ 1.78 %' │ │ 'request' │ 30 │ '5828.80 req/sec' │ '± 2.91 %' │ '+ 2.11 %' │ │ 'undici - fetch' │ 40 │ '5903.78 req/sec' │ '± 2.87 %' │ '+ 3.43 %' │ │ 'node-fetch' │ 10 │ '5945.40 req/sec' │ '± 2.13 %' │ '+ 4.15 %' │ │ 'got' │ 35 │ '6511.45 req/sec' │ '± 2.84 %' │ '+ 14.07 %' │ │ 'http - keepalive' │ 65 │ '9193.24 req/sec' │ '± 2.92 %' │ '+ 61.05 %' │ │ 'superagent' │ 35 │ '9339.43 req/sec' │ '± 2.95 %' │ '+ 63.61 %' │ │ 'undici - pipeline' │ 50 │ '13364.62 req/sec' │ '± 2.93 %' │ '+ 134.13 %' │ │ 'undici - stream' │ 95 │ '18245.36 req/sec' │ '± 2.99 %' │ '+ 219.63 %' │ │ 'undici - request' │ 50 │ '18340.17 req/sec' │ '± 2.84 %' │ '+ 221.29 %' │ │ 'undici - dispatch' │ 40 │ '22234.42 req/sec' │ '± 2.94 %' │ '+ 289.51 %' │ └────────────────────────┴─────────┴────────────────────┴────────────┴─────────────────────────┘

Quick Start

```js import { request } from 'undici'

const { statusCode, headers, trailers, body } = await request('http://localhost:3000/foo')

console.log('response received', statusCode) console.log('headers', headers)

for await (const data of body) { console.log('data', data) }

console.log('trailers', trailers) ```

Body Mixins

The body mixins are the most common way to format the request/response body. Mixins include:

[!NOTE] The body returned from undici.request does not implement .formData().

Example usage:

```js import { request } from 'undici'

const { statusCode, headers, trailers, body } = await request('http://localhost:3000/foo')

console.log('response received', statusCode) console.log('headers', headers) console.log('data', await body.json()) console.log('trailers', trailers) ```

Note: Once a mixin has been called then the body cannot be reused, thus calling additional mixins on .body, e.g. .body.json(); .body.text() will result in an error TypeError: unusable being thrown and returned through the Promise rejection.

Should you need to access the body in plain-text after using a mixin, the best practice is to use the .text() mixin first and then manually parse the text to the desired format.

For more information about their behavior, please reference the body mixin from the Fetch Standard.

Common API Methods

This section documents our most commonly used API methods. Additional APIs are documented in their own files within the docs folder and are accessible via the navigation list on the left side of the docs site.

undici.request([url, options]): Promise

Arguments:

  • url string | URL | UrlObject
  • options RequestOptions
    • dispatcher Dispatcher - Default: getGlobalDispatcher
    • method String - Default: PUT if options.body, otherwise GET

Returns a promise with the result of the Dispatcher.request method.

Calls options.dispatcher.request(options).

See Dispatcher.request for more details, and request examples for examples.

undici.stream([url, options, ]factory): Promise

Arguments:

  • url string | URL | UrlObject
  • options StreamOptions
    • dispatcher Dispatcher - Default: getGlobalDispatcher
    • method String - Default: PUT if options.body, otherwise GET
  • factory Dispatcher.stream.factory

Returns a promise with the result of the Dispatcher.stream method.

Calls options.dispatcher.stream(options, factory).

See Dispatcher.stream for more details.

undici.pipeline([url, options, ]handler): Duplex

Arguments:

  • url string | URL | UrlObject
  • options PipelineOptions
    • dispatcher Dispatcher - Default: getGlobalDispatcher
    • method String - Default: PUT if options.body, otherwise GET
  • handler Dispatcher.pipeline.handler

Returns: stream.Duplex

Calls options.dispatch.pipeline(options, handler).

See Dispatcher.pipeline for more details.

undici.connect([url, options]): Promise

Starts two-way communications with the requested resource using HTTP CONNECT.

Arguments:

  • url string | URL | UrlObject
  • options ConnectOptions
  • callback (err: Error | null, data: ConnectData | null) => void (optional)

Returns a promise with the result of the Dispatcher.connect method.

Calls options.dispatch.connect(options).

See Dispatcher.connect for more details.

undici.fetch(input[, init]): Promise

Implements fetch.

  • https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
  • https://fetch.spec.whatwg.org/#fetch-method

Basic usage example:

```js import { fetch } from 'undici'

const res = await fetch('https://example.com') const json = await res.json() console.log(json) ```

You can pass an optional dispatcher to fetch as:

```js import { fetch, Agent } from 'undici'

const res = await fetch('https://example.com', { // Mocks are also supported dispatcher: new Agent({ keepAliveTimeout: 10, keepAliveMaxTimeout: 10 }) }) const json = await res.json() console.log(json) ```

request.body

A body can be of the following types:

  • ArrayBuffer
  • ArrayBufferView
  • AsyncIterables
  • Blob
  • Iterables
  • String
  • URLSearchParams
  • FormData

In this implementation of fetch, request.body now accepts Async Iterables. It is not present in the Fetch Standard.

```js import { fetch } from 'undici'

const data = { async *Symbol.asyncIterator { yield 'hello' yield 'world' }, }

await fetch('https://example.com', { body: data, method: 'POST', duplex: 'half' }) ```

FormData besides text data and buffers can also utilize streams via Blob objects:

```js import { openAsBlob } from 'node:fs'

const file = await openAsBlob('./big.csv') const body = new FormData() body.set('file', file, 'big.csv')

await fetch('http://example.com', { method: 'POST', body }) ```

request.duplex

  • 'half'

In this implementation of fetch, request.duplex must be set if request.body is ReadableStream or Async Iterables, however, even though the value must be set to 'half', it is actually a full duplex. For more detail refer to the Fetch Standard.

response.body

Nodejs has two kinds of streams: web streams, which follow the API of the WHATWG web standard found in browsers, and an older Node-specific streams API. 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().

```js import { fetch } from 'undici' import { Readable } from 'node:stream'

const response = await fetch('https://example.com') const readableWebStream = response.body const readableNodeStream = Readable.fromWeb(readableWebStream) ```

Specification Compliance

This section documents parts of the Fetch Standard that Undici does not support or does not fully implement.

Garbage Collection
  • https://fetch.spec.whatwg.org/#garbage-collection

The Fetch Standard allows users to skip consuming the response body by relying on 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.

Garbage collection in Node is less aggressive and deterministic (due to the lack of clear idle periods that browsers have through the rendering refresh rate) which means that leaving the release of connection resources to the garbage collector can lead to excessive connection usage, reduced performance (due to less connection re-use), and even stalls or deadlocks when running out of connections.

```js // Do const headers = await fetch(url) .then(async res => { for await (const chunk of res.body) { // force consumption of body } return res.headers })

// Do not const headers = await fetch(url) .then(res => res.headers) ```

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 for more details.

js const headers = await fetch(url, { method: 'HEAD' }) .then(res => res.headers)

Forbidden and Safelisted Header Names
  • https://fetch.spec.whatwg.org/#cors-safelisted-response-header-name
  • https://fetch.spec.whatwg.org/#forbidden-header-name
  • https://fetch.spec.whatwg.org/#forbidden-response-header-name
  • https://github.com/wintercg/fetch/issues/6

The Fetch Standard 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.

undici.upgrade([url, options]): Promise

Upgrade to a different protocol. See MDN - HTTP - Protocol upgrade mechanism for more details.

Arguments:

  • url string | URL | UrlObject
  • options UpgradeOptions
  • callback (error: Error | null, data: UpgradeData) => void (optional)

Returns a promise with the result of the Dispatcher.upgrade method.

Calls options.dispatcher.upgrade(options).

See Dispatcher.upgrade for more details.

undici.setGlobalDispatcher(dispatcher)

  • dispatcher Dispatcher

Sets the global dispatcher used by Common API Methods.

undici.getGlobalDispatcher()

Gets the global dispatcher used by Common API Methods.

Returns: Dispatcher

undici.setGlobalOrigin(origin)

  • origin string | URL | undefined

Sets the global origin used in fetch.

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.

```js setGlobalOrigin('http://localhost:3000')

const response = await fetch('/api/ping')

console.log(response.url) // http://localhost:3000/api/ping ```

undici.getGlobalOrigin()

Gets the global origin used in fetch.

Returns: URL

UrlObject

  • port string | number (optional)
  • path string (optional)
  • pathname string (optional)
  • hostname string (optional)
  • origin string (optional)
  • protocol string (optional)
  • search string (optional)

Specification Compliance

This section documents parts of the HTTP/1.1 specification that Undici does not support or does not fully implement.

Expect

Undici does not support the Expect request header field. The request body is always immediately sent and the 100 Continue response will be ignored.

Refs: https://tools.ietf.org/html/rfc7231#section-5.1.1

Pipelining

Undici will only use pipelining if configured with a pipelining factor greater than 1. Also it is important to pass blocking: false to the request options to properly pipeline requests.

Undici always assumes that connections are persistent and will immediately pipeline requests, without checking whether the connection is persistent. Hence, automatic fallback to HTTP/1.0 or HTTP/1.1 without pipelining is not supported.

Undici will immediately pipeline when retrying requests after a failed connection. However, Undici will not retry the first remaining requests in the prior pipeline and instead error the corresponding callback/promise/stream.

Undici will abort all running requests in the pipeline when any of them are aborted.

  • Refs: https://tools.ietf.org/html/rfc2616#section-8.1.2.2
  • Refs: https://tools.ietf.org/html/rfc7230#section-6.3.2

Manual Redirect

Since it is not possible to manually follow an HTTP redirect on the server-side, Undici returns the actual response instead of an opaqueredirect filtered one when invoked with a manual redirect. This aligns fetch() with the other implementations in Deno and Cloudflare Workers.

Refs: https://fetch.spec.whatwg.org/#atomic-http-redirect-handling

Workarounds

Network address family autoselection.

If you experience problem when connecting to a remote server that is resolved by your DNS servers to a IPv6 (AAAA record) first, there are chances that your local router or ISP might have problem connecting to IPv6 networks. In that case undici will throw an error with code UND_ERR_CONNECT_TIMEOUT.

If the target server resolves to both a IPv6 and IPv4 (A records) address and you are using a compatible Node version (18.3.0 and above), you can fix the problem by providing the autoSelectFamily option (support by both undici.request and undici.Agent) which will enable the family autoselection algorithm when establishing the connection.

Collaborators

Releasers

License

MIT

Owner

  • Name: cy-suite
  • Login: cy-suite
  • Kind: organization

GitHub Events

Total
  • Public event: 1
Last Year
  • Public event: 1

Dependencies

.github/workflows/autobahn.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • actions/setup-node 60edb5dd545a775178f52524783378180af0d1f8 composite
  • docker://docker * composite
  • crossbario/autobahn-testsuite latest docker
.github/workflows/backport.yml actions
  • tibdex/backport v2 composite
.github/workflows/bench.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • actions/setup-node 60edb5dd545a775178f52524783378180af0d1f8 composite
.github/workflows/codeql.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • github/codeql-action/analyze 662472033e021d55d94146f66f6058822b0b39fd composite
  • github/codeql-action/autobuild 662472033e021d55d94146f66f6058822b0b39fd composite
  • github/codeql-action/init 662472033e021d55d94146f66f6058822b0b39fd composite
  • step-security/harden-runner 91182cccc01eb5e619899d80e4e971d6181294a7 composite
.github/workflows/nightly.yml actions
  • actions/github-script 60a0d83039c74a4aee543508d2ffcb1c3799cdea composite
.github/workflows/nodejs-shared.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • actions/github-script 60a0d83039c74a4aee543508d2ffcb1c3799cdea composite
  • actions/setup-node 60edb5dd545a775178f52524783378180af0d1f8 composite
  • hendrikmuhs/ccache-action ed74d11c0b343532753ecead8a951bb09bb34bc9 composite
.github/workflows/nodejs.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • actions/dependency-review-action 4081bf99e2866ebe428fc0477b69eb4fcda7220a composite
  • actions/github-script 60a0d83039c74a4aee543508d2ffcb1c3799cdea composite
  • actions/setup-node 60edb5dd545a775178f52524783378180af0d1f8 composite
  • fastify/github-action-merge-dependabot c3bde0759d4f24db16f7b250b2122bc2df57e817 composite
  • hendrikmuhs/ccache-action ed74d11c0b343532753ecead8a951bb09bb34bc9 composite
  • step-security/harden-runner 91182cccc01eb5e619899d80e4e971d6181294a7 composite
.github/workflows/release-create-pr.yml actions
  • actions/checkout v4 composite
  • actions/github-script v7 composite
  • actions/setup-node v4 composite
.github/workflows/release.yml actions
  • actions/checkout v4 composite
  • actions/github-script v7 composite
  • actions/setup-node v4 composite
.github/workflows/scorecard.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • actions/upload-artifact b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 composite
  • github/codeql-action/upload-sarif 662472033e021d55d94146f66f6058822b0b39fd composite
  • ossf/scorecard-action 62b2cac7ed8198b15735ed49ab1e5cf35480ba46 composite
.github/workflows/test.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • actions/setup-node 60edb5dd545a775178f52524783378180af0d1f8 composite
  • codecov/codecov-action b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 composite
.github/workflows/triggered-autobahn.yml actions
.github/workflows/update-wpt.yml actions
  • actions/checkout 9bb56186c3b09b4f86b1c65136769dd318469633 composite
  • peter-evans/create-pull-request 5e914681df9dc83aa4e4905692ca88beb2f9e91f composite
test/fixtures/wpt/common/META.yml cpan
test/fixtures/wpt/eventsource/META.yml cpan
test/fixtures/wpt/fetch/META.yml cpan
test/fixtures/wpt/fetch/fetch-later/META.yml cpan
test/fixtures/wpt/fetch/metadata/META.yml cpan
test/fixtures/wpt/fetch/private-network-access/META.yml cpan
test/fixtures/wpt/interfaces/META.yml cpan
test/fixtures/wpt/mimesniff/META.yml cpan
test/fixtures/wpt/resources/META.yml cpan
test/fixtures/wpt/service-workers/META.yml cpan
test/fixtures/wpt/service-workers/cache-storage/META.yml cpan
test/fixtures/wpt/storage/META.yml cpan
test/fixtures/wpt/storage/buckets/META.yml cpan
test/fixtures/wpt/websockets/META.yml cpan
test/fixtures/wpt/xhr/META.yml cpan
benchmarks/package.json npm
  • axios ^1.6.7
  • concurrently ^9.0.0
  • cronometro ^3.0.1
  • got ^14.2.0
  • mitata ^1.0.4
  • node-fetch ^3.3.2
  • request ^2.88.2
  • superagent ^10.0.0
  • wait-on ^8.0.0
docs/package.json npm
  • docsify-cli ^4.4.4
package.json npm
  • @fastify/busboy 3.0.0 development
  • @matteo.collina/tspl ^0.1.1 development
  • @sinonjs/fake-timers ^12.0.0 development
  • @types/node ^18.19.50 development
  • abort-controller ^3.0.0 development
  • borp ^0.18.0 development
  • c8 ^10.0.0 development
  • cross-env ^7.0.3 development
  • dns-packet ^5.4.0 development
  • esbuild ^0.24.0 development
  • eslint ^9.9.0 development
  • fast-check ^3.17.1 development
  • https-pem ^3.0.0 development
  • husky ^9.0.7 development
  • jest ^29.0.2 development
  • neostandard ^0.11.2 development
  • node-forge ^1.3.1 development
  • proxy ^2.1.1 development
  • tsd ^0.31.2 development
  • typescript ^5.6.2 development
  • ws ^8.11.0 development
test/fixtures/wpt/resources/test/requirements.txt pypi
  • html5lib ==1.1 test