Skip to main content

euv_cli/hot/
struct.rs

1use super::*;
2
3/// Filesystem modification stamp used for deterministic cross-platform change detection.
4#[derive(Clone, Copy, Debug, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
5pub struct FileStamp {
6    /// Nanoseconds since UNIX epoch of last file modification.
7    #[get(type(copy))]
8    pub(crate) modified_nanos: u128,
9    /// File byte length.
10    #[get(type(copy))]
11    pub(crate) size_bytes: u64,
12}
13
14/// Recursive file tree snapshot capturing stamps for all watched source files.
15#[derive(Clone, Debug, Default, Eq, Getter, PartialEq)]
16pub struct WatchSnapshot {
17    /// Ordered map from watched file paths to their modification stamps.
18    pub(crate) entries: BTreeMap<PathBuf, FileStamp>,
19}
20
21/// Categorized file changes detected between two consecutive `WatchSnapshot` scans.
22#[derive(Clone, Debug, Default, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
23pub struct FileChangeSet {
24    /// Newly created file paths.
25    pub(crate) added: Vec<String>,
26    /// Modified file paths.
27    pub(crate) modified: Vec<String>,
28    /// Deleted file paths.
29    pub(crate) removed: Vec<String>,
30}
31
32/// Signed Dynamic WASM OTA manifest served at `/__euv_ota/manifest.json`.
33#[derive(Clone, Debug, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
34pub struct OtaManifest {
35    /// Monotonically increasing hot-update version number.
36    #[get(type(copy))]
37    pub(crate) version: u64,
38    /// Target dynamic component slot identifier.
39    pub(crate) slot_id: String,
40    /// HTTP URL path serving the executable `.wasm` binary.
41    pub(crate) module_url: String,
42    /// FNV-1a integrity checksum of the `.wasm` bytes.
43    #[get(type(copy))]
44    pub(crate) checksum: u64,
45    /// Keyed integrity signature matching `OTA_DEV_SIGNING_KEY`.
46    pub(crate) signature: String,
47    /// Byte size of the compiled `.wasm` binary.
48    #[get(type(copy))]
49    pub(crate) size_bytes: usize,
50    /// Source files that triggered this hot-update build.
51    pub(crate) changed_files: Vec<String>,
52}
53
54/// In-memory pair holding the active OTA manifest and its executable `.wasm` bytes.
55#[derive(Clone, Debug, Eq, Getter, New, PartialEq)]
56pub struct OtaBundleState {
57    /// Public manifest metadata.
58    pub(crate) manifest: OtaManifest,
59    /// Raw executable `.wasm` binary bytes.
60    pub(crate) wasm_bytes: Vec<u8>,
61}
62
63/// Structured live-reload notification broadcast over SSE `/__euv_hmr`.
64#[derive(Clone, Debug, Deserialize, Eq, Getter, New, PartialEq, Serialize)]
65pub struct HmrEvent {
66    /// Monotonic reload revision number.
67    #[get(type(copy))]
68    pub(crate) revision: u64,
69    /// Event kind (`Reload`, `OtaUpdate`, or `BuildError`).
70    #[get(type(copy))]
71    pub(crate) kind: HmrEventKind,
72    /// Human-readable status summary.
73    pub(crate) message: String,
74    /// Paths of changed files that triggered this event.
75    pub(crate) changed_files: Vec<String>,
76}
77
78/// Configuration for starting a live hot-reload and v1 OTA development server.
79#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
80pub struct DevServerConfig {
81    /// Development target (`Docs`, `Web`, `Native`, `Tui`, `Cli`, `Harmony`, `Wearable`, `Miniapp`, `Quickapp`).
82    #[get(type(copy))]
83    pub(crate) target: DevTargetArg,
84    /// Render mode forwarded to native or web runs.
85    #[get(type(copy))]
86    pub(crate) render: RenderArg,
87    /// Whether release optimization profile is enabled for Web or Native rebuilds.
88    #[get(type(copy))]
89    pub(crate) release: bool,
90    /// Source root directory watched for live changes.
91    pub(crate) src_dir: PathBuf,
92    /// Output directory served over HTTP.
93    pub(crate) out_dir: PathBuf,
94    /// Optional site title (used in Docs mode).
95    pub(crate) title: Option<String>,
96    /// Optional base URL prefix (used in Docs mode).
97    pub(crate) base_url: String,
98    /// Optional Cargo package name (used in Web/Native modes).
99    pub(crate) package: Option<String>,
100    /// Bind host address (default `127.0.0.1`).
101    pub(crate) host: String,
102    /// Bind TCP port (`0` selects an available ephemeral port).
103    #[get(type(copy))]
104    pub(crate) port: u16,
105    /// File watcher polling interval in milliseconds.
106    #[get(type(copy))]
107    pub(crate) poll_ms: u64,
108    /// Whether to expose v1 OTA endpoints (and legacy compatibility shim) on the dev server.
109    #[get(type(copy))]
110    pub(crate) enable_ota: bool,
111    /// When `true`, performs initial build and server check then exits immediately.
112    #[get(type(copy))]
113    pub(crate) once: bool,
114    /// When `true`, prioritizes live v1 OTA hot-push without restarting the host app.
115    #[get(type(copy))]
116    #[serde(default)]
117    pub(crate) hot_ota: bool,
118    /// Optional watched `.json` spec, `.wasm` binary, or dynamic widget crate pushed via v1 OTA.
119    #[serde(default)]
120    pub(crate) watch_ota_path: Option<PathBuf>,
121    /// Optional explicit v1 OTA store root directory (defaults to `<out_dir>/.euv-updates`).
122    #[serde(default)]
123    pub(crate) ota_store_dir: Option<PathBuf>,
124    /// Logical v1 OTA project identifier mounted inside `euv dev`.
125    #[serde(default)]
126    pub(crate) ota_project: String,
127    /// Logical v1 OTA release channel mounted inside `euv dev` (default `"dev"`).
128    #[serde(default)]
129    pub(crate) ota_channel: String,
130    /// HMAC-SHA256 signing secret used for local v1 OTA releases.
131    #[serde(default)]
132    pub(crate) ota_signing_key: String,
133    /// Optional target route path updated by `--watch-ota` pushes.
134    #[serde(default)]
135    pub(crate) ota_route: Option<String>,
136    /// Optional target component slot identifier updated by `--watch-ota` pushes.
137    #[serde(default)]
138    pub(crate) ota_slot: Option<String>,
139    /// Debounce settle window in milliseconds coalescing rapid multi-file saves.
140    #[get(type(copy))]
141    #[serde(default)]
142    pub(crate) debounce_ms: u64,
143    /// When `true`, emits `Cross-Origin-Opener-Policy` and `Cross-Origin-Embedder-Policy` headers for `SharedArrayBuffer`.
144    #[get(type(copy))]
145    #[serde(default)]
146    pub(crate) coop_coep: bool,
147    /// When `true`, falls back to `index.html` for non-asset client-side SPA routes.
148    #[get(type(copy))]
149    #[serde(default)]
150    pub(crate) spa_fallback: bool,
151}
152
153/// Machine-readable report returned when starting or probing the dev/docs server.
154#[derive(Clone, Data, Debug, Deserialize, Eq, New, PartialEq, Serialize)]
155pub struct DevServerReport {
156    /// Active dev mode (`docs`, `web`, `native`, `tui`, `cli`, `harmony`, `wearable`, `miniapp`, `quickapp`).
157    pub(crate) mode: String,
158    /// Base HTTP URL of the live dev server.
159    pub(crate) url: String,
160    /// SSE hot-reload stream URL.
161    pub(crate) hmr_url: String,
162    /// Legacy Dynamic WASM OTA manifest compatibility URL.
163    pub(crate) ota_manifest_url: String,
164    /// Output bundle directory served by the dev server.
165    pub(crate) out_dir: String,
166    /// Initial build revision number.
167    #[get(type(copy))]
168    pub(crate) revision: u64,
169    /// Total generated documentation pages (for Docs mode, `0` otherwise).
170    #[get(type(copy))]
171    pub(crate) page_count: usize,
172    /// Real v1 OTA signed manifest endpoint URL.
173    #[new(skip)]
174    #[serde(default)]
175    pub(crate) v1_manifest_url: String,
176    /// Real v1 OTA SSE events endpoint URL.
177    #[new(skip)]
178    #[serde(default)]
179    pub(crate) v1_events_url: String,
180    /// Health endpoint URL (`/healthz`).
181    #[new(skip)]
182    #[serde(default)]
183    pub(crate) health_url: String,
184    /// Detected LAN IPv4 v1 OTA manifest URLs reachable from real mobile devices and emulators.
185    #[new(skip)]
186    #[serde(default)]
187    pub(crate) lan_urls: Vec<String>,
188    /// Active v1 OTA logical project identifier.
189    #[new(skip)]
190    #[serde(default)]
191    pub(crate) ota_project: String,
192    /// Active v1 OTA release channel name.
193    #[new(skip)]
194    #[serde(default)]
195    pub(crate) ota_channel: String,
196}
197
198/// Coalescing debounced file-change accumulator preventing redundant rebuild bursts on multi-file saves.
199#[derive(Clone, Debug, Eq, Getter, PartialEq)]
200pub struct DebouncedWatcher {
201    /// Minimum quiet window in milliseconds before flushing accumulated changes.
202    #[get(type(copy))]
203    pub(crate) debounce_ms: u64,
204    /// Accumulated deduplicated file changes waiting for the quiet window to expire.
205    pub(crate) pending: FileChangeSet,
206    /// Timestamp of the most recently recorded filesystem change.
207    pub(crate) last_change_at: Option<Instant>,
208}
209
210/// Deterministic state machine tracking zero-flicker build-before-swap transitions for native targets.
211#[derive(Clone, Data, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
212pub struct BuildBeforeSwapController {
213    /// Active running process generation (`0` before initial launch).
214    #[get(type(copy))]
215    pub(crate) active_generation: u64,
216    /// Total successful build-then-swap replacements completed.
217    #[get(type(copy))]
218    pub(crate) swap_count: u64,
219    /// Number of failed rebuilds where the previous running app stayed alive.
220    #[get(type(copy))]
221    pub(crate) kept_alive_on_error_count: u64,
222    /// Most recent compiler error recorded when keeping the prior process running.
223    pub(crate) last_error: Option<String>,
224}
225
226/// RAII wrapper ensuring spawned native child processes are terminated and reaped on drop or shutdown.
227#[derive(Debug, Default)]
228pub(crate) struct ChildProcessGuard {
229    /// Active spawned child process handle, if running.
230    pub(crate) child: Option<Child>,
231}
232
233/// Shared concurrent state coordinating SSE reload notifications, OTA bundles, and server shutdown.
234#[derive(Debug)]
235pub struct HmrHub {
236    /// Monotonically incremented rebuild revision counter.
237    pub(crate) revision: AtomicU64,
238    /// Latest broadcast `HmrEvent`.
239    pub(crate) last_event: Mutex<Option<HmrEvent>>,
240    /// Latest signed Dynamic WASM OTA manifest and binary bytes.
241    pub(crate) ota_state: Mutex<OtaBundleState>,
242    /// Flag indicating whether background HTTP and watcher loops should keep running.
243    pub(crate) running: AtomicBool,
244}
245
246/// Running hot-reload dev server handle supporting clean programmatic shutdown and inspection.
247#[derive(Debug)]
248pub struct DevServerHandle {
249    /// Bound socket address (`ip:port`).
250    pub(crate) address: SocketAddr,
251    /// Served output directory path.
252    pub(crate) out_dir: PathBuf,
253    /// Active mode label (`docs`, `web`, `native`, `tui`, `cli`, `harmony`, `wearable`, `miniapp`, `quickapp`).
254    pub(crate) mode: String,
255    /// Initial page count produced during startup build.
256    pub(crate) page_count: usize,
257    /// Backing v1 OTA `UpdateStore` mounted inside this dev server.
258    pub(crate) ota_store: UpdateStore,
259    /// Logical v1 OTA project identifier served by this handle.
260    pub(crate) ota_project: String,
261    /// Logical v1 OTA channel name served by this handle.
262    pub(crate) ota_channel: String,
263    /// Shared HMR and OTA state hub.
264    pub(crate) hub: Arc<HmrHub>,
265    /// Background HTTP/SSE/OTA server thread.
266    pub(crate) http_thread: Option<JoinHandle<()>>,
267    /// Background recursive file watcher and rebuild thread.
268    pub(crate) watch_thread: Option<JoinHandle<()>>,
269}
270
271/// Internal HTTP serving parameters shared across accepted connections.
272#[derive(Clone, Debug, Getter)]
273pub(crate) struct HttpServeContext {
274    /// Output root directory served over HTTP.
275    #[get(pub(crate))]
276    pub(crate) out_dir: PathBuf,
277    /// Optional base URL prefix stripped from incoming static requests.
278    #[get(pub(crate))]
279    pub(crate) base_url: String,
280    /// Whether Dynamic WASM and v1 OTA endpoints are exposed.
281    #[get(pub(crate), type(copy))]
282    pub(crate) enable_ota: bool,
283    /// Whether `COOP`/`COEP` cross-origin isolation headers are emitted.
284    #[get(pub(crate), type(copy))]
285    pub(crate) coop_coep: bool,
286    /// Whether non-asset 404 routes fall back to `index.html`.
287    #[get(pub(crate), type(copy))]
288    pub(crate) spa_fallback: bool,
289    /// Backing v1 OTA store mounted on `/v1/*` and `/healthz`.
290    #[get(pub(crate))]
291    pub(crate) ota_store: UpdateStore,
292    /// Shared HMR and OTA state hub.
293    #[get(pub(crate))]
294    pub(crate) hub: Arc<HmrHub>,
295}