@maiyunnet/kebab 9.26.5 → 9.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * --- 本文件用来定义每个目录实体地址的常量 ---
5
5
  */
6
6
  /** --- 当前系统版本号 --- */
7
- export declare const VER = "9.26.5";
7
+ export declare const VER = "9.27.1";
8
8
  /** --- 框架根目录,以 / 结尾 --- */
9
9
  export declare const ROOT_PATH: string;
10
10
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export const VER = '9.26.5';
8
+ export const VER = '9.27.1';
9
9
  // --- 服务端用的路径 ---
10
10
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
11
11
  /** --- /xxx/xxx --- */
@@ -0,0 +1,61 @@
1
+ import type * as http from 'node:http';
2
+ import type * as kebab from '#kebab/index.js';
3
+ /** --- 经认证的 RPC 命令及请求体摘要 --- */
4
+ export interface IMessage {
5
+ 'action': string;
6
+ 'bodyHash': string;
7
+ [key: string]: kebab.Json;
8
+ }
9
+ /**
10
+ * --- 校验密钥:至少 32 个字符,去除首尾空白后拒绝占位值及短重复模式 ---
11
+ * @param secret 待校验密钥
12
+ * @returns 密钥是否合格
13
+ */
14
+ export declare const validSecret: (secret: unknown) => secret is string;
15
+ /**
16
+ * --- 计算内容的 SHA-256 ---
17
+ * @param data 请求体内容
18
+ * @returns 小写十六进制摘要
19
+ */
20
+ export declare const hash: (data: string | Buffer) => string;
21
+ /**
22
+ * --- 流式计算文件的 SHA-256 ---
23
+ * @param file 文件路径
24
+ * @returns 小写十六进制摘要;读取失败返回 false
25
+ */
26
+ export declare const hashFile: (file: string) => Promise<string | false>;
27
+ /**
28
+ * --- 加密认证命令、HTTP 方法和请求体摘要;自动生成时间与 nonce ---
29
+ * @param message 动作及参数
30
+ * @param secret RPC 密钥
31
+ * @param method HTTP 方法,默认 GET
32
+ * @param bodyHash 请求体的 SHA-256,默认空内容摘要
33
+ * @returns URL 中的命令;密钥不合格时返回不可认证标记
34
+ */
35
+ export declare const sign: (message: Record<string, unknown>, secret: string, method?: string, bodyHash?: string) => string;
36
+ /**
37
+ * --- 校验密钥、方法及距当前时间不超过 5 秒的命令,并登记未使用的 nonce ---
38
+ * @param req 请求,使用 URL、方法和 socket IP
39
+ * @param secret RPC 密钥
40
+ * @returns 认证命令;无效或已用 nonce、限速及缓存满时返回 false
41
+ */
42
+ export declare const authenticate: (req: http.IncomingMessage, secret: unknown) => IMessage | false;
43
+ /**
44
+ * --- 记录 socket IP 的失败次数 ---
45
+ * @param req 被拒绝的请求
46
+ * @returns 每个限速窗口仅首次失败返回 true,供审计
47
+ */
48
+ export declare const reject: (req: http.IncomingMessage) => boolean;
49
+ /**
50
+ * --- 按字节上限读取并校验请求体;上传的临时文件由调用方删除 ---
51
+ * @param req 请求流
52
+ * @param expected 已认证的 SHA-256 摘要
53
+ * @param directory 临时文件目录
54
+ * @param upload 是否将请求体写入临时文件
55
+ * @param limit 允许的最大字节数
56
+ * @returns 内存内容或临时文件;失败返回 false 并清理临时文件
57
+ */
58
+ export declare const readBody: (req: http.IncomingMessage, expected: string, directory: string, upload: boolean, limit: number) => Promise<{
59
+ 'body'?: Buffer;
60
+ 'file'?: string;
61
+ } | false>;
@@ -0,0 +1,51 @@
1
+ import * as implementation from '#kebab/abox/rpc.js';
2
+ /**
3
+ * --- 校验密钥:至少 32 个字符,去除首尾空白后拒绝占位值及短重复模式 ---
4
+ * @param secret 待校验密钥
5
+ * @returns 密钥是否合格
6
+ */
7
+ export const validSecret = implementation.validSecret;
8
+ /**
9
+ * --- 计算内容的 SHA-256 ---
10
+ * @param data 请求体内容
11
+ * @returns 小写十六进制摘要
12
+ */
13
+ export const hash = implementation.hash;
14
+ /**
15
+ * --- 流式计算文件的 SHA-256 ---
16
+ * @param file 文件路径
17
+ * @returns 小写十六进制摘要;读取失败返回 false
18
+ */
19
+ export const hashFile = implementation.hashFile;
20
+ /**
21
+ * --- 加密认证命令、HTTP 方法和请求体摘要;自动生成时间与 nonce ---
22
+ * @param message 动作及参数
23
+ * @param secret RPC 密钥
24
+ * @param method HTTP 方法,默认 GET
25
+ * @param bodyHash 请求体的 SHA-256,默认空内容摘要
26
+ * @returns URL 中的命令;密钥不合格时返回不可认证标记
27
+ */
28
+ export const sign = implementation.sign;
29
+ /**
30
+ * --- 校验密钥、方法及距当前时间不超过 5 秒的命令,并登记未使用的 nonce ---
31
+ * @param req 请求,使用 URL、方法和 socket IP
32
+ * @param secret RPC 密钥
33
+ * @returns 认证命令;无效或已用 nonce、限速及缓存满时返回 false
34
+ */
35
+ export const authenticate = implementation.authenticate;
36
+ /**
37
+ * --- 记录 socket IP 的失败次数 ---
38
+ * @param req 被拒绝的请求
39
+ * @returns 每个限速窗口仅首次失败返回 true,供审计
40
+ */
41
+ export const reject = implementation.reject;
42
+ /**
43
+ * --- 按字节上限读取并校验请求体;上传的临时文件由调用方删除 ---
44
+ * @param req 请求流
45
+ * @param expected 已认证的 SHA-256 摘要
46
+ * @param directory 临时文件目录
47
+ * @param upload 是否将请求体写入临时文件
48
+ * @param limit 允许的最大字节数
49
+ * @returns 内存内容或临时文件;失败返回 false 并清理临时文件
50
+ */
51
+ export const readBody = implementation.readBody;
package/lib/core.d.ts CHANGED
@@ -307,15 +307,14 @@ export interface ILogOptions {
307
307
  'hostname'?: string;
308
308
  'req'?: http2.Http2ServerRequest | http.IncomingMessage | null;
309
309
  'get'?: Record<string, any>;
310
- 'cookie'?: Record<string, string>;
311
- 'session'?: Record<string, any>;
312
310
  'headers'?: http.IncomingHttpHeaders;
313
311
  }
