@napuketto/kernel 0.15.4 → 0.16.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/dist/index.mjs CHANGED
@@ -713,6 +713,52 @@ function createConsoleFormatStream(opts) {
713
713
  out.write(render(parsed, ctx));
714
714
  } };
715
715
  }
716
+ // src/infra/log-redaction.ts
717
+ /** 结构化字段名策略;统一小写匹配,新增凭据字段只需扩展此集合。 */
718
+ const SENSITIVE_FIELDS = /* @__PURE__ */ new Set([
719
+ "token",
720
+ "access_token",
721
+ "authorization",
722
+ "cookie",
723
+ "cookies",
724
+ "set-cookie",
725
+ "password",
726
+ "secret",
727
+ "ticket",
728
+ "clientkey",
729
+ "skey",
730
+ "p_skey",
731
+ "csrf_token",
732
+ "bkn"
733
+ ]);
734
+ /** 只遍历刚解析的 JSON 树;不会触碰业务对象、getter、toJSON 或循环引用。 */
735
+ function redactFields(record) {
736
+ const pending = [record];
737
+ while (pending.length > 0) {
738
+ const value = pending.pop();
739
+ if (typeof value !== "object" || value === null) continue;
740
+ for (const [key, child] of Object.entries(value)) if (SENSITIVE_FIELDS.has(key.toLowerCase())) Object.defineProperty(value, key, {
741
+ value: "[Redacted]",
742
+ enumerable: true,
743
+ writable: true,
744
+ configurable: true
745
+ });
746
+ else if (typeof child === "object" && child !== null) pending.push(child);
747
+ }
748
+ }
749
+ /**
750
+ * pino streamWrite 钩子接收完整 JSON 行;在所有目的地之前保护最终序列化结构。
751
+ * 不解析字符串中的 JSON / URL / 自由文本。失败时不回退输出原始记录。
752
+ */
753
+ function redactLogLine(chunk) {
754
+ try {
755
+ const record = JSON.parse(chunk);
756
+ redactFields(record);
757
+ return `${JSON.stringify(record)}\n`;
758
+ } catch {
759
+ throw kernelError("日志结构化脱敏失败", "INVALID_STATE");
760
+ }
761
+ }
716
762
  // src/infra/logger.ts
717
763
  /**
718
764
  * pino 日志封装(ADR-007)
@@ -727,14 +773,6 @@ function createConsoleFormatStream(opts) {
727
773
  * Date(实测记录时间冻结为假值、回调外恢复),故时间戳统一取
728
774
  * performance.timeOrigin + performance.now() 单调派生墙钟,不受 Date 篡改影响。
729
775
  */
730
- /** 默认 redact 路径:顶层 token/票据 + 任意深度的密码/密钥。 */
731
- const DEFAULT_REDACT_PATHS = [
732
- "token",
733
- "ticket",
734
- "cookie",
735
- "password",
736
- "secret"
737
- ];
738
776
  /** 级别数值序(求多目的地最低级别用)。 */
