Skip to content

Commit

Permalink
refactor: clarify runtimes and isNode behavior (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 22, 2023
1 parent 3250631 commit d57a5d8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
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

0 comments on commit d57a5d8

Please sign in to comment.