314
312
  /**
315
- * --- 写入文件日志 ---
313
+ * --- 写入文件日志,不采集 Cookie、Session 或请求体 ---
316
314
  * @param opt 选项
317
315
  * @param msg 自定义内容
318
316
  * @param fend 文件名追加
317
+ * @returns 无返回值
319
318
  */
320
319
  export declare function log(opt: sCtr.Ctr | ILogOptions, msg: string, fend?: string): void;
321
320
  /**
@@ -405,7 +404,7 @@ export declare function getMonitorFile(opt: {
405
404
  * @param opt 参数
406
405
  */
407
406
  export declare function ls(opt: {
408
- /** --- 如 2024/08/01/22,无所谓开头结尾是否有 /,不会逃逸出 cwd 路径 --- */
407
+ /** --- 相对运行根目录的路径,如 2024/08/01/22,可带首尾 /;拒绝越界的 .. 和软链接 --- */
409
408
  'path': string;
410
409
  'encoding'?: BufferEncoding;
411
410
  /** --- 获取局域网服务器的目录列表,为空代表获取本机的 --- */
package/lib/core.js CHANGED
@@ -21,6 +21,8 @@ import * as lCrypto from '#kebab/lib/crypto.js';
21
21
  import * as lNetResponse from '#kebab/lib/net/response.js';
22
22
  import * as lUndiciResponse from '#kebab/lib/undici/response.js';
23
23
  import * as sCtr from '#kebab/sys/ctr.js';
24
+ import * as rpc from '#kebab/lib/core/rpc.js';
25
+ import * as nodeFs from 'node:fs';
24
26
  /** --- Valibot 数据校验工具,业务侧可通过 lCore.v 使用 --- */
25
27
  export { valibot as v };
26
28
  /** --- 全局参数 --- */
@@ -640,10 +642,10 @@ export async function sendReload(hosts) {
640
642
  }
641
643
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
642
644
  const results = await Promise.all(hosts.map(async (host) => {
643
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
645
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
644
646
  'action': 'reload',
645
647
  'time': lTime.stamp()
646
- }), globalConfig.rpcSecret), {
648
+ }, globalConfig.rpcSecret), {
647
649
  'timeout': 5
648
650
  });
