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

feat(ses): Support importHook alias returns #432

Merged
merged 1 commit into from
Aug 26, 2020

Conversation

kriskowal
Copy link
Member

This change enables importHook to return a record for a module that has a different specifier than the requested module specifier. This enables the Node.js pattern of redirecting name to name/index.js and respecting the latter name as the referrer for its own imports.

@kriskowal kriskowal requested a review from erights August 25, 2020 00:41
@kriskowal kriskowal mentioned this pull request Aug 25, 2020
36 tasks
@kriskowal kriskowal force-pushed the kris/ses-import-hook-alias branch 2 times, most recently from a34836d to e5e6609 Compare August 25, 2020 21:50
Copy link
Contributor

@erights erights left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +186 to +187
and return a module that has a different "response specifier" than the original
"request specifier".
Copy link
Contributor

Choose a reason for hiding this comment

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

I really like the terminology "request specifier" and "response specifier"


```js
const importHook = async specifier => {
const candidates = [specifier, specifier + ".js", specifier + "/index.js"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const candidates = [specifier, specifier + ".js", specifier + "/index.js"];
const candidates = [specifier, `${specifier}.js`, `${specifier}/index.js`];

Only if you agree it is at least as clear.

const importHook = async specifier => {
const candidates = [specifier, specifier + ".js", specifier + "/index.js"];
for (const candidate of candidates) {
const record = wrappedImportHook(candidate).catch(_ => undefined);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need an await?

Comment on lines +188 to +189
The `importHook` may return an "alias" envelope with `alias`, `compartment`,
and `module` properties.
Copy link
Contributor

Choose a reason for hiding this comment

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

The example code below instead returns a record with alias and specifier properties?

What does "envelope" mean here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Revised to avoid “envelope” term.

Comment on lines +236 to +242
const moduleMapHook = moduleSpecifier => {
if (moduleSpecifier === 'even') {
return even.module('./index.js');
} else if (moduleSpecifier === 'odd') {
return odd.module('./index.js');
}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Our old even/odd friend, I missed you.

// Await all dependencies to load, recursively.
await Promise.all(
values(resolvedImports).map(fullSpecifier =>
// Behold: recursion.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wish "Behold: recursion." was the eslint directive ;)

Actually, I'll take this opportunity to ask: Do you think the Agoric style should turn this warning off by default?

Copy link
Member Author

Choose a reason for hiding this comment

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

It’s a rare enough problem that it doesn’t bother me.

I don’t think I’ve ever used recursion accidentally, though.

The directive does force an opinionated order. In this particular case, these functions tend to tail-recur so they’re actually clearer to read in reverse declaration order.

I have no strong feelings.


const makeImportHook = makeNodeImporter({
'https://example.com/main/index.js': `
export const unique = {n: Math.random()};
Copy link
Contributor

Choose a reason for hiding this comment

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

Really vivid way to test this. I like it.


// TODO The following commented test does not pass, and might not be valid.
// Web browsers appear to have taken the stance that they will load a static
// module record once per *response url* and create unique a unique module
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// module record once per *response url* and create unique a unique module
// module record once per *response url* and create a unique module

@kriskowal kriskowal force-pushed the kris/ses-import-hook-alias branch 2 times, most recently from cf7b21e to 8b4bee3 Compare August 26, 2020 18:25
This change enables importHook to return a record for a module that has a different specifier than the requested module specifier.  This enables the Node.js pattern of redirecting `name` to `name/index.js` and respecting the latter name as the referrer for its own imports.
Base automatically changed from kris/ses-compartment-names to kris/endo-mitm August 26, 2020 19:02
@kriskowal kriskowal merged commit 93a7f21 into kris/endo-mitm Aug 26, 2020
@kriskowal kriskowal deleted the kris/ses-import-hook-alias branch August 26, 2020 19:02
kriskowal added a commit that referenced this pull request Aug 26, 2020
This change enables importHook to return a record for a module that has a different specifier than the requested module specifier.  This enables the Node.js pattern of redirecting `name` to `name/index.js` and respecting the latter name as the referrer for its own imports.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants