From 052416a5eb3c5810473b8d6f9e92c34beca92887 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:37:22 +0100 Subject: [PATCH] update --- README.md | 2 +- src/runtimes.ts | 51 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0e93c88..f24e511 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ console.log(runtimeInfo); You can also use individual named exports for each runtime detection: > [!NOTE] -> When Bun and Deno run in Node.js compatibility mode, `isNode` flag will be also `true`. +> When running code in Bun and Deno with Node.js compatibility mode, `isNode` flag will be also `true`, indicating running in a Node.js compatible runtime. > > Use `runtime == "node"` if you need strict check for Node.js runtime. diff --git a/src/runtimes.ts b/src/runtimes.ts index 6193c10..10a90f1 100644 --- a/src/runtimes.ts +++ b/src/runtimes.ts @@ -12,27 +12,64 @@ export type RuntimeName = export type RuntimeInfo = { name: RuntimeName }; +/** + * Indicates if running in Node.js or a Node.js compatible runtime. + * + * **Note:** When running code in Bun and Deno with Node.js compatibility mode, `isNode` flag will be also `true`, indicating running in a Node.js compatible runtime. + * + * Use `runtime == "node"` if you need strict check for Node.js runtime. + */ +export const isNode = globalThis.process?.release?.name === "node"; + +/** + * Indicates if running in Bun runtime. + */ +export const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun; + +/** + * Indicates if running in Deno runtime. + */ +export const isDeno = !!globalThis.Deno; + +/** + * Indicates if running in Fastly runtime. + */ +export const isFastly = !!globalThis.fastly; + +/** + * Indicates if running in Netlify runtime. + */ export const isNetlify = !!globalThis.Netlify; + +/** + * + * Indicates if running in EdgeLight (Vercel Edge) runtime. + */ export const isEdgeLight = !!globalThis.EdgeRuntime; // https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent + +/** + * Indicates if running in Cloudflare Workers runtime. + */ export const isWorkerd = globalThis.navigator?.userAgent === "Cloudflare-Workers"; -export const isDeno = !!globalThis.Deno; -// https://nodejs.org/api/process.html#processrelease + +/** + * Indicates if running in Lagon runtime. + * + * @deprecated https://github.com/unjs/std-env/issues/105 + */ export const isLagon = !!globalThis.__lagon__; -export const isNode = globalThis.process?.release?.name === "node"; -export const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun; -export const isFastly = !!globalThis.fastly; const runtimeChecks: [boolean, RuntimeName][] = [ [isNetlify, "netlify"], [isEdgeLight, "edge-light"], [isWorkerd, "workerd"], + [isFastly, "fastly"], [isDeno, "deno"], - [isLagon, "lagon"], [isBun, "bun"], [isNode, "node"], - [isFastly, "fastly"], + [isLagon, "lagon"], ]; function _detectRuntime(): RuntimeInfo | undefined {