@napuketto/kernel 0.16.4 → 0.17.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.d.mts CHANGED
@@ -3251,11 +3251,14 @@ interface CoreLoginOptions {
3251
3251
  * (qrFallback 可接)。底层 promise 无法取消:超时后悬挂丢弃。
3252
3252
  */
3253
3253
  quickLoginTimeoutMs?: number | undefined;
3254
- /** 快速登录失败时回退 QR 登录(二维码写缓存目录),默认 false。 */
3254
+ /** 快速登录失败时回退 QR 登录(二维码写账号 cache/qr 临时目录),默认 false。 */
3255
3255
  qrFallback?: boolean | undefined;
3256
3256
  /** 强制扫码:跳过快速登录,直接走 QR 登录(koishi 强制扫码 / 切账号)。默认 false。 */
3257
3257
  qrOnly?: boolean | undefined;
3258
- /** QR 二维码图片保存路径(默认缓存目录 qrcode.png)。 */
3258
+ /**
3259
+ * QR 二维码图片保存路径(显式指定时由调用方接管生命周期;默认
3260
+ * `<账号目录>/cache/qr/qrcode.png`,登录结束删除)。
3261
+ */
3259
3262
  qrCodePath?: string | undefined;
3260
3263
  /** 登录进度回调(QR 阶段:二维码数据 + 状态变化)。子进程 IPC 模式转发用。 */
3261
3264
  onLoginProgress?: ((progress: LoginProgress) => void) | undefined;
@@ -3275,6 +3278,8 @@ export declare class NapukettoCore {
3275
3278
  readonly ctx: CoreContext;
3276
3279
  /** QR 登录会话句柄(登录期间非空;登录终态后清空)。refreshQr() 据此可达。 */
3277
3280
  private qrSession;
3281
+ /** 本次登录默认落盘的临时二维码路径(显式 qrCodePath 不纳入自动清理)。 */
3282
+ private ephemeralQrCodePath;
3278
3283
  /** 由 createRuntime() 装配的运行时服务集合(stop/dispose 时统一清理)。 */
3279
3284
  private runtime;
3280
3285
  /** stop 幂等标记。 */
@@ -3345,7 +3350,7 @@ export declare class NapukettoCore {
3345
3350
  * koishi 前端「刷新二维码」按钮经 loader IPC `login.refreshQr` 动作直达。
3346
3351
  */
3347
3352
  refreshQr(): boolean;
3348
- /** QR 登录:QrLoginSession 状态机 + 二维码写缓存目录(无 UI,cli 可打印路径)。 */
3353
+ /** QR 登录:QrLoginSession 状态机 + 二维码写独占缓存目录(无 UI,cli 可打印路径)。 */
3349
3354
  private loginByQr;
3350
3355
  /** 订阅 QR 进度:二维码写盘 + 状态机经 onLoginProgress 转发。 */
3351
3356
  private subscribeQrProgress;
@@ -3356,6 +3361,8 @@ export declare class NapukettoCore {
3356
3361
  * 重复调用直接返回(宿主关停钩子可能同时触发多次)。
3357
3362
  */
3358
3363
  stop(): void;
3364
+ /** 删除本次登录默认落盘的临时二维码(显式 qrCodePath 不删)。幂等。 */
3365
+ private cleanupEphemeralQrCode;
3359
3366
  /** 显式释放(`using` / 手动调用的统一入口;等价 stop)。 */
3360
3367
  dispose(): void;
3361
3368
  /** `using core = ...` 自动释放。 */
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
- import { appendFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
2
+ import { appendFileSync, chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
3
3
  import { open, readFile, rename, rm, stat } from "node:fs/promises";
4
4
  import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
5
5
  import process from "node:process";
@@ -750,7 +750,10 @@ const SENSITIVE_FIELDS = /* @__PURE__ */ new Set([
750
750
  "skey",
751
751
  "p_skey",
752
752
  "csrf_token",
753
- "bkn"
753
+ "bkn",
754
+ "qrcodeurl",
755
+ "qr_code_url",
756
+ "qrcode_url"
754
757
  ]);
755
758
  /** 只遍历刚解析的 JSON 树;不会触碰业务对象、getter、toJSON 或循环引用。 */
756
759
  function redactFields(record) {
@@ -5849,6 +5852,8 @@ var NapukettoCore = class NapukettoCore {
5849
5852
  ctx;
5850
5853
  /** QR 登录会话句柄(登录期间非空;登录终态后清空)。refreshQr() 据此可达。 */
5851
5854
  qrSession = null;
5855
+ /** 本次登录默认落盘的临时二维码路径(显式 qrCodePath 不纳入自动清理)。 */
5856
+ ephemeralQrCodePath = null;
5852
5857
  /** 由 createRuntime() 装配的运行时服务集合(stop/dispose 时统一清理)。 */
5853
5858
  runtime = null;
5854
5859
  /** stop 幂等标记。 */
@@ -6019,13 +6024,15 @@ var NapukettoCore = class NapukettoCore {
6019
6024
  this.qrSession.refresh();
6020
6025
  return true;
6021
6026
  }
6022
- /** QR 登录:QrLoginSession 状态机 + 二维码写缓存目录(无 UI,cli 可打印路径)。 */
6027
+ /** QR 登录:QrLoginSession 状态机 + 二维码写独占缓存目录(无 UI,cli 可打印路径)。 */
6023
6028
  async loginByQr(opts) {
6024
6029
  const { wrapper } = this.ctx;
6025
6030
  if (wrapper === null || wrapper.loginService === null) throw kernelError("wrapper/loginService 不可用,无法 QR 登录", "INVALID_STATE");
6026
6031
  const session = new QrLoginSession(wrapper.loginService);
6027
6032
  this.qrSession = session;
6028
- const qrPath = opts.qrCodePath ?? this.ctx.paths.file("cache", "qrcode.png");
6033
+ const explicitPath = opts.qrCodePath;
6034
+ const qrPath = explicitPath ?? this.ctx.paths.file("cache", QR_CODE_DIR, QR_CODE_FILENAME);
6035
+ this.ephemeralQrCodePath = explicitPath === void 0 ? qrPath : null;
6029
6036
  this.subscribeQrProgress(session, opts, qrPath);
6030
6037
  try {
6031
6038
  session.start();
@@ -6039,12 +6046,13 @@ var NapukettoCore = class NapukettoCore {
6039
6046
  } finally {
6040
6047
  session.stop();
6041
6048
  this.qrSession = null;
6049
+ this.cleanupEphemeralQrCode();
6042
6050
  }
6043
6051
  }
6044
6052
  /** 订阅 QR 进度:二维码写盘 + 状态机经 onLoginProgress 转发。 */
6045
6053
  subscribeQrProgress(session, opts, qrPath) {
6046
6054
  session.onQrCode((qr) => {
6047
- this.ctx.logger.warn(`请扫描二维码登录(保存: ${qrPath} | URL: ${qr.qrcodeUrl})`);
6055
+ this.ctx.logger.warn(`请扫描二维码登录(图片: ${qrPath})`);
6048
6056
  if (qr.pngBase64 !== "") writeQrCodePng(qrPath, qr.pngBase64);
6049
6057
  opts.onLoginProgress?.({
6050
6058
  state: "waiting_scan",
@@ -6070,10 +6078,17 @@ var NapukettoCore = class NapukettoCore {
6070
6078
  this.stopped = true;
6071
6079
  this.qrSession?.stop();
6072
6080
  this.qrSession = null;
6081
+ this.cleanupEphemeralQrCode();
6073
6082
  this.runtime?.dispose();
6074
6083
  this.runtime = null;
6075
6084
  this.ctx.logger.info("NapukettoCore 停止");
6076
6085
  }
6086
+ /** 删除本次登录默认落盘的临时二维码(显式 qrCodePath 不删)。幂等。 */
6087
+ cleanupEphemeralQrCode() {
6088
+ const path = this.ephemeralQrCodePath;
6089
+ this.ephemeralQrCodePath = null;
6090
+ if (path !== null) removeQrCodePng(path);
6091
+ }
6077
6092
  /** 显式释放(`using` / 手动调用的统一入口;等价 stop)。 */
6078
6093
  dispose() {
6079
6094
  this.stop();
@@ -6121,11 +6136,33 @@ function resolveSessionAccountPath(ctx, wrapper) {
6121
6136
  function hasInitConfig(val) {
6122
6137
  return typeof val["initConfig"] === "function";
6123
6138
  }
6124
- /** 写二维码 png 到磁盘(base64 解码,供 cli/用户扫码)。 */
6139
+ /** 二维码 png 默认落盘目录(账号 cache 下的独占子目录)。 */
6140
+ const QR_CODE_DIR = "qr";
6141
+ /** 二维码 png 默认文件名。 */
6142
+ const QR_CODE_FILENAME = "qrcode.png";
6143
+ /**
6144
+ * 写二维码 png 到磁盘(base64 解码,供 cli/用户扫码)。
6145
+ *
6146
+ * 安全边界:目录按 0o700 创建、文件按 0o600 写入;写入前拒绝符号链接,避免被
6147
+ * 预置链接引到其他文件。失败不阻塞登录(cli 仍可打印 URL / 终端渲染)。
6148
+ */
6125
6149
  function writeQrCodePng(filePath, pngBase64) {
6126
6150
  try {
6127
- mkdirSync(dirname(filePath), { recursive: true });
6128
- writeFileSync(filePath, Buffer.from(pngBase64, "base64"));
6151
+ mkdirSync(dirname(filePath), {
6152
+ recursive: true,
6153
+ mode: 448
6154
+ });
6155
+ try {
6156
+ if (lstatSync(filePath).isSymbolicLink()) return;
6157
+ } catch {}
6158
+ writeFileSync(filePath, Buffer.from(pngBase64, "base64"), { mode: 384 });
6159
+ chmodSync(filePath, 384);
6160
+ } catch {}
6161
+ }
6162
+ /** 删除默认落盘的二维码文件(不存在 / 权限失败静默)。 */
6163
+ function removeQrCodePng(filePath) {
6164
+ try {
6165
+ unlinkSync(filePath);
6129
6166
  } catch {}
6130
6167
  }
6131
6168
  /** CoreLoginOptions → quickLogin 参数(仅取快速登录相关两键,QR 键不透传)。 */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@napuketto/kernel",
4
- "version": "0.16.4",
4
+ "version": "0.17.0",
5
5
  "type": "module",
6
6
  "description": "Core runtime kernel of NapukettoQQ",
7
7
  "keywords": [