Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clarify runtimes and isNode behavior #108

Merged
merged 10 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,18 @@ console.log(runtimeInfo);

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.

- `isNode`
- `isBun`
- `isDeno`
- `isNetlify`
- `isEdgeLight`
- `isWorkerd`
- `isDeno`
- `isLagon`
- `isNode`
- `isBun`
- `isFastly`

List of well known providers can be found from [./src/runtimes.ts](./src/runtimes.ts).
Expand Down
51 changes: 44 additions & 7 deletions src/runtimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe("std-env", () => {
"process",
"providerInfo",
"provider",
"isNode",
"isBun",
"isDeno",
"isFastly",
"isNetlify",
"isEdgeLight",
"isWorkerd",
"isDeno",
"isLagon",
"isNode",
"isBun",
"isFastly",
"runtimeInfo",
"runtime",
]
Expand Down