Skip to content

Commit 7195c24

Browse files
authored
fix(etag): copy pending stream bytes (#5239)
1 parent 4eb022d commit 7195c24

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

‎src/middleware/etag/digest.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { generateDigest } from './digest'
2+
3+
describe('generateDigest', () => {
4+
it('Should copy pending bytes from a stream that reuses its buffer', async () => {
5+
const scratch = new Uint8Array(4)
6+
let pullCount = 0
7+
const stream = new ReadableStream({
8+
pull(controller) {
9+
scratch.fill(pullCount === 0 ? 0x41 : 0x42)
10+
controller.enqueue(scratch)
11+
if (++pullCount === 2) {
12+
controller.close()
13+
}
14+
},
15+
})
16+
17+
const digest = await generateDigest(stream, (body) => crypto.subtle.digest('SHA-1', body))
18+
19+
expect(digest).toBe('7cd188ef3a9ea7fa0ee9c62c168709695460f5c0')
20+
})
21+
})

‎src/middleware/etag/digest.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const generateDigest = async (
5151
const requiredLength = chunkLength + remaining
5252
if (requiredLength < CHUNK_SIZE) {
5353
if (!chunk) {
54-
chunk = value.subarray(offset)
54+
chunk = value.slice(offset)
5555
} else {
5656
if (chunk.byteLength < requiredLength) {
5757
const nextChunk = new Uint8Array<ArrayBuffer>(

‎src/middleware/etag/index.test.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { Hono } from '../../hono'
22
import { RETAINED_304_HEADERS, etag } from '.'
33

4+
const createPatternedBody = (length: number) => {
5+
const body = new Uint8Array(length)
6+
for (let i = 0; i < body.byteLength; i++) {
7+
body[i] = i % 256
8+
}
9+
return body
10+
}
11+
412
describe('Etag Middleware', () => {
513
it('Should return etag header', async () => {
614
const app = new Hono()
@@ -133,11 +141,12 @@ describe('Etag Middleware', () => {
133141
it('Should return the same etag regardless of ReadableStream chunk boundaries', async () => {
134142
const app = new Hono()
135143
app.use('/etag/*', etag())
144+
const body = createPatternedBody(1_000_000)
136145
app.get('/etag/rs1', (c) => {
137146
return c.body(
138147
new ReadableStream({
139148
start(controller) {
140-
controller.enqueue(new Uint8Array(1_000_000))
149+
controller.enqueue(body)
141150
controller.close()
142151
},
143152
})
@@ -147,9 +156,9 @@ describe('Etag Middleware', () => {
147156
return c.body(
148157
new ReadableStream({
149158
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))
153162
controller.close()
154163
},
155164
})
@@ -158,7 +167,18 @@ describe('Etag Middleware', () => {
158167

159168
const res1 = await app.request('http://localhost/etag/rs1')
160169
const res2 = await app.request('http://localhost/etag/rs2')
161-
expect(res2.headers.get('ETag')).toBe(res1.headers.get('ETag'))
170+
const expected = '"550563e0460b33a3540278a8446a70bb96eb5172"'
171+
expect(res1.headers.get('ETag')).toBe(expected)
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"')
162182
})
163183

164184
it('Should not return etag header when the stream is empty', async () => {

0 commit comments

Comments
 (0)