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: fixed error handling for proxied websockets #6673

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,16 +466,20 @@ const initializeProxy = async function ({
})

proxy.on('error', (_, req, res) => {
// @ts-expect-error TS(2339) FIXME: Property 'writeHead' does not exist on type 'Socke... Remove this comment to see the full error message
res.writeHead(500, {
'Content-Type': 'text/plain',
})
// res can be http.ServerResponse or net.Socket
if ('writeHead' in res) {
res.writeHead(500, {
'Content-Type': 'text/plain',
})

const message = isEdgeFunctionsRequest(req)
? 'There was an error with an Edge Function. Please check the terminal for more details.'
: 'Could not proxy request.'
const message = isEdgeFunctionsRequest(req)
? 'There was an error with an Edge Function. Please check the terminal for more details.'
: 'Could not proxy request.'

res.end(message)
res.end(message)
} else {
res.end()
}
})
proxy.on('proxyReq', (proxyReq, req) => {
const requestID = generateRequestID()
Expand Down Expand Up @@ -661,7 +665,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 +880,16 @@ 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)
console.log(`${NETLIFYDEVLOG} WS Redirect ${req.url} to ${destURL}`)
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