-
Notifications
You must be signed in to change notification settings - Fork 26
/
runtimes.ts
76 lines (64 loc) · 1.89 KB
/
runtimes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// https://runtime-keys.proposal.wintercg.org/
export type RuntimeName =
| "workerd"
| "deno"
| "netlify"
| "node"
| "bun"
| "edge-light"
| "fastly"
| "";
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";
const runtimeChecks: [boolean, RuntimeName][] = [
[isNetlify, "netlify"],
[isEdgeLight, "edge-light"],
[isWorkerd, "workerd"],
[isFastly, "fastly"],
[isDeno, "deno"],
[isBun, "bun"],
[isNode, "node"],
];
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: RuntimeName = runtimeInfo?.name || "";