aqualink 2.18.1 → 2.19.1
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/build/handlers/autoplay.js +148 -121
- package/build/handlers/fetchImage.js +152 -148
- package/build/index.d.ts +1401 -1080
- package/build/index.js +23 -12
- package/build/structures/Aqua.js +1085 -896
- package/build/structures/AqualinkEvents.js +43 -45
- package/build/structures/Connection.js +576 -483
- package/build/structures/Filters.js +351 -244
- package/build/structures/Node.js +666 -548
- package/build/structures/Player.js +1143 -904
- package/build/structures/Plugins.js +10 -11
- package/build/structures/Queue.js +129 -111
- package/build/structures/Rest.js +856 -645
- package/build/structures/Track.js +158 -129
- package/package.json +1 -1
package/build/structures/Node.js
CHANGED
|
@@ -1,549 +1,667 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
this.
|
|
101
|
-
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
104
|
-
|
|
105
|
-
this.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
this.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
this.
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (this.
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
this.
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
this.
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
if (
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
this.aqua
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
1
|
+
const IS_BUN = !!(process?.isBun || process?.versions?.bun || globalThis.Bun)
|
|
2
|
+
if (process && typeof process.isBun !== 'boolean') process.isBun = IS_BUN
|
|
3
|
+
|
|
4
|
+
const WebSocketImpl = process.isBun ? globalThis.WebSocket : require('ws')
|
|
5
|
+
|
|
6
|
+
const Rest = require('./Rest')
|
|
7
|
+
const { AqualinkEvents } = require('./AqualinkEvents')
|
|
8
|
+
|
|
9
|
+
const privateData = new WeakMap()
|
|
10
|
+
|
|
11
|
+
const NODE_STATE = Object.freeze({
|
|
12
|
+
IDLE: 0,
|
|
13
|
+
CONNECTING: 1,
|
|
14
|
+
READY: 2,
|
|
15
|
+
DISCONNECTING: 3,
|
|
16
|
+
RECONNECTING: 4
|
|
17
|
+
})
|
|
18
|
+
const WS_STATES = Object.freeze({
|
|
19
|
+
CONNECTING: 0,
|
|
20
|
+
OPEN: 1,
|
|
21
|
+
CLOSING: 2,
|
|
22
|
+
CLOSED: 3
|
|
23
|
+
})
|
|
24
|
+
const FATAL_CLOSE_CODES = Object.freeze([4003, 4004, 4010, 4011, 4012, 4015])
|
|
25
|
+
const WS_PATH = '/v4/websocket'
|
|
26
|
+
const LYRICS_PREFIX = 'Lyrics'
|
|
27
|
+
const OPS_STATS = 'stats'
|
|
28
|
+
const OPS_READY = 'ready'
|
|
29
|
+
const OPS_PLAYER_UPDATE = 'playerUpdate'
|
|
30
|
+
const OPS_EVENT = 'event'
|
|
31
|
+
|
|
32
|
+
const unrefTimer = (t) => {
|
|
33
|
+
try {
|
|
34
|
+
t?.unref?.()
|
|
35
|
+
} catch {}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const _functions = {
|
|
39
|
+
buildWsUrl(host, port, ssl) {
|
|
40
|
+
const needsBrackets = host.includes(':') && !host.startsWith('[')
|
|
41
|
+
return `ws${ssl ? 's' : ''}://${needsBrackets ? `[${host}]` : host}:${port}${WS_PATH}`
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
isLyricsOp(op) {
|
|
45
|
+
return typeof op === 'string' && op.startsWith(LYRICS_PREFIX)
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
reasonToString(reason) {
|
|
49
|
+
if (!reason) return 'No reason provided'
|
|
50
|
+
if (typeof reason === 'string') return reason
|
|
51
|
+
if (Buffer.isBuffer(reason)) {
|
|
52
|
+
try {
|
|
53
|
+
return reason.toString('utf8')
|
|
54
|
+
} catch {
|
|
55
|
+
return String(reason)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (reason instanceof ArrayBuffer) {
|
|
59
|
+
try {
|
|
60
|
+
return Buffer.from(reason).toString('utf8')
|
|
61
|
+
} catch {
|
|
62
|
+
return String(reason)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (ArrayBuffer.isView(reason)) {
|
|
66
|
+
try {
|
|
67
|
+
return Buffer.from(
|
|
68
|
+
reason.buffer,
|
|
69
|
+
reason.byteOffset,
|
|
70
|
+
reason.byteLength
|
|
71
|
+
).toString('utf8')
|
|
72
|
+
} catch {
|
|
73
|
+
return String(reason)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (typeof reason === 'object')
|
|
77
|
+
return reason.message || reason.code || JSON.stringify(reason)
|
|
78
|
+
return String(reason)
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
errMsg(err) {
|
|
82
|
+
return err?.message || String(err)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
class Node {
|
|
87
|
+
static BACKOFF_MULTIPLIER = 1.5
|
|
88
|
+
static MAX_BACKOFF = 60000
|
|
89
|
+
static DEFAULT_RECONNECT_TIMEOUT = 2000
|
|
90
|
+
static DEFAULT_RESUME_TIMEOUT = 60
|
|
91
|
+
static JITTER_MAX = 2000
|
|
92
|
+
static JITTER_FACTOR = 0.2
|
|
93
|
+
static WS_CLOSE_NORMAL = 1000
|
|
94
|
+
static DEFAULT_MAX_PAYLOAD = 1048576
|
|
95
|
+
static DEFAULT_HANDSHAKE_TIMEOUT = 15000
|
|
96
|
+
static INFO_FETCH_TIMEOUT = 10000
|
|
97
|
+
static INFINITE_BACKOFF = 10000
|
|
98
|
+
|
|
99
|
+
constructor(aqua, connOptions, options = {}) {
|
|
100
|
+
this.aqua = aqua
|
|
101
|
+
|
|
102
|
+
this.host = connOptions.host || 'localhost'
|
|
103
|
+
this.name = connOptions.name || this.host
|
|
104
|
+
this.port = connOptions.port || 2333
|
|
105
|
+
this.auth = connOptions.auth || 'youshallnotpass'
|
|
106
|
+
this.sessionId = connOptions.sessionId || null
|
|
107
|
+
this.regions = connOptions.regions || []
|
|
108
|
+
this.ssl = !!connOptions.ssl
|
|
109
|
+
this.wsUrl = _functions.buildWsUrl(this.host, this.port, this.ssl)
|
|
110
|
+
|
|
111
|
+
this.rest = new Rest(aqua, this)
|
|
112
|
+
|
|
113
|
+
this.resumeTimeout = options.resumeTimeout ?? Node.DEFAULT_RESUME_TIMEOUT
|
|
114
|
+
this.autoResume = options.autoResume ?? false
|
|
115
|
+
this.reconnectTimeout =
|
|
116
|
+
options.reconnectTimeout ?? Node.DEFAULT_RECONNECT_TIMEOUT
|
|
117
|
+
this.reconnectTries = options.reconnectTries ?? 3
|
|
118
|
+
this.infiniteReconnects = options.infiniteReconnects ?? false
|
|
119
|
+
this.timeout = options.timeout ?? Node.DEFAULT_HANDSHAKE_TIMEOUT
|
|
120
|
+
this.maxPayload = options.maxPayload ?? Node.DEFAULT_MAX_PAYLOAD
|
|
121
|
+
this.skipUTF8Validation = options.skipUTF8Validation ?? true
|
|
122
|
+
|
|
123
|
+
this.connected = false
|
|
124
|
+
this.state = NODE_STATE.IDLE
|
|
125
|
+
this.info = null
|
|
126
|
+
this.ws = null
|
|
127
|
+
this.reconnectAttempted = 0
|
|
128
|
+
this.reconnectTimeoutId = null
|
|
129
|
+
this.isDestroyed = false
|
|
130
|
+
this._isConnecting = false
|
|
131
|
+
this.isNodelink = false
|
|
132
|
+
|
|
133
|
+
this._wsIsBun = !!process.isBun
|
|
134
|
+
this._bunCleanup = null
|
|
135
|
+
|
|
136
|
+
this.stats = {
|
|
137
|
+
players: 0,
|
|
138
|
+
playingPlayers: 0,
|
|
139
|
+
uptime: 0,
|
|
140
|
+
ping: 0,
|
|
141
|
+
memory: { free: 0, used: 0, allocated: 0, reservable: 0 },
|
|
142
|
+
cpu: { cores: 0, systemLoad: 0, lavalinkLoad: 0 },
|
|
143
|
+
frameStats: { sent: 0, nulled: 0, deficit: 0 }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this._clientName = `Aqua/${this.aqua.version} https://github.com/ToddyTheNoobDud/AquaLink`
|
|
147
|
+
this._headers = this._buildHeaders()
|
|
148
|
+
|
|
149
|
+
privateData.set(this, {
|
|
150
|
+
boundHandlers: {
|
|
151
|
+
open: this._handleOpen.bind(this),
|
|
152
|
+
error: this._handleError.bind(this),
|
|
153
|
+
message: this._handleMessage.bind(this),
|
|
154
|
+
close: this._handleClose.bind(this),
|
|
155
|
+
connect: this.connect.bind(this)
|
|
156
|
+
}
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
_buildHeaders() {
|
|
161
|
+
const headers = {
|
|
162
|
+
Authorization: this.auth,
|
|
163
|
+
'User-Id': this.aqua.clientId,
|
|
164
|
+
'Client-Name': this._clientName
|
|
165
|
+
}
|
|
166
|
+
if (this.sessionId) headers['Session-Id'] = this.sessionId
|
|
167
|
+
return headers
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
get _boundHandlers() {
|
|
171
|
+
return privateData.get(this)?.boundHandlers
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
_clearSession() {
|
|
175
|
+
this.sessionId = null
|
|
176
|
+
delete this._headers['Session-Id']
|
|
177
|
+
this.rest?.setSessionId?.(null)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
_getPlayer(guildId) {
|
|
181
|
+
return guildId ? this.aqua?.players?.get?.(guildId) : null
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async _handleOpen() {
|
|
185
|
+
this.connected = true
|
|
186
|
+
this.state = NODE_STATE.READY
|
|
187
|
+
this._isConnecting = false
|
|
188
|
+
this.reconnectAttempted = 0
|
|
189
|
+
this._emitDebug('WebSocket connection established')
|
|
190
|
+
|
|
191
|
+
if (!this.aqua?.bypassChecks?.nodeFetchInfo && !this.info) {
|
|
192
|
+
const timeoutId = setTimeout(() => {
|
|
193
|
+
if (!this.isDestroyed) this._emitError('Node info fetch timeout')
|
|
194
|
+
}, Node.INFO_FETCH_TIMEOUT)
|
|
195
|
+
unrefTimer(timeoutId)
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
this.info = await this.rest.makeRequest('GET', '/v4/info')
|
|
199
|
+
this.isNodelink = !!this.info?.isNodelink
|
|
200
|
+
} catch (err) {
|
|
201
|
+
this.info = null
|
|
202
|
+
this._emitError(`Failed to fetch node info: ${_functions.errMsg(err)}`)
|
|
203
|
+
} finally {
|
|
204
|
+
clearTimeout(timeoutId)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
this.aqua.emit(AqualinkEvents.NodeConnect, this)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
_handleError(error) {
|
|
212
|
+
const err = error instanceof Error ? error : new Error(String(error))
|
|
213
|
+
this.aqua.emit(AqualinkEvents.NodeError, this, err)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
_handleMessage(data, isBinary) {
|
|
217
|
+
if (isBinary) return
|
|
218
|
+
|
|
219
|
+
let payload
|
|
220
|
+
try {
|
|
221
|
+
payload = JSON.parse(data)
|
|
222
|
+
} catch (err) {
|
|
223
|
+
this._emitDebug(() => `Invalid JSON from Lavalink: ${err.message}`)
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const op = payload?.op
|
|
228
|
+
if (!op) return
|
|
229
|
+
|
|
230
|
+
if (op === OPS_PLAYER_UPDATE)
|
|
231
|
+
this._emitToPlayer(AqualinkEvents.PlayerUpdate, payload)
|
|
232
|
+
else if (op === OPS_EVENT) this._emitToPlayer('event', payload)
|
|
233
|
+
else if (op === OPS_STATS) this._updateStats(payload)
|
|
234
|
+
else if (op === OPS_READY) this._handleReady(payload)
|
|
235
|
+
else this._handleCustomStringOp(op, payload)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
_emitToPlayer(eventName, payload) {
|
|
239
|
+
const player = this._getPlayer(payload?.guildId)
|
|
240
|
+
if (!player?.emit) return
|
|
241
|
+
try {
|
|
242
|
+
player.emit(eventName, payload)
|
|
243
|
+
} catch (err) {
|
|
244
|
+
this._emitError(`Player emit error: ${_functions.errMsg(err)}`)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_handleCustomStringOp(op, payload) {
|
|
249
|
+
if (_functions.isLyricsOp(op)) {
|
|
250
|
+
this.aqua.emit(
|
|
251
|
+
op,
|
|
252
|
+
this._getPlayer(payload.guildId),
|
|
253
|
+
payload.track || null,
|
|
254
|
+
payload
|
|
255
|
+
)
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
this.aqua.emit(AqualinkEvents.NodeCustomOp, this, op, payload)
|
|
259
|
+
this._emitDebug(() => `Unknown op from Lavalink: ${op}`)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
_handleClose(code, reason) {
|
|
263
|
+
this.connected = false
|
|
264
|
+
this.state = this.isDestroyed ? NODE_STATE.IDLE : NODE_STATE.RECONNECTING
|
|
265
|
+
this._isConnecting = false
|
|
266
|
+
|
|
267
|
+
this.aqua.emit(AqualinkEvents.NodeDisconnect, this, {
|
|
268
|
+
code,
|
|
269
|
+
reason: _functions.reasonToString(reason)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
if (this.isDestroyed) return
|
|
273
|
+
|
|
274
|
+
const isFatal = FATAL_CLOSE_CODES.includes(code)
|
|
275
|
+
if (
|
|
276
|
+
code !== Node.WS_CLOSE_NORMAL &&
|
|
277
|
+
code !== 1001 &&
|
|
278
|
+
!isFatal &&
|
|
279
|
+
this.sessionId
|
|
280
|
+
) {
|
|
281
|
+
this._clearSession()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const shouldReconnect =
|
|
285
|
+
(code !== Node.WS_CLOSE_NORMAL || this.infiniteReconnects) && !isFatal
|
|
286
|
+
|
|
287
|
+
if (!shouldReconnect) {
|
|
288
|
+
if (code === 4011) this._clearSession()
|
|
289
|
+
this._emitError(
|
|
290
|
+
new Error(`WebSocket closed (code ${code}). Not reconnecting.`)
|
|
291
|
+
)
|
|
292
|
+
this.destroy(true)
|
|
293
|
+
return
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
this.aqua.handleNodeFailover?.(this)
|
|
297
|
+
this._scheduleReconnect()
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
_scheduleReconnect() {
|
|
301
|
+
this._clearReconnectTimeout()
|
|
302
|
+
|
|
303
|
+
const attempt = ++this.reconnectAttempted
|
|
304
|
+
|
|
305
|
+
if (this.infiniteReconnects) {
|
|
306
|
+
this.aqua.emit(AqualinkEvents.NodeReconnect, this, {
|
|
307
|
+
infinite: true,
|
|
308
|
+
attempt,
|
|
309
|
+
backoffTime: Node.INFINITE_BACKOFF
|
|
310
|
+
})
|
|
311
|
+
this.reconnectTimeoutId = setTimeout(
|
|
312
|
+
this._boundHandlers.connect,
|
|
313
|
+
Node.INFINITE_BACKOFF
|
|
314
|
+
)
|
|
315
|
+
unrefTimer(this.reconnectTimeoutId)
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (this.reconnectAttempted > this.reconnectTries) {
|
|
320
|
+
this._emitError(
|
|
321
|
+
new Error(`Max reconnection attempts reached (${this.reconnectTries})`)
|
|
322
|
+
)
|
|
323
|
+
this.destroy(true)
|
|
324
|
+
return
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const backoffTime = this._calcBackoff(attempt)
|
|
328
|
+
this.aqua.emit(AqualinkEvents.NodeReconnect, this, {
|
|
329
|
+
infinite: false,
|
|
330
|
+
attempt,
|
|
331
|
+
backoffTime
|
|
332
|
+
})
|
|
333
|
+
this.reconnectTimeoutId = setTimeout(
|
|
334
|
+
this._boundHandlers.connect,
|
|
335
|
+
backoffTime
|
|
336
|
+
)
|
|
337
|
+
unrefTimer(this.reconnectTimeoutId)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
_calcBackoff(attempt) {
|
|
341
|
+
const baseBackoff =
|
|
342
|
+
this.reconnectTimeout *
|
|
343
|
+
Node.BACKOFF_MULTIPLIER ** Math.min(attempt, 10)
|
|
344
|
+
const maxJitter = Math.min(
|
|
345
|
+
Node.JITTER_MAX,
|
|
346
|
+
baseBackoff * Node.JITTER_FACTOR
|
|
347
|
+
)
|
|
348
|
+
return Math.min(baseBackoff + Math.random() * maxJitter, Node.MAX_BACKOFF)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
_clearReconnectTimeout() {
|
|
352
|
+
if (!this.reconnectTimeoutId) return
|
|
353
|
+
clearTimeout(this.reconnectTimeoutId)
|
|
354
|
+
this.reconnectTimeoutId = null
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
connect() {
|
|
358
|
+
if (this.isDestroyed || this._isConnecting) return
|
|
359
|
+
|
|
360
|
+
const state = this.ws?.readyState
|
|
361
|
+
if (state === WS_STATES.OPEN) {
|
|
362
|
+
this._emitDebug('WebSocket already connected')
|
|
363
|
+
return
|
|
364
|
+
}
|
|
365
|
+
if (state === WS_STATES.CONNECTING || state === WS_STATES.CLOSING) {
|
|
366
|
+
this._emitDebug('WebSocket is connecting/closing; skipping new connect')
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
this._isConnecting = true
|
|
371
|
+
this.state = NODE_STATE.CONNECTING
|
|
372
|
+
this._cleanup()
|
|
373
|
+
|
|
374
|
+
try {
|
|
375
|
+
const h = this._boundHandlers
|
|
376
|
+
|
|
377
|
+
if (this._wsIsBun) {
|
|
378
|
+
const ws = new WebSocketImpl(this.wsUrl, { headers: this._headers })
|
|
379
|
+
ws.binaryType = 'arraybuffer'
|
|
380
|
+
|
|
381
|
+
const offs = []
|
|
382
|
+
const add = (type, fn, once = false) => {
|
|
383
|
+
const wrapped = once
|
|
384
|
+
? (ev) => {
|
|
385
|
+
try {
|
|
386
|
+
ws.removeEventListener(type, wrapped)
|
|
387
|
+
} catch {}
|
|
388
|
+
fn(ev)
|
|
389
|
+
}
|
|
390
|
+
: fn
|
|
391
|
+
ws.addEventListener(type, wrapped)
|
|
392
|
+
offs.push(() => {
|
|
393
|
+
try {
|
|
394
|
+
ws.removeEventListener(type, wrapped)
|
|
395
|
+
} catch {}
|
|
396
|
+
})
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
add('open', () => h.open(), true)
|
|
400
|
+
|
|
401
|
+
add(
|
|
402
|
+
'error',
|
|
403
|
+
(event) => {
|
|
404
|
+
const err = event?.error
|
|
405
|
+
h.error(err instanceof Error ? err : new Error('WebSocket error'))
|
|
406
|
+
},
|
|
407
|
+
true
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
add('message', (event) => {
|
|
411
|
+
const data = event?.data
|
|
412
|
+
if (typeof data === 'string') h.message(data, false)
|
|
413
|
+
else h.message(data, true)
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
add(
|
|
417
|
+
'close',
|
|
418
|
+
(event) => {
|
|
419
|
+
h.close(
|
|
420
|
+
typeof event?.code === 'number'
|
|
421
|
+
? event.code
|
|
422
|
+
: Node.WS_CLOSE_NORMAL,
|
|
423
|
+
typeof event?.reason === 'string' ? event.reason : ''
|
|
424
|
+
)
|
|
425
|
+
},
|
|
426
|
+
true
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
this._bunCleanup = () => {
|
|
430
|
+
for (let i = 0; i < offs.length; i++) offs[i]()
|
|
431
|
+
}
|
|
432
|
+
this.ws = ws
|
|
433
|
+
return
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const ws = new WebSocketImpl(this.wsUrl, {
|
|
437
|
+
headers: this._headers,
|
|
438
|
+
perMessageDeflate: true,
|
|
439
|
+
handshakeTimeout: this.timeout,
|
|
440
|
+
maxPayload: this.maxPayload,
|
|
441
|
+
skipUTF8Validation: this.skipUTF8Validation
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
ws.binaryType = 'nodebuffer'
|
|
445
|
+
|
|
446
|
+
ws.once('open', h.open)
|
|
447
|
+
ws.once('error', h.error)
|
|
448
|
+
ws.on('message', h.message)
|
|
449
|
+
ws.once('close', h.close)
|
|
450
|
+
|
|
451
|
+
this.ws = ws
|
|
452
|
+
} catch (err) {
|
|
453
|
+
this._isConnecting = false
|
|
454
|
+
this._emitError(`Failed to create WebSocket: ${_functions.errMsg(err)}`)
|
|
455
|
+
this._scheduleReconnect()
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
_cleanup() {
|
|
460
|
+
const ws = this.ws
|
|
461
|
+
if (!ws) return
|
|
462
|
+
|
|
463
|
+
if (this._wsIsBun) {
|
|
464
|
+
try {
|
|
465
|
+
this._bunCleanup?.()
|
|
466
|
+
} catch {}
|
|
467
|
+
this._bunCleanup = null
|
|
468
|
+
} else {
|
|
469
|
+
ws.removeAllListeners?.()
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
try {
|
|
473
|
+
const state = ws.readyState
|
|
474
|
+
if (state === WS_STATES.OPEN || state === WS_STATES.CONNECTING) {
|
|
475
|
+
ws.close(Node.WS_CLOSE_NORMAL)
|
|
476
|
+
} else if (!this._wsIsBun && state !== WS_STATES.CLOSED) {
|
|
477
|
+
ws.terminate?.()
|
|
478
|
+
}
|
|
479
|
+
} catch (err) {
|
|
480
|
+
this._emitError(`WebSocket cleanup error: ${_functions.errMsg(err)}`)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
this.ws = null
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
destroy(clean = false) {
|
|
487
|
+
if (this.isDestroyed) return
|
|
488
|
+
|
|
489
|
+
this.isDestroyed = true
|
|
490
|
+
this.state = NODE_STATE.IDLE
|
|
491
|
+
this._isConnecting = false
|
|
492
|
+
this._clearReconnectTimeout()
|
|
493
|
+
this._cleanup()
|
|
494
|
+
|
|
495
|
+
if (!clean) this.aqua.handleNodeFailover?.(this)
|
|
496
|
+
|
|
497
|
+
this.connected = false
|
|
498
|
+
this.aqua.destroyNode?.(this.name)
|
|
499
|
+
this.aqua.emit(AqualinkEvents.NodeDestroy, this)
|
|
500
|
+
|
|
501
|
+
this.rest?.destroy?.()
|
|
502
|
+
|
|
503
|
+
this.info = null
|
|
504
|
+
this.rest = null
|
|
505
|
+
this.aqua = null
|
|
506
|
+
this._headers = null
|
|
507
|
+
this.stats = null
|
|
508
|
+
|
|
509
|
+
privateData.delete(this)
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
async getStats() {
|
|
513
|
+
if (this.connected) return this.stats
|
|
514
|
+
|
|
515
|
+
try {
|
|
516
|
+
const newStats = await this.rest.getStats()
|
|
517
|
+
if (newStats) this._updateStats(newStats)
|
|
518
|
+
} catch (err) {
|
|
519
|
+
this._emitError(`Failed to fetch node stats: ${_functions.errMsg(err)}`)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return this.stats
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
_updateStats(payload) {
|
|
526
|
+
if (!payload) return
|
|
527
|
+
const s = this.stats
|
|
528
|
+
|
|
529
|
+
if (payload.players !== undefined) s.players = payload.players
|
|
530
|
+
if (payload.playingPlayers !== undefined)
|
|
531
|
+
s.playingPlayers = payload.playingPlayers
|
|
532
|
+
if (payload.uptime !== undefined) s.uptime = payload.uptime
|
|
533
|
+
if (payload.ping !== undefined) s.ping = payload.ping
|
|
534
|
+
|
|
535
|
+
if (payload.memory) {
|
|
536
|
+
const m = s.memory,
|
|
537
|
+
pm = payload.memory
|
|
538
|
+
if (pm.free !== undefined) m.free = pm.free
|
|
539
|
+
if (pm.used !== undefined) m.used = pm.used
|
|
540
|
+
if (pm.allocated !== undefined) m.allocated = pm.allocated
|
|
541
|
+
if (pm.reservable !== undefined) m.reservable = pm.reservable
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
if (payload.cpu) {
|
|
545
|
+
const c = s.cpu,
|
|
546
|
+
pc = payload.cpu
|
|
547
|
+
if (pc.cores !== undefined) c.cores = pc.cores
|
|
548
|
+
if (pc.systemLoad !== undefined) c.systemLoad = pc.systemLoad
|
|
549
|
+
if (pc.lavalinkLoad !== undefined) c.lavalinkLoad = pc.lavalinkLoad
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
if (payload.frameStats) {
|
|
553
|
+
const f = s.frameStats,
|
|
554
|
+
pf = payload.frameStats
|
|
555
|
+
if (pf.sent !== undefined) f.sent = pf.sent
|
|
556
|
+
if (pf.nulled !== undefined) f.nulled = pf.nulled
|
|
557
|
+
if (pf.deficit !== undefined) f.deficit = pf.deficit
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
async _handleReady(payload) {
|
|
562
|
+
const sessionId = payload?.sessionId
|
|
563
|
+
if (!sessionId) {
|
|
564
|
+
this._emitError('Ready payload missing sessionId')
|
|
565
|
+
return
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const oldSessionId = this.sessionId
|
|
569
|
+
const sessionChanged =
|
|
570
|
+
oldSessionId && oldSessionId !== sessionId && !payload.resumed
|
|
571
|
+
|
|
572
|
+
this.sessionId = sessionId
|
|
573
|
+
this.rest.setSessionId(sessionId)
|
|
574
|
+
this._headers['Session-Id'] = sessionId
|
|
575
|
+
|
|
576
|
+
if (sessionChanged && this.aqua?.players) {
|
|
577
|
+
this._emitDebug(
|
|
578
|
+
`Session changed from ${oldSessionId} to ${sessionId}, invalidating stale players`
|
|
579
|
+
)
|
|
580
|
+
const playersToDestroy = []
|
|
581
|
+
for (const [guildId, player] of this.aqua.players) {
|
|
582
|
+
if (player?.nodes === this || player?.nodes?.name === this.name) {
|
|
583
|
+
playersToDestroy.push(guildId)
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const guildId of playersToDestroy) {
|
|
587
|
+
try {
|
|
588
|
+
this._emitDebug(`Destroying stale player for guild ${guildId}`)
|
|
589
|
+
await this.aqua.destroyPlayer(guildId)
|
|
590
|
+
} catch (e) {
|
|
591
|
+
this._emitDebug(
|
|
592
|
+
`Failed to destroy stale player ${guildId}: ${e?.message || e}`
|
|
593
|
+
)
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
this.aqua.emit(AqualinkEvents.NodeReady, this, {
|
|
599
|
+
resumed: !!payload.resumed,
|
|
600
|
+
sessionChanged
|
|
601
|
+
})
|
|
602
|
+
|
|
603
|
+
if (this.autoResume) {
|
|
604
|
+
setImmediate(() => {
|
|
605
|
+
this._resumePlayers().catch((err) => {
|
|
606
|
+
this._emitError(`_resumePlayers failed: ${_functions.errMsg(err)}`)
|
|
607
|
+
})
|
|
608
|
+
})
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
async _resumePlayers() {
|
|
613
|
+
if (!this.sessionId) return
|
|
614
|
+
|
|
615
|
+
try {
|
|
616
|
+
await this.rest.makeRequest('PATCH', `/v4/sessions/${this.sessionId}`, {
|
|
617
|
+
resuming: true,
|
|
618
|
+
timeout: this.resumeTimeout
|
|
619
|
+
})
|
|
620
|
+
|
|
621
|
+
if (this.aqua?.players) {
|
|
622
|
+
for (const [guildId, player] of this.aqua.players) {
|
|
623
|
+
if (
|
|
624
|
+
(player?.nodes === this || player?.nodes?.name === this.name) &&
|
|
625
|
+
player.voiceChannel
|
|
626
|
+
) {
|
|
627
|
+
try {
|
|
628
|
+
this._emitDebug(`Rejoining voice for guild ${guildId} on resume`)
|
|
629
|
+
player.connect({
|
|
630
|
+
voiceChannel: player.voiceChannel,
|
|
631
|
+
deaf: player.deaf,
|
|
632
|
+
mute: player.mute
|
|
633
|
+
})
|
|
634
|
+
} catch (e) {
|
|
635
|
+
this._emitDebug(
|
|
636
|
+
`Failed to rejoin voice for ${guildId}: ${e?.message || e}`
|
|
637
|
+
)
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (this.aqua.loadPlayers && this.aqua.players.size === 0) {
|
|
644
|
+
await this.aqua.loadPlayers()
|
|
645
|
+
}
|
|
646
|
+
} catch (err) {
|
|
647
|
+
this._emitError(`Failed to resume session: ${_functions.errMsg(err)}`)
|
|
648
|
+
throw err
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
_emitError(error) {
|
|
653
|
+
const errorObj = error instanceof Error ? error : new Error(String(error))
|
|
654
|
+
this.aqua.emit(AqualinkEvents.Error, this, errorObj)
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
_emitDebug(message) {
|
|
658
|
+
if (!this.aqua?.listenerCount?.(AqualinkEvents.Debug)) return
|
|
659
|
+
this.aqua.emit(
|
|
660
|
+
AqualinkEvents.Debug,
|
|
661
|
+
this.name,
|
|
662
|
+
typeof message === 'function' ? message() : message
|
|
663
|
+
)
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
549
667
|
module.exports = Node
|