Skip to content

Commit

Permalink
TMP commit
Browse files Browse the repository at this point in the history
  • Loading branch information
naugtur committed May 2, 2022
1 parent f158c85 commit 62d70bc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 22 deletions.
9 changes: 7 additions & 2 deletions packages/compartment-mapper/src/import-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
/** @typedef {import('ses').ImportHook} ImportHook */
/** @typedef {import('ses').StaticModuleType} StaticModuleType */
/** @typedef {import('./types.js').ReadFn} ReadFn */
/** @typedef {import('./types.js').ReadPowers} ReadPowers */
/** @typedef {import('./types.js').HashFn} HashFn */
/** @typedef {import('./types.js').Sources} Sources */
/** @typedef {import('./types.js').CompartmentSources} CompartmentSources */
/** @typedef {import('./types.js').CompartmentDescriptor} CompartmentDescriptor */
/** @typedef {import('./types.js').ImportHookMaker} ImportHookMaker */

import { parseExtension } from './extension.js';
import { unpackReadPowers } from './powers.js';

// q, as in quote, for quoting strings in error messages.
const q = JSON.stringify;
Expand Down Expand Up @@ -44,7 +46,7 @@ function getImportsFromRecord(record) {
}

/**
* @param {ReadFn} read
* @param {ReadFn|ReadPowers} readPowers
* @param {string} baseLocation
* @param {Sources} sources
* @param {Record<string, CompartmentDescriptor>} compartments
Expand All @@ -53,7 +55,7 @@ function getImportsFromRecord(record) {
* @returns {ImportHookMaker}
*/
export const makeImportHookMaker = (
read,
readPowers,
baseLocation,
sources = Object.create(null),
compartments = Object.create(null),
Expand Down Expand Up @@ -150,6 +152,8 @@ export const makeImportHookMaker = (
}
}

const { read } = unpackReadPowers(readPowers);

