Package Exports
- undici
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (undici) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
undici
A 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 a Q&A Discussion or join our official OpenJS Slack channel.
Install
npm i undici
Benchmarks
Machine: AMD EPYC 7502P
Node 15
http - keepalive x 12,028 ops/sec ±2.60% (265 runs sampled)
undici - pipeline x 31,321 ops/sec ±0.77% (276 runs sampled)
undici - request x 36,612 ops/sec ±0.71% (277 runs sampled)
undici - stream x 41,291 ops/sec ±0.90% (268 runs sampled)
undici - dispatch x 47,319 ops/sec ±1.17% (263 runs sampled)
The benchmark is a simple hello world
example using a
single unix socket with pipelining.
Quick Start
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)
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 | object
- options
RequestOptions
- dispatcher
Dispatcher
- Default: getGlobalDispatcher - method
String
- Default:PUT
ifoptions.body
, otherwiseGET
- maxRedirections
Integer
- Default:0
- dispatcher
Returns a promise with the result of the Dispatcher.request
method.
url
may contain pathname. options
may not contain path.
Calls options.dispatcher.request(options)
.
See Dispatcher.request for more details.
undici.stream([url, options, ]factory): Promise
Arguments:
- url
string | URL | object
- 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 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 | object
- options
PipelineOptions
- dispatcher
Dispatcher
- Default: getGlobalDispatcher - method
String
- Default:PUT
ifoptions.body
, otherwiseGET
- dispatcher
- 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 | object
- options
ConnectOptions
- dispatcher
Dispatcher
- Default: getGlobalDispatcher
- dispatcher
- 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.upgrade([url, options]): Promise
Upgrade to a different protocol. See MDN - HTTP - Protocol upgrade mechanism for more details.
Arguments:
- url
string | URL | object
- options
UpgradeOptions
- dispatcher
Dispatcher
- Default: getGlobalDispatcher
- dispatcher
- 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 global API methods.
undici.getGlobalDispatcher()
Gets the global dispatcher used by global API methods.
Returns: Dispatcher
Specification Compliance
This section documents parts of the HTTP/1.1 specification which 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
Uncidi will only use pipelining if configured with a pipelining
factor
greater than 1
.
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 afters 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
Collaborators
- Ethan Arrowood, https://www.npmjs.com/~ethan_arrowood
- Daniele Belardi, https://www.npmjs.com/~dnlup
- Tomas Della Vedova, https://www.npmjs.com/~delvedor
- Matteo Collina, https://www.npmjs.com/~matteo.collina
- Robert Nagy, https://www.npmjs.com/~ronag
Releasers
License
MIT