Tuesday, March 21, 2023
HomeNodejsIntroducing Undici@4. After just a few years because the first… | by...

Introducing Undici@4. After just a few years because the first… | by Node.js


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.

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.

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)

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!

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments