Skip to content

Commit

Permalink
refactor(compartment-mapper): Decouple node_modules mapping strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jun 3, 2024
1 parent 57649f8 commit 756c062
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 152 deletions.
10 changes: 5 additions & 5 deletions packages/compartment-mapper/archive-lite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export {
makeArchive,
makeAndHashArchive,
writeArchive,
mapLocation,
hashLocation,
makeArchiveFromMap,
makeAndHashArchiveFromMap,
writeArchiveFromMap,
mapFromMap,
hashFromMap,
} from './src/archive-lite.js';
2 changes: 1 addition & 1 deletion packages/compartment-mapper/import-lite.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { loadLocation, importLocation } from './src/import-lite.js';
export { loadFromMap, importFromMap } from './src/import-lite.js';
1 change: 1 addition & 0 deletions packages/compartment-mapper/node-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mapNodeModules } from './src/node-modules.js';
1 change: 1 addition & 0 deletions packages/compartment-mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
85 changes: 34 additions & 51 deletions packages/compartment-mapper/src/archive-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
);

Expand All @@ -420,40 +395,44 @@ export const makeAndHashArchive = async (powers, moduleLocation, options) => {

/**
* @param {ReadFn | ReadPowers} powers
* @param {string} moduleLocation
* @param {CompartmentMapDescriptor} compartmentMap
* @param {ArchiveOptions} [options]
* @returns {Promise<Uint8Array>}
*/
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<Uint8Array>}
*/
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;
};

/**
* @param {HashPowers} powers
* @param {string} moduleLocation
* @param {CompartmentMapDescriptor} compartmentMap
* @param {ArchiveOptions} [options]
* @returns {Promise<string>}
*/
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;
Expand All @@ -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);
};
87 changes: 67 additions & 20 deletions packages/compartment-mapper/src/archive.js
Original file line number Diff line number Diff line change
@@ -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' */
Expand All @@ -34,39 +33,79 @@ 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
* @param {string} moduleLocation
* @param {ArchiveOptions} [options]
* @returns {Promise<Uint8Array>}
*/
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
* @param {string} moduleLocation
* @param {ArchiveOptions} [options]
* @returns {Promise<Uint8Array>}
*/
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
* @param {string} moduleLocation
* @param {ArchiveOptions} [options]
* @returns {Promise<string>}
*/
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
Expand All @@ -80,12 +119,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),
);
};
Loading

0 comments on commit 756c062

Please sign in to comment.