From 2135d35b6ff7d8146ffb925a75c05cdffbefe9f4 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Fri, 7 Jun 2024 12:09:18 -0700 Subject: [PATCH] chore: WIP fixing conflicts --- .../compartment-mapper/src/import-hook.js | 2 +- .../compartment-mapper/src/import-lite.js | 22 ++++++++++++-- packages/compartment-mapper/src/link.js | 29 ++++++++++++++----- .../compartment-mapper/src/node-modules.js | 2 ++ packages/compartment-mapper/src/parse-json.js | 10 +++++-- packages/compartment-mapper/src/powers.js | 13 +++++---- packages/compartment-mapper/src/types.js | 1 + 7 files changed, 59 insertions(+), 20 deletions(-) diff --git a/packages/compartment-mapper/src/import-hook.js b/packages/compartment-mapper/src/import-hook.js index c72915d6c3..77df8f9c9c 100644 --- a/packages/compartment-mapper/src/import-hook.js +++ b/packages/compartment-mapper/src/import-hook.js @@ -27,7 +27,7 @@ import { attenuateModuleHook, enforceModulePolicy } from './policy.js'; import { resolve } from './node-module-specifier.js'; import { unpackReadPowers } from './powers.js'; -import { DYNAMIC_POLICY_VALUE } from './policy-format.js'; +// import { DYNAMIC_POLICY_VALUE } from './policy-format.js'; // q, as in quote, for quoting strings in error messages. const q = JSON.stringify; diff --git a/packages/compartment-mapper/src/import-lite.js b/packages/compartment-mapper/src/import-lite.js index 68c80143a3..7d73361cf4 100644 --- a/packages/compartment-mapper/src/import-lite.js +++ b/packages/compartment-mapper/src/import-lite.js @@ -17,7 +17,7 @@ // @ts-check /* eslint no-shadow: "off" */ -/** @import {CompartmentMapDescriptor} from './types.js' */ +/** @import {ArchiveOptions, CompartmentMapDescriptor, ModuleTransforms, SyncArchiveOptions} from './types.js' */ /** @import {Application} from './types.js' */ /** @import {ImportLocationOptions} from './types.js' */ /** @import {LoadLocationOptions} from './types.js' */ @@ -42,7 +42,7 @@ const { assign, create, freeze } = Object; */ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => { const { - moduleTransforms = {}, + syncModuleTransforms = {}, searchSuffixes = undefined, parserForLanguage: parserForLanguageOption = {}, languageForExtension: languageForExtensionOption = {}, @@ -55,6 +55,24 @@ export const loadFromMap = async (readPowers, compartmentMap, options = {}) => { assign(create(null), languageForExtensionOption), ); + /** + * This type guard determines which of the two paths through the code is taken. + * + * If `options` is `SyncArchiveOptions`, we will permit dynamic requires. By definition, this must not include async module transforms, and must have a non-empty `dynamicHook` + * + * If `options` isn't `SyncArchiveOptions`, then no. + * + * @param {ArchiveOptions|SyncArchiveOptions} value + * @returns {value is SyncArchiveOptions} + */ + const isSyncOptions = value => 'dynamicHook' in value; + + const moduleTransforms = isSyncOptions(options) + ? undefined + : /** @type {ModuleTransforms} */ ({ + ...syncModuleTransforms, + ...(options.moduleTransforms || {}), + }); const { entry: { compartment: entryCompartmentName, module: entryModuleSpecifier }, } = compartmentMap; diff --git a/packages/compartment-mapper/src/link.js b/packages/compartment-mapper/src/link.js index 5908edeb5b..859707b350 100644 --- a/packages/compartment-mapper/src/link.js +++ b/packages/compartment-mapper/src/link.js @@ -10,7 +10,14 @@ /** @import {ImportNowHook} from 'ses' */ /** @import {ModuleMapHook} from 'ses' */ -/** @import {ParseFn, ParserForLanguage} from './types.js' */ +/** @import {ImportNowHookMaker} from './types.js' */ +/** @import {Language} from './types.js' */ +/** @import {LinkResult} from './types.js' */ +/** @import {ParseFn} from './types.js' */ +/** @import {ParseFnAsync} from './types.js' */ +/** @import {ParserForLanguage} from './types.js' */ +/** @import {SyncLinkOptions} from './types.js' */ +/** @import {SyncModuleTransforms} from './types.js' */ /** @import {ParserImplementation} from './types.js' */ /** @import {ShouldDeferError} from './types.js' */ /** @import {ModuleTransforms} from './types.js' */ @@ -581,13 +588,19 @@ export const link = ( languageForExtensionOverrides, ), ); - - const parse = mapParsers( - languageForExtension, - languageForModuleSpecifier, - parserForLanguage, - moduleTransforms, - ); + const parse = isSync + ? mapParsersSync( + languageForExtension, + languageForModuleSpecifier, + parserForLanguage, + syncModuleTransforms, + ) + : mapParsers( + languageForExtension, + languageForModuleSpecifier, + parserForLanguage, + moduleTransforms, + ); /** @type {ShouldDeferError} */ const shouldDeferError = language => { if (language && has(parserForLanguage, language)) { diff --git a/packages/compartment-mapper/src/node-modules.js b/packages/compartment-mapper/src/node-modules.js index d61d96fc24..25dbc98fa7 100644 --- a/packages/compartment-mapper/src/node-modules.js +++ b/packages/compartment-mapper/src/node-modules.js @@ -12,6 +12,8 @@ /* eslint no-shadow: 0 */ /** @import {CanonicalFn} from './types.js' */ +/** @import {CompartmentMapForNodeModulesOptions} from './types.js' */ +/** @import {Policy} from './types.js' */ /** @import {CompartmentDescriptor} from './types.js' */ /** @import {CompartmentMapDescriptor} from './types.js' */ /** @import {Language} from './types.js' */ diff --git a/packages/compartment-mapper/src/parse-json.js b/packages/compartment-mapper/src/parse-json.js index a59ec16555..929ac60308 100644 --- a/packages/compartment-mapper/src/parse-json.js +++ b/packages/compartment-mapper/src/parse-json.js @@ -2,19 +2,23 @@ // @ts-check +/** @import {Harden} from 'ses' */ +/** @import {ParseFn} from './types.js' */ +/** @import {ParserImplementation} from './types.js' */ + import { parseLocatedJson } from './json.js'; /** * TypeScript cannot be relied upon to deal with the nuances of Readonly, so we * borrow the pass-through type definition of harden here. * - * @type {import('ses').Harden} + * @type {Harden} */ const freeze = Object.freeze; const textDecoder = new TextDecoder(); -/** @type {import('./types.js').SyncParseFn} */ +/** @type {ParseFn} */ export const parseJson = (bytes, _specifier, location, _packageLocation) => { const source = textDecoder.decode(bytes); const imports = freeze([]); @@ -36,7 +40,7 @@ export const parseJson = (bytes, _specifier, location, _packageLocation) => { }; }; -/** @type {import('./types.js').ParserImplementation} */ +/** @type {ParserImplementation} */ export default { parse: parseJson, heuristicImports: false, diff --git a/packages/compartment-mapper/src/powers.js b/packages/compartment-mapper/src/powers.js index 2053e345c4..4e9d8f39ff 100644 --- a/packages/compartment-mapper/src/powers.js +++ b/packages/compartment-mapper/src/powers.js @@ -7,7 +7,11 @@ // @ts-check -/** @import {CanonicalFn, ReadFn, ReadPowers, MaybeReadPowers, MaybeReadFn} from './types.js' */ +/** @import {CanonicalFn} from './types.js' */ +/** @import {ReadFn} from './types.js' */ +/** @import {ReadPowers} from './types.js' */ +/** @import {MaybeReadPowers} from './types.js' */ +/** @import {MaybeReadFn} from './types.js' */ /** @type {CanonicalFn} */ const canonicalShim = async path => path; @@ -27,8 +31,7 @@ export const unpackReadPowers = powers => { if (typeof powers === 'function') { read = powers; } else { - ({ read, maybeRead, canonical } = - /** @type {MaybeReadPowers} */ (powers)); + ({ read, maybeRead, canonical } = /** @type {MaybeReadPowers} */ (powers)); } if (canonical === undefined) { @@ -38,9 +41,7 @@ export const unpackReadPowers = powers => { if (maybeRead === undefined) { /** @param {string} path */ maybeRead = path => - /** @type {ReadFn} */ (read)(path).catch( - _error => undefined, - ); + /** @type {ReadFn} */ (read)(path).catch(_error => undefined); } return { diff --git a/packages/compartment-mapper/src/types.js b/packages/compartment-mapper/src/types.js index 5135d5ed2c..534f4c5e73 100644 --- a/packages/compartment-mapper/src/types.js +++ b/packages/compartment-mapper/src/types.js @@ -6,6 +6,7 @@ export {}; /** @import {FinalStaticModuleType} from 'ses' */ /** @import {ThirdPartyStaticModuleInterface} from 'ses' */ /** @import {ImportHook} from 'ses' */ +/** @import {ImportNowHook} from 'ses' */ /** @import {StaticModuleType} from 'ses' */ /** @import {Transform} from 'ses' */