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(websocket): handle errors in handleUpgrade #823

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

nwalters512
Copy link

@nwalters512 nwalters512 commented Sep 5, 2022

Description

When there is an error from user-provided code (pathFilter, rewritePath, or router), that error is now handled and exposed to the user via proxy.emit('error', ...).

error-response-plugin was also updated to handle the fact that it might receive a socket. If it does, it will call socket.destroy().

Motivation and Context

See #822 for a description of the issue being fixed. This PR closes #822.

How has this been tested?

I added a new test case. It fails on master, and works correctly with my new changes.

Types of changes

I'm split on whether or not this should be considered a breaking change. Folks could have written code assuming that the error handler would always get a Response as the third argument, whereas now it would get a Socket. That said, the TypeScript types from http-proxy do reflect that that could be a Socket, so it shouldn't come as too much of a surprise.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Comment on lines +9 to +11
function isSocketLike(obj: any): obj is Socket {
return obj && typeof obj.write === 'function' && !('writeHead' in obj);
}
Copy link
Author

Choose a reason for hiding this comment

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

We can't just check for instanceof Socket since per the documentation, the user may be able to provide a different socket implementation:

This event is guaranteed to be passed an instance of the <net.Socket> class, a subclass of <stream.Duplex>, unless the user specifies a socket type other than <net.Socket>.

@@ -97,15 +97,14 @@ describe('E2E WebSocket proxy', () => {

describe('with router and pathRewrite', () => {
beforeEach(() => {
// override
proxyServer = createApp(
const proxyMiddleware = createProxyMiddleware({
Copy link
Author

Choose a reason for hiding this comment

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

This was a bug with the existing tests: because it didn't reassign proxyMiddleware, the proxyMiddleware.upgrade access a few lines after this was reading from the wrong object.

@nwalters512
Copy link
Author

@chimurai 👋 can I do anything to help this along?

@chimurai
Copy link
Owner

Sorry for the late reply. I'll need some time to review 🙏

@nwalters512
Copy link
Author

@chimurai thanks!

@nwalters512
Copy link
Author

@chimurai is there any chance you'd be able to review soon? If so, I'll resolve the merge conflicts with master.

@nwalters512
Copy link
Author

@chimurai sorry to poke again, but I'd like to understand if there's a chance that this will be merged, or if I'll have to fork. Thanks for your work maintaining this package!

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.

Errors in prepareProxyRequest not handled for websockets
2 participants