|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { isBrowser, isNode } from '@firebase/util'; |
19 |
| -import { AIError } from '../errors'; |
20 |
| -import { AIErrorCode } from '../types'; |
21 | 18 | import { NodeWebSocketHandler } from './node/websocket';
|
22 |
| -import { BrowserWebSocketHandler } from './browser/websocket'; |
23 | 19 |
|
24 | 20 | /**
|
25 | 21 | * A standardized interface for interacting with a WebSocket connection.
|
@@ -64,53 +60,27 @@ export interface WebSocketHandler {
|
64 | 60 | }
|
65 | 61 |
|
66 | 62 | /**
|
67 |
| - * Factory function to create the appropriate WebSocketHandler for the current environment. |
68 |
| - * |
69 |
| - * Even though the browser and Node >=22 WebSocket APIs are now very similar, |
70 |
| - * we use two separate handler classes. There are two reasons for this: |
| 63 | + * NOTE: Imports to this these APIs are renamed to either `platform/browser/websocket.ts` or |
| 64 | + * `platform/node/websocket.ts` during build time. |
71 | 65 | *
|
72 |
| - * 1. Module Loading: The primary difference is how the `WebSocket` class is |
73 |
| - * accessed. In browsers, it's a global (`window.WebSocket`). In Node, it |
74 |
| - * must be imported from the built-in `'ws'` module. |
| 66 | + * The types are still useful for type-checking during development. |
| 67 | + * These are only used during the Node tests, which are ran against non-bundled code. |
| 68 | + */ |
| 69 | + |
| 70 | +/** |
| 71 | + * Factory function to create the appropriate WebSocketHandler for the current environment. |
75 | 72 | *
|
76 |
| - * 2. Type Safety: TypeScript's type definitions for the browser's WebSocket |
77 |
| - * (from `lib.dom.d.ts`) and Node's WebSocket (from `@types/node`) are |
78 |
| - * distinct. Using separate classes ensures type correctness for each environment. |
| 73 | + * This is only a stub for tests. See the real definitions in `./browser/websocket.ts` and |
| 74 | + * `./node/websocket.ts`. |
79 | 75 | *
|
80 | 76 | * @internal
|
81 | 77 | */
|
82 | 78 | export function createWebSocketHandler(): WebSocketHandler {
|
83 |
| - if (isNode()) { |
84 |
| - if (typeof process === 'object' && process.versions?.node) { |
85 |
| - const [major] = process.versions.node.split('.').map(Number); |
86 |
| - if (major < 22) { |
87 |
| - throw new AIError( |
88 |
| - AIErrorCode.UNSUPPORTED, |
89 |
| - `The "Live" feature is being used in a Node environment, but the ` + |
90 |
| - `runtime version is ${process.versions.node}. This feature requires Node.js ` + |
91 |
| - `version 22 or higher for native WebSocket support.` |
92 |
| - ); |
93 |
| - } |
94 |
| - return new NodeWebSocketHandler(); |
95 |
| - } |
96 |
| - } |
97 |
| - |
98 |
| - if (isBrowser()) { |
99 |
| - if (typeof WebSocket !== 'undefined') { |
100 |
| - return new BrowserWebSocketHandler(); |
101 |
| - } else { |
102 |
| - throw new AIError( |
103 |
| - AIErrorCode.UNSUPPORTED, |
104 |
| - 'The WebSocket API is not available in this browser-like environment. ' + |
105 |
| - 'The Firebase AI "Live" feature is not supported here. It is supported in ' + |
106 |
| - 'standard browser windows, Web Workers with WebSocket support, and Node >= 22.' |
107 |
| - ); |
108 |
| - } |
| 79 | + if (typeof WebSocket === 'undefined') { |
| 80 | + throw Error( |
| 81 | + 'WebSocket API is not available. Make sure tests are being ran in Node >= 22.' |
| 82 | + ); |
109 | 83 | }
|
110 | 84 |
|
111 |
| - throw new AIError( |
112 |
| - AIErrorCode.UNSUPPORTED, |
113 |
| - 'This environment is not supported by the "Live" feature. ' + |
114 |
| - 'Supported environments are modern web browsers and Node >= 22.' |
115 |
| - ); |
| 85 | + return new NodeWebSocketHandler(); |
116 | 86 | }
|
0 commit comments