@supabase/realtime-js 3.0.0-next.2 → 3.0.0-next.21

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.
Files changed (53) hide show
  1. package/dist/main/RealtimeChannel.d.ts +5 -4
  2. package/dist/main/RealtimeChannel.d.ts.map +1 -1
  3. package/dist/main/RealtimeChannel.js +7 -5
  4. package/dist/main/RealtimeChannel.js.map +1 -1
  5. package/dist/main/RealtimeClient.d.ts +11 -8
  6. package/dist/main/RealtimeClient.d.ts.map +1 -1
  7. package/dist/main/RealtimeClient.js +44 -12
  8. package/dist/main/RealtimeClient.js.map +1 -1
  9. package/dist/main/lib/constants.d.ts +3 -3
  10. package/dist/main/lib/constants.d.ts.map +1 -1
  11. package/dist/main/lib/normalizeChannelError.d.ts +10 -0
  12. package/dist/main/lib/normalizeChannelError.d.ts.map +1 -0
  13. package/dist/main/lib/normalizeChannelError.js +29 -0
  14. package/dist/main/lib/normalizeChannelError.js.map +1 -0
  15. package/dist/main/lib/version.d.ts +1 -1
  16. package/dist/main/lib/version.d.ts.map +1 -1
  17. package/dist/main/lib/version.js +1 -1
  18. package/dist/main/lib/version.js.map +1 -1
  19. package/dist/main/lib/websocket-factory.d.ts +2 -1
  20. package/dist/main/lib/websocket-factory.d.ts.map +1 -1
  21. package/dist/main/lib/websocket-factory.js +12 -10
  22. package/dist/main/lib/websocket-factory.js.map +1 -1
  23. package/dist/module/RealtimeChannel.d.ts +5 -4
  24. package/dist/module/RealtimeChannel.d.ts.map +1 -1
  25. package/dist/module/RealtimeChannel.js +7 -5
  26. package/dist/module/RealtimeChannel.js.map +1 -1
  27. package/dist/module/RealtimeClient.d.ts +11 -8
  28. package/dist/module/RealtimeClient.d.ts.map +1 -1
  29. package/dist/module/RealtimeClient.js +44 -12
  30. package/dist/module/RealtimeClient.js.map +1 -1
  31. package/dist/module/lib/constants.d.ts +3 -3
  32. package/dist/module/lib/constants.d.ts.map +1 -1
  33. package/dist/module/lib/normalizeChannelError.d.ts +10 -0
  34. package/dist/module/lib/normalizeChannelError.d.ts.map +1 -0
  35. package/dist/module/lib/normalizeChannelError.js +26 -0
  36. package/dist/module/lib/normalizeChannelError.js.map +1 -0
  37. package/dist/module/lib/version.d.ts +1 -1
  38. package/dist/module/lib/version.d.ts.map +1 -1
  39. package/dist/module/lib/version.js +1 -1
  40. package/dist/module/lib/version.js.map +1 -1
  41. package/dist/module/lib/websocket-factory.d.ts +2 -1
  42. package/dist/module/lib/websocket-factory.d.ts.map +1 -1
  43. package/dist/module/lib/websocket-factory.js +12 -10
  44. package/dist/module/lib/websocket-factory.js.map +1 -1
  45. package/dist/tsconfig.module.tsbuildinfo +1 -1
  46. package/dist/tsconfig.tsbuildinfo +1 -1
  47. package/package.json +2 -2
  48. package/src/RealtimeChannel.ts +11 -11
  49. package/src/RealtimeClient.ts +60 -13
  50. package/src/lib/constants.ts +1 -1
  51. package/src/lib/normalizeChannelError.ts +30 -0
  52. package/src/lib/version.ts +1 -1
  53. package/src/lib/websocket-factory.ts +28 -12
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Normalize the various shapes a channel error reason can take into a real `Error`.
3
+ *
4
+ * Transport-level channel errors arrive as a `CloseEvent`, a transport `Event`, an `Error`,
5
+ * a string, or `undefined` depending on which path in the underlying socket fired. Server-reply
6
+ * errors arrive as a payload object. This helper produces a consistent `Error` for every case
7
+ * and preserves the original via `cause` so callers can still inspect the raw event.
8
+ */
9
+ export function normalizeChannelError(reason: unknown): Error {
10
+ if (reason instanceof Error) {
11
+ return reason
12
+ }
13
+
14
+ if (typeof reason === 'string') {
15
+ return new Error(reason)
16
+ }
17
+
18
+ if (reason && typeof reason === 'object') {
19
+ const obj = reason as Record<string, unknown>
20
+
21
+ if (typeof obj.code === 'number') {
22
+ const detail = typeof obj.reason === 'string' && obj.reason ? ` (${obj.reason})` : ''
23
+ return new Error(`socket closed: ${obj.code}${detail}`, { cause: reason })
24
+ }
25
+
26
+ return new Error('channel error: transport failure', { cause: reason })
27
+ }
28
+
29
+ return new Error('channel error: connection lost')
30
+ }
@@ -4,4 +4,4 @@
4
4
  // - Debugging and support (identifying which version is running)
