feat(compartment-mapper): move require.resolve implementation to readPowers
MODIFY
packages/compartment-mapper/src/archive.js
packages/compartment-mapper/src/archive.js
@@ -253,7 +253,6 @@ const digestLocation = async (powers, moduleLocation, options) => {
modules: exitModules = {},
dev = false,
captureSourceLocation = undefined,
- requireResolve = null,
} = options || {};
const { read, computeSha512 } = unpackReadPowers(powers);
const {
@@ -293,7 +292,6 @@ const digestLocation = async (powers, moduleLocation, options) => {
const makeImportHook = makeImportHookMaker(
read,
packageLocation,
- requireResolve,
sources,
compartments,
exitModules,

MODIFY
packages/compartment-mapper/src/bundle.js
packages/compartment-mapper/src/bundle.js
@@ -129,11 +129,10 @@ const sortedModules = (
* @param {string} moduleLocation
* @param {Object} [options]
* @param {ModuleTransforms} [options.moduleTransforms]
- * @param {Function} [options.requireResolve]
* @returns {Promise<string>}
*/
export const makeBundle = async (read, moduleLocation, options) => {
- const { moduleTransforms, requireResolve = null } = options || {};
+ const { moduleTransforms } = options || {};
const {
packageLocation,
packageDescriptorText,
@@ -166,7 +165,6 @@ export const makeBundle = async (read, moduleLocation, options) => {
const makeImportHook = makeImportHookMaker(
read,
packageLocation,
- requireResolve,
sources,
compartments,
);

MODIFY
packages/compartment-mapper/src/import-hook.js
packages/compartment-mapper/src/import-hook.js
@@ -48,7 +48,6 @@ function getImportsFromRecord(record) {
/**
* @param {ReadFn|ReadPowers} readPowers
* @param {string} baseLocation
- * @param {Function|null} [requireResolve]
* @param {Sources} sources
* @param {Record<string, CompartmentDescriptor>} compartments
* @param {Record<string, any>} exitModules
@@ -58,7 +57,6 @@ function getImportsFromRecord(record) {
export const makeImportHookMaker = (
readPowers,
baseLocation,
- requireResolve = null,
sources = Object.create(null),
compartments = Object.create(null),
exitModules = Object.create(null),
@@ -180,7 +178,6 @@ export const makeImportHookMaker = (
packageLocation,
{
readPowers,
- requireResolve,
},
);
const {

MODIFY
packages/compartment-mapper/src/import.js
packages/compartment-mapper/src/import.js
@@ -37,8 +37,7 @@ export const parserForLanguage = {
* @returns {Promise<Application>}
*/
export const loadLocation = async (readPowers, moduleLocation, options) => {
- const { moduleTransforms = {}, dev = false, requireResolve = null } =
- options || {};
+ const { moduleTransforms = {}, dev = false } = options || {};
const { read } = unpackReadPowers(readPowers);
@@ -74,11 +73,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
__shimTransforms__,
Compartment,
} = options;
- const makeImportHook = makeImportHookMaker(
- readPowers,
- packageLocation,
- requireResolve,
- );
+ const makeImportHook = makeImportHookMaker(readPowers, packageLocation);
const { compartment } = link(compartmentMap, {
makeImportHook,
parserForLanguage,

MODIFY
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
@@ -69,7 +69,7 @@ export const getModulePaths = (readPowers, location) => {
* @param {Compartment} in.compartment
* @param {Record<string, string>} in.resolvedImports
* @param {string} in.location
- * @param {Function} [in.requireResolve]
+ * @param {ReadFn|ReadPowers} [in.readPowers]
* @returns {{
* module: { exports: any },
* moduleExports: any,
@@ -82,7 +82,7 @@ export const wrap = ({
compartment,
resolvedImports,
location,
- requireResolve,
+ readPowers,
}) => {
// This initial default value makes things like exports.hasOwnProperty() work in cjs.
moduleEnvironmentRecord.default = create(
@@ -141,13 +141,15 @@ export const wrap = ({
return namespace;
}
};
- if (requireResolve) {
+ if (readPowers && readPowers.requireResolve) {
require.resolve = freeze((specifier, options) =>
- requireResolve(location, specifier, options),
+ readPowers.requireResolve(location, specifier, options),
);
} else {
require.resolve = freeze(specifier => {
- const error = Error(`Cannot find module '${specifier}'`);
+ const error = Error(
+ `Cannot find module '${specifier}'\nPass ReadPowers with a requireResolve function to provide require.resolve`,
+ );
defineProperty(error, 'code', { value: 'MODULE_NOT_FOUND' });
throw error;
});

MODIFY
packages/compartment-mapper/src/parse-cjs.js
packages/compartment-mapper/src/parse-cjs.js
@@ -13,7 +13,7 @@ export const parseCjs = async (
_specifier,
location,
_packageLocation,
- { readPowers, requireResolve } = {},
+ { readPowers } = {},
) => {
const source = textDecoder.decode(bytes);
@@ -43,7 +43,7 @@ export const parseCjs = async (
compartment,
resolvedImports,
location,
- requireResolve,
+ readPowers,
});
functor(require, moduleExports, module, filename, dirname);

MODIFY
packages/compartment-mapper/src/parse-pre-cjs.js
packages/compartment-mapper/src/parse-pre-cjs.js
@@ -11,7 +11,7 @@ export const parsePreCjs = async (
_specifier,
location,
_packageLocation,
- { readPowers, requireResolve } = {},
+ { readPowers } = {},
) => {
const text = textDecoder.decode(bytes);
const { source, imports, exports, reexports } = parseLocatedJson(
@@ -33,8 +33,8 @@ export const parsePreCjs = async (
moduleEnvironmentRecord,
compartment,
resolvedImports,
- requireResolve,
location,
+ readPowers,
});
functor(require, moduleExports, module, filename, dirname);

MODIFY
packages/compartment-mapper/src/types.js
packages/compartment-mapper/src/types.js
@@ -151,6 +151,7 @@ export {};
* @property {HashFn} [computeSha512]
* @property {Function} [fileURLToPath]
* @property {Function} [pathToFileURL]
+ * @property {Function} [requireResolve]
*/
/**
@@ -199,7 +200,6 @@ export {};
* @param {string} packageLocation
* @param {object} [options]
* @param {ReadFn | ReadPowers} [options.readPowers]
- * @param {Function} [options.requireResolve]
* @returns {Promise<{
* bytes: Uint8Array,
* parser: Language,
@@ -310,5 +310,4 @@ export {};
* @property {Record<string, any>} [modules]
* @property {boolean} [dev]
* @property {CaptureSourceLocationHook} [captureSourceLocation]
- * @property {Function} [requireResolve]
*/

MODIFY
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/main.js
packages/compartment-mapper/test/fixtures-cjs-compat/node_modules/require-resolve/main.js
@@ -9,6 +9,14 @@ const adaptToAssertions = (f) => {
}
}
+const errorMessageDetails = (f) => {
+ try {
+ f();
+ } catch (e) {
+ return e.message.split('\n')[1];
+ }
+}
+
module.exports = [
adaptToAssertions(()=>require.resolve(".")),
adaptToAssertions(()=>require.resolve("./package.json")),
@@ -19,4 +27,5 @@ module.exports = [
adaptToAssertions(()=>require.resolve("app")),
adaptToAssertions(()=>require.resolve("fs")),
adaptToAssertions(()=>require.resolve("nested-export/callBound")),
+ errorMessageDetails(()=>require.resolve(".")),
]
\ No newline at end of file

MODIFY
packages/compartment-mapper/test/scaffold.js
packages/compartment-mapper/test/scaffold.js
import 'ses';
import fs from 'fs';
import crypto from 'crypto';
-import { createRequire } from 'module';
import url from 'url';
import { ZipReader, ZipWriter } from '@endo/zip';
import {
@@ -88,8 +87,6 @@ export function scaffold(
const application = await loadLocation(readPowers, fixture, {
dev: true,
- requireResolve: (from, specifier, options) =>
- createRequire(from).resolve(specifier, options),
});
const { namespace } = await application.import({
globals,
@@ -110,8 +107,6 @@ export function scaffold(
modules,
Compartment,
dev: true,
- requireResolve: (from, specifier, options) =>
- createRequire(from).resolve(specifier, options),
});
return namespace;
});

MODIFY
packages/compartment-mapper/test/test-cjs-compat.js
packages/compartment-mapper/test/test-cjs-compat.js
@@ -37,6 +37,7 @@ const assertFixture = (t, { namespace, testCategoryHint }) => {
'/skipped/fixtures-cjs-compat/node_modules/app/index.js',
'fs',
'/skipped/fixtures-cjs-compat/node_modules/nested-export/callBound.js',
+ 'Require stack:',
]);
} else {
t.deepEqual(results.requireResolvePaths, [
@@ -49,6 +50,7 @@ const assertFixture = (t, { namespace, testCategoryHint }) => {
"Cannot find module 'app'",
"Cannot find module 'fs'",
"Cannot find module 'nested-export/callBound'",
+ 'Pass ReadPowers with a requireResolve function to provide require.resolve',
]);
}
t.pass();

feat(compartment-mapper): move require.resolve implementation to readPowers