Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
als-require
Advanced tools
als-require
is a lightweight utility that enables the importation and modification of CommonJS modules before execution in both browser and Node.js environments.
Capabilities of als-require
:
Where it can be particularly useful:
To install als-require
, use npm:
npm install als-require
Import in nodejs:
const Require = require('als-require')
const module = Require.getModule('./some/path')
Import in browser:
<script src="/node_modules/als-require/require.js"></script>
<script>
Require.getModule('./some/path')
.then(module => {
// ...
})
// or Require.getModule('./some/path')
require('./some/path').then(module => {
// ...
})
</script>
als-require
has two files for NodeJS and browser which has same structure and api.
Each file includes Require
class with folowing structure:
class Require {
static getModule(path, context,contextName) {}
static contents = {}
constructor(path, context = {},contextName) {
this.modules = {}
this.contents = {}
this.path = path
this.context = context
}
getContent(path = this.path, from = Require.relativePath) {}
build(modules = this.modules,context,contextName = this.contextName) {}
}
The structure above describes only properties and methods for usage, without additional properties and methods which used as private methods.
The constructor gets two arguments: path and context.
path
(String): relative path to module for requirecontext
(Object): shared object which will be available in all modulescontextName
(String): Name for context variable (default context)Here explanation what each method and property used for:
Require.getModule
- quick way to get contents and build them in one step
contents
, modules
and result
Require.contents
- includes modules contents and their children list and used as cacherequire.getContent()
- used for reading module file's contents
Require.contents
and to require.contents
async
and NodeJS version is sync
require.build()
- builds all modules results
modules
- you can add custom modules (for example modules from backend for same path)context
- private context for curent build (will includes instance.context properties too)contextName
- private conteName for curent buildrequire.modules
- includes all module's results after buildrequire.result
- has the main module result (export) after buildconst Require = require('als-require')
const path = './relative/path/to/module'
const context = {} // shared object for all modules empty object by default
const mod = new Require(path,context)
mod.getContent() // reading all modules
for(const path in mod.contents) {
mod.contents[path] = mod.contents[path] // modify if needed
}
mod.build() // build the result
mod.result // includes the result of main module
mod.modules // object with all module`s results {relativePath:moduleResult,...}
If you want to use node modules libraries, you need to specify relative path.
<script src="/node_modules/als-require/require.js"></script>
<script>
const path = './relative/path/to/module'
const context = {} // shared object for all modules empty object by default
const mod = new Require(path,context)
const promise = mod.getContent() // fetching all modules
promise.then(mod => {
for(const path in mod.contents) {
mod.contents[path] = mod.contents[path] // modify if needed
}
mod.build() // build the result
mod.result // includes the result of main module
mod.modules // object with all module`s results {relativePath:moduleResult,...}
})
</script>
In case of path which not starts with .
, Require will look for file in node_modules (by checking in package.json).
If such package not found (for example require('fs')
), result for this package will return null.
const somePackage = require('some-package');
const somePackage1 = require('./node_modules/some-package/index.js');
const fs = require('fs');
module.exports = {somePackage,somePackage1,fs}
In case above somePackage
and somePackage1
should return the package, but fs, should return null.
Pay attention, using './node_modules/some-package/index.js'
instead 'some-package'
, you save extra request/readFile for looking the filename.
The context
object is a shared space accessible by all modules loaded by als-require
. This allows modules to read and write shared data, enabling more interactive and dynamic module behaviors.
Make sure you are using the context
for static value (like constants and common variables and functions) and not dynamic, cause it available to all require's results.
als-require
throwing errors in case of cyclic dependencies and if some module throwing error.
For the case of module's error, Require
adds to stack modules paths which has called.
FAQs
A utility for using CommonJS require in the browser and creating bundles.
The npm package als-require receives a total of 4 weekly downloads. As such, als-require popularity was classified as not popular.
We found that als-require demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.