@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.
Files changed (88) hide show
  1. package/backend/auth/permissions.ts +23 -0
  2. package/backend/database/crypto/secret-columns.ts +5 -1
  3. package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
  4. package/backend/database/migrations/index.ts +7 -0
  5. package/backend/database/queries/index.ts +6 -0
  6. package/backend/database/queries/worktree-branch-queries.ts +331 -0
  7. package/backend/db-client/cloning.test.ts +161 -0
  8. package/backend/db-client/cloning.ts +672 -0
  9. package/backend/db-client/connection-url.test.ts +117 -0
  10. package/backend/db-client/connection-url.ts +126 -0
  11. package/backend/db-client/drivers/mongodb.ts +2 -14
  12. package/backend/db-client/drivers/mysql.ts +2 -8
  13. package/backend/db-client/drivers/postgres.ts +10 -13
  14. package/backend/db-client/drivers/redis.ts +2 -6
  15. package/backend/db-client/env/detect.ts +438 -0
  16. package/backend/db-client/env/diff.ts +94 -0
  17. package/backend/db-client/env/env.test.ts +574 -0
  18. package/backend/db-client/env/index.ts +15 -0
  19. package/backend/db-client/env/naming.ts +202 -0
  20. package/backend/db-client/env/service.ts +487 -0
  21. package/backend/db-client/integrations/index.ts +7 -0
  22. package/backend/db-client/integrations/links.ts +41 -10
  23. package/backend/db-client/providers/neon/adapter.ts +522 -0
  24. package/backend/db-client/providers/neon/api-types.ts +182 -0
  25. package/backend/db-client/providers/neon/client.ts +298 -0
  26. package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
  27. package/backend/db-client/providers/neon/endpoints.ts +314 -0
  28. package/backend/db-client/providers/neon/list.test.ts +181 -0
  29. package/backend/db-client/providers/supabase/adapter.ts +25 -2
  30. package/backend/db-client/providers/types.ts +57 -0
  31. package/backend/env-files/discovery.ts +48 -0
  32. package/backend/env-files/edit.ts +213 -0
  33. package/backend/env-files/env-files.test.ts +515 -0
  34. package/backend/env-files/git.ts +27 -0
  35. package/backend/env-files/index.ts +44 -0
  36. package/backend/env-files/keys.ts +69 -0
  37. package/backend/env-files/parse.ts +157 -0
  38. package/backend/env-files/write.ts +229 -0
  39. package/backend/integrations/health.ts +18 -4
  40. package/backend/integrations/projections/types.ts +9 -2
  41. package/backend/integrations/registry/index.ts +3 -0
  42. package/backend/integrations/registry/providers/context7.ts +1 -0
  43. package/backend/integrations/registry/providers/firecrawl.ts +1 -0
  44. package/backend/integrations/registry/providers/github.ts +1 -0
  45. package/backend/integrations/registry/providers/neon.ts +55 -0
  46. package/backend/integrations/registry/providers/supabase.ts +1 -0
  47. package/backend/integrations/registry/providers/vercel.ts +1 -0
  48. package/backend/integrations/registry/types.ts +2 -0
  49. package/backend/work/start-work.ts +4 -0
  50. package/backend/worktrees/branching/engine.ts +557 -0
  51. package/backend/worktrees/branching/index.ts +56 -0
  52. package/backend/worktrees/branching/naming.test.ts +118 -0
  53. package/backend/worktrees/branching/naming.ts +97 -0
  54. package/backend/worktrees/branching/projector.test.ts +229 -0
  55. package/backend/worktrees/branching/projector.ts +139 -0
  56. package/backend/worktrees/branching/providers/neon.ts +220 -0
  57. package/backend/worktrees/branching/registry.ts +27 -0
  58. package/backend/worktrees/branching/service.ts +620 -0
  59. package/backend/worktrees/branching/suggest.test.ts +154 -0
  60. package/backend/worktrees/branching/suggest.ts +202 -0
  61. package/backend/worktrees/branching/types.ts +88 -0
  62. package/backend/worktrees/index.ts +7 -0
  63. package/backend/worktrees/service.ts +48 -1
  64. package/backend/ws/db-client/env.ts +142 -0
  65. package/backend/ws/db-client/index.ts +2 -0
  66. package/backend/ws/projects/index.ts +6 -1
  67. package/backend/ws/projects/info.ts +70 -6
  68. package/backend/ws/projects/overview.test.ts +108 -0
  69. package/backend/ws/projects/overview.ts +300 -0
  70. package/backend/ws/system/device-info.ts +307 -76
  71. package/backend/ws/worktrees/branching.ts +212 -0
  72. package/backend/ws/worktrees/crud.ts +33 -7
  73. package/backend/ws/worktrees/index.ts +14 -0
  74. package/dist/assets/{index-C8Gi-b6U.js → index-D4igjF6e.js} +1 -1
  75. package/dist/assets/{index-BrF6nqld.js → index-DONNwiXm.js} +1 -1
  76. package/dist/assets/main-BrfXH1bL.js +1205 -0
  77. package/dist/assets/main-CT1PygsW.css +1 -0
  78. package/dist/index.html +2 -2
  79. package/package.json +1 -1
  80. package/shared/constants/tool-icons.ts +6 -2
  81. package/shared/types/db-client/env.ts +192 -0
  82. package/shared/types/db-client/index.ts +1 -0
  83. package/shared/types/db-client/integrations.ts +30 -0
  84. package/shared/types/integrations.ts +9 -0
  85. package/shared/types/worktree-branching.ts +315 -0
  86. package/shared/utils/logger.ts +1 -0
  87. package/dist/assets/main-Bd3gb79F.css +0 -1
  88. package/dist/assets/main-Bo1hD7mt.js +0 -1191
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Tests for the one connection URL builder.
3
+ *
4
+ * These lock the strings the DRIVERS produce, not just the ones a user copies.
5
+ * Four adapters were folded onto this function, and a regression here does not
6
+ * fail a type check — it fails a connection, or worse, connects to the wrong
7
+ * database. The tunnel cases matter most: the drivers rewrite host and port to
8
+ * the local end of an SSH tunnel, and the environment variable must never.
9
+ */
10
+
11
+ import { describe, it, expect } from 'bun:test';
12
+ import type { DbClientConnection, DbDriver } from '$shared/types/db-client';
13
+ import { buildConnectionUrl } from './connection-url';
14
+
15
+ function connection(overrides: Partial<DbClientConnection> & { driver: DbDriver }): DbClientConnection {
16
+ return {
17
+ id: 'c1',
18
+ name: 'Test',
19
+ host: 'db.example.com',
20
+ port: null,
21
+ username: 'app',
22
+ password: 'pa ss/word',
23
+ database: 'shop',
24
+ sslMode: 'disable',
25
+ sslCa: null,
26
+ ssh: {
27
+ enabled: false,
28
+ connectionId: null,
29
+ host: '',
30
+ port: 22,
31
+ username: '',
32
+ authMethod: 'password'
33
+ },
34
+ options: {},
35
+ color: null,
36
+ createdAt: '',
37
+ updatedAt: '',
38
+ lastUsedAt: null,
39
+ ...overrides
40
+ };
41
+ }
42
+
43
+ describe('buildConnectionUrl', () => {
44
+ it('percent-encodes a password that would otherwise break the URL', () => {
45
+ // The whole reason this is one function: a `/` in a password silently
46
+ // becomes a path segment, and the driver then reports "database not found".
47
+ const url = buildConnectionUrl(connection({ driver: 'postgres' }));
48
+ expect(url).toBe('postgresql://app:pa%20ss%2Fword@db.example.com:5432/shop');
49
+ });
50
+
51
+ it('uses the scheme Bun.sql wants when asked, and the readable one otherwise', () => {
52
+ const conn = connection({ driver: 'postgres' });
53
+ expect(buildConnectionUrl(conn, { scheme: 'postgres' })).toStartWith('postgres://');
54
+ expect(buildConnectionUrl(conn)).toStartWith('postgresql://');
55
+ });
56
+
57
+ it('adds sslmode only when SSL is actually on', () => {
58
+ expect(buildConnectionUrl(connection({ driver: 'postgres', sslMode: 'require' }))).toContain(
59
+ '?sslmode=require'
60
+ );
61
+ expect(buildConnectionUrl(connection({ driver: 'postgres' }))).not.toContain('sslmode');
62
+ });
63
+
64
+ it('substitutes the fallback database only where the caller asks for one', () => {
65
+ const conn = connection({ driver: 'postgres', database: null });
66
+ expect(buildConnectionUrl(conn, { fallbackDatabase: 'postgres' })).toEndWith('/postgres');
67
+ expect(buildConnectionUrl(conn)).toEndWith(':5432');
68
+ });
69
+
70
+ it('rewrites host and port for a tunnel, which the env variable must not do', () => {
71
+ const conn = connection({ driver: 'mysql', port: 3306 });
72
+ expect(buildConnectionUrl(conn, { tunnelPort: 54321 })).toBe(
73
+ 'mysql://app:pa%20ss%2Fword@127.0.0.1:54321/shop'
74
+ );
75
+ expect(buildConnectionUrl(conn)).toContain('db.example.com:3306');
76
+ });
77
+
78
+ it('drops the password when there is no username to carry it', () => {
79
+ // Legal in Redis and nowhere else, which is where `omitUsername` is used.
80
+ const conn = connection({ driver: 'postgres', username: null });
81
+ expect(buildConnectionUrl(conn)).toBe('postgresql://db.example.com:5432/shop');
82
+ });
83
+
84
+ it('keeps Mongo authSource, defaulting to admin only when a user is set', () => {
85
+ expect(buildConnectionUrl(connection({ driver: 'mongodb' }))).toContain('?authSource=admin');
86
+ expect(
87
+ buildConnectionUrl(connection({ driver: 'mongodb', options: { authSource: 'shop' } }))
88
+ ).toContain('?authSource=shop');
89
+ expect(
90
+ buildConnectionUrl(connection({ driver: 'mongodb', username: null, password: null }))
91
+ ).toBe('mongodb://db.example.com:27017/shop');
92
+ });
93
+
94
+ it('builds Redis with the password but no username, and only a numeric index', () => {
95
+ expect(buildConnectionUrl(connection({ driver: 'redis', database: '3' }))).toBe(
96
+ 'redis://:pa%20ss%2Fword@db.example.com:6379/3'
97
+ );
98
+ // A non-numeric value means the connection names no index, not a database
99
+ // called that.
100
+ expect(buildConnectionUrl(connection({ driver: 'redis', database: 'cache' }))).toBe(
101
+ 'redis://:pa%20ss%2Fword@db.example.com:6379'
102
+ );
103
+ });
104
+
105
+ it('answers with a file: path for SQLite, which has no endpoint', () => {
106
+ expect(buildConnectionUrl(connection({ driver: 'sqlite', database: '/data/app.db' }))).toBe(
107
+ 'file:/data/app.db'
108
+ );
109
+ expect(buildConnectionUrl(connection({ driver: 'sqlite', database: null }))).toBe('');
110
+ });
111
+
112
+ it('falls back to localhost and the driver default port', () => {
113
+ expect(
114
+ buildConnectionUrl(connection({ driver: 'mssql', host: null, port: null }))
115
+ ).toBe('mssql://app:pa%20ss%2Fword@127.0.0.1:1433/shop');
116
+ });
117
+ });
@@ -0,0 +1,126 @@
1
+ /**
2
+ * One connection URL, built in one place.
3
+ *
4
+ * Four drivers each assembled their own `scheme://user:pass@host:port/db`, and
5
+ * then DB Client grew a fifth caller — the variable it writes into a project's
6
+ * `.env`. Five hand-rolled builders is five chances to forget to percent-encode
7
+ * a password, and the one the user COPIES has to agree with the one Clopen
8
+ * connects with or the panel is lying about what it is connected to.
9
+ *
10
+ * The differences between callers are real, so they are options rather than
11
+ * forks: the drivers rewrite host and port to the local end of an SSH tunnel,
12
+ * Bun's `SQL` wants the `postgres` scheme where a human expects `postgresql`,
13
+ * and Redis's URL has only ever carried a password. Everything else — encoding,
14
+ * the empty-username case, default ports — is shared, which is the point.
15
+ */
16
+
17
+ import type { DbClientConnection, DbDriver } from '$shared/types/db-client';
18
+
19
+ export const DEFAULT_PORTS: Record<DbDriver, number | null> = {
20
+ postgres: 5432,
21
+ mysql: 3306,
22
+ mongodb: 27017,
23
+ redis: 6379,
24
+ mssql: 1433,
25
+ // A SQLite connection is a file path, not an endpoint.
26
+ sqlite: null
27
+ };
28
+
29
+ export interface ConnectionUrlOptions {
30
+ /**
31
+ * Rewrites host and port to the local end of an open SSH tunnel.
32
+ *
33
+ * The DRIVERS pass this; the environment variable must not. A tunnel belongs
34
+ * to Clopen's own process, so a project's dev server cannot reach it and a
35
+ * `127.0.0.1` URL in someone's `.env` would fail the moment Clopen closed.
36
+ */
37
+ tunnelPort?: number;
38
+ /** `postgres` for Bun's `SQL`, `postgresql` for a string a human reads. */
39
+ scheme?: string;
40
+ /** Substituted when the connection names no database. */
41
+ fallbackDatabase?: string;
42
+ /** Redis's URL has only ever carried the password. */
43
+ omitUsername?: boolean;
44
+ }
45
+
46
+ function authOf(
47
+ conn: DbClientConnection,
48
+ options: ConnectionUrlOptions
49
+ ): string {
50
+ const user = options.omitUsername ? '' : encodeURIComponent(conn.username ?? '');
51
+ const password = conn.password ? `:${encodeURIComponent(conn.password)}` : '';
52
+ // A password with no user is legal in Redis (`redis://:pw@host`) and nowhere
53
+ // else, which is exactly where `omitUsername` is used.
54
+ if (!user && !options.omitUsername) return '';
55
+ if (!user && !password) return '';
56
+ return `${user}${password}@`;
57
+ }
58
+
59
+ function hostOf(conn: DbClientConnection, options: ConnectionUrlOptions): string {
60
+ return options.tunnelPort ? '127.0.0.1' : (conn.host ?? '127.0.0.1');
61
+ }
62
+
63
+ function portOf(conn: DbClientConnection, options: ConnectionUrlOptions): number {
64
+ return options.tunnelPort ?? conn.port ?? DEFAULT_PORTS[conn.driver] ?? 0;
65
+ }
66
+
67
+ function databaseOf(conn: DbClientConnection, options: ConnectionUrlOptions): string {
68
+ if (conn.database) return `/${encodeURIComponent(conn.database)}`;
69
+ return options.fallbackDatabase ? `/${encodeURIComponent(options.fallbackDatabase)}` : '';
70
+ }
71
+
72
+ function withQuery(base: string, params: URLSearchParams): string {
73
+ const query = params.toString();
74
+ return query ? `${base}?${query}` : base;
75
+ }
76
+
77
+ /**
78
+ * The URL for a connection, in whichever dialect the caller needs.
79
+ *
80
+ * SQLite answers with the `file:` form every ORM in common use accepts, because
81
+ * there is no endpoint to describe — and returning an empty string would put a
82
+ * blank `DATABASE_URL` into someone's environment.
83
+ */
84
+ export function buildConnectionUrl(
85
+ conn: DbClientConnection,
86
+ options: ConnectionUrlOptions = {}
87
+ ): string {
88
+ const auth = authOf(conn, options);
89
+ const host = hostOf(conn, options);
90
+ const port = portOf(conn, options);
91
+
92
+ switch (conn.driver) {
93
+ case 'postgres': {
94
+ const params = new URLSearchParams();
95
+ if (conn.sslMode && conn.sslMode !== 'disable') params.set('sslmode', conn.sslMode);
96
+ const scheme = options.scheme ?? 'postgresql';
97
+ return withQuery(`${scheme}://${auth}${host}:${port}${databaseOf(conn, options)}`, params);
98
+ }
99
+
100
+ case 'mysql':
101
+ return `mysql://${auth}${host}:${port}${databaseOf(conn, options)}`;
102
+
103
+ case 'mongodb': {
104
+ const params = new URLSearchParams();
105
+ if (conn.username) {
106
+ const authSource =
107
+ typeof conn.options?.authSource === 'string' ? conn.options.authSource : 'admin';
108
+ params.set('authSource', authSource);
109
+ }
110
+ return withQuery(`mongodb://${auth}${host}:${port}${databaseOf(conn, options)}`, params);
111
+ }
112
+
113
+ case 'redis': {
114
+ // Redis's `database` is a numeric index, and a non-numeric value means
115
+ // the connection names no index rather than a database called that.
116
+ const index = conn.database && /^\d+$/.test(conn.database) ? `/${conn.database}` : '';
117
+ return `redis://${authOf(conn, { ...options, omitUsername: true })}${host}:${port}${index}`;
118
+ }
119
+
120
+ case 'mssql':
121
+ return `mssql://${auth}${host}:${port}${databaseOf(conn, options)}`;
122
+
123
+ case 'sqlite':
124
+ return conn.database ? `file:${conn.database}` : '';
125
+ }
126
+ }
@@ -18,6 +18,7 @@ import type {
18
18
  } from '$shared/types/db-client';
