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

handle ws error #5167

Merged
merged 2 commits into from
May 15, 2024
Merged

handle ws error #5167

merged 2 commits into from
May 15, 2024

Conversation

mifi
Copy link
Contributor

@mifi mifi commented May 15, 2024

to prevent companion from crashing with RangeError: Max payload size exceeded

to prevent companion from crashing with RangeError: Max payload size exceeded
Copy link
Contributor

github-actions bot commented May 15, 2024

Diff output files
diff --git a/packages/@uppy/companion/lib/server/socket.js b/packages/@uppy/companion/lib/server/socket.js
index fdc9706..0ec77d2 100644
--- a/packages/@uppy/companion/lib/server/socket.js
+++ b/packages/@uppy/companion/lib/server/socket.js
@@ -47,6 +47,18 @@ module.exports = (server) => {
     }
     emitter().emit(`connection:${token}`);
     emitter().on(token, send);
+    ws.on("error", (err) => {
+      // https://github.com/websockets/ws/issues/1543
+      // https://github.com/websockets/ws/blob/b73b11828d166e9692a9bffe9c01a7e93bab04a8/test/receiver.test.js#L936
+      if (
+        (err === null || err === void 0 ? void 0 : err.name) === "RangeError" && "code" in err
+        && err.code === "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
+      ) {
+        logger.error("WebSocket message too large", "websocket.error", shortenToken(token));
+      } else {
+        logger.error(err, "websocket.error", shortenToken(token));
+      }
+    });
     ws.on("message", (jsonData) => {
       const data = JSON.parse(jsonData.toString());
       // whitelist triggered actions

Co-authored-by: Antoine du Hamel <antoine@transloadit.com>
ws.on('error', (err) => {
// https://github.com/websockets/ws/issues/1543
// https://github.com/websockets/ws/blob/b73b11828d166e9692a9bffe9c01a7e93bab04a8/test/receiver.test.js#L936
if (err?.name === 'RangeError' && 'code' in err && err.code === 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH') {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check for 'code' in err?

Suggested change
if (err?.name === 'RangeError' && 'code' in err && err.code === 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH') {
if (err?.name === 'RangeError' && err.code === 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH') {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes or else we get a ts error

@aduh95 aduh95 merged commit 1ac7f9c into main May 15, 2024
20 checks passed
@aduh95 aduh95 deleted the ws-error-handle branch May 15, 2024 13:20
@github-actions github-actions bot mentioned this pull request May 22, 2024
github-actions bot added a commit that referenced this pull request May 22, 2024
| Package         | Version | Package         | Version |
| --------------- | ------- | --------------- | ------- |
| @uppy/companion |  4.13.3 | @uppy/tus       |   3.5.5 |
| @uppy/svelte    |   3.1.5 | uppy            |  3.25.4 |

- @uppy/svelte: do not attempt removing plugin before it's created (Antoine du Hamel / #5186)
- docs: Update `facebook.mdx` (Evgenia Karunus)
- @uppy/tus: fix no headers passed to companion if argument is a function (netdown / #5182)
- @uppy/companion: fix google drive gsuite export large size (Milan Nakum / #5144)
- meta: Improve provider docs: Box & Zoom (Evgenia Karunus / #5166)
- meta: add MDX file to `lint-staged` list (Antoine du Hamel / #5174)
- @uppy/companion: handle ws `'error'` event (Mikael Finstad / #5167)
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.

None yet

2 participants