for (const candidateSpecifier of candidates) {
// Using a specifier as a location.
// This is not always valid.
Expand All @@ -170,6 +174,7 @@ export const makeImportHookMaker = (
candidateSpecifier,
moduleLocation,
packageLocation,
readPowers,
);
const {
parser,
Expand Down
12 changes: 8 additions & 4 deletions packages/compartment-mapper/src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
__shimTransforms__,
Compartment,
} = options;
const makeImportHook = makeImportHookMaker(read, packageLocation);
const makeImportHook = makeImportHookMaker(readPowers, packageLocation);
const { compartment } = link(compartmentMap, {
makeImportHook,
parserForLanguage,
Expand All @@ -88,13 +88,17 @@ export const loadLocation = async (readPowers, moduleLocation, options) => {
};

/**
* @param {ReadFn} read
* @param {ReadFn | ReadPowers} readPowers
* @param {string} moduleLocation
* @param {ExecuteOptions & ArchiveOptions} [options]
* @returns {Promise<Object>} the object of the imported modules exported
* names.
*/
export const importLocation = async (read, moduleLocation, options = {}) => {
const application = await loadLocation(read, moduleLocation, options);
export const importLocation = async (
readPowers,
moduleLocation,
options = {},
) => {
const application = await loadLocation(readPowers, moduleLocation, options);
return application.import(options);
};
2 changes: 1 addition & 1 deletion packages/compartment-mapper/src/node-powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
}
: undefined;

return { read, canonical, computeSha512 };
return { read, fileURLToPath, canonical, computeSha512 };
};

/**
Expand Down
24 changes: 22 additions & 2 deletions packages/compartment-mapper/src/parse-cjs-shared-export-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,33 @@ const { freeze, keys, create, hasOwnProperty } = Object;
* @returns {boolean}
*/
const has = (object, key) => apply(hasOwnProperty, object, [key]);

export const dropFileProtocol = url => {
const removeProtocol = url => {
if (url.substring(0, 7) === 'file://') {
url = url.substring(7);
}
return url;
};
const locationParent = location => {
const index = location.lastIndexOf('/');
if (index >= 0) {
return location.slice(0, index);
}
return location;
};

export const getPaths = async (readPowers, url) => {
let urlF = url;
let urlD = locationParent(url);

if (readPowers && readPowers.canonical) {
urlF = await readPowers.canonical(urlF);
urlD = await readPowers.canonical(urlD);
}
return {
filename: removeProtocol(urlF),
dirname: removeProtocol(urlD),
};
};

/**
* ModuleEnvironmentRecord wrapper
Expand Down
9 changes: 6 additions & 3 deletions packages/compartment-mapper/src/parse-cjs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

import { analyzeCommonJS } from '@endo/cjs-module-analyzer';
import { wrap, dropFileProtocol } from './parse-cjs-shared-export-wrapper.js';
import { wrap, getPaths } from './parse-cjs-shared-export-wrapper.js';

const textDecoder = new TextDecoder();

Expand All @@ -13,6 +13,7 @@ export const parseCjs = async (
_specifier,
location,
_packageLocation,
readPowers,
) => {
const source = textDecoder.decode(bytes);

Expand All @@ -25,6 +26,8 @@ export const parseCjs = async (
exports.push('default');
}

const lowlow = await getPaths(readPowers, location); // __filename

/**
* @param {Object} moduleEnvironmentRecord
* @param {Compartment} compartment
Expand All @@ -45,8 +48,8 @@ export const parseCjs = async (
require,
moduleExports,
module,
dropFileProtocol(location), // __filename
dropFileProtocol(new URL('./', location).toString()), // __dirname
lowlow.filename,
lowlow.dirname,
);

afterExecute();
Expand Down
16 changes: 6 additions & 10 deletions packages/compartment-mapper/src/parse-pre-cjs.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
// @ts-check

import { parseLocatedJson } from './json.js';
import { wrap, dropFileProtocol } from './parse-cjs-shared-export-wrapper.js';
import { wrap, getPaths } from './parse-cjs-shared-export-wrapper.js';

const textDecoder = new TextDecoder();

const locationParent = location => {
const index = location.lastIndexOf('/');
if (index >= 0) {
return location.slice(0, index);
}
return location;
};

/** @type {import('./types.js').ParseFn} */
export const parsePreCjs = async (
bytes,
_specifier,
location,
_packageLocation,
readPowers,
) => {
const text = textDecoder.decode(bytes);
const { source, imports, exports, reexports } = parseLocatedJson(
text,
location,
);

const lowlow = await getPaths(readPowers, location); // __filename

/**
* @param {Object} moduleEnvironmentRecord
* @param {Compartment} compartment
Expand All @@ -44,8 +40,8 @@ export const parsePreCjs = async (
require,
moduleExports,
module,
dropFileProtocol(location), // __filename
dropFileProtocol(locationParent(location)), // __dirname
lowlow.filename, // __filename
lowlow.dirname, // __dirname
);

afterExecute();
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 @@ -190,6 +190,7 @@ export const moduleJSDocTypes = true;
* @param {string} specifier
* @param {string} location
* @param {string} packageLocation
* @param {ReadFn | ReadPowers} [readPowers]
* @returns {Promise<{
* bytes: Uint8Array,
* parser: Language,
Expand Down

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

18 changes: 18 additions & 0 deletions packages/compartment-mapper/test/test-cjs-compat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
// import "./ses-lockdown.js";
import 'ses';
import test from 'ava';
Expand All @@ -8,6 +9,10 @@ const fixture = new URL(
'fixtures-cjs-compat/node_modules/app/index.js',
import.meta.url,
).toString();
const fixtureDirname = new URL(
'fixtures-cjs-compat/node_modules/app/dirname.js',
import.meta.url,
).toString();

const assertFixture = (t, { namespace }) => {
const { assertions } = namespace;
Expand All @@ -32,3 +37,16 @@ scaffold(
assertFixture,
fixtureAssertionCount,
);

scaffold(
'fixtures-cjs-compat-__dirname',
test,
fixtureDirname,
(t, { namespace }) => {
const { __dirname, __filename } = namespace;
t.is(__filename, `${__dirname}/dirname.js`);
t.assert(!__dirname.startsWith('file://'));
throw JSON.stringify({ __dirname, __filename });
},
2,
);

0 comments on commit 62d70bc

Please sign in to comment.