@myrialabs/clopen 0.4.39 → 0.4.41
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/.env.example +1 -1
- package/backend/artifacts/detect.ts +15 -3
- package/backend/artifacts/generate.ts +2 -2
- package/backend/artifacts/index.ts +12 -4
- package/backend/artifacts/markers.ts +24 -1
- package/backend/artifacts/matrix.ts +47 -3
- package/backend/artifacts/sync.test.ts +3 -3
- package/backend/artifacts/sync.ts +8 -20
- package/backend/artifacts/types.ts +9 -3
- package/backend/auth/permissions.ts +29 -11
- package/backend/auth/session-cleanup.ts +10 -1
- package/backend/auth/tokens.ts +9 -1
- package/backend/chat/stream-manager.ts +42 -0
- package/backend/database/crypto/secret-columns.ts +5 -1
- package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
- package/backend/database/migrations/079_merge_commands_into_skills.ts +222 -0
- package/backend/database/migrations/080_create_file_shares.ts +111 -0
- package/backend/database/migrations/081_create_push_subscriptions.ts +32 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/file-share-queries.ts +163 -0
- package/backend/database/queries/index.ts +12 -4
- package/backend/database/queries/profile-queries.ts +5 -3
- package/backend/database/queries/push-subscription-queries.test.ts +90 -0
- package/backend/database/queries/push-subscription-queries.ts +99 -0
- package/backend/database/queries/skill-queries.ts +96 -18
- package/backend/database/queries/worktree-branch-queries.ts +331 -0
- package/backend/db-client/cloning.test.ts +161 -0
- package/backend/db-client/cloning.ts +672 -0
- package/backend/db-client/connection-url.test.ts +117 -0
- package/backend/db-client/connection-url.ts +126 -0
- package/backend/db-client/drivers/mongodb.ts +2 -14
- package/backend/db-client/drivers/mysql.ts +2 -8
- package/backend/db-client/drivers/postgres.ts +10 -13
- package/backend/db-client/drivers/redis.ts +2 -6
- package/backend/db-client/env/detect.ts +438 -0
- package/backend/db-client/env/diff.ts +94 -0
- package/backend/db-client/env/env.test.ts +574 -0
- package/backend/db-client/env/index.ts +15 -0
- package/backend/db-client/env/naming.ts +202 -0
- package/backend/db-client/env/service.ts +487 -0
- package/backend/db-client/integrations/index.ts +7 -0
- package/backend/db-client/integrations/links.ts +41 -10
- package/backend/db-client/providers/neon/adapter.ts +522 -0
- package/backend/db-client/providers/neon/api-types.ts +182 -0
- package/backend/db-client/providers/neon/client.ts +298 -0
- package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
- package/backend/db-client/providers/neon/endpoints.ts +314 -0
- package/backend/db-client/providers/neon/list.test.ts +181 -0
- package/backend/db-client/providers/supabase/adapter.ts +25 -2
- package/backend/db-client/providers/types.ts +57 -0
- package/backend/engine/adapters/claude/stream.ts +2 -1
- package/backend/engine/adapters/cline/stream.ts +3 -3
- package/backend/engine/adapters/codex/stream.ts +3 -3
- package/backend/engine/adapters/copilot/stream.ts +3 -3
- package/backend/engine/adapters/cursor/stream.ts +25 -3
- package/backend/engine/adapters/opencode/stream.ts +4 -6
- package/backend/engine/adapters/pi/stream.ts +2 -2
- package/backend/engine/adapters/qwen/stream.ts +2 -2
- package/backend/engine/artifact-sync.ts +26 -20
- package/backend/engine/docs/artifacts.md +81 -22
- package/backend/engine/docs/quick-reference.md +2 -1
- package/backend/engine/resolve-model.test.ts +108 -0
- package/backend/engine/resolve-model.ts +53 -10
- package/backend/engine/structured-helpers.ts +22 -0
- package/backend/env-files/discovery.ts +48 -0
- package/backend/env-files/edit.ts +213 -0
- package/backend/env-files/env-files.test.ts +515 -0
- package/backend/env-files/git.ts +27 -0
- package/backend/env-files/index.ts +44 -0
- package/backend/env-files/keys.ts +69 -0
- package/backend/env-files/parse.ts +157 -0
- package/backend/env-files/write.ts +229 -0
- package/backend/files/file-shares.ts +369 -0
- package/backend/files/path-browsing.ts +27 -21
- package/backend/http/files-shared.test.ts +505 -0
- package/backend/http/files-shared.ts +351 -0
- package/backend/http/push.ts +150 -0
- package/backend/index.ts +8 -0
- package/backend/instructions/index.ts +1 -1
- package/backend/instructions/sync.ts +30 -6
- package/backend/integrations/health.ts +18 -4
- package/backend/integrations/projections/types.ts +9 -2
- package/backend/integrations/registry/index.ts +3 -0
- package/backend/integrations/registry/providers/context7.ts +1 -0
- package/backend/integrations/registry/providers/firecrawl.ts +1 -0
- package/backend/integrations/registry/providers/github.ts +1 -0
- package/backend/integrations/registry/providers/neon.ts +55 -0
- package/backend/integrations/registry/providers/supabase.ts +1 -0
- package/backend/integrations/registry/providers/vercel.ts +1 -0
- package/backend/integrations/registry/types.ts +2 -0
- package/backend/memory/compose.ts +2 -2
- package/backend/memory/consolidate.ts +2 -2
- package/backend/memory/extract/episodic.ts +2 -2
- package/backend/memory/judge.ts +2 -2
- package/backend/profiles/service.ts +7 -6
- package/backend/push/sender.ts +172 -0
- package/backend/push/tags.ts +25 -0
- package/backend/push/vapid-keys.ts +77 -0
- package/backend/push/webpush-crypto.test.ts +200 -0
- package/backend/push/webpush-crypto.ts +231 -0
- package/backend/skills/detect.ts +28 -0
- package/backend/skills/index.ts +22 -4
- package/backend/skills/invoke.test.ts +114 -0
- package/backend/skills/invoke.ts +222 -0
- package/backend/skills/service.ts +194 -44
- package/backend/skills/spec.ts +35 -0
- package/backend/skills/sync.ts +85 -55
- package/backend/subagents/detect.ts +2 -3
- package/backend/subagents/sync.ts +15 -8
- package/backend/work/pull-requests.ts +2 -2
- package/backend/work/start-work.ts +4 -0
- package/backend/worktrees/branching/engine.ts +557 -0
- package/backend/worktrees/branching/index.ts +56 -0
- package/backend/worktrees/branching/naming.test.ts +118 -0
- package/backend/worktrees/branching/naming.ts +97 -0
- package/backend/worktrees/branching/projector.test.ts +229 -0
- package/backend/worktrees/branching/projector.ts +139 -0
- package/backend/worktrees/branching/providers/neon.ts +220 -0
- package/backend/worktrees/branching/registry.ts +27 -0
- package/backend/worktrees/branching/service.ts +620 -0
- package/backend/worktrees/branching/suggest.test.ts +154 -0
- package/backend/worktrees/branching/suggest.ts +202 -0
- package/backend/worktrees/branching/types.ts +88 -0
- package/backend/worktrees/index.ts +7 -0
- package/backend/worktrees/service.ts +48 -1
- package/backend/ws/chat/stream.ts +100 -8
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- package/backend/ws/files/index.ts +4 -0
- package/backend/ws/files/share.ts +95 -0
- package/backend/ws/git/branch-name.ts +2 -2
- package/backend/ws/git/commit-message.ts +2 -2
- package/backend/ws/index.ts +3 -4
- package/backend/ws/profiles/crud.ts +2 -3
- package/backend/ws/projects/index.ts +6 -1
- package/backend/ws/projects/info.ts +70 -6
- package/backend/ws/projects/overview.test.ts +108 -0
- package/backend/ws/projects/overview.ts +300 -0
- package/backend/ws/skills/catalog.ts +5 -1
- package/backend/ws/skills/crud.ts +128 -22
- package/backend/ws/skills/index.ts +4 -1
- package/backend/ws/system/device-info.ts +307 -76
- package/backend/ws/user/crud.ts +12 -3
- package/backend/ws/worktrees/branching.ts +212 -0
- package/backend/ws/worktrees/crud.ts +33 -7
- package/backend/ws/worktrees/index.ts +14 -0
- package/dist/assets/{index-C8Gi-b6U.js → index-CeNKKSDR.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DW89566K.js} +1 -1
- package/dist/assets/main-Cy5DXWPT.js +1207 -0
- package/dist/assets/main-DJKj_4By.css +1 -0
- package/dist/index.html +7 -2
- package/dist/sw.js +112 -0
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +31 -6
- package/shared/constants/notification-messages.ts +142 -0
- package/shared/constants/tool-icons.ts +6 -2
- package/shared/types/db-client/env.ts +192 -0
- package/shared/types/db-client/index.ts +1 -0
- package/shared/types/db-client/integrations.ts +30 -0
- package/shared/types/integrations.ts +9 -0
- package/shared/types/unified/stream.ts +7 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/backend/commands/detect.ts +0 -25
- package/backend/commands/index.ts +0 -9
- package/backend/commands/service.ts +0 -178
- package/backend/commands/store.ts +0 -45
- package/backend/commands/sync.ts +0 -94
- package/backend/database/queries/command-queries.ts +0 -82
- package/backend/ws/commands/crud.ts +0 -157
- package/backend/ws/commands/index.ts +0 -8
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a connection is called in a dotenv file.
|
|
3
|
+
*
|
|
4
|
+
* TWO SHAPES AND A PREFIX, and nothing else. An earlier version shipped a
|
|
5
|
+
* dropdown of frameworks — Laravel, Prisma, libpq, Vercel/Supabase, Neon — and
|
|
6
|
+
* every entry in it was the same assertion: *this* is what a project of that
|
|
7
|
+
* kind calls its database. Which is a guess about a project whose files are
|
|
8
|
+
* right there on disk.
|
|
9
|
+
*
|
|
10
|
+
* So the frameworks are gone and `detect.ts` answers instead. It reads the
|
|
11
|
+
* project's own `.env`, its `.env.example` and its ORM config, and hands back
|
|
12
|
+
* the ACTUAL key names with a role attached to each — which is what `keyMap`
|
|
13
|
+
* carries here. A project using `DB_USERNAME` gets `DB_USERNAME` written; one
|
|
14
|
+
* using `POSTGRES_URL` gets that; and the two generic shapes below are what a
|
|
15
|
+
* project with nothing in it is offered.
|
|
16
|
+
*
|
|
17
|
+
* What remains here is DRIVER vocabulary, not framework vocabulary: Mongo's
|
|
18
|
+
* variable has always been `MONGODB_URI` and not `MONGODB_URL`, and a Redis
|
|
19
|
+
* connection has no user to name. That is a fact about the driver, so it lives
|
|
20
|
+
* with the driver rather than in a list of frameworks.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type {
|
|
24
|
+
DbClientConnection,
|
|
25
|
+
DbDriver,
|
|
26
|
+
DbEnvRole,
|
|
27
|
+
DbEnvShape,
|
|
28
|
+
DbEnvShapeInfo,
|
|
29
|
+
DbEnvVar
|
|
30
|
+
} from '$shared/types/db-client';
|
|
31
|
+
import { DEFAULT_PORTS } from '../connection-url';
|
|
32
|
+
|
|
33
|
+
/** How each driver's own ecosystem spells things. */
|
|
34
|
+
const DRIVER_NAMING: Record<DbDriver, { prefix: string; urlSuffix: string }> = {
|
|
35
|
+
postgres: { prefix: 'DATABASE', urlSuffix: '_URL' },
|
|
36
|
+
mysql: { prefix: 'DATABASE', urlSuffix: '_URL' },
|
|
37
|
+
mssql: { prefix: 'DATABASE', urlSuffix: '_URL' },
|
|
38
|
+
sqlite: { prefix: 'DATABASE', urlSuffix: '_URL' },
|
|
39
|
+
// Every Mongo driver looks for `MONGODB_URI`. `MONGODB_URL` is not a variant
|
|
40
|
+
// of it, it is a variable nothing reads.
|
|
41
|
+
mongodb: { prefix: 'MONGODB', urlSuffix: '_URI' },
|
|
42
|
+
redis: { prefix: 'REDIS', urlSuffix: '_URL' }
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** `DATABASE_HOST` is nobody's convention; `DB_HOST` is everybody's. */
|
|
46
|
+
const SPLIT_PREFIX = 'DB';
|
|
47
|
+
|
|
48
|
+
/** The suffix a second endpoint's variable carries, when the provider has one. */
|
|
49
|
+
export const DEFAULT_ALT_SUFFIX = '_UNPOOLED';
|
|
50
|
+
|
|
51
|
+
export interface EnvNaming {
|
|
52
|
+
shape: DbEnvShape;
|
|
53
|
+
prefix: string;
|
|
54
|
+
/** Exact names, overriding what the prefix would produce. */
|
|
55
|
+
keyMap?: Partial<Record<DbEnvRole, string>>;
|
|
56
|
+
/** From the provider, for the second endpoint — `_UNPOOLED`, `_NON_POOLING`. */
|
|
57
|
+
altSuffix?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function defaultPrefixFor(driver: DbDriver, shape: DbEnvShape): string {
|
|
61
|
+
return shape === 'split' ? SPLIT_PREFIX : DRIVER_NAMING[driver].prefix;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The variable a role lands in, unless the project already calls it something. */
|
|
65
|
+
export function keyFor(role: DbEnvRole, driver: DbDriver, naming: EnvNaming): string {
|
|
66
|
+
const named = naming.keyMap?.[role];
|
|
67
|
+
if (named) return named;
|
|
68
|
+
|
|
69
|
+
const prefix = naming.prefix;
|
|
70
|
+
switch (role) {
|
|
71
|
+
case 'url':
|
|
72
|
+
return `${prefix}${DRIVER_NAMING[driver].urlSuffix}`;
|
|
73
|
+
case 'altUrl':
|
|
74
|
+
return `${prefix}${DRIVER_NAMING[driver].urlSuffix}${naming.altSuffix ?? DEFAULT_ALT_SUFFIX}`;
|
|
75
|
+
case 'host':
|
|
76
|
+
return `${prefix}_HOST`;
|
|
77
|
+
case 'port':
|
|
78
|
+
return `${prefix}_PORT`;
|
|
79
|
+
case 'user':
|
|
80
|
+
return `${prefix}_USER`;
|
|
81
|
+
case 'password':
|
|
82
|
+
return `${prefix}_PASSWORD`;
|
|
83
|
+
case 'database':
|
|
84
|
+
// A SQLite connection is a file path, so the field is not a name.
|
|
85
|
+
return driver === 'sqlite' ? `${prefix}_PATH` : `${prefix}_NAME`;
|
|
86
|
+
case 'sslMode':
|
|
87
|
+
return `${prefix}_SSLMODE`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface EnvRenderInput {
|
|
92
|
+
connection: DbClientConnection;
|
|
93
|
+
naming: EnvNaming;
|
|
94
|
+
/** The URL for the connection exactly as DB Client reaches it. */
|
|
95
|
+
url: string;
|
|
96
|
+
/** The provider's direct/unpooled counterpart, when one was resolved. */
|
|
97
|
+
alternateUrl: string | null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Render the variables.
|
|
102
|
+
*
|
|
103
|
+
* An empty value is DROPPED rather than written, because `DB_HOST=` reads as a
|
|
104
|
+
* configured empty host and fails later than not being there at all. The
|
|
105
|
+
* password is the exception: a local database with no password is a real
|
|
106
|
+
* configuration, and a project whose `.env` ships `DB_PASSWORD=` empty needs
|
|
107
|
+
* that line to keep existing.
|
|
108
|
+
*/
|
|
109
|
+
export function renderEnvVars(input: EnvRenderInput): DbEnvVar[] {
|
|
110
|
+
const { connection, naming } = input;
|
|
111
|
+
const driver = connection.driver;
|
|
112
|
+
const vars: DbEnvVar[] = [];
|
|
113
|
+
|
|
114
|
+
const push = (
|
|
115
|
+
role: DbEnvRole,
|
|
116
|
+
value: string | null | undefined,
|
|
117
|
+
options?: { secret?: boolean; keepEmpty?: boolean; note?: string }
|
|
118
|
+
): void => {
|
|
119
|
+
const text = value ?? '';
|
|
120
|
+
if (!text && !options?.keepEmpty) return;
|
|
121
|
+
vars.push({
|
|
122
|
+
key: keyFor(role, driver, naming),
|
|
123
|
+
value: text,
|
|
124
|
+
isSecret: Boolean(options?.secret),
|
|
125
|
+
note: options?.note ?? null
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
if (naming.shape === 'url') {
|
|
130
|
+
push('url', input.url, { secret: Boolean(connection.password) });
|
|
131
|
+
if (input.alternateUrl) {
|
|
132
|
+
push('altUrl', input.alternateUrl, {
|
|
133
|
+
secret: Boolean(connection.password),
|
|
134
|
+
note: 'The direct endpoint, which migrations need.'
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return vars;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// SQLite has no endpoint, so the split shape is one variable: the path.
|
|
141
|
+
// Writing a host and a port for it would be inventing an endpoint.
|
|
142
|
+
if (driver === 'sqlite') {
|
|
143
|
+
push('database', connection.database);
|
|
144
|
+
return vars;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
push('host', connection.host);
|
|
148
|
+
const port = connection.port ?? DEFAULT_PORTS[driver];
|
|
149
|
+
push('port', port ? String(port) : '');
|
|
150
|
+
// Redis's URL has only ever carried a password, so there is no user to name.
|
|
151
|
+
if (driver !== 'redis') push('user', connection.username);
|
|
152
|
+
push('password', connection.password, { secret: true, keepEmpty: true });
|
|
153
|
+
push('database', connection.database);
|
|
154
|
+
if (connection.sslMode && connection.sslMode !== 'disable') {
|
|
155
|
+
push('sslMode', connection.sslMode);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return vars;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Both shapes, with the keys each would actually write for this connection. */
|
|
162
|
+
export function shapeInfos(input: {
|
|
163
|
+
connection: DbClientConnection;
|
|
164
|
+
naming: EnvNaming;
|
|
165
|
+
url: string;
|
|
166
|
+
hasAlternate: boolean;
|
|
167
|
+
}): DbEnvShapeInfo[] {
|
|
168
|
+
const describe = (shape: DbEnvShape, label: string, description: string): DbEnvShapeInfo => {
|
|
169
|
+
// The keys shown are the keys that WOULD be written, built by rendering —
|
|
170
|
+
// not a second hard-coded list that can drift from the first.
|
|
171
|
+
const naming: EnvNaming = {
|
|
172
|
+
...input.naming,
|
|
173
|
+
shape,
|
|
174
|
+
prefix:
|
|
175
|
+
input.naming.shape === shape
|
|
176
|
+
? input.naming.prefix
|
|
177
|
+
: defaultPrefixFor(input.connection.driver, shape),
|
|
178
|
+
// A key map belongs to the shape it was detected in; carrying a split
|
|
179
|
+
// project's `DB_HOST` into the URL preview would show a name that
|
|
180
|
+
// shape never writes.
|
|
181
|
+
keyMap: input.naming.shape === shape ? input.naming.keyMap : undefined
|
|
182
|
+
};
|
|
183
|
+
return {
|
|
184
|
+
id: shape,
|
|
185
|
+
label,
|
|
186
|
+
description,
|
|
187
|
+
sampleKeys: renderEnvVars({
|
|
188
|
+
connection: input.connection,
|
|
189
|
+
naming,
|
|
190
|
+
url: input.url,
|
|
191
|
+
// Presence only: the sample has to include the second variable when
|
|
192
|
+
// there is a second endpoint to put in it.
|
|
193
|
+
alternateUrl: shape === 'url' && input.hasAlternate ? 'pending' : null
|
|
194
|
+
}).map((entry) => entry.key)
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
return [
|
|
199
|
+
describe('url', 'Single URL', 'One variable holding the whole connection string.'),
|
|
200
|
+
describe('split', 'Separate variables', 'Host, port, user, password and database, one each.')
|
|
201
|
+
];
|
|
202
|
+
}
|
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The connection as environment variables — detect, preview, apply, undo.
|
|
3
|
+
*
|
|
4
|
+
* What this closes: DB Client could reach a database that the project sitting
|
|
5
|
+
* next to it could not, and the step between the two was a user retyping a
|
|
6
|
+
* host, a port, a password and an SSL mode into a dotenv file whose name and
|
|
7
|
+
* whose variable names both varied by framework.
|
|
8
|
+
*
|
|
9
|
+
* Three rules hold the whole thing together.
|
|
10
|
+
*
|
|
11
|
+
* **The project's names win.** `detect.ts` reads what the project already uses
|
|
12
|
+
* and that is what is offered; a preset is what a project with nothing in it
|
|
13
|
+
* gets asked. Nothing is guessed while the answer is on disk.
|
|
14
|
+
*
|
|
15
|
+
* **Nothing is written that was not previewed.** `plan()` and `apply()` share
|
|
16
|
+
* one `planEnvWrite`, so the diff the user approved is the write that happens.
|
|
17
|
+
*
|
|
18
|
+
* **A tracked file is refused.** Writing a live password into a file git tracks
|
|
19
|
+
* commits one, so it takes an explicit override — and the override exists
|
|
20
|
+
* because this is the user's own project and their own database, unlike the
|
|
21
|
+
* automatic write worktree branching does.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
integrationAccountQueries,
|
|
26
|
+
integrationDbLinkQueries
|
|
27
|
+
} from '$backend/database/queries';
|
|
28
|
+
import {
|
|
29
|
+
clearEnvVars,
|
|
30
|
+
formatEnvLine,
|
|
31
|
+
isSafeEnvFileName,
|
|
32
|
+
isValidEnvKey,
|
|
33
|
+
planEnvWrite,
|
|
34
|
+
writeEnvVars
|
|
35
|
+
} from '$backend/env-files';
|
|
36
|
+
import type {
|
|
37
|
+
DbClientConnection,
|
|
38
|
+
DbEnvApplyResult,
|
|
39
|
+
DbEnvDetection,
|
|
40
|
+
DbEnvPlan,
|
|
41
|
+
DbEnvRequest,
|
|
42
|
+
DbEnvShape,
|
|
43
|
+
DbEnvState,
|
|
44
|
+
DbEnvVar
|
|
45
|
+
} from '$shared/types/db-client';
|
|
46
|
+
import { buildConnectionUrl } from '../connection-url';
|
|
47
|
+
import { dbLinks } from '../integrations/links';
|
|
48
|
+
import { getDbProvider } from '../providers/registry';
|
|
49
|
+
import type { DbProviderEnvHints } from '../providers/types';
|
|
50
|
+
import { detectEnvUsage } from './detect';
|
|
51
|
+
import { diffLines } from './diff';
|
|
52
|
+
import { defaultPrefixFor, renderEnvVars, shapeInfos, type EnvNaming } from './naming';
|
|
53
|
+
import { debug } from '$shared/utils/logger';
|
|
54
|
+
|
|
55
|
+
/** The project a write would land in. Null when none is in scope. */
|
|
56
|
+
export interface EnvScope {
|
|
57
|
+
projectId: string;
|
|
58
|
+
projectName: string;
|
|
59
|
+
root: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const EMPTY_DETECTION: DbEnvDetection = {
|
|
63
|
+
projectId: null,
|
|
64
|
+
projectName: null,
|
|
65
|
+
files: [],
|
|
66
|
+
existing: [],
|
|
67
|
+
sources: []
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** The provider behind a derived connection, and what its docs call things. */
|
|
71
|
+
function providerFor(connectionId: string): {
|
|
72
|
+
hints: DbProviderEnvHints | null;
|
|
73
|
+
mode: string | null;
|
|
74
|
+
remoteRef: string | null;
|
|
75
|
+
accountId: string | null;
|
|
76
|
+
provider: string | null;
|
|
77
|
+
modeLabels: Map<string, string>;
|
|
78
|
+
} {
|
|
79
|
+
const linked = dbLinks.linkForConnection(connectionId);
|
|
80
|
+
if (!linked) {
|
|
81
|
+
return { hints: null, mode: null, remoteRef: null, accountId: null, provider: null, modeLabels: new Map() };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const adapter = getDbProvider(linked.account.provider);
|
|
85
|
+
return {
|
|
86
|
+
hints: adapter?.envHints?.() ?? null,
|
|
87
|
+
mode: linked.link.mode,
|
|
88
|
+
remoteRef: linked.link.remote_ref,
|
|
89
|
+
accountId: linked.account.id,
|
|
90
|
+
provider: linked.account.provider,
|
|
91
|
+
modeLabels: new Map((adapter?.info().modes ?? []).map((entry) => [entry.id, entry.label]))
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Whether a second, non-pooled URL is worth offering.
|
|
97
|
+
*
|
|
98
|
+
* False when the connection is ALREADY on the direct endpoint, because a
|
|
99
|
+
* `DIRECT_URL` equal to `DATABASE_URL` looks like configuration and changes
|
|
100
|
+
* nothing — which is worse than not offering it.
|
|
101
|
+
*/
|
|
102
|
+
function alternateAvailable(info: ReturnType<typeof providerFor>): boolean {
|
|
103
|
+
const { hints, mode } = info;
|
|
104
|
+
if (!hints?.directMode || !mode) return false;
|
|
105
|
+
return (hints.pooledModes ?? []).includes(mode);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Ask the provider where its direct endpoint answers.
|
|
110
|
+
*
|
|
111
|
+
* Fail-soft: a provider outage costs the second variable and nothing else, and
|
|
112
|
+
* the caller reports the reason alongside the URL it did get. Throwing here
|
|
113
|
+
* would lose a preview the user can act on over a variable they may not need.
|
|
114
|
+
*/
|
|
115
|
+
async function resolveAlternateUrl(
|
|
116
|
+
connection: DbClientConnection,
|
|
117
|
+
info: ReturnType<typeof providerFor>
|
|
118
|
+
): Promise<{ url: string | null; error: string | null }> {
|
|
119
|
+
if (!info.accountId || !info.provider || !info.remoteRef || !info.hints?.directMode) {
|
|
120
|
+
return { url: null, error: null };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const account = integrationAccountQueries.getById(info.accountId);
|
|
124
|
+
const adapter = getDbProvider(info.provider);
|
|
125
|
+
if (!account || !adapter) return { url: null, error: null };
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
const { secrets, ...endpoint } = await adapter.resolveEndpoint(
|
|
129
|
+
{ accountId: account.id, credentials: integrationAccountQueries.credentialsOf(account) },
|
|
130
|
+
{ remoteRef: info.remoteRef, mode: info.hints.directMode }
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const linked = dbLinks.linkForConnection(connection.id);
|
|
134
|
+
// Three places a password can come from, most authoritative first: the one
|
|
135
|
+
// the provider just handed back, the one stored on the link, and the one
|
|
136
|
+
// on the row. They agree in the ordinary case; when they do not, the fresh
|
|
137
|
+
// one is the one that works.
|
|
138
|
+
const password =
|
|
139
|
+
secrets?.password ??
|
|
140
|
+
(linked ? integrationDbLinkQueries.secretsOf(linked.link).password : null) ??
|
|
141
|
+
connection.password;
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
url: buildConnectionUrl({
|
|
145
|
+
...connection,
|
|
146
|
+
host: endpoint.host,
|
|
147
|
+
port: endpoint.port,
|
|
148
|
+
username: endpoint.username,
|
|
149
|
+
database: endpoint.database,
|
|
150
|
+
sslMode: endpoint.sslMode,
|
|
151
|
+
password: password ?? null
|
|
152
|
+
}),
|
|
153
|
+
error: null
|
|
154
|
+
};
|
|
155
|
+
} catch (error) {
|
|
156
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
157
|
+
debug.warn('db-client', `Could not resolve the direct endpoint for ${connection.id}: ${message}`);
|
|
158
|
+
return { url: null, error: message };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** One sentence each, in the order a reader needs them. */
|
|
163
|
+
function warningsFor(input: {
|
|
164
|
+
connection: DbClientConnection;
|
|
165
|
+
info: ReturnType<typeof providerFor>;
|
|
166
|
+
includesAlternate: boolean;
|
|
167
|
+
alternateError: string | null;
|
|
168
|
+
}): string[] {
|
|
169
|
+
const { connection, info } = input;
|
|
170
|
+
const warnings: string[] = [];
|
|
171
|
+
|
|
172
|
+
if (connection.ssh?.enabled) {
|
|
173
|
+
warnings.push(
|
|
174
|
+
'Clopen reaches this database through an SSH tunnel it opens itself, so your app needs its own tunnel before this URL will connect.'
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const host = (connection.host ?? '').toLowerCase();
|
|
179
|
+
if (host === 'localhost' || host === '127.0.0.1' || host === '::1') {
|
|
180
|
+
warnings.push(
|
|
181
|
+
'The host is localhost, so anything running in a container needs a host name that resolves from inside it.'
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (connection.driver === 'sqlite') {
|
|
186
|
+
warnings.push('SQLite has no endpoint, so the variable holds the path to the database file.');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const managed = dbLinks.managedBy(connection.id);
|
|
190
|
+
if (managed) {
|
|
191
|
+
warnings.push(
|
|
192
|
+
`This connection is derived from the ${managed.providerName} account, so refreshing the link can change these values.`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (input.includesAlternate && info.hints?.directNotice) {
|
|
197
|
+
warnings.push(info.hints.directNotice);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (input.alternateError) {
|
|
201
|
+
warnings.push(
|
|
202
|
+
`Could not reach the provider for the direct endpoint, so only the pooled URL is shown — ${input.alternateError}`
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return warnings;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Uppercase, underscore-safe, and refused rather than mangled. */
|
|
210
|
+
function normalizePrefix(raw: string, fallback: string): string {
|
|
211
|
+
const trimmed = (raw ?? '').trim().toUpperCase().replace(/_+$/, '');
|
|
212
|
+
if (!trimmed) return fallback;
|
|
213
|
+
if (!isValidEnvKey(trimmed)) {
|
|
214
|
+
throw new Error(`"${raw}" is not a valid variable prefix — use letters, digits and underscores.`);
|
|
215
|
+
}
|
|
216
|
+
return trimmed;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The file a write should land in, given what detection found. */
|
|
220
|
+
function suggestFileName(detection: DbEnvDetection, preferred: string[]): string | null {
|
|
221
|
+
const writable = detection.files.filter((file) => !file.isTemplate);
|
|
222
|
+
|
|
223
|
+
for (const name of preferred) {
|
|
224
|
+
if (writable.some((file) => file.name === name)) return name;
|
|
225
|
+
}
|
|
226
|
+
// `files` is already ordered most-precedent first, so the first writable
|
|
227
|
+
// entry is the one a framework would actually read.
|
|
228
|
+
if (writable.length > 0) return writable[0].name;
|
|
229
|
+
// Nothing exists yet. `.env` is the one name every loader reads, and a file
|
|
230
|
+
// that does not exist cannot be tracked, so creating it is safe.
|
|
231
|
+
return detection.projectId ? '.env' : null;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The connection as the project should reach it, with the browsed database. */
|
|
235
|
+
function targetOf(connection: DbClientConnection, database?: string): DbClientConnection {
|
|
236
|
+
const named = (database ?? '').trim();
|
|
237
|
+
if (!named || named === connection.database) return connection;
|
|
238
|
+
// A connection with no database of its own browses one, and it is THAT one
|
|
239
|
+
// the project needs. Using the connection's empty field would write a string
|
|
240
|
+
// pointing at the server with no database on the end of it.
|
|
241
|
+
return { ...connection, database: named };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function namingFor(
|
|
245
|
+
connection: DbClientConnection,
|
|
246
|
+
info: ReturnType<typeof providerFor>,
|
|
247
|
+
input: { shape: DbEnvShape; prefix?: string; keyMap?: DbEnvRequest['keyMap'] }
|
|
248
|
+
): EnvNaming {
|
|
249
|
+
return {
|
|
250
|
+
shape: input.shape,
|
|
251
|
+
prefix: normalizePrefix(
|
|
252
|
+
input.prefix ?? '',
|
|
253
|
+
defaultPrefixFor(connection.driver, input.shape)
|
|
254
|
+
),
|
|
255
|
+
keyMap: input.keyMap,
|
|
256
|
+
altSuffix: info.hints?.altKeySuffix
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export const dbClientEnv = {
|
|
261
|
+
/** Everything the panel needs to open on the right names and file. */
|
|
262
|
+
async state(
|
|
263
|
+
connection: DbClientConnection,
|
|
264
|
+
scope: EnvScope | null,
|
|
265
|
+
database?: string
|
|
266
|
+
): Promise<DbEnvState> {
|
|
267
|
+
const target = targetOf(connection, database);
|
|
268
|
+
const info = providerFor(connection.id);
|
|
269
|
+
const hasAlternate = alternateAvailable(info);
|
|
270
|
+
|
|
271
|
+
const detection = scope
|
|
272
|
+
? await detectEnvUsage({
|
|
273
|
+
root: scope.root,
|
|
274
|
+
projectId: scope.projectId,
|
|
275
|
+
projectName: scope.projectName,
|
|
276
|
+
driver: target.driver
|
|
277
|
+
})
|
|
278
|
+
: EMPTY_DETECTION;
|
|
279
|
+
|
|
280
|
+
// Whatever the project already uses wins outright — that is the answer
|
|
281
|
+
// this feature exists to find, and a shape picked over it would be a
|
|
282
|
+
// guess standing in front of a fact.
|
|
283
|
+
const best = detection.existing[0] ?? null;
|
|
284
|
+
const shape: DbEnvShape = best?.shape ?? 'url';
|
|
285
|
+
const prefix =
|
|
286
|
+
best?.prefix ??
|
|
287
|
+
info.hints?.urlPrefix ??
|
|
288
|
+
defaultPrefixFor(target.driver, shape);
|
|
289
|
+
|
|
290
|
+
// The file the detected group actually lives in, so an apply updates the
|
|
291
|
+
// copy that wins at runtime rather than adding a second one elsewhere.
|
|
292
|
+
const fileName = suggestFileName(detection, best?.files ?? []);
|
|
293
|
+
const naming = namingFor(target, info, { shape, prefix, keyMap: best?.keyMap });
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
connectionId: connection.id,
|
|
297
|
+
connectionName: connection.name,
|
|
298
|
+
driver: target.driver,
|
|
299
|
+
database: target.database,
|
|
300
|
+
shapes: shapeInfos({
|
|
301
|
+
connection: target,
|
|
302
|
+
naming,
|
|
303
|
+
url: buildConnectionUrl(target),
|
|
304
|
+
hasAlternate
|
|
305
|
+
}),
|
|
306
|
+
detection,
|
|
307
|
+
suggestion: {
|
|
308
|
+
shape,
|
|
309
|
+
prefix: naming.prefix,
|
|
310
|
+
fileName,
|
|
311
|
+
keyMap: best?.keyMap ?? null
|
|
312
|
+
},
|
|
313
|
+
hasAlternate,
|
|
314
|
+
alternateLabel: hasAlternate
|
|
315
|
+
? (info.modeLabels.get(info.hints?.directMode ?? '') ?? 'Direct')
|
|
316
|
+
: null,
|
|
317
|
+
warnings: warningsFor({
|
|
318
|
+
connection: target,
|
|
319
|
+
info,
|
|
320
|
+
includesAlternate: false,
|
|
321
|
+
alternateError: null
|
|
322
|
+
})
|
|
323
|
+
};
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
/** What applying would do, without touching the file. */
|
|
327
|
+
async plan(
|
|
328
|
+
connection: DbClientConnection,
|
|
329
|
+
scope: EnvScope | null,
|
|
330
|
+
request: DbEnvRequest
|
|
331
|
+
): Promise<DbEnvPlan> {
|
|
332
|
+
if (request.shape !== 'url' && request.shape !== 'split') {
|
|
333
|
+
throw new Error(`"${request.shape}" is not a shape DB Client can write.`);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const target = targetOf(connection, request.database);
|
|
337
|
+
const info = providerFor(connection.id);
|
|
338
|
+
const naming = namingFor(target, info, request);
|
|
339
|
+
|
|
340
|
+
const wantsAlternate = Boolean(request.includeAlternate) && alternateAvailable(info);
|
|
341
|
+
const alternate = wantsAlternate
|
|
342
|
+
? await resolveAlternateUrl(target, info)
|
|
343
|
+
: { url: null, error: null };
|
|
344
|
+
|
|
345
|
+
const vars = renderEnvVars({
|
|
346
|
+
connection: target,
|
|
347
|
+
naming,
|
|
348
|
+
url: buildConnectionUrl(target),
|
|
349
|
+
alternateUrl: alternate.url
|
|
350
|
+
});
|
|
351
|
+
if (vars.length === 0) {
|
|
352
|
+
throw new Error('This connection has nothing to write — give it a host and a database first.');
|
|
353
|
+
}
|
|
354
|
+
for (const entry of vars) {
|
|
355
|
+
if (!isValidEnvKey(entry.key)) {
|
|
356
|
+
throw new Error(`"${entry.key}" is not a valid environment variable name.`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const text = `${vars.map((entry) => formatEnvLine(entry.key, entry.value)).join('\n')}\n`;
|
|
361
|
+
const warnings = warningsFor({
|
|
362
|
+
connection: target,
|
|
363
|
+
info,
|
|
364
|
+
includesAlternate: Boolean(alternate.url),
|
|
365
|
+
alternateError: alternate.error
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
const fileName = (request.fileName ?? '').trim();
|
|
369
|
+
if (!scope || !fileName) {
|
|
370
|
+
return {
|
|
371
|
+
vars,
|
|
372
|
+
text,
|
|
373
|
+
fileName: null,
|
|
374
|
+
fileExists: false,
|
|
375
|
+
fileTracked: false,
|
|
376
|
+
updated: [],
|
|
377
|
+
appended: [],
|
|
378
|
+
unchanged: [],
|
|
379
|
+
diff: [],
|
|
380
|
+
warnings
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
if (!isSafeEnvFileName(fileName)) {
|
|
384
|
+
throw new Error(`"${fileName}" is not a dotenv file in the project root.`);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const plan = await planEnvWrite({
|
|
388
|
+
root: scope.root,
|
|
389
|
+
fileName,
|
|
390
|
+
vars: varsToRecord(vars)
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
vars,
|
|
395
|
+
text,
|
|
396
|
+
fileName,
|
|
397
|
+
fileExists: plan.exists,
|
|
398
|
+
fileTracked: plan.tracked,
|
|
399
|
+
updated: plan.updated,
|
|
400
|
+
appended: plan.appended,
|
|
401
|
+
unchanged: plan.unchanged,
|
|
402
|
+
diff: diffLines(plan.current, plan.next),
|
|
403
|
+
warnings
|
|
404
|
+
};
|
|
405
|
+
},
|
|
406
|
+
|
|
407
|
+
/** Write it. The plan the panel showed is the plan that runs. */
|
|
408
|
+
async apply(
|
|
409
|
+
connection: DbClientConnection,
|
|
410
|
+
scope: EnvScope,
|
|
411
|
+
request: DbEnvRequest
|
|
412
|
+
): Promise<DbEnvApplyResult> {
|
|
413
|
+
const plan = await this.plan(connection, scope, request);
|
|
414
|
+
if (!plan.fileName) {
|
|
415
|
+
throw new Error('Pick a dotenv file before applying.');
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const result = await writeEnvVars({
|
|
419
|
+
root: scope.root,
|
|
420
|
+
fileName: plan.fileName,
|
|
421
|
+
vars: varsToRecord(plan.vars),
|
|
422
|
+
allowTracked: request.allowTracked
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
return {
|
|
426
|
+
status: result.status,
|
|
427
|
+
detail: result.detail,
|
|
428
|
+
fileName: result.fileName,
|
|
429
|
+
updated: result.updated,
|
|
430
|
+
appended: result.appended
|
|
431
|
+
};
|
|
432
|
+
},
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Take it back out.
|
|
436
|
+
*
|
|
437
|
+
* Rendered again rather than remembered, so an undo removes exactly what the
|
|
438
|
+
* matching apply wrote. A value replaced in place is restored from the
|
|
439
|
+
* comment left beside it; a key that was appended is removed ONLY while it
|
|
440
|
+
* still holds what was written, so a value the user has edited since is
|
|
441
|
+
* theirs and stays.
|
|
442
|
+
*/
|
|
443
|
+
async remove(
|
|
444
|
+
connection: DbClientConnection,
|
|
445
|
+
scope: EnvScope,
|
|
446
|
+
request: DbEnvRequest
|
|
447
|
+
): Promise<DbEnvApplyResult> {
|
|
448
|
+
const fileName = (request.fileName ?? '').trim();
|
|
449
|
+
if (!fileName || !isSafeEnvFileName(fileName)) {
|
|
450
|
+
throw new Error(`"${fileName}" is not a dotenv file in the project root.`);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const target = targetOf(connection, request.database);
|
|
454
|
+
const info = providerFor(connection.id);
|
|
455
|
+
const alternate = alternateAvailable(info)
|
|
456
|
+
? await resolveAlternateUrl(target, info).then((result) => result.url)
|
|
457
|
+
: null;
|
|
458
|
+
|
|
459
|
+
const vars = renderEnvVars({
|
|
460
|
+
connection: target,
|
|
461
|
+
naming: namingFor(target, info, request),
|
|
462
|
+
url: buildConnectionUrl(target),
|
|
463
|
+
alternateUrl: alternate
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
const { changed } = await clearEnvVars({
|
|
467
|
+
root: scope.root,
|
|
468
|
+
fileName,
|
|
469
|
+
keys: vars.map((entry) => entry.key),
|
|
470
|
+
values: varsToRecord(vars)
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
return {
|
|
474
|
+
status: changed ? 'written' : 'unchanged',
|
|
475
|
+
detail: null,
|
|
476
|
+
fileName,
|
|
477
|
+
updated: [],
|
|
478
|
+
appended: []
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
function varsToRecord(vars: DbEnvVar[]): Record<string, string> {
|
|
484
|
+
const record: Record<string, string> = {};
|
|
485
|
+
for (const entry of vars) record[entry.key] = entry.value;
|
|
486
|
+
return record;
|
|
487
|
+
}
|