Skip to content

Commit 4c27f1c

Browse files
authoredMay 29, 2024··
fix: websocket redirects (#6672)
* fix: fix websocket redirects
1 parent 1ffa58c commit 4c27f1c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed
 

‎src/utils/proxy.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ const initializeProxy = async function ({
661661
return proxy.web(req, res, options)
662662
},
663663
// @ts-expect-error TS(7006) FIXME: Parameter 'req' implicitly has an 'any' type.
664-
ws: (req, socket, head) => proxy.ws(req, socket, head),
664+
ws: (req, socket, head, options) => proxy.ws(req, socket, head, options),
665665
}
666666

667667
return handlers
@@ -876,8 +876,15 @@ export const startProxy = async function ({
876876
const primaryServer = settings.https
877877
? https.createServer({ cert: settings.https.cert, key: settings.https.key }, onRequestWithOptions)
878878
: http.createServer(onRequestWithOptions)
879-
const onUpgrade = function onUpgrade(req: http.IncomingMessage, socket: Duplex, head: Buffer) {
880-
proxy.ws(req, socket, head)
879+
const onUpgrade = async function onUpgrade(req: http.IncomingMessage, socket: Duplex, head: Buffer) {
880+
const match = await rewriter(req)
881+
if (match && !match.force404 && isExternal(match)) {
882+
const reqUrl = reqToURL(req, req.url)
883+
const dest = new URL(match.to, `${reqUrl.protocol}//${reqUrl.host}`)
884+
const destURL = stripOrigin(dest)
885+
return proxy.ws(req, socket, head, { target: dest.origin, changeOrigin: true, pathRewrite: () => destURL })
886+
}
887+
return proxy.ws(req, socket, head, {})
881888
}
882889

883890
primaryServer.on('upgrade', onUpgrade)

2 commit comments

Comments
 (2)

github-actions[bot] commented on May 29, 2024

@github-actions[bot]

📊 Benchmark results

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

github-actions[bot] commented on May 29, 2024

@github-actions[bot]

📊 Benchmark results

  • Dependency count: 1,235
  • Package size: 295 MB
  • Number of ts-expect-error directives: 989
Please sign in to comment.