kugelaudio 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -8
- package/dist/index.d.mts +175 -17
- package/dist/index.d.ts +175 -17
- package/dist/index.js +287 -13
- package/dist/index.mjs +294 -13
- package/package.json +5 -1
- package/src/client.ts +354 -17
- package/src/index.ts +6 -2
- package/src/types.ts +83 -12
- package/src/websocket.ts +44 -0
package/src/websocket.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket compatibility layer for browser and Node.js environments.
|
|
3
|
+
*
|
|
4
|
+
* IMPORTANT: WebSocket resolution is lazy to avoid top-level side-effects
|
|
5
|
+
* that break server-side bundlers (Turbopack / Webpack) when this module
|
|
6
|
+
* is imported in a Node.js (API route) context.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
let _cachedWs: typeof WebSocket | null = null;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get the WebSocket constructor for the current environment.
|
|
13
|
+
* Uses native WebSocket in browsers, ws package in Node.js.
|
|
14
|
+
* Result is cached after first call.
|
|
15
|
+
*/
|
|
16
|
+
export function getWebSocket(): typeof WebSocket {
|
|
17
|
+
if (_cachedWs) return _cachedWs;
|
|
18
|
+
|
|
19
|
+
// Browser environment
|
|
20
|
+
if (typeof globalThis !== 'undefined' && typeof (globalThis as any).WebSocket !== 'undefined') {
|
|
21
|
+
_cachedWs = (globalThis as any).WebSocket;
|
|
22
|
+
return _cachedWs!;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Node.js environment - use ws package via dynamic require
|
|
26
|
+
try {
|
|
27
|
+
// Use Function constructor to hide require from static analysis by bundlers
|
|
28
|
+
// eslint-disable-next-line no-new-func
|
|
29
|
+
const _require = typeof require !== 'undefined'
|
|
30
|
+
? require
|
|
31
|
+
: Function('return typeof require !== "undefined" ? require : undefined')();
|
|
32
|
+
if (_require) {
|
|
33
|
+
const ws = _require('ws');
|
|
34
|
+
_cachedWs = ws.default || ws;
|
|
35
|
+
return _cachedWs!;
|
|
36
|
+
}
|
|
37
|
+
} catch {
|
|
38
|
+
// Fall through to error
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
throw new Error(
|
|
42
|
+
'WebSocket not available. In Node.js, install the "ws" package: npm install ws'
|
|
43
|
+
);
|
|
44
|
+
}
|