fix(etag): copy pending stream bytes (#5239)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent 4eb022d · Browse files · 3 files·+47 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+47 added−6 removed
- tests:+46 added−5 removed
- other:+1 added−1 removed
Provenance
- Commit
7195c24- Tree
24a37ad- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
4eb022d
3 files changed+47 added−6 removed
added:src/middleware/etag/digest.test.ts+21 added
123456789101112131415161718192021import { generateDigest } from './digest'
describe('generateDigest', () => { it('Should copy pending bytes from a stream that reuses its buffer', async () => { const scratch = new Uint8Array(4) let pullCount = 0 const stream = new ReadableStream({ pull(controller) { scratch.fill(pullCount === 0 ? 0x41 : 0x42) controller.enqueue(scratch) if (++pullCount === 2) { controller.close() } }, })
const digest = await generateDigest(stream, (body) => crypto.subtle.digest('SHA-1', body))
expect(digest).toBe('7cd188ef3a9ea7fa0ee9c62c168709695460f5c0') })})modified:src/middleware/etag/digest.ts+1 added-1 removed
49 unmodified lines5051525354545556575837 unmodified lines49 unmodified lines
const requiredLength = chunkLength + remaining if (requiredLength < CHUNK_SIZE) { if (!chunk) { chunk = value.subarray(offset) chunk = value.slice(offset) } else { if (chunk.byteLength < requiredLength) { const nextChunk = new Uint8Array<ArrayBuffer>( new ArrayBuffer(Math.min(CHUNK_SIZE, Math.max(requiredLength, chunk.byteLength * 2)))37 unmodified linesmodified:src/middleware/etag/index.test.ts+25 added-5 removed
1 import { Hono } from '../../hono'2 import { RETAINED_304_HEADERS, etag } from '.'3 4+const createPatternedBody = (length: number) => {5+ const body = new Uint8Array(length)6+ for (let i = 0; i < body.byteLength; i++) {7+ body[i] = i % 2568+ }9+ return body10+}11+12 describe('Etag Middleware', () => {13 it('Should return etag header', async () => {14 const app = new Hono()15 app.use('/etag/*', etag())124 unmodified lines140 141 it('Should return the same etag regardless of ReadableStream chunk boundaries', async () => {142 const app = new Hono()143 app.use('/etag/*', etag())144+ const body = createPatternedBody(1_000_000)145 app.get('/etag/rs1', (c) => {146 return c.body(147 new ReadableStream({148 start(controller) {140- controller.enqueue(new Uint8Array(1_000_000))149+ controller.enqueue(body)150 controller.close()151 },152 })153 )1 unmodified line155 app.get('/etag/rs2', (c) => {156 return c.body(157 new ReadableStream({158 start(controller) {150- controller.enqueue(new Uint8Array(1))151- controller.enqueue(new Uint8Array(32_768))152- controller.enqueue(new Uint8Array(967_231))159+ controller.enqueue(body.slice(0, 1))160+ controller.enqueue(body.slice(1, 32_769))161+ controller.enqueue(body.slice(32_769))162 controller.close()163 },164 })165 )166 })167 168 const res1 = await app.request('http://localhost/etag/rs1')169 const res2 = await app.request('http://localhost/etag/rs2')170+ const expected = '"550563e0460b33a3540278a8446a70bb96eb5172"'171+ expect(res1.headers.get('ETag')).toBe(expected)161- expect(res2.headers.get('ETag')).toBe(res1.headers.get('ETag'))172+ expect(res2.headers.get('ETag')).toBe(expected)173 })174 175+ it('Should preserve the SHA-1 digest for bodies up to 256 KiB', async () => {176+ const app = new Hono()177+ app.use('/etag/*', etag())178+ app.get('/etag', (c) => c.body(createPatternedBody(256 * 1024)))179+180+ const res = await app.request('http://localhost/etag')181+ expect(res.headers.get('ETag')).toBe('"37ef77696fc255bf53b4cdd014b223676f2dc8bb"')182+ })183+184 it('Should not return etag header when the stream is empty', async () => {185 const app = new Hono()186 app.use('/etag/*', etag())187 app.get('/etag/abc', (c) => {277 unmodified lines