npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

threadwarden

v1.0.3

Published

A high-performance Node.js library that distributes work across multiple CPU cores using worker threads with automatic load balancing

Downloads

199

Readme

ThreadWarden - Distributed Worker Threading System 🚀

Demo

ThreadWarden is a high-performance Node.js library that distributes work across multiple CPU cores using worker threads. It automatically balances the load by sending tasks to the least busy thread, maximizing your application's performance.

✨ Features

  • 🧵 Multi-Threading: Utilizes all available CPU cores
  • ⚖️ Load Balancing: Automatically sends work to the least busy thread
  • 📨 Flexible Messaging: Send messages to specific workers or all workers
  • 🔄 Self-Healing: Workers automatically restart if they crash

🔧 Installation

npm install threadwarden

🚀 Usage

First, create a worker file that will be executed in each thread:

worker.js

// worker.js - This defines what each worker should do with received messages
module.exports = function (message, workerId) {
    // Process the message
    if (typeof message === "number")
        return message + 1;  // For numbers, add 1
        
    // For other messages, return a formatted response
    return `Worker ${workerId} processed: ${message}`;
}

Then, initialize ThreadWarden with your worker file:

index.js

async function main() {
    // Initialize ThreadWarden with the path to your worker file
    const { sendMsg, sendDirectMsg, sendToAll } = await require('threadwarden')("./worker.js");

    // Send a message to the least busy worker thread
    const response = await sendMsg(1);
    console.log("1+1=", response);  // Output: 1+1= 2

    // Send a message to a specific worker thread
    const specificResponse = await sendDirectMsg(2, "Hello worker!");
    console.log(specificResponse);  // Output depends on your worker implementation

    // Broadcast a message to all worker threads
    sendToAll({ notification: "System update in progress" });
}
main();

🔍 API Reference

require('threadwarden')(workerFilePath)

Initializes ThreadWarden with the specified worker file. Returns a Promise that resolves to an object containing the messaging functions.

sendMsg(message)

Sends a message to the worker with the lowest CPU load and returns a Promise with the response.

sendDirectMsg(workerId, message)

Sends a message to a specific worker by ID and returns a Promise with the response.

sendToAll(message)

Broadcasts a message to all workers (no response).

Worker File Structure

Your worker file should export a function that accepts two parameters:

  • message: The data sent to the worker
  • workerId: The ID of the worker processing the message

The function should return the result to be sent back to the main thread.


ThreadWarden - Dağıtılmış İş Parçacığı Sistemi 🚀

ThreadWarden, işleri birden çok CPU çekirdeğine worker thread'ler kullanarak dağıtan yüksek performanslı bir Node.js kütüphanesidir. En az meşgul olan iş parçacığına görevler göndererek yükü otomatik olarak dengeler ve uygulamanızın performansını en üst düzeye çıkarır.

✨ Özellikler

  • 🧵 Çoklu İş Parçacığı: Mevcut tüm CPU çekirdeklerini kullanır
  • ⚖️ Yük Dengeleme: İşi otomatik olarak en az meşgul iş parçacığına gönderir
  • 📨 Esnek Mesajlaşma: Belirli worker'lara veya tüm worker'lara mesaj gönderme
  • 🔄 Kendini Onarma: Worker'lar çökerse otomatik olarak yeniden başlar

🔧 Kurulum

npm install threadwarden

🚀 Kullanım

Önce, her iş parçacığında çalıştırılacak bir worker dosyası oluşturun:

worker.js

// worker.js - Her worker'ın gelen mesajlarla ne yapacağını tanımlar
module.exports = function (message, workerId) {
    // Mesajı işle
    if (typeof message === "number")
        return message + 1;  // Sayılar için 1 ekle
        
    // Diğer mesajlar için biçimlendirilmiş bir yanıt döndür
    return `Worker ${workerId} işledi: ${message}`;
}

Ardından, ThreadWarden'ı worker dosyanızla başlatın:

index.js

async function main() {
    // ThreadWarden'ı worker dosyanızın yoluyla başlatın
    const { sendMsg, sendDirectMsg, sendToAll } = await require('threadwarden')("./worker.js");

    // En az meşgul worker thread'e mesaj gönder
    const response = await sendMsg(1);
    console.log("1+1=", response);  // Çıktı: 1+1= 2

    // Belirli bir worker thread'e mesaj gönder
    const specificResponse = await sendDirectMsg(2, "Merhaba worker!");
    console.log(specificResponse);  // Çıktı worker implementasyonunuza bağlıdır

    // Tüm worker thread'lere mesaj yayınla
    sendToAll({ bildirim: "Sistem güncellemesi devam ediyor" });
}
main();

🔍 API Referansı

require('threadwarden')(workerFilePath)

ThreadWarden'ı belirtilen worker dosyasıyla başlatır. Mesajlaşma fonksiyonlarını içeren bir nesne ile çözülen bir Promise döndürür.

sendMsg(message)

En düşük CPU yüküne sahip worker'a mesaj gönderir ve yanıtla birlikte Promise döndürür.

sendDirectMsg(workerId, message)

Kimliğe göre belirli bir worker'a mesaj gönderir ve yanıtla birlikte Promise döndürür.

sendToAll(message)

Tüm worker'lara mesaj yayınlar (yanıt yoktur).

Worker Dosyası Yapısı

Worker dosyanız iki parametre alan bir fonksiyon dışa aktarmalıdır:

  • message: Worker'a gönderilen veri
  • workerId: Mesajı işleyen worker'ın ID'si

Fonksiyon, ana iş parçacığına geri gönderilecek sonucu döndürmelidir.