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

* chore(WebSocketShard): improve zlib-sync errors

* chore(WebSocketShard): move zlib-sync error numbers to constants file

* chore(WebSocketShard): move enum, zlib-sync is lazily loaded

---------

Co-authored-by: Almeida <github@almeidx.dev>
This commit is contained in:
LJ
2025-07-02 16:18:11 -07:00
committed by GitHub
parent 02fbb706aa
commit 4dbeed933b

View File

@@ -668,10 +668,23 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH); this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);
if (this.zLibSyncInflate.err) { if (this.zLibSyncInflate.err) {
this.emit( // Must be here because zlib-sync is lazily loaded
WebSocketShardEvents.Error, const ZlibErrorCodes = {
new Error(`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`), [zLibSync.Z_NEED_DICT]: 'Z_NEED_DICT',
); [zLibSync.Z_STREAM_END]: 'Z_STREAM_END',
[zLibSync.Z_ERRNO]: 'Z_ERRNO',
[zLibSync.Z_STREAM_ERROR]: 'Z_STREAM_ERROR',
[zLibSync.Z_DATA_ERROR]: 'Z_DATA_ERROR',
[zLibSync.Z_MEM_ERROR]: 'Z_MEM_ERROR',
[zLibSync.Z_BUF_ERROR]: 'Z_BUF_ERROR',
[zLibSync.Z_VERSION_ERROR]: 'Z_VERSION_ERROR',
} as const satisfies Record<number, string>;
// 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) { if (!flush) {