Skip to content

fix(WebSocketShard): buffer native zlib decompression payload #10416

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

Merged
merged 4 commits into from
Aug 15, 2024
Merged
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
33 changes: 30 additions & 3 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {

private zLibSyncInflate: ZlibSync.Inflate | null = null;

/**
* @privateRemarks
*
* Used only for native zlib inflate, zlib-sync buffering is handled by the library itself.
*/
private inflateBuffer: Buffer[] = [];

private readonly textDecoder = new TextDecoder();

private replayedEvents = 0;
Expand Down Expand Up @@ -198,11 +205,17 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
case CompressionMethod.ZlibNative: {
const zlib = await getNativeZlib();
if (zlib) {
this.inflateBuffer = [];

const inflate = zlib.createInflate({
chunkSize: 65_535,
flush: zlib.constants.Z_SYNC_FLUSH,
});

inflate.on('data', (chunk) => {
this.inflateBuffer.push(chunk);
});

inflate.on('error', (error) => {
this.emit(WebSocketShardEvents.Error, error);
});
Expand Down Expand Up @@ -627,14 +640,28 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
decompressable.at(-1) === 0xff;

if (this.nativeInflate) {
this.nativeInflate.write(decompressable, 'binary');
const doneWriting = new Promise<void>((resolve) => {
// eslint-disable-next-line promise/prefer-await-to-callbacks
this.nativeInflate!.write(decompressable, 'binary', (error) => {
if (error) {
this.emit(WebSocketShardEvents.Error, error);
}

resolve();
});
});

if (!flush) {
return null;
}

const [result] = await once(this.nativeInflate, 'data');
return this.parseInflateResult(result);
// This way we're ensuring the latest write has flushed and our buffer is ready
await doneWriting;

const result = this.parseInflateResult(Buffer.concat(this.inflateBuffer));
this.inflateBuffer = [];

return result;
} else if (this.zLibSyncInflate) {
const zLibSync = (await getZlibSync())!;
this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);
Expand Down
Loading