Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
interruptible
Advanced tools
interruptible
is a library that makes asynchronous functions interruptible
by using generators.
To mark an async function as interruptible, convert it to a generator, and use
yield
to indicate where your function can be interrupted.
From:
async function work (a, b) {
const foo = await makeFoo(a, b)
const result = await processFoo(foo)
return result
}
const result = await work()
To:
import {asInterruptible, InterruptError} from 'interruptible'
async function *interruptibleWork (a, b) {
const foo = yield makeFoo(a, b) // Use `yield` to indicate that function can be interrupted after this async operation has resolved
// 🚫 If operation is interrupted before this line is reached, execution will stop here.
const result = await processFoo(foo) // `await` wont stop execution even if operation is interrupted
return result
}
const interruptibleTask = asInterruptible(interruptibleWork)
try {
const result = await interruptibleTask.run(this, 'a', 'b')
} catch(error) {
if (error instanceof InterruptError) {
console.log('Interruptible task interrupted')
return
}
console.log('Interruptible task errored')
}
// You can interrupt the task from another execution context
setTimeout(() => {
if (someConiditon) {
interruptibleTask.interrupt()
}
}, 100)
You can nest generators within generators, and interrupt them as well. The interrupt will buble all the way to the parent generator:
function *makeFoo(a, b) {
// Execution can also be stopped inside this generator
const resA = yield makeA(a)
const resB = yield makeB(b)
return resA + resB
}
async function *interruptibleWork (a, b) {
const foo = yield makeFoo(a, b)
const result = await processFoo(foo) // `await` wont stop execution even if operation is interrupted
return result
}
You may have noticed that in our example we used both await
and yield
keywords:
async function *interruptibleWork (a, b) {
const foo = yield makeFoo(a, b)
-----
const result = await processFoo(foo) // `await` wont stop execution even if operation is interrupted
-----
return result
}
These are async generators, and are an ECMAScript Stage 3 proposal. In order to write these functions, you'll need to transpile your code with the appropriate babel plugin.
You may want to interrupt code for different reasons, principally in order to prevent work that is no longer needed.
The motivation for writing interruptible
comes from the need of having a simple
abstraction to be able write complex asynchronous tasks that could be
interrupted at different points of their execution by a scheduling system.
It is trivial to interrupt a single function using a conditional based on some
state, but it becomes extremely cumbersome to do so systematically across large
amounts of code when you want to be able to interrupt at different execution
points, and it ends up littering the code with if
statements.
interruptible
provides a simple mechanism to do so, albeit with the cost
of additional mental overhead of keeping in mind what using yield
implies.
See libdef.js
FAQs
Make functions interruptible
The npm package interruptible receives a total of 3 weekly downloads. As such, interruptible popularity was classified as not popular.
We found that interruptible 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.