magicrew-auth 0.3.32 → 0.3.33
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/AGENTS.md +3 -1
- package/CHANGELOG.md +63 -0
- package/README.md +1 -1
- package/index.mjs +28 -19
- package/package.json +10 -2
- package/page/app.js +243 -0
- package/page/index.html +53 -0
- package/src/browser-login.mjs +99 -22
- package/src/hooks/loader.mjs +40 -2
- package/src/hooks/methods.mjs +56 -167
- package/src/hooks/usage.mjs +12 -0
- package/src/store.mjs +146 -5
package/src/store.mjs
CHANGED
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
//
|
|
8
8
|
// ⚠ 这不是「状态管理」,是缓存:删掉全部,插件照样能跑(只是要重新登录一次)。
|
|
9
9
|
|
|
10
|
-
import { readFileSync, writeFileSync, mkdirSync, unlinkSync } from "node:fs"
|
|
10
|
+
import { readFileSync, writeFileSync, mkdirSync, unlinkSync, existsSync } from "node:fs"
|
|
11
11
|
import { homedir } from "node:os"
|
|
12
12
|
import { join, dirname } from "node:path"
|
|
13
|
+
import { fileURLToPath } from "node:url"
|
|
13
14
|
import { fetchOwnInviteCode } from "./api.mjs"
|
|
14
15
|
|
|
15
16
|
function cacheDir() {
|
|
@@ -115,11 +116,45 @@ function writeInvite(siteId, code) {
|
|
|
115
116
|
|
|
116
117
|
/** 这次登录该带的邀请码:锁定 → 策略 → 记录 → 作者码 */
|
|
117
118
|
function inviteToUse(siteId, locked = true) {
|
|
119
|
+
const live = liveOrgs(siteId)
|
|
120
|
+
// 账号文件读不到 / 一个号都没有时按「不校验」处理,行为与以前一致
|
|
121
|
+
const known = live !== null && live.size > 0
|
|
118
122
|
if (locked) {
|
|
119
123
|
const pol = readInvitePolicy(siteId)
|
|
120
|
-
if ((pol.mode === "fixed" || pol.mode === "custom") && pol.code)
|
|
124
|
+
if ((pol.mode === "fixed" || pol.mode === "custom") && pol.code) {
|
|
125
|
+
// fixed 挂在某个账号上 —— 账号删了码就别再带(新号会注册落空)
|
|
126
|
+
if (!(known && pol.mode === "fixed" && pol.org && !live.has(pol.org))) return pol.code
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const rolling = readInvite(siteId)
|
|
130
|
+
if (rolling) {
|
|
131
|
+
if (!known) return rolling
|
|
132
|
+
// 滚动码也可能属于已删除的账号:按码反查归属,归属没了就换作者码兜底
|
|
133
|
+
const codes = readAccountCodes(siteId)
|
|
134
|
+
const owner = Object.keys(codes).find((o) => codes[o]?.code === rolling)
|
|
135
|
+
if (!owner || live.has(owner)) return rolling
|
|
136
|
+
}
|
|
137
|
+
return AUTHOR_INVITE[siteId] || ""
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* magpie 账号文件里这个站点还登录着的 org(只读 plugin-auth.json)。
|
|
142
|
+
* 返回 null 表示读不到 —— 调用方应按「不校验」处理,而不是当空集。
|
|
143
|
+
*/
|
|
144
|
+
export function liveOrgs(siteId) {
|
|
145
|
+
try {
|
|
146
|
+
const raw = JSON.parse(readFileSync(join(homedir(), ".config", "magpie", "plugin-auth.json"), "utf8"))
|
|
147
|
+
const out = new Set()
|
|
148
|
+
for (const [k, v] of Object.entries(raw || {})) {
|
|
149
|
+
if (k === siteId || k.startsWith(siteId + "#")) {
|
|
150
|
+
const org = String(v?.org || "").trim()
|
|
151
|
+
if (org) out.add(org)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return out
|
|
155
|
+
} catch {
|
|
156
|
+
return null
|
|
121
157
|
}
|
|
122
|
-
return readInvite(siteId) || AUTHOR_INVITE[siteId] || ""
|
|
123
158
|
}
|
|
124
159
|
|
|
125
160
|
/** 作者自己的码(「我的」那一项) */
|
|
@@ -151,10 +186,12 @@ export function readInvitePolicy(siteId) {
|
|
|
151
186
|
try {
|
|
152
187
|
const v = JSON.parse(readFileSync(invitePolicyPath(siteId), "utf8"))
|
|
153
188
|
const mode = ["auto", "fixed", "custom"].includes(v?.mode) ? v.mode : "auto"
|
|
189
|
+
const org = String(v?.org || "").trim()
|
|
154
190
|
return {
|
|
155
191
|
mode,
|
|
156
192
|
code: String(v?.code || "").trim(),
|
|
157
|
-
org:
|
|
193
|
+
// 旧版把作者码存成 org:"author";作者不是账号,按「无 org」处理
|
|
194
|
+
org: org === "author" ? "" : org,
|
|
158
195
|
accountId: String(v?.accountId || "").trim(),
|
|
159
196
|
at: String(v?.at || ""),
|
|
160
197
|
}
|
|
@@ -173,11 +210,12 @@ export function writeInvitePolicy(siteId, policy) {
|
|
|
173
210
|
JSON.stringify({
|
|
174
211
|
mode,
|
|
175
212
|
code: String(policy?.code || "").trim(),
|
|
176
|
-
org: String(policy?.org || "").trim(),
|
|
213
|
+
org: String(policy?.org || "").trim() === "author" ? "" : String(policy?.org || "").trim(),
|
|
177
214
|
accountId: String(policy?.accountId || "").trim(),
|
|
178
215
|
at: new Date().toISOString(),
|
|
179
216
|
}),
|
|
180
217
|
)
|
|
218
|
+
syncInvitePage() // 页面上的「当前」跟着刷新
|
|
181
219
|
} catch {}
|
|
182
220
|
}
|
|
183
221
|
|
|
@@ -217,6 +255,109 @@ export function noteAccountCode(siteId, org, code, accountId) {
|
|
|
217
255
|
const f = inviteCodesPath(siteId)
|
|
218
256
|
mkdirSync(dirname(f), { recursive: true })
|
|
219
257
|
writeFileSync(f, JSON.stringify(all))
|
|
258
|
+
syncInvitePage() // 页面清单跟着刷新
|
|
259
|
+
} catch {}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 把「有哪些账号、各自的邀请码、当前策略」写进 page/codes.js。
|
|
264
|
+
*
|
|
265
|
+
* 插件页面是沙箱:没有 fetch、读不到磁盘,也读不到账号(magpie 只给它 usage / providers)。
|
|
266
|
+
* 所以清单靠这里预先生成一份静态脚本,页面 <script src="codes.js"> 直接读 —— 与
|
|
267
|
+
* agent-purge 的 gen-personas.mjs 是同一个套路。
|
|
268
|
+
*/
|
|
269
|
+
/**
|
|
270
|
+
* org → 便于辨认的名字(打码手机号 / 邮箱),从 magpie 自己的账号文件**只读**。
|
|
271
|
+
* 页面要显示「178****697(IV…)」,而 rollInvite 只拿得到 org,所以在这里补。
|
|
272
|
+
*/
|
|
273
|
+
function readAccountLabels() {
|
|
274
|
+
const out = {}
|
|
275
|
+
try {
|
|
276
|
+
const raw = JSON.parse(readFileSync(join(homedir(), ".config", "magpie", "plugin-auth.json"), "utf8"))
|
|
277
|
+
for (const v of Object.values(raw || {})) {
|
|
278
|
+
const org = String(v?.org || "")
|
|
279
|
+
const id = String(v?.accountId || "")
|
|
280
|
+
if (org && id && !out[org]) out[org] = id
|
|
281
|
+
}
|
|
282
|
+
} catch {}
|
|
283
|
+
return out
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 删除账号后的确定性清理:码缓存去掉死号;滚动码若属死号一并清掉
|
|
288
|
+
* (不清掉的话,缓存一清 inviteToUse 就查不到归属,反判成「归属不明」保留)。
|
|
289
|
+
*
|
|
290
|
+
* ⚠ 新号竞态保护(2026-10-29 实测复现):注册登录时 rollInvite 先记码,
|
|
291
|
+
* magpie 把新号写进 plugin-auth.json 在**之后** —— 此刻 liveOrgs 看不到新号,
|
|
292
|
+
* 直接清理会把刚记的码当死号删掉,页面要等下次登录/签到才有它。
|
|
293
|
+
* 所以 **10 分钟内记录的条目不参与清理**;这窗口内 inviteToUse 的死号门
|
|
294
|
+
* (owner 死→回落)仍然兜底。
|
|
295
|
+
* 账号文件读不到(liveOrgs === null)时不动,避免误清。
|
|
296
|
+
*/
|
|
297
|
+
const FRESH_ACCOUNT_MS = 10 * 60_000
|
|
298
|
+
|
|
299
|
+
export function pruneDeadAccounts(siteId) {
|
|
300
|
+
const live = liveOrgs(siteId)
|
|
301
|
+
if (!live || !live.size) return null
|
|
302
|
+
const codes = readAccountCodes(siteId)
|
|
303
|
+
const fresh = (v) => {
|
|
304
|
+
try {
|
|
305
|
+
return Date.now() - Date.parse(v?.at || "") < FRESH_ACCOUNT_MS
|
|
306
|
+
} catch {
|
|
307
|
+
return false
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const dead = Object.keys(codes).filter((org) => !live.has(org) && !fresh(codes[org]))
|
|
311
|
+
if (!dead.length) return { dead: [], rollingCleared: false }
|
|
312
|
+
let rollingCleared = false
|
|
313
|
+
const rollingNow = readInvite(siteId)
|
|
314
|
+
if (rollingNow && dead.some((org) => codes[org]?.code === rollingNow)) {
|
|
315
|
+
try {
|
|
316
|
+
unlinkSync(invitePath(siteId))
|
|
317
|
+
rollingCleared = true
|
|
318
|
+
} catch {}
|
|
319
|
+
}
|
|
320
|
+
const kept = { ...codes }
|
|
321
|
+
for (const org of dead) delete kept[org]
|
|
322
|
+
try {
|
|
323
|
+
const f = inviteCodesPath(siteId)
|
|
324
|
+
mkdirSync(dirname(f), { recursive: true })
|
|
325
|
+
writeFileSync(f, JSON.stringify(kept))
|
|
326
|
+
} catch {}
|
|
327
|
+
return { dead, rollingCleared }
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function syncInvitePage({ root: target } = {}) {
|
|
331
|
+
try {
|
|
332
|
+
const root = target || dirname(dirname(fileURLToPath(import.meta.url)))
|
|
333
|
+
// 仓库里的测试/开发不生成(否则 page/codes.js 会脏工作区);发布包装有 .git 才写。
|
|
334
|
+
// 显式传 root(测试)时不受这个保护限制。
|
|
335
|
+
if (!target && (process.env.MAGICREW_NO_PAGE_SYNC === "1" || existsSync(join(root, ".git")))) return
|
|
336
|
+
const labels = readAccountLabels()
|
|
337
|
+
const sites = {}
|
|
338
|
+
for (const site of ["magicrew", "magicrew-intl"]) {
|
|
339
|
+
const live = liveOrgs(site)
|
|
340
|
+
pruneDeadAccounts(site)
|
|
341
|
+
let codes = readAccountCodes(site)
|
|
342
|
+
for (const [org, v] of Object.entries(codes)) {
|
|
343
|
+
if (v && !v.accountId && labels[org]) v.accountId = labels[org]
|
|
344
|
+
}
|
|
345
|
+
const pol = readInvitePolicy(site)
|
|
346
|
+
const rolling = readInvite(site)
|
|
347
|
+
const ownerOf = (code) => Object.keys(codes).find((o) => codes[o]?.code === code) || ""
|
|
348
|
+
sites[site] = {
|
|
349
|
+
codes,
|
|
350
|
+
policy: pol,
|
|
351
|
+
// 当前选择指向已删除的账号 —— 页面据此提示,登录侧已自动回退
|
|
352
|
+
policyStale: Boolean(live && live.size && pol.mode === "fixed" && pol.org && !live.has(pol.org)),
|
|
353
|
+
rolling,
|
|
354
|
+
rollingStale: Boolean(live && live.size && rolling && ownerOf(rolling) && !live.has(ownerOf(rolling))),
|
|
355
|
+
author: AUTHOR_INVITE[site] || "",
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const f = join(root, "page", "codes.js")
|
|
359
|
+
mkdirSync(dirname(f), { recursive: true })
|
|
360
|
+
writeFileSync(f, "window.MAGICREW_CODES = " + JSON.stringify({ at: new Date().toISOString(), sites }) + ";\n")
|
|
220
361
|
} catch {}
|
|
221
362
|
}
|
|
222
363
|
|