Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The node-hook package allows you to hook into Node.js' require function to modify the behavior of module loading. This can be useful for tasks such as transpiling code on the fly, mocking modules for testing, or adding custom behavior to module loading.
Transpile Code on the Fly
This feature allows you to transpile ES6 code to ES5 on the fly using Babel. By hooking into the '.js' extension, you can transform the source code before it is executed.
const hook = require('node-hook');
const babel = require('@babel/core');
hook.hook('.js', (source, filename) => {
const result = babel.transformSync(source, { filename });
return result.code;
});
require('./your-es6-file');
Mocking Modules for Testing
This feature allows you to mock specific modules for testing purposes. By checking the filename, you can return a mocked version of the module.
const hook = require('node-hook');
hook.hook('.js', (source, filename) => {
if (filename.endsWith('some-module.js')) {
return 'module.exports = { mockedFunction: () => "mocked" };';
}
return source;
});
const someModule = require('./some-module');
console.log(someModule.mockedFunction()); // Outputs: mocked
Custom Behavior for Module Loading
This feature allows you to add custom behavior to the module loading process. In this example, it logs the filename of each module being loaded.
const hook = require('node-hook');
hook.hook('.js', (source, filename) => {
console.log(`Loading module: ${filename}`);
return source;
});
require('./your-module');
The pirates package provides a similar functionality to node-hook by allowing you to hook into Node.js' require function. It is often used for transpiling code on the fly or adding custom behavior to module loading. Pirates is more focused on providing a simple and flexible API for hooking into the require function.
Proxyquire allows you to override dependencies during testing without modifying the original code. It is particularly useful for mocking modules in unit tests. Unlike node-hook, which hooks into the require function globally, proxyquire allows you to override dependencies on a per-require basis.
Babel-register is a package that hooks into Node.js' require function to transpile ES6/ES7 code on the fly using Babel. It is specifically designed for transpiling code and is often used in development environments. Unlike node-hook, which can be used for various custom behaviors, babel-register is focused solely on code transpilation.
Run source transform function on Node require
npm install --save node-hook
Before loading desired .js files, install hook
var hook = require('node-hook');
function logLoadedFilename(source, filename) {
return 'console.log("' + filename + '");\n' + source;
}
hook.hook('.js', logLoadedFilename);
require('./dummy');
// prints fulle dummy.js filename, runs dummy.js
hook.unhook('.js'); // removes your own transform
remember: Nodejs caches compiled modules, so if the transform is not
working, you might need to delete the cached entry in require.cache
,
then call require(filename)
again to force reload.
Related: Node require replacement really-need.
You can hook several transformers thanks to the code submitted by djulien
You can get the current transform and run any source through it. For example to see how the current source looks when loaded but before evaluated
const filename = resolve('./call-foo.js')
const transform = Module._extensions['.js']
const fakeModule = {
_compile: source => {
console.log('transformed code')
console.log(source)
}
}
transform(fakeModule, filename)
Author: Gleb Bahmutov © 2013
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github
FAQs
Run source transform function on Node require
We found that node-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.