From b057700f53c1a152a38b267e48e2a9e21ab5f76f Mon Sep 17 00:00:00 2001 From: naugtur Date: Mon, 17 Apr 2023 13:27:54 +0200 Subject: [PATCH] chore(compartment-mapper): rename exitModuleImportHook to importHook in compartment-mapper public API --- .../compartment-mapper/demo/policy/index.mjs | 6 ++--- packages/compartment-mapper/src/archive.js | 6 ++--- .../compartment-mapper/src/import-archive.js | 24 +++++++------------ packages/compartment-mapper/src/import.js | 6 ++--- packages/compartment-mapper/src/policy.js | 7 +----- packages/compartment-mapper/src/types.js | 4 ++-- .../compartment-mapper/test/test-exit-hook.js | 4 ++-- .../compartment-mapper/test/test-policy.js | 2 +- 8 files changed, 23 insertions(+), 36 deletions(-) diff --git a/packages/compartment-mapper/demo/policy/index.mjs b/packages/compartment-mapper/demo/policy/index.mjs index fc36fc93fc..1fb2b743ec 100644 --- a/packages/compartment-mapper/demo/policy/index.mjs +++ b/packages/compartment-mapper/demo/policy/index.mjs @@ -93,7 +93,7 @@ const options = { process, setTimeout, }, - exitModuleImportHook: async specifier => { + importHook: async specifier => { const ns = await import(specifier); return Object.freeze({ imports: [], @@ -125,7 +125,7 @@ console.log('\n\n________________________________________________ Archive\n'); const archive = await makeArchive(readPower, entrypointPath, { modules: options.modules, policy: options.policy, - exitModuleImportHook: options.exitModuleImportHook, + importHook: options.importHook, }); console.log('>----------makeArchive -> parseArchive'); const application = await parseArchive(archive, '', { @@ -135,7 +135,7 @@ console.log('\n\n________________________________________________ Archive\n'); const { namespace } = await application.import({ globals: options.globals, modules: options.modules, - exitModuleImportHook: options.exitModuleImportHook, + importHook: options.importHook, }); console.log('>----------import -> end'); console.log(2, namespace.poem); diff --git a/packages/compartment-mapper/src/archive.js b/packages/compartment-mapper/src/archive.js index 8d9975ec84..f0f32d8a16 100644 --- a/packages/compartment-mapper/src/archive.js +++ b/packages/compartment-mapper/src/archive.js @@ -300,7 +300,7 @@ const digestLocation = async (powers, moduleLocation, options) => { captureSourceLocation = undefined, searchSuffixes = undefined, commonDependencies = undefined, - exitModuleImportHook = undefined, + importHook: exitModuleImportHook = undefined, policy = undefined, } = options || {}; const { read, computeSha512 } = unpackReadPowers(powers); @@ -336,7 +336,7 @@ const digestLocation = async (powers, moduleLocation, options) => { /** @type {Sources} */ const sources = Object.create(null); - const internalExitModuleImportHook = exitModuleImportHookMaker({ + const compartmentExitModuleImportHook = exitModuleImportHookMaker({ modules: exitModules, exitModuleImportHook, }); @@ -346,7 +346,7 @@ const digestLocation = async (powers, moduleLocation, options) => { baseLocation: packageLocation, sources, compartmentDescriptors: compartments, - exitModuleImportHook: internalExitModuleImportHook, + exitModuleImportHook: compartmentExitModuleImportHook, archiveOnly: true, computeSha512, searchSuffixes, diff --git a/packages/compartment-mapper/src/import-archive.js b/packages/compartment-mapper/src/import-archive.js index dbb97acb44..9c2666c7c3 100644 --- a/packages/compartment-mapper/src/import-archive.js +++ b/packages/compartment-mapper/src/import-archive.js @@ -70,14 +70,6 @@ const postponeErrorToExecute = errorMessage => { return record; }; -// /** -// * @callback ArchiveImportHookMaker -// * @param {string} packageLocation -// * @param {string} packageName -// * @param {DeferredAttenuatorsProvider} attenuators -// * @returns {ImportHook} -// */ - /** * @param {(path: string) => Uint8Array} get * @param {Record} compartments @@ -234,7 +226,7 @@ const makeFeauxModuleExportsNamespace = Compartment => { }; // Have to give it a name to capture the external meaning of Compartment -// Otherwise @param {typeof COmpartment} takes the Compartment to mean +// Otherwise @param {typeof Compartment} takes the Compartment to mean // the const variable defined within the function. /** @typedef {typeof Compartment} CompartmentConstructor */ @@ -245,7 +237,7 @@ const makeFeauxModuleExportsNamespace = Compartment => { * @param {string} [options.expectedSha512] * @param {HashFn} [options.computeSha512] * @param {Record} [options.modules] - * @param {ExitModuleImportHook} [options.exitModuleImportHook] + * @param {ExitModuleImportHook} [options.importHook] * @param {CompartmentConstructor} [options.Compartment] * @param {ComputeSourceLocationHook} [options.computeSourceLocation] * @returns {Promise} @@ -261,10 +253,10 @@ export const parseArchive = async ( computeSourceLocation = undefined, Compartment = DefaultCompartment, modules = undefined, - exitModuleImportHook = undefined, + importHook: exitModuleImportHook = undefined, } = options; - const internalExitModuleImportHook = exitModuleImportHookMaker({ + const compartmentExitModuleImportHook = exitModuleImportHookMaker({ modules, exitModuleImportHook, }); @@ -326,7 +318,7 @@ export const parseArchive = async ( archiveLocation, computeSha512, computeSourceLocation, - internalExitModuleImportHook, + compartmentExitModuleImportHook, ); // A weakness of the current Compartment design is that the `modules` map // must be given a module namespace object that passes a brand check. @@ -360,10 +352,10 @@ export const parseArchive = async ( transforms, __shimTransforms__, Compartment, - exitModuleImportHook, + importHook: exitModuleImportHook, } = options || {}; - const internalExitModuleImportHook = exitModuleImportHookMaker({ + const compartmentExitModuleImportHook = exitModuleImportHookMaker({ modules, exitModuleImportHook, }); @@ -373,7 +365,7 @@ export const parseArchive = async ( archiveLocation, computeSha512, computeSourceLocation, - internalExitModuleImportHook, + compartmentExitModuleImportHook, ); const { compartment, pendingJobsPromise } = link(compartmentMap, { makeImportHook, diff --git a/packages/compartment-mapper/src/import.js b/packages/compartment-mapper/src/import.js index e3dc34d47b..c22c387095 100644 --- a/packages/compartment-mapper/src/import.js +++ b/packages/compartment-mapper/src/import.js @@ -79,9 +79,9 @@ export const loadLocation = async (readPowers, moduleLocation, options) => { transforms, __shimTransforms__, Compartment, - exitModuleImportHook, + importHook: exitModuleImportHook, } = options; - const internalExitModuleImportHook = exitModuleImportHookMaker({ + const compartmentExitModuleImportHook = exitModuleImportHookMaker({ modules, exitModuleImportHook, }); @@ -89,7 +89,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => { readPowers, baseLocation: packageLocation, compartmentDescriptors: compartmentMap.compartments, - exitModuleImportHook: internalExitModuleImportHook, + exitModuleImportHook: compartmentExitModuleImportHook, archiveOnly: false, searchSuffixes, }); diff --git a/packages/compartment-mapper/src/policy.js b/packages/compartment-mapper/src/policy.js index 1644fab77b..03dd5fa146 100644 --- a/packages/compartment-mapper/src/policy.js +++ b/packages/compartment-mapper/src/policy.js @@ -419,12 +419,7 @@ async function attenuateModule({ exports: originalModuleRecord.exports, execute: (moduleExports, compartment, resolvedImports) => { const ns = {}; - originalModuleRecord.execute(ns, compartment, resolvedImports); // TODO: fix typing - - // TODO: attenuator being async forces us to call original execute out of order before returning the record. - // one solution is to make the attenuator itself synchronous. - // we could make attenuator accept and return a record, so that it can remain asynchronous while execute could be called in time. - + originalModuleRecord.execute(ns, compartment, resolvedImports); const attenuated = attenuate(ns); moduleExports.default = attenuated; assign(moduleExports, attenuated); diff --git a/packages/compartment-mapper/src/types.js b/packages/compartment-mapper/src/types.js index 3a2c5b4d8e..06e7791f41 100644 --- a/packages/compartment-mapper/src/types.js +++ b/packages/compartment-mapper/src/types.js @@ -251,7 +251,7 @@ export {}; * @property {Array} [transforms] * @property {Array} [__shimTransforms__] * @property {Record} [modules] - * @property {ExitModuleImportHook} [exitModuleImportHook] + * @property {ExitModuleImportHook} [importHook] * @property {Record} [attenuations] * @property {typeof Compartment} [Compartment] */ @@ -332,7 +332,7 @@ export {}; * @property {object} [policy] * @property {Set} [tags] * @property {CaptureSourceLocationHook} [captureSourceLocation] - * @property {ExitModuleImportHook} [exitModuleImportHook] + * @property {ExitModuleImportHook} [importHook] * @property {Array} [searchSuffixes] * @property {Record} [commonDependencies] */ diff --git a/packages/compartment-mapper/test/test-exit-hook.js b/packages/compartment-mapper/test/test-exit-hook.js index 255299f7ba..297e1d8055 100644 --- a/packages/compartment-mapper/test/test-exit-hook.js +++ b/packages/compartment-mapper/test/test-exit-hook.js @@ -9,7 +9,7 @@ const fixture = new URL( ).toString(); scaffold( - 'exitModuleImportHook - import actual builtin', + 'exitModule importHook - import actual builtin', test, fixture, (t, { namespace }) => { @@ -18,7 +18,7 @@ scaffold( 1, // expected number of assertions { additionalOptions: { - exitModuleImportHook: async specifier => { + importHook: async specifier => { const ns = await import(specifier); return Object.freeze({ imports: [], diff --git a/packages/compartment-mapper/test/test-policy.js b/packages/compartment-mapper/test/test-policy.js index 49be3d1c24..3f63566711 100644 --- a/packages/compartment-mapper/test/test-policy.js +++ b/packages/compartment-mapper/test/test-policy.js @@ -385,7 +385,7 @@ scaffold( policy, additionalOptions: { modules: {}, - exitModuleImportHook: async specifier => { + importHook: async specifier => { const ns = { a: 1, b: 2,