feat(WebsocketManager): retroactive token setting (#10418)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
DD
2024-07-31 14:46:39 +03:00
committed by GitHub
parent 8f97d2bacf
commit de94eaf351
3 changed files with 35 additions and 8 deletions

View File

@@ -33,7 +33,6 @@ export interface IContextFetchingStrategy {
} }
export async function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions> { export async function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions> {
/* eslint-disable @typescript-eslint/unbound-method */
const { const {
buildIdentifyThrottler, buildIdentifyThrottler,
buildStrategy, buildStrategy,
@@ -44,10 +43,10 @@ export async function managerToFetchingStrategyOptions(manager: WebSocketManager
rest, rest,
...managerOptions ...managerOptions
} = manager.options; } = manager.options;
/* eslint-enable @typescript-eslint/unbound-method */
return { return {
...managerOptions, ...managerOptions,
token: manager.token,
gatewayInformation: await manager.fetchGatewayInformation(), gatewayInformation: await manager.fetchGatewayInformation(),
shardCount: await manager.getShardCount(), shardCount: await manager.getShardCount(),
}; };

View File

@@ -68,7 +68,7 @@ export const DefaultWebSocketManagerOptions = {
handshakeTimeout: 30_000, handshakeTimeout: 30_000,
helloTimeout: 60_000, helloTimeout: 60_000,
readyTimeout: 15_000, readyTimeout: 15_000,
} as const satisfies OptionalWebSocketManagerOptions; } as const satisfies Omit<OptionalWebSocketManagerOptions, 'token'>;
export const ImportantGatewayOpcodes = new Set([ export const ImportantGatewayOpcodes = new Set([
GatewayOpcodes.Heartbeat, GatewayOpcodes.Heartbeat,

View File

@@ -67,10 +67,6 @@ export interface RequiredWebSocketManagerOptions {
* The REST instance to use for fetching gateway information * The REST instance to use for fetching gateway information
*/ */
rest: REST; rest: REST;
/**
* The token to use for identifying with the gateway
*/
token: string;
} }
/** /**
@@ -172,6 +168,12 @@ export interface OptionalWebSocketManagerOptions {
* ``` * ```
*/ */
shardIds: number[] | ShardRange | null; shardIds: number[] | ShardRange | null;
/**
* The token to use for identifying with the gateway
*
* If not provided, the token must be set using {@link WebSocketManager.setToken}
*/
token: string;
/** /**
* Function used to store session information for a given shard * Function used to store session information for a given shard
*/ */
@@ -211,10 +213,12 @@ export interface ManagerShardEventsMap {
} }
export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> implements AsyncDisposable { export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> implements AsyncDisposable {
#token: string | null = null;
/** /**
* The options being used by this manager * The options being used by this manager
*/ */
public readonly options: WebSocketManagerOptions; public readonly options: Omit<WebSocketManagerOptions, 'token'>;
/** /**
* Internal cache for a GET /gateway/bot result * Internal cache for a GET /gateway/bot result
@@ -236,10 +240,26 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
*/ */
private readonly strategy: IShardingStrategy; private readonly strategy: IShardingStrategy;
/**
* Gets the token set for this manager. If no token is set, an error is thrown.
* To set the token, use {@link WebSocketManager.setToken} or pass it in the options.
*
* @remarks
* This getter is mostly used to pass the token to the sharding strategy internally, there's not much reason to use it.
*/
public get token(): string {
if (!this.#token) {
throw new Error('Token has not been set');
}
return this.#token;
}
public constructor(options: CreateWebSocketManagerOptions) { public constructor(options: CreateWebSocketManagerOptions) {
super(); super();
this.options = { ...DefaultWebSocketManagerOptions, ...options }; this.options = { ...DefaultWebSocketManagerOptions, ...options };
this.strategy = this.options.buildStrategy(this); this.strategy = this.options.buildStrategy(this);
this.#token = options.token ?? null;
} }
/** /**
@@ -334,6 +354,14 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
await this.strategy.connect(); await this.strategy.connect();
} }
public setToken(token: string): void {
if (this.#token) {
throw new Error('Token has already been set');
}
this.#token = token;
}
public destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>) { public destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>) {
return this.strategy.destroy(options); return this.strategy.destroy(options);
} }