dsh-voice 0.9.0 → 0.10.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/lib/edge-tts.d.ts CHANGED
@@ -28,6 +28,10 @@ export interface TtsDeps {
28
28
  proxyUrl?: string;
29
29
  /** 取消信号(可选);也可通过 synthesizeSpeech 第 4 个参数传入。 */
30
30
  signal?: AbortSignal;
31
+ /** 是否显式启用 Keep-Alive WebSocket 连接池(未注入 webSocketFactory 时默认开启)。 */
32
+ usePool?: boolean;
33
+ /** 瞬态网络断连重试次数(未注入 webSocketFactory 时默认为 1,测试注入时默认为 0)。 */
34
+ maxRetries?: number;
31
35
  }
32
36
  /** unexpected-response 事件中的 HTTP 响应最小面。 */
33
37
  export interface UnexpectedResponseInfo {
@@ -41,6 +45,8 @@ export interface WebSocketLike {
41
45
  data: unknown;
42
46
  }) => void): void;
43
47
  addEventListener(type: 'open' | 'close' | 'error', listener: (event?: unknown) => void): void;
48
+ removeEventListener?(type: 'message' | 'open' | 'close' | 'error', listener: (...args: any[]) => void): void;
49
+ readyState?: number;
44
50
  on?(event: 'unexpected-response', listener: (req: unknown, res: UnexpectedResponseInfo) => void): void;
45
51
  send(data: string): void;
46
52
  close(): void;
