Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 22, 2023
1 parent 8f22559 commit 052416a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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

0 comments on commit 052416a

Please sign in to comment.