@fish-under-sea/magpie-dragonapi 0.1.5 → 0.2.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/lib/store.mjs CHANGED
@@ -9,7 +9,7 @@
9
9
  // 因为每次建钥匙都要用它,而 magpie 的 auth 表存的是供应商凭据不是这个。
10
10
  // PAT 只写在这个 0600 的文件里,不写日志、不回显完整值。
11
11
 
12
- import { mkdirSync, readFileSync, renameSync, writeFileSync, chmodSync, existsSync } from 'node:fs'
12
+ import { mkdirSync, readFileSync, renameSync, writeFileSync, chmodSync, existsSync, rmSync } from 'node:fs'
13
13
  import { dirname, join } from 'node:path'
14
14
 
15
15
  const FILE_NAME = 'dragonapi-state.json'
@@ -21,6 +21,8 @@ function emptyState() {
21
21
  pat: '',
22
22
  patHint: '',
23
23
  patScopes: [],
24
+ // PAT 是不是真的被站点验通过。false 表示「存下了,但还没确认能用」。
25
+ patVerified: false,
24
26
  // 已绑定的账号:key 是 magpie 的账号键(provider 或 provider#slot),
25
27
  // 记的是它对应的站点分组与打码钥匙,供页面显示。
26
28
  accounts: {},
@@ -54,19 +56,29 @@ export function loadState(dir) {
54
56
  }
55
57
  }
56
58
 
57
- /** saveState 原子写状态。写失败返回 false,不抛——状态存不下不该让插件停摆。 */
59
+ /**
60
+ * saveState 原子写状态。
61
+ *
62
+ * 返回 `{ saved, path, error }`,**不抛**——状态存不下不该让插件停摆,
63
+ * 但调用方必须能看到这件事:之前它返回一个被所有人忽略的 false,于是
64
+ * 页面在磁盘写失败时照样弹「PAT 已保存」,重启后令牌就没了。
65
+ */
58
66
  export function saveState(dir, state) {
59
67
  const p = statePath(dir)
68
+ // 临时名带 pid 与随机串:两个进程(插件 + 独立服务)同时写同一份状态时,
69
+ // 共用一个临时文件名会让后一次 rename 扑空。
70
+ const tmp = `${p}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 8)}`
60
71
  try {
61
72
  mkdirSync(dirname(p), { recursive: true })
62
- const tmp = p + '.tmp-' + process.pid
63
73
  const body = JSON.stringify({ ...state, updatedAt: Date.now() }, null, 2)
64
74
  writeFileSync(tmp, body, { mode: 0o600 })
65
75
  try { chmodSync(tmp, 0o600) } catch { /* Windows 上 chmod 基本无效,忽略 */ }
66
76
  renameSync(tmp, p)
67
- return true
68
- } catch {
69
- return false
77
+ return { saved: true, path: p, error: '' }
78
+ } catch (e) {
79
+ // 别把半截临时文件留在配置目录里。
80
+ try { if (existsSync(tmp)) rmSync(tmp, { force: true }) } catch { /* 清不掉也不影响返回值 */ }
81
+ return { saved: false, path: p, error: String(e?.message || e) }
70
82
  }
71
83
  }
72
84
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fish-under-sea/magpie-dragonapi",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "DragonAPI for magpie: a provider plugin whose models, groups and prices come from the site's public /api/pricing, with balance and per-call usage from an sk- key, plus a local page that lists the market (read-only), shows usage and can mint new keys for a group.",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
package/serve.mjs CHANGED
@@ -84,14 +84,17 @@ const info = await startService({
84
84
  pat: state.pat || '',
85
85
  patHint: state.patHint || '',
86
86
  patScopes: state.patScopes || [],
87
+ patVerified: state.patVerified === true,
87
88
  enabledGroups: state.enabledGroups || [],
88
89
  }),
89
- setPat: async ({ pat, hint, scopes, user }) => {
90
+ setPat: async ({ pat, hint, scopes, user, verified }) => {
90
91
  state.pat = pat
91
92
  state.patHint = hint
92
93
  state.patScopes = scopes
94
+ state.patVerified = verified === true
93
95
  if (user) state.user = { id: user.id, name: user.username }
94
- saveState(dir, state)
96
+ // 返回落盘结果,让服务能把「写失败」如实报给页面。
97
+ return saveState(dir, state)
95
98
  },
96
99
  // 单独跑时没有 magpie 的钥匙,余额会显示为 —。
97
100
  keyOf: () => '',