@@ -48,19 +54,22 @@ export interface WebSocketLike {
48
54
  export type WebSocketFactory = (url: string, options: {
49
55
  headers: Record<string, string>;
50
56
  }) => WebSocketLike;
57
+ /** 关闭并清空所有 Edge TTS 空闲保活 WebSocket 连接池。 */
58
+ export declare function closeEdgeConnectionPool(): void;
51
59
  /**
52
60
  * 本地生成 Sec-MS-GEC v2 令牌(对齐 edge-tts 7.3.1)。
53
61
  */
54
62
  export declare function generateSecMsGec(nowSeconds?: number): string;
55
63
  /** 生成 SSML。 */
56
64
  export declare function buildSsml(options: TtsOptions): string;
65
+ export type EdgeBoundaryMode = 'sentence' | 'word' | 'all' | false;
57
66
  /**
58
67
  * 合成语音并可选返回句/词级边界字幕时间轴。
59
- * 收到 HTTP 403 且响应带有效 Date 头时自动校准时钟偏差并单次重试。
68
+ * 支持 Keep-Alive WebSocket 连接池复用、HTTP 403 时钟自动校准与瞬态断连自动重试。
60
69
  * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
61
70
  */
62
71
  export declare function synthesizeSpeechWithMetadata(options: TtsOptions & {
63
- boundary?: 'sentence' | 'word' | false;
72
+ boundary?: EdgeBoundaryMode;
64
73
  }, deps?: TtsDeps, timeoutMs?: number, signal?: AbortSignal): Promise<{
65
74
  audio: Buffer;
66
75
  cues: SubtitleCue[];
package/lib/edge-tts.js CHANGED
@@ -9,6 +9,7 @@ import { randomBytes, randomUUID } from 'node:crypto';
9
9
  import WebSocket from 'ws';
10
10
  import { edgeTokenV2 } from './edge-drm-v2.js';
11
11
  import { getProxyWsAgent } from './proxy-fetch.js';
12
+ import { buildPronunciationPattern, splitInlinePauses } from './tts-text.js';
12
13
  import { VOICES } from './voices.js';
13
14
  const TRUSTED_CLIENT_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
14
15
  const WS_BASE = 'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
@@ -42,6 +43,161 @@ class ClockSkewRetrySignal extends Error {
42
43
  super('clock-skew-retry');
43
44
  }
44
45
  }
46
+ /** 内部哨兵错误:池中复用的空闲连接在收到任何音频帧前已失效,立即换全新连接重试。 */
47
+ class StalePooledSocketSignal extends Error {
48
+ constructor() {
49
+ super('stale-pooled-socket');
50
+ }
51
+ }
52
+ /** 标记可安全重试的瞬态 WebSocket / 握手错误(保留原始中文报错文本)。 */
53
+ class TransientWsError extends Error {
54
+ }
55
+ const POOL_MAX_IDLE_PER_KEY = 2;
56
+ const POOL_IDLE_TTL_MS = 15_000;
57
+ const edgeConnectionPool = new Map();
58
+ function removeFromIdlePool(target) {
59
+ const bucket = edgeConnectionPool.get(target.key);
60
+ if (!bucket)
61
+ return;
62
+ const idx = bucket.indexOf(target);
63
+ if (idx >= 0) {
64
+ clearTimeout(target.timer);
65
+ bucket.splice(idx, 1);
66
+ if (bucket.length === 0)
67
+ edgeConnectionPool.delete(target.key);
68
+ }
69
+ }
70
+ function isSocketReusable(entry) {
71
+ if (entry.closedOrErrored || !entry.opened)
72
+ return false;
73
+ const rs = entry.socket.readyState;
74
+ if (typeof rs === 'number') {
75
+ return rs === 1;
76
+ }
77
+ return true;
78
+ }
79
+ function createManagedSocketEntry(key, factory, url, headers) {
80
+ const socket = factory(url, { headers });
81
+ const dummyTimer = setTimeout(() => { }, 0);
82
+ clearTimeout(dummyTimer);
83
+ const entry = {
84
+ socket,
85
+ timer: dummyTimer,
86
+ key,
87
+ opened: socket.readyState === 1,
88
+ closedOrErrored: false,
89
+ handlers: null,
90
+ };
91
+ socket.addEventListener('open', () => {
92
+ entry.opened = true;
93
+ entry.handlers?.onOpen?.();
94
+ });
95
+ socket.addEventListener('message', (event) => {
96
+ entry.handlers?.onMessage?.(event);
97
+ });
98
+ socket.addEventListener('error', (event) => {
99
+ entry.closedOrErrored = true;
100
+ removeFromIdlePool(entry);
101
+ entry.handlers?.onError?.(event);
102
+ });
103
+ socket.addEventListener('close', (event) => {
104
+ entry.closedOrErrored = true;
105
+ removeFromIdlePool(entry);
106
+ entry.handlers?.onClose?.(event);
107
+ });
108
+ if (typeof socket.on === 'function') {
109
+ socket.on('unexpected-response', (req, res) => {
110
+ entry.closedOrErrored = true;
111
+ removeFromIdlePool(entry);
112
+ entry.handlers?.onUnexpectedResponse?.(req, res);
113
+ });
114
+ }
115
+ return entry;
116
+ }
117
+ function acquirePooledSocket(key) {
118
+ const bucket = edgeConnectionPool.get(key);
119
+ if (!bucket || bucket.length === 0)
120
+ return null;
121
+ while (bucket.length > 0) {
122
+ const entry = bucket.pop();
123
+ clearTimeout(entry.timer);
124
+ if (bucket.length === 0)
125
+ edgeConnectionPool.delete(key);
126
+ if (isSocketReusable(entry)) {
127
+ return entry;
128
+ }
129
+ entry.closedOrErrored = true;
130
+ entry.handlers = null;
131
+ try {
132
+ entry.socket.close();
133
+ }
134
+ catch { /* 忽略 */ }
135
+ }
136
+ return null;
137
+ }
138
+ function releaseSocketToPool(entry) {
139
+ entry.handlers = null;
140
+ if (!isSocketReusable(entry)) {
141
+ entry.closedOrErrored = true;
142
+ try {
143
+ entry.socket.close();
144
+ }
145
+ catch { /* 忽略 */ }
146
+ return;
147
+ }
148
+ let bucket = edgeConnectionPool.get(entry.key);
149
+ if (!bucket) {
150
+ bucket = [];
151
+ edgeConnectionPool.set(entry.key, bucket);
152
+ }
153
+ while (bucket.length >= POOL_MAX_IDLE_PER_KEY) {
154
+ const oldest = bucket.shift();
155
+ clearTimeout(oldest.timer);
156
+ oldest.closedOrErrored = true;
157
+ oldest.handlers = null;
158
+ try {
159
+ oldest.socket.close();
160
+ }
161
+ catch { /* 忽略 */ }
162
+ }
163
+ const timer = setTimeout(() => {
164
+ removeFromIdlePool(entry);
165
+ entry.closedOrErrored = true;
166
+ entry.handlers = null;
167
+ try {
168
+ entry.socket.close();
169
+ }
170
+ catch { /* 忽略 */ }
171
+ }, POOL_IDLE_TTL_MS);
172
+ timer.unref?.();
173
+ entry.timer = timer;
174
+ bucket.push(entry);
175
+ }
176
+ function discardSocketEntry(entry) {
177
+ removeFromIdlePool(entry);
178
+ clearTimeout(entry.timer);
179
+ entry.closedOrErrored = true;
180
+ entry.handlers = null;
181
+ try {
182
+ entry.socket.close();
183
+ }
184
+ catch { /* 忽略 */ }
185
+ }
186
+ /** 关闭并清空所有 Edge TTS 空闲保活 WebSocket 连接池。 */
187
+ export function closeEdgeConnectionPool() {
188
+ for (const bucket of edgeConnectionPool.values()) {
189
+ for (const entry of bucket) {
190
+ clearTimeout(entry.timer);
191
+ entry.closedOrErrored = true;
192
+ entry.handlers = null;
193
+ try {
194
+ entry.socket.close();
195
+ }
196
+ catch { /* 忽略 */ }
197
+ }
198
+ }
199
+ edgeConnectionPool.clear();
200
+ }
45
201
  /** 直连工厂(ws 库,Node 全版本可用)。 */
46
202
  function directWebSocket(url, options) {
47
203
  return new WebSocket(url, { headers: options.headers });
@@ -78,40 +234,64 @@ function escapeXmlAttr(value) {
78
234
  .replace(/'/g, '&apos;')
79
235
  .replace(/"/g, '&quot;');
80
236
  }
81
- function renderSsmlBody(text, pronunciations) {
82
- const cleanText = stripIllegalXmlChars(text);
83
- if (!Array.isArray(pronunciations) || pronunciations.length === 0) {
84
- return escapeXml(cleanText);
237
+ function renderTextWithPronunciations(part, map, regex) {
238
+ if (regex === null || map.size === 0) {
239
+ return escapeXml(part);
85
240
  }
86
- const map = new Map();
87
- for (const rule of pronunciations) {
88
- if (!rule || typeof rule.word !== 'string' || typeof rule.alias !== 'string')
89
- continue;
90
- const word = stripIllegalXmlChars(rule.word).trim();
91
- const alias = stripIllegalXmlChars(rule.alias).trim();
92
- if (word !== '' && alias !== '') {
93
- map.set(word, alias);
94
- }
95
- }
96
- if (map.size === 0) {
97
- return escapeXml(cleanText);
98
- }
99
- const sortedWords = Array.from(map.keys()).sort((a, b) => b.length - a.length);
100
- const regex = new RegExp(sortedWords.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'g');
241
+ regex.lastIndex = 0;
101
242
  let out = '';
102
243
  let lastIndex = 0;
103
244
  let match;
104
- while ((match = regex.exec(cleanText)) !== null) {
245
+ while ((match = regex.exec(part)) !== null) {
105
246
  if (match.index > lastIndex) {
106
- out += escapeXml(cleanText.slice(lastIndex, match.index));
247
+ out += escapeXml(part.slice(lastIndex, match.index));
107
248
  }
108
249
  const matchedWord = match[0];
109
250
  const alias = map.get(matchedWord);
110
- out += `<sub alias='${escapeXmlAttr(alias)}'>${escapeXml(matchedWord)}</sub>`;
251
+ if (alias !== undefined) {
252
+ out += `<sub alias='${escapeXmlAttr(alias)}'>${escapeXml(matchedWord)}</sub>`;
253
+ }
254
+ else {
255
+ out += escapeXml(matchedWord);
256
+ }
111
257
  lastIndex = match.index + matchedWord.length;
112
258
  }
113
- if (lastIndex < cleanText.length) {
114
- out += escapeXml(cleanText.slice(lastIndex));
259
+ if (lastIndex < part.length) {
260
+ out += escapeXml(part.slice(lastIndex));
261
+ }
262
+ return out;
263
+ }
264
+ function renderSsmlBody(text, pronunciations) {
265
+ const cleanText = stripIllegalXmlChars(text);
266
+ const map = new Map();
267
+ if (Array.isArray(pronunciations)) {
268
+ for (const rule of pronunciations) {
269
+ if (!rule || typeof rule.word !== 'string' || typeof rule.alias !== 'string')
270
+ continue;
271
+ const word = stripIllegalXmlChars(rule.word).trim();
272
+ const alias = stripIllegalXmlChars(rule.alias).trim();
273
+ if (word !== '' && alias !== '') {
274
+ map.set(word, alias);
275
+ }
276
+ }
277
+ }
278
+ const pieces = splitInlinePauses(cleanText);
279
+ const hasPause = pieces.some((p) => p.kind === 'pause');
280
+ if (!hasPause && map.size === 0) {
281
+ return escapeXml(cleanText);
282
+ }
283
+ const regex = map.size > 0 ? buildPronunciationPattern(Array.from(map.keys())) : null;
284
+ if (!hasPause) {
285
+ return renderTextWithPronunciations(cleanText, map, regex);
286
+ }
287
+ let out = '';
288
+ for (const piece of pieces) {
289
+ if (piece.kind === 'pause') {
290
+ out += `<break time='${piece.durationMs}ms'/>`;
291
+ }
292
+ else {
293
+ out += renderTextWithPronunciations(piece.text, map, regex);
294
+ }
115
295
  }
116
296
  return out;
117
297
  }
@@ -166,13 +346,61 @@ function parseMetadataCues(data, cues) {
166
346
  cues.push({ index: cues.length + 1, startMs, endMs, text });
167
347
  }
168
348
  }
169
- async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutMs, totalTimeoutMs, abortSignal, canRetryOn403) {
170
- const token = generateSecMsGec(nowSeconds);
171
- // 对齐 edge-tts 当前协议:Sec-MS-GEC 走 URL 查询参数,MUID 走 header
172
- const url = WS_BASE + '?TrustedClientToken=' + TRUSTED_CLIENT_TOKEN + '&ConnectionId=' + randomUUID().replace(/-/g, '') + '&Sec-MS-GEC=' + token + '&Sec-MS-GEC-Version=' + SEC_MS_GEC_VERSION;
173
- const socket = factory(url, {
174
- headers: { ...WSS_HEADERS, Cookie: 'muid=' + randomBytes(16).toString('hex').toUpperCase() + ';' },
349
+ function sendSynthesisTurn(socket, options) {
350
+ const sentenceBoundaryEnabled = options.boundary === 'sentence' || options.boundary === 'all' ? 'true' : 'false';
351
+ const wordBoundaryEnabled = options.boundary === 'word' || options.boundary === 'all' ? 'true' : 'false';
352
+ const config = protocolHeader('speech.config', { 'Content-Type': 'application/json; charset=utf-8' })
353
+ + JSON.stringify({
354
+ context: {
355
+ synthesis: {
356
+ audio: {
357
+ metadataoptions: { sentenceBoundaryEnabled, wordBoundaryEnabled },
358
+ outputFormat: OUTPUT_FORMAT,
359
+ },
360
+ },
361
+ },
362
+ });
363
+ const ssml = protocolHeader('ssml', { 'Content-Type': 'application/ssml+xml', 'X-RequestId': randomUUID() }) + buildSsml(options);
364
+ socket.send(config);
365
+ socket.send(ssml);
366
+ }
367
+ function sleepWithAbort(ms, signal) {
368
+ if (signal?.aborted === true)
369
+ return Promise.reject(signal.reason);
370
+ if (ms <= 0)
371
+ return Promise.resolve();
372
+ return new Promise((resolvePromise, rejectPromise) => {
373
+ const canListen = typeof signal?.addEventListener === 'function';
374
+ let onAbort = () => { };
375
+ const timer = setTimeout(() => {
376
+ if (canListen)
377
+ signal.removeEventListener('abort', onAbort);
378
+ resolvePromise();
379
+ }, ms);
380
+ timer.unref?.();
381
+ if (canListen) {
382
+ onAbort = () => {
383
+ clearTimeout(timer);
384
+ rejectPromise(signal.reason);
385
+ };
386
+ signal.addEventListener('abort', onAbort, { once: true });
387
+ }
175
388
  });
389
+ }
390
+ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutMs, totalTimeoutMs, abortSignal, canRetryOn403, poolingEnabled, allowPoolReuse, poolKey) {
391
+ let reusedEntry = poolingEnabled && allowPoolReuse ? acquirePooledSocket(poolKey) : null;
392
+ const isReused = reusedEntry !== null;
393
+ let entry;
394
+ if (reusedEntry !== null) {
395
+ entry = reusedEntry;
396
+ }
397
+ else {
398
+ const token = generateSecMsGec(nowSeconds);
399
+ // 对齐 edge-tts 当前协议:Sec-MS-GEC 走 URL 查询参数,MUID 走 header
400
+ const url = WS_BASE + '?TrustedClientToken=' + TRUSTED_CLIENT_TOKEN + '&ConnectionId=' + randomUUID().replace(/-/g, '') + '&Sec-MS-GEC=' + token + '&Sec-MS-GEC-Version=' + SEC_MS_GEC_VERSION;
401
+ const headers = { ...WSS_HEADERS, Cookie: 'muid=' + randomBytes(16).toString('hex').toUpperCase() + ';' };
402
+ entry = createManagedSocketEntry(poolKey, factory, url, headers);
403
+ }
176
404
  const chunks = [];
177
405
  const cues = [];
178
406
  let done = false;
@@ -181,45 +409,44 @@ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutM
181
409
  let settled = false;
182
410
  let timer;
183
411
  let onAbort = () => { };
184
- // 先结算再关 socket:即使 socket.close() 同步触发 close 事件,取消原因也不会被通用错误覆盖。
185
- const fail = (error) => {
412
+ // 先结算再关/回收 socket:即使 socket.close() 同步触发 close 事件,取消原因也不会被通用错误覆盖。
413
+ const fail = (error, shouldCloseSocket = true) => {
186
414
  if (settled)
187
415
  return;
188
416
  settled = true;
417
+ entry.handlers = null;
189
418
  if (timer !== undefined)
190
419
  clearTimeout(timer);
191
420
  if (canListen)
192
421
  abortSignal.removeEventListener('abort', onAbort);
422
+ if (shouldCloseSocket) {
423
+ discardSocketEntry(entry);
424
+ }
193
425
  reject(error);
194
426
  };
195
427
  const finish = () => {
196
428
  if (settled)
197
429
  return;
198
430
  settled = true;
431
+ entry.handlers = null;
199
432
  if (timer !== undefined)
200
433
  clearTimeout(timer);
201
434
  if (canListen)
202
435
  abortSignal.removeEventListener('abort', onAbort);
203
- try {
204
- socket.close();
436
+ if (poolingEnabled) {
437
+ releaseSocketToPool(entry);
438
+ }
439
+ else {
440
+ discardSocketEntry(entry);
205
441
  }
206
- catch { /* 忽略 */ }
207
442
  resolvePromise();
208
443
  };
209
444
  onAbort = () => {
210
445
  const reason = abortSignal?.reason ?? new Error('语音合成已取消。');
211
- fail(reason);
212
- try {
213
- socket.close();
214
- }
215
- catch { /* 忽略 */ }
446
+ fail(reason, true);
216
447
  };
217
448
  timer = setTimeout(() => {
218
- fail(new Error('语音合成超时(' + totalTimeoutMs + ' 毫秒无完整音频),请重试或检查网络。'));
219
- try {
220
- socket.close();
221
- }
222
- catch { /* 忽略 */ }
449
+ fail(new Error('语音合成超时(' + totalTimeoutMs + ' 毫秒无完整音频),请重试或检查网络。'), true);
223
450
  }, attemptTimeoutMs);
224
451
  if (canListen)
225
452
  abortSignal.addEventListener('abort', onAbort, { once: true });
@@ -227,8 +454,8 @@ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutM
227
454
  onAbort();
228
455
  return;
229
456
  }
230
- if (typeof socket.on === 'function') {
231
- socket.on('unexpected-response', (_req, res) => {
457
+ entry.handlers = {
458
+ onUnexpectedResponse(_req, res) {
232
459
  const status = res?.statusCode;
233
460
  if (status === 403 && canRetryOn403) {
234
461
  const rawDate = res.headers?.date ?? res.headers?.Date;
@@ -240,11 +467,7 @@ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutM
240
467
  res.resume?.();
241
468
  }
242
469
  catch { /* 忽略 */ }
243
- fail(new ClockSkewRetrySignal());
244
- try {
245
- socket.close();
246
- }
247
- catch { /* 忽略 */ }
470
+ fail(new ClockSkewRetrySignal(), true);
248
471
  return;
249
472
  }
250
473
  }
@@ -252,76 +475,89 @@ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutM
252
475
  res?.resume?.();
253
476
  }
254
477
  catch { /* 忽略 */ }
255
- fail(new Error(status === 403
256
- ? 'edge-tts 服务端返回 HTTP 403(请求被拒绝或本机时钟偏差过大)。'
257
- : 'edge-tts 握手失败(HTTP ' + String(status ?? 'unknown') + ')。'));
478
+ if (status === 403) {
479
+ fail(new Error('edge-tts 服务端返回 HTTP 403(请求被拒绝或本机时钟偏差过大)。'), true);
480
+ }
481
+ else if (status === undefined || status >= 500 || status === 429) {
482
+ fail(new TransientWsError('edge-tts 握手失败(HTTP ' + String(status ?? 'unknown') + ')。'), true);
483
+ }
484
+ else {
485
+ fail(new Error('edge-tts 握手失败(HTTP ' + String(status) + ')。'), true);
486
+ }
487
+ },
488
+ onOpen() {
258
489
  try {
259
- socket.close();
490
+ sendSynthesisTurn(entry.socket, options);
260
491
  }
261
- catch { /* 忽略 */ }
262
- });
263
- }
264
- socket.addEventListener('open', () => {
265
- const sentenceBoundaryEnabled = options.boundary === 'sentence' ? 'true' : 'false';
266
- const wordBoundaryEnabled = options.boundary === 'word' ? 'true' : 'false';
267
- const config = protocolHeader('speech.config', { 'Content-Type': 'application/json; charset=utf-8' })
268
- + JSON.stringify({
269
- context: {
270
- synthesis: {
271
- audio: {
272
- metadataoptions: { sentenceBoundaryEnabled, wordBoundaryEnabled },
273
- outputFormat: OUTPUT_FORMAT,
274
- },
275
- },
276
- },
277
- });
278
- const ssml = protocolHeader('ssml', { 'Content-Type': 'application/ssml+xml', 'X-RequestId': randomUUID() }) + buildSsml(options);
279
- try {
280
- socket.send(config);
281
- socket.send(ssml);
282
- }
283
- catch (error) {
284
- fail(error instanceof Error ? error : new Error(String(error)));
285
- }
286
- });
287
- socket.addEventListener('message', (event) => {
288
- const data = event.data;
289
- if (typeof data === 'string') {
290
- if (data.includes('Path:audio.metadata')) {
291
- parseMetadataCues(data, cues);
292
- return;
492
+ catch (error) {
493
+ fail(new TransientWsError(error instanceof Error ? error.message : String(error)), true);
293
494
  }
294
- if (data.includes('Path:turn.end')) {
295
- if (chunks.length === 0) {
296
- fail(new Error('合成结束但没有收到音频数据(服务端可能拒绝了请求)。'));
495
+ },
496
+ onMessage(event) {
497
+ const data = event.data;
498
+ if (typeof data === 'string') {
499
+ if (data.includes('Path:audio.metadata')) {
500
+ parseMetadataCues(data, cues);
501
+ return;
297
502
  }
298
- else if (!done) {
299
- done = true;
300
- finish();
503
+ if (data.includes('Path:turn.end')) {
504
+ if (chunks.length === 0) {
505
+ if (isReused) {
506
+ fail(new StalePooledSocketSignal(), true);
507
+ }
508
+ else {
509
+ fail(new Error('合成结束但没有收到音频数据(服务端可能拒绝了请求)。'), true);
510
+ }
511
+ }
512
+ else if (!done) {
513
+ done = true;
514
+ finish();
515
+ }
301
516
  }
517
+ return;
302
518
  }
303
- return;
304
- }
305
- // 二进制帧:2 字节大端头长 + 头文本 + 音频数据;只收 Path:audio 帧避免非音频头污染 MP3 流
306
- const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
307
- if (buffer.length > 2) {
308
- const headerLength = buffer.readUInt16BE(0);
309
- if (buffer.length > 2 + headerLength) {
310
- const header = buffer.subarray(2, 2 + headerLength).toString('utf8');
311
- if (/(?:^|\r?\n)Path:audio(?:\r?\n|$)/.test(header)) {
312
- chunks.push(buffer.subarray(2 + headerLength));
519
+ // 二进制帧:2 字节大端头长 + 头文本 + 音频数据;只收 Path:audio 帧避免非音频头污染 MP3 流
520
+ const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
521
+ if (buffer.length > 2) {
522
+ const headerLength = buffer.readUInt16BE(0);
523
+ if (buffer.length > 2 + headerLength) {
524
+ const header = buffer.subarray(2, 2 + headerLength).toString('utf8');
525
+ if (/(?:^|\r?\n)Path:audio(?:\r?\n|$)/.test(header)) {
526
+ chunks.push(buffer.subarray(2 + headerLength));
527
+ }
313
528
  }
314
529
  }
530
+ },
531
+ onError() {
532
+ if (isReused && chunks.length === 0) {
533
+ fail(new StalePooledSocketSignal(), true);
534
+ return;
535
+ }
536
+ fail(new TransientWsError('edge-tts WebSocket 连接失败。若网络需要特殊代理(梯子),请在 DSH「设置 → 插件 → 语音」配置 proxyUrl(保存即生效)。'), true);
537
+ },
538
+ onClose() {
539
+ if (!done) {
540
+ if (isReused && chunks.length === 0) {
541
+ fail(new StalePooledSocketSignal(), true);
542
+ return;
543
+ }
544
+ fail(new TransientWsError('edge-tts 连接在收到完整音频前关闭(未收到 turn.end)。'), true);
545
+ }
546
+ },
547
+ };
548
+ if (isReused || entry.opened) {
549
+ try {
550
+ sendSynthesisTurn(entry.socket, options);
315
551
  }
316
- });
317
- socket.addEventListener('error', () => {
318
- fail(new Error('edge-tts WebSocket 连接失败。若网络需要特殊代理(梯子),请在 DSH「设置 → 插件 → 语音」配置 proxyUrl(保存即生效)。'));
319
- });
320
- socket.addEventListener('close', () => {
321
- if (!done) {
322
- fail(new Error('edge-tts 连接在收到完整音频前关闭(未收到 turn.end)。'));
552
+ catch (error) {
553
+ if (isReused) {
554
+ fail(new StalePooledSocketSignal(), true);
555
+ }
556
+ else {
557
+ fail(new TransientWsError(error instanceof Error ? error.message : String(error)), true);
558
+ }
323
559
  }
324
- });
560
+ }
325
561
  });
326
562
  const audio = Buffer.concat(chunks);
327
563
  if (audio.length === 0)
@@ -330,33 +566,57 @@ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutM
330
566
  }
331
567
  /**
332
568
  * 合成语音并可选返回句/词级边界字幕时间轴。
333
- * 收到 HTTP 403 且响应带有效 Date 头时自动校准时钟偏差并单次重试。
569
+ * 支持 Keep-Alive WebSocket 连接池复用、HTTP 403 时钟自动校准与瞬态断连自动重试。
334
570
  * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
335
571
  */
336
572
  export async function synthesizeSpeechWithMetadata(options, deps = {}, timeoutMs = 30000, signal) {
337
573
  const abortSignal = signal ?? deps.signal;
338
- if (abortSignal?.aborted === true)
339
- throw abortSignal.reason;
574
+ throwIfAborted(abortSignal);
340
575
  if (options.text.trim() === '')
341
576
  throw new Error('要合成的文本为空。');
342
577
  if (options.text.length > 5000)
343
578
  throw new Error('文本过长(超过 5000 字符),请分段合成。');
344
579
  const factory = deps.webSocketFactory ?? (deps.proxyUrl !== undefined && deps.proxyUrl !== '' ? proxyWebSocket(deps.proxyUrl) : directWebSocket);
580
+ const poolingEnabled = deps.usePool === true || deps.webSocketFactory === undefined;
581
+ const poolKey = deps.proxyUrl && deps.proxyUrl !== '' ? deps.proxyUrl : 'direct';
582
+ const rawRetries = deps.maxRetries ?? (deps.webSocketFactory === undefined ? 1 : 0);
583
+ let retriesLeft = Number.isFinite(rawRetries) ? Math.max(0, Math.floor(rawRetries)) : 0;
584
+ let canRetryOn403 = true;
585
+ let allowPoolReuse = poolingEnabled;
345
586
  const startedAt = Date.now();
346
587
  const now = () => (deps.nowSeconds ? deps.nowSeconds() : Date.now() / 1000 + clockSkewSeconds);
347
- try {
348
- return await runSynthesisAttempt(options, factory, now(), timeoutMs, timeoutMs, abortSignal, true);
349
- }
350
- catch (error) {
351
- if (!(error instanceof ClockSkewRetrySignal))
352
- throw error;
353
- if (abortSignal?.aborted === true)
354
- throw abortSignal.reason;
588
+ while (true) {
589
+ throwIfAborted(abortSignal);
355
590
  const remainingMs = timeoutMs - (Date.now() - startedAt);
356
591
  if (remainingMs <= 0) {
357
592
  throw new Error('语音合成超时(' + timeoutMs + ' 毫秒无完整音频),请重试或检查网络。');
358
593
  }
359
- return await runSynthesisAttempt(options, factory, now(), remainingMs, timeoutMs, abortSignal, false);
594
+ try {
595
+ return await runSynthesisAttempt(options, factory, now(), remainingMs, timeoutMs, abortSignal, canRetryOn403, poolingEnabled, allowPoolReuse, poolKey);
596
+ }
597
+ catch (error) {
598
+ throwIfAborted(abortSignal);
599
+ if (error instanceof StalePooledSocketSignal) {
600
+ allowPoolReuse = false;
601
+ continue;
602
+ }
603
+ if (error instanceof ClockSkewRetrySignal) {
604
+ canRetryOn403 = false;
605
+ allowPoolReuse = false;
606
+ continue;
607
+ }
608
+ if (error instanceof TransientWsError && retriesLeft > 0) {
609
+ retriesLeft -= 1;
610
+ allowPoolReuse = false;
611
+ const leftBeforeWait = timeoutMs - (Date.now() - startedAt);
612
+ if (leftBeforeWait <= 0) {
613
+ throw new Error('语音合成超时(' + timeoutMs + ' 毫秒无完整音频),请重试或检查网络。');
614
+ }
615
+ await sleepWithAbort(Math.min(150, leftBeforeWait), abortSignal);
616
+ continue;
617
+ }
618
+ throw error;
619
+ }
360
620
  }
361
621
  }
362
622
  /**