Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxy): remove spread operator from Request and Response classes #3928

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

BarryThePenguin
Copy link
Contributor

Removes usage of the spread operator (...) on Request and Response classes

Follow-up from #3927

Copy link

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.29%. Comparing base (0c405b9) to head (b1af554).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3928      +/-   ##
==========================================
- Coverage   91.32%   91.29%   -0.04%     
==========================================
  Files         168      168              
  Lines       10689    10774      +85     
  Branches     3056     3101      +45     
==========================================
+ Hits         9762     9836      +74     
- Misses        926      937      +11     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@usualoma
Copy link
Member

Hi @BarryThePenguin. Thank you.

Please do not modify the proxyInit object received as an argument, as follows.

diff --git a/src/helper/proxy/index.ts b/src/helper/proxy/index.ts
index 4c010a2d..93a7543b 100644
--- a/src/helper/proxy/index.ts
+++ b/src/helper/proxy/index.ts
@@ -49,13 +49,13 @@ const buildRequestInitFromRequest = (
   }
 }
 
-const preprocessRequestInit = (requestInit: RequestInit): RequestInit => {
+const preprocessRequestInit = (requestInit: Readonly<ProxyRequestInit>): RequestInit => {
   if (
     !requestInit.headers ||
     Array.isArray(requestInit.headers) ||
     requestInit.headers instanceof Headers
   ) {
-    return requestInit
+    return requestInit as RequestInit
   }
 
   const headers = new Headers()
@@ -67,8 +67,10 @@ const preprocessRequestInit = (requestInit: RequestInit): RequestInit => {
       headers.set(key, value)
     }
   }
-  requestInit.headers = headers
-  return requestInit
+  return {
+    ...requestInit,
+    headers,
+  }
 }
 
 /**
@@ -111,7 +113,7 @@ const preprocessRequestInit = (requestInit: RequestInit): RequestInit => {
 export const proxy: ProxyFetch = async (input, proxyInit = {}) => {
   const req = new Request(input, {
     ...buildRequestInitFromRequest(proxyInit.raw),
-    ...preprocessRequestInit(proxyInit as RequestInit),
+    ...preprocessRequestInit(proxyInit),
   })
   req.headers.delete('accept-encoding')
 

Also, if possible, since the changes to src/helper/proxy/index.ts are different from the following, it would be helpful if you could include only one purpose in one PR.

Removes usage of the spread operator (...) on Request and Response classes

@BarryThePenguin
Copy link
Contributor Author

Also, if possible, since the changes to src/helper/proxy/index.ts are different from the following, it would be helpful if you could include only one purpose in one PR.

I was trying to avoid using the spread operator on proxyInit

const { raw, ...requestInit } = proxyInit ?? {}

As the current API for proxy makes it very easy to pass Request as proxyInit, for example

app.get('/proxy/:path', (c) => {
  return proxy(`http://${originServer}/${c.req.param('path')}`, c.req.raw)
})

Though I'm not sure if this is intentional or not. I'll revert the changes to src/helper/proxy/index.ts

@usualoma
Copy link
Member

Hi @BarryThePenguin. Thank you for getting back to me.

I see, as you say, it would be better if the following code (although it is not the recommended way to use it) processed c.req.raw as RequestInit. Or, if it is not processed, it would be better if it caused an error.

app.get(/proxy/:path’, (c) => {
  return proxy(`http://${originServer}/${c.req.param(‘path’)}`, c.req.raw)
})

Let's discuss it in another PR.

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@BarryThePenguin

Thank you. Looks good. I'll merge this now.

@yusukebe yusukebe merged commit df33970 into honojs:main Feb 18, 2025
16 checks passed
@BarryThePenguin BarryThePenguin deleted the fix/request-response-spread branch February 18, 2025 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants