Visitar URL original
lib: add fetch by targos · Pull Request #41749 · nodejs/node · GitHub
The Wayback Machine - https://web.archive.org/web/20220130192207/https://github.com/nodejs/node/pull/41749
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add fetch #41749

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

lib: add fetch #41749

wants to merge 3 commits into from

Conversation

@targos
Copy link
Member

@targos targos commented Jan 29, 2022 •

Refs: nodejs/undici#1183

/cc @nodejs/undici

This adds a --experimental-fetch flag that installs the fetch, Request, Response and Headers globals.

@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Jan 29, 2022

@ronag
Copy link
Member

@ronag ronag commented Jan 29, 2022

@ronag ronag requested review from jasnell and mcollina Jan 29, 2022
aduh95
aduh95 approved these changes Jan 29, 2022
Copy link
Contributor

@aduh95 aduh95 left a comment

Should we include WPT tests as well?

lib/internal/bootstrap/pre_execution.js Outdated Show resolved Hide resolved
@targos
Copy link
Member Author

@targos targos commented Jan 29, 2022

Should we include WPT tests as well?

I'll try. And I also need to add a few parallel tests as well.

@targos
Copy link
Member Author

@targos targos commented Jan 29, 2022

Note: We need to document the 4 globals that are added by this flag in globals.md. Anyone feel free to help on that front.

@aduh95
Copy link
Contributor

@aduh95 aduh95 commented Jan 29, 2022

Note: We need to document the 4 globals that are added by this flag in globals.md. Anyone feel free to help on that front.

Doing it right now. Should we convert to draft in the mean time?

@targos targos marked this pull request as draft Jan 29, 2022
@devsnek
Copy link
Member

@devsnek devsnek commented Jan 29, 2022

:shipit:

@targos
Copy link
Member Author

@targos targos commented Jan 29, 2022 •

WPT blocked on #41749 (comment) should be fixed

@targos
Copy link
Member Author

@targos targos commented Jan 29, 2022

I'm not going to add WPT in this PR. I tried and there is a massive amount of errors, many of them because the tests expect to be run in a web browser (they use fetch('../some/relative/path') and other Web APIs that we don't provide).

doc/node.1 Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

lgtm

@mcollina
Copy link
Member

@mcollina mcollina commented Jan 29, 2022

I added a baking-for-lts label on this one.

