feat(http): ETag and 304 for static files and buffered responses (CARD-15, #114)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 27s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 14s

Conditional GETs were never answered: every revalidation re-downloaded
the file or response.

- Static (D12, always on): a hit carries W/"<size>-<mtimeMs base36>",
  Last-Modified and Cache-Control from the stat the resolver already did.
  A matching If-None-Match, or If-Modified-Since when If-None-Match is
  absent, answers 304 before the file is read. StaticHit now carries the
  stat and a read() for the bytes, and HEAD no longer reads either.
- static: { dir?, cacheControl? } (rule 1a): Cache-Control defaults to
  no-cache so browsers revalidate rather than reuse a file for a lifetime
  guessed from Last-Modified. An unknown key or CR/LF throws at boot.
- createApp({ etag: true }) (off by default, boolean only): a buffered 200
  to GET/HEAD gets a weak tag from a 32-bit FNV-1a hash of its UTF-8 bytes
  plus its length, computed before HEAD blanks the body. A handler-set
  etag is kept and only compared. Weak comparison, lists split outside
  quotes, and * are supported.
- Every buffered 304 leaves dispatch bodiless, with its content-* headers
  dropped, so NotModified matches the ETag path on every adapter.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-23 00:02:49 -05:00
1 parent 47743fdf1f
commit fc1db2dbab
14 files changed
+709 -39

No files matched your search

+37
View File
@@ -0,0 +1,37 @@
// CARD-15 (#114) under the real Bun runtime: static 304 without reading the file (T1), and a
// handler ETag round-trip under `etag: true` (T5). Run with: npm run test:bun
import 'reflect-metadata';
import fs from 'node:fs';
import { test, expect, spyOn } from 'bun:test';
import { createApp } from '../../src/index.ts';
import { serveBun } from '../../src/bun.ts';
import { EtagModule, STATIC_DIR } from '../fixtures/etag-app.ts';
test('T1/T5 on Bun.serve: static and handler 304s, readFile once', async () => {
const readFile = spyOn(fs.promises, 'readFile');
const server = await serveBun(createApp({ modules: [EtagModule], etag: true, static: STATIC_DIR }), {
hostname: '127.0.0.1',
port: 0,
});
const base = `http://127.0.0.1:${server.port}`;
try {
const s1 = await fetch(`${base}/asset.txt`);
expect(await s1.text()).toBe('served');
const etag = s1.headers.get('etag')!;
expect(etag).toMatch(/^W\/"\d+-[0-9a-z]+"$/);
expect(s1.headers.get('cache-control')).toBe('no-cache');
const s2 = await fetch(`${base}/asset.txt`, { headers: { 'if-none-match': etag } });
expect([s2.status, await s2.text(), s2.headers.get('etag'), s2.headers.get('content-type')]).toEqual([304, '', etag, null]);
expect(readFile).toHaveBeenCalledTimes(1);
const h1 = await fetch(`${base}/h/json`);
await h1.text();
const tag = h1.headers.get('etag')!;
const h2 = await fetch(`${base}/h/json`, { headers: { 'if-none-match': tag } });
expect([h2.status, await h2.text(), h2.headers.get('etag')]).toEqual([304, '', tag]);
} finally {
readFile.mockRestore();
await server.close({ timeoutMs: 1000 });
}
});