Skip to content

Commit

Permalink
fix: websocket redirects (#6672)
Browse files Browse the repository at this point in the history
* fix: fix websocket redirects
  • Loading branch information
davbree authored May 29, 2024
1 parent 1ffa58c commit 4c27f1c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ const initializeProxy = async function ({
return proxy.web(req, res, options)
},
// @ts-expect-error TS(7006) FIXME: Parameter 'req' implicitly has an 'any' type.
ws: (req, socket, head) => proxy.ws(req, socket, head),
ws: (req, socket, head, options) => proxy.ws(req, socket, head, options),
}

return handlers
Expand Down Expand Up @@ -876,8 +876,15 @@ export const startProxy = async function ({
const primaryServer = settings.https
? https.createServer({ cert: settings.https.cert, key: settings.https.key }, onRequestWithOptions)
: http.createServer(onRequestWithOptions)
const onUpgrade = function onUpgrade(req: http.IncomingMessage, socket: Duplex, head: Buffer) {
proxy.ws(req, socket, head)
const onUpgrade = async function onUpgrade(req: http.IncomingMessage, socket: Duplex, head: Buffer) {
const match = await rewriter(req)
if (match && !match.force404 && isExternal(match)) {
const reqUrl = reqToURL(req, req.url)
const dest = new URL(match.to, `${reqUrl.protocol}//${reqUrl.host}`)
const destURL = stripOrigin(dest)
return proxy.ws(req, socket, head, { target: dest.origin, changeOrigin: true, pathRewrite: () => destURL })
}
return proxy.ws(req, socket, head, {})
}

primaryServer.on('upgrade', onUpgrade)
Expand Down

2 comments on commit 4c27f1c

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

  • Dependency count: 1,235
  • Package size: 295 MB
  • Number of ts-expect-error directives: 989

@github-actions
Copy link

Choose a reason for hiding this comment

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

📊 Benchmark results

  • Dependency count: 1,235
  • Package size: 295 MB
  • Number of ts-expect-error directives: 989

Please sign in to comment.