Skip to content

Commit 1b5d59d

Browse files
committed
add new runtime-info utility
1 parent 6dc218b commit 1b5d59d

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/common/utils/runtime-info.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { detectRuntime } from './env';
2+
3+
export interface RuntimeInfo {
4+
name: string;
5+
version?: string;
6+
}
7+
8+
/**
9+
* Get runtime information including name and version.
10+
* Safely extracts version information for different JavaScript runtimes.
11+
* @returns RuntimeInfo object with name and optional version
12+
*/
13+
export function getRuntimeInfo(): RuntimeInfo {
14+
const name = detectRuntime();
15+
let version: string | undefined;
16+
17+
try {
18+
switch (name) {
19+
case 'node':
20+
// process.version includes 'v' prefix (e.g., "v20.5.0")
21+
version = typeof process !== 'undefined' ? process.version : undefined;
22+
break;
23+
24+
case 'deno':
25+
// Deno.version.deno returns just version number (e.g., "1.36.4")
26+
version = (globalThis as any).Deno?.version?.deno;
27+
break;
28+
29+
case 'bun':
30+
version = globalThis.Bun?.version || extractBunVersionFromUserAgent();
31+
break;
32+
33+
// These environments typically don't expose version info
34+
case 'cloudflare':
35+
case 'fastly':
36+
case 'edge-light':
37+
case 'other':
38+
default:
39+
version = undefined;
40+
break;
41+
}
42+
} catch {
43+
version = undefined;
44+
}
45+
46+
return {
47+
name,
48+
version,
49+
};
50+
}
51+
52+
/**
53+
* Extract Bun version from navigator.userAgent as fallback.
54+
* @returns Bun version string or undefined
55+
*/
56+
function extractBunVersionFromUserAgent(): string | undefined {
57+
try {
58+
if (typeof navigator !== 'undefined' && navigator.userAgent) {
59+
const match = navigator.userAgent.match(/Bun\/(\d+\.\d+\.\d+)/);
60+
return match?.[1];
61+
}
62+
} catch {
63+
// Ignore errors
64+
}
65+
return undefined;
66+
}

0 commit comments

Comments
 (0)