nx-sk 0.27.0 → 0.28.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 +24 -0
- package/README.md +18 -5
- package/bin/nx-sk.mjs +21 -3
- package/{src → dist}/core/argsafe.js +24 -24
- package/dist/core/attachments.js +463 -0
- package/dist/core/crypto.js +71 -0
- package/{src → dist}/core/errors.js +27 -29
- package/dist/core/fields/behaviors/bool.js +24 -0
- package/dist/core/fields/behaviors/common.js +37 -0
- package/dist/core/fields/behaviors/date.js +12 -0
- package/dist/core/fields/behaviors/entrylist.js +112 -0
- package/dist/core/fields/behaviors/file.js +38 -0
- package/dist/core/fields/behaviors/md.js +21 -0
- package/dist/core/fields/behaviors/month.js +12 -0
- package/dist/core/fields/behaviors/number.js +18 -0
- package/dist/core/fields/behaviors/secret.js +12 -0
- package/dist/core/fields/behaviors/select.js +12 -0
- package/dist/core/fields/behaviors/source.js +71 -0
- package/dist/core/fields/behaviors/table.js +185 -0
- package/dist/core/fields/behaviors/tags.js +20 -0
- package/dist/core/fields/behaviors/text.js +12 -0
- package/dist/core/fields/behaviors/textarea.js +12 -0
- package/dist/core/fields/registry.js +85 -0
- package/dist/core/fields/seal/common.js +39 -0
- package/dist/core/fields/seal/entrylist.js +46 -0
- package/dist/core/fields/seal/index.js +23 -0
- package/dist/core/fields/seal/table.js +86 -0
- package/dist/core/fields.js +828 -0
- package/dist/core/fsx.js +119 -0
- package/dist/core/ids.js +16 -0
- package/{src → dist}/core/mdimages.js +35 -35
- package/dist/core/open.js +36 -0
- package/{src → dist}/core/paths.js +57 -71
- package/dist/core/render.js +167 -0
- package/dist/core/securezip.js +68 -0
- package/dist/core/sensitive.js +27 -0
- package/{src → dist}/core/serve-port.js +59 -61
- package/dist/core/source.js +261 -0
- package/dist/core/store.js +296 -0
- package/dist/core/values.js +42 -0
- package/dist/core/vault.js +131 -0
- package/dist/core/zip.js +198 -0
- package/dist/index.js +12 -0
- package/dist/modules/entries/index.js +280 -0
- package/dist/modules/entries/service.js +1239 -0
- package/dist/modules/files/index.js +123 -0
- package/dist/modules/files/service.js +328 -0
- package/dist/modules/sections/index.js +142 -0
- package/dist/modules/sections/service.js +806 -0
- package/dist/modules/settings/index.js +32 -0
- package/dist/modules/settings/service.js +234 -0
- package/dist/modules/skill/index.js +50 -0
- package/dist/modules/skill/service.js +238 -0
- package/dist/modules/system/index.js +40 -0
- package/dist/modules/system/service.js +157 -0
- package/dist/modules/transfer/index.js +112 -0
- package/dist/modules/transfer/service.js +1004 -0
- package/dist/runtime/api.js +139 -0
- package/dist/runtime/cli.js +388 -0
- package/dist/runtime/module-registry.js +88 -0
- package/dist/runtime/registry.js +62 -0
- package/dist/runtime/server.js +78 -0
- package/dist/runtime/spec.js +193 -0
- package/dist/shared/blockLayout.js +382 -0
- package/dist/shared/fields/types.js +36 -0
- package/dist/shared/model.js +1 -0
- package/dist/shared/table.js +957 -0
- package/{src/web/public/assets/files--kTIDQLp.js → dist/web/public/assets/files-DDDN-GbJ.js} +1 -1
- package/dist/web/public/assets/index-BpUTnBjj.js +41 -0
- package/dist/web/public/assets/view-BgznDLss.js +46 -0
- package/{src/web/public/assets/view-ChbT0W1t.js → dist/web/public/assets/view-C6I3Vf9F.js} +2 -2
- package/{src/web/public/assets/view-Bji535Kc.js → dist/web/public/assets/view-ClLIweOz.js} +1 -1
- package/dist/web/public/assets/view-DOpzNX1y.js +3 -0
- package/dist/web/public/assets/view-TJPSoVht.js +1 -0
- package/{src → dist}/web/public/index.html +1 -1
- package/package.json +15 -8
- package/src/core/attachments.js +0 -446
- package/src/core/crypto.js +0 -73
- package/src/core/fields.js +0 -895
- package/src/core/fsx.js +0 -103
- package/src/core/ids.js +0 -19
- package/src/core/open.js +0 -32
- package/src/core/render.js +0 -239
- package/src/core/securezip.js +0 -72
- package/src/core/source.js +0 -230
- package/src/core/store.js +0 -297
- package/src/core/vault.js +0 -125
- package/src/core/zip.js +0 -198
- package/src/index.js +0 -13
- package/src/modules/entries/index.js +0 -286
- package/src/modules/entries/kvTable.jsx +0 -165
- package/src/modules/entries/service.js +0 -1508
- package/src/modules/entries/view.jsx +0 -666
- package/src/modules/export/index.js +0 -14
- package/src/modules/export/view.jsx +0 -25
- package/src/modules/files/index.js +0 -125
- package/src/modules/files/service.js +0 -327
- package/src/modules/files/view.jsx +0 -400
- package/src/modules/sections/index.js +0 -144
- package/src/modules/sections/service.js +0 -815
- package/src/modules/sections/view.jsx +0 -1199
- package/src/modules/settings/index.js +0 -34
- package/src/modules/settings/navManager.jsx +0 -213
- package/src/modules/settings/service.js +0 -221
- package/src/modules/settings/view.jsx +0 -224
- package/src/modules/skill/index.js +0 -52
- package/src/modules/skill/service.js +0 -237
- package/src/modules/system/index.js +0 -42
- package/src/modules/system/service.js +0 -163
- package/src/modules/transfer/index.js +0 -113
- package/src/modules/transfer/service.js +0 -958
- package/src/runtime/api.js +0 -140
- package/src/runtime/cli.js +0 -352
- package/src/runtime/registry.js +0 -67
- package/src/runtime/server.js +0 -80
- package/src/runtime/spec.js +0 -169
- package/src/shared/blockLayout.js +0 -367
- package/src/shared/table.js +0 -884
- package/src/web/frontend/App.jsx +0 -274
- package/src/web/frontend/activeView.js +0 -42
- package/src/web/frontend/api/client.js +0 -26
- package/src/web/frontend/api/files.js +0 -49
- package/src/web/frontend/api/zip.js +0 -34
- package/src/web/frontend/components/blockBoard.jsx +0 -307
- package/src/web/frontend/components/fieldEditor.jsx +0 -737
- package/src/web/frontend/components/lightbox.jsx +0 -96
- package/src/web/frontend/components/listDrag.jsx +0 -82
- package/src/web/frontend/components/mdView.jsx +0 -193
- package/src/web/frontend/components/navModel.js +0 -293
- package/src/web/frontend/components/sourcePanel.jsx +0 -558
- package/src/web/frontend/components/tableField.jsx +0 -635
- package/src/web/frontend/components/transferPanel.jsx +0 -494
- package/src/web/frontend/components/ui.jsx +0 -152
- package/src/web/frontend/index.html +0 -16
- package/src/web/frontend/listSort.js +0 -18
- package/src/web/frontend/main.jsx +0 -13
- package/src/web/frontend/mdInline.js +0 -185
- package/src/web/frontend/mdSmart.js +0 -164
- package/src/web/frontend/mdSource.js +0 -41
- package/src/web/frontend/registry.js +0 -21
- package/src/web/frontend/store.jsx +0 -107
- package/src/web/frontend/styles.css +0 -1535
- package/src/web/public/assets/index-Dk-VfQfE.js +0 -41
- package/src/web/public/assets/view-BaXP_gJq.js +0 -1
- package/src/web/public/assets/view-Bcy062Uw.js +0 -3
- package/src/web/public/assets/view-D_mRuEro.js +0 -46
- package/src/web/public/favicon-16.png +0 -0
- package/src/web/public/favicon-32.png +0 -0
- package/src/web/public/favicon-48.png +0 -0
- package/src/web/public/favicon.ico +0 -0
- package/src/web/public/logo-rounded.png +0 -0
- package/src/web/public/logo.png +0 -0
- /package/{src → dist}/web/public/assets/index-DDEmDb0K.css +0 -0
- /package/{src → dist}/web/public/assets/view-CgLZr6WS.css +0 -0
- /package/{src → dist}/web/public/assets/zip-gQ69BVQr.js +0 -0
- /package/{src/web/frontend → dist/web}/public/favicon-16.png +0 -0
- /package/{src/web/frontend → dist/web}/public/favicon-32.png +0 -0
- /package/{src/web/frontend → dist/web}/public/favicon-48.png +0 -0
- /package/{src/web/frontend → dist/web}/public/favicon.ico +0 -0
- /package/{src/web/frontend → dist/web}/public/logo-rounded.png +0 -0
- /package/{src/web/frontend → dist/web}/public/logo.png +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// 密文原语:AES-256-GCM。只做算法,不认识 store、不认识栏目。
|
|
2
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes, scryptSync } from 'node:crypto';
|
|
3
|
+
import { blocked, badInput } from "./errors.js";
|
|
4
|
+
export const ALG = 'aes-256-gcm';
|
|
5
|
+
export const KEY_BYTES = 32; // AES-256
|
|
6
|
+
export const BLOB_VERSION = 1;
|
|
7
|
+
export function newKey() {
|
|
8
|
+
return randomBytes(KEY_BYTES);
|
|
9
|
+
}
|
|
10
|
+
export function deriveKey(passphrase, salt) {
|
|
11
|
+
const s = Buffer.isBuffer(salt) ? salt : Buffer.from(String(salt), 'base64');
|
|
12
|
+
// scrypt 默认 N=16384, r=8, p=1 —— 对本机工具足够,且是同步的(无回调地狱)
|
|
13
|
+
return scryptSync(String(passphrase), s, KEY_BYTES);
|
|
14
|
+
}
|
|
15
|
+
/** 密钥指纹:写进密文,用来在「口令/密钥来源变了」时给出明确报错,而不是静默解不开。 */
|
|
16
|
+
export function keyId(key) {
|
|
17
|
+
return createHash('sha256').update(key).digest('hex').slice(0, 12);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 加密一个字符串值 → 可 JSON 化的密文对象。
|
|
21
|
+
* 明文绝不会以任何形式留在返回值里。
|
|
22
|
+
*/
|
|
23
|
+
export function encryptValue(key, plaintext) {
|
|
24
|
+
const iv = randomBytes(12);
|
|
25
|
+
const cipher = createCipheriv(ALG, key, iv);
|
|
26
|
+
const ct = Buffer.concat([cipher.update(String(plaintext), 'utf8'), cipher.final()]);
|
|
27
|
+
return {
|
|
28
|
+
v: BLOB_VERSION,
|
|
29
|
+
alg: ALG,
|
|
30
|
+
kid: keyId(key),
|
|
31
|
+
iv: iv.toString('base64'),
|
|
32
|
+
tag: cipher.getAuthTag().toString('base64'),
|
|
33
|
+
ct: ct.toString('base64'),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export function isCipherBlob(value) {
|
|
37
|
+
if (!value || typeof value !== 'object')
|
|
38
|
+
return false;
|
|
39
|
+
const v = value;
|
|
40
|
+
return typeof v.ct === 'string' && typeof v.iv === 'string';
|
|
41
|
+
}
|
|
42
|
+
export function decryptValue(key, blob) {
|
|
43
|
+
if (!isCipherBlob(blob))
|
|
44
|
+
throw badInput('不是合法的密文对象');
|
|
45
|
+
if (blob.alg !== ALG)
|
|
46
|
+
throw badInput(`不支持的加密算法: ${blob.alg}`);
|
|
47
|
+
if (blob.kid && blob.kid !== keyId(key)) {
|
|
48
|
+
throw blocked('当前密钥与写入时的密钥不一致,无法解密。' +
|
|
49
|
+
'(换过 NX_SK_PASSPHRASE,或删掉过 ~/nx-sk/.vaultkey 都会这样——旧密文只能用旧密钥解)');
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
const decipher = createDecipheriv(ALG, key, Buffer.from(blob.iv, 'base64'));
|
|
53
|
+
decipher.setAuthTag(Buffer.from(blob.tag, 'base64'));
|
|
54
|
+
return Buffer.concat([decipher.update(Buffer.from(blob.ct, 'base64')), decipher.final()]).toString('utf8');
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw blocked('解密失败:密文已损坏,或密钥不正确。');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 脱敏展示:`sk-1234567890abcdef` → `sk-1****cdef`。
|
|
62
|
+
* 长度不足以露出首尾时全部打码——**宁可比用户预期更严**,泄露是不可逆的。
|
|
63
|
+
*/
|
|
64
|
+
export function maskValue(text) {
|
|
65
|
+
const s = String(text ?? '');
|
|
66
|
+
if (!s)
|
|
67
|
+
return '';
|
|
68
|
+
if (s.length <= 10)
|
|
69
|
+
return '*'.repeat(s.length);
|
|
70
|
+
return `${s.slice(0, 4)}${'*'.repeat(6)}${s.slice(-4)}`;
|
|
71
|
+
}
|
|
@@ -1,52 +1,50 @@
|
|
|
1
1
|
// AppError + code → HTTP 状态 / CLI 退出码的唯一映射。
|
|
2
2
|
// 上层拿 err.code 分支,**不要对错误文本做字符串匹配**。
|
|
3
|
-
|
|
4
3
|
export const CODES = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
INVALID_INPUT: 'INVALID_INPUT',
|
|
5
|
+
NOT_FOUND: 'NOT_FOUND',
|
|
6
|
+
CONFLICT: 'CONFLICT',
|
|
7
|
+
BLOCKED: 'BLOCKED',
|
|
8
|
+
EXTERNAL: 'EXTERNAL',
|
|
9
|
+
INTERNAL: 'INTERNAL',
|
|
11
10
|
};
|
|
12
|
-
|
|
13
11
|
export class AppError extends Error {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
code;
|
|
13
|
+
details;
|
|
14
|
+
constructor(code, message, details) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = 'AppError';
|
|
17
|
+
this.code = code || CODES.INTERNAL;
|
|
18
|
+
if (details !== undefined)
|
|
19
|
+
this.details = details;
|
|
20
|
+
}
|
|
20
21
|
}
|
|
21
|
-
|
|
22
22
|
export const badInput = (msg, details) => new AppError(CODES.INVALID_INPUT, msg, details);
|
|
23
23
|
export const notFound = (msg, details) => new AppError(CODES.NOT_FOUND, msg, details);
|
|
24
24
|
export const conflict = (msg, details) => new AppError(CODES.CONFLICT, msg, details);
|
|
25
25
|
export const blocked = (msg, details) => new AppError(CODES.BLOCKED, msg, details);
|
|
26
26
|
export const external = (msg, details) => new AppError(CODES.EXTERNAL, msg, details);
|
|
27
27
|
export const internal = (msg, details) => new AppError(CODES.INTERNAL, msg, details);
|
|
28
|
-
|
|
29
28
|
const HTTP = {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
29
|
+
[CODES.INVALID_INPUT]: 400,
|
|
30
|
+
[CODES.NOT_FOUND]: 404,
|
|
31
|
+
[CODES.CONFLICT]: 409,
|
|
32
|
+
[CODES.BLOCKED]: 409,
|
|
33
|
+
[CODES.EXTERNAL]: 502,
|
|
34
|
+
[CODES.INTERNAL]: 500,
|
|
36
35
|
};
|
|
37
|
-
|
|
38
36
|
/** CONFLICT 与 BLOCKED 都映射 409:前者是「你选哪边」,后者是「你先去满足前置条件」。 */
|
|
39
37
|
export function httpStatusOf(code) {
|
|
40
|
-
|
|
38
|
+
return HTTP[code] || 500;
|
|
41
39
|
}
|
|
42
|
-
|
|
43
40
|
/** 失败一律退出码 1。冲突是**业务结果**不是失败——那种情况根本不会走到这里。 */
|
|
44
41
|
export function exitCodeOf(_code) {
|
|
45
|
-
|
|
42
|
+
return 1;
|
|
46
43
|
}
|
|
47
|
-
|
|
48
44
|
/** 把任意 throw 出来的东西归一成 { code, message },供 CLI / API 统一渲染。 */
|
|
49
45
|
export function toErrorShape(err) {
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
if (err instanceof AppError)
|
|
47
|
+
return { code: err.code, message: err.message, details: err.details };
|
|
48
|
+
const e = err;
|
|
49
|
+
return { code: CODES.INTERNAL, message: e && e.message ? e.message : String(err) };
|
|
52
50
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BOOL_FALSE, BOOL_TRUE } from "../../fields.js";
|
|
2
|
+
import { badInput } from "../../errors.js";
|
|
3
|
+
import { identityRemap, identityReveal, isSensitiveDef, noRefs, noSubFields } from "./common.js";
|
|
4
|
+
export const boolBehavior = {
|
|
5
|
+
cell: true,
|
|
6
|
+
normalizeDef: () => { },
|
|
7
|
+
coerce(def, raw) {
|
|
8
|
+
if (typeof raw === 'boolean')
|
|
9
|
+
return raw;
|
|
10
|
+
const s = String(raw).trim().toLowerCase();
|
|
11
|
+
if (BOOL_TRUE.includes(s))
|
|
12
|
+
return true;
|
|
13
|
+
if (BOOL_FALSE.includes(s))
|
|
14
|
+
return false;
|
|
15
|
+
throw badInput(`字段「${def.label}」需要是/否,收到 ${JSON.stringify(raw)}`);
|
|
16
|
+
},
|
|
17
|
+
reveal: identityReveal,
|
|
18
|
+
refs: noRefs,
|
|
19
|
+
remap: identityRemap,
|
|
20
|
+
// 只替布尔值:字符串 "true" 这类历史数据继续走通用兜底(String(v)),与重构前一致
|
|
21
|
+
format: (_def, value) => (typeof value === 'boolean' ? [value ? '是' : '否'] : null),
|
|
22
|
+
sensitive: isSensitiveDef,
|
|
23
|
+
subFields: noSubFields,
|
|
24
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// 字段行为的公共零件。
|
|
2
|
+
//
|
|
3
|
+
// 只放「多个类型完全一样」的实现,避免 14 个文件各写一遍;有差异的钩子绝不塞进这里。
|
|
4
|
+
import { displaySensitive } from "../../sensitive.js";
|
|
5
|
+
import { isPlainRow } from "../../values.js";
|
|
6
|
+
/** 是否按密文处理:字段显式 `sensitive`,或类型就是 `secret`。 */
|
|
7
|
+
export const isSensitiveDef = (def) => def?.sensitive === true || def?.type === 'secret';
|
|
8
|
+
/** 标量字段没有子字段。 */
|
|
9
|
+
export const noSubFields = () => [];
|
|
10
|
+
/** 不引用附件。 */
|
|
11
|
+
export const noRefs = () => [];
|
|
12
|
+
/** 读取侧默认原样带出。 */
|
|
13
|
+
export const identityReveal = (_def, raw, _opts) => raw;
|
|
14
|
+
/** 导入时默认不做 id 重映射。 */
|
|
15
|
+
export const identityRemap = (_def, value, _idMap) => value;
|
|
16
|
+
/** 文本类字段的写入归一:空串 = 未填(null),否则原样(不 trim 非空值)。 */
|
|
17
|
+
export function coerceText(_def, raw) {
|
|
18
|
+
const s = String(raw);
|
|
19
|
+
return s.trim() === '' ? null : s;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 清单类的一行:敏感子值走与顶层敏感字段同一条 displaySensitive 边界。
|
|
23
|
+
* 非对象行(坏数据)原样放过。
|
|
24
|
+
*/
|
|
25
|
+
export function displayRow(subs, row, opts) {
|
|
26
|
+
if (!isPlainRow(row))
|
|
27
|
+
return row;
|
|
28
|
+
const sensitiveSubs = subs.filter((s) => s.sensitive);
|
|
29
|
+
if (!sensitiveSubs.length)
|
|
30
|
+
return row;
|
|
31
|
+
const out = { ...row };
|
|
32
|
+
for (const s of sensitiveSubs) {
|
|
33
|
+
if (out[s.key] !== undefined && out[s.key] !== null)
|
|
34
|
+
out[s.key] = displaySensitive(out[s.key], opts);
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { coerceText, identityRemap, identityReveal, isSensitiveDef, noRefs, noSubFields, } from "./common.js";
|
|
2
|
+
export const dateBehavior = {
|
|
3
|
+
cell: true,
|
|
4
|
+
normalizeDef: () => { },
|
|
5
|
+
coerce: coerceText,
|
|
6
|
+
reveal: identityReveal,
|
|
7
|
+
refs: noRefs,
|
|
8
|
+
remap: identityRemap,
|
|
9
|
+
format: () => null,
|
|
10
|
+
sensitive: isSensitiveDef,
|
|
11
|
+
subFields: noSubFields,
|
|
12
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { badInput } from "../../errors.js";
|
|
2
|
+
import { assertFieldKey } from "../../paths.js";
|
|
3
|
+
import { isBlank, isPlainRow } from "../../values.js";
|
|
4
|
+
import { displayRow, identityRemap, isSensitiveDef } from "./common.js";
|
|
5
|
+
/**
|
|
6
|
+
* 结构清单的行归一:只留声明过的子字段(键写错当场报,不静默丢数据);
|
|
7
|
+
* 空格子剔除、全空行丢弃。列没声明时(写入自动登记的字段)从行键**推断**清单列——
|
|
8
|
+
* 「直接写就登记」的手感在结构清单上也成立。
|
|
9
|
+
*/
|
|
10
|
+
export function normalizeRows(def, list) {
|
|
11
|
+
if (!def.fields || !def.fields.length) {
|
|
12
|
+
const keys = [];
|
|
13
|
+
for (const row of list) {
|
|
14
|
+
if (!isPlainRow(row))
|
|
15
|
+
continue;
|
|
16
|
+
for (const k of Object.keys(row))
|
|
17
|
+
if (!keys.includes(k))
|
|
18
|
+
keys.push(k);
|
|
19
|
+
}
|
|
20
|
+
if (keys.length)
|
|
21
|
+
def.fields = keys.map((k) => ({ key: k, label: k }));
|
|
22
|
+
}
|
|
23
|
+
const subs = def.fields || [];
|
|
24
|
+
const rows = [];
|
|
25
|
+
for (const item of list) {
|
|
26
|
+
if (!isPlainRow(item)) {
|
|
27
|
+
throw badInput(`字段「${def.label}」的每行都要是对象({"子字段": "值"}),收到 ${JSON.stringify(item).slice(0, 80)}`);
|
|
28
|
+
}
|
|
29
|
+
const row = {};
|
|
30
|
+
for (const [k, v] of Object.entries(item)) {
|
|
31
|
+
// 列名**key 与 label 都认**(与顶层 `--set 字段=值` 同一口径):
|
|
32
|
+
// 人写的是「密码」,清单声明的是 pass —— 不该让人先背一遍英文 key。
|
|
33
|
+
const sub = subs.find((s) => s.key === k) || subs.find((s) => s.label === k);
|
|
34
|
+
if (!sub) {
|
|
35
|
+
throw badInput(`字段「${def.label}」没有子字段「${k}」(清单列: ${subs.map((s) => s.label).join('、') || '无'})。`
|
|
36
|
+
+ `清单列在字段定义里,要改用 section update <栏目> --fields '<整份字段数组>'`);
|
|
37
|
+
}
|
|
38
|
+
const text = String(v ?? '').trim();
|
|
39
|
+
// 一律归到列的 **key** 下:密文判定、面板渲染都按 key 走
|
|
40
|
+
if (text !== '')
|
|
41
|
+
row[sub.key] = text;
|
|
42
|
+
}
|
|
43
|
+
if (Object.keys(row).length)
|
|
44
|
+
rows.push(row);
|
|
45
|
+
}
|
|
46
|
+
if (def.maxItems && rows.length > def.maxItems) {
|
|
47
|
+
throw badInput(`字段「${def.label}」最多 ${def.maxItems} 行,收到 ${rows.length} 行`);
|
|
48
|
+
}
|
|
49
|
+
return rows.length ? rows : null;
|
|
50
|
+
}
|
|
51
|
+
export const entrylistBehavior = {
|
|
52
|
+
cell: false,
|
|
53
|
+
normalizeDef(raw, def) {
|
|
54
|
+
// 结构清单的列就是它自己的小字典:不声明列就没法渲染、没法校验。
|
|
55
|
+
if (!Array.isArray(raw.fields) || !raw.fields.length) {
|
|
56
|
+
throw badInput(`字段 ${def.key} 是 entrylist(结构清单),必须声明子字段 fields。`
|
|
57
|
+
+ `例如 fields:[{"key":"url","label":"链接"},{"key":"pass","label":"密码","sensitive":true}]`);
|
|
58
|
+
}
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
def.fields = raw.fields.map((s) => {
|
|
61
|
+
if (!s || typeof s !== 'object')
|
|
62
|
+
throw badInput(`字段 ${def.key} 的子字段必须是对象,收到 ${JSON.stringify(s)}`);
|
|
63
|
+
const sk = assertFieldKey(String(s.key || '').trim());
|
|
64
|
+
if (seen.has(sk))
|
|
65
|
+
throw badInput(`字段 ${def.key} 的子字段 key 重复: ${sk}`);
|
|
66
|
+
seen.add(sk);
|
|
67
|
+
const out = { key: sk, label: String(s.label || sk) };
|
|
68
|
+
if (s.sensitive === true)
|
|
69
|
+
out.sensitive = true;
|
|
70
|
+
if (s.hint)
|
|
71
|
+
out.hint = String(s.hint);
|
|
72
|
+
return out;
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
coerce(def, raw) {
|
|
76
|
+
// 两种入口:--data 直接给 JSON 数组;--set 给 JSON 串(解析失败就报,别静默吞)。
|
|
77
|
+
let list = raw;
|
|
78
|
+
if (typeof raw === 'string') {
|
|
79
|
+
if (!raw.trim())
|
|
80
|
+
return null; // 「--set 字段=」(空值)= 清空,与其它类型同一约定
|
|
81
|
+
try {
|
|
82
|
+
list = JSON.parse(raw);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
list = null;
|
|
86
|
+
}
|
|
87
|
+
if (!Array.isArray(list)) {
|
|
88
|
+
throw badInput(`字段「${def.label}」是结构清单(entrylist),值要是行数组的 JSON,`
|
|
89
|
+
+ `例如 '[{"名称":"主控台","链接":"https://…"}]'`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!Array.isArray(list)) {
|
|
93
|
+
throw badInput(`字段「${def.label}」是结构清单(entrylist),值要是行数组(JSON),收到 ${JSON.stringify(raw).slice(0, 80)}`);
|
|
94
|
+
}
|
|
95
|
+
return normalizeRows(def, list);
|
|
96
|
+
},
|
|
97
|
+
reveal: (def, raw, opts) => (Array.isArray(raw) ? raw.map((row) => displayRow(def.fields || [], row, opts)) : raw),
|
|
98
|
+
refs: () => [],
|
|
99
|
+
remap: identityRemap,
|
|
100
|
+
format: (def, value, ctx) => {
|
|
101
|
+
if (!(Array.isArray(value) && value.length && value.every(isPlainRow)))
|
|
102
|
+
return null;
|
|
103
|
+
const subs = ctx.entryFields?.[def.key] || [];
|
|
104
|
+
const labelOf = (k) => subs.find((s) => s.key === k)?.label || k;
|
|
105
|
+
return value.map((row, i) => Object.entries(row)
|
|
106
|
+
.filter(([, x]) => !isBlank(x))
|
|
107
|
+
.map(([k2, x]) => `${labelOf(k2)}=${x}`)
|
|
108
|
+
.join(' · ') || `第 ${i + 1} 行(空)`);
|
|
109
|
+
},
|
|
110
|
+
sensitive: isSensitiveDef,
|
|
111
|
+
subFields: (def) => def?.fields || [],
|
|
112
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { badInput } from "../../errors.js";
|
|
2
|
+
import { fileIdsOf } from "../../attachments.js";
|
|
3
|
+
import { identityReveal, isSensitiveDef, noSubFields } from "./common.js";
|
|
4
|
+
/**
|
|
5
|
+
* 值 = 附件 id(或唯一文件名)的**列表**:一个字段可以挂多个附件。
|
|
6
|
+
* **新写入统一存数组**(哪怕只有一个);历史数据里的字符串单值由 fileIdsOf 按单元素处理。
|
|
7
|
+
* 不校验存在性——附件可能被还原/删掉,前端按「引用的附件不存在」展示。
|
|
8
|
+
*/
|
|
9
|
+
export const fileBehavior = {
|
|
10
|
+
cell: false,
|
|
11
|
+
normalizeDef: () => { },
|
|
12
|
+
coerce(def, raw) {
|
|
13
|
+
const list = fileIdsOf(Array.isArray(raw) ? raw : String(raw).split(/[,,、;;]+/));
|
|
14
|
+
if (def.maxItems && list.length > def.maxItems) {
|
|
15
|
+
throw badInput(`字段「${def.label}」最多 ${def.maxItems} 项,收到 ${list.length} 项`);
|
|
16
|
+
}
|
|
17
|
+
return list.length ? list : null;
|
|
18
|
+
},
|
|
19
|
+
reveal: identityReveal,
|
|
20
|
+
refs: (_def, value) => fileIdsOf(value),
|
|
21
|
+
remap: (_def, value, idMap) => {
|
|
22
|
+
const ids = fileIdsOf(value).map((id) => idMap.get(id) || id).filter(Boolean);
|
|
23
|
+
return ids.length ? ids : null;
|
|
24
|
+
},
|
|
25
|
+
format: (_def, value, ctx) => {
|
|
26
|
+
const asFile = (x) => {
|
|
27
|
+
const rec = ctx.files?.[String(x)];
|
|
28
|
+
return rec ? `${rec.name} ${rec.path}` : null;
|
|
29
|
+
};
|
|
30
|
+
if (Array.isArray(value) && value.length && value.every((x) => asFile(x))) {
|
|
31
|
+
return value.map((x) => asFile(x));
|
|
32
|
+
}
|
|
33
|
+
const one = asFile(value);
|
|
34
|
+
return one ? [one] : null;
|
|
35
|
+
},
|
|
36
|
+
sensitive: isSensitiveDef,
|
|
37
|
+
subFields: noSubFields,
|
|
38
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { mdImageIds, rewriteMdImages } from "../../mdimages.js";
|
|
2
|
+
import { coerceText, identityReveal, isSensitiveDef, noSubFields, } from "./common.js";
|
|
3
|
+
export const mdBehavior = {
|
|
4
|
+
cell: false,
|
|
5
|
+
normalizeDef: () => { },
|
|
6
|
+
coerce: coerceText,
|
|
7
|
+
reveal: identityReveal,
|
|
8
|
+
refs: (_def, value) => mdImageIds(value),
|
|
9
|
+
remap: (_def, value, idMap) => {
|
|
10
|
+
if (typeof value !== 'string')
|
|
11
|
+
return value;
|
|
12
|
+
return value.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (m, alt, id) => (idMap.has(id) ? `})` : m));
|
|
13
|
+
},
|
|
14
|
+
format: (_def, value, ctx) => {
|
|
15
|
+
// 图文字段:`` → `[图片: 说明] 本地路径`,否则 CLI 只吐一串 id。
|
|
16
|
+
const text = rewriteMdImages(String(value), (id) => ctx.files?.[id] || null);
|
|
17
|
+
return text.includes('\n') ? text.split('\n') : [text];
|
|
18
|
+
},
|
|
19
|
+
sensitive: isSensitiveDef,
|
|
20
|
+
subFields: noSubFields,
|
|
21
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { coerceText, identityRemap, identityReveal, isSensitiveDef, noRefs, noSubFields, } from "./common.js";
|
|
2
|
+
export const monthBehavior = {
|
|
3
|
+
cell: true,
|
|
4
|
+
normalizeDef: () => { },
|
|
5
|
+
coerce: coerceText,
|
|
6
|
+
reveal: identityReveal,
|
|
7
|
+
refs: noRefs,
|
|
8
|
+
remap: identityRemap,
|
|
9
|
+
format: () => null,
|
|
10
|
+
sensitive: isSensitiveDef,
|
|
11
|
+
subFields: noSubFields,
|
|
12
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { badInput } from "../../errors.js";
|
|
2
|
+
import { identityRemap, identityReveal, isSensitiveDef, noRefs, noSubFields } from "./common.js";
|
|
3
|
+
export const numberBehavior = {
|
|
4
|
+
cell: true,
|
|
5
|
+
normalizeDef: () => { },
|
|
6
|
+
coerce(def, raw) {
|
|
7
|
+
const n = Number(String(raw).trim());
|
|
8
|
+
if (!Number.isFinite(n))
|
|
9
|
+
throw badInput(`字段「${def.label}」需要数字,收到 ${JSON.stringify(raw)}`);
|
|
10
|
+
return n;
|
|
11
|
+
},
|
|
12
|
+
reveal: identityReveal,
|
|
13
|
+
refs: noRefs,
|
|
14
|
+
remap: identityRemap,
|
|
15
|
+
format: () => null,
|
|
16
|
+
sensitive: isSensitiveDef,
|
|
17
|
+
subFields: noSubFields,
|
|
18
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { coerceText, identityRemap, identityReveal, isSensitiveDef, noRefs, noSubFields, } from "./common.js";
|
|
2
|
+
export const secretBehavior = {
|
|
3
|
+
cell: false,
|
|
4
|
+
normalizeDef: () => { },
|
|
5
|
+
coerce: coerceText,
|
|
6
|
+
reveal: identityReveal,
|
|
7
|
+
refs: noRefs,
|
|
8
|
+
remap: identityRemap,
|
|
9
|
+
format: () => null,
|
|
10
|
+
sensitive: isSensitiveDef,
|
|
11
|
+
subFields: noSubFields,
|
|
12
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { coerceText, identityRemap, identityReveal, isSensitiveDef, noRefs, noSubFields, } from "./common.js";
|
|
2
|
+
export const selectBehavior = {
|
|
3
|
+
cell: true,
|
|
4
|
+
normalizeDef: () => { },
|
|
5
|
+
coerce: coerceText,
|
|
6
|
+
reveal: identityReveal,
|
|
7
|
+
refs: noRefs,
|
|
8
|
+
remap: identityRemap,
|
|
9
|
+
format: () => null,
|
|
10
|
+
sensitive: isSensitiveDef,
|
|
11
|
+
subFields: noSubFields,
|
|
12
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { badInput } from "../../errors.js";
|
|
2
|
+
import { mdImageIds } from "../../mdimages.js";
|
|
3
|
+
import { normalizeSourceItems, sourceItemSummary } from "../../source.js";
|
|
4
|
+
import { identityRemap, identityReveal, isSensitiveDef, noSubFields } from "./common.js";
|
|
5
|
+
/**
|
|
6
|
+
* 原始资料袋:值 = 一组 item(文字/附件/网址)。形状与归一规则见 core/source.js。
|
|
7
|
+
* 三种入口都认:数组(面板/JSON)、JSON 串(--set)、一段普通文字(--set 随手记)。
|
|
8
|
+
*/
|
|
9
|
+
export const sourceBehavior = {
|
|
10
|
+
cell: false,
|
|
11
|
+
normalizeDef: () => { },
|
|
12
|
+
coerce(def, raw) {
|
|
13
|
+
try {
|
|
14
|
+
return normalizeSourceItems(raw);
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
throw badInput(`字段「${def.label}」的原始资料有问题:${e.message}`);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
reveal: identityReveal,
|
|
21
|
+
refs: (_def, value, opts) => {
|
|
22
|
+
const ids = [];
|
|
23
|
+
if (!Array.isArray(value))
|
|
24
|
+
return ids;
|
|
25
|
+
for (const item of value) {
|
|
26
|
+
if (!item || typeof item !== 'object')
|
|
27
|
+
continue;
|
|
28
|
+
if (item.kind === 'file' && item.fileId)
|
|
29
|
+
ids.push(item.fileId);
|
|
30
|
+
// text 的底层是 markdown:内嵌 `` 是否算引用由调用点的口径决定
|
|
31
|
+
//(导出打包要算;附件中心的「谁引用了它」不算——这是既有行为,P2 不改)。
|
|
32
|
+
else if (opts?.sourceTextImages && item.kind === 'text' && typeof item.text === 'string') {
|
|
33
|
+
for (const id of mdImageIds(item.text))
|
|
34
|
+
ids.push(id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return ids;
|
|
38
|
+
},
|
|
39
|
+
remap: (_def, value, idMap) => {
|
|
40
|
+
if (!Array.isArray(value))
|
|
41
|
+
return identityRemap(_def, value, idMap);
|
|
42
|
+
return value.map((item) => {
|
|
43
|
+
if (!item || typeof item !== 'object')
|
|
44
|
+
return item;
|
|
45
|
+
if (item.kind === 'file' && item.fileId && idMap.has(item.fileId)) {
|
|
46
|
+
return { ...item, fileId: idMap.get(item.fileId) };
|
|
47
|
+
}
|
|
48
|
+
if (item.kind === 'text' && typeof item.text === 'string') {
|
|
49
|
+
return {
|
|
50
|
+
...item,
|
|
51
|
+
text: item.text.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (m, alt, id) => (idMap.has(id) ? `})` : m)),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return item;
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
format: (_def, value, ctx) => {
|
|
58
|
+
if (!Array.isArray(value))
|
|
59
|
+
return null;
|
|
60
|
+
const kindLabel = { text: '文字', file: '附件', url: '网址' };
|
|
61
|
+
return value.map((item) => {
|
|
62
|
+
const tag = kindLabel[item.kind] || item.kind || '资料';
|
|
63
|
+
const rec = item.kind === 'file' && item.fileId ? ctx.files?.[item.fileId] : null;
|
|
64
|
+
const body = rec ? `${rec.name} ${rec.path}` : sourceItemSummary(item);
|
|
65
|
+
const mapped = item.mapped?.length ? ` → 已整理到 ${item.mapped.join('、')}` : '';
|
|
66
|
+
return `[${tag}] ${item.name || body}${body && body !== item.name ? ` ${body}` : ''}${mapped}`;
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
sensitive: isSensitiveDef,
|
|
70
|
+
subFields: noSubFields,
|
|
71
|
+
};
|