Skip to content
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

feat: fetch gateway information without requiring rest in ws #10651

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

Conversation

vladfrangu
Copy link
Member

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

Currently the only way to override the gateway URL that /ws will connect to is by intercepting the rest request and overriding it. This PR lets you pick a different ws url, if you may be proxying it or have custom implementations or what not.

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • This PR changes the library's interface (methods or parameters added)

@vladfrangu vladfrangu requested a review from a team as a code owner December 9, 2024 17:42
@vladfrangu vladfrangu requested review from iCrawl and didinele December 9, 2024 17:42
Copy link

vercel bot commented Dec 9, 2024

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 Dec 14, 2024 9:05pm
discord-js-guide ⬜️ Ignored (Inspect) Visit Preview Dec 14, 2024 9:05pm

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 64.70588% with 6 lines in your changes missing coverage. Please review.

Project coverage is 37.21%. Comparing base (35ebcc7) to head (1d19b72).

Files with missing lines Patch % Lines
packages/ws/src/ws/WebSocketManager.ts 62.50% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10651   +/-   ##
=======================================
  Coverage   37.20%   37.21%           
=======================================
  Files         239      239           
  Lines       15147    15157   +10     
  Branches     1373     1377    +4     
=======================================
+ Hits         5636     5640    +4     
- Misses       9500     9505    +5     
- Partials       11       12    +1     
Flag Coverage Δ
ws 34.68% <64.70%> (+0.23%) ⬆️

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.

Copy link
Member

@didinele didinele left a comment

Choose a reason for hiding this comment

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

This looks good. Just one classic mistake I've made a few times too, callbacks aren't encodeable over worker_threads, so this needs updating, otherwise we get hard crashes:

export interface FetchingStrategyOptions
extends Omit<
WebSocketManagerOptions,
| 'buildIdentifyThrottler'
| 'buildStrategy'
| 'rest'
| 'retrieveSessionInfo'
| 'shardCount'
| 'shardIds'
| 'updateSessionInfo'
> {
readonly gatewayInformation: APIGatewayBotInfo;
readonly shardCount: number;
}

Along with its runtime counter-part:

export async function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions> {
const {
buildIdentifyThrottler,
buildStrategy,
retrieveSessionInfo,
updateSessionInfo,
shardCount,
shardIds,
rest,
...managerOptions
} = manager.options;
return {
...managerOptions,
token: manager.token,
gatewayInformation: await manager.fetchGatewayInformation(),
shardCount: await manager.getShardCount(),
};
}

In the future I'll probably rewrite this to a Pick 😅

@Jiralite Jiralite added this to the ws 2.1.0 milestone Dec 9, 2024
almeidx
almeidx previously requested changes Dec 9, 2024
Copy link
Member

@almeidx almeidx left a comment

Choose a reason for hiding this comment

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

Seems the README and tests need updating

packages/ws/src/ws/WebSocketManager.ts Outdated Show resolved Hide resolved
@vladfrangu vladfrangu changed the title feat: overridable initial gateway URL feat: fetch gateway information without requiring rest in ws Dec 9, 2024
@didinele
Copy link
Member

this needs a rebase now

@vladfrangu vladfrangu force-pushed the feat/custom-gateway-url-support branch from 89c2b68 to 2626c9a Compare December 14, 2024 20:58
Comment on lines +141 to +144
/**
* The REST instance to use for fetching gateway information
*/
rest?: REST;
Copy link
Member

Choose a reason for hiding this comment

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

Now we can deprecate (or remove) this 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

I dont want this PR to become a major. We can re-deprecate it >.>

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