Last active
August 24, 2023 16:20
-
-
Save gtrabanco/38cb21f716253f75b9709521068498b1 to your computer and use it in GitHub Desktop.
Get the runtime, browser or web worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isBun() { | |
return Boolean(globalThis.Bun); | |
} | |
function isDeno() { | |
return Boolean(globalThis.Deno); | |
} | |
function isNode() { | |
return Boolean(globalThis.process?.versions?.node); | |
} | |
function isRuntime() { | |
return Boolean(globalThis.process) || isDeno(); | |
} | |
function isBrowser() { | |
return Boolean(globalThis.window) && !isDeno(); | |
} | |
function isWorker() { | |
return typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope; | |
} | |
function getEnv() { | |
if (isNode()) { | |
return "node"; | |
} | |
if (isBun()) { | |
return "bun"; | |
} | |
if (isDeno()) { | |
return "deno"; | |
} | |
if (isBrowser()) { | |
return "browser"; | |
} | |
if (isWorker()) { | |
return "worker"; | |
} | |
if (isRuntime()) { | |
return "runtime"; | |
} | |
return "unknown"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment