@@ -83,25 +83,46 @@ export interface WebSocketHandler {
83
83
* @internal
84
84
*/
85
85
export function createWebSocketHandler ( ) : WebSocketHandler {
86
- // `isNode()` is replaced with a static boolean during build time to enable tree shaking
86
+ // `isNode()` is replaced with a static boolean during build time so this block will be
87
+ // tree-shaken in browser builds.
87
88
if ( isNode ( ) ) {
88
- const [ major ] = process . versions . node . split ( '.' ) . map ( Number ) ;
89
- if ( major < 22 ) {
89
+ // At this point we're certain we're in a Node bundle, but we still need to have checks
90
+ // to be certain we're in a Node environment, and not something like Deno, Bun, or Edge workers.
91
+ if ( typeof process === 'object' && process . versions ?. node ) {
92
+ const [ major ] = process . versions . node . split ( '.' ) . map ( Number ) ;
93
+ if ( major < 22 ) {
94
+ throw new AIError (
95
+ AIErrorCode . UNSUPPORTED ,
96
+ `The "Live" feature is being used in a Node environment, but the ` +
97
+ `runtime version is ${ process . versions . node } . This feature requires Node.js ` +
98
+ `version 22 or higher for native WebSocket support.`
99
+ ) ;
100
+ }
101
+ return new NodeWebSocketHandler ( ) ;
102
+ }
103
+ }
104
+
105
+ // `isBrowser()` is replaced with a static boolean during build time so this block will be
106
+ // tree-shaken in Node builds.
107
+ if ( isBrowser ( ) ) {
108
+ // At this point we're certain we're in a browser build, but we still need to check for the
109
+ // existence of the `WebSocket` API. This check would fail in environments that use a browser
110
+ // bundle, but don't support WebSockets (Web workers and SSR).
111
+ if ( typeof WebSocket !== 'undefined' ) {
112
+ return new BrowserWebSocketHandler ( ) ;
113
+ } else {
90
114
throw new AIError (
91
115
AIErrorCode . UNSUPPORTED ,
92
- 'The Live feature requires Node version 22 or higher for native WebSocket support.'
116
+ 'The WebSocket API is not available in this browser-like environment. ' +
117
+ 'The Firebase AI "Live" feature is not supported here. It is supported in ' +
118
+ 'standard browser windows, Web Workers with WebSocket support, and Node >= 22.'
93
119
) ;
94
120
}
95
- return new NodeWebSocketHandler ( ) ;
96
- }
97
-
98
- // `isBrowser()` is replaced with a static boolean during build time to enable tree shaking
99
- if ( isBrowser ( ) && window . WebSocket ) {
100
- return new BrowserWebSocketHandler ( ) ;
101
121
}
102
122
103
123
throw new AIError (
104
124
AIErrorCode . UNSUPPORTED ,
105
- 'A WebSocket API is not available in this environment.'
125
+ 'This environment is not supported by the "Live" feature. ' +
126
+ 'Supported environments are modern web browsers and Node >= 22.'
106
127
) ;
107
128
}
0 commit comments