Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
insert-module-globals
Advanced tools
The insert-module-globals npm package is used to insert global variables into the scope of a module. This is particularly useful for browserify transforms, allowing you to inject variables like __filename, __dirname, process, Buffer, and global into your modules.
Injecting __filename
This feature allows you to inject the __filename variable into your module's scope. The code sample demonstrates how to use the insert-module-globals package with browserify to transform a file and insert the __filename variable.
const insertGlobals = require('insert-module-globals');
const through = require('through2');
const b = browserify();
b.transform(function (file) {
return through(function (buf, enc, next) {
this.push(insertGlobals(file, buf.toString('utf8')));
next();
});
});
Injecting __dirname
This feature allows you to inject the __dirname variable into your module's scope. The code sample demonstrates how to use the insert-module-globals package with browserify to transform a file and insert the __dirname variable.
const insertGlobals = require('insert-module-globals');
const through = require('through2');
const b = browserify();
b.transform(function (file) {
return through(function (buf, enc, next) {
this.push(insertGlobals(file, buf.toString('utf8')));
next();
});
});
Injecting process
This feature allows you to inject the process variable into your module's scope. The code sample demonstrates how to use the insert-module-globals package with browserify to transform a file and insert the process variable.
const insertGlobals = require('insert-module-globals');
const through = require('through2');
const b = browserify();
b.transform(function (file) {
return through(function (buf, enc, next) {
this.push(insertGlobals(file, buf.toString('utf8')));
next();
});
});
Injecting Buffer
This feature allows you to inject the Buffer variable into your module's scope. The code sample demonstrates how to use the insert-module-globals package with browserify to transform a file and insert the Buffer variable.
const insertGlobals = require('insert-module-globals');
const through = require('through2');
const b = browserify();
b.transform(function (file) {
return through(function (buf, enc, next) {
this.push(insertGlobals(file, buf.toString('utf8')));
next();
});
});
Injecting global
This feature allows you to inject the global variable into your module's scope. The code sample demonstrates how to use the insert-module-globals package with browserify to transform a file and insert the global variable.
const insertGlobals = require('insert-module-globals');
const through = require('through2');
const b = browserify();
b.transform(function (file) {
return through(function (buf, enc, next) {
this.push(insertGlobals(file, buf.toString('utf8')));
next();
});
});
Envify is a browserify transform that replaces occurrences of process.env.NODE_ENV with the value of NODE_ENV. It is useful for injecting environment variables into your modules, similar to how insert-module-globals injects global variables.
Browserify-shim is a browserify transform that allows you to shim non-CommonJS modules to work with browserify. It can be used to inject global variables and dependencies into your modules, similar to insert-module-globals.
Exposify is a browserify transform that allows you to expose global variables to your modules. It is similar to insert-module-globals in that it helps you manage global variables within your browserify bundles.
insert implicit module globals
(__filename
, __dirname
, process
, global
, setImmediate
, clearImmediate
and Buffer
)
as a browserify-style transform
var mdeps = require('module-deps');
var bpack = require('browser-pack');
var insert = require('insert-module-globals');
function inserter (file) {
return insert(file, { basedir: __dirname + '/files' });
}
var files = [ __dirname + '/files/main.js' ];
mdeps(files, { transform: inserter })
.pipe(bpack({ raw: true }))
.pipe(process.stdout)
;
$ node example/insert.js | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}
or use the command-line scripts:
$ module-deps main.js | insert-module-globals | browser-pack | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}
or use insert-module-globals as a transform:
$ module-deps main.js --transform insert-module-globals | browser-pack | node
in main.js: {"__filename":"/main.js","__dirname":"/"}
in foo/index.js: {"__filename":"/foo/index.js","__dirname":"/foo"}
var insertGlobals = require('insert-module-globals')
Return a transform stream inserter
for the filename file
that will accept a
javascript file as input and will output the file with a closure around the
contents as necessary to define extra builtins.
When opts.always
is true, wrap every file with all the global variables
without parsing. This is handy because parsing the scope can take a long time,
so you can prioritize fast builds over saving bytes in the final output. When
opts.always
is truthy but not true, avoid parsing but perform a quick test to
determine if wrapping should be skipped.
Use opts.vars
to override the default inserted variables, or set
opts.vars[name]
to undefined
to not insert a variable which would otherwise
be inserted.
opts.vars
properties with a .
in their name will be executed instead of the
parent object if ONLY that property is used. For example, "Buffer.isBuffer"
will mask "Buffer"
only when there is a Buffer.isBuffer()
call in a file and
no other references to Buffer
.
If opts.debug
is true, an inline source map will be generated to compensate
for the extra lines.
When a global is detected, the inserter stream emits a 'global'
event.
usage: insert-module-globals {basedir}
With npm, to get the library do:
npm install insert-module-globals
and to get the bin script do:
npm install -g insert-module-globals
insert-module-globals
can also insert arbitary globals into files.
Pass in an object of functions as the vars
option.
var vars = {
process: function (file, basedir) {
return {
id: "path/to/custom_process.js",
source: customProcessContent
}
},
Buffer: function (file, basedir) {
return {
id: 'path/to/custom_buffer.js',
source: customProcessContent,
//suffix is optional
//it's used to extract the value from the module.
//it becomes: require(...).Buffer in this case.
suffix: '.Buffer'
}
},
Math: function () {
//if you return a string,
//it's simply set as the value.
return '{}'
//^ any attempt to use Math[x] will throw!
}
}
function inserter (file) {
return insert(file, { vars: vars });
}
mdeps(files, { transform: inserter })
.pipe(bpack({ raw: true }))
.pipe(process.stdout)
MIT
FAQs
insert implicit module globals into a module-deps stream
We found that insert-module-globals demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 40 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.