dsh-codearts 1.2.14 → 1.4.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/CHANGELOG.md +598 -515
- package/lib/auth-Ch0vYSAo.mjs +1287 -0
- package/lib/bin.mjs +3 -1
- package/lib/client/index.d.cts +30 -0
- package/lib/client/index.js +632 -178
- package/lib/index.d.mts +122 -4
- package/lib/index.mjs +591 -141
- package/lib/keeper-MHeAp_eV.mjs +215 -0
- package/lib/keeper-main.d.mts +1 -0
- package/lib/keeper-main.mjs +210 -0
- package/lib/{version-Msk-1m2n.mjs → upstream-CVtspzAv.mjs} +2 -1469
- package/lib/version-BjBwEMks.mjs +539 -0
- package/package.json +125 -125
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
//#region src/keeper.ts
|
|
7
|
+
/**
|
|
8
|
+
* 凭证守护进程:把 refresh_token 交给一个**常驻子进程**去续期。
|
|
9
|
+
*
|
|
10
|
+
* ## 为什么需要它
|
|
11
|
+
*
|
|
12
|
+
* CodeArts 签发的 STS token 有效期是**固定的 2 小时**(实测,见 `auth.ts` 的
|
|
13
|
+
* `REFRESH_SKEW_MS` 注释),而 refresh_token 有 30 天。插件自己的 `keepAliveLoop`
|
|
14
|
+
* 只在 DSH 运行时能跑 —— 用户晚上关掉 DSH,凭证两小时后就过期,第二天打开又要重新
|
|
15
|
+
* 登录。要跨过「DSH 没开」这段时间,续期动作必须发生在 DSH 之外,所以这里起一个
|
|
16
|
+
* 独立的常驻进程。
|
|
17
|
+
*
|
|
18
|
+
* ## 进程形态
|
|
19
|
+
*
|
|
20
|
+
* DSH Desktop 是 Electron,宿主 exe 可以直接当 Node 用:设置
|
|
21
|
+
* `ELECTRON_RUN_AS_NODE=1` 后启动它就是 node 24.21.0(已实测)。所以不依赖用户
|
|
22
|
+
* 装没装 node,也不引入任何原生依赖。启动后 `detached` + `unref` + `stdio:'ignore'`,
|
|
23
|
+
* DSH 退出它不跟着退出(同样已实测)。
|
|
24
|
+
*
|
|
25
|
+
* ## 谁有权续期(重要)
|
|
26
|
+
*
|
|
27
|
+
* refresh_token 是**一次性的**:刷一次就轮换,另一份旧的就作废。如果守护进程和 DSH
|
|
28
|
+
* 插件同时续期,后刷的那次会拿着已作废的 token 失败,并把账号写成 `stale` —— 正是
|
|
29
|
+
* 「隔天就要重新登录」这个症状的成因之一。所以两者**互斥**:
|
|
30
|
+
*
|
|
31
|
+
* - 守护进程活着且心跳新鲜时,**由它独占续期**,插件自己不再刷,只从磁盘重读它写回
|
|
32
|
+
* 的结果;
|
|
33
|
+
* - 守护进程不在(被 kill、崩溃、从未开启)时,插件恢复自己刷。
|
|
34
|
+
*
|
|
35
|
+
* 判据就是心跳文件 `~/.dsh/.codearts-keeper.json` 的 `heartbeatAt` 与那个 `pid` 是否
|
|
36
|
+
* 还在 —— 光看文件不够,进程被 kill 时来不及写任何东西,文件会留成「看起来很新鲜」。
|
|
37
|
+
*
|
|
38
|
+
* @module dsh-codearts/keeper
|
|
39
|
+
*/
|
|
40
|
+
/** 心跳超过这么久没更新,就认为守护进程已经不在(它每 30 秒写一次)。 */
|
|
41
|
+
const KEEPER_STALE_MS = 9e4;
|
|
42
|
+
/** 心跳写入间隔。 */
|
|
43
|
+
const KEEPER_HEARTBEAT_MS = 3e4;
|
|
44
|
+
/** 守护进程的续期节奏:每 20 分钟过一遍,与插件里的 `KEEP_ALIVE_INTERVAL_MS` 对齐。 */
|
|
45
|
+
const KEEPER_TICK_MS = 12e5;
|
|
46
|
+
function dshHome() {
|
|
47
|
+
return process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
48
|
+
}
|
|
49
|
+
/** 心跳/状态文件:`~/.dsh/.codearts-keeper.json`。 */
|
|
50
|
+
function keeperStatePath(home) {
|
|
51
|
+
return join(home ?? dshHome(), ".codearts-keeper.json");
|
|
52
|
+
}
|
|
53
|
+
/** 守护进程自己的日志(stdout 被 ignore 了,出问题只能看这里)。 */
|
|
54
|
+
function keeperLogPath(home) {
|
|
55
|
+
return join(home ?? dshHome(), ".codearts-keeper.log");
|
|
56
|
+
}
|
|
57
|
+
/** 读状态文件;读不到/格式不对都返回 undefined(不抛)。 */
|
|
58
|
+
function readKeeperState(home) {
|
|
59
|
+
try {
|
|
60
|
+
const parsed = JSON.parse(readFileSync(keeperStatePath(home), "utf8"));
|
|
61
|
+
if (typeof parsed.pid !== "number" || typeof parsed.heartbeatAt !== "number") return void 0;
|
|
62
|
+
return {
|
|
63
|
+
version: 1,
|
|
64
|
+
pid: parsed.pid,
|
|
65
|
+
startedAt: typeof parsed.startedAt === "number" ? parsed.startedAt : parsed.heartbeatAt,
|
|
66
|
+
heartbeatAt: parsed.heartbeatAt,
|
|
67
|
+
...parsed.pluginVersion === void 0 ? {} : { pluginVersion: parsed.pluginVersion },
|
|
68
|
+
...parsed.lastPass === void 0 ? {} : { lastPass: parsed.lastPass }
|
|
69
|
+
};
|
|
70
|
+
} catch {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function writeKeeperState(state, home) {
|
|
75
|
+
try {
|
|
76
|
+
writeFileSync(keeperStatePath(home), JSON.stringify(state, null, 2), { mode: 384 });
|
|
77
|
+
} catch {}
|
|
78
|
+
}
|
|
79
|
+
/** 进程还在吗。信号 0 只探活,不真的发信号(Windows 上同样适用)。 */
|
|
80
|
+
function pidAlive(pid) {
|
|
81
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
82
|
+
try {
|
|
83
|
+
process.kill(pid, 0);
|
|
84
|
+
return true;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return error?.code === "EPERM";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** 状态文件的 mtime(用来判断账号文档是不是被守护进程改过)。 */
|
|
90
|
+
function keeperStats(home) {
|
|
91
|
+
const state = readKeeperState(home);
|
|
92
|
+
if (state === void 0) return { alive: false };
|
|
93
|
+
return {
|
|
94
|
+
alive: Date.now() - state.heartbeatAt <= 9e4 && pidAlive(state.pid),
|
|
95
|
+
state
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** 守护进程的脚本路径:和当前 bundle 同目录的 `keeper-main.mjs`(tsdown 入口名)。 */
|
|
99
|
+
function keeperScriptPath() {
|
|
100
|
+
return fileURLToPath(new URL("./keeper-main.mjs", import.meta.url));
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 拿 `ELECTRON_RUN_AS_NODE` 这条路启动 node 所需的 exe。
|
|
104
|
+
*
|
|
105
|
+
* 优先级:宿主自己给的 `DSH_DESKTOP_NODE_EXECUTABLE`(Desktop 注入,最准)→
|
|
106
|
+
* `process.execPath`(Electron 自己或普通 node 都适用)→ PATH 里的 `node`。
|
|
107
|
+
*/
|
|
108
|
+
function nodeLauncher() {
|
|
109
|
+
const desktop = process.env.DSH_DESKTOP_NODE_EXECUTABLE;
|
|
110
|
+
if (desktop !== void 0 && desktop !== "" && existsSync(desktop)) return {
|
|
111
|
+
command: desktop,
|
|
112
|
+
env: {
|
|
113
|
+
...process.env,
|
|
114
|
+
ELECTRON_RUN_AS_NODE: "1"
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
command: process.execPath,
|
|
119
|
+
env: {
|
|
120
|
+
...process.env,
|
|
121
|
+
ELECTRON_RUN_AS_NODE: "1"
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 起一个守护进程。已经在跑就返回现有的那个(幂等,不重复起)。
|
|
127
|
+
*
|
|
128
|
+
* `detached` + `unref` + `stdio:'ignore'` 三件套缺一不可:少了任何一个,DSH 退出时
|
|
129
|
+
* 子进程会被一起带走(Windows 上尤其明显),守护也就没有意义了。
|
|
130
|
+
*/
|
|
131
|
+
function spawnKeeper(options) {
|
|
132
|
+
if (!existsSync(options.scriptPath)) return {
|
|
133
|
+
ok: false,
|
|
134
|
+
reason: `keeper script missing: ${options.scriptPath}`
|
|
135
|
+
};
|
|
136
|
+
const running = keeperStats();
|
|
137
|
+
if (running.alive && running.state !== void 0) return {
|
|
138
|
+
ok: true,
|
|
139
|
+
pid: running.state.pid
|
|
140
|
+
};
|
|
141
|
+
const { command, env } = nodeLauncher();
|
|
142
|
+
try {
|
|
143
|
+
const child = spawn(command, [
|
|
144
|
+
options.scriptPath,
|
|
145
|
+
"--home",
|
|
146
|
+
dshHome()
|
|
147
|
+
], {
|
|
148
|
+
detached: true,
|
|
149
|
+
stdio: "ignore",
|
|
150
|
+
windowsHide: true,
|
|
151
|
+
env: {
|
|
152
|
+
...env,
|
|
153
|
+
...options.pluginVersion === void 0 ? {} : { DSH_CODEARTS_PLUGIN_VERSION: options.pluginVersion }
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
child.unref();
|
|
157
|
+
if (child.pid === void 0) return {
|
|
158
|
+
ok: false,
|
|
159
|
+
reason: "spawn produced no pid"
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
ok: true,
|
|
163
|
+
pid: child.pid
|
|
164
|
+
};
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return {
|
|
167
|
+
ok: false,
|
|
168
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 停掉守护进程。
|
|
174
|
+
*
|
|
175
|
+
* 先礼后兵:先请它自己退(它能顺手把状态文件标成已停),等一小会儿还在就直杀。
|
|
176
|
+
* 不管走哪条路,最后都把状态文件删掉 —— 留着它只会让下一次启动以为「已经在跑」。
|
|
177
|
+
*/
|
|
178
|
+
async function stopKeeper(home) {
|
|
179
|
+
const state = readKeeperState(home);
|
|
180
|
+
if (state === void 0 || !pidAlive(state.pid)) {
|
|
181
|
+
clearKeeperState(home);
|
|
182
|
+
return { ok: true };
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
writeFileSync(join(home ?? dshHome(), ".codearts-keeper.stop"), String(Date.now()), { mode: 384 });
|
|
186
|
+
} catch {}
|
|
187
|
+
const deadline = Date.now() + 3e3;
|
|
188
|
+
while (Date.now() < deadline && pidAlive(state.pid)) await new Promise((resolve) => setTimeout(resolve, 100));
|
|
189
|
+
if (pidAlive(state.pid)) try {
|
|
190
|
+
process.kill(state.pid);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
if (error?.code !== "ESRCH") return {
|
|
193
|
+
ok: false,
|
|
194
|
+
pid: state.pid,
|
|
195
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
clearKeeperState(home);
|
|
199
|
+
return {
|
|
200
|
+
ok: true,
|
|
201
|
+
pid: state.pid
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/** 删掉状态文件与停止标记。 */
|
|
205
|
+
function clearKeeperState(home) {
|
|
206
|
+
for (const path of [keeperStatePath(home), join(home ?? dshHome(), ".codearts-keeper.stop")]) try {
|
|
207
|
+
if (existsSync(path)) unlinkSync(path);
|
|
208
|
+
} catch {}
|
|
209
|
+
}
|
|
210
|
+
/** 停止标记存在吗(守护进程每轮自查)。 */
|
|
211
|
+
function stopRequested(home) {
|
|
212
|
+
return existsSync(join(home ?? dshHome(), ".codearts-keeper.stop"));
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
export { keeperLogPath as a, keeperStats as c, stopKeeper as d, stopRequested as f, clearKeeperState as i, readKeeperState as l, KEEPER_STALE_MS as n, keeperScriptPath as o, writeKeeperState as p, KEEPER_TICK_MS as r, keeperStatePath as s, KEEPER_HEARTBEAT_MS as t, spawnKeeper as u };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { d as OAuthTokenError, f as defaultLoginConfig, i as codeartsAccountsPath, l as readAccountDocument, p as refreshToken, u as writeJsonAtomic } from "./auth-Ch0vYSAo.mjs";
|
|
2
|
+
import { a as keeperLogPath, f as stopRequested, i as clearKeeperState, l as readKeeperState, p as writeKeeperState, r as KEEPER_TICK_MS, s as keeperStatePath, t as KEEPER_HEARTBEAT_MS } from "./keeper-MHeAp_eV.mjs";
|
|
3
|
+
import { appendFileSync, existsSync, unlinkSync } from "node:fs";
|
|
4
|
+
//#region src/keeper-main.ts
|
|
5
|
+
/**
|
|
6
|
+
* dsh-codearts 凭证守护进程(独立入口,由 DSH 用 `ELECTRON_RUN_AS_NODE=1` 拉起)。
|
|
7
|
+
*
|
|
8
|
+
* 它只做一件事:**在 DSH 关掉之后,继续把 refresh_token 续下去**,这样第二天打开
|
|
9
|
+
* DSH 不必重新登录。DSH 开着的时候它同样活着,但续期的**独占权**在它手里
|
|
10
|
+
* (见 `keeper.ts` 的模块注释:refresh_token 一次性,两边同时刷会把号刷成 stale)。
|
|
11
|
+
*
|
|
12
|
+
* 进程形态上的三条硬要求,缺一条这个功能就不成立:
|
|
13
|
+
*
|
|
14
|
+
* 1. `detached: true` —— 不跟着 DSH 的进程组一起被回收;
|
|
15
|
+
* 2. `child.unref()` —— 父进程的事件循环不为它续命;
|
|
16
|
+
* 3. `stdio: 'ignore'` —— 父进程退出后管道断了,子孙进程写 stdout 会 EPIPE。
|
|
17
|
+
*
|
|
18
|
+
* 三条都在 `keeper.ts` 的 `spawnKeeper` 里。本文件只被当作独立入口跑,
|
|
19
|
+
* **不要从插件主流程 import 它**(import 会顺手执行监听逻辑)。
|
|
20
|
+
*
|
|
21
|
+
* @module dsh-codearts/keeper-main
|
|
22
|
+
*/
|
|
23
|
+
/** 距过期不足这么久就续(与服务端插件保持一致)。 */
|
|
24
|
+
const REFRESH_SKEW_MS = 18e5;
|
|
25
|
+
function parseHomeArg(argv) {
|
|
26
|
+
const at = argv.indexOf("--home");
|
|
27
|
+
if (at >= 0 && at + 1 < argv.length) return argv[at + 1];
|
|
28
|
+
}
|
|
29
|
+
const home = parseHomeArg(process.argv.slice(2));
|
|
30
|
+
const logPath = keeperLogPath(home);
|
|
31
|
+
/** 只往日志里写人能看懂的一行;绝不写 token/refresh_token/私钥。 */
|
|
32
|
+
function log(message) {
|
|
33
|
+
try {
|
|
34
|
+
appendFileSync(logPath, `${(/* @__PURE__ */ new Date()).toISOString()} ${message}\n`, { mode: 384 });
|
|
35
|
+
} catch {}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 只有一份守护进程。
|
|
39
|
+
*
|
|
40
|
+
* 从卡片连点两次、或同时开两个 DSH 窗口时,第二次 spawn 必须让路 —— 两个进程同时
|
|
41
|
+
* 刷 refresh_token 就是上面说的那个翻车场景。
|
|
42
|
+
*/
|
|
43
|
+
function alreadyRunning() {
|
|
44
|
+
const state = readKeeperState(home);
|
|
45
|
+
if (state === void 0) return void 0;
|
|
46
|
+
if (state.pid === process.pid) return void 0;
|
|
47
|
+
try {
|
|
48
|
+
process.kill(state.pid, 0);
|
|
49
|
+
return state.pid;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return error?.code === "EPERM" ? state.pid : void 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 一轮续期:读文档 → 逐个续 → 写回。
|
|
56
|
+
*
|
|
57
|
+
* 每轮都重新读盘,而不是启动时读一次:DSH 在运行期间会登录新账号、删账号、写新
|
|
58
|
+
* 凭证,守护进程必须看见这些变化,否则会把一份过期的内存快照写回去,把刚登录的
|
|
59
|
+
* 号覆盖掉。
|
|
60
|
+
*/
|
|
61
|
+
async function tick(state) {
|
|
62
|
+
const accountsPath = codeartsAccountsPath(home);
|
|
63
|
+
const doc = await readAccountDocument(accountsPath);
|
|
64
|
+
if (doc === void 0) {
|
|
65
|
+
state.lastPass = {
|
|
66
|
+
at: Date.now(),
|
|
67
|
+
checked: 0,
|
|
68
|
+
refreshed: 0,
|
|
69
|
+
needsLogin: []
|
|
70
|
+
};
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const needsLogin = [];
|
|
74
|
+
let refreshed = 0;
|
|
75
|
+
let checked = 0;
|
|
76
|
+
let dirty = false;
|
|
77
|
+
for (const id of doc.order) {
|
|
78
|
+
const cred = doc.accounts[id];
|
|
79
|
+
if (cred === void 0) continue;
|
|
80
|
+
if (cred.stale === true) {
|
|
81
|
+
needsLogin.push(id);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (!cred.refreshToken || !cred.dpopPrivateKey || !cred.clientId) continue;
|
|
85
|
+
const exp = cred.expiration ? Date.parse(cred.expiration) : NaN;
|
|
86
|
+
if (Number.isNaN(exp)) continue;
|
|
87
|
+
if (exp - Date.now() >= REFRESH_SKEW_MS) continue;
|
|
88
|
+
checked += 1;
|
|
89
|
+
try {
|
|
90
|
+
const next = await refreshToken(defaultLoginConfig(), {
|
|
91
|
+
userId: cred.userId ?? "",
|
|
92
|
+
userName: cred.userName ?? "",
|
|
93
|
+
domainId: "",
|
|
94
|
+
securityToken: cred.token,
|
|
95
|
+
accessKeyId: cred.accessKeyId ?? "",
|
|
96
|
+
secretAccessKey: cred.secretAccessKey ?? "",
|
|
97
|
+
expiration: cred.expiration ?? "",
|
|
98
|
+
refreshToken: cred.refreshToken,
|
|
99
|
+
clientId: cred.clientId,
|
|
100
|
+
dpopPrivateKey: cred.dpopPrivateKey
|
|
101
|
+
});
|
|
102
|
+
doc.accounts[id] = {
|
|
103
|
+
...cred,
|
|
104
|
+
token: next.securityToken,
|
|
105
|
+
accessKeyId: next.accessKeyId,
|
|
106
|
+
secretAccessKey: next.secretAccessKey,
|
|
107
|
+
refreshToken: next.refreshToken === "" ? cred.refreshToken : next.refreshToken,
|
|
108
|
+
dpopPrivateKey: next.dpopPrivateKey,
|
|
109
|
+
clientId: next.clientId,
|
|
110
|
+
expiration: next.expiration,
|
|
111
|
+
storedAt: Date.now(),
|
|
112
|
+
stale: false
|
|
113
|
+
};
|
|
114
|
+
dirty = true;
|
|
115
|
+
refreshed += 1;
|
|
116
|
+
log(`refreshed ${id.slice(0, 12)} → expires ${next.expiration}`);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
const used = error instanceof OAuthTokenError && error.refreshTokenUsed;
|
|
119
|
+
if (used) {
|
|
120
|
+
const fresh = await readAccountDocument(accountsPath);
|
|
121
|
+
const advanced = fresh !== void 0 && fresh.accounts[id]?.refreshToken !== void 0 && fresh.accounts[id]?.refreshToken !== cred.refreshToken;
|
|
122
|
+
log(`refresh SKIPPED ${id.slice(0, 12)} (refresh token already used${advanced ? ", newer token on disk" : ""})`);
|
|
123
|
+
if (advanced) continue;
|
|
124
|
+
}
|
|
125
|
+
const permanent = !used && error instanceof OAuthTokenError && error.permanent;
|
|
126
|
+
if (permanent) {
|
|
127
|
+
doc.accounts[id] = {
|
|
128
|
+
...cred,
|
|
129
|
+
stale: true
|
|
130
|
+
};
|
|
131
|
+
dirty = true;
|
|
132
|
+
needsLogin.push(id);
|
|
133
|
+
}
|
|
134
|
+
const reason = error instanceof OAuthTokenError ? `HTTP ${error.status} ${error.body.slice(0, 200)}` : error instanceof Error ? error.message : String(error);
|
|
135
|
+
log(`refresh FAILED ${id.slice(0, 12)} (${permanent ? "permanent, marked stale" : "transient, retry next pass"}): ${reason}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (dirty) await writeJsonAtomic(accountsPath, doc);
|
|
139
|
+
state.lastPass = {
|
|
140
|
+
at: Date.now(),
|
|
141
|
+
checked,
|
|
142
|
+
refreshed,
|
|
143
|
+
needsLogin
|
|
144
|
+
};
|
|
145
|
+
if (refreshed > 0 || needsLogin.length > 0) log(`pass done: checked=${checked} refreshed=${refreshed} needsLogin=${needsLogin.length}`);
|
|
146
|
+
}
|
|
147
|
+
async function main() {
|
|
148
|
+
const other = alreadyRunning();
|
|
149
|
+
if (other !== void 0) {
|
|
150
|
+
log(`another keeper is already running (pid ${other}); exiting`);
|
|
151
|
+
process.exit(0);
|
|
152
|
+
}
|
|
153
|
+
const state = {
|
|
154
|
+
version: 1,
|
|
155
|
+
pid: process.pid,
|
|
156
|
+
startedAt: Date.now(),
|
|
157
|
+
heartbeatAt: Date.now(),
|
|
158
|
+
...process.env.DSH_CODEARTS_PLUGIN_VERSION === void 0 ? {} : { pluginVersion: process.env.DSH_CODEARTS_PLUGIN_VERSION }
|
|
159
|
+
};
|
|
160
|
+
writeKeeperState(state, home);
|
|
161
|
+
log(`keeper started pid=${process.pid}`);
|
|
162
|
+
let stopped = false;
|
|
163
|
+
const shutdown = (reason) => {
|
|
164
|
+
if (stopped) return;
|
|
165
|
+
stopped = true;
|
|
166
|
+
log(`keeper stopping: ${reason}`);
|
|
167
|
+
clearKeeperState(home);
|
|
168
|
+
process.exit(0);
|
|
169
|
+
};
|
|
170
|
+
for (const signal of [
|
|
171
|
+
"SIGINT",
|
|
172
|
+
"SIGTERM",
|
|
173
|
+
"SIGHUP"
|
|
174
|
+
]) process.on(signal, () => shutdown(`signal ${signal}`));
|
|
175
|
+
process.on("exit", () => {
|
|
176
|
+
if (!stopped) clearKeeperState(home);
|
|
177
|
+
});
|
|
178
|
+
/** 心跳:卡片据此判断「这个 pid 还活着」,也是插件判断能不能让路的依据。 */
|
|
179
|
+
setInterval(() => {
|
|
180
|
+
state.heartbeatAt = Date.now();
|
|
181
|
+
writeKeeperState(state, home);
|
|
182
|
+
if (stopRequested(home)) shutdown("stop requested by plugin");
|
|
183
|
+
}, KEEPER_HEARTBEAT_MS);
|
|
184
|
+
const loop = async () => {
|
|
185
|
+
if (stopped) return;
|
|
186
|
+
if (stopRequested(home)) {
|
|
187
|
+
shutdown("stop requested by plugin");
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
try {
|
|
191
|
+
await tick(state);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
log(`pass failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
194
|
+
}
|
|
195
|
+
state.heartbeatAt = Date.now();
|
|
196
|
+
writeKeeperState(state, home);
|
|
197
|
+
if (stopped) return;
|
|
198
|
+
setTimeout(() => void loop(), KEEPER_TICK_MS);
|
|
199
|
+
};
|
|
200
|
+
loop();
|
|
201
|
+
}
|
|
202
|
+
main().catch((error) => {
|
|
203
|
+
log(`keeper fatal: ${error instanceof Error ? error.message : String(error)}`);
|
|
204
|
+
try {
|
|
205
|
+
if (existsSync(keeperStatePath(home))) unlinkSync(keeperStatePath(home));
|
|
206
|
+
} catch {}
|
|
207
|
+
process.exit(1);
|
|
208
|
+
});
|
|
209
|
+
//#endregion
|
|
210
|
+
export {};
|