
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
lazy-singleton
Advanced tools
Create lazy initialized singletons
$ npm install lazy-singleton
const lazySingleton = require('lazy-singleton');
const lazyDependency = lazySingleton(someExpensiveFunction)(...argsToPass);
// later - the expensive function won't be called until here
lazyDependency().doSomething();
// wrapped function will be called with `new`
const lazyInstance = new lazySingleton.Sync(SomeConstructor)(...argsToPass);
lazyInstance().memberFunction();
// it can be used as a lazy require wrapper - note we aren't chaining (...args) on the initial call.
const lazyRequire = require('lazy-singleton')(require);
const _ = lazyRequire('lodash');
const lazyFoo = lazyRequire('foo');
_().isNumber(3);
This is similar to, but different from, the popular once package, in that both ensure your wrapped function is only called once. With once the first caller determines which arguments are passed, and all future arguments are ignored. With this library, args are determined at creation time.
Note: All the API examples below show the common usage of two chained function calls. This is so you can create a generator that creates multiple lazy singletons with different args passed to the function (see the lazyRequire example above).
Type: function
The function to be called.
Type: function
The args for to be passed to the lazily called function.
&npbsp;
Note: If lazySingleton(fn) is called with new, then fn will also be called with new if/when it is invoked. Useful for classes.
These are both just an alias for the main function, the capitalized .Sync is useful when using new to prevent your linter from complaining.
This is just a wrapper allowing easy promise creation. fn will be called resolve, and reject as it's first two arguments.
lazySingleton.promise((resolve, reject) => resolve('foo'))();
// is equivalent to:
lazySingleton(() => new Promise((resolve, reject) => resolve('foo')))();
Note that you can still pass additional ...args to your promise executor. Any extra args are just appended after resolve and reject.
lazySingleton.promise(fn)('foo', 'bar');
// fn will be called with the extra args:
fn(resolve, reject, 'foo', 'bar');
Finally, the returned function will also accept a node style callback as it's first argument, allowing flexibility:
const lazy = lazySingleton.promise(promiseExecutor)();
lazy().then(doSomething);
//or
lazy((err, result) => {/* doSomething */});
Another sugar function. When lazily initialized, a node style callback will be appended to ..args, allowing you to leverage node style libraries:
const fileContents = lazySingleton.callback(fs.readFile)('some/path/to/file.txt', 'utf8');
// can be used as a promise
fileContents().then(contents => console.log(contents));
// or a node style callback
fileContents((err, contents) => console.log(contents))
MIT © James Talmage
FAQs
Create lazy initialized singletons
The npm package lazy-singleton receives a total of 113 weekly downloads. As such, lazy-singleton popularity was classified as not popular.
We found that lazy-singleton 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.