Skip to content

Commit d57a5d8

Browse files
authored
refactor: clarify runtimes and isNode behavior (#108)
1 parent 3250631 commit d57a5d8

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,18 @@ console.log(runtimeInfo);
8282

8383
You can also use individual named exports for each runtime detection:
8484

85+
> [!NOTE]
86+
> 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.
87+
>
88+
> Use `runtime === "node"` if you need strict check for Node.js runtime.
89+
90+
- `isNode`
91+
- `isBun`
92+
- `isDeno`
8593
- `isNetlify`
8694
- `isEdgeLight`
8795
- `isWorkerd`
88-
- `isDeno`
8996
- `isLagon`
90-
- `isNode`
91-
- `isBun`
9297
- `isFastly`
9398

9499
List of well known providers can be found from [./src/runtimes.ts](./src/runtimes.ts).

src/runtimes.ts

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,64 @@ export type RuntimeName =
1212

1313
export type RuntimeInfo = { name: RuntimeName };
1414

15+
/**
16+
* Indicates if running in Node.js or a Node.js compatible runtime.
17+
*
18+
* **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.
19+
*
20+
* Use `runtime === "node"` if you need strict check for Node.js runtime.
21+
*/
22+
export const isNode = globalThis.process?.release?.name === "node";
23+
24+
/**
25+
* Indicates if running in Bun runtime.
26+
*/
27+
export const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun;
28+
29+
/**
30+
* Indicates if running in Deno runtime.
31+
*/
32+
export const isDeno = !!globalThis.Deno;
33+
34+
/**
35+
* Indicates if running in Fastly runtime.
36+
*/
37+
export const isFastly = !!globalThis.fastly;
38+
39+
/**
40+
* Indicates if running in Netlify runtime.
41+
*/
1542
export const isNetlify = !!globalThis.Netlify;
43+
44+
/**
45+
*
46+
* Indicates if running in EdgeLight (Vercel Edge) runtime.
47+
*/
1648
export const isEdgeLight = !!globalThis.EdgeRuntime;
1749
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
50+
51+
/**
52+
* Indicates if running in Cloudflare Workers runtime.
53+
*/
1854
export const isWorkerd =
1955
globalThis.navigator?.userAgent === "Cloudflare-Workers";
20-
export const isDeno = !!globalThis.Deno;
21-
// https://nodejs.org/api/process.html#processrelease
56+
57+
/**
58+
* Indicates if running in Lagon runtime.
59+
*
60+
* @deprecated https://github.com/unjs/std-env/issues/105
61+
*/
2262
export const isLagon = !!globalThis.__lagon__;
23-
export const isNode = globalThis.process?.release?.name === "node";
24-
export const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun;
25-
export const isFastly = !!globalThis.fastly;
2663

2764
const runtimeChecks: [boolean, RuntimeName][] = [
2865
[isNetlify, "netlify"],
2966
[isEdgeLight, "edge-light"],
3067
[isWorkerd, "workerd"],
68+
[isFastly, "fastly"],
3169
[isDeno, "deno"],
32-
[isLagon, "lagon"],
3370
[isBun, "bun"],
3471
[isNode, "node"],
35-
[isFastly, "fastly"],
72+
[isLagon, "lagon"],
3673
];
3774

3875
function _detectRuntime(): RuntimeInfo | undefined {

test/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ describe("std-env", () => {
2525
"process",
2626
"providerInfo",
2727
"provider",
28+
"isNode",
29+
"isBun",
30+
"isDeno",
31+
"isFastly",
2832
"isNetlify",
2933
"isEdgeLight",
3034
"isWorkerd",
31-
"isDeno",
3235
"isLagon",
33-
"isNode",
34-
"isBun",
35-
"isFastly",
3636
"runtimeInfo",
3737
"runtime",
3838
]

0 commit comments

Comments
 (0)