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

esm: align sync and async load implementations #49152

Merged
merged 1 commit into from
Aug 20, 2023

Conversation

aduh95
Copy link
Contributor

@aduh95 aduh95 commented Aug 13, 2023

Refs: #48272
Refs: #47999

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/modules

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Aug 13, 2023
if (protocol === 'file:') {
source = readFileSync(url);
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
Copy link
Member

Choose a reason for hiding this comment

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

worth pulling this off of url on line 79, in case there's a getter, so as to unconditionally trigger it?

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

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

Example benefits:

  • a getter that throws will do so at the top of the method, before expensive stuff happens
  • algorithm changes won't be as observable - it would otherwise be part of the API of this to only do a Get of "pathname" in this branch, and to only do one total, so refactors could become unintentional breaking changes
  • it becomes much clearer what properties are accessed since they're all accessed once, statically, at the top of the method

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 href rather than the pathname.

  • a getter that throws will do so at the top of the method, before expensive stuff happens

It's still the first operation after the destructuring in that function.

  • algorithm changes won't be as observable - it would otherwise be part of the API of this to only do a Get of "pathname" in this branch, and to only do one total, so refactors could become unintentional breaking changes

You mean if we start needing pathname for other protocols? But a case could be made for the other properties of URL, what if we end up needing search or origin, should we get them as well just in case?

Copy link
Member

@ljharb ljharb Aug 14, 2023

Choose a reason for hiding this comment

The 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)

@GeoffreyBooth
Copy link
Member

What is the goal here?

@anonrig
Copy link
Member

anonrig commented Aug 13, 2023

What is the goal here?

Reduce unnecessary URL initializations.

Comment on lines +74 to 78
* @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) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @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) {
* @param {URL} urlInstance URL to the module
* @param {ESModuleContext} context used to decorate error messages
* @returns {{ responseURL: string, source: string | BufferView }}
*/
function getSourceSync(urlInstance, context) {

Copy link
Contributor

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).

Copy link
Contributor Author

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 url and not urlInstance as 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 PR

Copy link
Member

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 urlInstance somewhere else in the file, so we might want to standardize at some point.

Copy link
Contributor

@JakobJingleheimer JakobJingleheimer left a comment

Choose a reason for hiding this comment

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

nice improvement :)

@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 19, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 19, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@lpinca lpinca added the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 20, 2023
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 20, 2023
@nodejs-github-bot nodejs-github-bot merged commit 2557932 into nodejs:main Aug 20, 2023
37 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 2557932

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants