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(compartment-map): Decouple discovery and languages #2306

Merged
merged 9 commits into from
Jun 4, 2024

Conversation

kriskowal
Copy link
Member

@kriskowal kriskowal commented Jun 3, 2024

Refs: #400 and #2294

Description

In this change, we carve and export -lite.js and -parsers.js out of import.js, archive.js, and import-archive.js, as well revealing mapNodeModules through @endo/compartment-mapper/node-modules.js revealing hidden flexibility already present in the Compartment Mapper. This allows the Compartment Mapper to mix and match “language behaviors” with different workflows (e.g., using import-parsers.js with archive-lite.js instead of archive-parsers.js generates archives with original sources instead of precompiled sources.) We also decouple the process of discovering the physical compartments such that node-modules.js can be substituted for alternate compartment exploration algorithms, e.g., combining a custom lockfile and package cache from a specific package management solution.

Security Considerations

Does not cross security considerations.

Scaling Considerations

Exporting more narrowly scoped modules allows consumers to be avoid entraining Babel in a greater variety of scenarios.

Documentation Considerations

Evolution of the reference documentation should suffice, though another pass at README.md to dig into these more surgical and composable APIs may be worthwhile.

Testing Considerations

The existing scenarios are extensive and fully cover the behaviors that we’ve factored out. I expect #2294 tests to cover the new scenarios.

Compatibility Considerations

This change takes great care to preserve the existing behavior of the composite import.js, archive.js, and import-archive.js interfaces, only revealing existing behaviors that were previously internal, allowing more expressible scenarios.

Upgrade Considerations

This change will not affect upgrade and paves a migration path forward for preserving backward-compatibility for bundleSource and importBundle as we draw closer to supporting XS native compartments.

@kriskowal kriskowal force-pushed the kriskowal-compartment-map-lite branch from 756c062 to 1d02471 Compare June 3, 2024 23:18
@kriskowal kriskowal marked this pull request as ready for review June 3, 2024 23:19
@kriskowal kriskowal requested a review from boneskull June 3, 2024 23:19
@kriskowal
Copy link
Member Author

Advise reviewing commits individually. I took some care to rename and edit in separate steps.

Copy link
Collaborator

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

if I'm being fussy, I'd want to see, for each of these new files, a module-level comment describing the module's use-case. and descriptions for each of the exported functions.

probably needs a NEWS.md update.

there's nothing here that I'd block about.

@@ -28,8 +28,14 @@
"default": "./index.js"
},
"./import.js": "./import.js",
"./import-lite.js": "./import-lite.js",
Copy link
Collaborator

Choose a reason for hiding this comment

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

(suggestion/observation/trolling) there's obviously a convention at play here, but I'd omit the .js from the keys

Copy link
Member Author

Choose a reason for hiding this comment

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

I very much want to. It’s not just convention, though. This makes the Compartment Mapper continue to work when used on some older foundations like node -r esm that do not read the "exports" directive, so we can’t use it for aliases, just to limit exposure. We have one lingering integration test that needs to be replaced before we can drop this pattern.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Didn't realize that esm was a target

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be reasonable to provide both extension-laden and extension-free exports?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that’s reasonable when after eliminate standardthings/esm from our toolchain. Until then, better to encourage usage patterns that are portable.

packages/compartment-mapper/src/archive.js Show resolved Hide resolved
packages/compartment-mapper/src/import-archive.js Outdated Show resolved Hide resolved
Comment on lines 15 to 40
// Have to give it a name to capture the external meaning of Compartment
// Otherwise @param {typeof Compartment} takes the Compartment to mean
// the const variable defined within the function.
//
Copy link
Collaborator

Choose a reason for hiding this comment

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

(suggestion) IMO it isn't necessary to explain this, but nbd. I would probably even just zap the @typedef altogether and use typeof Compartment in the parseArchive signature's docstring

Copy link
Member Author

Choose a reason for hiding this comment

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

Using typedef Compartment doesn’t work for some reason, thus the comment. My preference as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@kriskowal FWIW I cannot reproduce whatever the problem was that you ran into. This compiles cleanly for me:

/**
 * @typedef {object} Options
 * @property {string} [expectedSha512]
 * @property {HashFn} [computeSha512]
 * @property {Record<string, unknown>} [modules]
 * @property {ExitModuleImportHook} [importHook]
 * @property {typeof Compartment} [Compartment]
 * @property {ComputeSourceLocationHook} [computeSourceLocation]
 * @property {ComputeSourceMapLocationHook} [computeSourceMapLocation]
 * @property {ParserForLanguage} [parserForLanguage]
 */

Copy link
Collaborator

Choose a reason for hiding this comment

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

that said, it is a little weird that we are expecting the Compartment constructor here. there is only one Compartment constructor. is there a place where we're actually using a different one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, there are Compartment compatible constructors created by wrapping the Compartment constructor. Notably, Compartment before lockdown is different than after. Every Compartment has its own instance of Compartment. They’re structurally compatible, though. Unclear to me why that would make a difference to types, but they do have different values.

Copy link
Collaborator

@boneskull boneskull Jun 4, 2024

Choose a reason for hiding this comment

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

even though TS is structural, because Compartment is a function, it will only ever allow the global Compartment from whichever version of ses provides it. That may be fine, but also seems redundant. Though it may be useful if we're using some mocked-out Compartment in tests? even then it seems like it would be the same Compartment constructor

Copy link
Member Author

Choose a reason for hiding this comment

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

src/import-archive-lite.js:266:4 - error TS2502: 'Compartment' is referenced directly or indirectly in its own type annotation.

266  * @param {typeof Compartment} [options.Compartment]

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah, I see that now. It's because of the DefaultCompartment. typeof DefaultCompartment works

packages/compartment-mapper/src/import.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

if I'm being fussy, I'd want to see, for each of these new files, a module-level comment describing the module's use-case. and descriptions for each of the exported functions.

probably needs a NEWS.md update.

there's nothing here that I'd block about.

@kriskowal kriskowal force-pushed the kriskowal-compartment-map-lite branch 2 times, most recently from 27e7d9c to 68ddb71 Compare June 4, 2024 05:58
Copy link
Collaborator

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

mo tags mo problems

@@ -28,8 +28,14 @@
"default": "./index.js"
},
"./import.js": "./import.js",
"./import-lite.js": "./import-lite.js",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Didn't realize that esm was a target

packages/compartment-mapper/src/archive-parsers.js Outdated Show resolved Hide resolved
packages/compartment-mapper/src/link.js Outdated Show resolved Hide resolved
@kriskowal kriskowal force-pushed the kriskowal-compartment-map-lite branch from 68ddb71 to 3872e35 Compare June 4, 2024 18:38
@kriskowal kriskowal force-pushed the kriskowal-compartment-map-lite branch from 3872e35 to d241fcb Compare June 4, 2024 18:41
@kriskowal kriskowal requested a review from boneskull June 4, 2024 18:42
Copy link
Collaborator

@boneskull boneskull left a comment

Choose a reason for hiding this comment

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

:shipit:

@kriskowal kriskowal merged commit e74e977 into master Jun 4, 2024
17 checks passed
@kriskowal kriskowal deleted the kriskowal-compartment-map-lite branch June 4, 2024 19:25
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.

None yet

2 participants