@cowwo/dsh-lookout 0.2.0 → 0.5.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 +34 -8
- package/client/client.js +938 -1084
- package/lib/index.js +29 -3
- package/lib/notify/config.js +119 -153
- package/lib/notify/index.js +47 -112
- package/lib/notify/render.js +4 -18
- package/lib/notify/send/index.js +10 -13
- package/lib/notify/service.js +4 -16
- package/lib/notify/test-send.js +5 -8
- package/lib/notify/typert.host.js +2 -24
- package/lib/typert.host.js +2 -23
- package/package.json +1 -1
- package/lib/notify/state.js +0 -102
package/lib/notify/state.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
// dsh-notify-p · 投递账本(落盘,用于"到底发没发出去"的可验证性)
|
|
2
|
-
//
|
|
3
|
-
// 为什么需要它:cordis 的控制台 exporter 默认只输出 error 级,
|
|
4
|
-
// 插件的 info/warn 日志**不会**出现在 dsh 的 stdout 里——
|
|
5
|
-
// 所以"看日志确认"是不可行的。这里把每次投递结果落成一个 JSON 文件,
|
|
6
|
-
// 出问题时 `cat $DSH_HOME/dsh-notify-p/state.json` 一眼就能看到。
|
|
7
|
-
//
|
|
8
|
-
// 只写真值,不写密钥、不写正文。
|
|
9
|
-
|
|
10
|
-
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
11
|
-
import { homedir } from 'node:os';
|
|
12
|
-
import { dirname, join } from 'node:path';
|
|
13
|
-
|
|
14
|
-
/** 账本保留的最近投递条数。 */
|
|
15
|
-
const RECENT_LIMIT = 50;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 解析账本路径:配置优先,其次 $DSH_HOME/dsh-notify-p/state.json,
|
|
19
|
-
* 最后回退 ~/.dsh/dsh-notify-p/state.json。
|
|
20
|
-
* @param {string} [configured] 配置里的 statePath
|
|
21
|
-
* @returns {string} 绝对路径
|
|
22
|
-
*/
|
|
23
|
-
export function resolveStatePath(configured) {
|
|
24
|
-
if (typeof configured === 'string' && configured.trim() !== '') return configured.trim();
|
|
25
|
-
const home = typeof process.env.DSH_HOME === 'string' && process.env.DSH_HOME !== ''
|
|
26
|
-
? process.env.DSH_HOME
|
|
27
|
-
: join(homedir(), '.dsh');
|
|
28
|
-
return join(home, 'dsh-notify-p', 'state.json');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 建一个投递账本。
|
|
33
|
-
* @param {object} [options] 参数
|
|
34
|
-
* @param {string} [options.path] 账本路径
|
|
35
|
-
* @param {object} [options.logger] 日志器
|
|
36
|
-
* @returns {{ record: (entry: object) => void, snapshot: () => object, flush: () => void, path: string }}
|
|
37
|
-
*/
|
|
38
|
-
export function createLedger(options = {}) {
|
|
39
|
-
const logger = options.logger ?? {};
|
|
40
|
-
const path = resolveStatePath(options.path);
|
|
41
|
-
const startedAt = new Date().toISOString();
|
|
42
|
-
/** @type {object[]} */
|
|
43
|
-
const recent = [];
|
|
44
|
-
const counts = { sent: 0, failed: 0 };
|
|
45
|
-
|
|
46
|
-
const persist = () => {
|
|
47
|
-
const payload = {
|
|
48
|
-
version: 1,
|
|
49
|
-
startedAt,
|
|
50
|
-
updatedAt: new Date().toISOString(),
|
|
51
|
-
counts,
|
|
52
|
-
recent,
|
|
53
|
-
};
|
|
54
|
-
try {
|
|
55
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
56
|
-
writeFileSync(path, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
|
|
57
|
-
} catch (error) {
|
|
58
|
-
// 账本写不进去不能影响发信本身
|
|
59
|
-
logger.debug?.(`dsh-notify-p: 账本写入失败 / ledger write failed: ${String(error)}`);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
// 进程一起来就先落一个文件,避免"文件不存在"被误读成"插件没加载"
|
|
64
|
-
persist();
|
|
65
|
-
|
|
66
|
-
return {
|
|
67
|
-
path,
|
|
68
|
-
/**
|
|
69
|
-
* 记一条投递结果。
|
|
70
|
-
* @param {object} entry 结果(at/ok/outcome/sessionId/subject/error/attempts)
|
|
71
|
-
* @returns {void}
|
|
72
|
-
*/
|
|
73
|
-
record(entry) {
|
|
74
|
-
if (entry && entry.ok === true) counts.sent += 1;
|
|
75
|
-
else counts.failed += 1;
|
|
76
|
-
recent.push(entry);
|
|
77
|
-
while (recent.length > RECENT_LIMIT) recent.shift();
|
|
78
|
-
persist();
|
|
79
|
-
},
|
|
80
|
-
/**
|
|
81
|
-
* 当前账本内容。
|
|
82
|
-
* @returns {object}
|
|
83
|
-
*/
|
|
84
|
-
snapshot() {
|
|
85
|
-
return { version: 1, startedAt, updatedAt: new Date().toISOString(), counts, recent: [...recent] };
|
|
86
|
-
},
|
|
87
|
-
flush: persist,
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 读一个账本文件(排错用)。
|
|
93
|
-
* @param {string} path 账本路径
|
|
94
|
-
* @returns {object|undefined}
|
|
95
|
-
*/
|
|
96
|
-
export function readLedger(path) {
|
|
97
|
-
try {
|
|
98
|
-
return JSON.parse(readFileSync(path, 'utf8'));
|
|
99
|
-
} catch {
|
|
100
|
-
return undefined;
|
|
101
|
-
}
|
|
102
|
-
}
|