5
5
  // - Telemetry and logging (version reporting in errors/analytics)
6
6
  // - Ensuring build artifacts match the published package version
7
- export const version = '3.0.0-next.2'
7
+ export const version = '3.0.0-next.21'
@@ -39,11 +39,22 @@ export interface WebSocketLike {
39
39
 
40
40
  export interface WebSocketEnvironment {
41
41
  type: 'native' | 'ws' | 'cloudflare' | 'unsupported'
42
- constructor?: any
42
+ /** WebSocket constructor for this environment, if available. */
43
+ wsConstructor?: typeof WebSocket
43
44
  error?: string
44
45
  workaround?: string
45
46
  }
46
47
 
48
+ /**
49
+ * Extended globalThis with optional runtime-specific properties
50
+ * that may or may not exist depending on the environment.
51
+ */
52
+ interface RuntimeGlobals {
53
+ WebSocket?: { new (url: string, protocols?: string | string[]): WebSocketLike }
54
+ WebSocketPair?: unknown
55
+ EdgeRuntime?: unknown
56
+ }
57
+
47
58
  /**
48
59
  * Utilities for creating WebSocket instances across runtimes.
49
60
  */
@@ -54,20 +65,23 @@ export class WebSocketFactory {
54
65
  private constructor() {}
55
66
  private static detectEnvironment(): WebSocketEnvironment {
56
67
  if (typeof WebSocket !== 'undefined') {
57
- return { type: 'native', constructor: WebSocket }
68
+ return { type: 'native', wsConstructor: WebSocket }
58
69
  }
59
70
 
60
- if (typeof globalThis !== 'undefined' && typeof (globalThis as any).WebSocket !== 'undefined') {
61
- return { type: 'native', constructor: (globalThis as any).WebSocket }
71
+ const gt = globalThis as typeof globalThis & RuntimeGlobals
72
+ if (typeof globalThis !== 'undefined' && typeof gt.WebSocket !== 'undefined') {
73
+ return { type: 'native', wsConstructor: gt.WebSocket as typeof WebSocket }
62
74
  }
63
75
 
64
- if (typeof global !== 'undefined' && typeof (global as any).WebSocket !== 'undefined') {
65
- return { type: 'native', constructor: (global as any).WebSocket }
76
+ const gl =
77
+ typeof global !== 'undefined' ? (global as typeof global & RuntimeGlobals) : undefined
78
+ if (gl && typeof gl.WebSocket !== 'undefined') {
79
+ return { type: 'native', wsConstructor: gl.WebSocket as typeof WebSocket }
66
80
  }
67
81
 
68
82
  if (
69
83
  typeof globalThis !== 'undefined' &&
70
- typeof (globalThis as any).WebSocketPair !== 'undefined' &&
84
+ typeof gt.WebSocketPair !== 'undefined' &&
71
85
  typeof globalThis.WebSocket === 'undefined'
72
86
  ) {
73
87
  return {
@@ -80,7 +94,7 @@ export class WebSocketFactory {
80
94
  }
81
95
 
82
96
  if (
83
- (typeof globalThis !== 'undefined' && (globalThis as any).EdgeRuntime) ||
97
+ (typeof globalThis !== 'undefined' && gt.EdgeRuntime) ||
84
98
  (typeof navigator !== 'undefined' && navigator.userAgent?.includes('Vercel-Edge'))
85
99
  ) {
86
100
  return {
@@ -93,7 +107,9 @@ export class WebSocketFactory {
93
107
  }
94
108
 
95
109
  // Use dynamic property access to avoid Next.js Edge Runtime static analysis warnings
96
- const _process = (globalThis as any)['process']
110
+ const _process = (globalThis as Record<string, unknown>)['process'] as
111
+ | { versions?: { node?: string } }
112
+ | undefined
97
113
  if (_process) {
98
114
  const processVersions = _process['versions']
99
115
  if (processVersions && processVersions['node']) {
@@ -105,7 +121,7 @@ export class WebSocketFactory {
105
121
  if (nodeVersion >= 22) {
106
122
  // Check if native WebSocket is available (should be in Node.js 22+)
107
123
  if (typeof globalThis.WebSocket !== 'undefined') {
108
- return { type: 'native', constructor: globalThis.WebSocket }
124
+ return { type: 'native', wsConstructor: globalThis.WebSocket }
109
125
  }
110
126
  // If not available, user needs to provide it
111
127
  return {
@@ -152,8 +168,8 @@ export class WebSocketFactory {
152
168
  */
153
169
  public static getWebSocketConstructor(): typeof WebSocket {
154
170
  const env = this.detectEnvironment()
155
- if (env.constructor) {
156
- return env.constructor
171
+ if (env.wsConstructor) {
172
+ return env.wsConstructor
157
173
  }
158
174
  let errorMessage = env.error || 'WebSocket not supported in this environment.'
159
175
  if (env.workaround) {