From e7f32442123cb553d36118644996ea33f43d4d51 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:28:36 +0100 Subject: [PATCH 01/10] docs: clarify `isNode` flag behavior --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 776c030..dfac3a1 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,9 @@ 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 true. Use `runtime==="node"` if you need explicit check. + - `isNetlify` - `isEdgeLight` - `isWorkerd` From 5c94fba47765c51ddcacdd3e7c72b54ad7c6a675 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:29:59 +0100 Subject: [PATCH 02/10] try important --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dfac3a1..900995a 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ console.log(runtimeInfo); You can also use individual named exports for each runtime detection: -> **Note** +> [!IMPORTANT] > When Bun and Deno run in Node.js compatibility mode, `isNode` flag will be true. Use `runtime==="node"` if you need explicit check. - `isNetlify` From b1c36ff9d6dd8e6c48be4a347a3b38c57f044fc5 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:30:46 +0100 Subject: [PATCH 03/10] update --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 900995a..07fa795 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,6 @@ console.log(runtimeInfo); You can also use individual named exports for each runtime detection: -> [!IMPORTANT] -> When Bun and Deno run in Node.js compatibility mode, `isNode` flag will be true. Use `runtime==="node"` if you need explicit check. - - `isNetlify` - `isEdgeLight` - `isWorkerd` @@ -94,6 +91,9 @@ You can also use individual named exports for each runtime detection: - `isBun` - `isFastly` +> [!NOTE] +> When Bun and Deno run in Node.js compatibility mode, `isNode` flag will be true. Use `runtime == "node"` if you need explicit check. + List of well known providers can be found from [./src/runtimes.ts](./src/runtimes.ts). ## Platform-Agnostic `env` From 479ed7c776ac357f3eb55e4c7f9b3ea1b4f63f6b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:31:40 +0100 Subject: [PATCH 04/10] update --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 07fa795..a1406ff 100644 --- a/README.md +++ b/README.md @@ -82,18 +82,19 @@ 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`. Use `runtime == "node"` if you need explicit check. + +- `isNode` +- `isBun` +- `isDeno` + - `isNetlify` - `isEdgeLight` - `isWorkerd` -- `isDeno` - `isLagon` -- `isNode` -- `isBun` - `isFastly` -> [!NOTE] -> When Bun and Deno run in Node.js compatibility mode, `isNode` flag will be true. Use `runtime == "node"` if you need explicit check. - List of well known providers can be found from [./src/runtimes.ts](./src/runtimes.ts). ## Platform-Agnostic `env` From 0906bc702a1cc61cf2ed1a06275fa4f7862f7430 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:31:58 +0100 Subject: [PATCH 05/10] update --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a1406ff..0642f42 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,6 @@ You can also use individual named exports for each runtime detection: - `isNode` - `isBun` - `isDeno` - - `isNetlify` - `isEdgeLight` - `isWorkerd` From 3ae961c605e3ccf0352d942972ffa1e9581af7dd Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:32:20 +0100 Subject: [PATCH 06/10] update --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0642f42..f60e80d 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,8 @@ 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`. Use `runtime == "node"` if you need explicit check. +> When Bun and Deno run in Node.js compatibility mode, `isNode` flag will be also `true`. +> Use `runtime == "node"` if you need strict check for Node.js runtime. - `isNode` - `isBun` From 8f2255997ebb6724f8b7c8e8ac12cfb98cae44b2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:32:40 +0100 Subject: [PATCH 07/10] update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f60e80d..0e93c88 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ 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`. +> > Use `runtime == "node"` if you need strict check for Node.js runtime. - `isNode` From 052416a5eb3c5810473b8d6f9e92c34beca92887 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:37:22 +0100 Subject: [PATCH 08/10] 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 { From 381069cd35ea762e05247c4cb215ffd2085b45c9 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Fri, 22 Dec 2023 09:37:57 +0000 Subject: [PATCH 09/10] chore: apply automated fixes --- test/index.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index 48e0adb..dc6ecd5 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -25,14 +25,14 @@ describe("std-env", () => { "process", "providerInfo", "provider", + "isNode", + "isBun", + "isDeno", + "isFastly", "isNetlify", "isEdgeLight", "isWorkerd", - "isDeno", "isLagon", - "isNode", - "isBun", - "isFastly", "runtimeInfo", "runtime", ] From bec505da90e96f177d11091a556fd7a1a1f29dda Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 22 Dec 2023 10:38:26 +0100 Subject: [PATCH 10/10] fix typo --- README.md | 2 +- src/runtimes.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f24e511..966a379 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ You can also use individual named exports for each runtime detection: > [!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. +> Use `runtime === "node"` if you need strict check for Node.js runtime. - `isNode` - `isBun` diff --git a/src/runtimes.ts b/src/runtimes.ts index 10a90f1..a8e471e 100644 --- a/src/runtimes.ts +++ b/src/runtimes.ts @@ -17,7 +17,7 @@ export type RuntimeInfo = { name: RuntimeName }; * * **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. + * Use `runtime === "node"` if you need strict check for Node.js runtime. */ export const isNode = globalThis.process?.release?.name === "node";