Skip to content

Commit 685fc19

Browse files
committed
fix stub ws file
1 parent 4c81e4f commit 685fc19

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

packages/ai/src/platform/websocket.ts

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { isBrowser, isNode } from '@firebase/util';
19-
import { AIError } from '../errors';
20-
import { AIErrorCode } from '../types';
2118
import { NodeWebSocketHandler } from './node/websocket';
22-
import { BrowserWebSocketHandler } from './browser/websocket';
2319

2420
/**
2521
* A standardized interface for interacting with a WebSocket connection.
@@ -64,53 +60,27 @@ export interface WebSocketHandler {
6460
}
6561

6662
/**
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.
7165
*
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.
7572
*
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`.
7975
*
8076
* @internal
8177
*/
8278
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+
);
10983
}
11084

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();
11686
}

0 commit comments

Comments
 (0)