Skip to content

Commit de94eaf

Browse files
feat(WebsocketManager): retroactive token setting (#10418)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 8f97d2b commit de94eaf

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

packages/ws/src/strategies/context/IContextFetchingStrategy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export interface IContextFetchingStrategy {
3333
}
3434

3535
export async function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions> {
36-
/* eslint-disable @typescript-eslint/unbound-method */
3736
const {
3837
buildIdentifyThrottler,
3938
buildStrategy,
@@ -44,10 +43,10 @@ export async function managerToFetchingStrategyOptions(manager: WebSocketManager
4443
rest,
4544
...managerOptions
4645
} = manager.options;
47-
/* eslint-enable @typescript-eslint/unbound-method */
4846

4947
return {
5048
...managerOptions,
49+
token: manager.token,
5150
gatewayInformation: await manager.fetchGatewayInformation(),
5251
shardCount: await manager.getShardCount(),
5352
};

packages/ws/src/utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const DefaultWebSocketManagerOptions = {
6868
handshakeTimeout: 30_000,
6969
helloTimeout: 60_000,
7070
readyTimeout: 15_000,
71-
} as const satisfies OptionalWebSocketManagerOptions;
71+
} as const satisfies Omit<OptionalWebSocketManagerOptions, 'token'>;
7272

7373
export const ImportantGatewayOpcodes = new Set([
7474
GatewayOpcodes.Heartbeat,

packages/ws/src/ws/WebSocketManager.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ export interface RequiredWebSocketManagerOptions {
6767
* The REST instance to use for fetching gateway information
6868
*/
6969
rest: REST;
70-
/**
71-
* The token to use for identifying with the gateway
72-
*/
73-
token: string;
7470
}
7571

7672
/**
@@ -172,6 +168,12 @@ export interface OptionalWebSocketManagerOptions {
172168
* ```
173169
*/
174170
shardIds: number[] | ShardRange | null;
171+
/**
172+
* The token to use for identifying with the gateway
173+
*
174+
* If not provided, the token must be set using {@link WebSocketManager.setToken}
175+
*/
176+
token: string;
175177
/**
176178
* Function used to store session information for a given shard
177179
*/
@@ -211,10 +213,12 @@ export interface ManagerShardEventsMap {
211213
}
212214

213215
export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> implements AsyncDisposable {
216+
#token: string | null = null;
217+
214218
/**
215219
* The options being used by this manager
216220
*/
217-
public readonly options: WebSocketManagerOptions;
221+
public readonly options: Omit<WebSocketManagerOptions, 'token'>;
218222

219223
/**
220224
* Internal cache for a GET /gateway/bot result
@@ -236,10 +240,26 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
236240
*/
237241
private readonly strategy: IShardingStrategy;
238242

243+
/**
244+
* Gets the token set for this manager. If no token is set, an error is thrown.
245+
* To set the token, use {@link WebSocketManager.setToken} or pass it in the options.
246+
*
247+
* @remarks
248+
* This getter is mostly used to pass the token to the sharding strategy internally, there's not much reason to use it.
249+
*/
250+
public get token(): string {
251+
if (!this.#token) {
252+
throw new Error('Token has not been set');
253+
}
254+
255+
return this.#token;
256+
}
257+
239258
public constructor(options: CreateWebSocketManagerOptions) {
240259
super();
241260
this.options = { ...DefaultWebSocketManagerOptions, ...options };
242261
this.strategy = this.options.buildStrategy(this);
262+
this.#token = options.token ?? null;
243263
}
244264

245265
/**
@@ -334,6 +354,14 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
334354
await this.strategy.connect();
335355
}
336356

357+
public setToken(token: string): void {
358+
if (this.#token) {
359+
throw new Error('Token has already been set');
360+
}
361+
362+
this.#token = token;
363+
}
364+
337365
public destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>) {
338366
return this.strategy.destroy(options);
339367
}

0 commit comments

Comments
 (0)