@maiyunnet/kebab 9.29.5 → 10.0.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/abox/proxy.js +24 -2
- package/abox/response.js +36 -17
- package/abox/session-login.js +1 -13
- package/abox/text.d.ts +2 -0
- package/abox/text.js +51 -0
- package/doc/kebab-rag.md +2700 -1270
- package/index.d.ts +12 -1
- package/index.js +1 -1
- package/lib/ai.d.ts +4 -0
- package/lib/ai.js +32 -16
- package/lib/cookie.d.ts +2 -0
- package/lib/cookie.js +47 -45
- package/lib/core.d.ts +26 -0
- package/lib/core.js +36 -13
- package/lib/cron.js +1 -8
- package/lib/crypto.js +20 -6
- package/lib/db/conn.d.ts +10 -1
- package/lib/db/conn.js +1 -5
- package/lib/db/pool.d.ts +10 -2
- package/lib/db/pool.js +0 -6
- package/lib/db/tran.d.ts +10 -1
- package/lib/db/tran.js +0 -5
- package/lib/db.d.ts +3 -3
- package/lib/fs.js +78 -31
- package/lib/kv.d.ts +216 -88
- package/lib/kv.js +520 -230
- package/lib/s3.d.ts +2 -0
- package/lib/s3.js +14 -27
- package/lib/session.d.ts +7 -0
- package/lib/session.js +23 -2
- package/lib/socket.d.ts +4 -1
- package/lib/socket.js +1 -0
- package/lib/sql.d.ts +0 -5
- package/lib/sql.js +48 -12
- package/lib/text.d.ts +32 -8
- package/lib/text.js +30 -17
- package/lib/turnstile.d.ts +6 -0
- package/lib/turnstile.js +16 -9
- package/lib/undici/response.d.ts +13 -3
- package/lib/undici/response.js +0 -1
- package/lib/undici.d.ts +33 -1
- package/lib/undici.js +72 -56
- package/lib/vector.d.ts +33 -19
- package/lib/vector.js +0 -1
- package/lib/ws.d.ts +1 -1
- package/lib/ws.js +85 -128
- package/lib/zip.js +46 -45
- package/package.json +2 -2
- package/sys/child.js +178 -146
- package/sys/cmd.js +196 -197
- package/sys/ctr.d.ts +14 -14
- package/sys/ctr.js +145 -59
- package/sys/master.js +111 -80
- package/sys/mod.d.ts +112 -51
- package/sys/mod.js +23 -51
- package/sys/monitor/files.d.ts +2 -1
- package/sys/monitor/files.js +77 -21
- package/sys/monitor/watchdog.d.ts +7 -0
- package/sys/monitor/watchdog.js +311 -203
- package/sys/monitor.d.ts +59 -54
- package/sys/monitor.js +742 -681
- package/sys/route.d.ts +4 -4
- package/sys/route.js +93 -52
- package/www/example/ctr/test.js +8 -8
package/lib/turnstile.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Date: 2025-6-11
|
|
4
4
|
* Last: 2025-6-11 21:50:31
|
|
5
5
|
*/
|
|
6
|
+
// --- 第三方 ---
|
|
6
7
|
import * as tcc from 'tencentcloud-sdk-nodejs';
|
|
7
8
|
// --- 库和定义 ---
|
|
8
9
|
import * as lUndici from '#kebab/lib/undici.js';
|
|
@@ -17,6 +18,12 @@ export var EFACTORY;
|
|
|
17
18
|
EFACTORY[EFACTORY["CLOUDFLARE"] = 0] = "CLOUDFLARE";
|
|
18
19
|
EFACTORY[EFACTORY["TENCENT"] = 1] = "TENCENT";
|
|
19
20
|
})(EFACTORY || (EFACTORY = {}));
|
|
21
|
+
/**
|
|
22
|
+
* --- 校验人机验证令牌 ---
|
|
23
|
+
* @param ctr 控制器实例
|
|
24
|
+
* @param opt 厂家、前端令牌与客户端 IP
|
|
25
|
+
* @returns 是否通过验证
|
|
26
|
+
*/
|
|
20
27
|
export async function verify(ctr, opt) {
|
|
21
28
|
const config = ctr.getPrototype('_config');
|
|
22
29
|
switch (opt.factory) {
|
|
@@ -31,12 +38,11 @@ export async function verify(ctr, opt) {
|
|
|
31
38
|
if (!content) {
|
|
32
39
|
return false;
|
|
33
40
|
}
|
|
34
|
-
const
|
|
35
|
-
const json = lText.parseJson(str);
|
|
41
|
+
const json = lText.parseJson(content.toString());
|
|
36
42
|
if (!json) {
|
|
37
43
|
return false;
|
|
38
44
|
}
|
|
39
|
-
return json.success;
|
|
45
|
+
return !!json.success;
|
|
40
46
|
}
|
|
41
47
|
case EFACTORY.TENCENT: {
|
|
42
48
|
// --- 腾讯云验证 ---
|
|
@@ -46,12 +52,12 @@ export async function verify(ctr, opt) {
|
|
|
46
52
|
'secretKey': config.turnstile['TENCENT'].skey,
|
|
47
53
|
},
|
|
48
54
|
'profile': {
|
|
49
|
-
'signMethod': 'HmacSHA256',
|
|
55
|
+
'signMethod': 'HmacSHA256',
|
|
50
56
|
'httpProfile': {
|
|
51
|
-
'reqMethod': 'POST',
|
|
52
|
-
'reqTimeout': 30,
|
|
53
|
-
}
|
|
54
|
-
}
|
|
57
|
+
'reqMethod': 'POST',
|
|
58
|
+
'reqTimeout': 30,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
55
61
|
});
|
|
56
62
|
const io = opt.token.indexOf('|');
|
|
57
63
|
if (io === -1) {
|
|
@@ -62,7 +68,8 @@ export async function verify(ctr, opt) {
|
|
|
62
68
|
'Ticket': opt.token.slice(0, io),
|
|
63
69
|
'UserIp': opt.ip,
|
|
64
70
|
'Randstr': opt.token.slice(io + 1),
|
|
65
|
-
|
|
71
|
+
// --- 兼容 config 中 aid 为字符串的情况,无损转整数 ---
|
|
72
|
+
'CaptchaAppId': parseInt(config.turnstile['TENCENT'].aid),
|
|
66
73
|
'AppSecretKey': config.turnstile['TENCENT'].akey,
|
|
67
74
|
});
|
|
68
75
|
return res.CaptchaCode === 1;
|
package/lib/undici/response.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import * as undici from 'undici';
|
|
7
7
|
import * as zlib from 'zlib';
|
|
8
|
+
import type * as kebab from '#kebab/index.js';
|
|
8
9
|
import * as lUndici from '#kebab/lib/undici.js';
|
|
9
10
|
export declare class Response {
|
|
10
11
|
/** --- httpClient 请求对象 --- */
|
|
@@ -21,8 +22,17 @@ export declare class Response {
|
|
|
21
22
|
getContent(): Promise<Buffer | null>;
|
|
22
23
|
/** --- 读取所有内容为文本 --- */
|
|
23
24
|
getText(): Promise<string | null>;
|
|
24
|
-
/**
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* --- 读取并解析 JSON 响应 ---
|
|
27
|
+
* @returns 解析结果,响应为空或读取失败返回 null,JSON 解析失败返回 false
|
|
28
|
+
*/
|
|
29
|
+
getJson(): Promise<kebab.Json>;
|
|
30
|
+
/**
|
|
31
|
+
* --- 读取并解析 JSON 响应 ---
|
|
32
|
+
* @typeParam T 预期的响应类型,解析时只检查 JSON 语法
|
|
33
|
+
* @returns 解析结果,响应为空或读取失败返回 null,JSON 解析失败返回 false
|
|
34
|
+
*/
|
|
35
|
+
getJson<T>(): Promise<T | false | null>;
|
|
26
36
|
/**
|
|
27
37
|
* --- 用户自定义的 content 内容 ---
|
|
28
38
|
* @param v 内容值
|
|
@@ -31,7 +41,7 @@ export declare class Response {
|
|
|
31
41
|
/**
|
|
32
42
|
* --- 获取响应读取流对象 ---
|
|
33
43
|
*/
|
|
34
|
-
getStream():
|
|
44
|
+
getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | zlib.Gunzip | zlib.Inflate | zlib.BrotliDecompress | null;
|
|
35
45
|
/**
|
|
36
46
|
* --- 获取原生响应读取流对象 ---
|
|
37
47
|
*/
|
package/lib/undici/response.js
CHANGED
package/lib/undici.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as undici from 'undici';
|
|
2
1
|
import * as stream from 'stream';
|
|
3
2
|
import * as http from 'http';
|
|
4
3
|
import * as http2 from 'http2';
|
|
4
|
+
import * as undici from 'undici';
|
|
5
5
|
import * as kebab from '#kebab/index.js';
|
|
6
6
|
import * as lCookie from '#kebab/lib/cookie.js';
|
|
7
7
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
@@ -44,6 +44,15 @@ export declare function postJson(u: string, data: kebab.Json[] | Record<string,
|
|
|
44
44
|
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
45
45
|
*/
|
|
46
46
|
export declare function postJsonResponseJson(u: string, data: kebab.Json[] | Record<string, kebab.Json>, opt?: IRequestOptions): Promise<kebab.Json | null | false>;
|
|
47
|
+
/**
|
|
48
|
+
* --- 发起 JSON 请求并解析 JSON 响应 ---
|
|
49
|
+
* @typeParam T 预期的响应类型,解析时只检查 JSON 语法
|
|
50
|
+
* @param u 网址
|
|
51
|
+
* @param data 数据
|
|
52
|
+
* @param opt 选项
|
|
53
|
+
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
54
|
+
*/
|
|
55
|
+
export declare function postJsonResponseJson<T>(u: string, data: kebab.Json[] | Record<string, kebab.Json>, opt?: IRequestOptions): Promise<T | false | null>;
|
|
47
56
|
/**
|
|
48
57
|
* --- 发起 POST 请求并解析 JSON 响应 ---
|
|
49
58
|
* @param u 网址
|
|
@@ -52,6 +61,15 @@ export declare function postJsonResponseJson(u: string, data: kebab.Json[] | Rec
|
|
|
52
61
|
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
53
62
|
*/
|
|
54
63
|
export declare function postResponseJson(u: string, data: Record<string, kebab.Json>, opt?: IRequestOptions): Promise<kebab.Json | null | false>;
|
|
64
|
+
/**
|
|
65
|
+
* --- 发起 POST 请求并解析 JSON 响应 ---
|
|
66
|
+
* @typeParam T 预期的响应类型,解析时只检查 JSON 语法
|
|
67
|
+
* @param u 网址
|
|
68
|
+
* @param data 数据
|
|
69
|
+
* @param opt 选项
|
|
70
|
+
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
71
|
+
*/
|
|
72
|
+
export declare function postResponseJson<T>(u: string, data: Record<string, kebab.Json>, opt?: IRequestOptions): Promise<T | false | null>;
|
|
55
73
|
/**
|
|
56
74
|
* --- 发起 GET 请求并解析 JSON 响应 ---
|
|
57
75
|
* @param u 网址
|
|
@@ -59,6 +77,14 @@ export declare function postResponseJson(u: string, data: Record<string, kebab.J
|
|
|
59
77
|
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
60
78
|
*/
|
|
61
79
|
export declare function getResponseJson(u: string, opt?: IRequestOptions): Promise<kebab.Json | null | false>;
|
|
80
|
+
/**
|
|
81
|
+
* --- 发起 GET 请求并解析 JSON 响应 ---
|
|
82
|
+
* @typeParam T 预期的响应类型,解析时只检查 JSON 语法
|
|
83
|
+
* @param u 网址
|
|
84
|
+
* @param opt 选项
|
|
85
|
+
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
86
|
+
*/
|
|
87
|
+
export declare function getResponseJson<T>(u: string, opt?: IRequestOptions): Promise<T | false | null>;
|
|
62
88
|
/**
|
|
63
89
|
* --- 发起一个完全兼容 fetch 的请求 ---
|
|
64
90
|
* @param input 请求的 URL 或 Request 对象
|
|
@@ -177,10 +203,13 @@ export interface IMproxyOptions {
|
|
|
177
203
|
'proxyMode'?: 'passthrough' | 'request';
|
|
178
204
|
/** --- 秒数,默认 300 秒 --- */
|
|
179
205
|
'timeout'?: number;
|
|
206
|
+
/** --- 追踪 location 次数,0 为不追踪,默认为 0 --- */
|
|
180
207
|
'follow'?: number;
|
|
181
208
|
/** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
|
|
182
209
|
'hosts'?: Record<string, string> | string;
|
|
210
|
+
/** --- 使用的本地网卡 IP --- */
|
|
183
211
|
'local'?: string;
|
|
212
|
+
/** --- 自定义请求头 --- */
|
|
184
213
|
'headers'?: THttpHeaders;
|
|
185
214
|
/** --- 空闲连接允许复用的最长秒数;非正数或无效值按未设置处理;默认无服务端提示时为 4 秒,有提示时采用提示值减 2 秒且最多 600 秒 --- */
|
|
186
215
|
'keepAliveTimeout'?: number;
|
|
@@ -195,10 +224,13 @@ export interface IMproxyOptions {
|
|
|
195
224
|
export interface IRproxyOptions extends lProxy.IProxyHeaderOptions {
|
|
196
225
|
/** --- 秒数,默认 300 秒 --- */
|
|
197
226
|
'timeout'?: number;
|
|
227
|
+
/** --- 追踪 location 次数,0 为不追踪,默认为 0 --- */
|
|
198
228
|
'follow'?: number;
|
|
199
229
|
/** --- 自定义 host 映射,如 {'www.maiyun.net': '127.0.0.1'},或全部映射到一个 host --- */
|
|
200
230
|
'hosts'?: Record<string, string> | string;
|
|
231
|
+
/** --- 使用的本地网卡 IP --- */
|
|
201
232
|
'local'?: string;
|
|
233
|
+
/** --- 自定义请求头 --- */
|
|
202
234
|
'headers'?: THttpHeaders;
|
|
203
235
|
/** --- 空闲连接允许复用的最长秒数;非正数或无效值按未设置处理;默认无服务端提示时为 4 秒,有提示时采用提示值减 2 秒且最多 600 秒 --- */
|
|
204
236
|
'keepAliveTimeout'?: number;
|
package/lib/undici.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
// --- 系统 ---
|
|
2
2
|
import * as stream from 'stream';
|
|
3
3
|
import * as dns from 'dns';
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
// --- 第三方 ---
|
|
6
|
+
import * as undici from 'undici';
|
|
4
7
|
import * as lFs from '#kebab/lib/fs.js';
|
|
5
8
|
import * as lText from '#kebab/lib/text.js';
|
|
6
9
|
import * as lCrypto from '#kebab/lib/crypto.js';
|
|
@@ -151,8 +154,7 @@ export async function get(u, opt = {}) {
|
|
|
151
154
|
* @param opt 参数
|
|
152
155
|
*/
|
|
153
156
|
export async function post(u, data, opt = {}) {
|
|
154
|
-
opt
|
|
155
|
-
return request(u, data, opt);
|
|
157
|
+
return request(u, data, { ...opt, 'method': 'POST' });
|
|
156
158
|
}
|
|
157
159
|
/**
|
|
158
160
|
* --- 发起 JSON 请求 ---
|
|
@@ -161,9 +163,7 @@ export async function post(u, data, opt = {}) {
|
|
|
161
163
|
* @param opt 选项
|
|
162
164
|
*/
|
|
163
165
|
export async function postJson(u, data, opt = {}) {
|
|
164
|
-
opt
|
|
165
|
-
opt.type = 'json';
|
|
166
|
-
return request(u, data, opt);
|
|
166
|
+
return request(u, data, { ...opt, 'method': 'POST', 'type': 'json' });
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
169
|
* --- 发起请求并解析 JSON 响应,解析失败或重试验证未通过时重新请求 ---
|
|
@@ -201,35 +201,12 @@ async function requestResponseJson(u, data, opt, action) {
|
|
|
201
201
|
}
|
|
202
202
|
return false;
|
|
203
203
|
}
|
|
204
|
-
/**
|
|
205
|
-
* --- 发起 JSON 请求并解析 JSON 响应 ---
|
|
206
|
-
* @param u 网址
|
|
207
|
-
* @param data 数据
|
|
208
|
-
* @param opt 选项
|
|
209
|
-
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
210
|
-
*/
|
|
211
204
|
export async function postJsonResponseJson(u, data, opt = {}) {
|
|
212
|
-
opt
|
|
213
|
-
opt.type = 'json';
|
|
214
|
-
return requestResponseJson(u, data, opt, 'POSTJSONRESPONSEJSON');
|
|
205
|
+
return requestResponseJson(u, data, { ...opt, 'method': 'POST', 'type': 'json' }, 'POSTJSONRESPONSEJSON');
|
|
215
206
|
}
|
|
216
|
-
/**
|
|
217
|
-
* --- 发起 POST 请求并解析 JSON 响应 ---
|
|
218
|
-
* @param u 网址
|
|
219
|
-
* @param data 数据
|
|
220
|
-
* @param opt 选项
|
|
221
|
-
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
222
|
-
*/
|
|
223
207
|
export async function postResponseJson(u, data, opt = {}) {
|
|
224
|
-
opt
|
|
225
|
-
return requestResponseJson(u, data, opt, 'POSTRESPONSEJSON');
|
|
208
|
+
return requestResponseJson(u, data, { ...opt, 'method': 'POST' }, 'POSTRESPONSEJSON');
|
|
226
209
|
}
|
|
227
|
-
/**
|
|
228
|
-
* --- 发起 GET 请求并解析 JSON 响应 ---
|
|
229
|
-
* @param u 网址
|
|
230
|
-
* @param opt 选项
|
|
231
|
-
* @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
|
|
232
|
-
*/
|
|
233
210
|
export async function getResponseJson(u, opt = {}) {
|
|
234
211
|
return requestResponseJson(u, undefined, opt, 'GETRESPONSEJSON');
|
|
235
212
|
}
|
|
@@ -381,7 +358,7 @@ function buildDnsLookup(hosts) {
|
|
|
381
358
|
return undefined;
|
|
382
359
|
}
|
|
383
360
|
return (hostname, opts, cb) => {
|
|
384
|
-
const override = typeof hosts === 'string' ? hosts : hosts[hostname];
|
|
361
|
+
const override = typeof hosts === 'string' ? hosts : (Object.hasOwn(hosts, hostname) ? hosts[hostname] : undefined);
|
|
385
362
|
if (override) {
|
|
386
363
|
cb(null, [{ 'address': override, 'family': lText.isIPv4(override) ? 4 : 6 }]);
|
|
387
364
|
}
|
|
@@ -459,7 +436,7 @@ export async function request(u, data, opt = {}) {
|
|
|
459
436
|
}
|
|
460
437
|
if (typeof hosts === 'string' ?
|
|
461
438
|
!hosts :
|
|
462
|
-
(hosts
|
|
439
|
+
(Object.hasOwn(hosts, hostname) && !hosts[hostname])) {
|
|
463
440
|
const res = new lResponse.Response(null);
|
|
464
441
|
res.error = {
|
|
465
442
|
'name': 'hosts error',
|
|
@@ -537,22 +514,43 @@ export async function request(u, data, opt = {}) {
|
|
|
537
514
|
}
|
|
538
515
|
// --- 哦,要追踪 ---
|
|
539
516
|
const nextUrl = lText.urlResolve(u, req.headers['location']);
|
|
540
|
-
|
|
517
|
+
// --- 仅允许跟随 http/https 跳转,禁止跳向非 HTTP 协议 ---
|
|
518
|
+
let nextProtocol = '';
|
|
519
|
+
try {
|
|
520
|
+
nextProtocol = new URL(nextUrl).protocol;
|
|
521
|
+
}
|
|
522
|
+
catch {
|
|
523
|
+
return res;
|
|
524
|
+
}
|
|
525
|
+
if (nextProtocol !== 'http:' && nextProtocol !== 'https:') {
|
|
526
|
+
return res;
|
|
527
|
+
}
|
|
528
|
+
const sameOrigin = lText.isSameOrigin(u, nextUrl);
|
|
529
|
+
let nextMethod = method;
|
|
530
|
+
let nextData = data;
|
|
531
|
+
let nextHosts = hosts;
|
|
532
|
+
if (sameOrigin) {
|
|
541
533
|
headers['referer'] = u;
|
|
542
534
|
}
|
|
543
535
|
else {
|
|
544
|
-
// ---
|
|
536
|
+
// --- 跨域跳转不得携带来源站点凭据、hosts 覆盖与敏感请求体 ---
|
|
545
537
|
delete headers['authorization'];
|
|
546
538
|
delete headers['proxy-authorization'];
|
|
547
539
|
delete headers['cookie'];
|
|
548
540
|
delete headers['referer'];
|
|
541
|
+
nextHosts = {};
|
|
542
|
+
if (nextMethod !== 'GET') {
|
|
543
|
+
delete headers['content-type'];
|
|
544
|
+
delete headers['content-length'];
|
|
545
|
+
nextData = undefined;
|
|
546
|
+
}
|
|
549
547
|
}
|
|
550
|
-
let nextMethod = method;
|
|
551
|
-
let nextData = data;
|
|
552
548
|
const status = res.headers['http-code'];
|
|
553
549
|
if (status === 303 || ((status === 301 || status === 302) && method === 'POST')) {
|
|
554
550
|
nextMethod = 'GET';
|
|
555
551
|
nextData = undefined;
|
|
552
|
+
delete headers['content-type'];
|
|
553
|
+
delete headers['content-length'];
|
|
556
554
|
}
|
|
557
555
|
if (nextData instanceof stream.Readable) {
|
|
558
556
|
return res;
|
|
@@ -561,14 +559,12 @@ export async function request(u, data, opt = {}) {
|
|
|
561
559
|
await req.body.dump();
|
|
562
560
|
return request(nextUrl, nextData, {
|
|
563
561
|
...opt,
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
'headers': headers,
|
|
571
|
-
},
|
|
562
|
+
'method': nextMethod,
|
|
563
|
+
'type': type,
|
|
564
|
+
'timeout': timeout,
|
|
565
|
+
'follow': follow - 1,
|
|
566
|
+
'hosts': nextHosts,
|
|
567
|
+
'headers': headers,
|
|
572
568
|
});
|
|
573
569
|
}
|
|
574
570
|
/**
|
|
@@ -600,22 +596,41 @@ export async function mproxy(ctr, auth, opt = {}) {
|
|
|
600
596
|
if (req.readableEnded) {
|
|
601
597
|
return -4;
|
|
602
598
|
}
|
|
603
|
-
|
|
599
|
+
const suppliedAuth = get['auth'];
|
|
600
|
+
if (!auth || typeof suppliedAuth !== 'string') {
|
|
601
|
+
return 0;
|
|
602
|
+
}
|
|
603
|
+
const authBuf = Buffer.from(auth);
|
|
604
|
+
const suppliedBuf = Buffer.from(suppliedAuth);
|
|
605
|
+
if (authBuf.length !== suppliedBuf.length || !crypto.timingSafeEqual(authBuf, suppliedBuf)) {
|
|
604
606
|
return 0;
|
|
605
607
|
}
|
|
606
|
-
|
|
608
|
+
const targetUrl = get['url'];
|
|
609
|
+
if (typeof targetUrl !== 'string' || !targetUrl) {
|
|
610
|
+
return -1;
|
|
611
|
+
}
|
|
612
|
+
try {
|
|
613
|
+
const parsedTarget = new URL(targetUrl);
|
|
614
|
+
if (parsedTarget.protocol !== 'http:' && parsedTarget.protocol !== 'https:') {
|
|
615
|
+
return -1;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
catch {
|
|
607
619
|
return -1;
|
|
608
620
|
}
|
|
609
|
-
|
|
610
|
-
|
|
621
|
+
const proxyOpt = {
|
|
622
|
+
...opt,
|
|
623
|
+
'method': (req.method ?? 'GET'),
|
|
624
|
+
'headers': opt.headers ?? {},
|
|
625
|
+
};
|
|
611
626
|
/** --- 传来的 hosts --- */
|
|
612
|
-
const hosts = lText.parseJson(get['hosts']);
|
|
627
|
+
const hosts = typeof get['hosts'] === 'string' ? lText.parseJson(get['hosts']) : null;
|
|
613
628
|
const headers = lProxy.getProxyHeaders(req, opt);
|
|
614
629
|
// --- 发起请求 ---
|
|
615
|
-
const rres = await request(
|
|
616
|
-
...
|
|
630
|
+
const rres = await request(targetUrl, req, {
|
|
631
|
+
...proxyOpt,
|
|
617
632
|
'hosts': lText.logicalOr(hosts, opt.hosts),
|
|
618
|
-
headers,
|
|
633
|
+
'headers': headers,
|
|
619
634
|
});
|
|
620
635
|
const stream = rres.getRawStream();
|
|
621
636
|
if (!stream) {
|
|
@@ -721,10 +736,12 @@ export async function rproxy(ctr, route, opt = {}) {
|
|
|
721
736
|
catch {
|
|
722
737
|
return false;
|
|
723
738
|
}
|
|
724
|
-
// --- 发起请求(opt.body 优先于原始 req
|
|
739
|
+
// --- 发起请求(opt.body 优先于原始 req 流;反代强制不自动跨源跟随重定向,防止上游 302 SSRF) ---
|
|
725
740
|
const rres = await request(proxyUrl, opt.body ?? req, {
|
|
726
741
|
...opt,
|
|
727
|
-
|
|
742
|
+
'method': (req.method ?? 'GET'),
|
|
743
|
+
'follow': 0,
|
|
744
|
+
'headers': headers,
|
|
728
745
|
});
|
|
729
746
|
const stream = rres.getRawStream();
|
|
730
747
|
if (!stream) {
|
|
@@ -777,4 +794,3 @@ export async function rproxy(ctr, route, opt = {}) {
|
|
|
777
794
|
}
|
|
778
795
|
return false;
|
|
779
796
|
}
|
|
780
|
-
/* eslint-enable */
|
package/lib/vector.d.ts
CHANGED
|
@@ -18,29 +18,43 @@ export interface IOptions {
|
|
|
18
18
|
/** --- 密码 --- */
|
|
19
19
|
'pwd'?: string;
|
|
20
20
|
}
|
|
21
|
+
/** --- 向量搜索参数 --- */
|
|
22
|
+
export interface ISearchOptions {
|
|
23
|
+
/** --- 表名 --- */
|
|
24
|
+
'collection': string;
|
|
25
|
+
/** --- 查询的向量 --- */
|
|
26
|
+
'data': number[];
|
|
27
|
+
/** --- 过滤器,如 word_count > 0 and book_id in [1, 2, 3] --- */
|
|
28
|
+
'filter'?: string;
|
|
29
|
+
/** --- 返回的结果数量,默认为 3 --- */
|
|
30
|
+
'limit'?: number;
|
|
31
|
+
/** --- 计算两个向量相似度的度量,默认 L2 --- */
|
|
32
|
+
'metric'?: 'L2' | 'IP' | 'COSINE';
|
|
33
|
+
/** --- 输出的字段,如 ['book_id', 'word_count'],默认全部 --- */
|
|
34
|
+
'fields'?: string[];
|
|
35
|
+
}
|
|
36
|
+
/** --- 向量搜索命中的固定字段 --- */
|
|
37
|
+
export interface ISearchHit {
|
|
38
|
+
'id': string | number;
|
|
39
|
+
'distance': number;
|
|
40
|
+
}
|
|
21
41
|
export declare class Vector {
|
|
22
42
|
/** --- 当前的 vector 连接信息 --- */
|
|
23
43
|
private readonly _etc;
|
|
24
44
|
constructor(etc: kebab.IConfigVector);
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
'fields'?: string[];
|
|
39
|
-
}): Promise<Array<{
|
|
40
|
-
'id': string | number;
|
|
41
|
-
'distance': number;
|
|
42
|
-
[key: string]: any;
|
|
43
|
-
}> | false>;
|
|
45
|
+
/**
|
|
46
|
+
* --- 搜索相近向量,允许动态访问输出字段 ---
|
|
47
|
+
* @param data 查询向量及搜索选项
|
|
48
|
+
* @returns 搜索命中列表,搜索失败时返回 false
|
|
49
|
+
*/
|
|
50
|
+
search(data: ISearchOptions): Promise<Array<ISearchHit & Record<string, any>> | false>;
|
|
51
|
+
/**
|
|
52
|
+
* --- 搜索相近向量,按指定类型返回额外字段 ---
|
|
53
|
+
* @typeParam TExtra 由调用方根据输出字段指定的结构,id 和 distance 保持固定类型
|
|
54
|
+
* @param data 查询向量及搜索选项
|
|
55
|
+
* @returns 搜索命中列表,搜索失败时返回 false
|
|
56
|
+
*/
|
|
57
|
+
search<TExtra extends object>(data: ISearchOptions): Promise<Array<ISearchHit & Omit<TExtra, keyof ISearchHit>> | false>;
|
|
44
58
|
/** --- 插入数据 --- */
|
|
45
59
|
insert(data: {
|
|
46
60
|
/** --- 表名 --- */
|
package/lib/vector.js
CHANGED
package/lib/ws.d.ts
CHANGED
|
@@ -229,7 +229,7 @@ export declare class Socket {
|
|
|
229
229
|
get isServer(): boolean;
|
|
230
230
|
/** --- 发送 ping --- */
|
|
231
231
|
ping(data?: Buffer | string): boolean;
|
|
232
|
-
/** --- 发送
|
|
232
|
+
/** --- 发送 pong --- */
|
|
233
233
|
pong(data?: Buffer | string): boolean;
|
|
234
234
|
/**
|
|
235
235
|
* --- 发送控制帧 ---
|