@contrast/require-hook
Advanced tools
Comparing version 1.0.1-beta.3 to 1.1.0
@@ -13,11 +13,22 @@ 'use strict'; | ||
class ExportHandlerRegistry extends EventEmitter { | ||
/** | ||
* Instantiates the export handler registry. | ||
* This leverages DI to make it easier to test without | ||
* proxying mocks | ||
* @param {Object} params | ||
* @param {Object} [params.origModule={resolveFileName: Module._resolveFilename, require: Module.prototype.require }] | ||
* @param {Function} [params.resolveMeta=PackageFinder.resolveMeta] | ||
* | ||
*/ | ||
constructor({ | ||
logger, | ||
origModule = { | ||
resolveFilename: Module._resolveFilename, | ||
require: Module.prototype.require, | ||
require: Module.prototype.require | ||
}, | ||
resolveMetadata = PackageFinder.resolveMetadata, | ||
resolveMetadata = PackageFinder.resolveMetadata | ||
} = {}) { | ||
super(); | ||
this.logger = logger; | ||
this.resolveFilename = origModule.resolveFilename; | ||
@@ -35,2 +46,3 @@ this.require = origModule.require; | ||
* Update the registry with the handler info for the given module name. | ||
* This gets called when an agent registers a handler for a given module. | ||
* @param {string} name | ||
@@ -71,5 +83,6 @@ * @param {Object} handlerInfo | ||
const metadata = this.resolveMetadata({ | ||
logger: this.logger, | ||
parentModule, | ||
origRequire: this.require, | ||
resolvedName, | ||
resolvedName | ||
}); | ||
@@ -82,3 +95,3 @@ | ||
registered, | ||
resolvedName, | ||
resolvedName | ||
}); | ||
@@ -140,3 +153,3 @@ | ||
registered, | ||
resolvedName, | ||
resolvedName | ||
}) { | ||
@@ -143,0 +156,0 @@ const fileHandlers = {}; |
@@ -21,3 +21,3 @@ 'use strict'; | ||
handlers = [], | ||
version = DEFAULT_VERSION, | ||
version = DEFAULT_VERSION | ||
} = {}) { | ||
@@ -24,0 +24,0 @@ ExportHookDescriptor.validate({ name, file, handlers, version }); |
@@ -11,11 +11,15 @@ 'use strict'; | ||
* options can be supplied to customize the search process. | ||
* @param {string} resolvedName Absolute path to the module file | ||
* @param {string} [file=package.json] allow to pass in for testing purposes | ||
* @param {Object} params | ||
* @param {string} params.resolvedName Absolute path to the module file | ||
* @param {string} [params.file=package.json] allow to pass in for testing purposes | ||
* @param {Module} params.parentModule original Module before we started patching it | ||
* @param {Function} [params.origRequire=Module.prototype.require] used to read the package.json for a given module | ||
* @returns {Object} | ||
*/ | ||
static resolveMetadata({ | ||
logger = console, | ||
parentModule, | ||
origRequire = Module.prototype.require, | ||
resolvedName, | ||
file = 'package.json', | ||
file = 'package.json' | ||
} = {}) { | ||
@@ -45,4 +49,4 @@ if (PackageFinder.isNative(resolvedName)) { | ||
try { | ||
// Try to get the name/version of the package.json | ||
// bind with the parentModule that is being passed in from lib/require-hook.js | ||
// Use the require that was created before we started patching it | ||
// bind it with the original Module(parentModule) | ||
const { name, version } = origRequire.call(parentModule, filePath); | ||
@@ -52,8 +56,6 @@ return { | ||
version, | ||
packageDir: rootDir, | ||
packageDir: rootDir | ||
}; | ||
} catch (err) { | ||
// TODO this obv sucks | ||
// eslint-disable-next-line no-console | ||
console.log('Unable to parse %s, err: %o', filePath, err); | ||
logger.error('Unable to parse %s, err: %o', filePath, err); | ||
break; | ||
@@ -68,4 +70,4 @@ } | ||
* | ||
* @param {} name | ||
* @returns {} | ||
* @param {string} name | ||
* @returns {Boolean} | ||
*/ | ||
@@ -72,0 +74,0 @@ static isNative(name) { |
@@ -10,3 +10,3 @@ 'use strict'; | ||
HandlerRegistry, | ||
ERRORS: { ERR_RESOLVE_FILENAME }, | ||
ERRORS: { ERR_RESOLVE_FILENAME } | ||
} = require('./export-handler-registry'); | ||
@@ -19,11 +19,15 @@ | ||
class RequireHook extends EventEmitter { | ||
constructor() { | ||
constructor(logger) { | ||
super(); | ||
// set this up before we start patching moulde methods | ||
this.origModule = { | ||
require: Module.prototype.require, | ||
resolveFilename: Module._resolveFilename, | ||
resolveFilename: Module._resolveFilename | ||
}; | ||
this.invoker = new HandlerInvoker(); | ||
this.registry = new HandlerRegistry({ origModule: this.origModule }); | ||
this.registry = new HandlerRegistry({ | ||
logger, | ||
origModule: this.origModule | ||
}); | ||
this.requiredModules = new WeakMap(); | ||
@@ -81,3 +85,3 @@ this.resets = new Set(); | ||
proto.require = function (...args) { | ||
proto.require = function(...args) { | ||
const [name] = args; | ||
@@ -84,0 +88,0 @@ let xportSubstitution; |
{ | ||
"name": "@contrast/require-hook", | ||
"version": "1.0.1-beta.3", | ||
"version": "1.1.0", | ||
"description": "Post hooks for Module.prototype.require", | ||
@@ -40,5 +40,5 @@ "main": "lib/index.js", | ||
"chai": "^4.2.0", | ||
"eslint": "^7.8.1", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-mocha": "^8.0.0", | ||
"eslint-plugin-mocha": "^6.3.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
@@ -52,3 +52,3 @@ "eslint-plugin-prettier": "^3.1.4", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.1.1", | ||
"prettier": "^1.19.1", | ||
"sinon": "^9.0.3", | ||
@@ -55,0 +55,0 @@ "sinon-chai": "^3.5.0" |
@@ -1,5 +0,8 @@ | ||
# require-hook | ||
# @contrast/require-hook | ||
Intercept calls to `require` in order to modify or replace exports. | ||
## Module Flow | ||
![require-hook flow](module-flow.svg) | ||
## API | ||
@@ -6,0 +9,0 @@ |
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
507
1
90
0
20748
8