Introduction to Undici
The Undici project is an HTTP/1.1 client written specifically for Node.js, aiming to provide a high-performance interface for making HTTP requests. Named after the Italian word for eleven ('Undici'), reflecting the HTTP/1.1 version that it supports, this client offers an alternative to the built-in http
module in Node.js.
Features and Benefits
Undici boasts several features that make it an attractive choice for developers needing to perform HTTP requests in their Node.js applications:
-
Performance: Undici demonstrates superior performance compared to other HTTP clients available in Node.js, as evidenced by its benchmark results which showcase its ability to handle more requests per second.
-
Fetch API Compliance: Adhering to the Fetch Standard, Undici includes methods like
fetch()
, which developers familiar with the Fetch API in the browser will recognize and be able to use seamlessly in a Node.js environment. -
Streaming and Pipelining: The client supports HTTP pipelining, allowing multiple requests to be sent out without waiting for the corresponding responses, as well as the ability to work efficiently with streams.
-
Garbage Collection Considerations: Given Node.js's less aggressive garbage collection compared to browsers, Undici recommends manually consuming response bodies to avoid issues such as excessive connection usage or deadlocks.
Installation and Usage
Installing Undici is straightforward, and it can be done using package managers like npm. Once installed, importing and using it is relatively simple, with methods available to send HTTP requests (undici.request
), stream responses (undici.stream
), and work with upgraded HTTP protocols (undici.upgrade
). Here's a basic example of how to perform a GET request and print the response:
import { request } from 'undici';
const { statusCode, headers, body, trailers } = 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);
Advanced Features
Apart from its basic usage, Undici provides several advanced features:
-
Body Mixins: Simplify the process of consuming response bodies by providing methods like
.json()
,.text()
, and.formData()
. -
Global Dispatcher: Configure a global dispatcher to manage how requests are made across an application.
-
Specification Compliance: While aiming to comply with HTTP/1.1 specifications, Undici also documents any deviations or unsupported features, such as the 'Expect' header.
-
Workarounds: For example, network address family autoselection can be controlled using the
autoSelectFamily
option inundici.request
or theundici.Agent
class.
Collaborators and Licensing
Undici benefits from the contributions of a community of collaborators, including notable individuals such as Matteo Collina and Robert Nagy, among others. The project is available under the MIT license, allowing for permissive free usage and contribution.
Conclusion
In summary, Node.js developers in search of a high-performance HTTP client that complies with the Fetch Standard may find Undici to be an excellent fit. Its fast performance, support for streaming and pipelining, and feature set aimed at both ease of use and compliance with standards make it a competitive choice in the landscape of Node.js HTTP clients.
Tags: #Undici #Nodejs #HTTPClient #FetchAPI #Performance