649
651
  const content = await res.getContent();
@@ -680,10 +682,10 @@ export async function sendRestart(hosts) {
680
682
  }
681
683
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
682
684
  const results = await Promise.all(hosts.map(async (host) => {
683
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
685
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
684
686
  'action': 'restart',
685
687
  'time': lTime.stamp()
686
- }), globalConfig.rpcSecret), {
688
+ }, globalConfig.rpcSecret), {
687
689
  'timeout': 5
688
690
  });
689
691
  const content = await res.getContent();
@@ -721,10 +723,10 @@ export async function sendStop(hosts) {
721
723
  }
722
724
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
723
725
  const results = await Promise.all(hosts.map(async (host) => {
724
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
726
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
725
727
  'action': 'stop',
726
728
  'time': lTime.stamp()
727
- }), globalConfig.rpcSecret), {
729
+ }, globalConfig.rpcSecret), {
728
730
  'timeout': 5
729
731
  });
730
732
  const content = await res.getContent();
@@ -753,12 +755,12 @@ export async function sendPm2(name, action = 'restart', hosts) {
753
755
  }
754
756
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
755
757
  const results = await Promise.all(hosts.map(async (host) => {
756
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
758
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
757
759
  'action': 'pm2',
758
760
  'time': lTime.stamp(),
759
761
  'name': name,
760
762
  'pm2Action': action
761
- }), globalConfig.rpcSecret), {
763
+ }, globalConfig.rpcSecret), {
762
764
  'timeout': 5
763
765
  });
764
766
  const content = await res.getContent();
@@ -788,11 +790,11 @@ export async function sendNpm(path, hosts) {
788
790
  }
789
791
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
790
792
  const results = await Promise.all(hosts.map(async (host) => {
791
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
793
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
792
794
  'action': 'npm',
793
795
  'time': lTime.stamp(),
794
796
  'path': path
795
- }), globalConfig.rpcSecret), {
797
+ }, globalConfig.rpcSecret), {
796
798
  'timeout': 330,
797
799
  });
798
800
  const content = await res.getContent();
@@ -832,12 +834,12 @@ export async function setGlobal(key, data, hosts) {
832
834
  }
833
835
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
834
836
  const results = await Promise.all(hosts.map(async (host) => {
835
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
837
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
836
838
  'action': 'global',
837
839
  'time': lTime.stamp(),
838
840
  'key': key,
839
841
  'data': data
840
- }), globalConfig.rpcSecret), {
842
+ }, globalConfig.rpcSecret), {
841
843
  'timeout': 5
842
844
  });
843
845
  const content = await res.getContent();
