UNPKG

mockaton

Version:
51 lines (40 loc) 1.41 kB
import { join } from 'node:path' import { EventEmitter } from 'node:events' import { watch, readdirSync } from 'node:fs' import { sendJSON, sendNotFound } from './utils/http-response.js' import { LONG_POLL_SERVER_TIMEOUT } from './ApiConstants.js' const DEV = process.env.NODE_ENV === 'development' export const CLIENT_DIR = join(import.meta.dirname, '../client') export const DASHBOARD_ASSETS = readdirSync(CLIENT_DIR) const devClientWatcher = new class extends EventEmitter { emit(file) { super.emit('RELOAD', file) } subscribe(listener) { this.once('RELOAD', listener) } unsubscribe(listener) { this.removeListener('RELOAD', listener) } } // Although `client/indexHtml.js` is watched, it returns a stale version. // i.e., it would need to be a dynamic import + cache busting. export function watchDevSPA() { watch(CLIENT_DIR, (_, file) => { devClientWatcher.emit(file) }) } /** Realtime notify Dev UI changes */ export function longPollDevClientHotReload(req, response) { if (!DEV) { sendNotFound(response) return } function onDevChange(file) { devClientWatcher.unsubscribe(onDevChange) sendJSON(response, file) } response.setTimeout(LONG_POLL_SERVER_TIMEOUT, () => { devClientWatcher.unsubscribe(onDevChange) sendJSON(response, '') }) req.on('error', () => { devClientWatcher.unsubscribe(onDevChange) response.destroy() }) devClientWatcher.subscribe(onDevChange) }