
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
@nnilky/workify-node
Advanced tools
A version of @nnilky/workify for Node.js workers, allowing to create worker interfaces to make requests
A version of @nnilky/workify for Node.js workers, allowing to create worker interfaces to make requests
npm install @nnilky/workify-node
// worker.ts
import { attachMessageHandler, type InferInterface } from "@nnilky/workify-node";
const add = (a: number, b: number) => a + b;
const handler = attachMessageHandler({ add });
export type Interface = InferInterface<typeof handler>;
onmessage = handler;
// client.ts
import { createWorker } from "@nnilky/workify-node";
import type { Interface } from "./worker";
const [worker] = createWorker<Interface>("./worker");
const result = await worker.add(1, 2);
console.log(`1 + 2 = ${result}`);
You can construct a worker pool the same way you'd make a worker. You can optionally specify the number of workers to use with the default being os.availableParallelism()
.
import { createWorkerPool } from "@nnilky/workify-node";
import type { Interface } from "./worker";
const [worker] = createWorkerPool<Interface>("./worker");
const promises = []
for (let i = 0; i < 16; i++) {
promises.push(worker.renderFrame(index))
}
const frames = await Promise.all(promises)
This just redirects each function call to a different worker round robin style.
In order to transfer objects to and from workers, use transfer()
. You can only transfer types that are Transferable.
// In client
import { transfer } from "@nnilky/workify-node";
import type { Interface } from "./worker";
const [worker] = createWorker<Interface>("./worker");
const canvas = new OffscreenCanvas(100,100)
const image = canvas.transferToImageBitmap()
transfer(image)
worker.resizeImage(image)
// In a worker
import { transfer } from "@nnilky/workify-node";
const createImage = () => {
const canvas = new OffscreenCanvas(100,100)
const image = canvas.transferToImageBitmap()
transfer(image)
return image
}
const handler = attachMessageHandler({ createImage });
export type Interface = InferInterface<typeof handler>;
This works under the hood by creating a list of values that are included in the transfers in the next request/reponse.
Because of this, It's critical you do this right before sending a request/returning a response. This to avoid any race conditions caused by sending those objects with different request/response.
// ❌ Incorrect
const image = await createImage()
transfer(image)
const thumbnail = await generateThumbnail(image)
transfer(thumbnail)
return { image, thumbnail }
// ✔️ Correct
const image = await createImage()
const thumbnail = await generateThumbnail(image)
transfer(image)
transfer(thumbnail)
return { image, thumbnail }
In order to terminate workers when you don't need them, createWorker
and createWorkerPool
both return the actual workers as their second return value. You can use this to terminate your worker when you no longer need it.
Here's an example for a single request:
const [api, worker] = createWorker("./worker");
const result = await api.foo()
worker.terminate()
Under the hood, when you try to call a method on a worker, the reference to the function is proxied. Only the function name and arguments are sent to the worker, this is then recieved on the other end and mapped to the correct function.
The Interface
generic lets you have a usable developer experience by providing proper typing to the proxy object, otherwise you'll get no type completition on what methods are available.
FAQs
A version of @nnilky/workify for Node.js workers, allowing to create worker interfaces to make requests
The npm package @nnilky/workify-node receives a total of 3 weekly downloads. As such, @nnilky/workify-node popularity was classified as not popular.
We found that @nnilky/workify-node 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.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.