Skip to content

Commit 852a02d

Browse files
authored
merge next to master (#491)
* fix: leavePush timer leaked (#483) * fix: add isows (#485) * fix: add isows * fix tests * fix error event
1 parent 2fc5162 commit 852a02d

11 files changed

+134
-136
lines changed

package-lock.json

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"dependencies": {
4141
"@supabase/node-fetch": "^2.6.13",
4242
"@types/phoenix": "^1.6.6",
43-
"@types/ws": "^8.18.1",
44-
"ws": "^8.18.2"
43+
"isows": "^1.0.7"
4544
},
4645
"devDependencies": {
4746
"@arethetypeswrong/cli": "^0.16.4",
@@ -62,18 +61,5 @@
6261
"typescript": "^5.8.3",
6362
"vitest": "^3.1.4",
6463
"web-worker": "1.2.0"
65-
},
66-
"exports": {
67-
".": {
68-
"node":"./dist/main/index.js",
69-
"browser": "./dist/module/index.js",
70-
"default": "./dist/main/index.js"
71-
},
72-
"./websocket": {
73-
"node": "./dist/WebSocket.js",
74-
"browser": "./dist/WebSocket.native.js",
75-
"types": "./dist/WebSocket.d.ts",
76-
"default": "./dist/WebSocket.native.js"
77-
}
7864
}
7965
}

pnpm-lock.yaml

Lines changed: 15 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RealtimeChannel.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export default class RealtimeChannel {
414414
): RealtimeChannel
415415
on(
416416
type: `${REALTIME_LISTEN_TYPES}`,
417-
filter: { event: string; [key: string]: string },
417+
filter: { event: string;[key: string]: string },
418418
callback: (payload: any) => void
419419
): RealtimeChannel {
420420
return this._on(type, filter, callback)
@@ -514,8 +514,10 @@ export default class RealtimeChannel {
514514

515515
this.joinPush.destroy()
516516

517-
return new Promise((resolve) => {
518-
const leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout)
517+
let leavePush: Push | null = null
518+
519+
return new Promise<RealtimeChannelSendResponse>((resolve) => {
520+
leavePush = new Push(this, CHANNEL_EVENTS.leave, {}, timeout)
519521
leavePush
520522
.receive('ok', () => {
521523
onClose()
@@ -534,6 +536,9 @@ export default class RealtimeChannel {
534536
leavePush.trigger('ok', {})
535537
}
536538
})
539+
.finally(() => {
540+
leavePush?.destroy()
541+
})
537542
}
538543
/**
539544
* Teardown the channel.
@@ -644,7 +649,7 @@ export default class RealtimeChannel {
644649
payload.ids?.includes(bindId) &&
645650
(bindEvent === '*' ||
646651
bindEvent?.toLocaleLowerCase() ===
647-
payload.data?.type.toLocaleLowerCase())
652+
payload.data?.type.toLocaleLowerCase())
648653
)
649654
} else {
650655
const bindEvent = bind?.filter?.event?.toLocaleLowerCase()

src/RealtimeClient.ts

Lines changed: 25 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import WebSocket from './WebSocket'
1+
import { WebSocket } from 'isows'
22

33
import {
44
CHANNEL_EVENTS,
@@ -44,18 +44,16 @@ export type HeartbeatStatus =
4444
| 'timeout'
4545
| 'disconnected'
4646

47-
const noop = () => {}
47+
const noop = () => { }
4848

4949
export interface WebSocketLikeConstructor {
50-
new (
50+
new(
5151
address: string | URL,
5252
_ignored?: any,
5353
options?: { headers: Object | undefined }
54-
): WebSocketLike
54+
): WebSocket
5555
}
5656

57-
export type WebSocketLike = WebSocket | WSWebSocketDummy
58-
5957
export interface WebSocketLikeError {
6058
error: any
6159
message: string
@@ -109,7 +107,7 @@ export default class RealtimeClient {
109107
encode: Function
110108
decode: Function
111109
reconnectAfterMs: Function
112-
conn: WebSocketLike | null = null
110+
conn: WebSocket | null = null
113111
sendBuffer: Function[] = []
114112
serializer: Serializer = new Serializer()
115113
stateChangeCallbacks: {
@@ -118,11 +116,11 @@ export default class RealtimeClient {
118116
error: Function[]
119117
message: Function[]
120118
} = {
121-
open: [],
122-
close: [],
123-
error: [],
124-
message: [],
125-
}
119+
open: [],
120+
close: [],
121+
error: [],
122+
message: [],
123+
}
126124
fetch: Fetch
127125
accessToken: (() => Promise<string | null>) | null = null
128126
worker?: boolean
@@ -176,13 +174,13 @@ export default class RealtimeClient {
176174
this.reconnectAfterMs = options?.reconnectAfterMs
177175
? options.reconnectAfterMs
178176
: (tries: number) => {
179-
return [1000, 2000, 5000, 10000][tries - 1] || 10000
180-
}
177+
return [1000, 2000, 5000, 10000][tries - 1] || 10000
178+
}
181179
this.encode = options?.encode
182180
? options.encode
183181
: (payload: JSON, callback: Function) => {
184-
return callback(JSON.stringify(payload))
185-
}
182+
return callback(JSON.stringify(payload))
183+
}
186184
this.decode = options?.decode
187185
? options.decode
188186
: this.serializer.decode.bind(this.serializer)
@@ -212,25 +210,11 @@ export default class RealtimeClient {
212210
if (!this.transport) {
213211
this.transport = WebSocket
214212
}
215-
if (this.transport) {
216-
// Detect if using the native browser WebSocket
217-
const isBrowser =
218-
typeof window !== 'undefined' && this.transport === window.WebSocket
219-
if (isBrowser) {
220-
this.conn = new this.transport(this.endpointURL())
221-
} else {
222-
this.conn = new this.transport(this.endpointURL(), undefined, {
223-
headers: this.headers,
224-
})
225-
}
226-
this.setupConnection()
227-
return
228-
}
229-
this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
230-
close: () => {
231-
this.conn = null
232-
},
233-
})
213+
214+
this.conn = new this.transport(this.endpointURL(), undefined, {
215+
headers: this.headers,
216+
}) as WebSocket
217+
this.setupConnection()
234218
}
235219

236220
/**
@@ -252,7 +236,7 @@ export default class RealtimeClient {
252236
*/
253237
disconnect(code?: number, reason?: string): void {
254238
if (this.conn) {
255-
this.conn.onclose = function () {} // noop
239+
this.conn.onclose = function () { } // noop
256240
if (code) {
257241
this.conn.close(code, reason ?? '')
258242
} else {
@@ -518,8 +502,7 @@ export default class RealtimeClient {
518502
if (this.conn) {
519503
this.conn.binaryType = 'arraybuffer'
520504
this.conn.onopen = () => this._onConnOpen()
521-
this.conn.onerror = (error: WebSocketLikeError) =>
522-
this._onConnError(error as WebSocketLikeError)
505+
this.conn.onerror = (error: Event) => this._onConnError(error)
523506
this.conn.onmessage = (event: any) => this._onConnMessage(event)
524507
this.conn.onclose = (event: any) => this._onConnClose(event)
525508
}
@@ -540,8 +523,7 @@ export default class RealtimeClient {
540523

541524
this.log(
542525
'receive',
543-
`${payload.status || ''} ${topic} ${event} ${
544-
(ref && '(' + ref + ')') || ''
526+
`${payload.status || ''} ${topic} ${event} ${(ref && '(' + ref + ')') || ''
545527
}`,
546528
payload
547529
)
@@ -613,8 +595,8 @@ export default class RealtimeClient {
613595
}
614596

615597
/** @internal */
616-
private _onConnError(error: WebSocketLikeError) {
617-
this.log('transport', error.message)
598+
private _onConnError(error: Event) {
599+
this.log('transport', `${error}`)
618600
this._triggerChanError()
619601
this.stateChangeCallbacks.error.forEach((callback) => callback(error))
620602
}
@@ -649,25 +631,4 @@ export default class RealtimeClient {
649631
}
650632
return result_url
651633
}
652-
}
653-
654-
class WSWebSocketDummy {
655-
binaryType: string = 'arraybuffer'
656-
close: Function
657-
onclose: Function = () => {}
658-
onerror: Function = () => {}
659-
onmessage: Function = () => {}
660-
onopen: Function = () => {}
661-
readyState: number = SOCKET_STATES.connecting
662-
send: Function = () => {}
663-
url: string | URL | null = null
664-
665-
constructor(
666-
address: string,
667-
_protocols: undefined,
668-
options: { close: Function }
669-
) {
670-
this.url = address
671-
this.close = options.close
672-
}
673-
}
634+
}

src/WebSocket.browser.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/WebSocket.native.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/WebSocket.node.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/WebSocket.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)