@napuketto/loader 0.0.4 → 0.0.6
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/host/self-host.cjs +78 -48
- package/package.json +1 -1
package/dist/host/self-host.cjs
CHANGED
|
@@ -24,8 +24,8 @@ let node_fs = require("node:fs");
|
|
|
24
24
|
let node_path = require("node:path");
|
|
25
25
|
let node_process = require("node:process");
|
|
26
26
|
node_process = __toESM(node_process, 1);
|
|
27
|
-
let node_readline = require("node:readline");
|
|
28
27
|
let node_os = require("node:os");
|
|
28
|
+
let node_readline = require("node:readline");
|
|
29
29
|
//#region src/instance-lock.ts
|
|
30
30
|
/**
|
|
31
31
|
* instance-lock.ts:单实例锁(2026-08-07 根治「多实例抢数据目录锁挂起」)
|
|
@@ -173,10 +173,63 @@ const env = {
|
|
|
173
173
|
NAPKETTO_DATA: process.env["NAPKETTO_DATA"]
|
|
174
174
|
};
|
|
175
175
|
//#endregion
|
|
176
|
+
//#region src/host/util.ts
|
|
177
|
+
/**
|
|
178
|
+
* util.ts:引导公共工具(日志 + 共享状态)。
|
|
179
|
+
* 2026-08-07 阶段 2:由 runtime/boot-util.js TS 化(零语义改动)。
|
|
180
|
+
* 由 self-host.ts 及各拆分模块 import(CJS bundle,运行在自建宿主引导进程内)。
|
|
181
|
+
*/
|
|
182
|
+
/** boot 日志路径(NAPUTO_CFG_DIR 下,与 stub 验证日志同目录)。 */
|
|
183
|
+
const LOG_PATH = env.NAPUTO_CFG_DIR ? (0, node_path.join)(env.NAPUTO_CFG_DIR, "napuketto-boot.log") : (0, node_path.join)((0, node_os.tmpdir)(), "napuketto-boot.log");
|
|
184
|
+
/** 追加一行 boot 日志(失败静默,不阻塞引导)。 */
|
|
185
|
+
function log(msg) {
|
|
186
|
+
try {
|
|
187
|
+
(0, node_fs.appendFileSync)(LOG_PATH, `[${(/* @__PURE__ */ new Date()).toISOString()}] ${msg}\n`);
|
|
188
|
+
} catch {}
|
|
189
|
+
}
|
|
190
|
+
/** 错误信息提取(保留 JS 原语义:message 存在用 message,否则用错误对象本身)。 */
|
|
191
|
+
function errMsg(e) {
|
|
192
|
+
return `${e?.message ?? e}`;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* 逐条处理消息回调参数(onRecvMsg 为消息数组——2026-08-07 运行时实证;
|
|
196
|
+
* 兼容单条对象,过滤无效项)。
|
|
197
|
+
*/
|
|
198
|
+
function forEachRawMessage(msgs, handler) {
|
|
199
|
+
const list = Array.isArray(msgs) ? msgs : [msgs];
|
|
200
|
+
for (const msg of list) {
|
|
201
|
+
if (!msg || typeof msg !== "object") continue;
|
|
202
|
+
handler(msg);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function createState() {
|
|
206
|
+
return {
|
|
207
|
+
wrapperExports: null,
|
|
208
|
+
qqSession: null,
|
|
209
|
+
qqLoginService: null,
|
|
210
|
+
bootstrapped: false
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
176
214
|
//#region src/host/ipc/ipc-actions.ts
|
|
177
215
|
/**
|
|
216
|
+
* ipc-actions.ts:IPC 动作表(koishi 动作 → kernel API)。
|
|
217
|
+
*
|
|
218
|
+
* 协议契约:动作名点分域(`msg.sendMessage` 等),params 宽松透传。
|
|
219
|
+
* 本轮实现 koishi Bot 核心动作(发消息/撤回/历史/已读/列表/self);
|
|
220
|
+
* 其余动作随 koishi 插件 actions.ts 轮次按需扩充。
|
|
221
|
+
*
|
|
222
|
+
* ⚠️ 不自研 kernel 依赖(loader 运行时 file:// 动态 import kernel,编译期不建
|
|
223
|
+
* workspace 依赖——见 types.ts 注释)。错误码提取用宽松结构判断。
|
|
224
|
+
*/
|
|
225
|
+
/**
|
|
178
226
|
* 从宽松 params 构造 Peer(缺省 chatType=1 群聊)。
|
|
179
|
-
* 优先 peerUid(uid 直通);缺省时 peerUin
|
|
227
|
+
* 优先 peerUid(uid 直通);缺省时 peerUin 经 uinToUid 转换。
|
|
228
|
+
*
|
|
229
|
+
* ⚠️ 2026-08-09 修复:群聊(chatType=2)peerUid 直接用群号(kernel Peer 定义
|
|
230
|
+
* 「群号 / 用户 uid」,OB11 resolve-peer 同构),**不走 uinToUid**——
|
|
231
|
+
* getUidByUins 是「用户 uin → uid」转换,传群号属非法调用(QQ 原生内部
|
|
232
|
+
* 抛 `Cannot read properties of undefined (reading 'service')`,实测)。
|
|
180
233
|
*/
|
|
181
234
|
async function toPeer(params, uinToUid) {
|
|
182
235
|
const chatType = typeof params["chatType"] === "number" ? params["chatType"] : 1;
|
|
@@ -186,13 +239,20 @@ async function toPeer(params, uinToUid) {
|
|
|
186
239
|
peerUid: uidParam
|
|
187
240
|
};
|
|
188
241
|
const uinParam = params["peerUin"];
|
|
189
|
-
if (typeof uinParam === "string" && uinParam !== ""
|
|
190
|
-
|
|
191
|
-
if (uid !== void 0 && uid !== "") return {
|
|
242
|
+
if (typeof uinParam === "string" && uinParam !== "") {
|
|
243
|
+
if (chatType === 2) return {
|
|
192
244
|
chatType,
|
|
193
|
-
peerUid:
|
|
245
|
+
peerUid: uinParam
|
|
194
246
|
};
|
|
195
|
-
|
|
247
|
+
if (uinToUid !== void 0) {
|
|
248
|
+
const uid = (await uinToUid([uinParam])).get(uinParam);
|
|
249
|
+
if (uid !== void 0 && uid !== "") return {
|
|
250
|
+
chatType,
|
|
251
|
+
peerUid: uid
|
|
252
|
+
};
|
|
253
|
+
throw new Error(`uin 转 uid 失败: ${uinParam}`);
|
|
254
|
+
}
|
|
255
|
+
throw new Error("缺 peerUid(或 peerUin 且未注入 uinToUid)");
|
|
196
256
|
}
|
|
197
257
|
throw new Error("缺 peerUid(或 peerUin 且未注入 uinToUid)");
|
|
198
258
|
}
|
|
@@ -246,6 +306,7 @@ async function callIpcAction(actions, action, params) {
|
|
|
246
306
|
};
|
|
247
307
|
} catch (err) {
|
|
248
308
|
const message = err instanceof Error ? err.message : String(err);
|
|
309
|
+
log(`[ipc] action=${action} 失败: ${message}\n${err instanceof Error && err.stack !== void 0 ? err.stack : ""}`);
|
|
249
310
|
return {
|
|
250
311
|
ok: false,
|
|
251
312
|
error: {
|
|
@@ -460,44 +521,6 @@ function startIpcMode(services) {
|
|
|
460
521
|
};
|
|
461
522
|
}
|
|
462
523
|
//#endregion
|
|
463
|
-
//#region src/host/util.ts
|
|
464
|
-
/**
|
|
465
|
-
* util.ts:引导公共工具(日志 + 共享状态)。
|
|
466
|
-
* 2026-08-07 阶段 2:由 runtime/boot-util.js TS 化(零语义改动)。
|
|
467
|
-
* 由 self-host.ts 及各拆分模块 import(CJS bundle,运行在自建宿主引导进程内)。
|
|
468
|
-
*/
|
|
469
|
-
/** boot 日志路径(NAPUTO_CFG_DIR 下,与 stub 验证日志同目录)。 */
|
|
470
|
-
const LOG_PATH = env.NAPUTO_CFG_DIR ? (0, node_path.join)(env.NAPUTO_CFG_DIR, "napuketto-boot.log") : (0, node_path.join)((0, node_os.tmpdir)(), "napuketto-boot.log");
|
|
471
|
-
/** 追加一行 boot 日志(失败静默,不阻塞引导)。 */
|
|
472
|
-
function log(msg) {
|
|
473
|
-
try {
|
|
474
|
-
(0, node_fs.appendFileSync)(LOG_PATH, `[${(/* @__PURE__ */ new Date()).toISOString()}] ${msg}\n`);
|
|
475
|
-
} catch {}
|
|
476
|
-
}
|
|
477
|
-
/** 错误信息提取(保留 JS 原语义:message 存在用 message,否则用错误对象本身)。 */
|
|
478
|
-
function errMsg(e) {
|
|
479
|
-
return `${e?.message ?? e}`;
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* 逐条处理消息回调参数(onRecvMsg 为消息数组——2026-08-07 运行时实证;
|
|
483
|
-
* 兼容单条对象,过滤无效项)。
|
|
484
|
-
*/
|
|
485
|
-
function forEachRawMessage(msgs, handler) {
|
|
486
|
-
const list = Array.isArray(msgs) ? msgs : [msgs];
|
|
487
|
-
for (const msg of list) {
|
|
488
|
-
if (!msg || typeof msg !== "object") continue;
|
|
489
|
-
handler(msg);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
function createState() {
|
|
493
|
-
return {
|
|
494
|
-
wrapperExports: null,
|
|
495
|
-
qqSession: null,
|
|
496
|
-
qqLoginService: null,
|
|
497
|
-
bootstrapped: false
|
|
498
|
-
};
|
|
499
|
-
}
|
|
500
|
-
//#endregion
|
|
501
524
|
//#region src/host/core/login.ts
|
|
502
525
|
/** 打印可用快速登录账号 + 返回目标账号(启动横幅)。forcedUin(cli `-q` / NAPUTO_QUICK_UIN)优先。 */
|
|
503
526
|
async function pickLoginAccount(kernel, ctx, loginResultRef, forcedUin) {
|
|
@@ -1094,6 +1117,8 @@ const LOGIN_STATES = [
|
|
|
1094
1117
|
function isLoginState(value) {
|
|
1095
1118
|
return LOGIN_STATES.includes(value);
|
|
1096
1119
|
}
|
|
1120
|
+
/** 非 IPC 模式 QR 透出标记行前缀(cli forwardFiltered 解析后终端渲染)。 */
|
|
1121
|
+
const QR_LINE_PREFIX = "NAPUTO_QR ";
|
|
1097
1122
|
/** 登录参数(NAPUTO_QUICK_UIN 强制指定 / ref 目标 / 默认)。 */
|
|
1098
1123
|
function buildLoginOpts(Appid, forcedUin, ref) {
|
|
1099
1124
|
const quickUin = forcedUin ?? ref.targetUin;
|
|
@@ -1102,9 +1127,14 @@ function buildLoginOpts(Appid, forcedUin, ref) {
|
|
|
1102
1127
|
initTimeoutMs: 2e4,
|
|
1103
1128
|
...quickUin !== void 0 ? { quickUin } : {}
|
|
1104
1129
|
};
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
if (
|
|
1130
|
+
const ipcMode = env.NAPUTO_IPC === "1";
|
|
1131
|
+
opts["onLoginProgress"] = (progress) => {
|
|
1132
|
+
if (progress.qr !== void 0) if (ipcMode) sendQr(progress.qr.pngBase64, progress.qr.qrcodeUrl);
|
|
1133
|
+
else node_process.default.stdout.write(`${QR_LINE_PREFIX}${JSON.stringify({
|
|
1134
|
+
pngBase64: progress.qr.pngBase64,
|
|
1135
|
+
qrcodeUrl: progress.qr.qrcodeUrl
|
|
1136
|
+
})}\n`);
|
|
1137
|
+
if (ipcMode && isLoginState(progress.state)) sendLogin(progress.state, progress.selfInfo);
|
|
1108
1138
|
};
|
|
1109
1139
|
return opts;
|
|
1110
1140
|
}
|