Skip to content

chore(WebSocketShard): improve zlib-sync errors #10808

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ export enum WebSocketShardDestroyRecovery {
Resume,
}

enum ZlibErrorCodes {
Z_NEED_DICT = 2,
Z_STREAM_END = 1,
Z_ERRNO = -1,
Z_STREAM_ERROR = -2,
Z_DATA_ERROR = -3,
Z_MEM_ERROR = -4,
Z_BUF_ERROR = -5,
Z_VERSION_ERROR = -6,
}
Comment on lines +62 to +71
Copy link
Member

Choose a reason for hiding this comment

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

Having these hardcoded here is not ideal.. Especially since zlib-sync exports each of these codes as constants

We could import them and construct this object using them
https://github.com/abalabahaha/zlib-sync/blob/7f1b404ba9e1bb9af1f47ca395f33e4fdfc2299c/src/zlib_sync.cc#L220-L228

(This would imply moving this object creation into the unpackMessage method as that's where zlib is being imported)


export interface WebSocketShardEventsMap {
[WebSocketShardEvents.Closed]: [code: number];
[WebSocketShardEvents.Debug]: [message: string];
Expand Down Expand Up @@ -667,10 +678,11 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);

if (this.zLibSyncInflate.err) {
this.emit(
WebSocketShardEvents.Error,
new Error(`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`),
);
// Try to match nodejs zlib errors as much as possible
const error: NodeJS.ErrnoException = new Error(this.zLibSyncInflate.msg ?? undefined);
error.errno = this.zLibSyncInflate.err;
error.code = ZlibErrorCodes[this.zLibSyncInflate.err];
this.emit(WebSocketShardEvents.Error, error);
}

if (!flush) {
Expand Down