@@ -13,30 +13,54 @@ export const isRunningInBrowser = () => {
1313 ) ;
1414} ;
1515
16- type DetectedPlatform = 'deno' | 'node ' | 'edge' | 'unknown' ;
16+ type DetectedPlatform = 'deno' | 'bun ' | 'workerd' | ' edge' | 'node ' | 'unknown' ;
1717
1818/**
1919 * Note this does not detect 'browser'; for that, use getBrowserInfo().
2020 */
2121function getDetectedPlatform ( ) : DetectedPlatform {
22+ // Deno
2223 if ( typeof Deno !== 'undefined' && Deno . build != null ) {
2324 return 'deno' ;
2425 }
26+ // Bun (must be checked before Node since Bun provides a Node-compatible process)
27+ if ( typeof Bun !== 'undefined' ) {
28+ return 'bun' ;
29+ }
30+ // Cloudflare Workers / workerd
31+ // Heuristics: WebSocketPair global and/or navigator.userAgent === 'Cloudflare-Workers'
32+ if (
33+ typeof WebSocketPair !== 'undefined' ||
34+ ( typeof navigator !== 'undefined' && navigator && ( navigator as any ) . userAgent === 'Cloudflare-Workers' )
35+ ) {
36+ return 'workerd' ;
37+ }
38+ // Vercel Edge Runtime
2539 if ( typeof EdgeRuntime !== 'undefined' ) {
2640 return 'edge' ;
2741 }
42+ // Node.js
2843 if (
2944 Object . prototype . toString . call (
3045 typeof ( globalThis as any ) . process !== 'undefined' ? ( globalThis as any ) . process : 0 ,
3146 ) === '[object process]'
3247 ) {
3348 return 'node' ;
3449 }
50+ // Fallback Node.js heuristic for environments where toString check fails (e.g., some test runners)
51+ if ( typeof ( globalThis as any ) . process !== 'undefined' ) {
52+ const p = ( globalThis as any ) . process ;
53+ if ( ( p ?. versions && typeof p . versions . node === 'string' ) || p ?. release ?. name === 'node' ) {
54+ return 'node' ;
55+ }
56+ }
3557 return 'unknown' ;
3658}
3759
3860declare const Deno : any ;
3961declare const EdgeRuntime : any ;
62+ declare const Bun : any ;
63+ declare const WebSocketPair : any ;
4064type Arch = 'x32' | 'x64' | 'arm' | 'arm64' | `other:${string } ` | 'unknown' ;
4165type PlatformName =
4266 | 'MacOS'
@@ -54,7 +78,7 @@ type PlatformProperties = {
5478 'X-Stainless-Package-Version' : string ;
5579 'X-Stainless-OS' : PlatformName ;
5680 'X-Stainless-Arch' : Arch ;
57- 'X-Stainless-Runtime' : 'node' | 'deno' | 'edge' | `browser:${Browser } ` | 'unknown' ;
81+ 'X-Stainless-Runtime' : 'node' | 'deno' | 'bun' | 'workerd' | ' edge' | `browser:${Browser } ` | 'unknown' ;
5882 'X-Stainless-Runtime-Version' : string ;
5983} ;
6084const getPlatformProperties = ( ) : PlatformProperties => {
@@ -70,6 +94,28 @@ const getPlatformProperties = (): PlatformProperties => {
7094 typeof Deno . version === 'string' ? Deno . version : Deno . version ?. deno ?? 'unknown' ,
7195 } ;
7296 }
97+ if ( detectedPlatform === 'bun' ) {
98+ return {
99+ 'X-Stainless-Lang' : 'js' ,
100+ 'X-Stainless-Package-Version' : VERSION ,
101+ 'X-Stainless-OS' : normalizePlatform ( ( globalThis as any ) . process ?. platform ?? 'unknown' ) ,
102+ 'X-Stainless-Arch' : normalizeArch ( ( globalThis as any ) . process ?. arch ?? 'unknown' ) ,
103+ 'X-Stainless-Runtime' : 'bun' ,
104+ 'X-Stainless-Runtime-Version' : ( globalThis as any ) . Bun ?. version ??
105+ ( globalThis as any ) . process ?. versions ?. bun ?? 'unknown' ,
106+ } ;
107+ }
108+ if ( detectedPlatform === 'workerd' ) {
109+ return {
110+ 'X-Stainless-Lang' : 'js' ,
111+ 'X-Stainless-Package-Version' : VERSION ,
112+ 'X-Stainless-OS' : 'Unknown' ,
113+ 'X-Stainless-Arch' : 'unknown' ,
114+ 'X-Stainless-Runtime' : 'workerd' ,
115+ 'X-Stainless-Runtime-Version' :
116+ ( typeof navigator !== 'undefined' && ( navigator as any ) ?. userAgent ) || 'unknown' ,
117+ } ;
118+ }
73119 if ( typeof EdgeRuntime !== 'undefined' ) {
74120 return {
75121 'X-Stainless-Lang' : 'js' ,
0 commit comments