cache less here

it does add some inefficiency, but that can be dealt with later
This commit is contained in:
RiskyMH
2025-08-06 20:12:35 +10:00
parent 9ce27b92ec
commit 7281dfd0ed
2 changed files with 73 additions and 15 deletions

View File

@@ -203,22 +203,9 @@ pub const AnyRoute = union(enum) {
const bytes = response.body.value.InternalBlob.bytes.items;
if (bytes.len == @sizeOf(*HTMLBundle)) {
const html_bundle_ptr = @as(**HTMLBundle, @ptrCast(@alignCast(bytes.ptr))).*;
const entry = init_ctx.dedupe_html_bundle_map.getOrPut(html_bundle_ptr) catch bun.outOfMemory();
if (!entry.found_existing) {
entry.value_ptr.* = HTMLBundle.Route.init(html_bundle_ptr);
const route = entry.value_ptr.*;
const needs_custom = response.init.headers != null or response.statusCode() != 200;
if (response.init.headers) |headers| {
route.data.custom_headers = bun.http.Headers.from(headers, bun.default_allocator, .{}) catch bun.outOfMemory();
}
const status = response.statusCode();
if (status != 200) {
route.data.custom_status = status;
}
return .{ .html = route };
} else {
if (needs_custom) {
var route = HTMLBundle.Route.init(html_bundle_ptr);
if (response.init.headers) |headers| {
@@ -231,6 +218,15 @@ pub const AnyRoute = union(enum) {
}
return .{ .html = route };
} else {
const entry = init_ctx.dedupe_html_bundle_map.getOrPut(html_bundle_ptr) catch bun.outOfMemory();
if (!entry.found_existing) {
entry.value_ptr.* = HTMLBundle.Route.init(html_bundle_ptr);
return .{ .html = entry.value_ptr.* };
} else {
return .{ .html = entry.value_ptr.dupeRef() };
}
}
}
}

View File

@@ -99,6 +99,12 @@ const server = Bun.serve({
}
}),
"/home": new Response(html),
"/haha": new Response(html, {status: 400}),
"/index.html": html,
"/tea": {
GET: new Response(html, {status: 418}),
POST: () => new Response("Teapot!!!"),
},
"/hello": new Response(hello),
"/*": new Response(html, {
status: 404,
@@ -149,6 +155,62 @@ console.log(server.port);
expect(text).toMatch(/src="[^"]+\.js"/);
}
{
const response = await fetch(`http://localhost:${port}/index.html`);
expect(response.status).toBe(200);
expect(response.headers.get("X-Custom")).not.toBe("custom-value");
expect(response.headers.get("X-Test")).not.toBe("test-value");
expect(response.headers.get("Content-Type")).toBe("text/html;charset=utf-8");
const text = await response.text();
expect(text).toContain("Test Page");
expect(text).toContain("Hello from HTMLBundle");
expect(text).toMatch(/src="[^"]+\.js"/);
}
{
const response = await fetch(`http://localhost:${port}/haha`);
expect(response.status).toBe(400);
expect(response.headers.get("X-Custom")).not.toBe("custom-value");
expect(response.headers.get("X-Test")).not.toBe("test-value");
expect(response.headers.get("Content-Type")).toBe("text/html;charset=utf-8");
const text = await response.text();
expect(text).toContain("Test Page");
expect(text).toContain("Hello from HTMLBundle");
expect(text).toMatch(/src="[^"]+\.js"/);
}
{
const response = await fetch(`http://localhost:${port}/tea`, {
method: "GET",
});
expect(response.status).toBe(418);
expect(response.headers.get("X-Custom")).not.toBe("custom-value");
expect(response.headers.get("X-Test")).not.toBe("test-value");
expect(response.headers.get("Content-Type")).toBe("text/html;charset=utf-8");
const text = await response.text();
expect(text).toContain("Test Page");
expect(text).toContain("Hello from HTMLBundle");
expect(text).toMatch(/src="[^"]+\.js"/);
}
{
const response = await fetch(`http://localhost:${port}/tea`, {
method: "POST",
});
expect(response.status).toBe(200);
expect(response.headers.get("X-Custom")).not.toBe("custom-value");
expect(response.headers.get("X-Test")).not.toBe("test-value");
expect(response.headers.get("Content-Type")).toBe("text/plain;charset=utf-8");
const text = await response.text();
expect(text).toBe("Teapot!!!");
}
{
const response = await fetch(`http://localhost:${port}/hello`);
expect(response.status).toBe(200);