|
|
|
|
|
|
|
|
import type { WebSocketMessage } from '@vercel/turbopack-ecmascript-runtime/browser/dev/hmr-client/hmr-client' |
|
|
|
|
|
let source: WebSocket |
|
|
const eventCallbacks: ((msg: WebSocketMessage) => void)[] = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getSocketProtocol(assetPrefix: string): string { |
|
|
let protocol = location.protocol |
|
|
|
|
|
try { |
|
|
|
|
|
protocol = new URL(assetPrefix).protocol |
|
|
} catch (_) {} |
|
|
|
|
|
return protocol === 'http:' ? 'ws' : 'wss' |
|
|
} |
|
|
|
|
|
export function addMessageListener(cb: (msg: WebSocketMessage) => void) { |
|
|
eventCallbacks.push(cb) |
|
|
} |
|
|
|
|
|
export function sendMessage(data: any) { |
|
|
if (!source || source.readyState !== source.OPEN) return |
|
|
return source.send(data) |
|
|
} |
|
|
|
|
|
export type HMROptions = { |
|
|
path: string |
|
|
assetPrefix: string |
|
|
timeout?: number |
|
|
log?: boolean |
|
|
} |
|
|
|
|
|
|
|
|
export function connectHMR(options: HMROptions) { |
|
|
const { timeout = 5 * 1000 } = options |
|
|
|
|
|
function init() { |
|
|
if (source) source.close() |
|
|
|
|
|
console.log('[HMR] connecting...') |
|
|
|
|
|
function handleOnline() { |
|
|
const connected = { type: 'turbopack-connected' as const } |
|
|
eventCallbacks.forEach((cb) => { |
|
|
cb(connected) |
|
|
}) |
|
|
|
|
|
if (options.log) console.log('[HMR] connected') |
|
|
|
|
|
} |
|
|
|
|
|
function handleMessage(event: MessageEvent) { |
|
|
|
|
|
|
|
|
const message = { |
|
|
type: 'turbopack-message' as const, |
|
|
data: JSON.parse(event.data), |
|
|
} |
|
|
eventCallbacks.forEach((cb) => { |
|
|
cb(message) |
|
|
}) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function handleDisconnect() { |
|
|
source.close() |
|
|
setTimeout(init, timeout) |
|
|
} |
|
|
|
|
|
const { hostname, port } = location |
|
|
const protocol = getSocketProtocol(options.assetPrefix || '') |
|
|
const assetPrefix = options.assetPrefix.replace(/^\/+/, '') |
|
|
|
|
|
let url = `${protocol}://${hostname}:${port}${ |
|
|
assetPrefix ? `/${assetPrefix}` : '' |
|
|
}` |
|
|
|
|
|
if (assetPrefix.startsWith('http')) { |
|
|
url = `${protocol}://${assetPrefix.split('://')[1]}` |
|
|
} |
|
|
|
|
|
source = new window.WebSocket(`${url}${options.path}`) |
|
|
source.onopen = handleOnline |
|
|
source.onerror = handleDisconnect |
|
|
source.onmessage = handleMessage |
|
|
} |
|
|
|
|
|
init() |
|
|
} |
|
|
|