@zxlll/dsh-integration-env 0.0.0-stage → 0.1.1

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/lib/kinds.js ADDED
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Kind 注册表:Host 唯一的数据源,`catalog.kinds` 整体下发给 Client 动态渲染。
3
+ *
4
+ * 字段描述符必须与 docs/contract.md §3 完全一致;将来新增 kind 只改这里。
5
+ * 本文件不读取任何配置,纯静态数据 + 少量派生辅助函数。
6
+ */
7
+
8
+ /** 五个 kind 的完整描述符(顺序即 catalog / 工具里的展示顺序)。 */
9
+ export const KINDS = [
10
+ {
11
+ id: 'mysql',
12
+ label: 'MySQL',
13
+ description: '连接 MySQL 数据库,执行 SQL 查询(默认只读)。',
14
+ connectable: true,
15
+ fields: [
16
+ { key: 'host', label: '主机', type: 'text', required: true, placeholder: '127.0.0.1' },
17
+ { key: 'port', label: '端口', type: 'number', default: 3306, min: 1, max: 65535 },
18
+ { key: 'user', label: '用户名', type: 'text', required: true, placeholder: 'root' },
19
+ { key: 'password', label: '密码', type: 'secret' },
20
+ { key: 'database', label: '数据库', type: 'text', placeholder: 'demo_db' },
21
+ {
22
+ key: 'charset',
23
+ label: '字符集',
24
+ type: 'select',
25
+ default: 'utf8mb4',
26
+ options: [
27
+ { value: 'utf8mb4', label: 'utf8mb4' },
28
+ { value: 'utf8', label: 'utf8' },
29
+ { value: 'utf8mb4_general_ci', label: 'utf8mb4_general_ci' },
30
+ { value: 'utf8mb4_unicode_ci', label: 'utf8mb4_unicode_ci' },
31
+ { value: 'utf8_general_ci', label: 'utf8_general_ci' },
32
+ { value: 'latin1', label: 'latin1' },
33
+ ],
34
+ },
35
+ {
36
+ key: 'readOnly',
37
+ label: '只读',
38
+ type: 'boolean',
39
+ default: true,
40
+ help: '只允许 SELECT/SHOW/DESC/EXPLAIN;关闭后才能执行写操作',
41
+ },
42
+ ],
43
+ },
44
+ {
45
+ id: 'nacos',
46
+ label: 'Nacos',
47
+ description: '读取 Nacos 的配置、命名空间、服务与实例。',
48
+ connectable: true,
49
+ fields: [
50
+ {
51
+ key: 'baseUrl',
52
+ label: '服务地址',
53
+ type: 'text',
54
+ required: true,
55
+ placeholder: 'http://127.0.0.1:31878/nacos',
56
+ help: '要带上下文路径 /nacos',
57
+ },
58
+ { key: 'namespace', label: '命名空间', type: 'text', default: '', help: '命名空间 ID,public 留空' },
59
+ { key: 'group', label: '分组', type: 'text', default: 'DEFAULT_GROUP' },
60
+ { key: 'username', label: '用户名', type: 'text' },
61
+ { key: 'password', label: '密码', type: 'secret' },
62
+ ],
63
+ },
64
+ {
65
+ id: 'redis',
66
+ label: 'Redis',
67
+ description: '连接 Redis,执行只读命令。',
68
+ connectable: true,
69
+ fields: [
70
+ { key: 'host', label: '主机', type: 'text', required: true, placeholder: '127.0.0.1' },
71
+ { key: 'port', label: '端口', type: 'number', default: 6379 },
72
+ { key: 'username', label: '用户名', type: 'text' },
73
+ { key: 'password', label: '密码', type: 'secret' },
74
+ { key: 'database', label: '数据库编号', type: 'number', default: 0, min: 0, max: 15 },
75
+ { key: 'tls', label: '加密连接', type: 'boolean', default: false },
76
+ {
77
+ key: 'readOnly',
78
+ label: '只读',
79
+ type: 'boolean',
80
+ default: true,
81
+ help: '只允许读命令;关闭后才能 SET/DEL 等',
82
+ },
83
+ ],
84
+ },
85
+ {
86
+ id: 'mongodb',
87
+ label: 'MongoDB',
88
+ description: '配置 MongoDB 连接(v1 仅支持端口连通测试)。',
89
+ connectable: false,
90
+ fields: [
91
+ {
92
+ key: 'uri',
93
+ label: '连接串',
94
+ type: 'text',
95
+ required: true,
96
+ placeholder: 'mongodb://user:pass@host:27017/db',
97
+ },
98
+ ],
99
+ },
100
+ {
101
+ id: 'mqtt',
102
+ label: 'MQTT',
103
+ description: '配置 MQTT 连接(v1 仅支持端口连通测试)。',
104
+ connectable: false,
105
+ fields: [
106
+ { key: 'host', label: '主机', type: 'text', required: true, placeholder: '127.0.0.1' },
107
+ { key: 'port', label: '端口', type: 'number', default: 1883 },
108
+ { key: 'username', label: '用户名', type: 'text' },
109
+ { key: 'password', label: '密码', type: 'secret' },
110
+ { key: 'clientId', label: '客户端 ID', type: 'text', default: 'dsh-integration-env' },
111
+ { key: 'tls', label: '加密连接', type: 'boolean', default: false },
112
+ ],
113
+ },
114
+ ]
115
+
116
+ /** id → Kind 的索引。 */
117
+ export const KIND_MAP = new Map(KINDS.map((kind) => [kind.id, kind]))
118
+
119
+ /** 允许出现在 `kind` 参数里的 id 列表(工具 enum 复用)。 */
120
+ export const KIND_IDS = KINDS.map((kind) => kind.id)
121
+
122
+ /** 取一个 kind 描述符;未知返回 undefined。 */
123
+ export function getKind(id) {
124
+ return KIND_MAP.get(String(id ?? ''))
125
+ }
126
+
127
+ /** kind 的展示名;未知时回退成 id 本身。 */
128
+ export function kindLabel(kindId) {
129
+ return getKind(kindId)?.label ?? String(kindId ?? '')
130
+ }
131
+
132
+ /** 一个 kind 的所有 secret 字段 key。 */
133
+ export function secretKeysOf(kindId) {
134
+ const kind = getKind(kindId)
135
+ if (!kind) return []
136
+ return kind.fields.filter((field) => field.type === 'secret').map((field) => field.key)
137
+ }
138
+
139
+ /** 把「带冒号的主机:端口」拼出来;端口缺失时只给主机。 */
140
+ function hostPort(config, fallbackPort) {
141
+ const host = String(config.host ?? '').trim()
142
+ const port = config.port === undefined || config.port === null || config.port === '' ? fallbackPort : config.port
143
+ return port === undefined || port === '' ? host : `${host}:${port}`
144
+ }
145
+
146
+ /** mongodb 连接串去掉密码(只用于展示,绝不回传明文)。 */
147
+ export function redactMongoUri(uri) {
148
+ const text = String(uri ?? '')
149
+ // mongodb://user:pass@host/db → mongodb://user:***@host/db
150
+ const redacted = text.replace(/^(mongodb(?:\+srv)?:\/\/[^:@/]+:)[^@/]+@/i, '$1***@')
151
+ if (redacted !== text) return redacted
152
+ // 没有用户名时也可能出现 :pass@,统一兜底。
153
+ return text.replace(/^(mongodb(?:\+srv)?:\/\/[^@/]*?:)[^@/]+@/i, '$1***@')
154
+ }
155
+
156
+ /**
157
+ * catalog / 工具里的实例短摘要(契约 §3)。
158
+ * @param instance - 存储态实例。
159
+ * @returns 单行摘要字符串。
160
+ */
161
+ export function summarize(instance) {
162
+ const config = instance?.config && typeof instance.config === 'object' ? instance.config : {}
163
+ switch (instance?.kind) {
164
+ case 'mysql': {
165
+ const base = hostPort(config, 3306)
166
+ const database = String(config.database ?? '').trim()
167
+ return database ? `${base} / ${database}` : base
168
+ }
169
+ case 'nacos': {
170
+ const baseUrl = String(config.baseUrl ?? '').trim()
171
+ const namespace = String(config.namespace ?? '').trim()
172
+ return `${baseUrl || '(未填写地址)'} · ns=${namespace || 'public'}`
173
+ }
174
+ case 'redis': {
175
+ const database = config.database === undefined || config.database === '' ? 0 : config.database
176
+ return `${hostPort(config, 6379)} / db${database}`
177
+ }
178
+ case 'mongodb':
179
+ return redactMongoUri(config.uri)
180
+ case 'mqtt':
181
+ return hostPort(config, 1883)
182
+ default:
183
+ return ''
184
+ }
185
+ }
186
+
187
+ /**
188
+ * 提示块(§5)里的展示摘要:nacos 用「命名空间 public」而不是「ns=public」。
189
+ * 与 summarize() 分开,是因为两处文案在契约里是分别规定的。
190
+ */
191
+ export function displaySummary(instance) {
192
+ const config = instance?.config && typeof instance.config === 'object' ? instance.config : {}
193
+ if (instance?.kind === 'nacos') {
194
+ const baseUrl = String(config.baseUrl ?? '').trim()
195
+ const namespace = String(config.namespace ?? '').trim()
196
+ return `${baseUrl || '(未填写地址)'} · 命名空间 ${namespace || 'public'}`
197
+ }
198
+ return summarize(instance)
199
+ }
200
+
201
+ /** mongodb 的 TCP 探测目标(从连接串里取 host/port)。 */
202
+ export function mongoTarget(uri) {
203
+ const text = String(uri ?? '')
204
+ const match = /:\/\/(?:[^@/]+@)?([^:/?#,]+)(?::(\d+))?/.exec(text)
205
+ return {
206
+ host: match?.[1] ?? '',
207
+ port: match?.[2] ? Number(match[2]) : 27017,
208
+ }
209
+ }