From 4f42c7528fa4d6ecbbccbdba62b595d572be626b Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Fri, 31 May 2024 12:55:24 -0700 Subject: [PATCH 1/9] refactor(compartment-mapper): Extract default parsers modules --- .../compartment-mapper/src/archive-parsers.js | 20 ++++++++++++++++ packages/compartment-mapper/src/archive.js | 21 ++--------------- packages/compartment-mapper/src/bundle.js | 23 +------------------ .../src/import-archive-parsers.js | 18 +++++++++++++++ .../compartment-mapper/src/import-archive.js | 17 +------------- .../compartment-mapper/src/import-parsers.js | 18 +++++++++++++++ packages/compartment-mapper/src/import.js | 18 +-------------- 7 files changed, 61 insertions(+), 74 deletions(-) create mode 100644 packages/compartment-mapper/src/archive-parsers.js create mode 100644 packages/compartment-mapper/src/import-archive-parsers.js create mode 100644 packages/compartment-mapper/src/import-parsers.js diff --git a/packages/compartment-mapper/src/archive-parsers.js b/packages/compartment-mapper/src/archive-parsers.js new file mode 100644 index 0000000000..9519cdfc02 --- /dev/null +++ b/packages/compartment-mapper/src/archive-parsers.js @@ -0,0 +1,20 @@ +/** @import {ParserForLanguage} from './types.js' */ + +import parserJson from './parse-json.js'; +import parserText from './parse-text.js'; +import parserBytes from './parse-bytes.js'; +import parserArchiveCjs from './parse-archive-cjs.js'; +import parserArchiveMjs from './parse-archive-mjs.js'; + +/** @satisfies {Readonly} */ +export const defaultParserForLanguage = Object.freeze( + /** @type {const} */ ({ + mjs: parserArchiveMjs, + 'pre-mjs-json': parserArchiveMjs, + cjs: parserArchiveCjs, + 'pre-cjs-json': parserArchiveCjs, + json: parserJson, + text: parserText, + bytes: parserBytes, + }), +); diff --git a/packages/compartment-mapper/src/archive.js b/packages/compartment-mapper/src/archive.js index a86eb98eac..86d80ef817 100644 --- a/packages/compartment-mapper/src/archive.js +++ b/packages/compartment-mapper/src/archive.js @@ -1,7 +1,7 @@ // @ts-check /* eslint no-shadow: 0 */ -/** @import {ArchiveOptions, ParserForLanguage} from './types.js' */ +/** @import {ArchiveOptions} from './types.js' */ /** @import {ArchiveWriter} from './types.js' */ /** @import {CompartmentDescriptor} from './types.js' */ /** @import {CompartmentMapDescriptor} from './types.js' */ @@ -22,11 +22,7 @@ import { exitModuleImportHookMaker, makeImportHookMaker, } from './import-hook.js'; -import parserJson from './parse-json.js'; -import parserText from './parse-text.js'; -import parserBytes from './parse-bytes.js'; -import parserArchiveCjs from './parse-archive-cjs.js'; -import parserArchiveMjs from './parse-archive-mjs.js'; +import { defaultParserForLanguage } from './archive-parsers.js'; import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; import { @@ -40,19 +36,6 @@ const textEncoder = new TextEncoder(); const { assign, create, freeze } = Object; -/** @satisfies {Readonly} */ -const defaultParserForLanguage = freeze( - /** @type {const} */ ({ - mjs: parserArchiveMjs, - 'pre-mjs-json': parserArchiveMjs, - cjs: parserArchiveCjs, - 'pre-cjs-json': parserArchiveCjs, - json: parserJson, - text: parserText, - bytes: parserBytes, - }), -); - /** * @param {string} rel - a relative URL * @param {string} abs - a fully qualified URL diff --git a/packages/compartment-mapper/src/bundle.js b/packages/compartment-mapper/src/bundle.js index 99f007d9c5..cf24553a52 100644 --- a/packages/compartment-mapper/src/bundle.js +++ b/packages/compartment-mapper/src/bundle.js @@ -2,7 +2,6 @@ /* eslint no-shadow: 0 */ /** @import {PrecompiledStaticModuleInterface} from 'ses' */ -/** @import {ParserForLanguage} from './types.js' */ /** @import {CompartmentDescriptor} from './types.js' */ /** @import {CompartmentSources} from './types.js' */ /** @import {ReadFn} from './types.js' */ @@ -16,11 +15,7 @@ import { compartmentMapForNodeModules } from './node-modules.js'; import { search } from './search.js'; import { link } from './link.js'; import { makeImportHookMaker } from './import-hook.js'; -import parserJson from './parse-json.js'; -import parserText from './parse-text.js'; -import parserBytes from './parse-bytes.js'; -import parserArchiveCjs from './parse-archive-cjs.js'; -import parserArchiveMjs from './parse-archive-mjs.js'; +import { defaultParserForLanguage } from './archive-parsers.js'; import { parseLocatedJson } from './json.js'; import mjsSupport from './bundle-mjs.js'; @@ -28,24 +23,8 @@ import cjsSupport from './bundle-cjs.js'; const textEncoder = new TextEncoder(); -const { freeze } = Object; const { quote: q } = assert; -/** - * @satisfies {Readonly} - */ -const defaultParserForLanguage = freeze( - /** @type {const} */ ({ - mjs: parserArchiveMjs, - 'pre-mjs-json': parserArchiveMjs, - cjs: parserArchiveCjs, - 'pre-cjs-json': parserArchiveCjs, - json: parserJson, - text: parserText, - bytes: parserBytes, - }), -); - /** * @param {Record} compartmentDescriptors * @param {Record} compartmentSources diff --git a/packages/compartment-mapper/src/import-archive-parsers.js b/packages/compartment-mapper/src/import-archive-parsers.js new file mode 100644 index 0000000000..f08de871d1 --- /dev/null +++ b/packages/compartment-mapper/src/import-archive-parsers.js @@ -0,0 +1,18 @@ +/** @import {ParserForLanguage} from './types.js' */ + +import parserPreCjs from './parse-pre-cjs.js'; +import parserJson from './parse-json.js'; +import parserText from './parse-text.js'; +import parserBytes from './parse-bytes.js'; +import parserPreMjs from './parse-pre-mjs.js'; + +/** @satisfies {Readonly} */ +export const defaultParserForLanguage = Object.freeze( + /** @type {const} */ ({ + 'pre-cjs-json': parserPreCjs, + 'pre-mjs-json': parserPreMjs, + json: parserJson, + text: parserText, + bytes: parserBytes, + }), +); diff --git a/packages/compartment-mapper/src/import-archive.js b/packages/compartment-mapper/src/import-archive.js index aca46aa547..1039b6f902 100644 --- a/packages/compartment-mapper/src/import-archive.js +++ b/packages/compartment-mapper/src/import-archive.js @@ -21,11 +21,7 @@ import { ZipReader } from '@endo/zip'; import { link } from './link.js'; -import parserPreCjs from './parse-pre-cjs.js'; -import parserJson from './parse-json.js'; -import parserText from './parse-text.js'; -import parserBytes from './parse-bytes.js'; -import parserPreMjs from './parse-pre-mjs.js'; +import { defaultParserForLanguage } from './import-archive-parsers.js'; import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; import { join } from './node-module-specifier.js'; @@ -41,17 +37,6 @@ const textDecoder = new TextDecoder(); const { assign, create, freeze } = Object; -/** @satisfies {Readonly} */ -const defaultParserForLanguage = freeze( - /** @type {const} */ ({ - 'pre-cjs-json': parserPreCjs, - 'pre-mjs-json': parserPreMjs, - json: parserJson, - text: parserText, - bytes: parserBytes, - }), -); - /** * @param {string} errorMessage - error to throw on execute * @returns {StaticModuleType} diff --git a/packages/compartment-mapper/src/import-parsers.js b/packages/compartment-mapper/src/import-parsers.js new file mode 100644 index 0000000000..681a0c2b78 --- /dev/null +++ b/packages/compartment-mapper/src/import-parsers.js @@ -0,0 +1,18 @@ +/** @import {ParserForLanguage} from './types.js' */ + +import parserJson from './parse-json.js'; +import parserText from './parse-text.js'; +import parserBytes from './parse-bytes.js'; +import parserCjs from './parse-cjs.js'; +import parserMjs from './parse-mjs.js'; + +/** @satisfies {Readonly} */ +export const defaultParserForLanguage = Object.freeze( + /** @type {const} */ ({ + mjs: parserMjs, + cjs: parserCjs, + json: parserJson, + text: parserText, + bytes: parserBytes, + }), +); diff --git a/packages/compartment-mapper/src/import.js b/packages/compartment-mapper/src/import.js index 47bff1cb1d..5e8078d44b 100644 --- a/packages/compartment-mapper/src/import.js +++ b/packages/compartment-mapper/src/import.js @@ -4,7 +4,6 @@ /** @import {Application} from './types.js' */ /** @import {ImportLocationOptions} from './types.js' */ /** @import {LoadLocationOptions} from './types.js' */ -/** @import {ParserForLanguage} from './types.js' */ /** @import {ExecuteFn} from './types.js' */ /** @import {ReadFn} from './types.js' */ /** @import {ReadPowers} from './types.js' */ @@ -17,27 +16,12 @@ import { exitModuleImportHookMaker, makeImportHookMaker, } from './import-hook.js'; -import parserJson from './parse-json.js'; -import parserText from './parse-text.js'; -import parserBytes from './parse-bytes.js'; -import parserCjs from './parse-cjs.js'; -import parserMjs from './parse-mjs.js'; +import { defaultParserForLanguage } from './import-parsers.js'; import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; const { assign, create, freeze } = Object; -/** @satisfies {Readonly} */ -export const defaultParserForLanguage = freeze( - /** @type {const} */ ({ - mjs: parserMjs, - cjs: parserCjs, - json: parserJson, - text: parserText, - bytes: parserBytes, - }), -); - /** * @param {ReadFn | ReadPowers} readPowers * @param {string} moduleLocation From 7b5432f247ac60d6ec0fef7b7e619124689c8495 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Mon, 20 May 2024 17:34:05 -0700 Subject: [PATCH 2/9] feat(compartment-mapper): Export lite and parser modules --- packages/compartment-mapper/archive-lite.js | 7 +++++++ packages/compartment-mapper/archive-parsers.js | 1 + packages/compartment-mapper/import-archive-lite.js | 5 +++++ packages/compartment-mapper/import-archive-parsers.js | 1 + packages/compartment-mapper/import-lite.js | 1 + packages/compartment-mapper/import-parsers.js | 1 + packages/compartment-mapper/package.json | 6 ++++++ 7 files changed, 22 insertions(+) create mode 100644 packages/compartment-mapper/archive-lite.js create mode 100644 packages/compartment-mapper/archive-parsers.js create mode 100644 packages/compartment-mapper/import-archive-lite.js create mode 100644 packages/compartment-mapper/import-archive-parsers.js create mode 100644 packages/compartment-mapper/import-lite.js create mode 100644 packages/compartment-mapper/import-parsers.js diff --git a/packages/compartment-mapper/archive-lite.js b/packages/compartment-mapper/archive-lite.js new file mode 100644 index 0000000000..1b20c84f67 --- /dev/null +++ b/packages/compartment-mapper/archive-lite.js @@ -0,0 +1,7 @@ +export { + makeArchive, + makeAndHashArchive, + writeArchive, + mapLocation, + hashLocation, +} from './src/archive-lite.js'; diff --git a/packages/compartment-mapper/archive-parsers.js b/packages/compartment-mapper/archive-parsers.js new file mode 100644 index 0000000000..8f14267fd7 --- /dev/null +++ b/packages/compartment-mapper/archive-parsers.js @@ -0,0 +1 @@ +export { parserForLanguage } from './src/archive-parsers.js'; diff --git a/packages/compartment-mapper/import-archive-lite.js b/packages/compartment-mapper/import-archive-lite.js new file mode 100644 index 0000000000..def7c36bf0 --- /dev/null +++ b/packages/compartment-mapper/import-archive-lite.js @@ -0,0 +1,5 @@ +export { + parseArchive, + loadArchive, + importArchive, +} from './src/import-archive-lite.js'; diff --git a/packages/compartment-mapper/import-archive-parsers.js b/packages/compartment-mapper/import-archive-parsers.js new file mode 100644 index 0000000000..f9e55aa23b --- /dev/null +++ b/packages/compartment-mapper/import-archive-parsers.js @@ -0,0 +1 @@ +export { parserForLanguage } from './src/import-archive-parsers.js'; diff --git a/packages/compartment-mapper/import-lite.js b/packages/compartment-mapper/import-lite.js new file mode 100644 index 0000000000..48d1dea9d0 --- /dev/null +++ b/packages/compartment-mapper/import-lite.js @@ -0,0 +1 @@ +export { loadLocation, importLocation } from './src/import-lite.js'; diff --git a/packages/compartment-mapper/import-parsers.js b/packages/compartment-mapper/import-parsers.js new file mode 100644 index 0000000000..0805c0c9b6 --- /dev/null +++ b/packages/compartment-mapper/import-parsers.js @@ -0,0 +1 @@ +export { parserForLanguage } from './src/import-parsers.js'; diff --git a/packages/compartment-mapper/package.json b/packages/compartment-mapper/package.json index 61aec3f999..63da566cdc 100644 --- a/packages/compartment-mapper/package.json +++ b/packages/compartment-mapper/package.json @@ -28,8 +28,14 @@ "default": "./index.js" }, "./import.js": "./import.js", + "./import-lite.js": "./import-lite.js", + "./import-parsers.js": "./import-parsers.js", "./archive.js": "./archive.js", + "./archive-lite.js": "./archive-lite.js", + "./archive-parsers.js": "./archive-parsers.js", "./import-archive.js": "./import-archive.js", + "./import-archive-lite.js": "./import-archive-lite.js", + "./import-archive-parsers.js": "./import-archive-parsers.js", "./bundle.js": "./bundle.js", "./node-powers.js": "./node-powers.js", "./package.json": "./package.json" From 24fbaa9b2b07b2f4ea604a72cd2057f218c6c960 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Fri, 31 May 2024 19:26:51 -0700 Subject: [PATCH 3/9] refactor(compartment-mapper): Rename hevy to lite --- packages/compartment-mapper/src/{archive.js => archive-lite.js} | 0 .../src/{import-archive.js => import-archive-lite.js} | 0 packages/compartment-mapper/src/{import.js => import-lite.js} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename packages/compartment-mapper/src/{archive.js => archive-lite.js} (100%) rename packages/compartment-mapper/src/{import-archive.js => import-archive-lite.js} (100%) rename packages/compartment-mapper/src/{import.js => import-lite.js} (100%) diff --git a/packages/compartment-mapper/src/archive.js b/packages/compartment-mapper/src/archive-lite.js similarity index 100% rename from packages/compartment-mapper/src/archive.js rename to packages/compartment-mapper/src/archive-lite.js diff --git a/packages/compartment-mapper/src/import-archive.js b/packages/compartment-mapper/src/import-archive-lite.js similarity index 100% rename from packages/compartment-mapper/src/import-archive.js rename to packages/compartment-mapper/src/import-archive-lite.js diff --git a/packages/compartment-mapper/src/import.js b/packages/compartment-mapper/src/import-lite.js similarity index 100% rename from packages/compartment-mapper/src/import.js rename to packages/compartment-mapper/src/import-lite.js From 29eb346ce0245ec328784bca20eb3675df70db4f Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Fri, 31 May 2024 20:19:38 -0700 Subject: [PATCH 4/9] refactor(compartment-mapper): Extract lite from hevy --- .../compartment-mapper/archive-parsers.js | 2 +- .../import-archive-parsers.js | 2 +- packages/compartment-mapper/import-parsers.js | 2 +- .../compartment-mapper/src/archive-lite.js | 3 +- packages/compartment-mapper/src/archive.js | 95 ++++++++++++++++++ .../src/import-archive-lite.js | 3 +- .../compartment-mapper/src/import-archive.js | 88 ++++++++++++++++ .../compartment-mapper/src/import-lite.js | 3 +- packages/compartment-mapper/src/import.js | 55 ++++++++++ .../test/snapshots/error-handling.test.js.md | 36 +++---- .../snapshots/error-handling.test.js.snap | Bin 781 -> 784 bytes .../test/snapshots/policy-format.test.js.snap | Bin 834 -> 832 bytes .../test/snapshots/policy.test.js.snap | Bin 788 -> 782 bytes 13 files changed, 262 insertions(+), 27 deletions(-) create mode 100644 packages/compartment-mapper/src/archive.js create mode 100644 packages/compartment-mapper/src/import-archive.js create mode 100644 packages/compartment-mapper/src/import.js diff --git a/packages/compartment-mapper/archive-parsers.js b/packages/compartment-mapper/archive-parsers.js index 8f14267fd7..18bbb3f184 100644 --- a/packages/compartment-mapper/archive-parsers.js +++ b/packages/compartment-mapper/archive-parsers.js @@ -1 +1 @@ -export { parserForLanguage } from './src/archive-parsers.js'; +export { defaultParserForLanguage } from './src/archive-parsers.js'; diff --git a/packages/compartment-mapper/import-archive-parsers.js b/packages/compartment-mapper/import-archive-parsers.js index f9e55aa23b..ae8748f8b5 100644 --- a/packages/compartment-mapper/import-archive-parsers.js +++ b/packages/compartment-mapper/import-archive-parsers.js @@ -1 +1 @@ -export { parserForLanguage } from './src/import-archive-parsers.js'; +export { defaultParserForLanguage } from './src/import-archive-parsers.js'; diff --git a/packages/compartment-mapper/import-parsers.js b/packages/compartment-mapper/import-parsers.js index 0805c0c9b6..19076fb378 100644 --- a/packages/compartment-mapper/import-parsers.js +++ b/packages/compartment-mapper/import-parsers.js @@ -1 +1 @@ -export { parserForLanguage } from './src/import-parsers.js'; +export { defaultParserForLanguage } from './src/import-parsers.js'; diff --git a/packages/compartment-mapper/src/archive-lite.js b/packages/compartment-mapper/src/archive-lite.js index 86d80ef817..ebba03df9e 100644 --- a/packages/compartment-mapper/src/archive-lite.js +++ b/packages/compartment-mapper/src/archive-lite.js @@ -22,7 +22,6 @@ import { exitModuleImportHookMaker, makeImportHookMaker, } from './import-hook.js'; -import { defaultParserForLanguage } from './archive-parsers.js'; import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; import { @@ -294,7 +293,7 @@ const digestLocation = async (powers, moduleLocation, options = {}) => { } = options; const parserForLanguage = freeze( - assign(create(null), defaultParserForLanguage, parserForLanguageOption), + assign(create(null), parserForLanguageOption), ); const languageForExtension = freeze( assign(create(null), languageForExtensionOption), diff --git a/packages/compartment-mapper/src/archive.js b/packages/compartment-mapper/src/archive.js new file mode 100644 index 0000000000..862f66551a --- /dev/null +++ b/packages/compartment-mapper/src/archive.js @@ -0,0 +1,95 @@ +// @ts-check + +import { defaultParserForLanguage } from './archive-parsers.js'; +import { + makeAndHashArchive as makeAndHashArchiveLite, + makeArchive as makeArchiveLite, + mapLocation as mapLocationLite, + hashLocation as hashLocationLite, + writeArchive as writeArchiveLite, +} from './archive-lite.js'; + +export { makeArchiveCompartmentMap } from './archive-lite.js'; + +const { assign, create, freeze } = Object; + +/** @import {ArchiveOptions} from './types.js' */ +/** @import {ReadFn} from './types.js' */ +/** @import {ReadPowers} from './types.js' */ +/** @import {HashPowers} from './types.js' */ +/** @import {WriteFn} from './types.js' */ + +/** + * Add the default parserForLanguage option. + * @param {ArchiveOptions} [options] + * @returns {ArchiveOptions} + */ +const assignParserForLanguage = (options = {}) => { + const { parserForLanguage: parserForLanguageOption, ...rest } = options; + const parserForLanguage = freeze( + assign(create(null), defaultParserForLanguage, parserForLanguageOption), + ); + return { ...rest, parserForLanguage }; +}; + +/** + * @param {ReadFn | ReadPowers} powers + * @param {string} moduleLocation + * @param {ArchiveOptions} [options] + * @returns {Promise<{bytes: Uint8Array, sha512?: string}>} + */ +export const makeAndHashArchive = async (powers, moduleLocation, options) => + makeAndHashArchiveLite( + powers, + moduleLocation, + assignParserForLanguage(options), + ); + +/** + * @param {ReadFn | ReadPowers} powers + * @param {string} moduleLocation + * @param {ArchiveOptions} [options] + * @returns {Promise} + */ +export const makeArchive = async (powers, moduleLocation, options) => + makeArchiveLite(powers, moduleLocation, assignParserForLanguage(options)); + +/** + * @param {ReadFn | ReadPowers} powers + * @param {string} moduleLocation + * @param {ArchiveOptions} [options] + * @returns {Promise} + */ +export const mapLocation = async (powers, moduleLocation, options) => + mapLocationLite(powers, moduleLocation, assignParserForLanguage(options)); + +/** + * @param {HashPowers} powers + * @param {string} moduleLocation + * @param {ArchiveOptions} [options] + * @returns {Promise} + */ +export const hashLocation = async (powers, moduleLocation, options) => + hashLocationLite(powers, moduleLocation, assignParserForLanguage(options)); + +/** + * @param {WriteFn} write + * @param {ReadFn | ReadPowers} readPowers + * @param {string} archiveLocation + * @param {string} moduleLocation + * @param {ArchiveOptions} [options] + */ +export const writeArchive = async ( + write, + readPowers, + archiveLocation, + moduleLocation, + options, +) => + writeArchiveLite( + write, + readPowers, + archiveLocation, + moduleLocation, + assignParserForLanguage(options), + ); diff --git a/packages/compartment-mapper/src/import-archive-lite.js b/packages/compartment-mapper/src/import-archive-lite.js index 1039b6f902..45e69d7fe4 100644 --- a/packages/compartment-mapper/src/import-archive-lite.js +++ b/packages/compartment-mapper/src/import-archive-lite.js @@ -21,7 +21,6 @@ import { ZipReader } from '@endo/zip'; import { link } from './link.js'; -import { defaultParserForLanguage } from './import-archive-parsers.js'; import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; import { join } from './node-module-specifier.js'; @@ -281,7 +280,7 @@ export const parseArchive = async ( } = options; const parserForLanguage = freeze( - assign(create(null), defaultParserForLanguage, parserForLanguageOption), + assign(create(null), parserForLanguageOption), ); const languageForExtension = freeze( assign(create(null), languageForExtensionOption), diff --git a/packages/compartment-mapper/src/import-archive.js b/packages/compartment-mapper/src/import-archive.js new file mode 100644 index 0000000000..022325a7c0 --- /dev/null +++ b/packages/compartment-mapper/src/import-archive.js @@ -0,0 +1,88 @@ +// @ts-check + +import { defaultParserForLanguage } from './import-archive-parsers.js'; +import { + parseArchive as parseArchiveLite, + loadArchive as loadArchiveLite, + importArchive as importArchiveLite, +} from './import-archive-lite.js'; + +const { assign, create, freeze } = Object; + +/** @import {Application, ComputeSourceLocationHook, ComputeSourceMapLocationHook, ExecuteOptions, ExitModuleImportHook, HashFn, LoadArchiveOptions, ReadPowers} from './types.js' */ +/** @import {ParserForLanguage} from './types.js' */ + +// 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. +// +/** @typedef {typeof Compartment} CompartmentConstructor */ + +/** + * @typedef {object} Options + * @property {string} [expectedSha512] + * @property {HashFn} [computeSha512] + * @property {Record} [modules] + * @property {ExitModuleImportHook} [importHook] + * @property {CompartmentConstructor} [Compartment] + * @property {ComputeSourceLocationHook} [computeSourceLocation] + * @property {ComputeSourceMapLocationHook} [computeSourceMapLocation] + * @property {ParserForLanguage} [parserForLanguage] + */ + +/** + * Add the default parserForLanguage option. + * @param {Options} [options] + * @returns {Options} + */ +const assignParserForLanguage = (options = {}) => { + const { parserForLanguage: parserForLanguageOption, ...rest } = options; + /** @type {ParserForLanguage} */ + const parserForLanguage = freeze( + assign(create(null), defaultParserForLanguage, parserForLanguageOption), + ); + return { ...rest, parserForLanguage }; +}; + +/** + * @param {Uint8Array} archiveBytes + * @param {string} [archiveLocation] + * @param {Options} [options] + * @returns {Promise} + */ +export const parseArchive = async ( + archiveBytes, + archiveLocation = '', + options = {}, +) => + parseArchiveLite( + archiveBytes, + archiveLocation, + assignParserForLanguage(options), + ); + +/** + * @param {import('@endo/zip').ReadFn | ReadPowers} readPowers + * @param {string} archiveLocation + * @param {LoadArchiveOptions} [options] + * @returns {Promise} + */ +export const loadArchive = async (readPowers, archiveLocation, options) => + loadArchiveLite( + readPowers, + archiveLocation, + assignParserForLanguage(options), + ); + +/** + * @param {import('@endo/zip').ReadFn | ReadPowers} readPowers + * @param {string} archiveLocation + * @param {ExecuteOptions & LoadArchiveOptions} options + * @returns {Promise} + */ +export const importArchive = async (readPowers, archiveLocation, options) => + importArchiveLite( + readPowers, + archiveLocation, + assignParserForLanguage(options), + ); diff --git a/packages/compartment-mapper/src/import-lite.js b/packages/compartment-mapper/src/import-lite.js index 5e8078d44b..653a950734 100644 --- a/packages/compartment-mapper/src/import-lite.js +++ b/packages/compartment-mapper/src/import-lite.js @@ -16,7 +16,6 @@ import { exitModuleImportHookMaker, makeImportHookMaker, } from './import-hook.js'; -import { defaultParserForLanguage } from './import-parsers.js'; import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; @@ -45,7 +44,7 @@ export const loadLocation = async ( } = options; const parserForLanguage = freeze( - assign(create(null), defaultParserForLanguage, parserForLanguageOption), + assign(create(null), parserForLanguageOption), ); const languageForExtension = freeze( assign(create(null), languageForExtensionOption), diff --git a/packages/compartment-mapper/src/import.js b/packages/compartment-mapper/src/import.js new file mode 100644 index 0000000000..6a96a35ca0 --- /dev/null +++ b/packages/compartment-mapper/src/import.js @@ -0,0 +1,55 @@ +// @ts-check + +import { defaultParserForLanguage } from './import-parsers.js'; +import { + loadLocation as loadLocationLite, + importLocation as importLocationLite, +} from './import-lite.js'; + +const { assign, create, freeze } = Object; + +/** @import {Application} from './types.js' */ +/** @import {ArchiveOptions} from './types.js' */ +/** @import {ExecuteOptions} from './types.js' */ +/** @import {ReadFn} from './types.js' */ +/** @import {ReadPowers} from './types.js' */ + +/** + * Add the default parserForLanguage option. + * @param {ArchiveOptions} [options] + * @returns {ArchiveOptions} + */ +const assignParserForLanguage = (options = {}) => { + const { parserForLanguage: parserForLanguageOption, ...rest } = options; + const parserForLanguage = freeze( + assign(create(null), defaultParserForLanguage, parserForLanguageOption), + ); + return { ...rest, parserForLanguage }; +}; + +/** + * @param {ReadFn | ReadPowers} readPowers + * @param {string} moduleLocation + * @param {ArchiveOptions} [options] + * @returns {Promise} + */ +export const loadLocation = async (readPowers, moduleLocation, options) => + loadLocationLite( + readPowers, + moduleLocation, + assignParserForLanguage(options), + ); + +/** + * @param {ReadFn | ReadPowers} readPowers + * @param {string} moduleLocation + * @param {ExecuteOptions & ArchiveOptions} [options] + * @returns {Promise} the object of the imported modules exported + * names. + */ +export const importLocation = async (readPowers, moduleLocation, options) => + importLocationLite( + readPowers, + moduleLocation, + assignParserForLanguage(options), + ); diff --git a/packages/compartment-mapper/test/snapshots/error-handling.test.js.md b/packages/compartment-mapper/test/snapshots/error-handling.test.js.md index 0bdaee0e8e..bc36aa2aca 100644 --- a/packages/compartment-mapper/test/snapshots/error-handling.test.js.md +++ b/packages/compartment-mapper/test/snapshots/error-handling.test.js.md @@ -31,9 +31,9 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive.js:…)␊ + at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -44,9 +44,9 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive.js:…)␊ + at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -57,10 +57,10 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive.js:…)␊ - at async writeArchive (file://.../compartment-mapper/src/archive.js:…)␊ + at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async writeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -71,10 +71,10 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive.js:…)␊ - at async writeArchive (file://.../compartment-mapper/src/archive.js:…)␊ + at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async writeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -99,7 +99,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `Error: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/cjs/␊ - at Object.execute (file://.../compartment-mapper/src/import-archive.js:…)␊ + at Object.execute (file://.../compartment-mapper/src/import-archive-lite.js:…)␊ at execute (file://.../ses/src/module-instance.js:…)␊ at compartmentImportNow (file://.../ses/src/compartment.js:…)␊ at Compartment.importNow (file://.../ses/src/compartment.js:…)␊ @@ -115,7 +115,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `Error: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/cjs/␊ - at Object.execute (file://.../compartment-mapper/src/import-archive.js:…)␊ + at Object.execute (file://.../compartment-mapper/src/import-archive-lite.js:…)␊ at execute (file://.../ses/src/module-instance.js:…)␊ at compartmentImportNow (file://.../ses/src/compartment.js:…)␊ at Compartment.importNow (file://.../ses/src/compartment.js:…)␊ @@ -131,7 +131,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `Error: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/cjs/␊ - at Object.execute (file://.../compartment-mapper/src/import-archive.js:…)␊ + at Object.execute (file://.../compartment-mapper/src/import-archive-lite.js:…)␊ at execute (file://.../ses/src/module-instance.js:…)␊ at compartmentImportNow (file://.../ses/src/compartment.js:…)␊ at Compartment.importNow (file://.../ses/src/compartment.js:…)␊ @@ -147,7 +147,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 `Error: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/cjs/␊ - at Object.execute (file://.../compartment-mapper/src/import-archive.js:…)␊ + at Object.execute (file://.../compartment-mapper/src/import-archive-lite.js:…)␊ at execute (file://.../ses/src/module-instance.js:…)␊ at compartmentImportNow (file://.../ses/src/compartment.js:…)␊ at Compartment.importNow (file://.../ses/src/compartment.js:…)␊ diff --git a/packages/compartment-mapper/test/snapshots/error-handling.test.js.snap b/packages/compartment-mapper/test/snapshots/error-handling.test.js.snap index 1ce050b4d14931e0403a262e7a913eb63ec546f4..e940823a3ff20cf37c9823764fcca09308949948 100644 GIT binary patch literal 784 zcmV+r1MmDnRzVm10Re}f|9JG*}5j(1}&0%h}718Y(IW}cKcq$r&9Bg{r-cTaFQnb zv6#5RkPXNfZGse4B!x+x3k+J3b4 zZ0E(!$uy_8rP2W2p^y}T)0GBfIw~YVKV%sS6%K75gn}IT2or*SEF=vW3&W6co#kj; zM#=>l<~bSW$U1h*RGHEdeW~RwBb%{4?7oIVMP%f(TuY2XmK_Ihgi2`#u}~4v#F0@b zuV$7BYlTW0^Lof>Jplkf2QD>we3&GL5_Gj&usb)9CCjYgtnM3>?uA1;`1SSY-b|u$ zbj3bsr;2x3kmVT1TFwOS+kY0duq|pL%e8SnY(8xF58I$EY#EN|(C}286EMip*xB2g z!Rx(j1n+6j2XDiJ7m0-I)lt&WSg^oS66?(mq;RxYvr3GLj?t#G;5?k}Lkg~sy5qZ3 z2V>zwiz4 zWKXgud9z4fQe)#o+gfAWe$^oN`SvBXYa@gY?G+*QsdKFLsLL+Zt%FqS(c(?2o+k#g zTuY&-D>xlWOtsT(B_{86XRnw1&aAy&slrk;e2;sZgExl4T$g}KO_AIZ953K OrujE{0?P)2B>({5aD7Pt literal 781 zcmV+o1M>VqRzVuV_^Az5~t2`rh z99XXO_#cZ100000000B+m``uhFc`+yRS1a_f4;y=Pn)#F;#y5e(=;?BG%mYvKZt)fM850Tp1kL}0L&u-tV_(EzvvOj*36He0P zAQlss8?pfzqfL;Yilk5jU_dqn22x}6L34CMD`6F8Hqp+0zltJs_-VJ--FdoGygE;I zUhKZwJ)dTDQYiJ|9STViI9+N$s-s*I^g@=RP~p(_Kq$zNk8nWHi-n{k~)-wPAbl?)B$47&Kp#i$eE!dkI$dYB&a8~sV3iraH?f?GvYkwwD zI=W&Xv{S{~EXZ<9eBorlgtx2C&oY=oPmuGq2VciZP0W@jEVkJ&b1R?G7L%k1Hb%vS$}Jhh%$ zPwnQYy{9_Hhqkdg)_Gl{^kwIo(2dc+hxUes>V!Gfdemlvs@6fG^=R=XmCtj7X{M!6 z)E1H!#i7dSrs9zIy0zE${C=#wUa2;c)qKb9Hu?VXVo2PDG$Ee5RtM3w_e;*BMMAd> z7s@(R{7%=o@!pqXoaymGowE^a*qeugX}^6hamp} LdQIG*DJ1{^2QP*2 diff --git a/packages/compartment-mapper/test/snapshots/policy-format.test.js.snap b/packages/compartment-mapper/test/snapshots/policy-format.test.js.snap index 621aab35a1ad7991fa5deed80736bf46ad8f0770..73af6c64ce4c2c30cc604cb444de751e6cbd716a 100644 GIT binary patch literal 832 zcmV-G1Hb%1RzVc~y2|{gmz0mYi28{$l1<_gzH1f+zq|lnjmzQwN zb_tE6STd5xZLZ|A{U{=eY`&ZHroHwHokJ5hJ?BP`-uz#U=axXzW7}6To8s02p7V zJRo#U0PKNZZniA+4=(8|n%-MgE&$Co>OR#Q@2aiAn|_t^BXL>u-(Bh-jn?o3Kqiot z{(;o808g67PnUVrbsoAz`@;{*nAIrcpMoYP8`IKly3f!P#q`5vOfcR-!V=JC!GPM6 z-jW{>(SC<Bxxr~sGWl$Zm zyA7CZ^)5uR-yEOJXP3#WkG_`52nKy#qqWvTd*zb0plJdmPequwG0cM}5ULI+Wf4?k z29ZI`_agj*D->AuKX*yGSd_GNuwip)u%*D#?uAQDsi>x1=;67U@kCH1DOLFqHe2DA z@>1)9=8a3|4edNqYVKx}O2n*Yw%EVoC(od1~5-UcM`t5s+r0-QdM zM%&G)X&;LS00000000BM*gbC(F&GE%bF0z`(W(qcEY?jF4p4y#R79n=&`wB20;!_P z;Jb5D6W2c3zN$3Jz=Rk;FfbtjMNAAx@DZ3`VCF;cCAeJd=F%qhrHP3Y<^A&f{=W0u zel?^#s>`>Zup)}$-CIaXEYv!R>Zpd-IVI843k^SIcsIe3!tpA>H1-#iNO5x(US5QR z?I22GA{k2L7FY7wejFo(Hr|bUlV0lv&VjL;+2ft;J_E+4!NffwxR?Q_%u@i&v#F^m z51jS@@W7eYm(PN64@^vh$!#oUR*`wVnB=9w?NlnrxPn_~2SdE81cHg+NoF8e2C$ZW zw0Wfbis4@E%}v^`rN6LQu_)iWR4%oZS4=%^Zipd*0!LiLkXK>IImL)+?4jZ<&@d%b z5mpBJ)g|dGFEpAK`UjWvWlis{st|x?8+DKBjd#V?;0?dZ`H{FR`tL6FkA`dbJ|Gjw za_>NDd4MO)N*cyqP_EjlCT=Z#a__FWMf)Rn@%5kqL_ZTj0ul+kgx=_Q8J*` zq&M(GBwFuelg=f!r2m!+JtelK_Qz#vdrnd-r8ZH<<*vxCW11?m&MyBn$T|Rlw)Nm1 zG@2D_tbZNGI`-0Yc+n1?go%1>;HWzXIX=zbE>F_r|Ag7Gn!FyD96d*JLjH0YAsxw} zI^gNnXR_735XpaYd@`S1CbKsDS}G?P^m&ceS_|!!OWK^K2`qUkqN0sK9zMa5YJ*Z1 z<0@e|Hi+47gnw{^6082_E=d>5k~R-EY%cY;lvvumaH$E(YFdRJo|_p@)^sSPsyM>t zE8L(cwJvDhxOCpo&LgE3E;gw|)@hsVH`Lj1oA2L#jb`9;!s?|q7hk&+ULR<4F|WDu M4^yi5HcAx$0C$U?lmGw# diff --git a/packages/compartment-mapper/test/snapshots/policy.test.js.snap b/packages/compartment-mapper/test/snapshots/policy.test.js.snap index 2848cf1ebcbe596d2dc05c8b1ee51aa5bb4ec330..a541d7c80026bb1fac4986c2cce956356d14265b 100644 GIT binary patch literal 782 zcmV+p1M&PpRzVCvmc8H00000000B+nca%hKoo$tE8?|paL9$M&?dX0UIYy2;V^GW`8yHH8fA0aTkOWHXzbH4MDPR>O%;WC)&pTEd3!)fs( zlbi)h7y%lC!4yU?Q8L%0069YqBgmOa>WhR410HaR;TsvCVNx(HFw==N`ujx~qQMV? zA2*I}9TlI!&7<4LUj{FbaSFl6av-EKoN)rj^{Oe|&)NYdAj4paF@X~?PQK^4uImLd z%}|+?gc+rnWu!bq+ISILn3*Cys+R8w8PcaJu=X&^JO@tBU?xIRe91%%5i(v3@L`;0 zQkmkO6QU?*c4$tc`I&p_o;r}D1|ba~=Q5`dCd+1J8tPz*g4hikX!XDA-x+ z`@{U*9++1TcuT!M)cs59A=AyqntYeMNhzr|`=5UN=NjMN$N%KZL`aK6;l0<2H$WwM z1sFLCIwCHIJg#E+5pvzax|!`i(=L~J{Z_e>0{_rvVG=}T!?QTFf*>GBT2s?nEXuxhMR8PLS)%s6~)6toGq~89q-K zr>pNp3~@aslB&kyRDL??@@jRC56<{<91UR%CQ&kXo#AGYYZ?cLj9Jz#ZbRYWrW9H` zyOzT)9L_i8(5dsNdv4+vZSQ+?QPT&D;shpbvwkXuef zBsI}y=O2p*00000000B+na_*UKorNf74htU@W??{*e1K89)gG#f31jk5(Gh}$)p)O znF%w~rd2$6^y+`GUi=UI`+P~7ZVQz(X)6S#r%W>Q=DqiMU((6Dibh-p6aDiSHOz22 zf09bhf&~nK7(;>yUW}B?G*v)2BO1pUGx2f|Ghx63E=l-C2E;Ham=+|}u{8SoRTvUO zKJ?{Zty(9?tU|IK64pcxZ)n|&CwGznL zS=D!k`MU!!FJJJMdUvS%E9xQBRbW-V3*J;IsdoFH-v6_~_jmuFd>Q6{ek{EAYVig{ zNnQfRat<0IE|xqlZTJ!4x`uVN+kd8AxVd?&@Fd57X0x%DjY)GG z*d~Wp&E(MJ)Mus9m13~c*d~qlhmuC!;n)hLE2UwDa)5V8lWz!C8eJ&{D~)Z^c)gc2 zYCg4Cadf2|tT?ub Date: Mon, 3 Jun 2024 14:33:32 -0700 Subject: [PATCH 5/9] refactor(compartment-mapper): Decouple node_modules mapping strategy --- packages/compartment-mapper/archive-lite.js | 10 +- packages/compartment-mapper/import-lite.js | 2 +- packages/compartment-mapper/node-modules.js | 1 + packages/compartment-mapper/package.json | 1 + .../compartment-mapper/src/archive-lite.js | 85 +++++++---------- packages/compartment-mapper/src/archive.js | 87 ++++++++++++++---- .../compartment-mapper/src/import-lite.js | 69 +++++--------- packages/compartment-mapper/src/import.js | 34 ++++--- .../compartment-mapper/src/node-modules.js | 43 ++++++++- .../test/snapshots/error-handling.test.js.md | 28 +++--- .../snapshots/error-handling.test.js.snap | Bin 784 -> 787 bytes 11 files changed, 208 insertions(+), 152 deletions(-) create mode 100644 packages/compartment-mapper/node-modules.js diff --git a/packages/compartment-mapper/archive-lite.js b/packages/compartment-mapper/archive-lite.js index 1b20c84f67..06ce1b188b 100644 --- a/packages/compartment-mapper/archive-lite.js +++ b/packages/compartment-mapper/archive-lite.js @@ -1,7 +1,7 @@ export { - makeArchive, - makeAndHashArchive, - writeArchive, - mapLocation, - hashLocation, + makeArchiveFromMap, + makeAndHashArchiveFromMap, + writeArchiveFromMap, + mapFromMap, + hashFromMap, } from './src/archive-lite.js'; diff --git a/packages/compartment-mapper/import-lite.js b/packages/compartment-mapper/import-lite.js index 48d1dea9d0..bc7493028f 100644 --- a/packages/compartment-mapper/import-lite.js +++ b/packages/compartment-mapper/import-lite.js @@ -1 +1 @@ -export { loadLocation, importLocation } from './src/import-lite.js'; +export { loadFromMap, importFromMap } from './src/import-lite.js'; diff --git a/packages/compartment-mapper/node-modules.js b/packages/compartment-mapper/node-modules.js new file mode 100644 index 0000000000..e6d1b20115 --- /dev/null +++ b/packages/compartment-mapper/node-modules.js @@ -0,0 +1 @@ +export { mapNodeModules } from './src/node-modules.js'; diff --git a/packages/compartment-mapper/package.json b/packages/compartment-mapper/package.json index 63da566cdc..d4d7c74d88 100644 --- a/packages/compartment-mapper/package.json +++ b/packages/compartment-mapper/package.json @@ -38,6 +38,7 @@ "./import-archive-parsers.js": "./import-archive-parsers.js", "./bundle.js": "./bundle.js", "./node-powers.js": "./node-powers.js", + "./node-modules.js": "./node-modules.js", "./package.json": "./package.json" }, "scripts": { diff --git a/packages/compartment-mapper/src/archive-lite.js b/packages/compartment-mapper/src/archive-lite.js index ebba03df9e..8f66a46a2e 100644 --- a/packages/compartment-mapper/src/archive-lite.js +++ b/packages/compartment-mapper/src/archive-lite.js @@ -15,14 +15,11 @@ import { writeZip } from '@endo/zip'; import { resolve } from './node-module-specifier.js'; -import { compartmentMapForNodeModules } from './node-modules.js'; -import { search } from './search.js'; import { link } from './link.js'; import { exitModuleImportHookMaker, makeImportHookMaker, } from './import-hook.js'; -import { parseLocatedJson } from './json.js'; import { unpackReadPowers } from './powers.js'; import { assertCompartmentMap, @@ -272,19 +269,16 @@ export const makeArchiveCompartmentMap = (compartmentMap, sources) => { /** * @param {ReadFn | ReadPowers} powers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ArchiveOptions} [options] * @returns {Promise<{sources: Sources, compartmentMapBytes: Uint8Array, sha512?: string}>} */ -const digestLocation = async (powers, moduleLocation, options = {}) => { +const digestFromMap = async (powers, compartmentMap, options = {}) => { const { moduleTransforms, modules: exitModules = {}, - dev = false, - tags = new Set(), captureSourceLocation = undefined, searchSuffixes = undefined, - commonDependencies = undefined, importHook: exitModuleImportHook = undefined, policy = undefined, sourceMapHook = undefined, @@ -300,29 +294,6 @@ const digestLocation = async (powers, moduleLocation, options = {}) => { ); const { read, computeSha512 } = unpackReadPowers(powers); - const { - packageLocation, - packageDescriptorText, - packageDescriptorLocation, - moduleSpecifier, - } = await search(read, moduleLocation); - - tags.add('endo'); - tags.add('import'); - tags.add('default'); - - const packageDescriptor = parseLocatedJson( - packageDescriptorText, - packageDescriptorLocation, - ); - const compartmentMap = await compartmentMapForNodeModules( - powers, - packageLocation, - tags, - packageDescriptor, - moduleSpecifier, - { dev, commonDependencies, policy }, - ); const { compartments, @@ -337,7 +308,7 @@ const digestLocation = async (powers, moduleLocation, options = {}) => { exitModuleImportHook, }); - const makeImportHook = makeImportHookMaker(read, packageLocation, { + const makeImportHook = makeImportHookMaker(read, entryCompartmentName, { sources, compartmentDescriptors: compartments, archiveOnly: true, @@ -399,14 +370,18 @@ const digestLocation = async (powers, moduleLocation, options = {}) => { /** * @param {ReadFn | ReadPowers} powers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ArchiveOptions} [options] * @returns {Promise<{bytes: Uint8Array, sha512?: string}>} */ -export const makeAndHashArchive = async (powers, moduleLocation, options) => { - const { compartmentMapBytes, sources, sha512 } = await digestLocation( +export const makeAndHashArchiveFromMap = async ( + powers, + compartmentMap, + options, +) => { + const { compartmentMapBytes, sources, sha512 } = await digestFromMap( powers, - moduleLocation, + compartmentMap, options, ); @@ -420,25 +395,29 @@ export const makeAndHashArchive = async (powers, moduleLocation, options) => { /** * @param {ReadFn | ReadPowers} powers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const makeArchive = async (powers, moduleLocation, options) => { - const { bytes } = await makeAndHashArchive(powers, moduleLocation, options); +export const makeArchiveFromMap = async (powers, compartmentMap, options) => { + const { bytes } = await makeAndHashArchiveFromMap( + powers, + compartmentMap, + options, + ); return bytes; }; /** * @param {ReadFn | ReadPowers} powers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const mapLocation = async (powers, moduleLocation, options) => { - const { compartmentMapBytes } = await digestLocation( +export const mapFromMap = async (powers, compartmentMap, options) => { + const { compartmentMapBytes } = await digestFromMap( powers, - moduleLocation, + compartmentMap, options, ); return compartmentMapBytes; @@ -446,14 +425,14 @@ export const mapLocation = async (powers, moduleLocation, options) => { /** * @param {HashPowers} powers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const hashLocation = async (powers, moduleLocation, options) => { - const { compartmentMapBytes } = await digestLocation( +export const hashFromMap = async (powers, compartmentMap, options) => { + const { compartmentMapBytes } = await digestFromMap( powers, - moduleLocation, + compartmentMap, options, ); const { computeSha512 } = powers; @@ -464,16 +443,20 @@ export const hashLocation = async (powers, moduleLocation, options) => { * @param {WriteFn} write * @param {ReadFn | ReadPowers} readPowers * @param {string} archiveLocation - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ArchiveOptions} [options] */ -export const writeArchive = async ( +export const writeArchiveFromMap = async ( write, readPowers, archiveLocation, - moduleLocation, + compartmentMap, options, ) => { - const archiveBytes = await makeArchive(readPowers, moduleLocation, options); + const archiveBytes = await makeArchiveFromMap( + readPowers, + compartmentMap, + options, + ); await write(archiveLocation, archiveBytes); }; diff --git a/packages/compartment-mapper/src/archive.js b/packages/compartment-mapper/src/archive.js index 862f66551a..ac3a069360 100644 --- a/packages/compartment-mapper/src/archive.js +++ b/packages/compartment-mapper/src/archive.js @@ -1,16 +1,15 @@ // @ts-check import { defaultParserForLanguage } from './archive-parsers.js'; +import { mapNodeModules } from './node-modules.js'; import { - makeAndHashArchive as makeAndHashArchiveLite, - makeArchive as makeArchiveLite, - mapLocation as mapLocationLite, - hashLocation as hashLocationLite, - writeArchive as writeArchiveLite, + makeAndHashArchiveFromMap, + makeArchiveFromMap, + mapFromMap, + hashFromMap, + writeArchiveFromMap, } from './archive-lite.js'; -export { makeArchiveCompartmentMap } from './archive-lite.js'; - const { assign, create, freeze } = Object; /** @import {ArchiveOptions} from './types.js' */ @@ -38,12 +37,18 @@ const assignParserForLanguage = (options = {}) => { * @param {ArchiveOptions} [options] * @returns {Promise<{bytes: Uint8Array, sha512?: string}>} */ -export const makeAndHashArchive = async (powers, moduleLocation, options) => - makeAndHashArchiveLite( +export const makeAndHashArchive = async ( + powers, + moduleLocation, + options = {}, +) => { + const compartmentMap = await mapNodeModules(powers, moduleLocation, options); + return makeAndHashArchiveFromMap( powers, - moduleLocation, + compartmentMap, assignParserForLanguage(options), ); +}; /** * @param {ReadFn | ReadPowers} powers @@ -51,8 +56,22 @@ export const makeAndHashArchive = async (powers, moduleLocation, options) => * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const makeArchive = async (powers, moduleLocation, options) => - makeArchiveLite(powers, moduleLocation, assignParserForLanguage(options)); +export const makeArchive = async (powers, moduleLocation, options = {}) => { + const { dev, tags, commonDependencies, policy } = options; + + const compartmentMap = await mapNodeModules(powers, moduleLocation, { + dev, + tags, + commonDependencies, + policy, + }); + + return makeArchiveFromMap( + powers, + compartmentMap, + assignParserForLanguage(options), + ); +}; /** * @param {ReadFn | ReadPowers} powers @@ -60,8 +79,18 @@ export const makeArchive = async (powers, moduleLocation, options) => * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const mapLocation = async (powers, moduleLocation, options) => - mapLocationLite(powers, moduleLocation, assignParserForLanguage(options)); +export const mapLocation = async (powers, moduleLocation, options = {}) => { + const { dev, tags, commonDependencies, policy } = options; + + const compartmentMap = await mapNodeModules(powers, moduleLocation, { + dev, + tags, + commonDependencies, + policy, + }); + + return mapFromMap(powers, compartmentMap, assignParserForLanguage(options)); +}; /** * @param {HashPowers} powers @@ -69,8 +98,18 @@ export const mapLocation = async (powers, moduleLocation, options) => * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const hashLocation = async (powers, moduleLocation, options) => - hashLocationLite(powers, moduleLocation, assignParserForLanguage(options)); +export const hashLocation = async (powers, moduleLocation, options = {}) => { + const { dev, tags, commonDependencies, policy } = options; + + const compartmentMap = await mapNodeModules(powers, moduleLocation, { + dev, + tags, + commonDependencies, + policy, + }); + + return hashFromMap(powers, compartmentMap, assignParserForLanguage(options)); +}; /** * @param {WriteFn} write @@ -84,12 +123,20 @@ export const writeArchive = async ( readPowers, archiveLocation, moduleLocation, - options, -) => - writeArchiveLite( + options = {}, +) => { + const { dev, tags, commonDependencies, policy } = options; + const compartmentMap = await mapNodeModules(readPowers, moduleLocation, { + dev, + tags, + commonDependencies, + policy, + }); + return writeArchiveFromMap( write, readPowers, archiveLocation, - moduleLocation, + compartmentMap, assignParserForLanguage(options), ); +}; diff --git a/packages/compartment-mapper/src/import-lite.js b/packages/compartment-mapper/src/import-lite.js index 653a950734..cf556a0ea8 100644 --- a/packages/compartment-mapper/src/import-lite.js +++ b/packages/compartment-mapper/src/import-lite.js @@ -1,6 +1,7 @@ // @ts-check /* eslint no-shadow: "off" */ +/** @import {CompartmentMapDescriptor} from './types.js' */ /** @import {Application} from './types.js' */ /** @import {ImportLocationOptions} from './types.js' */ /** @import {LoadLocationOptions} from './types.js' */ @@ -9,36 +10,24 @@ /** @import {ReadPowers} from './types.js' */ /** @import {SomeObject} from './types.js' */ -import { compartmentMapForNodeModules } from './node-modules.js'; -import { search } from './search.js'; import { link } from './link.js'; import { exitModuleImportHookMaker, makeImportHookMaker, } from './import-hook.js'; -import { parseLocatedJson } from './json.js'; -import { unpackReadPowers } from './powers.js'; const { assign, create, freeze } = Object; /** * @param {ReadFn | ReadPowers} readPowers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {LoadLocationOptions} [options] * @returns {Promise} */ -export const loadLocation = async ( - readPowers, - moduleLocation, - options = {}, -) => { +export const loadFromMap = async (readPowers, compartmentMap, options = {}) => { const { moduleTransforms = {}, - dev = false, - tags = new Set(), searchSuffixes = undefined, - commonDependencies = undefined, - policy, parserForLanguage: parserForLanguageOption = {}, languageForExtension: languageForExtensionOption = {}, } = options; @@ -50,27 +39,9 @@ export const loadLocation = async ( assign(create(null), languageForExtensionOption), ); - const { read } = unpackReadPowers(readPowers); - const { - packageLocation, - packageDescriptorText, - packageDescriptorLocation, - moduleSpecifier, - } = await search(read, moduleLocation); - - const packageDescriptor = parseLocatedJson( - packageDescriptorText, - packageDescriptorLocation, - ); - const compartmentMap = await compartmentMapForNodeModules( - readPowers, - packageLocation, - tags, - packageDescriptor, - moduleSpecifier, - { dev, commonDependencies, policy }, - ); + entry: { compartment: entryCompartmentName, module: entryModuleSpecifier }, + } = compartmentMap; /** @type {ExecuteFn} */ const execute = async (options = {}) => { @@ -86,14 +57,18 @@ export const loadLocation = async ( modules, exitModuleImportHook, }); - const makeImportHook = makeImportHookMaker(readPowers, packageLocation, { - compartmentDescriptors: compartmentMap.compartments, - searchSuffixes, - archiveOnly: false, - entryCompartmentName: packageLocation, - entryModuleSpecifier: moduleSpecifier, - exitModuleImportHook: compartmentExitModuleImportHook, - }); + const makeImportHook = makeImportHookMaker( + readPowers, + entryCompartmentName, + { + compartmentDescriptors: compartmentMap.compartments, + searchSuffixes, + archiveOnly: false, + entryCompartmentName, + entryModuleSpecifier, + exitModuleImportHook: compartmentExitModuleImportHook, + }, + ); const { compartment, pendingJobsPromise } = link(compartmentMap, { makeImportHook, parserForLanguage, @@ -107,7 +82,7 @@ export const loadLocation = async ( await pendingJobsPromise; - return compartment.import(moduleSpecifier); + return compartment.import(entryModuleSpecifier); }; return { import: execute }; @@ -115,16 +90,16 @@ export const loadLocation = async ( /** * @param {ReadFn | ReadPowers} readPowers - * @param {string} moduleLocation + * @param {CompartmentMapDescriptor} compartmentMap * @param {ImportLocationOptions} [options] * @returns {Promise} the object of the imported modules exported * names. */ -export const importLocation = async ( +export const importFromMap = async ( readPowers, - moduleLocation, + compartmentMap, options = {}, ) => { - const application = await loadLocation(readPowers, moduleLocation, options); + const application = await loadFromMap(readPowers, compartmentMap, options); return application.import(options); }; diff --git a/packages/compartment-mapper/src/import.js b/packages/compartment-mapper/src/import.js index 6a96a35ca0..3a5f897826 100644 --- a/packages/compartment-mapper/src/import.js +++ b/packages/compartment-mapper/src/import.js @@ -1,10 +1,8 @@ // @ts-check import { defaultParserForLanguage } from './import-parsers.js'; -import { - loadLocation as loadLocationLite, - importLocation as importLocationLite, -} from './import-lite.js'; +import { mapNodeModules } from './node-modules.js'; +import { loadFromMap } from './import-lite.js'; const { assign, create, freeze } = Object; @@ -33,12 +31,24 @@ const assignParserForLanguage = (options = {}) => { * @param {ArchiveOptions} [options] * @returns {Promise} */ -export const loadLocation = async (readPowers, moduleLocation, options) => - loadLocationLite( +export const loadLocation = async ( + readPowers, + moduleLocation, + options = {}, +) => { + const { dev, tags, commonDependencies, policy } = options; + const compartmentMap = await mapNodeModules(readPowers, moduleLocation, { + dev, + tags, + commonDependencies, + policy, + }); + return loadFromMap( readPowers, - moduleLocation, + compartmentMap, assignParserForLanguage(options), ); +}; /** * @param {ReadFn | ReadPowers} readPowers @@ -47,9 +57,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => * @returns {Promise} the object of the imported modules exported * names. */ -export const importLocation = async (readPowers, moduleLocation, options) => - importLocationLite( - readPowers, - moduleLocation, - assignParserForLanguage(options), - ); +export const importLocation = async (readPowers, moduleLocation, options) => { + const application = await loadLocation(readPowers, moduleLocation, options); + return application.import(options); +}; diff --git a/packages/compartment-mapper/src/node-modules.js b/packages/compartment-mapper/src/node-modules.js index aaee187034..937a985b64 100644 --- a/packages/compartment-mapper/src/node-modules.js +++ b/packages/compartment-mapper/src/node-modules.js @@ -55,7 +55,7 @@ import { getPolicyForPackage, } from './policy.js'; import { unpackReadPowers } from './powers.js'; -import { searchDescriptor } from './search.js'; +import { search, searchDescriptor } from './search.js'; const { assign, create, keys, values } = Object; @@ -784,3 +784,44 @@ export const compartmentMapForNodeModules = async ( return compartmentMap; }; + +/** + * @param {ReadFn | ReadPowers} readPowers + * @param {string} moduleLocation + * @param {object} [options] + * @param {Set} [options.tags] + * @param {boolean} [options.dev] + * @param {object} [options.commonDependencies] + * @param {object} [options.policy] + * @returns {Promise} + */ +export const mapNodeModules = async ( + readPowers, + moduleLocation, + options = {}, +) => { + const { tags = new Set(), dev = false, commonDependencies, policy } = options; + + const { read } = unpackReadPowers(readPowers); + + const { + packageLocation, + packageDescriptorText, + packageDescriptorLocation, + moduleSpecifier, + } = await search(read, moduleLocation); + + const packageDescriptor = parseLocatedJson( + packageDescriptorText, + packageDescriptorLocation, + ); + + return compartmentMapForNodeModules( + readPowers, + packageLocation, + tags, + packageDescriptor, + moduleSpecifier, + { dev, commonDependencies, policy }, + ); +}; diff --git a/packages/compartment-mapper/test/snapshots/error-handling.test.js.md b/packages/compartment-mapper/test/snapshots/error-handling.test.js.md index bc36aa2aca..500b959374 100644 --- a/packages/compartment-mapper/test/snapshots/error-handling.test.js.md +++ b/packages/compartment-mapper/test/snapshots/error-handling.test.js.md @@ -31,9 +31,9 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async digestFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -44,9 +44,9 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async digestFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -57,10 +57,10 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async writeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async digestFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async writeArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` @@ -71,10 +71,10 @@ Generated by [AVA](https://avajs.dev). `TypeError: Failed to load module "./main.js" in package "file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/" (1 underlying failures: Cannot find external module "missing" in package file://.../compartment-mapper/test/fixtures-error-handling/node_modules/esm/␊ at throwAggregateError (file://.../ses/src/module-load.js:…)␊ at load (file://.../ses/src/module-load.js:…)␊ - at async digestLocation (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeAndHashArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async makeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ - at async writeArchive (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async digestFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeAndHashArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async makeArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ + at async writeArchiveFromMap (file://.../compartment-mapper/src/archive-lite.js:…)␊ at async file://.../compartment-mapper/test/scaffold.js:…␊ at async file://.../compartment-mapper/test/scaffold.js:…` diff --git a/packages/compartment-mapper/test/snapshots/error-handling.test.js.snap b/packages/compartment-mapper/test/snapshots/error-handling.test.js.snap index e940823a3ff20cf37c9823764fcca09308949948..376992fd849cca90cf901c0843fdf788e7d8b19d 100644 GIT binary patch literal 787 zcmV+u1MK`kRzV5SFhROOz_Nq6ytm%tCrC4SnT{AX~ zTDbCB;U9|#00000000B+n9Xj}Koo`BMhJ-=utvIM)k?7~wo!#tRjrD|&!!tz=yrS^ zPcoh{Gvoa1V8M=;C<`8pCqcHGVz+7Rw517Xvapn7=FH5w=d+W09-RrrM&|nuvfK)q z_98yDnI_|r)=GOZ%204QfB?uO5P(n^zEun@SCSivsfm>}U(dr3Eq>hTcD5dG<*)5W zTTgeM@0`w3I?9#$aEx3~2v!vukf<;d1YJK!kV}7Py1*r*$VNCI=tf*nKL~u^512|) z)HWe$y#&*gbYRKY;D)URQib%Xls5sHBTFmmZM94)xu@8qRrLqul83Ii$ zX^G-;X9+ik%Ry~l_qnYn003yg##&7d1_Mn4w540HTUp4E35;ezIoHeG^M|JY>&wr* zxkTaUigVD+Bx`dZ!!U}Jm{yb`aQ`C5pDs5fZT-fd&wtiFC5**QiW-%WV1dyUO z^S9T7*Xh-Q_oCy1x8cDH`GAZ))+%|2X-#6m`Eo@pYwrneX|ZGx7#W_Ri7$e3|MGu9 zcYO$++#W%ga2o?cswv`A2c!ei0r>#W#EIs%LS$aeo1cl_Y6y-Uf)2rd1;J|0{}Zdwl)tID8a7k@pYSE-Boo;GUIj>uLz2x_3>GeuInylt~)Y&B8Kb#GTS)ZoFGD|CX zzwD;8NbQR2TpEj#U7K22^i8qQdo@|8QyF+wek5b85qB8UP&xd)uj>af5yvaozkw+IJ<_qlgs%o`0S1)sU zs8kmR+pq$+8r`DV)5LOPv&WI`$aZ9}i|ody^h#YL$FgJDvAiypmvo61vfR`qax^=d R9nB9-^KXZ0n6kws008I=e^meg literal 784 zcmV+r1MmDnRzVm10Re}f|9JG*}5j(1}&0%h}718Y(IW}cKcq$r&9Bg{r-cTaFQnb zv6#5RkPXNfZGse4B!x+x3k+J3b4 zZ0E(!$uy_8rP2W2p^y}T)0GBfIw~YVKV%sS6%K75gn}IT2or*SEF=vW3&W6co#kj; zM#=>l<~bSW$U1h*RGHEdeW~RwBb%{4?7oIVMP%f(TuY2XmK_Ihgi2`#u}~4v#F0@b zuV$7BYlTW0^Lof>Jplkf2QD>we3&GL5_Gj&usb)9CCjYgtnM3>?uA1;`1SSY-b|u$ zbj3bsr;2x3kmVT1TFwOS+kY0duq|pL%e8SnY(8xF58I$EY#EN|(C}286EMip*xB2g z!Rx(j1n+6j2XDiJ7m0-I)lt&WSg^oS66?(mq;RxYvr3GLj?t#G;5?k}Lkg~sy5qZ3 z2V>zwiz4 zWKXgud9z4fQe)#o+gfAWe$^oN`SvBXYa@gY?G+*QsdKFLsLL+Zt%FqS(c(?2o+k#g zTuY&-D>xlWOtsT(B_{86XRnw1&aAy&slrk;e2;sZgExl4T$g}KO_AIZ953K OrujE{0?P)2B>({5aD7Pt From 89de68d627eb1005bd36c35ce7c2547b4a0aafa2 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Mon, 3 Jun 2024 16:18:19 -0700 Subject: [PATCH 6/9] docs(compartment-mapper): Announce lite modules --- packages/compartment-mapper/NEWS.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/compartment-mapper/NEWS.md b/packages/compartment-mapper/NEWS.md index ec11bab989..9c0387e180 100644 --- a/packages/compartment-mapper/NEWS.md +++ b/packages/compartment-mapper/NEWS.md @@ -12,6 +12,15 @@ User-visible changes to the compartment mapper: application and the `"parsers"` property in individual `package.json` descriptors may extend or override using any of the configured or built-in language parser names. +- Exports `import-lite.js`, `archive-lite.js`, `import-archive-lite.js`, + `import-parsers.js`, `archive-parsers.js`, `import-archive-parsers.js`, and + `node-modules.js`, allowing these to be mixed and matched. + The existing `import.js`, `archive.js`, and `import-archive.js` all entrain + by import their corresponding default behaviors, where the new modules do + not. + For example, `import-parsers.js` does not entrain Babel. + The new `import-lite.js` does not entrain `node-modules.js` and composes + with potential alternative package discovery, storage, and locks. # 0.9.0 (2023-08-07) From 1716b2114aeeebcc69706aa6490f9330a7ee825f Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Mon, 3 Jun 2024 17:38:12 -0700 Subject: [PATCH 7/9] refactor(compartment-mapper): Separate lines for type imports --- packages/compartment-mapper/src/import-archive.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/compartment-mapper/src/import-archive.js b/packages/compartment-mapper/src/import-archive.js index 022325a7c0..426a91abaa 100644 --- a/packages/compartment-mapper/src/import-archive.js +++ b/packages/compartment-mapper/src/import-archive.js @@ -9,12 +9,19 @@ import { const { assign, create, freeze } = Object; -/** @import {Application, ComputeSourceLocationHook, ComputeSourceMapLocationHook, ExecuteOptions, ExitModuleImportHook, HashFn, LoadArchiveOptions, ReadPowers} from './types.js' */ +/** @import {Application} from './types.js' */ +/** @import {ComputeSourceLocationHook} from './types.js' */ +/** @import {ComputeSourceMapLocationHook} from './types.js' */ +/** @import {ExecuteOptions} from './types.js' */ +/** @import {ExitModuleImportHook} from './types.js' */ +/** @import {HashFn} from './types.js' */ +/** @import {LoadArchiveOptions} from './types.js' */ +/** @import {ReadPowers} from './types.js' */ /** @import {ParserForLanguage} from './types.js' */ -// 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. +// Must give the type of Compartment 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. // /** @typedef {typeof Compartment} CompartmentConstructor */ From 007b1a71b560b3a059231e4941de2be7c33c3593 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Mon, 3 Jun 2024 22:58:38 -0700 Subject: [PATCH 8/9] docs(compartment-mapper): Add overview for every module --- .../compartment-mapper/src/archive-lite.js | 30 +++++++++++++++++++ .../compartment-mapper/src/archive-parsers.js | 6 ++++ packages/compartment-mapper/src/archive.js | 15 ++++++++++ packages/compartment-mapper/src/bundle-cjs.js | 1 + packages/compartment-mapper/src/bundle-mjs.js | 2 ++ .../compartment-mapper/src/compartment-map.js | 2 ++ packages/compartment-mapper/src/extension.js | 2 ++ .../src/import-archive-lite.js | 13 ++++++++ .../src/import-archive-parsers.js | 6 ++++ .../compartment-mapper/src/import-archive.js | 15 ++++++++++ .../compartment-mapper/src/import-hook.js | 8 +++++ .../compartment-mapper/src/import-lite.js | 16 ++++++++++ .../compartment-mapper/src/import-parsers.js | 6 ++++ packages/compartment-mapper/src/import.js | 12 ++++++++ .../compartment-mapper/src/infer-exports.js | 7 +++++ packages/compartment-mapper/src/json.js | 1 + packages/compartment-mapper/src/link.js | 9 ++++++ .../src/node-module-specifier.js | 5 ++++ .../compartment-mapper/src/node-modules.js | 10 +++++++ .../compartment-mapper/src/node-powers.js | 8 +++++ .../src/parse-archive-cjs.js | 4 +++ .../src/parse-archive-mjs.js | 3 ++ .../compartment-mapper/src/parse-bytes.js | 4 +++ .../src/parse-cjs-shared-export-wrapper.js | 5 ++++ packages/compartment-mapper/src/parse-cjs.js | 4 +++ packages/compartment-mapper/src/parse-json.js | 2 ++ packages/compartment-mapper/src/parse-mjs.js | 2 ++ .../compartment-mapper/src/parse-pre-cjs.js | 6 ++++ .../compartment-mapper/src/parse-pre-mjs.js | 6 ++++ packages/compartment-mapper/src/parse-text.js | 5 ++++ .../compartment-mapper/src/policy-format.js | 4 +++ packages/compartment-mapper/src/policy.js | 3 ++ packages/compartment-mapper/src/powers.js | 7 +++++ packages/compartment-mapper/src/search.js | 4 +++ packages/compartment-mapper/src/url.js | 5 ++++ 35 files changed, 238 insertions(+) diff --git a/packages/compartment-mapper/src/archive-lite.js b/packages/compartment-mapper/src/archive-lite.js index 8f66a46a2e..000790718d 100644 --- a/packages/compartment-mapper/src/archive-lite.js +++ b/packages/compartment-mapper/src/archive-lite.js @@ -1,3 +1,33 @@ +/* Provides functions to create an archive (zip file with a + * compartment-map.json) from a partially completed compartment map (it must + * mention all packages/compartments as well as inter-compartment references + * but does not contain an entry for every module reachable from its entry + * module) and the means to read them from the containing file system. + * + * These functions do not have a bias for any particular mapping, so you will + * need to use `mapNodeModules` from `@endo/compartment-map/node-modules.js` or + * a similar device to construct one. + * + * The default `parserForLanguage` mapping is empty. + * You will need to provide the `defaultParserForLanguage` from + * `@endo/compartment-mapper/import-parsers.js` or + * `@endo/compartment-mapper/archive-parsers.js`. + * + * If you use `@endo/compartment-mapper/archive-parsers.js`, the archive + * will contain pre-compiled ESM and CJS modules wrapped in a JSON + * envelope, suitable for use with the SES shim in any environment + * including a web page, without a client-side dependency on Babel. + * + * If you use `@endo/compartment-mapper/import-parsers.js`, the archive + * will contain original sources, so to import the archive with + * `src/import-archive-lite.js`, you will need to provide the archive parsers + * and entrain a runtime dependency on Babel. + * + * In fruition of https://github.com/endojs/endo/issues/400, we will be able to + * use original source archives on XS and Node.js, but not on the web until the + * web platform makes further progress on virtual module loaers. + */ + // @ts-check /* eslint no-shadow: 0 */ diff --git a/packages/compartment-mapper/src/archive-parsers.js b/packages/compartment-mapper/src/archive-parsers.js index 9519cdfc02..54ff2a4eeb 100644 --- a/packages/compartment-mapper/src/archive-parsers.js +++ b/packages/compartment-mapper/src/archive-parsers.js @@ -1,3 +1,9 @@ +/* Provides a set of default language behaviors (parsers) suitable for creating + * archives (zip files with a `compartment-map.json` and a file for each + * module) with pre-compiled sources. + * + * This module entrains a dependency upon the core of Babel. + */ /** @import {ParserForLanguage} from './types.js' */ import parserJson from './parse-json.js'; diff --git a/packages/compartment-mapper/src/archive.js b/packages/compartment-mapper/src/archive.js index ac3a069360..9eab2f1d3a 100644 --- a/packages/compartment-mapper/src/archive.js +++ b/packages/compartment-mapper/src/archive.js @@ -1,3 +1,18 @@ +/* Provides mechanisms for creating archives (zip files with a + * `compartmeent-map.json` and a file for every static dependency of an entry + * module). + * + * Uses the conventions of Node.js package managers and the `node_modules`. + * Archives will contain a copy for every physical copy of a package on disk, + * such that importing will construct a separate instance for each. + * + * These archives will contain pre-compiled ESM and CJS and this module + * entrains a dependency on the core of Babel. + * + * See `archive-lite.js` for a variation that does not depend on Babel + * for cases like XS native Compartments where pre-compilation is not + * necessary or where the dependency on Babel can be dererred to runtime. + */ // @ts-check import { defaultParserForLanguage } from './archive-parsers.js'; diff --git a/packages/compartment-mapper/src/bundle-cjs.js b/packages/compartment-mapper/src/bundle-cjs.js index 2f3d6635a4..25dbb64321 100644 --- a/packages/compartment-mapper/src/bundle-cjs.js +++ b/packages/compartment-mapper/src/bundle-cjs.js @@ -1,3 +1,4 @@ +/* Provides CommonJS support for `bundle.js`. */ // @ts-nocheck /** quotes strings */ const q = JSON.stringify; diff --git a/packages/compartment-mapper/src/bundle-mjs.js b/packages/compartment-mapper/src/bundle-mjs.js index 5e87d55cfe..28cd8591a0 100644 --- a/packages/compartment-mapper/src/bundle-mjs.js +++ b/packages/compartment-mapper/src/bundle-mjs.js @@ -1,3 +1,5 @@ +/* Provides ESM support for `bundle.js`. */ + /** quotes strings */ const q = JSON.stringify; diff --git a/packages/compartment-mapper/src/compartment-map.js b/packages/compartment-mapper/src/compartment-map.js index a866848acc..bf85596733 100644 --- a/packages/compartment-mapper/src/compartment-map.js +++ b/packages/compartment-mapper/src/compartment-map.js @@ -1,3 +1,5 @@ +/* Validates a compartment map against its schema. */ + // @ts-check /// diff --git a/packages/compartment-mapper/src/extension.js b/packages/compartment-mapper/src/extension.js index b8ffbb274d..398eaa7abb 100644 --- a/packages/compartment-mapper/src/extension.js +++ b/packages/compartment-mapper/src/extension.js @@ -1,3 +1,5 @@ +/* Extracts the extension from a URL pathname. */ + // @ts-check /** diff --git a/packages/compartment-mapper/src/import-archive-lite.js b/packages/compartment-mapper/src/import-archive-lite.js index 45e69d7fe4..01de9ff02c 100644 --- a/packages/compartment-mapper/src/import-archive-lite.js +++ b/packages/compartment-mapper/src/import-archive-lite.js @@ -1,3 +1,16 @@ +/* Provides functions for evaluating the modules in an archive (a zip + * file with a `compartment-map.json` and a file for each module it contains.) + * + * These functions do not have a bias for any particular mapping, so you will + * need to use `mapNodeModules` from `@endo/compartment-map/node-modules.js` or + * a similar device to construct one. + * + * The default `parserForLanguage` mapping is empty. + * You will need to provide the `defaultParserForLanguage` from + * `@endo/compartment-mapper/import-parsers.js` or + * `@endo/compartment-mapper/archive-parsers.js`. + */ + // @ts-check /* eslint no-shadow: "off" */ diff --git a/packages/compartment-mapper/src/import-archive-parsers.js b/packages/compartment-mapper/src/import-archive-parsers.js index f08de871d1..a560afbf84 100644 --- a/packages/compartment-mapper/src/import-archive-parsers.js +++ b/packages/compartment-mapper/src/import-archive-parsers.js @@ -1,3 +1,9 @@ +/* Provides a set of default language behaviors (parsers) suitable for + * evaluating archives (zip files with a `compartment-map.json` and a file for + * each module) with pre-compiled ESM and CommonJS. + * + * This module does not entrain a dependency on Babel. + */ /** @import {ParserForLanguage} from './types.js' */ import parserPreCjs from './parse-pre-cjs.js'; diff --git a/packages/compartment-mapper/src/import-archive.js b/packages/compartment-mapper/src/import-archive.js index 426a91abaa..98e6c2e484 100644 --- a/packages/compartment-mapper/src/import-archive.js +++ b/packages/compartment-mapper/src/import-archive.js @@ -1,3 +1,18 @@ +/* Provides functions for evaluating modules in an archive (a zip file + * with a `compartment-map.json` and a file for a module and each of its + * transitive dependencies.) + * + * These functions accept the URL of an entry module and find its transitive + * dependencies through the Node.js `node_modules` conventions. + * + * These functions use the default parsers in `import-archive-parsers.js`, + * which support only pre-compiled ESM and CommonJS. + * + * See `import-archive-lite.js` for functions that are not coupled to these + * parsers or the `node_modules` conventions without necessarily entraining a + * dependency on Babel. + */ + // @ts-check import { defaultParserForLanguage } from './import-archive-parsers.js'; diff --git a/packages/compartment-mapper/src/import-hook.js b/packages/compartment-mapper/src/import-hook.js index 63db287814..889318a295 100644 --- a/packages/compartment-mapper/src/import-hook.js +++ b/packages/compartment-mapper/src/import-hook.js @@ -1,3 +1,11 @@ +/* Provides the implementation of each compartment's `importHook` when using + * `import.js`, `import-lite.js`, `archive.js`, or `archive-lite.js`. + * However, `import-archive.js` and `import-archive-lite.js` use their own variant. + * + * For building archives, these import hooks create a table of all the modules + * in a "working set" of transitive dependencies. + */ + // @ts-check /** @import {ImportHook} from 'ses' */ diff --git a/packages/compartment-mapper/src/import-lite.js b/packages/compartment-mapper/src/import-lite.js index cf556a0ea8..68c80143a3 100644 --- a/packages/compartment-mapper/src/import-lite.js +++ b/packages/compartment-mapper/src/import-lite.js @@ -1,3 +1,19 @@ +/* Provides functions for evaluating a module and its transitive dependencies + * given a partially completed compartment map. + * The compartment map needs to describe every reachable compartment, where to + * find modules in that compartment, and how to link modules between + * compartments, but does not need to capture a module descriptor for every + * module in the working set of transitive dependencies from the entry module. + * + * These functions do not have a bias for any particular mapping, so you will + * need to use `mapNodeModules` from `@endo/compartment-map/node-modules.js` or + * a similar device to construct one. + * + * The default `parserForLanguage` mapping is empty. + * You will need to provide the `defaultParserForLanguage` from + * `@endo/compartment-mapper/import-parsers.js` or similar. + */ + // @ts-check /* eslint no-shadow: "off" */ diff --git a/packages/compartment-mapper/src/import-parsers.js b/packages/compartment-mapper/src/import-parsers.js index 681a0c2b78..99c5f04cc5 100644 --- a/packages/compartment-mapper/src/import-parsers.js +++ b/packages/compartment-mapper/src/import-parsers.js @@ -1,3 +1,9 @@ +/* Provides a set of default language behaviors (parsers) suitable for + * evaluating modules that have not been pre-compiled. + * + * This module entrains a dependency upon the core of Babel. + */ + /** @import {ParserForLanguage} from './types.js' */ import parserJson from './parse-json.js'; diff --git a/packages/compartment-mapper/src/import.js b/packages/compartment-mapper/src/import.js index 3a5f897826..578a1f7265 100644 --- a/packages/compartment-mapper/src/import.js +++ b/packages/compartment-mapper/src/import.js @@ -1,3 +1,15 @@ +/* Provides functions for evaluating a module and its transitive + * dependencies given the URL of the entry module and assuming packages + * laid out according to the `node_modules` conventions. + * + * To import modules according to any other convention, use `import-lite.js` + * and provide a compartment map with a custom analog to `mapNodeModules` from + * `node-modules.js`. + * + * The default `parserForLanguage` is `import-parsers.js`, which is suitable + * for most cases. + */ + // @ts-check import { defaultParserForLanguage } from './import-parsers.js'; diff --git a/packages/compartment-mapper/src/infer-exports.js b/packages/compartment-mapper/src/infer-exports.js index 40f0108e53..436a14577b 100644 --- a/packages/compartment-mapper/src/infer-exports.js +++ b/packages/compartment-mapper/src/infer-exports.js @@ -1,3 +1,10 @@ +/* Provides functions needed by `node-modules.js` for building + * inter-compartment linkage according to the specifications in a + * `package.json` as laid out in the `node_modules` convention. + * These functions implement the behavior for a package's `"main"`, + * `"browser"`, `"imports"`, and `"exports"` properties in a `package.json`. + */ + // @ts-check /** @import {LanguageForExtension} from './types.js' */ diff --git a/packages/compartment-mapper/src/json.js b/packages/compartment-mapper/src/json.js index af6b991c17..e612f44b1d 100644 --- a/packages/compartment-mapper/src/json.js +++ b/packages/compartment-mapper/src/json.js @@ -1,3 +1,4 @@ +/* Annotates JSON parse exceptions with the location of the source. */ // @ts-check /** diff --git a/packages/compartment-mapper/src/link.js b/packages/compartment-mapper/src/link.js index 8f14309207..d1bd8646ac 100644 --- a/packages/compartment-mapper/src/link.js +++ b/packages/compartment-mapper/src/link.js @@ -1,3 +1,11 @@ +/* Provides the linking behavior shared by all Compartment Mapper workflows. + * This involves creating and configuring compartments according to the + * specifications in a compartment map, and is suitable for compartment maps + * that just outline the locations of compartments and their inter-linkage and + * also compartment maps that include every module descriptor in the transitive + * dependencies of their entry module. + */ + // @ts-check /** @import {ModuleMapHook} from 'ses' */ @@ -494,6 +502,7 @@ export const link = ( /** * @param {CompartmentMapDescriptor} compartmentMap * @param {LinkOptions} options + * @deprecated Use {@link link}. */ export const assemble = (compartmentMap, options) => link(compartmentMap, options).compartment; diff --git a/packages/compartment-mapper/src/node-module-specifier.js b/packages/compartment-mapper/src/node-module-specifier.js index af4dece2de..cf85401ed7 100644 --- a/packages/compartment-mapper/src/node-module-specifier.js +++ b/packages/compartment-mapper/src/node-module-specifier.js @@ -1,3 +1,8 @@ +/* Provides functions for interacting with Node.js module specifiers in + * their canonical form. + * This is a kind of path math that is platform-agnostic. + */ + // @ts-check // q, as in quote, for error messages. diff --git a/packages/compartment-mapper/src/node-modules.js b/packages/compartment-mapper/src/node-modules.js index 937a985b64..5509d3cd35 100644 --- a/packages/compartment-mapper/src/node-modules.js +++ b/packages/compartment-mapper/src/node-modules.js @@ -1,3 +1,13 @@ +/* Provides functions for constructing a compartment map that has a compartment + * descriptor corresponding to every reachable package from an entry module and + * how to create links between them. + * The resulting compartment map does not describe individual modules but does + * capture every usable route between packages including those generalized by + * wildcard expansion. + * See {@link link} to expand a compartment map to capture module descriptors + * for transitive dependencies. + */ + // @ts-check /* eslint no-shadow: 0 */ diff --git a/packages/compartment-mapper/src/node-powers.js b/packages/compartment-mapper/src/node-powers.js index 92c558a7cd..1ea242cdb6 100644 --- a/packages/compartment-mapper/src/node-powers.js +++ b/packages/compartment-mapper/src/node-powers.js @@ -1,3 +1,11 @@ +/* Provides adapters for Compartment Mapper I/O to the corresponding Node.js + * implementations of those behaviors. + * + * The Compartment Mapper generalizes its I/O interface to allow for a wide + * variety of I/O providers, but especially for reading and writing from + * virtualized file systems like zip files. + */ + // @ts-check /** @import {ReadPowers} from './types.js' */ diff --git a/packages/compartment-mapper/src/parse-archive-cjs.js b/packages/compartment-mapper/src/parse-archive-cjs.js index 6a710c1d66..b0881cb02c 100644 --- a/packages/compartment-mapper/src/parse-archive-cjs.js +++ b/packages/compartment-mapper/src/parse-archive-cjs.js @@ -1,3 +1,7 @@ +/* Provides language behavior for analyzing, pre-compiling, and storing + * CommonJS modules for an archive. + */ + // @ts-check import { analyzeCommonJS } from '@endo/cjs-module-analyzer'; diff --git a/packages/compartment-mapper/src/parse-archive-mjs.js b/packages/compartment-mapper/src/parse-archive-mjs.js index 9b70fe23f9..c5a2a0ff04 100644 --- a/packages/compartment-mapper/src/parse-archive-mjs.js +++ b/packages/compartment-mapper/src/parse-archive-mjs.js @@ -1,3 +1,6 @@ +/* Provides language behavior for analyzing, pre-compiling, and storing ESM + * modules for an archive. + */ // @ts-check import { StaticModuleRecord } from '@endo/static-module-record'; diff --git a/packages/compartment-mapper/src/parse-bytes.js b/packages/compartment-mapper/src/parse-bytes.js index e86977b2d2..d2de12e733 100644 --- a/packages/compartment-mapper/src/parse-bytes.js +++ b/packages/compartment-mapper/src/parse-bytes.js @@ -1,3 +1,7 @@ +/* Provides rudimentary support for treating an arbitrary file as a module that + * exports the bytes of that file. + */ + // @ts-check /** diff --git a/packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js b/packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js index 9ea24a0bea..2826b6ccd4 100644 --- a/packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js +++ b/packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js @@ -1,3 +1,8 @@ +/* Provides shared functionality for {@link parse-cjs.js} and {@link + * parse-archive-cjs.js} toward importing or archiving CommonJS as a virtual + * module source. + */ + // @ts-check /** @import {ReadFn} from './types.js' */ diff --git a/packages/compartment-mapper/src/parse-cjs.js b/packages/compartment-mapper/src/parse-cjs.js index 6005a9d7fc..dccd7d0cea 100644 --- a/packages/compartment-mapper/src/parse-cjs.js +++ b/packages/compartment-mapper/src/parse-cjs.js @@ -1,3 +1,7 @@ +/* Provides language behavior (parser) for importing CommonJS as a virtual + * module source. + */ + // @ts-check import { analyzeCommonJS } from '@endo/cjs-module-analyzer'; diff --git a/packages/compartment-mapper/src/parse-json.js b/packages/compartment-mapper/src/parse-json.js index de83e7a756..1a315a3fcd 100644 --- a/packages/compartment-mapper/src/parse-json.js +++ b/packages/compartment-mapper/src/parse-json.js @@ -1,3 +1,5 @@ +/* Provides language support for importing JSON modules. */ + // @ts-check import { parseLocatedJson } from './json.js'; diff --git a/packages/compartment-mapper/src/parse-mjs.js b/packages/compartment-mapper/src/parse-mjs.js index d5aa2ed106..95f663e9ba 100644 --- a/packages/compartment-mapper/src/parse-mjs.js +++ b/packages/compartment-mapper/src/parse-mjs.js @@ -1,3 +1,5 @@ +/* Provides language behavior (a parser) for importing ESM. */ + // @ts-check import { StaticModuleRecord } from '@endo/static-module-record'; diff --git a/packages/compartment-mapper/src/parse-pre-cjs.js b/packages/compartment-mapper/src/parse-pre-cjs.js index 21adaa2947..d6e02f3068 100644 --- a/packages/compartment-mapper/src/parse-pre-cjs.js +++ b/packages/compartment-mapper/src/parse-pre-cjs.js @@ -1,3 +1,9 @@ +/* Provides language-specific behavior for importing pre-compiled CommonJS. + * Pre-compiled CommonJS is a module in JSON format that describes its imports, + * exports, and source to execute in the presence of `require`, `module`, and + * `exports`. + */ + // @ts-check import { parseLocatedJson } from './json.js'; diff --git a/packages/compartment-mapper/src/parse-pre-mjs.js b/packages/compartment-mapper/src/parse-pre-mjs.js index 160d8531ba..331927b83c 100644 --- a/packages/compartment-mapper/src/parse-pre-mjs.js +++ b/packages/compartment-mapper/src/parse-pre-mjs.js @@ -1,3 +1,9 @@ +/* Provides language-specific behaviors for importing pre-compiled ESM. + * Pre-compiling or translating ESM from a module to a script with a + * calling-convention is necessary to prepare an archive so that it can be + * imported by the SES shim without entraining a dependency on Babel. + */ + // @ts-check import { parseLocatedJson } from './json.js'; diff --git a/packages/compartment-mapper/src/parse-text.js b/packages/compartment-mapper/src/parse-text.js index 32a9b5ffea..301b7f6042 100644 --- a/packages/compartment-mapper/src/parse-text.js +++ b/packages/compartment-mapper/src/parse-text.js @@ -1,3 +1,8 @@ +/* Provides language-behaviors for importing a module as a document that + * exports itself as a string based on a UTF-8 interpretation of the module's + * text. + */ + // @ts-check /** diff --git a/packages/compartment-mapper/src/policy-format.js b/packages/compartment-mapper/src/policy-format.js index 346c889ef8..abc069b5e7 100644 --- a/packages/compartment-mapper/src/policy-format.js +++ b/packages/compartment-mapper/src/policy-format.js @@ -1,3 +1,7 @@ +/* Provides functions for enforcing compartment-map linkage and global variable + * policies for each compartment. + */ + // @ts-check /** @import {SomePackagePolicy} from './types.js' */ diff --git a/packages/compartment-mapper/src/policy.js b/packages/compartment-mapper/src/policy.js index ce724a171f..ee2a8fb52d 100644 --- a/packages/compartment-mapper/src/policy.js +++ b/packages/compartment-mapper/src/policy.js @@ -1,3 +1,6 @@ +/* Provides mechanisms for interacting with Compartment Map runtime policies. + */ + // @ts-check import { diff --git a/packages/compartment-mapper/src/powers.js b/packages/compartment-mapper/src/powers.js index 887299acab..be37b6c235 100644 --- a/packages/compartment-mapper/src/powers.js +++ b/packages/compartment-mapper/src/powers.js @@ -1,3 +1,10 @@ +/* As the interface of the Compartment Mapper evolved, it became necessary to + * expand some function signatures that accepted a single power to one that + * accepted a powers object. + * This module provides functions for safely unpacking I/O capabilities and + * maintaining backward-compatibility with older accepted usage patterns. + */ + // @ts-check /** @type {import('./types.js').CanonicalFn} */ diff --git a/packages/compartment-mapper/src/search.js b/packages/compartment-mapper/src/search.js index 4a9606632b..098516cd96 100644 --- a/packages/compartment-mapper/src/search.js +++ b/packages/compartment-mapper/src/search.js @@ -1,3 +1,7 @@ +/* Provides the behavior for `node-modules.js` to find modules and packages + * according to the Node.js `node_modules` convention. + */ + // @ts-check /** @import {ReadFn} from './types.js' */ diff --git a/packages/compartment-mapper/src/url.js b/packages/compartment-mapper/src/url.js index e16c0ec509..6942d7679a 100644 --- a/packages/compartment-mapper/src/url.js +++ b/packages/compartment-mapper/src/url.js @@ -1,3 +1,8 @@ +/* Minimal support for URL manipulation beyond what most hosts provide + * natively, used to normalize URLs before capturing them in a platform- and + * location-agnostic archive. + */ + // @ts-check // Derrived from https://github.com/junosuarez/url-relative From d241fcb4e43dd6d29984c5a9893f340f7b304a81 Mon Sep 17 00:00:00 2001 From: Kris Kowal Date: Tue, 4 Jun 2024 11:27:48 -0700 Subject: [PATCH 9/9] refactor(compartment-mapper): Remove superfluous type imports --- packages/compartment-mapper/src/bundle.js | 1 - packages/compartment-mapper/src/import-hook.js | 3 --- 2 files changed, 4 deletions(-) diff --git a/packages/compartment-mapper/src/bundle.js b/packages/compartment-mapper/src/bundle.js index cf24553a52..87e823a953 100644 --- a/packages/compartment-mapper/src/bundle.js +++ b/packages/compartment-mapper/src/bundle.js @@ -5,7 +5,6 @@ /** @import {CompartmentDescriptor} from './types.js' */ /** @import {CompartmentSources} from './types.js' */ /** @import {ReadFn} from './types.js' */ -/** @import {ModuleTransforms} from './types.js' */ /** @import {Sources} from './types.js' */ /** @import {WriteFn} from './types.js' */ /** @import {ArchiveOptions} from './types.js' */ diff --git a/packages/compartment-mapper/src/import-hook.js b/packages/compartment-mapper/src/import-hook.js index 889318a295..5d73be530d 100644 --- a/packages/compartment-mapper/src/import-hook.js +++ b/packages/compartment-mapper/src/import-hook.js @@ -11,15 +11,12 @@ /** @import {ImportHook} from 'ses' */ /** @import {StaticModuleType} from 'ses' */ /** @import {RedirectStaticModuleInterface} from 'ses' */ -/** @import {ThirdPartyStaticModuleInterface} from 'ses' */ /** @import {ReadFn} from './types.js' */ /** @import {ReadPowers} from './types.js' */ /** @import {HashFn} from './types.js' */ /** @import {Sources} from './types.js' */ -/** @import {CompartmentSources} from './types.js' */ /** @import {CompartmentDescriptor} from './types.js' */ /** @import {ImportHookMaker} from './types.js' */ -/** @import {DeferredAttenuatorsProvider} from './types.js' */ /** @import {ExitModuleImportHook} from './types.js' */ import { attenuateModuleHook, enforceModulePolicy } from './policy.js';