@everystack/server 0.4.3 → 0.4.4

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 (2) hide show
  1. package/package.json +2 -2
  2. package/src/db.ts +31 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@everystack/server",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Server runtime primitives for Lambda — event adapters, routing, SSR, image processing",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "Scalable Technology, Inc. <licensing@scalable.technology>",
@@ -151,8 +151,8 @@
151
151
  "sst": "4.13.1",
152
152
  "ts-jest": "29.4.9",
153
153
  "typescript": "5.9.3",
154
- "@everystack/cli": "0.4.12",
155
154
  "@everystack/auth": "0.4.0",
155
+ "@everystack/cli": "0.4.18",
156
156
  "@everystack/pg-tools": "0.4.0"
157
157
  },
158
158
  "scripts": {
package/src/db.ts CHANGED
@@ -96,6 +96,34 @@ export function getJwtSecret(): Uint8Array {
96
96
  throw new Error('JWT_SECRET not set — link an sst.Secret or set process.env.JWT_SECRET');
97
97
  }
98
98
 
99
+ /**
100
+ * The ssl option for a connection URL: an explicit `sslmode` query param wins
101
+ * (return no override — postgres.js honors the URL natively), otherwise default
102
+ * to 'require' (RDS). This is what lets ONE handler serve both venues: local
103
+ * Postgres appends `?sslmode=disable` to its URL, deployed stages carry no
104
+ * param and keep TLS. An unparseable URL fails toward TLS and lets the connect
105
+ * report the real error.
106
+ */
107
+ export function sslOptionForUrl(url: string): 'require' | undefined {
108
+ try {
109
+ return new URL(url).searchParams.has('sslmode') ? undefined : 'require';
110
+ } catch {
111
+ return 'require';
112
+ }
113
+ }
114
+
115
+ /** postgres.js options shared by every connection — ssl derived from the URL. */
116
+ function connectionOptions(url: string, maxConnections?: number): Parameters<typeof postgres>[1] {
117
+ const ssl = sslOptionForUrl(url);
118
+ return {
119
+ max: maxConnections ?? 1,
120
+ idle_timeout: 60,
121
+ connect_timeout: 5,
122
+ max_lifetime: 60 * 5,
123
+ ...(ssl ? { ssl } : {}),
124
+ };
125
+ }
126
+
99
127
  // Lazy singleton DB connection
100
128
  let dbInstance: ReturnType<typeof drizzle> | null = null;
101
129
  let sqlClient: ReturnType<typeof postgres> | null = null;
@@ -110,13 +138,7 @@ export function createDb<T extends Record<string, unknown>>(
110
138
  if (dbInstance) return { db: dbInstance, schema };
111
139
 
112
140
  const url = getDatabaseUrl();
113
- sqlClient = postgres(url, {
114
- max: options?.maxConnections ?? 1,
115
- idle_timeout: 60,
116
- connect_timeout: 5,
117
- max_lifetime: 60 * 5,
118
- ssl: 'require',
119
- });
141
+ sqlClient = postgres(url, connectionOptions(url, options?.maxConnections));
120
142
  dbInstance = drizzle(sqlClient, { schema });
121
143
  return { db: dbInstance, schema };
122
144
  }
@@ -242,13 +264,7 @@ export function createAdminDb<T extends Record<string, unknown>>(
242
264
  return createDb(schema, options);
243
265
  }
244
266
 
245
- adminSqlClient = postgres(adminUrl, {
246
- max: options?.maxConnections ?? 1,
247
- idle_timeout: 60,
248
- connect_timeout: 5,
249
- max_lifetime: 60 * 5,
250
- ssl: 'require',
251
- });
267
+ adminSqlClient = postgres(adminUrl, connectionOptions(adminUrl, options?.maxConnections));
252
268
  adminDbInstance = drizzle(adminSqlClient, { schema });
253
269
  return { db: adminDbInstance, schema };
254
270
  }
@@ -314,13 +330,7 @@ export function createReadDb<T extends Record<string, unknown>>(
314
330
  const url = getReadDatabaseUrl();
315
331
  if (!url) return createDb(schema, options);
316
332
 
317
- readSqlClient = postgres(url, {
318
- max: options?.maxConnections ?? 1,
319
- idle_timeout: 60,
320
- connect_timeout: 5,
321
- max_lifetime: 60 * 5,
322
- ssl: 'require',
323
- });
333
+ readSqlClient = postgres(url, connectionOptions(url, options?.maxConnections));
324
334
  readDbInstance = drizzle(readSqlClient, { schema });
325
335
  return { db: readDbInstance, schema };
326
336
  }