Skip to content

Commit

Permalink
refactor(compartment-mapper): Extract lite from hevy
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jun 3, 2024
1 parent 234b72d commit 57649f8
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/compartment-mapper/archive-parsers.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { parserForLanguage } from './src/archive-parsers.js';
export { defaultParserForLanguage } from './src/archive-parsers.js';
2 changes: 1 addition & 1 deletion packages/compartment-mapper/import-archive-parsers.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { parserForLanguage } from './src/import-archive-parsers.js';
export { defaultParserForLanguage } from './src/import-archive-parsers.js';
2 changes: 1 addition & 1 deletion packages/compartment-mapper/import-parsers.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { parserForLanguage } from './src/import-parsers.js';
export { defaultParserForLanguage } from './src/import-parsers.js';
3 changes: 1 addition & 2 deletions packages/compartment-mapper/src/archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
91 changes: 91 additions & 0 deletions packages/compartment-mapper/src/archive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// @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.
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<Uint8Array>}
*/
export const makeArchive = async (powers, moduleLocation, options) =>
makeArchiveLite(powers, moduleLocation, assignParserForLanguage(options));

/**
* @param {ReadFn | ReadPowers} powers
* @param {string} moduleLocation
* @param {ArchiveOptions} [options]
* @returns {Promise<Uint8Array>}
*/
export const mapLocation = async (powers, moduleLocation, options) =>
mapLocationLite(powers, moduleLocation, assignParserForLanguage(options));

/**
* @param {HashPowers} powers
* @param {string} moduleLocation
* @param {ArchiveOptions} [options]
* @returns {Promise<string>}
*/
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),
);
3 changes: 1 addition & 2 deletions packages/compartment-mapper/src/import-archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
Expand Down
80 changes: 80 additions & 0 deletions packages/compartment-mapper/src/import-archive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// @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 */

// Add the default parserForLanguage option.
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 {object} [options]
* @param {string} [options.expectedSha512]
* @param {HashFn} [options.computeSha512]
* @param {Record<string, unknown>} [options.modules]
* @param {ExitModuleImportHook} [options.importHook]
* @param {CompartmentConstructor} [options.Compartment]
* @param {ComputeSourceLocationHook} [options.computeSourceLocation]
* @param {ComputeSourceMapLocationHook} [options.computeSourceMapLocation]
* @param {ParserForLanguage} [options.parserForLanguage]
* @returns {Promise<Application>}
*/
export const parseArchive = async (
archiveBytes,
archiveLocation = '<unknown>',
options = {},
) =>
parseArchiveLite(
archiveBytes,
archiveLocation,
assignParserForLanguage(options),
);

/**
* @param {import('@endo/zip').ReadFn | ReadPowers} readPowers
* @param {string} archiveLocation
* @param {LoadArchiveOptions} [options]
* @returns {Promise<Application>}
*/
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<object>}
*/
export const importArchive = async (readPowers, archiveLocation, options) =>
importArchiveLite(
readPowers,
archiveLocation,
assignParserForLanguage(options),
);
3 changes: 1 addition & 2 deletions packages/compartment-mapper/src/import-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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),
Expand Down
51 changes: 51 additions & 0 deletions packages/compartment-mapper/src/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @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.
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<Application>}
*/
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<import('./types.js').SomeObject>} the object of the imported modules exported
* names.
*/
export const importLocation = async (readPowers, moduleLocation, options) =>
importLocationLite(
readPowers,
moduleLocation,
assignParserForLanguage(options),
);
Original file line number Diff line number Diff line change
Expand Up @@ -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:…`

Expand All @@ -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:…`

Expand All @@ -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:…`

Expand All @@ -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:…`

Expand All @@ -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:…)␊
Expand All @@ -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:…)␊
Expand All @@ -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:…)␊
Expand All @@ -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:…)␊
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified packages/compartment-mapper/test/snapshots/policy.test.js.snap
Binary file not shown.

0 comments on commit 57649f8

Please sign in to comment.