-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
esm: throw on any non-2xx response #43742
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,7 +148,9 @@ function fetchWithRedirects(parsed) { | |
err.message = `Cannot find module '${parsed.href}', HTTP 404`; | ||
throw err; | ||
} | ||
if (res.statusCode < 200 || res.statusCode >= 400) { | ||
// This condition catches all unsupported status codes, including | ||
// 3xx redirection codes without `Location` HTTP header. | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
throw new ERR_NETWORK_IMPORT_DISALLOWED( | ||
Comment on lines
+153
to
154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you thinking correcting the error be handled separately/subsequently? I think it would be better to create helpers for these, like Kidonng just added with if (!isStatusOk(res.statsuCode)) { // keeping with fetch's response.ok
throw new ERR_NETWORK_IMPORT_BAD_RESPONSE(…); I think If you'd rather that also be a follow-up, sure, I can do. A little troubling that no test needed to be updated for this 😨 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why deviate? AFAICT web browsers would accept any response with a 2xx status code as valid (as long as it has the correct Repro// server.cjs
const http = require('node:http');
http.createServer((req, res) => {
if (/^\/\d+$/.test(req.url))
res.writeHead(Number(req.url.slice(1)), { 'Content-Type': 'text/javascript' });
res.end();
}).listen(8000); Then open http://localhost:8000 on the web browser you want to test and paste the following in the DevTools JS console: Promise.allSettled(Array.from({length: 99}, (_, i) => import(`/${i+200}`))).then(console.log) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean what reason would we have for deviating, or why would using a different name be deviating? I'm not proposing different status codes, merely word-smithing the name of the potential helper ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, I don't understand what you mean by "because 200 = OK, and response.ok is any 2xx", or how that's a justification for preferring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Noooo ;)
Fetch's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll be happy with any outcome that will be most appropriate according to "big picture" of this subsystem. My personal preference for this part is to see it rewritten using As for naming and helper functions, my personal preference for Regarding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oow, actually, that's an excellent point. Is there any reason we can't port this to actual fetch? Would we run into any cache contamination? 🤔 CORS would suddenly come into the picture. Fetch didn't exist yet in Node when this was written, so that discussion hasn't already happened AFAIK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, porting this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I vaguely remember that it was but I couldn't tell you the rationale at the time. |
||
res.headers.location, | ||
parsed.href, | ||
|
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 think an explanation for this
if
condition would be helpful. Is this the correct reason?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.
Added a comment via 640b85d.
I think, this condition can be read properly and unambiguously as "we need 2xx code", so there is no difference between
1xx
,3xx
,5xx
or any invalid number.What we might need to point out is that isRedirect check above handled only machine-readable redirects with
Location
, so we want to filter them again. If this part will be refactored into somevalidateStatusCode
that throws meaningful errors for different coderanges, we don't want these to be omitted.