module-keys
Advanced tools
Comparing version 1.0.10 to 2.0.0
@@ -26,3 +26,3 @@ /** | ||
* <li>attaches the public key to {@code module.exports}</li> | ||
* <li>makes the module keys available via {@code require.keys}</li> | ||
* <li>makes the module keys available via {@code require.moduleKeys}</li> | ||
* <li>hooks into the module so that common patterns of replacing | ||
@@ -37,3 +37,3 @@ * {@code module.exports} will still export {@code publicKey}.</li> | ||
const { sep } = require('path'); | ||
const { makeModuleKeys, publicKeySymbol, keysSymbol } = require('../index.js'); | ||
const { makeModuleKeys, publicKeySymbol } = require('../index.js'); | ||
@@ -44,3 +44,3 @@ const sepGlobalPattern = sep === '\\' ? /\\/g : new RegExp(`[${ sep }]`, 'g'); | ||
/** | ||
* Makes module keys available to module code as {@code require.keys} | ||
* Makes module keys available to module code as {@code require.moduleKeys} | ||
* and makes a best effort to export the modules public key via | ||
@@ -53,9 +53,4 @@ * an exported property named "publicKey" and via the public key symbol. | ||
function polyfill(module, require) { | ||
const hasKeysProp = apply(hasOwnProperty, require, [ 'keys' ]); | ||
if (hasKeysProp) { | ||
const oldKeys = require.keys; | ||
if (oldKeys && typeof oldKeys.publicKey === 'function' && | ||
typeof oldKeys.isPublicKey === 'function') { | ||
return; | ||
} | ||
if (apply(hasOwnProperty, require, [ 'moduleKeys' ])) { | ||
return; | ||
} | ||
@@ -71,6 +66,3 @@ let moduleIdentifier = `${ module.filename }`; | ||
const keysObj = makeModuleKeys(moduleIdentifier); | ||
if (!hasKeysProp) { | ||
require.keys = keysObj; | ||
} | ||
require[keysSymbol] = keysObj; | ||
require.moduleKeys = keysObj; | ||
@@ -77,0 +69,0 @@ const { publicKey } = keysObj; |
@@ -124,7 +124,2 @@ /** | ||
/** | ||
* Symbol used to attach keys to CommonJS require. | ||
*/ | ||
const keysSymbol = Symbol('keys'); | ||
/** An opaque token used to represent a boxed value in transit. */ | ||
@@ -284,3 +279,2 @@ class Box { | ||
publicKeySymbol: { value: publicKeySymbol, enumerable: true }, | ||
keysSymbol: { value: keysSymbol, enumerable: true }, | ||
@@ -287,0 +281,0 @@ // The public key for this module. Exported for consistency. |
{ | ||
"name": "module-keys", | ||
"description": "Module identity as a basis for privilege separation for ESM & CommonJS modules", | ||
"version": "1.0.10", | ||
"version": "2.0.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "design-patterns", |
@@ -94,7 +94,7 @@ # Module Keys | ||
Once you've run the Babel plugin over your modules, each module will have | ||
its own keys available via `require.keys`, and will export its `publicKey` | ||
its own keys available via `require.moduleKeys`, and will export its `publicKey` | ||
if doing so would not conflict with an explicit export. | ||
```js | ||
require.keys.box(...); // boxes a value. See API below | ||
require.moduleKeys.box(...); // boxes a value. See API below | ||
``` | ||
@@ -137,3 +137,3 @@ | ||
const { publicKey: fooKey } = require('./foo'); | ||
const box = require.keys.box(value, (k) => k === fooKey && k()); | ||
const box = require.moduleKeys.box(value, (k) => k === fooKey && k()); | ||
// box may only be opened via ./foo's unboxer. | ||
@@ -167,3 +167,3 @@ ``` | ||
function f(box) { | ||
console.log(`I got ${ require.keys.unbox(box, () => true, 'a box I cannot open') }`) | ||
console.log(`I got ${ require.moduleKeys.unbox(box, () => true, 'a box I cannot open') }`) | ||
} | ||
@@ -170,0 +170,0 @@ ``` |
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
55499
982