@maiyunnet/kebab 9.25.1 → 9.25.2

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/text.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export declare function redact(value: string, secrets: readonly string[], replacement: string, encoded: boolean): string;
1
2
  export declare function parseJson<T>(str: string): T | false;
2
3
  export declare function stringifyJson(obj: unknown, space?: string | number): string;
package/abox/text.js CHANGED
@@ -1,5 +1,41 @@
1
1
  // TypeScript's bundled JSON declarations do not yet include these Node 22 APIs.
2
2
  const nativeJson = JSON;
3
+ export function redact(value, secrets, replacement, encoded) {
4
+ if (!value || !secrets.length)
5
+ return value;
6
+ const patterns = new Map();
7
+ const add = (text, uri = false) => {
8
+ if (text.length > value.length)
9
+ return;
10
+ let pattern = text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
11
+ if (uri) {
12
+ pattern = pattern.replace(/%[\dA-F]{2}/g, part => part.replace(/[A-F]/g, hex => `[${hex}${hex.toLowerCase()}]`));
13
+ }
14
+ patterns.set(pattern, text.length);
15
+ };
16
+ for (const secret of new Set(secrets)) {
17
+ if (!secret || secret.length > value.length)
18
+ continue;
19
+ add(secret);
20
+ if (!encoded)
21
+ continue;
22
+ const json = JSON.stringify(secret).slice(1, -1);
23
+ if (json !== secret)
24
+ add(json);
25
+ try {
26
+ const uri = encodeURIComponent(secret);
27
+ if (uri !== secret)
28
+ add(uri, true);
29
+ }
30
+ catch {
31
+ // A lone surrogate has no URI encoding; raw and JSON forms remain usable.
32
+ }
33
+ }
34
+ if (!patterns.size)
35
+ return value;
36
+ const pattern = [...patterns].sort((a, b) => b[1] - a[1]).map(([text]) => text).join('|');
37
+ return value.replace(new RegExp(pattern, 'g'), () => replacement);
38
+ }
3
39
  export function parseJson(str) {
4
40
  try {
5
41
  return nativeJson.parse(str, (_key, value, context) => {
package/doc/kebab-rag.md CHANGED
@@ -1759,7 +1759,7 @@ index/variables/VER.md
1759
1759
 
1760
1760
  # Variable: VER
1761
1761
 
1762
- > `const` **VER**: `"9.25.1"` = `'9.25.1'`
1762
+ > `const` **VER**: `"9.25.2"` = `'9.25.2'`
1763
1763
 
1764
1764
  Defined in: [index.ts:10](https://github.com/maiyunnet/kebab/blob/master/index.ts#L10)
1765
1765
 
@@ -12690,7 +12690,7 @@ The `expects` values.
12690
12690
 
12691
12691
  ### separator
12692
12692
 
12693
- `"&"` \| "\|"
12693
+ "\|" \| `"&"`
12694
12694
 
12695
12695
  The separator.
12696
12696
 
@@ -127489,6 +127489,61 @@ Defined in: [lib/text.ts:476](https://github.com/maiyunnet/kebab/blob/master/lib
127489
127489
 
127490
127490
  `string`
127491
127491
 
127492
+ lib/text/functions/redact.md
127493
+ ---
127494
+
127495
+ [**Documents for @maiyunnet/kebab**](../../../index.md)
127496
+
127497
+ ***
127498
+
127499
+ [Documents for @maiyunnet/kebab](../../../index.md) / [lib/text](../index.md) / redact
127500
+
127501
+ # Function: redact()
127502
+
127503
+ > **redact**(`value`, `secrets`, `options?`): `string`
127504
+
127505
+ Defined in: [lib/text.ts:878](https://github.com/maiyunnet/kebab/blob/master/lib/text.ts#L878)
127506
+
127507
+ 将文本中的指定敏感字符串替换为占位文本
127508
+
127509
+ ## Parameters
127510
+
127511
+ ### value
127512
+
127513
+ `string`
127514
+
127515
+ 待处理文本
127516
+
127517
+ ### secrets
127518
+
127519
+ readonly `string`[]
127520
+
127521
+ 敏感字符串,忽略空项和重复项,不修改数组
127522
+
127523
+ ### options?
127524
+
127525
+ 替换选项
127526
+
127527
+ #### encoded?
127528
+
127529
+ `boolean`
127530
+
127531
+ 是否同时匹配编码形式,默认 true;包括 encodeURIComponent 生成形式
127532
+ (百分号十六进制不区分大小写)和 JSON.stringify 生成的转义正文,不含外层引号;
127533
+ 孤立代理项跳过 URI 编码,仍匹配原文及 JSON 转义形式
127534
+
127535
+ #### replacement?
127536
+
127537
+ `string`
127538
+
127539
+ 替换文本,默认 [REDACTED],按字面量插入且不参与后续匹配
127540
+
127541
+ ## Returns
127542
+
127543
+ `string`
127544
+
127545
+ 处理后的文本;原文区分大小写,从左向右匹配,同一位置优先最长串,匹配范围不重叠
127546
+
127492
127547
  lib/text/functions/sizeFormat.md
127493
127548
  ---
127494
127549
 
@@ -127842,6 +127897,7 @@ lib/text/index.md
127842
127897
  - [parseVersion](functions/parseVersion.md)
127843
127898
  - [queryParse](functions/queryParse.md)
127844
127899
  - [queryStringify](functions/queryStringify.md)
127900
+ - [redact](functions/redact.md)
127845
127901
  - [sizeFormat](functions/sizeFormat.md)
127846
127902
  - [str2int](functions/str2int.md)
127847
127903
  - [stringifyBuffer](functions/stringifyBuffer.md)
package/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "9.25.1";
8
+ export declare const VER = "9.25.2";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '9.25.1';
9
+ export const VER = '9.25.2';
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
@@ -241,3 +241,18 @@ export declare function str2int(str: string, digits?: number): number;
241
241
  * @param decimal 最终保留的小数位数
242
242
  */
243
243
  export declare function int2str(int: number, digits?: number, decimal?: number): string;
244
+ /**
245
+ * --- 将文本中的指定敏感字符串替换为占位文本 ---
246
+ * @param value 待处理文本
247
+ * @param secrets 敏感字符串,忽略空项和重复项,不修改数组
248
+ * @param options 替换选项
249
+ * @param options.replacement 替换文本,默认 [REDACTED],按字面量插入且不参与后续匹配
250
+ * @param options.encoded 是否同时匹配编码形式,默认 true;包括 encodeURIComponent 生成形式
251
+ * (百分号十六进制不区分大小写)和 JSON.stringify 生成的转义正文,不含外层引号;
252
+ * 孤立代理项跳过 URI 编码,仍匹配原文及 JSON 转义形式
253
+ * @returns 处理后的文本;原文区分大小写,从左向右匹配,同一位置优先最长串,匹配范围不重叠
254
+ */
255
+ export declare function redact(value: string, secrets: readonly string[], options?: {
256
+ 'replacement'?: string;
257
+ 'encoded'?: boolean;
258
+ }): string;
package/lib/text.js CHANGED
@@ -4,7 +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
+ import * as textImpl from '#kebab/abox/text.js';
8
8
  import * as kebab from '#kebab/index.js';
9
9
  import * as lFs from './fs.js';
10
10
  import * as lCore from './core.js';
@@ -626,7 +626,7 @@ export function stringifyResult(rtn) {
626
626
  * @returns 解析结果,无效 JSON 返回 false;小数及指数形式仍为 number
627
627
  */
628
628
  export function parseJson(str) {
629
- return textJson.parseJson(str);
629
+ return textImpl.parseJson(str);
630
630
  }
631
631
  /**
632
632
  * --- 将对象转换为 JSON 字符串,BigInt 输出为整数字面量 ---
@@ -635,7 +635,7 @@ export function parseJson(str) {
635
635
  * @returns JSON 字符串,循环引用等序列化错误沿用 JSON.stringify 异常
636
636
  */
637
637
  export function stringifyJson(obj, space) {
638
- return textJson.stringifyJson(obj, space);
638
+ return textImpl.stringifyJson(obj, space);
639
639
  }
640
640
  /**
641
641
  * --- 将未知异常转换为适合单行日志的文本,Error 优先返回完整堆栈 ---
@@ -773,3 +773,17 @@ export function int2str(int, digits = 4, decimal = 2) {
773
773
  const decResult = newDecPart.toString().padStart(decimal, '0');
774
774
  return `${sign}${intNum}${decResult ? `.${decResult}` : ''}`;
775
775
  }
776
+ /**
777
+ * --- 将文本中的指定敏感字符串替换为占位文本 ---
778
+ * @param value 待处理文本
779
+ * @param secrets 敏感字符串,忽略空项和重复项,不修改数组
780
+ * @param options 替换选项
781
+ * @param options.replacement 替换文本,默认 [REDACTED],按字面量插入且不参与后续匹配
782
+ * @param options.encoded 是否同时匹配编码形式,默认 true;包括 encodeURIComponent 生成形式
783
+ * (百分号十六进制不区分大小写)和 JSON.stringify 生成的转义正文,不含外层引号;
784
+ * 孤立代理项跳过 URI 编码,仍匹配原文及 JSON 转义形式
785
+ * @returns 处理后的文本;原文区分大小写,从左向右匹配,同一位置优先最长串,匹配范围不重叠
786
+ */
787
+ export function redact(value, secrets, options = {}) {
788
+ return textImpl.redact(value, secrets, options.replacement ?? '[REDACTED]', options.encoded ?? true);
789
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.25.1",
3
+ "version": "9.25.2",
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": [