1
- import { RuntimeError } from "@terminal-nerds/snippets-error/custom" ;
1
+ /* import { RuntimeError } from "@terminal-nerds/snippets-error/custom"; */
2
2
import { z } from "zod" ;
3
3
/* prettier-ignore */
4
4
export const RUNTIME_ENVIRONMENTS = [
@@ -15,43 +15,65 @@ export const RUNTIME_ENVIRONMENTS = [
15
15
export type RuntimeEnvironment = ( typeof RUNTIME_ENVIRONMENTS ) [ number ] ;
16
16
export const RUNTIME_ENVIRONMENT_SCHEMA = z . enum ( RUNTIME_ENVIRONMENTS ) ;
17
17
18
- /** @see {@link https://bun.sh/docs/api/globals } */
18
+ /**
19
+ * @see {@link https://bun.sh/docs/api/globals }
20
+ */
19
21
export const IN_BUN = globalThis . Bun !== undefined && Bun . version !== undefined ;
20
22
21
- /** @see {@link https://deno.land/[email protected] /runtime#deno-global } */
23
+ /**
24
+ * @see {@link https://deno.land/[email protected] /runtime#deno-global }
25
+ */
22
26
export const IN_DENO = globalThis . Deno !== undefined && Deno . version !== undefined && Deno . version . deno !== undefined ;
23
27
24
- /** Detects if the DOM is available. */
28
+ /**
29
+ * Detects if the DOM is available.
30
+ */
25
31
export const IN_DOM =
26
32
globalThis . window !== undefined &&
27
33
! globalThis . navigator !== undefined &&
28
34
// @ts -ignore FIXME: Find a way to make it typed?
29
35
window . document !== undefined ;
30
36
31
- /** @see {@link https://edge-runtime.vercel.app/features/available-apis#addressing-the-runtime } */
37
+ /**
38
+ * @see {@link https://edge-runtime.vercel.app/features/available-apis#addressing-the-runtime }
39
+ */
32
40
// @ts -ignore FIXME: Find a way to make it typed?
33
41
export const IN_EDGE = globalThis . EdgeRuntime !== undefined ;
34
42
35
- /** @see {@link https://github.com/jsdom/jsdom } */
43
+ /**
44
+ * @see {@link https://github.com/jsdom/jsdom }
45
+ */
36
46
export const IN_JSDOM = IN_DOM && navigator . userAgent . includes ( "jsdom" ) ;
37
47
38
- /** @see {@link https://github.com/capricorn86/happy-dom/discussions/481 } */
48
+ /**
49
+ * @see {@link https://github.com/capricorn86/happy-dom/discussions/481 }
50
+ */
39
51
// @ts -ignore FIXME: Find a way to make it typed?
40
52
export const IN_HAPPY_DOM = IN_DOM && window . happyDOM !== undefined ;
41
53
42
- /** @see {@link https://nodejs.org/dist/latest-v18.x/docs/api/process.html#process } */
54
+ /**
55
+ * @see {@link https://nodejs.org/dist/latest-v18.x/docs/api/process.html#process }
56
+ */
43
57
export const IN_NODE = globalThis . process !== undefined && process . version !== undefined ;
44
58
45
- /** Detect if is running in browser, and is not in a browser emulator (e.g. jsdom or Happy DOM) */
59
+ /**
60
+ * Detect if is running in browser, and is not in a browser emulator (e.g. jsdom or Happy DOM)
61
+ */
46
62
export const IN_BROWSER = IN_DOM && ! IN_HAPPY_DOM && ! IN_JSDOM ;
47
63
48
- /** @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers } */
64
+ /**
65
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers }
66
+ */
49
67
export const IN_WEB_WORKER =
50
68
typeof self === "object" && self . constructor && self . constructor . name === "DedicatedWorkerGlobalScope" ;
51
69
52
- /** Receive the name _(in lowercase)_ of the currently running JavaScript environment. */
70
+ /**
71
+ * Receive the name _(in lowercase)_ of the currently running JavaScript environment.
72
+ */
53
73
export function getRuntimeEnvironmentName ( ) : RuntimeEnvironment {
54
- /** NOTE: Order matters! */
74
+ /**
75
+ * NOTE: Order matters!
76
+ */
55
77
const conditions = {
56
78
"edge" : IN_EDGE ,
57
79
"bun" : IN_BUN ,
@@ -68,7 +90,7 @@ export function getRuntimeEnvironmentName(): RuntimeEnvironment {
68
90
if ( isTrue ) return name as RuntimeEnvironment ;
69
91
}
70
92
71
- throw new RuntimeError ( `Unrecognized JavaScript runtime environment!` ) ;
93
+ throw new Error ( `Unrecognized JavaScript runtime environment!` ) ;
72
94
}
73
95
74
96
export function validateRuntimeEnvironmentName ( name : string ) : asserts name is RuntimeEnvironment {
0 commit comments