Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

module-keys

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-keys - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

lib/relpath.js

44

babel/index.js

@@ -32,2 +32,12 @@ /**

const defaultRootDir = (() => {
// dir containing babel
const root = path.join(__dirname, '..');
// dir containing module-keys
const rootParent = path.join(root, '..');
return path.basename(rootParent) === 'node_modules' ?
path.join(rootParent, '..') :
root;
})();
function isIdentifierNamed(node, name) {

@@ -83,3 +93,4 @@ return node.type === 'Identifier' && node.name === name;

(arg) => isIdentifierNamed(arg, ID_MODULE),
(arg) => isIdentifierNamed(arg, ID_REQUIRE))) {
(arg) => isIdentifierNamed(arg, ID_REQUIRE),
(arg) => arg.type === 'StringLiteral')) {
sawCjsPolyfill = true;

@@ -106,6 +117,11 @@ }

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();
const { filename } = state.file.opts;
if (path.join(__dirname, '..') === path.dirname(filename)) {
const basename = path.basename(filename);
if (basename === 'index.js') {
// Don't polyfill the index file. It bootstraps itself
sawCjsPolyfill = true;
} else if (basename === 'index.mjs') {
sawEsPolyfill = true;
}
}

@@ -117,5 +133,9 @@ },

}
const { filename } = state.file.opts;
const { rootDir = defaultRootDir } = state.opts;
const importSpec = path.relative(rootDir, filename);
const polyfills = [];
if (isCommonJsModule) {
// require('module-keys/cjs').polyfill(module, require);
// require('module-keys/cjs').polyfill(module, require, 'path');
polyfills.push(

@@ -129,5 +149,4 @@ t.expressionStatement(

t.identifier(ID_POLYFILL)),
[ t.identifier(ID_MODULE), t.identifier(ID_REQUIRE) ])));
[ t.identifier(ID_MODULE), t.identifier(ID_REQUIRE), t.stringLiteral(importSpec) ])));
} else {
const { filename } = state.file.opts;
// Compute the absolute path to the ESM index file.

@@ -138,3 +157,3 @@ const moduleKeysPath = path.join(__dirname, '..', 'index.mjs');

// import { makeModuleKeys as __moduleKeysMaker } from "./path/to/module-keys";
// const moduleKeys = __moduleKeysMaker(import.meta.url);
// const moduleKeys = __moduleKeysMaker('importSpec');
// const { publicKey: __moduleKeysPublicKey } = moduleKeys;

@@ -157,8 +176,3 @@ // export { __moduleKeysPublicKey as publicKey };

t.identifier('__moduleKeysMaker'),
[
t.memberExpression(
// TODO: should this use t.metaProperty?
t.memberExpression(t.identifier('import'), t.identifier('meta')),
t.identifier('url')),
])),
[ t.stringLiteral(importSpec) ])),
t.variableDeclarator(

@@ -165,0 +179,0 @@ t.objectPattern(

@@ -20,4 +20,2 @@ /**

const { makeModuleKeys, publicKeySymbol } = require('../index.js');
/**

@@ -35,4 +33,16 @@ * @fileoverview

function polyfill(module, require) {
const keysObj = makeModuleKeys(module.filename);
const { makeModuleKeys, publicKeySymbol } = require('../index.js');
const { apply } = Reflect;
const { defineProperties, hasOwnProperty } = Object;
/**
* Makes module keys available to module code as {@code require.keys}
* and makes a best effort to export the modules public key via
* an exported property named "publicKey" and via the public key symbol.
*
* @param {!Module} module the CommonJS module to polyfill.
* @param {!function(string):*} require module's require function.
*/
function polyfill(module, require, moduleIdentifier) {
const keysObj = makeModuleKeys(moduleIdentifier);
require.keys = keysObj;

@@ -48,3 +58,3 @@ const { publicKey } = keysObj;

delete module.loaded;
Object.defineProperties(
defineProperties(
module, {

@@ -62,3 +72,3 @@ exports: {

typeof newExports === 'function')) {
if (!Object.hasOwnProperty.call(exports, 'publicKey')) {
if (!apply(hasOwnProperty, exports, [ 'publicKey' ])) {
try {

@@ -70,3 +80,3 @@ module.exports.publicKey = publicKey;

}
if (!Object.hasOwnProperty.call(exports, publicKeySymbol)) {
if (!apply(hasOwnProperty, exports, [ publicKeySymbol ])) {
try {

@@ -73,0 +83,0 @@ module.exports[publicKeySymbol] = publicKey;

{
"name": "module-keys",
"description": "Module identity as a basis for privilege separation for ESM & CommonJS modules",
"version": "1.0.2",
"version": "1.0.3",
"keywords": [

@@ -17,3 +17,4 @@ "design-patterns",

"babel/index.js",
"cjs/index.js"
"cjs/index.js",
"lib/relpath.js"
],

@@ -20,0 +21,0 @@ "dependencies": {},

@@ -58,4 +58,5 @@ # Module Keys

```
## Babel Plugin
The babel plugin will add keys to all your modules.
The babel plugin will add keys to your modules.

@@ -67,6 +68,11 @@ ### Via .babelrc (Recommended)

{
"plugins": ["module-keys/babel"]
"plugins": [
[ "module-keys/babel", { "rootDir": "/path/to/module/root" } ]
]
}
```
The optional `"rootDir"` option lets you specify the base URL
used to compute relative module identifiers.
### Via CLI

@@ -80,6 +86,9 @@ ```sh

require("@babel/core").transform("code", {
plugins: ["module-keys/babel"]
plugins: [
[ "module-keys/babel", { "rootDir": "/path/to/module/root" } ]
]
});
```
### CommonJS Modules

@@ -216,2 +225,5 @@ Once you've run the Babel plugin over your modules, each module will have

Each `publicKey` also has a `moduleIdentifier` property which
specifies the location of the module relative to the module root.
### `makeModuleKeys`

@@ -218,0 +230,0 @@ `makeModuleKeys()` returns a new module keys bundle with its own

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc