@vrplatform/kysely 1.3.45-stage.1262 → 1.3.45-stage.1279

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.
@@ -0,0 +1,12 @@
1
+ import type { Kysely } from './index';
2
+ import type { JsonObject } from './v1.generated';
3
+ /**
4
+ * Passes secrets between the API and Trigger.dev tasks through the tenant's
5
+ * database, so connect credentials never enter Trigger.dev payloads or outputs.
6
+ * Rows expire after one hour, the lifetime of a reused connect run. Reads do not
7
+ * consume the row: a retried or reused run must still find it.
8
+ */
9
+ export declare function useConnectHandoff(kysely: Kysely, tenantId: string): {
10
+ put(appId: string, value: JsonObject): Promise<string>;
11
+ get(ref: string, appId: string): Promise<JsonObject | undefined>;
12
+ };
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useConnectHandoff = useConnectHandoff;
4
+ const kysely_1 = require("kysely");
5
+ // Rows never belong to a user, so no authenticated user can claim one through
6
+ // the OAuth state flow that shares the table.
7
+ const handoffUserId = '00000000-0000-0000-0000-000000000000';
8
+ const handoffRefPattern = /^handoff_[A-Za-z0-9_-]{22}$/;
9
+ async function hashRef(ref) {
10
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(ref));
11
+ return [...new Uint8Array(digest)]
12
+ .map((byte) => byte.toString(16).padStart(2, '0'))
13
+ .join('');
14
+ }
15
+ /**
16
+ * Passes secrets between the API and Trigger.dev tasks through the tenant's
17
+ * database, so connect credentials never enter Trigger.dev payloads or outputs.
18
+ * Rows expire after one hour, the lifetime of a reused connect run. Reads do not
19
+ * consume the row: a retried or reused run must still find it.
20
+ */
21
+ function useConnectHandoff(kysely, tenantId) {
22
+ return {
23
+ async put(appId, value) {
24
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
25
+ const ref = `handoff_${btoa(String.fromCharCode(...bytes))
26
+ .replaceAll('+', '-')
27
+ .replaceAll('/', '_')
28
+ .replaceAll('=', '')}`;
29
+ await kysely
30
+ .deleteFrom('public.connectionOauthState')
31
+ .where('stateHash', 'in', kysely
32
+ .selectFrom('public.connectionOauthState')
33
+ .select('stateHash')
34
+ .where('expiresAt', '<=', (0, kysely_1.sql) `clock_timestamp()`)
35
+ .orderBy('expiresAt')
36
+ .limit(100)
37
+ .forUpdate()
38
+ .skipLocked())
39
+ .execute();
40
+ // Transient authentication coordination is excluded from business audit:
41
+ // references, setup parameters and credentials must never be audited.
42
+ await kysely
43
+ .insertInto('public.connectionOauthState')
44
+ .values({
45
+ stateHash: await hashRef(ref),
46
+ tenantId,
47
+ userId: handoffUserId,
48
+ appId,
49
+ connectionId: null,
50
+ params: value,
51
+ redirectUri: '',
52
+ expiresAt: (0, kysely_1.sql) `clock_timestamp() + interval '1 hour'`,
53
+ })
54
+ .execute();
55
+ return ref;
56
+ },
57
+ async get(ref, appId) {
58
+ if (!handoffRefPattern.test(ref))
59
+ return undefined;
60
+ const row = await kysely
61
+ .selectFrom('public.connectionOauthState')
62
+ .select('params')
63
+ .where('stateHash', '=', await hashRef(ref))
64
+ .where('tenantId', '=', tenantId)
65
+ .where('appId', '=', appId)
66
+ .where('userId', '=', handoffUserId)
67
+ .where('expiresAt', '>', (0, kysely_1.sql) `clock_timestamp()`)
68
+ .executeTakeFirst();
69
+ return row?.params;
70
+ },
71
+ };
72
+ }
73
+ //# sourceMappingURL=connect-handoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect-handoff.js","sourceRoot":"src/","sources":["connect-handoff.ts"],"names":[],"mappings":";;AAyBA,8CAsDC;AA/ED,mCAA6B;AAI7B,8EAA8E;AAC9E,8CAA8C;AAC9C,MAAM,aAAa,GAAG,sCAAsC,CAAC;AAC7D,MAAM,iBAAiB,GAAG,6BAA6B,CAAC;AAExD,KAAK,UAAU,OAAO,CAAC,GAAW;IAChC,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CACvC,SAAS,EACT,IAAI,WAA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,CAAC,GAAG,CAAC;gBAC7B,QAAQ;gBACR,MAAM,EAAE,aAAa;gBACrB,KAAK;gBACL,YAAY,EAAE,IAAI;gBAClB,MAAM,EAAE,KAAK;gBACb,WAAW,EAAE,EAAE;gBACf,SAAS,EAAE,IAAA,YAAG,EAAA,uCAAuC;aACtD,CAAC;iBACD,OAAO,EAAE,CAAC;YACb,OAAO,GAAG,CAAC;QACb,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAa;YAClC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC;gBAAE,OAAO,SAAS,CAAC;YACnD,MAAM,GAAG,GAAG,MAAM,MAAM;iBACrB,UAAU,CAAC,6BAA6B,CAAC;iBACzC,MAAM,CAAC,QAAQ,CAAC;iBAChB,KAAK,CAAC,WAAW,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;iBAC3C,KAAK,CAAC,UAAU,EAAE,GAAG,EAAE,QAAQ,CAAC;iBAChC,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;iBAC1B,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,aAAa,CAAC;iBACnC,KAAK,CAAC,WAAW,EAAE,GAAG,EAAE,IAAA,YAAG,EAAQ,mBAAmB,CAAC;iBACvD,gBAAgB,EAAE,CAAC;YACtB,OAAO,GAAG,EAAE,MAAM,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { sql } from 'kysely';\nimport type { Kysely } from './index';\nimport type { JsonObject } from './v1.generated';\n\n// Rows never belong to a user, so no authenticated user can claim one through\n// the OAuth state flow that shares the table.\nconst handoffUserId = '00000000-0000-0000-0000-000000000000';\nconst handoffRefPattern = /^handoff_[A-Za-z0-9_-]{22}$/;\n\nasync function hashRef(ref: string) {\n const digest = await crypto.subtle.digest(\n 'SHA-256',\n new TextEncoder().encode(ref)\n );\n return [...new Uint8Array(digest)]\n .map((byte) => byte.toString(16).padStart(2, '0'))\n .join('');\n}\n\n/**\n * Passes secrets between the API and Trigger.dev tasks through the tenant's\n * database, so connect credentials never enter Trigger.dev payloads or outputs.\n * Rows expire after one hour, the lifetime of a reused connect run. Reads do not\n * consume the row: a retried or reused run must still find it.\n */\nexport function useConnectHandoff(kysely: Kysely, tenantId: string) {\n return {\n async put(appId: string, value: JsonObject) {\n const bytes = crypto.getRandomValues(new Uint8Array(16));\n const ref = `handoff_${btoa(String.fromCharCode(...bytes))\n .replaceAll('+', '-')\n .replaceAll('/', '_')\n .replaceAll('=', '')}`;\n await kysely\n .deleteFrom('public.connectionOauthState')\n .where(\n 'stateHash',\n 'in',\n kysely\n .selectFrom('public.connectionOauthState')\n .select('stateHash')\n .where('expiresAt', '<=', sql<string>`clock_timestamp()`)\n .orderBy('expiresAt')\n .limit(100)\n .forUpdate()\n .skipLocked()\n )\n .execute();\n // Transient authentication coordination is excluded from business audit:\n // references, setup parameters and credentials must never be audited.\n await kysely\n .insertInto('public.connectionOauthState')\n .values({\n stateHash: await hashRef(ref),\n tenantId,\n userId: handoffUserId,\n appId,\n connectionId: null,\n params: value,\n redirectUri: '',\n expiresAt: sql`clock_timestamp() + interval '1 hour'`,\n })\n .execute();\n return ref;\n },\n async get(ref: string, appId: string) {\n if (!handoffRefPattern.test(ref)) return undefined;\n const row = await kysely\n .selectFrom('public.connectionOauthState')\n .select('params')\n .where('stateHash', '=', await hashRef(ref))\n .where('tenantId', '=', tenantId)\n .where('appId', '=', appId)\n .where('userId', '=', handoffUserId)\n .where('expiresAt', '>', sql<string>`clock_timestamp()`)\n .executeTakeFirst();\n return row?.params;\n },\n };\n}\n"]}
@@ -2,6 +2,7 @@ import type { Kysely as K, LogConfig, Transaction as T } from 'kysely';
2
2
  import postgres, { type Sql } from 'postgres';
3
3
  import { type ControlPlaneDB } from './control-plane';
4
4
  export type { Insertable, LogEvent } from 'kysely';
5
+ export * from './connect-handoff';
5
6
  export * from './error';
6
7
  export * from './isomorphic';
7
8
  export * from './postgres-error';
@@ -35,6 +35,7 @@ const control_plane_1 = require("./control-plane");
35
35
  const error_1 = require("./error");
36
36
  const plugins_1 = require("./plugins");
37
37
  const postgres_error_1 = require("./postgres-error");
38
+ __exportStar(require("./connect-handoff"), exports);
38
39
  __exportStar(require("./error"), exports);
39
40
  __exportStar(require("./isomorphic"), exports);
