@depup/supabase__realtime-js 2.99.3-depup.0

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 (104) hide show
  1. package/README.md +32 -0
  2. package/changes.json +14 -0
  3. package/dist/main/RealtimeChannel.d.ts +370 -0
  4. package/dist/main/RealtimeChannel.d.ts.map +1 -0
  5. package/dist/main/RealtimeChannel.js +644 -0
  6. package/dist/main/RealtimeChannel.js.map +1 -0
  7. package/dist/main/RealtimeClient.d.ts +238 -0
  8. package/dist/main/RealtimeClient.d.ts.map +1 -0
  9. package/dist/main/RealtimeClient.js +870 -0
  10. package/dist/main/RealtimeClient.js.map +1 -0
  11. package/dist/main/RealtimePresence.d.ts +76 -0
  12. package/dist/main/RealtimePresence.d.ts.map +1 -0
  13. package/dist/main/RealtimePresence.js +237 -0
  14. package/dist/main/RealtimePresence.js.map +1 -0
  15. package/dist/main/index.d.ts +6 -0
  16. package/dist/main/index.d.ts.map +1 -0
  17. package/dist/main/index.js +18 -0
  18. package/dist/main/index.js.map +1 -0
  19. package/dist/main/lib/constants.d.ts +39 -0
  20. package/dist/main/lib/constants.d.ts.map +1 -0
  21. package/dist/main/lib/constants.js +48 -0
  22. package/dist/main/lib/constants.js.map +1 -0
  23. package/dist/main/lib/push.d.ts +48 -0
  24. package/dist/main/lib/push.d.ts.map +1 -0
  25. package/dist/main/lib/push.js +102 -0
  26. package/dist/main/lib/push.js.map +1 -0
  27. package/dist/main/lib/serializer.d.ts +33 -0
  28. package/dist/main/lib/serializer.d.ts.map +1 -0
  29. package/dist/main/lib/serializer.js +155 -0
  30. package/dist/main/lib/serializer.js.map +1 -0
  31. package/dist/main/lib/timer.d.ts +22 -0
  32. package/dist/main/lib/timer.d.ts.map +1 -0
  33. package/dist/main/lib/timer.js +39 -0
  34. package/dist/main/lib/timer.js.map +1 -0
  35. package/dist/main/lib/transformers.d.ts +109 -0
  36. package/dist/main/lib/transformers.d.ts.map +1 -0
  37. package/dist/main/lib/transformers.js +241 -0
  38. package/dist/main/lib/transformers.js.map +1 -0
  39. package/dist/main/lib/version.d.ts +2 -0
  40. package/dist/main/lib/version.d.ts.map +1 -0
  41. package/dist/main/lib/version.js +11 -0
  42. package/dist/main/lib/version.js.map +1 -0
  43. package/dist/main/lib/websocket-factory.d.ts +81 -0
  44. package/dist/main/lib/websocket-factory.d.ts.map +1 -0
  45. package/dist/main/lib/websocket-factory.js +131 -0
  46. package/dist/main/lib/websocket-factory.js.map +1 -0
  47. package/dist/module/RealtimeChannel.d.ts +370 -0
  48. package/dist/module/RealtimeChannel.d.ts.map +1 -0
  49. package/dist/module/RealtimeChannel.js +639 -0
  50. package/dist/module/RealtimeChannel.js.map +1 -0
  51. package/dist/module/RealtimeClient.d.ts +238 -0
  52. package/dist/module/RealtimeClient.d.ts.map +1 -0
  53. package/dist/module/RealtimeClient.js +866 -0
  54. package/dist/module/RealtimeClient.js.map +1 -0
  55. package/dist/module/RealtimePresence.d.ts +76 -0
  56. package/dist/module/RealtimePresence.d.ts.map +1 -0
  57. package/dist/module/RealtimePresence.js +233 -0
  58. package/dist/module/RealtimePresence.js.map +1 -0
  59. package/dist/module/index.d.ts +6 -0
  60. package/dist/module/index.d.ts.map +1 -0
  61. package/dist/module/index.js +6 -0
  62. package/dist/module/index.js.map +1 -0
  63. package/dist/module/lib/constants.d.ts +39 -0
  64. package/dist/module/lib/constants.d.ts.map +1 -0
  65. package/dist/module/lib/constants.js +45 -0
  66. package/dist/module/lib/constants.js.map +1 -0
  67. package/dist/module/lib/push.d.ts +48 -0
  68. package/dist/module/lib/push.d.ts.map +1 -0
  69. package/dist/module/lib/push.js +99 -0
  70. package/dist/module/lib/push.js.map +1 -0
  71. package/dist/module/lib/serializer.d.ts +33 -0
  72. package/dist/module/lib/serializer.d.ts.map +1 -0
  73. package/dist/module/lib/serializer.js +152 -0
  74. package/dist/module/lib/serializer.js.map +1 -0
  75. package/dist/module/lib/timer.d.ts +22 -0
  76. package/dist/module/lib/timer.d.ts.map +1 -0
  77. package/dist/module/lib/timer.js +36 -0
  78. package/dist/module/lib/timer.js.map +1 -0
  79. package/dist/module/lib/transformers.d.ts +109 -0
  80. package/dist/module/lib/transformers.d.ts.map +1 -0
  81. package/dist/module/lib/transformers.js +229 -0
  82. package/dist/module/lib/transformers.js.map +1 -0
  83. package/dist/module/lib/version.d.ts +2 -0
  84. package/dist/module/lib/version.d.ts.map +1 -0
  85. package/dist/module/lib/version.js +8 -0
  86. package/dist/module/lib/version.js.map +1 -0
  87. package/dist/module/lib/websocket-factory.d.ts +81 -0
  88. package/dist/module/lib/websocket-factory.d.ts.map +1 -0
  89. package/dist/module/lib/websocket-factory.js +127 -0
  90. package/dist/module/lib/websocket-factory.js.map +1 -0
  91. package/dist/tsconfig.module.tsbuildinfo +1 -0
  92. package/dist/tsconfig.tsbuildinfo +1 -0
  93. package/package.json +85 -0
  94. package/src/RealtimeChannel.ts +1039 -0
  95. package/src/RealtimeClient.ts +1023 -0
  96. package/src/RealtimePresence.ts +346 -0
  97. package/src/index.ts +53 -0
  98. package/src/lib/constants.ts +49 -0
  99. package/src/lib/push.ts +121 -0
  100. package/src/lib/serializer.ts +203 -0
  101. package/src/lib/timer.ts +43 -0
  102. package/src/lib/transformers.ts +270 -0
  103. package/src/lib/version.ts +7 -0
  104. package/src/lib/websocket-factory.ts +192 -0
