effect-vercel 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,6 +13,7 @@ import * as AiGateway from "effect-vercel/AiGateway"
13
13
  | `effect-vercel/AiGatewayCredentials` | How AI Gateway requests authenticate |
14
14
  | `effect-vercel/Functions` | Bindings for `@vercel/functions`: `waitUntil` |
15
15
  | `effect-vercel/Oidc` | The OIDC token Vercel issues to a deployment |
16
+ | `effect-vercel/PgClient` | `@effect/sql-pg` pool that doesn't leave connections open on frozen instances |
16
17
  | `effect-vercel/RuntimeCache` | [Runtime Cache](https://vercel.com/docs/runtime-cache) for Effect `Persistence` |
17
18
 
18
19
  ## Install
@@ -21,6 +22,7 @@ import * as AiGateway from "effect-vercel/AiGateway"
21
22
  pnpm add effect-vercel effect
22
23
  pnpm add @effect/ai-anthropic # only for effect-vercel/AiGateway
23
24
  pnpm add @vercel/functions # only for effect-vercel/Functions and RuntimeCache
25
+ pnpm add @effect/sql-pg # only for effect-vercel/PgClient
24
26
  ```
25
27
 
26
28
  ## AI Gateway
@@ -144,6 +146,40 @@ const handler = Effect.gen(function* () {
144
146
  - Vercel stops the work when the Function reaches its maximum duration.
145
147
  - Off Vercel, `waitUntil` does nothing and the fiber runs in the background.
146
148
 
149
+ ## Postgres
150
+
151
+ `PgClient.layer` is `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up
152
+ until the pool has closed its connections.
153
+
154
+ Vercel freezes an instance once no request or `waitUntil` promise is left. A frozen instance
155
+ keeps its sockets open, so a pooler like PgBouncer still counts them against `max_client_conn`.
156
+ During a deploy many instances freeze at once, which can use up the limit. `attachDatabasePool`
157
+ from `@vercel/functions` handles this for node-postgres pools by waiting out the idle timeout
158
+ after each release. It doesn't support the Effect pool, which also closes an idle connection up
159
+ to twice its `idleTimeout` after its last use.
160
+
161
+ ```ts
162
+ import * as PgClient from "effect-vercel/PgClient"
163
+ import { Redacted } from "effect"
164
+
165
+ const SqlLive = PgClient.layer({
166
+ url: Redacted.make(process.env.DATABASE_URL!),
167
+ maxConnections: 3,
168
+ idleTimeout: "5 seconds",
169
+ })
170
+ ```
171
+
172
+ - It takes the same config as `@effect/sql-pg`, except `stream`: it opens each socket itself
173
+ and counts it until it closes. While a socket is open, one `waitUntil` promise keeps the
174
+ instance up.
175
+ - Each time a request takes a connection from the pool, the promise moves to that request, so a
176
+ busy instance never keeps a finished request open. The wait also ends a second before the
177
+ request's deadline.
178
+ - It moves the promise from `onAcquire`. An `onAcquire` in your config still runs, after.
179
+ - Keep `idleTimeout` short. The instance stays up until the pool closes its last idle
180
+ connection.
181
+ - Off Vercel, it behaves like the plain layer.
182
+
147
183
  ## Runtime Cache
148
184
 
149
185
  `RuntimeCache.layer` provides Effect's `Persistence`, stored in the Vercel Runtime Cache.
@@ -0,0 +1,17 @@
1
+ import { Layer } from "effect";
2
+ import { PgClient } from "@effect/sql-pg";
3
+ //#region src/PgClient.d.ts
4
+ /** `PgClient.PgPoolConfig` without `stream`, which this module sets. */
5
+ export type Config<R = never> = Omit<PgClient.PgPoolConfig<R>, "stream">;
6
+ /**
7
+ * `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up
8
+ * until the pool has closed every connection, or the request's deadline
9
+ * comes. Off Vercel it behaves like the plain layer.
10
+ *
11
+ * It opens each socket itself, through the `stream` option, and moves the
12
+ * wait to the request that takes each connection, through `onAcquire`. An
13
+ * `onAcquire` in `config` still runs, after that.
14
+ */
15
+ export declare const layer: <R = never>(config: Config<R>) => Layer.Layer<PgClient.PgClient | import("effect/sql/SqlClient").SqlClient, import("effect/sql/SqlError").SqlError, Exclude<R, import("effect/Scope").Scope | import("effect/reactivity/Reactivity").Reactivity>>;
16
+ //#endregion
17
+ //# sourceMappingURL=PgClient.d.ts.map
@@ -0,0 +1,119 @@
1
+ import { Duration, Effect, Layer, Redacted } from "effect";
2
+ import * as Net from "node:net";
3
+ import { PgClient } from "@effect/sql-pg";
4
+ import { constVoid } from "effect/Function";
5
+ //#region src/PgClient.ts
6
+ /**
7
+ * `@effect/sql-pg` for Vercel Functions: a pool that keeps the instance up
8
+ * while it has a connection open.
9
+ *
10
+ * Vercel freezes an instance once no request or `waitUntil` promise is left.
11
+ * A frozen instance keeps its sockets, so a pooler like PgBouncer keeps
12
+ * counting them as clients until the instance wakes up or dies. With many
13
+ * instances, as during a deploy, they fill up `max_client_conn`.
14
+ * `attachDatabasePool` from `@vercel/functions` waits out the idle timeout
15
+ * after each release, but it only supports node-postgres-like pools, and the
16
+ * Effect pool closes idle connections up to twice its `idleTimeout` after
17
+ * their last use. So this layer counts the pool's sockets and waits until
18
+ * the last one closes.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import * as PgClient from "effect-vercel/PgClient"
23
+ * import { Redacted } from "effect"
24
+ *
25
+ * const SqlLive = PgClient.layer({
26
+ * url: Redacted.make(process.env.DATABASE_URL!),
27
+ * idleTimeout: "5 seconds",
28
+ * })
29
+ * ```
30
+ */
31
+ /**
32
+ * Vercel exposes the current request's context on a well-known global symbol.
33
+ * `@vercel/functions` reads `waitUntil` and the deadline from there. Off
34
+ * Vercel, or outside a request, there is no `waitUntil`.
35
+ */
36
+ const REQUEST_CONTEXT = Symbol.for("@vercel/request-context");
37
+ const currentRequest = () => {
38
+ return globalThis[REQUEST_CONTEXT]?.get?.();
39
+ };
40
+ /**
41
+ * Keeps the instance up while a tracked socket is open. One `waitUntil`
42
+ * promise holds it, and `follow` moves it to the request running now, as
43
+ * `attachDatabasePool` does, so a busy instance never holds a finished
44
+ * request open. The wait also ends a second before the request's deadline.
45
+ * Outside a request, `follow` keeps the wait where it is.
46
+ */
47
+ const makeKeepAlive = () => {
48
+ let open = 0;
49
+ let release = constVoid;
50
+ const follow = () => {
51
+ const request = currentRequest();
52
+ if (open === 0 || request?.waitUntil === void 0) return;
53
+ release();
54
+ const cutoff = Date.parse(request.deadline ?? "") - 1e3;
55
+ request.waitUntil(new Promise((resolve) => {
56
+ const timer = Number.isNaN(cutoff) ? void 0 : setTimeout(resolve, cutoff - Date.now());
57
+ release = () => {
58
+ clearTimeout(timer);
59
+ resolve();
60
+ };
61
+ }));
62
+ };
63
+ const track = (socket) => {
64
+ open++;
65
+ follow();
66
+ socket.once("close", () => {
67
+ if (--open === 0) release();
68
+ });
69
+ return socket;
70
+ };
71
+ return {
72
+ follow,
73
+ track
74
+ };
75
+ };
76
+ /**
77
+ * Opens the TCP or Unix socket `@effect/sql-pg` would open for `config`.
78
+ * It still runs TLS and the startup on it.
79
+ */
80
+ const connect = (config) => {
81
+ const url = config.url && new URL(Redacted.value(config.url));
82
+ const param = (name) => url?.searchParams.getAll(name).at(-1);
83
+ const urlHost = url?.hostname && decodeURIComponent(url.hostname.replace(/^\[(.*)\]$/, "$1"));
84
+ const host = config.host ?? param("host") ?? (urlHost || "localhost");
85
+ const port = config.port ?? Number(param("port") ?? (url?.port || 5432));
86
+ const path = config.path ?? (host.startsWith("/") ? `${host}/.s.PGSQL.${port}` : void 0);
87
+ if (path !== void 0) return Net.connect({ path });
88
+ return Net.connect({
89
+ host,
90
+ port,
91
+ noDelay: true,
92
+ ...config.keepAlive !== void 0 && {
93
+ keepAlive: true,
94
+ keepAliveInitialDelay: Duration.toMillis(Duration.fromInputUnsafe(config.keepAlive))
95
+ }
96
+ });
97
+ };
98
+ /**
99
+ * `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up
100
+ * until the pool has closed every connection, or the request's deadline
101
+ * comes. Off Vercel it behaves like the plain layer.
102
+ *
103
+ * It opens each socket itself, through the `stream` option, and moves the
104
+ * wait to the request that takes each connection, through `onAcquire`. An
105
+ * `onAcquire` in `config` still runs, after that.
106
+ */
107
+ const layer = (config) => Layer.suspend(() => {
108
+ const keepAlive = makeKeepAlive();
109
+ const follow = Effect.sync(keepAlive.follow);
110
+ return PgClient.layer({
111
+ ...config,
112
+ stream: () => keepAlive.track(connect(config)),
113
+ onAcquire: config.onAcquire ? Effect.andThen(follow, config.onAcquire) : follow
114
+ });
115
+ });
116
+ //#endregion
117
+ export { layer };
118
+
119
+ //# sourceMappingURL=PgClient.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PgClient.js","names":["holder"],"sources":["../src/PgClient.ts"],"sourcesContent":["/**\n * `@effect/sql-pg` for Vercel Functions: a pool that keeps the instance up\n * while it has a connection open.\n *\n * Vercel freezes an instance once no request or `waitUntil` promise is left.\n * A frozen instance keeps its sockets, so a pooler like PgBouncer keeps\n * counting them as clients until the instance wakes up or dies. With many\n * instances, as during a deploy, they fill up `max_client_conn`.\n * `attachDatabasePool` from `@vercel/functions` waits out the idle timeout\n * after each release, but it only supports node-postgres-like pools, and the\n * Effect pool closes idle connections up to twice its `idleTimeout` after\n * their last use. So this layer counts the pool's sockets and waits until\n * the last one closes.\n *\n * @example\n * ```ts\n * import * as PgClient from \"effect-vercel/PgClient\"\n * import { Redacted } from \"effect\"\n *\n * const SqlLive = PgClient.layer({\n * url: Redacted.make(process.env.DATABASE_URL!),\n * idleTimeout: \"5 seconds\",\n * })\n * ```\n */\n\nimport * as Net from \"node:net\"\nimport { PgClient } from \"@effect/sql-pg\"\nimport { Duration, Effect, Layer, Redacted } from \"effect\"\nimport { constVoid } from \"effect/Function\"\n\n/**\n * Vercel exposes the current request's context on a well-known global symbol.\n * `@vercel/functions` reads `waitUntil` and the deadline from there. Off\n * Vercel, or outside a request, there is no `waitUntil`.\n */\nconst REQUEST_CONTEXT = Symbol.for(\"@vercel/request-context\")\n\ntype RequestContext = {\n readonly waitUntil?: (promise: Promise<unknown>) => void\n readonly deadline?: string\n}\n\nconst currentRequest = (): RequestContext | undefined => {\n const holder = globalThis as typeof globalThis & {\n [REQUEST_CONTEXT]?: { get?: () => RequestContext | undefined }\n }\n return holder[REQUEST_CONTEXT]?.get?.()\n}\n\n/**\n * Keeps the instance up while a tracked socket is open. One `waitUntil`\n * promise holds it, and `follow` moves it to the request running now, as\n * `attachDatabasePool` does, so a busy instance never holds a finished\n * request open. The wait also ends a second before the request's deadline.\n * Outside a request, `follow` keeps the wait where it is.\n */\nconst makeKeepAlive = () => {\n let open = 0\n let release = constVoid\n\n const follow = () => {\n const request = currentRequest()\n if (open === 0 || request?.waitUntil === undefined) return\n release()\n const cutoff = Date.parse(request.deadline ?? \"\") - 1_000\n request.waitUntil(\n new Promise<void>((resolve) => {\n const timer = Number.isNaN(cutoff) ? undefined : setTimeout(resolve, cutoff - Date.now())\n release = () => {\n clearTimeout(timer)\n resolve()\n }\n }),\n )\n }\n\n const track = (socket: Net.Socket) => {\n open++\n follow()\n socket.once(\"close\", () => {\n if (--open === 0) release()\n })\n return socket\n }\n\n return { follow, track }\n}\n\n/** `PgClient.PgPoolConfig` without `stream`, which this module sets. */\nexport type Config<R = never> = Omit<PgClient.PgPoolConfig<R>, \"stream\">\n\n/**\n * Opens the TCP or Unix socket `@effect/sql-pg` would open for `config`.\n * It still runs TLS and the startup on it.\n */\nconst connect = (config: Config<unknown>): Net.Socket => {\n const url = config.url && new URL(Redacted.value(config.url))\n const param = (name: string) => url?.searchParams.getAll(name).at(-1)\n const urlHost = url?.hostname && decodeURIComponent(url.hostname.replace(/^\\[(.*)\\]$/, \"$1\"))\n const host = config.host ?? param(\"host\") ?? (urlHost || \"localhost\")\n const port = config.port ?? Number(param(\"port\") ?? (url?.port || 5432))\n const path = config.path ?? (host.startsWith(\"/\") ? `${host}/.s.PGSQL.${port}` : undefined)\n if (path !== undefined) return Net.connect({ path })\n return Net.connect({\n host,\n port,\n noDelay: true,\n ...(config.keepAlive !== undefined && {\n keepAlive: true,\n keepAliveInitialDelay: Duration.toMillis(Duration.fromInputUnsafe(config.keepAlive)),\n }),\n })\n}\n\n/**\n * `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up\n * until the pool has closed every connection, or the request's deadline\n * comes. Off Vercel it behaves like the plain layer.\n *\n * It opens each socket itself, through the `stream` option, and moves the\n * wait to the request that takes each connection, through `onAcquire`. An\n * `onAcquire` in `config` still runs, after that.\n */\nexport const layer = <R = never>(config: Config<R>) =>\n Layer.suspend(() => {\n const keepAlive = makeKeepAlive()\n const follow = Effect.sync(keepAlive.follow)\n return PgClient.layer({\n ...config,\n stream: () => keepAlive.track(connect(config)),\n onAcquire: config.onAcquire ? Effect.andThen(follow, config.onAcquire) : follow,\n })\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,kBAAkB,OAAO,IAAI,yBAAyB;AAO5D,MAAM,uBAAmD;CAIvD,OAAOA,WAAO,gBAAgB,EAAE,MAAM;AACxC;;;;;;;;AASA,MAAM,sBAAsB;CAC1B,IAAI,OAAO;CACX,IAAI,UAAU;CAEd,MAAM,eAAe;EACnB,MAAM,UAAU,eAAe;EAC/B,IAAI,SAAS,KAAK,SAAS,cAAc,KAAA,GAAW;EACpD,QAAQ;EACR,MAAM,SAAS,KAAK,MAAM,QAAQ,YAAY,EAAE,IAAI;EACpD,QAAQ,UACN,IAAI,SAAe,YAAY;GAC7B,MAAM,QAAQ,OAAO,MAAM,MAAM,IAAI,KAAA,IAAY,WAAW,SAAS,SAAS,KAAK,IAAI,CAAC;GACxF,gBAAgB;IACd,aAAa,KAAK;IAClB,QAAQ;GACV;EACF,CAAC,CACH;CACF;CAEA,MAAM,SAAS,WAAuB;EACpC;EACA,OAAO;EACP,OAAO,KAAK,eAAe;GACzB,IAAI,EAAE,SAAS,GAAG,QAAQ;EAC5B,CAAC;EACD,OAAO;CACT;CAEA,OAAO;EAAE;EAAQ;CAAM;AACzB;;;;;AASA,MAAM,WAAW,WAAwC;CACvD,MAAM,MAAM,OAAO,OAAO,IAAI,IAAI,SAAS,MAAM,OAAO,GAAG,CAAC;CAC5D,MAAM,SAAS,SAAiB,KAAK,aAAa,OAAO,IAAI,CAAC,CAAC,GAAG,EAAE;CACpE,MAAM,UAAU,KAAK,YAAY,mBAAmB,IAAI,SAAS,QAAQ,cAAc,IAAI,CAAC;CAC5F,MAAM,OAAO,OAAO,QAAQ,MAAM,MAAM,MAAM,WAAW;CACzD,MAAM,OAAO,OAAO,QAAQ,OAAO,MAAM,MAAM,MAAM,KAAK,QAAQ,KAAK;CACvE,MAAM,OAAO,OAAO,SAAS,KAAK,WAAW,GAAG,IAAI,GAAG,KAAK,YAAY,SAAS,KAAA;CACjF,IAAI,SAAS,KAAA,GAAW,OAAO,IAAI,QAAQ,EAAE,KAAK,CAAC;CACnD,OAAO,IAAI,QAAQ;EACjB;EACA;EACA,SAAS;EACT,GAAI,OAAO,cAAc,KAAA,KAAa;GACpC,WAAW;GACX,uBAAuB,SAAS,SAAS,SAAS,gBAAgB,OAAO,SAAS,CAAC;EACrF;CACF,CAAC;AACH;;;;;;;;;;AAWA,MAAa,SAAoB,WAC/B,MAAM,cAAc;CAClB,MAAM,YAAY,cAAc;CAChC,MAAM,SAAS,OAAO,KAAK,UAAU,MAAM;CAC3C,OAAO,SAAS,MAAM;EACpB,GAAG;EACH,cAAc,UAAU,MAAM,QAAQ,MAAM,CAAC;EAC7C,WAAW,OAAO,YAAY,OAAO,QAAQ,QAAQ,OAAO,SAAS,IAAI;CAC3E,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "effect-vercel",
3
- "version": "0.5.0",
4
- "description": "Effect integrations for Vercel: AI Gateway, OIDC, Runtime Cache, waitUntil",
3
+ "version": "0.6.0",
4
+ "description": "Effect integrations for Vercel: AI Gateway, OIDC, Postgres pools, Runtime Cache, waitUntil",
5
5
  "keywords": [
6
6
  "ai-gateway",
7
7
  "effect",
8
8
  "effect-ai",
9
9
  "oidc",
10
+ "postgres",
10
11
  "runtime-cache",
11
12
  "vercel",
12
13
  "wait-until"
@@ -32,6 +33,7 @@
32
33
  "./AiGatewayCredentials": "./dist/AiGatewayCredentials.js",
33
34
  "./Functions": "./dist/Functions.js",
34
35
  "./Oidc": "./dist/Oidc.js",
36
+ "./PgClient": "./dist/PgClient.js",
35
37
  "./RuntimeCache": "./dist/RuntimeCache.js",
36
38
  "./package.json": "./package.json"
37
39
  },
@@ -50,10 +52,11 @@
50
52
  "devDependencies": {
51
53
  "@effect/ai-anthropic": "^4.0.0",
52
54
  "@effect/language-service": "^0.87.3",
55
+ "@effect/sql-pg": "^4.0.6",
53
56
  "@effect/vitest": "^4.0.0",
54
57
  "@types/node": "^24",
55
58
  "@vercel/functions": "^3.9.9",
56
- "effect": "^4.0.0",
59
+ "effect": "^4.0.6",
57
60
  "oxfmt": "^0.68.0",
58
61
  "oxlint": "^1.83.0",
59
62
  "tsdown": "^0.23.0",
@@ -62,6 +65,7 @@
62
65
  },
63
66
  "peerDependencies": {
64
67
  "@effect/ai-anthropic": "^4.0.0",
68
+ "@effect/sql-pg": "^4.0.6",
65
69
  "@vercel/functions": "^3.9.9",
66
70
  "effect": "^4.0.0"
67
71
  },
@@ -69,6 +73,9 @@
69
73
  "@effect/ai-anthropic": {
70
74
  "optional": true
71
75
  },
76
+ "@effect/sql-pg": {
77
+ "optional": true
78
+ },
72
79
  "@vercel/functions": {
73
80
  "optional": true
74
81
  }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * `@effect/sql-pg` for Vercel Functions: a pool that keeps the instance up
3
+ * while it has a connection open.
4
+ *
5
+ * Vercel freezes an instance once no request or `waitUntil` promise is left.
6
+ * A frozen instance keeps its sockets, so a pooler like PgBouncer keeps
7
+ * counting them as clients until the instance wakes up or dies. With many
8
+ * instances, as during a deploy, they fill up `max_client_conn`.
9
+ * `attachDatabasePool` from `@vercel/functions` waits out the idle timeout
10
+ * after each release, but it only supports node-postgres-like pools, and the
11
+ * Effect pool closes idle connections up to twice its `idleTimeout` after
12
+ * their last use. So this layer counts the pool's sockets and waits until
13
+ * the last one closes.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import * as PgClient from "effect-vercel/PgClient"
18
+ * import { Redacted } from "effect"
19
+ *
20
+ * const SqlLive = PgClient.layer({
21
+ * url: Redacted.make(process.env.DATABASE_URL!),
22
+ * idleTimeout: "5 seconds",
23
+ * })
24
+ * ```
25
+ */
26
+
27
+ import * as Net from "node:net"
28
+ import { PgClient } from "@effect/sql-pg"
29
+ import { Duration, Effect, Layer, Redacted } from "effect"
30
+ import { constVoid } from "effect/Function"
31
+
32
+ /**
33
+ * Vercel exposes the current request's context on a well-known global symbol.
34
+ * `@vercel/functions` reads `waitUntil` and the deadline from there. Off
35
+ * Vercel, or outside a request, there is no `waitUntil`.
36
+ */
37
+ const REQUEST_CONTEXT = Symbol.for("@vercel/request-context")
38
+
39
+ type RequestContext = {
40
+ readonly waitUntil?: (promise: Promise<unknown>) => void
41
+ readonly deadline?: string
42
+ }
43
+
44
+ const currentRequest = (): RequestContext | undefined => {
45
+ const holder = globalThis as typeof globalThis & {
46
+ [REQUEST_CONTEXT]?: { get?: () => RequestContext | undefined }
47
+ }
48
+ return holder[REQUEST_CONTEXT]?.get?.()
49
+ }
50
+
51
+ /**
52
+ * Keeps the instance up while a tracked socket is open. One `waitUntil`
53
+ * promise holds it, and `follow` moves it to the request running now, as
54
+ * `attachDatabasePool` does, so a busy instance never holds a finished
55
+ * request open. The wait also ends a second before the request's deadline.
56
+ * Outside a request, `follow` keeps the wait where it is.
57
+ */
58
+ const makeKeepAlive = () => {
59
+ let open = 0
60
+ let release = constVoid
61
+
62
+ const follow = () => {
63
+ const request = currentRequest()
64
+ if (open === 0 || request?.waitUntil === undefined) return
65
+ release()
66
+ const cutoff = Date.parse(request.deadline ?? "") - 1_000
67
+ request.waitUntil(
68
+ new Promise<void>((resolve) => {
69
+ const timer = Number.isNaN(cutoff) ? undefined : setTimeout(resolve, cutoff - Date.now())
70
+ release = () => {
71
+ clearTimeout(timer)
72
+ resolve()
73
+ }
74
+ }),
75
+ )
76
+ }
77
+
78
+ const track = (socket: Net.Socket) => {
79
+ open++
80
+ follow()
81
+ socket.once("close", () => {
82
+ if (--open === 0) release()
83
+ })
84
+ return socket
85
+ }
86
+
87
+ return { follow, track }
88
+ }
89
+
90
+ /** `PgClient.PgPoolConfig` without `stream`, which this module sets. */
91
+ export type Config<R = never> = Omit<PgClient.PgPoolConfig<R>, "stream">
92
+
93
+ /**
94
+ * Opens the TCP or Unix socket `@effect/sql-pg` would open for `config`.
95
+ * It still runs TLS and the startup on it.
96
+ */
97
+ const connect = (config: Config<unknown>): Net.Socket => {
98
+ const url = config.url && new URL(Redacted.value(config.url))
99
+ const param = (name: string) => url?.searchParams.getAll(name).at(-1)
100
+ const urlHost = url?.hostname && decodeURIComponent(url.hostname.replace(/^\[(.*)\]$/, "$1"))
101
+ const host = config.host ?? param("host") ?? (urlHost || "localhost")
102
+ const port = config.port ?? Number(param("port") ?? (url?.port || 5432))
103
+ const path = config.path ?? (host.startsWith("/") ? `${host}/.s.PGSQL.${port}` : undefined)
104
+ if (path !== undefined) return Net.connect({ path })
105
+ return Net.connect({
106
+ host,
107
+ port,
108
+ noDelay: true,
109
+ ...(config.keepAlive !== undefined && {
110
+ keepAlive: true,
111
+ keepAliveInitialDelay: Duration.toMillis(Duration.fromInputUnsafe(config.keepAlive)),
112
+ }),
113
+ })
114
+ }
115
+
116
+ /**
117
+ * `PgClient.layer` from `@effect/sql-pg` that keeps the Function instance up
118
+ * until the pool has closed every connection, or the request's deadline
119
+ * comes. Off Vercel it behaves like the plain layer.
120
+ *
121
+ * It opens each socket itself, through the `stream` option, and moves the
122
+ * wait to the request that takes each connection, through `onAcquire`. An
123
+ * `onAcquire` in `config` still runs, after that.
124
+ */
125
+ export const layer = <R = never>(config: Config<R>) =>
126
+ Layer.suspend(() => {
127
+ const keepAlive = makeKeepAlive()
128
+ const follow = Effect.sync(keepAlive.follow)
129
+ return PgClient.layer({
130
+ ...config,
131
+ stream: () => keepAlive.track(connect(config)),
132
+ onAcquire: config.onAcquire ? Effect.andThen(follow, config.onAcquire) : follow,
133
+ })
134
+ })