Skip to content

Commit

Permalink
chore: WIP fixing conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jun 7, 2024
1 parent 593af4e commit 2135d35
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/compartment-mapper/src/import-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 20 additions & 2 deletions packages/compartment-mapper/src/import-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' */
Expand All @@ -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 = {},
Expand All @@ -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;
Expand Down
29 changes: 21 additions & 8 deletions packages/compartment-mapper/src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' */
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' */
Expand Down
10 changes: 7 additions & 3 deletions packages/compartment-mapper/src/parse-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
Expand All @@ -36,7 +40,7 @@ export const parseJson = (bytes, _specifier, location, _packageLocation) => {
};
};

/** @type {import('./types.js').ParserImplementation} */
/** @type {ParserImplementation} */
export default {
parse: parseJson,
heuristicImports: false,
Expand Down
13 changes: 7 additions & 6 deletions packages/compartment-mapper/src/powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions packages/compartment-mapper/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' */

Expand Down

0 comments on commit 2135d35

Please sign in to comment.