Skip to content

Instantly share code, notes, and snippets.

@gtrabanco
Last active August 24, 2023 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gtrabanco/38cb21f716253f75b9709521068498b1 to your computer and use it in GitHub Desktop.
Save gtrabanco/38cb21f716253f75b9709521068498b1 to your computer and use it in GitHub Desktop.
Get the runtime, browser or web worker
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