Skip to content

Commit

Permalink
fix: do not swallow user exceptions
Browse files Browse the repository at this point in the history
Following [1], any exception in a user-provided event listener would
get caught in the try...catch of the decoder and result in the
reconnection of the socket.

[1]: c597023

Related:

- #1551
- #1554
- #1557
  • Loading branch information
darrachequesne committed Oct 13, 2022
1 parent 1098618 commit 2403b88
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/manager.ts
Expand Up @@ -2,6 +2,7 @@ import {
Socket as Engine,
SocketOptions as EngineOptions,
installTimerFunctions,
nextTick,
} from "engine.io-client";
import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
import * as parser from "socket.io-parser";
Expand Down Expand Up @@ -427,7 +428,7 @@ export class Manager<
try {
this.decoder.add(data);
} catch (e) {
this.onclose("parse error");
this.onclose("parse error", e as Error);
}
}

Expand All @@ -437,7 +438,10 @@ export class Manager<
* @private
*/
private ondecoded(packet): void {
this.emitReserved("packet", packet);
// the nextTick call prevents an exception in a user-provided event listener from triggering a disconnection due to a "parse error"
nextTick(() => {
this.emitReserved("packet", packet);
}, this.setTimeoutFn);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/socket.ts
Expand Up @@ -725,5 +725,6 @@ export namespace Socket {
| "io client disconnect"
| "ping timeout"
| "transport close"
| "transport error";
| "transport error"
| "parse error";
}
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@socket.io/component-emitter": "~3.1.0",
"debug": "~4.3.2",
"engine.io-client": "~6.2.1",
"engine.io-client": "~6.2.3",
"socket.io-parser": "~4.2.0"
},
"devDependencies": {
Expand Down

0 comments on commit 2403b88

Please sign in to comment.