dsh-voice 0.6.1 → 0.8.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/CHANGELOG.md +18 -0
- package/README.en.md +26 -15
- package/README.md +26 -15
- package/docs/USAGE.en.md +84 -40
- package/docs/USAGE.md +84 -40
- package/lib/config.d.ts +44 -3
- package/lib/config.js +138 -9
- package/lib/edge-tts.d.ts +44 -0
- package/lib/edge-tts.js +305 -37
- package/lib/host-config.d.ts +1 -1
- package/lib/host-config.js +49 -20
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/openai-tts.d.ts +29 -0
- package/lib/openai-tts.js +149 -0
- package/lib/paths.d.ts +4 -0
- package/lib/paths.js +21 -11
- package/lib/proxy-fetch.d.ts +6 -0
- package/lib/proxy-fetch.js +344 -4
- package/lib/stt.d.ts +43 -9
- package/lib/stt.js +164 -14
- package/lib/tools.d.ts +9 -2
- package/lib/tools.js +855 -81
- package/lib/tts-text.d.ts +48 -0
- package/lib/tts-text.js +368 -0
- package/lib/update-client.js +1 -1
- package/lib/voices.d.ts +24 -2
- package/lib/voices.js +179 -25
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +4 -3
- package/skills/voice-workflow/SKILL.md +96 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI 兼容 TTS 客户端:支持 OpenAI / SiliconFlow / 本地 Kokoro 等 /audio/speech 端点。
|
|
3
|
+
*
|
|
4
|
+
* @module dsh-voice/openai-tts
|
|
5
|
+
*/
|
|
6
|
+
function isLoopbackUrl(rawUrl) {
|
|
7
|
+
try {
|
|
8
|
+
const host = new URL(rawUrl).hostname.toLowerCase();
|
|
9
|
+
return host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]';
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function throwIfAborted(signal) {
|
|
16
|
+
if (signal?.aborted === true)
|
|
17
|
+
throw signal.reason;
|
|
18
|
+
}
|
|
19
|
+
function raceAbort(value, signal) {
|
|
20
|
+
const promise = Promise.resolve(value);
|
|
21
|
+
if (signal === undefined || typeof signal.addEventListener !== 'function')
|
|
22
|
+
return promise;
|
|
23
|
+
if (signal.aborted === true)
|
|
24
|
+
return Promise.reject(signal.reason);
|
|
25
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
26
|
+
const onAbort = () => rejectPromise(signal.reason);
|
|
27
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
28
|
+
promise.then((settled) => { signal.removeEventListener('abort', onAbort); resolvePromise(settled); }, (error) => { signal.removeEventListener('abort', onAbort); rejectPromise(error); });
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 将 edge-tts 百分比语速(如 `+20%`、`-15%`、`+0%`)换算为 OpenAI speed 倍率(钳制在 `[0.25, 4.0]`)。
|
|
33
|
+
*/
|
|
34
|
+
export function rateToSpeed(rate) {
|
|
35
|
+
const match = /^([+-]?\d+(?:\.\d+)?)%$/.exec(rate.trim());
|
|
36
|
+
if (!match)
|
|
37
|
+
return 1.0;
|
|
38
|
+
const percent = Number(match[1]);
|
|
39
|
+
if (!Number.isFinite(percent))
|
|
40
|
+
return 1.0;
|
|
41
|
+
const raw = Math.round((1 + percent / 100) * 10000) / 10000;
|
|
42
|
+
return Math.min(4.0, Math.max(0.25, raw));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 调用 OpenAI 兼容 `/audio/speech` 接口合成 MP3 音频。
|
|
46
|
+
* 本地回环地址(localhost / 127.0.0.1 / ::1)允许空密钥直连。
|
|
47
|
+
*/
|
|
48
|
+
export async function synthesizeOpenAiSpeech(baseUrl, apiKey, options, fetchImpl = globalThis.fetch, timeoutMs = 60000, signal) {
|
|
49
|
+
const abortSignal = signal ?? options.signal;
|
|
50
|
+
throwIfAborted(abortSignal);
|
|
51
|
+
const trimmedBaseUrl = baseUrl.trim().replace(/\/+$/, '');
|
|
52
|
+
if (trimmedBaseUrl === '') {
|
|
53
|
+
throw new Error('未配置 TTS 接口地址:请在 DSH「设置 → 插件 → 语音」填写 ttsBaseUrl。');
|
|
54
|
+
}
|
|
55
|
+
if (options.text.trim() === '') {
|
|
56
|
+
throw new Error('要合成的文本为空。');
|
|
57
|
+
}
|
|
58
|
+
const key = apiKey.trim();
|
|
59
|
+
if (key === '' && !isLoopbackUrl(trimmedBaseUrl)) {
|
|
60
|
+
throw new Error('未配置 TTS 密钥:请在 DSH「设置 → 插件 → 语音」填写 ttsApiKey 或设置环境变量 DSH_VOICE_TTS_KEY。');
|
|
61
|
+
}
|
|
62
|
+
const hasReferences = Array.isArray(options.references) && options.references.length > 0;
|
|
63
|
+
if (options.voice.trim() === '' && !hasReferences) {
|
|
64
|
+
throw new Error('未指定音色:请填写 voice 或提供参考音频 references。');
|
|
65
|
+
}
|
|
66
|
+
const headers = {
|
|
67
|
+
'content-type': 'application/json',
|
|
68
|
+
};
|
|
69
|
+
if (key !== '') {
|
|
70
|
+
headers.authorization = 'Bearer ' + key;
|
|
71
|
+
}
|
|
72
|
+
const effectiveModel = options.model || 'gpt-4o-mini-tts';
|
|
73
|
+
const isCosyVoice = effectiveModel.toLowerCase().includes('cosyvoice');
|
|
74
|
+
const instr = options.instructions?.trim() ?? '';
|
|
75
|
+
let input = options.text;
|
|
76
|
+
if (instr !== '' && isCosyVoice && !options.text.includes('<|endofprompt|>')) {
|
|
77
|
+
input = instr + '<|endofprompt|>' + options.text;
|
|
78
|
+
}
|
|
79
|
+
const payload = {
|
|
80
|
+
model: effectiveModel,
|
|
81
|
+
input,
|
|
82
|
+
voice: options.voice,
|
|
83
|
+
response_format: 'mp3',
|
|
84
|
+
...(options.speed !== undefined && options.speed !== 1 ? { speed: options.speed } : {}),
|
|
85
|
+
...(instr !== '' && !isCosyVoice ? { instructions: instr } : {}),
|
|
86
|
+
...(hasReferences
|
|
87
|
+
? {
|
|
88
|
+
references: options.references.map((ref) => ({
|
|
89
|
+
audio: ref.audioDataUri,
|
|
90
|
+
text: ref.text ?? '',
|
|
91
|
+
})),
|
|
92
|
+
}
|
|
93
|
+
: {}),
|
|
94
|
+
};
|
|
95
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
96
|
+
const requestSignal = abortSignal === undefined ? timeoutSignal : AbortSignal.any([timeoutSignal, abortSignal]);
|
|
97
|
+
let response;
|
|
98
|
+
try {
|
|
99
|
+
response = await raceAbort(fetchImpl(trimmedBaseUrl + '/audio/speech', {
|
|
100
|
+
method: 'POST',
|
|
101
|
+
redirect: 'manual',
|
|
102
|
+
headers,
|
|
103
|
+
body: JSON.stringify(payload),
|
|
104
|
+
signal: requestSignal,
|
|
105
|
+
}), requestSignal);
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
throwIfAborted(abortSignal);
|
|
109
|
+
if (timeoutSignal.aborted)
|
|
110
|
+
throw new Error('TTS 请求超时,请重试或检查网络。');
|
|
111
|
+
throw new Error('TTS 请求失败:' + (error instanceof Error ? error.message : String(error)) + '。若接口需要特殊代理(梯子),请在 DSH「设置 → 插件 → 语音」配置 proxyUrl(保存即生效)。');
|
|
112
|
+
}
|
|
113
|
+
if (response.status >= 300 && response.status < 400) {
|
|
114
|
+
try {
|
|
115
|
+
await raceAbort(response.body?.cancel(), requestSignal);
|
|
116
|
+
}
|
|
117
|
+
catch { /* 忽略 */ }
|
|
118
|
+
throwIfAborted(abortSignal);
|
|
119
|
+
throw new Error('TTS 接口返回重定向(HTTP ' + response.status + '),已阻止跳转。请确认 ttsBaseUrl 为可信的最终接口地址。');
|
|
120
|
+
}
|
|
121
|
+
if (!response.ok) {
|
|
122
|
+
let body = '';
|
|
123
|
+
try {
|
|
124
|
+
body = (await raceAbort(response.text(), requestSignal)).slice(0, 300);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
throwIfAborted(abortSignal);
|
|
128
|
+
if (timeoutSignal.aborted)
|
|
129
|
+
throw new Error('TTS 响应读取超时,请重试。');
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
throw new Error('TTS 失败:HTTP ' + response.status + '。' + body);
|
|
133
|
+
}
|
|
134
|
+
let arrayBuffer;
|
|
135
|
+
try {
|
|
136
|
+
arrayBuffer = await raceAbort(response.arrayBuffer(), requestSignal);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
throwIfAborted(abortSignal);
|
|
140
|
+
if (timeoutSignal.aborted)
|
|
141
|
+
throw new Error('TTS 响应读取超时,请重试。');
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
const audio = Buffer.from(arrayBuffer);
|
|
145
|
+
if (audio.length === 0) {
|
|
146
|
+
throw new Error('TTS 合成结果为空。');
|
|
147
|
+
}
|
|
148
|
+
return audio;
|
|
149
|
+
}
|
package/lib/paths.d.ts
CHANGED
package/lib/paths.js
CHANGED
|
@@ -68,14 +68,8 @@ function assertWritableTarget(target, root, label) {
|
|
|
68
68
|
throw new Error(label + ' 超出会话工作区:' + target + '(实际解析到 ' + real + ')。请把输出放在会话工作目录内。');
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
-
/**
|
|
72
|
-
|
|
73
|
-
*
|
|
74
|
-
* 相对路径按 cwd(会话工作区)解析,返回解析后的绝对路径;符号链接会先
|
|
75
|
-
* 解析真实路径再判断边界,避免把工作区外的隐私文件(如 id_ed25519)当成
|
|
76
|
-
* 音频整份上传给 ASR 服务。
|
|
77
|
-
*/
|
|
78
|
-
export function assertAudioFile(input, cwd = process.cwd()) {
|
|
71
|
+
/** 校验工作区内的普通文件,允许调用方定制越界提示后缀。 */
|
|
72
|
+
function assertWorkspaceFileInternal(input, cwd, label, outsideHint) {
|
|
79
73
|
const root = workspaceRoot(cwd);
|
|
80
74
|
const requested = resolve(root, input);
|
|
81
75
|
let real;
|
|
@@ -83,15 +77,31 @@ export function assertAudioFile(input, cwd = process.cwd()) {
|
|
|
83
77
|
real = realpathSync(requested);
|
|
84
78
|
}
|
|
85
79
|
catch {
|
|
86
|
-
throw new Error('
|
|
80
|
+
throw new Error(label + '不存在:' + input);
|
|
87
81
|
}
|
|
88
82
|
if (!isInside(root, real)) {
|
|
89
|
-
throw new Error('
|
|
83
|
+
throw new Error(label + '必须位于会话工作区内:' + input + '(实际解析到 ' + real + ')。' + outsideHint);
|
|
90
84
|
}
|
|
91
85
|
if (!statSync(real).isFile())
|
|
92
|
-
throw new Error('
|
|
86
|
+
throw new Error(label + '不是普通文件:' + input);
|
|
93
87
|
return requested;
|
|
94
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* 校验工作区内的普通文件(如文稿输入):必须存在于会话工作区内且是普通文件。
|
|
91
|
+
*/
|
|
92
|
+
export function assertWorkspaceFile(input, cwd = process.cwd(), label = '输入文件') {
|
|
93
|
+
return assertWorkspaceFileInternal(input, cwd, label, '请先复制到工作目录。');
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 校验音频文件:必须存在于会话工作区内,且是普通文件。
|
|
97
|
+
*
|
|
98
|
+
* 相对路径按 cwd(会话工作区)解析,返回解析后的绝对路径;符号链接会先
|
|
99
|
+
* 解析真实路径再判断边界,避免把工作区外的隐私文件(如 id_ed25519)当成
|
|
100
|
+
* 音频整份上传给 ASR 服务。
|
|
101
|
+
*/
|
|
102
|
+
export function assertAudioFile(input, cwd = process.cwd()) {
|
|
103
|
+
return assertWorkspaceFileInternal(input, cwd, '音频文件', '请先复制到工作目录再转写。');
|
|
104
|
+
}
|
|
95
105
|
/**
|
|
96
106
|
* 决定输出路径:缺省放在会话工作目录(缺省回退宿主 cwd),同名自动加 _1/_2 序号。
|
|
97
107
|
*
|
package/lib/proxy-fetch.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as net from 'node:net';
|
|
1
2
|
/**
|
|
2
3
|
* 代理 fetch 一并带出 undici 自己的 FormData 构造器。
|
|
3
4
|
*
|
|
@@ -8,4 +9,9 @@
|
|
|
8
9
|
export type ProxyFetch = typeof globalThis.fetch & {
|
|
9
10
|
FormData: typeof globalThis.FormData;
|
|
10
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* 通过 RFC 1928 / RFC 1929 SOCKS5 协议建立到目标 host:port 的 TCP 隧道。
|
|
14
|
+
*/
|
|
15
|
+
export declare function connectSocks5Tunnel(proxyUrl: URL, targetHost: string, targetPort: number, useRemoteDns: boolean, signal?: AbortSignal): Promise<net.Socket>;
|
|
11
16
|
export declare function createProxyFetch(proxyUrl: string): ProxyFetch;
|
|
17
|
+
export declare function getProxyWsAgent(proxyUrl: string): unknown;
|
package/lib/proxy-fetch.js
CHANGED
|
@@ -1,10 +1,350 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 插件级代理支持(HTTP(S) + 零依赖 SOCKS5 / SOCKS5H):
|
|
3
|
+
* - createProxyFetch:给 TTS 令牌、OpenAI TTS 与 ASR 请求提供走代理的 fetch(附带 undici FormData);
|
|
4
|
+
* - getProxyWsAgent:给 edge-tts WebSocket 提供按 proxyUrl 复用的代理 Agent。
|
|
3
5
|
*/
|
|
4
|
-
import
|
|
6
|
+
import * as https from 'node:https';
|
|
7
|
+
import * as net from 'node:net';
|
|
8
|
+
import * as tls from 'node:tls';
|
|
9
|
+
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
10
|
+
import { Agent, FormData as UndiciFormData, ProxyAgent, fetch as undiciFetch } from 'undici';
|
|
11
|
+
const wsAgentCache = new Map();
|
|
12
|
+
const SOCKS5_ERRORS = {
|
|
13
|
+
0x01: '一般性 SOCKS 服务器故障',
|
|
14
|
+
0x02: '规则集不允许连接',
|
|
15
|
+
0x03: '网络不可达',
|
|
16
|
+
0x04: '主机不可达',
|
|
17
|
+
0x05: '目标拒绝连接',
|
|
18
|
+
0x06: 'TTL 已过期',
|
|
19
|
+
0x07: '不支持的 SOCKS 命令',
|
|
20
|
+
0x08: '不支持的地址类型',
|
|
21
|
+
};
|
|
22
|
+
function stripBrackets(host) {
|
|
23
|
+
return host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
|
|
24
|
+
}
|
|
25
|
+
function ipv6ToBuffer(ip) {
|
|
26
|
+
const noZone = ip.split('%')[0];
|
|
27
|
+
const halves = noZone.split('::');
|
|
28
|
+
if (halves.length > 2)
|
|
29
|
+
throw new Error('非法的 IPv6 地址:' + ip);
|
|
30
|
+
const parseSide = (part) => {
|
|
31
|
+
if (part === '')
|
|
32
|
+
return [];
|
|
33
|
+
const tokens = part.split(':');
|
|
34
|
+
const words = [];
|
|
35
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
36
|
+
const token = tokens[i];
|
|
37
|
+
if (i === tokens.length - 1 && token.includes('.')) {
|
|
38
|
+
const octets = token.split('.').map(Number);
|
|
39
|
+
if (octets.length !== 4 || octets.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) {
|
|
40
|
+
throw new Error('非法的 IPv4 映射 IPv6 地址:' + ip);
|
|
41
|
+
}
|
|
42
|
+
words.push((octets[0] << 8) | octets[1], (octets[2] << 8) | octets[3]);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const val = parseInt(token, 16);
|
|
46
|
+
if (!Number.isInteger(val) || val < 0 || val > 0xffff) {
|
|
47
|
+
throw new Error('非法的 IPv6 地址段:' + ip);
|
|
48
|
+
}
|
|
49
|
+
words.push(val);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return words;
|
|
53
|
+
};
|
|
54
|
+
const left = parseSide(halves[0]);
|
|
55
|
+
const right = halves.length === 2 ? parseSide(halves[1]) : [];
|
|
56
|
+
const missing = 8 - left.length - right.length;
|
|
57
|
+
if ((halves.length === 1 && missing !== 0) || missing < 0) {
|
|
58
|
+
throw new Error('非法的 IPv6 地址长度:' + ip);
|
|
59
|
+
}
|
|
60
|
+
const words = [...left, ...new Array(missing).fill(0), ...right];
|
|
61
|
+
const buf = Buffer.alloc(16);
|
|
62
|
+
for (let i = 0; i < 8; i++)
|
|
63
|
+
buf.writeUInt16BE(words[i], i * 2);
|
|
64
|
+
return buf;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 通过 RFC 1928 / RFC 1929 SOCKS5 协议建立到目标 host:port 的 TCP 隧道。
|
|
68
|
+
*/
|
|
69
|
+
export async function connectSocks5Tunnel(proxyUrl, targetHost, targetPort, useRemoteDns, signal) {
|
|
70
|
+
if (signal?.aborted)
|
|
71
|
+
throw signal.reason;
|
|
72
|
+
if (!Number.isInteger(targetPort) || targetPort < 1 || targetPort > 65535) {
|
|
73
|
+
throw new Error('目标端口不合法:' + String(targetPort));
|
|
74
|
+
}
|
|
75
|
+
const proxyHost = stripBrackets(proxyUrl.hostname);
|
|
76
|
+
const proxyPort = proxyUrl.port ? Number(proxyUrl.port) : 1080;
|
|
77
|
+
if (!proxyHost || !Number.isInteger(proxyPort) || proxyPort < 1 || proxyPort > 65535) {
|
|
78
|
+
throw new Error('SOCKS5 代理地址不合法:' + proxyUrl.href);
|
|
79
|
+
}
|
|
80
|
+
const hasAuth = proxyUrl.username !== '' || proxyUrl.password !== '';
|
|
81
|
+
const username = decodeURIComponent(proxyUrl.username);
|
|
82
|
+
const password = decodeURIComponent(proxyUrl.password);
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
const socket = net.connect({ host: proxyHost, port: proxyPort });
|
|
85
|
+
let settled = false;
|
|
86
|
+
let buffered = [];
|
|
87
|
+
let bufferedBytes = 0;
|
|
88
|
+
let pendingRead = null;
|
|
89
|
+
const cleanupListeners = () => {
|
|
90
|
+
socket.removeListener('data', onData);
|
|
91
|
+
socket.removeListener('error', onError);
|
|
92
|
+
socket.removeListener('close', onClose);
|
|
93
|
+
signal?.removeEventListener('abort', onAbort);
|
|
94
|
+
};
|
|
95
|
+
const fail = (err) => {
|
|
96
|
+
if (settled)
|
|
97
|
+
return;
|
|
98
|
+
settled = true;
|
|
99
|
+
cleanupListeners();
|
|
100
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
101
|
+
if (pendingRead) {
|
|
102
|
+
const pr = pendingRead;
|
|
103
|
+
pendingRead = null;
|
|
104
|
+
pr.reject(error);
|
|
105
|
+
}
|
|
106
|
+
socket.destroy();
|
|
107
|
+
reject(error);
|
|
108
|
+
};
|
|
109
|
+
const onAbort = () => fail(signal?.reason ?? new Error('SOCKS5 连接已取消'));
|
|
110
|
+
const onError = (err) => fail(err);
|
|
111
|
+
const onClose = () => fail(new Error('SOCKS5 代理在握手完成前断开连接'));
|
|
112
|
+
const tryFlushRead = () => {
|
|
113
|
+
if (!pendingRead || bufferedBytes < pendingRead.needed)
|
|
114
|
+
return;
|
|
115
|
+
const { needed, resolve: res } = pendingRead;
|
|
116
|
+
pendingRead = null;
|
|
117
|
+
const full = buffered.length === 1 ? buffered[0] : Buffer.concat(buffered);
|
|
118
|
+
const head = full.subarray(0, needed);
|
|
119
|
+
const tail = full.subarray(needed);
|
|
120
|
+
buffered = tail.length > 0 ? [tail] : [];
|
|
121
|
+
bufferedBytes = tail.length;
|
|
122
|
+
res(head);
|
|
123
|
+
};
|
|
124
|
+
const onData = (chunk) => {
|
|
125
|
+
buffered.push(chunk);
|
|
126
|
+
bufferedBytes += chunk.length;
|
|
127
|
+
tryFlushRead();
|
|
128
|
+
};
|
|
129
|
+
const readBytes = (needed) => {
|
|
130
|
+
if (settled)
|
|
131
|
+
return Promise.reject(new Error('SOCKS5 连接已关闭'));
|
|
132
|
+
if (bufferedBytes >= needed) {
|
|
133
|
+
return new Promise((res) => {
|
|
134
|
+
pendingRead = { needed, resolve: res, reject: () => { } };
|
|
135
|
+
tryFlushRead();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return new Promise((res, rej) => {
|
|
139
|
+
pendingRead = { needed, resolve: res, reject: rej };
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
socket.on('data', onData);
|
|
143
|
+
socket.on('error', onError);
|
|
144
|
+
socket.on('close', onClose);
|
|
145
|
+
if (signal)
|
|
146
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
147
|
+
socket.once('connect', () => {
|
|
148
|
+
void (async () => {
|
|
149
|
+
// 1. 认证方法协商(RFC 1928 §3)
|
|
150
|
+
socket.write(hasAuth ? Buffer.from([0x05, 0x02, 0x00, 0x02]) : Buffer.from([0x05, 0x01, 0x00]));
|
|
151
|
+
const methodReply = await readBytes(2);
|
|
152
|
+
if (methodReply[0] !== 0x05)
|
|
153
|
+
throw new Error('SOCKS5 代理响应版本不合法:' + methodReply[0]);
|
|
154
|
+
const method = methodReply[1];
|
|
155
|
+
if (method === 0xff)
|
|
156
|
+
throw new Error('SOCKS5 代理没有可接受的认证方式');
|
|
157
|
+
if (method === 0x02) {
|
|
158
|
+
if (!hasAuth)
|
|
159
|
+
throw new Error('SOCKS5 代理要求用户名和密码认证');
|
|
160
|
+
const userBuf = Buffer.from(username, 'utf8');
|
|
161
|
+
const passBuf = Buffer.from(password, 'utf8');
|
|
162
|
+
if (userBuf.length > 255 || passBuf.length > 255) {
|
|
163
|
+
throw new Error('SOCKS5 用户名或密码过长(超过 255 字节)');
|
|
164
|
+
}
|
|
165
|
+
socket.write(Buffer.concat([Buffer.from([0x01, userBuf.length]), userBuf, Buffer.from([passBuf.length]), passBuf]));
|
|
166
|
+
const authReply = await readBytes(2);
|
|
167
|
+
if (authReply[1] !== 0x00)
|
|
168
|
+
throw new Error('SOCKS5 代理用户名或密码认证失败');
|
|
169
|
+
}
|
|
170
|
+
else if (method !== 0x00) {
|
|
171
|
+
throw new Error('不支持的 SOCKS5 认证方式:' + method);
|
|
172
|
+
}
|
|
173
|
+
// 2. CONNECT 请求(RFC 1928 §4)
|
|
174
|
+
const cleanHost = stripBrackets(targetHost);
|
|
175
|
+
const ipType = net.isIP(cleanHost);
|
|
176
|
+
let atyp;
|
|
177
|
+
let addrBuf;
|
|
178
|
+
if (ipType === 4) {
|
|
179
|
+
atyp = 0x01;
|
|
180
|
+
addrBuf = Buffer.from(cleanHost.split('.').map(Number));
|
|
181
|
+
}
|
|
182
|
+
else if (ipType === 6) {
|
|
183
|
+
atyp = 0x04;
|
|
184
|
+
addrBuf = ipv6ToBuffer(cleanHost);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
// socks5h:// 与 socks5:// 对域名均使用 ATYP 0x03(域名由代理端解析)
|
|
188
|
+
void useRemoteDns;
|
|
189
|
+
const hostBuf = Buffer.from(cleanHost, 'utf8');
|
|
190
|
+
if (hostBuf.length === 0 || hostBuf.length > 255) {
|
|
191
|
+
throw new Error('SOCKS5 目标主机名不合法:' + targetHost);
|
|
192
|
+
}
|
|
193
|
+
atyp = 0x03;
|
|
194
|
+
addrBuf = Buffer.concat([Buffer.from([hostBuf.length]), hostBuf]);
|
|
195
|
+
}
|
|
196
|
+
const portBuf = Buffer.alloc(2);
|
|
197
|
+
portBuf.writeUInt16BE(targetPort, 0);
|
|
198
|
+
socket.write(Buffer.concat([Buffer.from([0x05, 0x01, 0x00, atyp]), addrBuf, portBuf]));
|
|
199
|
+
// 3. 解析 CONNECT 响应
|
|
200
|
+
const header = await readBytes(4);
|
|
201
|
+
if (header[0] !== 0x05)
|
|
202
|
+
throw new Error('SOCKS5 CONNECT 响应版本不合法:' + header[0]);
|
|
203
|
+
if (header[1] !== 0x00) {
|
|
204
|
+
throw new Error('SOCKS5 连接失败:' + (SOCKS5_ERRORS[header[1]] ?? '错误码 0x' + header[1].toString(16)));
|
|
205
|
+
}
|
|
206
|
+
const bndAtyp = header[3];
|
|
207
|
+
if (bndAtyp === 0x01) {
|
|
208
|
+
await readBytes(6);
|
|
209
|
+
}
|
|
210
|
+
else if (bndAtyp === 0x04) {
|
|
211
|
+
await readBytes(18);
|
|
212
|
+
}
|
|
213
|
+
else if (bndAtyp === 0x03) {
|
|
214
|
+
const lenBuf = await readBytes(1);
|
|
215
|
+
await readBytes(lenBuf[0] + 2);
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
throw new Error('SOCKS5 响应地址类型不合法:' + bndAtyp);
|
|
219
|
+
}
|
|
220
|
+
if (settled)
|
|
221
|
+
return;
|
|
222
|
+
settled = true;
|
|
223
|
+
cleanupListeners();
|
|
224
|
+
if (bufferedBytes > 0) {
|
|
225
|
+
const leftover = buffered.length === 1 ? buffered[0] : Buffer.concat(buffered);
|
|
226
|
+
buffered = [];
|
|
227
|
+
bufferedBytes = 0;
|
|
228
|
+
socket.unshift(leftover);
|
|
229
|
+
}
|
|
230
|
+
resolve(socket);
|
|
231
|
+
})().catch(fail);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
class Socks5WsAgent extends https.Agent {
|
|
236
|
+
proxyUrl;
|
|
237
|
+
useRemoteDns;
|
|
238
|
+
constructor(proxyUrl, useRemoteDns) {
|
|
239
|
+
super({ keepAlive: false });
|
|
240
|
+
this.proxyUrl = proxyUrl;
|
|
241
|
+
this.useRemoteDns = useRemoteDns;
|
|
242
|
+
}
|
|
243
|
+
createConnection(options, callback) {
|
|
244
|
+
const targetHost = options.hostname || options.host || '';
|
|
245
|
+
const isSecure = options.protocol ? options.protocol === 'https:' : true;
|
|
246
|
+
const targetPort = Number(options.port || (isSecure ? 443 : 80));
|
|
247
|
+
connectSocks5Tunnel(this.proxyUrl, targetHost, targetPort, this.useRemoteDns)
|
|
248
|
+
.then((rawSocket) => {
|
|
249
|
+
if (isSecure) {
|
|
250
|
+
const cleanHost = stripBrackets(targetHost);
|
|
251
|
+
const servername = options.servername || (!net.isIP(cleanHost) ? cleanHost : undefined);
|
|
252
|
+
const tlsSocket = tls.connect({
|
|
253
|
+
socket: rawSocket,
|
|
254
|
+
...(servername ? { servername } : {}),
|
|
255
|
+
...(options.rejectUnauthorized !== undefined ? { rejectUnauthorized: options.rejectUnauthorized } : {}),
|
|
256
|
+
});
|
|
257
|
+
callback?.(null, tlsSocket);
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
callback?.(null, rawSocket);
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
.catch((error) => {
|
|
264
|
+
callback?.(error instanceof Error ? error : new Error(String(error)), undefined);
|
|
265
|
+
});
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
5
269
|
export function createProxyFetch(proxyUrl) {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
270
|
+
const parsed = new URL(proxyUrl);
|
|
271
|
+
const isSocks5 = parsed.protocol === 'socks5:' || parsed.protocol === 'socks5h:';
|
|
272
|
+
const dispatcher = isSocks5
|
|
273
|
+
? new Agent({
|
|
274
|
+
connect(opts, cb) {
|
|
275
|
+
const useRemoteDns = parsed.protocol === 'socks5h:';
|
|
276
|
+
const targetPort = Number(opts.port || (opts.protocol === 'https:' ? 443 : 80));
|
|
277
|
+
const signal = opts.signal;
|
|
278
|
+
connectSocks5Tunnel(parsed, opts.hostname, targetPort, useRemoteDns, signal)
|
|
279
|
+
.then((rawSocket) => {
|
|
280
|
+
if (opts.protocol === 'https:') {
|
|
281
|
+
const cleanHost = stripBrackets(opts.servername || opts.hostname);
|
|
282
|
+
const servername = net.isIP(cleanHost) ? undefined : cleanHost;
|
|
283
|
+
const tlsSocket = tls.connect({
|
|
284
|
+
socket: rawSocket,
|
|
285
|
+
...(servername ? { servername } : {}),
|
|
286
|
+
});
|
|
287
|
+
let settled = false;
|
|
288
|
+
const cleanup = () => {
|
|
289
|
+
tlsSocket.removeListener('secureConnect', onSecureConnect);
|
|
290
|
+
tlsSocket.removeListener('error', onError);
|
|
291
|
+
signal?.removeEventListener('abort', onAbort);
|
|
292
|
+
};
|
|
293
|
+
const onSecureConnect = () => {
|
|
294
|
+
if (settled)
|
|
295
|
+
return;
|
|
296
|
+
settled = true;
|
|
297
|
+
cleanup();
|
|
298
|
+
cb(null, tlsSocket);
|
|
299
|
+
};
|
|
300
|
+
const onError = (err) => {
|
|
301
|
+
if (settled)
|
|
302
|
+
return;
|
|
303
|
+
settled = true;
|
|
304
|
+
cleanup();
|
|
305
|
+
tlsSocket.destroy();
|
|
306
|
+
cb(err, null);
|
|
307
|
+
};
|
|
308
|
+
const onAbort = () => {
|
|
309
|
+
if (settled)
|
|
310
|
+
return;
|
|
311
|
+
settled = true;
|
|
312
|
+
cleanup();
|
|
313
|
+
const reason = signal?.reason;
|
|
314
|
+
const err = reason instanceof Error ? reason : new Error(String(reason ?? 'SOCKS5 TLS handshake aborted'));
|
|
315
|
+
tlsSocket.destroy(err);
|
|
316
|
+
cb(err, null);
|
|
317
|
+
};
|
|
318
|
+
if (signal?.aborted) {
|
|
319
|
+
onAbort();
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
tlsSocket.once('secureConnect', onSecureConnect);
|
|
323
|
+
tlsSocket.once('error', onError);
|
|
324
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
cb(null, rawSocket);
|
|
328
|
+
}
|
|
329
|
+
})
|
|
330
|
+
.catch((error) => {
|
|
331
|
+
cb(error instanceof Error ? error : new Error(String(error)), null);
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
})
|
|
335
|
+
: new ProxyAgent(proxyUrl);
|
|
336
|
+
const impl = ((input, init) => undiciFetch(input, { ...init, dispatcher }));
|
|
8
337
|
impl.FormData = UndiciFormData;
|
|
9
338
|
return impl;
|
|
10
339
|
}
|
|
340
|
+
export function getProxyWsAgent(proxyUrl) {
|
|
341
|
+
const cached = wsAgentCache.get(proxyUrl);
|
|
342
|
+
if (cached !== undefined)
|
|
343
|
+
return cached;
|
|
344
|
+
const parsed = new URL(proxyUrl);
|
|
345
|
+
const agent = parsed.protocol === 'socks5:' || parsed.protocol === 'socks5h:'
|
|
346
|
+
? new Socks5WsAgent(parsed, parsed.protocol === 'socks5h:')
|
|
347
|
+
: new HttpsProxyAgent(proxyUrl);
|
|
348
|
+
wsAgentCache.set(proxyUrl, agent);
|
|
349
|
+
return agent;
|
|
350
|
+
}
|
package/lib/stt.d.ts
CHANGED
|
@@ -1,24 +1,58 @@
|
|
|
1
|
+
/** 时间戳分段结构(verbose_json 返回)。 */
|
|
2
|
+
export interface SttSegment {
|
|
3
|
+
id: number;
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
text: string;
|
|
7
|
+
}
|
|
1
8
|
/** 转写选项。 */
|
|
2
9
|
export interface SttOptions {
|
|
3
|
-
audio: Buffer;
|
|
10
|
+
audio: Buffer | Blob;
|
|
4
11
|
filename: string;
|
|
5
12
|
model: string;
|
|
6
13
|
language?: string;
|
|
7
14
|
prompt?: string;
|
|
15
|
+
timestamps?: boolean;
|
|
16
|
+
allowEmptyApiKey?: boolean;
|
|
8
17
|
/** 取消信号(可选);也可通过 transcribe 第 6 个参数传入。 */
|
|
9
18
|
signal?: AbortSignal;
|
|
10
19
|
}
|
|
20
|
+
/** 转写结果。 */
|
|
21
|
+
export interface SttResult {
|
|
22
|
+
text: string;
|
|
23
|
+
model: string;
|
|
24
|
+
language?: string;
|
|
25
|
+
duration?: number;
|
|
26
|
+
segments?: SttSegment[];
|
|
27
|
+
}
|
|
28
|
+
/** 预处理后的单个音频切片。 */
|
|
29
|
+
export interface PreparedAudioChunk {
|
|
30
|
+
audio: Blob | Buffer;
|
|
31
|
+
filename: string;
|
|
32
|
+
offsetSeconds: number;
|
|
33
|
+
}
|
|
34
|
+
/** 超限音频预处理结果(含临时目录清理回调)。 */
|
|
35
|
+
export interface OversizeAudioPreparation {
|
|
36
|
+
chunks: PreparedAudioChunk[];
|
|
37
|
+
cleanup: () => void;
|
|
38
|
+
}
|
|
11
39
|
/** 字节数超过 25MB 时抛错:调用方可在读文件前先按 statSync 大小快速拒绝,避免白读整文件。 */
|
|
12
40
|
export declare function assertAudioSize(bytes: number): void;
|
|
13
|
-
/**
|
|
14
|
-
* 调用 OpenAI 兼容 ASR 接口转写音频。
|
|
15
|
-
* @throws 缺密钥 / 文件过大 / HTTP 错误 / 无文本时抛中文错误。
|
|
16
|
-
*/
|
|
17
41
|
/** 传给 transcribe 的 fetch:可以像代理那样一并带上自己的 FormData/Blob 构造器。 */
|
|
18
42
|
export type SttFetch = typeof fetch & {
|
|
19
43
|
FormData?: typeof FormData;
|
|
20
44
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
45
|
+
/**
|
|
46
|
+
* 调用 OpenAI 兼容 ASR 接口转写音频。
|
|
47
|
+
* @throws 缺密钥 / 文件过大 / HTTP 错误 / 无文本时抛中文错误。
|
|
48
|
+
*/
|
|
49
|
+
export declare function transcribe(baseUrl: string, apiKey: string, options: SttOptions, fetchImpl?: SttFetch, timeoutMs?: number, signal?: AbortSignal): Promise<SttResult>;
|
|
50
|
+
/** 将 ASR 返回的 segments 格式化为 SRT 或 WebVTT 字幕文本。 */
|
|
51
|
+
export declare function formatSttSubtitles(segments: SttSegment[], format: 'srt' | 'vtt'): string;
|
|
52
|
+
/** 检测环境 PATH 中是否可用 ffmpeg。 */
|
|
53
|
+
export declare function hasFfmpeg(env?: NodeJS.ProcessEnv): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 使用 ffmpeg 将大于 25MB 的音频转码为 16kHz 单声道 32kbps MP3;
|
|
56
|
+
* 若转码后仍超过 24MB,则按每段 20 分钟(1200 秒)无损切分。
|
|
57
|
+
*/
|
|
58
|
+
export declare function prepareAudioForAsr(audioPath: string, signal?: AbortSignal): Promise<OversizeAudioPreparation>;
|