Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions packages/ws/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum Encoding {
export enum CompressionMethod {
ZlibNative,
ZlibSync,
ZstdNative,
}

export const DefaultDeviceProperty = `@discordjs/ws [VI]{{inject}}[/VI]` as `@discordjs/ws ${string}`;
Expand All @@ -29,6 +30,7 @@ const getDefaultSessionStore = lazy(() => new Collection<number, SessionInfo | n
export const CompressionParameterMap = {
[CompressionMethod.ZlibNative]: 'zlib-stream',
[CompressionMethod.ZlibSync]: 'zlib-stream',
[CompressionMethod.ZstdNative]: 'zstd-stream',
} as const satisfies Record<CompressionMethod, string>;

/**
Expand Down
42 changes: 36 additions & 6 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {

this.nativeInflate = inflate;
} else {
console.warn('WebSocketShard: Compression is set to native but node:zlib is not available.');
console.warn('WebSocketShard: Compression is set to native zlib but node:zlib is not available.');
params.delete('compress');
}

Expand All @@ -238,6 +238,34 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {

break;
}

case CompressionMethod.ZstdNative: {
const zlib = await getNativeZlib();
if (zlib && 'createZstdDecompress' in zlib) {
this.inflateBuffer = [];

const inflate = zlib.createZstdDecompress({
chunkSize: 65_535,
}) as nativeZlib.Inflate;

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

inflate.on('error', (error) => {
this.emit(WebSocketShardEvents.Error, error);
});

this.nativeInflate = inflate;
} else {
console.warn(
'WebSocketShard: Compression is set to native zstd but node:zlib is not available or your node version does not support zstd decompression.',
);
params.delete('compress');
}

break;
}
}
}

Expand Down Expand Up @@ -628,12 +656,14 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {

// Deal with transport compression
if (this.transportCompressionEnabled) {
// Each WS message received is a full gateway message for zstd streaming, but for zlib it's chunked
const flush =
decompressable.length >= 4 &&
decompressable.at(-4) === 0x00 &&
decompressable.at(-3) === 0x00 &&
decompressable.at(-2) === 0xff &&
decompressable.at(-1) === 0xff;
this.strategy.options.compression === CompressionMethod.ZstdNative ||
(decompressable.length >= 4 &&
decompressable.at(-4) === 0x00 &&
decompressable.at(-3) === 0x00 &&
decompressable.at(-2) === 0xff &&
decompressable.at(-1) === 0xff);

if (this.nativeInflate) {
const doneWriting = new Promise<void>((resolve) => {
Expand Down