typed-worker-threads
v0.4.0
Published
Type-safe wrapper for Node.js Worker Threads
Downloads
43
Maintainers
Readme
Typed Worker Threads
Typed Worker Threads is a library designed to make working with threads in Node.js easier, while providing type safety with TypeScript. This README will guide you through setting up and using the library in your project.
Installation
Install the package using npm or yarn or pnpm.
npm install typed-worker-threadsUsage (recommended)
- Create a folder called
threadsin your project root. - Create a new file in the
threadsdirectory. Name it to describe the thread's purpose, for example,pdf_thread.tsorcompress_thread.ts. - In the file, import the
Threadclass and create a new instance of it. Pass the path to the file that will be executed in the thread as the first argument. The path is relative to thethreadsdirectory. The second argument is the name of the thread, which is optional and defaults to the name of the file. - Export the instance of the
Threadclass. - In the main thread, import the created thread and use it (handle events, send messages, etc.).
- In the worker thread, import
parentPortfrom the created thread instance and use it.
Here's an example of how to set up a typed worker thread:
// 'compress_thread.ts' file in 'threads' directory
import { Thread } from "typed-worker-threads";
type ParentToChild = {
type: "compress";
payload: {
name: string;
buffer: ArrayBuffer;
outDir: string;
compressionLevel: number;
type: "gzip" | "deflate";
};
};
type CompressionError = {
type: "error";
payload: { error: Error };
};
type CompressionSuccess = {
type: "success";
payload: {
name: string;
path: string;
};
};
type ChildToParent = CompressionError | CompressionSuccess;
const compressionThread = new Thread<ParentToChild, ChildToParent>(
"../dist/workers/compress_worker.js",
"compress_thread"
);
export default compressionThread;
// Main thread
import compressionThread from "./threads/compress_thread";
compressionThread.worker.on("message", (message) => {
switch (message.type) {
case "success":
console.log(`File ${message.payload.name} compressed successfully`);
break;
case "error":
console.error(message.payload.error);
break;
}
});
// Worker thread
import { parentPort } from "./threads/compress_thread";
parentPort.on("message", (message) => {
// Make heavy computations (compress file, etc.)
// If an error occurs, send an error message to the main thread
});API
Thread<ParentToChild, ChildToParent>: Class for creating and managing worker threads.parentPort: An instance ofMessagePortto communicate with the main thread.
Notice about transfer between threads
The "structured clone algorithm" is used to send messages between threads. Read more about it here. typed-worker-threads has a built-in type StructureCloned that describes the types that can be sent between threads safely. If you want to send a type that is not StructureCloned, you can use the transferList (supports ArrayBuffer, MessagePort, FileHandle) option when sending a message. Read more about it here.
License
Typed Worker Threads is released under the BSD-3-Clause License. See LICENSE for details.
