pgserve 1.1.10 → 2.0.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.
Files changed (43) hide show
  1. package/.genie/brainstorms/pgserve-v2/DESIGN.md +174 -0
  2. package/.genie/wishes/pgserve-v2/BRIEF-from-genie-pgserve.md +99 -0
  3. package/.genie/wishes/pgserve-v2/WISH.md +442 -0
  4. package/.genie/wishes/release-system-genie-pattern/WISH.md +268 -0
  5. package/.genie/wishes/release-system-genie-pattern/validation.md +205 -0
  6. package/.github/workflows/ci.yml +8 -4
  7. package/.github/workflows/release.yml +233 -111
  8. package/.github/workflows/{build-all-platforms.yml → version.yml} +32 -8
  9. package/AGENTS.md +10 -8
  10. package/CHANGELOG.md +150 -0
  11. package/Makefile +18 -41
  12. package/README.md +186 -1
  13. package/SECURITY.md +109 -0
  14. package/bin/pglite-server.js +253 -1
  15. package/eslint.config.js +2 -0
  16. package/package.json +1 -1
  17. package/src/admin-client.js +171 -0
  18. package/src/audit.js +168 -0
  19. package/src/control-db.js +313 -0
  20. package/src/daemon-control.js +408 -0
  21. package/src/daemon-shared.js +18 -0
  22. package/src/daemon-tcp.js +296 -0
  23. package/src/daemon.js +629 -0
  24. package/src/fingerprint.js +453 -0
  25. package/src/gc.js +351 -0
  26. package/src/index.js +11 -0
  27. package/src/postgres.js +54 -0
  28. package/src/protocol.js +131 -0
  29. package/src/router.js +78 -5
  30. package/src/tenancy.js +75 -0
  31. package/src/tokens.js +102 -0
  32. package/tests/audit.test.js +189 -0
  33. package/tests/control-db.test.js +285 -0
  34. package/tests/daemon-fingerprint-integration.test.js +109 -0
  35. package/tests/daemon-pr24-regression.test.js +201 -0
  36. package/tests/fingerprint.test.js +249 -0
  37. package/tests/fixtures/240-orphan-seed.sql +30 -0
  38. package/tests/multi-tenant.test.js +164 -0
  39. package/tests/orphan-cleanup.test.js +390 -0
  40. package/tests/tcp-listen.test.js +368 -0
  41. package/tests/tenancy.test.js +403 -0
  42. package/.github/release.yml +0 -30
  43. package/scripts/release.cjs +0 -198
