Skip to content
Myr

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

3 files+47 added−6 removed

added:src/middleware/etag/digest.test.ts+21 added
modified:src/middleware/etag/digest.ts+1 added-1 removed
modified:src/middleware/etag/index.test.ts+25 added-5 removed
1import { Hono } from '../../hono'2import { RETAINED_304_HEADERS, etag } from '.'34const 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}1112describe('Etag Middleware', () => {13 it('Should return etag header', async () => {14 const app = new Hono()15 app.use('/etag/*', etag())
124 unmodified lines
140141 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 line
155 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 })167168 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 })174175 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)))179180 const res = await app.request('http://localhost/etag')181 expect(res.headers.get('ETag')).toBe('"37ef77696fc255bf53b4cdd014b223676f2dc8bb"')182 })183184 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