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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dandre3000/thread

v1.0.4

Published

A class for managing web and node.js Workers.

Downloads

351

Readme

thread

Thread is a class that features a consistent API for using Workers on a web browser, node, bun or deno. All Threads are interconnected and can import modules to and call functions from each other.

Installation

npm install @dandre3000/thread

Usage

import { Thread } from '@dandre3000/thread'

// When a Thread is created
Thread.eventTarget.addEventListener('online', event => console.log('online event', event.thread.id, Thread.id))

// When a Thread has closed
Thread.eventTarget.addEventListener('exit', event => console.log('exit event', event.threadId, Thread.id))

// This function is exposed to other Threads
Thread.expose('test', (...args) => {
    return `Thread ${Thread.id} says: YO`
})

if (Thread.isMainThread) {
    console.log(Thread.id) // 0
    console.log(Thread.workerData) // null
    console.log(Thread.mainThread) // null

    Thread.create(999).then(async thread => {
        await thread.import(new URL(import.meta.url))

        console.log(await thread.invoke('test')) // Thread 1 says: YO
    })
} else {
    console.log(Thread.id) // 1
    console.log(Thread.workerData) // 999
    console.log(Thread.mainThread) // Thread instance for main thread
}

Exports

Class: Thread

Static properties

isMainThread: boolean

True if the current thread is the main thread.

id: number

Identifier for the current thread.

workerdata: any

Data sent to a thread upon creation.

transfer: Transferable[]

Array of objects that will be transfered and emptied whenever another thread uses Thread.prototype.invoke to call a function on this thread made available using Thread.expose.

// transfer a MessagePort from a worker to the main thread
import { Thread } from '@dandre3000/thread'

if (Thread.isMainThread) {
    const thread = await Thread.create()
    await thread.import(new URL(import.meta.url))

    ;(await thread.invoke('getPort')).postMessage('Transfer successful')
} else {
    Thread.expose('getPort', () => {
        const { port1, port2 } = new MessageChannel
        port1.addEventListener('message', ({ data }) => console.log(data))
        port1.start()
        Thread.transfer.push(port2)
        return port2
    })
}

eventTarget: EventTarget

The target for events broadcasted from other threads.

import { Thread } from '@dandre3000/thread'

Thread.eventTarget.addEventListener('online', event => console.log(event))
Thread.eventTarget.addEventListener('exit', event => console.log(event))

mainThread: Thread

The Thread instance connected to the main thread if the current thread is a worker otherwise null.

create(workerData: any): Promise

Return a Promise that resolves to a new Thread. This method is the only way to create a new Thread.

getThread(threadId: any): Thread

Return the Thread corresponding to the given threadId or return null if no online Thread exists where Thread.id === threadId.

getAllThreads(): Thread[]

Return an array of all online Threads.

expose(id: any, fn: Function): void

Make a function available to other threads when using Thread.prototype.invoke.

unexpose(id: any): void

Remove a function from those available to other threads when using Thread.prototype.invoke.

close(exitCode?: number): never

Alias for globalThis.close or process.exit.

Instance properties

id: number

Identifier for this Thread instance.

isOnline(): boolean

Returns true until the thread is closed.

import(moduleId: any, signal?: AbortSignal): Promise

Dynamically import an ES module to the thread and return a Promise that resolves when the module is loaded.

invoke(id: any, args?: any[], transfer?: (Transferable)[], signal?: AbortSignal): Promise

Call a function on the thread added using Thread.expose and return a Promise that resolves to the value returned by that function.

terminate(): Promise

Close this Thread instance.

Class: OnlineEvent

Dispatched when a Thread is created.

Instance properties

thread: thread

Class: ExitEvent

Dispatched when a Thread is closed.

Instance properties

thread: thread

exitCode: number

License

MIT