Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/runtime/bake/DevServer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1737,13 +1737,14 @@ fn on_js_request(dev: &mut DevServer, req: &mut Request, resp: AnyResponse) {
return;
}

let route_bundle_index =
route_bundle::Index::init(u32::try_from(id & 0xFFFFFFFF).expect("int cast"));
let generation: u32 = u32::try_from(id >> 32).expect("int cast");
let index = id as u32;
let generation = (id >> 32) as u32;

if route_bundle_index.get() as usize >= dev.route_bundles.len() {
// The URL can hold any u32 and `Index::init` asserts on `u32::MAX`: check the range first.
if index as usize >= dev.route_bundles.len() {
return not_found(resp);
}
let route_bundle_index = route_bundle::Index::init(index);

let route_bundle = &dev.route_bundles[route_bundle_index.get() as usize];
if route_bundle.client_script_generation != generation
Expand Down
26 changes: 26 additions & 0 deletions test/bake/dev/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ devTest("error report endpoint blanks stray non-text bytes in reported frames",
await dev.fetch("/").expect.toInclude("<h1>Frame Bytes</h1>");
},
});

devTest("client script route answers 404 when the route bundle index is out of range", {
files: {
"index.html": emptyHtmlFile({
scripts: ["/script.ts"],
body: "<h1>Client Script</h1>",
}),
"script.ts": `
console.log("client-script-marker");
`,
},
async test(dev) {
// `/_bun/client/{name}-{index}{generation}.js`: each hex group holds the four bytes of a u32.
const page = await dev.fetch("/").text();
const [script, generation] = page.match(/\/_bun\/client\/index-[0-9a-f]{8}([0-9a-f]{8})\.js/) ?? [];
expect(script).toBeString();
await dev.fetch(script).expect.toInclude("client-script-marker");

// `ffffffff` is the value that the index type reserves for "no index".
await dev.fetch(`/_bun/client/index-ffffffff${generation}.js`).expect404();
await dev.fetch(`/_bun/client/index-feffffff${generation}.js`).expect404();

await dev.fetch("/").expect.toInclude("<h1>Client Script</h1>");
},
});

devTest("editing a file imported from outside the project root hot-reloads", {
// The Windows watcher does not watch files outside the project directory.
skip: ["win32"],
Expand Down
Loading