739
777
  const LEVEL_ORDER = [
740
778
  "trace",
@@ -812,7 +850,9 @@ function createLogger(opts = {}) {
812
850
  if (targets.length === 0) throw kernelError("logger 至少需要一个输出目标:console 或 file", "INVALID_PARAM");
813
851
  const pinoOptions = {
814
852
  level: gateLevel(targets),
815
- redact: [...DEFAULT_REDACT_PATHS, ...opts.redact ?? []],
853
+ redact: opts.redact ?? [],
854
+ serializers: { err: pino.stdSerializers.errWithCause },
855
+ hooks: { streamWrite: redactLogLine },
816
856
  timestamp: () => `,"time":${wallMs()}`,
817
857
  ...opts.base === void 0 ? {} : { base: {
818
858
  pid: process.pid,
@@ -1345,6 +1385,13 @@ function toDoubtFriendRequestInfo(item, uinMap) {
1345
1385
  *
1346
1386
  * ⚠️ 部分字段按公开资料(wrapper 外部契约)自研描述,待下一次进程内
1347
1387
  * 探测(probe.ts)校准——接口签名是外部系统事实,自研描述其形状。
1388
+ *
1389
+ * ⚠️ 跨平台空值契约(2026-11 Linux 3.2.x 线上事故):Windows 原生对象省略
1390
+ * 未命中的变体字段(`undefined`),Linux 原生对象则把未命中的变体字段**显式
1391
+ * 置 `null`**(纯文本元素的 `picElement: null` / `grayTipElement: null` 等),
1392
+ * 元素子字段同样可能是 null。因此本文件所有可选字段都声明为 `T | null`
1393
+ * (而非仅 `T`):读取方必须用 `== null` / `!= null`(同时覆盖 null 与
1394
+ * undefined),禁止只判 `=== undefined`。这样编译器会持续拦截新的漏网读取。
1348
1395
  */
1349
1396
  /** 会话类型(QQ wrapper 外部契约)。 */
1350
1397
  const ChatType = {
@@ -1475,18 +1522,18 @@ const AtType = {
1475
1522
  * 直接 `.get(0)` 抛 TypeError,整条视频消息事件分派失败。
1476
1523
  */
1477
1524
  function readVideoThumbPath(thumb) {
1478
- if (thumb === void 0 || thumb === "") return;
1525
+ if (thumb == null || thumb === "") return;
1479
1526
  if (typeof thumb === "string") return thumb;
1480
1527
  if (thumb instanceof Map) {
1481
1528
  const value = thumb.get(0);
1482
- return value === "" ? void 0 : value;
1529
+ return value == null || value === "" ? void 0 : value;
1483
1530
  }
1484
1531
  const value = thumb[String(0)];
1485
- return value === "" ? void 0 : value;
1532
+ return value == null || value === "" ? void 0 : value;
1486
1533
  }
1487
1534
  /** TEXT 元素 → canonical(at 或纯文本)。 */
1488
1535
  function textToCanonical(textEl) {
1489
- if (textEl === void 0) return {
1536
+ if (textEl == null) return {
1490
1537
  type: "text",
1491
1538
  text: ""
1492
1539
  };
@@ -1512,9 +1559,15 @@ function textToCanonical(textEl) {
1512
1559
  text: content
1513
1560
  };
1514
1561
  }
1515
- /** 空串视为缺失(Linux 原生对象常给 "" 而不是省略字段)。 */
1562
+ /**
1563
+ * 空值视为缺失(跨平台原生对象形态差异):
1564
+ * - Windows 省略未命中的字段 → undefined;
1565
+ * - Linux 3.2.x 显式置 null;
1566
+ * - Linux 原生对象也常给 "" 而不是省略字段。
1567
+ * 三者统一归为 undefined,避免 null 被当成有效值继续下传。
1568
+ */
1516
1569
  function emptyToUndefined(value) {
1517
- return value === void 0 || value === "" ? void 0 : value;
1570
+ return value == null || value === "" ? void 0 : value;
1518
1571
  }
1519
1572
  /** 取第一个非空字符串(跨平台字段名不一致时的兜底链)。 */
1520
1573
  function firstNonEmpty(...values) {
@@ -1537,7 +1590,7 @@ function isLocalPathCandidate(value) {
1537
1590
  * pic-path-grace(桥层等待落盘)与 picToCanonical 共用同一候选序。
1538
1591
  */
1539
1592
  function picLocalCandidates(pic) {
1540
- if (pic === void 0) return [];
1593
+ if (pic == null) return [];
1541
1594
  const seen = /* @__PURE__ */ new Set();
1542
1595
  const out = [];
1543
1596
  for (const raw of [pic.picPath, pic.sourcePath]) {
@@ -1551,7 +1604,7 @@ function picLocalCandidates(pic) {
1551
1604
  }
1552
1605
  /** PIC 元素 → canonical image。 */
1553
1606
  function picToCanonical(pic) {
1554
- if (pic === void 0) return {
1607
+ if (pic == null) return {
1555
1608
  type: "image",
1556
1609
  path: ""
1557
1610
  };
@@ -1570,7 +1623,7 @@ function picToCanonical(pic) {
1570
1623
  }
1571
1624
  /** FACE 元素 → canonical face。 */
1572
1625
  function faceToCanonical(face) {
1573
- if (face === void 0) return {
1626
+ if (face == null) return {
1574
1627
  type: "face",
1575
1628
  id: "0"
1576
1629
  };
@@ -1581,7 +1634,7 @@ function faceToCanonical(face) {
1581
1634
  }
1582
1635
  /** PTT 元素 → canonical voice。 */
1583
1636
  function pttToCanonical(ptt) {
1584
- if (ptt === void 0) return {
1637
+ if (ptt == null) return {
1585
1638
  type: "voice",
1586
1639
  path: ""
1587
1640
  };
@@ -1592,7 +1645,7 @@ function pttToCanonical(ptt) {
1592
1645
  }
1593
1646
  /** VIDEO 元素 → canonical video。 */
1594
1647
  function videoToCanonical(video) {
1595
- if (video === void 0) return {
1648
+ if (video == null) return {
1596
1649
  type: "video",
1597
1650
  path: ""
1598
1651
  };
@@ -1600,23 +1653,23 @@ function videoToCanonical(video) {
1600
1653
  type: "video",
1601
1654
  path: video.filePath ?? video.videoUrl ?? ""
1602
1655
  };
1603
- if (video.videoUrl !== void 0) base.url = video.videoUrl;
1656
+ if (video.videoUrl != null) base.url = video.videoUrl;
1604
1657
  const thumb = readVideoThumbPath(video.thumbPath);
1605
1658
  if (thumb !== void 0) base.thumbPath = thumb;
1606
- if (video.thumbWidth !== void 0 && video.thumbWidth > 0) base.width = video.thumbWidth;
1607
- if (video.thumbHeight !== void 0 && video.thumbHeight > 0) base.height = video.thumbHeight;
1608
- if (video.fileTime !== void 0 && video.fileTime > 0) base.durationSec = video.fileTime;
1659
+ if (video.thumbWidth != null && video.thumbWidth > 0) base.width = video.thumbWidth;
1660
+ if (video.thumbHeight != null && video.thumbHeight > 0) base.height = video.thumbHeight;
1661
+ if (video.fileTime != null && video.fileTime > 0) base.durationSec = video.fileTime;
1609
1662
  return base;
1610
1663
  }
1611
1664
  /** FILE 元素 → canonical file。 */
1612
1665
  function fileToCanonical(file) {
1613
- if (file === void 0) return {
1666
+ if (file == null) return {
1614
1667
  type: "file",
1615
1668
  path: ""
1616
1669
  };
1617
1670
  const path = file.filePath ?? "";
1618
1671
  const name = file.fileName;
1619
- if (name === void 0) return {
1672
+ if (name == null) return {
1620
1673
  type: "file",
1621
1674
  path
1622
1675
  };
@@ -1628,13 +1681,13 @@ function fileToCanonical(file) {
1628
1681
  }
1629
1682
  /** REPLY 元素 → canonical reply。 */
1630
1683
  function replyToCanonical(reply) {
1631
- if (reply === void 0) return {
1684
+ if (reply == null) return {
1632
1685
  type: "reply",
1633
1686
  messageId: ""
1634
1687
  };
1635
1688
  return {
1636
1689
  type: "reply",
1637
- messageId: reply.replayMsgId
1690
+ messageId: reply.replayMsgId ?? ""
1638
1691
  };
1639
1692
  }
1640
1693
  /**
@@ -1646,7 +1699,7 @@ function replyToCanonical(reply) {
1646
1699
  */
1647
1700
  function arkToCanonical(ark, el) {
1648
1701
  const bytes = ark?.bytesData;
1649
- if (bytes === void 0 || bytes === "") return {
1702
+ if (bytes == null || bytes === "") return {
1650
1703
  type: "unknown",
1651
1704
  raw: el
1652
1705
  };
@@ -2305,8 +2358,8 @@ const PTT_POLL_INTERVAL_MS = 300;
2305
2358
  const PTT_POLL_TIMES = 34;
2306
2359
  /** 语音下载完成判定:有路径且(传输完成或末次轮询)才返回元素,否则 null。 */
2307
2360
  function pttDownloadResult(latest, isLastPoll) {
2308
- if (latest === void 0) return null;
2309
- const hasPath = latest.filePath !== void 0 && latest.filePath !== "";
2361
+ if (latest == null) return null;
2362
+ const hasPath = latest.filePath != null && latest.filePath !== "";
2310
2363
  const done = latest.transferStatus === 2 || latest.transferStatus === 4;
2311
2364
  return hasPath && (done || isLastPoll) ? latest : null;
2312
2365
  }
@@ -2670,7 +2723,7 @@ var MsgApi = class {
2670
2723
  if (ptt === null) throw kernelError("消息中不包含语音", "NOT_FOUND");
2671
2724
  unwrapResult("translatePtt2Text", await this.service.translatePtt2Text(msgId, target, ptt));
2672
2725
  const text = findPttElement(await this.fetchMsgsByMsgId(target, [msgId]))?.pttElement?.text;
2673
- if (text === void 0 || text === "") throw kernelError("获取语音转文字结果失败", "UNKNOWN");
2726
+ if (text == null || text === "") throw kernelError("获取语音转文字结果失败", "UNKNOWN");
2674
2727
  return text;
2675
2728
  }
2676
2729
  /** 标记会话已读。 */
@@ -2705,7 +2758,7 @@ var MsgApi = class {
2705
2758
  async pttElementOf(msgId, target) {
2706
2759
  const el = findPttElement(await this.fetchMsgsByMsgId(target, [msgId]));
2707
2760
  const ptt = el?.pttElement;
2708
- if (el === null || ptt === void 0) return null;
2761
+ if (el === null || ptt == null) return null;
2709
2762
  return {
2710
2763
  elemId: el.elementId ?? "",
2711
2764
  ptt
@@ -2764,7 +2817,7 @@ var MsgApi = class {
2764
2817
  });
2765
2818
  unwrapResult("buildMultiForwardMsg", built);
2766
2819
  const elements = built.rspInfo?.elements;
2767
- if (elements === void 0 || elements.length === 0) throw kernelError("合并转发组装失败:无元素", "UNKNOWN");
2820
+ if (elements == null || elements.length === 0) throw kernelError("合并转发组装失败:无元素", "UNKNOWN");
2768
2821
  const msgId = this.service.generateMsgUniqueId(target.chatType, String(Date.now()));
2769
2822
  unwrapResult("sendMsg", await this.service.sendMsg(msgId, target, elements, /* @__PURE__ */ new Map()));
2770
2823
  return { msgId };
@@ -2772,7 +2825,7 @@ var MsgApi = class {
2772
2825
  /** 获取合并转发内容(get_forward_msg;resId 取自 multiForwardMsgElement)。 */
2773
2826
  async fetchForwardMessage(peer, msgId) {
2774
2827
  const forward = findForwardElement(await this.fetchMsgsByMsgId(peer, [msgId]));
2775
- if (forward === null || forward.resId === "") throw kernelError("消息不包含合并转发内容", "NOT_FOUND");
2828
+ if (forward === null || forward.resId == null || forward.resId === "") throw kernelError("消息不包含合并转发内容", "NOT_FOUND");
2776
2829
  const raw = await this.service.getMultiMsg(peer, msgId, forward.resId);
2777
2830
  unwrapResult("getMultiMsg", raw);
2778
2831
  return raw.msgList ?? [];
@@ -2787,17 +2840,26 @@ var MsgApi = class {
2787
2840
  unwrapResult("setStatus", await this.service.setStatus(opts));
2788
2841
  }
2789
2842
  };
2790
- /** 在消息列表中找含 pttElement 的元素(找不到返回 null)。 */
2843
+ /**
2844
+ * 在消息列表中找含 pttElement 的元素(找不到返回 null)。
2845
+ *
2846
+ * ⚠️ `!= null` 而非 `!== undefined`(Linux 原生宿主):Windows 省略未命中的
2847
+ * 变体字段,Linux 3.2.x 把所有变体字段显式置 null。用 `!== undefined` 会让
2848
+ * 首个元素(哪怕是文本消息)被误判成语音元素,进而把 null 传给原生下载接口。
2849
+ */
2791
2850
  function findPttElement(msgs) {
2792
2851
  const [first] = msgs;
2793
2852
  if (first === void 0) return null;
2794
- return first.elements.find((el) => el.pttElement !== void 0) ?? null;
2853
+ return first.elements.find((el) => el.pttElement != null) ?? null;
2795
2854
  }
2796
- /** 在消息列表中找含 multiForwardMsgElement 的元素(找不到返回 null)。 */
2855
+ /**
2856
+ * 在消息列表中找含 multiForwardMsgElement 的元素(找不到返回 null)。
2857
+ * 同 findPttElement:`!= null` 兼容 Linux 的显式 null 变体字段。
2858
+ */
2797
2859
  function findForwardElement(msgs) {
2798
2860
  const [first] = msgs;
2799
2861
  if (first === void 0) return null;
2800
- return first.elements.find((el) => el.multiForwardMsgElement !== void 0)?.multiForwardMsgElement ?? null;
2862
+ return first.elements.find((el) => el.multiForwardMsgElement != null)?.multiForwardMsgElement ?? null;
2801
2863
  }
2802
2864
  // src/apis/stranger-info.ts
2803
2865
  /** 宽松取值:undefined 时给默认值(null 也归默认)。 */
@@ -3565,9 +3627,12 @@ const DEFAULT_INTERVAL_MS = 250;
3565
3627
  function collectCandidates(msgs) {
3566
3628
  const seen = /* @__PURE__ */ new Set();
3567
3629
  const out = [];
3568
- for (const msg of msgs) for (const element of msg.elements ?? []) for (const candidate of picLocalCandidates(element.picElement)) if (!seen.has(candidate)) {
3569
- seen.add(candidate);
3570
- out.push(candidate);
3630
+ for (const msg of msgs) for (const element of msg.elements ?? []) {
3631
+ if (element.elementType !== ElementType.PIC || element.picElement == null) continue;
3632
+ for (const candidate of picLocalCandidates(element.picElement)) if (!seen.has(candidate)) {
3633
+ seen.add(candidate);
3634
+ out.push(candidate);
3635
+ }
3571
3636
  }
3572
3637
  return out;
3573
3638
  }
@@ -3613,7 +3678,13 @@ var MsgBridge = class {
3613
3678
  * 超时后原样放行(canonical 层置空裁决)。
3614
3679
  */
3615
3680
  emitPicAware(event, msgs) {
3616
- if (!hasPendingPicPaths(msgs)) {
3681
+ let pending;
3682
+ try {
3683
+ pending = hasPendingPicPaths(msgs);
3684
+ } catch {
3685
+ pending = false;
3686
+ }
3687
+ if (!pending) {
3617
3688
  this.channel.emit(event, msgs);
3618
3689
  return;
3619
3690
  }