Skip to content

Commit 91aeb6a

Browse files
committed
fix: detect more JS runtime
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
1 parent e687b02 commit 91aeb6a

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/utils.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ import type { Fn, Statistics } from './types';
33

44
/**
55
* The JavaScript runtime environment.
6+
* @see https://runtime-keys.proposal.wintercg.org/
67
*/
78
export enum JSRuntime {
89
v8 = 'v8',
910
bun = 'bun',
1011
deno = 'deno',
1112
node = 'node',
13+
netlify = 'netlify',
14+
'edge-light' = 'edge-light',
15+
lagon = 'lagon',
16+
fastly = 'fastly',
1217
'quickjs-ng' = 'quickjs-ng',
1318
spidermonkey = 'spidermonkey',
1419
hermes = 'hermes',
1520
jsc = 'jsc',
1621
workerd = 'workerd',
17-
'XS Moddable' = 'XS Moddable',
22+
moddable = 'moddable',
1823
browser = 'browser',
1924
}
2025

@@ -35,10 +40,18 @@ const isQuickJsNg = !!(globalThis as any).navigator?.userAgent
3540
?.toLowerCase?.()
3641
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
3742
?.includes?.('quickjs-ng');
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
44+
const isNetlify = typeof (globalThis as any).Netlify === 'object';
45+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
46+
const isEdgeLight = typeof (globalThis as any).EdgeRuntime === 'string';
47+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
48+
const isLagon = !!(globalThis as any).__lagon__;
49+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
50+
const isFastly = !!(globalThis as any).fastly;
3851
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unnecessary-condition
3952
const isSpiderMonkey = !!(globalThis as any).inIon && !!(globalThis as any).performance?.mozMemory;
4053
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
41-
const isXsModdable = !!(globalThis as any).$262
54+
const isModdable = !!(globalThis as any).$262
4255
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
4356
&& !!(globalThis as any).lockdown
4457
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
@@ -52,25 +65,25 @@ const isJsc = !!(globalThis as any).$
5265
const isBrowser = !!(globalThis as any).window && !!(globalThis as any).navigator;
5366

5467
export const runtime: JSRuntime | 'unknown' = (() => {
55-
if (isV8) return JSRuntime.v8;
56-
if (isSpiderMonkey) return JSRuntime.spidermonkey;
57-
if (isQuickJsNg) return JSRuntime['quickjs-ng'];
58-
if (isXsModdable) return JSRuntime['XS Moddable'];
59-
if (isJsc) return JSRuntime.jsc;
6068
if (isBun) return JSRuntime.bun;
6169
if (isDeno) return JSRuntime.deno;
6270
if (isNode) return JSRuntime.node;
6371
if (isHermes) return JSRuntime.hermes;
72+
if (isNetlify) return JSRuntime.netlify;
73+
if (isEdgeLight) return JSRuntime['edge-light'];
74+
if (isLagon) return JSRuntime.lagon;
75+
if (isFastly) return JSRuntime.fastly;
6476
if (isWorkerd) return JSRuntime.workerd;
77+
if (isQuickJsNg) return JSRuntime['quickjs-ng'];
78+
if (isModdable) return JSRuntime.moddable;
79+
if (isV8) return JSRuntime.v8;
80+
if (isSpiderMonkey) return JSRuntime.spidermonkey;
81+
if (isJsc) return JSRuntime.jsc;
6582
if (isBrowser) return JSRuntime.browser;
6683
return 'unknown';
6784
})();
6885

6986
export const runtimeVersion: string = (() => {
70-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call,
71-
if (runtime === JSRuntime.v8) return (globalThis as any).version?.() as string;
72-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call,
73-
if (runtime === JSRuntime['quickjs-ng']) return (globalThis as any).navigator?.userAgent?.split?.('/')[1] as string;
7487
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
7588
if (runtime === JSRuntime.bun) return (globalThis as any).Bun?.version as string;
7689
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
@@ -84,6 +97,10 @@ export const runtimeVersion: string = (() => {
8497
'OSS Release Version'
8598
] as string;
8699
}
100+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call,
101+
if (runtime === JSRuntime.v8) return (globalThis as any).version?.() as string;
102+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call,
103+
if (runtime === JSRuntime['quickjs-ng']) return (globalThis as any).navigator?.userAgent?.split?.('/')[1] as string;
87104
return 'unknown';
88105
})();
89106

0 commit comments

Comments
 (0)