-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
442 lines (410 loc) · 13 KB
/
mod.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* Private functions */
function getChromeVersion(userAgent: string) {
const match = userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return match ? match[2] : "Unknown";
}
function getFirefoxVersion(userAgent: string) {
const match = userAgent.match(/Firefox\/([0-9]+)\./);
return match ? match[1] : "Unknown";
}
function getEdgeVersion(userAgent: string) {
const match = userAgent.match(/Edg\/([0-9]+)\./);
return match ? match[1] : "Unknown";
}
function getSafariVersion(userAgent: string) {
const match = userAgent.match(/Version\/([0-9]+)\.([0-9]+)(\.[0-9]+)?/);
if (match) return `${match[1]}.${match[2]}`;
return "Unknown";
}
function getOperaVersion(userAgent: string) {
// Look for either 'Opera/' or 'OPR/' followed by the version
const match = userAgent.match(/(Opera|OPR)\/([0-9]+)\./);
return match ? match[2] : "Unknown";
}
function getBraveVersion(userAgent: string) {
const match = userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return match ? match[2] : "Unknown";
}
/**
* Enum of supported Runtimes.
* @enum {string}
*/
export enum Runtime {
Deno = "deno",
Bun = "bun",
Node = "node",
Browser = "browser",
Workerd = "workerd",
Netlify = "netlify",
EdgeLight = "edgelight",
Fastly = "fastly",
Unsupported = "unsupported",
}
/**
* Enum of supported Operating Systems.
* @enum {string}
*/
export enum OperatingSystem {
Windows = "windows",
macOS = "macos",
Linux = "linux",
Android = "android",
Unix = "unix",
iOS = "ios",
Unsupported = "unsupported",
}
/**
* Enum of supported Products.
* @enum {string}
*/
export enum Product {
// All runtimes
Deno = "deno",
Bun = "bun",
Node = "node",
Workerd = "workerd",
Netlify = "netlify",
EdgeLight = "edgelight",
Fastly = "fastly",
// All browsers
Firefox = "firefox",
Safari = "safari",
Chrome = "chrome",
Edge = "edge",
Opera = "opera",
Brave = "brave",
// And unsupported
Unsupported = "unsupported",
}
/**
* Enum of common CPU architectures.
* @enum {string}
*/
export enum Architecture {
x86 = "x86",
x64 = "x86_64",
arm = "arm",
arm64 = "arm64",
Unsupported = "unsupported",
}
/**
* Verifies if a property exists in the global namespace and optionally checks its type.
*
* @param {string} name - The name of the property to verify.
* @param {string} [typeString] - The expected type of the property (optional).
* @returns {boolean} True if the property exists and matches the type (if provided), False otherwise.
*/
function verifyGlobal(name: string, typeString?: string) {
return name in globalThis && (!typeString || typeof (globalThis as Record<string, unknown>)[name] === typeString);
}
/**
* Dynamically determines the current runtime environment.
*/
export function getCurrentRuntime(): Runtime {
if (verifyGlobal("Deno", "object")) return Runtime.Deno;
if (verifyGlobal("Bun", "object")) return Runtime.Bun;
if (verifyGlobal("Netlify", "object")) return Runtime.Netlify;
if (verifyGlobal("EdgeRuntime", "string")) return Runtime.EdgeLight;
if (globalThis.navigator?.userAgent === "Cloudflare-Workers") return Runtime.Workerd;
if (verifyGlobal("fastly", "object")) return Runtime.Fastly;
if (
verifyGlobal("process", "object") &&
//@ts-ignore Runtime detection
typeof process.versions !== "undefined" &&
//@ts-ignore Runtime detection
typeof process.versions.node !== "undefined"
) {
return Runtime.Node;
}
if (verifyGlobal("window", "object")) { // Check for Browser
return Runtime.Browser;
}
return Runtime.Unsupported;
}
/**
* Dynamically determines the current operating system.
*/
export function getCurrentOS(): OperatingSystem {
const runtime = getCurrentRuntime();
switch (runtime) {
case Runtime.Deno:
switch (Deno.build.os) {
case "darwin":
return OperatingSystem.macOS;
case "windows":
return OperatingSystem.Windows;
case "linux":
return OperatingSystem.Linux;
case "android":
return OperatingSystem.Android;
case "aix":
case "freebsd":
case "illumos":
case "netbsd":
case "solaris":
return OperatingSystem.Unix;
}
return OperatingSystem.Unsupported;
case Runtime.Node:
// @ts-ignore Cross Runtime
switch (process.platform) {
case "darwin":
return OperatingSystem.macOS;
case "win32":
return OperatingSystem.Windows;
case "linux":
return OperatingSystem.Linux;
case "android":
return OperatingSystem.Android;
case "aix":
case "freebsd":
case "openbsd":
case "sunos":
return OperatingSystem.Unix;
}
return OperatingSystem.Unsupported;
case Runtime.Bun:
// @ts-ignore Cross Runtime
switch (process.platform) {
case "darwin":
return OperatingSystem.macOS;
case "win32":
return OperatingSystem.Windows;
case "linux":
return OperatingSystem.Linux;
case "android":
return OperatingSystem.Android;
case "aix":
case "freebsd":
case "openbsd":
case "sunos":
return OperatingSystem.Unix;
}
return OperatingSystem.Unsupported;
case Runtime.Browser: {
if ("userAgent" in navigator) {
const userAgent = navigator.userAgent;
return getOSFromUserAgent(userAgent);
}
}
}
return OperatingSystem.Unsupported;
}
/**
* Determine operating system from user agent string, if possible
*/
export function getOSFromUserAgent(userAgent: string): OperatingSystem {
if (userAgent.indexOf("Win") !== -1) return OperatingSystem.Windows;
if (userAgent.indexOf("like Mac") !== -1) return OperatingSystem.iOS;
if (userAgent.indexOf("Mac") !== -1) return OperatingSystem.macOS;
if (userAgent.indexOf("Android") !== -1) return OperatingSystem.Android;
if (userAgent.indexOf("X11") !== -1 || userAgent.indexOf("Linux") !== -1) return OperatingSystem.Linux;
return OperatingSystem.Unsupported;
}
/**
* Dynamically determines the current browser and its version (if applicable).
*/
export function getCurrentProduct(): Product {
const runtime = getCurrentRuntime();
switch (runtime) {
case Runtime.Deno:
return Product.Deno;
case Runtime.Node:
return Product.Node;
case Runtime.Bun:
return Product.Bun;
case Runtime.Workerd:
return Product.Workerd;
case Runtime.Netlify:
return Product.Netlify;
case Runtime.EdgeLight:
return Product.EdgeLight;
case Runtime.Fastly:
return Product.Fastly;
case Runtime.Browser: {
// Brave can not be detected from user agent string, handle separately
if (verifyGlobal("brave") && "brave" in navigator) return Product.Brave;
// For browser, get the specific browser
const userAgent = navigator.userAgent;
return getProductFromUserAgent(userAgent);
}
default:
return Product.Unsupported;
}
}
/**
* Determines the product from a user agent string, if possible
*/
export function getProductFromUserAgent(userAgent: string): Product {
if (userAgent.indexOf("Opera") !== -1 || userAgent.indexOf("OPR") !== -1) return Product.Opera;
if (userAgent.indexOf("Safari") !== -1 && userAgent.indexOf("Chrome") === -1) return Product.Safari;
if (userAgent.indexOf("Edg") !== -1) return Product.Edge;
if (userAgent.indexOf("Chrome") !== -1) return Product.Chrome;
if (userAgent.indexOf("Firefox") !== -1) return Product.Firefox;
return Product.Unsupported;
}
/**
* Dynamically determines the version of the current product/runtime
* @returns {string} A string containing the detected version, or undefined if the product is not supported.
*/
export function getCurrentVersion(): string | undefined {
const product = getCurrentProduct();
switch (product) {
case Product.Deno:
// @ts-ignore Runtime detection
return Deno.version.deno;
case Product.Node:
// @ts-ignore Runtime detection
return process.versions.node;
case Product.Bun:
// @ts-ignore Runtime detection
return process.versions.bun;
default: {
const userAgent = globalThis.navigator?.userAgent;
return getVersionFromUserAgent(userAgent);
}
}
}
/**
* Determines the product version from a user agent string, if possible
*/
export function getVersionFromUserAgent(userAgent: string): string | undefined {
const product = getProductFromUserAgent(userAgent);
switch (product) {
case Product.Chrome:
return getChromeVersion(userAgent);
case Product.Firefox:
return getFirefoxVersion(userAgent);
case Product.Edge:
return getEdgeVersion(userAgent);
case Product.Safari:
return getSafariVersion(userAgent);
case Product.Opera:
return getOperaVersion(userAgent);
case Product.Brave:
return getBraveVersion(userAgent);
default:
return undefined;
}
}
/**
* Attempts to determine the current CPU architecture of the runtime's environment.
*/
export function getCurrentArchitecture(): Architecture {
const runtime = getCurrentRuntime();
switch (runtime) {
case Runtime.Deno:
if (Deno.build.arch === "x86_64") return Architecture.x64;
if (Deno.build.arch === "aarch64") return Architecture.arm64;
if (Deno.build.os === "darwin") return Architecture.x64;
return Architecture.x86;
case Runtime.Bun:
case Runtime.Node:
// @ts-ignore Cross Runtime
switch (process.arch) {
case "arm":
return Architecture.arm;
case "arm64":
return Architecture.arm64;
case "ia32":
return Architecture.x86;
case "x64":
return Architecture.x64;
case "loong64":
case "mips":
case "mipsel":
case "ppc":
case "ppc64":
case "riscv64":
case "s390":
case "s390x":
return Architecture.Unsupported;
}
return Architecture.Unsupported;
case Runtime.Browser: {
const userAgent = navigator.userAgent;
// @ts-ignore Cross Runtime
const platform = navigator.platform;
if (platform.indexOf("Win64") !== -1 || platform.indexOf("x64") !== -1 || platform.indexOf("x86_64") !== -1) return Architecture.x64;
if (platform.indexOf("Win32") !== -1 || (platform.indexOf("x86") !== -1 && platform.indexOf("x86_64") === -1)) return Architecture.x86;
if (userAgent.indexOf("Win64") !== -1 || userAgent.indexOf("x64") !== -1 || userAgent.indexOf("x86_64") !== -1) return Architecture.x64;
if (userAgent.indexOf("Win32") !== -1 || (userAgent.indexOf("x86") !== -1 && userAgent.indexOf("x86_64") === -1)) return Architecture.x86;
if (userAgent.indexOf("arm64") !== -1) return Architecture.arm64;
if (userAgent.indexOf("arm") !== -1) {
return Architecture.arm;
}
// @ts-ignore Cross Runtime
if (platform.indexOf("iPhone") || platform.indexOf("iPad") || (userAgent.indexOf("Mac") !== -1 && "ontouchend" in document)) {
// Likely aarch64 on newer iOS devices and Apple Silicon Macs
return Architecture.arm64;
}
return Architecture.Unsupported;
}
}
return Architecture.Unsupported;
}
/**
* Represents a group of system information gathered by the library.
*/
interface SystemInfo {
runtime: Runtime;
product: Product;
version: string | undefined;
os: OperatingSystem | undefined;
architecture: Architecture | undefined;
}
/**
* Retrieves the current system information.
* @returns {SystemInfo} An object containing the system information.
*/
function getSystemInfoInternal(): SystemInfo {
const systemInfo: SystemInfo = {
runtime: CurrentRuntime,
product: CurrentProduct,
version: CurrentVersion,
os: CurrentOS,
architecture: CurrentArchitecture,
};
return systemInfo;
}
/**
* Logs current system information to the console.
*
* @param {boolean} [useTable=false] - If true, formats the output as a table.
*/
export function dumpSystemInfo(useTable = false): void {
const systemInfo: SystemInfo = getSystemInfoInternal();
if (useTable) {
console.table(systemInfo);
} else {
console.log(JSON.stringify(systemInfo, null, 2));
}
}
/**
* Gets the current system information as a formatted JSON string.
* @returns {string}
*/
export function getSystemInfo(): string {
const systemInfo: SystemInfo = getSystemInfoInternal();
return JSON.stringify(systemInfo);
}
/**
* Static variable containing the current runtime.
*/
export const CurrentRuntime: Runtime = getCurrentRuntime();
/**
* Static variable containing the current product.
*/
export const CurrentProduct: Product = getCurrentProduct();
/**
* Static variable containing the current product/runtime version.
*/
export const CurrentVersion: string | undefined = getCurrentVersion();
/**
* Static variable containing the current operating system.
*/
export const CurrentOS: OperatingSystem | undefined = getCurrentOS();
/**
* Static variable containing the current operating system.
*/
export const CurrentArchitecture: Architecture | undefined = getCurrentArchitecture();