koishi-plugin-oni-sync-bot 0.10.0 → 0.10.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/lib/index.js CHANGED
@@ -250,11 +250,73 @@ async function withRetry(fn, options = {}) {
250
250
  }
251
251
  __name(withRetry, "withRetry");
252
252
 
253
- // src/services/wikiBotService.ts
253
+ // src/services/reloginAwareProxy.ts
254
254
  function isAssertUserFailed(error) {
255
255
  return typeof error === "object" && error !== null && error.code === "assertuserfailed";
256
256
  }
257
257
  __name(isAssertUserFailed, "isAssertUserFailed");
258
+ var METHODS_TO_SKIP = /* @__PURE__ */ new Set(["cookieJar", "setRequestOptions"]);
259
+ function memberOf(bot, prop) {
260
+ return bot[prop];
261
+ }
262
+ __name(memberOf, "memberOf");
263
+ function createReloginAwareProxy(options) {
264
+ const { getBot, relogin, label } = options;
265
+ const invokeWithRelogin = /* @__PURE__ */ __name(async (invoke) => {
266
+ try {
267
+ return await invoke(getBot());
268
+ } catch (error) {
269
+ if (!isAssertUserFailed(error)) throw error;
270
+ await relogin();
271
+ return await invoke(getBot());
272
+ }
273
+ }, "invokeWithRelogin");
274
+ return new Proxy({}, {
275
+ get: /* @__PURE__ */ __name((_target, prop) => {
276
+ const value = memberOf(getBot(), prop);
277
+ if (typeof value !== "function") return value;
278
+ if (typeof prop === "string" && METHODS_TO_SKIP.has(prop)) {
279
+ return value.bind(getBot());
280
+ }
281
+ if (prop === "continuedQueryGen") {
282
+ return (...args) => {
283
+ const createIterator = /* @__PURE__ */ __name((callArgs) => {
284
+ const bot = getBot();
285
+ const generate = memberOf(bot, "continuedQueryGen");
286
+ return generate.apply(bot, callArgs)[Symbol.asyncIterator]();
287
+ }, "createIterator");
288
+ let iterator = createIterator(args);
289
+ return {
290
+ [Symbol.asyncIterator]() {
291
+ return this;
292
+ },
293
+ async next() {
294
+ try {
295
+ return await iterator.next();
296
+ } catch (error) {
297
+ if (!isAssertUserFailed(error)) throw error;
298
+ await relogin();
299
+ iterator = createIterator(args);
300
+ return await iterator.next();
301
+ }
302
+ }
303
+ };
304
+ };
305
+ }
306
+ return (...args) => invokeWithRelogin((bot) => {
307
+ const method = memberOf(bot, prop);
308
+ if (typeof method !== "function") {
309
+ throw new Error(`${label} bot 缺少方法 ${String(prop)}`);
310
+ }
311
+ const call = method;
312
+ return call.apply(bot, args);
313
+ });
314
+ }, "get")
315
+ });
316
+ }
317
+ __name(createReloginAwareProxy, "createReloginAwareProxy");
318
+
319
+ // src/services/wikiBotService.ts
258
320
  var WikiBotService = class _WikiBotService extends import_koishi4.Service {
259
321
  static {
260
322
  __name(this, "WikiBotService");
@@ -419,78 +481,18 @@ var WikiBotService = class _WikiBotService extends import_koishi4.Service {
419
481
  isBWikiBotReady() {
420
482
  return this.botOf("bwiki") !== null;
421
483
  }
422
- /**
423
- * 生成一个"登录态永远有效"的客户端代理。
424
- *
425
- * 设计说明(为什么必须是代理而不是普通对象):
426
- * Mwn 的方法很多,且调用方需要 `continuedQueryGen` 这类**异步生成器**以及
427
- * `options` 这类**非函数属性**。用代理可以完整转发任意成员,同时把
428
- * "assert=user 失败 → 重新登录 → 重放调用" 这件事对调用方完全隐藏。
429
- */
430
484
  createBotProxy(site) {
431
485
  const label = _WikiBotService.siteLabel(site);
432
- const METHODS_TO_SKIP = /* @__PURE__ */ new Set(["cookieJar", "setRequestOptions"]);
433
- const getCurrentBot = /* @__PURE__ */ __name(() => {
434
- const bot = this.botOf(site);
435
- if (!bot) {
436
- throw new Error(`${label} bot 尚未就绪,请检查登录配置或查看日志`);
437
- }
438
- return bot;
439
- }, "getCurrentBot");
440
- const memberOf = /* @__PURE__ */ __name((bot, prop) => bot[prop], "memberOf");
441
- const invokeWithRelogin = /* @__PURE__ */ __name(async (invoke) => {
442
- try {
443
- return await invoke(getCurrentBot());
444
- } catch (error) {
445
- if (!isAssertUserFailed(error)) throw error;
446
- await this.reloginSite(site);
447
- return await invoke(getCurrentBot());
448
- }
449
- }, "invokeWithRelogin");
450
- return new Proxy({}, {
451
- get: /* @__PURE__ */ __name((_target, prop) => {
452
- const value = memberOf(getCurrentBot(), prop);
453
- if (typeof value !== "function") return value;
454
- if (typeof prop === "string" && METHODS_TO_SKIP.has(prop)) {
455
- return value.bind(this.botOf(site));
486
+ return createReloginAwareProxy({
487
+ getBot: /* @__PURE__ */ __name(() => {
488
+ const bot = this.botOf(site);
489
+ if (!bot) {
490
+ throw new Error(`${label} bot 尚未就绪,请检查登录配置或查看日志`);
456
491
  }
457
- if (prop === "continuedQueryGen") {
458
- return (...args) => {
459
- const relogin = /* @__PURE__ */ __name(() => this.reloginSite(site), "relogin");
460
- const createIterator = /* @__PURE__ */ __name((callArgs) => {
461
- const generate = memberOf(
462
- getCurrentBot(),
463
- "continuedQueryGen"
464
- );
465
- return generate(...callArgs)[Symbol.asyncIterator]();
466
- }, "createIterator");
467
- let iterator = createIterator(args);
468
- return {
469
- [Symbol.asyncIterator]() {
470
- return this;
471
- },
472
- async next() {
473
- try {
474
- return await iterator.next();
475
- } catch (error) {
476
- if (!isAssertUserFailed(error)) throw error;
477
- await relogin();
478
- iterator = createIterator(args);
479
- return await iterator.next();
480
- }
481
- }
482
- };
483
- };
484
- }
485
- return (...args) => invokeWithRelogin((bot) => {
486
- const method = memberOf(bot, prop);
487
- if (typeof method !== "function") {
488
- throw new Error(`${label} bot 缺少方法 ${String(prop)}`);
489
- }
490
- const call = method;
491
- return call.apply(bot, args);
492
- });
493
- }, "get")
492
+ return bot;
493
+ }, "getBot"),
494
+ relogin: /* @__PURE__ */ __name(() => this.reloginSite(site), "relogin"),
495
+ label
494
496
  });
495
497
  }
496
498
  /** WIKIGG 客户端(透明处理登录过期) */
@@ -0,0 +1,24 @@
1
+ import type { Mwn } from "mwn";
2
+ export interface ReloginAwareProxyOptions {
3
+ /** 取当前 bot;未就绪时抛错 */
4
+ getBot: () => Mwn;
5
+ /** 登录态失效时调用,用于重新登录 */
6
+ relogin: () => Promise<void>;
7
+ /** 出错信息里使用的站点名 */
8
+ label: string;
9
+ }
10
+ /**
11
+ * 生成一个「登录态永远有效」的客户端代理。
12
+ *
13
+ * 为什么必须是代理而不是普通对象:Mwn 的方法很多,调用方还需要
14
+ * `continuedQueryGen` 这类**异步生成器**以及 `options` 这类**非函数属性**。
15
+ * 代理可以完整转发任意成员,同时把「assert=user 失败 → 重新登录 → 重放调用」
16
+ * 这件事对调用方完全隐藏。
17
+ *
18
+ * ⚠️ 转发时**必须保留 `this` 绑定**:一律用 `fn.apply(bot, args)`,
19
+ * 不能把方法先取出来再裸调(`const f = bot.f; f(...)`)。
20
+ * Mwn 的方法内部会访问 `this.request` 等成员,丢失 `this` 会直接抛
21
+ * `Cannot read properties of undefined (reading 'request')`。
22
+ * 该约束由 tests/reloginAwareProxy.spec.ts 覆盖。
23
+ */
24
+ export declare function createReloginAwareProxy(options: ReloginAwareProxyOptions): Mwn;
@@ -53,14 +53,6 @@ export declare class WikiBotService extends Service {
53
53
  get isReady(): boolean;
54
54
  isGGBotReady(): boolean;
55
55
  isBWikiBotReady(): boolean;
56
- /**
57
- * 生成一个"登录态永远有效"的客户端代理。
58
- *
59
- * 设计说明(为什么必须是代理而不是普通对象):
60
- * Mwn 的方法很多,且调用方需要 `continuedQueryGen` 这类**异步生成器**以及
61
- * `options` 这类**非函数属性**。用代理可以完整转发任意成员,同时把
62
- * "assert=user 失败 → 重新登录 → 重放调用" 这件事对调用方完全隐藏。
63
- */
64
56
  private createBotProxy;
65
57
  /** WIKIGG 客户端(透明处理登录过期) */
66
58
  getGGBot(): Mwn;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-oni-sync-bot",
3
3
  "description": "缺氧Wiki站镜像点同步-测试",
4
- "version": "0.10.0",
4
+ "version": "0.10.1",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [