Skip to content

Commit

Permalink
fix(compartment-mapper)!: Temporarily disable CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Oct 23, 2020
1 parent 35a48a7 commit 8d7fb04
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 197 deletions.
3 changes: 3 additions & 0 deletions packages/compartment-mapper/README.md
@@ -1,5 +1,8 @@
# Compartment mapper

> :warning: CommonJS support is temporarily disabled, pending a solution for
> heuristic static analysis that does not entrain any Node.js built-ins.
The compartment mapper builds _compartment maps_ for Node.js style
applications, finding their dependencies and describing how to create
[Compartments][] for each package in the application.
Expand Down
2 changes: 0 additions & 2 deletions packages/compartment-mapper/package.json
Expand Up @@ -22,8 +22,6 @@
"test": "yarn build && yarn qt"
},
"dependencies": {
"@babel/parser": "^7.8.4",
"@babel/traverse": "^7.8.4",
"ses": "^0.10.4"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions packages/compartment-mapper/src/node-modules.js
Expand Up @@ -76,9 +76,9 @@ const findPackage = async (readDescriptor, directory, name) => {
}
};

const languages = ["cjs", "mjs", "json"];
const uncontroversialParsers = { cjs: "cjs", mjs: "mjs", json: "json" };
const commonParsers = { js: "cjs", ...uncontroversialParsers };
const languages = ["mjs", "json"];
const uncontroversialParsers = { mjs: "mjs", json: "json" };
const commonParsers = uncontroversialParsers;
const moduleParsers = { js: "mjs", ...uncontroversialParsers };

const inferParsers = (descriptor, location) => {
Expand All @@ -88,7 +88,7 @@ const inferParsers = (descriptor, location) => {
throw new Error(
`Cannot interpret parser map ${JSON.stringify(
parsers
)} of package at ${location}, must be an object mapping file extensions to corresponding languages (mjs for ECMAScript modules, cjs for CommonJS modules, or json for JSON modules`
)} of package at ${location}, must be an object mapping file extensions to corresponding languages (mjs for ECMAScript modules or json for JSON modules`
);
}
const invalidLanguages = values(parsers).filter(
Expand All @@ -98,7 +98,7 @@ const inferParsers = (descriptor, location) => {
throw new Error(
`Cannot interpret parser map language values ${JSON.stringify(
invalidLanguages
)} of package at ${location}, must be an object mapping file extensions to corresponding languages (mjs for ECMAScript modules, cjs for CommonJS modules, or json for JSON modules`
)} of package at ${location}, must be an object mapping file extensions to corresponding languages (mjs for ECMAScript modules or json for JSON modules`
);
}
return { ...uncontroversialParsers, ...parsers };
Expand Down
64 changes: 0 additions & 64 deletions packages/compartment-mapper/src/parse-requires.js

This file was deleted.

54 changes: 0 additions & 54 deletions packages/compartment-mapper/src/parse.js
@@ -1,4 +1,3 @@
import { parseRequires } from "./parse-requires.js";
import { parseExtension } from "./extension.js";
import * as json from "./json.js";

Expand All @@ -17,58 +16,6 @@ export const parseMjs = (source, _specifier, location, _packageLocation) => {
};
};

export const parseCjs = (source, _specifier, location, packageLocation) => {
if (typeof source !== "string") {
throw new TypeError(
`Cannot create CommonJS static module record, module source must be a string, got ${source}`
);
}
if (typeof location !== "string") {
throw new TypeError(
`Cannot create CommonJS static module record, module location must be a string, got ${location}`
);
}

const imports = parseRequires(source, location, packageLocation);
const execute = (exports, compartment, resolvedImports) => {
const functor = compartment.evaluate(
`(function (require, exports, module, __filename, __dirname) { ${source} //*/\n})\n//# sourceURL=${location}`
);

let moduleExports = exports;

const module = freeze({
get exports() {
return moduleExports;
},
set exports(namespace) {
moduleExports = namespace;
exports.default = namespace;
}
});

const require = freeze(importSpecifier => {
const namespace = compartment.importNow(resolvedImports[importSpecifier]);
if (namespace.default !== undefined) {
return namespace.default;
}
return namespace;
});

functor(
require,
exports,
module,
location, // __filename
new URL("./", location).toString() // __dirname
);
};
return {
parser: "cjs",
record: freeze({ imports, execute })
};
};

export const parseJson = (source, _specifier, location, _packageLocation) => {
const imports = freeze([]);
const execute = exports => {
Expand Down Expand Up @@ -100,7 +47,6 @@ export const makeExtensionParser = (extensions, types) => {

export const parserForLanguage = {
mjs: parseMjs,
cjs: parseCjs,
json: parseJson
};

Expand Down
27 changes: 2 additions & 25 deletions packages/compartment-mapper/test/main.test.js
@@ -1,4 +1,3 @@
// import "./ses-lockdown.js";
import "ses";
import fs from "fs";
import tape from "tape";
Expand Down Expand Up @@ -33,20 +32,14 @@ const assertFixture = (t, namespace) => {
avery,
brooke,
clarke,
danny,
builtin,
receivedGlobalProperty,
receivedGlobalLexical,
typecommon,
typemodule,
typehybrid,
typeparsers
receivedGlobalLexical
} = namespace;

t.equal(avery, "Avery", "exports avery");
t.equal(brooke, "Brooke", "exports brooke");
t.equal(clarke, "Clarke", "exports clarke");
t.equal(danny, "Danny", "exports danny");

t.equal(builtin, "builtin", "exports builtin");

Expand All @@ -56,25 +49,9 @@ const assertFixture = (t, namespace) => {
globalLexicals.globalLexical,
"exports global lexical"
);
t.deepEqual(
typecommon,
[42, 42, 42, 42],
"type=common package carries exports"
);
t.deepEqual(
typemodule,
[42, 42, 42, 42],
"type=module package carries exports"
);
t.deepEqual(
typeparsers,
[42, 42, 42, 42],
"parsers-specifying package carries exports"
);
t.equal(typehybrid, 42, "type=module and module= package carries exports");
};

const fixtureAssertionCount = 11;
const fixtureAssertionCount = 6;

// The "create builtin" test prepares a builtin module namespace object that
// gets threaded into all subsequent tests to satisfy the "builtin" module
Expand Down
8 changes: 0 additions & 8 deletions packages/compartment-mapper/test/node_modules/app/main.js

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.

33 changes: 0 additions & 33 deletions packages/compartment-mapper/test/parse-requires.test.js

This file was deleted.

0 comments on commit 8d7fb04

Please sign in to comment.