Skip to content

dandre3000/environment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@dandre3000/environment

Check if code is running on the main thread or a worker of a browser, node, bun or deno instance.

Installation

npm i @dandre3000/environment

Usage

import { isMainThread, runtime } from '@dandre3000/environment'

if (isMainThread) {
    const url = new URL(import.meta.url)
    
    if (runtime === 'node') {
        const worker = new (await import('node:worker_threads')).Worker(url)

        worker.on('message', data => console.log(data))
    } else {
        const worker = new Worker(url, { type: 'module' })

        worker.addEventListener('message', ({ data }) => console.log(data))
    }
} else {
    if (runtime === 'node') {
        (await import('node:worker_threads')).parentPort.postMessage('YOOO')
    } else {
        postMessage('YOOO')
    }
}

Exports

runtime: 'bun' | 'browser' | 'deno' | 'node'

The name of the current runtme.

isMainThread: boolean

True if the current global scope is the main thread or false otherwise.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors