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

feat: detect the underlying runtime #66

Merged
merged 12 commits into from
Aug 16, 2023
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const { env, isDevelopment, isProduction } = require("std-env");
- `isColorSupported`
- `nodeVersion`
- `nodeMajorVersion`
- `isNetlify`
- `isEdgeLight`
- `isWorkerd`
- `isDeno`
- `isLagon`
- `isNode`
- `isBun`
- `isFastly`

You can read more about how each flag works from [./src/flags.ts](./src/flags.ts).

Expand All @@ -55,12 +63,12 @@ You can read more about how each flag works from [./src/flags.ts](./src/flags.ts
You can use `isCI` and `platform` exports to detect it:

```ts
import { isCI, platform, platformInfo } from "std-env";
import { isCI, provider, providerInfo } from "std-env";

console.log({
isCI, // true
platform, // "github_actions"
platformInfo, // { name: "github_actions", isCI: true }
provider, // "github_actions"
providerInfo, // { name: "github_actions", isCI: true }
});
```

Expand All @@ -75,7 +83,23 @@ console.log(detectProvider({ VERCEL: "1" }));

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

## Platform agnotic env
## Runtime Detection

`std-env` can automatically detect the current JavaScript runtime based on global variables, following the [WinterCG Runtime Keys proposal](https://runtime-keys.proposal.wintercg.org/):

```ts
import { runtime, runtimeInfo } from "std-env";

// "" | "node" | "deno" | "bun" | "workerd" | "lagon" ...
console.log(runtime);

// { name: "node" }
console.log(runtimeInfo);
```

You can also use `isNode`, `isDeno`, `isBun`, `isWorkerd`,... helper exports.

## Platform agnostic env

`std-env` provides a lightweight proxy to access environment variables in a platform agnostic way.

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./env";
export * from "./flags";
export * from "./process";
export * from "./providers";
export * from "./runtimes";
57 changes: 57 additions & 0 deletions src/runtimes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable no-var */
declare global {
var EdgeRuntime: any;
var Netlify: any;
var Deno: any;
var __lagon__: any;
var fastly: any;
}

// https://runtime-keys.proposal.wintercg.org/
export type RuntimeName =
| "workerd"
| "deno"
| "lagon"
| "netlify"
| "node"
| "bun"
| "edge-light"
| "fastly"
| "";

export type RuntimeInfo = { name: RuntimeName };

export const isNetlify = !!globalThis.Netlify;
export const isEdgeLight = !!globalThis.EdgeRuntime;
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
export const isWorkerd =
globalThis.navigator?.userAgent === "Cloudflare-Workers";
export const isDeno = !!globalThis.Deno;
// https://nodejs.org/api/process.html#processrelease
export const isLagon = !!globalThis.__lagon__;
export const isNode = globalThis.process?.release?.name === "node";
export const isBun = globalThis.process?.release?.name === "bun";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could be mistaken but i believe we updated this property to be "node" due to a compatibility fix of an old package.

use !!globalThis.bun or !!process.versions.bun

export const isFastly = !!globalThis.fastly;

const runtimeChecks: [boolean, RuntimeName][] = [
[isNetlify, "netlify"],
[isEdgeLight, "edge-light"],
[isWorkerd, "workerd"],
[isDeno, "deno"],
[isLagon, "lagon"],
[isNode, "node"],
[isBun, "bun"],
[isFastly, "fastly"],
];

function _detectRuntime(): RuntimeInfo | undefined {
const detectedRuntime = runtimeChecks.find((check) => check[0]);
if (detectedRuntime) {
const name = detectedRuntime[1];
return { name };
}
}

export const runtimeInfo = _detectRuntime();

export const runtime = runtimeInfo?.name || "";
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ describe("std-env", () => {
"_process",
"process",
"detectProvider",
"isNetlify",
"isEdgeLight",
"isWorkerd",
"isDeno",
"isLagon",
"isNode",
"isBun",
"isFastly",
"runtimeInfo",
"runtime",
]
`);
});
Expand Down