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
Typescript
Module System
Node Version
NPM Version
99.6
Supply Chain
89.8
Quality
75.3
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
11,507,750,895
Last Day
10,899,182
Last Week
60,529,966
Last Month
255,360,202
Last Year
2,701,730,242
ISC License
222 Stars
24 Commits
29 Forks
7 Watchers
7 Branches
6 Contributors
Updated on Aug 29, 2024
Minified
Minified + Gzipped
Latest Version
1.4.0
Package Id
once@1.4.0
Size
1.93 kB
NPM Version
3.10.7
Node Version
6.5.0
Published on
Sep 06, 2016
Cumulative downloads
Total Downloads
Last Day
45.4%
10,899,182
Compared to previous day
Last Week
6.3%
60,529,966
Compared to previous week
Last Month
-1.7%
255,360,202
Compared to previous month
Last Year
15%
2,701,730,242
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 you 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
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-05-05
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