@@ -876,19 +878,14 @@ export async function updateCode(sourcePath, path, hosts, config = true, strict
876
878
  if (!hosts?.length) {
877
879
  hosts = ['127.0.0.1'];
878
880
  }
881
+ const bodyHash = await rpc.hashFile(sourcePath);
882
+ if (bodyHash === false) {
883
+ return {};
884
+ }
879
885
  /** --- 各主机使用独立上传流并行部署,结果保持输入顺序 --- */
880
886
  const results = await Promise.all(hosts.map(async (host) => {
881
- const fd = lUndici.getFormData();
882
- if (!await fd.putFile('file', sourcePath)) {
883
- return;
884
- }
885
- fd.putString('path', path);
886
- fd.putString('config', config);
887
- fd.putString('strict', strict ? '1' : '0');
888
- const res = await lUndici.post('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
889
- 'action': 'code',
890
- 'time': lTime.stamp()
891
- }), globalConfig.rpcSecret), fd, {
887
+ const command = rpc.sign({ 'action': 'code', path, config, strict: strict ? '1' : '0' }, globalConfig.rpcSecret, 'POST', bodyHash);
888
+ const res = await lUndici.post(`http://${host}:${globalConfig.rpcPort}/${command}`, nodeFs.createReadStream(sourcePath), {
892
889
  'timeout': 180,
893
890
  });
894
891
  const content = await res.getContent();
@@ -917,12 +914,12 @@ export async function sendProject(path, key, value, hosts) {
917
914
  }
918
915
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
919
916
  const results = await Promise.all(hosts.map(async (host) => {
920
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
917
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
921
918
  'action': 'project',
922
919
  'time': lTime.stamp(),
923
920
  'path': path,
924
921
  [key]: value
925
- }), globalConfig.rpcSecret), {
922
+ }, globalConfig.rpcSecret), {
926
923
  'timeout': 5
927
924
  });
928
925
  const content = await res.getContent();
@@ -952,11 +949,11 @@ export async function sendPackage(content, hosts) {
952
949
  }
953
950
  /** --- 并行发送,各主机独立等待,结果保持输入顺序 --- */
954
951
  const results = await Promise.all(hosts.map(async (host) => {
955
- const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
952
+ const res = await lUndici.get('http://' + host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
956
953
  'action': 'package',
957
954
  'time': lTime.stamp(),
958
955
  'content': content
959
- }), globalConfig.rpcSecret), {
956
+ }, globalConfig.rpcSecret), {
960
957
  'timeout': 5
961
958
  });
962
959
  const resContent = await res.getContent();
