@supabase/realtime-js 2.11.4-next.1 → 2.11.5
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/dist/main/RealtimeChannel.d.ts +9 -9
- package/dist/main/RealtimeChannel.d.ts.map +1 -1
- package/dist/main/RealtimeChannel.js +20 -10
- package/dist/main/RealtimeChannel.js.map +1 -1
- package/dist/main/RealtimeClient.d.ts +29 -25
- package/dist/main/RealtimeClient.d.ts.map +1 -1
- package/dist/main/RealtimeClient.js +41 -40
- package/dist/main/RealtimeClient.js.map +1 -1
- package/dist/main/RealtimePresence.d.ts +6 -6
- package/dist/main/RealtimePresence.d.ts.map +1 -1
- package/dist/main/RealtimePresence.js +1 -1
- package/dist/main/RealtimePresence.js.map +1 -1
- package/dist/main/index.js +17 -7
- package/dist/main/index.js.map +1 -1
- package/dist/main/lib/constants.d.ts +6 -0
- package/dist/main/lib/constants.d.ts.map +1 -1
- package/dist/main/lib/constants.js +13 -6
- package/dist/main/lib/constants.js.map +1 -1
- package/dist/main/lib/push.js.map +1 -1
- package/dist/main/lib/serializer.js.map +1 -1
- package/dist/main/lib/transformers.d.ts +4 -4
- package/dist/main/lib/transformers.d.ts.map +1 -1
- package/dist/main/lib/transformers.js +1 -1
- package/dist/main/lib/transformers.js.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/module/RealtimeChannel.d.ts +9 -9
- package/dist/module/RealtimeChannel.d.ts.map +1 -1
- package/dist/module/RealtimeChannel.js.map +1 -1
- package/dist/module/RealtimeClient.d.ts +29 -25
- package/dist/module/RealtimeClient.d.ts.map +1 -1
- package/dist/module/RealtimeClient.js +23 -32
- package/dist/module/RealtimeClient.js.map +1 -1
- package/dist/module/RealtimePresence.d.ts +6 -6
- package/dist/module/RealtimePresence.d.ts.map +1 -1
- package/dist/module/RealtimePresence.js.map +1 -1
- package/dist/module/lib/constants.d.ts +6 -0
- package/dist/module/lib/constants.d.ts.map +1 -1
- package/dist/module/lib/constants.js +7 -0
- package/dist/module/lib/constants.js.map +1 -1
- package/dist/module/lib/push.js.map +1 -1
- package/dist/module/lib/serializer.js.map +1 -1
- package/dist/module/lib/transformers.d.ts +4 -4
- package/dist/module/lib/transformers.d.ts.map +1 -1
- package/dist/module/lib/transformers.js.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/package.json +7 -7
- package/src/RealtimeClient.ts +53 -57
- package/src/lib/constants.ts +8 -0
- package/src/lib/version.ts +1 -1
package/src/RealtimeClient.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { WebSocket as WSWebSocket } from 'ws'
|
|
2
|
+
|
|
1
3
|
import {
|
|
2
4
|
CHANNEL_EVENTS,
|
|
3
5
|
CONNECTION_STATE,
|
|
@@ -7,7 +9,9 @@ import {
|
|
|
7
9
|
TRANSPORTS,
|
|
8
10
|
VSN,
|
|
9
11
|
WS_CLOSE_NORMAL,
|
|
12
|
+
LOG_LEVEL,
|
|
10
13
|
} from './lib/constants'
|
|
14
|
+
|
|
11
15
|
import Serializer from './lib/serializer'
|
|
12
16
|
import Timer from './lib/timer'
|
|
13
17
|
|
|
@@ -23,23 +27,7 @@ export type Channel = {
|
|
|
23
27
|
updated_at: string
|
|
24
28
|
id: number
|
|
25
29
|
}
|
|
26
|
-
|
|
27
|
-
export type RealtimeClientOptions = {
|
|
28
|
-
transport?: WebSocketLikeConstructor
|
|
29
|
-
timeout?: number
|
|
30
|
-
heartbeatIntervalMs?: number
|
|
31
|
-
logger?: Function
|
|
32
|
-
encode?: Function
|
|
33
|
-
decode?: Function
|
|
34
|
-
reconnectAfterMs?: Function
|
|
35
|
-
headers?: { [key: string]: string }
|
|
36
|
-
params?: { [key: string]: any }
|
|
37
|
-
log_level?: 'info' | 'debug' | 'warn' | 'error'
|
|
38
|
-
fetch?: Fetch
|
|
39
|
-
worker?: boolean
|
|
40
|
-
workerUrl?: string
|
|
41
|
-
accessToken?: () => Promise<string | null>
|
|
42
|
-
}
|
|
30
|
+
export type LogLevel = LOG_LEVEL
|
|
43
31
|
|
|
44
32
|
export type RealtimeMessage = {
|
|
45
33
|
topic: string
|
|
@@ -60,13 +48,8 @@ export interface WebSocketLikeConstructor {
|
|
|
60
48
|
options?: { headers: Object | undefined }
|
|
61
49
|
): WebSocketLike
|
|
62
50
|
}
|
|
63
|
-
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'
|
|
64
|
-
|
|
65
|
-
const WSWebSocket = NATIVE_WEBSOCKET_AVAILABLE
|
|
66
|
-
? WebSocket
|
|
67
|
-
: require('ws').WebSocket
|
|
68
51
|
|
|
69
|
-
export type WebSocketLike = WebSocket |
|
|
52
|
+
export type WebSocketLike = WebSocket | WSWebSocket | WSWebSocketDummy
|
|
70
53
|
|
|
71
54
|
export interface WebSocketLikeError {
|
|
72
55
|
error: any
|
|
@@ -74,6 +57,26 @@ export interface WebSocketLikeError {
|
|
|
74
57
|
type: string
|
|
75
58
|
}
|
|
76
59
|
|
|
60
|
+
export type RealtimeClientOptions = {
|
|
61
|
+
transport?: WebSocketLikeConstructor
|
|
62
|
+
timeout?: number
|
|
63
|
+
heartbeatIntervalMs?: number
|
|
64
|
+
logger?: Function
|
|
65
|
+
encode?: Function
|
|
66
|
+
decode?: Function
|
|
67
|
+
reconnectAfterMs?: Function
|
|
68
|
+
headers?: { [key: string]: string }
|
|
69
|
+
params?: { [key: string]: any }
|
|
70
|
+
//Deprecated: Use it in favour of correct casing `logLevel`
|
|
71
|
+
log_level?: LogLevel
|
|
72
|
+
logLevel?: LogLevel
|
|
73
|
+
fetch?: Fetch
|
|
74
|
+
worker?: boolean
|
|
75
|
+
workerUrl?: string
|
|
76
|
+
accessToken?: () => Promise<string | null>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'
|
|
77
80
|
const WORKER_SCRIPT = `
|
|
78
81
|
addEventListener("message", (e) => {
|
|
79
82
|
if (e.data.event === "start") {
|
|
@@ -90,12 +93,13 @@ export default class RealtimeClient {
|
|
|
90
93
|
params?: { [key: string]: string } = {}
|
|
91
94
|
timeout: number = DEFAULT_TIMEOUT
|
|
92
95
|
transport: WebSocketLikeConstructor | null
|
|
93
|
-
heartbeatIntervalMs: number =
|
|
96
|
+
heartbeatIntervalMs: number = 25000
|
|
94
97
|
heartbeatTimer: ReturnType<typeof setInterval> | undefined = undefined
|
|
95
98
|
pendingHeartbeatRef: string | null = null
|
|
96
99
|
ref: number = 0
|
|
97
100
|
reconnectTimer: Timer
|
|
98
101
|
logger: Function = noop
|
|
102
|
+
logLevel?: LogLevel
|
|
99
103
|
encode: Function
|
|
100
104
|
decode: Function
|
|
101
105
|
reconnectAfterMs: Function
|
|
@@ -130,6 +134,7 @@ export default class RealtimeClient {
|
|
|
130
134
|
* @param options.headers The optional headers to pass when connecting.
|
|
131
135
|
* @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
|
|
132
136
|
* @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
|
|
137
|
+
* @param options.logLevel Sets the log level for Realtime
|
|
133
138
|
* @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
|
|
134
139
|
* @param options.decode The function to decode incoming messages. Defaults to Serializer's decode.
|
|
135
140
|
* @param options.reconnectAfterMs he optional function that returns the millsec reconnect interval. Defaults to stepped backoff off.
|
|
@@ -148,6 +153,11 @@ export default class RealtimeClient {
|
|
|
148
153
|
if (options?.headers) this.headers = { ...this.headers, ...options.headers }
|
|
149
154
|
if (options?.timeout) this.timeout = options.timeout
|
|
150
155
|
if (options?.logger) this.logger = options.logger
|
|
156
|
+
if (options?.logLevel || options?.log_level) {
|
|
157
|
+
this.logLevel = options.logLevel || options.log_level
|
|
158
|
+
this.params = { ...this.params, log_level: this.logLevel as string }
|
|
159
|
+
}
|
|
160
|
+
|
|
151
161
|
if (options?.heartbeatIntervalMs)
|
|
152
162
|
this.heartbeatIntervalMs = options.heartbeatIntervalMs
|
|
153
163
|
|
|
@@ -206,20 +216,20 @@ export default class RealtimeClient {
|
|
|
206
216
|
this.conn = new WebSocket(this.endpointURL())
|
|
207
217
|
this.setupConnection()
|
|
208
218
|
return
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
|
|
222
|
+
close: () => {
|
|
223
|
+
this.conn = null
|
|
224
|
+
},
|
|
225
|
+
})
|
|
215
226
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
})
|
|
220
|
-
this.setupConnection()
|
|
227
|
+
import('ws').then(({ default: WS }) => {
|
|
228
|
+
this.conn = new WS(this.endpointURL(), undefined, {
|
|
229
|
+
headers: this.headers,
|
|
221
230
|
})
|
|
222
|
-
|
|
231
|
+
this.setupConnection()
|
|
232
|
+
})
|
|
223
233
|
}
|
|
224
234
|
|
|
225
235
|
/**
|
|
@@ -362,28 +372,14 @@ export default class RealtimeClient {
|
|
|
362
372
|
(this.accessToken && (await this.accessToken())) ||
|
|
363
373
|
this.accessTokenValue
|
|
364
374
|
|
|
365
|
-
if (tokenToSend) {
|
|
366
|
-
let parsed = null
|
|
367
|
-
try {
|
|
368
|
-
parsed = JSON.parse(atob(tokenToSend.split('.')[1]))
|
|
369
|
-
} catch (_error) {}
|
|
370
|
-
if (parsed && parsed.exp) {
|
|
371
|
-
let now = Math.floor(Date.now() / 1000)
|
|
372
|
-
let valid = now - parsed.exp < 0
|
|
373
|
-
if (!valid) {
|
|
374
|
-
this.log(
|
|
375
|
-
'auth',
|
|
376
|
-
`InvalidJWTToken: Invalid value for JWT claim "exp" with value ${parsed.exp}`
|
|
377
|
-
)
|
|
378
|
-
return Promise.reject(
|
|
379
|
-
`InvalidJWTToken: Invalid value for JWT claim "exp" with value ${parsed.exp}`
|
|
380
|
-
)
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
375
|
+
if (this.accessTokenValue != tokenToSend) {
|
|
384
376
|
this.accessTokenValue = tokenToSend
|
|
385
377
|
this.channels.forEach((channel) => {
|
|
386
|
-
tokenToSend &&
|
|
378
|
+
tokenToSend &&
|
|
379
|
+
channel.updateJoinPayload({
|
|
380
|
+
access_token: tokenToSend,
|
|
381
|
+
version: this.headers && this.headers['X-Client-Info'],
|
|
382
|
+
})
|
|
387
383
|
|
|
388
384
|
if (channel.joinedOnce && channel._isJoined()) {
|
|
389
385
|
channel._push(CHANNEL_EVENTS.access_token, {
|
|
@@ -416,7 +412,7 @@ export default class RealtimeClient {
|
|
|
416
412
|
payload: {},
|
|
417
413
|
ref: this.pendingHeartbeatRef,
|
|
418
414
|
})
|
|
419
|
-
this.setAuth()
|
|
415
|
+
await this.setAuth()
|
|
420
416
|
}
|
|
421
417
|
|
|
422
418
|
/**
|
package/src/lib/constants.ts
CHANGED
|
@@ -4,6 +4,8 @@ export const DEFAULT_HEADERS = { 'X-Client-Info': `realtime-js/${version}` }
|
|
|
4
4
|
|
|
5
5
|
export const VSN: string = '1.0.0'
|
|
6
6
|
|
|
7
|
+
export const VERSION = version
|
|
8
|
+
|
|
7
9
|
export const DEFAULT_TIMEOUT = 10000
|
|
8
10
|
|
|
9
11
|
export const WS_CLOSE_NORMAL = 1000
|
|
@@ -42,3 +44,9 @@ export enum CONNECTION_STATE {
|
|
|
42
44
|
Closing = 'closing',
|
|
43
45
|
Closed = 'closed',
|
|
44
46
|
}
|
|
47
|
+
|
|
48
|
+
export enum LOG_LEVEL {
|
|
49
|
+
Info = 'info',
|
|
50
|
+
Warn = 'warn',
|
|
51
|
+
Error = 'error',
|
|
52
|
+
}
|
package/src/lib/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '2.11.
|
|
1
|
+
export const version = '2.11.5'
|