feat(WebSocketShard): support new resume url (#8480)

This commit is contained in:
DD
2022-08-14 13:01:35 +03:00
committed by GitHub
parent d08da8a212
commit bc06cc638d
8 changed files with 22 additions and 18 deletions

View File

@@ -55,7 +55,7 @@
"homepage": "https://discord.js.org", "homepage": "https://discord.js.org",
"dependencies": { "dependencies": {
"@sapphire/shapeshift": "^3.5.1", "@sapphire/shapeshift": "^3.5.1",
"discord-api-types": "^0.36.3", "discord-api-types": "^0.37.2",
"fast-deep-equal": "^3.1.3", "fast-deep-equal": "^3.1.3",
"ts-mixer": "^6.0.1", "ts-mixer": "^6.0.1",
"tslib": "^2.4.0" "tslib": "^2.4.0"

View File

@@ -54,7 +54,7 @@
"@discordjs/rest": "workspace:^", "@discordjs/rest": "workspace:^",
"@sapphire/snowflake": "^3.2.2", "@sapphire/snowflake": "^3.2.2",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
"discord-api-types": "^0.36.3", "discord-api-types": "^0.37.2",
"fast-deep-equal": "^3.1.3", "fast-deep-equal": "^3.1.3",
"lodash.snakecase": "^4.1.1", "lodash.snakecase": "^4.1.1",
"tslib": "^2.4.0", "tslib": "^2.4.0",

View File

@@ -55,7 +55,7 @@
"@discordjs/collection": "workspace:^", "@discordjs/collection": "workspace:^",
"@sapphire/async-queue": "^1.4.0", "@sapphire/async-queue": "^1.4.0",
"@sapphire/snowflake": "^3.2.2", "@sapphire/snowflake": "^3.2.2",
"discord-api-types": "^0.36.3", "discord-api-types": "^0.37.2",
"file-type": "^17.1.6", "file-type": "^17.1.6",
"tslib": "^2.4.0", "tslib": "^2.4.0",
"undici": "^5.8.2" "undici": "^5.8.2"

View File

@@ -53,7 +53,7 @@
"homepage": "https://discord.js.org", "homepage": "https://discord.js.org",
"dependencies": { "dependencies": {
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
"discord-api-types": "^0.36.3", "discord-api-types": "^0.37.2",
"prism-media": "^1.3.4", "prism-media": "^1.3.4",
"tslib": "^2.4.0", "tslib": "^2.4.0",
"ws": "^8.8.1" "ws": "^8.8.1"

View File

@@ -57,7 +57,7 @@
"@sapphire/async-queue": "^1.4.0", "@sapphire/async-queue": "^1.4.0",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
"@vladfrangu/async_event_emitter": "^2.0.1", "@vladfrangu/async_event_emitter": "^2.0.1",
"discord-api-types": "^0.36.3", "discord-api-types": "^0.37.2",
"tslib": "^2.4.0", "tslib": "^2.4.0",
"ws": "^8.8.1" "ws": "^8.8.1"
}, },

View File

@@ -43,6 +43,10 @@ export interface SessionInfo {
* The total number of shards at the time of this shard identifying * The total number of shards at the time of this shard identifying
*/ */
shardCount: number; shardCount: number;
/**
* URL to use when resuming
*/
resumeURL: string;
} }
/** /**

View File

@@ -107,8 +107,6 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
throw new Error("Tried to connect a shard that wasn't idle"); throw new Error("Tried to connect a shard that wasn't idle");
} }
const data = this.strategy.options.gatewayInformation;
const { version, encoding, compression } = this.strategy.options; const { version, encoding, compression } = this.strategy.options;
const params = new URLSearchParams({ v: version, encoding }); const params = new URLSearchParams({ v: version, encoding });
if (compression) { if (compression) {
@@ -127,7 +125,9 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
} }
} }
const url = `${data.url}?${params.toString()}`; const session = this.session ?? (await this.strategy.retrieveSessionInfo(this.id));
const url = `${session?.resumeURL ?? this.strategy.options.gatewayInformation.url}?${params.toString()}`;
this.debug([`Connecting to ${url}`]); this.debug([`Connecting to ${url}`]);
const connection = new WebSocket(url, { handshakeTimeout: this.strategy.options.handshakeTimeout ?? undefined }) const connection = new WebSocket(url, { handshakeTimeout: this.strategy.options.handshakeTimeout ?? undefined })
/* eslint-disable @typescript-eslint/no-misused-promises */ /* eslint-disable @typescript-eslint/no-misused-promises */
@@ -143,7 +143,6 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
await this.waitForEvent(WebSocketShardEvents.Hello, this.strategy.options.helloTimeout); await this.waitForEvent(WebSocketShardEvents.Hello, this.strategy.options.helloTimeout);
const session = this.session ?? (await this.strategy.retrieveSessionInfo(this.id));
if (session?.shardCount === this.strategy.options.shardCount) { if (session?.shardCount === this.strategy.options.shardCount) {
this.session = session; this.session = session;
await this.resume(session); await this.resume(session);
@@ -382,6 +381,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
sessionId: payload.d.session_id, sessionId: payload.d.session_id,
shardId: this.id, shardId: this.id,
shardCount: this.strategy.options.shardCount, shardCount: this.strategy.options.shardCount,
resumeURL: payload.d.resume_gateway_url,
}; };
await this.strategy.updateSessionInfo(this.id, this.session); await this.strategy.updateSessionInfo(this.id, this.session);

View File

@@ -1819,7 +1819,7 @@ __metadata:
"@sapphire/shapeshift": ^3.5.1 "@sapphire/shapeshift": ^3.5.1
"@types/node": ^16.11.47 "@types/node": ^16.11.47
c8: ^7.12.0 c8: ^7.12.0
discord-api-types: ^0.36.3 discord-api-types: ^0.37.2
downlevel-dts: ^0.10.0 downlevel-dts: ^0.10.0
eslint: ^8.21.0 eslint: ^8.21.0
fast-deep-equal: ^3.1.3 fast-deep-equal: ^3.1.3
@@ -1951,7 +1951,7 @@ __metadata:
"@sapphire/snowflake": ^3.2.2 "@sapphire/snowflake": ^3.2.2
"@types/node": ^16.11.47 "@types/node": ^16.11.47
c8: ^7.12.0 c8: ^7.12.0
discord-api-types: ^0.36.3 discord-api-types: ^0.37.2
downlevel-dts: ^0.10.0 downlevel-dts: ^0.10.0
eslint: ^8.21.0 eslint: ^8.21.0
file-type: ^17.1.6 file-type: ^17.1.6
@@ -1996,7 +1996,7 @@ __metadata:
"@types/jest": ^28.1.6 "@types/jest": ^28.1.6
"@types/node": ^16.11.47 "@types/node": ^16.11.47
"@types/ws": ^8.5.3 "@types/ws": ^8.5.3
discord-api-types: ^0.36.3 discord-api-types: ^0.37.2
downlevel-dts: ^0.10.0 downlevel-dts: ^0.10.0
eslint: ^8.21.0 eslint: ^8.21.0
jest: ^28.1.3 jest: ^28.1.3
@@ -2076,7 +2076,7 @@ __metadata:
"@types/ws": ^8.5.3 "@types/ws": ^8.5.3
"@vladfrangu/async_event_emitter": ^2.0.1 "@vladfrangu/async_event_emitter": ^2.0.1
c8: ^7.12.0 c8: ^7.12.0
discord-api-types: ^0.36.3 discord-api-types: ^0.37.2
eslint: ^8.21.0 eslint: ^8.21.0
mock-socket: ^9.1.5 mock-socket: ^9.1.5
prettier: ^2.7.1 prettier: ^2.7.1
@@ -6581,10 +6581,10 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"discord-api-types@npm:^0.36.3": "discord-api-types@npm:^0.37.2":
version: 0.36.3 version: 0.37.2
resolution: "discord-api-types@npm:0.36.3" resolution: "discord-api-types@npm:0.37.2"
checksum: 3089c0fb37425dc5df03c76d82988d43fcc272699b06a02fc830d0a3bef550009aaebdf6d646529e8a7ccea76ae3f43b099d736ea5ef37a0be143142ab49871d checksum: cf89c232a70c8f77653fdfa7fc34e109f51ba2650f98d54d456aefdb0a90872e2189292dfcf7572a9ece1d16652c6993df64f75199eaee3715f28e592dbf6c25
languageName: node languageName: node
linkType: hard linkType: hard
@@ -6600,7 +6600,7 @@ __metadata:
"@sapphire/snowflake": ^3.2.2 "@sapphire/snowflake": ^3.2.2
"@types/node": ^16.11.47 "@types/node": ^16.11.47
"@types/ws": ^8.5.3 "@types/ws": ^8.5.3
discord-api-types: ^0.36.3 discord-api-types: ^0.37.2
dtslint: ^4.2.1 dtslint: ^4.2.1
eslint: ^8.21.0 eslint: ^8.21.0
fast-deep-equal: ^3.1.3 fast-deep-equal: ^3.1.3