feat(http): ETag and 304 for static files and buffered responses (CARD-15, #114)
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:
14 files changed
+706
-37
No files matched your search
@@ -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 });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user