Skip to content

Commit 08a61ca

Browse files
authored
refactor(WebSocketManager)!: remove deprecated rest option (#10998)
BREAKING CHANGE: The `rest` option in the `WebSocketManager` constructor has been removed. Pass a `fetchGatewayInformation` function instead.
1 parent 2e9bfba commit 08a61ca

File tree

9 files changed

+98
-281
lines changed

9 files changed

+98
-281
lines changed

packages/core/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,24 @@ pnpm add @discordjs/core
3737
These examples use [ES modules](https://nodejs.org/api/esm.html#enabling).
3838

3939
```ts
40+
import {
41+
Client,
42+
GatewayDispatchEvents,
43+
GatewayIntentBits,
44+
InteractionType,
45+
MessageFlags,
46+
type RESTGetAPIGatewayBotResult,
47+
} from '@discordjs/core';
4048
import { REST } from '@discordjs/rest';
4149
import { WebSocketManager } from '@discordjs/ws';
42-
import { GatewayDispatchEvents, GatewayIntentBits, InteractionType, MessageFlags, Client } from '@discordjs/core';
4350

4451
// Create REST and WebSocket managers directly
4552
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
4653

4754
const gateway = new WebSocketManager({
4855
token: process.env.DISCORD_TOKEN,
4956
intents: GatewayIntentBits.GuildMessages | GatewayIntentBits.MessageContent,
50-
rest,
57+
fetchGatewayInformation: () => rest.get('/gateway/bot') as Promise<RESTGetAPIGatewayBotResult>,
5158
});
5259

5360
// Create a client to emit relevant events.

packages/discord.js/src/client/Client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Client extends BaseClient {
163163
const wsOptions = {
164164
...this.options.ws,
165165
intents: this.options.intents.bitfield,
166-
rest: this.rest,
166+
fetchGatewayInformation: () => this.rest.get(Routes.gatewayBot()),
167167
// Explicitly nulled to always be set using `setToken` in `login`
168168
token: null,
169169
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { RESTGetAPIGatewayBotResult } from 'discord-api-types/v10';
2+
3+
export const mockGatewayInformation: RESTGetAPIGatewayBotResult = {
4+
shards: 1,
5+
session_start_limit: {
6+
max_concurrency: 3,
7+
reset_after: 60,
8+
remaining: 3,
9+
total: 3,
10+
},
11+
url: 'wss://gateway.discord.gg',
12+
};

packages/ws/__tests__/strategy/WorkerContextFetchingStrategy.test.ts

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint-disable @typescript-eslint/consistent-type-imports */
22
// @ts-nocheck
3-
import { REST } from '@discordjs/rest';
4-
import { MockAgent, type Interceptable } from 'undici';
53
import { beforeEach, test, vi, expect } from 'vitest';
64
import {
75
managerToFetchingStrategyOptions,
@@ -12,15 +10,7 @@ import {
1210
type WorkerReceivePayload,
1311
type WorkerSendPayload,
1412
} from '../../src/index.js';
15-
16-
let mockAgent: MockAgent;
17-
let mockPool: Interceptable;
18-
19-
beforeEach(() => {
20-
mockAgent = new MockAgent();
21-
mockAgent.disableNetConnect();
22-
mockPool = mockAgent.get('https://discord.com');
23-
});
13+
import { mockGatewayInformation } from '../gateway.mock.js';
2414

2515
const session = {
2616
shardId: 0,
@@ -52,32 +42,13 @@ vi.mock('node:worker_threads', async () => {
5242
});
5343

5444
test('session info', async () => {
55-
const rest = new REST().setAgent(mockAgent).setToken('A-Very-Fake-Token');
56-
const manager = new WebSocketManager({ token: 'A-Very-Fake-Token', intents: 0, rest });
57-
58-
mockPool
59-
.intercept({
60-
path: '/api/v10/gateway/bot',
61-
method: 'GET',
62-
})
63-
.reply(() => ({
64-
data: {
65-
shards: 1,
66-
session_start_limit: {
67-
max_concurrency: 3,
68-
reset_after: 60,
69-
remaining: 3,
70-
total: 3,
71-
},
72-
url: 'wss://gateway.discord.gg',
73-
},
74-
statusCode: 200,
75-
responseOptions: {
76-
headers: {
77-
'content-type': 'application/json',
78-
},
79-
},
80-
}));
45+
const manager = new WebSocketManager({
46+
token: 'A-Very-Fake-Token',
47+
intents: 0,
48+
async fetchGatewayInformation() {
49+
return mockGatewayInformation;
50+
},
51+
});
8152

8253
const strategy = new WorkerContextFetchingStrategy(await managerToFetchingStrategyOptions(manager));
8354

packages/ws/__tests__/strategy/WorkerShardingStrategy.test.ts

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/* eslint-disable id-length */
22
import { setImmediate } from 'node:timers';
3-
import { REST } from '@discordjs/rest';
4-
import type { RESTGetAPIGatewayBotResult, GatewayDispatchPayload, GatewaySendPayload } from 'discord-api-types/v10';
5-
import { GatewayDispatchEvents, GatewayOpcodes, Routes } from 'discord-api-types/v10';
6-
import { MockAgent, type Interceptable } from 'undici';
7-
import { beforeEach, test, vi, expect, afterEach } from 'vitest';
3+
import type { GatewayDispatchPayload, GatewaySendPayload } from 'discord-api-types/v10';
4+
import { GatewayDispatchEvents, GatewayOpcodes } from 'discord-api-types/v10';
5+
import { test, vi, expect, afterEach } from 'vitest';
86
import {
97
WebSocketManager,
108
WorkerSendPayloadOp,
@@ -15,9 +13,7 @@ import {
1513
type WorkerSendPayload,
1614
type SessionInfo,
1715
} from '../../src/index.js';
18-
19-
let mockAgent: MockAgent;
20-
let mockPool: Interceptable;
16+
import { mockGatewayInformation } from '../gateway.mock.js';
2117

2218
const mockConstructor = vi.fn();
2319
const mockSend = vi.fn();
@@ -135,28 +131,20 @@ vi.mock('node:worker_threads', async () => {
135131
};
136132
});
137133

138-
beforeEach(() => {
139-
mockAgent = new MockAgent();
140-
mockAgent.disableNetConnect();
141-
mockPool = mockAgent.get('https://discord.com');
142-
});
143-
144134
afterEach(() => {
145135
mockConstructor.mockClear();
146136
mockSend.mockClear();
147137
mockTerminate.mockClear();
148138
});
149139

150140
test('spawn, connect, send a message, session info, and destroy', async () => {
151-
const rest = new REST().setAgent(mockAgent).setToken('A-Very-Fake-Token');
152-
153141
const mockRetrieveSessionInfo = vi.fn();
154142
const mockUpdateSessionInfo = vi.fn();
155143
const manager = new WebSocketManager({
156144
token: 'A-Very-Fake-Token',
157145
intents: 0,
158146
async fetchGatewayInformation() {
159-
return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
147+
return mockGatewayInformation;
160148
},
161149
shardIds: [0, 1],
162150
retrieveSessionInfo: mockRetrieveSessionInfo,
@@ -166,30 +154,6 @@ test('spawn, connect, send a message, session info, and destroy', async () => {
166154

167155
const managerEmitSpy = vi.spyOn(manager, 'emit');
168156

169-
mockPool
170-
.intercept({
171-
path: '/api/v10/gateway/bot',
172-
method: 'GET',
173-
})
174-
.reply(() => ({
175-
data: {
176-
shards: 1,
177-
session_start_limit: {
178-
max_concurrency: 3,
179-
reset_after: 60,
180-
remaining: 3,
181-
total: 3,
182-
},
183-
url: 'wss://gateway.discord.gg',
184-
},
185-
statusCode: 200,
186-
responseOptions: {
187-
headers: {
188-
'content-type': 'application/json',
189-
},
190-
},
191-
}));
192-
193157
await manager.connect();
194158
expect(mockConstructor).toHaveBeenCalledWith(
195159
expect.stringContaining('defaultWorker.js'),

0 commit comments

Comments
 (0)