hono icon indicating copy to clipboard operation
hono copied to clipboard

feat(etag): add `getBody` option

Open yusukebe opened this issue 7 months ago • 7 comments

Fixes #4031

This PR introduces a new option getBody for ETag middleware.

This will help customize the function to get the content to generate a hash in such a way on the AWS Lambda, which can't use c.res.clone().

The author should do the following, if applicable

  • [ ] Add tests
  • [ ] Run tests
  • [ ] bun run format:fix && bun run lint:fix to format the code
  • [ ] Add TSDoc/JSDoc to document the code

yusukebe avatar Jun 07 '25 02:06 yusukebe

Bundle size check

main (4a1dd5f) #4201 (1328e85) +/-
Bundle Size (B) 19,027B 19,027B 0B
Bundle Size (KB) 18.58K 18.58K 0K

Compiler Diagnostics (typescript-go)

main (4a1dd5f) #4201 (1328e85) +/-
Files 231 231 0
Lines 106,340 106,340 0
Identifiers 106,183 106,183 0
Symbols 371,551 371,551 0
Types 293,004 293,004 0
Instantiations 3,568,489 3,568,489 0
Memory used 229,864K 229,765K -99K
Memory allocs 10,003,691 10,003,732 41
Parse time 0.066s 0.067s 0.001s
Bind time 0.014s 0.015s 0.001s
Check time 1.343s 1.436s 0.093s
Emit time 0s 0s 0s
Total time 1.425s 1.52s 0.095s

Reported by octocov

github-actions[bot] avatar Jun 07 '25 02:06 github-actions[bot]

Bundle size check

main (4a1dd5f) #4201 (1328e85) +/-
Bundle Size (B) 19,027B 19,027B 0B
Bundle Size (KB) 18.58K 18.58K 0K

Compiler Diagnostics (tsc)

main (4a1dd5f) #4201 (1328e85) +/-
Files 261 261 0
Lines 116,441 116,441 0
Identifiers 114,443 114,443 0
Symbols 259,900 259,900 0
Types 162,567 162,567 0
Instantiations 3,039,293 3,039,293 0
Memory used 272,070K 272,549K 479K
I/O read 0.02s 0.02s 0s
I/O write 0s 0s 0s
Parse time 0.65s 0.69s 0.04s
Bind time 0.27s 0.29s 0.02s
Check time 3.74s 3.92s 0.18s
Emit time 0s 0s 0s
Total time 4.66s 4.9s 0.24s

Reported by octocov

github-actions[bot] avatar Jun 07 '25 02:06 github-actions[bot]

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 91.32%. Comparing base (4a1dd5f) to head (27bead1).

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #4201    +/-   ##
========================================
  Coverage   91.31%   91.32%            
========================================
  Files         168      168            
  Lines       10791    10796     +5     
  Branches     3186     3070   -116     
========================================
+ Hits         9854     9859     +5     
  Misses        936      936            
  Partials        1        1            

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Jun 07 '25 02:06 codecov[bot]

Bundle size check

main (4a1dd5f) #4201 (6baa461) +/-
Bundle Size (B) 19,027B 19,027B 0B
Bundle Size (KB) 18.58K 18.58K 0K

Compiler Diagnostics (typescript-go)

main (4a1dd5f) #4201 (6baa461) +/-
Files 231 231 0
Lines 106,340 106,340 0
Identifiers 106,183 106,183 0
Symbols 371,551 371,551 0
Types 293,004 293,004 0
Instantiations 3,568,489 3,568,489 0
Memory used 229,864K 229,718K -146K
Memory allocs 10,003,691 10,003,595 -96
Parse time 0.066s 0.071s 0.005s
Bind time 0.014s 0.041s 0.027s
Check time 1.343s 1.392s 0.049s
Emit time 0s 0s 0s
Total time 1.425s 1.508s 0.083s

Reported by octocov

github-actions[bot] avatar Jun 07 '25 02:06 github-actions[bot]

Bundle size check

main (4a1dd5f) #4201 (6baa461) +/-
Bundle Size (B) 19,027B 19,027B 0B
Bundle Size (KB) 18.58K 18.58K 0K

Compiler Diagnostics (tsc)

main (4a1dd5f) #4201 (6baa461) +/-
Files 261 261 0
Lines 116,441 116,441 0
Identifiers 114,443 114,443 0
Symbols 259,900 259,900 0
Types 162,567 162,567 0
Instantiations 3,039,293 3,039,293 0
Memory used 272,070K 272,866K 796K
I/O read 0.02s 0.02s 0s
I/O write 0s 0s 0s
Parse time 0.65s 0.65s 0s
Bind time 0.27s 0.27s 0s
Check time 3.74s 3.8s 0.06s
Emit time 0s 0s 0s
Total time 4.66s 4.72s 0.06s

Reported by octocov

github-actions[bot] avatar Jun 07 '25 02:06 github-actions[bot]

@cjnoname Can you review this?

yusukebe avatar Jun 07 '25 02:06 yusukebe

@cjnoname Can you review this?

Hey mate,

It doesn't work because once you consume the ArrayBuffer, the stream is no longer available—you'll need to recreate the request at that point.

This will work:

      let body: ArrayBuffer | ReadableStream<Uint8Array<ArrayBufferLike>> | null;
      if (options?.arrayBuffer) {
        body = await res.arrayBuffer();
        c.res = new Response(body, {
          status: res.status,
          statusText: res.statusText,
          headers: new Headers(res.headers)
        });
      } else {
        body = res.clone().body;
      }

cjnoname avatar Jun 07 '25 16:06 cjnoname