19
19
  import type { DbClientDriverAdapter, IndexDefinition, SchemaOpts, TableDefinition } from './types';
20
20
  import { debug } from '$shared/utils/logger';
21
+ import { buildConnectionUrl } from '../connection-url';
21
22
 
22
23
  interface MongoCommand {
23
24
  collection: string;
@@ -35,20 +36,7 @@ export class MongoDbAdapter implements DbClientDriverAdapter {
35
36
  private alive = false;
36
37
 
37
38
  async connect(conn: DbClientConnection, tunnelPort?: number): Promise<void> {
38
- const host = tunnelPort ? '127.0.0.1' : (conn.host ?? '127.0.0.1');
39
- const port = tunnelPort ?? conn.port ?? 27017;
40
- const user = conn.username ? encodeURIComponent(conn.username) : '';
41
- const pass = conn.password ? `:${encodeURIComponent(conn.password)}` : '';
42
- const auth = user ? `${user}${pass}@` : '';
43
- const dbPart = conn.database ? `/${encodeURIComponent(conn.database)}` : '';
44
-
45
- const params = new URLSearchParams();
46
- const optsAuthSource = typeof conn.options?.authSource === 'string' ? conn.options.authSource : null;
47
- if (user) {
48
- params.set('authSource', optsAuthSource ?? 'admin');
49
- }
50
- const qs = params.toString();
51
- const uri = `mongodb://${auth}${host}:${port}${dbPart}${qs ? `?${qs}` : ''}`;
39
+ const uri = buildConnectionUrl(conn, { tunnelPort });
52
40
 
53
41
  this.client = new MongoClient(uri, { serverSelectionTimeoutMS: 10_000 });
54
42
  await this.client.connect();
@@ -38,6 +38,7 @@ import {
38
38
  renderCreateTable
39
39
  } from './sql-builders';
40
40
  import { debug } from '$shared/utils/logger';
41
+ import { buildConnectionUrl } from '../connection-url';
41
42
 
42
43
  const Q = quoteMysql;
43
44
 
@@ -52,14 +53,7 @@ export class MysqlAdapter implements DbClientDriverAdapter {
52
53
  private configuredDb: string | null = null;
53
54
 
54
55
  async connect(conn: DbClientConnection, tunnelPort?: number): Promise<void> {
55
- const host = tunnelPort ? '127.0.0.1' : (conn.host ?? '127.0.0.1');
56
- const port = tunnelPort ?? conn.port ?? 3306;
57
- const user = encodeURIComponent(conn.username ?? '');
58
- const pass = conn.password ? `:${encodeURIComponent(conn.password)}` : '';
59
- const auth = user ? `${user}${pass}@` : '';
60
- const db = conn.database ? `/${encodeURIComponent(conn.database)}` : '';
61
-
62
- const url = `mysql://${auth}${host}:${port}${db}`;
56
+ const url = buildConnectionUrl(conn, { tunnelPort });
63
57
  this.sql = new SQL(url, BUN_SQL_POOL_OPTIONS);
64
58
  await this.sql.connect();
65
59
  this.configuredDb = conn.database || null;
@@ -37,6 +37,7 @@ import {
37
37
  renderCreateTable
38
38
  } from './sql-builders';
39
39
  import { debug } from '$shared/utils/logger';
40
+ import { buildConnectionUrl } from '../connection-url';
40
41
 
41
42
  const Q = quotePg;
42
43
  const PG_PLACEHOLDER = (i: number): string => `$${i + 1}`;
@@ -62,20 +63,16 @@ export class PostgresAdapter implements DbClientDriverAdapter {
62
63
  private tunnelPort: number | undefined = undefined;
63
64
  private ensureLock = Promise.resolve();
64
65
 
66
+ /**
67
+ * Bun's `SQL` wants the `postgres` scheme, and a connection with no database
68
+ * still has to land somewhere — everything else is the shared builder.
69
+ */
65
70
  private buildUrl(conn: DbClientConnection, tunnelPort?: number): string {
66
- const host = tunnelPort ? '127.0.0.1' : (conn.host ?? '127.0.0.1');
67
- const port = tunnelPort ?? conn.port ?? 5432;
68
- const user = encodeURIComponent(conn.username ?? '');
69
- const pass = conn.password ? `:${encodeURIComponent(conn.password)}` : '';
70
- const auth = user ? `${user}${pass}@` : '';
71
- const db = conn.database ? `/${encodeURIComponent(conn.database)}` : '/postgres';
72
-
73
- const params = new URLSearchParams();
74
- if (conn.sslMode && conn.sslMode !== 'disable') {
75
- params.set('sslmode', conn.sslMode);
76
- }
77
- const qs = params.toString();
78
- return `postgres://${auth}${host}:${port}${db}${qs ? `?${qs}` : ''}`;
71
+ return buildConnectionUrl(conn, {
72
+ tunnelPort,
73
+ scheme: 'postgres',
74
+ fallbackDatabase: 'postgres'
75
+ });
79
76
  }
80
77
 
81
78
  async connect(conn: DbClientConnection, tunnelPort?: number): Promise<void> {
@@ -21,6 +21,7 @@ import type {
21
21
  } from '$shared/types/db-client';
22
22
  import type { DbClientDriverAdapter } from './types';
23
23
  import { debug } from '$shared/utils/logger';
24
+ import { buildConnectionUrl } from '../connection-url';
24
25
 
25
26
  const SCAN_CAP = 1000;
26
27
 
@@ -31,12 +32,7 @@ export class RedisAdapter implements DbClientDriverAdapter {
31
32
  private alive = false;
32
33
 
33
34
  async connect(conn: DbClientConnection, tunnelPort?: number): Promise<void> {
34
- const host = tunnelPort ? '127.0.0.1' : (conn.host ?? '127.0.0.1');
35
- const port = tunnelPort ?? conn.port ?? 6379;
36
- const pass = conn.password ? `:${encodeURIComponent(conn.password)}@` : '';
37
- const dbIdx = conn.database && /^\d+$/.test(conn.database) ? `/${conn.database}` : '';
38
-
39
- const url = `redis://${pass}${host}:${port}${dbIdx}`;
35
+ const url = buildConnectionUrl(conn, { tunnelPort });
40
36
  this.client = new RedisClient(url);
41
37
  await this.client.connect();
42
38
  this.alive = true;