@@ -0,0 +1,249 @@
1
+ /**
2
+ * Tests for src/fingerprint.js — kernel-rooted peer identity.
3
+ *
4
+ * Coverage:
5
+ * - getPeerCred() returns the calling process's pid/uid/gid via SO_PEERCRED
6
+ * - findNearestPackageJson() walks upward; deepest match wins (monorepo)
7
+ * - derivePackageFingerprint() is stable across cwd changes in the same project
8
+ * - same name + different paths → different fingerprints
9
+ * - same path + different uid → different fingerprints
10
+ * - script fallback triggers when no package.json above cwd
11
+ * - end-to-end: handleControlAccept() emits a connection_routed audit entry
12
+ */
13
+
14
+ import { test, expect, beforeAll, beforeEach, afterEach } from 'bun:test';
15
+ import fs from 'fs';
16
+ import os from 'os';
17
+ import path from 'path';
18
+ import net from 'net';
19
+ import {
20
+ initFingerprintFfi,
21
+ getPeerCred,
22
+ findNearestPackageJson,
23
+ readPackageName,
24
+ derivePackageFingerprint,
25
+ deriveScriptFingerprint,
26
+ fingerprintFromCred,
27
+ handleControlAccept,
28
+ _setPeerCredImpl,
29
+ } from '../src/fingerprint.js';
30
+ import { configureAudit, AUDIT_EVENTS } from '../src/audit.js';
31
+
32
+ let scratch;
33
+
34
+ beforeAll(async () => {
35
+ await initFingerprintFfi();
36
+ });
37
+
38
+ beforeEach(() => {
39
+ scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'pgserve-fp-test-'));
40
+ configureAudit({
41
+ logFile: path.join(scratch, 'audit.log'),
42
+ target: 'file',
43
+ });
44
+ });
45
+
46
+ afterEach(() => {
47
+ _setPeerCredImpl(null);
48
+ try { fs.rmSync(scratch, { recursive: true, force: true }); } catch { /* noop */ }
49
+ });
50
+
51
+ // ---------------------------------------------------------------------------
52
+ // SO_PEERCRED smoke — proves the FFI path works end-to-end on this kernel.
53
+ // ---------------------------------------------------------------------------
54
+
55
+ test('getPeerCred reads kernel-attested pid/uid/gid via Unix socket pair', async () => {
56
+ const sockPath = path.join(scratch, 'peer.sock');
57
+ const expectedUid = process.getuid();
58
+ const expectedGid = process.getgid();
59
+ const expectedPid = process.pid;
60
+
61
+ const cred = await new Promise((resolve, reject) => {
62
+ const server = net.createServer((socket) => {
63
+ try {
64
+ const c = getPeerCred(socket);
65
+ socket.end();
66
+ server.close(() => resolve(c));
67
+ } catch (err) {
68
+ server.close(() => reject(err));
69
+ }
70
+ });
71
+ server.on('error', reject);
72
+ server.listen(sockPath, () => {
73
+ const client = net.createConnection(sockPath);
74
+ client.on('error', reject);
75
+ });
76
+ });
77
+
78
+ expect(cred.pid).toBe(expectedPid);
79
+ expect(cred.uid).toBe(expectedUid);
80
+ expect(cred.gid).toBe(expectedGid);
81
+ });
82
+
83
+ // ---------------------------------------------------------------------------
84
+ // Pure-function tests on derivation surface
85
+ // ---------------------------------------------------------------------------
86
+
87
+ test('fingerprint stable across cwd change in the same project', () => {
88
+ // Layout:
89
+ // <scratch>/proj/package.json (name=alpha)
90
+ // <scratch>/proj/sub/deep/
91
+ // Same project → same fingerprint regardless of starting cwd.
92
+ const proj = path.join(scratch, 'proj');
93
+ fs.mkdirSync(path.join(proj, 'sub', 'deep'), { recursive: true });
94
+ fs.writeFileSync(path.join(proj, 'package.json'), JSON.stringify({ name: 'alpha' }));
95
+
96
+ const root = findNearestPackageJson(proj);
97
+ const fromSub = findNearestPackageJson(path.join(proj, 'sub'));
98
+ const fromDeep = findNearestPackageJson(path.join(proj, 'sub', 'deep'));
99
+
100
+ expect(root).not.toBeNull();
101
+ expect(fromSub).toBe(root);
102
+ expect(fromDeep).toBe(root);
103
+
104
+ const fp1 = derivePackageFingerprint({ packageRealpath: root, name: 'alpha', uid: 1000 });
105
+ const fp2 = derivePackageFingerprint({ packageRealpath: fromSub, name: 'alpha', uid: 1000 });
106
+ const fp3 = derivePackageFingerprint({ packageRealpath: fromDeep, name: 'alpha', uid: 1000 });
107
+ expect(fp1).toBe(fp2);
108
+ expect(fp2).toBe(fp3);
109
+ expect(fp1).toMatch(/^[0-9a-f]{12}$/);
110
+ });
111
+
112
+ test('two projects with the same name but different paths get different fingerprints', () => {
113
+ const a = path.join(scratch, 'a-project');
114
+ const b = path.join(scratch, 'b-project');
115
+ fs.mkdirSync(a);
116
+ fs.mkdirSync(b);
117
+ fs.writeFileSync(path.join(a, 'package.json'), JSON.stringify({ name: 'shared' }));
118
+ fs.writeFileSync(path.join(b, 'package.json'), JSON.stringify({ name: 'shared' }));
119
+
120
+ const pa = findNearestPackageJson(a);
121
+ const pb = findNearestPackageJson(b);
122
+ expect(pa).not.toBe(pb);
123
+
124
+ const fpa = derivePackageFingerprint({ packageRealpath: pa, name: 'shared', uid: 1000 });
125
+ const fpb = derivePackageFingerprint({ packageRealpath: pb, name: 'shared', uid: 1000 });
126
+ expect(fpa).not.toBe(fpb);
127
+ });
128
+
129
+ test('same path + different uid → different fingerprints', () => {
130
+ const proj = path.join(scratch, 'multi-user');
131
+ fs.mkdirSync(proj);
132
+ fs.writeFileSync(path.join(proj, 'package.json'), JSON.stringify({ name: 'multi' }));
133
+ const realpath = findNearestPackageJson(proj);
134
+
135
+ const fp1000 = derivePackageFingerprint({ packageRealpath: realpath, name: 'multi', uid: 1000 });
136
+ const fp1001 = derivePackageFingerprint({ packageRealpath: realpath, name: 'multi', uid: 1001 });
137
+ expect(fp1000).not.toBe(fp1001);
138
+ });
139
+
140
+ test('script fallback triggered when no package.json above cwd', () => {
141
+ // Build an isolated path tree under scratch with no package.json anywhere.
142
+ // We point fingerprintFromCred at an override cwd inside scratch so the
143
+ // upward walk hits the filesystem root (no package.json in /tmp/.. either,
144
+ // because we use a deliberately ephemeral dir tree owned by the test).
145
+ const isolated = path.join(scratch, 'isolated', 'deep');
146
+ fs.mkdirSync(isolated, { recursive: true });
147
+
148
+ // Sanity: walking up from `isolated` finds no package.json (until at least
149
+ // /tmp/... or higher; we trust the host doesn't have one in /tmp).
150
+ // If the host *does* have one above /tmp, the result would still be deterministic
151
+ // and correct (mode='package'), but we want to test the script-fallback branch
152
+ // here. Mock findNearestPackageJson by passing a cwdOverride beneath a fake
153
+ // chroot — the easiest way is to walk to a path that we control: use an
154
+ // empty subtree under scratch and pretend the walk has hit the root.
155
+ const sentinelFile = findNearestPackageJson(isolated);
156
+ // If the host has no package.json anywhere up to /, sentinelFile is null.
157
+ // If it does, this assertion would falsely target the host's package.json.
158
+ // To make the test deterministic, we drive the script branch directly via
159
+ // deriveScriptFingerprint; the integration of "no package.json found" is
160
+ // covered by fingerprintFromCred's branch logic with cmdlineOverride.
161
+
162
+ const fp = deriveScriptFingerprint({
163
+ uid: 1000,
164
+ cwd: '/some/orphan/dir',
165
+ cmdline1: '/usr/local/bin/foo.js',
166
+ });
167
+ expect(fp).toMatch(/^[0-9a-f]{12}$/);
168
+
169
+ // Also verify fingerprintFromCred picks the script branch when cwdOverride
170
+ // points at a path with no ancestor package.json — we use a path under
171
+ // scratch since scratch itself has no package.json, and we pass cmdlineOverride.
172
+ const info = fingerprintFromCred(
173
+ { pid: 9999, uid: 1000, gid: 1000 },
174
+ {
175
+ cwdOverride: isolated,
176
+ cmdlineOverride: ['/usr/local/bin/bun', '/some/orphan/dir/foo.js'],
177
+ },
178
+ );
179
+ // sentinelFile may be null (script mode) or non-null (if host has /package.json upstream).
180
+ if (sentinelFile === null) {
181
+ expect(info.mode).toBe('script');
182
+ expect(info.fingerprint).toMatch(/^[0-9a-f]{12}$/);
183
+ expect(info.packageRealpath).toBeNull();
184
+ } else {
185
+ // Host has an ancestor package.json. Still verify that derivation produces
186
+ // a 12-hex value and that the 'package' branch was chosen — the
187
+ // script-fallback behavior is independently exercised by deriveScriptFingerprint above.
188
+ expect(info.mode).toBe('package');
189
+ expect(info.fingerprint).toMatch(/^[0-9a-f]{12}$/);
190
+ }
191
+ });
192
+
193
+ test('monorepo: nested package.json wins (deepest match)', () => {
194
+ // Layout:
195
+ // <scratch>/mono/package.json (name=workspace-root)
196
+ // <scratch>/mono/packages/api/package.json (name=api)
197
+ // <scratch>/mono/packages/api/src/
198
+ // Walking up from src/ must find the api package.json, not the workspace root.
199
+ const root = path.join(scratch, 'mono');
200
+ const api = path.join(root, 'packages', 'api');
201
+ const apiSrc = path.join(api, 'src');
202
+ fs.mkdirSync(apiSrc, { recursive: true });
203
+ fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ name: 'workspace-root' }));
204
+ fs.writeFileSync(path.join(api, 'package.json'), JSON.stringify({ name: 'api' }));
205
+
206
+ const found = findNearestPackageJson(apiSrc);
207
+ expect(found).toBe(fs.realpathSync(path.join(api, 'package.json')));
208
+ expect(readPackageName(found)).toBe('api');
209
+
210
+ const info = fingerprintFromCred(
211
+ { pid: 9999, uid: 1000, gid: 1000 },
212
+ { cwdOverride: apiSrc, cmdlineOverride: ['bun', 'src/index.js'] },
213
+ );
214
+ expect(info.mode).toBe('package');
215
+ expect(info.name).toBe('api');
216
+ expect(info.packageRealpath).toBe(found);
217
+ });
218
+
219
+ // ---------------------------------------------------------------------------
220
+ // End-to-end: handleControlAccept emits connection_routed
221
+ // ---------------------------------------------------------------------------
222
+
223
+ test('handleControlAccept emits a connection_routed audit event with 12-hex fingerprint', () => {
224
+ const proj = path.join(scratch, 'audit-target');
225
+ fs.mkdirSync(proj);
226
+ fs.writeFileSync(path.join(proj, 'package.json'), JSON.stringify({ name: 'audit-app' }));
227
+
228
+ // Stub peer-cred impl so we don't need a real socket.
229
+ _setPeerCredImpl(() => ({ pid: 4242, uid: 1000, gid: 1000 }));
230
+
231
+ const info = handleControlAccept(
232
+ { /* fake socket */ },
233
+ { cwdOverride: proj, cmdlineOverride: ['bun', 'index.js'] },
234
+ );
235
+
236
+ expect(info.fingerprint).toMatch(/^[0-9a-f]{12}$/);
237
+ expect(info.mode).toBe('package');
238
+ expect(info.name).toBe('audit-app');
239
+
240
+ const logFile = path.join(scratch, 'audit.log');
241
+ const lines = fs.readFileSync(logFile, 'utf8').trim().split('\n').filter(Boolean);
242
+ expect(lines.length).toBe(1);
243
+ const entry = JSON.parse(lines[0]);
244
+ expect(entry.event).toBe(AUDIT_EVENTS.CONNECTION_ROUTED);
245
+ expect(entry.fingerprint).toBe(info.fingerprint);
246
+ expect(entry.peer_pid).toBe(4242);
247
+ expect(entry.peer_uid).toBe(1000);
248
+ expect(entry.mode).toBe('package');
249
+ });
@@ -0,0 +1,30 @@
1
+ -- Group 5 — synthetic orphan fixture.
2
+ --
3
+ -- Seeds 240 rows in `pgserve_meta` with stale `last_connection_at`
4
+ -- (48 hours old, well past the 24h TTL) and a dead `liveness_pid`. Half
5
+ -- the rows use a guaranteed-out-of-range PID (2147483646, far above
6
+ -- Linux's pid_max ≤ 2^22 ≈ 4M); the other half use NULL so the sweep
7
+ -- exercises both audit code paths (`db_reaped_liveness` vs `db_reaped_ttl`).
8
+ --
9
+ -- The accompanying harness `tests/orphan-cleanup.test.js` runs this file
10
+ -- and then `CREATE DATABASE`s each row's `database_name` so the sweep
11
+ -- actually has something to DROP.
12
+
13
+ INSERT INTO pgserve_meta (
14
+ database_name,
15
+ fingerprint,
16
+ peer_uid,
17
+ package_realpath,
18
+ last_connection_at,
19
+ liveness_pid,
20
+ persist
21
+ )
22
+ SELECT
23
+ format('app_orphan_%s', lpad(to_hex(i), 12, '0')),
24
+ lpad(to_hex(i), 12, '0'),
25
+ 1000,
26
+ NULL,
27
+ now() - interval '48 hours',
28
+ CASE WHEN i % 2 = 0 THEN 2147483646 ELSE NULL END,
29
+ false
30
+ FROM generate_series(1, 240) AS i;
@@ -161,6 +161,170 @@ test('Multi-tenant router - multiple databases isolated', async () => {
161
161
  cleanup();
162
162
  });
163
163
 
164
+ test('Router - pre-handshake buffer is bounded (issue #18 root cause #2)', async () => {
165
+ // Regression test for issue #18: without a bound on state.buffer, a
166
+ // client that sends garbage and never completes the PG startup would
167
+ // grow router memory unbounded (traced to the production 74 GiB VmSize).
168
+ // After fix, the router must close the connection once the buffer
169
+ // exceeds MAX_STARTUP_BUFFER_SIZE (1 MiB).
170
+ cleanup();
171
+
172
+ const router = await startMultiTenantServer({
173
+ port: 15546,
174
+ baseDir: testDataDir,
175
+ logLevel: 'warn',
176
+ });
177
+
178
+ const net = await import('net');
179
+ const sock = net.connect(15546, '127.0.0.1');
180
+ await new Promise((resolve) => sock.once('connect', resolve));
181
+
182
+ const garbage = Buffer.alloc(256 * 1024, 0x41); // 256 KiB of 'A'
183
+ let closed = false;
184
+ sock.on('close', () => { closed = true; });
185
+
186
+ // Send 5 × 256 KiB = 1.25 MiB, exceeding the 1 MiB cap.
187
+ for (let i = 0; i < 5 && !closed; i++) {
188
+ await new Promise((resolve) => sock.write(garbage, resolve));
189
+ }
190
+
191
+ // Wait up to 2s for the proxy to close the connection.
192
+ const deadline = Date.now() + 2000;
193
+ while (!closed && Date.now() < deadline) {
194
+ await new Promise((r) => setTimeout(r, 50));
195
+ }
196
+
197
+ expect(closed).toBe(true);
198
+ sock.destroy();
199
+
200
+ await router.stop();
201
+ cleanup();
202
+ });
203
+
204
+ test('Router - socket state has startupInProgress flag (issue #18 root cause #1)', async () => {
205
+ // White-box regression test for the reentrancy guard. Without
206
+ // state.startupInProgress, two data events arriving during the first
207
+ // processStartupMessage() await would launch concurrent async tasks
208
+ // that race to assign state.pgSocket, leaking the loser. This test
209
+ // verifies the flag is wired into the state object.
210
+ cleanup();
211
+
212
+ const router = await startMultiTenantServer({
213
+ port: 15547,
214
+ baseDir: testDataDir,
215
+ logLevel: 'warn',
216
+ });
217
+
218
+ const net = await import('net');
219
+ const sock = net.connect(15547, '127.0.0.1');
220
+ await new Promise((resolve) => sock.once('connect', resolve));
221
+
222
+ // Router tracks client sockets in this.connections; introspect to pull
223
+ // the state object and confirm the flag exists and defaults to false.
224
+ // On some platforms (macOS in particular) the client-side 'connect'
225
+ // event fires slightly before the server-side handleSocketOpen runs,
226
+ // so poll until the router has registered the connection rather than
227
+ // asserting synchronously.
228
+ const deadline = Date.now() + 2000;
229
+ while (router.connections.size === 0 && Date.now() < deadline) {
230
+ await new Promise((r) => setTimeout(r, 20));
231
+ }
232
+ expect(router.connections.size).toBeGreaterThan(0);
233
+ const bunSocket = [...router.connections][0];
234
+ const state = router.socketState.get(bunSocket);
235
+ expect(state).toBeDefined();
236
+ expect(state.startupInProgress).toBe(false);
237
+
238
+ sock.destroy();
239
+ await router.stop();
240
+ cleanup();
241
+ });
242
+
243
+ test('PostgresManager - stop() nulls socketDir/databaseDir (issue #24)', async () => {
244
+ // Regression test for issue #24: router used to cache stale socketPath
245
+ // pointing to a directory that stop() had already rmSync'd. After fix,
246
+ // stop() nulls socketDir/databaseDir UNCONDITIONALLY so subsequent
247
+ // getSocketPath() returns null (forcing TCP fallback in the router).
248
+ cleanup();
249
+
250
+ const { PostgresManager } = await import('../src/postgres.js');
251
+ const { createLogger } = await import('../src/logger.js');
252
+ const pg = new PostgresManager({
253
+ port: 15543,
254
+ logger: createLogger({ level: 'warn' }),
255
+ });
256
+
257
+ await pg.start();
258
+ const socketPathBeforeStop = pg.getSocketPath();
259
+ expect(socketPathBeforeStop).not.toBeNull();
260
+ expect(fs.existsSync(pg.socketDir)).toBe(true);
261
+
262
+ await pg.stop();
263
+
264
+ // CORE ASSERTION: socketDir must be nulled after stop
265
+ expect(pg.socketDir).toBeNull();
266
+ expect(pg.getSocketPath()).toBeNull();
267
+ // databaseDir nulled only in memory mode (persistent mode keeps user-owned path)
268
+ expect(pg.databaseDir).toBeNull();
269
+ // And the dir on disk must actually be gone
270
+ // (socketPathBeforeStop points inside the deleted socketDir)
271
+ const staleSocketDir = path.dirname(socketPathBeforeStop);
272
+ expect(fs.existsSync(staleSocketDir)).toBe(false);
273
+ });
274
+
275
+ test('PostgresManager - start()+stop()+start() yields fresh socketDir (issue #24)', async () => {
276
+ // Regression test for issue #24: pgManager.start() called after stop()
277
+ // must produce a FRESH socketDir (different path). Without the fix, a
278
+ // re-entry guard was missing and socketDir could leak across restarts.
279
+ cleanup();
280
+
281
+ const { PostgresManager } = await import('../src/postgres.js');
282
+ const { createLogger } = await import('../src/logger.js');
283
+ const pg = new PostgresManager({
284
+ port: 15544,
285
+ logger: createLogger({ level: 'warn' }),
286
+ });
287
+
288
+ await pg.start();
289
+ const socketDir1 = pg.socketDir;
290
+ expect(socketDir1).not.toBeNull();
291
+
292
+ await pg.stop();
293
+ expect(pg.socketDir).toBeNull();
294
+
295
+ await pg.start();
296
+ const socketDir2 = pg.socketDir;
297
+ expect(socketDir2).not.toBeNull();
298
+ expect(socketDir2).not.toBe(socketDir1);
299
+ expect(fs.existsSync(socketDir2)).toBe(true);
300
+
301
+ await pg.stop();
302
+ });
303
+
304
+ test('PostgresManager - double start() is a no-op (issue #24 re-entry guard)', async () => {
305
+ // Without the guard, a second start() would overwrite socketDir/databaseDir
306
+ // and leak the previous tmp dir (the "1,457 stale sock dirs" symptom).
307
+ cleanup();
308
+
309
+ const { PostgresManager } = await import('../src/postgres.js');
310
+ const { createLogger } = await import('../src/logger.js');
311
+ const pg = new PostgresManager({
312
+ port: 15545,
313
+ logger: createLogger({ level: 'warn' }),
314
+ });
315
+
316
+ await pg.start();
317
+ const socketDir1 = pg.socketDir;
318
+
319
+ // Second start() should silently return the same instance without
320
+ // reassigning socketDir/databaseDir.
321
+ const result = await pg.start();
322
+ expect(result).toBe(pg);
323
+ expect(pg.socketDir).toBe(socketDir1);
324
+
325
+ await pg.stop();
326
+ });
327
+
164
328
  test('Multi-tenant router - instance reuse', async () => {
165
329
  cleanup();
166
330