|
| 1 | +// MessagePort-based HMR connection for browser environments (utoo-web) |
| 2 | +// This replaces WebSocket when running in an iframe with HmrServer |
| 3 | + |
| 4 | +type WebSocketMessage = |
| 5 | + | { |
| 6 | + type: "turbopack-connected"; |
| 7 | + } |
| 8 | + | { |
| 9 | + type: "turbopack-message"; |
| 10 | + data: Record<string, any>; |
| 11 | + }; |
| 12 | + |
| 13 | +let port: MessagePort | null = null; |
| 14 | +let eventCallbacks: Array<(event: WebSocketMessage) => void> = []; |
| 15 | + |
| 16 | +// Helper function to dispatch messages to all event callbacks |
| 17 | +function dispatchMessage(message: WebSocketMessage) { |
| 18 | + for (const eventCallback of eventCallbacks) { |
| 19 | + eventCallback(message); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export function addMessageListener( |
| 24 | + callback: (event: WebSocketMessage) => void, |
| 25 | +) { |
| 26 | + eventCallbacks.push(callback); |
| 27 | +} |
| 28 | + |
| 29 | +export function sendMessage(data: any) { |
| 30 | + if (port) { |
| 31 | + const message = typeof data === "string" ? data : JSON.stringify(data); |
| 32 | + port.postMessage(message); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +export interface HMROptions { |
| 37 | + // Not used for MessagePort, kept for API compatibility |
| 38 | + path?: string; |
| 39 | +} |
| 40 | + |
| 41 | +let reloading = false; |
| 42 | +let serverSessionId: number | null = null; |
| 43 | + |
| 44 | +function handleMessage(event: MessageEvent<string>) { |
| 45 | + if (reloading) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + const msg = |
| 51 | + typeof event.data === "string" ? JSON.parse(event.data) : event.data; |
| 52 | + |
| 53 | + // Handle the different message formats from HmrServer |
| 54 | + if (msg.action === "turbopack-connected") { |
| 55 | + if (serverSessionId !== null && serverSessionId !== msg.data.sessionId) { |
| 56 | + window.location.reload(); |
| 57 | + reloading = true; |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + serverSessionId = msg.data.sessionId; |
| 62 | + |
| 63 | + // Convert to turbopack format and trigger handleSocketConnected |
| 64 | + const connected: WebSocketMessage = { type: "turbopack-connected" }; |
| 65 | + dispatchMessage(connected); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (msg.action === "reload") { |
| 70 | + window.location.reload(); |
| 71 | + reloading = true; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (msg.action === "turbopack-message") { |
| 76 | + const turbopackMessage: WebSocketMessage = { |
| 77 | + type: "turbopack-message", |
| 78 | + data: msg.data, |
| 79 | + }; |
| 80 | + dispatchMessage(turbopackMessage); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Handle direct turbopack-dev-server messages |
| 85 | + if ( |
| 86 | + msg.type && |
| 87 | + ["partial", "restart", "notFound", "issues"].includes(msg.type) |
| 88 | + ) { |
| 89 | + const turbopackMessage: WebSocketMessage = { |
| 90 | + type: "turbopack-message", |
| 91 | + data: msg, |
| 92 | + }; |
| 93 | + dispatchMessage(turbopackMessage); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // TODO: handle rest msg.actions |
| 98 | + } catch (e) { |
| 99 | + console.error("[HMR] Failed to parse message:", e); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Connect to HMR server via MessagePort. |
| 105 | + * The iframe sends "hmr-ready" to parent, and parent responds with "hmr-connect" |
| 106 | + * containing the MessagePort for bidirectional communication. |
| 107 | + */ |
| 108 | +export function connectHMR(_options?: HMROptions) { |
| 109 | + if (typeof window === "undefined") { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + console.log("[HMR] waiting for MessagePort connection..."); |
| 114 | + |
| 115 | + window.addEventListener("message", (event) => { |
| 116 | + // Check if this is an HMR connect message with a port |
| 117 | + if (event.data?.type === "hmr-connect" && event.ports.length > 0) { |
| 118 | + if (port) { |
| 119 | + // Already connected, close previous port |
| 120 | + port.close(); |
| 121 | + } |
| 122 | + |
| 123 | + port = event.ports[0]; |
| 124 | + port.onmessage = handleMessage; |
| 125 | + port.onmessageerror = () => { |
| 126 | + console.error("[HMR] MessagePort error"); |
| 127 | + port = null; |
| 128 | + }; |
| 129 | + |
| 130 | + console.log("[HMR] connected via MessagePort"); |
| 131 | + |
| 132 | + // Don't dispatch connected event here - the server will send |
| 133 | + // a "turbopack-connected" message through the port which will |
| 134 | + // trigger the connected event via handleMessage |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + // Signal to parent that HMR client is ready |
| 139 | + if (window.parent && window.parent !== window) { |
| 140 | + window.parent.postMessage({ type: "hmr-ready" }, "*"); |
| 141 | + console.log("[HMR] sent hmr-ready to parent"); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Check if HMR is connected |
| 147 | + */ |
| 148 | +export function isConnected(): boolean { |
| 149 | + return port !== null; |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Disconnect HMR |
| 154 | + */ |
| 155 | +export function disconnect() { |
| 156 | + if (port) { |
| 157 | + port.close(); |
| 158 | + port = null; |
| 159 | + } |
| 160 | + eventCallbacks = []; |
| 161 | + serverSessionId = null; |
| 162 | + reloading = false; |
| 163 | +} |
0 commit comments