@@ -0,0 +1,192 @@
1
+ export interface WebSocketLike {
2
+ readonly CONNECTING: number
3
+ readonly OPEN: number
4
+ readonly CLOSING: number
5
+ readonly CLOSED: number
6
+ readonly readyState: number
7
+ readonly url: string
8
+ readonly protocol: string
9
+
10
+ /**
11
+ * Closes the socket, optionally providing a close code and reason.
12
+ */
13
+ close(code?: number, reason?: string): void
14
+ /**
15
+ * Sends data through the socket using the underlying implementation.
16
+ */
17
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void
18
+
19
+ onopen: ((this: any, ev: Event) => any) | null
20
+ onmessage: ((this: any, ev: MessageEvent) => any) | null
21
+ onclose: ((this: any, ev: CloseEvent) => any) | null
22
+ onerror: ((this: any, ev: Event) => any) | null
23
+
24
+ /**
25
+ * Registers an event listener on the socket (compatible with browser WebSocket API).
26
+ */
27
+ addEventListener(type: string, listener: EventListener): void
28
+ /**
29
+ * Removes a previously registered event listener.
30
+ */
31
+ removeEventListener(type: string, listener: EventListener): void
32
+
33
+ // Add additional properties that may exist on WebSocket implementations
34
+ binaryType?: string
35
+ bufferedAmount?: number
36
+ extensions?: string
37
+ dispatchEvent?: (event: Event) => boolean
38
+ }
39
+
40
+ export interface WebSocketEnvironment {
41
+ type: 'native' | 'ws' | 'cloudflare' | 'unsupported'
42
+ constructor?: any
43
+ error?: string
44
+ workaround?: string
45
+ }
46
+
47
+ /**
48
+ * Utilities for creating WebSocket instances across runtimes.
49
+ */
50
+ export class WebSocketFactory {
51
+ /**
52
+ * Static-only utility – prevent instantiation.
53
+ */
54
+ private constructor() {}
55
+ private static detectEnvironment(): WebSocketEnvironment {
56
+ if (typeof WebSocket !== 'undefined') {
57
+ return { type: 'native', constructor: WebSocket }
58
+ }
59
+
60
+ if (typeof globalThis !== 'undefined' && typeof (globalThis as any).WebSocket !== 'undefined') {
61
+ return { type: 'native', constructor: (globalThis as any).WebSocket }
62
+ }
63
+
64
+ if (typeof global !== 'undefined' && typeof (global as any).WebSocket !== 'undefined') {
65
+ return { type: 'native', constructor: (global as any).WebSocket }
66
+ }
67
+
68
+ if (
69
+ typeof globalThis !== 'undefined' &&
70
+ typeof (globalThis as any).WebSocketPair !== 'undefined' &&
71
+ typeof globalThis.WebSocket === 'undefined'
72
+ ) {
73
+ return {
74
+ type: 'cloudflare',
75
+ error:
76
+ 'Cloudflare Workers detected. WebSocket clients are not supported in Cloudflare Workers.',
77
+ workaround:
78
+ 'Use Cloudflare Workers WebSocket API for server-side WebSocket handling, or deploy to a different runtime.',
79
+ }
80
+ }
81
+
82
+ if (
83
+ (typeof globalThis !== 'undefined' && (globalThis as any).EdgeRuntime) ||
84
+ (typeof navigator !== 'undefined' && navigator.userAgent?.includes('Vercel-Edge'))
85
+ ) {
86
+ return {
87
+ type: 'unsupported',
88
+ error:
89
+ 'Edge runtime detected (Vercel Edge/Netlify Edge). WebSockets are not supported in edge functions.',
90
+ workaround:
91
+ 'Use serverless functions or a different deployment target for WebSocket functionality.',
92
+ }
93
+ }
94
+
95
+ // Use dynamic property access to avoid Next.js Edge Runtime static analysis warnings
96
+ const _process = (globalThis as any)['process']
97
+ if (_process) {
98
+ const processVersions = _process['versions']
99
+ if (processVersions && processVersions['node']) {
100
+ // Remove 'v' prefix if present and parse the major version
101
+ const versionString = processVersions['node']
102
+ const nodeVersion = parseInt(versionString.replace(/^v/, '').split('.')[0])
103
+
104
+ // Node.js 22+ should have native WebSocket
105
+ if (nodeVersion >= 22) {
106
+ // Check if native WebSocket is available (should be in Node.js 22+)
107
+ if (typeof globalThis.WebSocket !== 'undefined') {
108
+ return { type: 'native', constructor: globalThis.WebSocket }
109
+ }
110
+ // If not available, user needs to provide it
111
+ return {
112
+ type: 'unsupported',
113
+ error: `Node.js ${nodeVersion} detected but native WebSocket not found.`,
114
+ workaround: 'Provide a WebSocket implementation via the transport option.',
115
+ }
116
+ }
117
+
118
+ // Node.js < 22 doesn't have native WebSocket
119
+ return {
120
+ type: 'unsupported',
121
+ error: `Node.js ${nodeVersion} detected without native WebSocket support.`,
122
+ workaround:
123
+ 'For Node.js < 22, install "ws" package and provide it via the transport option:\n' +
124
+ 'import ws from "ws"\n' +
125
+ 'new RealtimeClient(url, { transport: ws })',
126
+ }
127
+ }
128
+ }
129
+
130
+ return {
131
+ type: 'unsupported',
132
+ error: 'Unknown JavaScript runtime without WebSocket support.',
133
+ workaround:
134
+ "Ensure you're running in a supported environment (browser, Node.js, Deno) or provide a custom WebSocket implementation.",
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Returns the best available WebSocket constructor for the current runtime.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * const WS = WebSocketFactory.getWebSocketConstructor()
144
+ * const socket = new WS('wss://realtime.supabase.co/socket')
145
+ * ```
146
+ */
147
+ public static getWebSocketConstructor(): typeof WebSocket {
148
+ const env = this.detectEnvironment()
149
+ if (env.constructor) {
150
+ return env.constructor
151
+ }
152
+ let errorMessage = env.error || 'WebSocket not supported in this environment.'
153
+ if (env.workaround) {
154
+ errorMessage += `\n\nSuggested solution: ${env.workaround}`
155
+ }
156
+ throw new Error(errorMessage)
157
+ }
158
+
159
+ /**
160
+ * Creates a WebSocket using the detected constructor.
161
+ *
162
+ * @example
163
+ * ```ts
164
+ * const socket = WebSocketFactory.createWebSocket('wss://realtime.supabase.co/socket')
165
+ * ```
166
+ */
167
+ public static createWebSocket(url: string | URL, protocols?: string | string[]): WebSocketLike {
168
+ const WS = this.getWebSocketConstructor()
169
+ return new WS(url, protocols)
170
+ }
171
+
172
+ /**
173
+ * Detects whether the runtime can establish WebSocket connections.
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * if (!WebSocketFactory.isWebSocketSupported()) {
178
+ * console.warn('Falling back to long polling')
179
+ * }
180
+ * ```
181
+ */
182
+ public static isWebSocketSupported(): boolean {
183
+ try {
184
+ const env = this.detectEnvironment()
185
+ return env.type === 'native' || env.type === 'ws'
186
+ } catch {
187
+ return false
188
+ }
189
+ }
190
+ }
191
+
192
+ export default WebSocketFactory