@maiyunnet/kebab 9.22.4 → 9.23.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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "9.22.4";
8
+ export declare const VER = "9.23.0";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  /** --- 框架的 LIB,以 / 结尾 --- */
@@ -156,10 +156,15 @@ export interface IConfigConst {
156
156
  'wechat': boolean;
157
157
  'miniprogram': '' | 'wechat';
158
158
  'https': boolean;
159
+ /** --- 原始主机地址,保留端口 --- */
159
160
  'host': string;
161
+ /** --- 主机名,不含端口或 IPv6 方括号 --- */
160
162
  'hostname': string;
163
+ /** --- 请求端口 --- */
161
164
  'hostport': number;
162
- /** --- 协议与 host,如 https://example.com:8443 --- */
165
+ /** --- 用于 URL 的主机地址,仅省略 HTTPS 的 443 和 HTTP 的 80 端口,保留 IPv6 方括号 --- */
166
+ 'hostCanonical': string;
167
+ /** --- 协议与 hostCanonical,如 https://example.com:8443 --- */
163
168
  'urlOrigin': string;
164
169
  'uri': IUrlParse;
165
170
  'rootPath': string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '9.22.4';
9
+ export const VER = '9.23.0';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/text.d.ts CHANGED
@@ -57,12 +57,12 @@ export declare const REGEXP_EMAIL: RegExp;
57
57
  */
58
58
  export declare function isEMail(email: string): boolean;
59
59
  /**
60
- * --- 是否是 IPv4(基于 Node 原生 net.isIP,严格校验) ---
60
+ * --- 是否是 IPv4(基于 Node 原生 net.isIPv4,严格校验) ---
61
61
  * @param ip
62
62
  */
63
63
  export declare function isIPv4(ip: string): boolean;
64
64
  /**
65
- * --- 是否是 IPv6(基于 Node 原生 net.isIP,严格校验) ---
65
+ * --- 是否是 IPv6(基于 Node 原生 net.isIPv6,严格校验) ---
66
66
  * @param ip
67
67
  */
68
68
  export declare function isIPv6(ip: string): boolean;
@@ -173,14 +173,16 @@ export declare function getFileNameExt(path: string): {
173
173
  */
174
174
  export declare function stringifyResult(rtn: kebab.Json): string;
175
175
  /**
176
- * --- 将字符串解析为对象,返回 false 代表解析失败,支持 BigInt ---
176
+ * --- 解析 JSON,超出安全整数范围的整数字面量转换为 BigInt ---
177
177
  * @param str 要解析的 json 字符串
178
+ * @returns 解析结果,无效 JSON 返回 false;小数及指数形式仍为 number
178
179
  */
179
180
  export declare function parseJson<T>(str: string): T | false;
180
181
  /**
181
- * --- 将对象转换为 json 字符串,返回 false 代表解析失败,支持 BigInt ---
182
+ * --- 将对象转换为 JSON 字符串,BigInt 输出为整数字面量 ---
182
183
  * @param obj 要转换的 json 对象
183
184
  * @param space 美化方式
185
+ * @returns JSON 字符串,循环引用等序列化错误沿用 JSON.stringify 异常
184
186
  */
185
187
  export declare function stringifyJson(obj: kebab.Json, space?: string | number): string;
186
188
  /**
package/lib/text.js CHANGED
@@ -4,6 +4,7 @@
4
4
  * Last: 2020-04-06 20:51:06, 2022-9-29 15:18:16, 2022-12-29 00:01:30, 2024-3-6 17:53:14, 2024-5-31 17:29:52, 2025-6-13 15:47:02, 2025-9-23 12:51:49, 2026-8-22
5
5
  */
6
6
  import * as net from 'net';
7
+ import * as textJson from '#kebab/abox/text.js';
7
8
  import * as kebab from '#kebab/index.js';
8
9
  import * as lFs from './fs.js';
9
10
  import * as lCore from './core.js';
@@ -275,18 +276,18 @@ export function isEMail(email) {
275
276
  return REGEXP_EMAIL.test(email);
276
277
  }
277
278
  /**
278
- * --- 是否是 IPv4(基于 Node 原生 net.isIP,严格校验) ---
279
+ * --- 是否是 IPv4(基于 Node 原生 net.isIPv4,严格校验) ---
279
280
  * @param ip
280
281
  */
281
282
  export function isIPv4(ip) {
282
- return net.isIP(ip) === 4;
283
+ return net.isIPv4(ip);
283
284
  }
284
285
  /**
285
- * --- 是否是 IPv6(基于 Node 原生 net.isIP,严格校验) ---
286
+ * --- 是否是 IPv6(基于 Node 原生 net.isIPv6,严格校验) ---
286
287
  * @param ip
287
288
  */
288
289
  export function isIPv6(ip) {
289
- return net.isIP(ip) === 6;
290
+ return net.isIPv6(ip);
290
291
  }
291
292
  export const REGEXP_DOMAIN = /^.+?\.((?![0-9]).)+$/i;
292
293
  /**
@@ -620,45 +621,21 @@ export function stringifyResult(rtn) {
620
621
  return JSON.stringify(rtn);
621
622
  }
622
623
  /**
623
- * --- 将字符串解析为对象,返回 false 代表解析失败,支持 BigInt ---
624
+ * --- 解析 JSON,超出安全整数范围的整数字面量转换为 BigInt ---
624
625
  * @param str 要解析的 json 字符串
626
+ * @returns 解析结果,无效 JSON 返回 false;小数及指数形式仍为 number
625
627
  */
626
628
  export function parseJson(str) {
627
- try {
628
- str = str.replace(/("[\w-]+?" *: *)([-+0-9]+)([ \r\n]*[,}]|$)/g, (v, v1, v2, v3) => {
629
- return v1 + '"-mybigint-' + v2 + '"' + v3;
630
- });
631
- return JSON.parse(str, (k, v) => {
632
- if (typeof v !== 'string') {
633
- return v;
634
- }
635
- if (!v.startsWith('-mybigint-')) {
636
- return v;
637
- }
638
- const ints = v.slice(10);
639
- const int = parseInt(ints);
640
- if ((int >= Number.MIN_SAFE_INTEGER) && (int <= Number.MAX_SAFE_INTEGER)) {
641
- return int;
642
- }
643
- return BigInt(ints);
644
- });
645
- }
646
- catch {
647
- return false;
648
- }
629
+ return textJson.parseJson(str);
649
630
  }
650
631
  /**
651
- * --- 将对象转换为 json 字符串,返回 false 代表解析失败,支持 BigInt ---
632
+ * --- 将对象转换为 JSON 字符串,BigInt 输出为整数字面量 ---
652
633
  * @param obj 要转换的 json 对象
653
634
  * @param space 美化方式
635
+ * @returns JSON 字符串,循环引用等序列化错误沿用 JSON.stringify 异常
654
636
  */
655
637
  export function stringifyJson(obj, space) {
656
- return JSON.stringify(obj, (k, v) => {
657
- if (typeof v === 'bigint') {
658
- return '-mybigint-' + v.toString();
659
- }
660
- return v;
661
- }, space).replace(/"-mybigint-([-+0-9]+?)"/g, '$1');
638
+ return textJson.stringifyJson(obj, space);
662
639
  }
663
640
  /**
664
641
  * --- 将未知异常转换为适合单行日志的文本,Error 优先返回完整堆栈 ---
@@ -31,7 +31,7 @@ export declare class Response {
31
31
  /**
32
32
  * --- 获取响应读取流对象 ---
33
33
  */
34
- getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | zlib.Gunzip | zlib.Inflate | zlib.BrotliDecompress | null;
34
+ getStream(): zlib.BrotliDecompress | zlib.Gunzip | zlib.Inflate | (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | null;
35
35
  /**
36
36
  * --- 获取原生响应读取流对象 ---
37
37
  */
package/lib/undici.d.ts CHANGED
@@ -40,7 +40,7 @@ export declare function postJson(u: string, data: kebab.Json[] | Record<string,
40
40
  * @param u 网址
41
41
  * @param data 数据
42
42
  * @param opt 选项
43
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
43
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
44
44
  */
45
45
  export declare function postJsonResponseJson(u: string, data: kebab.Json[] | Record<string, kebab.Json>, opt?: IRequestOptions): Promise<kebab.Json | null | false>;
46
46
  /**
@@ -48,14 +48,14 @@ export declare function postJsonResponseJson(u: string, data: kebab.Json[] | Rec
48
48
  * @param u 网址
49
49
  * @param data 数据
50
50
  * @param opt 选项
51
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
51
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
52
52
  */
53
53
  export declare function postResponseJson(u: string, data: Record<string, kebab.Json>, opt?: IRequestOptions): Promise<kebab.Json | null | false>;
54
54
  /**
55
55
  * --- 发起 GET 请求并解析 JSON 响应 ---
56
56
  * @param u 网址
57
57
  * @param opt 选项
58
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
58
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
59
59
  */
60
60
  export declare function getResponseJson(u: string, opt?: IRequestOptions): Promise<kebab.Json | null | false>;
61
61
  /**
@@ -135,9 +135,9 @@ export interface IRequestOptions {
135
135
  'timeout'?: number;
136
136
  /** --- 网络异常后的重试次数,默认 0;流式请求体不可重试,非幂等请求需由调用方保证安全 --- */
137
137
  'retry'?: number;
138
- /** --- JSON 解析失败后的重试次数,默认 0;仅适用于 ResponseJson 快捷方法,非幂等请求需由调用方保证安全 --- */
138
+ /** --- JSON 解析失败或重试验证未通过后的重试次数,默认 0;仅适用于 ResponseJson 快捷方法,非幂等请求需由调用方保证安全 --- */
139
139
  'retryJson'?: number;
140
- /** --- JSON 重试验证方法;返回 false 时按 retryJson 的设置重试,仅适用于 ResponseJson 快捷方法 --- */
140
+ /** --- JSON 重试验证方法;返回 false 时按 retryJson 的设置重试,耗尽后仍返回最后一次解析结果,业务结果由调用方判断;仅适用于 ResponseJson 快捷方法 --- */
141
141
  'retryJsonHandler'?: (json: kebab.Json) => boolean;
142
142
  /** --- 追踪 location 次数,0 为不追踪,默认为 0 --- */
143
143
  'follow'?: number;
package/lib/undici.js CHANGED
@@ -165,12 +165,12 @@ export async function postJson(u, data, opt = {}) {
165
165
  return request(u, data, opt);
166
166
  }
167
167
  /**
168
- * --- 发起请求并解析 JSON 响应,可在解析失败时重新请求 ---
168
+ * --- 发起请求并解析 JSON 响应,解析失败或重试验证未通过时重新请求 ---
169
169
  * @param u 网址
170
170
  * @param data 请求数据
171
171
  * @param opt 请求选项
172
172
  * @param action 日志动作名
173
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
173
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
174
174
  */
175
175
  async function requestResponseJson(u, data, opt, action) {
176
176
  const retryJson = normalizeRetry(opt.retryJson);
@@ -195,7 +195,8 @@ async function requestResponseJson(u, data, opt, action) {
195
195
  const reason = json ? 'retry json handler returned false' : 'parse json failed';
196
196
  lCore.log({}, `[UNDICI][${action}] ${reason}, url: ${u}${requestData}, content: ${rtnStr}`, '-neterror');
197
197
  }
198
- return false;
198
+ // --- 重试耗尽仍保留本次解析结果,交给调用方判断业务错误 ---
199
+ return json;
199
200
  }
200
201
  return false;
201
202
  }
@@ -204,7 +205,7 @@ async function requestResponseJson(u, data, opt, action) {
204
205
  * @param u 网址
205
206
  * @param data 数据
206
207
  * @param opt 选项
207
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
208
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
208
209
  */
209
210
  export async function postJsonResponseJson(u, data, opt = {}) {
210
211
  opt.method = 'POST';
@@ -216,7 +217,7 @@ export async function postJsonResponseJson(u, data, opt = {}) {
216
217
  * @param u 网址
217
218
  * @param data 数据
218
219
  * @param opt 选项
219
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
220
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
220
221
  */
221
222
  export async function postResponseJson(u, data, opt = {}) {
222
223
  opt.method = 'POST';
@@ -226,7 +227,7 @@ export async function postResponseJson(u, data, opt = {}) {
226
227
  * --- 发起 GET 请求并解析 JSON 响应 ---
227
228
  * @param u 网址
228
229
  * @param opt 选项
229
- * @returns JSON 数据;请求失败返回 null;JSON 解析失败返回 false
230
+ * @returns 最后一次响应的 JSON 数据,重试验证未通过也保留;请求或读取失败返回 null;JSON 解析失败返回 false
230
231
  */
231
232
  export async function getResponseJson(u, opt = {}) {
232
233
  return requestResponseJson(u, undefined, opt, 'GETRESPONSEJSON');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.22.4",
3
+ "version": "9.23.0",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -15,7 +15,8 @@
15
15
  "scripts": {
16
16
  "build-doc": "bash ./utils/build-doc.sh",
17
17
  "test:formdata": "node abox/test/formdata.mjs",
18
- "test:rpc": "node --test --test-isolation=none abox/test/rpc.mjs"
18
+ "test:rpc": "node --test --test-isolation=none abox/test/rpc.mjs",
19
+ "test:master": "node --test --test-isolation=none abox/test/master.mjs"
19
20
  },
20
21
  "bin": {
21
22
  "kebab": "./bin/kebab.js"