Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ws conditional import #444

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/RealtimeClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { WebSocket as WSWebSocket } from 'ws'

import {
CHANNEL_EVENTS,
CONNECTION_STATE,
Expand Down Expand Up @@ -62,16 +60,20 @@ export interface WebSocketLikeConstructor {
options?: { headers: Object | undefined }
): WebSocketLike
}
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'

export type WebSocketLike = WebSocket | WSWebSocket | WSWebSocketDummy
const WSWebSocket = NATIVE_WEBSOCKET_AVAILABLE
? WebSocket
: require('ws').WebSocket

export type WebSocketLike = WebSocket | typeof WSWebSocket | WSWebSocketDummy

export interface WebSocketLikeError {
error: any
message: string
type: string
}

const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== 'undefined'
const WORKER_SCRIPT = `
addEventListener("message", (e) => {
if (e.data.event === "start") {
Expand Down Expand Up @@ -204,20 +206,20 @@ export default class RealtimeClient {
this.conn = new WebSocket(this.endpointURL())
this.setupConnection()
return
}

this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
close: () => {
this.conn = null
},
})
} else {
this.conn = new WSWebSocketDummy(this.endpointURL(), undefined, {
close: () => {
this.conn = null
},
})

import('ws').then(({ default: WS }) => {
this.conn = new WS(this.endpointURL(), undefined, {
headers: this.headers,
import('ws').then(({ default: WS }) => {
this.conn = new WS(this.endpointURL(), undefined, {
headers: this.headers,
})
this.setupConnection()
})
this.setupConnection()
})
}
}

/**
Expand Down
File renamed without changes.
Loading