@napuketto/kernel 0.15.3 → 0.16.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/dist/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
2
- import { readFile, rename, stat, writeFile } from "node:fs/promises";
2
+ import { chmod, readFile, rename, stat, writeFile } from "node:fs/promises";
3
3
  import { basename, dirname, extname, join, resolve } from "node:path";
4
- import { parse, stringify } from "smol-toml";
5
4
  import process from "node:process";
5
+ import { parse, stringify } from "smol-toml";
6
6
  import pino from "pino";
7
7
  import { homedir, hostname, platform, release, type, version } from "node:os";
8
8
  import { fileURLToPath } from "node:url";
@@ -181,6 +181,12 @@ function requireKernelService(service, serviceName, requiredMethods = []) {
181
181
  * 协议包的 zod schema 天然满足;kernel 主配置用手写校验器包装。
182
182
  * 协议配置 schema 在各自协议包,见各包 docs/design.md。
183
183
  */
184
+ /**
185
+ * 配置落盘权限(POSIX 0o600:仅属主可读写)。配置可能含协议鉴权 token,
186
+ * 不得按默认 umask(常见 0o644)暴露给同机其他用户;Windows 以 ACL 为准,
187
+ * 忽略 mode 位。
188
+ */
189
+ const CONFIG_FILE_MODE = 384;
184
190
  /** 按扩展名推断格式(.toml → toml,其余 → json)。 */
185
191
  function inferFormat(path) {
186
192
  if (extname(path).toLowerCase() === ".toml") return "toml";
@@ -252,7 +258,11 @@ var ConfigBase = class {
252
258
  const parsed = this.parse(this.serialize(next));
253
259
  mkdirSync(dirname(this.path), { recursive: true });
254
260
  const tmpPath = `${this.path}.tmp`;
255
- await writeFile(tmpPath, `${this.serialize(parsed)}\n`, "utf8");
261
+ await writeFile(tmpPath, `${this.serialize(parsed)}\n`, {
262
+ encoding: "utf8",
263
+ ...process.platform === "win32" ? {} : { mode: 384 }
264
+ });
265
+ if (process.platform !== "win32") await chmod(tmpPath, 384);
256
266
  await rename(tmpPath, this.path);
257
267
  this.value = parsed;
258
268
  }
@@ -1335,6 +1345,13 @@ function toDoubtFriendRequestInfo(item, uinMap) {
1335
1345
  *
1336
1346
  * ⚠️ 部分字段按公开资料(wrapper 外部契约)自研描述,待下一次进程内
1337
1347
  * 探测(probe.ts)校准——接口签名是外部系统事实,自研描述其形状。
1348
+ *
1349
+ * ⚠️ 跨平台空值契约(2026-11 Linux 3.2.x 线上事故):Windows 原生对象省略
1350
+ * 未命中的变体字段(`undefined`),Linux 原生对象则把未命中的变体字段**显式
1351
+ * 置 `null`**(纯文本元素的 `picElement: null` / `grayTipElement: null` 等),
1352
+ * 元素子字段同样可能是 null。因此本文件所有可选字段都声明为 `T | null`
1353
+ * (而非仅 `T`):读取方必须用 `== null` / `!= null`(同时覆盖 null 与
1354
+ * undefined),禁止只判 `=== undefined`。这样编译器会持续拦截新的漏网读取。
1338
1355
  */
1339
1356
  /** 会话类型(QQ wrapper 外部契约)。 */
1340
1357
  const ChatType = {
@@ -1465,18 +1482,18 @@ const AtType = {
1465
1482
  * 直接 `.get(0)` 抛 TypeError,整条视频消息事件分派失败。
1466
1483
  */
1467
1484
  function readVideoThumbPath(thumb) {
1468
- if (thumb === void 0 || thumb === "") return;
1485
+ if (thumb == null || thumb === "") return;
1469
1486
  if (typeof thumb === "string") return thumb;
1470
1487
  if (thumb instanceof Map) {
1471
1488
  const value = thumb.get(0);
1472
- return value === "" ? void 0 : value;
1489
+ return value == null || value === "" ? void 0 : value;
1473
1490
  }
1474
1491
  const value = thumb[String(0)];
1475
- return value === "" ? void 0 : value;
1492
+ return value == null || value === "" ? void 0 : value;
1476
1493
  }
1477
1494
  /** TEXT 元素 → canonical(at 或纯文本)。 */
1478
1495
  function textToCanonical(textEl) {
1479
- if (textEl === void 0) return {
1496
+ if (textEl == null) return {
1480
1497
  type: "text",
1481
1498
  text: ""
1482
1499
  };
@@ -1502,9 +1519,15 @@ function textToCanonical(textEl) {
1502
1519
  text: content
1503
1520
  };
1504
1521
  }
1505
- /** 空串视为缺失(Linux 原生对象常给 "" 而不是省略字段)。 */
1522
+ /**
1523
+ * 空值视为缺失(跨平台原生对象形态差异):
1524
+ * - Windows 省略未命中的字段 → undefined;
1525
+ * - Linux 3.2.x 显式置 null;
1526
+ * - Linux 原生对象也常给 "" 而不是省略字段。
1527
+ * 三者统一归为 undefined,避免 null 被当成有效值继续下传。
1528
+ */
1506
1529
  function emptyToUndefined(value) {
1507
- return value === void 0 || value === "" ? void 0 : value;
1530
+ return value == null || value === "" ? void 0 : value;
1508
1531
  }
1509
1532
  /** 取第一个非空字符串(跨平台字段名不一致时的兜底链)。 */
1510
1533
  function firstNonEmpty(...values) {
@@ -1527,7 +1550,7 @@ function isLocalPathCandidate(value) {
1527
1550
  * pic-path-grace(桥层等待落盘)与 picToCanonical 共用同一候选序。
1528
1551
  */
1529
1552
  function picLocalCandidates(pic) {
1530
- if (pic === void 0) return [];
1553
+ if (pic == null) return [];
1531
1554
  const seen = /* @__PURE__ */ new Set();
1532
1555
  const out = [];
1533
1556
  for (const raw of [pic.picPath, pic.sourcePath]) {
@@ -1541,7 +1564,7 @@ function picLocalCandidates(pic) {
1541
1564
  }
1542
1565
  /** PIC 元素 → canonical image。 */
1543
1566
  function picToCanonical(pic) {
1544
- if (pic === void 0) return {
1567
+ if (pic == null) return {
1545
1568
  type: "image",
1546
1569
  path: ""
1547
1570
  };
@@ -1560,7 +1583,7 @@ function picToCanonical(pic) {
1560
1583
  }
1561
1584
  /** FACE 元素 → canonical face。 */
1562
1585
  function faceToCanonical(face) {
1563
- if (face === void 0) return {
1586
+ if (face == null) return {
1564
1587
  type: "face",
1565
1588
  id: "0"
1566
1589
  };
@@ -1571,7 +1594,7 @@ function faceToCanonical(face) {
1571
1594
  }
1572
1595
  /** PTT 元素 → canonical voice。 */
1573
1596
  function pttToCanonical(ptt) {
1574
- if (ptt === void 0) return {
1597
+ if (ptt == null) return {
1575
1598
  type: "voice",
1576
1599
  path: ""
1577
1600
  };
@@ -1582,7 +1605,7 @@ function pttToCanonical(ptt) {
1582
1605
  }
1583
1606
  /** VIDEO 元素 → canonical video。 */
1584
1607
  function videoToCanonical(video) {
1585
- if (video === void 0) return {
1608
+ if (video == null) return {
1586
1609
  type: "video",
1587
1610
  path: ""
1588
1611
  };
@@ -1590,23 +1613,23 @@ function videoToCanonical(video) {
1590
1613
  type: "video",
1591
1614
  path: video.filePath ?? video.videoUrl ?? ""
1592
1615
  };
1593
- if (video.videoUrl !== void 0) base.url = video.videoUrl;
1616
+ if (video.videoUrl != null) base.url = video.videoUrl;
1594
1617
  const thumb = readVideoThumbPath(video.thumbPath);
1595
1618
  if (thumb !== void 0) base.thumbPath = thumb;
1596
- if (video.thumbWidth !== void 0 && video.thumbWidth > 0) base.width = video.thumbWidth;
1597
- if (video.thumbHeight !== void 0 && video.thumbHeight > 0) base.height = video.thumbHeight;
1598
- if (video.fileTime !== void 0 && video.fileTime > 0) base.durationSec = video.fileTime;
1619
+ if (video.thumbWidth != null && video.thumbWidth > 0) base.width = video.thumbWidth;
1620
+ if (video.thumbHeight != null && video.thumbHeight > 0) base.height = video.thumbHeight;
1621
+ if (video.fileTime != null && video.fileTime > 0) base.durationSec = video.fileTime;
1599
1622
  return base;
1600
1623
  }
1601
1624
  /** FILE 元素 → canonical file。 */
1602
1625
  function fileToCanonical(file) {
1603
- if (file === void 0) return {
1626
+ if (file == null) return {
1604
1627
  type: "file",
1605
1628
  path: ""
1606
1629
  };
1607
1630
  const path = file.filePath ?? "";
1608
1631
  const name = file.fileName;
1609
- if (name === void 0) return {
1632
+ if (name == null) return {
1610
1633
  type: "file",
1611
1634
  path
1612
1635
  };
@@ -1618,13 +1641,13 @@ function fileToCanonical(file) {
1618
1641
  }
1619
1642
  /** REPLY 元素 → canonical reply。 */
1620
1643
  function replyToCanonical(reply) {
1621
- if (reply === void 0) return {
1644
+ if (reply == null) return {
1622
1645
  type: "reply",
1623
1646
  messageId: ""
1624
1647
  };
1625
1648
  return {
1626
1649
  type: "reply",
1627
- messageId: reply.replayMsgId
1650
+ messageId: reply.replayMsgId ?? ""
1628
1651
  };
1629
1652
  }
1630
1653
  /**
@@ -1636,7 +1659,7 @@ function replyToCanonical(reply) {
1636
1659
  */
1637
1660
  function arkToCanonical(ark, el) {
1638
1661
  const bytes = ark?.bytesData;
1639
- if (bytes === void 0 || bytes === "") return {
1662
+ if (bytes == null || bytes === "") return {
1640
1663
  type: "unknown",
1641
1664
  raw: el
1642
1665
  };
@@ -2295,8 +2318,8 @@ const PTT_POLL_INTERVAL_MS = 300;
2295
2318
  const PTT_POLL_TIMES = 34;
2296
2319
  /** 语音下载完成判定:有路径且(传输完成或末次轮询)才返回元素,否则 null。 */
2297
2320
  function pttDownloadResult(latest, isLastPoll) {
2298
- if (latest === void 0) return null;
2299
- const hasPath = latest.filePath !== void 0 && latest.filePath !== "";
2321
+ if (latest == null) return null;
2322
+ const hasPath = latest.filePath != null && latest.filePath !== "";
2300
2323
  const done = latest.transferStatus === 2 || latest.transferStatus === 4;
2301
2324
  return hasPath && (done || isLastPoll) ? latest : null;
2302
2325
  }
@@ -2660,7 +2683,7 @@ var MsgApi = class {
2660
2683
  if (ptt === null) throw kernelError("消息中不包含语音", "NOT_FOUND");
2661
2684
  unwrapResult("translatePtt2Text", await this.service.translatePtt2Text(msgId, target, ptt));
2662
2685
  const text = findPttElement(await this.fetchMsgsByMsgId(target, [msgId]))?.pttElement?.text;
2663
- if (text === void 0 || text === "") throw kernelError("获取语音转文字结果失败", "UNKNOWN");
2686
+ if (text == null || text === "") throw kernelError("获取语音转文字结果失败", "UNKNOWN");
2664
2687
  return text;
2665
2688
  }
2666
2689
  /** 标记会话已读。 */
@@ -2695,7 +2718,7 @@ var MsgApi = class {
2695
2718
  async pttElementOf(msgId, target) {
2696
2719
  const el = findPttElement(await this.fetchMsgsByMsgId(target, [msgId]));
2697
2720
  const ptt = el?.pttElement;
2698
- if (el === null || ptt === void 0) return null;
2721
+ if (el === null || ptt == null) return null;
2699
2722
  return {
2700
2723
  elemId: el.elementId ?? "",
2701
2724
  ptt
@@ -2754,7 +2777,7 @@ var MsgApi = class {
2754
2777
  });
2755
2778
  unwrapResult("buildMultiForwardMsg", built);
2756
2779
  const elements = built.rspInfo?.elements;
2757
- if (elements === void 0 || elements.length === 0) throw kernelError("合并转发组装失败:无元素", "UNKNOWN");
2780
+ if (elements == null || elements.length === 0) throw kernelError("合并转发组装失败:无元素", "UNKNOWN");
2758
2781
  const msgId = this.service.generateMsgUniqueId(target.chatType, String(Date.now()));
2759
2782
  unwrapResult("sendMsg", await this.service.sendMsg(msgId, target, elements, /* @__PURE__ */ new Map()));
2760
2783
  return { msgId };
@@ -2762,7 +2785,7 @@ var MsgApi = class {
2762
2785
  /** 获取合并转发内容(get_forward_msg;resId 取自 multiForwardMsgElement)。 */
2763
2786
  async fetchForwardMessage(peer, msgId) {
2764
2787
  const forward = findForwardElement(await this.fetchMsgsByMsgId(peer, [msgId]));
2765
- if (forward === null || forward.resId === "") throw kernelError("消息不包含合并转发内容", "NOT_FOUND");
2788
+ if (forward === null || forward.resId == null || forward.resId === "") throw kernelError("消息不包含合并转发内容", "NOT_FOUND");
2766
2789
  const raw = await this.service.getMultiMsg(peer, msgId, forward.resId);
2767
2790
  unwrapResult("getMultiMsg", raw);
2768
2791
  return raw.msgList ?? [];
@@ -2777,17 +2800,26 @@ var MsgApi = class {
2777
2800
  unwrapResult("setStatus", await this.service.setStatus(opts));
2778
2801
  }
2779
2802
  };
2780
- /** 在消息列表中找含 pttElement 的元素(找不到返回 null)。 */
2803
+ /**
2804
+ * 在消息列表中找含 pttElement 的元素(找不到返回 null)。
2805
+ *
2806
+ * ⚠️ `!= null` 而非 `!== undefined`(Linux 原生宿主):Windows 省略未命中的
2807
+ * 变体字段,Linux 3.2.x 把所有变体字段显式置 null。用 `!== undefined` 会让
2808
+ * 首个元素(哪怕是文本消息)被误判成语音元素,进而把 null 传给原生下载接口。
2809
+ */
2781
2810
  function findPttElement(msgs) {
2782
2811
  const [first] = msgs;
2783
2812
  if (first === void 0) return null;
2784
- return first.elements.find((el) => el.pttElement !== void 0) ?? null;
2813
+ return first.elements.find((el) => el.pttElement != null) ?? null;
2785
2814
  }
2786
- /** 在消息列表中找含 multiForwardMsgElement 的元素(找不到返回 null)。 */
2815
+ /**
2816
+ * 在消息列表中找含 multiForwardMsgElement 的元素(找不到返回 null)。
2817
+ * 同 findPttElement:`!= null` 兼容 Linux 的显式 null 变体字段。
2818
+ */
2787
2819
  function findForwardElement(msgs) {
2788
2820
  const [first] = msgs;
2789
2821
  if (first === void 0) return null;
2790
- return first.elements.find((el) => el.multiForwardMsgElement !== void 0)?.multiForwardMsgElement ?? null;
2822
+ return first.elements.find((el) => el.multiForwardMsgElement != null)?.multiForwardMsgElement ?? null;
2791
2823
  }
2792
2824
  // src/apis/stranger-info.ts
2793
2825
  /** 宽松取值:undefined 时给默认值(null 也归默认)。 */
@@ -3555,9 +3587,12 @@ const DEFAULT_INTERVAL_MS = 250;
3555
3587
  function collectCandidates(msgs) {
3556
3588
  const seen = /* @__PURE__ */ new Set();
3557
3589
  const out = [];
3558
- for (const msg of msgs) for (const element of msg.elements ?? []) for (const candidate of picLocalCandidates(element.picElement)) if (!seen.has(candidate)) {
3559
- seen.add(candidate);
3560
- out.push(candidate);
3590
+ for (const msg of msgs) for (const element of msg.elements ?? []) {
3591
+ if (element.elementType !== ElementType.PIC || element.picElement == null) continue;
3592
+ for (const candidate of picLocalCandidates(element.picElement)) if (!seen.has(candidate)) {
3593
+ seen.add(candidate);
3594
+ out.push(candidate);
3595
+ }
3561
3596
  }
3562
3597
  return out;
3563
3598
  }
@@ -3603,7 +3638,13 @@ var MsgBridge = class {
3603
3638
  * 超时后原样放行(canonical 层置空裁决)。
3604
3639
  */
3605
3640
  emitPicAware(event, msgs) {
3606
- if (!hasPendingPicPaths(msgs)) {
3641
+ let pending;
3642
+ try {
3643
+ pending = hasPendingPicPaths(msgs);
3644
+ } catch {
3645
+ pending = false;
3646
+ }
3647
+ if (!pending) {
3607
3648
  this.channel.emit(event, msgs);
3608
3649
  return;
3609
3650
  }
@@ -6869,6 +6910,6 @@ function listQQVersions(installDir) {
6869
6910
  return statSync(full).isDirectory() && existsSync(join(full, "resources", "app", "wrapper.node"));
6870
6911
  }).sort().reverse();
6871
6912
  }
6872
- export { BuddyCache, ChatType, ConfigBase, ConfigError, DEFAULT_DATA_ROOT_NAME, DependsAdapter, DispatcherAdapter, EXIT_CONFIG_INVALID, ElementType, EssenceListSync, FlashTransferApi, FriendApi, FriendBridge, GlobalAdapter, GrayTipSubType, GroupApi, GroupBridge, GroupCache, GroupListUpdateType, GroupMemberDataSource, GroupNotifyApi, GroupNotifyMsgStatus, GroupNotifyMsgType, KERNEL_ERROR_CODES, KernelError, LoginBridge, MAIN_CONFIG_FILE, MsgApi, MsgBridge, NTEventChannel, NTGroupMemberRole, NTGroupRequestOperateTypes, NapukettoCore, PathWrapper, ProfileApi, ProfileLikeApi, QrLoginSession, RecentContactApi, RecentContactBridge, RichMediaApi, TicketApi, TipGroupElementType, VIDEO_THUMB_KEY, WebApi, WebHonorType, activateO3MiscService, buildEngineConfig, buildLoginConfig, buildSessionConfig, configError, createConsoleFormatStream, createCoreContext, createKernelRuntime, createSessionListener as createLifecycleSessionListener, createSessionListener, createLogger, createLoginListener, createRecordingListener, createSession, createWrapper, diffEssenceList, dlopenWrapper, electronProcessType, errorMessage, getErrorCode, getErrorMessage, getExistingSession, getMainSession, getOwnProp, hasBooleanProp, hasCallableProp, hasFunctionProp, hasMethodSurface, hasNumberProp, hasStaticCreator, hasStaticGetter, hasStringProp, initAndStartSession, initEngine, initSession, isCallable, isKernelError, isPromiseLike, isRecord, kernelError, listLoginAccounts, listQQVersions, narrowBuddyCategories, narrowEssenceGroupArg, parseAppidFromMajor, parseJsonWithGuard, parseLogConfig, parseToml, probeRuntime, quickLogin, readMachineGuid, requireKernelService, resolveAppidQua, resolveConfigPath, resolveDataRoot, resolveQQVersion, resolveQqGlobalPath, resolveQqUserDataRoot, resolveWrapperPath, sleep, startListenerRecording, startNapuketto, startSession, stringifyToml, toCanonicalElements, toSendElements, waitFor, waitForNetworkConnection, waitSessionReady };
6913
+ export { BuddyCache, CONFIG_FILE_MODE, ChatType, ConfigBase, ConfigError, DEFAULT_DATA_ROOT_NAME, DependsAdapter, DispatcherAdapter, EXIT_CONFIG_INVALID, ElementType, EssenceListSync, FlashTransferApi, FriendApi, FriendBridge, GlobalAdapter, GrayTipSubType, GroupApi, GroupBridge, GroupCache, GroupListUpdateType, GroupMemberDataSource, GroupNotifyApi, GroupNotifyMsgStatus, GroupNotifyMsgType, KERNEL_ERROR_CODES, KernelError, LoginBridge, MAIN_CONFIG_FILE, MsgApi, MsgBridge, NTEventChannel, NTGroupMemberRole, NTGroupRequestOperateTypes, NapukettoCore, PathWrapper, ProfileApi, ProfileLikeApi, QrLoginSession, RecentContactApi, RecentContactBridge, RichMediaApi, TicketApi, TipGroupElementType, VIDEO_THUMB_KEY, WebApi, WebHonorType, activateO3MiscService, buildEngineConfig, buildLoginConfig, buildSessionConfig, configError, createConsoleFormatStream, createCoreContext, createKernelRuntime, createSessionListener as createLifecycleSessionListener, createSessionListener, createLogger, createLoginListener, createRecordingListener, createSession, createWrapper, diffEssenceList, dlopenWrapper, electronProcessType, errorMessage, getErrorCode, getErrorMessage, getExistingSession, getMainSession, getOwnProp, hasBooleanProp, hasCallableProp, hasFunctionProp, hasMethodSurface, hasNumberProp, hasStaticCreator, hasStaticGetter, hasStringProp, initAndStartSession, initEngine, initSession, isCallable, isKernelError, isPromiseLike, isRecord, kernelError, listLoginAccounts, listQQVersions, narrowBuddyCategories, narrowEssenceGroupArg, parseAppidFromMajor, parseJsonWithGuard, parseLogConfig, parseToml, probeRuntime, quickLogin, readMachineGuid, requireKernelService, resolveAppidQua, resolveConfigPath, resolveDataRoot, resolveQQVersion, resolveQqGlobalPath, resolveQqUserDataRoot, resolveWrapperPath, sleep, startListenerRecording, startNapuketto, startSession, stringifyToml, toCanonicalElements, toSendElements, waitFor, waitForNetworkConnection, waitSessionReady };
6873
6914
 
6874
6915
  //# sourceMappingURL=index.mjs.map