dsh-data-window 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/AGENTS.md +111 -0
- package/CHANGELOG.md +239 -0
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/client.js +7779 -0
- package/cordis.patch.yml +4 -0
- package/docs/BRIDGE.md +100 -0
- package/docs/DESIGN.md +417 -0
- package/docs/DETAIL.md +220 -0
- package/docs/screenshots/ddl.png +0 -0
- package/docs/screenshots/workbench.png +0 -0
- package/icon.svg +30 -0
- package/index.js +180 -0
- package/locale/en.json +286 -0
- package/locale/zh.json +286 -0
- package/package.json +68 -4
- package/src/client/00-css.js +993 -0
- package/src/client/05-icons.js +137 -0
- package/src/client/05-theme.js +107 -0
- package/src/client/06-shortcuts.js +89 -0
- package/src/client/07-bridge.js +193 -0
- package/src/client/10-core.js +1352 -0
- package/src/client/20-tree.js +281 -0
- package/src/client/25-sql.js +631 -0
- package/src/client/30-editor.js +537 -0
- package/src/client/40-results.js +556 -0
- package/src/client/45-export.js +219 -0
- package/src/client/50-panel.js +761 -0
- package/src/client/60-settings.js +180 -0
- package/src/client/63-schema.js +186 -0
- package/src/client/64-manual.js +350 -0
- package/src/client/65-ai.js +320 -0
- package/src/client/66-optimize.js +98 -0
- package/src/client/90-apply.js +183 -0
- package/src/host/ai.js +721 -0
- package/src/host/cache.js +157 -0
- package/src/host/catalog.js +161 -0
- package/src/host/consoles.js +116 -0
- package/src/host/discovery.js +603 -0
- package/src/host/manual.js +276 -0
- package/src/host/mysql.js +650 -0
- package/src/host/remembered.js +225 -0
- package/src/host/rpc.js +487 -0
- package/src/host/shared.js +172 -0
- package/src/host/yaml.js +335 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manually added connections.
|
|
3
|
+
*
|
|
4
|
+
* The rule scan and the AI fallback both try to *derive* a connection from files.
|
|
5
|
+
* Neither helps when the database a user needs simply is not described anywhere in
|
|
6
|
+
* the workspace — a local container, a shared test instance, a customer's replica.
|
|
7
|
+
* This module is the third path: the user types host, port, account and database,
|
|
8
|
+
* and the window keeps them.
|
|
9
|
+
*
|
|
10
|
+
* Two properties follow from the plugin's existing rules:
|
|
11
|
+
*
|
|
12
|
+
* - **A manual connection is proven before it is saved.** The same read-only
|
|
13
|
+
* gateway opens it once; a typo then fails at save time with the server's own
|
|
14
|
+
* message instead of becoming a tree row that never works.
|
|
15
|
+
* - **The password stays in the Host.** It is written to a 0600 file next to the
|
|
16
|
+
* discovery cache (one file per workspace) and never appears in the payload the
|
|
17
|
+
* browser renders; the browser only ever names a target id.
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from 'node:crypto'
|
|
20
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, writeFileSync } from 'node:fs'
|
|
21
|
+
import { basename, isAbsolute, join } from 'node:path'
|
|
22
|
+
import { stableId } from './discovery.js'
|
|
23
|
+
|
|
24
|
+
/** Bump when the document shape changes so old files are ignored. */
|
|
25
|
+
const MANUAL_VERSION = 1
|
|
26
|
+
|
|
27
|
+
/** More manual connections than this in one workspace is a mistake, not a workflow. */
|
|
28
|
+
const MAX_ENTRIES = 60
|
|
29
|
+
|
|
30
|
+
/** A label long enough to tell two connections apart, short enough for the tree. */
|
|
31
|
+
const MAX_LABEL = 40
|
|
32
|
+
|
|
33
|
+
/** The shared document: one list of manual connections for the whole machine. */
|
|
34
|
+
const SHARED_FILE = 'connections.json'
|
|
35
|
+
|
|
36
|
+
/** One workspace's file, named so a human can tell them apart. */
|
|
37
|
+
function manualFile(directory, root) {
|
|
38
|
+
const hash = createHash('sha256').update(root).digest('hex').slice(0, 16)
|
|
39
|
+
const name = basename(root).replace(/[^\w.-]+/g, '_').slice(0, 40) || 'workspace'
|
|
40
|
+
return join(directory, `${hash}-${name}.manual.json`)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Coerce one submitted entry into the shape the store keeps; undefined when unsuited. */
|
|
44
|
+
export function normalizeManualEntry(value) {
|
|
45
|
+
if (value === null || typeof value !== 'object') return undefined
|
|
46
|
+
const host = String(value.host ?? '').trim()
|
|
47
|
+
const user = String(value.user ?? '').trim()
|
|
48
|
+
const port = Number(value.port ?? 3306)
|
|
49
|
+
if (host === '' || !/^[A-Za-z0-9._:[\]-]+$/.test(host)) return undefined
|
|
50
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) return undefined
|
|
51
|
+
if (user === '' || user.includes('\u0000')) return undefined
|
|
52
|
+
const database = String(value.database ?? '').trim()
|
|
53
|
+
const label = String(value.label ?? '').trim().slice(0, MAX_LABEL) || `${host}:${port}`
|
|
54
|
+
return {
|
|
55
|
+
id: `m:${stableId('manual-entry', label, host, String(port), user)}`,
|
|
56
|
+
label,
|
|
57
|
+
host,
|
|
58
|
+
port,
|
|
59
|
+
user,
|
|
60
|
+
password: String(value.password ?? ''),
|
|
61
|
+
database,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Manual entries, one small JSON file per workspace. */
|
|
66
|
+
export class ManualStore {
|
|
67
|
+
/**
|
|
68
|
+
* @param directory the `manual/` storage directory
|
|
69
|
+
* @param options `{ shared: true }` keeps one list for the whole machine instead
|
|
70
|
+
* of one per workspace; the tree is shared, so its manual entries are too
|
|
71
|
+
*/
|
|
72
|
+
constructor(directory, options = {}) {
|
|
73
|
+
this.directory = directory
|
|
74
|
+
this.shared = options.shared === true
|
|
75
|
+
if (this.directory !== '') {
|
|
76
|
+
mkdirSync(this.directory, { recursive: true, mode: 0o700 })
|
|
77
|
+
if (this.shared) this.migrateLegacy()
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The document these entries live in. */
|
|
82
|
+
fileFor(root) {
|
|
83
|
+
if (this.shared) return join(this.directory, SHARED_FILE)
|
|
84
|
+
return manualFile(this.directory, root)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Fold the per-workspace files into the shared one, once. */
|
|
88
|
+
migrateLegacy() {
|
|
89
|
+
const shared = this.fileFor('')
|
|
90
|
+
if (existsSync(shared)) return
|
|
91
|
+
let names = []
|
|
92
|
+
try {
|
|
93
|
+
names = readdirSync(this.directory).filter((name) => name.endsWith('.manual.json'))
|
|
94
|
+
} catch (error) {
|
|
95
|
+
void error
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (names.length === 0) return
|
|
99
|
+
const entries = []
|
|
100
|
+
for (const name of names) {
|
|
101
|
+
try {
|
|
102
|
+
const document = JSON.parse(readFileSync(join(this.directory, name), 'utf8'))
|
|
103
|
+
for (const raw of Array.isArray(document?.entries) ? document.entries : []) {
|
|
104
|
+
const entry = normalizeManualEntry(raw)
|
|
105
|
+
if (entry === undefined) continue
|
|
106
|
+
if (entries.some((existing) => existing.id === entry.id)) continue
|
|
107
|
+
entries.push(entry)
|
|
108
|
+
}
|
|
109
|
+
} catch (error) {
|
|
110
|
+
void error
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
this.save('', entries)
|
|
114
|
+
// The legacy files stay as a readable backup, but stop being sources: a deleted
|
|
115
|
+
// connection must not come back from a file nothing updates any more.
|
|
116
|
+
for (const name of names) {
|
|
117
|
+
try {
|
|
118
|
+
renameSync(join(this.directory, name), join(this.directory, `${name}.migrated`))
|
|
119
|
+
} catch (error) {
|
|
120
|
+
void error
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Read the entries; a missing or unreadable file means "none added yet". */
|
|
126
|
+
load(root) {
|
|
127
|
+
if (this.directory === '') return { entries: [] }
|
|
128
|
+
if (!this.shared && (typeof root !== 'string' || root === '' || !isAbsolute(root))) return { entries: [] }
|
|
129
|
+
try {
|
|
130
|
+
const parsed = JSON.parse(readFileSync(this.fileFor(root), 'utf8'))
|
|
131
|
+
if (parsed?.version !== MANUAL_VERSION) return { entries: [] }
|
|
132
|
+
const entries = []
|
|
133
|
+
for (const raw of Array.isArray(parsed.entries) ? parsed.entries : []) {
|
|
134
|
+
const entry = normalizeManualEntry(raw)
|
|
135
|
+
// A hand-edited file can contain the same id twice; the first one wins.
|
|
136
|
+
if (entry === undefined || entries.some((existing) => existing.id === entry.id)) continue
|
|
137
|
+
entries.push(entry)
|
|
138
|
+
}
|
|
139
|
+
return { entries: entries.slice(0, MAX_ENTRIES) }
|
|
140
|
+
} catch (error) {
|
|
141
|
+
void error
|
|
142
|
+
return { entries: [] }
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Replace one workspace's entries atomically, 0600. */
|
|
147
|
+
save(root, entries) {
|
|
148
|
+
if (this.directory === '') {
|
|
149
|
+
const error = new Error('manual connections are unavailable in this build')
|
|
150
|
+
error.code = 'manual-unavailable'
|
|
151
|
+
throw error
|
|
152
|
+
}
|
|
153
|
+
if (!this.shared && (typeof root !== 'string' || root === '' || !isAbsolute(root))) {
|
|
154
|
+
const error = new Error('workspace root must be an absolute path')
|
|
155
|
+
error.code = 'invalid-root'
|
|
156
|
+
throw error
|
|
157
|
+
}
|
|
158
|
+
const document = JSON.stringify({
|
|
159
|
+
version: MANUAL_VERSION,
|
|
160
|
+
...(this.shared ? {} : { root }),
|
|
161
|
+
savedAt: Date.now(),
|
|
162
|
+
entries: entries.slice(0, MAX_ENTRIES),
|
|
163
|
+
})
|
|
164
|
+
const file = this.fileFor(root)
|
|
165
|
+
const temporary = `${file}.${process.pid}.tmp`
|
|
166
|
+
writeFileSync(temporary, document, { mode: 0o600 })
|
|
167
|
+
renameSync(temporary, file)
|
|
168
|
+
return { saved: true, entries: entries.length }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Add or replace one entry, keyed by its stable id. */
|
|
172
|
+
upsert(root, entry) {
|
|
173
|
+
const { entries } = this.load(root)
|
|
174
|
+
const next = entries.some((existing) => existing.id === entry.id)
|
|
175
|
+
? entries.map((existing) => (existing.id === entry.id ? entry : existing))
|
|
176
|
+
: [...entries, entry]
|
|
177
|
+
this.save(root, next)
|
|
178
|
+
return entry
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Drop one entry; returns whether it existed. */
|
|
182
|
+
remove(root, id) {
|
|
183
|
+
const { entries } = this.load(root)
|
|
184
|
+
const next = entries.filter((entry) => entry.id !== id)
|
|
185
|
+
if (next.length === entries.length) return false
|
|
186
|
+
this.save(root, next)
|
|
187
|
+
return true
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Turn stored entries into the environment shape the panel already renders.
|
|
193
|
+
*
|
|
194
|
+
* Entries sharing a label form one environment, so "本地" can hold both MySQL 5.7
|
|
195
|
+
* and 8.0 without inventing two entries a user has to tell apart.
|
|
196
|
+
*
|
|
197
|
+
* @returns `{ environments, targets }`; only `targets` carries passwords
|
|
198
|
+
*/
|
|
199
|
+
export function manualEnvironments(entries) {
|
|
200
|
+
const groups = new Map()
|
|
201
|
+
const targets = new Map()
|
|
202
|
+
for (const entry of Array.isArray(entries) ? entries : []) {
|
|
203
|
+
const env = `manual:${stableId('manual-env', entry.label)}`
|
|
204
|
+
const instanceId = `i:${stableId('manual', env, entry.host, String(entry.port), entry.user)}`
|
|
205
|
+
const databaseId = `d:${stableId('manual', instanceId, entry.database)}`
|
|
206
|
+
let group = groups.get(env)
|
|
207
|
+
if (group === undefined) {
|
|
208
|
+
group = { env, label: entry.label, instances: [], targets: [], databaseCount: 0 }
|
|
209
|
+
groups.set(env, group)
|
|
210
|
+
}
|
|
211
|
+
let instance = group.instances.find((candidate) => candidate.id === instanceId)
|
|
212
|
+
if (instance === undefined) {
|
|
213
|
+
instance = {
|
|
214
|
+
id: instanceId,
|
|
215
|
+
host: entry.host,
|
|
216
|
+
port: entry.port,
|
|
217
|
+
label: `${entry.host}:${entry.port}`,
|
|
218
|
+
// The user typed this host, so it is the name to show in the tree — unlike
|
|
219
|
+
// a discovered instance, there is no configuration alias hiding behind it.
|
|
220
|
+
aliases: [entry.host],
|
|
221
|
+
user: entry.user,
|
|
222
|
+
manual: true,
|
|
223
|
+
entryId: entry.id,
|
|
224
|
+
databases: [],
|
|
225
|
+
}
|
|
226
|
+
group.instances.push(instance)
|
|
227
|
+
group.targets.push([instanceId, {
|
|
228
|
+
id: instanceId,
|
|
229
|
+
env,
|
|
230
|
+
host: entry.host,
|
|
231
|
+
port: entry.port,
|
|
232
|
+
user: entry.user,
|
|
233
|
+
password: entry.password,
|
|
234
|
+
database: undefined,
|
|
235
|
+
manual: true,
|
|
236
|
+
}])
|
|
237
|
+
}
|
|
238
|
+
if (entry.database !== '' && !instance.databases.some((database) => database.name === entry.database)) {
|
|
239
|
+
instance.databases.push({
|
|
240
|
+
id: databaseId,
|
|
241
|
+
name: entry.database,
|
|
242
|
+
user: entry.user,
|
|
243
|
+
alias: entry.label,
|
|
244
|
+
configured: true,
|
|
245
|
+
manual: true,
|
|
246
|
+
})
|
|
247
|
+
group.targets.push([databaseId, {
|
|
248
|
+
id: databaseId,
|
|
249
|
+
env,
|
|
250
|
+
host: entry.host,
|
|
251
|
+
port: entry.port,
|
|
252
|
+
user: entry.user,
|
|
253
|
+
password: entry.password,
|
|
254
|
+
database: entry.database,
|
|
255
|
+
manual: true,
|
|
256
|
+
}])
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const environments = []
|
|
261
|
+
for (const group of groups.values()) {
|
|
262
|
+
const databaseCount = group.instances.reduce((total, instance) => total + instance.databases.length, 0)
|
|
263
|
+
for (const [id, target] of group.targets) targets.set(id, target)
|
|
264
|
+
environments.push({
|
|
265
|
+
env: group.env,
|
|
266
|
+
label: group.label,
|
|
267
|
+
manual: true,
|
|
268
|
+
cacheServerMatch: false,
|
|
269
|
+
variableFiles: [],
|
|
270
|
+
sources: [],
|
|
271
|
+
databaseCount,
|
|
272
|
+
instances: group.instances,
|
|
273
|
+
})
|
|
274
|
+
}
|
|
275
|
+
return { environments, targets }
|
|
276
|
+
}
|