Gathering detailed insights and metrics for once
Gathering detailed insights and metrics for once
Gathering detailed insights and metrics for once
Gathering detailed insights and metrics for once
npm install once
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
222 Stars
24 Commits
29 Forks
8 Watching
7 Branches
6 Contributors
Updated on 29 Aug 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.5%
10,957,110
Compared to previous day
Last week
3.1%
59,983,209
Compared to previous week
Last month
14.7%
242,397,905
Compared to previous month
Last year
13%
2,519,806,486
Compared to previous year
Only call a function once.
1var once = require('once') 2 3function load (file, cb) { 4 cb = once(cb) 5 loader.load('file') 6 loader.once('load', cb) 7 loader.once('error', cb) 8}
Or add to the Function.prototype in a responsible way:
1// only has to be done once 2require('once').proto() 3 4function load (file, cb) { 5 cb = cb.once() 6 loader.load('file') 7 loader.once('load', cb) 8 loader.once('error', cb) 9}
Ironically, the prototype feature makes this module twice as complicated as necessary.
To check whether your function has been called, use fn.called
. Once the
function is called for the first time the return value of the original
function is saved in fn.value
and subsequent calls will continue to
return this value.
1var once = require('once') 2 3function load (cb) { 4 cb = once(cb) 5 var stream = createStream() 6 stream.once('data', cb) 7 stream.once('end', function () { 8 if (!cb.called) cb(new Error('not found')) 9 }) 10}
once.strict(func)
Throw an error if the function is called twice.
Some functions are expected to be called only once. Using once
for them would
potentially hide logical errors.
In the example below, the greet
function has to call the callback only once:
1function greet (name, cb) { 2 // return is missing from the if statement 3 // when no name is passed, the callback is called twice 4 if (!name) cb('Hello anonymous') 5 cb('Hello ' + name) 6} 7 8function log (msg) { 9 console.log(msg) 10} 11 12// this will print 'Hello anonymous' but the logical error will be missed 13greet(null, once(msg)) 14 15// once.strict will print 'Hello anonymous' and throw an error when the callback will be called the second time 16greet(null, once.strict(msg))
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 3/24 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More