Live data from Hacker News

Fetch API has landed into Node.js

github.com

131–140 of 210 posts

Re: Fetch API has landed into Node.js

#131
post #4

Hey, Node core person here (and the person who triggered the land) - we're super excited for this and would love help and feedback. This is still experimental and we'd love to hear from the community what you'd like to see.

Hi there, > ...we'd love to hear from the community what you'd like to see. In our tests, fetch on Deno was wayy faster than undici fetch that now has been merged into nodejs. I couldn't figure out why that was case, but forwarding requests over node's http2 client (instead of undici fetch ) then had comparable (but not as fast) performance as Deno (presumably because our hand rolled impl lacked connection pooling).

This is very new and the implementation _just_ landed today so it makes sense Deno would be faster with an API they have been working on for years.

I am confident that Node's implementation will _eventually_ have comparable performance.

That said: please do open an issue in the undici repo at https://nodejs.org/node/undici so this gets tracked.

Re: Fetch API has landed into Node.js

#132
post #129
post #4

Hey, Node core person here (and the person who triggered the land) - we're super excited for this and would love help and feedback. This is still experimental and we'd love to hear from the community what you'd like to see.

Is there support for timeouts? It's the main reason I use https://github.com/sindresorhus/got

You can use timeouts through `AbortController/AbortSignal` - eventually it'll even be built in with `AbortSignal.timeout` which is currently under-works in the spec level.

Re: Fetch API has landed into Node.js

#133
post #72
post #4

Hey, Node core person here (and the person who triggered the land) - we're super excited for this and would love help and feedback. This is still experimental and we'd love to hear from the community what you'd like to see.

Where can I find an explanation about what is Fetch and why it should be in Node.js?

fetch is a "modern" (ES6 era, so not brand new) API for making HTTP requests, to replace XMLHttpRequest (the older method that IE pioneered).

It's promise based (thus easier to integrate into code than older callback styles), and designed to give some better options around CORS and handling responses.

> Why it should be in node?

There is a push to adopt some certain "web apis" (APIs that emerged in web browsers) to increase compatibility, reusability and reduce cognitive load when working with both backend and frontend javascript.

Having a common low level call like fetch() means that libraries that are built atop it (SDKs to talk to services, or apply common middleware like JSON:API, oAuth etc) can be shared between node/deno and browsers.

Re: Fetch API has landed into Node.js

#135
post #4

Hey, Node core person here (and the person who triggered the land) - we're super excited for this and would love help and feedback. This is still experimental and we'd love to hear from the community what you'd like to see.

Hi there, > ...we'd love to hear from the community what you'd like to see. In our tests, fetch on Deno was wayy faster than undici fetch that now has been merged into nodejs. I couldn't figure out why that was case, but forwarding requests over node's http2 client (instead of undici fetch ) then had comparable (but not as fast) performance as Deno (presumably because our hand rolled impl lacked connection pooling).

Deno's is probably written in rust via the tokio rust crate. This version of fetch in NodeJS is written in JavaScript. It would probably get upgraded to C++ in due time once it's mature enough.

Re: Fetch API has landed into Node.js

#136
post #112

Earlier quoted context omitted.

Really happy to see fetch() merged into core! Looking at the PR, what are those WASM blobs in Undici and how is it that Node.js accepts a random very large compiled blob instead of asking for the source file + compilation step? https://github.com/nodejs/node/commit/6ec225392675c92b102d3c...

Those wasm blobs are Node's own llhttp https://github.com/nodejs/llhttp in wasm to speed up HTTP parsing. The project itself added as a dependency is also by Node https://github.com/nodejs/undici . The question is totally legitimate but please assume core doesn't make "load random binary" level kind of goofs :)

Except that's exactly what Corepack does, just silently and opaquely on the user's system. At least, the moment it switches from opt-in to opt-out.

Its unverified binaries are just as good as random binaries, and silently replacing any pre-existing yarn/pnpm/whathaveyou without checking if it was perhaps a custom build is not exactly predictable either.

Other than Corepack, though, I think you people are doing an amazing job!

Re: Fetch API has landed into Node.js

#137
post #48
post #31

Earlier quoted context omitted.

First, this is awesome and congratulations! Second this sounds like a lot of work! Can you comment why was it so hard to add fetch to node, yet packages like “node-fetch” have existed for some time and seem to implement fetch rather easily? Only asking from a curiosity perspective.

node-fetch (which is great btw!) implements fetch "reasonably" as in stuff like `fetch('./foo').then(x => x.json())` works but it's very far from a "real" fetch. Some people argued users would still like it and there was an attempt[1] by Myles but at the end of the day - the changes were pretty big. For example: a response body in fetch is a web stream (a whole new stream type) and to cancel you use an AbortControlle…

THANKS!

Re: Fetch API has landed into Node.js

#138
post #78

Earlier quoted context omitted.

Browsers will happily accept `.mjs` files they don't actually care about file extensions only about `Content-Type` and type=module on the script tag :]

Plenty of (third-party) imports specifically reference "*.js" files so no, this doesn't work.

why are you writing node applications without using package.json?

it sounds like you're looking to have a problem with javascript tbh. don't use it, whatever

Re: Fetch API has landed into Node.js

#139
I love fetch, except for the way that it combines headers. If more than one of the same header is set (such as multiple set-cookie headers), it combines them all into a single comma-separated string. I know this is allowed in the HTTP spec, and it's probably even a sensible default. But the fetch spec doesn't allow any access to the raw headers, so there's no straightforward way to get the original uncombined headers back. Set-Cookie headers, in particular, often contain commas in their expiration dates, so just splitting around `, ` will lead to problems.

I took a look through the source of this new fetch API, and it seems to have inherited that wart: https://github.com/nodejs/undici/blob/2dd3437e20c5a3cc466226...

I've argued with the authors of the fetch spec about this before, and ultimately settled on using https://www.npmjs.com/package/set-cookie-parser#user-content... to work around this flaw. (For clarity, I published the package, but chrusart wrote that method - https://github.com/nfriedly/set-cookie-parser/pull/19)

Post reply on HN