@discordjs/ws 3.0.0-dev.1794009739-5bb74922a → 3.0.0-dev.1794830522-2b3dbd432

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/index.d.ts +14 -737
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +16 -1638
  4. package/dist/index.js.map +1 -1
  5. package/dist/strategies/context/IContextFetchingStrategy.d.ts +23 -0
  6. package/dist/strategies/context/IContextFetchingStrategy.d.ts.map +1 -0
  7. package/dist/strategies/context/IContextFetchingStrategy.js +20 -0
  8. package/dist/strategies/context/IContextFetchingStrategy.js.map +1 -0
  9. package/dist/strategies/context/SimpleContextFetchingStrategy.d.ts +14 -0
  10. package/dist/strategies/context/SimpleContextFetchingStrategy.d.ts.map +1 -0
  11. package/dist/strategies/context/SimpleContextFetchingStrategy.js +31 -0
  12. package/dist/strategies/context/SimpleContextFetchingStrategy.js.map +1 -0
  13. package/dist/strategies/context/WorkerContextFetchingStrategy.d.ts +12 -0
  14. package/dist/strategies/context/WorkerContextFetchingStrategy.d.ts.map +1 -0
  15. package/dist/strategies/context/WorkerContextFetchingStrategy.js +78 -0
  16. package/dist/strategies/context/WorkerContextFetchingStrategy.js.map +1 -0
  17. package/dist/strategies/sharding/IShardingStrategy.d.ts +30 -0
  18. package/dist/strategies/sharding/IShardingStrategy.d.ts.map +1 -0
  19. package/dist/strategies/sharding/IShardingStrategy.js +2 -0
  20. package/dist/strategies/sharding/IShardingStrategy.js.map +1 -0
  21. package/dist/strategies/sharding/SimpleShardingStrategy.d.ts +34 -0
  22. package/dist/strategies/sharding/SimpleShardingStrategy.d.ts.map +1 -0
  23. package/dist/strategies/sharding/SimpleShardingStrategy.js +67 -0
  24. package/dist/strategies/sharding/SimpleShardingStrategy.js.map +1 -0
  25. package/dist/strategies/sharding/WorkerShardingStrategy.d.ts +142 -0
  26. package/dist/strategies/sharding/WorkerShardingStrategy.d.ts.map +1 -0
  27. package/dist/strategies/sharding/WorkerShardingStrategy.js +269 -0
  28. package/dist/strategies/sharding/WorkerShardingStrategy.js.map +1 -0
  29. package/dist/strategies/sharding/defaultWorker.d.ts +2 -0
  30. package/dist/strategies/sharding/defaultWorker.d.ts.map +1 -0
  31. package/dist/strategies/sharding/defaultWorker.js +4 -0
  32. package/dist/strategies/sharding/defaultWorker.js.map +1 -0
  33. package/dist/throttling/IIdentifyThrottler.d.ts +12 -0
  34. package/dist/throttling/IIdentifyThrottler.d.ts.map +1 -0
  35. package/dist/throttling/IIdentifyThrottler.js +2 -0
  36. package/dist/throttling/IIdentifyThrottler.js.map +1 -0
  37. package/dist/throttling/SimpleIdentifyThrottler.d.ts +22 -0
  38. package/dist/throttling/SimpleIdentifyThrottler.d.ts.map +1 -0
  39. package/dist/throttling/SimpleIdentifyThrottler.js +37 -0
  40. package/dist/throttling/SimpleIdentifyThrottler.js.map +1 -0
  41. package/dist/utils/WorkerBootstrapper.d.ts +51 -0
  42. package/dist/utils/WorkerBootstrapper.d.ts.map +1 -0
  43. package/dist/utils/WorkerBootstrapper.js +131 -0
  44. package/dist/utils/WorkerBootstrapper.js.map +1 -0
  45. package/dist/utils/constants.d.ts +54 -0
  46. package/dist/utils/constants.d.ts.map +1 -0
  47. package/dist/utils/constants.js +78 -0
  48. package/dist/utils/constants.js.map +1 -0
  49. package/dist/ws/WebSocketManager.d.ts +278 -0
  50. package/dist/ws/WebSocketManager.d.ts.map +1 -0
  51. package/dist/ws/WebSocketManager.js +136 -0
  52. package/dist/ws/WebSocketManager.js.map +1 -0
  53. package/dist/ws/WebSocketShard.d.ts +102 -0
  54. package/dist/ws/WebSocketShard.d.ts.map +1 -0
  55. package/dist/ws/WebSocketShard.js +761 -0
  56. package/dist/ws/WebSocketShard.js.map +1 -0
  57. package/package.json +10 -24
  58. package/dist/defaultWorker.d.mts +0 -2
  59. package/dist/defaultWorker.d.ts +0 -2
  60. package/dist/defaultWorker.js +0 -1176
  61. package/dist/defaultWorker.js.map +0 -1
  62. package/dist/defaultWorker.mjs +0 -1158
  63. package/dist/defaultWorker.mjs.map +0 -1
  64. package/dist/index.d.mts +0 -741
  65. package/dist/index.mjs +0 -1599
  66. package/dist/index.mjs.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,741 +1,18 @@
