-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
esm: align sync and async load implementations #49152
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
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 |
|---|---|---|
|
|
@@ -70,25 +70,30 @@ async function getSource(url, context) { | |
| return { __proto__: null, responseURL, source }; | ||
| } | ||
|
|
||
| /** | ||
| * @param {URL} url URL to the module | ||
| * @param {ESModuleContext} context used to decorate error messages | ||
| * @returns {{ responseURL: string, source: string | BufferView }} | ||
| */ | ||
| function getSourceSync(url, context) { | ||
| const parsed = new URL(url); | ||
| const responseURL = url; | ||
| const { protocol, href } = url; | ||
| const responseURL = href; | ||
| let source; | ||
| if (parsed.protocol === 'file:') { | ||
| source = readFileSync(parsed); | ||
| } else if (parsed.protocol === 'data:') { | ||
| const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname); | ||
| if (protocol === 'file:') { | ||
| source = readFileSync(url); | ||
| } else if (protocol === 'data:') { | ||
| const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname); | ||
|
Member
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. worth pulling this off of
Member
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. +1
Contributor
Author
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. That seems wasteful, I'm not sure I understand what would be the upside of doing so
Member
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. Example benefits:
Contributor
Author
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. If we really care about this, I think we should rather adapt the regex to take the
It's still the first operation after the destructuring in that function.
You mean if we start needing
Member
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. yep (to be clear, i don't mean get them now just in case, i mean as soon as they're used once in the function, get them unconditionally at the top) |
||
| if (!match) { | ||
| throw new ERR_INVALID_URL(url); | ||
| throw new ERR_INVALID_URL(responseURL); | ||
| } | ||
| const { 1: base64, 2: body } = match; | ||
| source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8'); | ||
| } else { | ||
| const supportedSchemes = ['file', 'data']; | ||
| throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes); | ||
| throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes); | ||
| } | ||
| if (policy?.manifest) { | ||
| policy.manifest.assertIntegrity(parsed, source); | ||
| policy.manifest.assertIntegrity(url, source); | ||
| } | ||
| return { __proto__: null, responseURL, source }; | ||
| } | ||
|
|
@@ -159,14 +164,18 @@ function defaultLoadSync(url, context = kEmptyObject) { | |
| source, | ||
| } = context; | ||
|
|
||
| format ??= defaultGetFormat(new URL(url), context); | ||
| const urlInstance = new URL(url); | ||
|
|
||
| throwIfUnsupportedURLScheme(urlInstance, false); | ||
|
|
||
| format ??= defaultGetFormat(urlInstance, context); | ||
|
|
||
| validateAssertions(url, format, importAssertions); | ||
|
|
||
| if (format === 'builtin') { | ||
| source = null; | ||
| } else if (source == null) { | ||
| ({ responseURL, source } = getSourceSync(url, context)); | ||
| ({ responseURL, source } = getSourceSync(urlInstance, context)); | ||
| } | ||
|
|
||
| return { | ||
|
|
||
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.
nit: I think that is not necessary because the code doc already identifies it as type
URL, and I think (aside from mistakenly constructing a new instance), the difference likely would not matter because it's castable to a string.That said, I do sometimes do similar things, like
fooRgx, so I'm not opposed to it (just pointing out that info is already available).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.
Another thing to consider is that the async implementation also uses
urland noturlInstanceas a name, and since this PR goal is to align both implementation, that seems to be another reason not to take that suggestion in that PRThere 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 don't really care, you can feel free to disregard. I suggested it because I think I saw we called this
urlInstancesomewhere else in the file, so we might want to standardize at some point.