@@ -68,12 +68,11 @@ if (process.argv.length === 2 &&
!process.env.NODE_SKIP_FLAG_CHECK &&
isMainThread &&
hasCrypto &&
require.main &&
Copy link
Member Author

@targos targos Jan 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there another way to know whether node was run with a file entry point? require.main doesn't exist in ESM and this change doesn't work for all tests.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@targos targos Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but I don't think I can use this.
test/common/index.js is not the entry point (and it's not ESM either). import chain is:

  • node test/parallel/something.mjs (ESM)
  • import '../common/index.mjs' (ESM)
  • import './index.js' (CJS)

Can I find out from common/index.js that the entry point was a file (ESM or CJS, I don't need to know in this case)?

Example of an entry point that is not a file:

  • node -e "require('./test/parallel/sometest.js')"

Copy link
Member Author

@targos targos Jan 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced require.main with fs.existsSync(process.argv[1])

Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood left a comment

🚀 incredible work - excited to see this land. Fetch may finally land in core 🙌

Copy link
Member

@jasnell jasnell left a comment

Looks good so far. Would prefer more tests and WPT to land with it but understand if those aren't ready.

@aduh95
Copy link
Contributor

@aduh95 aduh95 commented Jan 29, 2022

I added a baking-for-lts label on this one.

I would be in favor not to wait to backport it to LTS, since it lands as flagged I don't think it represents a big risk and could allow us to gather more feedback.

@mcollina
Copy link
Member

@mcollina mcollina commented Jan 29, 2022

Let's bring it to v16.

doc/api/globals.md Show resolved Hide resolved
doc/api/globals.md Show resolved Hide resolved
@devsnek
Copy link
Member

@devsnek devsnek commented Jan 30, 2022

i think saying it doesn't comply is kind of a strawman argument. "call it something else because it doesn't support cors" there's no origin in node anyway, so it doesn't matter, it's a category error. If it helps you, you can imagine a behavior for each of the things it doesn't support that technically makes it compliant. for example, it supports cors but the origin is a wildcard, or it supports cookies but the cookie jar is always empty. it doesn't matter.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

If it’s called fetch, i, and others, will assume that code written in the browser with fetch works in node. If it doesn’t, then it’s confusing.

Why can’t we just pick another name for it?

@devsnek
Copy link
Member

@devsnek devsnek commented Jan 30, 2022

i think the point is that the code does work?

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

If it doesn’t work identically - and as discussed, it can’t ever - then it’s not the same thing. “good enough” is fine for a library, but that kind of sloppiness shouldn’t be appropriate for node.

@devsnek
Copy link
Member

@devsnek devsnek commented Jan 30, 2022 •

@ljharb i think it does work identically for the constructed environment, and this is what i mean by a category error. you can complain about having no cookies but if you take fetch code and run it in a compliant browser you can get the same behavior, by it being run on a site without any cookies. if you take fetch code and run it in a compliant browser you can see examples of it both using cors preflight checks and not using preflight checks, by whether the request url is to the same origin as the site or not.

if you have code examples that demonstrate the breakage you're concerned about i would really appreciate you providing them so we can discuss those on a technical level instead of discussing theoreticals.

@aduh95
Copy link
Contributor

@aduh95 aduh95 commented Jan 30, 2022 •

if you have code examples that demonstrate the breakage you're concerned about i would really appreciate you providing them so we can discuss those on a technical level instead of discussing theoreticals.

Even you we have such code examples, I don't think it should block this PR: this is labeled as experimental, and exposed behind a flag. It could be a blocker for unflagging, but for now it's fine imo.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

@devsnek i understand that many use cases will Just Work. I also agree that this functionality is important and valuable. I just don’t understand why everyone’s attached to the name “fetch” when it has baggage, and when we could literally pick any other synonym and avoid that baggage.

I can certainly dive into the spec and come up with some code that behaves differently - but I’m not sure why we need to have a technical discussion when it’s just a naming issue.

@devsnek
Copy link
Member

@devsnek devsnek commented Jan 30, 2022

i'm personally more concerned about the practical usage than about the to-the-letter implementation of the whatwg spec, and i feel comfortable with that because we've had many many years of node-fetch working fine as a drop-in replacement. i think the only actual thing that will come out of giving it a different name is code that looks like (fetch || whateverNodeNameFetch)(), i.e. people will still not have any problems with using them interchangeably but they'll need to paper over the different names.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

The practical usage - even intending it to be a drop-in replacement for fetch for the subset of uses it works for - works just fine with a different name.

@jasnell
Copy link
Member

@jasnell jasnell commented Jan 30, 2022

works just fine with a different name.

Works just fine with the name fetch also. You've made the point several times now that you disagree with the name because it's not 100% like the browser implementation. A number of us have countered that it's never expected to be 100% like the browser implementation but still prefer the name fetch for a number of reasons.

At this point, I don't think it's going to do much good to keep arguing the same point. Do you have a concrete alternative suggestion for a name? With a concrete suggestion we can then open it for an informal vote to see what folks prefer.

@devsnek
Copy link
Member

@devsnek devsnek commented Jan 30, 2022

i don't like wrapping contention into flags but tbh if your only concern really is the name of the global, maybe it is better to hold that discussion for the unflagging.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

@jasnell i still haven’t heard any reasons to prefer the name fetch other than inertia. Can you elaborate?

@Ethan-Arrowood
Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood commented Jan 30, 2022

The major motivation for this work is the community request for "fetch in node". The users themselves have directly asked for it to be called fetch and that's what we are delivering

@ronag
Copy link
Member

@ronag ronag commented Jan 30, 2022

I mean part of the reason we want to add this is psychology in our user base. It’s bad marketing wise that we don’t have fetch. Calling it something else kind of kills IMHO one of the primary reasons we want to add it.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

@Ethan-Arrowood theyve asked for the semantics; and unless there’s reasons provided for why it needs that name, then i fail to see the point of using it.

@ronag i understand that. How come non-compliance doesn’t kill it in the same way? Spec compliance is binary; it’s either 100% compliant or it’s violating the spec.

@ronag
Copy link
Member

@ronag ronag commented Jan 30, 2022 •

As far as I’m concerned it will be 100% spec compliant. We have some bugs to sort out but other than that I think it’s there. I’m not sure what you are referring to.

@mariotacke
Copy link

@mariotacke mariotacke commented Jan 30, 2022

I have no qualms with the name fetch, however I wonder why this is will be a global? What's the reasoning for making it global vs. a module import?

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

@ronag my understanding is that since node doesn’t have concepts like “the current url” or cookies, nor obey the same-origin restriction, that it’s simply not achievable.

@aduh95
Copy link
Contributor

@aduh95 aduh95 commented Jan 30, 2022 •

I have no qualms with the name fetch, however I wonder why this is will be a global? What's the reasoning for making it global vs. a module import?

Browser compatibility, like we did for AbortController, AbortSignal, Blob,BroadcastChannel, console, URL, URLSearchParams, DOMException, TextEncoder, TextDecoder, EventTarget, Event, MessageChannel, MessagePort, MessagePort, atob, btoa, clearInterval, clearTimeout, setInterval, setTimeout, queueMicrotask, structuredClone, and performance. The counter examples would be Worker and WebCrypto.
We could still decide to make it available via a core module if the web compatibility is not achievable, but it's too soon to decide yet.

@ronag
Copy link
Member

@ronag ronag commented Jan 30, 2022

@ronag my understanding is that since node doesn’t have concepts like “the current url” or cookies, nor obey the same-origin restriction, that it’s simply not achievable.

I’m not sure what part of the spec we can’t implement due to that. They would just be “empty”. Can you reference any part of the spec which would be a problem?

@benjamingr
Copy link
Member

@benjamingr benjamingr commented Jan 30, 2022 •

For what it's worth WHATWG have happily worked with Deno (like they are always useful in these issues when we implement specs) to help them figure out what in fetch they can and can't implement and WHATWG PRs to fetch even Ping Deno now (and us for EventTarget/AbortSignal) for feedback e.g. whatwg/fetch@84c726a

So it's not like WHATWG is opposed to implementing fetch without CORS or anything.

That said: I think it is way too early to bring them in since our implementation here is in the "literally first PR to add it under a flag" phase and is very far from the state I'd feel comfortable asking them to take their time to look at stuff.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

@ronag i will be happy to dive into the spec and do the work to confirm, but first I’d want to know what the objective criteria would be to invalidate the name “fetch”. Give me a goal node can commit to, and I’ll either achieve it or confirm that i can’t.

@jasnell
Copy link
Member

@jasnell jasnell commented Jan 30, 2022

Absent any concrete alternative naming suggestions we can consider, I'm -1 on renaming this and +1 on landing as is.

@benjamingr
Copy link
Member

@benjamingr benjamingr commented Jan 30, 2022 •

@ljharb

I think a reasonable goal for unflagging would be to conform to the spec as much as possible except where it doesn't make sense for a server-side implementation (the points mentioned above: CORS, relative paths and GC mostly).

However I would definitely like us to talk to whatwg before that (and after we have a lot more tests).

I am also happy to ask Deno folks about it. I'll go ahead and open an issue.

Edit: opened a Deno issue: denoland/deno#13541

Edit: list of Deno deviations https://deno.land/manual@29346ec07971a376530a75dedbc329eafbb10d79/runtime/web_platform_apis#spec-deviations

@GeoffreyBooth
Copy link
Member

@GeoffreyBooth GeoffreyBooth commented Jan 30, 2022

I'm a strong +1 on landing a global fetch with as close spec compliance as is reasonable. I want to be able to share code between frontend and backend, and only having fetch as a global achieves that. Having a global fetch that implements the most commonly used parts of the spec also allows me to import most browser libraries that use fetch. Global fetch is a major win for interoperability.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

@jasnell tbh “request” seems like a good concrete suggestion.

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 30, 2022

Interopability is only achieved when things are 100% compliant. Otherwise, it’s just a bug farm - where you’ll be able to import something you think will work, because the global is present, but it silently won’t in nonzero cases.

@benjamingr
Copy link
Member

@benjamingr benjamingr commented Jan 30, 2022

Otherwise, it’s just a bug farm - where you’ll be able to import something you think will work, because the global is present, but it silently won’t in nonzero cases.

Maybe @jasnell can share if this has been an issue with cloudflare workers or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment