@agentuity/postgres 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tls.ts ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * TLS/SSL utilities for PostgreSQL connections.
3
+ *
4
+ * @see https://github.com/agentuity/sdk/issues/921
5
+ */
6
+
7
+ /**
8
+ * Injects `sslmode=require` into a PostgreSQL connection URL when TLS is
9
+ * requested but the URL doesn't already contain an `sslmode` parameter.
10
+ *
11
+ * Bun.SQL requires `sslmode` in the connection URL to trigger PostgreSQL
12
+ * TLS negotiation (SSLRequest). The `tls` option alone configures *how*
13
+ * TLS works but doesn't initiate the protocol handshake.
14
+ *
15
+ * If `sslmode` is already present in the URL (e.g. `sslmode=disable`,
16
+ * `sslmode=prefer`), the existing value is preserved — the user's
17
+ * explicit setting always takes precedence.
18
+ *
19
+ * @param url - PostgreSQL connection URL (may be undefined)
20
+ * @param tls - TLS config value from the caller (truthy = wants TLS)
21
+ * @returns The URL with `sslmode=require` injected, or the original URL
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * // Injects sslmode=require
26
+ * injectSslMode('postgresql://host/db', true);
27
+ * // => 'postgresql://host/db?sslmode=require'
28
+ *
29
+ * // Preserves existing sslmode
30
+ * injectSslMode('postgresql://host/db?sslmode=prefer', true);
31
+ * // => 'postgresql://host/db?sslmode=prefer'
32
+ *
33
+ * // No-op when tls is falsy
34
+ * injectSslMode('postgresql://host/db', false);
35
+ * // => 'postgresql://host/db'
36
+ * ```
37
+ */
38
+ export function injectSslMode(url: string | undefined, tls: unknown): string | undefined {
39
+ if (!url || tls === undefined || tls === false) {
40
+ return url;
41
+ }
42
+
43
+ try {
44
+ const parsed = new URL(url);
45
+ if (!parsed.searchParams.has('sslmode')) {
46
+ parsed.searchParams.set('sslmode', 'require');
47
+ return parsed.toString();
48
+ }
49
+ } catch {
50
+ // Not a parseable URL — return as-is
51
+ }
52
+
53
+ return url;
54
+ }
package/src/types.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type pg from 'pg';
2
+
1
3
  /**
2
4
  * TLS configuration options for PostgreSQL connections.
3
5
  */
@@ -279,66 +281,14 @@ export interface PoolSSLConfig {
279
281
  * Configuration options for PostgresPool.
280
282
  * Extends standard pg.Pool options with reconnection support.
281
283
  */
282
- export interface PoolConfig {
283
- /**
284
- * PostgreSQL connection URL.
285
- * If not provided, uses `process.env.DATABASE_URL`.
286
- */
287
- connectionString?: string;
288
-
289
- /**
290
- * Database hostname.
291
- */
292
- host?: string;
293
-
294
- /**
295
- * Database port.
296
- * @default 5432
297
- */
298
- port?: number;
299
-
300
- /**
301
- * Database username.
302
- */
303
- user?: string;
304
-
305
- /**
306
- * Database password.
307
- */
308
- password?: string;
309
-
310
- /**
311
- * Database name.
312
- */
313
- database?: string;
314
-
315
- /**
316
- * Maximum number of connections in the pool.
317
- * @default 10
318
- */
319
- max?: number;
320
-
321
- /**
322
- * Number of milliseconds a client must sit idle before being disconnected.
323
- * Set to 0 to disable idle timeout.
324
- * @default 10000
325
- */
326
- idleTimeoutMillis?: number;
327
-
328
- /**
329
- * Number of milliseconds to wait when connecting a new client before timing out.
330
- * Set to 0 to disable connection timeout.
331
- * @default 0
332
- */
333
- connectionTimeoutMillis?: number;
334
-
284
+ export interface PoolConfig extends pg.PoolConfig {
335
285
  /**
336
286
  * SSL configuration.
337
287
  * Set to `true` to enable SSL with default settings.
338
288
  * Set to an object to configure SSL options.
339
289
  * Set to `false` or omit to disable SSL.
340
290
  */
341
- ssl?: boolean | PoolSSLConfig;
291
+ ssl?: pg.PoolConfig['ssl'] | PoolSSLConfig;
342
292
 
343
293
  /**
344
294
  * Reconnection configuration.