Skip to content

Commit

Permalink
fix(compartment-mapper): Thread compartment name to all exit import h…
Browse files Browse the repository at this point in the history
…ooks
  • Loading branch information
kriskowal committed Nov 8, 2024
1 parent 169b9fa commit abd1ddd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/compartment-mapper/src/archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ const digestFromMap = async (powers, compartmentMap, options = {}) => {
const consolidatedExitModuleImportHook = exitModuleImportHookMaker({
modules: exitModules,
exitModuleImportHook,
entryCompartmentName,
});

const makeImportHook = makeImportHookMaker(read, entryCompartmentName, {
Expand Down
1 change: 1 addition & 0 deletions packages/compartment-mapper/src/capture-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ export const captureFromMap = async (powers, compartmentMap, options = {}) => {
const consolidatedExitModuleImportHook = exitModuleImportHookMaker({
modules: exitModules,
exitModuleImportHook,
entryCompartmentName,
});

const makeImportHook = makeImportHookMaker(read, entryCompartmentName, {
Expand Down
23 changes: 14 additions & 9 deletions packages/compartment-mapper/src/import-archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ const makeArchiveImportHookMaker = (
moduleSpecifier,
)} was not in the archive and an attempt was made to load it as a builtin`,
});
const record = await exitModuleImportHook(moduleSpecifier);
const record = await exitModuleImportHook(
moduleSpecifier,
packageLocation,
);
if (record) {
// note it's not being marked as exit in sources
// it could get marked and the second pass, when the archive is being executed, would have the data
Expand Down Expand Up @@ -267,11 +270,6 @@ export const parseArchive = async (
assign(create(null), languageForExtensionOption),
);

const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
});

const archive = new ZipReader(archiveBytes, { name: archiveLocation });

// Track all modules that get loaded, all files that are used.
Expand Down Expand Up @@ -316,9 +314,15 @@ export const parseArchive = async (

const {
compartments,
entry: { module: moduleSpecifier },
entry: { module: entryModuleSpecifier, compartment: entryCompartmentName },
} = compartmentMap;

const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
entryCompartmentName,
});

// Archive integrity checks: ensure every module is pre-loaded so its hash
// gets checked, and ensure that every file in the archive is used, and
// therefore checked.
Expand Down Expand Up @@ -351,7 +355,7 @@ export const parseArchive = async (

await pendingJobsPromise;

await compartment.load(moduleSpecifier);
await compartment.load(entryModuleSpecifier);
unseen.size === 0 ||
Fail`Archive contains extraneous files: ${q([...unseen])} in ${q(
archiveLocation,
Expand All @@ -372,6 +376,7 @@ export const parseArchive = async (
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
entryCompartmentName,
});
const makeImportHook = makeArchiveImportHookMaker(
get,
Expand All @@ -397,7 +402,7 @@ export const parseArchive = async (
await pendingJobsPromise;

// eslint-disable-next-line dot-notation
return compartment['import'](moduleSpecifier);
return compartment['import'](entryModuleSpecifier);
};

return { import: execute, sha512 };
Expand Down
14 changes: 11 additions & 3 deletions packages/compartment-mapper/src/import-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,15 @@ const findRedirect = ({

/**
* @param {object} params
* @param {Record<string, any>=} params.modules
* @param {Record<string, any>} [params.modules]
* @param {ExitModuleImportHook} [params.exitModuleImportHook]
* @param {string} params.entryCompartmentName
* @returns {ExitModuleImportHook|undefined}
*/
export const exitModuleImportHookMaker = ({
modules = undefined,
exitModuleImportHook = undefined,
entryCompartmentName,
}) => {
if (!modules && !exitModuleImportHook) {
return undefined;
Expand All @@ -207,7 +209,10 @@ export const exitModuleImportHookMaker = ({
});
}
if (exitModuleImportHook) {
return exitModuleImportHook(specifier);
// The entryCompartmentName is a file URL when constructing an archive or
// importing from a file system, but is merely a zip archive root
// directory name when importing from an archive.
return exitModuleImportHook(specifier, entryCompartmentName);
}
return undefined;
};
Expand Down Expand Up @@ -518,7 +523,10 @@ export const makeImportHookMaker = (
// we allow importing any exit.
if (moduleSpecifier !== '.' && !moduleSpecifier.startsWith('./')) {
if (exitModuleImportHook) {
const record = await exitModuleImportHook(moduleSpecifier);
const record = await exitModuleImportHook(
moduleSpecifier,
packageLocation,
);
if (record) {
// It'd be nice to check the policy before importing it, but we can only throw a policy error if the
// hook returns something. Otherwise, we need to fall back to the 'cannot find' error below.
Expand Down
1 change: 1 addition & 0 deletions packages/compartment-mapper/src/import-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => {
const compartmentExitModuleImportHook = exitModuleImportHookMaker({
modules,
exitModuleImportHook,
entryCompartmentName,
});
const makeImportHook = makeImportHookMaker(
readPowers,
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/types/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,12 @@ type ModuleTransformResult = {

export type ExitModuleImportHook = (
specifier: string,
packageLocation: string,
) => Promise<ThirdPartyStaticModuleInterface | undefined>;

export type ExitModuleImportNowHook = (
specifier: string,
packageLocation: string,
) => ThirdPartyStaticModuleInterface | undefined;

export type ComputeSourceLocationHook = (
Expand Down

0 comments on commit abd1ddd

Please sign in to comment.