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
+709
-39
No files matched your search
+15
-1
@@ -78,6 +78,20 @@ async function main() {
|
||||
const readCookie = await mf.dispatchFetch('http://localhost/rt/cookie', { headers: { cookie: 'a=1; b=%41' } });
|
||||
assert.deepEqual(await readCookie.json(), { b: 'A' });
|
||||
|
||||
// CARD-15 (#114), T5/T11: handler ETag round-trip on workerd. Static is a boot error there (N/A).
|
||||
const tag1 = await mf.dispatchFetch('http://etag/rt/hello');
|
||||
await tag1.arrayBuffer();
|
||||
const etag = tag1.headers.get('etag');
|
||||
assert.match(String(etag), /^W\/"\w+-\w+"$/);
|
||||
const tag2 = await mf.dispatchFetch('http://etag/rt/hello', { headers: { 'if-none-match': String(etag) } });
|
||||
assert.equal(tag2.status, 304);
|
||||
assert.equal(await tag2.text(), '');
|
||||
assert.equal(tag2.headers.get('etag'), etag);
|
||||
assert.equal(tag2.headers.get('content-type'), null);
|
||||
const untagged = await mf.dispatchFetch('http://localhost/rt/hello');
|
||||
await untagged.arrayBuffer();
|
||||
assert.equal(untagged.headers.get('etag'), null, 'etag off: no ETag');
|
||||
|
||||
// Body limits (#105): the same statuses as app.fetch, Deno and Bun. workerd may drop a
|
||||
// chunked upload's content-length, so the streamed case is enforced by the read count alone.
|
||||
const LIMIT = 1000; // the route's maxBodyBytes, see worker.ts
|
||||
@@ -150,7 +164,7 @@ async function main() {
|
||||
assert.equal((await exchange(mf, 'http://localhost/rt/echo', ['a'.repeat(1_048_576)])).replies.length, 1);
|
||||
assert.equal((await exchange(mf, 'http://localhost/rt/echo', ['a'.repeat(1_048_577)])).close?.code, 1009);
|
||||
|
||||
console.log('edge (workerd via miniflare): HTTP 200 + cookies + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS');
|
||||
console.log('edge (workerd via miniflare): HTTP 200 + cookies + etag/304 + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS');
|
||||
} finally {
|
||||
await mf.dispose();
|
||||
}
|
||||
|
||||
+5
-2
@@ -66,9 +66,12 @@ class M {}
|
||||
const app = createApp({ modules: [M] });
|
||||
// #107: the same graph with tight ws limits, reached through the `limited` host.
|
||||
const limited = createApp({ modules: [M], limits: { maxWsMessageBytes: 1024, maxWsQueue: 4 } });
|
||||
const handlers = { default: edgeHandler(app), limited: edgeHandler(limited) };
|
||||
// CARD-15 (#114): the same graph with handler ETags on, reached through the `etag` host.
|
||||
const tagged = createApp({ modules: [M], etag: true });
|
||||
const handlers = { default: edgeHandler(app), limited: edgeHandler(limited), etag: edgeHandler(tagged) };
|
||||
const hosts: Record<string, typeof handlers.default> = { limited: handlers.limited, etag: handlers.etag };
|
||||
|
||||
export default {
|
||||
fetch: (request: Request, env?: unknown, ctx?: Parameters<typeof handlers.default>[2]) =>
|
||||
(new URL(request.url).hostname === 'limited' ? handlers.limited : handlers.default)(request, env, ctx),
|
||||
(hosts[new URL(request.url).hostname] ?? handlers.default)(request, env, ctx),
|
||||
};
|
||||
Reference in new issue
Block a user