After just a few years because the first launch of Undici 3 years in the past, we’ve got gathered many contributors and plenty of new options. This little HTTP shopper that Matteo constructed as an experiment has shortly grown to be extraordinarily steady and performant, main the way in which in a renovated effort of bringing Node.js ahead.
This new launch brings many breaking adjustments each inside and exterior. The brand new web site (https://undici.nodejs.org/) exhibits all of the documentation: take your time to familiarize your self with the brand new APIs.
The total change is obtainable at: https://github.com/nodejs/undici/releases/tag/v4.0.0, right here we checklist a very powerful components.
Lots of people nonetheless ask us why constructing a alternative for Node.js core HTTP stack — whereas it really works so nicely for them. The fact is that the Node.js core HTTP stack suffers from basic design points which might be inconceivable to beat with out breaking the API. There are particular bugs or efficiency bottlenecks we can’t repair with out breaking nearly all of our customers — each on the shopper and server implementations as they’re deeply tied.
The staff behind Undici is dedicated to growing a quick, dependable, and spec-compliant HTTP shopper for Node.js. Undici is production-ready. Attempt it out!
Undici v3 used the interior Node.js HTTP parser by way of dynamic binding which has been deprecated in Node 16. To resolve this, and in addition additional enhance maintainability and efficiency, Undici has moved to a WASM construct of llhttp (https://github.com/nodejs/llhttp). Utilizing a wasm construct additionally elevated Undici’s efficiency.
Due to this function, we may leverage the brand new SIMD acceleration that’s coming to V8 (and Node.js) later this yr. You possibly can consider the distinction by beginning node with the--experimental-wasm-simd
possibility.
As a part of this variation, we’re additionally eradicating assist for the--insecure-http-parser
possibility.
Undici will now routinely comply with redirects for requests dispatched by way of the Agent and world API. As a person, you need to specify the `maxRedirections` choice to a price larger than zero. Then, Undici will comply with 302 and placement headers!
Undici helps a local mocking layer that you need to use to intercept any outgoing calls both in a pass-through or blocking kind.
import { request, MockAgent, setGlobalDispatcher } from './index.js'const agent = new MockAgent()agent.disableNetConnect()
agent.get('http://localhost:3000')
.intercept({
path: '/foo',
methodology: 'GET'
})
.reply(200, 'hey world')setGlobalDispatcher(agent)const {
statusCode,
headers,
trailers,
physique
} = await request('http://localhost:3000/foo')console.log('response acquired', statusCode)
console.log('headers', headers)physique.setEncoding('utf8')
for await (const information of physique) {
console.log('information', information)
}console.log('trailers', trailers)
The mock system was drastically impressed by nock.
Undici has a brand new documentation web site: https://undici.nodejs.org/. It’s constructed on docsify.
The API of Undici has been unified in order that Shopper, Pool, and Agent all implement the identical and easier Dispatcher API. This permits these totally different lessons for use interchangeably and in addition makes it easier to increase Undici with customized behaviors whereas re-using present performance.
As Node.js v10 ended its Lengthy Time period Help in April 2021, we’re migrating the minimal supported Node.js model to v12.x.
Ethan has been engaged on https://github.com/Ethan-Arrowood/undici-fetch which is a WHATWG fetch implementation constructed on high of Undici. It’s considerably sooner than any of the options of Node.js. Keep tuned for some information on this area!
Resume work on the HTTP/2 assist. Including it to Undici is vital as the brand new design could possibly be tailored to deal with all variations of HTTP: 1.1, 2 and three when it might be obtainable.
The benchmark is a straightforward hey world instance utilizing Unix sockets (connections) with a pipelining depth of 10 working on Node.js 16. The benchmarks have the simd function enabled.
Due to all the oldsters that contributed to this launch. Undici brings a brand new life to the Node.js HTTP stack — innovating on some previous however nonetheless precise expertise. We welcome all contributions and there may be lots to do, try our “good first points” https://github.com/nodejs/undici/points?q=ispercent3Aopen+ispercent3Aissue+labelpercent3Apercent22good+first+issuepercent22.