dsh-plugin-cyrene-live2d 0.2.9 → 0.3.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/README.md +8 -2
- package/assets/default-prefs.json +8 -8
- package/assets/icon.svg +89 -89
- package/assets/locale/notes/en.json +6 -6
- package/assets/locale/notes/zh-CN.json +6 -6
- package/assets/locale/reminder/en.json +6 -6
- package/assets/locale/reminder/zh-CN.json +6 -6
- package/assets/model/Cyrene.model3.json +441 -441
- package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
- package/assets/model/bongo-standard/cat.cdi3.json +296 -296
- package/assets/model/bongo-standard/cat.model3.json +33 -33
- package/assets/model/bongo-standard/cat.physics3.json +232 -232
- package/assets/model/expressions/0.exp3.json +24 -24
- package/assets/model/expressions/1.exp3.json +11 -11
- package/assets/model/expressions/10.exp3.json +9 -9
- package/assets/model/expressions/11.exp3.json +9 -9
- package/assets/model/expressions/2.exp3.json +9 -9
- package/assets/model/expressions/3.exp3.json +9 -9
- package/assets/model/expressions/4.exp3.json +11 -11
- package/assets/model/expressions/5.exp3.json +11 -11
- package/assets/model/expressions/6.exp3.json +11 -11
- package/assets/model/expressions/7.exp3.json +11 -11
- package/assets/model/expressions/8.exp3.json +11 -11
- package/assets/model/expressions/9.exp3.json +9 -9
- package/assets/model/model.physics3.json +641 -641
- package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
- package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
- package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
- package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
- package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
- package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
- package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
- package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
- package/assets/model/we-q/model0.model3.json +245 -245
- package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
- package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
- package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
- package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
- package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
- package/assets/runtime/live2dcubismcore.min.js +8 -8
- package/assets/stickers.json +84 -84
- package/assets/vendor/cubism4.min.js +1 -1
- package/assets/vendor/pixi.min.js +9 -9
- package/assets/vendor/theme-cyrene-factory.js +4314 -4314
- package/assets/vendor/theme-cyrene.js +4317 -4317
- package/assets/voice/angry-00.opus +0 -0
- package/assets/voice/angry-02.opus +0 -0
- package/assets/voice/approval-00.opus +0 -0
- package/assets/voice/approval-01.opus +0 -0
- package/assets/voice/approval-02.opus +0 -0
- package/assets/voice/askAck-00.opus +0 -0
- package/assets/voice/away-01.opus +0 -0
- package/assets/voice/away-02.opus +0 -0
- package/assets/voice/away-03.opus +0 -0
- package/assets/voice/away-05.opus +0 -0
- package/assets/voice/back-00.opus +0 -0
- package/assets/voice/back-01.opus +0 -0
- package/assets/voice/backLong-05.opus +0 -0
- package/assets/voice/backShort-03.opus +0 -0
- package/assets/voice/backShort-04.opus +0 -0
- package/assets/voice/backShort-07.opus +0 -0
- package/assets/voice/backVeryLong-01.opus +0 -0
- package/assets/voice/backVeryLong-02.opus +0 -0
- package/assets/voice/backVeryLong-03.opus +0 -0
- package/assets/voice/backVeryLong-04.opus +0 -0
- package/assets/voice/backVeryLong-05.opus +0 -0
- package/assets/voice/backVeryLong-07.opus +0 -0
- package/assets/voice/backVeryLong-08.opus +0 -0
- package/assets/voice/balLow-00.opus +0 -0
- package/assets/voice/balLow-01.opus +0 -0
- package/assets/voice/birthday-02.opus +0 -0
- package/assets/voice/click-00.opus +0 -0
- package/assets/voice/click-01.opus +0 -0
- package/assets/voice/click-07.opus +0 -0
- package/assets/voice/click-10.opus +0 -0
- package/assets/voice/click-11.opus +0 -0
- package/assets/voice/click-12.opus +0 -0
- package/assets/voice/click-16.opus +0 -0
- package/assets/voice/click-17.opus +0 -0
- package/assets/voice/click-18.opus +0 -0
- package/assets/voice/comfort-03.opus +0 -0
- package/assets/voice/comfort-04.opus +0 -0
- package/assets/voice/cost10-00.opus +0 -0
- package/assets/voice/cost2-00.opus +0 -0
- package/assets/voice/cost5-00.opus +0 -0
- package/assets/voice/costTurn-01.opus +0 -0
- package/assets/voice/costTurn-02.opus +0 -0
- package/assets/voice/ctxFull-01.opus +0 -0
- package/assets/voice/ctxHigh-00.opus +0 -0
- package/assets/voice/dawn-01.opus +0 -0
- package/assets/voice/dawn-04.opus +0 -0
- package/assets/voice/dawn-06.opus +0 -0
- package/assets/voice/dawn-07.opus +0 -0
- package/assets/voice/deepWake-03.opus +0 -0
- package/assets/voice/deepWake-04.opus +0 -0
- package/assets/voice/deepWake-06.opus +0 -0
- package/assets/voice/deepWake-07.opus +0 -0
- package/assets/voice/done-05.opus +0 -0
- package/assets/voice/done-08.opus +0 -0
- package/assets/voice/done-09.opus +0 -0
- package/assets/voice/done-11.opus +0 -0
- package/assets/voice/empty-00.opus +0 -0
- package/assets/voice/encourage-02.opus +0 -0
- package/assets/voice/failed-01.opus +0 -0
- package/assets/voice/failed-05.opus +0 -0
- package/assets/voice/failkind-auth-01.opus +0 -0
- package/assets/voice/failkind-auth-02.opus +0 -0
- package/assets/voice/failkind-ctx-00.opus +0 -0
- package/assets/voice/failkind-missing-01.opus +0 -0
- package/assets/voice/focusEnd-02.opus +0 -0
- package/assets/voice/focusEnd-04.opus +0 -0
- package/assets/voice/forgive-00.opus +0 -0
- package/assets/voice/hello-00.opus +0 -0
- package/assets/voice/hello-03.opus +0 -0
- package/assets/voice/hold-00.opus +0 -0
- package/assets/voice/idleTap-00.opus +0 -0
- package/assets/voice/idleTapLong-02.opus +0 -0
- package/assets/voice/lateNight-00.opus +0 -0
- package/assets/voice/lateNight-02.opus +0 -0
- package/assets/voice/lateNight-03.opus +0 -0
- package/assets/voice/lateNight-06.opus +0 -0
- package/assets/voice/lateNight-07.opus +0 -0
- package/assets/voice/murmur-04.opus +0 -0
- package/assets/voice/night-01.opus +0 -0
- package/assets/voice/night-03.opus +0 -0
- package/assets/voice/night-05.opus +0 -0
- package/assets/voice/noon-04.opus +0 -0
- package/assets/voice/petArm-02.opus +0 -0
- package/assets/voice/petCheek-00.opus +0 -0
- package/assets/voice/petCheek-01.opus +0 -0
- package/assets/voice/petCheek-02.opus +0 -0
- package/assets/voice/petHead-01.opus +0 -0
- package/assets/voice/petHead-03.opus +0 -0
- package/assets/voice/petHead-04.opus +0 -0
- package/assets/voice/praised-01.opus +0 -0
- package/assets/voice/praised-03.opus +0 -0
- package/assets/voice/question-00.opus +0 -0
- package/assets/voice/question-03.opus +0 -0
- package/assets/voice/question-04.opus +0 -0
- package/assets/voice/question-06.opus +0 -0
- package/assets/voice/shy-00.opus +0 -0
- package/assets/voice/spot-0-0.opus +0 -0
- package/assets/voice/spot-0-1.opus +0 -0
- package/assets/voice/spot-0-2.opus +0 -0
- package/assets/voice/spot-3-0.opus +0 -0
- package/assets/voice/spot-4-0.opus +0 -0
- package/assets/voice/spot-4-2.opus +0 -0
- package/assets/voice/spot-5-2.opus +0 -0
- package/assets/voice/spot-6-0.opus +0 -0
- package/assets/voice/spot-6-1.opus +0 -0
- package/assets/voice/spot-7-0.opus +0 -0
- package/assets/voice/spot-7-2.opus +0 -0
- package/assets/voice/spot-8-1.opus +0 -0
- package/assets/voice/tagent-00.opus +0 -0
- package/assets/voice/thinkFast-01.opus +0 -0
- package/assets/voice/thinkFast-02.opus +0 -0
- package/assets/voice/thinkFast-03.opus +0 -0
- package/assets/voice/thinkSlow-01.opus +0 -0
- package/assets/voice/thinkSlow-03.opus +0 -0
- package/assets/voice/thinking-01.opus +0 -0
- package/assets/voice/thinking-02.opus +0 -0
- package/assets/voice/thinking-03.opus +0 -0
- package/assets/voice/thinking-09.opus +0 -0
- package/assets/voice/tread-00.opus +0 -0
- package/assets/voice/tread-01.opus +0 -0
- package/assets/voice/trun-02.opus +0 -0
- package/assets/voice/tsun-00.opus +0 -0
- package/assets/voice/tsun-01.opus +0 -0
- package/assets/voice/tsun-04.opus +0 -0
- package/assets/voice/twrite-00.opus +0 -0
- package/assets/voice/unknown-01.opus +0 -0
- package/assets/voice/waitApproval-00.opus +0 -0
- package/assets/voice/waitApproval-01.opus +0 -0
- package/assets/voice/waitApproval-02.opus +0 -0
- package/assets/voice/waitApproval-03.opus +0 -0
- package/assets/voice/waitApprovalLate-02.opus +0 -0
- package/assets/voice/waitApprovalLate-03.opus +0 -0
- package/assets/voice/waitAsk-00.opus +0 -0
- package/assets/voice/waitAsk-02.opus +0 -0
- package/assets/voice/waitAskLate-03.opus +0 -0
- package/assets/voice/wake-03.opus +0 -0
- package/assets/voice/wake-04.opus +0 -0
- package/assets/voice/wake-05.opus +0 -0
- package/assets/voice/wake-06.opus +0 -0
- package/lib/client.js +7 -7
- package/lib/features/notes.js +172 -172
- package/lib/features/reminder.js +216 -216
- package/lib/l2d-plan.js +295 -295
- package/lib/l2d-tools.js +293 -293
- package/lib/ledger.js +163 -163
- package/lib/mys/config.js +183 -183
- package/lib/mys/ds.js +209 -209
- package/lib/mys/http.js +85 -85
- package/lib/mys/mys.js +419 -419
- package/lib/mys/qr.js +434 -434
- package/lib/mys/scheduler.js +206 -206
- package/lib/mys/store.js +248 -248
- package/lib/mys/tz.js +174 -174
- package/lib/wavtrim.js +245 -245
- package/lib/worldbook/NOTICE.md +18 -18
- package/lib/worldbook/data/Cyrene.md +111 -111
- package/lib/worldbook/data/_glossary.md +181 -181
- package/lib/worldbook/data/characters.md +289 -289
- package/lib/worldbook/data/story.md +130 -130
- package/lib/worldbook/data/world.md +182 -182
- package/lib/worldbook.js +189 -189
- package/package.json +72 -69
package/lib/mys/store.js
CHANGED
|
@@ -1,248 +1,248 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Durable plugin state: configuration, credentials, run history.
|
|
3
|
-
*
|
|
4
|
-
* The file lives under `$DSH_HOME` (default `~/.dsh`). Writes are coalesced and
|
|
5
|
-
* atomic (write-temp + rename), so a crash mid-write cannot leave a truncated
|
|
6
|
-
* JSON file behind. Credentials never leave this process: the HTTP surface only
|
|
7
|
-
* reports whether they exist.
|
|
8
|
-
*/
|
|
9
|
-
import fs from 'node:fs'
|
|
10
|
-
import os from 'node:os'
|
|
11
|
-
import path from 'node:path'
|
|
12
|
-
import { defaultConfig, normalizeConfig } from './config.js'
|
|
13
|
-
import { spawnSync } from 'node:child_process'
|
|
14
|
-
|
|
15
|
-
const STATE_VERSION = 1
|
|
16
|
-
const LOG_LIMIT = 200
|
|
17
|
-
const WRITE_DELAY_MS = 250
|
|
18
|
-
|
|
19
|
-
/** Resolve the DSH home directory the same way the host does. */
|
|
20
|
-
export function resolveHome() {
|
|
21
|
-
const configured = process.env.DSH_HOME
|
|
22
|
-
if (typeof configured === 'string' && configured.trim() !== '') return configured.trim()
|
|
23
|
-
return path.join(os.homedir(), '.dsh')
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function emptyState() {
|
|
27
|
-
return {
|
|
28
|
-
version: STATE_VERSION,
|
|
29
|
-
config: defaultConfig(),
|
|
30
|
-
auth: null,
|
|
31
|
-
runtime: {
|
|
32
|
-
planDate: null,
|
|
33
|
-
plannedAt: null,
|
|
34
|
-
nextRunAt: null,
|
|
35
|
-
lastRunDate: null,
|
|
36
|
-
lastRunAt: null,
|
|
37
|
-
lastResult: null,
|
|
38
|
-
lastError: null,
|
|
39
|
-
roles: null,
|
|
40
|
-
log: [],
|
|
41
|
-
},
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function isObject(value) {
|
|
46
|
-
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Coerce a loaded document into the current state shape. */
|
|
50
|
-
function migrate(raw, entryConfig) {
|
|
51
|
-
const state = emptyState()
|
|
52
|
-
if (isObject(raw)) {
|
|
53
|
-
state.config = normalizeConfig({ ...(isObject(raw.config) ? raw.config : {}) })
|
|
54
|
-
if (isObject(raw.auth)) {
|
|
55
|
-
const cookies = isObject(raw.auth.cookies) ? raw.auth.cookies : {}
|
|
56
|
-
const clean = {}
|
|
57
|
-
for (const [name, value] of Object.entries(cookies)) {
|
|
58
|
-
if (typeof value === 'string' && value !== '') clean[name] = value
|
|
59
|
-
}
|
|
60
|
-
state.auth = {
|
|
61
|
-
cookies: clean,
|
|
62
|
-
mid: typeof raw.auth.mid === 'string' ? raw.auth.mid : null,
|
|
63
|
-
aid: typeof raw.auth.aid === 'string' ? raw.auth.aid : null,
|
|
64
|
-
stuid: typeof raw.auth.stuid === 'string' ? raw.auth.stuid : null,
|
|
65
|
-
accountName: typeof raw.auth.accountName === 'string' ? raw.auth.accountName : null,
|
|
66
|
-
avatar: typeof raw.auth.avatar === 'string' ? raw.auth.avatar : null,
|
|
67
|
-
loginAt: typeof raw.auth.loginAt === 'number' ? raw.auth.loginAt : null,
|
|
68
|
-
lastRefreshAt: typeof raw.auth.lastRefreshAt === 'number' ? raw.auth.lastRefreshAt : null,
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
if (isObject(raw.runtime)) {
|
|
72
|
-
const runtime = state.runtime
|
|
73
|
-
for (const key of ['planDate', 'plannedAt', 'nextRunAt', 'planReason', 'lastRunDate', 'lastRunAt', 'lastError', 'deviceId']) {
|
|
74
|
-
if (raw.runtime[key] !== undefined) runtime[key] = raw.runtime[key]
|
|
75
|
-
}
|
|
76
|
-
if (isObject(raw.runtime.lastResult)) runtime.lastResult = raw.runtime.lastResult
|
|
77
|
-
if (isObject(raw.runtime.roles)) runtime.roles = raw.runtime.roles
|
|
78
|
-
if (Array.isArray(raw.runtime.log)) runtime.log = raw.runtime.log.slice(-LOG_LIMIT)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
// The cordis entry's config is a base layer; saved user values win.
|
|
82
|
-
state.config = normalizeConfig({ ...entryConfig, ...state.config })
|
|
83
|
-
return state
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export class StateStore {
|
|
87
|
-
/**
|
|
88
|
-
* @param {object} options
|
|
89
|
-
* @param {string} options.directory - directory holding `state.json`
|
|
90
|
-
* @param {object} [options.entryConfig] - config from the cordis entry
|
|
91
|
-
* @param {(message: string) => void} [options.onWriteError] - persistence error sink
|
|
92
|
-
*/
|
|
93
|
-
constructor({ directory, entryConfig = {}, onWriteError }) {
|
|
94
|
-
this.directory = directory
|
|
95
|
-
this.file = path.join(directory, 'state.json')
|
|
96
|
-
this.entryConfig = entryConfig
|
|
97
|
-
this.onWriteError = onWriteError
|
|
98
|
-
this.timer = null
|
|
99
|
-
this.closed = false
|
|
100
|
-
this.state = emptyState()
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Read (or create) the state document. Never throws. */
|
|
104
|
-
load() {
|
|
105
|
-
try {
|
|
106
|
-
fs.mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
107
|
-
} catch {
|
|
108
|
-
/* a read-only home still allows in-memory operation */
|
|
109
|
-
}
|
|
110
|
-
try {
|
|
111
|
-
const text = fs.readFileSync(this.file, 'utf8')
|
|
112
|
-
this.state = migrate(JSON.parse(text), this.entryConfig)
|
|
113
|
-
} catch (error) {
|
|
114
|
-
if (error?.code !== 'ENOENT') {
|
|
115
|
-
// 主档坏了(半截 JSON / 权限)先试 .bak —— 丢了 Cookie 是要重新扫码的。
|
|
116
|
-
let recovered = null
|
|
117
|
-
try {
|
|
118
|
-
recovered = JSON.parse(fs.readFileSync(`${this.file}.bak`, 'utf8'))
|
|
119
|
-
} catch {
|
|
120
|
-
recovered = null
|
|
121
|
-
}
|
|
122
|
-
if (recovered !== null) {
|
|
123
|
-
this.state = migrate(recovered, this.entryConfig)
|
|
124
|
-
this.onWriteError?.('状态文件损坏,已用 .bak 恢复')
|
|
125
|
-
return this.state
|
|
126
|
-
}
|
|
127
|
-
this.onWriteError?.(`状态文件读取失败,已使用默认配置:${String(error?.message ?? error)}`)
|
|
128
|
-
}
|
|
129
|
-
this.state = migrate(null, this.entryConfig)
|
|
130
|
-
}
|
|
131
|
-
return this.state
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Current in-memory document. */
|
|
135
|
-
get() {
|
|
136
|
-
return this.state
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** Replace the configuration section (already normalized) and persist. */
|
|
140
|
-
setConfig(config) {
|
|
141
|
-
this.state.config = config
|
|
142
|
-
this.save()
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
setAuth(auth) {
|
|
146
|
-
this.state.auth = auth
|
|
147
|
-
this.save()
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** Merge fields into the runtime section and persist. */
|
|
151
|
-
patchRuntime(patch) {
|
|
152
|
-
this.state.runtime = { ...this.state.runtime, ...patch }
|
|
153
|
-
this.save()
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** Append one log entry, keeping the ring bounded. */
|
|
157
|
-
log(level, message, extra = undefined) {
|
|
158
|
-
const entry = { at: Date.now(), level, message: String(message) }
|
|
159
|
-
if (extra !== undefined && extra !== null) entry.detail = extra
|
|
160
|
-
const log = this.state.runtime.log ?? []
|
|
161
|
-
log.push(entry)
|
|
162
|
-
if (log.length > LOG_LIMIT) log.splice(0, log.length - LOG_LIMIT)
|
|
163
|
-
this.state.runtime.log = log
|
|
164
|
-
this.save()
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** Persist soon; repeated calls collapse into one write. */
|
|
168
|
-
save() {
|
|
169
|
-
if (this.closed) return
|
|
170
|
-
if (this.timer !== null) return
|
|
171
|
-
this.timer = setTimeout(() => {
|
|
172
|
-
this.timer = null
|
|
173
|
-
this.flush()
|
|
174
|
-
}, WRITE_DELAY_MS)
|
|
175
|
-
if (typeof this.timer.unref === 'function') this.timer.unref()
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Write the document atomically right now. */
|
|
179
|
-
flush() {
|
|
180
|
-
if (this.closed) return
|
|
181
|
-
const tmp = `${this.file}.${process.pid}.tmp`
|
|
182
|
-
try {
|
|
183
|
-
fs.mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
184
|
-
// 写 → fsync → rename:这份文件里装着登录 Cookie,丢了要重新扫码,
|
|
185
|
-
// 代价和记忆同一量级。只 rename 的话,断电可能得到「新名字 + 空内容」。
|
|
186
|
-
const buf = Buffer.from(JSON.stringify(this.state, null, 2), 'utf8')
|
|
187
|
-
const fd = fs.openSync(tmp, 'w', 0o600)
|
|
188
|
-
try {
|
|
189
|
-
// 部分写(磁盘满)也会「成功」rename 出一份半截 JSON —— 必须核对字节数
|
|
190
|
-
const written = fs.writeSync(fd, buf, 0, buf.length)
|
|
191
|
-
if (written !== buf.length) throw new Error(`short write ${written}/${buf.length}`)
|
|
192
|
-
try {
|
|
193
|
-
fs.fsyncSync(fd)
|
|
194
|
-
} catch {
|
|
195
|
-
/* 有些文件系统不支持 fsync,忽略 */
|
|
196
|
-
}
|
|
197
|
-
} finally {
|
|
198
|
-
try {
|
|
199
|
-
fs.closeSync(fd)
|
|
200
|
-
} catch {
|
|
201
|
-
/* ignore */
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
// rename 前留一份旧档:这解决 fsync 解决不了的「半截 JSON 已经写进去了」。
|
|
205
|
-
try {
|
|
206
|
-
if (fs.existsSync(this.file)) fs.copyFileSync(this.file, `${this.file}.bak`)
|
|
207
|
-
} catch {
|
|
208
|
-
/* 备份失败不阻止主写入 */
|
|
209
|
-
}
|
|
210
|
-
fs.renameSync(tmp, this.file)
|
|
211
|
-
// Windows: POSIX mode (0o600) is a no-op here; restrict the ACL of the
|
|
212
|
-
// file that actually landed (and its backup) to the current user.
|
|
213
|
-
// Use DOMAIN\USER -- a bare USERNAME can be wrong for domain accounts.
|
|
214
|
-
if (process.platform === 'win32') {
|
|
215
|
-
const who =
|
|
216
|
-
process.env.USERDOMAIN && process.env.USERNAME
|
|
217
|
-
? process.env.USERDOMAIN + '\\' + process.env.USERNAME
|
|
218
|
-
: process.env.USERNAME
|
|
219
|
-
if (who) {
|
|
220
|
-
for (const p of [this.file, this.file + '.bak']) {
|
|
221
|
-
try {
|
|
222
|
-
spawnSync('icacls', [p, '/inheritance:r', '/grant:r', who + ':F'], { stdio: 'ignore' })
|
|
223
|
-
} catch {
|
|
224
|
-
/* best effort */
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
} catch (error) {
|
|
230
|
-
try {
|
|
231
|
-
fs.rmSync(tmp, { force: true })
|
|
232
|
-
} catch {
|
|
233
|
-
/* ignore */
|
|
234
|
-
}
|
|
235
|
-
this.onWriteError?.(`状态文件写入失败:${String(error?.message ?? error)}`)
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/** Flush pending writes and stop accepting new ones. */
|
|
240
|
-
close() {
|
|
241
|
-
if (this.timer !== null) {
|
|
242
|
-
clearTimeout(this.timer)
|
|
243
|
-
this.timer = null
|
|
244
|
-
}
|
|
245
|
-
this.flush()
|
|
246
|
-
this.closed = true
|
|
247
|
-
}
|
|
248
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Durable plugin state: configuration, credentials, run history.
|
|
3
|
+
*
|
|
4
|
+
* The file lives under `$DSH_HOME` (default `~/.dsh`). Writes are coalesced and
|
|
5
|
+
* atomic (write-temp + rename), so a crash mid-write cannot leave a truncated
|
|
6
|
+
* JSON file behind. Credentials never leave this process: the HTTP surface only
|
|
7
|
+
* reports whether they exist.
|
|
8
|
+
*/
|
|
9
|
+
import fs from 'node:fs'
|
|
10
|
+
import os from 'node:os'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
import { defaultConfig, normalizeConfig } from './config.js'
|
|
13
|
+
import { spawnSync } from 'node:child_process'
|
|
14
|
+
|
|
15
|
+
const STATE_VERSION = 1
|
|
16
|
+
const LOG_LIMIT = 200
|
|
17
|
+
const WRITE_DELAY_MS = 250
|
|
18
|
+
|
|
19
|
+
/** Resolve the DSH home directory the same way the host does. */
|
|
20
|
+
export function resolveHome() {
|
|
21
|
+
const configured = process.env.DSH_HOME
|
|
22
|
+
if (typeof configured === 'string' && configured.trim() !== '') return configured.trim()
|
|
23
|
+
return path.join(os.homedir(), '.dsh')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function emptyState() {
|
|
27
|
+
return {
|
|
28
|
+
version: STATE_VERSION,
|
|
29
|
+
config: defaultConfig(),
|
|
30
|
+
auth: null,
|
|
31
|
+
runtime: {
|
|
32
|
+
planDate: null,
|
|
33
|
+
plannedAt: null,
|
|
34
|
+
nextRunAt: null,
|
|
35
|
+
lastRunDate: null,
|
|
36
|
+
lastRunAt: null,
|
|
37
|
+
lastResult: null,
|
|
38
|
+
lastError: null,
|
|
39
|
+
roles: null,
|
|
40
|
+
log: [],
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isObject(value) {
|
|
46
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Coerce a loaded document into the current state shape. */
|
|
50
|
+
function migrate(raw, entryConfig) {
|
|
51
|
+
const state = emptyState()
|
|
52
|
+
if (isObject(raw)) {
|
|
53
|
+
state.config = normalizeConfig({ ...(isObject(raw.config) ? raw.config : {}) })
|
|
54
|
+
if (isObject(raw.auth)) {
|
|
55
|
+
const cookies = isObject(raw.auth.cookies) ? raw.auth.cookies : {}
|
|
56
|
+
const clean = {}
|
|
57
|
+
for (const [name, value] of Object.entries(cookies)) {
|
|
58
|
+
if (typeof value === 'string' && value !== '') clean[name] = value
|
|
59
|
+
}
|
|
60
|
+
state.auth = {
|
|
61
|
+
cookies: clean,
|
|
62
|
+
mid: typeof raw.auth.mid === 'string' ? raw.auth.mid : null,
|
|
63
|
+
aid: typeof raw.auth.aid === 'string' ? raw.auth.aid : null,
|
|
64
|
+
stuid: typeof raw.auth.stuid === 'string' ? raw.auth.stuid : null,
|
|
65
|
+
accountName: typeof raw.auth.accountName === 'string' ? raw.auth.accountName : null,
|
|
66
|
+
avatar: typeof raw.auth.avatar === 'string' ? raw.auth.avatar : null,
|
|
67
|
+
loginAt: typeof raw.auth.loginAt === 'number' ? raw.auth.loginAt : null,
|
|
68
|
+
lastRefreshAt: typeof raw.auth.lastRefreshAt === 'number' ? raw.auth.lastRefreshAt : null,
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (isObject(raw.runtime)) {
|
|
72
|
+
const runtime = state.runtime
|
|
73
|
+
for (const key of ['planDate', 'plannedAt', 'nextRunAt', 'planReason', 'lastRunDate', 'lastRunAt', 'lastError', 'deviceId']) {
|
|
74
|
+
if (raw.runtime[key] !== undefined) runtime[key] = raw.runtime[key]
|
|
75
|
+
}
|
|
76
|
+
if (isObject(raw.runtime.lastResult)) runtime.lastResult = raw.runtime.lastResult
|
|
77
|
+
if (isObject(raw.runtime.roles)) runtime.roles = raw.runtime.roles
|
|
78
|
+
if (Array.isArray(raw.runtime.log)) runtime.log = raw.runtime.log.slice(-LOG_LIMIT)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// The cordis entry's config is a base layer; saved user values win.
|
|
82
|
+
state.config = normalizeConfig({ ...entryConfig, ...state.config })
|
|
83
|
+
return state
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class StateStore {
|
|
87
|
+
/**
|
|
88
|
+
* @param {object} options
|
|
89
|
+
* @param {string} options.directory - directory holding `state.json`
|
|
90
|
+
* @param {object} [options.entryConfig] - config from the cordis entry
|
|
91
|
+
* @param {(message: string) => void} [options.onWriteError] - persistence error sink
|
|
92
|
+
*/
|
|
93
|
+
constructor({ directory, entryConfig = {}, onWriteError }) {
|
|
94
|
+
this.directory = directory
|
|
95
|
+
this.file = path.join(directory, 'state.json')
|
|
96
|
+
this.entryConfig = entryConfig
|
|
97
|
+
this.onWriteError = onWriteError
|
|
98
|
+
this.timer = null
|
|
99
|
+
this.closed = false
|
|
100
|
+
this.state = emptyState()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Read (or create) the state document. Never throws. */
|
|
104
|
+
load() {
|
|
105
|
+
try {
|
|
106
|
+
fs.mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
107
|
+
} catch {
|
|
108
|
+
/* a read-only home still allows in-memory operation */
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
const text = fs.readFileSync(this.file, 'utf8')
|
|
112
|
+
this.state = migrate(JSON.parse(text), this.entryConfig)
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (error?.code !== 'ENOENT') {
|
|
115
|
+
// 主档坏了(半截 JSON / 权限)先试 .bak —— 丢了 Cookie 是要重新扫码的。
|
|
116
|
+
let recovered = null
|
|
117
|
+
try {
|
|
118
|
+
recovered = JSON.parse(fs.readFileSync(`${this.file}.bak`, 'utf8'))
|
|
119
|
+
} catch {
|
|
120
|
+
recovered = null
|
|
121
|
+
}
|
|
122
|
+
if (recovered !== null) {
|
|
123
|
+
this.state = migrate(recovered, this.entryConfig)
|
|
124
|
+
this.onWriteError?.('状态文件损坏,已用 .bak 恢复')
|
|
125
|
+
return this.state
|
|
126
|
+
}
|
|
127
|
+
this.onWriteError?.(`状态文件读取失败,已使用默认配置:${String(error?.message ?? error)}`)
|
|
128
|
+
}
|
|
129
|
+
this.state = migrate(null, this.entryConfig)
|
|
130
|
+
}
|
|
131
|
+
return this.state
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Current in-memory document. */
|
|
135
|
+
get() {
|
|
136
|
+
return this.state
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Replace the configuration section (already normalized) and persist. */
|
|
140
|
+
setConfig(config) {
|
|
141
|
+
this.state.config = config
|
|
142
|
+
this.save()
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
setAuth(auth) {
|
|
146
|
+
this.state.auth = auth
|
|
147
|
+
this.save()
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Merge fields into the runtime section and persist. */
|
|
151
|
+
patchRuntime(patch) {
|
|
152
|
+
this.state.runtime = { ...this.state.runtime, ...patch }
|
|
153
|
+
this.save()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Append one log entry, keeping the ring bounded. */
|
|
157
|
+
log(level, message, extra = undefined) {
|
|
158
|
+
const entry = { at: Date.now(), level, message: String(message) }
|
|
159
|
+
if (extra !== undefined && extra !== null) entry.detail = extra
|
|
160
|
+
const log = this.state.runtime.log ?? []
|
|
161
|
+
log.push(entry)
|
|
162
|
+
if (log.length > LOG_LIMIT) log.splice(0, log.length - LOG_LIMIT)
|
|
163
|
+
this.state.runtime.log = log
|
|
164
|
+
this.save()
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Persist soon; repeated calls collapse into one write. */
|
|
168
|
+
save() {
|
|
169
|
+
if (this.closed) return
|
|
170
|
+
if (this.timer !== null) return
|
|
171
|
+
this.timer = setTimeout(() => {
|
|
172
|
+
this.timer = null
|
|
173
|
+
this.flush()
|
|
174
|
+
}, WRITE_DELAY_MS)
|
|
175
|
+
if (typeof this.timer.unref === 'function') this.timer.unref()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Write the document atomically right now. */
|
|
179
|
+
flush() {
|
|
180
|
+
if (this.closed) return
|
|
181
|
+
const tmp = `${this.file}.${process.pid}.tmp`
|
|
182
|
+
try {
|
|
183
|
+
fs.mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
184
|
+
// 写 → fsync → rename:这份文件里装着登录 Cookie,丢了要重新扫码,
|
|
185
|
+
// 代价和记忆同一量级。只 rename 的话,断电可能得到「新名字 + 空内容」。
|
|
186
|
+
const buf = Buffer.from(JSON.stringify(this.state, null, 2), 'utf8')
|
|
187
|
+
const fd = fs.openSync(tmp, 'w', 0o600)
|
|
188
|
+
try {
|
|
189
|
+
// 部分写(磁盘满)也会「成功」rename 出一份半截 JSON —— 必须核对字节数
|
|
190
|
+
const written = fs.writeSync(fd, buf, 0, buf.length)
|
|
191
|
+
if (written !== buf.length) throw new Error(`short write ${written}/${buf.length}`)
|
|
192
|
+
try {
|
|
193
|
+
fs.fsyncSync(fd)
|
|
194
|
+
} catch {
|
|
195
|
+
/* 有些文件系统不支持 fsync,忽略 */
|
|
196
|
+
}
|
|
197
|
+
} finally {
|
|
198
|
+
try {
|
|
199
|
+
fs.closeSync(fd)
|
|
200
|
+
} catch {
|
|
201
|
+
/* ignore */
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// rename 前留一份旧档:这解决 fsync 解决不了的「半截 JSON 已经写进去了」。
|
|
205
|
+
try {
|
|
206
|
+
if (fs.existsSync(this.file)) fs.copyFileSync(this.file, `${this.file}.bak`)
|
|
207
|
+
} catch {
|
|
208
|
+
/* 备份失败不阻止主写入 */
|
|
209
|
+
}
|
|
210
|
+
fs.renameSync(tmp, this.file)
|
|
211
|
+
// Windows: POSIX mode (0o600) is a no-op here; restrict the ACL of the
|
|
212
|
+
// file that actually landed (and its backup) to the current user.
|
|
213
|
+
// Use DOMAIN\USER -- a bare USERNAME can be wrong for domain accounts.
|
|
214
|
+
if (process.platform === 'win32') {
|
|
215
|
+
const who =
|
|
216
|
+
process.env.USERDOMAIN && process.env.USERNAME
|
|
217
|
+
? process.env.USERDOMAIN + '\\' + process.env.USERNAME
|
|
218
|
+
: process.env.USERNAME
|
|
219
|
+
if (who) {
|
|
220
|
+
for (const p of [this.file, this.file + '.bak']) {
|
|
221
|
+
try {
|
|
222
|
+
spawnSync('icacls', [p, '/inheritance:r', '/grant:r', who + ':F'], { stdio: 'ignore' })
|
|
223
|
+
} catch {
|
|
224
|
+
/* best effort */
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} catch (error) {
|
|
230
|
+
try {
|
|
231
|
+
fs.rmSync(tmp, { force: true })
|
|
232
|
+
} catch {
|
|
233
|
+
/* ignore */
|
|
234
|
+
}
|
|
235
|
+
this.onWriteError?.(`状态文件写入失败:${String(error?.message ?? error)}`)
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Flush pending writes and stop accepting new ones. */
|
|
240
|
+
close() {
|
|
241
|
+
if (this.timer !== null) {
|
|
242
|
+
clearTimeout(this.timer)
|
|
243
|
+
this.timer = null
|
|
244
|
+
}
|
|
245
|
+
this.flush()
|
|
246
|
+
this.closed = true
|
|
247
|
+
}
|
|
248
|
+
}
|