@@ -40,12 +40,6 @@ function getBraveVersion() {
40
40
return match ? match [ 2 ] : "Unknown" ;
41
41
}
42
42
43
- function getVivaldiVersion ( ) {
44
- const ua = navigator . userAgent ;
45
- const match = ua . match ( / V i v a l d i \/ ( [ 0 - 9 ] + ) \. / ) ;
46
- return match ? match [ 1 ] : "Unknown" ;
47
- }
48
-
49
43
/**
50
44
* Enum of supported Runtimes.
51
45
* @enum {string}
@@ -58,6 +52,20 @@ export enum Runtime {
58
52
Unsupported = "unsupported" ,
59
53
}
60
54
55
+ /**
56
+ * Enum of supported Operating Systems.
57
+ * @enum {string}
58
+ */
59
+ export enum OperatingSystem {
60
+ Windows = "windows" ,
61
+ macOS = "macos" ,
62
+ Linux = "linux" ,
63
+ Android = "android" ,
64
+ Unix = "unix" ,
65
+ iOS = "ios" ,
66
+ Unsupported = "unsupported" ,
67
+ }
68
+
61
69
/**
62
70
* Enum of supported Products.
63
71
* @enum {string}
@@ -75,12 +83,23 @@ export enum Product {
75
83
Edge = "edge" ,
76
84
Opera = "opera" ,
77
85
Brave = "brave" ,
78
- Vivaldi = "vivaldi" ,
79
86
80
87
// And unsupported
81
88
Unsupported = "unsupported" ,
82
89
}
83
90
91
+ /**
92
+ * Enum of common CPU architectures.
93
+ * @enum {string}
94
+ */
95
+ export enum Architecture {
96
+ x86 = "x86" ,
97
+ x64 = "x86_64" ,
98
+ arm = "arm" ,
99
+ arm64 = "arm64" ,
100
+ Unsupported = "unsupported" ,
101
+ }
102
+
84
103
/**
85
104
* Dynamically determines the current runtime environment.
86
105
*/
@@ -107,6 +126,80 @@ export function getCurrentRuntime(): Runtime {
107
126
}
108
127
}
109
128
129
+ /**
130
+ * Dynamically determines the current operating system.
131
+ */
132
+ export function getCurrentOS ( ) : OperatingSystem {
133
+ const runtime = getCurrentRuntime ( ) ;
134
+ switch ( runtime ) {
135
+ case Runtime . Deno :
136
+ switch ( Deno . build . os ) {
137
+ case "darwin" :
138
+ return OperatingSystem . macOS ;
139
+ case "windows" :
140
+ return OperatingSystem . Windows ;
141
+ case "linux" :
142
+ return OperatingSystem . Linux ;
143
+ case "android" :
144
+ return OperatingSystem . Android ;
145
+ case "aix" :
146
+ case "freebsd" :
147
+ case "illumos" :
148
+ case "netbsd" :
149
+ case "solaris" :
150
+ return OperatingSystem . Unix ;
151
+ }
152
+ return OperatingSystem . Unsupported ;
153
+ case Runtime . Node :
154
+ // @ts -ignore Cross Runtime
155
+ switch ( process . platform ) {
156
+ case "darwin" :
157
+ return OperatingSystem . macOS ;
158
+ case "win32" :
159
+ return OperatingSystem . Windows ;
160
+ case "linux" :
161
+ return OperatingSystem . Linux ;
162
+ case "android" :
163
+ return OperatingSystem . Android ;
164
+ case "aix" :
165
+ case "freebsd" :
166
+ case "openbsd" :
167
+ case "sunos" :
168
+ return OperatingSystem . Unix ;
169
+ }
170
+ return OperatingSystem . Unsupported ;
171
+ case Runtime . Bun :
172
+ // @ts -ignore Cross Runtime
173
+ switch ( process . platform ) {
174
+ case "darwin" :
175
+ return OperatingSystem . macOS ;
176
+ case "win32" :
177
+ return OperatingSystem . Windows ;
178
+ case "linux" :
179
+ return OperatingSystem . Linux ;
180
+ case "android" :
181
+ return OperatingSystem . Android ;
182
+ case "aix" :
183
+ case "freebsd" :
184
+ case "openbsd" :
185
+ case "sunos" :
186
+ return OperatingSystem . Unix ;
187
+ }
188
+ return OperatingSystem . Unsupported ;
189
+ case Runtime . Browser : {
190
+ if ( "userAgent" in navigator ) {
191
+ const userAgent = navigator . userAgent ;
192
+ if ( userAgent . indexOf ( "Win" ) !== - 1 ) return OperatingSystem . Windows ;
193
+ if ( userAgent . indexOf ( "like Mac" ) !== - 1 ) return OperatingSystem . iOS ;
194
+ if ( userAgent . indexOf ( "Mac" ) !== - 1 ) return OperatingSystem . macOS ;
195
+ if ( userAgent . indexOf ( "Android" ) !== - 1 ) return OperatingSystem . Android ;
196
+ if ( userAgent . indexOf ( "X11" ) !== - 1 || userAgent . indexOf ( "Linux" ) !== - 1 ) return OperatingSystem . Linux ;
197
+ }
198
+ }
199
+ }
200
+ return OperatingSystem . Unsupported ;
201
+ }
202
+
110
203
/**
111
204
* Dynamically determines the current browser and its version (if applicable).
112
205
*/
@@ -126,8 +219,6 @@ export function getCurrentProduct(): Product {
126
219
return Product . Opera ;
127
220
} else if ( "brave" in navigator ) {
128
221
return Product . Brave ;
129
- } else if ( userAgent . indexOf ( "Vivaldi" ) !== - 1 ) {
130
- return Product . Vivaldi ;
131
222
} else if ( userAgent . indexOf ( "Safari" ) !== - 1 && userAgent . indexOf ( "Chrome" ) === - 1 ) {
132
223
return Product . Safari ;
133
224
} else if ( userAgent . indexOf ( "Edg" ) !== - 1 ) {
@@ -173,13 +264,77 @@ export function getCurrentVersion(): string | undefined {
173
264
return getOperaVersion ( ) ;
174
265
case Product . Brave :
175
266
return getBraveVersion ( ) ;
176
- case Product . Vivaldi :
177
- return getVivaldiVersion ( ) ;
178
267
default :
179
268
return undefined ;
180
269
}
181
270
}
182
271
272
+ /**
273
+ * Attempts to determine the current CPU architecture of the runtime's environment.
274
+ */
275
+ export function getCurrentArchitecture ( ) : Architecture {
276
+ const runtime = getCurrentRuntime ( ) ;
277
+ switch ( runtime ) {
278
+ case Runtime . Deno :
279
+ if ( Deno . build . arch === "x86_64" ) {
280
+ return Architecture . x64 ;
281
+ }
282
+ if ( Deno . build . arch === "aarch64" ) {
283
+ return Architecture . arm64 ;
284
+ }
285
+ if ( Deno . build . os === "darwin" ) {
286
+ return Architecture . x64 ;
287
+ }
288
+ return Architecture . x86 ;
289
+ case Runtime . Bun :
290
+ case Runtime . Node :
291
+ // @ts -ignore Cross Runtime
292
+ switch ( process . arch ) {
293
+ case "arm" :
294
+ return Architecture . arm ;
295
+ case "arm64" :
296
+ return Architecture . arm64 ;
297
+ case "ia32" :
298
+ return Architecture . x86 ;
299
+ case "x64" :
300
+ return Architecture . x64 ;
301
+ case "loong64" :
302
+ case "mips" :
303
+ case "mipsel" :
304
+ case "ppc" :
305
+ case "ppc64" :
306
+ case "riscv64" :
307
+ case "s390" :
308
+ case "s390x" :
309
+ return Architecture . Unsupported ;
310
+ }
311
+ return Architecture . Unsupported ;
312
+ case Runtime . Browser : {
313
+ const userAgent = navigator . userAgent ;
314
+ // @ts -ignore Cross Runtime
315
+ const platform = navigator . platform ;
316
+ // Clues for x86/x64
317
+ if ( platform . indexOf ( "Win64" ) !== - 1 || platform . indexOf ( "x64" ) !== - 1 ) {
318
+ return Architecture . x64 ;
319
+ } else if ( platform . indexOf ( "Win32" ) !== - 1 || platform . indexOf ( "x86" ) !== - 1 ) {
320
+ return Architecture . x86 ;
321
+ }
322
+ // Clues for ARM
323
+ if ( userAgent . indexOf ( "arm64" ) !== - 1 ) {
324
+ return Architecture . arm64 ;
325
+ } else if ( userAgent . indexOf ( "arm" ) !== - 1 ) {
326
+ return Architecture . arm ;
327
+ // @ts -ignore Cross Runtime
328
+ } else if ( platform . indexOf ( "iPhone" ) || platform . indexOf ( "iPad" ) || ( userAgent . indexOf ( "Mac" ) !== - 1 && "ontouchend" in document ) ) {
329
+ // Likely aarch64 on newer iOS devices and Apple Silicon Macs
330
+ return Architecture . arm64 ;
331
+ }
332
+ return Architecture . Unsupported ;
333
+ }
334
+ }
335
+ return Architecture . Unsupported ;
336
+ }
337
+
183
338
/**
184
339
* Static variable containing the current runtime.
185
340
*/
@@ -194,3 +349,13 @@ export const CurrentProduct: Product = getCurrentProduct();
194
349
* Static variable containing the current product/runtime version.
195
350
*/
196
351
export const CurrentVersion : string | undefined = getCurrentVersion ( ) ;
352
+
353
+ /**
354
+ * Static variable containing the current operating system.
355
+ */
356
+ export const CurrentOS : string | undefined = getCurrentOS ( ) ;
357
+
358
+ /**
359
+ * Static variable containing the current operating system.
360
+ */
361
+ export const CurrentArchitecture : string | undefined = getCurrentArchitecture ( ) ;
0 commit comments