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.
limit-once
Advanced tools
Create a function that caches the result of the first function call. limit-once
let's you lazily evaluate a value (using a function), and then hold onto the value forever.
import { once } from 'limit-once';
function sayHello(name: string): string {
return `Hello ${name}`;
}
const cached = once(sayHello);
cached('Alex');
// sayHello called and "Hello Alex" is returned
// "Hello Alex" is put into the cache.
// returns "Hello Alex"
cached('Sam');
// sayHello is not called
// "Hello Alex" is returned from the cache.
cached('Greg');
// sayHello is not called
// "Hello Alex" is returned from the cache.
// is-safari.ts
import { once } from 'limit-once';
// We are caching the result of our 'isSafari()' function as the result
// of `isSafari()` won't change.
export const isSafari = once(function isSafari(): boolean {
const { userAgent } = navigator;
return userAgent.includes('AppleWebKit') && !userAgent.includes('Chrome');
});
// app.ts
if (isSafari()) {
applySafariFix();
}
# yarn
yarn add limit-once
# npm
npm install limit-once
# bun
bun add limit-once
.clear()
)You can clear the cache of a memoized function by using a .clear()
function that is on your cached function.
import { once } from 'limit-once';
function sayHello(name: string): string {
return `Hello ${name}`;
}
const cached = once(sayHello);
cached('Alex');
// sayHello called and "Hello Alex" is returned.
// "Hello Alex" is put into the cache
// cached function returns "Hello Alex"
cached('Sam');
// sayHello is not called
// "Hello Alex" is returned from cache
cached.clear();
cached('Greg');
// sayHello is called and "Hello Greg" is returned.
// "Hello Greg" is put into the cache
// "Hello Greg" is returned.
FAQs
Remember the first result of a function call
The npm package limit-once receives a total of 0 weekly downloads. As such, limit-once popularity was classified as not popular.
We found that limit-once demonstrated a healthy version release cadence and project activity because the last version was released less than 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.