-
-
Notifications
You must be signed in to change notification settings - Fork 4k
feat: zstd streaming support #10758
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
base: main
Are you sure you want to change the base?
feat: zstd streaming support #10758
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,7 +222,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'); | ||
} | ||
|
||
|
@@ -243,6 +243,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; | ||
Comment on lines
+252
to
+254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this will need to set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last I tested this, it worked fine without that 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, each ws message is a full payload compressed with zstd, and I've also tested this on a bot in a guild that was getting many payloads/sec with 0 issues. Coulddd you test the hypothesis please and let us know results 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this applies only to the compression stream, not decompression. Ignore me! 👍 |
||
|
||
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; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -633,12 +661,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 || | ||
didinele marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(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) => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.