dsh-data-window 0.0.0-stage → 0.1.2

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.
Files changed (45) hide show
  1. package/AGENTS.md +111 -0
  2. package/CHANGELOG.md +267 -0
  3. package/LICENSE +21 -0
  4. package/README.md +151 -2
  5. package/client.js +8271 -0
  6. package/cordis.patch.yml +4 -0
  7. package/docs/BRIDGE.md +106 -0
  8. package/docs/DESIGN.md +417 -0
  9. package/docs/DETAIL.md +242 -0
  10. package/docs/screenshots/ddl.png +0 -0
  11. package/docs/screenshots/workbench.png +0 -0
  12. package/icon.svg +30 -0
  13. package/index.js +180 -0
  14. package/locale/en.json +304 -0
  15. package/locale/zh.json +304 -0
  16. package/package.json +68 -4
  17. package/src/client/00-css.js +1110 -0
  18. package/src/client/05-icons.js +156 -0
  19. package/src/client/05-theme.js +107 -0
  20. package/src/client/06-shortcuts.js +89 -0
  21. package/src/client/07-bridge.js +202 -0
  22. package/src/client/10-core.js +1320 -0
  23. package/src/client/20-tree.js +293 -0
  24. package/src/client/25-sql.js +631 -0
  25. package/src/client/30-editor.js +742 -0
  26. package/src/client/40-results.js +556 -0
  27. package/src/client/45-export.js +219 -0
  28. package/src/client/50-panel.js +768 -0
  29. package/src/client/60-settings.js +180 -0
  30. package/src/client/63-schema.js +186 -0
  31. package/src/client/64-manual.js +350 -0
  32. package/src/client/65-ai.js +320 -0
  33. package/src/client/66-optimize.js +217 -0
  34. package/src/client/90-apply.js +183 -0
  35. package/src/host/ai.js +721 -0
  36. package/src/host/cache.js +157 -0
  37. package/src/host/catalog.js +161 -0
  38. package/src/host/consoles.js +116 -0
  39. package/src/host/discovery.js +603 -0
  40. package/src/host/manual.js +276 -0
  41. package/src/host/mysql.js +650 -0
  42. package/src/host/remembered.js +225 -0
  43. package/src/host/rpc.js +487 -0
  44. package/src/host/shared.js +180 -0
  45. package/src/host/yaml.js +335 -0
