fix(etag): stabilize digest across stream chunks (#5205)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(etag): stabilize digest across stream chunks * perf(etag): optimize fixed-size digest chunks
authored by Taku Amano · committed by GitHub · parent 7075369 · Browse files · 2 files·+84 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+84 added−1 removed
- tests:+31 added−0 removed
- other:+53 added−1 removed
Provenance
- Commit
26de731- Tree
be146fa- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
7075369
2 files changed+84 added−1 removed
modified:src/middleware/etag/digest.ts+53 added-1 removed
11 unmodified lines1213141516171819202122232425262728293031323334353637383940334142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990914 unmodified lines11 unmodified lines merged.set(buffer2, buffer1.byteLength) return merged}
const CHUNK_SIZE = 256 * 1024
export const generateDigest = async ( stream: ReadableStream<Uint8Array<ArrayBuffer>> | null, generator: (body: Uint8Array<ArrayBuffer>) => ArrayBuffer | Promise<ArrayBuffer>): Promise<string | null> => { if (!stream) { return null }
let result: ArrayBuffer | undefined = undefined let chunk: Uint8Array<ArrayBuffer> | undefined let chunkLength = 0
const digest = async (body: Uint8Array<ArrayBuffer>) => { result = await generator(mergeBuffers(result, body)) }
const reader = stream.getReader() for (;;) { const { value, done } = await reader.read() if (done) { break }
result = await generator(mergeBuffers(result, value)) let offset = 0 while (offset < value.byteLength) { const remaining = value.byteLength - offset
if (chunkLength === 0 && remaining >= CHUNK_SIZE) { await digest(value.subarray(offset, offset + CHUNK_SIZE)) offset += CHUNK_SIZE continue }
const requiredLength = chunkLength + remaining if (requiredLength < CHUNK_SIZE) { if (!chunk) { chunk = value.subarray(offset) } else { if (chunk.byteLength < requiredLength) { const nextChunk = new Uint8Array<ArrayBuffer>( new ArrayBuffer(Math.min(CHUNK_SIZE, Math.max(requiredLength, chunk.byteLength * 2))) ) nextChunk.set(chunk.subarray(0, chunkLength)) chunk = nextChunk } chunk.set(value.subarray(offset), chunkLength) } chunkLength = requiredLength break }
const length = CHUNK_SIZE - chunkLength if (chunk?.byteLength !== CHUNK_SIZE) { const nextChunk = new Uint8Array<ArrayBuffer>(new ArrayBuffer(CHUNK_SIZE)) if (chunk) { nextChunk.set(chunk.subarray(0, chunkLength)) } chunk = nextChunk } chunk.set(value.subarray(offset, offset + length), chunkLength) await digest(chunk) chunkLength = 0 offset += length } }
if (chunk && chunkLength > 0) { await digest(chunk.subarray(0, chunkLength)) }
if (!result) { return null }
4 unmodified linesmodified:src/middleware/etag/index.test.ts+31 added
128 unmodified lines129 res = await app.request('http://localhost/etag/rs2')130 expect(res.headers.get('ETag')).not.toBe(hash)131 })132 133+ it('Should return the same etag regardless of ReadableStream chunk boundaries', async () => {134+ const app = new Hono()135+ app.use('/etag/*', etag())136+ app.get('/etag/rs1', (c) => {137+ return c.body(138+ new ReadableStream({139+ start(controller) {140+ controller.enqueue(new Uint8Array(1_000_000))141+ controller.close()142+ },143+ })144+ )145+ })146+ app.get('/etag/rs2', (c) => {147+ return c.body(148+ new ReadableStream({149+ start(controller) {150+ controller.enqueue(new Uint8Array(1))151+ controller.enqueue(new Uint8Array(32_768))152+ controller.enqueue(new Uint8Array(967_231))153+ controller.close()154+ },155+ })156+ )157+ })158+159+ const res1 = await app.request('http://localhost/etag/rs1')160+ const res2 = await app.request('http://localhost/etag/rs2')161+ expect(res2.headers.get('ETag')).toBe(res1.headers.get('ETag'))162+ })163+164 it('Should not return etag header when the stream is empty', async () => {165 const app = new Hono()166 app.use('/etag/*', etag())167 app.get('/etag/abc', (c) => {277 unmodified lines