Introduction to Undici
Undici is an HTTP/1.1 client specifically built for Node.js. The name "Undici" translates to "eleven" in Italian and has a playful origin, referencing HTTP 1.1 as 11 and drawing inspiration from "Stranger Things."
Getting Involved with Undici
For those interested in using Undici, the community invites you to join the project's official OpenJS Slack channel or open a Q&A discussion on their GitHub page. If you're looking to contribute to the project, starting with the contributing guide on their GitHub repository is recommended.
Installation
To start using Undici, you can install it via npm with the following command:
npm i undici
Performance Benchmarks
Undici demonstrates notable performance with its HTTP client features. Benchmarks reveal Undici's efficiency in handling requests, comparing favorably against traditional Node.js HTTP clients like axios, node-fetch, and superagent.
- For instance, Undici's pipeline method can handle up to 15,053.41 requests per second, surpassing many other libraries.
- It also excels in scenarios involving sending data, with features like
undici.request
reaching up to 6,397 requests per second.
Quick Start
Here's a quick example of how to perform an HTTP request using Undici:
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
Undici supports several mixins to handle request and response bodies in various formats, including:
.arrayBuffer()
.blob()
.bytes()
.json()
.text()
These mixins help in processing response bodies efficiently, adhering to the Fetch Standard. However, it's important to note that once a mixin is called, the body cannot be reused, meaning making successive mixin calls like .body.json(); .body.text()
will cause errors.
Common API Methods
Undici offers several API methods to suit different request-handling needs, like:
undici.request
: Performs HTTP requests.undici.stream
: Handles streaming requests and responses.undici.pipeline
: Used for pipelined HTTP requests.undici.connect
: Initiates two-way communication with the HTTP CONNECT method.undici.fetch
: Provides a fetch-like API compatible with the Fetch Standard.
Specification Compliance and Workarounds
While Undici strives for extensive specification compliance, it has some limitations, such as not supporting the Expect
header. In protocols requiring pipelining or manual redirects, Undici applies specific workarounds and considerations to ensure performance and stability.
Community and Development
Development and maintenance for Undici involve a dedicated group of collaborators, with developers like Daniele Belardi, Ethan Arrowood, and Matteo Collina leading the project. They encourage contributions and provide support for developers interested in using or enhancing Undici's functionality.
Undici represents a modern, high-performance HTTP client for Node.js applications, equipped to handle contemporary web communication needs with efficiency and flexibility.