@@ -987,10 +984,9 @@ export async function sendVhost(files, hosts) {
987
984
  // --- 每个节点独立发送和返回,失败节点可单独重试 ---
988
985
  const results = await Promise.all(hosts.map(async (host) => {
989
986
  try {
990
- const res = await lUndici.postJson(`http://${host}:${globalConfig.rpcPort}/${lCrypto.aesEncrypt(lText.stringifyJson({
991
- 'action': 'vhost',
992
- 'time': lTime.stamp(),
993
- }), globalConfig.rpcSecret)}`, files, {
987
+ const body = lText.stringifyJson(files);
988
+ const command = rpc.sign({ 'action': 'vhost' }, globalConfig.rpcSecret, 'POST', rpc.hash(body));
989
+ const res = await lUndici.post(`http://${host}:${globalConfig.rpcPort}/${command}`, body, {
994
990
  'timeout': 30,
995
991
  });
996
992
  const content = await res.getContent();
@@ -1007,18 +1003,17 @@ export async function sendVhost(files, hosts) {
1007
1003
  return Object.fromEntries(results);
1008
1004
  }
1009
1005
  /**
1010
- * --- 写入文件日志 ---
1006
+ * --- 写入文件日志,不采集 Cookie、Session 或请求体 ---
1011
1007
  * @param opt 选项
1012
1008
  * @param msg 自定义内容
1013
1009
  * @param fend 文件名追加
1010
+ * @returns 无返回值
1014
1011
  */
1015
1012
  export function log(opt, msg, fend = '') {
1016
1013
  (async () => {
1017
1014
  let req;
1018
1015
  let headers;
1019
1016
  let get;
1020
- let cookie;
1021
- let session;
1022
1017
  let wpath;
1023
1018
  let urlFull;
1024
1019
  let hostname;
@@ -1026,8 +1021,6 @@ export function log(opt, msg, fend = '') {
1026
1021
  req = opt.getPrototype('_req');
1027
1022
  headers = opt.getPrototype('_headers');
1028
1023
  get = opt.getPrototype('_get');
1029
- cookie = opt.getPrototype('_cookie');
1030
- session = opt.getPrototype('_session');
1031
1024
  const config = opt.getPrototype('_config');
1032
1025
  wpath = config.const.path;
1033
1026
  urlFull = config.const.urlFull;
@@ -1037,8 +1030,6 @@ export function log(opt, msg, fend = '') {
1037
1030
  req = opt.req ?? null;
1038
1031
  headers = opt.headers ?? {};
1039
1032
  get = opt.get ?? {};
1040
- cookie = opt.cookie ?? {};
1041
- session = opt.session ?? {};
1042
1033
  wpath = opt.path ?? '';
1043
1034
  urlFull = opt.urlFull ?? '';
1044
1035
  hostname = opt.hostname ?? '';
@@ -1068,8 +1059,6 @@ export function log(opt, msg, fend = '') {
1068
1059
  'time': lTime.format(null, 'H:i:s', now),
1069
1060
  'unix': lTime.stamp(now),
1070
1061
  'url': urlFull + wpath + (Object.keys(get).length ? '?' + lText.queryStringify(get) : ''),
1071
- 'cookie': cookie,
1072
- 'session': session,
1073
1062
  'userAgent': headers['user-agent'] ?? '',
1074
1063
  'realIp': realIp,
1075
1064
  'cfIp': ipss.cf,
@@ -1085,10 +1074,9 @@ export function log(opt, msg, fend = '') {
1085
1074
  });
1086
1075
  }
1087
1076
  else {
1088
- // --- CSV 格式(默认) ---
1089
1077
  path += h + '.csv';
1090
1078
  if (!await lFs.isFile(path)) {
1091
- if (!await lFs.putContent(path, 'TIME,UNIX,URL,COOKIE,SESSION,USER_AGENT,REALIP,CFIP,XIP,OS,PROCESS,MESSAGE\n', {
1079
+ if (!await lFs.putContent(path, 'TIME,UNIX,URL,USER_AGENT,REALIP,CFIP,XIP,OS,PROCESS,MESSAGE\n', {
1092
1080
  'encoding': 'utf8',
1093
1081
  'mode': 0o777
1094
1082
  })) {
@@ -1099,8 +1087,6 @@ export function log(opt, msg, fend = '') {
1099
1087
  lText.csvescape(lTime.format(null, 'H:i:s', now)) + '","' +
1100
1088
  lText.csvescape(lTime.stamp(now).toString()) + '","' +
1101
1089
  lText.csvescape(urlFull + wpath + (Object.keys(get).length ? '?' + lText.queryStringify(get) : '')) + '","' +
1102
- lText.csvescape(lText.queryStringify(cookie)) + '","' +
1103
- lText.csvescape(lText.stringifyJson(session)) + '","' +
1104
1090
  lText.csvescape(headers['user-agent'] ?? 'No HTTP_USER_AGENT') + '","' +
1105
1091
  lText.csvescape(realIp) + '","' +
1106
1092
  lText.csvescape(ipss.cf) + '","' +
@@ -1125,7 +1111,7 @@ export async function getLog(opt) {
1125
1111
  opt.host ??= '127.0.0.1';
1126
1112
  // --- 局域网模式 ---
1127
1113
  const time = lTime.stamp();
1128
- const res = await lUndici.getResponseJson('http://' + opt.host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
1114
+ const res = await lUndici.getResponseJson('http://' + opt.host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
1129
1115
  'action': 'log',
1130
1116
  'time': time,
1131
1117
  'hostname': opt.hostname,
@@ -1134,7 +1120,7 @@ export async function getLog(opt) {
1134
1120
  'search': opt.search,
1135
1121
  'offset': opt.offset,
1136
1122
  'limit': opt.limit,
1137
- }), globalConfig.rpcSecret), {
1123
+ }, globalConfig.rpcSecret), {
1138
1124
  'timeout': 5
1139
1125
  });
1140
1126
  if (!res) {
@@ -1157,10 +1143,10 @@ async function requestMonitor(action, opt) {
1157
1143
  if (!Number.isInteger(globalConfig.rpcPort) || typeof globalConfig.rpcSecret !== 'string') {
1158
1144
  return false;
1159
1145
  }
1160
- const command = lCrypto.aesEncrypt(lText.stringifyJson({
1146
+ const command = rpc.sign({
1161
1147
  action, 'time': lTime.stamp(), 'path': opt.path, 'offset': opt.offset,
1162
1148
  'limit': opt.limit, 'preview': opt.preview,
1163
- }), globalConfig.rpcSecret);
1149
+ }, globalConfig.rpcSecret);
1164
1150
  const res = await lUndici.get(`http://${opt.host ?? '127.0.0.1'}:${globalConfig.rpcPort}/${command}`, {
1165
1151
  'timeout': action === 'monitor-file' ? 300 : 5,
1166
1152
  'follow': 0,
@@ -1236,12 +1222,12 @@ export async function ls(opt) {
1236
1222
  opt.host ??= '127.0.0.1';
1237
1223
  // --- 局域网模式 ---
1238
1224
  const time = lTime.stamp();
1239
- const res = await lUndici.get('http://' + opt.host + ':' + globalConfig.rpcPort.toString() + '/' + lCrypto.aesEncrypt(lText.stringifyJson({
1225
+ const res = await lUndici.get('http://' + opt.host + ':' + globalConfig.rpcPort.toString() + '/' + rpc.sign({
1240
1226
  'action': 'ls',
1241
1227
  'time': time,
1242
1228
  'path': opt.path,
1243
1229
  'encoding': opt.encoding,
1244
- }), globalConfig.rpcSecret), {
1230
+ }, globalConfig.rpcSecret), {
1245
1231
  'timeout': 5
1246
1232
  });
1247
1233
  const content = await res.getContent();
package/lib/sql.d.ts CHANGED
@@ -194,7 +194,8 @@ export declare class Sql {
194
194
  private _whereDataPosition;
195
195
  /**
196
196
  * --- 筛选器 ---
197
- * --- 标量相等:'city': 'bj', 'type': '2' ---
197
+ * --- 等值条件:'city': 'bj';字符串、数字、布尔值、bigint、Date 和 Buffer 均作为单个参数 ---
198
+ * --- null 生成 IS NULL,数组生成 IN 条件 ---
198
199
  * --- 运算符条件:['type', '>', '1'] ---
199
200
  * --- 集合条件:['type', 'in', ['1', '2']] 或 'type': ['1', '2'] ---
200
201
  * --- 逻辑分组:'$or': [{'city': 'bj'}, {'city': 'sh'}, [['age', '>', '10']]] ---
package/lib/sql.js CHANGED
@@ -504,7 +504,8 @@ export class Sql {
504
504
  _whereDataPosition = [0, 0];
505
505
  /**
506
506
  * --- 筛选器 ---
507
- * --- 标量相等:'city': 'bj', 'type': '2' ---
507
+ * --- 等值条件:'city': 'bj';字符串、数字、布尔值、bigint、Date 和 Buffer 均作为单个参数 ---
508
+ * --- null 生成 IS NULL,数组生成 IN 条件 ---
508
509
  * --- 运算符条件:['type', '>', '1'] ---
509
510
  * --- 集合条件:['type', 'in', ['1', '2']] 或 'type': ['1', '2'] ---
510
511
  * --- 逻辑分组:'$or': [{'city': 'bj'}, {'city': 'sh'}, [['age', '>', '10']]] ---
@@ -700,11 +701,6 @@ export class Sql {
700
701
  // --- NULL 判断 ---
701
702
  sql += this.field(k) + ' IS NULL AND ';
702
703
  }
703
- else if (typeof v === 'string' || typeof v === 'number') {
704
- // --- 标量相等 ---
705
- sql += this.field(k) + ' = ' + this._placeholder() + ' AND ';
706
- data.push(v);
707
- }
708
704
  else if (lSqlValue.isJson(v)) {
709
705
  // --- JSON 相等 ---
710
706
  const result = this._processValue(v);
@@ -715,7 +711,7 @@ export class Sql {
715
711
  // --- 字段相等 ---
716
712
  sql += this.field(k) + ' = ' + this.field(v.value) + ' AND ';
717
713
  }
718
- else {
714
+ else if (Array.isArray(v)) {
719
715
  // --- IN 查询,如 'type': ['1', '2'] ---
720
716
  if (v.length > 0) {
721
717
  sql += this.field(k) + ' IN (';
@@ -739,6 +735,11 @@ export class Sql {
739
735
  sql += this.field(k) + ' IN (NULL) AND ';
740
736
  }
741
737
  }
738
+ else {
739
+ // --- 普通值使用等号比较,原值交给数据库驱动处理 ---
740
+ sql += this.field(k) + ' = ' + this._placeholder() + ' AND ';
741
+ data.push(v);
742
+ }
742
743
  }
743
744
  }
744
745
  }
package/lib/text.d.ts CHANGED
@@ -42,12 +42,14 @@ export declare function isSameOrigin(from: string, to: string): boolean;
42
42
  * --- 将相对路径根据基准路径进行转换 ---
43
43
  * @param from 基准路径
44
44
  * @param to 相对路径
45
- * @param limit 是否限定结果不能逃逸出基准路径
45
+ * @param limit 是否将基准路径视作目录并限定结果不能逃逸,默认 false
46
+ * @returns 转换后的地址,超出限定目录时返回基准目录
46
47
  */
47
48
  export declare function urlResolve(from: string, to: string, limit?: boolean): string;
48
49
  /**
49
50
  * --- 将路径中的 ../ ./ 都按规范妥善处理 ---
50
- * @param url 要处理的地址
51
+ * @param url 要处理的地址;相对路径保留无法抵消的 .. 段
52
+ * @returns 规范化后的地址,查询和片段保持不变
51
53
  */
52
54
  export declare function urlAtom(url: string): string;
53
55
  export declare const REGEXP_EMAIL: RegExp;
@@ -133,7 +135,8 @@ export declare function queryStringify(query: Record<string, any>, options: {
133
135
  }): string;
134
136
  /**
135
137
  * --- 将 query string 转换为对象 ---
136
- * @param query 要转换的字符串
138
+ * @param query 要转换的字符串,+ 表示空格,%2B 表示加号
139
+ * @returns 查询参数对象,重复键对应字符串数组
137
140
  */
138
141
  export declare function queryParse(query: string): Record<string, string | string[]>;
139
142
  /**
package/lib/text.js CHANGED
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import * as net from 'net';
7
7
  import * as textImpl from '#kebab/abox/text.js';
8
+ import * as textPath from '#kebab/abox/text-path.js';
8
9
  import * as kebab from '#kebab/index.js';
9
10
  import * as lFs from './fs.js';
10
11
  import * as lCore from './core.js';
@@ -173,13 +174,23 @@ export function isSameOrigin(from, to) {
173
174
  (fromUri.hostname === toUri.hostname) &&
174
175
  (getPort(fromUri) === getPort(toUri)));
175
176
  }
176
- /** --- 限定结果不能逃逸出基准路径 --- */
177
+ /**
178
+ * --- 限定结果不能逃逸出基准路径;HTTP 地址按浏览器路径规则比较 ---
179
+ * @param from 基准路径
180
+ * @param limit 是否启用限制
181
+ * @param rtn 已解析的目标地址
182
+ * @returns 限制后的目标地址或基准目录
183
+ */
177
184
  function urlResolveLimit(from, limit, rtn) {
178
185
  if (!limit) {
179
186
  return rtn;
180
187
  }
181
- const base = urlAtom(from).replace(/\/+$/, '') + '/';
182
- if ((rtn === base.slice(0, -1)) || rtn.startsWith(base)) {
188
+ const base = urlAtom(from).split(/[?#]/, 1)[0].replace(/\/+$/, '') + '/';
189
+ const path = rtn.split(/[?#]/, 1)[0];
190
+ if (/^https?:\/\//i.test(base)) {
191
+ return textPath.urlWithinHttpBase(base, rtn) ? rtn : base;
192
+ }
193
+ if ((path === base.slice(0, -1)) || path.startsWith(base)) {
183
194
  return rtn;
184
195
  }
185
196
  return base;
@@ -188,7 +199,8 @@ function urlResolveLimit(from, limit, rtn) {
188
199
  * --- 将相对路径根据基准路径进行转换 ---
189
200
  * @param from 基准路径
190
201
  * @param to 相对路径
191
- * @param limit 是否限定结果不能逃逸出基准路径
202
+ * @param limit 是否将基准路径视作目录并限定结果不能逃逸,默认 false
203
+ * @returns 转换后的地址,超出限定目录时返回基准目录
192
204
  */
193
205
  export function urlResolve(from, to, limit = false) {
194
206
  from = from.replace(/\\/g, '/');
@@ -196,6 +208,9 @@ export function urlResolve(from, to, limit = false) {
196
208
  if (limit) {
197
209
  // --- 限定模式下,开头的 / 代表从 from 内部开始,而不是系统根目录 ---
198
210
  to = to.replace(/^\/+/, '');
211
+ if (to && !to.startsWith('?') && !to.startsWith('#')) {
212
+ from = from.replace(/[?#].*$/, '').replace(/\/+$/, '') + '/';
213
+ }
199
214
  }
200
215
  // --- to 为空,直接返回 form ---
201
216
  if (to === '') {
@@ -220,7 +235,7 @@ export function urlResolve(from, to, limit = false) {
220
235
  }
221
236
  // --- # 或 ? 替换后返回 ---
222
237
  if (to.startsWith('#') || to.startsWith('?')) {
223
- const sp = from.indexOf(to[0]);
238
+ const sp = to.startsWith('?') ? from.search(/[?#]/) : from.indexOf('#');
224
239
  if (sp !== -1) {
225
240
  return urlAtom(from.slice(0, sp) + to);
226
241
  }
@@ -253,19 +268,11 @@ export function urlResolve(from, to, limit = false) {
253
268
  }
254
269
  /**
255
270
  * --- 将路径中的 ../ ./ 都按规范妥善处理 ---
256
- * @param url 要处理的地址
271
+ * @param url 要处理的地址;相对路径保留无法抵消的 .. 段
272
+ * @returns 规范化后的地址,查询和片段保持不变
257
273
  */
258
274
  export function urlAtom(url) {
259
- // --- 删掉 ./ ---
260
- while (url.includes('/./')) {
261
- url = url.replace(/\/\.\//g, '/');
262
- }
263
- // --- 删掉 ../ ---
264
- while (/\/(?!\.\.)[^/]+\/\.\.\//.test(url)) {
265
- url = url.replace(/\/(?!\.\.)[^/]+\/\.\.\//g, '/');
266
- }
267
- url = url.replace(/\.\.\//g, '');
268
- return url;
275
+ return textPath.urlAtom(url);
269
276
  }
270
277
  export const REGEXP_EMAIL = /^[-_\w.]+@[-_\w.]+\.([a-zA-Z]+)$/i;
271
278
  /**
@@ -452,53 +459,11 @@ export function queryStringify(query, encode = true) {
452
459
  }
453
460
  /**
454
461
  * --- 将 query string 转换为对象 ---
455
- * @param query 要转换的字符串
462
+ * @param query 要转换的字符串,+ 表示空格,%2B 表示加号
463
+ * @returns 查询参数对象,重复键对应字符串数组
456
464
  */
457
465
  export function queryParse(query) {
458
- const ret = {};
459
- const arrayKeys = {};
460
- const arr = query.split('&');
461
- for (const i of arr) {
462
- if (!i.length) {
463
- continue;
464
- }
465
- const pos = i.indexOf('=');
466
- let key;
467
- try {
468
- key = decodeURIComponent(pos === -1 ? i : i.slice(0, pos));
469
- }
470
- catch {
471
- key = pos === -1 ? i : i.slice(0, pos);
472
- }
473
- let value = '';
474
- try {
475
- value = pos === -1 ? '' : decodeURIComponent(i.slice(pos + 1));
476
- }
477
- catch {
478
- value = pos === -1 ? '' : i.slice(pos + 1);
479
- }
480
- if (Object.hasOwn(arrayKeys, key)) {
481
- ret[key].push(value);
482
- }
483
- else if (!Object.hasOwn(ret, key)) {
484
- Object.defineProperty(ret, key, {
485
- 'configurable': true,
486
- 'enumerable': true,
487
- 'value': value,
488
- 'writable': true,
489
- });
490
- }
491
- else {
492
- ret[key] = [ret[key], value];
493
- Object.defineProperty(arrayKeys, key, {
494
- 'configurable': true,
495
- 'enumerable': true,
496
- 'value': true,
497
- 'writable': true,
498
- });
499
- }
500
- }
501
- return ret;
466
+ return textPath.queryParse(query);
502
467
  }
503
468
  /**
504
469
  * --- HTML 特殊字符转换为实体字符 ---