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
base: master
Are you sure you want to change the base?
lib: add fetch #41749
Conversation
|
Review requested: |
I'll try. And I also need to add a few parallel tests as well. |
|
Note: We need to document the 4 globals that are added by this flag in |
Doing it right now. Should we convert to draft in the mean time? |
|
|
|
|
|
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 |
|
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 && | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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')"
There was a problem hiding this comment.
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])
Looks good so far. Would prefer more tests and WPT to land with it but understand if those aren't ready.
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. |
|
Let's bring it to v16. |
|
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. |
|
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? |
|
i think the point is that the code does work? |
|
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. |
|
@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. |
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. |
|
@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. |
|
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 |
|
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. |
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. |
|
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. |
|
@jasnell i still haven’t heard any reasons to prefer the name fetch other than inertia. Can you elaborate? |
|
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 |
|
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. |
|
@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. |
|
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. |
|
I have no qualms with the name |
|
@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. |
Browser compatibility, like we did for |
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? |
|
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. |
|
@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. |
|
Absent any concrete alternative naming suggestions we can consider, I'm -1 on renaming this and +1 on landing as is. |
|
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 |
|
I'm a strong +1 on landing a global |
|
@jasnell tbh “request” seems like a good concrete suggestion. |
|
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. |
Maybe @jasnell can share if this has been an issue with cloudflare workers or not? |


Refs: nodejs/undici#1183
/cc @nodejs/undici
This adds a
--experimental-fetchflag that installs thefetch,Request,ResponseandHeadersglobals.The text was updated successfully, but these errors were encountered: