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.
Have a question about using Undici? Open aQ&A Discussionor join our official OpenJSSlackchannel.
Looking to contribute? Start by reading thecontributing guide
npm i undici
The benchmark is a simple getting dataexampleusing a 50 TCP connections with a pipelining depth of 10 running on Node 20.10.0.
Tests | Samples | Result | Tolerance | Difference with slowest |
---|---|---|---|---|
undici - fetch | 30 | 3704.43 req/sec | ± 2.95 % | - |
http - no keepalive | 20 | 4275.30 req/sec | ± 2.60 % | + 15.41 % |
node-fetch | 10 | 4759.42 req/sec | ± 0.87 % | + 28.48 % |
request | 40 | 4803.37 req/sec | ± 2.77 % | + 29.67 % |
axios | 45 | 4951.97 req/sec | ± 2.88 % | + 33.68 % |
got | 10 | 5969.67 req/sec | ± 2.64 % | + 61.15 % |
superagent | 10 | 9471.48 req/sec | ± 1.50 % | + 155.68 % |
http - keepalive | 25 | 10327.49 req/sec | ± 2.95 % | + 178.79 % |
undici - pipeline | 10 | 15053.41 req/sec | ± 1.63 % | + 306.36 % |
undici - request | 10 | 19264.24 req/sec | ± 1.74 % | + 420.03 % |
undici - stream | 15 | 20317.29 req/sec | ± 2.13 % | + 448.46 % |
undici - dispatch | 10 | 24883.28 req/sec | ± 1.54 % | + 571.72 % |
The benchmark is a simple sending dataexampleusing a 50 TCP connections with a pipelining depth of 10 running on Node 20.10.0.
Tests | Samples | Result | Tolerance | Difference with slowest |
---|---|---|---|---|
undici - fetch | 20 | 1968.42 req/sec | ± 2.63 % | - |
http - no keepalive | 25 | 2330.30 req/sec | ± 2.99 % | + 18.38 % |
node-fetch | 20 | 2485.36 req/sec | ± 2.70 % | + 26.26 % |
got | 15 | 2787.68 req/sec | ± 2.56 % | + 41.62 % |
request | 30 | 2805.10 req/sec | ± 2.59 % | + 42.50 % |
axios | 10 | 3040.45 req/sec | ± 1.72 % | + 54.46 % |
superagent | 20 | 3358.29 req/sec | ± 2.51 % | + 70.61 % |
http - keepalive | 20 | 3477.94 req/sec | ± 2.51 % | + 76.69 % |
undici - pipeline | 25 | 3812.61 req/sec | ± 2.80 % | + 93.69 % |
undici - request | 10 | 6067.00 req/sec | ± 0.94 % | + 208.22 % |
undici - stream | 10 | 6391.61 req/sec | ± 1.98 % | + 224.71 % |
undici - dispatch | 10 | 6397.00 req/sec | ± 1.48 % | + 224.98 % |
import{request}from'undici'
const{
statusCode,
headers,
trailers,
body
}=awaitrequest('http://localhost:3000/foo')
console.log('response received',statusCode)
console.log('headers',headers)
forawait(constdataofbody){console.log('data',data)}
console.log('trailers',trailers)
Thebody
mixins are the most common way to format the request/response body. Mixins include:
Note
The body returned fromundici.request
does not implement.formData()
.
Example usage:
import{request}from'undici'
const{
statusCode,
headers,
trailers,
body
}=awaitrequest('http://localhost:3000/foo')
console.log('response received',statusCode)
console.log('headers',headers)
console.log('data',awaitbody.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 errorTypeError: unusable
being thrown and returned through thePromise
rejection.
Should you need to access thebody
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 theFetch Standard.
This section documents our most commonly used API methods. Additional APIs are documented in their own files within thedocsfolder and are accessible via the navigation list on the left side of the docs site.
Arguments:
- url
string | URL | UrlObject
- options
RequestOptions
- dispatcher
Dispatcher
- Default:getGlobalDispatcher - method
String
- Default:PUT
ifoptions.body
,otherwiseGET
- dispatcher
Returns a promise with the result of theDispatcher.request
method.
Callsoptions.dispatcher.request(options)
.
SeeDispatcher.requestfor more details, andrequest examplesfor examples.
Arguments:
- url
string | URL | UrlObject
- options
StreamOptions
- dispatcher
Dispatcher
- Default:getGlobalDispatcher - method
String
- Default:PUT
ifoptions.body
,otherwiseGET
- dispatcher
- factory
Dispatcher.stream.factory
Returns a promise with the result of theDispatcher.stream
method.
Callsoptions.dispatcher.stream(options, factory)
.
SeeDispatcher.streamfor more details.
Arguments:
- url
string | URL | UrlObject
- options
PipelineOptions
- dispatcher
Dispatcher
- Default:getGlobalDispatcher - method
String
- Default:PUT
ifoptions.body
,otherwiseGET
- dispatcher
- handler
Dispatcher.pipeline.handler
Returns:stream.Duplex
Callsoptions.dispatch.pipeline(options, handler)
.
SeeDispatcher.pipelinefor more details.
Starts two-way communications with the requested resource usingHTTP CONNECT.
Arguments:
- url
string | URL | UrlObject
- options
ConnectOptions
- dispatcher
Dispatcher
- Default:getGlobalDispatcher
- dispatcher
- callback
(err: Error | null, data: ConnectData | null) => void
(optional)
Returns a promise with the result of theDispatcher.connect
method.
Callsoptions.dispatch.connect(options)
.
SeeDispatcher.connectfor more details.
Implementsfetch.
- https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
- https://fetch.spec.whatwg.org/#fetch-method
Basic usage example:
import{fetch}from'undici'
constres=awaitfetch('https://example ')
constjson=awaitres.json()
console.log(json)
You can pass an optional dispatcher tofetch
as:
import{fetch,Agent}from'undici'
constres=awaitfetch('https://example ',{
// Mocks are also supported
dispatcher:newAgent({
keepAliveTimeout:10,
keepAliveMaxTimeout:10
})
})
constjson=awaitres.json()
console.log(json)
A body can be of the following types:
- ArrayBuffer
- ArrayBufferView
- AsyncIterables
- Blob
- Iterables
- String
- URLSearchParams
- FormData
In this implementation of fetch,request.body
now acceptsAsync Iterables
.It is not present in theFetch Standard.
import{fetch}from'undici'
constdata={
async*[Symbol.asyncIterator](){
yield'hello'
yield'world'
},
}
awaitfetch('https://example ',{body:data,method:'POST',duplex:'half'})
FormDatabesides text data and buffers can also utilize streams viaBlobobjects:
import{openAsBlob}from'node:fs'
constfile=awaitopenAsBlob('./big.csv')
constbody=newFormData()
body.set('file',file,'big.csv')
awaitfetch('http://example ',{method:'POST',body})
'half'
In this implementation of fetch,request.duplex
must be set ifrequest.body
isReadableStream
orAsync Iterables
,however, even though the value must be set to'half'
,it is actually afullduplex. For more detail refer to theFetch Standard..
Nodejs has two kinds of streams:web streams,which follow the API of the WHATWG web standard found in browsers, and an older Node-specificstreams 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()
.
import{fetch}from'undici'
import{Readable}from'node:stream'
constresponse=awaitfetch('https://example ')
constreadableWebStream=response.body
constreadableNodeStream=Readable.fromWeb(readableWebStream)
This section documents parts of theFetch Standardthat Undici does not support or does not fully implement.
TheFetch Standardallows users to skip consuming the response body by relying on garbage collectionto 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.
// Do
constheaders=awaitfetch(url)
.then(asyncres=>{
forawait(constchunkofres.body){
// force consumption of body
}
returnres.headers
})
// Do not
constheaders=awaitfetch(url)
.then(res=>res.headers)
However, if you want to get only headers, it might be better to useHEAD
request method. Usage of this method will obviate the need for consumption or cancelling of the response body. SeeMDN - HTTP - HTTP request methods - HEADfor more details.
constheaders=awaitfetch(url,{method:'HEAD'})
.then(res=>res.headers)
- 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
- wintercg/fetch#6
TheFetch Standardrequires 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.
Upgrade to a different protocol. SeeMDN - HTTP - Protocol upgrade mechanismfor more details.
Arguments:
- url
string | URL | UrlObject
- options
UpgradeOptions
- dispatcher
Dispatcher
- Default:getGlobalDispatcher
- dispatcher
- callback
(error: Error | null, data: UpgradeData) => void
(optional)
Returns a promise with the result of theDispatcher.upgrade
method.
Callsoptions.dispatcher.upgrade(options)
.
SeeDispatcher.upgradefor more details.
- dispatcher
Dispatcher
Sets the global dispatcher used by Common API Methods.
Gets the global dispatcher used by Common API Methods.
Returns:Dispatcher
- origin
string | URL | undefined
Sets the global origin used infetch
.
Ifundefined
is passed, the global origin will be reset. This will causeResponse.redirect
,new Request()
,andfetch
to throw an error when a relative path is passed.
setGlobalOrigin('http://localhost:3000')
constresponse=awaitfetch('/api/ping')
console.log(response.url)// http://localhost:3000/api/ping
Gets the global origin used infetch
.
Returns:URL
- port
string | number
(optional) - path
string
(optional) - pathname
string
(optional) - hostname
string
(optional) - origin
string
(optional) - protocol
string
(optional) - search
string
(optional)
This section documents parts of the HTTP/1.1 specification that Undici does not support or does not fully implement.
Undici does not support theExpect
request header field. The request
body is always immediately sent and the100 Continue
response will be
ignored.
Refs:https://tools.ietf.org/html/rfc7231#section-5.1.1
Undici will only use pipelining if configured with apipelining
factor
greater than1
.
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
Since it is not possible to manually follow an HTTP redirect on the server-side,
Undici returns the actual response instead of anopaqueredirect
filtered one
when invoked with amanual
redirect. This alignsfetch()
with the other
implementations in Deno and Cloudflare Workers.
Refs:https://fetch.spec.whatwg.org/#atomic-http-redirect-handling
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 codeUND_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 theautoSelectFamily
option (support by bothundici.request
andundici.Agent
) which will enable the family autoselection algorithm when establishing the connection.
- Daniele Belardi,https:// npmjs /~dnlup
- Ethan Arrowood,https:// npmjs /~ethan_arrowood
- Matteo Collina,https:// npmjs /~matteo.collina
- Matthew Aitken,https:// npmjs /~khaf
- Robert Nagy,https:// npmjs /~ronag
- Szymon Marczak,https:// npmjs /~szmarczak
- Tomas Della Vedova,https:// npmjs /~delvedor
- Ethan Arrowood,https:// npmjs /~ethan_arrowood
- Matteo Collina,https:// npmjs /~matteo.collina
- Robert Nagy,https:// npmjs /~ronag
- Matthew Aitken,https:// npmjs /~khaf
MIT