Skip to content

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

feat: zstd streaming support #10758

wants to merge 4 commits into from

Conversation

vladfrangu
Copy link
Member

Please describe the changes this PR makes and why it should be merged:

Brings in native zstd streaming support for gateway decompression in environments that have it

That said, I'm leaving this as a draft for now, and seeing how zstd support progresses in node (specifically the backporting)

If someone wants to test it locally with their bots that have bigger payloads, you can use a script like this, after cloning this PR and building the repo

import { REST } from '@discordjs/rest';
import { CompressionMethod, WebSocketManager } from '@discordjs/ws';

const token = '';

const rest = new REST({}).setToken(token);

const manager = new WebSocketManager({
	intents: 0,
	rest,
	token,
	shardCount: 16,
	shardIds: [0],
	helloTimeout: 10_000,
	compression: CompressionMethod.ZstdNative,
});

manager.on('debug', console.log);
manager.on('error', console.error);

await manager.connect();
console.log('Connected');

Runnable with volta run --node 23 node script.mjs or whichever tool floats your boat.

Ideally we'd have some non native zstd module too but there doesn't seem to be any that exist right now 🙃

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating

Copy link

vercel bot commented Feb 14, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
discord-js ⬜️ Ignored (Inspect) Visit Preview May 9, 2025 3:04pm
discord-js-guide ⬜️ Ignored (Inspect) Visit Preview May 9, 2025 3:04pm

Copy link

codecov bot commented Feb 14, 2025

Codecov Report

Attention: Patch coverage is 6.45161% with 29 lines in your changes missing coverage. Please review.

Project coverage is 38.53%. Comparing base (f6da949) to head (02175c0).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
packages/ws/src/ws/WebSocketShard.ts 0.00% 29 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10758      +/-   ##
==========================================
- Coverage   42.60%   38.53%   -4.07%     
==========================================
  Files         262      241      -21     
  Lines       16129    14673    -1456     
  Branches     1549     1387     -162     
==========================================
- Hits         6871     5654    -1217     
+ Misses       9246     9007     -239     
  Partials       12       12              
Flag Coverage Δ
core 0.89% <ø> (ø)
ws 35.79% <6.45%> (-0.51%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Jiralite Jiralite added this to the ws 3.0.0 milestone Feb 14, 2025
@sdanialraza sdanialraza changed the title feat(ws): zstd streaming support feat: zstd streaming support Feb 15, 2025
@almeidx
Copy link
Member

almeidx commented May 9, 2025

This branch is available for testing at @discordjs/[email protected]

Comment on lines +253 to +255
const inflate = zlib.createZstdDecompress({
chunkSize: 65_535,
}) as nativeZlib.Inflate;
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this will need to set flush: zlib.constants.ZSTD_e_flush, as the default zstd behaviour is not to flush the output buffer if the received message doesn't close the current frame.

Copy link
Member Author

Choose a reason for hiding this comment

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

Last I tested this, it worked fine without that 👀

Copy link
Member Author

Choose a reason for hiding this comment

The 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 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The 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! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Review in Progress
Development

Successfully merging this pull request may close these issues.

6 participants