@@ -0,0 +1,335 @@
1
+ /**
2
+ * A YAML subset parser for Spring/Nacos configuration files.
3
+ *
4
+ * A database viewer must read the project's own configuration, and those files
5
+ * are plain Spring YAML: nested maps, block lists, multi-document files, `#`
6
+ * comments and quoted scalars. That is a small, well-defined slice of YAML, so
7
+ * the plugin carries this reader instead of depending on a full parser (which
8
+ * would drag a YAML engine into the Host for four features).
9
+ *
10
+ * Explicitly unsupported, because a datasource block never uses them: anchors and
11
+ * aliases, merge keys, flow collections (`{a: 1}`, `[a, b]`), tags, and folded
12
+ * block scalars. `|` and `>` blocks are skipped rather than mis-parsed.
13
+ */
14
+
15
+ /** Split a raw line at a `#` comment that is outside quotes. */
16
+ function stripComment(line) {
17
+ let single = false
18
+ let double = false
19
+ for (let index = 0; index < line.length; index += 1) {
20
+ const character = line[index]
21
+ if (character === "'" && !double) single = !single
22
+ else if (character === '"' && !single) double = !double
23
+ else if (character === '#' && !single && !double && (index === 0 || /\s/.test(line[index - 1]))) {
24
+ return line.slice(0, index)
25
+ }
26
+ }
27
+ return line
28
+ }
29
+
30
+ /** Turn one raw scalar into a value; unknown shapes stay strings. */
31
+ function scalar(raw) {
32
+ const value = raw.trim()
33
+ if (value === '' || value === '~' || value === 'null') return null
34
+ if (value.length >= 2) {
35
+ const first = value[0]
36
+ const last = value[value.length - 1]
37
+ if (first === last && (first === '"' || first === "'")) return value.slice(1, -1)
38
+ }
39
+ if (value === 'true') return true
40
+ if (value === 'false') return false
41
+ // Numbers stay strings on purpose: a port, a pool size and a password all read
42
+ // back as text and only the consumer decides what is numeric.
43
+ return value
44
+ }
45
+
46
+ /**
47
+ * Find the `key:` separator of a mapping line.
48
+ *
49
+ * The first colon followed by whitespace (or ending the line) wins, which is what
50
+ * keeps `url: jdbc:mysql://host/db` splitting after `url` instead of after
51
+ * `jdbc`.
52
+ */
53
+ function keySeparator(text) {
54
+ for (let index = 0; index < text.length; index += 1) {
55
+ if (text[index] !== ':') continue
56
+ const next = text[index + 1]
57
+ if (next === undefined || next === ' ' || next === '\t') {
58
+ return { colon: index, rest: text.slice(index + 1).trim() }
59
+ }
60
+ }
61
+ return null
62
+ }
63
+
64
+ /** A block scalar body (`|`, `>`) belongs to the line before it; drop it. */
65
+ function isBlockScalarHeader(text) {
66
+ const separator = keySeparator(text)
67
+ if (separator === null) return false
68
+ return /^[|>][-+]?[0-9]?$/.test(separator.rest)
69
+ }
70
+
71
+ /** Parse a mapping starting at `start` whose keys sit at `indent`. */
72
+ function parseMapping(lines, start, indent) {
73
+ const node = {}
74
+ let index = start
75
+ while (index < lines.length) {
76
+ const line = lines[index]
77
+ if (line.indent < indent) break
78
+ if (line.indent > indent) {
79
+ index += 1
80
+ continue
81
+ }
82
+ if (line.text === '-' || line.text.startsWith('- ')) break
83
+ const separator = keySeparator(line.text)
84
+ if (separator === null) {
85
+ index += 1
86
+ continue
87
+ }
88
+ const key = line.text.slice(0, separator.colon).trim()
89
+ if (key === '') {
90
+ index += 1
91
+ continue
92
+ }
93
+ if (separator.rest === '' || isBlockScalarHeader(line.text)) {
94
+ const next = lines[index + 1]
95
+ if (next !== undefined && next.indent > indent) {
96
+ const parsed = parseBlock(lines, index + 1, next.indent)
97
+ node[key] = parsed.node
98
+ index = parsed.next
99
+ } else if (next !== undefined && next.indent === indent && (next.text === '-' || next.text.startsWith('- '))) {
100
+ const parsed = parseSequence(lines, index + 1, indent)
101
+ node[key] = parsed.node
102
+ index = parsed.next
103
+ } else {
104
+ node[key] = null
105
+ index += 1
106
+ }
107
+ continue
108
+ }
109
+ node[key] = scalar(separator.rest)
110
+ index += 1
111
+ }
112
+ return { node, next: index }
113
+ }
114
+
115
+ /** Parse a block sequence starting at `start` whose dashes sit at `indent`. */
116
+ function parseSequence(lines, start, indent) {
117
+ const node = []
118
+ let index = start
119
+ while (index < lines.length) {
120
+ const line = lines[index]
121
+ if (line.indent !== indent) break
122
+ if (line.text !== '-' && !line.text.startsWith('- ')) break
123
+ const rest = line.text === '-' ? '' : line.text.slice(1).trim()
124
+ if (rest === '') {
125
+ const next = lines[index + 1]
126
+ if (next !== undefined && next.indent > indent) {
127
+ const parsed = parseBlock(lines, index + 1, next.indent)
128
+ node.push(parsed.node)
129
+ index = parsed.next
130
+ } else {
131
+ node.push(null)
132
+ index += 1
133
+ }
134
+ continue
135
+ }
136
+ const separator = keySeparator(rest)
137
+ if (separator === null) {
138
+ node.push(scalar(rest))
139
+ index += 1
140
+ continue
141
+ }
142
+ // `- key: value` opens a mapping whose remaining keys sit deeper than the dash.
143
+ const item = {}
144
+ const firstKey = rest.slice(0, separator.colon).trim()
145
+ item[firstKey] = separator.rest === '' ? null : scalar(separator.rest)
146
+ index += 1
147
+ const next = lines[index]
148
+ if (next !== undefined && next.indent > indent) {
149
+ const parsed = parseMapping(lines, index, next.indent)
150
+ Object.assign(item, parsed.node)
151
+ index = parsed.next
152
+ }
153
+ node.push(item)
154
+ }
155
+ return { node, next: index }
156
+ }
157
+
158
+ function parseBlock(lines, start, indent) {
159
+ if (lines[start] !== undefined && (lines[start].text === '-' || lines[start].text.startsWith('- '))) {
160
+ return parseSequence(lines, start, indent)
161
+ }
162
+ return parseMapping(lines, start, indent)
163
+ }
164
+
165
+ /** Rewrite continuation lines of a block scalar so they are not read as keys. */
166
+ function neutralizeBlockScalars(rawLines) {
167
+ const result = []
168
+ let skipIndent = -1
169
+ for (const raw of rawLines) {
170
+ const line = stripComment(raw)
171
+ const trimmed = line.trim()
172
+ if (skipIndent >= 0) {
173
+ if (trimmed === '' || line.length - line.trimStart().length > skipIndent) {
174
+ result.push({ indent: 0, text: '~', blank: true })
175
+ continue
176
+ }
177
+ skipIndent = -1
178
+ }
179
+ if (trimmed === '') {
180
+ result.push({ indent: 0, text: '', blank: true })
181
+ continue
182
+ }
183
+ result.push({ indent: line.length - line.trimStart().length, text: trimmed, blank: false })
184
+ if (isBlockScalarHeader(trimmed)) skipIndent = line.length - line.trimStart().length
185
+ }
186
+ return result
187
+ }
188
+
189
+ /**
190
+ * Parse a YAML document into plain objects.
191
+ *
192
+ * @returns every non-empty document, in file order.
193
+ */
194
+ export function parseYaml(text) {
195
+ const documents = []
196
+ let buffer = []
197
+ const flush = () => {
198
+ if (buffer.some((line) => stripComment(line).trim() !== '')) {
199
+ const lines = neutralizeBlockScalars(buffer).filter((line) => line.blank === false)
200
+ if (lines.length > 0) documents.push(parseBlock(lines, 0, lines[0].indent).node)
201
+ }
202
+ buffer = []
203
+ }
204
+ for (const raw of String(text).replace(/\r\n?/g, '\n').split('\n')) {
205
+ if (/^---\s*$/.test(raw) || /^\.\.\.\s*$/.test(raw)) {
206
+ flush()
207
+ continue
208
+ }
209
+ buffer.push(raw)
210
+ }
211
+ flush()
212
+ return documents
213
+ }
214
+
215
+ /** Flatten a parsed document into `dotted.path → scalar` entries. */
216
+ export function flattenYaml(node, prefix = '', into = new Map()) {
217
+ if (node === null || typeof node !== 'object') {
218
+ if (prefix !== '') into.set(prefix, node)
219
+ return into
220
+ }
221
+ if (Array.isArray(node)) {
222
+ node.forEach((item, index) => flattenYaml(item, prefix === '' ? String(index) : `${prefix}.${index}`, into))
223
+ return into
224
+ }
225
+ for (const [key, value] of Object.entries(node)) {
226
+ flattenYaml(value, prefix === '' ? key : `${prefix}.${key}`, into)
227
+ }
228
+ return into
229
+ }
230
+
231
+ /** Parse every document of one file and return one merged, flattened map. */
232
+ export function readVariables(text) {
233
+ const variables = new Map()
234
+ for (const document of parseYaml(text)) {
235
+ for (const [key, value] of flattenYaml(document)) variables.set(key, value)
236
+ }
237
+ return variables
238
+ }
239
+
240
+ /** Index of the `}` closing the placeholder opened at `from`; -1 when unbalanced. */
241
+ function closingBrace(text, from) {
242
+ let level = 1
243
+ for (let index = from; index < text.length; index += 1) {
244
+ if (text.startsWith('${', index)) {
245
+ level += 1
246
+ index += 1
247
+ continue
248
+ }
249
+ if (text[index] === '}') {
250
+ level -= 1
251
+ if (level === 0) return index
252
+ }
253
+ }
254
+ return -1
255
+ }
256
+
257
+ /** First `:` that is not inside a nested placeholder. */
258
+ function topLevelColon(expression) {
259
+ let level = 0
260
+ for (let index = 0; index < expression.length; index += 1) {
261
+ if (expression.startsWith('${', index)) {
262
+ level += 1
263
+ index += 1
264
+ continue
265
+ }
266
+ if (expression[index] === '}') {
267
+ level = Math.max(0, level - 1)
268
+ continue
269
+ }
270
+ if (expression[index] === ':' && level === 0) return index
271
+ }
272
+ return -1
273
+ }
274
+
275
+ /**
276
+ * Resolve `${a.b}` and `${a.b:fallback}` against a variable map.
277
+ *
278
+ * Resolution recurses because one config value can name another placeholder, and
279
+ * an unresolvable placeholder becomes its fallback (or an empty string), which is
280
+ * exactly what Spring does. The default may itself contain a placeholder
281
+ * (`${new.name:${old.name}}`), so the closing brace is found by counting nesting
282
+ * instead of taking the first `}`.
283
+ */
284
+ export function resolveTemplate(text, variables, depth = 0) {
285
+ if (typeof text !== 'string' || text === '') return text
286
+ if (depth > 12) return text
287
+ let output = ''
288
+ let index = 0
289
+ while (index < text.length) {
290
+ const start = text.indexOf('${', index)
291
+ if (start < 0) {
292
+ output += text.slice(index)
293
+ break
294
+ }
295
+ output += text.slice(index, start)
296
+ const end = closingBrace(text, start + 2)
297
+ if (end < 0) {
298
+ output += text.slice(start)
299
+ break
300
+ }
301
+ const expression = text.slice(start + 2, end)
302
+ const separator = topLevelColon(expression)
303
+ const name = (separator >= 0 ? expression.slice(0, separator) : expression).trim()
304
+ const fallback = separator >= 0 ? expression.slice(separator + 1) : ''
305
+ const found = variables.get(name)
306
+ output += found === undefined || found === null
307
+ ? resolveTemplate(fallback, variables, depth + 1)
308
+ : resolveTemplate(String(found), variables, depth + 1)
309
+ index = end + 1
310
+ }
311
+ return output
312
+ }
313
+
314
+ /** Every key a template references, including keys inside nested defaults. */
315
+ export function placeholderNames(text) {
316
+ const names = []
317
+ const visit = (value, depth) => {
318
+ if (typeof value !== 'string' || value === '' || depth > 12) return
319
+ let index = 0
320
+ while (index < value.length) {
321
+ const start = value.indexOf('${', index)
322
+ if (start < 0) return
323
+ const end = closingBrace(value, start + 2)
324
+ if (end < 0) return
325
+ const expression = value.slice(start + 2, end)
326
+ const separator = topLevelColon(expression)
327
+ const name = (separator >= 0 ? expression.slice(0, separator) : expression).trim()
328
+ if (name !== '' && !names.includes(name)) names.push(name)
329
+ if (separator >= 0) visit(expression.slice(separator + 1), depth + 1)
330
+ index = end + 1
331
+ }
332
+ }
333
+ visit(text, 0)
334
+ return names
335
+ }