Introducing Undici@4

Why Undici

A lot of people still ask us why building a replacement for Node.js core HTTP stack — while it works so well for them. The reality is that the Node.js core HTTP stack suffers from fundamental design issues that are impossible to overcome without breaking the API. There are certain bugs or performance bottlenecks we cannot fix without breaking the majority of our users — both on the client and server implementations as they are deeply tied.

Notable Changes

WASM Parser

Undici v3 used the internal Node.js HTTP parser through dynamic binding which has been deprecated in Node 16. To resolve this, and also further improve maintainability and performance, Undici has moved to a WASM build of llhttp (https://github.com/nodejs/llhttp). Using a wasm build also increased Undici’s performance.

Redirect Support

Undici will now automatically follow redirects for requests dispatched through the Agent and global API. As a user, you have to specify the `maxRedirections` option to a value greater than zero. Then, Undici will follow 302 and location headers!

Native Mock

Undici supports a native mocking layer that you can use to intercept any outgoing calls either in a pass-through or blocking form.

import { request, MockAgent, setGlobalDispatcher } from './index.js'const agent = new MockAgent()agent.disableNetConnect()
agent.get('http://localhost:3000')
.intercept({
path: '/foo',
method: 'GET'
})
.reply(200, 'hello world')
setGlobalDispatcher(agent)const {
statusCode,
headers,
trailers,
body
} = await request('http://localhost:3000/foo')
console.log('response received', statusCode)
console.log('headers', headers)
body.setEncoding('utf8')
for await (const data of body) {
console.log('data', data)
}
console.log('trailers', trailers)

Website and Documentation

Undici has a new documentation website: https://undici.nodejs.org/. It is built on docsify.

Dispatcher instead of Agent

The API of Undici has been unified so that Client, Pool, and Agent all implement the same and simpler Dispatcher API. This allows these different classes to be used interchangeably and also makes it simpler to extend Undici with custom behaviors while re-using existing functionality.

Support Node v12.x

As Node.js v10 ended its Long Term Support in April 2021, we are migrating the minimum supported Node.js version to v12.x.

What’s next?

Ethan has been working on https://github.com/Ethan-Arrowood/undici-fetch which is a WHATWG fetch implementation built on top of Undici. It is significantly faster than any of the alternatives of Node.js. Stay tuned for some news in this space!

Benchmarks

The benchmark is a simple hello world example using Unix sockets (connections) with a pipelining depth of 10 running on Node.js 16. The benchmarks have the simd feature enabled.

Thanks

Thanks to all the folks that contributed to this release. Undici brings a new life to the Node.js HTTP stack — innovating on some old but still actual technology. We welcome all contributions and there is plenty to do, check out our “good first issues” https://github.com/nodejs/undici/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Node.js

Node.js

117K Followers

Node.js is a collaborative open source project dedicated to building and supporting the Node.js platform. https://nodejs.org/en/