@discordjs/ws 2.0.3 → 3.0.0-core-gateway-rl.1762368996-5fa92a1ea

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.
package/README.md CHANGED
@@ -5,12 +5,13 @@
5
5
  </p>
6
6
  <br />
7
7
  <p>
8
- <a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
8
+ <a href="https://discord.gg/djs"><img src="https://img.shields.io/badge/join_us-on_discord-5865F2?logo=discord&logoColor=white" alt="Discord server" /></a>
9
9
  <a href="https://www.npmjs.com/package/@discordjs/ws"><img src="https://img.shields.io/npm/v/@discordjs/ws.svg?maxAge=3600" alt="npm version" /></a>
10
10
  <a href="https://www.npmjs.com/package/@discordjs/ws"><img src="https://img.shields.io/npm/dt/@discordjs/ws.svg?maxAge=3600" alt="npm downloads" /></a>
11
- <a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
12
- <a href="https://github.com/discordjs/discord.js/commits/main/packages/ws"><img alt="Last commit." src="https://img.shields.io/github/last-commit/discordjs/discord.js?logo=github&logoColor=ffffff&path=packages%2Fws"></a>
11
+ <a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/tests.yml/badge.svg" alt="Build status" /></a>
12
+ <a href="https://github.com/discordjs/discord.js/commits/main/packages/ws"><img alt="Last commit." src="https://img.shields.io/github/last-commit/discordjs/discord.js?logo=github&logoColor=ffffff&path=packages%2Fws" /></a>
13
13
  <a href="https://codecov.io/gh/discordjs/discord.js"><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=ws" alt="Code coverage" /></a>
14
+ <a href="https://opencollective.com/discordjs"><img src="https://img.shields.io/opencollective/backers/discordjs?maxAge=3600&logo=opencollective" alt="backers" /></a>
14
15
  </p>
15
16
  <p>
16
17
  <a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
@@ -24,7 +25,7 @@
24
25
 
25
26
  ## Installation
26
27
 
27
- **Node.js 20 or newer is required.**
28
+ **Node.js 22.12.0 or newer is required.**
28
29
 
29
30
  ```sh
30
31
  npm install @discordjs/ws
@@ -40,16 +41,21 @@ bun add @discordjs/ws
40
41
 
41
42
  ## Example usage
42
43
 
44
+ The example uses [ES modules](https://nodejs.org/api/esm.html#enabling).
45
+
43
46
  ```ts
44
47
  import { WebSocketManager, WebSocketShardEvents, CompressionMethod } from '@discordjs/ws';
45
48
  import { REST } from '@discordjs/rest';
49
+ import type { RESTGetAPIGatewayBotResult } from 'discord-api-types/v10';
46
50
 
47
51
  const rest = new REST().setToken(process.env.DISCORD_TOKEN);
48
52
  // This example will spawn Discord's recommended shard count, all under the current process.
