All files / client/src/hmr client.js

6.71% Statements 9/134
0% Branches 0/58
5.26% Functions 1/19
6.76% Lines 9/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444                                                                                                                                  2x           2x           2x           2x           2x           2x           2x           2x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             2x  
/**
 * HMRClient - Orchestrates WebSocket connection and HMR updates
 *
 * Integrates all HMR modules (cleanup tracker, state capturer, error overlay,
 * connection indicator) to provide seamless hot module replacement with
 * state preservation.
 *
 * @module @coherent.js/client/hmr/client
 */
 
import { cleanupTracker } from './cleanup-tracker.js';
import { stateCapturer } from './state-capturer.js';
import { errorOverlay } from './overlay.js';
import { connectionIndicator } from './indicator.js';
import { moduleTracker } from './module-tracker.js';
 
/**
 * Parse error stack to extract file/line info
 *
 * @param {Error} error - Error object
 * @returns {{ file: string|null, line: number|null, column: number|null }} Parsed location
 */
function parseErrorLocation(error) {
  const result = { file: null, line: null, column: null };
 
  if (!error.stack) {
    return result;
  }
 
  // Match common stack trace formats:
  // Chrome/Node: "at Function (file.js:10:5)"
  // Firefox: "Function@file.js:10:5"
  // Safari: "file.js:10:5"
  const patterns = [
    /at\s+.*?\((.+?):(\d+):(\d+)\)/,  // Chrome/Node with parens
    /at\s+(.+?):(\d+):(\d+)/,          // Chrome/Node without parens
    /@(.+?):(\d+):(\d+)/,              // Firefox
    /^(.+?):(\d+):(\d+)/,              // Safari
  ];
 
  const lines = error.stack.split('\n');
  for (const line of lines) {
    for (const pattern of patterns) {
      const match = line.match(pattern);
      if (match) {
        result.file = match[1];
        result.line = parseInt(match[2], 10);
        result.column = parseInt(match[3], 10);
        return result;
      }
    }
  }
 
  return result;
}
 
/**
 * HMR Client class - manages WebSocket connection and update orchestration
 */
export class HMRClient {
  constructor() {
    /**
     * WebSocket connection
     * @type {WebSocket|null}
     */
    this.socket = null;
 
    /**
     * Connection state
     * @type {boolean}
     */
    this.connected = false;
 
    /**
     * Reconnection attempt counter
     * @type {number}
     */
    this.reconnectAttempts = 0;
 
    /**
     * Maximum reconnection attempts
     * @type {number}
     */
    this.maxReconnectAttempts = 10;
 
    /**
     * Base reconnection delay in ms
     * @type {number}
     */
    this.reconnectDelay = 1000;
 
    /**
     * Whether we've had a disconnect (for reload detection)
     * @type {boolean}
     */
    this.hadDisconnect = false;
 
    /**
     * Reconnect timeout ID
     * @type {number|null}
     */
    this.reconnectTimeout = null;
 
    /**
     * Initialization flag
     * @type {boolean}
     */
    this.initialized = false;
  }
 
  /**
   * Connect to the dev server WebSocket
   *
   * Establishes WebSocket connection with automatic reconnection using
   * exponential backoff with jitter.
   *
   * @returns {void}
   */
  connect() {
    if (typeof window === 'undefined') {
      return;
    }
 
    // Clear any pending reconnect
    if (this.reconnectTimeout !== null) {
      clearTimeout(this.reconnectTimeout);
      this.reconnectTimeout = null;
    }
 
    try {
      const protocol = location.protocol === 'https:' ? 'wss' : 'ws';
      const wsUrl = `${protocol}://${location.host}`;
      this.socket = new WebSocket(wsUrl);
 
      // Share socket with module tracker for invalidation messages
      moduleTracker.setSocket(this.socket);
 
      this.socket.addEventListener('open', () => {
        console.log('[HMR] Connected');
        this.connected = true;
        this.reconnectAttempts = 0;
        connectionIndicator.update('connected');
 
        // Send connection acknowledgment
        this.socket.send(JSON.stringify({ type: 'connected' }));
 
        // If reconnecting after disconnect, reload (server may have restarted)
        if (this.hadDisconnect) {
          console.log('[HMR] Reconnected after disconnect, reloading page');
          setTimeout(() => location.reload(), 200);
          return;
        }
      });
 
      this.socket.addEventListener('close', () => {
        this.connected = false;
        this.hadDisconnect = true;
        connectionIndicator.update('disconnected');
        moduleTracker.setSocket(null);
        this.scheduleReconnect();
      });
 
      this.socket.addEventListener('error', (event) => {
        console.warn('[HMR] WebSocket error:', event);
        connectionIndicator.update('error');
        try {
          this.socket.close();
        } catch {
          // Ignore close errors
        }
      });
 
      this.socket.addEventListener('message', (event) => {
        this.handleMessage(event);
      });
    } catch (error) {
      console.warn('[HMR] Failed to connect:', error);
      this.scheduleReconnect();
    }
  }
 
