magpie-plugin-loomy-auth 0.1.13 → 0.1.15
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/CHANGELOG.md +29 -0
- package/README.md +2 -0
- package/assets/icon.png +0 -0
- package/docs/internals.md +7 -2
- package/index.mjs +3 -15
- package/package.json +51 -44
- package/publish-npm.mjs +40 -0
- package/src/self-register.mjs +0 -234
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# 更新记录
|
|
2
|
+
|
|
3
|
+
> 每个版本一节,**一条一行**,说用户能感知的变化。
|
|
4
|
+
> 格式与发布流程见 [magpie-plugin-template](https://github.com/MYCF711/magpie-plugin-template) 的 [UPDATING.md](https://github.com/MYCF711/magpie-plugin-template/blob/main/UPDATING.md)。
|
|
5
|
+
|
|
6
|
+
## 0.1.15 — 2026-10-27
|
|
7
|
+
|
|
8
|
+
- **插件不再写 magpie 自己的缓存** —— 删掉 `src/self-register.mjs` 与 index 里的调用:`~/.cache/magpie/plugin-market.json` 属于 magpie,每次加载都去改它等于把 registry 条目盖掉
|
|
9
|
+
- **卡片标题与文案改由 registry 条目承担** —— 提交到 `magpie-community/plugins` 的条目是唯一受支持的做法
|
|
10
|
+
- 其余行为(登录、模型、积分、签到)**一行未动**,凭据形状不变,不用重新登录
|
|
11
|
+
|
|
12
|
+
## 0.1.14 — 2026-10-26
|
|
13
|
+
|
|
14
|
+
首次留档。0.1.5–0.1.13 是同一天的连续迭代,没有分开记 —— 下面这些是这个插件**现在**
|
|
15
|
+
给用户的东西:
|
|
16
|
+
|
|
17
|
+
- **两种积分池分开上报** —— 今日池参与路由,永久池只显示不参与;空号不会再以「满额」的姿态排在队首
|
|
18
|
+
- **签到一次做完三件事** —— 刷新今日额度、领 10,000 新手任务积分、兑换邀请码,全部幂等
|
|
19
|
+
- **免费模型按厂商口径判定** —— 不靠「倍率是 0」猜
|
|
20
|
+
- **邀请码只判断「这个号是否已被邀请过」** —— 不做多余判断
|
|
21
|
+
- **256px 图标** —— 卡片上不再是糊的
|
|
22
|
+
- **README 重写** —— 开头给数字、功能一条一行、安装放最后、参考源逐条列出
|
|
23
|
+
- **新增 `CHANGELOG.md`**,并让推 tag 自动生成 GitHub Release
|
|
24
|
+
- **仓库里的 `name` 改成 `Loomy`** —— 卡片标题就是包名(`plugins/package.json` 的依赖键由包内 `name` 决定),改这里才永久生效:`add` / `update` 不会再把它重置回 `magpie-plugin-loomy-auth`
|
|
25
|
+
- **新增 `publish-npm.mjs`** —— npm 上的包名仍是 `magpie-plugin-loomy-auth`(registry 不接受含大写的新包名);发布改用 `npm run publish:npm`,别再裸跑 `npm publish`
|
|
26
|
+
|
|
27
|
+
## 更早
|
|
28
|
+
|
|
29
|
+
未留档 —— `git log --oneline` 是唯一的记录。
|
package/README.md
CHANGED
package/assets/icon.png
ADDED
|
Binary file
|
package/docs/internals.md
CHANGED
|
@@ -304,8 +304,13 @@ cd ~/.config/magpie/plugins && ~/.cache/magpie/bun/1.4.4/magpie-bun.exe install
|
|
|
304
304
|
magpie 的「有新版本」检查按**包名**去 npm 查,与仓库属主无关 —— 撞名会让界面提示
|
|
305
305
|
一个**别人的**新版,点「更新」就把别人的包装进来顶掉本插件。加 `-plugin-` 段避开。
|
|
306
306
|
|
|
307
|
-
⚠
|
|
308
|
-
|
|
307
|
+
⚠ **提交给 registry 的条目里 `package` 必须与 `package.json` 的 `name` 一字不差**
|
|
308
|
+
(`magpie-plugin-loomy-auth`),否则市场卡片对不上这个包。
|
|
309
|
+
|
|
310
|
+
⚠ 插件**不写** magpie 的市场缓存(`~/.cache/magpie/plugin-market.json`)——
|
|
311
|
+
那份文件属于 magpie,插件每次加载都去 merge,等于把 registry 条目盖掉。
|
|
312
|
+
0.1.14 及更早曾用 `src/self-register.mjs` 这么干,0.1.15 删除。
|
|
313
|
+
卡片标题与文案的唯一载体是 registry 条目。
|
|
309
314
|
|
|
310
315
|
---
|
|
311
316
|
|
package/index.mjs
CHANGED
|
@@ -73,22 +73,10 @@ import { readDesktopSession, readAllLoomySessions, readAllLoomyAccounts } from "
|
|
|
73
73
|
import { staticModels, liveModels } from "./src/models.mjs"
|
|
74
74
|
import { readUsage, publicUsage, quotaToRemember, clearUsageCache } from "./src/usage.mjs"
|
|
75
75
|
import { chatFetch } from "./src/chat.mjs"
|
|
76
|
-
import { startSelfRegister } from "./src/self-register.mjs"
|
|
77
76
|
|
|
78
77
|
const API_URL = `${INTEGRATION_BASE}/api/v1`
|
|
79
78
|
|
|
80
|
-
const
|
|
81
|
-
package: "magpie-plugin-loomy-auth",
|
|
82
|
-
name: PROVIDER_NAME,
|
|
83
|
-
providers: [PROVIDER],
|
|
84
|
-
community: true,
|
|
85
|
-
summary: {
|
|
86
|
-
en: "Loomy (iFlytek) coding subscription for magpie: desktop-session import / SMS / session sign-in, live model catalog with reasoning efforts, chat with tool calling and images, and the two credit pools (daily + permanent) reported as the host routes them — the daily pool counts, the permanent one is shown aside.",
|
|
87
|
-
zh: "讯飞 Loomy 编程订阅:桌面端登录态导入 / 短信 / 粘贴 session 三种登录,实时模型目录与思考档位,工具调用与图片对话,以及分池上报的积分(今日池参与路由、永久池仅展示)—— 修掉「退化账号显示剩余 100%」这类占比缺陷。",
|
|
88
|
-
},
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export const LoomyAuthPlugin = async ({ client, directory } = {}) => {
|
|
79
|
+
export const LoomyAuthPlugin = async ({ client } = {}) => {
|
|
92
80
|
const log = (message, level = "info") => {
|
|
93
81
|
try {
|
|
94
82
|
client?.app?.log?.({ service: "loomy", level, message })
|
|
@@ -97,8 +85,8 @@ export const LoomyAuthPlugin = async ({ client, directory } = {}) => {
|
|
|
97
85
|
}
|
|
98
86
|
}
|
|
99
87
|
|
|
100
|
-
//
|
|
101
|
-
|
|
88
|
+
// 卡片标题与文案不在这里做 —— 那是 registry 条目的职责(registry-entry.json)。
|
|
89
|
+
// 插件不写 magpie 自己的缓存目录:那份文件属于 magpie,插件只读不写。
|
|
102
90
|
|
|
103
91
|
// 目录进程内缓存:session -> { at, models }
|
|
104
92
|
const catalog = new Map()
|
package/package.json
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "magpie-plugin-loomy-auth",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Loomy (iFlytek) for magpie: desktop-session import / SMS / session sign-in, live model catalog with reasoning efforts, chat with tools and images, and the two credit pools reported as the host routes them — the daily pool counts, the permanent one is shown aside.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./index.mjs",
|
|
7
|
-
"exports": "./index.mjs",
|
|
8
|
-
"files": [
|
|
9
|
-
"index.mjs",
|
|
10
|
-
"src",
|
|
11
|
-
"docs",
|
|
12
|
-
"README.md",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
},
|
|
43
|
-
"
|
|
44
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "magpie-plugin-loomy-auth",
|
|
3
|
+
"version": "0.1.15",
|
|
4
|
+
"description": "Loomy (iFlytek) for magpie: desktop-session import / SMS / session sign-in, live model catalog with reasoning efforts, chat with tools and images, and the two credit pools reported as the host routes them — the daily pool counts, the permanent one is shown aside.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.mjs",
|
|
7
|
+
"exports": "./index.mjs",
|
|
8
|
+
"files": [
|
|
9
|
+
"index.mjs",
|
|
10
|
+
"src",
|
|
11
|
+
"docs",
|
|
12
|
+
"README.md",
|
|
13
|
+
"CHANGELOG.md",
|
|
14
|
+
"NOTICE.md",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"assets",
|
|
17
|
+
"publish-npm.mjs"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [
|
|
20
|
+
"magpie",
|
|
21
|
+
"provider",
|
|
22
|
+
"auth",
|
|
23
|
+
"magpie-plugin",
|
|
24
|
+
"opencode",
|
|
25
|
+
"opencode-plugin",
|
|
26
|
+
"opencode-auth",
|
|
27
|
+
"loomy",
|
|
28
|
+
"xunfei",
|
|
29
|
+
"iflytek"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/MYCF711/magpie-loomy-auth.git"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node --test tests/",
|
|
41
|
+
"publish:npm": "node publish-npm.mjs"
|
|
42
|
+
},
|
|
43
|
+
"magpie": {
|
|
44
|
+
"icon": "https://raw.githubusercontent.com/MYCF711/magpie-loomy-auth/main/assets/icon.png",
|
|
45
|
+
"maxConcurrency": 4
|
|
46
|
+
},
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/MYCF711/magpie-loomy-auth/issues"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/MYCF711/magpie-loomy-auth#readme"
|
|
51
|
+
}
|
package/publish-npm.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// 发布到 npm 的包装器 —— 用 `npm run publish:npm`,不要裸跑 `npm publish`。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要有它:两个读者开的是**同一个** package.json。
|
|
4
|
+
//
|
|
5
|
+
// bun(magpie 的 git 安装)读它 → 决定插件卡片上的标题(依赖键 = name)
|
|
6
|
+
// npm publish 读它 → 决定包名
|
|
7
|
+
//
|
|
8
|
+
// npm 不允许**新**包名含大写(registry 会拒:name can no longer contain capital
|
|
9
|
+
// letters),而卡片标题想要供应商自己的写法。所以发布这一下把 name 换成 npm 上的
|
|
10
|
+
// 包名,发完立刻还原 —— 仓库里永远留着好看的那个名字。
|
|
11
|
+
//
|
|
12
|
+
// ⚠ 从此不要裸跑 `npm publish`:那会拿 "Loomy" 去发,被 registry 拒绝。
|
|
13
|
+
// ⚠ 机制说明见 magpie-plugin-template 的 UPDATING.md。
|
|
14
|
+
|
|
15
|
+
import fs from "node:fs"
|
|
16
|
+
import { fileURLToPath } from "node:url"
|
|
17
|
+
import { execSync } from "node:child_process"
|
|
18
|
+
|
|
19
|
+
/** npm 上的包名。与仓库里的 name 不同是**故意的** —— 已装的用户按这个名收更新。 */
|
|
20
|
+
const PUBLISH_AS = "magpie-plugin-loomy-auth"
|
|
21
|
+
|
|
22
|
+
const FILE = fileURLToPath(new URL("./package.json", import.meta.url))
|
|
23
|
+
|
|
24
|
+
const orig = fs.readFileSync(FILE, "utf8")
|
|
25
|
+
const pkg = JSON.parse(orig)
|
|
26
|
+
|
|
27
|
+
if (pkg.name === PUBLISH_AS) {
|
|
28
|
+
console.error(`package.json 的 name 已经是 ${PUBLISH_AS} —— 没有可换的,停。`)
|
|
29
|
+
process.exit(1)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
pkg.name = PUBLISH_AS
|
|
33
|
+
fs.writeFileSync(FILE, JSON.stringify(pkg, null, 2) + "\n")
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
execSync(process.argv[2] || "npm publish", { stdio: "inherit" })
|
|
37
|
+
} finally {
|
|
38
|
+
fs.writeFileSync(FILE, orig)
|
|
39
|
+
console.log(`name 已还原为 ${JSON.parse(orig).name}`)
|
|
40
|
+
}
|
package/src/self-register.mjs
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
// 自注册:把本插件写进 magpie 的市场缓存
|
|
2
|
-
//
|
|
3
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
4
|
-
// 问题
|
|
5
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
6
|
-
//
|
|
7
|
-
// magpie 的插件卡片上,「源码」按钮和卡片标题来自
|
|
8
|
-
// ~/.cache/magpie/plugin-market.json —— magpie 的社区插件列表缓存。
|
|
9
|
-
//
|
|
10
|
-
// 包不在那个列表里 ⇒ 卡片标题退回包名,没有源码按钮。
|
|
11
|
-
//
|
|
12
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
13
|
-
// 做法:攒一批,合并去重,写一次
|
|
14
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
15
|
-
//
|
|
16
|
-
// magpie 把**所有插件加载进同一个 Bun 进程**(它自己的文档写
|
|
17
|
-
// "one Bun process, the plugin host")。所以插件之间可以通过
|
|
18
|
-
// globalThis 共享一个队列。
|
|
19
|
-
//
|
|
20
|
-
// 插件 A 加载 → 入队
|
|
21
|
-
// 插件 B 加载 → 入队
|
|
22
|
-
// ... ↓ 100ms 窗口
|
|
23
|
-
// 合并去重 → 一次写入
|
|
24
|
-
//
|
|
25
|
-
// 实测:10 个插件同时启动,**只写盘 1 次**,全部存活,官方条目不动。
|
|
26
|
-
//
|
|
27
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
28
|
-
// 只写一次,绝不轮询
|
|
29
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
30
|
-
//
|
|
31
|
-
// 实测(2026-10-25):
|
|
32
|
-
//
|
|
33
|
-
// 手工往缓存里加一条 → 等 10 秒 → 没被覆盖
|
|
34
|
-
// 再跑 plugin update → 也没被覆盖
|
|
35
|
-
// 连续观察 3 分钟 → mtime 零变化
|
|
36
|
-
//
|
|
37
|
-
// magpie 只在启动时重写这个文件,静默运行期间完全不碰。
|
|
38
|
-
//
|
|
39
|
-
// ⚠⚠ 绝对不要用 fs.watch 或 setInterval 去「补」。
|
|
40
|
-
// 我试过:我们写文件(rename)会触发自己的 watch,watch 又写,
|
|
41
|
-
// 每秒 3-4 次,永不停止(实测 60 秒内 mtime 变化 19 次)。
|
|
42
|
-
// 删掉后 60 秒变化 0 次。
|
|
43
|
-
//
|
|
44
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
45
|
-
|
|
46
|
-
import fs from "node:fs"
|
|
47
|
-
import path from "node:path"
|
|
48
|
-
import os from "node:os"
|
|
49
|
-
|
|
50
|
-
/** 官方 registry 的地址(缓存丢了时的兜底)*/
|
|
51
|
-
const OFFICIAL_REGISTRY =
|
|
52
|
-
"https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json"
|
|
53
|
-
|
|
54
|
-
/** 队列的共享键(Symbol.for 保证同一进程内所有插件拿到同一个)*/
|
|
55
|
-
const QUEUE_KEY = Symbol.for("magpie.plugin-template.registry-queue")
|
|
56
|
-
|
|
57
|
-
/** 攒批窗口。够短(卡片不卡),够长(同期插件都能进来)*/
|
|
58
|
-
const FLUSH_MS = 100
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 从 magpie 的配置目录推出它的缓存目录。
|
|
62
|
-
*
|
|
63
|
-
* 插件的 directory 是 magpie 的 config folder(~/.config/magpie)。
|
|
64
|
-
* XDG 规则下 cache 和 config 平级。
|
|
65
|
-
*/
|
|
66
|
-
export function cacheDirOf(configDir) {
|
|
67
|
-
if (configDir && typeof configDir === "string") {
|
|
68
|
-
const cfg = path.resolve(configDir)
|
|
69
|
-
const guess = path.join(path.dirname(path.dirname(cfg)), ".cache", "magpie")
|
|
70
|
-
if (fs.existsSync(guess)) return guess
|
|
71
|
-
if (process.platform === "darwin") {
|
|
72
|
-
const mac = path.join(os.homedir(), "Library", "Caches", "magpie")
|
|
73
|
-
if (fs.existsSync(mac)) return mac
|
|
74
|
-
}
|
|
75
|
-
const xdg = process.env.XDG_CACHE_HOME
|
|
76
|
-
if (xdg) return path.join(xdg, "magpie")
|
|
77
|
-
return guess
|
|
78
|
-
}
|
|
79
|
-
const xdg = process.env.XDG_CACHE_HOME
|
|
80
|
-
if (xdg) return path.join(xdg, "magpie")
|
|
81
|
-
return path.join(os.homedir(), ".cache", "magpie")
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** 取(或创建)共享队列 */
|
|
85
|
-
function queue(configDir) {
|
|
86
|
-
if (!globalThis[QUEUE_KEY]) {
|
|
87
|
-
globalThis[QUEUE_KEY] = {
|
|
88
|
-
/** package → entry,按包名天然去重 */
|
|
89
|
-
entries: new Map(),
|
|
90
|
-
timer: null,
|
|
91
|
-
configDir,
|
|
92
|
-
log: () => {},
|
|
93
|
-
writes: 0,
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
const q = globalThis[QUEUE_KEY]
|
|
97
|
-
if (configDir) q.configDir = configDir
|
|
98
|
-
return q
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 把一个条目加入队列。
|
|
103
|
-
*
|
|
104
|
-
* 同一个包名重复入队只保留最后一次 —— 这就是去重。
|
|
105
|
-
*
|
|
106
|
-
* @param {object} entry { package, name, providers?, community?, summary }
|
|
107
|
-
* @param {{configDir?: string, log?: Function, timeoutMs?: number}} [opts]
|
|
108
|
-
*/
|
|
109
|
-
export function registerSelf(entry, opts = {}) {
|
|
110
|
-
if (!entry || typeof entry.package !== "string" || entry.package.length === 0) {
|
|
111
|
-
return Promise.resolve({ ok: false, reason: "entry.package 缺失" })
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const q = queue(opts.configDir)
|
|
115
|
-
if (typeof opts.log === "function") q.log = opts.log
|
|
116
|
-
|
|
117
|
-
q.entries.set(entry.package, entry)
|
|
118
|
-
|
|
119
|
-
if (q.timer) return Promise.resolve({ ok: true, queued: true })
|
|
120
|
-
|
|
121
|
-
return new Promise((resolve) => {
|
|
122
|
-
q.timer = setTimeout(() => {
|
|
123
|
-
q.timer = null
|
|
124
|
-
const r = flush(q, opts.timeoutMs)
|
|
125
|
-
resolve(r)
|
|
126
|
-
}, FLUSH_MS)
|
|
127
|
-
if (typeof q.timer.unref === "function") q.timer.unref()
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* 一次读、一次合并、一次写。
|
|
133
|
-
*
|
|
134
|
-
* 队列里的所有条目(可能来自多个插件)在这个函数里一起进缓存,
|
|
135
|
-
* 所以并发插件之间不存在「你覆盖我」的窗口。
|
|
136
|
-
*/
|
|
137
|
-
function flush(q, timeoutMs = 15_000) {
|
|
138
|
-
const log = q.log
|
|
139
|
-
if (q.entries.size === 0) return { ok: true, changed: false }
|
|
140
|
-
|
|
141
|
-
const cacheDir = cacheDirOf(q.configDir)
|
|
142
|
-
const marketPath = path.join(cacheDir, "plugin-market.json")
|
|
143
|
-
|
|
144
|
-
try {
|
|
145
|
-
let current = null
|
|
146
|
-
if (fs.existsSync(marketPath)) {
|
|
147
|
-
try {
|
|
148
|
-
current = JSON.parse(fs.readFileSync(marketPath, "utf8"))
|
|
149
|
-
} catch {
|
|
150
|
-
current = null
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (!current || !Array.isArray(current.plugins)) {
|
|
155
|
-
// 缓存不可用就跳过(不阻塞插件加载)。下次启动还会再试。
|
|
156
|
-
const names = [...q.entries.keys()].join(", ")
|
|
157
|
-
log(`市场缓存不可用,跳过注册:${names}`)
|
|
158
|
-
q.entries.clear()
|
|
159
|
-
return { ok: false, reason: "市场缓存不可用" }
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const snapshot = JSON.stringify(current, null, 2)
|
|
163
|
-
const added = []
|
|
164
|
-
const updated = []
|
|
165
|
-
|
|
166
|
-
// 按包名合并:有就原地更新,没有才追加
|
|
167
|
-
for (const [pkg, entry] of q.entries) {
|
|
168
|
-
const idx = current.plugins.findIndex((x) => x?.package === pkg)
|
|
169
|
-
if (idx >= 0) {
|
|
170
|
-
current.plugins[idx] = entry
|
|
171
|
-
updated.push(pkg)
|
|
172
|
-
} else {
|
|
173
|
-
current.plugins.push(entry)
|
|
174
|
-
added.push(pkg)
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
q.entries.clear()
|
|
178
|
-
|
|
179
|
-
const next = JSON.stringify(current, null, 2)
|
|
180
|
-
if (snapshot === next) {
|
|
181
|
-
return { ok: true, changed: false, total: current.plugins.length }
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
fs.mkdirSync(cacheDir, { recursive: true })
|
|
185
|
-
const tmp = `${marketPath}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 6)}`
|
|
186
|
-
fs.writeFileSync(tmp, next, "utf8")
|
|
187
|
-
fs.renameSync(tmp, marketPath)
|
|
188
|
-
q.writes++
|
|
189
|
-
|
|
190
|
-
const parts = []
|
|
191
|
-
if (added.length) parts.push(`新增 ${added.length}`)
|
|
192
|
-
if (updated.length) parts.push(`更新 ${updated.length}`)
|
|
193
|
-
log(`已注册 ${parts.join(",")}(共 ${current.plugins.length} 条,这是第 ${q.writes} 次写入)`)
|
|
194
|
-
|
|
195
|
-
return {
|
|
196
|
-
ok: true,
|
|
197
|
-
changed: true,
|
|
198
|
-
total: current.plugins.length,
|
|
199
|
-
added,
|
|
200
|
-
updated,
|
|
201
|
-
}
|
|
202
|
-
} catch (e) {
|
|
203
|
-
const msg = e instanceof Error ? e.message : String(e)
|
|
204
|
-
log(`自注册失败:${msg}`)
|
|
205
|
-
q.entries.clear()
|
|
206
|
-
return { ok: false, reason: msg }
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* 启动自注册。
|
|
212
|
-
*
|
|
213
|
-
* 一次调用只入队一次。同期的其它插件会在同一个窗口里被合并,
|
|
214
|
-
* 一起写进缓存 —— 不管有多少个插件,写入次数都接近 1。
|
|
215
|
-
*
|
|
216
|
-
* @param {object} entry
|
|
217
|
-
* @param {{configDir?: string, log?: Function}} [opts]
|
|
218
|
-
*/
|
|
219
|
-
export function startSelfRegister(entry, opts = {}) {
|
|
220
|
-
registerSelf(entry, opts).catch(() => {})
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** 供测试:立刻清空队列(不影响真实使用)*/
|
|
224
|
-
export function _flushNow() {
|
|
225
|
-
const q = globalThis[QUEUE_KEY]
|
|
226
|
-
if (!q) return { ok: true, changed: false }
|
|
227
|
-
if (q.timer) {
|
|
228
|
-
clearTimeout(q.timer)
|
|
229
|
-
q.timer = null
|
|
230
|
-
}
|
|
231
|
-
return flush(q)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export const _internal = { cacheDirOf, OFFICIAL_REGISTRY, QUEUE_KEY, FLUSH_MS }
|