40
41
  __exportStar(require("./postgres-error"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAsNA,4DAOC;AAsLD,8BA0DC;AAID,kCA8CC;AAED,4CAoCC;AAED,wCAMC;AAED,kEAYC;AAED,sDAKC;AAED,gEAQC;AAED,sDAUC;AAxlBD,6DAA8C;AAC9C,yDAAgD;AAQhD,mCAAyC;AACzC,2DAI4B;AAC5B,wDAA8C;AAC9C,mDAA8E;AAC9E,mCAAsC;AACtC,uCAAsC;AACtC,qDAAmD;AAGnD,0CAAwB;AACxB,+CAA6B;AAC7B,mDAAiC;AACjC,0CAAwB;AACxB,6CAA2B;AAC3B,gDAA8B;AAE9B,6CAAkD;AA2ClD,MAAM,iBAAiB,GAAG;IACxB,mBAAmB;IACnB,iBAAiB;IACjB,uCAAuC;IACvC,oBAAoB;IACpB,mDAAmD;CACpD,CAAC;AACF,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC;;;;;;;;GAQG;AACH,MAAM,cAAe,SAAQ,GAAqB;IACvC,GAAG,CAAC,SAAiB,EAAE,KAAe;QAC7C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC;YAC9C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAM,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACrC,CAA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qB,CAC1B,MAAM,gBAAgB,CAAC,gBAAgB,EAAE,GAAG,CAAC,EAC7C,GAAG,CACJ,CAAC;AACJ,CAAC;AAEM,KAAK,UAAU,qBAAqB,CAAC,SAAS,GAAG,IAAI;IAC1D,MAAM,OAAO,CAAC,IAAI,CAAC;QACjB,OAAO,CAAC,GAAG,CACT,YAAY,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACtB,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,SAAS,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAC7D,CACF;QACD,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC,CAAC;KACxD,CAAC,CAAC;IACH,YAAY,GAAG,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import { backOff } from 'exponential-backoff';\nimport { Pool } from '@neondatabase/serverless';\nimport type {\n DatabaseConnection,\n Driver,\n Kysely as K,\n LogConfig,\n Transaction as T,\n} from 'kysely';\nimport { PostgresDialect } from 'kysely';\nimport {\n PostgresJSDialect,\n type PostgresJSDialectConfig,\n PostgresJSDriver,\n} from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\nimport { type ControlPlaneDB, withControlPlaneSchema } from './control-plane';\nimport { KyselyError } from './error';\nimport { getKysely } from './plugins';\nimport { isPostgresError } from './postgres-error';\n\nexport type { Insertable, LogEvent } from 'kysely';\nexport * from './error';\nexport * from './isomorphic';\nexport * from './postgres-error';\nexport * from './query';\nexport * from './regional';\nexport * from './transaction';\n\nimport { parseDuration } from '@vrplatform/utils';\nimport type { TenantDataRegion } from './regional';\nimport type { DB } from './v1.generated';\n\n/** The database a Worker reaches through its Hyperdrive binding. */\ntype HyperdriveDatabase = 'controlPlane' | 'regional';\n\ntype KyselyOpts = {\n dataRegion?: TenantDataRegion;\n hyperdrive?: HyperdriveDatabase;\n /** Called after every connection reservation, including the startup probe. */\n onReserve?: (timing: {\n durationMs: number;\n startedAtEpochMs: number;\n }) => void;\n onRetryDiagnostic?: (diagnostic: PostgresRetryDiagnostic) => void;\n repositoryName?: string;\n log?: true | LogConfig;\n /**\n * Scripts and repairs (PRO-19037). Probes an idle TCP connection after 10 s.\n * A query, commit, begin, rollback or savepoint with no answer for an hour,\n * longer than any statement timeout a script sets, fails the run and closes\n * the Postgres client that issued it. Backstop for a connection the kernel\n * keeps open while the peer never answers.\n */\n script?: true;\n};\n\ntype PostgresRetryDiagnostic = {\n attemptCount: number;\n dataRegion?: TenantDataRegion;\n errorClass: string;\n operationKind: 'acquisition' | 'mutation' | 'select' | 'transaction';\n outcome: 'exhausted' | 'notRetryable' | 'retrying';\n repositoryName?: string;\n};\n\ntype PgOpt = {\n forceDisableSSL?: boolean;\n probe?: true;\n retries?: false | number;\n} & Pick<KyselyOpts, 'hyperdrive' | 'onReserve' | 'script'>;\n\nconst retryableMessages = [\n 'CONNECTION_CLOSED',\n 'CONNECT_TIMEOUT',\n 'Idle connection closed by Hyperdrive.',\n 'server_login_retry',\n 'Timed out while creating a new server connection.',\n];\nconst postgresRetryAttempts = 3;\n\n/**\n * Parameter types PostgreSQL described per statement signature (PRO-18871).\n * Postgres.js describes a statement in a round trip of its own before it can\n * execute it when a parameter type is unknown, and a Worker request always\n * starts on new connections. The patched driver reads and fills this map\n * through its `statement_types` option and then parses, describes, binds and\n * executes a known statement in one round trip. It forgets a statement's\n * types when the statement fails, so a schema change is described again.\n */\nclass StatementTypes extends Map<string, number[]> {\n override set(signature: string, types: number[]) {\n if (!this.has(signature) && this.size >= 2000) {\n this.delete(this.keys().next().value!);\n }\n return super.set(signature, types);\n }\n}\n\n/** Kept for the isolate's lifetime, one map per Hyperdrive database. */\nconst hyperdriveStatementTypes: Record<HyperdriveDatabase, StatementTypes> = {\n controlPlane: new StatementTypes(),\n regional: new StatementTypes(),\n};\n\nfunction isRetryableConnectionError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return retryableMessages.some((retryable) => message.includes(retryable));\n}\n\nfunction isConnectionReset(error: unknown) {\n return (\n error instanceof Error && 'code' in error && error.code === 'ECONNRESET'\n );\n}\n\nfunction isRetryableConnectionAcquisitionError(error: unknown) {\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n (error instanceof Error &&\n (error.message === 'connection attempt failed' ||\n ('code' in error && error.code === 'CONNECTION_DESTROYED')))\n );\n}\n\nfunction isQueryRetryDiagnosticError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n message === 'Egress connection severed outside of Hyperdrive' ||\n message === 'Network connection lost.' ||\n message === 'Node shutting down' ||\n message === 'Node shutting down.' ||\n message === 'shutting down'\n );\n}\n\nasync function retryPostgres<T>(\n operation: () => Promise<T>,\n retry: (error: unknown, attemptNumber: number) => boolean | Promise<boolean>,\n diagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n }\n) {\n let attemptCount = 0;\n let retried = false;\n let lastError: unknown;\n const emitDiagnostic = (outcome: PostgresRetryDiagnostic['outcome']) => {\n if (!diagnostic) return;\n diagnostic.emit({\n attemptCount,\n dataRegion: diagnostic.dataRegion,\n errorClass:\n lastError instanceof Error\n ? 'code' in lastError && typeof lastError.code === 'string'\n ? lastError.code\n : lastError.constructor.name\n : 'unknown',\n operationKind: diagnostic.operationKind,\n outcome,\n repositoryName: diagnostic.repositoryName,\n });\n };\n try {\n return await backOff(\n async () => {\n attemptCount++;\n return await operation();\n },\n {\n numOfAttempts: postgresRetryAttempts,\n startingDelay: parseDuration('1s'),\n maxDelay: parseDuration('5s'),\n // Scheduled chunk fan-outs open many connections at once; full jitter\n // keeps their retries from reconnecting in lockstep.\n jitter: 'full',\n async retry(error, attemptNumber) {\n lastError = error;\n const shouldRetry =\n attemptNumber < postgresRetryAttempts &&\n (await retry(error, attemptNumber));\n if (shouldRetry && diagnostic) {\n retried = true;\n emitDiagnostic('retrying');\n }\n return shouldRetry;\n },\n }\n );\n } catch (error) {\n lastError = error;\n if (diagnostic?.shouldEmit(error)) {\n emitDiagnostic(\n retried && attemptCount === postgresRetryAttempts\n ? 'exhausted'\n : 'notRetryable'\n );\n }\n throw error;\n }\n}\n\n/**\n * Replays a complete database operation after a PostgreSQL deadlock (40P01)\n * or lock timeout (55P03), using the same three-attempt jittered budget as\n * connection retries. The operation must own its transaction and must not\n * contain external side effects such as partner API writes.\n */\nexport function retryPostgresLockFailure<T>(operation: () => Promise<T>) {\n return retryPostgres(\n operation,\n (error) =>\n isPostgresError(error, { code: '40P01' }) ||\n isPostgresError(error, { code: '55P03' })\n );\n}\n\nclass RetryingPostgresJSDialect extends PostgresJSDialect {\n constructor(\n private readonly config: PostgresJSDialectConfig,\n private readonly retryDiagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'operationKind' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n },\n private readonly closeOnScriptTimeout?: () => void\n ) {\n super(config);\n }\n\n /** Ends the run when a script's database operation gets no answer (PRO-19037). */\n private async withScriptDeadline<T>(operation: () => Promise<T>) {\n if (!this.closeOnScriptTimeout) return await operation();\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n operation(),\n new Promise<never>((_, reject) => {\n timer = setTimeout(() => {\n console.error('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n });\n this.closeOnScriptTimeout?.();\n reject(\n new KyselyError('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n })\n );\n }, scriptQueryTimeoutMs);\n }),\n ]);\n } finally {\n clearTimeout(timer);\n }\n }\n\n override createDriver(): Driver {\n const driver = new PostgresJSDriver(this.config);\n const connections = new WeakMap<\n DatabaseConnection,\n { connection: DatabaseConnection; inTransaction: boolean }\n >();\n const getConnection = (connection: DatabaseConnection) => {\n const state = connections.get(connection);\n if (!state) throw new Error('Postgres connection state is unavailable');\n return state;\n };\n\n return {\n init: () => driver.init(),\n acquireConnection: async () => {\n const state = {\n connection: await driver.acquireConnection(),\n inTransaction: false,\n };\n const connection: DatabaseConnection = {\n executeQuery: (compiledQuery) =>\n this.withScriptDeadline(() => {\n const operationKind = state.inTransaction\n ? 'transaction'\n : compiledQuery.query.kind === 'SelectQueryNode'\n ? 'select'\n : 'mutation';\n return retryPostgres(\n () => state.connection.executeQuery(compiledQuery),\n async (error) => {\n const message =\n error instanceof Error ? error.message : String(error);\n if (\n state.inTransaction ||\n (!isRetryableConnectionError(error) &&\n (compiledQuery.query.kind !== 'SelectQueryNode' ||\n (!isConnectionReset(error) &&\n message !==\n 'Egress connection severed outside of Hyperdrive' &&\n message !== 'Network connection lost.' &&\n message !== 'Node shutting down' &&\n message !== 'Node shutting down.' &&\n message !== 'shutting down')))\n ) {\n return false;\n }\n\n // Postgres.js already ejects a severed reserved connection.\n // Acquire a new reservation before replaying the safe read.\n state.connection = await driver.acquireConnection();\n return true;\n },\n this.retryDiagnostic\n ? {\n ...this.retryDiagnostic,\n operationKind,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined\n );\n }),\n streamQuery: (compiledQuery, chunkSize) => {\n if (chunkSize === undefined) {\n throw new Error('Postgres stream chunk size is required');\n }\n return state.connection.streamQuery(compiledQuery, chunkSize);\n },\n };\n connections.set(connection, state);\n return connection;\n },\n beginTransaction: async (connection, settings) => {\n const state = getConnection(connection);\n state.inTransaction = true;\n try {\n await this.withScriptDeadline(() =>\n driver.beginTransaction(state.connection, settings)\n );\n } catch (error) {\n state.inTransaction = false;\n throw error;\n }\n },\n commitTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.commitTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n rollbackTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.rollbackTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n savepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.savepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n rollbackToSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.rollbackToSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.releaseSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseConnection: async (connection) => {\n await driver.releaseConnection(getConnection(connection).connection);\n connections.delete(connection);\n },\n destroy: () => driver.destroy(),\n };\n }\n}\n\nexport type Kysely = K<DB>;\nexport type ControlPlaneKysely = K<ControlPlaneDB>;\nexport type ControlPlaneTransaction = T<ControlPlaneDB>;\nexport function useKysely<Database = DB>(postgres: Sql, options?: KyselyOpts) {\n console.debug('Creating Kysely instance');\n const emitRetryDiagnostic =\n options?.onRetryDiagnostic ??\n (options?.repositoryName\n ? (diagnostic: PostgresRetryDiagnostic) =>\n console.warn('Postgres operation retry diagnostic', diagnostic)\n : undefined);\n const reserveConnection = postgres.reserve.bind(postgres);\n postgres.reserve = async () => {\n const connection = await retryPostgres(\n reserveConnection,\n isRetryableConnectionAcquisitionError,\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n operationKind: 'acquisition',\n repositoryName: options?.repositoryName,\n shouldEmit: isRetryableConnectionAcquisitionError,\n }\n : undefined\n );\n if (options?.hyperdrive) {\n const unsafe = connection.unsafe;\n connection.unsafe = (query, parameters, queryOptions) =>\n unsafe(query, parameters, { ...queryOptions, prepare: true });\n }\n return connection;\n };\n const kysely = getKysely<Database>(\n new RetryingPostgresJSDialect(\n { postgres },\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n repositoryName: options?.repositoryName,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined,\n options?.script\n ? () => void postgres.end({ timeout: 0 }).catch(() => undefined)\n : undefined\n ),\n {\n repositoryName: options?.repositoryName,\n log: options?.log,\n }\n );\n kysely.destroy = async () => {\n console.debug('Closing Kysely connection');\n await postgres.end({ timeout: 5 }).catch(() => undefined);\n const index = _connections.indexOf(postgres);\n if (index !== -1) _connections.splice(index, 1);\n };\n\n return kysely;\n}\n\nconst scriptQueryTimeoutMs = parseDuration('1h');\nlet _connections: postgres.Sql[] = [];\nexport function usePostgres(\n connectionString: string,\n opt?: Pick<PgOpt, 'forceDisableSSL' | 'hyperdrive' | 'onReserve' | 'script'>\n) {\n const ssl = opt?.forceDisableSSL !== true && !opt?.hyperdrive;\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n target_session_attrs: 'read-write',\n // Named prepared statements: PRO-17965 (Hyperdrive and Crunchy PgBouncer).\n prepare: true,\n ...(opt?.hyperdrive\n ? {\n max: 5,\n // Retire idle clients before Hyperdrive can close them asynchronously.\n idle_timeout: 10,\n statement_types: hyperdriveStatementTypes[opt.hyperdrive],\n }\n : {\n connect_timeout: 10,\n idle_timeout: 60,\n ...(opt?.script ? { keep_alive: 10 } : {}),\n }),\n });\n if (opt?.onReserve) {\n const { onReserve } = opt;\n const reserve = sql.reserve.bind(sql);\n // Innermost wrapper: it times each open or reuse of a connection, the\n // startup probe too, and is not part of the retry loop (PRO-18952).\n sql.reserve = async () => {\n const startedAtEpochMs = Date.now();\n const startedAt = performance.now();\n try {\n return await reserve();\n } finally {\n onReserve({\n durationMs: performance.now() - startedAt,\n startedAtEpochMs,\n });\n }\n };\n }\n // statement_timeout/lock_timeout are role-level settings on the cluster\n // (ALTER ROLE application SET ...) so they cover every pooled\n // connection; per-session SETs here only ever applied to one of them\n _connections.push(sql);\n return sql;\n}\n\nexport async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {\n const delayMs = 2500;\n const probe = opt?.probe === true;\n const retries = probe ? (opt?.retries === false ? 0 : opt?.retries || 3) : 1;\n const url = new URL(connectionString);\n\n console.debug(\n `Creating Postgres connection with probe=${probe}, ${retries} attempts, ssl=${opt?.forceDisableSSL !== true && !opt?.hyperdrive}, and port=${url.port}`\n );\n if (_connections.length > 0)\n console.warn(`We already have ${_connections.length} Postgres connections`);\n\n for (let attempt = 1; attempt <= retries; attempt++) {\n let sql: Sql | undefined;\n try {\n sql = usePostgres(connectionString, opt);\n if (probe) {\n // Opening a connection already checks that the session is writable\n // and loads the array types; a probe query would add a round trip.\n (await sql.reserve()).release();\n }\n return sql;\n } catch (err) {\n if (sql) {\n await sql.end({ timeout: 0 }).catch(() => undefined);\n _connections.splice(_connections.indexOf(sql), 1);\n }\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n console.error('error', err);\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new KyselyError('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n const sql = await useAsyncPostgres(connectionString, opt);\n return useKysely(sql, opt);\n}\n\nexport async function useAsyncNeonWebSocketKysely(\n connectionString: string,\n opt?: Pick<PgOpt, 'probe'> & KyselyOpts\n) {\n const pool = new Pool({ connectionString, max: 5 });\n const kysely = getKysely<DB>(new PostgresDialect({ pool }), opt ?? {});\n kysely.destroy = async () => {\n console.debug('Closing Neon WebSocket Kysely connection');\n await pool.end();\n };\n if (opt?.probe) await pool.query('select 1');\n return kysely;\n}\n\nexport function useControlPlaneKysely(postgres: Sql, options?: KyselyOpts) {\n return withControlPlaneSchema(\n useKysely<ControlPlaneDB>(postgres, options),\n 'public'\n );\n}\n\nexport async function useAsyncControlPlaneKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n return useControlPlaneKysely(\n await useAsyncPostgres(connectionString, opt),\n opt\n );\n}\n\nexport async function forceCloseAllPostgres(timeoutMs = 5000) {\n await Promise.race([\n Promise.all(\n _connections.map((pg) =>\n pg.end({ timeout: timeoutMs / 1000 }).catch(() => undefined)\n )\n ),\n new Promise((res) => setTimeout(res, timeoutMs + 1000)),\n ]);\n _connections = [];\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAuNA,4DAOC;AAsLD,8BA0DC;AAID,kCA8CC;AAED,4CAoCC;AAED,wCAMC;AAED,kEAYC;AAED,sDAKC;AAED,gEAQC;AAED,sDAUC;AAzlBD,6DAA8C;AAC9C,yDAAgD;AAQhD,mCAAyC;AACzC,2DAI4B;AAC5B,wDAA8C;AAC9C,mDAA8E;AAC9E,mCAAsC;AACtC,uCAAsC;AACtC,qDAAmD;AAGnD,oDAAkC;AAClC,0CAAwB;AACxB,+CAA6B;AAC7B,mDAAiC;AACjC,0CAAwB;AACxB,6CAA2B;AAC3B,gDAA8B;AAE9B,6CAAkD;AA2ClD,MAAM,iBAAiB,GAAG;IACxB,mBAAmB;IACnB,iBAAiB;IACjB,uCAAuC;IACvC,oBAAoB;IACpB,mDAAmD;CACpD,CAAC;AACF,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC;;;;;;;;GAQG;AACH,MAAM,cAAe,SAAQ,GAAqB;IACvC,GAAG,CAAC,SAAiB,EAAE,KAAe;QAC7C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAAI,CAAC,IAAI,IAAI,IAAI,EAAE,CAAC;YAC9C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAM,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,KAAK,CAAC,GAAG,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACrC,CAAC;CACF;AAED,wEAAwE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AAgB,CAAC,gBAAgB,EAAE,GAAG,CAAC,EAC7C,GAAG,CACJ,CAAC;AACJ,CAAC;AAEM,KAAK,UAAU,qBAAqB,CAAC,SAAS,GAAG,IAAI;IAC1D,MAAM,OAAO,CAAC,IAAI,CAAC;QACjB,OAAO,CAAC,GAAG,CACT,YAAY,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACtB,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,SAAS,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAC7D,CACF;QACD,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC,CAAC;KACxD,CAAC,CAAC;IACH,YAAY,GAAG,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import { backOff } from 'exponential-backoff';\nimport { Pool } from '@neondatabase/serverless';\nimport type {\n DatabaseConnection,\n Driver,\n Kysely as K,\n LogConfig,\n Transaction as T,\n} from 'kysely';\nimport { PostgresDialect } from 'kysely';\nimport {\n PostgresJSDialect,\n type PostgresJSDialectConfig,\n PostgresJSDriver,\n} from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\nimport { type ControlPlaneDB, withControlPlaneSchema } from './control-plane';\nimport { KyselyError } from './error';\nimport { getKysely } from './plugins';\nimport { isPostgresError } from './postgres-error';\n\nexport type { Insertable, LogEvent } from 'kysely';\nexport * from './connect-handoff';\nexport * from './error';\nexport * from './isomorphic';\nexport * from './postgres-error';\nexport * from './query';\nexport * from './regional';\nexport * from './transaction';\n\nimport { parseDuration } from '@vrplatform/utils';\nimport type { TenantDataRegion } from './regional';\nimport type { DB } from './v1.generated';\n\n/** The database a Worker reaches through its Hyperdrive binding. */\ntype HyperdriveDatabase = 'controlPlane' | 'regional';\n\ntype KyselyOpts = {\n dataRegion?: TenantDataRegion;\n hyperdrive?: HyperdriveDatabase;\n /** Called after every connection reservation, including the startup probe. */\n onReserve?: (timing: {\n durationMs: number;\n startedAtEpochMs: number;\n }) => void;\n onRetryDiagnostic?: (diagnostic: PostgresRetryDiagnostic) => void;\n repositoryName?: string;\n log?: true | LogConfig;\n /**\n * Scripts and repairs (PRO-19037). Probes an idle TCP connection after 10 s.\n * A query, commit, begin, rollback or savepoint with no answer for an hour,\n * longer than any statement timeout a script sets, fails the run and closes\n * the Postgres client that issued it. Backstop for a connection the kernel\n * keeps open while the peer never answers.\n */\n script?: true;\n};\n\ntype PostgresRetryDiagnostic = {\n attemptCount: number;\n dataRegion?: TenantDataRegion;\n errorClass: string;\n operationKind: 'acquisition' | 'mutation' | 'select' | 'transaction';\n outcome: 'exhausted' | 'notRetryable' | 'retrying';\n repositoryName?: string;\n};\n\ntype PgOpt = {\n forceDisableSSL?: boolean;\n probe?: true;\n retries?: false | number;\n} & Pick<KyselyOpts, 'hyperdrive' | 'onReserve' | 'script'>;\n\nconst retryableMessages = [\n 'CONNECTION_CLOSED',\n 'CONNECT_TIMEOUT',\n 'Idle connection closed by Hyperdrive.',\n 'server_login_retry',\n 'Timed out while creating a new server connection.',\n];\nconst postgresRetryAttempts = 3;\n\n/**\n * Parameter types PostgreSQL described per statement signature (PRO-18871).\n * Postgres.js describes a statement in a round trip of its own before it can\n * execute it when a parameter type is unknown, and a Worker request always\n * starts on new connections. The patched driver reads and fills this map\n * through its `statement_types` option and then parses, describes, binds and\n * executes a known statement in one round trip. It forgets a statement's\n * types when the statement fails, so a schema change is described again.\n */\nclass StatementTypes extends Map<string, number[]> {\n override set(signature: string, types: number[]) {\n if (!this.has(signature) && this.size >= 2000) {\n this.delete(this.keys().next().value!);\n }\n return super.set(signature, types);\n }\n}\n\n/** Kept for the isolate's lifetime, one map per Hyperdrive database. */\nconst hyperdriveStatementTypes: Record<HyperdriveDatabase, StatementTypes> = {\n controlPlane: new StatementTypes(),\n regional: new StatementTypes(),\n};\n\nfunction isRetryableConnectionError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return retryableMessages.some((retryable) => message.includes(retryable));\n}\n\nfunction isConnectionReset(error: unknown) {\n return (\n error instanceof Error && 'code' in error && error.code === 'ECONNRESET'\n );\n}\n\nfunction isRetryableConnectionAcquisitionError(error: unknown) {\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n (error instanceof Error &&\n (error.message === 'connection attempt failed' ||\n ('code' in error && error.code === 'CONNECTION_DESTROYED')))\n );\n}\n\nfunction isQueryRetryDiagnosticError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n message === 'Egress connection severed outside of Hyperdrive' ||\n message === 'Network connection lost.' ||\n message === 'Node shutting down' ||\n message === 'Node shutting down.' ||\n message === 'shutting down'\n );\n}\n\nasync function retryPostgres<T>(\n operation: () => Promise<T>,\n retry: (error: unknown, attemptNumber: number) => boolean | Promise<boolean>,\n diagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n }\n) {\n let attemptCount = 0;\n let retried = false;\n let lastError: unknown;\n const emitDiagnostic = (outcome: PostgresRetryDiagnostic['outcome']) => {\n if (!diagnostic) return;\n diagnostic.emit({\n attemptCount,\n dataRegion: diagnostic.dataRegion,\n errorClass:\n lastError instanceof Error\n ? 'code' in lastError && typeof lastError.code === 'string'\n ? lastError.code\n : lastError.constructor.name\n : 'unknown',\n operationKind: diagnostic.operationKind,\n outcome,\n repositoryName: diagnostic.repositoryName,\n });\n };\n try {\n return await backOff(\n async () => {\n attemptCount++;\n return await operation();\n },\n {\n numOfAttempts: postgresRetryAttempts,\n startingDelay: parseDuration('1s'),\n maxDelay: parseDuration('5s'),\n // Scheduled chunk fan-outs open many connections at once; full jitter\n // keeps their retries from reconnecting in lockstep.\n jitter: 'full',\n async retry(error, attemptNumber) {\n lastError = error;\n const shouldRetry =\n attemptNumber < postgresRetryAttempts &&\n (await retry(error, attemptNumber));\n if (shouldRetry && diagnostic) {\n retried = true;\n emitDiagnostic('retrying');\n }\n return shouldRetry;\n },\n }\n );\n } catch (error) {\n lastError = error;\n if (diagnostic?.shouldEmit(error)) {\n emitDiagnostic(\n retried && attemptCount === postgresRetryAttempts\n ? 'exhausted'\n : 'notRetryable'\n );\n }\n throw error;\n }\n}\n\n/**\n * Replays a complete database operation after a PostgreSQL deadlock (40P01)\n * or lock timeout (55P03), using the same three-attempt jittered budget as\n * connection retries. The operation must own its transaction and must not\n * contain external side effects such as partner API writes.\n */\nexport function retryPostgresLockFailure<T>(operation: () => Promise<T>) {\n return retryPostgres(\n operation,\n (error) =>\n isPostgresError(error, { code: '40P01' }) ||\n isPostgresError(error, { code: '55P03' })\n );\n}\n\nclass RetryingPostgresJSDialect extends PostgresJSDialect {\n constructor(\n private readonly config: PostgresJSDialectConfig,\n private readonly retryDiagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'operationKind' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n },\n private readonly closeOnScriptTimeout?: () => void\n ) {\n super(config);\n }\n\n /** Ends the run when a script's database operation gets no answer (PRO-19037). */\n private async withScriptDeadline<T>(operation: () => Promise<T>) {\n if (!this.closeOnScriptTimeout) return await operation();\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n operation(),\n new Promise<never>((_, reject) => {\n timer = setTimeout(() => {\n console.error('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n });\n this.closeOnScriptTimeout?.();\n reject(\n new KyselyError('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n })\n );\n }, scriptQueryTimeoutMs);\n }),\n ]);\n } finally {\n clearTimeout(timer);\n }\n }\n\n override createDriver(): Driver {\n const driver = new PostgresJSDriver(this.config);\n const connections = new WeakMap<\n DatabaseConnection,\n { connection: DatabaseConnection; inTransaction: boolean }\n >();\n const getConnection = (connection: DatabaseConnection) => {\n const state = connections.get(connection);\n if (!state) throw new Error('Postgres connection state is unavailable');\n return state;\n };\n\n return {\n init: () => driver.init(),\n acquireConnection: async () => {\n const state = {\n connection: await driver.acquireConnection(),\n inTransaction: false,\n };\n const connection: DatabaseConnection = {\n executeQuery: (compiledQuery) =>\n this.withScriptDeadline(() => {\n const operationKind = state.inTransaction\n ? 'transaction'\n : compiledQuery.query.kind === 'SelectQueryNode'\n ? 'select'\n : 'mutation';\n return retryPostgres(\n () => state.connection.executeQuery(compiledQuery),\n async (error) => {\n const message =\n error instanceof Error ? error.message : String(error);\n if (\n state.inTransaction ||\n (!isRetryableConnectionError(error) &&\n (compiledQuery.query.kind !== 'SelectQueryNode' ||\n (!isConnectionReset(error) &&\n message !==\n 'Egress connection severed outside of Hyperdrive' &&\n message !== 'Network connection lost.' &&\n message !== 'Node shutting down' &&\n message !== 'Node shutting down.' &&\n message !== 'shutting down')))\n ) {\n return false;\n }\n\n // Postgres.js already ejects a severed reserved connection.\n // Acquire a new reservation before replaying the safe read.\n state.connection = await driver.acquireConnection();\n return true;\n },\n this.retryDiagnostic\n ? {\n ...this.retryDiagnostic,\n operationKind,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined\n );\n }),\n streamQuery: (compiledQuery, chunkSize) => {\n if (chunkSize === undefined) {\n throw new Error('Postgres stream chunk size is required');\n }\n return state.connection.streamQuery(compiledQuery, chunkSize);\n },\n };\n connections.set(connection, state);\n return connection;\n },\n beginTransaction: async (connection, settings) => {\n const state = getConnection(connection);\n state.inTransaction = true;\n try {\n await this.withScriptDeadline(() =>\n driver.beginTransaction(state.connection, settings)\n );\n } catch (error) {\n state.inTransaction = false;\n throw error;\n }\n },\n commitTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.commitTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n rollbackTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.rollbackTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n savepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.savepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n rollbackToSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.rollbackToSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.releaseSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseConnection: async (connection) => {\n await driver.releaseConnection(getConnection(connection).connection);\n connections.delete(connection);\n },\n destroy: () => driver.destroy(),\n };\n }\n}\n\nexport type Kysely = K<DB>;\nexport type ControlPlaneKysely = K<ControlPlaneDB>;\nexport type ControlPlaneTransaction = T<ControlPlaneDB>;\nexport function useKysely<Database = DB>(postgres: Sql, options?: KyselyOpts) {\n console.debug('Creating Kysely instance');\n const emitRetryDiagnostic =\n options?.onRetryDiagnostic ??\n (options?.repositoryName\n ? (diagnostic: PostgresRetryDiagnostic) =>\n console.warn('Postgres operation retry diagnostic', diagnostic)\n : undefined);\n const reserveConnection = postgres.reserve.bind(postgres);\n postgres.reserve = async () => {\n const connection = await retryPostgres(\n reserveConnection,\n isRetryableConnectionAcquisitionError,\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n operationKind: 'acquisition',\n repositoryName: options?.repositoryName,\n shouldEmit: isRetryableConnectionAcquisitionError,\n }\n : undefined\n );\n if (options?.hyperdrive) {\n const unsafe = connection.unsafe;\n connection.unsafe = (query, parameters, queryOptions) =>\n unsafe(query, parameters, { ...queryOptions, prepare: true });\n }\n return connection;\n };\n const kysely = getKysely<Database>(\n new RetryingPostgresJSDialect(\n { postgres },\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n repositoryName: options?.repositoryName,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined,\n options?.script\n ? () => void postgres.end({ timeout: 0 }).catch(() => undefined)\n : undefined\n ),\n {\n repositoryName: options?.repositoryName,\n log: options?.log,\n }\n );\n kysely.destroy = async () => {\n console.debug('Closing Kysely connection');\n await postgres.end({ timeout: 5 }).catch(() => undefined);\n const index = _connections.indexOf(postgres);\n if (index !== -1) _connections.splice(index, 1);\n };\n\n return kysely;\n}\n\nconst scriptQueryTimeoutMs = parseDuration('1h');\nlet _connections: postgres.Sql[] = [];\nexport function usePostgres(\n connectionString: string,\n opt?: Pick<PgOpt, 'forceDisableSSL' | 'hyperdrive' | 'onReserve' | 'script'>\n) {\n const ssl = opt?.forceDisableSSL !== true && !opt?.hyperdrive;\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n target_session_attrs: 'read-write',\n // Named prepared statements: PRO-17965 (Hyperdrive and Crunchy PgBouncer).\n prepare: true,\n ...(opt?.hyperdrive\n ? {\n max: 5,\n // Retire idle clients before Hyperdrive can close them asynchronously.\n idle_timeout: 10,\n statement_types: hyperdriveStatementTypes[opt.hyperdrive],\n }\n : {\n connect_timeout: 10,\n idle_timeout: 60,\n ...(opt?.script ? { keep_alive: 10 } : {}),\n }),\n });\n if (opt?.onReserve) {\n const { onReserve } = opt;\n const reserve = sql.reserve.bind(sql);\n // Innermost wrapper: it times each open or reuse of a connection, the\n // startup probe too, and is not part of the retry loop (PRO-18952).\n sql.reserve = async () => {\n const startedAtEpochMs = Date.now();\n const startedAt = performance.now();\n try {\n return await reserve();\n } finally {\n onReserve({\n durationMs: performance.now() - startedAt,\n startedAtEpochMs,\n });\n }\n };\n }\n // statement_timeout/lock_timeout are role-level settings on the cluster\n // (ALTER ROLE application SET ...) so they cover every pooled\n // connection; per-session SETs here only ever applied to one of them\n _connections.push(sql);\n return sql;\n}\n\nexport async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {\n const delayMs = 2500;\n const probe = opt?.probe === true;\n const retries = probe ? (opt?.retries === false ? 0 : opt?.retries || 3) : 1;\n const url = new URL(connectionString);\n\n console.debug(\n `Creating Postgres connection with probe=${probe}, ${retries} attempts, ssl=${opt?.forceDisableSSL !== true && !opt?.hyperdrive}, and port=${url.port}`\n );\n if (_connections.length > 0)\n console.warn(`We already have ${_connections.length} Postgres connections`);\n\n for (let attempt = 1; attempt <= retries; attempt++) {\n let sql: Sql | undefined;\n try {\n sql = usePostgres(connectionString, opt);\n if (probe) {\n // Opening a connection already checks that the session is writable\n // and loads the array types; a probe query would add a round trip.\n (await sql.reserve()).release();\n }\n return sql;\n } catch (err) {\n if (sql) {\n await sql.end({ timeout: 0 }).catch(() => undefined);\n _connections.splice(_connections.indexOf(sql), 1);\n }\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n console.error('error', err);\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new KyselyError('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n const sql = await useAsyncPostgres(connectionString, opt);\n return useKysely(sql, opt);\n}\n\nexport async function useAsyncNeonWebSocketKysely(\n connectionString: string,\n opt?: Pick<PgOpt, 'probe'> & KyselyOpts\n) {\n const pool = new Pool({ connectionString, max: 5 });\n const kysely = getKysely<DB>(new PostgresDialect({ pool }), opt ?? {});\n kysely.destroy = async () => {\n console.debug('Closing Neon WebSocket Kysely connection');\n await pool.end();\n };\n if (opt?.probe) await pool.query('select 1');\n return kysely;\n}\n\nexport function useControlPlaneKysely(postgres: Sql, options?: KyselyOpts) {\n return withControlPlaneSchema(\n useKysely<ControlPlaneDB>(postgres, options),\n 'public'\n );\n}\n\nexport async function useAsyncControlPlaneKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n return useControlPlaneKysely(\n await useAsyncPostgres(connectionString, opt),\n opt\n );\n}\n\nexport async function forceCloseAllPostgres(timeoutMs = 5000) {\n await Promise.race([\n Promise.all(\n _connections.map((pg) =>\n pg.end({ timeout: timeoutMs / 1000 }).catch(() => undefined)\n )\n ),\n new Promise((res) => setTimeout(res, timeoutMs + 1000)),\n ]);\n _connections = [];\n}\n"]}
@@ -8,5 +8,5 @@ export type DatabaseKysely<Database> = K<Database>;
8
8
  export type DatabaseTransaction<Database> = T<Database>;
9
9
  export type Transaction = DatabaseTransaction<DB>;
10
10
  export type ControlledTransaction<S extends string[] = string[]> = CT<DB, S>;
11
- export { sql } from 'kysely';
11
+ export { PostgresQueryCompiler, sql } from 'kysely';
12
12
  export type Kysely = DatabaseKysely<DB>;
@@ -14,11 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.sql = exports.jsonArrayFrom = void 0;
17
+ exports.sql = exports.PostgresQueryCompiler = exports.jsonArrayFrom = void 0;
18
18
  var postgres_1 = require("kysely/helpers/postgres");
19
19
  Object.defineProperty(exports, "jsonArrayFrom", { enumerable: true, get: function () { return postgres_1.jsonArrayFrom; } });
20
20
  __exportStar(require("./domain"), exports);
21
21
  __exportStar(require("./v1.generated"), exports);
22
22
  var kysely_1 = require("kysely");
23
+ Object.defineProperty(exports, "PostgresQueryCompiler", { enumerable: true, get: function () { return kysely_1.PostgresQueryCompiler; } });
23
24
  Object.defineProperty(exports, "sql", { enumerable: true, get: function () { return kysely_1.sql; } });
24
25
  //# sourceMappingURL=isomorphic.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"isomorphic.js","sourceRoot":"src/","sources":["isomorphic.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AA2BA,oDAAwD;AAA/C,yGAAA,aAAa,OAAA;AACtB,2CAAyB;AACzB,iDAA+B;AAQ/B,iCAA6B;AAApB,6FAAA,GAAG,OAAA","sourcesContent":["import type {\n ControlledTransaction as CT,\n Kysely as K,\n Transaction as T,\n} from 'kysely';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n KyselyPlugin,\n Nullable,\n OperandExpression,\n PluginTransformQueryArgs,\n PluginTransformResultArgs,\n QueryResult,\n RootOperationNode,\n Selectable,\n SelectQueryBuilder,\n SqlBool,\n UnknownRow,\n Updateable,\n} from 'kysely';\nexport { jsonArrayFrom } from 'kysely/helpers/postgres';\nexport * from './domain';\nexport * from './v1.generated';\n\nimport type { DB } from './v1.generated';\n\nexport type DatabaseKysely<Database> = K<Database>;\nexport type DatabaseTransaction<Database> = T<Database>;\nexport type Transaction = DatabaseTransaction<DB>;\nexport type ControlledTransaction<S extends string[] = string[]> = CT<DB, S>;\nexport { sql } from 'kysely';\n\nexport type Kysely = DatabaseKysely<DB>;\n"]}
1
+ {"version":3,"file":"isomorphic.js","sourceRoot":"src/","sources":["isomorphic.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AA2BA,oDAAwD;AAA/C,yGAAA,aAAa,OAAA;AACtB,2CAAyB;AACzB,iDAA+B;AAQ/B,iCAAoD;AAA3C,+GAAA,qBAAqB,OAAA;AAAE,6FAAA,GAAG,OAAA","sourcesContent":["import type {\n ControlledTransaction as CT,\n Kysely as K,\n Transaction as T,\n} from 'kysely';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n KyselyPlugin,\n Nullable,\n OperandExpression,\n PluginTransformQueryArgs,\n PluginTransformResultArgs,\n QueryResult,\n RootOperationNode,\n Selectable,\n SelectQueryBuilder,\n SqlBool,\n UnknownRow,\n Updateable,\n} from 'kysely';\nexport { jsonArrayFrom } from 'kysely/helpers/postgres';\nexport * from './domain';\nexport * from './v1.generated';\n\nimport type { DB } from './v1.generated';\n\nexport type DatabaseKysely<Database> = K<Database>;\nexport type DatabaseTransaction<Database> = T<Database>;\nexport type Transaction = DatabaseTransaction<DB>;\nexport type ControlledTransaction<S extends string[] = string[]> = CT<DB, S>;\nexport { PostgresQueryCompiler, sql } from 'kysely';\n\nexport type Kysely = DatabaseKysely<DB>;\n"]}
@@ -0,0 +1,12 @@
1
+ import type { Kysely } from './index';
2
+ import type { JsonObject } from './v1.generated';
3
+ /**
4
+ * Passes secrets between the API and Trigger.dev tasks through the tenant's
5
+ * database, so connect credentials never enter Trigger.dev payloads or outputs.
6
+ * Rows expire after one hour, the lifetime of a reused connect run. Reads do not
7
+ * consume the row: a retried or reused run must still find it.
8
+ */
9
+ export declare function useConnectHandoff(kysely: Kysely, tenantId: string): {
10
+ put(appId: string, value: JsonObject): Promise<string>;
11
+ get(ref: string, appId: string): Promise<JsonObject | undefined>;
12
+ };
@@ -0,0 +1,70 @@
1
+ import { sql } from 'kysely';
2
+ // Rows never belong to a user, so no authenticated user can claim one through
3
+ // the OAuth state flow that shares the table.
4
+ const handoffUserId = '00000000-0000-0000-0000-000000000000';
5
+ const handoffRefPattern = /^handoff_[A-Za-z0-9_-]{22}$/;
6
+ async function hashRef(ref) {
7
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(ref));
8
+ return [...new Uint8Array(digest)]
9
+ .map((byte) => byte.toString(16).padStart(2, '0'))
10
+ .join('');
11
+ }
12
+ /**
13
+ * Passes secrets between the API and Trigger.dev tasks through the tenant's
14
+ * database, so connect credentials never enter Trigger.dev payloads or outputs.
15
+ * Rows expire after one hour, the lifetime of a reused connect run. Reads do not
16
+ * consume the row: a retried or reused run must still find it.
17
+ */
18
+ export function useConnectHandoff(kysely, tenantId) {
19
+ return {
20
+ async put(appId, value) {
21
+ const bytes = crypto.getRandomValues(new Uint8Array(16));
22
+ const ref = `handoff_${btoa(String.fromCharCode(...bytes))
23
+ .replaceAll('+', '-')
24
+ .replaceAll('/', '_')
25
+ .replaceAll('=', '')}`;
26
+ await kysely
27
+ .deleteFrom('public.connectionOauthState')
28
+ .where('stateHash', 'in', kysely
29
+ .selectFrom('public.connectionOauthState')
30
+ .select('stateHash')
31
+ .where('expiresAt', '<=', sql `clock_timestamp()`)
32
+ .orderBy('expiresAt')
33
+ .limit(100)
34
+ .forUpdate()
35
+ .skipLocked())
36
+ .execute();
37
+ // Transient authentication coordination is excluded from business audit:
38
+ // references, setup parameters and credentials must never be audited.
39
+ await kysely
40
+ .insertInto('public.connectionOauthState')
41
+ .values({
42
+ stateHash: await hashRef(ref),
43
+ tenantId,
44
+ userId: handoffUserId,
45
+ appId,
46
+ connectionId: null,
47
+ params: value,
48
+ redirectUri: '',
49
+ expiresAt: sql `clock_timestamp() + interval '1 hour'`,
50
+ })
51
+ .execute();
52
+ return ref;
53
+ },
54
+ async get(ref, appId) {
55
+ if (!handoffRefPattern.test(ref))
56
+ return undefined;
57
+ const row = await kysely
58
+ .selectFrom('public.connectionOauthState')
59
+ .select('params')
60
+ .where('stateHash', '=', await hashRef(ref))
61
+ .where('tenantId', '=', tenantId)
62
+ .where('appId', '=', appId)
63
+ .where('userId', '=', handoffUserId)
64
+ .where('expiresAt', '>', sql `clock_timestamp()`)
65
+ .executeTakeFirst();
66
+ return row?.params;
67
+ },
68
+ };
69
+ }
70
+ //# sourceMappingURL=connect-handoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect-handoff.js","sourceRoot":"src/","sources":["connect-handoff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAI7B,8EAA8E;AAC9E,8CAA8C;AAC9C,MAAM,aAAa,GAAG,sCAAsC,CAAC;AAC7D,MAAM,iBAAiB,GAAG,6BAA6B,CAAC;AAExD,KAAK,UAAU,OAAO,CAAC,GAAW;IAChC,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CACvC,SAAS,EACT,IAAI,WAAW,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AC,GAAG,CAAC;gBAC7B,QAAQ;gBACR,MAAM,EAAE,aAAa;gBACrB,KAAK;gBACL,YAAY,EAAE,IAAI;gBAClB,MAAM,EAAE,KAAK;gBACb,WAAW,EAAE,EAAE;gBACf,SAAS,EAAE,GAAG,CAAA,uCAAuC;aACtD,CAAC;iBACD,OAAO,EAAE,CAAC;YACb,OAAO,GAAG,CAAC;QACb,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAa;YAClC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC;gBAAE,OAAO,SAAS,CAAC;YACnD,MAAM,GAAG,GAAG,MAAM,MAAM;iBACrB,UAAU,CAAC,6BAA6B,CAAC;iBACzC,MAAM,CAAC,QAAQ,CAAC;iBAChB,KAAK,CAAC,WAAW,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;iBAC3C,KAAK,CAAC,UAAU,EAAE,GAAG,EAAE,QAAQ,CAAC;iBAChC,KAAK,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;iBAC1B,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,aAAa,CAAC;iBACnC,KAAK,CAAC,WAAW,EAAE,GAAG,EAAE,GAAG,CAAQ,mBAAmB,CAAC;iBACvD,gBAAgB,EAAE,CAAC;YACtB,OAAO,GAAG,EAAE,MAAM,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { sql } from 'kysely';\nimport type { Kysely } from './index';\nimport type { JsonObject } from './v1.generated';\n\n// Rows never belong to a user, so no authenticated user can claim one through\n// the OAuth state flow that shares the table.\nconst handoffUserId = '00000000-0000-0000-0000-000000000000';\nconst handoffRefPattern = /^handoff_[A-Za-z0-9_-]{22}$/;\n\nasync function hashRef(ref: string) {\n const digest = await crypto.subtle.digest(\n 'SHA-256',\n new TextEncoder().encode(ref)\n );\n return [...new Uint8Array(digest)]\n .map((byte) => byte.toString(16).padStart(2, '0'))\n .join('');\n}\n\n/**\n * Passes secrets between the API and Trigger.dev tasks through the tenant's\n * database, so connect credentials never enter Trigger.dev payloads or outputs.\n * Rows expire after one hour, the lifetime of a reused connect run. Reads do not\n * consume the row: a retried or reused run must still find it.\n */\nexport function useConnectHandoff(kysely: Kysely, tenantId: string) {\n return {\n async put(appId: string, value: JsonObject) {\n const bytes = crypto.getRandomValues(new Uint8Array(16));\n const ref = `handoff_${btoa(String.fromCharCode(...bytes))\n .replaceAll('+', '-')\n .replaceAll('/', '_')\n .replaceAll('=', '')}`;\n await kysely\n .deleteFrom('public.connectionOauthState')\n .where(\n 'stateHash',\n 'in',\n kysely\n .selectFrom('public.connectionOauthState')\n .select('stateHash')\n .where('expiresAt', '<=', sql<string>`clock_timestamp()`)\n .orderBy('expiresAt')\n .limit(100)\n .forUpdate()\n .skipLocked()\n )\n .execute();\n // Transient authentication coordination is excluded from business audit:\n // references, setup parameters and credentials must never be audited.\n await kysely\n .insertInto('public.connectionOauthState')\n .values({\n stateHash: await hashRef(ref),\n tenantId,\n userId: handoffUserId,\n appId,\n connectionId: null,\n params: value,\n redirectUri: '',\n expiresAt: sql`clock_timestamp() + interval '1 hour'`,\n })\n .execute();\n return ref;\n },\n async get(ref: string, appId: string) {\n if (!handoffRefPattern.test(ref)) return undefined;\n const row = await kysely\n .selectFrom('public.connectionOauthState')\n .select('params')\n .where('stateHash', '=', await hashRef(ref))\n .where('tenantId', '=', tenantId)\n .where('appId', '=', appId)\n .where('userId', '=', handoffUserId)\n .where('expiresAt', '>', sql<string>`clock_timestamp()`)\n .executeTakeFirst();\n return row?.params;\n },\n };\n}\n"]}
@@ -2,6 +2,7 @@ import type { Kysely as K, LogConfig, Transaction as T } from 'kysely';
2
2
  import postgres, { type Sql } from 'postgres';
3
3
  import { type ControlPlaneDB } from './control-plane';
4
4
  export type { Insertable, LogEvent } from 'kysely';
5
+ export * from './connect-handoff';
5
6
  export * from './error';
6
7
  export * from './isomorphic';
7
8
  export * from './postgres-error';
@@ -7,6 +7,7 @@ import { withControlPlaneSchema } from './control-plane';
7
7
  import { KyselyError } from './error';
8
8
  import { getKysely } from './plugins';
9
9
  import { isPostgresError } from './postgres-error';
10
+ export * from './connect-handoff';
10
11
  export * from './error';
11
12
  export * from './isomorphic';
12
13
  export * from './postgres-error';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAQhD,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACzC,OAAO,EACL,iBAAiB,EAEjB,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,QAAsB,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAuB,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAGnD,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAE9B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AA2ClD,MAAM,iBAAiB,GAAG;IACxB,mBAAmB;IACnB,iBAAiB;IACjB,uCAAuC;IACvC,oBAAoB;IACpB,mDAAmD;CACpD,CAAC;AACF,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC;;;;;;;;GAQG;AACH,MAAM,cAAe,SAAQ,GAAqB;IACvC,GAAG,CAAC,SAAiB,EAAE,KAAe;QAC7C,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAA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,UAAU,qBAAqB,CAAC,QAAa,EAAE,OAAoB;IACvE,OAAO,sBAAsB,CAC3B,SAAS,CAAiB,QAAQ,EAAE,OAAO,CAAC,EAC5C,QAAQ,CACT,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,gBAAwB,EACxB,GAAwB;IAExB,OAAO,qBAAqB,CAC1B,MAAM,gBAAgB,CAAC,gBAAgB,EAAE,GAAG,CAAC,EAC7C,GAAG,CACJ,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,SAAS,GAAG,IAAI;IAC1D,MAAM,OAAO,CAAC,IAAI,CAAC;QACjB,OAAO,CAAC,GAAG,CACT,YAAY,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACtB,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,SAAS,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAC7D,CACF;QACD,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC,CAAC;KACxD,CAAC,CAAC;IACH,YAAY,GAAG,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import { backOff } from 'exponential-backoff';\nimport { Pool } from '@neondatabase/serverless';\nimport type {\n DatabaseConnection,\n Driver,\n Kysely as K,\n LogConfig,\n Transaction as T,\n} from 'kysely';\nimport { PostgresDialect } from 'kysely';\nimport {\n PostgresJSDialect,\n type PostgresJSDialectConfig,\n PostgresJSDriver,\n} from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\nimport { type ControlPlaneDB, withControlPlaneSchema } from './control-plane';\nimport { KyselyError } from './error';\nimport { getKysely } from './plugins';\nimport { isPostgresError } from './postgres-error';\n\nexport type { Insertable, LogEvent } from 'kysely';\nexport * from './error';\nexport * from './isomorphic';\nexport * from './postgres-error';\nexport * from './query';\nexport * from './regional';\nexport * from './transaction';\n\nimport { parseDuration } from '@vrplatform/utils';\nimport type { TenantDataRegion } from './regional';\nimport type { DB } from './v1.generated';\n\n/** The database a Worker reaches through its Hyperdrive binding. */\ntype HyperdriveDatabase = 'controlPlane' | 'regional';\n\ntype KyselyOpts = {\n dataRegion?: TenantDataRegion;\n hyperdrive?: HyperdriveDatabase;\n /** Called after every connection reservation, including the startup probe. */\n onReserve?: (timing: {\n durationMs: number;\n startedAtEpochMs: number;\n }) => void;\n onRetryDiagnostic?: (diagnostic: PostgresRetryDiagnostic) => void;\n repositoryName?: string;\n log?: true | LogConfig;\n /**\n * Scripts and repairs (PRO-19037). Probes an idle TCP connection after 10 s.\n * A query, commit, begin, rollback or savepoint with no answer for an hour,\n * longer than any statement timeout a script sets, fails the run and closes\n * the Postgres client that issued it. Backstop for a connection the kernel\n * keeps open while the peer never answers.\n */\n script?: true;\n};\n\ntype PostgresRetryDiagnostic = {\n attemptCount: number;\n dataRegion?: TenantDataRegion;\n errorClass: string;\n operationKind: 'acquisition' | 'mutation' | 'select' | 'transaction';\n outcome: 'exhausted' | 'notRetryable' | 'retrying';\n repositoryName?: string;\n};\n\ntype PgOpt = {\n forceDisableSSL?: boolean;\n probe?: true;\n retries?: false | number;\n} & Pick<KyselyOpts, 'hyperdrive' | 'onReserve' | 'script'>;\n\nconst retryableMessages = [\n 'CONNECTION_CLOSED',\n 'CONNECT_TIMEOUT',\n 'Idle connection closed by Hyperdrive.',\n 'server_login_retry',\n 'Timed out while creating a new server connection.',\n];\nconst postgresRetryAttempts = 3;\n\n/**\n * Parameter types PostgreSQL described per statement signature (PRO-18871).\n * Postgres.js describes a statement in a round trip of its own before it can\n * execute it when a parameter type is unknown, and a Worker request always\n * starts on new connections. The patched driver reads and fills this map\n * through its `statement_types` option and then parses, describes, binds and\n * executes a known statement in one round trip. It forgets a statement's\n * types when the statement fails, so a schema change is described again.\n */\nclass StatementTypes extends Map<string, number[]> {\n override set(signature: string, types: number[]) {\n if (!this.has(signature) && this.size >= 2000) {\n this.delete(this.keys().next().value!);\n }\n return super.set(signature, types);\n }\n}\n\n/** Kept for the isolate's lifetime, one map per Hyperdrive database. */\nconst hyperdriveStatementTypes: Record<HyperdriveDatabase, StatementTypes> = {\n controlPlane: new StatementTypes(),\n regional: new StatementTypes(),\n};\n\nfunction isRetryableConnectionError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return retryableMessages.some((retryable) => message.includes(retryable));\n}\n\nfunction isConnectionReset(error: unknown) {\n return (\n error instanceof Error && 'code' in error && error.code === 'ECONNRESET'\n );\n}\n\nfunction isRetryableConnectionAcquisitionError(error: unknown) {\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n (error instanceof Error &&\n (error.message === 'connection attempt failed' ||\n ('code' in error && error.code === 'CONNECTION_DESTROYED')))\n );\n}\n\nfunction isQueryRetryDiagnosticError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n message === 'Egress connection severed outside of Hyperdrive' ||\n message === 'Network connection lost.' ||\n message === 'Node shutting down' ||\n message === 'Node shutting down.' ||\n message === 'shutting down'\n );\n}\n\nasync function retryPostgres<T>(\n operation: () => Promise<T>,\n retry: (error: unknown, attemptNumber: number) => boolean | Promise<boolean>,\n diagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n }\n) {\n let attemptCount = 0;\n let retried = false;\n let lastError: unknown;\n const emitDiagnostic = (outcome: PostgresRetryDiagnostic['outcome']) => {\n if (!diagnostic) return;\n diagnostic.emit({\n attemptCount,\n dataRegion: diagnostic.dataRegion,\n errorClass:\n lastError instanceof Error\n ? 'code' in lastError && typeof lastError.code === 'string'\n ? lastError.code\n : lastError.constructor.name\n : 'unknown',\n operationKind: diagnostic.operationKind,\n outcome,\n repositoryName: diagnostic.repositoryName,\n });\n };\n try {\n return await backOff(\n async () => {\n attemptCount++;\n return await operation();\n },\n {\n numOfAttempts: postgresRetryAttempts,\n startingDelay: parseDuration('1s'),\n maxDelay: parseDuration('5s'),\n // Scheduled chunk fan-outs open many connections at once; full jitter\n // keeps their retries from reconnecting in lockstep.\n jitter: 'full',\n async retry(error, attemptNumber) {\n lastError = error;\n const shouldRetry =\n attemptNumber < postgresRetryAttempts &&\n (await retry(error, attemptNumber));\n if (shouldRetry && diagnostic) {\n retried = true;\n emitDiagnostic('retrying');\n }\n return shouldRetry;\n },\n }\n );\n } catch (error) {\n lastError = error;\n if (diagnostic?.shouldEmit(error)) {\n emitDiagnostic(\n retried && attemptCount === postgresRetryAttempts\n ? 'exhausted'\n : 'notRetryable'\n );\n }\n throw error;\n }\n}\n\n/**\n * Replays a complete database operation after a PostgreSQL deadlock (40P01)\n * or lock timeout (55P03), using the same three-attempt jittered budget as\n * connection retries. The operation must own its transaction and must not\n * contain external side effects such as partner API writes.\n */\nexport function retryPostgresLockFailure<T>(operation: () => Promise<T>) {\n return retryPostgres(\n operation,\n (error) =>\n isPostgresError(error, { code: '40P01' }) ||\n isPostgresError(error, { code: '55P03' })\n );\n}\n\nclass RetryingPostgresJSDialect extends PostgresJSDialect {\n constructor(\n private readonly config: PostgresJSDialectConfig,\n private readonly retryDiagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'operationKind' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n },\n private readonly closeOnScriptTimeout?: () => void\n ) {\n super(config);\n }\n\n /** Ends the run when a script's database operation gets no answer (PRO-19037). */\n private async withScriptDeadline<T>(operation: () => Promise<T>) {\n if (!this.closeOnScriptTimeout) return await operation();\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n operation(),\n new Promise<never>((_, reject) => {\n timer = setTimeout(() => {\n console.error('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n });\n this.closeOnScriptTimeout?.();\n reject(\n new KyselyError('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n })\n );\n }, scriptQueryTimeoutMs);\n }),\n ]);\n } finally {\n clearTimeout(timer);\n }\n }\n\n override createDriver(): Driver {\n const driver = new PostgresJSDriver(this.config);\n const connections = new WeakMap<\n DatabaseConnection,\n { connection: DatabaseConnection; inTransaction: boolean }\n >();\n const getConnection = (connection: DatabaseConnection) => {\n const state = connections.get(connection);\n if (!state) throw new Error('Postgres connection state is unavailable');\n return state;\n };\n\n return {\n init: () => driver.init(),\n acquireConnection: async () => {\n const state = {\n connection: await driver.acquireConnection(),\n inTransaction: false,\n };\n const connection: DatabaseConnection = {\n executeQuery: (compiledQuery) =>\n this.withScriptDeadline(() => {\n const operationKind = state.inTransaction\n ? 'transaction'\n : compiledQuery.query.kind === 'SelectQueryNode'\n ? 'select'\n : 'mutation';\n return retryPostgres(\n () => state.connection.executeQuery(compiledQuery),\n async (error) => {\n const message =\n error instanceof Error ? error.message : String(error);\n if (\n state.inTransaction ||\n (!isRetryableConnectionError(error) &&\n (compiledQuery.query.kind !== 'SelectQueryNode' ||\n (!isConnectionReset(error) &&\n message !==\n 'Egress connection severed outside of Hyperdrive' &&\n message !== 'Network connection lost.' &&\n message !== 'Node shutting down' &&\n message !== 'Node shutting down.' &&\n message !== 'shutting down')))\n ) {\n return false;\n }\n\n // Postgres.js already ejects a severed reserved connection.\n // Acquire a new reservation before replaying the safe read.\n state.connection = await driver.acquireConnection();\n return true;\n },\n this.retryDiagnostic\n ? {\n ...this.retryDiagnostic,\n operationKind,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined\n );\n }),\n streamQuery: (compiledQuery, chunkSize) => {\n if (chunkSize === undefined) {\n throw new Error('Postgres stream chunk size is required');\n }\n return state.connection.streamQuery(compiledQuery, chunkSize);\n },\n };\n connections.set(connection, state);\n return connection;\n },\n beginTransaction: async (connection, settings) => {\n const state = getConnection(connection);\n state.inTransaction = true;\n try {\n await this.withScriptDeadline(() =>\n driver.beginTransaction(state.connection, settings)\n );\n } catch (error) {\n state.inTransaction = false;\n throw error;\n }\n },\n commitTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.commitTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n rollbackTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.rollbackTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n savepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.savepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n rollbackToSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.rollbackToSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.releaseSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseConnection: async (connection) => {\n await driver.releaseConnection(getConnection(connection).connection);\n connections.delete(connection);\n },\n destroy: () => driver.destroy(),\n };\n }\n}\n\nexport type Kysely = K<DB>;\nexport type ControlPlaneKysely = K<ControlPlaneDB>;\nexport type ControlPlaneTransaction = T<ControlPlaneDB>;\nexport function useKysely<Database = DB>(postgres: Sql, options?: KyselyOpts) {\n console.debug('Creating Kysely instance');\n const emitRetryDiagnostic =\n options?.onRetryDiagnostic ??\n (options?.repositoryName\n ? (diagnostic: PostgresRetryDiagnostic) =>\n console.warn('Postgres operation retry diagnostic', diagnostic)\n : undefined);\n const reserveConnection = postgres.reserve.bind(postgres);\n postgres.reserve = async () => {\n const connection = await retryPostgres(\n reserveConnection,\n isRetryableConnectionAcquisitionError,\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n operationKind: 'acquisition',\n repositoryName: options?.repositoryName,\n shouldEmit: isRetryableConnectionAcquisitionError,\n }\n : undefined\n );\n if (options?.hyperdrive) {\n const unsafe = connection.unsafe;\n connection.unsafe = (query, parameters, queryOptions) =>\n unsafe(query, parameters, { ...queryOptions, prepare: true });\n }\n return connection;\n };\n const kysely = getKysely<Database>(\n new RetryingPostgresJSDialect(\n { postgres },\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n repositoryName: options?.repositoryName,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined,\n options?.script\n ? () => void postgres.end({ timeout: 0 }).catch(() => undefined)\n : undefined\n ),\n {\n repositoryName: options?.repositoryName,\n log: options?.log,\n }\n );\n kysely.destroy = async () => {\n console.debug('Closing Kysely connection');\n await postgres.end({ timeout: 5 }).catch(() => undefined);\n const index = _connections.indexOf(postgres);\n if (index !== -1) _connections.splice(index, 1);\n };\n\n return kysely;\n}\n\nconst scriptQueryTimeoutMs = parseDuration('1h');\nlet _connections: postgres.Sql[] = [];\nexport function usePostgres(\n connectionString: string,\n opt?: Pick<PgOpt, 'forceDisableSSL' | 'hyperdrive' | 'onReserve' | 'script'>\n) {\n const ssl = opt?.forceDisableSSL !== true && !opt?.hyperdrive;\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n target_session_attrs: 'read-write',\n // Named prepared statements: PRO-17965 (Hyperdrive and Crunchy PgBouncer).\n prepare: true,\n ...(opt?.hyperdrive\n ? {\n max: 5,\n // Retire idle clients before Hyperdrive can close them asynchronously.\n idle_timeout: 10,\n statement_types: hyperdriveStatementTypes[opt.hyperdrive],\n }\n : {\n connect_timeout: 10,\n idle_timeout: 60,\n ...(opt?.script ? { keep_alive: 10 } : {}),\n }),\n });\n if (opt?.onReserve) {\n const { onReserve } = opt;\n const reserve = sql.reserve.bind(sql);\n // Innermost wrapper: it times each open or reuse of a connection, the\n // startup probe too, and is not part of the retry loop (PRO-18952).\n sql.reserve = async () => {\n const startedAtEpochMs = Date.now();\n const startedAt = performance.now();\n try {\n return await reserve();\n } finally {\n onReserve({\n durationMs: performance.now() - startedAt,\n startedAtEpochMs,\n });\n }\n };\n }\n // statement_timeout/lock_timeout are role-level settings on the cluster\n // (ALTER ROLE application SET ...) so they cover every pooled\n // connection; per-session SETs here only ever applied to one of them\n _connections.push(sql);\n return sql;\n}\n\nexport async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {\n const delayMs = 2500;\n const probe = opt?.probe === true;\n const retries = probe ? (opt?.retries === false ? 0 : opt?.retries || 3) : 1;\n const url = new URL(connectionString);\n\n console.debug(\n `Creating Postgres connection with probe=${probe}, ${retries} attempts, ssl=${opt?.forceDisableSSL !== true && !opt?.hyperdrive}, and port=${url.port}`\n );\n if (_connections.length > 0)\n console.warn(`We already have ${_connections.length} Postgres connections`);\n\n for (let attempt = 1; attempt <= retries; attempt++) {\n let sql: Sql | undefined;\n try {\n sql = usePostgres(connectionString, opt);\n if (probe) {\n // Opening a connection already checks that the session is writable\n // and loads the array types; a probe query would add a round trip.\n (await sql.reserve()).release();\n }\n return sql;\n } catch (err) {\n if (sql) {\n await sql.end({ timeout: 0 }).catch(() => undefined);\n _connections.splice(_connections.indexOf(sql), 1);\n }\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n console.error('error', err);\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new KyselyError('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n const sql = await useAsyncPostgres(connectionString, opt);\n return useKysely(sql, opt);\n}\n\nexport async function useAsyncNeonWebSocketKysely(\n connectionString: string,\n opt?: Pick<PgOpt, 'probe'> & KyselyOpts\n) {\n const pool = new Pool({ connectionString, max: 5 });\n const kysely = getKysely<DB>(new PostgresDialect({ pool }), opt ?? {});\n kysely.destroy = async () => {\n console.debug('Closing Neon WebSocket Kysely connection');\n await pool.end();\n };\n if (opt?.probe) await pool.query('select 1');\n return kysely;\n}\n\nexport function useControlPlaneKysely(postgres: Sql, options?: KyselyOpts) {\n return withControlPlaneSchema(\n useKysely<ControlPlaneDB>(postgres, options),\n 'public'\n );\n}\n\nexport async function useAsyncControlPlaneKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n return useControlPlaneKysely(\n await useAsyncPostgres(connectionString, opt),\n opt\n );\n}\n\nexport async function forceCloseAllPostgres(timeoutMs = 5000) {\n await Promise.race([\n Promise.all(\n _connections.map((pg) =>\n pg.end({ timeout: timeoutMs / 1000 }).catch(() => undefined)\n )\n ),\n new Promise((res) => setTimeout(res, timeoutMs + 1000)),\n ]);\n _connections = [];\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAQhD,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAC;AACzC,OAAO,EACL,iBAAiB,EAEjB,gBAAgB,GACjB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,QAAsB,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAuB,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAGnD,cAAc,mBAAmB,CAAC;AAClC,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC;AACjC,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAE9B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AA2ClD,MAAM,i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ACvE,MAAM,CAAC,OAAO,GAAG,KAAK,IAAI,EAAE;QAC1B,OAAO,CAAC,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC1D,MAAM,IAAI,CAAC,GAAG,EAAE,CAAC;IACnB,CAAC,CAAC;IACF,IAAI,GAAG,EAAE,KAAK;QAAE,MAAM,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAC7C,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,qBAAqB,CAAC,QAAa,EAAE,OAAoB;IACvE,OAAO,sBAAsB,CAC3B,SAAS,CAAiB,QAAQ,EAAE,OAAO,CAAC,EAC5C,QAAQ,CACT,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,gBAAwB,EACxB,GAAwB;IAExB,OAAO,qBAAqB,CAC1B,MAAM,gBAAgB,CAAC,gBAAgB,EAAE,GAAG,CAAC,EAC7C,GAAG,CACJ,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,SAAS,GAAG,IAAI;IAC1D,MAAM,OAAO,CAAC,IAAI,CAAC;QACjB,OAAO,CAAC,GAAG,CACT,YAAY,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACtB,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,SAAS,GAAG,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAC7D,CACF;QACD,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC,CAAC;KACxD,CAAC,CAAC;IACH,YAAY,GAAG,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import { backOff } from 'exponential-backoff';\nimport { Pool } from '@neondatabase/serverless';\nimport type {\n DatabaseConnection,\n Driver,\n Kysely as K,\n LogConfig,\n Transaction as T,\n} from 'kysely';\nimport { PostgresDialect } from 'kysely';\nimport {\n PostgresJSDialect,\n type PostgresJSDialectConfig,\n PostgresJSDriver,\n} from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\nimport { type ControlPlaneDB, withControlPlaneSchema } from './control-plane';\nimport { KyselyError } from './error';\nimport { getKysely } from './plugins';\nimport { isPostgresError } from './postgres-error';\n\nexport type { Insertable, LogEvent } from 'kysely';\nexport * from './connect-handoff';\nexport * from './error';\nexport * from './isomorphic';\nexport * from './postgres-error';\nexport * from './query';\nexport * from './regional';\nexport * from './transaction';\n\nimport { parseDuration } from '@vrplatform/utils';\nimport type { TenantDataRegion } from './regional';\nimport type { DB } from './v1.generated';\n\n/** The database a Worker reaches through its Hyperdrive binding. */\ntype HyperdriveDatabase = 'controlPlane' | 'regional';\n\ntype KyselyOpts = {\n dataRegion?: TenantDataRegion;\n hyperdrive?: HyperdriveDatabase;\n /** Called after every connection reservation, including the startup probe. */\n onReserve?: (timing: {\n durationMs: number;\n startedAtEpochMs: number;\n }) => void;\n onRetryDiagnostic?: (diagnostic: PostgresRetryDiagnostic) => void;\n repositoryName?: string;\n log?: true | LogConfig;\n /**\n * Scripts and repairs (PRO-19037). Probes an idle TCP connection after 10 s.\n * A query, commit, begin, rollback or savepoint with no answer for an hour,\n * longer than any statement timeout a script sets, fails the run and closes\n * the Postgres client that issued it. Backstop for a connection the kernel\n * keeps open while the peer never answers.\n */\n script?: true;\n};\n\ntype PostgresRetryDiagnostic = {\n attemptCount: number;\n dataRegion?: TenantDataRegion;\n errorClass: string;\n operationKind: 'acquisition' | 'mutation' | 'select' | 'transaction';\n outcome: 'exhausted' | 'notRetryable' | 'retrying';\n repositoryName?: string;\n};\n\ntype PgOpt = {\n forceDisableSSL?: boolean;\n probe?: true;\n retries?: false | number;\n} & Pick<KyselyOpts, 'hyperdrive' | 'onReserve' | 'script'>;\n\nconst retryableMessages = [\n 'CONNECTION_CLOSED',\n 'CONNECT_TIMEOUT',\n 'Idle connection closed by Hyperdrive.',\n 'server_login_retry',\n 'Timed out while creating a new server connection.',\n];\nconst postgresRetryAttempts = 3;\n\n/**\n * Parameter types PostgreSQL described per statement signature (PRO-18871).\n * Postgres.js describes a statement in a round trip of its own before it can\n * execute it when a parameter type is unknown, and a Worker request always\n * starts on new connections. The patched driver reads and fills this map\n * through its `statement_types` option and then parses, describes, binds and\n * executes a known statement in one round trip. It forgets a statement's\n * types when the statement fails, so a schema change is described again.\n */\nclass StatementTypes extends Map<string, number[]> {\n override set(signature: string, types: number[]) {\n if (!this.has(signature) && this.size >= 2000) {\n this.delete(this.keys().next().value!);\n }\n return super.set(signature, types);\n }\n}\n\n/** Kept for the isolate's lifetime, one map per Hyperdrive database. */\nconst hyperdriveStatementTypes: Record<HyperdriveDatabase, StatementTypes> = {\n controlPlane: new StatementTypes(),\n regional: new StatementTypes(),\n};\n\nfunction isRetryableConnectionError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return retryableMessages.some((retryable) => message.includes(retryable));\n}\n\nfunction isConnectionReset(error: unknown) {\n return (\n error instanceof Error && 'code' in error && error.code === 'ECONNRESET'\n );\n}\n\nfunction isRetryableConnectionAcquisitionError(error: unknown) {\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n (error instanceof Error &&\n (error.message === 'connection attempt failed' ||\n ('code' in error && error.code === 'CONNECTION_DESTROYED')))\n );\n}\n\nfunction isQueryRetryDiagnosticError(error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return (\n isRetryableConnectionError(error) ||\n isConnectionReset(error) ||\n message === 'Egress connection severed outside of Hyperdrive' ||\n message === 'Network connection lost.' ||\n message === 'Node shutting down' ||\n message === 'Node shutting down.' ||\n message === 'shutting down'\n );\n}\n\nasync function retryPostgres<T>(\n operation: () => Promise<T>,\n retry: (error: unknown, attemptNumber: number) => boolean | Promise<boolean>,\n diagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n }\n) {\n let attemptCount = 0;\n let retried = false;\n let lastError: unknown;\n const emitDiagnostic = (outcome: PostgresRetryDiagnostic['outcome']) => {\n if (!diagnostic) return;\n diagnostic.emit({\n attemptCount,\n dataRegion: diagnostic.dataRegion,\n errorClass:\n lastError instanceof Error\n ? 'code' in lastError && typeof lastError.code === 'string'\n ? lastError.code\n : lastError.constructor.name\n : 'unknown',\n operationKind: diagnostic.operationKind,\n outcome,\n repositoryName: diagnostic.repositoryName,\n });\n };\n try {\n return await backOff(\n async () => {\n attemptCount++;\n return await operation();\n },\n {\n numOfAttempts: postgresRetryAttempts,\n startingDelay: parseDuration('1s'),\n maxDelay: parseDuration('5s'),\n // Scheduled chunk fan-outs open many connections at once; full jitter\n // keeps their retries from reconnecting in lockstep.\n jitter: 'full',\n async retry(error, attemptNumber) {\n lastError = error;\n const shouldRetry =\n attemptNumber < postgresRetryAttempts &&\n (await retry(error, attemptNumber));\n if (shouldRetry && diagnostic) {\n retried = true;\n emitDiagnostic('retrying');\n }\n return shouldRetry;\n },\n }\n );\n } catch (error) {\n lastError = error;\n if (diagnostic?.shouldEmit(error)) {\n emitDiagnostic(\n retried && attemptCount === postgresRetryAttempts\n ? 'exhausted'\n : 'notRetryable'\n );\n }\n throw error;\n }\n}\n\n/**\n * Replays a complete database operation after a PostgreSQL deadlock (40P01)\n * or lock timeout (55P03), using the same three-attempt jittered budget as\n * connection retries. The operation must own its transaction and must not\n * contain external side effects such as partner API writes.\n */\nexport function retryPostgresLockFailure<T>(operation: () => Promise<T>) {\n return retryPostgres(\n operation,\n (error) =>\n isPostgresError(error, { code: '40P01' }) ||\n isPostgresError(error, { code: '55P03' })\n );\n}\n\nclass RetryingPostgresJSDialect extends PostgresJSDialect {\n constructor(\n private readonly config: PostgresJSDialectConfig,\n private readonly retryDiagnostic?: Omit<\n PostgresRetryDiagnostic,\n 'attemptCount' | 'errorClass' | 'operationKind' | 'outcome'\n > & {\n emit: (diagnostic: PostgresRetryDiagnostic) => void;\n shouldEmit: (error: unknown) => boolean;\n },\n private readonly closeOnScriptTimeout?: () => void\n ) {\n super(config);\n }\n\n /** Ends the run when a script's database operation gets no answer (PRO-19037). */\n private async withScriptDeadline<T>(operation: () => Promise<T>) {\n if (!this.closeOnScriptTimeout) return await operation();\n let timer: ReturnType<typeof setTimeout> | undefined;\n try {\n return await Promise.race([\n operation(),\n new Promise<never>((_, reject) => {\n timer = setTimeout(() => {\n console.error('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n });\n this.closeOnScriptTimeout?.();\n reject(\n new KyselyError('Postgres operation timed out', {\n timeoutMs: scriptQueryTimeoutMs,\n })\n );\n }, scriptQueryTimeoutMs);\n }),\n ]);\n } finally {\n clearTimeout(timer);\n }\n }\n\n override createDriver(): Driver {\n const driver = new PostgresJSDriver(this.config);\n const connections = new WeakMap<\n DatabaseConnection,\n { connection: DatabaseConnection; inTransaction: boolean }\n >();\n const getConnection = (connection: DatabaseConnection) => {\n const state = connections.get(connection);\n if (!state) throw new Error('Postgres connection state is unavailable');\n return state;\n };\n\n return {\n init: () => driver.init(),\n acquireConnection: async () => {\n const state = {\n connection: await driver.acquireConnection(),\n inTransaction: false,\n };\n const connection: DatabaseConnection = {\n executeQuery: (compiledQuery) =>\n this.withScriptDeadline(() => {\n const operationKind = state.inTransaction\n ? 'transaction'\n : compiledQuery.query.kind === 'SelectQueryNode'\n ? 'select'\n : 'mutation';\n return retryPostgres(\n () => state.connection.executeQuery(compiledQuery),\n async (error) => {\n const message =\n error instanceof Error ? error.message : String(error);\n if (\n state.inTransaction ||\n (!isRetryableConnectionError(error) &&\n (compiledQuery.query.kind !== 'SelectQueryNode' ||\n (!isConnectionReset(error) &&\n message !==\n 'Egress connection severed outside of Hyperdrive' &&\n message !== 'Network connection lost.' &&\n message !== 'Node shutting down' &&\n message !== 'Node shutting down.' &&\n message !== 'shutting down')))\n ) {\n return false;\n }\n\n // Postgres.js already ejects a severed reserved connection.\n // Acquire a new reservation before replaying the safe read.\n state.connection = await driver.acquireConnection();\n return true;\n },\n this.retryDiagnostic\n ? {\n ...this.retryDiagnostic,\n operationKind,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined\n );\n }),\n streamQuery: (compiledQuery, chunkSize) => {\n if (chunkSize === undefined) {\n throw new Error('Postgres stream chunk size is required');\n }\n return state.connection.streamQuery(compiledQuery, chunkSize);\n },\n };\n connections.set(connection, state);\n return connection;\n },\n beginTransaction: async (connection, settings) => {\n const state = getConnection(connection);\n state.inTransaction = true;\n try {\n await this.withScriptDeadline(() =>\n driver.beginTransaction(state.connection, settings)\n );\n } catch (error) {\n state.inTransaction = false;\n throw error;\n }\n },\n commitTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.commitTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n rollbackTransaction: async (connection) => {\n const state = getConnection(connection);\n try {\n await this.withScriptDeadline(() =>\n driver.rollbackTransaction(state.connection)\n );\n } finally {\n state.inTransaction = false;\n }\n },\n savepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.savepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n rollbackToSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.rollbackToSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseSavepoint: (connection, savepointName, compileQuery) =>\n this.withScriptDeadline(() =>\n driver.releaseSavepoint(\n getConnection(connection).connection,\n savepointName,\n compileQuery\n )\n ),\n releaseConnection: async (connection) => {\n await driver.releaseConnection(getConnection(connection).connection);\n connections.delete(connection);\n },\n destroy: () => driver.destroy(),\n };\n }\n}\n\nexport type Kysely = K<DB>;\nexport type ControlPlaneKysely = K<ControlPlaneDB>;\nexport type ControlPlaneTransaction = T<ControlPlaneDB>;\nexport function useKysely<Database = DB>(postgres: Sql, options?: KyselyOpts) {\n console.debug('Creating Kysely instance');\n const emitRetryDiagnostic =\n options?.onRetryDiagnostic ??\n (options?.repositoryName\n ? (diagnostic: PostgresRetryDiagnostic) =>\n console.warn('Postgres operation retry diagnostic', diagnostic)\n : undefined);\n const reserveConnection = postgres.reserve.bind(postgres);\n postgres.reserve = async () => {\n const connection = await retryPostgres(\n reserveConnection,\n isRetryableConnectionAcquisitionError,\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n operationKind: 'acquisition',\n repositoryName: options?.repositoryName,\n shouldEmit: isRetryableConnectionAcquisitionError,\n }\n : undefined\n );\n if (options?.hyperdrive) {\n const unsafe = connection.unsafe;\n connection.unsafe = (query, parameters, queryOptions) =>\n unsafe(query, parameters, { ...queryOptions, prepare: true });\n }\n return connection;\n };\n const kysely = getKysely<Database>(\n new RetryingPostgresJSDialect(\n { postgres },\n emitRetryDiagnostic\n ? {\n dataRegion: options?.dataRegion,\n emit: emitRetryDiagnostic,\n repositoryName: options?.repositoryName,\n shouldEmit: isQueryRetryDiagnosticError,\n }\n : undefined,\n options?.script\n ? () => void postgres.end({ timeout: 0 }).catch(() => undefined)\n : undefined\n ),\n {\n repositoryName: options?.repositoryName,\n log: options?.log,\n }\n );\n kysely.destroy = async () => {\n console.debug('Closing Kysely connection');\n await postgres.end({ timeout: 5 }).catch(() => undefined);\n const index = _connections.indexOf(postgres);\n if (index !== -1) _connections.splice(index, 1);\n };\n\n return kysely;\n}\n\nconst scriptQueryTimeoutMs = parseDuration('1h');\nlet _connections: postgres.Sql[] = [];\nexport function usePostgres(\n connectionString: string,\n opt?: Pick<PgOpt, 'forceDisableSSL' | 'hyperdrive' | 'onReserve' | 'script'>\n) {\n const ssl = opt?.forceDisableSSL !== true && !opt?.hyperdrive;\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n target_session_attrs: 'read-write',\n // Named prepared statements: PRO-17965 (Hyperdrive and Crunchy PgBouncer).\n prepare: true,\n ...(opt?.hyperdrive\n ? {\n max: 5,\n // Retire idle clients before Hyperdrive can close them asynchronously.\n idle_timeout: 10,\n statement_types: hyperdriveStatementTypes[opt.hyperdrive],\n }\n : {\n connect_timeout: 10,\n idle_timeout: 60,\n ...(opt?.script ? { keep_alive: 10 } : {}),\n }),\n });\n if (opt?.onReserve) {\n const { onReserve } = opt;\n const reserve = sql.reserve.bind(sql);\n // Innermost wrapper: it times each open or reuse of a connection, the\n // startup probe too, and is not part of the retry loop (PRO-18952).\n sql.reserve = async () => {\n const startedAtEpochMs = Date.now();\n const startedAt = performance.now();\n try {\n return await reserve();\n } finally {\n onReserve({\n durationMs: performance.now() - startedAt,\n startedAtEpochMs,\n });\n }\n };\n }\n // statement_timeout/lock_timeout are role-level settings on the cluster\n // (ALTER ROLE application SET ...) so they cover every pooled\n // connection; per-session SETs here only ever applied to one of them\n _connections.push(sql);\n return sql;\n}\n\nexport async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {\n const delayMs = 2500;\n const probe = opt?.probe === true;\n const retries = probe ? (opt?.retries === false ? 0 : opt?.retries || 3) : 1;\n const url = new URL(connectionString);\n\n console.debug(\n `Creating Postgres connection with probe=${probe}, ${retries} attempts, ssl=${opt?.forceDisableSSL !== true && !opt?.hyperdrive}, and port=${url.port}`\n );\n if (_connections.length > 0)\n console.warn(`We already have ${_connections.length} Postgres connections`);\n\n for (let attempt = 1; attempt <= retries; attempt++) {\n let sql: Sql | undefined;\n try {\n sql = usePostgres(connectionString, opt);\n if (probe) {\n // Opening a connection already checks that the session is writable\n // and loads the array types; a probe query would add a round trip.\n (await sql.reserve()).release();\n }\n return sql;\n } catch (err) {\n if (sql) {\n await sql.end({ timeout: 0 }).catch(() => undefined);\n _connections.splice(_connections.indexOf(sql), 1);\n }\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n console.error('error', err);\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new KyselyError('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n const sql = await useAsyncPostgres(connectionString, opt);\n return useKysely(sql, opt);\n}\n\nexport async function useAsyncNeonWebSocketKysely(\n connectionString: string,\n opt?: Pick<PgOpt, 'probe'> & KyselyOpts\n) {\n const pool = new Pool({ connectionString, max: 5 });\n const kysely = getKysely<DB>(new PostgresDialect({ pool }), opt ?? {});\n kysely.destroy = async () => {\n console.debug('Closing Neon WebSocket Kysely connection');\n await pool.end();\n };\n if (opt?.probe) await pool.query('select 1');\n return kysely;\n}\n\nexport function useControlPlaneKysely(postgres: Sql, options?: KyselyOpts) {\n return withControlPlaneSchema(\n useKysely<ControlPlaneDB>(postgres, options),\n 'public'\n );\n}\n\nexport async function useAsyncControlPlaneKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n return useControlPlaneKysely(\n await useAsyncPostgres(connectionString, opt),\n opt\n );\n}\n\nexport async function forceCloseAllPostgres(timeoutMs = 5000) {\n await Promise.race([\n Promise.all(\n _connections.map((pg) =>\n pg.end({ timeout: timeoutMs / 1000 }).catch(() => undefined)\n )\n ),\n new Promise((res) => setTimeout(res, timeoutMs + 1000)),\n ]);\n _connections = [];\n}\n"]}
@@ -8,5 +8,5 @@ export type DatabaseKysely<Database> = K<Database>;
8
8
  export type DatabaseTransaction<Database> = T<Database>;
9
9
  export type Transaction = DatabaseTransaction<DB>;
10
10
  export type ControlledTransaction<S extends string[] = string[]> = CT<DB, S>;
11
- export { sql } from 'kysely';
11
+ export { PostgresQueryCompiler, sql } from 'kysely';
12
12
  export type Kysely = DatabaseKysely<DB>;
@@ -1,5 +1,5 @@
1
1
  export { jsonArrayFrom } from 'kysely/helpers/postgres';
2
2
  export * from './domain';
3
3
  export * from './v1.generated';
4
- export { sql } from 'kysely';
4
+ export { PostgresQueryCompiler, sql } from 'kysely';
5
5
  //# sourceMappingURL=isomorphic.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"isomorphic.js","sourceRoot":"src/","sources":["isomorphic.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAQ/B,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC","sourcesContent":["import type {\n ControlledTransaction as CT,\n Kysely as K,\n Transaction as T,\n} from 'kysely';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n KyselyPlugin,\n Nullable,\n OperandExpression,\n PluginTransformQueryArgs,\n PluginTransformResultArgs,\n QueryResult,\n RootOperationNode,\n Selectable,\n SelectQueryBuilder,\n SqlBool,\n UnknownRow,\n Updateable,\n} from 'kysely';\nexport { jsonArrayFrom } from 'kysely/helpers/postgres';\nexport * from './domain';\nexport * from './v1.generated';\n\nimport type { DB } from './v1.generated';\n\nexport type DatabaseKysely<Database> = K<Database>;\nexport type DatabaseTransaction<Database> = T<Database>;\nexport type Transaction = DatabaseTransaction<DB>;\nexport type ControlledTransaction<S extends string[] = string[]> = CT<DB, S>;\nexport { sql } from 'kysely';\n\nexport type Kysely = DatabaseKysely<DB>;\n"]}
1
+ {"version":3,"file":"isomorphic.js","sourceRoot":"src/","sources":["isomorphic.ts"],"names":[],"mappings":"AA2BA,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,cAAc,UAAU,CAAC;AACzB,cAAc,gBAAgB,CAAC;AAQ/B,OAAO,EAAE,qBAAqB,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC","sourcesContent":["import type {\n ControlledTransaction as CT,\n Kysely as K,\n Transaction as T,\n} from 'kysely';\n\nexport type {\n ColumnType,\n Expression,\n ExpressionBuilder,\n ExpressionOrFactory,\n Generated,\n Insertable,\n JSONColumnType,\n KyselyPlugin,\n Nullable,\n OperandExpression,\n PluginTransformQueryArgs,\n PluginTransformResultArgs,\n QueryResult,\n RootOperationNode,\n Selectable,\n SelectQueryBuilder,\n SqlBool,\n UnknownRow,\n Updateable,\n} from 'kysely';\nexport { jsonArrayFrom } from 'kysely/helpers/postgres';\nexport * from './domain';\nexport * from './v1.generated';\n\nimport type { DB } from './v1.generated';\n\nexport type DatabaseKysely<Database> = K<Database>;\nexport type DatabaseTransaction<Database> = T<Database>;\nexport type Transaction = DatabaseTransaction<DB>;\nexport type ControlledTransaction<S extends string[] = string[]> = CT<DB, S>;\nexport { PostgresQueryCompiler, sql } from 'kysely';\n\nexport type Kysely = DatabaseKysely<DB>;\n"]}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.3.45-stage.1262",
6
+ "version": "1.3.45-stage.1279",
7
7
  "description": "",
8
8
  "main": "build/main/index.js",
9
9
  "module": "build/module/index.js",