audio-channel-queue 1.8.0 → 1.9.0
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/dist/core.js +4 -3
- package/dist/errors.js +27 -17
- package/dist/events.js +21 -14
- package/dist/index.d.ts +5 -5
- package/dist/index.js +3 -2
- package/dist/info.js +8 -7
- package/dist/pause.d.ts +24 -12
- package/dist/pause.js +93 -41
- package/dist/types.d.ts +12 -3
- package/dist/types.js +6 -1
- package/dist/utils.js +4 -3
- package/dist/volume.js +37 -15
- package/package.json +8 -3
- package/src/core.ts +59 -42
- package/src/errors.ts +504 -480
- package/src/events.ts +36 -27
- package/src/index.ts +47 -43
- package/src/info.ts +23 -22
- package/src/pause.ts +168 -85
- package/src/types.ts +12 -2
- package/src/utils.ts +7 -7
- package/src/volume.ts +47 -30
package/src/errors.ts
CHANGED
|
@@ -1,480 +1,504 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Error handling, retry logic, and recovery mechanisms for the audio-channel-queue package
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
*
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
-
channel
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
return '
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
audio
|
|
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
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
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
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Error handling, retry logic, and recovery mechanisms for the audio-channel-queue package
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
AudioErrorInfo,
|
|
7
|
+
AudioErrorCallback,
|
|
8
|
+
RetryConfig,
|
|
9
|
+
ErrorRecoveryOptions,
|
|
10
|
+
ExtendedAudioQueueChannel
|
|
11
|
+
} from './types';
|
|
12
|
+
import { audioChannels } from './info';
|
|
13
|
+
import { extractFileName } from './utils';
|
|
14
|
+
|
|
15
|
+
let globalRetryConfig: RetryConfig = {
|
|
16
|
+
baseDelay: 1000,
|
|
17
|
+
enabled: true,
|
|
18
|
+
exponentialBackoff: true,
|
|
19
|
+
maxRetries: 3,
|
|
20
|
+
skipOnFailure: false,
|
|
21
|
+
timeoutMs: 10000
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
let globalErrorRecovery: ErrorRecoveryOptions = {
|
|
25
|
+
autoRetry: true,
|
|
26
|
+
fallbackToNextTrack: true,
|
|
27
|
+
logErrorsToAnalytics: false,
|
|
28
|
+
preserveQueueOnError: true,
|
|
29
|
+
showUserFeedback: false
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const retryAttempts: WeakMap<HTMLAudioElement, number> = new WeakMap();
|
|
33
|
+
|
|
34
|
+
const loadTimeouts: WeakMap<HTMLAudioElement, number> = new WeakMap();
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Subscribes to audio error events for a specific channel
|
|
38
|
+
* @param channelNumber - The channel number to listen to (defaults to 0)
|
|
39
|
+
* @param callback - Function to call when an audio error occurs
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* onAudioError(0, (errorInfo) => {
|
|
43
|
+
* console.log(`Audio error: ${errorInfo.error.message}`);
|
|
44
|
+
* console.log(`Error type: ${errorInfo.errorType}`);
|
|
45
|
+
* });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export const onAudioError = (channelNumber: number = 0, callback: AudioErrorCallback): void => {
|
|
49
|
+
// Ensure channel exists
|
|
50
|
+
while (audioChannels.length <= channelNumber) {
|
|
51
|
+
audioChannels.push({
|
|
52
|
+
audioCompleteCallbacks: new Set(),
|
|
53
|
+
audioErrorCallbacks: new Set(),
|
|
54
|
+
audioPauseCallbacks: new Set(),
|
|
55
|
+
audioResumeCallbacks: new Set(),
|
|
56
|
+
audioStartCallbacks: new Set(),
|
|
57
|
+
isPaused: false,
|
|
58
|
+
progressCallbacks: new Map(),
|
|
59
|
+
queue: [],
|
|
60
|
+
queueChangeCallbacks: new Set(),
|
|
61
|
+
volume: 1.0
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
66
|
+
if (!channel.audioErrorCallbacks) {
|
|
67
|
+
channel.audioErrorCallbacks = new Set();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
channel.audioErrorCallbacks.add(callback);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Unsubscribes from audio error events for a specific channel
|
|
75
|
+
* @param channelNumber - The channel number to stop listening to (defaults to 0)
|
|
76
|
+
* @param callback - The specific callback to remove (optional - if not provided, removes all)
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* offAudioError(0); // Remove all error callbacks for channel 0
|
|
80
|
+
* offAudioError(0, specificCallback); // Remove specific callback
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export const offAudioError = (channelNumber: number = 0, callback?: AudioErrorCallback): void => {
|
|
84
|
+
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
85
|
+
if (!channel?.audioErrorCallbacks) return;
|
|
86
|
+
|
|
87
|
+
if (callback) {
|
|
88
|
+
channel.audioErrorCallbacks.delete(callback);
|
|
89
|
+
} else {
|
|
90
|
+
channel.audioErrorCallbacks.clear();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Sets the global retry configuration for audio loading failures
|
|
96
|
+
* @param config - Retry configuration options
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* setRetryConfig({
|
|
100
|
+
* enabled: true,
|
|
101
|
+
* maxRetries: 5,
|
|
102
|
+
* baseDelay: 1000,
|
|
103
|
+
* exponentialBackoff: true,
|
|
104
|
+
* timeoutMs: 15000,
|
|
105
|
+
* fallbackUrls: ['https://cdn.backup.com/audio/'],
|
|
106
|
+
* skipOnFailure: true
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export const setRetryConfig = (config: Partial<RetryConfig>): void => {
|
|
111
|
+
globalRetryConfig = { ...globalRetryConfig, ...config };
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Gets the current global retry configuration
|
|
116
|
+
* @returns Current retry configuration
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* const config = getRetryConfig();
|
|
120
|
+
* console.log(`Max retries: ${config.maxRetries}`);
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export const getRetryConfig = (): RetryConfig => {
|
|
124
|
+
return { ...globalRetryConfig };
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Sets the global error recovery configuration
|
|
129
|
+
* @param options - Error recovery options
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* setErrorRecovery({
|
|
133
|
+
* autoRetry: true,
|
|
134
|
+
* showUserFeedback: true,
|
|
135
|
+
* logErrorsToAnalytics: true,
|
|
136
|
+
* preserveQueueOnError: true,
|
|
137
|
+
* fallbackToNextTrack: true
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export const setErrorRecovery = (options: Partial<ErrorRecoveryOptions>): void => {
|
|
142
|
+
globalErrorRecovery = { ...globalErrorRecovery, ...options };
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Gets the current global error recovery configuration
|
|
147
|
+
* @returns Current error recovery configuration
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* const recovery = getErrorRecovery();
|
|
151
|
+
* console.log(`Auto retry enabled: ${recovery.autoRetry}`);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export const getErrorRecovery = (): ErrorRecoveryOptions => {
|
|
155
|
+
return { ...globalErrorRecovery };
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Manually retries loading failed audio for a specific channel
|
|
160
|
+
* @param channelNumber - The channel number to retry (defaults to 0)
|
|
161
|
+
* @returns Promise that resolves to true if retry was successful, false otherwise
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const success = await retryFailedAudio(0);
|
|
165
|
+
* if (success) {
|
|
166
|
+
* console.log('Audio retry successful');
|
|
167
|
+
* } else {
|
|
168
|
+
* console.log('Audio retry failed');
|
|
169
|
+
* }
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export const retryFailedAudio = async (channelNumber: number = 0): Promise<boolean> => {
|
|
173
|
+
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
174
|
+
if (!channel || channel.queue.length === 0) return false;
|
|
175
|
+
|
|
176
|
+
const currentAudio: HTMLAudioElement = channel.queue[0];
|
|
177
|
+
const currentAttempts = retryAttempts.get(currentAudio) ?? 0;
|
|
178
|
+
|
|
179
|
+
if (currentAttempts >= globalRetryConfig.maxRetries) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
// Reset the audio element
|
|
185
|
+
currentAudio.currentTime = 0;
|
|
186
|
+
await currentAudio.play();
|
|
187
|
+
|
|
188
|
+
// Reset retry counter on successful play
|
|
189
|
+
retryAttempts.delete(currentAudio);
|
|
190
|
+
return true;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
// eslint-disable-next-line no-console
|
|
193
|
+
console.error(`Error in retryFailedAudio: ${error}`);
|
|
194
|
+
// Increment retry counter
|
|
195
|
+
retryAttempts.set(currentAudio, currentAttempts + 1);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Emits an audio error event to all registered listeners for a specific channel
|
|
202
|
+
* @param channelNumber - The channel number where the error occurred
|
|
203
|
+
* @param errorInfo - Information about the error
|
|
204
|
+
* @param audioChannels - Array of audio channels
|
|
205
|
+
* @internal
|
|
206
|
+
*/
|
|
207
|
+
export const emitAudioError = (
|
|
208
|
+
channelNumber: number,
|
|
209
|
+
errorInfo: AudioErrorInfo,
|
|
210
|
+
audioChannels: ExtendedAudioQueueChannel[]
|
|
211
|
+
): void => {
|
|
212
|
+
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
213
|
+
if (!channel?.audioErrorCallbacks) return;
|
|
214
|
+
|
|
215
|
+
// Log to analytics if enabled
|
|
216
|
+
if (globalErrorRecovery.logErrorsToAnalytics) {
|
|
217
|
+
// eslint-disable-next-line no-console
|
|
218
|
+
console.warn('Audio Error Analytics:', errorInfo);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
channel.audioErrorCallbacks.forEach((callback) => {
|
|
222
|
+
try {
|
|
223
|
+
callback(errorInfo);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
// eslint-disable-next-line no-console
|
|
226
|
+
console.error('Error in audio error callback:', error);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Determines the error type based on the error object and context
|
|
233
|
+
* @param error - The error that occurred
|
|
234
|
+
* @param audio - The audio element that failed
|
|
235
|
+
* @returns The categorized error type
|
|
236
|
+
* @internal
|
|
237
|
+
*/
|
|
238
|
+
export const categorizeError = (
|
|
239
|
+
error: Error,
|
|
240
|
+
audio: HTMLAudioElement
|
|
241
|
+
): AudioErrorInfo['errorType'] => {
|
|
242
|
+
const errorMessage = error.message.toLowerCase();
|
|
243
|
+
|
|
244
|
+
if (errorMessage.includes('network') || errorMessage.includes('fetch')) {
|
|
245
|
+
return 'network';
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Check for unsupported format first (more specific than decode)
|
|
249
|
+
if (
|
|
250
|
+
errorMessage.includes('not supported') ||
|
|
251
|
+
errorMessage.includes('unsupported') ||
|
|
252
|
+
errorMessage.includes('format not supported')
|
|
253
|
+
) {
|
|
254
|
+
return 'unsupported';
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (errorMessage.includes('decode') || errorMessage.includes('format')) {
|
|
258
|
+
return 'decode';
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (errorMessage.includes('permission') || errorMessage.includes('blocked')) {
|
|
262
|
+
return 'permission';
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (errorMessage.includes('abort')) {
|
|
266
|
+
return 'abort';
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (errorMessage.includes('timeout')) {
|
|
270
|
+
return 'timeout';
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Check audio element network state for more context
|
|
274
|
+
if (audio.networkState === HTMLMediaElement.NETWORK_NO_SOURCE) {
|
|
275
|
+
return 'network';
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (audio.networkState === HTMLMediaElement.NETWORK_LOADING) {
|
|
279
|
+
return 'timeout';
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return 'unknown';
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Sets up comprehensive error handling for an audio element
|
|
287
|
+
* @param audio - The audio element to set up error handling for
|
|
288
|
+
* @param channelNumber - The channel number this audio belongs to
|
|
289
|
+
* @param originalUrl - The original URL that was requested
|
|
290
|
+
* @param onError - Callback for when an error occurs
|
|
291
|
+
* @internal
|
|
292
|
+
*/
|
|
293
|
+
export const setupAudioErrorHandling = (
|
|
294
|
+
audio: HTMLAudioElement,
|
|
295
|
+
channelNumber: number,
|
|
296
|
+
originalUrl: string,
|
|
297
|
+
onError?: (error: Error) => Promise<void>
|
|
298
|
+
): void => {
|
|
299
|
+
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
300
|
+
if (!channel) return;
|
|
301
|
+
|
|
302
|
+
// Set up loading timeout with test environment compatibility
|
|
303
|
+
let timeoutId: number;
|
|
304
|
+
if (typeof setTimeout !== 'undefined') {
|
|
305
|
+
timeoutId = setTimeout(() => {
|
|
306
|
+
if (audio.networkState === HTMLMediaElement.NETWORK_LOADING) {
|
|
307
|
+
const timeoutError = new Error(
|
|
308
|
+
`Audio loading timeout after ${globalRetryConfig.timeoutMs}ms`
|
|
309
|
+
);
|
|
310
|
+
handleAudioError(audio, channelNumber, originalUrl, timeoutError);
|
|
311
|
+
}
|
|
312
|
+
}, globalRetryConfig.timeoutMs) as unknown as number;
|
|
313
|
+
|
|
314
|
+
loadTimeouts.set(audio, timeoutId);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Clear timeout when metadata loads successfully
|
|
318
|
+
const handleLoadSuccess = (): void => {
|
|
319
|
+
if (typeof setTimeout !== 'undefined') {
|
|
320
|
+
const timeoutId = loadTimeouts.get(audio);
|
|
321
|
+
if (timeoutId) {
|
|
322
|
+
clearTimeout(timeoutId);
|
|
323
|
+
loadTimeouts.delete(audio);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// Handle various error events
|
|
329
|
+
const handleError = (_event: Event): void => {
|
|
330
|
+
if (typeof setTimeout !== 'undefined') {
|
|
331
|
+
const timeoutId = loadTimeouts.get(audio);
|
|
332
|
+
if (timeoutId) {
|
|
333
|
+
clearTimeout(timeoutId);
|
|
334
|
+
loadTimeouts.delete(audio);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const error = new Error(`Audio loading failed: ${audio.error?.message || 'Unknown error'}`);
|
|
339
|
+
handleAudioError(audio, channelNumber, originalUrl, error);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const handleAbort = (): void => {
|
|
343
|
+
const error = new Error('Audio loading was aborted');
|
|
344
|
+
handleAudioError(audio, channelNumber, originalUrl, error);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const handleStall = (): void => {
|
|
348
|
+
const error = new Error('Audio loading stalled');
|
|
349
|
+
handleAudioError(audio, channelNumber, originalUrl, error);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
// Add event listeners
|
|
353
|
+
audio.addEventListener('error', handleError);
|
|
354
|
+
audio.addEventListener('abort', handleAbort);
|
|
355
|
+
audio.addEventListener('stalled', handleStall);
|
|
356
|
+
audio.addEventListener('loadedmetadata', handleLoadSuccess);
|
|
357
|
+
audio.addEventListener('canplay', handleLoadSuccess);
|
|
358
|
+
|
|
359
|
+
// Custom play error handling
|
|
360
|
+
if (onError) {
|
|
361
|
+
const originalPlay = audio.play.bind(audio);
|
|
362
|
+
const wrappedPlay = async (): Promise<void> => {
|
|
363
|
+
try {
|
|
364
|
+
await originalPlay();
|
|
365
|
+
} catch (error) {
|
|
366
|
+
await onError(error as Error);
|
|
367
|
+
throw error;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
audio.play = wrappedPlay;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Handles audio errors with retry logic and recovery mechanisms
|
|
377
|
+
* @param audio - The audio element that failed
|
|
378
|
+
* @param channelNumber - The channel number
|
|
379
|
+
* @param originalUrl - The original URL that was requested
|
|
380
|
+
* @param error - The error that occurred
|
|
381
|
+
* @internal
|
|
382
|
+
*/
|
|
383
|
+
export const handleAudioError = async (
|
|
384
|
+
audio: HTMLAudioElement,
|
|
385
|
+
channelNumber: number,
|
|
386
|
+
originalUrl: string,
|
|
387
|
+
error: Error
|
|
388
|
+
): Promise<void> => {
|
|
389
|
+
const channel: ExtendedAudioQueueChannel = audioChannels[channelNumber];
|
|
390
|
+
if (!channel) return;
|
|
391
|
+
|
|
392
|
+
const currentAttempts = retryAttempts.get(audio) ?? 0;
|
|
393
|
+
const retryConfig = channel.retryConfig ?? globalRetryConfig;
|
|
394
|
+
|
|
395
|
+
const errorInfo: AudioErrorInfo = {
|
|
396
|
+
channelNumber,
|
|
397
|
+
error,
|
|
398
|
+
errorType: categorizeError(error, audio),
|
|
399
|
+
fileName: extractFileName(originalUrl),
|
|
400
|
+
remainingInQueue: channel.queue.length - 1,
|
|
401
|
+
retryAttempt: currentAttempts,
|
|
402
|
+
src: originalUrl,
|
|
403
|
+
timestamp: Date.now()
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// Emit error event
|
|
407
|
+
emitAudioError(channelNumber, errorInfo, audioChannels);
|
|
408
|
+
|
|
409
|
+
// Attempt retry if enabled and within limits
|
|
410
|
+
if (
|
|
411
|
+
retryConfig.enabled &&
|
|
412
|
+
currentAttempts < retryConfig.maxRetries &&
|
|
413
|
+
globalErrorRecovery.autoRetry
|
|
414
|
+
) {
|
|
415
|
+
const delay = retryConfig.exponentialBackoff
|
|
416
|
+
? retryConfig.baseDelay * Math.pow(2, currentAttempts)
|
|
417
|
+
: retryConfig.baseDelay;
|
|
418
|
+
|
|
419
|
+
retryAttempts.set(audio, currentAttempts + 1);
|
|
420
|
+
|
|
421
|
+
const retryFunction = async (): Promise<void> => {
|
|
422
|
+
try {
|
|
423
|
+
// Try fallback URLs if available
|
|
424
|
+
if (retryConfig.fallbackUrls && retryConfig.fallbackUrls.length > 0) {
|
|
425
|
+
const fallbackIndex = currentAttempts % retryConfig.fallbackUrls.length;
|
|
426
|
+
const fallbackUrl =
|
|
427
|
+
retryConfig.fallbackUrls[fallbackIndex] + extractFileName(originalUrl);
|
|
428
|
+
audio.src = fallbackUrl;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
await audio.load();
|
|
432
|
+
await audio.play();
|
|
433
|
+
|
|
434
|
+
// Reset retry counter on success
|
|
435
|
+
retryAttempts.delete(audio);
|
|
436
|
+
} catch (retryError) {
|
|
437
|
+
await handleAudioError(audio, channelNumber, originalUrl, retryError as Error);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
setTimeout(retryFunction, delay);
|
|
442
|
+
} else {
|
|
443
|
+
// Max retries reached or retry disabled
|
|
444
|
+
if (retryConfig.skipOnFailure || globalErrorRecovery.fallbackToNextTrack) {
|
|
445
|
+
// Skip to next track in queue
|
|
446
|
+
channel.queue.shift();
|
|
447
|
+
|
|
448
|
+
// Import and use playAudioQueue to continue with next track
|
|
449
|
+
const { playAudioQueue } = await import('./core');
|
|
450
|
+
// eslint-disable-next-line no-console
|
|
451
|
+
playAudioQueue(channelNumber).catch(console.error);
|
|
452
|
+
} else if (!globalErrorRecovery.preserveQueueOnError) {
|
|
453
|
+
// Clear the entire queue on failure
|
|
454
|
+
channel.queue = [];
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Creates a timeout-protected audio element with comprehensive error handling
|
|
461
|
+
* @param url - The audio URL to load
|
|
462
|
+
* @param channelNumber - The channel number this audio belongs to
|
|
463
|
+
* @returns Promise that resolves to the configured audio element
|
|
464
|
+
* @internal
|
|
465
|
+
*/
|
|
466
|
+
export const createProtectedAudioElement = async (
|
|
467
|
+
url: string,
|
|
468
|
+
channelNumber: number
|
|
469
|
+
): Promise<HTMLAudioElement> => {
|
|
470
|
+
const audio = new Audio();
|
|
471
|
+
|
|
472
|
+
return new Promise((resolve, reject) => {
|
|
473
|
+
const cleanup = (): void => {
|
|
474
|
+
const timeoutId = loadTimeouts.get(audio);
|
|
475
|
+
if (timeoutId) {
|
|
476
|
+
clearTimeout(timeoutId);
|
|
477
|
+
loadTimeouts.delete(audio);
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
const handleSuccess = (): void => {
|
|
482
|
+
cleanup();
|
|
483
|
+
resolve(audio);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
const handleError = (error: Error): void => {
|
|
487
|
+
cleanup();
|
|
488
|
+
reject(error);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// Set up error handling
|
|
492
|
+
setupAudioErrorHandling(audio, channelNumber, url, async (error: Error) => {
|
|
493
|
+
handleError(error);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
// Set up success handlers
|
|
497
|
+
audio.addEventListener('canplay', handleSuccess, { once: true });
|
|
498
|
+
audio.addEventListener('loadedmetadata', handleSuccess, { once: true });
|
|
499
|
+
|
|
500
|
+
// Start loading
|
|
501
|
+
audio.src = url;
|
|
502
|
+
audio.load();
|
|
503
|
+
});
|
|
504
|
+
};
|