1
- import { Awaitable } from '@discordjs/util';
2
- import { GatewayDispatchPayload, GatewayReadyDispatchData, GatewaySendPayload, GatewayOpcodes, GatewayIntentBits, RESTGetAPIGatewayBotResult, GatewayCapabilityBits, GatewayIdentifyProperties, GatewayPresenceUpdateData, APIGatewayBotInfo } from 'discord-api-types/v10';
3
- import { Collection } from '@discordjs/collection';
4
- import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
5
- import { AsyncQueue } from '@sapphire/async-queue';
6
-
7
- declare enum WebSocketShardEvents {
8
- Closed = "closed",
9
- Debug = "debug",
10
- Dispatch = "dispatch",
11
- Error = "error",
12
- HeartbeatComplete = "heartbeat",
13
- Hello = "hello",
14
- Ready = "ready",
15
- Resumed = "resumed",
16
- SocketError = "socketError"
17
- }
18
- declare enum WebSocketShardStatus {
19
- Idle = 0,
20
- Connecting = 1,
21
- Resuming = 2,
22
- Ready = 3
23
- }
24
- declare enum WebSocketShardDestroyRecovery {
25
- Reconnect = 0,
26
- Resume = 1
27
- }
28
- interface WebSocketShardEventsMap {
29
- [WebSocketShardEvents.Closed]: [code: number];
30
- [WebSocketShardEvents.Debug]: [message: string];
31
- [WebSocketShardEvents.Dispatch]: [payload: GatewayDispatchPayload];
32
- [WebSocketShardEvents.Error]: [error: Error];
33
- [WebSocketShardEvents.Hello]: [];
34
- [WebSocketShardEvents.Ready]: [payload: GatewayReadyDispatchData];
35
- [WebSocketShardEvents.Resumed]: [];
36
- [WebSocketShardEvents.HeartbeatComplete]: [stats: {
37
- ackAt: number;
38
- heartbeatAt: number;
39
- latency: number;
40
- }];
41
- [WebSocketShardEvents.SocketError]: [error: Error];
42
- }
43
- interface WebSocketShardDestroyOptions {
44
- code?: number;
45
- reason?: string;
46
- recover?: WebSocketShardDestroyRecovery;
47
- }
48
- declare enum CloseCodes {
49
- Normal = 1000,
50
- Resuming = 4200
51
- }
52
- interface SendRateLimitState {
53
- resetAt: number;
54
- sent: number;
55
- }
56
- declare class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
57
- #private;
58
- private connection;
59
- private nativeInflate;
60
- private zLibSyncInflate;
61
- /**
62
- * @privateRemarks
63
- *
64
- * Used only for native zlib inflate, zlib-sync buffering is handled by the library itself.
65
- */
66
- private inflateBuffer;
67
- private readonly textDecoder;
68
- private replayedEvents;
69
- private isAck;
70
- private sendRateLimitState;
71
- private initialHeartbeatTimeoutController;
72
- private heartbeatInterval;
73
- private lastHeartbeatAt;
74
- private initialConnectResolved;
75
- private failedToConnectDueToNetworkError;
76
- private readonly sendQueue;
77
- private readonly timeoutAbortControllers;
78
- private readonly strategy;
79
- readonly id: number;
80
- private identifyCompressionEnabled;
81
- /**
82
- * @privateRemarks
83
- *
84
- * This is needed because `this.strategy.options.compression` is not an actual reflection of the compression method
85
- * used, but rather the compression method that the user wants to use. This is because the libraries could just be missing.
86
- */
87
- private get transportCompressionEnabled();
88
- get status(): WebSocketShardStatus;
89
- constructor(strategy: IContextFetchingStrategy, id: number);
90
- connect(): Promise<void>;
91
- private internalConnect;
92
- destroy(options?: WebSocketShardDestroyOptions): Promise<void>;
93
- private waitForEvent;
94
- send(payload: GatewaySendPayload): Promise<void>;
95
- private identify;
96
- private resume;
97
- private heartbeat;
98
- private parseInflateResult;
99
- private unpackMessage;
100
- private onMessage;
101
- private onError;
102
- private onClose;
103
- private debug;
104
- }
105
-
106
- /**
107
- * Strategies responsible for spawning, initializing connections, destroying shards, and relaying events
108
- */
109
- interface IShardingStrategy {
110
- /**
111
- * Initializes all the shards
112
- */
113
- connect(): Awaitable<void>;
114
- /**
115
- * Destroys all the shards
116
- */
117
- destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Awaitable<void>;
118
- /**
119
- * Fetches the status of all the shards
120
- */
121
- fetchStatus(): Awaitable<Collection<number, WebSocketShardStatus>>;
122
- /**
123
- * Sends a payload to a shard
124
- */
125
- send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
126
- /**
127
- * Spawns all the shards
128
- */
129
- spawn(shardIds: number[]): Awaitable<void>;
130
- }
131
-
132
- /**
133
- * IdentifyThrottlers are responsible for dictating when a shard is allowed to identify.
134
- *
135
- * @see {@link https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency}
136
- */
137
- interface IIdentifyThrottler {
138
- /**
139
- * Resolves once the given shard should be allowed to identify, or rejects if the operation was aborted.
140
- */
141
- waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
142
- }
143
-
144
- /**
145
- * Simple strategy that just spawns shards in the current process
146
- */
147
- declare class SimpleShardingStrategy implements IShardingStrategy {
148
- private readonly manager;
149
- private readonly shards;
150
- constructor(manager: WebSocketManager);
151
- /**
152
- * {@inheritDoc IShardingStrategy.spawn}
153
- */
154
- spawn(shardIds: number[]): Promise<void>;
155
- /**
156
- * {@inheritDoc IShardingStrategy.connect}
157
- */
158
- connect(): Promise<void>;
159
- /**
160
- * {@inheritDoc IShardingStrategy.destroy}
161
- */
162
- destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
163
- /**
164
- * {@inheritDoc IShardingStrategy.send}
165
- */
166
- send(shardId: number, payload: GatewaySendPayload): Promise<void>;
167
- /**
168
- * {@inheritDoc IShardingStrategy.fetchStatus}
169
- */
170
- fetchStatus(): Promise<Collection<number, WebSocketShardStatus>>;
171
- }
172
-
173
- /**
174
- * The state of a rate limit key's identify queue.
175
- */
176
- interface IdentifyState {
177
- queue: AsyncQueue;
178
- resetsAt: number;
179
- }
180
- /**
181
- * Local, in-memory identify throttler.
182
- */
183
- declare class SimpleIdentifyThrottler implements IIdentifyThrottler {
184
- private readonly maxConcurrency;
185
- private readonly states;
186
- constructor(maxConcurrency: number);
187
- /**
188
- * {@inheritDoc IIdentifyThrottler.waitForIdentify}
189
- */
190
- waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
191
- }
192
-
193
- /**
194
- * Valid encoding types
195
- */
196
- declare enum Encoding {
197
- JSON = "json"
198
- }
199
- /**
200
- * Valid compression methods
201
- */
202
- declare enum CompressionMethod {
203
- ZlibNative = 0,
204
- ZlibSync = 1,
205
- ZstdNative = 2
206
- }
207
- declare const DefaultDeviceProperty: `@discordjs/ws ${string}`;
208
- declare const CompressionParameterMap: {
209
- readonly 0: "zlib-stream";
210
- readonly 1: "zlib-stream";
211
- readonly 2: "zstd-stream";
212
- };
213
- /**
214
- * Default options used by the manager
215
- */
216
- declare const DefaultWebSocketManagerOptions: {
217
- readonly buildIdentifyThrottler: (manager: WebSocketManager) => SimpleIdentifyThrottler;
218
- readonly buildStrategy: (manager: WebSocketManager) => SimpleShardingStrategy;
219
- readonly capabilities: 0;
220
- readonly shardCount: null;
221
- readonly shardIds: null;
222
- readonly largeThreshold: null;
223
- readonly initialPresence: null;
224
- readonly identifyProperties: {
225
- readonly browser: `@discordjs/ws ${string}`;
226
- readonly device: `@discordjs/ws ${string}`;
227
- readonly os: NodeJS.Platform;
228
- };
229
- readonly version: "10";
230
- readonly encoding: Encoding;
231
- readonly compression: null;
232
- readonly useIdentifyCompression: false;
233
- readonly retrieveSessionInfo: (shardId: number) => SessionInfo | null;
234
- readonly updateSessionInfo: (shardId: number, info: SessionInfo | null) => void;
235
- readonly handshakeTimeout: 30000;
236
- readonly helloTimeout: 60000;
237
- readonly readyTimeout: 15000;
238
- };
239
- declare const ImportantGatewayOpcodes: Set<GatewayOpcodes>;
240
- declare function getInitialSendRateLimitState(): SendRateLimitState;
241
-
242
- /**
243
- * Represents a range of shard ids
244
- */
245
- interface ShardRange {
246
- end: number;
247
- start: number;
248
- }
249
- /**
250
- * Session information for a given shard, used to resume a session
251
- */
252
- interface SessionInfo {
253
- /**
254
- * URL to use when resuming
255
- */
256
- resumeURL: string;
257
- /**
258
- * The sequence number of the last message sent by the shard
259
- */
260
- sequence: number;
261
- /**
262
- * Session id for this shard
263
- */
264
- sessionId: string;
265
- /**
266
- * The total number of shards at the time of this shard identifying
267
- */
268
- shardCount: number;
269
- /**
270
- * The id of the shard
271
- */
272
- shardId: number;
273
- }
274
- /**
275
- * Required options for the WebSocketManager
276
- */
277
- interface RequiredWebSocketManagerOptions {
278
- /**
279
- * The intents to request
280
- */
281
- intents: GatewayIntentBits | 0;
282
- }
283
- /**
284
- * Optional additional configuration for the WebSocketManager
285
- */
286
- interface OptionalWebSocketManagerOptions {
287
- /**
288
- * Builds an identify throttler to use for this manager's shards
289
- */
290
- buildIdentifyThrottler(manager: WebSocketManager): Awaitable<IIdentifyThrottler>;
291
- /**
292
- * Builds the strategy to use for sharding
293
- *
294
- * @example
295
- * ```ts
296
- * const manager = new WebSocketManager({
297
- * token: process.env.DISCORD_TOKEN,
298
- * intents: 0, // for no intents
299
- * buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 }),
300
- * });
301
- * ```
302
- */
303
- buildStrategy(manager: WebSocketManager): IShardingStrategy;
304
- /**
305
- * The gateway capabilities to opt into
306
- *
307
- * @defaultValue `0`
308
- * @see {@link https://discord.com/developers/docs/events/gateway-events#identify-gateway-capabilities}
309
- */
310
- capabilities: GatewayCapabilityBits | 0;
311
- /**
312
- * The transport compression method to use - mutually exclusive with `useIdentifyCompression`
313
- *
314
- * @defaultValue `null` (no transport compression)
315
- */
316
- compression: CompressionMethod | null;
317
- /**
318
- * The encoding to use
319
- *
320
- * @defaultValue `'json'`
321
- */
322
- encoding: Encoding;
323
- /**
324
- * How long to wait for a shard to connect before giving up
325
- */
326
- handshakeTimeout: number | null;
327
- /**
328
- * How long to wait for a shard's HELLO packet before giving up
329
- */
330
- helloTimeout: number | null;
331
- /**
332
- * Properties to send to the gateway when identifying
333
- */
334
- identifyProperties: GatewayIdentifyProperties;
335
- /**
336
- * Initial presence data to send to the gateway when identifying
337
- */
338
- initialPresence: GatewayPresenceUpdateData | null;
339
- /**
340
- * Value between 50 and 250, total number of members where the gateway will stop sending offline members in the guild member list
341
- */
342
- largeThreshold: number | null;
343
- /**
344
- * How long to wait for a shard's READY packet before giving up
345
- */
346
- readyTimeout: number | null;
347
- /**
348
- * Function used to retrieve session information (and attempt to resume) for a given shard
349
- *
350
- * @example
351
- * ```ts
352
- * const manager = new WebSocketManager({
353
- * async retrieveSessionInfo(shardId): Awaitable<SessionInfo | null> {
354
- * // Fetch this info from redis or similar
355
- * return { sessionId: string, sequence: number };
356
- * // Return null if no information is found
357
- * },
358
- * });
359
- * ```
360
- */
361
- retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
362
- /**
363
- * The total number of shards across all WebsocketManagers you intend to instantiate.
364
- * Use `null` to use Discord's recommended shard count
365
- */
366
- shardCount: number | null;
367
- /**
368
- * The ids of the shards this WebSocketManager should manage.
369
- * Use `null` to simply spawn 0 through `shardCount - 1`
370
- *
371
- * @example
372
- * ```ts
373
- * const manager = new WebSocketManager({
374
- * shardIds: [1, 3, 7], // spawns shard 1, 3, and 7, nothing else
375
- * });
376
- * ```
377
- * @example
378
- * ```ts
379
- * const manager = new WebSocketManager({
380
- * shardIds: {
381
- * start: 3,
382
- * end: 6,
383
- * }, // spawns shards 3, 4, 5, and 6
384
- * });
385
- * ```
386
- */
387
- shardIds: number[] | ShardRange | null;
388
- /**
389
- * The token to use for identifying with the gateway
390
- *
391
- * If not provided, the token must be set using {@link WebSocketManager.setToken}
392
- */
393
- token: string;
394
- /**
395
- * Function used to store session information for a given shard
396
- */
397
- updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
398
- /**
399
- * Whether to use the `compress` option when identifying
400
- *
401
- * @defaultValue `false`
402
- */
403
- useIdentifyCompression: boolean;
404
- /**
405
- * The gateway version to use
406
- *
407
- * @defaultValue `'10'`
408
- */
409
- version: string;
410
- }
411
- interface WebSocketManagerOptions extends OptionalWebSocketManagerOptions, RequiredWebSocketManagerOptions {
412
- }
413
- interface CreateWebSocketManagerOptions extends Partial<OptionalWebSocketManagerOptions>, RequiredWebSocketManagerOptions {
414
- }
415
- /**
416
- * Options for {@link WebSocketManager.connect}
417
- */
418
- interface WebSocketManagerConnectOptions {
419
- /**
420
- * Information retrieved from the `/gateway/bot` endpoint, used as-is.
421
- * We recommend using a REST client that respects Discord's rate limits, such as `@discordjs/rest`,
422
- * and fetching this information right before connecting, as the session start limits it reports go stale.
423
- *
424
- * @example
425
- * ```ts
426
- * const rest = new REST().setToken(process.env.DISCORD_TOKEN);
427
- * await manager.connect({
428
- * gatewayInformation: (await rest.get(Routes.gatewayBot())) as RESTGetAPIGatewayBotResult,
429
- * });
430
- * ```
431
- */
432
- gatewayInformation: RESTGetAPIGatewayBotResult;
433
- }
434
- interface ManagerShardEventsMap {
435
- [WebSocketShardEvents.Closed]: [code: number, shardId: number];
436
- [WebSocketShardEvents.Debug]: [message: string, shardId: number];
437
- [WebSocketShardEvents.Dispatch]: [payload: GatewayDispatchPayload, shardId: number];
438
- [WebSocketShardEvents.Error]: [error: Error, shardId: number];
439
- [WebSocketShardEvents.Hello]: [shardId: number];
440
- [WebSocketShardEvents.Ready]: [data: GatewayReadyDispatchData, shardId: number];
441
- [WebSocketShardEvents.Resumed]: [shardId: number];
442
- [WebSocketShardEvents.HeartbeatComplete]: [
443
- stats: {
444
- ackAt: number;
445
- heartbeatAt: number;
446
- latency: number;
447
- },
448
- shardId: number
449
- ];
450
- [WebSocketShardEvents.SocketError]: [error: Error, shardId: number];
451
- }
452
- declare class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> implements AsyncDisposable {
453
- #private;
454
- /**
455
- * The options being used by this manager
456
- */
457
- readonly options: Omit<WebSocketManagerOptions, 'token'>;
458
- /**
459
- * Strategy used to manage shards
460
- *
461
- * @defaultValue `SimpleShardingStrategy`
462
- */
463
- private readonly strategy;
464
- /**
465
- * Gets the token set for this manager. If no token is set, an error is thrown.
466
- * To set the token, use {@link WebSocketManager.setToken} or pass it in the options.
467
- *
468
- * @remarks
469
- * This getter is mostly used to pass the token to the sharding strategy internally, there's not much reason to use it.
470
- */
471
- get token(): string;
472
- constructor(options: CreateWebSocketManagerOptions);
473
- /**
474
- * The `/gateway/bot` information provided to {@link WebSocketManager.connect}.
475
- * Throws if the method has not been invoked yet.
476
- */
477
- getGatewayInformation(): RESTGetAPIGatewayBotResult;
478
- /**
479
- * Updates your total shard count on-the-fly, re-spawning all shards to the new amount
480
- *
481
- * @param shardCount - The new shard count to use
482
- */
483
- updateShardCount(shardCount: number | null): Promise<this>;
484
- /**
485
- * Yields the total number of shards across for your bot, accounting for Discord recommendations
486
- *
487
- * @remarks
488
- * Throws if {@link WebSocketManager.connect} has not been invoked yet.
489
- */
490
- getShardCount(): number;
491
- /**
492
- * Yields the ids of the shards this manager should manage
493
- *
494
- * @remarks
495
- * Throws if {@link WebSocketManager.connect} has not been invoked yet.
496
- */
497
- getShardIds(): number[];
498
- connect(options: WebSocketManagerConnectOptions): Promise<void>;
499
- setToken(token: string): void;
500
- /**
501
- * Destroys all shards
502
- *
503
- * @remarks
504
- * To keep the shard(s) resumable, use the {@link CloseCodes.Resuming} code.
505
- */
506
- destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
507
- send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
508
- fetchStatus(): Awaitable<Collection<number, WebSocketShardStatus>>;
509
- [Symbol.asyncDispose](): Promise<void>;
510
- }
511
-
512
- interface FetchingStrategyOptions extends Pick<WebSocketManagerOptions, 'capabilities' | 'compression' | 'encoding' | 'handshakeTimeout' | 'helloTimeout' | 'identifyProperties' | 'initialPresence' | 'intents' | 'largeThreshold' | 'readyTimeout' | 'token' | 'useIdentifyCompression' | 'version'> {
513
- readonly gatewayInformation: APIGatewayBotInfo;
514
- readonly shardCount: number;
515
- }
516
- /**
517
- * Strategies responsible solely for making manager information accessible
518
- */
519
- interface IContextFetchingStrategy {
520
- readonly options: FetchingStrategyOptions;
521
- retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
522
- updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
523
- /**
524
- * Resolves once the given shard should be allowed to identify
525
- * This should correctly handle the signal and reject with an abort error if the operation is aborted.
526
- * Other errors will cause the shard to reconnect.
527
- */
528
- waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
529
- }
530
- declare function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions>;
531
-
532
- declare class SimpleContextFetchingStrategy implements IContextFetchingStrategy {
533
- private readonly manager;
534
- readonly options: FetchingStrategyOptions;
535
- private static throttlerCache;
536
- private static ensureThrottler;
537
- constructor(manager: WebSocketManager, options: FetchingStrategyOptions);
538
- retrieveSessionInfo(shardId: number): Promise<SessionInfo | null>;
539
- updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
540
- waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
541
- }
542
-
543
- declare class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
544
- readonly options: FetchingStrategyOptions;
545
- private readonly sessionPromises;
546
- private readonly waitForIdentifyPromises;
547
- constructor(options: FetchingStrategyOptions);
548
- retrieveSessionInfo(shardId: number): Promise<SessionInfo | null>;
549
- updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): void;
550
- waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
551
- }
552
-
553
- interface WorkerData extends FetchingStrategyOptions {
554
- shardIds: number[];
555
- }
556
- declare enum WorkerSendPayloadOp {
557
- Connect = 0,
558
- Destroy = 1,
559
- Send = 2,
560
- SessionInfoResponse = 3,
561
- ShardIdentifyResponse = 4,
562
- FetchStatus = 5
563
- }
564
- type WorkerSendPayload = {
565
- nonce: number;
566
- ok: boolean;
567
- op: WorkerSendPayloadOp.ShardIdentifyResponse;
568
- } | {
569
- nonce: number;
570
- op: WorkerSendPayloadOp.FetchStatus;
571
- shardId: number;
572
- } | {
573
- nonce: number;
574
- op: WorkerSendPayloadOp.SessionInfoResponse;
575
- session: SessionInfo | null;
576
- } | {
577
- op: WorkerSendPayloadOp.Connect;
578
- shardId: number;
579
- } | {
580
- op: WorkerSendPayloadOp.Destroy;
581
- options?: WebSocketShardDestroyOptions;
582
- shardId: number;
583
- } | {
584
- op: WorkerSendPayloadOp.Send;
585
- payload: GatewaySendPayload;
586
- shardId: number;
587
- };
588
- declare enum WorkerReceivePayloadOp {
589
- Connected = 0,
590
- Destroyed = 1,
591
- Event = 2,
592
- RetrieveSessionInfo = 3,
593
- UpdateSessionInfo = 4,
594
- WaitForIdentify = 5,
595
- FetchStatusResponse = 6,
596
- WorkerReady = 7,
597
- CancelIdentify = 8
598
- }
599
- type WorkerReceivePayload = {
600
- data: any[];
601
- event: WebSocketShardEvents;
602
- op: WorkerReceivePayloadOp.Event;
603
- shardId: number;
604
- } | {
605
- nonce: number;
606
- op: WorkerReceivePayloadOp.CancelIdentify;
607
- } | {
608
- nonce: number;
609
- op: WorkerReceivePayloadOp.FetchStatusResponse;
610
- status: WebSocketShardStatus;
611
- } | {
612
- nonce: number;
613
- op: WorkerReceivePayloadOp.RetrieveSessionInfo;
614
- shardId: number;
615
- } | {
616
- nonce: number;
617
- op: WorkerReceivePayloadOp.WaitForIdentify;
618
- shardId: number;
619
- } | {
620
- op: WorkerReceivePayloadOp.Connected;
621
- shardId: number;
622
- } | {
623
- op: WorkerReceivePayloadOp.Destroyed;
624
- shardId: number;
625
- } | {
626
- op: WorkerReceivePayloadOp.UpdateSessionInfo;
627
- session: SessionInfo | null;
628
- shardId: number;
629
- } | {
630
- op: WorkerReceivePayloadOp.WorkerReady;
631
- };
632
- /**
633
- * Options for a {@link WorkerShardingStrategy}
634
- */
635
- interface WorkerShardingStrategyOptions {
636
- /**
637
- * Dictates how many shards should be spawned per worker thread.
638
- */
639
- shardsPerWorker: number | 'all';
640
- /**
641
- * Handles a payload not recognized by the handler.
642
- */
643
- unknownPayloadHandler?(payload: any): unknown;
644
- /**
645
- * Path to the worker file to use. The worker requires quite a bit of setup, it is recommended you leverage the {@link WorkerBootstrapper} class.
646
- */
647
- workerPath?: string;
648
- }
649
- /**
650
- * Strategy used to spawn threads in worker_threads
651
- */
652
- declare class WorkerShardingStrategy implements IShardingStrategy {
653
- #private;
654
- private readonly manager;
655
- private readonly options;
656
- private readonly connectPromises;
657
- private readonly destroyPromises;
658
- private readonly fetchStatusPromises;
659
- private readonly waitForIdentifyControllers;
660
- private throttler?;
661
- constructor(manager: WebSocketManager, options: WorkerShardingStrategyOptions);
662
- /**
663
- * {@inheritDoc IShardingStrategy.spawn}
664
- */
665
- spawn(shardIds: number[]): Promise<void>;
666
- /**
667
- * {@inheritDoc IShardingStrategy.connect}
668
- */
669
- connect(): Promise<void>;
670
- /**
671
- * {@inheritDoc IShardingStrategy.destroy}
672
- */
673
- destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
674
- /**
675
- * {@inheritDoc IShardingStrategy.send}
676
- */
677
- send(shardId: number, data: GatewaySendPayload): void;
678
- /**
679
- * {@inheritDoc IShardingStrategy.fetchStatus}
680
- */
681
- fetchStatus(): Promise<Collection<number, WebSocketShardStatus>>;
682
- private setupWorker;
683
- private resolveWorkerPath;
684
- private waitForWorkerReady;
685
- private onMessage;
686
- private ensureThrottler;
687
- }
688
-
689
- /**
690
- * Options for bootstrapping the worker
691
- */
692
- interface BootstrapOptions {
693
- /**
694
- * Shard events to just arbitrarily forward to the parent thread for the manager to emit
695
- * Note: By default, this will include ALL events
696
- * you most likely want to handle dispatch within the worker itself
697
- */
698
- forwardEvents?: WebSocketShardEvents[];
699
- /**
700
- * Function to call when a shard is created for additional setup
701
- */
702
- shardCallback?(shard: WebSocketShard): Awaitable<void>;
703
- }
704
- /**
705
- * Utility class for bootstrapping a worker thread to be used for sharding
706
- */
707
- declare class WorkerBootstrapper {
708
- /**
709
- * The data passed to the worker thread
710
- */
711
- protected readonly data: WorkerData;
712
- /**
713
- * The shards that are managed by this worker
714
- */
715
- protected readonly shards: Collection<number, WebSocketShard>;
716
- constructor();
717
- /**
718
- * Helper method to initiate a shard's connection process
719
- */
720
- protected connect(shardId: number): Promise<void>;
721
- /**
722
- * Helper method to destroy a shard
723
- */
724
- protected destroy(shardId: number, options?: WebSocketShardDestroyOptions): Promise<void>;
725
- /**
726
- * Helper method to attach event listeners to the parentPort
727
- */
728
- protected setupThreadEvents(): void;
729
- /**
730
- * Bootstraps the worker thread with the provided options
731
- */
732
- bootstrap(options?: Readonly<BootstrapOptions>): Promise<void>;
733
- }
734
-
1
+ export * from './strategies/context/IContextFetchingStrategy.js';
2
+ export * from './strategies/context/SimpleContextFetchingStrategy.js';
3
+ export * from './strategies/context/WorkerContextFetchingStrategy.js';
4
+ export type * from './strategies/sharding/IShardingStrategy.js';
5
+ export * from './strategies/sharding/SimpleShardingStrategy.js';
6
+ export * from './strategies/sharding/WorkerShardingStrategy.js';
7
+ export type * from './throttling/IIdentifyThrottler.js';
8
+ export * from './throttling/SimpleIdentifyThrottler.js';
9
+ export * from './utils/constants.js';
10
+ export * from './utils/WorkerBootstrapper.js';
11
+ export * from './ws/WebSocketManager.js';
12
+ export * from './ws/WebSocketShard.js';
735
13
  /**
736
14
  * The {@link https://github.com/discordjs/discord.js/blob/main/packages/ws#readme | @discordjs/ws} version
737
15
  * that you are currently using.
738
16
  */
