Skip to content
Myr

fix(middleware/etag): generate etag hash value from all chunks (#3604)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(middleware/etag): generate etag hash value from all chunks * refactor(middleware/etag): returns immediately if digest cannot be generated. * refactor(utils/crypto): remove ReadableStream support * test(middleware/etag): add tests for empty stream and null body Co-authored-by: Yusuke Wada <yusuke@kamawada.com> ---------

authored by Taku Amano · committed by GitHub · parent 6b9fb24 · Browse files · 4 files+130 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

4 files+130 added−12 removed

  • tests:+82 added−0 removed
  • other:+48 added−12 removed

4 files+130 added−12 removed

added:src/middleware/etag/digest.ts+42 added
modified:src/middleware/etag/index.test.ts+82 added
63 unmodified lines
64 res = await app.request('http://localhost/etag/ui2')65 expect(res.headers.get('ETag')).not.toBe(hash)66 })6768 it('Should not be the same etag - ReadableStream', async () => {69 const app = new Hono()70 app.use('/etag/*', etag())71 app.get('/etag/rs1', (c) => {72 return c.body(73 new ReadableStream({74 start(controller) {75 controller.enqueue(new Uint8Array([1]))76 controller.enqueue(new Uint8Array([2]))77 controller.close()78 },79 })80 )81 })82 app.get('/etag/rs2', (c) => {83 return c.body(84 new ReadableStream({85 start(controller) {86 controller.enqueue(new Uint8Array([1]))87 controller.enqueue(new Uint8Array([3]))88 controller.close()89 },90 })91 )92 })9394 let res = await app.request('http://localhost/etag/rs1')95 const hash = res.headers.get('Etag')96 res = await app.request('http://localhost/etag/rs2')97 expect(res.headers.get('ETag')).not.toBe(hash)98 })99100 it('Should not return etag header when the stream is empty', async () => {101 const app = new Hono()102 app.use('/etag/*', etag())103 app.get('/etag/abc', (c) => {104 const stream = new ReadableStream({105 start(controller) {106 controller.close()107 },108 })109 return c.body(stream)110 })111 const res = await app.request('http://localhost/etag/abc')112 expect(res.status).toBe(200)113 expect(res.headers.get('ETag')).toBeNull()114 })115116 it('Should not return etag header when body is null', async () => {117 const app = new Hono()118 app.use('/etag/*', etag())119 app.get('/etag/abc', () => new Response(null, { status: 500 }))120 const res = await app.request('http://localhost/etag/abc')121 expect(res.status).toBe(500)122 expect(res.headers.get('ETag')).toBeNull()123 })124125 it('Should return etag header - weak', async () => {126 const app = new Hono()127 app.use('/etag/*', etag({ weak: true }))128 app.get('/etag/abc', (c) => {
106 unmodified lines
235 expect(res.headers.get('Cache-Control')).toBe(cacheControl)236 expect(res.headers.get('x-message-retain')).toBe(message)237 expect(res.headers.get('x-message')).toBeFalsy()238 })239240 describe('When crypto is not available', () => {241 let _crypto: Crypto | undefined242 beforeAll(() => {243 _crypto = globalThis.crypto244 Object.defineProperty(globalThis, 'crypto', {245 value: {},246 })247 })248249 afterAll(() => {250 Object.defineProperty(globalThis, 'crypto', {251 value: _crypto,252 })253 })254255 it('Should not generate etag', async () => {256 const app = new Hono()257 app.use('/etag/*', etag())258 app.get('/etag/no-digest', (c) => c.text('Hono is cool'))259 const res = await app.request('/etag/no-digest')260 expect(res.status).toBe(200)261 expect(res.headers.get('ETag')).toBeNull()262 })263 })264})
modified:src/middleware/etag/index.ts+5 added-2 removed
modified:src/utils/crypto.ts+1 added-10 removed