oj_server 0.2.41

Dev server: on-demand compile over HTTP, WebSocket HMR channel, file watcher
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
445
446
447
448
449
450
451
452
453
454
455
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Raphael Amorim

//! Vite's cold-boot dep optimization, end to end through the plugin host's
//! scan (scanProcessing itself is covered in optimize.rs's unit tests):
//! - the plan they compile against must match what the bundle commits for
//!   every common dep shape (react's named facade, the jsx runtime, plain
//!   CJS, ESM): a miss bumps the deps version and reloads the page;
//! - a dep the crawl finds that the scan missed joins the first pre-bundle
//!   (Vite's holdUntilCrawlEnd) instead of costing a rerun.

use std::path::Path;
use std::time::{Duration, Instant};

use regex::Regex;

use crate::App;

const INDEX: &str = r#"<!doctype html><html><head></head><body><script type="module" src="/main.js"></script></body></html>"#;

fn dep_urls(code: &str) -> Vec<String> {
    Regex::new(r#"(?:from|import)\s*"(/@oj-deps/[^"]+)""#)
        .unwrap()
        .captures_iter(code)
        .map(|c| c[1].to_string())
        .collect()
}

fn url_version(url: &str) -> &str {
    url.rsplit_once("?v=").map(|(_, v)| v).unwrap_or("")
}

/// The first deps manifest the commit writes.
async fn first_manifest(root: &Path) -> serde_json::Value {
    let path = oj_cache::cache_root(root).join("deps/manifest.json");
    let deadline = Instant::now() + Duration::from_secs(30);
    loop {
        if let Some(v) = std::fs::read_to_string(&path)
            .ok()
            .and_then(|raw| serde_json::from_str(&raw).ok())
        {
            return v;
        }
        assert!(Instant::now() < deadline, "no deps manifest was written");
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
}

fn plain_dep(name: &str, cjs: bool) -> [(String, String); 2] {
    let (kind, body) = if cjs {
        ("commonjs", "exports.hi = () => 1;\n")
    } else {
        ("module", "export const hi = () => 1;\n")
    };
    [
        (
            format!("node_modules/{name}/package.json"),
            format!(r#"{{"name":"{name}","version":"1.0.0","type":"{kind}","main":"index.js"}}"#),
        ),
        (format!("node_modules/{name}/index.js"), body.to_string()),
    ]
}

fn app_with(files: &[(String, String)]) -> App {
    let refs: Vec<(&str, &str)> = files
        .iter()
        .map(|(p, b)| (p.as_str(), b.as_str()))
        .collect();
    App::new(&refs)
}

#[tokio::test(flavor = "multi_thread")]
async fn cold_boot_plan_matches_the_committed_pre_bundle() {
    crate::require_fixture!("vite", "react", "react-dom");
    let mut files = vec![
        (
            "package.json".to_string(),
            r#"{"name":"plan-app","private":true,"type":"module"}"#.to_string(),
        ),
        ("vite.config.js".into(), "export default {};\n".into()),
        ("index.html".into(), INDEX.into()),
        (
            "main.js".into(),
            "import { useState } from \"react\";\n\
             import { createRoot } from \"react-dom/client\";\n\
             import { hi as a } from \"plain-cjs\";\n\
             import { hi as b } from \"plain-esm\";\n\
             window.__R = [useState, createRoot, a(), b()];\n"
                .into(),
        ),
    ];
    files.extend(plain_dep("plain-cjs", true));
    files.extend(plain_dep("plain-esm", false));
    let app = app_with(&files);
    for pkg in ["vite", "react", "react-dom"] {
        app.link_fixture(pkg);
    }
    let server = app.serve().await;

    let first = dep_urls(&server.text("/main.js").await);
    assert_eq!(
        first.len(),
        4,
        "every dep served from the pre-bundle: {first:?}"
    );
    // Each dep request waits for the commit.
    for url in &first {
        server.text(url).await;
    }
    let manifest = first_manifest(&app.root).await;
    assert_eq!(
        manifest["browserHash"].as_str().unwrap(),
        url_version(&first[0]),
        "the commit kept the planned version (a contradicted plan bumps it): {manifest:#}"
    );
    assert_eq!(
        dep_urls(&server.text("/main.js").await),
        first,
        "the committed URLs are the ones served before the commit"
    );
}

/// An app whose plugin transform adds an import (`late-esm`) the scan cannot
/// see, served without waiting for the file watcher (by then any rerun has
/// long committed too, and the first commit is unobservable). Returns the
/// app, the server, main.js's dep URLs and the manifest the first commit
/// wrote, read as soon as the planned dep's held request returns.
async fn missed_dep_app(
    optimize_deps: &str,
) -> (App, crate::Server, Vec<String>, serde_json::Value) {
    let mut files = vec![
        (
            "package.json".to_string(),
            r#"{"name":"hold-app","private":true,"type":"module"}"#.to_string(),
        ),
        (
            "vite.config.js".into(),
            format!(
                r#"export default {{
  optimizeDeps: {optimize_deps},
  plugins: [{{
    name: "inject-import",
    transform(code, id) {{
      if (id.endsWith("/main.js")) return 'import {{ hi as late }} from "late-esm";\nwindow.__L = late();\n' + code;
    }},
  }}],
}};
"#
            ),
        ),
        ("index.html".into(), INDEX.into()),
        (
            "main.js".into(),
            "import { hi } from \"plain-esm\";\nwindow.__R = hi();\n".into(),
        ),
    ];
    files.extend(plain_dep("plain-esm", false));
    files.extend(plain_dep("late-esm", false));
    let app = app_with(&files);
    app.link_fixture("vite");
    let built = app.dev().build_app().await.expect("dev server builds");
    let server = crate::Server {
        built,
        root: app.root.clone(),
    };
    let urls = dep_urls(&server.text("/main.js").await);
    assert_eq!(urls.len(), 2, "both deps optimized: {urls:?}");
    // Only the planned dep: its request returns with the first commit.
    let planned = urls.iter().find(|u| u.contains("plain-esm")).unwrap();
    server.text(planned).await;
    let manifest = first_manifest(&app.root).await;
    (app, server, urls, manifest)
}

// Vite's holdUntilCrawlEnd (its default): the crawl finds the dep the scan
// missed while the first pre-bundle runs, so the commit waits for the crawl
// and bundles it too, instead of committing and paying a rerun.
#[tokio::test(flavor = "multi_thread")]
async fn a_dep_the_scan_missed_joins_the_first_pre_bundle() {
    crate::require_fixture!("vite");
    let (_app, server, first, manifest) = missed_dep_app("{}").await;
    assert_eq!(
        manifest["discovered"],
        serde_json::json!(["late-esm"]),
        "the missed dep joined the first commit: {manifest:#}"
    );
    assert!(
        manifest["metadata"]["late-esm"]["fileHash"]
            .as_str()
            .is_some_and(|h| !h.is_empty()),
        "and was bundled by it: {manifest:#}"
    );
    let planned = first.iter().find(|u| u.contains("plain-esm")).unwrap();
    assert_eq!(
        manifest["browserHash"].as_str().unwrap(),
        url_version(planned),
        "no reload: {manifest:#}"
    );
    assert_eq!(
        dep_urls(&server.text("/main.js").await),
        first,
        "every URL keeps the version it was served under"
    );
}

// `holdUntilCrawlEnd: false`: Vite releases the first pre-bundle as soon as
// it is done; the missed dep is left to the rerun.
#[tokio::test(flavor = "multi_thread")]
async fn without_the_hold_the_first_commit_leaves_the_missed_dep_to_a_rerun() {
    crate::require_fixture!("vite");
    let (_app, server, _, manifest) = missed_dep_app("{ holdUntilCrawlEnd: false }").await;
    assert_eq!(
        manifest["discovered"],
        serde_json::json!([]),
        "the first commit did not wait: {manifest:#}"
    );
    assert!(
        manifest["metadata"].get("late-esm").is_none(),
        "{manifest:#}"
    );
    // The rerun commits it.
    let late = dep_urls(&server.text("/main.js").await)
        .into_iter()
        .find(|u| u.contains("late-esm"))
        .unwrap();
    assert_eq!(server.get(&late).await.status, 200);
}

fn plain_esm_app() -> App {
    let mut files = vec![
        (
            "package.json".to_string(),
            r#"{"name":"session-app","private":true,"type":"module"}"#.to_string(),
        ),
        ("vite.config.js".into(), "export default {};\n".into()),
        ("index.html".into(), INDEX.into()),
        (
            "main.js".into(),
            "import { hi } from \"plain-esm\";\nwindow.__R = hi();\n".into(),
        ),
    ];
    files.extend(plain_dep("plain-esm", false));
    let app = app_with(&files);
    app.link_fixture("vite");
    app
}

// Vite's optimizedDeps load: a versioned dep file gone from disk (a rerun or
// another session replaced the bundle) is the outdated 504 a reload gets
// past; only an unversioned miss is a 404.
#[tokio::test(flavor = "multi_thread")]
async fn a_vanished_versioned_dep_file_is_outdated_not_missing() {
    crate::require_fixture!("vite");
    let app = plain_esm_app();
    let server = app.serve().await;
    let url = dep_urls(&server.text("/main.js").await)
        .into_iter()
        .find(|u| u.contains("plain-esm"))
        .unwrap();
    server.text(&url).await;
    let file = url
        .trim_start_matches("/@oj-deps/")
        .split('?')
        .next()
        .unwrap();
    std::fs::remove_file(oj_cache::cache_root(&app.root).join("deps").join(file)).unwrap();
    assert_eq!(server.get(&url).await.status, 504, "versioned: {url}");
    assert_eq!(
        server.get(&format!("/@oj-deps/{file}")).await.status,
        404,
        "unversioned"
    );
}

// Vite's sessionTimestamp: dep URLs are cached immutable, so a cold session
// (a restart after the deps cache was cleared) must not serve its bundle
// under a URL an earlier session already handed out.
#[tokio::test(flavor = "multi_thread")]
async fn a_cold_session_never_reuses_an_earlier_sessions_dep_urls() {
    crate::require_fixture!("vite");
    let app = plain_esm_app();
    let first = {
        let server = app.serve().await;
        let urls = dep_urls(&server.text("/main.js").await);
        for url in &urls {
            server.text(url).await;
        }
        urls
    };
    std::fs::remove_dir_all(oj_cache::cache_root(&app.root).join("deps")).unwrap();
    let server = app.serve().await;
    let second = dep_urls(&server.text("/main.js").await);
    assert_eq!(first.len(), second.len(), "{first:?} vs {second:?}");
    for (a, b) in first.iter().zip(&second) {
        assert_ne!(a, b, "a cold session minted a URL an earlier one served");
        server.text(b).await;
    }
}

// Vite's cached _metadata.json: a warm session reuses the persisted versions,
// so every URL the first session served (and the browser cached immutable)
// still serves the same bundle.
#[tokio::test(flavor = "multi_thread")]
async fn a_warm_session_keeps_every_dep_url() {
    crate::require_fixture!("vite");
    let app = plain_esm_app();
    let first = {
        let server = app.serve().await;
        let urls = dep_urls(&server.text("/main.js").await);
        for url in &urls {
            server.text(url).await;
        }
        first_manifest(&app.root).await;
        urls
    };
    assert!(!first.is_empty());
    let server = app.serve().await;
    let second = dep_urls(&server.text("/main.js").await);
    assert_eq!(first, second, "a warm session moved the dep URLs");
    for url in &second {
        assert_eq!(server.get(url).await.status, 200, "{url}");
    }
}

/// The `/@oj-deps/` modulepreload hrefs oj's dev HTML carries.
fn dep_preloads(html: &str) -> Vec<String> {
    Regex::new(r#"<link rel="modulepreload" href="(/@oj-deps/[^"]+)""#)
        .unwrap()
        .captures_iter(html)
        .map(|c| c[1].to_string())
        .collect()
}

/// `a-esm` and `b-esm` share `shared-esm` (a chunk of the first pre-bundle);
/// `late-esm` is imported only by `extra.js`, which no entry reaches, so the
/// scan misses it and a request for `extra.js` discovers it at serve time.
fn preload_app() -> App {
    let dep = |name: &str, body: &str| {
        [
            (
                format!("node_modules/{name}/package.json"),
                format!(
                    r#"{{"name":"{name}","version":"1.0.0","type":"module","main":"index.js"}}"#
                ),
            ),
            (format!("node_modules/{name}/index.js"), body.to_string()),
        ]
    };
    let mut files = vec![
        (
            "package.json".to_string(),
            r#"{"name":"preload-app","private":true,"type":"module"}"#.to_string(),
        ),
        ("vite.config.js".into(), "export default {};\n".into()),
        ("index.html".into(), INDEX.into()),
        (
            "main.js".into(),
            "import { a } from \"a-esm\";\nimport { b } from \"b-esm\";\nwindow.__R = a() + b();\n"
                .into(),
        ),
        (
            "extra.js".into(),
            "import { late } from \"late-esm\";\nwindow.__L = late();\n".into(),
        ),
    ];
    files.extend(dep("shared-esm", "export const shared = () => \"s\";\n"));
    for name in ["a-esm", "b-esm", "late-esm"] {
        files.extend(dep(
            name,
            "import { shared } from \"shared-esm\";\nexport const a = shared, b = shared, late = shared;\n",
        ));
    }
    let app = app_with(&files);
    app.link_fixture("vite");
    app
}

/// Discovers `late-esm` through `extra.js` and waits for its rerun to commit
/// (the dep route holds a pending dep's request until then).
async fn discover_late(server: &crate::Server) -> String {
    let late = dep_urls(&server.text("/extra.js").await)
        .into_iter()
        .find(|u| u.contains("late-esm"))
        .expect("extra.js imports the late dep through /@oj-deps");
    assert_eq!(server.get(&late).await.status, 200, "{late}");
    late
}

/// Every dep preload in the page is a URL an import names (so preload and
/// import share one cache entry) and serves 200, never the outdated 504.
async fn assert_preloads_match_imports(server: &crate::Server, imports: &[String]) {
    let preloads = dep_preloads(&server.text("/").await);
    for url in imports {
        assert!(
            preloads.contains(url),
            "no preload names the import {url}: {preloads:?}"
        );
    }
    for url in &preloads {
        // A chunk preloads bare, as dep files import it.
        assert!(
            imports.contains(url) || !url.contains("?v="),
            "the preload {url} is no URL an import names: {imports:?}"
        );
        assert_eq!(server.get(url).await.status, 200, "preload {url}");
    }
}

// Vite's resolve gives each optimized entry its own browserHash
// (optimizedDepInfoFromFile), and a rerun that needs no reload keeps the
// committed entries' versions: after a discovery, entries carry different
// versions, so a preload stamped with the session's base version names a URL
// no import uses, and a discovered dep's preload is the outdated 504.
#[tokio::test(flavor = "multi_thread")]
async fn a_discovered_dep_preloads_under_its_own_version() {
    crate::require_fixture!("vite");
    let app = preload_app();
    let server = app.serve().await;
    let first = dep_urls(&server.text("/main.js").await);
    assert_eq!(first.len(), 2, "{first:?}");
    first_manifest(&app.root).await;
    let late = discover_late(&server).await;
    assert_ne!(
        url_version(&late),
        url_version(&first[0]),
        "the discovered dep carries the discovered set's version"
    );
    assert_eq!(
        dep_urls(&server.text("/main.js").await),
        first,
        "no reload: the committed entries keep their versions"
    );
    let mut imports = first;
    imports.push(late);
    assert_preloads_match_imports(&server, &imports).await;
}

// The same on a warm boot: the manifest restores each entry's own version,
// a discovered dep's included, before any request is served.
#[tokio::test(flavor = "multi_thread")]
async fn a_warm_session_preloads_every_entry_under_its_import_url() {
    crate::require_fixture!("vite");
    let app = preload_app();
    {
        let server = app.serve().await;
        server.text("/main.js").await;
        first_manifest(&app.root).await;
        discover_late(&server).await;
    }
    let server = app.serve().await;
    let mut imports = dep_urls(&server.text("/main.js").await);
    imports.extend(dep_urls(&server.text("/extra.js").await));
    assert_eq!(imports.len(), 3, "{imports:?}");
    assert_preloads_match_imports(&server, &imports).await;
}