Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
AMD module loader with deterministic and just-in time module initialization
Simple JavaScript AMD module loader.
Simply include the script:
<script src="https://unpkg.com/amdld/amdld.min.js"></script>
The API is very simple:
define(
id? : string,
dependencies? : string[],
factory : (...dependencies :any[])=>any
| {[key :string] :any}
) : boolean
E.g.
define("A", ["B", "C"], function(B, C) {
console.log(C.hello, B.world);
});
define("B", ["exports"], function(exports) {
exports.world = 'world';
});
define("C", { hello: 'Hello' });
Get a tested optimized ES5-compatible build from unpkg or NPM.
If you're only targeting ES6 hosts—for instance in an Electron app—use amdld.es6.min.js
instead which uses native generators and other ES6 features (Symbol, const, let, etc):
<script src="https://unpkg.com/amdld/amdld.es6.min.js"></script>
Since AMDLD uses initializeras with pause-and-resumable state, dependencies and modules can be loaded in random order, in synchronous or asynchronous fashion.
Say we have some HTML like this where four modules are loaded concurrently and asynchronously:
<script async src="module_1.js"></script>
<script async src="module_2_and_3.js"></script>
<script async src="module_4.js"></script>
Normally, this would require additional app code to track what modules have been loaded and when their dependencies has loaded. But not with AMDLD since dependency resolution is done just-in-time.
For this example, let's say that the files loaded have this content:
// module_1.js
define("mod1", ["mod3"], function (mod3) { console.log('hello from mod1'); });
// module_2_and_3.js
define("mod2", ["mod3"], function (mod3) { console.log('hello from mod2'); });
define("mod3", [], function () { console.log('hello from mod3'); });
// module_4.js
define("mod4", ["mod3"], function () { console.log('hello from mod4'); });
Now imagine that the following happens in the web browser:
The browser's console looks like this:
[09:30:19] hello from mod3
[09:30:19] hello from mod4
[09:30:19] hello from mod2
[09:31:21] hello from mod1
What's important to realize here is that "mod4" is initialized as soon as all it's dependencies are available, rather than when all modules has loaded. "mod1" therefore is in practice initialized later than "mod4", but since "mod1" doesn't depend on "mod4" (or vice versa), neither have to wait for the other. AMDLD guarantees that the order of initialization is the same every time for each dependency root — i.e. "mod3" is always initialized before "mod1" in our example above.
The "async" test verifies this behavior.
When loading modules over separate files, consider setting define.timeout
(see API.)
Cyclic dependencies cause a lot of issues, primarily by preventing module initialization from being deterministic. AMDLD forces dependencies to be non-cyclical and thus by using AMDLD, all module initialization is deterministic, meaning that when a module's function is executed all it's dependencies have been executed already; any dependant values required during module initialization are guaranteed.
Check this out. Here's an example of a simple program that defines two modules:
define('foo', ['bar'], function(bar) {
let sum = bar.value * 10;
console.log(`sum is ${sum}`); // what is printed?
})
define('bar', ['exports'], function(exports) {
exports.value = 3;
})
What is printed to the console? With some module loaders, it depends on when "bar" is defined (imagine loading them over network or from disk where one module might be loaded before the other differs from time to time.)
In this case AMDLD suspends initialization of foo
until bar
has fully initialized, meaning that no matter when what module is loaded, the result is always the same (prints sum is 30
to the console.)
When a cyclic dependency is detected, a short but descriptive error is thrown:
define("main", ["bar"], function (bar) {});
define("bar", ["baz"], function (baz) {});
define("baz", ["main"], function () {});
// Error: Cyclic module dependency: baz -> main -> bar -> baz
The "cyclic3" test verifies this behavior.
define(
id? : string,
dependencies? : string[],
factory : (...dependencies :any[])=>any
| {[key :string] :any}
) : boolean
// Define a module. Returns true if the module was initialized immediately,
// otherwise false is returned to indicate that initialization is suspended
// waiting for a dependency.
// See https://github.com/amdjs/amdjs-api/blob/master/AMD.md for details.
define.timeout : number
// Set to a number larger than zero to enable timeout.
// Whenever define() is called, the timeout is reset.
// When the timer expires an error is thrown if there are still undefined modules.
define.require(id :string) : any
// Acquire a module's exported API. Throws an error if the module is not defined.
// Might return undefined if the module has not yet been initialized.
Although you can use amdld.js
as-is in an ES6 environment (like latest Chrome or Firefox), there are three products that are generated from the source:
amdld.g.js
— assertions enabledamdld.min.js
— optimized code for ES5 targets (~4 kB)
amdld.min.js.map
— source mappings to amdld.js
amdld.es6.min.js
— optimized code for ES6 targets (~2.5 kB)
amdld.es6.min.js.map
— source mappings to amdld.js
To build all of these, first make sure you have java
in your $PATH
as it's required for closure-compiler. Then:
$ npm install # install typescript and closure-compiler
$ npm run build
...
$ npm run build
...
To run all unit tests, use npm test
or the much faster test/test.js
:
$ test/test.js
pass basic @amdld.js
pass basic @amdld.min.js
pass cyclic1 @amdld.js
...
all pass
Note: Building and testing requires Nodejs to be installed, but is not required for using AMDLD in a web browser.
Copyright (c) 2016 Rasmus Andersson https://rsms.me/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
AMD module loader with deterministic and just-in time module initialization
The npm package amdld receives a total of 16 weekly downloads. As such, amdld popularity was classified as not popular.
We found that amdld 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.