49
53
  const manager = new WebSocketManager({
50
54
  token: process.env.DISCORD_TOKEN,
51
55
  intents: 0, // for no intents
52
- rest,
56
+ fetchGatewayInformation() {
57
+ return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
58
+ },
53
59
  // uncomment if you have zlib-sync installed and want to use compression
54
60
  // compression: CompressionMethod.ZlibSync,
55
61
 
@@ -71,8 +77,10 @@ await manager.connect();
71
77
  const manager = new WebSocketManager({
72
78
  token: process.env.DISCORD_TOKEN,
73
79
  intents: 0,
74
- rest,
75
80
  shardCount: 4,
81
+ fetchGatewayInformation() {
82
+ return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
83
+ },
76
84
  });
77
85
 
78
86
  // The manager also supports being responsible for only a subset of your shards:
@@ -82,21 +90,25 @@ const manager = new WebSocketManager({
82
90
  const manager = new WebSocketManager({
83
91
  token: process.env.DISCORD_TOKEN,
84
92
  intents: 0,
85
- rest,
86
93
  shardCount: 8,
87
94
  shardIds: [0, 2, 4, 6],
95
+ fetchGatewayInformation() {
96
+ return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
97
+ },
88
98
  });
89
99
 
90
100
  // Alternatively, if your shards are consecutive, you can pass in a range
91
101
  const manager = new WebSocketManager({
92
102
  token: process.env.DISCORD_TOKEN,
93
103
  intents: 0,
94
- rest,
95
104
  shardCount: 8,
96
105
  shardIds: {
97
106
  start: 0,
98
107
  end: 4,
99
108
  },
109
+ fetchGatewayInformation() {
110
+ return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
111
+ },
100
112
  });
101
113
  ```
102
114
 
@@ -112,8 +124,10 @@ const rest = new REST().setToken(process.env.DISCORD_TOKEN);
112
124
  const manager = new WebSocketManager({
113
125
  token: process.env.DISCORD_TOKEN,
114
126
  intents: 0,
115
- rest,
116
127
  shardCount: 6,
128
+ fetchGatewayInformation() {
129
+ return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
130
+ },
117
131
  // This will cause 3 workers to spawn, 2 shards per each
118
132
  buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 }),
119
133
  // Or maybe you want all your shards under a single worker
@@ -131,7 +145,9 @@ const rest = new REST().setToken(process.env.DISCORD_TOKEN);
131
145
  const manager = new WebSocketManager({
132
146
  token: process.env.DISCORD_TOKEN,
133
147
  intents: 0,
134
- rest,
148
+ fetchGatewayInformation() {
149
+ return rest.get(Routes.gatewayBot()) as Promise<RESTGetAPIGatewayBotResult>;
150
+ },
135
151
  buildStrategy: (manager) =>
136
152
  new WorkerShardingStrategy(manager, {
137
153
  shardsPerWorker: 2,
@@ -179,7 +195,7 @@ parentPort!.postMessage({ custom: 'data' });
179
195
  - [Guide][guide] ([source][guide-source])
180
196
  Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
181
197
  - [discord.js Discord server][discord]
182
- - [Discord API Discord server][discord-api]
198
+ - [Discord Developers Discord server][discord-developers]
183
199
  - [GitHub][source]
184
200
  - [npm][npm]
185
201
  - [Related libraries][related-libs]
@@ -197,11 +213,11 @@ If you don't understand something in the documentation, you are experiencing pro
197
213
  [website]: https://discord.js.org
198
214
  [website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
199
215
  [documentation]: https://discord.js.org/docs/packages/ws/stable
200
- [guide]: https://discordjs.guide/
201
- [guide-source]: https://github.com/discordjs/guide
202
- [guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
216
+ [guide]: https://discordjs.guide
217
+ [guide-source]: https://github.com/discordjs/discord.js/tree/main/apps/guide
218
+ [guide-update]: https://discordjs.guide/legacy/additional-info/changes-in-v14
203
219
  [discord]: https://discord.gg/djs
204
- [discord-api]: https://discord.gg/discord-api
220
+ [discord-developers]: https://discord.gg/discord-developers
205
221
  [source]: https://github.com/discordjs/discord.js/tree/main/packages/ws
206
222
  [npm]: https://www.npmjs.com/package/@discordjs/ws
207
223
  [related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
@@ -39,19 +39,19 @@ var import_collection = require("@discordjs/collection");
39
39
 
40
40
  // src/strategies/context/IContextFetchingStrategy.ts
41
41
  async function managerToFetchingStrategyOptions(manager) {
42
- const {
43
- buildIdentifyThrottler,
44
- buildStrategy,
45
- retrieveSessionInfo,
46
- updateSessionInfo,
47
- shardCount,
48
- shardIds,
49
- rest,
50
- ...managerOptions
51
- } = manager.options;
52
42
  return {
53
- ...managerOptions,
43
+ compression: manager.options.compression,
44
+ encoding: manager.options.encoding,
45
+ handshakeTimeout: manager.options.handshakeTimeout,
46
+ helloTimeout: manager.options.helloTimeout,
47
+ identifyProperties: manager.options.identifyProperties,
48
+ initialPresence: manager.options.initialPresence,
49
+ intents: manager.options.intents,
50
+ largeThreshold: manager.options.largeThreshold,
51
+ readyTimeout: manager.options.readyTimeout,
54
52
  token: manager.token,
53
+ useIdentifyCompression: manager.options.useIdentifyCompression,
54
+ version: manager.options.version,
55
55
  gatewayInformation: await manager.fetchGatewayInformation(),
56
56
  shardCount: await manager.getShardCount()
57
57
  };
@@ -271,16 +271,14 @@ var SimpleIdentifyThrottler = class {
271
271
  */
272
272
  async waitForIdentify(shardId, signal) {
273
273
  const key = shardId % this.maxConcurrency;
274
- const state = this.states.ensure(key, () => {
275
- return {
276
- queue: new import_async_queue.AsyncQueue(),
277
- resetsAt: Number.POSITIVE_INFINITY
278
- };
279
- });
274
+ const state = this.states.ensure(key, () => ({
275
+ queue: new import_async_queue.AsyncQueue(),
276
+ resetsAt: Number.POSITIVE_INFINITY
277
+ }));
280
278
  await state.queue.wait({ signal });
281
279
  try {
282
280
  const diff = state.resetsAt - Date.now();
283
- if (diff <= 5e3) {
281
+ if (diff > 0 && diff <= 5e3) {
284
282
  const time = diff + Math.random() * 1500;
285
283
  await (0, import_promises.setTimeout)(time);
286
284
  }
@@ -431,8 +429,6 @@ var WebSocketShard = class extends import_async_event_emitter.AsyncEventEmitter
431
429
  void this.internalConnect();
432
430
  try {
433
431
  await promise;
434
- } catch ({ error }) {
435
- throw error;
436
432
  } finally {
437
433
  controller.abort();
438
434
  }
@@ -789,10 +785,20 @@ var WebSocketShard = class extends import_async_event_emitter.AsyncEventEmitter
789
785
  const zLibSync = await getZlibSync();
790
786
  this.zLibSyncInflate.push(import_node_buffer.Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);
791
787
  if (this.zLibSyncInflate.err) {
792
- this.emit(
793
- "error" /* Error */,
794
- new Error(`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ""}`)
795
- );
788
+ const ZlibErrorCodes = {
789
+ [zLibSync.Z_NEED_DICT]: "Z_NEED_DICT",
790
+ [zLibSync.Z_STREAM_END]: "Z_STREAM_END",
791
+ [zLibSync.Z_ERRNO]: "Z_ERRNO",
792
+ [zLibSync.Z_STREAM_ERROR]: "Z_STREAM_ERROR",
793
+ [zLibSync.Z_DATA_ERROR]: "Z_DATA_ERROR",
794
+ [zLibSync.Z_MEM_ERROR]: "Z_MEM_ERROR",
795
+ [zLibSync.Z_BUF_ERROR]: "Z_BUF_ERROR",
796
+ [zLibSync.Z_VERSION_ERROR]: "Z_VERSION_ERROR"
797
+ };
798
+ const error = new Error(this.zLibSyncInflate.msg ?? void 0);
799
+ error.errno = this.zLibSyncInflate.err;
800
+ error.code = ZlibErrorCodes[this.zLibSyncInflate.err];
801
+ this.emit("error" /* Error */, error);
796
802
  }
797
803
  if (!flush) {
798
804
  return null;