module-keys
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -27,8 +27,70 @@ /** | ||
const ID_POLYFILL = 'polyfill'; | ||
const ID_MODULE = 'module'; | ||
const ID_REQUIRE = 'require'; | ||
const STR_MODULE_KEYS_CJS = 'module-keys/cjs'; | ||
function isIdentifierNamed(node, name) { | ||
return node.type === 'Identifier' && node.name === name; | ||
} | ||
function isString(node, value) { | ||
return node.type === 'StringLiteral' && node.value === value; | ||
} | ||
function isCall(node, fnPredicate, ...argPredicates) { | ||
if (node.type !== 'CallExpression') { | ||
return false; | ||
} | ||
const { callee, 'arguments': args } = node; | ||
const nArgs = args.length; | ||
if (nArgs === argPredicates.length && | ||
fnPredicate(callee)) { | ||
for (let i = 0; i < nArgs; ++i) { | ||
if (!argPredicates[i](args[i])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
module.exports = function moduleKeysBabelPlugin({ types: t }) { | ||
let isCommonJsModule = true; | ||
// let sawPolyfill = false; // TODO: don't polyfill if already present | ||
let sawCjsPolyfill = false; | ||
let sawEsPolyfill = false; | ||
return { | ||
name: 'module-keys/babel plugin', | ||
visitor: { | ||
CallExpression(nodePath) { | ||
// If there's already a CJS polyfill, remember so we | ||
// don't add a redundant polyfill. | ||
// This helps us bootstrap the index file with a public key. | ||
const { callee } = nodePath.node; | ||
if (callee.type === 'MemberExpression' && | ||
isIdentifierNamed(callee.property, ID_POLYFILL) && | ||
// Expect top level. | ||
nodePath.parentPath.node.type === 'ExpressionStatement' && | ||
nodePath.parentPath.parentPath.node.type === 'Program' && | ||
isCall( | ||
nodePath.node, | ||
({ object }) => isCall( // eslint-disable-line id-blacklist | ||
object, // eslint-disable-line id-blacklist | ||
(fun) => isIdentifierNamed(fun, ID_REQUIRE), | ||
(arg) => isString(arg, STR_MODULE_KEYS_CJS)), | ||
(arg) => isIdentifierNamed(arg, ID_MODULE), | ||
(arg) => isIdentifierNamed(arg, ID_REQUIRE))) { | ||
sawCjsPolyfill = true; | ||
} | ||
}, | ||
VariableDeclarator(nodePath) { | ||
// If there's already a top level moduleKeys variable, don't insert the | ||
// ES6 polyfill. | ||
if (isIdentifierNamed(nodePath.node.id, 'moduleKeys') && | ||
nodePath.parentPath.node.type === 'VariableDeclaration' && | ||
nodePath.parentPath.parentPath.node.type === 'Program') { | ||
sawEsPolyfill = true; | ||
} | ||
}, | ||
ModuleDeclaration() { | ||
@@ -38,7 +100,17 @@ isCommonJsModule = false; | ||
Program: { | ||
enter() { | ||
enter(nodePath, state) { | ||
// until proven otherwise | ||
isCommonJsModule = true; | ||
sawCjsPolyfill = false; | ||
sawEsPolyfill = false; | ||
if (path.join(__dirname, '..', 'index.js') === state.file.opts.filename) { | ||
// Don't polyfill the index file. It bootstraps itself | ||
sawCjsPolyfill = true; | ||
state.stop(); | ||
} | ||
}, | ||
exit(nodePath, state) { | ||
if (isCommonJsModule ? sawCjsPolyfill : sawEsPolyfill) { | ||
return; | ||
} | ||
const polyfills = []; | ||
@@ -52,6 +124,6 @@ if (isCommonJsModule) { | ||
t.callExpression( | ||
t.identifier('require'), | ||
[ t.stringLiteral('module-keys/cjs') ]), | ||
t.identifier('polyfill')), | ||
[ t.identifier('module'), t.identifier('require') ]))); | ||
t.identifier(ID_REQUIRE), | ||
[ t.stringLiteral(STR_MODULE_KEYS_CJS) ]), | ||
t.identifier(ID_POLYFILL)), | ||
[ t.identifier(ID_MODULE), t.identifier(ID_REQUIRE) ]))); | ||
} else { | ||
@@ -58,0 +130,0 @@ const { filename } = state.file.opts; |
@@ -258,2 +258,4 @@ /** | ||
// CommonJS specific | ||
const { publicKey: myPublicKey } = makeModuleKeys(); | ||
module.exports = freeze(defineProperties( | ||
@@ -266,2 +268,6 @@ create(null), | ||
publicKeySymbol: { value: publicKeySymbol, enumerable: true }, | ||
// The public key for this module. Exported for consistency. | ||
publicKey: myPublicKey, | ||
[publicKeySymbol]: myPublicKey, | ||
})); | ||
@@ -268,0 +274,0 @@ |
{ | ||
"name": "module-keys", | ||
"description": "Module identity as a basis for privilege separation for ESM & CommonJS modules", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"keywords": [ | ||
@@ -45,5 +45,5 @@ "design-patterns", | ||
"eslint": "^4.15.0", | ||
"eslint-config-strict": "*", | ||
"eslint-config-strict": "^14.0.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^4.0.1", | ||
"mocha": "^5.2.0", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
@@ -50,0 +50,0 @@ "pre-commit": "^1.2.2" |
@@ -234,1 +234,5 @@ # Module Keys | ||
``` | ||
--- | ||
This is not an official Google product. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48163
778
238