Skip to content

Commit

Permalink
feat(compartment-mapper): allow alternate searchSuffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis committed Dec 14, 2022
1 parent 82bf889 commit 5f58cf6
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/compartment-mapper/src/archive.js
Expand Up @@ -254,6 +254,7 @@ const digestLocation = async (powers, moduleLocation, options) => {
dev = false,
tags = new Set(),
captureSourceLocation = undefined,
searchSuffixes = undefined,
} = options || {};
const { read, computeSha512 } = unpackReadPowers(powers);
const {
Expand Down Expand Up @@ -295,6 +296,7 @@ const digestLocation = async (powers, moduleLocation, options) => {
compartments,
exitModules,
computeSha512,
searchSuffixes,
);

// Induce importHook to record all the necessary modules to import the given module specifier.
Expand Down
11 changes: 10 additions & 1 deletion packages/compartment-mapper/src/bundle.js
Expand Up @@ -146,10 +146,16 @@ const sortedModules = (
* @param {ModuleTransforms} [options.moduleTransforms]
* @param {boolean} [options.dev]
* @param {Set<string>} [options.tags]
* @param {Array<string>} [options.searchSuffixes]
* @returns {Promise<string>}
*/
export const makeBundle = async (read, moduleLocation, options) => {
const { moduleTransforms, dev, tags: tagsOption } = options || {};
const {
moduleTransforms,
dev,
tags: tagsOption,
searchSuffixes,
} = options || {};
const tags = new Set(tagsOption);

const {
Expand Down Expand Up @@ -184,6 +190,9 @@ export const makeBundle = async (read, moduleLocation, options) => {
packageLocation,
sources,
compartments,
undefined,
undefined,
searchSuffixes,
);

// Induce importHook to record all the necessary modules to import the given module specifier.
Expand Down
21 changes: 16 additions & 5 deletions packages/compartment-mapper/src/import-hook.js
Expand Up @@ -45,13 +45,22 @@ function getImportsFromRecord(record) {
return (has(record, 'record') ? record.record.imports : record.imports) || [];
}

// Node.js default resolution allows for an incomplement specifier that does not include a suffix.
const nodejsConventionSearchSuffixes = ['.js', '/index.js'];

/**
* @param {ReadFn|ReadPowers} readPowers
* @param {string} baseLocation
* @param {Sources} sources
* @param {Record<string, CompartmentDescriptor>} compartmentDescriptors
* @param {Record<string, any>} exitModules
* @param {HashFn=} computeSha512
* @param {Array<string>} searchSuffixes - Suffixes to search if the unmodified specifier is not found.
* Pass [] to emulate Node.js’s strict behavior.
* The default handles Node.js’s CommonJS behavior.
* Unlike Node.js, the Compartment Mapper lifts CommonJS up, more like a bundler,
* and does not attempt to vary the behavior of resolution depending on the
* language of the importing module.
* @returns {ImportHookMaker}
*/
export const makeImportHookMaker = (
Expand All @@ -61,6 +70,7 @@ export const makeImportHookMaker = (
compartmentDescriptors = Object.create(null),
exitModules = Object.create(null),
computeSha512 = undefined,
searchSuffixes = nodejsConventionSearchSuffixes,
) => {
// Set of specifiers for modules whose parser is not using heuristics to determine imports
const strictlyRequired = new Set();
Expand Down Expand Up @@ -142,12 +152,13 @@ export const makeImportHookMaker = (
);
}

// Collate candidate locations for the moduleSpecifier per Node.js
// conventions.
const candidates = [];
// Collate candidate locations for the moduleSpecifier,
// to support Node.js conventions and similar.
const candidates = [moduleSpecifier];
if (moduleSpecifier !== '.') {
candidates.push(moduleSpecifier);
candidates.push(`${moduleSpecifier}.js`, `${moduleSpecifier}/index.js`);
for (const candidateSuffix of searchSuffixes) {
candidates.push(`${moduleSpecifier}${candidateSuffix}`);
}
}

const { read } = unpackReadPowers(readPowers);
Expand Down
4 changes: 4 additions & 0 deletions packages/compartment-mapper/src/import.js
Expand Up @@ -41,6 +41,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
moduleTransforms = {},
dev = false,
tags = new Set(),
searchSuffixes = undefined,
} = options || {};

const { read } = unpackReadPowers(readPowers);
Expand Down Expand Up @@ -74,6 +75,9 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
packageLocation,
undefined,
compartmentMap.compartments,
undefined,
undefined,
searchSuffixes,
);
const { compartment } = link(compartmentMap, {
makeImportHook,
Expand Down
1 change: 1 addition & 0 deletions packages/compartment-mapper/src/types.js
Expand Up @@ -311,4 +311,5 @@ export {};
* @property {boolean} [dev]
* @property {Set<string>} [tags]
* @property {CaptureSourceLocationHook} [captureSourceLocation]
* @property {Array<string>} [searchSuffixes]
*/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/compartment-mapper/test/scaffold.js
Expand Up @@ -58,6 +58,7 @@ export function scaffold(
shouldFailBeforeArchiveOperations = false,
knownFailure = false,
tags = undefined,
searchSuffixes = undefined,
} = {},
) {
// wrapping each time allows for convenient use of test.only
Expand Down Expand Up @@ -91,6 +92,7 @@ export function scaffold(
const application = await loadLocation(readPowers, fixture, {
dev: true,
tags,
searchSuffixes,
});
const { namespace } = await application.import({
globals,
Expand All @@ -110,6 +112,7 @@ export function scaffold(
Compartment,
dev: true,
tags,
searchSuffixes,
});
return namespace;
});
Expand All @@ -122,6 +125,7 @@ export function scaffold(
modules,
dev: true,
tags,
searchSuffixes,
});
const application = await parseArchive(archive, '<unknown>', {
modules: Object.fromEntries(
Expand Down Expand Up @@ -153,6 +157,7 @@ export function scaffold(
modules,
dev: true,
tags,
searchSuffixes,
});
const prefixArchive = new Uint8Array(archive.length + 10);
prefixArchive.set(archive, 10);
Expand Down Expand Up @@ -189,6 +194,7 @@ export function scaffold(
modules: { builtin: true },
dev: true,
tags,
searchSuffixes,
});
const application = await loadArchive(fakeRead, 'app.agar', {
modules,
Expand Down Expand Up @@ -221,6 +227,7 @@ export function scaffold(
modules,
dev: true,
tags,
searchSuffixes,
});
const { namespace } = await importArchive(fakeRead, 'app.agar', {
globals,
Expand All @@ -240,12 +247,14 @@ export function scaffold(
Compartment,
dev: true,
tags,
searchSuffixes,
});

const archiveBytes = await makeArchive(readPowers, fixture, {
modules,
dev: true,
tags,
searchSuffixes,
});

const { computeSha512 } = readPowers;
Expand All @@ -272,12 +281,14 @@ export function scaffold(
modules,
dev: true,
tags,
searchSuffixes,
});

const archive = await makeArchive(readPowers, fixture, {
modules,
dev: true,
tags,
searchSuffixes,
});

const reader = new ZipReader(archive);
Expand Down
16 changes: 16 additions & 0 deletions packages/compartment-mapper/test/test-main.js
Expand Up @@ -239,3 +239,19 @@ scaffold(
},
2,
);

scaffold(
'overwrite searchSuffixes',
test,
new URL(
'fixtures-0/node_modules/candidates-custom/index.js',
import.meta.url,
).toString(),
(t, { namespace: { fourthPrime } }) => {
t.is(fourthPrime, 7);
},
1,
{
searchSuffixes: ['.abc.js'],
},
);

0 comments on commit 5f58cf6

Please sign in to comment.