@myrialabs/clopen 0.3.7 → 0.3.8
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/backend/database/migrations/030_add_files_panel_state_to_user_projects.ts +34 -0
- package/backend/database/migrations/031_create_db_client_tables.ts +74 -0
- package/backend/database/migrations/index.ts +14 -0
- package/backend/database/queries/db-client-connection-queries.ts +237 -0
- package/backend/database/queries/db-client-query-history-queries.ts +113 -0
- package/backend/database/queries/index.ts +3 -1
- package/backend/database/queries/project-queries.ts +23 -0
- package/backend/db-client/connection-manager.ts +257 -0
- package/backend/db-client/drivers/bun-sql-helpers.ts +41 -0
- package/backend/db-client/drivers/mongodb.ts +358 -0
- package/backend/db-client/drivers/mysql.ts +385 -0
- package/backend/db-client/drivers/postgres.ts +446 -0
- package/backend/db-client/drivers/redis.ts +267 -0
- package/backend/db-client/drivers/sql-builders.ts +215 -0
- package/backend/db-client/drivers/sqlite.ts +346 -0
- package/backend/db-client/drivers/types.ts +92 -0
- package/backend/db-client/query-executor.ts +126 -0
- package/backend/db-client/ssh-tunnel.ts +122 -0
- package/backend/git/git-service.ts +12 -0
- package/backend/preview/browser/scripts/video-stream.ts +9 -3
- package/backend/ws/db-client/connections.ts +180 -0
- package/backend/ws/db-client/index.ts +17 -0
- package/backend/ws/db-client/io.ts +458 -0
- package/backend/ws/db-client/query.ts +162 -0
- package/backend/ws/db-client/schema.ts +76 -0
- package/backend/ws/db-client/structure.ts +218 -0
- package/backend/ws/files/index.ts +5 -1
- package/backend/ws/files/read.ts +32 -0
- package/backend/ws/files/state.ts +39 -0
- package/backend/ws/index.ts +5 -1
- package/dist/assets/{index-C86jwBxR.js → index-DqAGCTFT.js} +2 -2
- package/dist/assets/{main-DhLx8wD1.js → main-DsED0rLr.js} +216 -197
- package/dist/assets/main-eWY9R8g9.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +4 -1
- package/shared/types/database/schema.ts +48 -1
- package/shared/types/db-client/connection.ts +60 -0
- package/shared/types/db-client/index.ts +6 -0
- package/shared/types/db-client/query.ts +77 -0
- package/shared/utils/logger.ts +1 -0
- package/dist/assets/main-jTnmAoXS.css +0 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* db-client — query classification + safe-execute helpers.
|
|
3
|
+
*
|
|
4
|
+
* `runSafely` applies auto-LIMIT 500 to bare SELECT/WITH reads.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
DbClientQueryResult,
|
|
9
|
+
DbDriver
|
|
10
|
+
} from '$shared/types/db-client';
|
|
11
|
+
import type { DbClientDriverAdapter } from './drivers/types';
|
|
12
|
+
|
|
13
|
+
export type QueryClass = 'read' | 'write' | 'ddl' | 'unknown';
|
|
14
|
+
|
|
15
|
+
const SQL_READ_TOKENS = new Set(['SELECT', 'SHOW', 'DESCRIBE', 'DESC', 'EXPLAIN', 'WITH', 'PRAGMA']);
|
|
16
|
+
const SQL_WRITE_TOKENS = new Set(['INSERT', 'UPDATE', 'DELETE', 'REPLACE', 'MERGE', 'UPSERT']);
|
|
17
|
+
const SQL_DDL_TOKENS = new Set(['CREATE', 'DROP', 'ALTER', 'TRUNCATE', 'RENAME', 'GRANT', 'REVOKE']);
|
|
18
|
+
|
|
19
|
+
const MONGO_READ_OPS = new Set(['find', 'findOne', 'aggregate', 'countDocuments', 'distinct', 'estimatedDocumentCount']);
|
|
20
|
+
const REDIS_READ_CMDS = new Set([
|
|
21
|
+
'GET', 'MGET', 'EXISTS', 'TYPE', 'TTL', 'PTTL', 'KEYS', 'SCAN',
|
|
22
|
+
'HGET', 'HGETALL', 'HKEYS', 'HVALS', 'HLEN',
|
|
23
|
+
'LRANGE', 'LLEN', 'LINDEX',
|
|
24
|
+
'SMEMBERS', 'SISMEMBER', 'SCARD',
|
|
25
|
+
'ZRANGE', 'ZRANGEBYSCORE', 'ZRANK', 'ZSCORE', 'ZCARD',
|
|
26
|
+
'PING', 'INFO', 'DBSIZE', 'CLIENT'
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
function firstSqlKeyword(query: string): string {
|
|
30
|
+
let i = 0;
|
|
31
|
+
while (i < query.length) {
|
|
32
|
+
const ch = query[i];
|
|
33
|
+
if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
|
|
34
|
+
i++;
|
|
35
|
+
} else if (ch === '-' && query[i + 1] === '-') {
|
|
36
|
+
const nl = query.indexOf('\n', i);
|
|
37
|
+
if (nl === -1) return '';
|
|
38
|
+
i = nl + 1;
|
|
39
|
+
} else if (ch === '/' && query[i + 1] === '*') {
|
|
40
|
+
const end = query.indexOf('*/', i + 2);
|
|
41
|
+
if (end === -1) return '';
|
|
42
|
+
i = end + 2;
|
|
43
|
+
} else {
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const rest = query.slice(i);
|
|
48
|
+
const match = /^([A-Za-z]+)/.exec(rest);
|
|
49
|
+
return match ? match[1].toUpperCase() : '';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function classifyQuery(driver: DbDriver, query: string): QueryClass {
|
|
53
|
+
if (driver === 'mongodb') {
|
|
54
|
+
try {
|
|
55
|
+
const parsed = JSON.parse(query) as { op?: string };
|
|
56
|
+
if (parsed.op && MONGO_READ_OPS.has(parsed.op)) return 'read';
|
|
57
|
+
if (parsed.op) return 'write';
|
|
58
|
+
} catch {
|
|
59
|
+
return 'unknown';
|
|
60
|
+
}
|
|
61
|
+
return 'unknown';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (driver === 'redis') {
|
|
65
|
+
const trimmed = query.trim();
|
|
66
|
+
let cmd: string;
|
|
67
|
+
if (trimmed.startsWith('[')) {
|
|
68
|
+
try {
|
|
69
|
+
const parsed = JSON.parse(trimmed) as unknown;
|
|
70
|
+
if (Array.isArray(parsed) && parsed.length > 0) {
|
|
71
|
+
cmd = String(parsed[0]).toUpperCase();
|
|
72
|
+
} else {
|
|
73
|
+
return 'unknown';
|
|
74
|
+
}
|
|
75
|
+
} catch {
|
|
76
|
+
return 'unknown';
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
cmd = (trimmed.split(/\s+/)[0] ?? '').toUpperCase();
|
|
80
|
+
}
|
|
81
|
+
if (!cmd) return 'unknown';
|
|
82
|
+
return REDIS_READ_CMDS.has(cmd) ? 'read' : 'write';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const kw = firstSqlKeyword(query);
|
|
86
|
+
if (!kw) return 'unknown';
|
|
87
|
+
if (SQL_READ_TOKENS.has(kw)) return 'read';
|
|
88
|
+
if (SQL_WRITE_TOKENS.has(kw)) return 'write';
|
|
89
|
+
if (SQL_DDL_TOKENS.has(kw)) return 'ddl';
|
|
90
|
+
return 'unknown';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Append `LIMIT n` to a SELECT/WITH query that does not already specify
|
|
95
|
+
* one. Conservative — only touches plain SQL reads.
|
|
96
|
+
*/
|
|
97
|
+
export function applyAutoLimit(query: string, limit = 500): string {
|
|
98
|
+
const trimmed = query.replace(/;\s*$/, '');
|
|
99
|
+
const kw = firstSqlKeyword(trimmed);
|
|
100
|
+
if (kw !== 'SELECT' && kw !== 'WITH') return query;
|
|
101
|
+
if (/\blimit\s+\d+/i.test(trimmed)) return query;
|
|
102
|
+
return `${trimmed} LIMIT ${limit}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface RunSafelyInput {
|
|
106
|
+
driver: DbDriver;
|
|
107
|
+
adapter: DbClientDriverAdapter;
|
|
108
|
+
query: string;
|
|
109
|
+
params?: unknown[];
|
|
110
|
+
mode: 'read' | 'write';
|
|
111
|
+
database?: string;
|
|
112
|
+
limit?: number;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export async function runSafely(input: RunSafelyInput): Promise<DbClientQueryResult> {
|
|
116
|
+
const { adapter, driver, mode, params, database, limit } = input;
|
|
117
|
+
let query = input.query;
|
|
118
|
+
|
|
119
|
+
if (mode === 'read' && (driver === 'mysql' || driver === 'postgres' || driver === 'sqlite')) {
|
|
120
|
+
query = applyAutoLimit(query, limit ?? 500);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const fn = mode === 'read' ? adapter.executeRead : adapter.executeWrite;
|
|
124
|
+
if (!fn) throw new Error(`Driver ${driver} does not support ${mode}`);
|
|
125
|
+
return fn.call(adapter, query, params, { database });
|
|
126
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSH tunnel — opens a local TCP listener that forwards to a remote
|
|
3
|
+
* host:port through an authenticated `ssh2` client. Used by the
|
|
4
|
+
* connection manager to reach databases that sit behind a bastion.
|
|
5
|
+
*
|
|
6
|
+
* Pattern verified end-to-end in PHASE 0 (`test-ssh-mysql.ts`):
|
|
7
|
+
* 1. ssh2.Client.connect with password / privateKey + optional passphrase
|
|
8
|
+
* 2. net.createServer on an ephemeral port
|
|
9
|
+
* 3. for each accepted socket → client.forwardOut → pipe both directions
|
|
10
|
+
* 4. close() shuts down the local listener and ends the ssh client
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import net from 'net';
|
|
14
|
+
import { Client as SshClient, type ConnectConfig } from 'ssh2';
|
|
15
|
+
import type { DbClientSshConfig } from '$shared/types/db-client';
|
|
16
|
+
import { debug } from '$shared/utils/logger';
|
|
17
|
+
|
|
18
|
+
export interface SshTunnel {
|
|
19
|
+
localPort: number;
|
|
20
|
+
close(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function openSshTunnel(
|
|
24
|
+
ssh: DbClientSshConfig,
|
|
25
|
+
remoteHost: string,
|
|
26
|
+
remotePort: number
|
|
27
|
+
): Promise<SshTunnel> {
|
|
28
|
+
if (!ssh.enabled) {
|
|
29
|
+
throw new Error('SSH tunnel requested but ssh.enabled is false');
|
|
30
|
+
}
|
|
31
|
+
if (!ssh.host || !ssh.username) {
|
|
32
|
+
throw new Error('SSH tunnel requires host and username');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const connectConfig: ConnectConfig = {
|
|
36
|
+
host: ssh.host,
|
|
37
|
+
port: ssh.port || 22,
|
|
38
|
+
username: ssh.username,
|
|
39
|
+
readyTimeout: 15_000,
|
|
40
|
+
keepaliveInterval: 30_000
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (ssh.authMethod === 'key') {
|
|
44
|
+
if (!ssh.privateKey) {
|
|
45
|
+
throw new Error('SSH key auth requires privateKey');
|
|
46
|
+
}
|
|
47
|
+
connectConfig.privateKey = ssh.privateKey;
|
|
48
|
+
if (ssh.passphrase) connectConfig.passphrase = ssh.passphrase;
|
|
49
|
+
} else {
|
|
50
|
+
if (!ssh.password) {
|
|
51
|
+
throw new Error('SSH password auth requires password');
|
|
52
|
+
}
|
|
53
|
+
connectConfig.password = ssh.password;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const client = new SshClient();
|
|
57
|
+
|
|
58
|
+
await new Promise<void>((resolve, reject) => {
|
|
59
|
+
const onReady = (): void => {
|
|
60
|
+
client.removeListener('error', onError);
|
|
61
|
+
resolve();
|
|
62
|
+
};
|
|
63
|
+
const onError = (err: Error): void => {
|
|
64
|
+
client.removeListener('ready', onReady);
|
|
65
|
+
reject(err);
|
|
66
|
+
};
|
|
67
|
+
client.once('ready', onReady);
|
|
68
|
+
client.once('error', onError);
|
|
69
|
+
try {
|
|
70
|
+
client.connect(connectConfig);
|
|
71
|
+
} catch (err) {
|
|
72
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const server = net.createServer((socket) => {
|
|
77
|
+
client.forwardOut(
|
|
78
|
+
socket.remoteAddress ?? '127.0.0.1',
|
|
79
|
+
socket.remotePort ?? 0,
|
|
80
|
+
remoteHost,
|
|
81
|
+
remotePort,
|
|
82
|
+
(err, stream) => {
|
|
83
|
+
if (err) {
|
|
84
|
+
debug.warn('db-client', 'SSH forwardOut failed:', err.message);
|
|
85
|
+
socket.destroy();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
socket.pipe(stream).pipe(socket);
|
|
89
|
+
stream.on('error', () => socket.destroy());
|
|
90
|
+
socket.on('error', () => stream.destroy());
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const localPort = await new Promise<number>((resolve, reject) => {
|
|
96
|
+
server.once('error', reject);
|
|
97
|
+
server.listen(0, '127.0.0.1', () => {
|
|
98
|
+
const addr = server.address();
|
|
99
|
+
if (addr && typeof addr === 'object') {
|
|
100
|
+
resolve(addr.port);
|
|
101
|
+
} else {
|
|
102
|
+
reject(new Error('Failed to obtain ephemeral port for SSH tunnel'));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
debug.log('db-client', `SSH tunnel up: 127.0.0.1:${localPort} → ${remoteHost}:${remotePort} via ${ssh.host}`);
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
localPort,
|
|
111
|
+
async close() {
|
|
112
|
+
await new Promise<void>((resolve) => {
|
|
113
|
+
server.close(() => resolve());
|
|
114
|
+
});
|
|
115
|
+
try {
|
|
116
|
+
client.end();
|
|
117
|
+
} catch {
|
|
118
|
+
// noop — already closed
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -187,6 +187,18 @@ export class GitService {
|
|
|
187
187
|
return parseDiff(result.stdout);
|
|
188
188
|
}
|
|
189
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Read a file's content at a specific git ref (e.g. HEAD).
|
|
192
|
+
* Returns null when the file does not exist at that ref (untracked / new file).
|
|
193
|
+
*/
|
|
194
|
+
async getFileAtRef(cwd: string, ref: string, filePath: string): Promise<string | null> {
|
|
195
|
+
const result = await execGit(['show', `${ref}:${filePath}`], cwd);
|
|
196
|
+
if (result.exitCode !== 0) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
return result.stdout;
|
|
200
|
+
}
|
|
201
|
+
|
|
190
202
|
// ============================================
|
|
191
203
|
// Branches
|
|
192
204
|
// ============================================
|
|
@@ -34,9 +34,15 @@ export function videoEncoderScript(config: StreamingConfig['video']) {
|
|
|
34
34
|
let lastCursor = 'default';
|
|
35
35
|
let cursorCheckInterval: any = null;
|
|
36
36
|
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
|
|
37
|
+
// Public STUN lets the headless browser discover its public IP via srflx
|
|
38
|
+
// candidates. Required when peers are on different machines/networks
|
|
39
|
+
// (e.g. clopen deployed to Railway/VPS with the client browser elsewhere).
|
|
40
|
+
// Host candidates still resolve first on same-machine setups, so the
|
|
41
|
+
// happy path is not slowed.
|
|
42
|
+
const iceServers: { urls: string }[] = [
|
|
43
|
+
{ urls: 'stun:stun.l.google.com:19302' },
|
|
44
|
+
{ urls: 'stun:stun1.l.google.com:19302' }
|
|
45
|
+
];
|
|
40
46
|
|
|
41
47
|
// Create a loopback (127.0.0.1) copy of a host ICE candidate.
|
|
42
48
|
// Ensures WebRTC connects via loopback when VPN (e.g. Cloudflare WARP)
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* db-client — connection CRUD + health WS handlers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { t } from 'elysia';
|
|
6
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
7
|
+
import { initializeDatabase } from '../../database';
|
|
8
|
+
import { dbClientConnectionQueries } from '../../database/queries';
|
|
9
|
+
import { connectionManager } from '../../db-client/connection-manager';
|
|
10
|
+
import type {
|
|
11
|
+
DbClientConnectionInput,
|
|
12
|
+
DbDriver,
|
|
13
|
+
DbSshAuthMethod,
|
|
14
|
+
DbSslMode
|
|
15
|
+
} from '$shared/types/db-client';
|
|
16
|
+
import { debug } from '$shared/utils/logger';
|
|
17
|
+
|
|
18
|
+
const driverSchema = t.Union([
|
|
19
|
+
t.Literal('mysql'),
|
|
20
|
+
t.Literal('postgres'),
|
|
21
|
+
t.Literal('sqlite'),
|
|
22
|
+
t.Literal('mongodb'),
|
|
23
|
+
t.Literal('redis')
|
|
24
|
+
]);
|
|
25
|
+
|
|
26
|
+
const sslModeSchema = t.Union([
|
|
27
|
+
t.Literal('disable'),
|
|
28
|
+
t.Literal('require'),
|
|
29
|
+
t.Literal('verify-ca'),
|
|
30
|
+
t.Literal('verify-full')
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
const sshAuthSchema = t.Union([t.Literal('password'), t.Literal('key')]);
|
|
34
|
+
|
|
35
|
+
const sshInputSchema = t.Object({
|
|
36
|
+
enabled: t.Optional(t.Boolean()),
|
|
37
|
+
host: t.Optional(t.String()),
|
|
38
|
+
port: t.Optional(t.Number()),
|
|
39
|
+
username: t.Optional(t.String()),
|
|
40
|
+
authMethod: t.Optional(sshAuthSchema),
|
|
41
|
+
password: t.Optional(t.String()),
|
|
42
|
+
privateKey: t.Optional(t.String()),
|
|
43
|
+
passphrase: t.Optional(t.String())
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const connectionInputSchema = t.Object({
|
|
47
|
+
name: t.String({ minLength: 1 }),
|
|
48
|
+
driver: driverSchema,
|
|
49
|
+
host: t.Optional(t.String()),
|
|
50
|
+
port: t.Optional(t.Number()),
|
|
51
|
+
username: t.Optional(t.String()),
|
|
52
|
+
password: t.Optional(t.String()),
|
|
53
|
+
database: t.Optional(t.String()),
|
|
54
|
+
sslMode: t.Optional(sslModeSchema),
|
|
55
|
+
sslCa: t.Optional(t.String()),
|
|
56
|
+
ssh: t.Optional(sshInputSchema),
|
|
57
|
+
options: t.Optional(t.Record(t.String(), t.Any())),
|
|
58
|
+
color: t.Optional(t.String())
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const connectionPatchSchema = t.Object({
|
|
62
|
+
name: t.Optional(t.String({ minLength: 1 })),
|
|
63
|
+
driver: t.Optional(driverSchema),
|
|
64
|
+
host: t.Optional(t.String()),
|
|
65
|
+
port: t.Optional(t.Number()),
|
|
66
|
+
username: t.Optional(t.String()),
|
|
67
|
+
password: t.Optional(t.String()),
|
|
68
|
+
database: t.Optional(t.String()),
|
|
69
|
+
sslMode: t.Optional(sslModeSchema),
|
|
70
|
+
sslCa: t.Optional(t.String()),
|
|
71
|
+
ssh: t.Optional(sshInputSchema),
|
|
72
|
+
options: t.Optional(t.Record(t.String(), t.Any())),
|
|
73
|
+
color: t.Optional(t.String())
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const healthSchema = t.Object({
|
|
77
|
+
ok: t.Boolean(),
|
|
78
|
+
latencyMs: t.Nullable(t.Number()),
|
|
79
|
+
serverVersion: t.Nullable(t.String()),
|
|
80
|
+
sshOk: t.Nullable(t.Boolean()),
|
|
81
|
+
error: t.Nullable(t.String())
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const connectionTestSchema = t.Union([
|
|
85
|
+
t.Object({ id: t.String({ minLength: 1 }) }),
|
|
86
|
+
connectionInputSchema
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
function isInput(v: unknown): v is DbClientConnectionInput {
|
|
90
|
+
return typeof v === 'object' && v !== null && 'driver' in (v as Record<string, unknown>);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function normalizeSshPatch(input: DbClientConnectionInput['ssh']): DbClientConnectionInput['ssh'] {
|
|
94
|
+
if (!input) return undefined;
|
|
95
|
+
return {
|
|
96
|
+
...input,
|
|
97
|
+
authMethod: (input.authMethod ?? 'password') as DbSshAuthMethod
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function ensureInputDefaults(input: DbClientConnectionInput): DbClientConnectionInput {
|
|
102
|
+
return {
|
|
103
|
+
...input,
|
|
104
|
+
driver: input.driver as DbDriver,
|
|
105
|
+
sslMode: (input.sslMode ?? 'disable') as DbSslMode,
|
|
106
|
+
ssh: normalizeSshPatch(input.ssh)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const connectionsHandler = createRouter()
|
|
111
|
+
.http('db-client:list', {
|
|
112
|
+
data: t.Object({}),
|
|
113
|
+
response: t.Array(t.Any())
|
|
114
|
+
}, async () => {
|
|
115
|
+
await initializeDatabase();
|
|
116
|
+
return dbClientConnectionQueries.list();
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
.http('db-client:get', {
|
|
120
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
121
|
+
response: t.Any()
|
|
122
|
+
}, async ({ data }) => {
|
|
123
|
+
const conn = dbClientConnectionQueries.get(data.id);
|
|
124
|
+
if (!conn) throw new Error('db-client connection not found');
|
|
125
|
+
return conn;
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
.http('db-client:create', {
|
|
129
|
+
data: connectionInputSchema,
|
|
130
|
+
response: t.Any()
|
|
131
|
+
}, async ({ data }) => {
|
|
132
|
+
await initializeDatabase();
|
|
133
|
+
const conn = dbClientConnectionQueries.create(ensureInputDefaults(data as DbClientConnectionInput));
|
|
134
|
+
debug.log('db-client', `created connection ${conn.id} (${conn.driver})`);
|
|
135
|
+
return conn;
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
.http('db-client:update', {
|
|
139
|
+
data: t.Object({
|
|
140
|
+
id: t.String({ minLength: 1 }),
|
|
141
|
+
patch: connectionPatchSchema
|
|
142
|
+
}),
|
|
143
|
+
response: t.Any()
|
|
144
|
+
}, async ({ data }) => {
|
|
145
|
+
const patch = data.patch as Partial<DbClientConnectionInput>;
|
|
146
|
+
const normalized: Partial<DbClientConnectionInput> = {
|
|
147
|
+
...patch,
|
|
148
|
+
ssh: patch.ssh ? normalizeSshPatch(patch.ssh) : undefined
|
|
149
|
+
};
|
|
150
|
+
// Drop the live adapter so the next access re-opens with new settings.
|
|
151
|
+
await connectionManager.release(data.id);
|
|
152
|
+
return dbClientConnectionQueries.update(data.id, normalized);
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
.http('db-client:delete', {
|
|
156
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
157
|
+
response: t.Object({ ok: t.Boolean() })
|
|
158
|
+
}, async ({ data }) => {
|
|
159
|
+
await connectionManager.release(data.id);
|
|
160
|
+
dbClientConnectionQueries.delete(data.id);
|
|
161
|
+
return { ok: true };
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
.http('db-client:test', {
|
|
165
|
+
data: connectionTestSchema,
|
|
166
|
+
response: healthSchema
|
|
167
|
+
}, async ({ data }) => {
|
|
168
|
+
await initializeDatabase();
|
|
169
|
+
if (isInput(data)) {
|
|
170
|
+
return connectionManager.test(ensureInputDefaults(data as DbClientConnectionInput));
|
|
171
|
+
}
|
|
172
|
+
return connectionManager.test({ id: (data as { id: string }).id });
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
.http('db-client:health', {
|
|
176
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
177
|
+
response: healthSchema
|
|
178
|
+
}, async ({ data }) => {
|
|
179
|
+
return connectionManager.test({ id: data.id });
|
|
180
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* db-client WebSocket router.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
6
|
+
import { connectionsHandler } from './connections';
|
|
7
|
+
import { schemaHandler } from './schema';
|
|
8
|
+
import { queryHandler } from './query';
|
|
9
|
+
import { structureHandler } from './structure';
|
|
10
|
+
import { ioHandler } from './io';
|
|
11
|
+
|
|
12
|
+
export const dbClientRouter = createRouter()
|
|
13
|
+
.merge(connectionsHandler)
|
|
14
|
+
.merge(schemaHandler)
|
|
15
|
+
.merge(queryHandler)
|
|
16
|
+
.merge(structureHandler)
|
|
17
|
+
.merge(ioHandler);
|