Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
race-cancellation
Advanced tools
Utilities for using Promise.race([task, cancellation]) for async/await code.
This is a library with utils to implement a cancellable async function with a pattern that solves a number of issues with cancellation of promises.
The pattern is a cancellable async function takes a function that will build the cancellation race lazily.
export type AsyncFn<T> = () => Promise<T>;
export type RaceCancelFn = <T>(asyncFnOrPromise: AsyncFn<T> | PromiseLike<T>) => Promise<T>;
export type CancellableAsyncFn<T> = (raceCancel: RaceCancelFn) => Promise<T>;
Since race cancellation is a function it can lazily create the Promise to avoid unhandledRejection.
Since it can take a function to invoke to produce the task, it can avoid starting the task if it is already cancelled and can avoid creating the Promise if the task fails on invoke.
This also allows the combined race cancellations to be lazy. For example, if we are already cancelled because we are disconnected we don't need to start a timeout.
import * as fs from "fs";
async function pollFile(path, interval, raceCancel) {
while (!fs.existsSync(path)) {
await sleep(interval, raceCancel);
}
}
async function sleep(ms, raceCancel) {
let id;
try {
const createTimeout = () =>
new Promise<void>(resolve => {
id = setTimeout(resolve, ms);
});
// if cancellation has happened race cancel can return
// a rejected promise without invoking createTimeout
// otherwise createTimeout is called and raced against
// a new Promise<never> created from the cancellation Promise,
// cancellationPromise.then(throwCancellationError)
return await raceCancel(createTimeout);
} finally {
if (id !== undefined) {
// cleanup timeout so node will exit right away if
// our script is done.
clearTimeout(id);
}
}
}
FAQs
Utilities for using Promise.race([task, cancellation]) for async/await code.
The npm package race-cancellation receives a total of 606 weekly downloads. As such, race-cancellation popularity was classified as not popular.
We found that race-cancellation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.