  /**
   * Schedule a reconnection attempt with exponential backoff
   *
   * @private
   */
  scheduleReconnect() {
    if (this.reconnectAttempts >= this.maxReconnectAttempts) {
      console.warn('[HMR] Max reconnection attempts reached');
      connectionIndicator.update('disconnected');
      return;
    }
 
    connectionIndicator.update('reconnecting');
 
    // Exponential backoff with jitter
    const delay = Math.min(
      this.reconnectDelay * Math.pow(2, this.reconnectAttempts) + Math.random() * 1000,
      30000
    );
    this.reconnectAttempts++;
 
    console.log(`[HMR] Reconnecting in ${Math.round(delay)}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
 
    this.reconnectTimeout = setTimeout(() => {
      this.reconnectTimeout = null;
      this.connect();
    }, delay);
  }
 
  /**
   * Handle incoming WebSocket message
   *
   * @param {MessageEvent} event - WebSocket message event
   * @private
   */
  handleMessage(event) {
    let data;
    try {
      data = JSON.parse(event.data);
    } catch {
      return;
    }
 
    console.log('[HMR] message', data.type, data.filePath || data.webPath || '');
 
    switch (data.type) {
      case 'connected':
        // Connection acknowledged
        break;
 
      case 'hmr-full-reload':
      case 'reload':
        console.warn('[HMR] Server requested full reload');
        location.reload();
        break;
 
      case 'hmr-component-update':
      case 'hmr-update':
        this.handleUpdate(data);
        break;
 
      case 'hmr-error':
        this.showError(data.error || data);
        break;
 
      case 'preview-update':
        // No-op: used by dashboard for live preview panes
        break;
 
      default:
        // Unknown message type, ignore
        break;
    }
  }
 
  /**
   * Handle module update
   *
   * Orchestrates the full HMR update cycle:
   * 1. Capture form/scroll state
   * 2. Execute dispose handlers
   * 3. Clean up module resources
   * 4. Re-import module
   * 5. Execute accept handlers
   * 6. Restore state
   *
   * @param {Object} data - Update message data
   * @param {string} [data.filePath] - File path that changed
   * @param {string} [data.webPath] - Web-accessible path
   * @param {string} [data.updateType] - Type of update (component, style, etc.)
   */
  async handleUpdate(data) {
    const filePath = data.webPath || data.filePath || '';
    const moduleId = filePath;
 
    try {
      // 1. Capture current state
      stateCapturer.captureAll();
 
      // 2. Execute dispose handler if registered
      if (moduleTracker.hasModule(moduleId)) {
        moduleTracker.executeDispose(moduleId);
      }
 
      // 3. Clean up module resources
      if (cleanupTracker.hasResources(moduleId)) {
        cleanupTracker.checkForLeaks(moduleId);
        cleanupTracker.cleanup(moduleId);
      }
 
      // 4. Re-import module with cache bust
      const importPath = filePath.startsWith('/') ? filePath : `/${filePath}`;
      const newModule = await import(`${importPath}?t=${Date.now()}`);
 
      // 5. Execute accept handler if module can hot-update
      const accepted = moduleTracker.canHotUpdate(moduleId);
      if (accepted) {
        moduleTracker.executeAccept(moduleId, newModule);
      } else {
        // Fall back to autoHydrate for components without explicit accept
        await this.fallbackHydrate();
      }
 
      // 6. Restore state
      stateCapturer.restoreAll();
 
      // 7. Hide error overlay (in case previous error is now fixed)
      errorOverlay.hide();
 
      // 8. Log success
      console.log(`[HMR] Updated: ${data.updateType || 'module'} ${filePath}`);
    } catch (error) {
      this.handleUpdateError(error, filePath);
    }
  }
 
  /**
   * Fall back to autoHydrate for non-HMR-aware modules
   *
   * @private
   */
  async fallbackHydrate() {
    try {
      // Try to import autoHydrate from the hydration module
      const { autoHydrate } = await import('../hydration.js');
 
      // If examples register a component registry on window, prefer targeted hydrate
      if (typeof window !== 'undefined' && window.componentRegistry) {
        autoHydrate(window.componentRegistry);
      } else {
        autoHydrate();
      }
    } catch {
      // autoHydrate not available or failed
      console.warn('[HMR] autoHydrate not available, component may need manual refresh');
    }
  }
 
  /**
   * Handle update error
   *
   * @param {Error} error - Error that occurred
   * @param {string} filePath - File that was being updated
   * @private
   */
  handleUpdateError(error, filePath) {
    console.error('[HMR] Update failed:', error);
 
    // Parse error location from stack
    const location = parseErrorLocation(error);
 
    // Build error details for overlay
    const errorDetails = {
      message: error.message || 'Unknown error during HMR update',
      file: location.file || filePath,
      line: location.line,
      column: location.column,
      stack: error.stack,
    };
 
    this.showError(errorDetails);
  }
 
  /**
   * Show error overlay
   *
   * @param {Object} error - Error details
   */
  showError(error) {
    errorOverlay.show(error);
  }
 
  /**
   * Hide error overlay
   */
  hideError() {
    errorOverlay.hide();
  }
 
  /**
   * Initialize HMR client
   *
   * Guards against double initialization and connects to dev server.
   *
   * @returns {void}
   */
  initialize() {
    if (typeof window === 'undefined') {
      return;
    }
 
    // Guard against double initialization
    if (window.__coherent_hmr_initialized || this.initialized) {
      return;
    }
 
    window.__coherent_hmr_initialized = true;
    this.initialized = true;
 
    this.connect();
  }
 
  /**
   * Disconnect and clean up
   *
   * @returns {void}
   */
  disconnect() {
    if (this.reconnectTimeout !== null) {
      clearTimeout(this.reconnectTimeout);
      this.reconnectTimeout = null;
    }
 
    if (this.socket) {
      try {
        this.socket.close();
      } catch {
        // Ignore close errors
      }
      this.socket = null;
    }
 
    this.connected = false;
    moduleTracker.setSocket(null);
    connectionIndicator.destroy();
  }
 
  /**
   * Check if client is connected
   *
   * @returns {boolean} True if connected
   */
  isConnected() {
    return this.connected;
  }
}
 
/**
 * Singleton HMR client instance
 * @type {HMRClient}
 */
export const hmrClient = new HMRClient();