739
- declare const version: string;
740
-
741
- export { type BootstrapOptions, CloseCodes, CompressionMethod, CompressionParameterMap, type CreateWebSocketManagerOptions, DefaultDeviceProperty, DefaultWebSocketManagerOptions, Encoding, type FetchingStrategyOptions, type IContextFetchingStrategy, type IIdentifyThrottler, type IShardingStrategy, type IdentifyState, ImportantGatewayOpcodes, type ManagerShardEventsMap, type OptionalWebSocketManagerOptions, type RequiredWebSocketManagerOptions, type SendRateLimitState, type SessionInfo, type ShardRange, SimpleContextFetchingStrategy, SimpleIdentifyThrottler, SimpleShardingStrategy, WebSocketManager, type WebSocketManagerConnectOptions, type WebSocketManagerOptions, WebSocketShard, type WebSocketShardDestroyOptions, WebSocketShardDestroyRecovery, WebSocketShardEvents, type WebSocketShardEventsMap, WebSocketShardStatus, WorkerBootstrapper, WorkerContextFetchingStrategy, type WorkerData, type WorkerReceivePayload, WorkerReceivePayloadOp, type WorkerSendPayload, WorkerSendPayloadOp, WorkerShardingStrategy, type WorkerShardingStrategyOptions, getInitialSendRateLimitState, managerToFetchingStrategyOptions, version };
17
+ export declare const version: string;
18
+ //# sourceMappingURL=index.d.ts.map