@rebasepro/cli 0.16.0 → 0.16.1-canary.g0d7af95

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 (54) hide show
  1. package/dist/bundle.d.ts +28 -2
  2. package/dist/commands/build.d.ts +10 -0
  3. package/dist/commands/cloud/context.d.ts +17 -1
  4. package/dist/commands/cloud/databases.d.ts +1 -0
  5. package/dist/commands/cloud/deploy.d.ts +58 -0
  6. package/dist/commands/cloud/deployments.d.ts +42 -0
  7. package/dist/commands/cloud/env.d.ts +1 -0
  8. package/dist/commands/cloud/extensions.d.ts +1 -0
  9. package/dist/commands/cloud/projects.d.ts +14 -4
  10. package/dist/commands/cloud/resources.d.ts +10 -1
  11. package/dist/commands/db.d.ts +16 -0
  12. package/dist/commands/dev.d.ts +11 -0
  13. package/dist/commands/doctor.d.ts +1 -1
  14. package/dist/commands/init.d.ts +1 -1
  15. package/dist/commands/resources.d.ts +1 -0
  16. package/dist/constraints-BK1_4vci.js +80 -0
  17. package/dist/constraints-BK1_4vci.js.map +1 -0
  18. package/dist/daemon-Bdl4lrdt.js +252 -0
  19. package/dist/daemon-Bdl4lrdt.js.map +1 -0
  20. package/dist/daemon-entry-Brq-S8XX.js +378 -0
  21. package/dist/daemon-entry-Brq-S8XX.js.map +1 -0
  22. package/dist/dev-db/__fixtures__/cli-entry.d.ts +1 -0
  23. package/dist/dev-db/constraints.d.ts +98 -0
  24. package/dist/dev-db/daemon-entry.d.ts +35 -0
  25. package/dist/dev-db/daemon.d.ts +92 -0
  26. package/dist/dev-db/notification-proxy.d.ts +102 -0
  27. package/dist/dev-db/prepare.d.ts +63 -0
  28. package/dist/dev-db/pull.d.ts +92 -0
  29. package/dist/dev-db/resolve.d.ts +66 -0
  30. package/dist/dev-db/state.d.ts +93 -0
  31. package/dist/function-portability.d.ts +45 -0
  32. package/dist/index.d.ts +17 -17
  33. package/dist/index.es.js +5638 -4098
  34. package/dist/index.es.js.map +1 -1
  35. package/dist/manifest.d.ts +24 -1
  36. package/dist/pull-DqPRu1te.js +167 -0
  37. package/dist/pull-DqPRu1te.js.map +1 -0
  38. package/dist/resources/derive.d.ts +47 -0
  39. package/dist/state-c0CJ6Kwb.js +190 -0
  40. package/dist/state-c0CJ6Kwb.js.map +1 -0
  41. package/dist/telemetry/consent.d.ts +1 -1
  42. package/dist/telemetry/index.d.ts +7 -7
  43. package/dist/utils/dev-preflight.d.ts +73 -0
  44. package/package.json +13 -8
  45. package/templates/eject/backend/src/index.ts +15 -8
  46. package/templates/eject/config/resources.ts +24 -0
  47. package/templates/template/AGENTS.md +1 -1
  48. package/templates/template/CLAUDE.md +1 -1
  49. package/templates/template/README.md +1 -1
  50. package/templates/template/ai-instructions.md +5 -2
  51. package/templates/template/backend/functions/hello.ts +43 -22
  52. package/templates/template/config/resources.ts +57 -0
  53. package/templates/template/docker-compose.yml +10 -1
  54. package/templates/template/gitignore +1 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"daemon-entry-Brq-S8XX.js","names":[],"sources":["../src/dev-db/notification-proxy.ts","../src/dev-db/daemon-entry.ts"],"sourcesContent":["/**\n * A transparent Postgres proxy that puts LISTEN/NOTIFY back.\n *\n * Without this, realtime does not work against the managed database — and it\n * fails silently, which is worse than failing. The reason is specific and\n * measurable:\n *\n * PGlite is a *single* backend session, and `PGLiteSocketServer` multiplexes\n * every client connection onto it. `LISTEN` is therefore session-wide: whichever\n * client issues it arms the whole database. But a `NotificationResponse` is an\n * asynchronous message with no request to answer, so the multiplexer hands it to\n * whichever socket happens to be reading the protocol stream at that moment —\n * which is the client that *caused* the notification, not the one that asked for\n * it.\n *\n * Measured against pglite-socket 0.2.9:\n *\n * LISTEN and NOTIFY on one connection → delivered\n * trigger-fired pg_notify, same connection → delivered\n * another connection causes the notify → NOT delivered to the listener\n * …and the same notification IS delivered to the notifier, which never asked\n *\n * The realtime engine listens on a dedicated connection and the writes come\n * from request connections, so it is exactly the broken case, every time.\n *\n * The fix is to stop treating a notification as belonging to one connection,\n * which for a single-session database is the truth anyway: this proxy watches\n * the server→client direction, and every `NotificationResponse` frame it sees is\n * copied to every other connected client. A client that never issued `LISTEN`\n * may receive one it did not ask for; `pg` raises a `notification` event nobody\n * has subscribed to, which costs nothing. A client that *did* ask now always\n * gets it, which is the whole point.\n *\n * Two properties make this safe rather than clever:\n *\n * - **It never parses SQL and never rewrites a byte.** Frames are forwarded\n * verbatim; the only edit is delivering a copy of one to more sockets.\n * - **Injection only happens on a message boundary.** The server→client stream\n * is reassembled into whole protocol messages before anything is written on,\n * so an injected frame can never land inside another message.\n *\n * This exists only for the managed development database. Against a real Postgres\n * there is no proxy, because there is no defect to correct.\n */\n\nimport net from \"net\";\n\n/** `NotificationResponse`. The one message type this proxy treats specially. */\nconst NOTIFICATION_RESPONSE = 0x41; // 'A'\n\n/**\n * The SSL negotiation request, which is the one thing on the wire that is not\n * a typed message.\n *\n * A client may open with an 8-byte `SSLRequest` (length 8, code 80877103), and\n * the server answers with a *single untyped byte* — `N` or `S`. Feeding that\n * byte to a parser expecting `type + Int32 length` would desynchronise the\n * stream for the rest of the connection, so it is recognised and passed through.\n */\nconst SSL_REQUEST_LENGTH = 8;\nconst SSL_REQUEST_CODE = 80877103;\n\nfunction isSslRequest(chunk: Buffer): boolean {\n return (\n chunk.length >= SSL_REQUEST_LENGTH &&\n chunk.readInt32BE(0) === SSL_REQUEST_LENGTH &&\n chunk.readInt32BE(4) === SSL_REQUEST_CODE\n );\n}\n\n/**\n * Reassembles a server→client byte stream into whole protocol messages.\n *\n * Every backend message is `Int8 type` + `Int32 length` + payload, where the\n * length counts itself but not the type byte. Anything shorter than a full\n * message is held until the rest arrives — TCP offers no guarantee that a\n * message arrives in one chunk, and a proxy that assumed otherwise would inject\n * into the middle of a row description under load.\n */\nexport class BackendMessageParser {\n private buffered: Buffer = Buffer.alloc(0);\n /** Set once the untyped SSL negotiation byte has been dealt with. */\n private awaitingSslReply = false;\n\n expectSslReply(): void {\n this.awaitingSslReply = true;\n }\n\n /** Feed bytes in; get whole messages out, in order. */\n push(chunk: Buffer): Buffer[] {\n const messages: Buffer[] = [];\n this.buffered = this.buffered.length === 0 ? chunk : Buffer.concat([this.buffered, chunk]);\n\n if (this.awaitingSslReply && this.buffered.length >= 1) {\n // Single untyped byte: 'N' (no SSL) or 'S' (proceed).\n messages.push(this.buffered.subarray(0, 1));\n this.buffered = this.buffered.subarray(1);\n this.awaitingSslReply = false;\n }\n\n while (this.buffered.length >= 5) {\n const length = this.buffered.readInt32BE(1);\n // A length below 4 cannot describe itself; the stream is not one we\n // understand, so stop parsing and let the rest through untouched\n // rather than guessing.\n if (length < 4) break;\n const total = length + 1;\n if (this.buffered.length < total) break;\n messages.push(this.buffered.subarray(0, total));\n this.buffered = this.buffered.subarray(total);\n }\n\n return messages;\n }\n\n /** Bytes held back because they are not yet a whole message. */\n get pending(): number {\n return this.buffered.length;\n }\n}\n\nexport function isNotificationFrame(message: Buffer): boolean {\n return message.length > 0 && message[0] === NOTIFICATION_RESPONSE;\n}\n\n/** Channel and payload of a NotificationResponse, for logging and tests. */\nexport function decodeNotification(message: Buffer): { channel: string; payload: string } | null {\n if (!isNotificationFrame(message) || message.length < 10) return null;\n // 1 type byte + 4 length + 4 process id, then two null-terminated strings.\n const body = message.subarray(9);\n const split = body.indexOf(0);\n if (split === -1) return null;\n const channel = body.subarray(0, split).toString(\"utf8\");\n const rest = body.subarray(split + 1);\n const end = rest.indexOf(0);\n\n return { channel, payload: (end === -1 ? rest : rest.subarray(0, end)).toString(\"utf8\") };\n}\n\nexport interface NotificationProxyOptions {\n /** Port clients connect to. */\n listenPort: number;\n /** Port the real PGlite socket server is on. */\n upstreamPort: number;\n host?: string;\n /** Called for every notification broadcast. For diagnostics and tests. */\n onNotification?: (channel: string, payload: string, copies: number) => void;\n}\n\ninterface Connection {\n client: net.Socket;\n upstream: net.Socket;\n parser: BackendMessageParser;\n}\n\n/**\n * The proxy itself.\n *\n * One upstream connection per client connection, so the multiplexer downstream\n * sees exactly what it would have seen without the proxy.\n */\nexport class NotificationProxy {\n private server: net.Server | null = null;\n private readonly connections = new Set<Connection>();\n\n constructor(private readonly options: NotificationProxyOptions) {}\n\n get connectionCount(): number {\n return this.connections.size;\n }\n\n start(): Promise<void> {\n const host = this.options.host ?? \"127.0.0.1\";\n\n return new Promise((resolve, reject) => {\n const server = net.createServer((client) => this.accept(client, host));\n server.once(\"error\", reject);\n server.listen(this.options.listenPort, host, () => {\n this.server = server;\n resolve();\n });\n });\n }\n\n private accept(client: net.Socket, host: string): void {\n const upstream = net.connect(this.options.upstreamPort, host);\n const connection: Connection = { client, upstream, parser: new BackendMessageParser() };\n this.connections.add(connection);\n\n // Nagle would batch a notification behind nothing at all, adding latency\n // to the one message whose entire value is arriving promptly.\n client.setNoDelay(true);\n upstream.setNoDelay(true);\n\n client.on(\"data\", (chunk: Buffer) => {\n if (isSslRequest(chunk)) connection.parser.expectSslReply();\n upstream.write(chunk);\n });\n\n upstream.on(\"data\", (chunk: Buffer) => {\n for (const message of connection.parser.push(chunk)) {\n client.write(message);\n if (isNotificationFrame(message)) this.broadcast(message, connection);\n }\n });\n\n const close = () => {\n this.connections.delete(connection);\n client.destroy();\n upstream.destroy();\n };\n client.on(\"close\", close);\n client.on(\"error\", close);\n upstream.on(\"close\", close);\n upstream.on(\"error\", close);\n }\n\n /**\n * Copy a notification to every other client.\n *\n * Written directly rather than through a parser: it is already a whole\n * message, and every other socket is only ever written whole messages, so\n * there is no boundary to land inside.\n */\n private broadcast(message: Buffer, origin: Connection): void {\n let copies = 0;\n for (const connection of this.connections) {\n if (connection === origin) continue;\n if (connection.client.destroyed || !connection.client.writable) continue;\n connection.client.write(message);\n copies += 1;\n }\n\n const decoded = decodeNotification(message);\n if (decoded) this.options.onNotification?.(decoded.channel, decoded.payload, copies);\n }\n\n async stop(): Promise<void> {\n for (const connection of [...this.connections]) {\n connection.client.destroy();\n connection.upstream.destroy();\n }\n this.connections.clear();\n\n const server = this.server;\n this.server = null;\n if (!server) return;\n\n await new Promise<void>((resolve) => server.close(() => resolve()));\n }\n}\n","/**\n * The managed database process: one PGlite instance behind a Postgres socket.\n *\n * Runs as `rebase __dev-db-daemon`, a hidden subcommand rather than a separate\n * build entry point, so the same resolution works from `src` under tsx and from\n * the bundled `dist` a published CLI ships — there is no second file for a\n * build config to forget.\n *\n * It is deliberately detached from whoever started it. `rebase db push` in one\n * terminal and `rebase dev` in another must reach the same database, because\n * two processes opening one PGlite data directory would corrupt it, so the\n * daemon belongs to the *project* rather than to a command. What starts it is\n * incidental; what stops it is an explicit `rebase db stop`, an idle timeout,\n * or the machine going away.\n *\n * PGlite is imported dynamically. It is an optional dependency carrying a 25MB\n * WASM build, and the cost of that must fall only on someone who actually uses\n * the managed database — never on `rebase init`, and never on a CLI startup\n * that is about to print help.\n */\n\nimport fs from \"fs\";\nimport net from \"net\";\n\nimport {\n MANAGED_SERVER_MAX_CONNECTIONS,\n PGLITE_EXTENSION_NAMES\n} from \"./constraints\";\nimport { NotificationProxy } from \"./notification-proxy\";\nimport { clearState, dataDir, findFreePort, writeState } from \"./state\";\n\n/** Shut down after this long with nothing connected. */\nconst DEFAULT_IDLE_TIMEOUT_MS = 30 * 60_000;\n\n/** How often to check for idleness. */\nconst IDLE_CHECK_INTERVAL_MS = 60_000;\n\nexport interface DaemonArgs {\n projectRoot: string;\n port: number;\n token: string;\n idleTimeoutMs: number;\n}\n\n/**\n * `--project <dir> --port <n> --token <t> [--idle-timeout <ms>]`.\n *\n * Every field is required and unvalidated input is fatal: this process is\n * spawned by the CLI, never typed by a person, so a malformed argument is a bug\n * in the caller and guessing would hide it.\n */\nexport function parseDaemonArgs(argv: readonly string[]): DaemonArgs {\n const take = (flag: string): string | null => {\n const index = argv.indexOf(flag);\n\n return index >= 0 && index + 1 < argv.length ? argv[index + 1] : null;\n };\n\n const projectRoot = take(\"--project\");\n const port = Number(take(\"--port\"));\n const token = take(\"--token\");\n const idleRaw = take(\"--idle-timeout\");\n\n if (!projectRoot) throw new Error(\"__dev-db-daemon: --project is required\");\n if (!Number.isInteger(port) || port <= 0 || port > 65535) {\n throw new Error(\"__dev-db-daemon: --port must be a valid port\");\n }\n if (!token) throw new Error(\"__dev-db-daemon: --token is required\");\n\n const idleTimeoutMs = idleRaw === null ? DEFAULT_IDLE_TIMEOUT_MS : Number(idleRaw);\n if (!Number.isFinite(idleTimeoutMs) || idleTimeoutMs < 0) {\n throw new Error(\"__dev-db-daemon: --idle-timeout must be a non-negative number of milliseconds\");\n }\n\n return { projectRoot, port, token, idleTimeoutMs };\n}\n\n/**\n * Load the extension bundles PGlite needs by name.\n *\n * `CREATE EXTENSION pg_trgm` cannot install anything on its own here — PGlite\n * resolves extensions from bundles handed to the constructor, and a missing one\n * fails at migration time with `extension \"pg_trgm\" is not available`, which\n * reads like a broken database rather than a missing import.\n */\nasync function loadExtensions(): Promise<Record<string, unknown>> {\n const extensions: Record<string, unknown> = {};\n for (const name of PGLITE_EXTENSION_NAMES) {\n const module = (await import(`@electric-sql/pglite/contrib/${name}`)) as Record<string, unknown>;\n const bundle = module[name];\n if (!bundle) {\n throw new Error(\n `@electric-sql/pglite/contrib/${name} did not export \"${name}\". ` +\n \"The installed PGlite version may not ship this extension.\"\n );\n }\n extensions[name] = bundle;\n }\n\n return extensions;\n}\n\n/**\n * A tiny sidecar listener that answers one question: \"are you the daemon this\n * state file describes?\"\n *\n * Liveness cannot be answered by the pid — after a reboot the number belongs to\n * something else — nor by the port alone, for the same reason. Both would let\n * Rebase send a migration to a stranger. So the daemon publishes a token on a\n * second loopback port and the answer is only yes when the token matches.\n */\nfunction startIdentityServer(token: string, onConnection: () => void): Promise<net.Server> {\n return new Promise((resolve, reject) => {\n const server = net.createServer((socket) => {\n onConnection();\n socket.end(`rebase-dev-db ${token}\\n`);\n });\n server.once(\"error\", reject);\n server.listen(0, \"127.0.0.1\", () => resolve(server));\n });\n}\n\nexport async function runDaemon(args: DaemonArgs): Promise<void> {\n const directory = dataDir(args.projectRoot);\n fs.mkdirSync(directory, { recursive: true });\n\n const { PGlite } = (await import(\"@electric-sql/pglite\")) as {\n PGlite: { create(options: unknown): Promise<unknown> };\n };\n const { PGLiteSocketServer } = (await import(\"@electric-sql/pglite-socket\")) as {\n PGLiteSocketServer: new (options: unknown) => {\n start(): Promise<void>;\n stop(): Promise<void>;\n getStats(): { activeConnections: number; queuedQueries: number };\n };\n };\n\n const extensions = await loadExtensions();\n const db = (await PGlite.create({ dataDir: directory, extensions })) as { close(): Promise<void> };\n\n // The socket server listens privately; clients reach it through the\n // notification proxy on `args.port`. Realtime does not work otherwise —\n // PGlite is one session, so a NotificationResponse is handed to whichever\n // socket is reading rather than to the one that issued LISTEN. See\n // `notification-proxy.ts` for the measurements.\n const upstreamPort = await findFreePort();\n const server = new PGLiteSocketServer({\n db,\n port: upstreamPort,\n host: \"127.0.0.1\",\n // Above the client pool limit so a second *non-transactional* client is\n // refused with a connection error rather than deadlocking the\n // multiplexer. See `constraints.ts` — the pool limit is what actually\n // prevents overlapping transactions.\n maxConnections: MANAGED_SERVER_MAX_CONNECTIONS\n });\n await server.start();\n\n const proxy = new NotificationProxy({\n listenPort: args.port,\n upstreamPort,\n onNotification: (channel, _payload, copies) => {\n if (copies > 0) process.stdout.write(`dev-db: relayed notification on ${channel} to ${copies} client(s)\\n`);\n }\n });\n await proxy.start();\n\n // \"Idle\" means nothing is connected to the *database*. An earlier version\n // tracked identity pings instead, which meant a daemon serving queries\n // steadily for an hour would decide it was idle and shut down under a\n // running dev server.\n let idleSince: number | null = Date.now();\n const identity = await startIdentityServer(args.token, () => {\n idleSince = null;\n });\n const identityAddress = identity.address();\n const identityPort = identityAddress !== null && typeof identityAddress !== \"string\" ? identityAddress.port : 0;\n\n writeState(args.projectRoot, {\n port: args.port,\n pid: process.pid,\n dataDir: directory,\n startedAt: new Date().toISOString(),\n token: args.token,\n identityPort\n });\n\n let shuttingDown = false;\n const shutdown = async (reason: string) => {\n if (shuttingDown) return;\n shuttingDown = true;\n process.stdout.write(`dev-db: stopping (${reason})\\n`);\n // The state file goes first: a command that reads it during shutdown\n // should conclude \"not running\" and start a fresh daemon, rather than\n // connect to a socket that is closing under it.\n clearState(args.projectRoot);\n try {\n await proxy.stop();\n } catch { /* already down */ }\n try {\n await server.stop();\n } catch { /* already down */ }\n identity.close();\n try {\n await db.close();\n } catch { /* already closed */ }\n process.exit(0);\n };\n\n process.on(\"SIGINT\", () => void shutdown(\"SIGINT\"));\n process.on(\"SIGTERM\", () => void shutdown(\"SIGTERM\"));\n // The parent going away must not take the database with it — the daemon\n // belongs to the project. But an orphan with nobody left to serve should\n // not outlive the session either, which is what the idle timer is for.\n process.on(\"disconnect\", () => { /* detached on purpose */ });\n\n if (args.idleTimeoutMs > 0) {\n const timer = setInterval(() => {\n const stats = server.getStats();\n const busy = stats.activeConnections > 0 || stats.queuedQueries > 0;\n if (busy) {\n idleSince = null;\n\n return;\n }\n if (idleSince === null) {\n idleSince = Date.now();\n\n return;\n }\n if (Date.now() - idleSince >= args.idleTimeoutMs) {\n void shutdown(`idle for ${Math.round(args.idleTimeoutMs / 60_000)} minutes`);\n }\n }, IDLE_CHECK_INTERVAL_MS);\n timer.unref();\n }\n\n process.stdout.write(`dev-db: ready on 127.0.0.1:${args.port}\\n`);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,IAAM,wBAAwB;;;;;;;;;;AAW9B,IAAM,qBAAqB;AAC3B,IAAM,mBAAmB;AAEzB,SAAS,aAAa,OAAwB;CAC1C,OACI,MAAM,UAAU,sBAChB,MAAM,YAAY,CAAC,MAAM,sBACzB,MAAM,YAAY,CAAC,MAAM;AAEjC;;;;;;;;;;AAWA,IAAa,uBAAb,MAAkC;CAC9B,WAA2B,OAAO,MAAM,CAAC;;CAEzC,mBAA2B;CAE3B,iBAAuB;EACnB,KAAK,mBAAmB;CAC5B;;CAGA,KAAK,OAAyB;EAC1B,MAAM,WAAqB,CAAC;EAC5B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CAAA,CAAM,MAAM;CAGhB,QAAQ,OAAO,MAAM,8BAA8B,KAAK,KAAK,GAAG;AACpE"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,98 @@
1
+ /**
2
+ * What PGlite can and cannot do as a development database, measured rather
3
+ * than assumed.
4
+ *
5
+ * Everything in this directory is shaped by four facts, each established by
6
+ * running it against `@electric-sql/pglite` 0.5.6 and
7
+ * `@electric-sql/pglite-socket` 0.2.9 rather than by reading their docs. They
8
+ * are recorded here because two of them are silent failures — the kind that
9
+ * make a developer lose an evening to a feature that reports success and does
10
+ * nothing.
11
+ *
12
+ * 1. **It is really PostgreSQL 18.3.** `select version()` over the socket
13
+ * returns `PostgreSQL 18.3 (PGlite 0.5.6) on wasm32`, which is the same
14
+ * major as the `postgres:18-alpine` the eject template ships. So a dev
15
+ * database here and a compose database there are the same Postgres, and
16
+ * schema behaviour does not diverge between them.
17
+ *
18
+ * 2. **`pg_trgm` and `unaccent` are available**, which is what search
19
+ * collections need. They are not installed by a bare `CREATE EXTENSION`,
20
+ * though — PGlite ships them as separate bundles that must be passed to the
21
+ * constructor, and without that `CREATE EXTENSION pg_trgm` fails with
22
+ * `extension "pg_trgm" is not available`. {@link PGLITE_EXTENSIONS} is that
23
+ * list, and it has to stay in step with what the schema generator emits.
24
+ *
25
+ * 3. **RLS is enforced exactly as it is on a real server.** With
26
+ * `SET LOCAL ROLE "rebase_user"` inside a transaction — which is how
27
+ * `PostgresBackendDriver` isolates every request — `current_user` becomes
28
+ * the restricted role, `session_user` stays the owner, and a policy using
29
+ * `current_setting('app.tenant')` filters rows correctly, including under
30
+ * `FORCE ROW LEVEL SECURITY`. Measured: an owner saw 3 rows and the
31
+ * role-switched transaction saw 2, with the cross-tenant probe returning 0.
32
+ * This is the one that mattered most: a dev database that quietly failed to
33
+ * apply RLS would give false confidence about the product's central claim.
34
+ *
35
+ * 4. **Concurrency is the real limit, and it fails badly.** PGlite is a single
36
+ * session, and `PGLiteSocketServer` multiplexes connections onto it. Two
37
+ * pooled clients that hold *overlapping transactions* deadlock — not error,
38
+ * hang — which is precisely what a request-per-transaction server does under
39
+ * any concurrent load. {@link MANAGED_POOL_MAX} is the answer: one client
40
+ * connection, so requests queue in the pool instead of deadlocking in the
41
+ * multiplexer. Measured: with a pool of 1, four concurrent queries and a
42
+ * role-switched RLS transaction all pass; with a pool of 5 the same script
43
+ * hangs indefinitely.
44
+ *
45
+ * 5. **LISTEN/NOTIFY needed repairing, and now works.** A notification is an
46
+ * asynchronous message with no request to answer, and the multiplexer hands
47
+ * it to whichever socket is reading rather than to the one that issued
48
+ * `LISTEN` — so a dedicated listener connection, which is exactly how the
49
+ * realtime engine works, received nothing while the *writer* received
50
+ * notifications it never asked for. `notification-proxy.ts` corrects that by
51
+ * copying every `NotificationResponse` frame to every client, which for a
52
+ * single-session database is simply the truth. Realtime therefore works
53
+ * against the managed database, with no change to the server: it does
54
+ * ordinary `LISTEN` over ordinary libpq.
55
+ */
56
+ /**
57
+ * Extensions to hand PGlite's constructor.
58
+ *
59
+ * `CREATE EXTENSION` alone cannot install these — PGlite resolves them from
60
+ * bundles supplied at construction time, so anything missing here is missing
61
+ * from the database no matter what the migration says.
62
+ */
63
+ export declare const PGLITE_EXTENSION_NAMES: readonly ["pg_trgm", "unaccent"];
64
+ /**
65
+ * Client connections the managed database tolerates: exactly one.
66
+ *
67
+ * Not a tuning choice. Two concurrent transactions over the socket
68
+ * multiplexer deadlock, and a request-per-transaction server produces those
69
+ * the moment two requests overlap. One connection converts that deadlock into
70
+ * ordinary queueing, which is slower and correct.
71
+ */
72
+ export declare const MANAGED_POOL_MAX = 1;
73
+ /**
74
+ * Connections the socket server will accept.
75
+ *
76
+ * Above {@link MANAGED_POOL_MAX} so that a second *non-transactional* client —
77
+ * `rebase db push` in another terminal while `rebase dev` runs — is refused
78
+ * with a connection error rather than corrupting the multiplexer. The pool
79
+ * limit is what prevents overlapping transactions; this only stops a stampede.
80
+ */
81
+ export declare const MANAGED_SERVER_MAX_CONNECTIONS = 4;
82
+ /** What a managed PGlite database cannot do, in the words the user needs. */
83
+ export interface ManagedLimitation {
84
+ /** Stable id, so a warning can be suppressed or tested for. */
85
+ id: string;
86
+ /** One line, naming the feature rather than the mechanism. */
87
+ summary: string;
88
+ /** What to do instead. Always a concrete command. */
89
+ remedy: string;
90
+ }
91
+ /**
92
+ * Announced at startup, every time, rather than discovered.
93
+ *
94
+ * A developer who does not know realtime is off will read the silence as a bug
95
+ * in their own code, which is a worse outcome than not offering the managed
96
+ * database at all.
97
+ */
98
+ export declare const MANAGED_LIMITATIONS: readonly ManagedLimitation[];
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The managed database process: one PGlite instance behind a Postgres socket.
3
+ *
4
+ * Runs as `rebase __dev-db-daemon`, a hidden subcommand rather than a separate
5
+ * build entry point, so the same resolution works from `src` under tsx and from
6
+ * the bundled `dist` a published CLI ships — there is no second file for a
7
+ * build config to forget.
8
+ *
9
+ * It is deliberately detached from whoever started it. `rebase db push` in one
10
+ * terminal and `rebase dev` in another must reach the same database, because
11
+ * two processes opening one PGlite data directory would corrupt it, so the
12
+ * daemon belongs to the *project* rather than to a command. What starts it is
13
+ * incidental; what stops it is an explicit `rebase db stop`, an idle timeout,
14
+ * or the machine going away.
15
+ *
16
+ * PGlite is imported dynamically. It is an optional dependency carrying a 25MB
17
+ * WASM build, and the cost of that must fall only on someone who actually uses
18
+ * the managed database — never on `rebase init`, and never on a CLI startup
19
+ * that is about to print help.
20
+ */
21
+ export interface DaemonArgs {
22
+ projectRoot: string;
23
+ port: number;
24
+ token: string;
25
+ idleTimeoutMs: number;
26
+ }
27
+ /**
28
+ * `--project <dir> --port <n> --token <t> [--idle-timeout <ms>]`.
29
+ *
30
+ * Every field is required and unvalidated input is fatal: this process is
31
+ * spawned by the CLI, never typed by a person, so a malformed argument is a bug
32
+ * in the caller and guessing would hide it.
33
+ */
34
+ export declare function parseDaemonArgs(argv: readonly string[]): DaemonArgs;
35
+ export declare function runDaemon(args: DaemonArgs): Promise<void>;
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Starting, finding and stopping the managed database, from the caller's side.
3
+ *
4
+ * Every command that needs Postgres calls {@link ensureManagedDatabase} and
5
+ * gets a connection string back. Whether that started a process or found one
6
+ * already running is not the caller's business, which is the point: `rebase
7
+ * db push`, `rebase dev` and `rebase studio` in three terminals must all reach
8
+ * the same database without coordinating, because two processes opening one
9
+ * PGlite data directory would corrupt it.
10
+ *
11
+ * The hard part is not starting the daemon; it is deciding whether the one the
12
+ * state file describes is still there. A pid can be recycled after a reboot and
13
+ * a port can be taken by a stranger, so believing either on its own would let
14
+ * Rebase send a migration somewhere unintended. {@link isDaemonAlive} asks the
15
+ * daemon to identify itself instead.
16
+ */
17
+ import { type DaemonState } from "./state.js";
18
+ export interface ManagedDatabase {
19
+ /** Connection string for this project's managed database. */
20
+ url: string;
21
+ /** Where the data lives, for diagnostics and `--reset`. */
22
+ dataDir: string;
23
+ port: number;
24
+ pid: number;
25
+ /** True when this call started the daemon rather than finding it. */
26
+ started: boolean;
27
+ }
28
+ export declare function managedUrl(port: number): string;
29
+ /**
30
+ * Ask the process behind a state record to prove it is the one we wrote down.
31
+ *
32
+ * A pid check alone answers "is *a* process running", and a port check alone
33
+ * answers "is *something* listening" — after a reboot both say yes about
34
+ * strangers. The daemon answers with the token from its own state file, so a
35
+ * match is the only evidence accepted.
36
+ */
37
+ export declare function isDaemonAlive(state: DaemonState): Promise<boolean>;
38
+ /** The running daemon for this project, or `null`. Never starts anything. */
39
+ export declare function findRunningDaemon(projectRoot: string): Promise<DaemonState | null>;
40
+ /**
41
+ * How to re-invoke ourselves, which differs between a published CLI and this
42
+ * repository.
43
+ *
44
+ * A published `rebase` is `node bin/rebase.js`, and re-running that is trivial.
45
+ * Inside the monorepo the entry is TypeScript, which plain `node` cannot load —
46
+ * so the daemon has to be started through the same loader that is running now.
47
+ * Getting this wrong fails as a spawn that exits instantly with a syntax error,
48
+ * which is why the caller reads `pglite.log` on failure.
49
+ */
50
+ export declare function resolveSpawn(entry: string): {
51
+ execPath: string;
52
+ prefixArgs: string[];
53
+ };
54
+ export interface EnsureOptions {
55
+ /** Silence the "starting…" progress line. */
56
+ quiet?: boolean;
57
+ /** Milliseconds of inactivity before the daemon exits. 0 disables. */
58
+ idleTimeoutMs?: number;
59
+ /** Where progress goes. Injected for tests. */
60
+ onProgress?: (message: string) => void;
61
+ /** Override how the daemon process is launched. For tests. */
62
+ spawn?: {
63
+ execPath: string;
64
+ prefixArgs: string[];
65
+ };
66
+ /**
67
+ * Override the CLI entry to re-invoke. For tests.
68
+ *
69
+ * Necessary because under a test runner `process.argv[1]` is the runner
70
+ * itself, which exists and is therefore accepted by {@link resolveCliEntry}
71
+ * — spawning vitest with `__dev-db-daemon` rather than the CLI.
72
+ */
73
+ entry?: string;
74
+ }
75
+ /**
76
+ * The project's managed database, started if it is not already running.
77
+ *
78
+ * Safe to call concurrently from several commands: the loser of the race finds
79
+ * the winner's state file during its poll and adopts it rather than starting a
80
+ * second daemon.
81
+ */
82
+ export declare function ensureManagedDatabase(projectRoot: string, options?: EnsureOptions): Promise<ManagedDatabase>;
83
+ /** Stop the daemon. Returns false when there was nothing running. */
84
+ export declare function stopManagedDatabase(projectRoot: string): Promise<boolean>;
85
+ /**
86
+ * Stop the daemon and delete the data directory.
87
+ *
88
+ * Destructive and deliberately not clever: it removes the whole directory
89
+ * rather than dropping schemas, because "give me an empty database" is the only
90
+ * thing anyone means by it.
91
+ */
92
+ export declare function resetManagedDatabase(projectRoot: string): Promise<void>;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * A transparent Postgres proxy that puts LISTEN/NOTIFY back.
3
+ *
4
+ * Without this, realtime does not work against the managed database — and it
5
+ * fails silently, which is worse than failing. The reason is specific and
6
+ * measurable:
7
+ *
8
+ * PGlite is a *single* backend session, and `PGLiteSocketServer` multiplexes
9
+ * every client connection onto it. `LISTEN` is therefore session-wide: whichever
10
+ * client issues it arms the whole database. But a `NotificationResponse` is an
11
+ * asynchronous message with no request to answer, so the multiplexer hands it to
12
+ * whichever socket happens to be reading the protocol stream at that moment —
13
+ * which is the client that *caused* the notification, not the one that asked for
14
+ * it.
15
+ *
16
+ * Measured against pglite-socket 0.2.9:
17
+ *
18
+ * LISTEN and NOTIFY on one connection → delivered
19
+ * trigger-fired pg_notify, same connection → delivered
20
+ * another connection causes the notify → NOT delivered to the listener
21
+ * …and the same notification IS delivered to the notifier, which never asked
22
+ *
23
+ * The realtime engine listens on a dedicated connection and the writes come
24
+ * from request connections, so it is exactly the broken case, every time.
25
+ *
26
+ * The fix is to stop treating a notification as belonging to one connection,
27
+ * which for a single-session database is the truth anyway: this proxy watches
28
+ * the server→client direction, and every `NotificationResponse` frame it sees is
29
+ * copied to every other connected client. A client that never issued `LISTEN`
30
+ * may receive one it did not ask for; `pg` raises a `notification` event nobody
31
+ * has subscribed to, which costs nothing. A client that *did* ask now always
32
+ * gets it, which is the whole point.
33
+ *
34
+ * Two properties make this safe rather than clever:
35
+ *
36
+ * - **It never parses SQL and never rewrites a byte.** Frames are forwarded
37
+ * verbatim; the only edit is delivering a copy of one to more sockets.
38
+ * - **Injection only happens on a message boundary.** The server→client stream
39
+ * is reassembled into whole protocol messages before anything is written on,
40
+ * so an injected frame can never land inside another message.
41
+ *
42
+ * This exists only for the managed development database. Against a real Postgres
43
+ * there is no proxy, because there is no defect to correct.
44
+ */
45
+ /**
46
+ * Reassembles a server→client byte stream into whole protocol messages.
47
+ *
48
+ * Every backend message is `Int8 type` + `Int32 length` + payload, where the
49
+ * length counts itself but not the type byte. Anything shorter than a full
50
+ * message is held until the rest arrives — TCP offers no guarantee that a
51
+ * message arrives in one chunk, and a proxy that assumed otherwise would inject
52
+ * into the middle of a row description under load.
53
+ */
54
+ export declare class BackendMessageParser {
55
+ private buffered;
56
+ /** Set once the untyped SSL negotiation byte has been dealt with. */
57
+ private awaitingSslReply;
58
+ expectSslReply(): void;
59
+ /** Feed bytes in; get whole messages out, in order. */
60
+ push(chunk: Buffer): Buffer[];
61
+ /** Bytes held back because they are not yet a whole message. */
62
+ get pending(): number;
63
+ }
64
+ export declare function isNotificationFrame(message: Buffer): boolean;
65
+ /** Channel and payload of a NotificationResponse, for logging and tests. */
66
+ export declare function decodeNotification(message: Buffer): {
67
+ channel: string;
68
+ payload: string;
69
+ } | null;
70
+ export interface NotificationProxyOptions {
71
+ /** Port clients connect to. */
72
+ listenPort: number;
73
+ /** Port the real PGlite socket server is on. */
74
+ upstreamPort: number;
75
+ host?: string;
76
+ /** Called for every notification broadcast. For diagnostics and tests. */
77
+ onNotification?: (channel: string, payload: string, copies: number) => void;
78
+ }
79
+ /**
80
+ * The proxy itself.
81
+ *
82
+ * One upstream connection per client connection, so the multiplexer downstream
83
+ * sees exactly what it would have seen without the proxy.
84
+ */
85
+ export declare class NotificationProxy {
86
+ private readonly options;
87
+ private server;
88
+ private readonly connections;
89
+ constructor(options: NotificationProxyOptions);
90
+ get connectionCount(): number;
91
+ start(): Promise<void>;
92
+ private accept;
93
+ /**
94
+ * Copy a notification to every other client.
95
+ *
96
+ * Written directly rather than through a parser: it is already a whole
97
+ * message, and every other socket is only ever written whole messages, so
98
+ * there is no boundary to land inside.
99
+ */
100
+ private broadcast;
101
+ stop(): Promise<void>;
102
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * The one place a command asks "which database, and how do I reach it?".
3
+ *
4
+ * Every command that touches Postgres — `dev`, and the whole `db` namespace
5
+ * through the driver plugin — goes through {@link prepareDatabaseEnv}. It
6
+ * resolves the ordering in `resolve.ts`, starts the managed database if that is
7
+ * what the ordering chose, and hands back the environment additions the child
8
+ * process needs.
9
+ *
10
+ * Two things it deliberately does *not* do:
11
+ *
12
+ * - **It never overwrites an existing `DATABASE_URL`.** When the developer has
13
+ * named a database, the returned environment is empty and the child inherits
14
+ * exactly what it would have inherited before this feature existed. A
15
+ * migration must never be redirected away from the database its author meant.
16
+ *
17
+ * - **It never starts anything for a command that is not going to connect.**
18
+ * `--help` and argument errors are handled by the caller before this is
19
+ * reached, because booting a Postgres to print usage would be absurd.
20
+ */
21
+ import { type DevDatabase } from "./resolve.js";
22
+ export interface PrepareOptions {
23
+ /** `--database-url <url>`. */
24
+ flagUrl?: string | null;
25
+ /** `--docker`. */
26
+ flagDocker?: boolean;
27
+ /** Suppress the "starting…" progress line. */
28
+ quiet?: boolean;
29
+ /** Where human-facing lines go. Defaults to stdout via the caller. */
30
+ onProgress?: (message: string) => void;
31
+ }
32
+ export interface PreparedDatabase {
33
+ /** What the resolver chose, for the banner and for tests. */
34
+ database: DevDatabase;
35
+ /**
36
+ * Variables to add to a child process's environment.
37
+ *
38
+ * Empty for an external database: the child already has what it needs, and
39
+ * adding to it could only do harm.
40
+ */
41
+ env: Record<string, string>;
42
+ /** One line naming the database, suitable for a startup banner. */
43
+ description: string;
44
+ /** Absolute path of the managed data directory, when there is one. */
45
+ dataDir?: string;
46
+ /** True when this call started the managed database rather than finding it. */
47
+ startedDaemon?: boolean;
48
+ }
49
+ /**
50
+ * Resolve, start if needed, and describe the database for this command.
51
+ *
52
+ * `projectRoot` is where the managed database's data lives, so two projects on
53
+ * one machine get two databases without either being told about the other.
54
+ */
55
+ export declare function prepareDatabaseEnv(projectRoot: string, options?: PrepareOptions): Promise<PreparedDatabase>;
56
+ /**
57
+ * The lines to print about a managed database, in the order to print them.
58
+ *
59
+ * Returned rather than printed so the caller decides where they go — `dev` has
60
+ * a banner, `db push` has a single line above its own output — and so a test can
61
+ * assert on them without capturing a stream.
62
+ */
63
+ export declare function managedNotices(prepared: PreparedDatabase): string[];
@@ -0,0 +1,92 @@
1
+ /**
2
+ * `rebase db pull` — copy a database's contents into local development.
3
+ *
4
+ * The common case is production into local, and the reason it exists is that
5
+ * the alternative is worse: without it people hand-roll a `pg_dump | psql` and
6
+ * get the flags wrong in ways that either fail loudly at 2am or, more often,
7
+ * quietly restore half a schema.
8
+ *
9
+ * Three things this command insists on, because copying a production database
10
+ * onto a laptop is a data-protection event whether or not anyone calls it one:
11
+ *
12
+ * 1. **It says what it is about to do, in full, before doing it** — which
13
+ * database it will read, which one it will overwrite, and where the data will
14
+ * come to rest on disk. The target path matters: people forget that
15
+ * `.rebase/pgdata` is a directory their backup software may be indexing.
16
+ *
17
+ * 2. **It refuses to run unattended without being told to.** The target is
18
+ * destroyed, so a mistyped `--from` with no confirmation would take the
19
+ * developer's working database with it.
20
+ *
21
+ * 3. **It will not write to a remote database.** The target is always the local
22
+ * development database; there is no flag that makes this push. A tool that
23
+ * can copy in both directions eventually copies in the wrong one.
24
+ *
25
+ * Anonymization is opt-in (`--anonymize`), which is a deliberate choice and not
26
+ * an obviously safe one — the flag nobody types is the flag nobody gets. It is
27
+ * a best-effort pass over columns whose *names* look like personal data, and
28
+ * {@link ANONYMIZE_PATTERNS} says exactly which. It cannot find personal data in
29
+ * a column called `notes`, and this file says so rather than implying a
30
+ * guarantee it cannot keep.
31
+ */
32
+ /**
33
+ * Column-name patterns the anonymizer overwrites.
34
+ *
35
+ * Names, not contents: inspecting values would be slower, and would still miss
36
+ * the same things. This is a reasonable-effort measure for making a local copy
37
+ * less dangerous, and it is not a compliance control.
38
+ */
39
+ export declare const ANONYMIZE_PATTERNS: readonly {
40
+ pattern: RegExp;
41
+ replacement: string;
42
+ }[];
43
+ export declare function shouldAnonymize(columnName: string): boolean;
44
+ export declare function replacementFor(columnName: string): string | null;
45
+ /** A text-ish column the anonymizer can overwrite without a type error. */
46
+ export interface ColumnRef {
47
+ schema: string;
48
+ table: string;
49
+ column: string;
50
+ dataType: string;
51
+ }
52
+ /**
53
+ * Anonymizable columns: name looks personal, and the type can hold the
54
+ * replacement.
55
+ *
56
+ * The type check is what stops this generating `UPDATE … SET user_id =
57
+ * 'Redacted'` for an integer column called `user_id_email_seq` and failing the
58
+ * whole pass on a technicality.
59
+ */
60
+ export declare function anonymizableColumns(columns: readonly ColumnRef[]): ColumnRef[];
61
+ /** `UPDATE` statements for one anonymization pass, in a stable order. */
62
+ export declare function anonymizeStatements(columns: readonly ColumnRef[]): string[];
63
+ /** Host and database of a connection string, with no credentials in it. */
64
+ export declare function describeTarget(connectionString: string): string;
65
+ export interface PullPlan {
66
+ /** Where the data comes from. */
67
+ source: string;
68
+ /** Where it lands. Always local. */
69
+ target: string;
70
+ anonymize: boolean;
71
+ /** Schemas to copy. Empty means every non-system schema. */
72
+ schemas: string[];
73
+ }
74
+ /**
75
+ * `pg_dump` arguments for the source.
76
+ *
77
+ * `--no-owner` and `--no-acl` because the roles on a production server do not
78
+ * exist locally, and without them every `ALTER … OWNER TO` in the dump fails and
79
+ * buries the real output in noise. `--format=custom` so `pg_restore` can be told
80
+ * to continue past errors selectively rather than all-or-nothing.
81
+ */
82
+ export declare function dumpArgs(plan: PullPlan): string[];
83
+ /**
84
+ * `pg_restore` arguments for the target.
85
+ *
86
+ * `--clean --if-exists` because a pull replaces what is there: restoring into a
87
+ * database that already has the tables would otherwise fail on every one of
88
+ * them. `--no-owner` for the same reason as the dump.
89
+ */
90
+ export declare function restoreArgs(plan: PullPlan, dumpFile: string): string[];
91
+ /** Is `pg_dump` on PATH, and what version? Checked before anything destructive. */
92
+ export declare function findPgDump(): Promise<string | null>;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Which database a command should talk to, decided in one place.
3
+ *
4
+ * Before this existed every command that needed Postgres read `DATABASE_URL`
5
+ * for itself, which was fine while there was exactly one answer. Introducing a
6
+ * managed database makes the question real: a project may have no
7
+ * `DATABASE_URL` at all and still expect `rebase db push` to work, and a
8
+ * project that *does* set one must never be quietly redirected somewhere else.
9
+ *
10
+ * So the rule is ordered and boring, and the order is the promise:
11
+ *
12
+ * 1. `--database-url <url>` — said on this command line, wins over everything
13
+ * 2. `DATABASE_URL` in the shell environment
14
+ * 3. `DATABASE_URL` in the project's `.env`
15
+ * 4. `--docker` / a manifest preference of `docker`
16
+ * 5. the managed PGlite database
17
+ *
18
+ * An explicit connection string always wins. That is the whole point of the
19
+ * override: someone pointing Rebase at their own Postgres — a colleague's
20
+ * staging box, a Neon branch, a container they manage — must get exactly that,
21
+ * with no cleverness in between. The managed database is what fills the vacuum
22
+ * when nobody has said anything, and it is the only case where the CLI picks.
23
+ *
24
+ * {@link resolveDevDatabase} is pure: inputs in, decision out, no filesystem
25
+ * and no process. Reading `.env` and starting a daemon happen elsewhere, so
26
+ * the ordering above can be tested without either.
27
+ */
28
+ /** Where the answer came from. Carried so diagnostics can name it. */
29
+ export type DevDatabaseSource =
30
+ /** `--database-url` on the command line. */
31
+ "flag"
32
+ /** `DATABASE_URL` in the shell environment. */
33
+ | "environment"
34
+ /** `DATABASE_URL` in the project's `.env`. */
35
+ | "env-file"
36
+ /** `--docker`, or `devDatabase: "docker"` in the manifest. */
37
+ | "docker"
38
+ /** Nobody said anything, so the managed database fills in. */
39
+ | "managed";
40
+ export type DevDatabase = {
41
+ kind: "external";
42
+ /** The connection string, exactly as given. Never rewritten. */
43
+ url: string;
44
+ source: Extract<DevDatabaseSource, "flag" | "environment" | "env-file">;
45
+ } | {
46
+ kind: "docker";
47
+ source: "docker";
48
+ } | {
49
+ kind: "managed";
50
+ source: "managed";
51
+ };
52
+ export interface ResolveDevDatabaseInput {
53
+ /** `--database-url <url>`, if given. */
54
+ flagUrl?: string | null;
55
+ /** `--docker`, if given. */
56
+ flagDocker?: boolean;
57
+ /** The shell environment. Only `DATABASE_URL` is read. */
58
+ env?: Record<string, string | undefined>;
59
+ /** Parsed `.env` from the project root. Only `DATABASE_URL` is read. */
60
+ envFile?: Record<string, string> | null;
61
+ /** `devDatabase` from `rebase.json`, if the project recorded a preference. */
62
+ manifestPreference?: "managed" | "docker" | null;
63
+ }
64
+ export declare function resolveDevDatabase(input?: ResolveDevDatabaseInput): DevDatabase;
65
+ /** One line for the startup banner, naming both the database and why. */
66
+ export declare function describeDevDatabase(database: DevDatabase): string;