@dmall-inc/wd-cli 0.0.0-stage → 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/README.md +401 -2
- package/bin/wd-cli.js +106 -0
- package/docs/design-references.md +75 -0
- package/lib/args.js +299 -0
- package/lib/auth.js +77 -0
- package/lib/commands/agent.js +784 -0
- package/lib/commands/api.js +122 -0
- package/lib/commands/auth.js +314 -0
- package/lib/commands/automation.js +92 -0
- package/lib/commands/completion.js +141 -0
- package/lib/commands/config.js +120 -0
- package/lib/commands/credential.js +108 -0
- package/lib/commands/dataset.js +144 -0
- package/lib/commands/evaluate.js +170 -0
- package/lib/commands/help.js +81 -0
- package/lib/commands/index.js +46 -0
- package/lib/commands/kb.js +449 -0
- package/lib/commands/observability.js +381 -0
- package/lib/commands/session.js +189 -0
- package/lib/commands/skill.js +463 -0
- package/lib/commands/workspace.js +110 -0
- package/lib/config.js +269 -0
- package/lib/data.js +128 -0
- package/lib/http.js +402 -0
- package/lib/jq.js +342 -0
- package/lib/keyring.js +125 -0
- package/lib/oidc.js +344 -0
- package/lib/output.js +249 -0
- package/package.json +22 -4
package/lib/jq.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* jq 子集(零依赖)。对标 `gh --jq`,但只实现日常筛选够用的那部分:
|
|
5
|
+
*
|
|
6
|
+
* . 当前值
|
|
7
|
+
* .foo.bar 路径访问(缺失即 null)
|
|
8
|
+
* .[] 数组展开(后续管道段逐项作用)
|
|
9
|
+
* .[2] 下标
|
|
10
|
+
* | 管道
|
|
11
|
+
* select(EXPR) 过滤,EXPR 支持 ==、!=、比较、and/or、以及 test/startswith/endswith/contains
|
|
12
|
+
* length / keys / type / not
|
|
13
|
+
* "文本 \(.expr)" 字符串插值
|
|
14
|
+
* [a, b] / {k: .v} 构造(仅一层,够拼输出)
|
|
15
|
+
*
|
|
16
|
+
* 刻意不做完整 jq:表达式一复杂就明确报错并建议管道给系统 jq,
|
|
17
|
+
* 而不是悄悄给出一个「看起来对」的错结果。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
class JqUnsupported extends Error {
|
|
21
|
+
constructor(expr, detail) {
|
|
22
|
+
super(
|
|
23
|
+
`--jq 表达式暂不支持:${detail || expr}。` +
|
|
24
|
+
`内置的是 jq 子集(路径 / .[] / select / 比较 / 字符串插值);` +
|
|
25
|
+
`复杂表达式请改用 --output json 后管道给系统 jq:wd-cli ... --output json | jq '${expr}'`
|
|
26
|
+
);
|
|
27
|
+
this.name = 'JqUnsupported';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 按顶层 | 切分,忽略括号 / 方括号 / 引号内的竖线。 */
|
|
32
|
+
function splitPipes(expr) {
|
|
33
|
+
const parts = [];
|
|
34
|
+
let depth = 0;
|
|
35
|
+
let quote = null;
|
|
36
|
+
let current = '';
|
|
37
|
+
for (let i = 0; i < expr.length; i += 1) {
|
|
38
|
+
const ch = expr[i];
|
|
39
|
+
if (quote) {
|
|
40
|
+
current += ch;
|
|
41
|
+
if (ch === '\\') {
|
|
42
|
+
current += expr[i + 1] || '';
|
|
43
|
+
i += 1;
|
|
44
|
+
} else if (ch === quote) {
|
|
45
|
+
quote = null;
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (ch === '"' || ch === "'") {
|
|
50
|
+
quote = ch;
|
|
51
|
+
current += ch;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (ch === '(' || ch === '[' || ch === '{') depth += 1;
|
|
55
|
+
if (ch === ')' || ch === ']' || ch === '}') depth -= 1;
|
|
56
|
+
if (ch === '|' && depth === 0) {
|
|
57
|
+
parts.push(current.trim());
|
|
58
|
+
current = '';
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
current += ch;
|
|
62
|
+
}
|
|
63
|
+
parts.push(current.trim());
|
|
64
|
+
return parts.filter((p) => p.length > 0);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 顶层按分隔符切分(用于 select 内的 and/or、数组构造的逗号)。 */
|
|
68
|
+
function splitTop(expr, separator) {
|
|
69
|
+
const parts = [];
|
|
70
|
+
let depth = 0;
|
|
71
|
+
let quote = null;
|
|
72
|
+
let current = '';
|
|
73
|
+
let i = 0;
|
|
74
|
+
while (i < expr.length) {
|
|
75
|
+
const ch = expr[i];
|
|
76
|
+
if (quote) {
|
|
77
|
+
current += ch;
|
|
78
|
+
if (ch === '\\') {
|
|
79
|
+
current += expr[i + 1] || '';
|
|
80
|
+
i += 1;
|
|
81
|
+
} else if (ch === quote) quote = null;
|
|
82
|
+
i += 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (ch === '"' || ch === "'") {
|
|
86
|
+
quote = ch;
|
|
87
|
+
current += ch;
|
|
88
|
+
i += 1;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (ch === '(' || ch === '[' || ch === '{') depth += 1;
|
|
92
|
+
if (ch === ')' || ch === ']' || ch === '}') depth -= 1;
|
|
93
|
+
if (depth === 0 && expr.startsWith(separator, i) && isBoundary(expr, i, separator)) {
|
|
94
|
+
parts.push(current.trim());
|
|
95
|
+
current = '';
|
|
96
|
+
i += separator.length;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
current += ch;
|
|
100
|
+
i += 1;
|
|
101
|
+
}
|
|
102
|
+
parts.push(current.trim());
|
|
103
|
+
return parts;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function isBoundary(expr, index, separator) {
|
|
107
|
+
if (!/^[a-z]+$/.test(separator)) return true;
|
|
108
|
+
const before = index === 0 ? ' ' : expr[index - 1];
|
|
109
|
+
const after = expr[index + separator.length] || ' ';
|
|
110
|
+
return /\s|\(|\)/.test(before) && /\s|\(|\)/.test(after);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 路径取值:.a.b.c / .a[0] / .["k"];任一段缺失即 null。 */
|
|
114
|
+
function getPath(value, path) {
|
|
115
|
+
const segments = [];
|
|
116
|
+
const re = /\.([A-Za-z_][A-Za-z0-9_]*)|\[(\d+)\]|\["([^"]+)"\]/g;
|
|
117
|
+
let match;
|
|
118
|
+
// `.[0]` 的点只是「从根开始」的标记,下标紧随其后:跳过它再逐段匹配
|
|
119
|
+
let consumed = path.startsWith('.[') ? 1 : 0;
|
|
120
|
+
re.lastIndex = consumed;
|
|
121
|
+
while ((match = re.exec(path)) !== null) {
|
|
122
|
+
if (match.index !== consumed) break;
|
|
123
|
+
consumed = re.lastIndex;
|
|
124
|
+
if (match[1] !== undefined) segments.push(match[1]);
|
|
125
|
+
else if (match[2] !== undefined) segments.push(Number(match[2]));
|
|
126
|
+
else segments.push(match[3]);
|
|
127
|
+
}
|
|
128
|
+
if (consumed !== path.length) throw new JqUnsupported(path, `路径 ${path}`);
|
|
129
|
+
let current = value;
|
|
130
|
+
for (const seg of segments) {
|
|
131
|
+
if (current === null || current === undefined) return null;
|
|
132
|
+
current = current[seg];
|
|
133
|
+
}
|
|
134
|
+
return current === undefined ? null : current;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function parseLiteral(token) {
|
|
138
|
+
const text = token.trim();
|
|
139
|
+
if (text === 'true') return { ok: true, value: true };
|
|
140
|
+
if (text === 'false') return { ok: true, value: false };
|
|
141
|
+
if (text === 'null') return { ok: true, value: null };
|
|
142
|
+
if (/^-?\d+(\.\d+)?$/.test(text)) return { ok: true, value: Number(text) };
|
|
143
|
+
if (/^"([^"\\]|\\.)*"$/.test(text)) {
|
|
144
|
+
// 含 \( ) 插值的字符串交给 interpolate 处理,JSON.parse 会在 \( 上炸
|
|
145
|
+
if (text.includes('\\(')) return { ok: false, interpolate: true };
|
|
146
|
+
return { ok: true, value: JSON.parse(text) };
|
|
147
|
+
}
|
|
148
|
+
if (/^'([^'\\]|\\.)*'$/.test(text)) return { ok: true, value: text.slice(1, -1) };
|
|
149
|
+
return { ok: false };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 字符串插值:"共 \(.total) 条"。 */
|
|
153
|
+
function interpolate(item, literal) {
|
|
154
|
+
const raw = literal.slice(1, -1);
|
|
155
|
+
return raw.replace(/\\\(([^)]*)\)/g, (_, inner) => {
|
|
156
|
+
const value = evalExpr(item, inner.trim());
|
|
157
|
+
return value === null || value === undefined
|
|
158
|
+
? ''
|
|
159
|
+
: typeof value === 'object'
|
|
160
|
+
? JSON.stringify(value)
|
|
161
|
+
: String(value);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const COMPARATORS = ['==', '!=', '>=', '<=', '>', '<'];
|
|
166
|
+
|
|
167
|
+
function evalExpr(item, expr) {
|
|
168
|
+
const text = expr.trim();
|
|
169
|
+
if (text === '' || text === '.') return item;
|
|
170
|
+
|
|
171
|
+
// 管道优先:select(.name | startswith("x")) 这类嵌套里也会出现
|
|
172
|
+
const piped = splitPipes(text);
|
|
173
|
+
if (piped.length > 1) {
|
|
174
|
+
let current = item;
|
|
175
|
+
for (const stage of piped) {
|
|
176
|
+
current = evalExpr(current, stage);
|
|
177
|
+
if (current === undefined) return undefined;
|
|
178
|
+
}
|
|
179
|
+
return current;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// and / or(左结合,先切 or 再切 and)
|
|
183
|
+
for (const op of ['or', 'and']) {
|
|
184
|
+
const parts = splitTop(text, op);
|
|
185
|
+
if (parts.length > 1) {
|
|
186
|
+
const values = parts.map((p) => truthy(evalExpr(item, p)));
|
|
187
|
+
return op === 'or' ? values.some(Boolean) : values.every(Boolean);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
for (const op of COMPARATORS) {
|
|
192
|
+
const parts = splitTop(text, op);
|
|
193
|
+
if (parts.length === 2) {
|
|
194
|
+
const left = evalOperand(item, parts[0]);
|
|
195
|
+
const right = evalOperand(item, parts[1]);
|
|
196
|
+
switch (op) {
|
|
197
|
+
case '==':
|
|
198
|
+
return left === right;
|
|
199
|
+
case '!=':
|
|
200
|
+
return left !== right;
|
|
201
|
+
case '>=':
|
|
202
|
+
return left >= right;
|
|
203
|
+
case '<=':
|
|
204
|
+
return left <= right;
|
|
205
|
+
case '>':
|
|
206
|
+
return left > right;
|
|
207
|
+
default:
|
|
208
|
+
return left < right;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return evalOperand(item, text);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function evalOperand(item, expr) {
|
|
217
|
+
const text = expr.trim();
|
|
218
|
+
if (text === '.' || text === '') return item;
|
|
219
|
+
|
|
220
|
+
// 字符串插值先于字面量:"共 \(.total) 条"
|
|
221
|
+
if (/^"/.test(text) && text.endsWith('"') && text.includes('\\(')) {
|
|
222
|
+
return interpolate(item, text);
|
|
223
|
+
}
|
|
224
|
+
const literal = parseLiteral(text);
|
|
225
|
+
if (literal.ok) return literal.value;
|
|
226
|
+
|
|
227
|
+
if (text.startsWith('(') && text.endsWith(')')) return evalExpr(item, text.slice(1, -1));
|
|
228
|
+
|
|
229
|
+
// 函数调用
|
|
230
|
+
const fn = text.match(/^([a-z_]+)\s*\((.*)\)$/s);
|
|
231
|
+
if (fn) return callFunction(item, fn[1], fn[2]);
|
|
232
|
+
if (/^[a-z_]+$/.test(text)) return callFunction(item, text, null);
|
|
233
|
+
|
|
234
|
+
// 数组 / 对象构造
|
|
235
|
+
if (text.startsWith('[') && text.endsWith(']')) {
|
|
236
|
+
const inner = text.slice(1, -1).trim();
|
|
237
|
+
if (!inner) return [];
|
|
238
|
+
return splitTop(inner, ',').map((part) => evalExpr(item, part));
|
|
239
|
+
}
|
|
240
|
+
if (text.startsWith('{') && text.endsWith('}')) {
|
|
241
|
+
const out = {};
|
|
242
|
+
for (const pair of splitTop(text.slice(1, -1), ',')) {
|
|
243
|
+
const idx = pair.indexOf(':');
|
|
244
|
+
if (idx < 0) throw new JqUnsupported(text, `对象构造 ${pair}`);
|
|
245
|
+
const key = pair.slice(0, idx).trim().replace(/^"|"$/g, '');
|
|
246
|
+
out[key] = evalExpr(item, pair.slice(idx + 1));
|
|
247
|
+
}
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (text.startsWith('.')) return getPath(item, text);
|
|
252
|
+
throw new JqUnsupported(text);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function callFunction(item, name, argsRaw) {
|
|
256
|
+
const args = argsRaw === null ? [] : splitTop(argsRaw, ',').filter((a) => a !== '');
|
|
257
|
+
const arg0 = () => evalOperand(item, args[0] || '.');
|
|
258
|
+
switch (name) {
|
|
259
|
+
case 'length': {
|
|
260
|
+
if (item === null || item === undefined) return 0;
|
|
261
|
+
if (Array.isArray(item) || typeof item === 'string') return item.length;
|
|
262
|
+
if (typeof item === 'object') return Object.keys(item).length;
|
|
263
|
+
return 0;
|
|
264
|
+
}
|
|
265
|
+
case 'keys':
|
|
266
|
+
return item && typeof item === 'object' ? Object.keys(item).sort() : [];
|
|
267
|
+
case 'type':
|
|
268
|
+
if (item === null) return 'null';
|
|
269
|
+
if (Array.isArray(item)) return 'array';
|
|
270
|
+
return typeof item;
|
|
271
|
+
case 'not':
|
|
272
|
+
return !truthy(item);
|
|
273
|
+
case 'tostring':
|
|
274
|
+
return item === null || item === undefined ? '' : typeof item === 'object' ? JSON.stringify(item) : String(item);
|
|
275
|
+
case 'tonumber':
|
|
276
|
+
return Number(item);
|
|
277
|
+
case 'ascii_downcase':
|
|
278
|
+
return String(item).toLowerCase();
|
|
279
|
+
case 'ascii_upcase':
|
|
280
|
+
return String(item).toUpperCase();
|
|
281
|
+
case 'startswith':
|
|
282
|
+
return String(item ?? '').startsWith(String(arg0() ?? ''));
|
|
283
|
+
case 'endswith':
|
|
284
|
+
return String(item ?? '').endsWith(String(arg0() ?? ''));
|
|
285
|
+
case 'contains':
|
|
286
|
+
if (Array.isArray(item)) return item.includes(arg0());
|
|
287
|
+
return String(item ?? '').includes(String(arg0() ?? ''));
|
|
288
|
+
case 'test':
|
|
289
|
+
return new RegExp(String(arg0() ?? '')).test(String(item ?? ''));
|
|
290
|
+
case 'select':
|
|
291
|
+
return truthy(evalExpr(item, args.join(','))) ? item : undefined;
|
|
292
|
+
case 'join':
|
|
293
|
+
return Array.isArray(item) ? item.join(String(arg0() ?? '')) : String(item ?? '');
|
|
294
|
+
default:
|
|
295
|
+
throw new JqUnsupported(name, `函数 ${name}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function truthy(value) {
|
|
300
|
+
return value !== false && value !== null && value !== undefined;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** 执行一个管道段,输入输出都是「值的数组」(模拟 jq 的流)。 */
|
|
304
|
+
function applyStage(values, stage) {
|
|
305
|
+
const out = [];
|
|
306
|
+
for (const value of values) {
|
|
307
|
+
if (stage === '.[]') {
|
|
308
|
+
if (Array.isArray(value)) out.push(...value);
|
|
309
|
+
else if (value && typeof value === 'object') out.push(...Object.values(value));
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (stage.startsWith('.[]')) {
|
|
313
|
+
const rest = stage.slice(3);
|
|
314
|
+
const expanded = Array.isArray(value) ? value : value && typeof value === 'object' ? Object.values(value) : [];
|
|
315
|
+
for (const el of expanded) {
|
|
316
|
+
const result = evalExpr(el, rest.startsWith('.') ? rest : `.${rest}`);
|
|
317
|
+
if (result !== undefined) out.push(result);
|
|
318
|
+
}
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
const result = evalExpr(value, stage);
|
|
322
|
+
if (result !== undefined) out.push(result);
|
|
323
|
+
}
|
|
324
|
+
return out;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 对数据应用 jq 表达式。返回值遵循 jq 的直觉:
|
|
329
|
+
* 单个结果直接返回该值,多个结果返回数组,零结果返回 null。
|
|
330
|
+
*/
|
|
331
|
+
function applyJq(data, expr) {
|
|
332
|
+
const stages = splitPipes(String(expr));
|
|
333
|
+
let values = [data];
|
|
334
|
+
for (const stage of stages) {
|
|
335
|
+
values = applyStage(values, stage);
|
|
336
|
+
}
|
|
337
|
+
if (values.length === 0) return null;
|
|
338
|
+
if (values.length === 1) return values[0];
|
|
339
|
+
return values;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
module.exports = { applyJq, JqUnsupported, splitPipes, getPath };
|
package/lib/keyring.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 令牌存储:优先交给操作系统密钥链,不可用时回落到 hosts.json(借鉴 gh 的做法)。
|
|
5
|
+
*
|
|
6
|
+
* 零依赖实现——调系统自带的命令行工具:
|
|
7
|
+
* macOS security add-generic-password / find-generic-password / delete-generic-password
|
|
8
|
+
* Linux secret-tool store / lookup / clear(libsecret,多数桌面发行版自带)
|
|
9
|
+
* Windows 暂无零依赖方案,直接回落文件
|
|
10
|
+
*
|
|
11
|
+
* 拿不到密钥链(无图形会话的服务器、CI、未装 libsecret)是常态,不是错误:
|
|
12
|
+
* 静默回落文件,并在 auth status 里如实标注存储位置,用户想知道时能看到。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { execFileSync } = require('node:child_process');
|
|
16
|
+
|
|
17
|
+
const SERVICE = 'wd-cli';
|
|
18
|
+
|
|
19
|
+
function run(command, args, input) {
|
|
20
|
+
return execFileSync(command, args, {
|
|
21
|
+
input,
|
|
22
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
23
|
+
encoding: 'utf8',
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 当前平台是否有可用的密钥链后端。 */
|
|
28
|
+
function available(env = process.env) {
|
|
29
|
+
if (env.WD_CLI_INSECURE_STORAGE === '1') return false;
|
|
30
|
+
try {
|
|
31
|
+
if (process.platform === 'darwin') {
|
|
32
|
+
run('security', ['help']);
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (process.platform === 'linux') {
|
|
36
|
+
run('secret-tool', ['--version']);
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 存一条凭据(账号名用 profile 名)。成功返回 true。 */
|
|
46
|
+
function set(account, secret) {
|
|
47
|
+
try {
|
|
48
|
+
if (process.platform === 'darwin') {
|
|
49
|
+
// -U:已存在则更新;-w 从参数传值(内容本身不会进入 shell,execFile 不经 shell)
|
|
50
|
+
run('security', ['add-generic-password', '-U', '-s', SERVICE, '-a', account, '-w', secret]);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (process.platform === 'linux') {
|
|
54
|
+
run('secret-tool', ['store', '--label', `${SERVICE} (${account})`, 'service', SERVICE, 'account', account], secret);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
} catch {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* macOS 的 `security -w` 在密码含非 ASCII 字节时,不回原文而是回**十六进制转储**
|
|
65
|
+
* (纯 hex、无前缀)。登录态 JSON 里带用户姓名,中文名用户必然踩到:拿回来的是
|
|
66
|
+
* 一串 hex,JSON.parse 当场失败 → 登录态读不回来 → 每次都显示「未登录」。
|
|
67
|
+
*
|
|
68
|
+
* 判定从严,避免把本就是 hex 的明文误解码:整串是 hex、长度为偶数、解码后是
|
|
69
|
+
* 合法 UTF-8(无替换字符 U+FFFD)且可打印,才采用解码结果,否则原样返回。
|
|
70
|
+
*/
|
|
71
|
+
function decodeIfHexDump(value) {
|
|
72
|
+
if (!value || value.length % 2 !== 0 || !/^[0-9a-fA-F]+$/.test(value)) return value;
|
|
73
|
+
try {
|
|
74
|
+
const decoded = Buffer.from(value, 'hex').toString('utf8');
|
|
75
|
+
// 解码结果里出现替换字符说明原本就不是 UTF-8 文本,判定为误判
|
|
76
|
+
if (!decoded || decoded.includes('\uFFFD')) return value;
|
|
77
|
+
// 控制字符(除常见空白)同样说明不是文本
|
|
78
|
+
if (/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/.test(decoded)) return value;
|
|
79
|
+
return decoded;
|
|
80
|
+
} catch {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 取一条凭据;没有或取不到返回 null。 */
|
|
86
|
+
function get(account) {
|
|
87
|
+
try {
|
|
88
|
+
if (process.platform === 'darwin') {
|
|
89
|
+
const raw = run('security', ['find-generic-password', '-s', SERVICE, '-a', account, '-w']).trim();
|
|
90
|
+
return raw ? decodeIfHexDump(raw) : null;
|
|
91
|
+
}
|
|
92
|
+
if (process.platform === 'linux') {
|
|
93
|
+
return run('secret-tool', ['lookup', 'service', SERVICE, 'account', account]).trim() || null;
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 删一条凭据。 */
|
|
102
|
+
function remove(account) {
|
|
103
|
+
try {
|
|
104
|
+
if (process.platform === 'darwin') {
|
|
105
|
+
run('security', ['delete-generic-password', '-s', SERVICE, '-a', account]);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
if (process.platform === 'linux') {
|
|
109
|
+
run('secret-tool', ['clear', 'service', SERVICE, 'account', account]);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 供 auth status 显示:当前令牌存在哪儿。 */
|
|
119
|
+
function describe(env = process.env) {
|
|
120
|
+
if (env.WD_CLI_INSECURE_STORAGE === '1') return '配置文件(已显式关闭密钥链)';
|
|
121
|
+
if (!available(env)) return '配置文件(本机无可用密钥链)';
|
|
122
|
+
return process.platform === 'darwin' ? '系统钥匙串' : '系统密钥环';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = { SERVICE, available, set, get, remove, describe, decodeIfHexDump };
|