dsh-token-dash 2.3.1 → 2.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.en.md +8 -5
- package/README.md +9 -5
- package/client.js +450 -133
- package/index.js +86 -17
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +3 -2
- package/src/adapters.js +608 -561
- package/src/core.js +936 -887
- package/src/diagnostics.js +160 -108
- package/src/quota-sources.js +228 -224
- package/src/version.js +44 -37
package/src/diagnostics.js
CHANGED
|
@@ -1,114 +1,166 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Diagnostic JSONL log: module prefixes, redaction, rotation and export.
|
|
3
|
-
* Never writes credentials, prompts, bodies or full URLs.
|
|
4
|
-
*/
|
|
5
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
|
6
|
-
import { join } from 'node:path'
|
|
7
|
-
|
|
8
|
-
export const MAX_FILE =
|
|
9
|
-
export const MAX_EXPORT = 10 * 1024 * 1024
|
|
10
|
-
const KEEP_FILES =
|
|
11
|
-
|
|
12
|
-
const SECRET_PATTERNS = [
|
|
13
|
-
/sk-[a-zA-Z0-9_-]{8,}/g,
|
|
14
|
-
/Bearer\s+[^\s"']+/gi,
|
|
15
|
-
/("?(?:api[_-]?key|authorization|cookie|token|secret)"?\s*[:=]\s*")[^"]+"/gi,
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
export function redact(value) {
|
|
19
|
-
let text = String(value ?? '')
|
|
20
|
-
for (const pattern of SECRET_PATTERNS) text = text.replace(pattern, (match, prefix) => prefix === undefined ? '[REDACTED]' : `${prefix}[REDACTED]"`)
|
|
21
|
-
return text
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function hasSecret(value) {
|
|
25
|
-
return SECRET_PATTERNS.some((pattern) => {
|
|
26
|
-
pattern.lastIndex = 0
|
|
27
|
-
return pattern.test(String(value ?? ''))
|
|
28
|
-
})
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function pad(value) {
|
|
32
|
-
return String(value).padStart(2, '0')
|
|
33
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Diagnostic JSONL log: module prefixes, redaction, rotation and export.
|
|
3
|
+
* Never writes credentials, prompts, bodies or full URLs.
|
|
4
|
+
*/
|
|
5
|
+
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
export const MAX_FILE = 2 * 1024 * 1024
|
|
9
|
+
export const MAX_EXPORT = 10 * 1024 * 1024
|
|
10
|
+
const KEEP_FILES = 2
|
|
11
|
+
|
|
12
|
+
const SECRET_PATTERNS = [
|
|
13
|
+
/sk-[a-zA-Z0-9_-]{8,}/g,
|
|
14
|
+
/Bearer\s+[^\s"']+/gi,
|
|
15
|
+
/("?(?:api[_-]?key|authorization|cookie|token|secret)"?\s*[:=]\s*")[^"]+"/gi,
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
export function redact(value) {
|
|
19
|
+
let text = String(value ?? '')
|
|
20
|
+
for (const pattern of SECRET_PATTERNS) text = text.replace(pattern, (match, prefix) => prefix === undefined ? '[REDACTED]' : `${prefix}[REDACTED]"`)
|
|
21
|
+
return text
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function hasSecret(value) {
|
|
25
|
+
return SECRET_PATTERNS.some((pattern) => {
|
|
26
|
+
pattern.lastIndex = 0
|
|
27
|
+
return pattern.test(String(value ?? ''))
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function pad(value) {
|
|
32
|
+
return String(value).padStart(2, '0')
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function logFileFor(ts, dir) {
|
|
36
|
+
const d = new Date(ts)
|
|
37
|
+
return join(dir, `dsh-token-dash-${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}.log`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function rotate(file) {
|
|
41
|
+
try {
|
|
42
|
+
if (!existsSync(file) || statSync(file).size < MAX_FILE) return
|
|
43
|
+
const base = file.replace(/\.log$/, '')
|
|
44
|
+
for (let index = KEEP_FILES - 1; index >= 1; index -= 1) {
|
|
45
|
+
const from = index === 1 ? file : `${base}.${index - 1}.log`
|
|
46
|
+
const to = `${base}.${index}.log`
|
|
47
|
+
if (existsSync(from)) writeFileSync(to, readFileSync(from))
|
|
48
|
+
}
|
|
49
|
+
writeFileSync(file, '')
|
|
50
|
+
} catch {
|
|
51
|
+
// logging must never break the plugin
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 保留策略:先按天删,再按总量删最旧;永不删除当前活动文件。 */
|
|
56
|
+
export const KEEP_DAYS = 14
|
|
57
|
+
export const MAX_TOTAL = 20 * 1024 * 1024
|
|
34
58
|
|
|
35
|
-
|
|
36
|
-
const d = new Date(ts)
|
|
37
|
-
return join(dir, `dsh-token-dash-${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}.log`)
|
|
38
|
-
}
|
|
59
|
+
const LOG_NAME = /^dsh-token-dash-(\d{8})(?:\.\d+)?\.log$/
|
|
39
60
|
|
|
40
|
-
|
|
61
|
+
/**
|
|
62
|
+
* 清理旧日志:天数与总量双上限,取更严的一侧;`activeFile` 无论多旧都不删。
|
|
63
|
+
* 失败只返回已完成的量,绝不抛出——日志治理不能拖垮插件。
|
|
64
|
+
*/
|
|
65
|
+
export function pruneLogs({ dir, activeFile, days = KEEP_DAYS, totalBytes = MAX_TOTAL, now = Date.now() } = {}) {
|
|
66
|
+
let removed = 0
|
|
67
|
+
let bytes = 0
|
|
68
|
+
if (dir === undefined) return { removed, bytes }
|
|
69
|
+
const drop = (item) => {
|
|
70
|
+
try {
|
|
71
|
+
unlinkSync(item.full)
|
|
72
|
+
removed += 1
|
|
73
|
+
bytes += item.size
|
|
74
|
+
} catch { /* 单个文件删不掉不影响其余 */ }
|
|
75
|
+
}
|
|
41
76
|
try {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
77
|
+
const entries = readdirSync(dir)
|
|
78
|
+
.map((name) => {
|
|
79
|
+
const matched = LOG_NAME.exec(name)
|
|
80
|
+
if (matched === null) return null
|
|
81
|
+
const full = join(dir, name)
|
|
82
|
+
const stamp = matched[1]
|
|
83
|
+
const day = new Date(Number(stamp.slice(0, 4)), Number(stamp.slice(4, 6)) - 1, Number(stamp.slice(6, 8))).getTime()
|
|
84
|
+
let size = 0
|
|
85
|
+
try { size = statSync(full).size } catch { return null }
|
|
86
|
+
return { full, day, size }
|
|
87
|
+
})
|
|
88
|
+
.filter((item) => item !== null)
|
|
89
|
+
.sort((a, b) => a.day - b.day)
|
|
90
|
+
const cutoff = now - days * 86400_000
|
|
91
|
+
const kept = []
|
|
92
|
+
for (const item of entries) {
|
|
93
|
+
if (item.full === activeFile) { kept.push(item); continue }
|
|
94
|
+
if (item.day < cutoff) { drop(item); continue }
|
|
95
|
+
kept.push(item)
|
|
48
96
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
export function createLogger({ dir, level = 'info', version = '0.2.0', dshVersion = '0.2.1-rc.5' } = {}) {
|
|
56
|
-
if (dir === undefined) return { log() {}, info() {}, warn() {}, error() {}, file: null }
|
|
57
|
-
mkdirSync(dir, { recursive: true })
|
|
58
|
-
const file = logFileFor(Date.now(), dir)
|
|
59
|
-
const header = {
|
|
60
|
-
type: 'header',
|
|
61
|
-
plugin: 'dsh-token-dash',
|
|
62
|
-
version,
|
|
63
|
-
dshVersion,
|
|
64
|
-
platform: process.platform,
|
|
65
|
-
exportedAt: new Date().toISOString(),
|
|
66
|
-
}
|
|
67
|
-
let wroteHeader = false
|
|
68
|
-
const write = (module, event, fields = {}, levelName = 'info') => {
|
|
69
|
-
if (level !== 'debug' && levelName === 'debug') return
|
|
70
|
-
try {
|
|
71
|
-
rotate(file)
|
|
72
|
-
if (!wroteHeader) {
|
|
73
|
-
appendFileSync(file, `${JSON.stringify(header)}\n`, 'utf8')
|
|
74
|
-
wroteHeader = true
|
|
75
|
-
}
|
|
76
|
-
const line = {
|
|
77
|
-
ts: new Date().toISOString(),
|
|
78
|
-
level: levelName,
|
|
79
|
-
module,
|
|
80
|
-
prefix: `[TD:${module}]`,
|
|
81
|
-
event,
|
|
82
|
-
...Object.fromEntries(Object.entries(fields).map(([key, value]) => [key, typeof value === 'string' ? redact(value) : value])),
|
|
83
|
-
}
|
|
84
|
-
appendFileSync(file, `${JSON.stringify(line)}\n`, 'utf8')
|
|
85
|
-
} catch {
|
|
86
|
-
// ignore
|
|
97
|
+
let total = kept.reduce((sum, item) => sum + item.size, 0)
|
|
98
|
+
for (const item of kept) {
|
|
99
|
+
if (total <= totalBytes) break
|
|
100
|
+
if (item.full === activeFile) continue
|
|
101
|
+
drop(item)
|
|
102
|
+
total -= item.size
|
|
87
103
|
}
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
file,
|
|
91
|
-
log: write,
|
|
92
|
-
info: (module, event, fields) => write(module, event, fields, 'info'),
|
|
93
|
-
warn: (module, event, fields) => write(module, event, fields, 'warn'),
|
|
94
|
-
error: (module, event, fields) => write(module, event, fields, 'error'),
|
|
95
|
-
debug: (module, event, fields) => write(module, event, fields, 'debug'),
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function exportLog(dir, logger) {
|
|
100
|
-
const file = logger?.file ?? (dir === undefined ? null : logFileFor(Date.now(), dir))
|
|
101
|
-
if (file === null || !existsSync(file)) {
|
|
102
|
-
return { file, content: `${JSON.stringify({ type: 'header', plugin: 'dsh-token-dash', exportedAt: new Date().toISOString() })}\n`, bytes: 0 }
|
|
103
|
-
}
|
|
104
|
-
let content = readFileSync(file, 'utf8')
|
|
105
|
-
if (Buffer.byteLength(content, 'utf8') > MAX_EXPORT) content = content.slice(-MAX_EXPORT)
|
|
106
|
-
const redacted = redact(content)
|
|
107
|
-
if (hasSecret(content)) return { file, content: redacted, bytes: Buffer.byteLength(redacted, 'utf8'), redacted: true }
|
|
108
|
-
return { file, content, bytes: Buffer.byteLength(content, 'utf8'), redacted: false }
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export function clearLog(dir, logger) {
|
|
112
|
-
const file = logger?.file ?? (dir === undefined ? null : logFileFor(Date.now(), dir))
|
|
113
|
-
if (file !== null && existsSync(file)) writeFileSync(file, '')
|
|
104
|
+
} catch { /* 目录不可读时按无操作处理 */ }
|
|
105
|
+
return { removed, bytes }
|
|
114
106
|
}
|
|
107
|
+
export function createLogger({ dir, level = 'info', version = '0.2.0', dshVersion = '0.2.1-rc.5' } = {}) {
|
|
108
|
+
if (dir === undefined) return { log() {}, info() {}, warn() {}, error() {}, file: null }
|
|
109
|
+
mkdirSync(dir, { recursive: true })
|
|
110
|
+
const file = logFileFor(Date.now(), dir)
|
|
111
|
+
const header = {
|
|
112
|
+
type: 'header',
|
|
113
|
+
plugin: 'dsh-token-dash',
|
|
114
|
+
version,
|
|
115
|
+
dshVersion,
|
|
116
|
+
platform: process.platform,
|
|
117
|
+
exportedAt: new Date().toISOString(),
|
|
118
|
+
}
|
|
119
|
+
let wroteHeader = false
|
|
120
|
+
const write = (module, event, fields = {}, levelName = 'info') => {
|
|
121
|
+
if (level !== 'debug' && levelName === 'debug') return
|
|
122
|
+
try {
|
|
123
|
+
rotate(file)
|
|
124
|
+
if (!wroteHeader) {
|
|
125
|
+
appendFileSync(file, `${JSON.stringify(header)}\n`, 'utf8')
|
|
126
|
+
wroteHeader = true
|
|
127
|
+
}
|
|
128
|
+
const line = {
|
|
129
|
+
ts: new Date().toISOString(),
|
|
130
|
+
level: levelName,
|
|
131
|
+
module,
|
|
132
|
+
prefix: `[TD:${module}]`,
|
|
133
|
+
event,
|
|
134
|
+
...Object.fromEntries(Object.entries(fields).map(([key, value]) => [key, typeof value === 'string' ? redact(value) : value])),
|
|
135
|
+
}
|
|
136
|
+
appendFileSync(file, `${JSON.stringify(line)}\n`, 'utf8')
|
|
137
|
+
} catch {
|
|
138
|
+
// ignore
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
file,
|
|
143
|
+
log: write,
|
|
144
|
+
info: (module, event, fields) => write(module, event, fields, 'info'),
|
|
145
|
+
warn: (module, event, fields) => write(module, event, fields, 'warn'),
|
|
146
|
+
error: (module, event, fields) => write(module, event, fields, 'error'),
|
|
147
|
+
debug: (module, event, fields) => write(module, event, fields, 'debug'),
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function exportLog(dir, logger) {
|
|
152
|
+
const file = logger?.file ?? (dir === undefined ? null : logFileFor(Date.now(), dir))
|
|
153
|
+
if (file === null || !existsSync(file)) {
|
|
154
|
+
return { file, content: `${JSON.stringify({ type: 'header', plugin: 'dsh-token-dash', exportedAt: new Date().toISOString() })}\n`, bytes: 0 }
|
|
155
|
+
}
|
|
156
|
+
let content = readFileSync(file, 'utf8')
|
|
157
|
+
if (Buffer.byteLength(content, 'utf8') > MAX_EXPORT) content = content.slice(-MAX_EXPORT)
|
|
158
|
+
const redacted = redact(content)
|
|
159
|
+
if (hasSecret(content)) return { file, content: redacted, bytes: Buffer.byteLength(redacted, 'utf8'), redacted: true }
|
|
160
|
+
return { file, content, bytes: Buffer.byteLength(content, 'utf8'), redacted: false }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function clearLog(dir, logger) {
|
|
164
|
+
const file = logger?.file ?? (dir === undefined ? null : logFileFor(Date.now(), dir))
|
|
165
|
+
if (file !== null && existsSync(file)) writeFileSync(file, '')
|
|
166
|
+
}
|