@myrialabs/clopen 0.4.39 → 0.4.40
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/auth/permissions.ts +23 -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/index.ts +7 -0
- package/backend/database/queries/index.ts +6 -0
- 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/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/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/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/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- 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/system/device-info.ts +307 -76
- 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-D4igjF6e.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DONNwiXm.js} +1 -1
- package/dist/assets/main-BrfXH1bL.js +1205 -0
- package/dist/assets/main-CT1PygsW.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- 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/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Working out where a Neon branch answers.
|
|
3
|
+
*
|
|
4
|
+
* Shared by both surfaces Neon serves: DB Client resolves the DEFAULT branch of
|
|
5
|
+
* a linked project, and the worktree manager resolves the branch it just cut.
|
|
6
|
+
* Same question, same three facts about how Neon answers it, so it lives in one
|
|
7
|
+
* place rather than being solved twice with two sets of bugs.
|
|
8
|
+
*
|
|
9
|
+
* The three facts:
|
|
10
|
+
*
|
|
11
|
+
* 1. THERE ARE TWO DIFFERENT CONNECTION SHAPES, and confusing them is what
|
|
12
|
+
* shipped first. `GET /connection_uri` answers with `ConnectionURIResponse`
|
|
13
|
+
* — `{ uri }`, one bare string and nothing else. The `connection_uris[]`
|
|
14
|
+
* array on a CREATE response is `ConnectionDetails` — `{ connection_uri,
|
|
15
|
+
* connection_parameters }`, with the host, role, database and password as
|
|
16
|
+
* discrete fields. Reading the first as though it were the second leaves
|
|
17
|
+
* every field undefined, which is exactly how linking a project failed with
|
|
18
|
+
* "Neon did not report a host".
|
|
19
|
+
* 2. `GET /connection_uri` REQUIRES `database_name` and `role_name`. There is
|
|
20
|
+
* no "give me the obvious one" form, so both have to be discovered first.
|
|
21
|
+
* 3. A create response often — but NOT always — carries `connection_uris`
|
|
22
|
+
* already, complete with the password and both hosts. Using it when it is
|
|
23
|
+
* there turns three requests into zero; requiring it would break on any
|
|
24
|
+
* project with more than one role or database.
|
|
25
|
+
* 4. `connection_parameters` carries `host` AND `pooler_host` together, so
|
|
26
|
+
* pooled versus direct is a choice of which field to read rather than a
|
|
27
|
+
* second round trip. The bare `{ uri }` form carries only ONE host — the one
|
|
28
|
+
* the `pooled` query parameter asked for — so it is authoritative for the
|
|
29
|
+
* mode it was requested in and useless for the other.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import type { BranchConnection } from '$shared/types/worktree-branching';
|
|
33
|
+
import {
|
|
34
|
+
neonRequest,
|
|
35
|
+
NeonError,
|
|
36
|
+
type NeonCredentials
|
|
37
|
+
} from './client';
|
|
38
|
+
import type {
|
|
39
|
+
NeonApiBranch,
|
|
40
|
+
NeonApiBranchesResponse,
|
|
41
|
+
NeonApiConnectionUri,
|
|
42
|
+
NeonApiConnectionUriResponse,
|
|
43
|
+
NeonApiDatabase,
|
|
44
|
+
NeonApiRole,
|
|
45
|
+
NeonApiRolePassword
|
|
46
|
+
} from './api-types';
|
|
47
|
+
|
|
48
|
+
/** Neon answers on 5432 for both the direct host and the pooled one. */
|
|
49
|
+
export const NEON_PORT = 5432;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* `require`, not `verify-full`.
|
|
53
|
+
*
|
|
54
|
+
* Neon's certificates come from a public CA, so verification could work — but
|
|
55
|
+
* it depends on the root store the process happens to have, and a TLS failure
|
|
56
|
+
* here surfaces as an unexplained connection refusal rather than as a
|
|
57
|
+
* certificate problem. `require` is the honest setting for a client that does
|
|
58
|
+
* not ship its own trust store: encrypted, not pinned.
|
|
59
|
+
*/
|
|
60
|
+
export const NEON_SSL_MODE = 'require' as const;
|
|
61
|
+
|
|
62
|
+
export async function listBranches(
|
|
63
|
+
credentials: NeonCredentials,
|
|
64
|
+
projectId: string
|
|
65
|
+
): Promise<NeonApiBranch[]> {
|
|
66
|
+
const response = await neonRequest<NeonApiBranchesResponse>(
|
|
67
|
+
credentials,
|
|
68
|
+
`/projects/${encodeURIComponent(projectId)}/branches`
|
|
69
|
+
);
|
|
70
|
+
return response?.branches ?? [];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The branch new branches are cut from, and the one DB Client links.
|
|
75
|
+
*
|
|
76
|
+
* Neon has no `default_branch_id` on a project — the flag lives on the branch —
|
|
77
|
+
* so this is a list plus a find rather than a read. Falling back to the first
|
|
78
|
+
* branch is deliberate: a project always has at least one, and refusing to act
|
|
79
|
+
* because no row carried the flag would be a failure invented by the client.
|
|
80
|
+
*/
|
|
81
|
+
export async function defaultBranchOf(
|
|
82
|
+
credentials: NeonCredentials,
|
|
83
|
+
projectId: string
|
|
84
|
+
): Promise<NeonApiBranch> {
|
|
85
|
+
const branches = await listBranches(credentials, projectId);
|
|
86
|
+
const preferred = branches.find((branch) => branch.default === true) ?? branches[0];
|
|
87
|
+
if (!preferred) {
|
|
88
|
+
throw new NeonError(
|
|
89
|
+
`Neon reports no branches for project ${projectId}, so there is nothing to connect to.`,
|
|
90
|
+
404,
|
|
91
|
+
'config'
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
return preferred;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* A role to connect as.
|
|
99
|
+
*
|
|
100
|
+
* PROTECTED roles are skipped. Neon marks its system roles protected, and they
|
|
101
|
+
* are not the owner a migration needs — connecting as one produces permission
|
|
102
|
+
* errors deep inside someone's migration rather than at connection time.
|
|
103
|
+
*/
|
|
104
|
+
function pickRole(roles: NeonApiRole[]): NeonApiRole | null {
|
|
105
|
+
return roles.find((role) => role.protected !== true && role.name) ?? roles.find((role) => role.name) ?? null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function pickDatabase(databases: NeonApiDatabase[]): NeonApiDatabase | null {
|
|
109
|
+
return databases.find((database) => database.name) ?? null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Turn a connection-URI payload into the shape both surfaces store. */
|
|
113
|
+
function fromConnectionUri(entry: NeonApiConnectionUri, pooled: boolean): BranchConnection | null {
|
|
114
|
+
const parameters = entry.connection_parameters;
|
|
115
|
+
if (!parameters?.host || !parameters.role || !parameters.database) return null;
|
|
116
|
+
if (!parameters.password) return null;
|
|
117
|
+
|
|
118
|
+
const host = pooled ? (parameters.pooler_host || parameters.host) : parameters.host;
|
|
119
|
+
|
|
120
|
+
// The URI is REBUILT from the parts rather than taken verbatim, for one
|
|
121
|
+
// reason: `connection_uri` always names the direct host, so a pooled request
|
|
122
|
+
// answered with a URI that contradicts the parameters beside it. Building it
|
|
123
|
+
// here keeps the string and the fields describing the same endpoint.
|
|
124
|
+
return {
|
|
125
|
+
uri: buildUri({
|
|
126
|
+
host,
|
|
127
|
+
username: parameters.role,
|
|
128
|
+
password: parameters.password,
|
|
129
|
+
database: parameters.database
|
|
130
|
+
}),
|
|
131
|
+
host,
|
|
132
|
+
port: NEON_PORT,
|
|
133
|
+
username: parameters.role,
|
|
134
|
+
password: parameters.password,
|
|
135
|
+
database: parameters.database,
|
|
136
|
+
sslMode: NEON_SSL_MODE
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Read a libpq URI back into its parts.
|
|
142
|
+
*
|
|
143
|
+
* This is what `GET /connection_uri` needs, because that endpoint answers with
|
|
144
|
+
* the string and nothing else. The URI is kept VERBATIM rather than rebuilt:
|
|
145
|
+
* it is Neon's own, it already reflects the `pooled` parameter it was asked
|
|
146
|
+
* for, and it may carry options (`channel_binding`) that rebuilding would drop.
|
|
147
|
+
* The discrete fields are derived from the same string, so the two cannot
|
|
148
|
+
* disagree.
|
|
149
|
+
*/
|
|
150
|
+
export function parseConnectionUri(uri: string): BranchConnection | null {
|
|
151
|
+
let url: URL;
|
|
152
|
+
try {
|
|
153
|
+
url = new URL(uri);
|
|
154
|
+
} catch {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const host = url.hostname;
|
|
159
|
+
const username = decodeURIComponent(url.username);
|
|
160
|
+
const password = decodeURIComponent(url.password);
|
|
161
|
+
const database = decodeURIComponent(url.pathname.replace(/^\//, ''));
|
|
162
|
+
// A URI without a password cannot log in, and reporting it as resolved would
|
|
163
|
+
// push the failure into a driver that says "authentication failed" instead.
|
|
164
|
+
if (!host || !username || !password || !database) return null;
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
uri,
|
|
168
|
+
host,
|
|
169
|
+
port: url.port ? Number(url.port) : NEON_PORT,
|
|
170
|
+
username,
|
|
171
|
+
password,
|
|
172
|
+
database,
|
|
173
|
+
sslMode: NEON_SSL_MODE
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A libpq connection URI, with every component escaped. */
|
|
178
|
+
export function buildUri(input: {
|
|
179
|
+
host: string;
|
|
180
|
+
username: string;
|
|
181
|
+
password: string;
|
|
182
|
+
database: string;
|
|
183
|
+
}): string {
|
|
184
|
+
const user = encodeURIComponent(input.username);
|
|
185
|
+
const password = encodeURIComponent(input.password);
|
|
186
|
+
const database = encodeURIComponent(input.database);
|
|
187
|
+
return `postgresql://${user}:${password}@${input.host}/${database}?sslmode=${NEON_SSL_MODE}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Read a connection out of a create response, when it carried one.
|
|
192
|
+
*
|
|
193
|
+
* Returns null rather than throwing when it did not — the caller then resolves
|
|
194
|
+
* the long way. See `api-types.ts`: this field is documented as absent for any
|
|
195
|
+
* branch cut from a parent with more than one role or database.
|
|
196
|
+
*/
|
|
197
|
+
export function connectionFromCreateResponse(
|
|
198
|
+
entries: NeonApiConnectionUri[] | undefined,
|
|
199
|
+
pooled: boolean
|
|
200
|
+
): BranchConnection | null {
|
|
201
|
+
for (const entry of entries ?? []) {
|
|
202
|
+
const fromParameters = fromConnectionUri(entry, pooled);
|
|
203
|
+
if (fromParameters) return fromParameters;
|
|
204
|
+
|
|
205
|
+
// No `connection_parameters`, so only the string is available — and it
|
|
206
|
+
// names the DIRECT host. That is the answer for a direct request and the
|
|
207
|
+
// wrong one for a pooled request, where deriving the pooler hostname
|
|
208
|
+
// would mean guessing at a naming convention. Returning null there sends
|
|
209
|
+
// the caller to `GET /connection_uri?pooled=true`, which answers
|
|
210
|
+
// authoritatively.
|
|
211
|
+
if (!pooled && entry.connection_uri) {
|
|
212
|
+
const parsed = parseConnectionUri(entry.connection_uri);
|
|
213
|
+
if (parsed) return parsed;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Resolve where a branch answers, asking Neon for whatever is still unknown.
|
|
221
|
+
*
|
|
222
|
+
* Three requests in the worst case, and they are not avoidable: the endpoint
|
|
223
|
+
* that hands back a connection URI insists on being told which database and
|
|
224
|
+
* which role, and neither is derivable from the branch id.
|
|
225
|
+
*/
|
|
226
|
+
export async function resolveBranchConnection(
|
|
227
|
+
credentials: NeonCredentials,
|
|
228
|
+
input: { projectId: string; branchId: string; pooled: boolean }
|
|
229
|
+
): Promise<BranchConnection> {
|
|
230
|
+
const base = `/projects/${encodeURIComponent(input.projectId)}/branches/${encodeURIComponent(input.branchId)}`;
|
|
231
|
+
|
|
232
|
+
const [databases, roles] = await Promise.all([
|
|
233
|
+
neonRequest<{ databases?: NeonApiDatabase[] }>(credentials, `${base}/databases`),
|
|
234
|
+
neonRequest<{ roles?: NeonApiRole[] }>(credentials, `${base}/roles`)
|
|
235
|
+
]);
|
|
236
|
+
|
|
237
|
+
const database = pickDatabase(databases?.databases ?? []);
|
|
238
|
+
const role = pickRole(roles?.roles ?? []);
|
|
239
|
+
|
|
240
|
+
if (!database?.name || !role?.name) {
|
|
241
|
+
throw new NeonError(
|
|
242
|
+
'This Neon branch has no database and role to connect as, so no connection string can be built for it.',
|
|
243
|
+
404,
|
|
244
|
+
'config'
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// `{ uri }` and NOTHING else — see fact 1 in the header. The answer already
|
|
249
|
+
// reflects `pooled`, so parsing it is both simpler and more faithful than
|
|
250
|
+
// assembling one from parts we would have to fetch separately.
|
|
251
|
+
const response = await neonRequest<NeonApiConnectionUriResponse>(
|
|
252
|
+
credentials,
|
|
253
|
+
`/projects/${encodeURIComponent(input.projectId)}/connection_uri`,
|
|
254
|
+
{
|
|
255
|
+
query: {
|
|
256
|
+
branch_id: input.branchId,
|
|
257
|
+
database_name: database.name,
|
|
258
|
+
role_name: role.name,
|
|
259
|
+
pooled: input.pooled
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
const resolved = response?.uri ? parseConnectionUri(response.uri) : null;
|
|
265
|
+
if (resolved) return resolved;
|
|
266
|
+
|
|
267
|
+
// The URI was missing or carried no password. Neon stores role passwords
|
|
268
|
+
// unless a project opted out, so the role itself is the one remaining source
|
|
269
|
+
// — and it is a source no other managed Postgres in this codebase has.
|
|
270
|
+
const host = response?.uri ? safeHost(response.uri) : null;
|
|
271
|
+
if (!host) {
|
|
272
|
+
throw new NeonError(
|
|
273
|
+
`Neon returned no usable connection string for branch ${input.branchId}. Check the branch has a compute endpoint in the Neon console.`,
|
|
274
|
+
502,
|
|
275
|
+
'error'
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const revealed = await neonRequest<NeonApiRolePassword>(
|
|
280
|
+
credentials,
|
|
281
|
+
`${base}/roles/${encodeURIComponent(role.name)}/reveal_password`
|
|
282
|
+
);
|
|
283
|
+
if (!revealed?.password) {
|
|
284
|
+
throw new NeonError(
|
|
285
|
+
`Neon would not reveal the password for role "${role.name}". Create the project again with password storage enabled, or connect it by hand.`,
|
|
286
|
+
403,
|
|
287
|
+
'config'
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
uri: buildUri({
|
|
293
|
+
host,
|
|
294
|
+
username: role.name,
|
|
295
|
+
password: revealed.password,
|
|
296
|
+
database: database.name
|
|
297
|
+
}),
|
|
298
|
+
host,
|
|
299
|
+
port: NEON_PORT,
|
|
300
|
+
username: role.name,
|
|
301
|
+
password: revealed.password,
|
|
302
|
+
database: database.name,
|
|
303
|
+
sslMode: NEON_SSL_MODE
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** The hostname of a URI, or null when it is not one. */
|
|
308
|
+
function safeHost(uri: string): string | null {
|
|
309
|
+
try {
|
|
310
|
+
return new URL(uri).hostname || null;
|
|
311
|
+
} catch {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listing Neon projects, which is organisation-scoped.
|
|
3
|
+
*
|
|
4
|
+
* This file exists because the adapter shipped getting it wrong. `/projects`
|
|
5
|
+
* reads like an account-wide listing and is not: the document calls it "a list
|
|
6
|
+
* of projects for the specified organization", and a personal key that omits
|
|
7
|
+
* `org_id` is refused with
|
|
8
|
+
*
|
|
9
|
+
* 400 org_id is required, you can find it on your organization settings page
|
|
10
|
+
*
|
|
11
|
+
* which took down every read path — the picker, the branching parent list, the
|
|
12
|
+
* health probe and the readiness poll — against a real key. The tests below
|
|
13
|
+
* lock down the three things that were wrong or missing.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, expect, afterEach } from 'bun:test';
|
|
17
|
+
|
|
18
|
+
import { listNeonProjects } from './adapter';
|
|
19
|
+
import { NeonError } from './client';
|
|
20
|
+
|
|
21
|
+
const realFetch = globalThis.fetch;
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
globalThis.fetch = realFetch;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/** A distinct key per test — the organisation lookup is memoised per key. */
|
|
27
|
+
let keyCounter = 0;
|
|
28
|
+
function credentials() {
|
|
29
|
+
keyCounter += 1;
|
|
30
|
+
return { apiKey: `napi_test_${keyCounter}` };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function json(body: unknown, status = 200): Response {
|
|
34
|
+
return new Response(JSON.stringify(body), {
|
|
35
|
+
status,
|
|
36
|
+
headers: { 'content-type': 'application/json' }
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function project(id: string) {
|
|
41
|
+
return { id, name: id, region_id: 'aws-eu-central-1' };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Stand in for Neon, refusing `/projects` without `org_id` exactly as it does.
|
|
46
|
+
*
|
|
47
|
+
* `projectsByOrg` maps an organisation id to what that scope answers with;
|
|
48
|
+
* `organizations` is what `/users/me/organizations` reports.
|
|
49
|
+
*/
|
|
50
|
+
function mockNeon(input: {
|
|
51
|
+
organizations: { id: string; name?: string }[];
|
|
52
|
+
projectsByOrg: Record<string, unknown[]>;
|
|
53
|
+
/** Organisations whose listing fails, and with what status. */
|
|
54
|
+
failing?: Record<string, number>;
|
|
55
|
+
/** Allow a bare `/projects` call to succeed, as a legacy account would. */
|
|
56
|
+
bareProjects?: unknown[];
|
|
57
|
+
}) {
|
|
58
|
+
const requested: string[] = [];
|
|
59
|
+
|
|
60
|
+
globalThis.fetch = (async (target: string | URL) => {
|
|
61
|
+
const url = new URL(String(target));
|
|
62
|
+
requested.push(url.pathname + url.search);
|
|
63
|
+
|
|
64
|
+
if (url.pathname.endsWith('/users/me/organizations')) {
|
|
65
|
+
return json({ organizations: input.organizations });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (url.pathname.endsWith('/projects')) {
|
|
69
|
+
const orgId = url.searchParams.get('org_id');
|
|
70
|
+
if (!orgId) {
|
|
71
|
+
if (input.bareProjects) return json({ projects: input.bareProjects });
|
|
72
|
+
return json(
|
|
73
|
+
{ message: 'org_id is required, you can find it on your organization settings page' },
|
|
74
|
+
400
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const status = input.failing?.[orgId];
|
|
78
|
+
if (status) return json({ message: 'Nope' }, status);
|
|
79
|
+
return json({ projects: input.projectsByOrg[orgId] ?? [] });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
throw new Error(`unexpected request: ${url}`);
|
|
83
|
+
}) as typeof fetch;
|
|
84
|
+
|
|
85
|
+
return requested;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
describe('listNeonProjects', () => {
|
|
89
|
+
it('sends org_id, which is what the bare call is refused for', async () => {
|
|
90
|
+
const requested = mockNeon({
|
|
91
|
+
organizations: [{ id: 'org-one' }],
|
|
92
|
+
projectsByOrg: { 'org-one': [project('p1')] }
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const projects = await listNeonProjects(credentials());
|
|
96
|
+
|
|
97
|
+
expect(projects.map((entry) => entry.id)).toEqual(['p1']);
|
|
98
|
+
// The regression in one assertion: no request for `/projects` without a
|
|
99
|
+
// scope, because Neon answers that one with a 400.
|
|
100
|
+
expect(requested.some((path) => path.includes('/projects?') && !path.includes('org_id='))).toBe(false);
|
|
101
|
+
expect(requested).toContain('/api/v2/projects?limit=400&org_id=org-one');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('merges every organisation the key reaches', async () => {
|
|
105
|
+
// The quieter bug behind the 400: a key in two organisations would only
|
|
106
|
+
// ever have seen one of them, and nothing would have said so.
|
|
107
|
+
mockNeon({
|
|
108
|
+
organizations: [{ id: 'org-one' }, { id: 'org-two' }],
|
|
109
|
+
projectsByOrg: { 'org-one': [project('p1')], 'org-two': [project('p2')] }
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const projects = await listNeonProjects(credentials());
|
|
113
|
+
expect(projects.map((entry) => entry.id).sort()).toEqual(['p1', 'p2']);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('de-duplicates a project reported by two scopes', async () => {
|
|
117
|
+
mockNeon({
|
|
118
|
+
organizations: [{ id: 'org-one' }, { id: 'org-two' }],
|
|
119
|
+
projectsByOrg: { 'org-one': [project('p1')], 'org-two': [project('p1')] }
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
expect(await listNeonProjects(credentials())).toHaveLength(1);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('drops a soft-deleted project rather than offering a dead end', async () => {
|
|
126
|
+
mockNeon({
|
|
127
|
+
organizations: [{ id: 'org-one' }],
|
|
128
|
+
projectsByOrg: {
|
|
129
|
+
'org-one': [project('p1'), { ...project('p2'), deleted_at: '2026-01-01T00:00:00Z' }]
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect((await listNeonProjects(credentials())).map((entry) => entry.id)).toEqual(['p1']);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('returns what it got when one organisation fails, and throws when none answered', async () => {
|
|
137
|
+
// Partial failure is tolerated because half a list beats none; a TOTAL
|
|
138
|
+
// failure must not be reported as "no projects", which is the "an absent
|
|
139
|
+
// signal is not a negative one" mistake.
|
|
140
|
+
mockNeon({
|
|
141
|
+
organizations: [{ id: 'org-one' }, { id: 'org-two' }],
|
|
142
|
+
projectsByOrg: { 'org-one': [project('p1')] },
|
|
143
|
+
failing: { 'org-two': 500 }
|
|
144
|
+
});
|
|
145
|
+
expect((await listNeonProjects(credentials())).map((entry) => entry.id)).toEqual(['p1']);
|
|
146
|
+
|
|
147
|
+
mockNeon({
|
|
148
|
+
organizations: [{ id: 'org-one' }],
|
|
149
|
+
projectsByOrg: {},
|
|
150
|
+
failing: { 'org-one': 500 }
|
|
151
|
+
});
|
|
152
|
+
await expect(listNeonProjects(credentials())).rejects.toThrow();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('falls back to the bare call only when the key reports no organisation', async () => {
|
|
156
|
+
const requested = mockNeon({
|
|
157
|
+
organizations: [],
|
|
158
|
+
projectsByOrg: {},
|
|
159
|
+
bareProjects: [project('legacy')]
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect((await listNeonProjects(credentials())).map((entry) => entry.id)).toEqual(['legacy']);
|
|
163
|
+
expect(requested).toContain('/api/v2/projects?limit=400');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("rewrites Neon's org_id message, which names a page that cannot help", async () => {
|
|
167
|
+
// By the time this is reached it is OUR lookup that came back empty, so
|
|
168
|
+
// repeating "you can find it on your organization settings page" would
|
|
169
|
+
// send the user somewhere with nothing to fix.
|
|
170
|
+
mockNeon({ organizations: [], projectsByOrg: {} });
|
|
171
|
+
|
|
172
|
+
const error = await listNeonProjects(credentials()).catch((caught: unknown) => caught);
|
|
173
|
+
expect(error).toBeInstanceOf(NeonError);
|
|
174
|
+
expect((error as NeonError).message).toBe(
|
|
175
|
+
'This API key reaches no Neon organisation — create the key again from the account that owns your projects.'
|
|
176
|
+
);
|
|
177
|
+
// `config`, not `error`: the hub's strip should say there is something to
|
|
178
|
+
// fix rather than showing a red failure with no guidance.
|
|
179
|
+
expect((error as NeonError).kind).toBe('config');
|
|
180
|
+
});
|
|
181
|
+
});
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
import type {
|
|
26
26
|
DbProviderAdapter,
|
|
27
27
|
DbProviderContext,
|
|
28
|
-
DbProviderEndpoint
|
|
28
|
+
DbProviderEndpoint,
|
|
29
|
+
DbProviderEnvHints
|
|
29
30
|
} from '../types';
|
|
30
31
|
import type {
|
|
31
32
|
DbProviderCreateGroup,
|
|
@@ -199,6 +200,25 @@ export const supabaseDbAdapter: DbProviderAdapter = {
|
|
|
199
200
|
};
|
|
200
201
|
},
|
|
201
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Supabase documents the `POSTGRES_*` names Vercel's integration injects, so
|
|
205
|
+
* that is the preset. The direct endpoint is offered as the non-pooling half
|
|
206
|
+
* WITH its caveat attached rather than silently: on projects created since
|
|
207
|
+
* 2024 it answers only over IPv6, so a user on an IPv4-only network gets a
|
|
208
|
+
* URL that resolves and never connects — and being told that beats
|
|
209
|
+
* discovering it inside a migration.
|
|
210
|
+
*/
|
|
211
|
+
envHints(): DbProviderEnvHints {
|
|
212
|
+
return {
|
|
213
|
+
urlPrefix: 'POSTGRES',
|
|
214
|
+
altKeySuffix: '_NON_POOLING',
|
|
215
|
+
directMode: SUPABASE_MODES.direct,
|
|
216
|
+
pooledModes: [SUPABASE_MODES.poolerSession, SUPABASE_MODES.poolerTransaction],
|
|
217
|
+
directNotice:
|
|
218
|
+
'Supabase\u2019s direct endpoint is IPv6-only on projects created since 2024, so check it resolves before relying on it.'
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
|
|
202
222
|
async listDatabases(context: DbProviderContext): Promise<DbRemoteDatabase[]> {
|
|
203
223
|
const credentials = supabaseCredentialsOf(context);
|
|
204
224
|
// The organisation is fetched ALONGSIDE the projects, not for each of
|
|
@@ -378,7 +398,10 @@ export const supabaseDbAdapter: DbProviderAdapter = {
|
|
|
378
398
|
emptyGroupsNotice:
|
|
379
399
|
'This token reaches no organisation — create one, or regenerate the token with Organizations Read.',
|
|
380
400
|
canCreateGroup: true,
|
|
381
|
-
createGroupNotice: 'A new organisation is a billing entity and starts on the free plan.'
|
|
401
|
+
createGroupNotice: 'A new organisation is a billing entity and starts on the free plan.',
|
|
402
|
+
// `/v1/organizations/{slug}` is GET-only, so renaming and deleting are
|
|
403
|
+
// absent rather than broken and no permission would change that.
|
|
404
|
+
groupManagementNotice: "Rename or delete an organisation in Supabase's dashboard."
|
|
382
405
|
};
|
|
383
406
|
},
|
|
384
407
|
|
|
@@ -53,6 +53,55 @@ export interface DbProviderEndpoint {
|
|
|
53
53
|
database: string;
|
|
54
54
|
sslMode: DbSslMode;
|
|
55
55
|
options?: Record<string, unknown>;
|
|
56
|
+
/**
|
|
57
|
+
* Secrets the provider resolved for itself, for the providers that can.
|
|
58
|
+
*
|
|
59
|
+
* Supabase cannot: its Management API has no endpoint that reads a database
|
|
60
|
+
* password back, so a link asks the user for one and stores it. Neon can —
|
|
61
|
+
* `/reveal_password` exists — so it hands one back here and its
|
|
62
|
+
* `secretFields` is empty, which removes the only manual step in linking a
|
|
63
|
+
* database.
|
|
64
|
+
*
|
|
65
|
+
* `links.ts` PEELS THIS OFF before the endpoint is stored. An endpoint lives
|
|
66
|
+
* in the link's `config_json`, which is NOT a sealed column; the secrets go
|
|
67
|
+
* into `secrets`, which is. Leaving the password on the endpoint would write
|
|
68
|
+
* a working credential to disk in plaintext — the exact thing `Task 1`'s
|
|
69
|
+
* encryption layer exists to prevent.
|
|
70
|
+
*/
|
|
71
|
+
secrets?: Record<string, string>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What this provider's own documentation calls the connection in a dotenv file.
|
|
76
|
+
*
|
|
77
|
+
* Declared rather than derived, because both halves are vendor vocabulary that
|
|
78
|
+
* nothing outside the provider's directory can know: Neon's docs name the pair
|
|
79
|
+
* `DATABASE_URL` / `DATABASE_URL_UNPOOLED`, Supabase's name it `POSTGRES_URL` /
|
|
80
|
+
* `POSTGRES_URL_NON_POOLING`, and which of a provider's connection modes counts
|
|
81
|
+
* as "not behind a pooler" is a fact about its infrastructure.
|
|
82
|
+
*
|
|
83
|
+
* A DEFAULT only. Whatever the project's own files already call the variable
|
|
84
|
+
* wins over this — see `db-client/env/detect.ts`. These names are what a
|
|
85
|
+
* project with nothing in it is offered.
|
|
86
|
+
*
|
|
87
|
+
* The second endpoint is what makes this worth declaring at all. A pooled URL
|
|
88
|
+
* is the right default for an application and the WRONG one for migrations —
|
|
89
|
+
* Prisma's `DIRECT_URL` exists for exactly this — so a connection linked in
|
|
90
|
+
* pooled mode has a counterpart worth writing, and one already on the direct
|
|
91
|
+
* endpoint has none: a `DIRECT_URL` equal to `DATABASE_URL` would look like
|
|
92
|
+
* configuration and change nothing.
|
|
93
|
+
*/
|
|
94
|
+
export interface DbProviderEnvHints {
|
|
95
|
+
/** What this provider's docs prefix the URL with — `DATABASE`, `POSTGRES`. */
|
|
96
|
+
urlPrefix?: string;
|
|
97
|
+
/** What its docs suffix the second endpoint with — `_UNPOOLED`, `_NON_POOLING`. */
|
|
98
|
+
altKeySuffix?: string;
|
|
99
|
+
/** The mode that is not behind a pooler, when the provider has one. */
|
|
100
|
+
directMode?: string;
|
|
101
|
+
/** Modes that ARE pooled, so a connection on one has a counterpart. */
|
|
102
|
+
pooledModes?: string[];
|
|
103
|
+
/** One sentence naming what the direct endpoint costs, where it costs something. */
|
|
104
|
+
directNotice?: string;
|
|
56
105
|
}
|
|
57
106
|
|
|
58
107
|
export interface DbProviderAdapter {
|
|
@@ -74,6 +123,14 @@ export interface DbProviderAdapter {
|
|
|
74
123
|
/** Health of the credential itself, for the hub's status strip. */
|
|
75
124
|
probe(context: DbProviderContext): Promise<{ status: IntegrationStatus; detail: string | null }>;
|
|
76
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Optional: what this provider's docs call the connection in a dotenv file.
|
|
128
|
+
*
|
|
129
|
+
* Absent means DB Client offers its generic presets and no second endpoint,
|
|
130
|
+
* which is the correct answer for a provider with one endpoint per database.
|
|
131
|
+
*/
|
|
132
|
+
envHints?(): DbProviderEnvHints;
|
|
133
|
+
|
|
77
134
|
/**
|
|
78
135
|
* Optional: create a database rather than only listing existing ones.
|
|
79
136
|
*
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding the dotenv files a project actually has.
|
|
3
|
+
*
|
|
4
|
+
* Ordered rather than alphabetical, and the order is the reason this is shown
|
|
5
|
+
* at all: Next.js and Vite read `.env.local` IN PREFERENCE to `.env`, so a
|
|
6
|
+
* project with both has exactly one right answer and picking the other produces
|
|
7
|
+
* the worst outcome available — the variable is written and nothing reads it.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import fs from 'fs/promises';
|
|
11
|
+
import { envFilePrecedence, isDotenvFileName, isTemplateEnvFile } from './keys';
|
|
12
|
+
|
|
13
|
+
export interface DotenvFileEntry {
|
|
14
|
+
name: string;
|
|
15
|
+
/** True for `.env.example` and friends: read for their shape, never written. */
|
|
16
|
+
isTemplate: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Every dotenv-shaped file in the root, templates included, most-precedent first. */
|
|
20
|
+
export async function listEnvFileEntries(projectRoot: string): Promise<DotenvFileEntry[]> {
|
|
21
|
+
let entries: string[];
|
|
22
|
+
try {
|
|
23
|
+
entries = await fs.readdir(projectRoot);
|
|
24
|
+
} catch {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return entries
|
|
29
|
+
.filter((name) => isDotenvFileName(name))
|
|
30
|
+
.map((name) => ({ name, isTemplate: isTemplateEnvFile(name) }))
|
|
31
|
+
.sort(
|
|
32
|
+
(a, b) =>
|
|
33
|
+
Number(a.isTemplate) - Number(b.isTemplate) ||
|
|
34
|
+
envFilePrecedence(a.name) - envFilePrecedence(b.name) ||
|
|
35
|
+
a.name.localeCompare(b.name)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The files a project would LOAD, most-precedent first.
|
|
41
|
+
*
|
|
42
|
+
* Templates are excluded. `.env.example` is documentation, and writing a live
|
|
43
|
+
* password into the file a project commits is the opposite of the rule above.
|
|
44
|
+
*/
|
|
45
|
+
export async function listDotenvFiles(projectRoot: string): Promise<string[]> {
|
|
46
|
+
const entries = await listEnvFileEntries(projectRoot);
|
|
47
|
+
return entries.filter((entry) => !entry.isTemplate).map((entry) => entry.name);
|
|
48
|
+
}
|