@fuzdev/fuz_app 0.174.0 → 0.176.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 (86) hide show
  1. package/README.md +3 -2
  2. package/dist/actions/CLAUDE.md +10 -8
  3. package/dist/actions/action_codegen.js +3 -3
  4. package/dist/actions/ws_outbound_writer.d.ts +8 -8
  5. package/dist/actions/ws_outbound_writer.d.ts.map +1 -1
  6. package/dist/actions/ws_outbound_writer.js +8 -11
  7. package/dist/auth/keyring.d.ts +1 -1
  8. package/dist/auth/keyring.js +1 -1
  9. package/dist/db/CLAUDE.md +9 -0
  10. package/dist/db/fact_disk_storage.d.ts +3 -2
  11. package/dist/db/fact_disk_storage.d.ts.map +1 -1
  12. package/dist/db/fact_disk_storage.js +2 -1
  13. package/dist/db/postgres_url.d.ts +95 -0
  14. package/dist/db/postgres_url.d.ts.map +1 -0
  15. package/dist/db/postgres_url.js +167 -0
  16. package/dist/dev/setup.d.ts +101 -33
  17. package/dist/dev/setup.d.ts.map +1 -1
  18. package/dist/dev/setup.js +174 -71
  19. package/dist/env/dotenv.d.ts +1 -1
  20. package/dist/env/dotenv.d.ts.map +1 -1
  21. package/dist/env/dotenv.js +2 -3
  22. package/dist/http/CLAUDE.md +1 -1
  23. package/dist/http/body_limit.d.ts +10 -16
  24. package/dist/http/body_limit.d.ts.map +1 -1
  25. package/dist/http/body_limit.js +10 -16
  26. package/dist/runtime/deps.d.ts +81 -45
  27. package/dist/runtime/deps.d.ts.map +1 -1
  28. package/dist/runtime/deps.js +7 -2
  29. package/dist/runtime/mock.d.ts +63 -16
  30. package/dist/runtime/mock.d.ts.map +1 -1
  31. package/dist/runtime/mock.js +75 -35
  32. package/dist/runtime/node.d.ts +3 -2
  33. package/dist/runtime/node.d.ts.map +1 -1
  34. package/dist/runtime/node.js +20 -72
  35. package/dist/runtime/process_id.d.ts +24 -0
  36. package/dist/runtime/process_id.d.ts.map +1 -0
  37. package/dist/runtime/process_id.js +27 -0
  38. package/dist/runtime/secure_file.d.ts +8 -30
  39. package/dist/runtime/secure_file.d.ts.map +1 -1
  40. package/dist/runtime/secure_file.js +23 -51
  41. package/dist/server/app_server.d.ts +2 -2
  42. package/dist/server/static.d.ts +1 -1
  43. package/dist/testing/CLAUDE.md +28 -27
  44. package/dist/testing/cross_backend/backend_config.d.ts +7 -7
  45. package/dist/testing/cross_backend/backend_config.d.ts.map +1 -1
  46. package/dist/testing/cross_backend/bench/bench_report.d.ts +2 -2
  47. package/dist/testing/cross_backend/bench/bench_report.js +2 -2
  48. package/dist/testing/cross_backend/body_size_smuggling.d.ts.map +1 -1
  49. package/dist/testing/cross_backend/body_size_smuggling.js +1 -4
  50. package/dist/testing/cross_backend/bootstrap_backend.d.ts +1 -1
  51. package/dist/testing/cross_backend/capabilities.d.ts +2 -2
  52. package/dist/testing/cross_backend/create_cross_backend_global_setup.d.ts +2 -2
  53. package/dist/testing/cross_backend/create_cross_backend_global_setup.js +1 -1
  54. package/dist/testing/cross_backend/create_dual_spawn_global_setup.d.ts +2 -2
  55. package/dist/testing/cross_backend/default_backend_configs.d.ts +1 -1
  56. package/dist/testing/cross_backend/default_backend_configs.js +4 -4
  57. package/dist/testing/cross_backend/differential.d.ts +2 -2
  58. package/dist/testing/cross_backend/differential.js +6 -6
  59. package/dist/testing/cross_backend/fact_serving.js +1 -1
  60. package/dist/testing/cross_backend/make_cross_backend_project.d.ts +2 -2
  61. package/dist/testing/cross_backend/make_cross_backend_project.js +1 -1
  62. package/dist/testing/cross_backend/origin_conformance.json +1 -1
  63. package/dist/testing/cross_backend/postgres_database.d.ts +7 -21
  64. package/dist/testing/cross_backend/postgres_database.d.ts.map +1 -1
  65. package/dist/testing/cross_backend/postgres_database.js +24 -35
  66. package/dist/testing/cross_backend/testing_reset_actions.js +1 -1
  67. package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
  68. package/dist/testing/cross_backend/testing_server_bun.js +10 -12
  69. package/dist/testing/cross_backend/testing_server_core.d.ts +13 -17
  70. package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
  71. package/dist/testing/cross_backend/testing_server_core.js +1 -1
  72. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts +7 -17
  73. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
  74. package/dist/testing/cross_backend/ts_spine_backend_config.js +0 -51
  75. package/dist/testing/cross_backend/ws_outbound_overflow.js +2 -2
  76. package/dist/testing/daemon_info.d.ts +11 -6
  77. package/dist/testing/daemon_info.d.ts.map +1 -1
  78. package/dist/testing/daemon_info.js +11 -8
  79. package/dist/testing/middleware_gate_probes.js +2 -2
  80. package/package.json +11 -16
  81. package/dist/runtime/deno.d.ts +0 -21
  82. package/dist/runtime/deno.d.ts.map +0 -1
  83. package/dist/runtime/deno.js +0 -191
  84. package/dist/testing/cross_backend/testing_server_deno.d.ts +0 -5
  85. package/dist/testing/cross_backend/testing_server_deno.d.ts.map +0 -1
  86. package/dist/testing/cross_backend/testing_server_deno.js +0 -38
package/README.md CHANGED
@@ -10,7 +10,7 @@ It provides auth, sessions, accounts, database integration, middleware, and more
10
10
  the goal being an excellent and flexible whole-stack experience
11
11
  for developers, operators, and end-users.
12
12
 
13
- fuz_app supports deploying with Deno, Node, and Bun,
13
+ fuz_app supports deploying with Node and Bun,
14
14
  to servers, static websites, and local-first binaries, with more to come,
15
15
  eventually with compatible alternatives written in Rust.
16
16
 
@@ -35,6 +35,7 @@ Some projects using fuz_app are in progress and will be open source soon.
35
35
  Usage currently looks something like this:
36
36
 
37
37
  ```ts
38
+ import {serve} from '@hono/node-server';
38
39
  import {create_app_backend} from '@fuzdev/fuz_app/server/app_backend.ts';
39
40
  import {create_app_server} from '@fuzdev/fuz_app/server/app_server.ts';
40
41
 
@@ -42,7 +43,7 @@ const backend = await create_app_backend({...});
42
43
 
43
44
  const {app} = await create_app_server({backend, ...});
44
45
 
45
- Deno.serve({port: PORT, hostname: HOST}, app.fetch);
46
+ serve({fetch: app.fetch, port: PORT, hostname: HOST});
46
47
  ```
47
48
 
48
49
  See [CLAUDE.md](CLAUDE.md) for more usage patterns and the AI-generated docs,
@@ -532,11 +532,12 @@ which bypasses the budget. A send never waits and never throws.
532
532
  (`terminate()` on Node's `ws` and Bun; the client sees 1006), removing it and
533
533
  aborting its controller the same way. A close frame gets
534
534
  `WS_CLOSE_WRITE_TIMEOUT` (1 s), after which a socket still holding unwritten
535
- bytes is cut. Deno's standard `WebSocket` cannot be cut, so there a stalled
536
- connection is closed with `WS_CLOSE_OUTBOUND_OVERFLOW` instead, the close
537
- deadline cuts nothing, and the runtime keeps the socket until it or the
538
- client ends it. The timers are unref'd and cleared when the socket's close
539
- arrives (`remove_connection`, which is for a socket that has closed).
535
+ bytes is cut. A socket with no `terminate` (a web-standard `WebSocket`)
536
+ cannot be cut, so there a stalled connection is closed with
537
+ `WS_CLOSE_OUTBOUND_OVERFLOW` instead, the close deadline cuts nothing, and
538
+ the runtime keeps the socket until it or the client ends it. The timers are
539
+ unref'd and cleared when the socket's close arrives (`remove_connection`,
540
+ which is for a socket that has closed).
540
541
  - **Other closes.** A revocation, a cap eviction, the shutdown, and
541
542
  `close_connection` run the pump's rule once more: the frames still waiting
542
543
  are handed to the socket ahead of the close frame while its buffer is below
@@ -546,8 +547,9 @@ which bypasses the budget. A send never waits and never throws.
546
547
  - **After a close.** A closed or dropped connection is out of the map, so the
547
548
  per-account cap does not count it while its socket is still closing. Where
548
549
  the runtime reports its buffer, what it holds for that socket is less than
549
- the high-water mark plus one frame, and the close frame — for the close deadline on Node and Bun, and on
550
- Deno until the runtime or the client ends the socket.
550
+ the high-water mark plus one frame, and the close frame — for the close
551
+ deadline on Node and Bun, and on a socket that cannot be cut until the
552
+ runtime or the client ends it.
551
553
  - **Logging.** One `warn` per overflow (`ws: outbound queue overflow, closing
552
554
  the connection`, the Rust spine's line) or stall (`ws: write stalled,
553
555
  dropping the connection`), with `{connection_id, account_id, queued_bytes,
@@ -772,7 +774,7 @@ runs after server context is assembled so action lists can depend on
772
774
  Hono adapter helper. It is a factory because Node's `createNodeWebSocket({app})`
773
775
  needs the app `create_app_server` builds: it is called once, with that app
774
776
  (the same instance as `AppServer.app`), after the app exists and before the
775
- mount — and only when `ws_endpoints` resolves non-empty. Deno and Bun pass
777
+ mount — and only when `ws_endpoints` resolves non-empty. Bun passes
776
778
  `() => upgradeWebSocket`. `create_app_server` throws when `ws_endpoints`
777
779
  resolves non-empty but the factory is missing; a factory returning `[]` does
778
780
  NOT trip the check, so feature-flag gated WS surfaces stay safe.
@@ -44,9 +44,9 @@ export const is_protocol_action_method = (method) => PROTOCOL_METHOD_SET.has(met
44
44
  * imports.add('./action_specs.ts', '* as specs');
45
45
  *
46
46
  * // Generates:
47
- * // import type {Foo, Bar} from './types.js';
48
- * // import {helper, type HelperOptions} from './utils.js';
49
- * // import * as specs from './action_specs.js';
47
+ * // import type {Foo, Bar} from './types.ts';
48
+ * // import {helper, type HelperOptions} from './utils.ts';
49
+ * // import * as specs from './action_specs.ts';
50
50
  * ```
51
51
  */
52
52
  export class ImportBuilder {
@@ -57,13 +57,13 @@
57
57
  * ## Runtimes
58
58
  *
59
59
  * The buffer is read through the socket Hono's `WSContext` wraps (`raw`):
60
- * `bufferedAmount` on Node's `ws` and on Deno's standard `WebSocket`,
60
+ * `bufferedAmount` on Node's `ws` (and on a web-standard `WebSocket`),
61
61
  * `getBufferedAmount()` on Bun's `ServerWebSocket`. A socket that exposes
62
62
  * neither reads as never backed up: its frames are still budgeted while they
63
63
  * wait for the pump, but nothing detects a stall. The cut is `terminate()`,
64
- * which Node's `ws` and Bun's `ServerWebSocket` have. Deno's standard
65
- * `WebSocket` has no way to drop a socket without the closing handshake, so
66
- * there a stalled connection is closed with `WS_CLOSE_OUTBOUND_OVERFLOW`
64
+ * which Node's `ws` and Bun's `ServerWebSocket` have. A socket with no
65
+ * `terminate` (a web-standard `WebSocket` has no way to drop a socket without
66
+ * the closing handshake) is closed at a stall with `WS_CLOSE_OUTBOUND_OVERFLOW`
67
67
  * instead — a frame the client reads only if it starts reading again — and
68
68
  * the close deadline cuts nothing: a closed socket whose client is not reading
69
69
  * stays in the runtime, holding what its buffer held at the close ("Closing"),
@@ -94,10 +94,10 @@ export declare const WS_WRITE_STALL_TIMEOUT = 10000;
94
94
  export declare const WS_CLOSE_WRITE_TIMEOUT = 1000;
95
95
  /**
96
96
  * How many unwritten bytes a socket's buffer may hold before the pump stops
97
- * handing it waiting frames. Below it the pump keeps writing, so a runtime
98
- * that reports a frame as buffered until its write completes (Deno) is not
99
- * held to one frame per turn; at or over it frames wait in the budgeted queue
100
- * instead of the runtime's unbounded buffer.
97
+ * handing it waiting frames. Below it the pump keeps writing, so a socket
98
+ * holding a little unwritten data is not held to one frame per turn; at or
99
+ * over it frames wait in the budgeted queue instead of the runtime's
100
+ * unbounded buffer.
101
101
  */
102
102
  export declare const WS_WRITE_HIGH_WATER_BYTES: number;
103
103
  /** The outcome of one `WsOutboundWriter.push`. */
@@ -1 +1 @@
1
- {"version":3,"file":"ws_outbound_writer.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/actions/ws_outbound_writer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EAAkB,KAAK,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAOvF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAE7C;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,OAAQ,CAAC;AAE5C;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,QAAY,CAAC;AAQnD,kDAAkD;AAClD,MAAM,MAAM,cAAc;AACzB,yDAAyD;AACvD;IAAE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Q,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IAC3C,8EAA8E;IAC9E,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACnB;AA2CD;;;GAGG;AACH,qBAAa,gBAAgB;;gBA+BhB,OAAO,EAAE,uBAAuB;IAO5C,4EAA4E;IAC5E,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,qCAAqC;IACrC,IAAI,aAAa,IAAI,MAAM,CAE1B;IAED;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,cAAc;IAsBlD;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,iBAAiB,EAAE,UAAU,CAAC,EAAE,MAAM,IAAI,GAAG,IAAI;IA4B9F;;;;OAIG;IACH,OAAO,IAAI,IAAI;CAoJf"}
@@ -57,13 +57,13 @@
57
57
  * ## Runtimes
58
58
  *
59
59
  * The buffer is read through the socket Hono's `WSContext` wraps (`raw`):
60
- * `bufferedAmount` on Node's `ws` and on Deno's standard `WebSocket`,
60
+ * `bufferedAmount` on Node's `ws` (and on a web-standard `WebSocket`),
61
61
  * `getBufferedAmount()` on Bun's `ServerWebSocket`. A socket that exposes
62
62
  * neither reads as never backed up: its frames are still budgeted while they
63
63
  * wait for the pump, but nothing detects a stall. The cut is `terminate()`,
64
- * which Node's `ws` and Bun's `ServerWebSocket` have. Deno's standard
65
- * `WebSocket` has no way to drop a socket without the closing handshake, so
66
- * there a stalled connection is closed with `WS_CLOSE_OUTBOUND_OVERFLOW`
64
+ * which Node's `ws` and Bun's `ServerWebSocket` have. A socket with no
65
+ * `terminate` (a web-standard `WebSocket` has no way to drop a socket without
66
+ * the closing handshake) is closed at a stall with `WS_CLOSE_OUTBOUND_OVERFLOW`
67
67
  * instead — a frame the client reads only if it starts reading again — and
68
68
  * the close deadline cuts nothing: a closed socket whose client is not reading
69
69
  * stays in the runtime, holding what its buffer held at the close ("Closing"),
@@ -93,10 +93,10 @@ export const WS_WRITE_STALL_TIMEOUT = 10_000;
93
93
  export const WS_CLOSE_WRITE_TIMEOUT = 1_000;
94
94
  /**
95
95
  * How many unwritten bytes a socket's buffer may hold before the pump stops
96
- * handing it waiting frames. Below it the pump keeps writing, so a runtime
97
- * that reports a frame as buffered until its write completes (Deno) is not
98
- * held to one frame per turn; at or over it frames wait in the budgeted queue
99
- * instead of the runtime's unbounded buffer.
96
+ * handing it waiting frames. Below it the pump keeps writing, so a socket
97
+ * holding a little unwritten data is not held to one frame per turn; at or
98
+ * over it frames wait in the budgeted queue instead of the runtime's
99
+ * unbounded buffer.
100
100
  */
101
101
  export const WS_WRITE_HIGH_WATER_BYTES = 64 * 1024;
102
102
  /** The longest wait between two reads of a backed-up socket's buffer. */
@@ -202,9 +202,6 @@ export class WsOutboundWriter {
202
202
  this.#schedule_pump();
203
203
  if (!this.#write(text))
204
204
  return { kind: 'refused' };
205
- // TODO: a runtime that reports a frame as buffered until its write
206
- // completes (Deno) arms the deadline and the poll here and clears both
207
- // a tick later, for each idle send
208
205
  this.#watch(read_buffered_bytes(this.#ws));
209
206
  return { kind: 'accepted' };
210
207
  }
@@ -59,7 +59,7 @@ export type CreateKeyringResult = {
59
59
  * cookies. Twin of the Rust `Keyring::new`.
60
60
  *
61
61
  * Returns a discriminated union so callers handle exit/logging their own way
62
- * (e.g. `Deno.exit(1)` vs `runtime.exit(1)`). CryptoKeys are cached on first use.
62
+ * (e.g. `process.exit(1)` vs `runtime.exit(1)`). CryptoKeys are cached on first use.
63
63
  *
64
64
  * **Key rotation is an operational concern.** Put the new key first and keep
65
65
  * the old one after it: new cookies are signed with the new key, and cookies
@@ -40,7 +40,7 @@ const encoder = new TextEncoder();
40
40
  * cookies. Twin of the Rust `Keyring::new`.
41
41
  *
42
42
  * Returns a discriminated union so callers handle exit/logging their own way
43
- * (e.g. `Deno.exit(1)` vs `runtime.exit(1)`). CryptoKeys are cached on first use.
43
+ * (e.g. `process.exit(1)` vs `runtime.exit(1)`). CryptoKeys are cached on first use.
44
44
  *
45
45
  * **Key rotation is an operational concern.** Put the new key first and keep
46
46
  * the old one after it: new cookies are signed with the new key, and cookies
package/dist/db/CLAUDE.md CHANGED
@@ -21,6 +21,15 @@ the code.
21
21
  - `create_db.ts` — URL-based driver auto-detection (`postgres` /
22
22
  `pglite-file` for `file://` / `pglet-memory` for `memory://`, over
23
23
  `@fuzdev/pglet-wasm`). An in-memory PGlite is `create_pglite_db(new PGlite())`.
24
+ - `postgres_url.ts` — the libpq side of a Postgres `DATABASE_URL`, pure:
25
+ `parse_db_name`, `to_maintenance_database_url` (database swapped for
26
+ `postgres`, or `template1` for a target named `postgres`; password removed),
27
+ `to_postgres_password_env` (`{PGPASSWORD}` or `{}`), `to_createdb_args` /
28
+ `to_dropdb_args` (`--no-password`, `--maintenance-db`, `--` before the name),
29
+ `is_createdb_already_exists`, and `rewrite_query_pairs` — every query rewrite
30
+ edits raw pairs, since `URLSearchParams` reserializes `%20` as `+`, which
31
+ libpq reads literally. Used by `dev/setup.ts` and
32
+ `testing/cross_backend/postgres_database.ts`.
24
33
  - `db_pg.ts` / `db_pglite.ts` / `db_pglet.ts` — driver adapters. `db_pg.ts`'s
25
34
  `create_pg_pool` builds the pool with a bounded checkout
26
35
  (`connectionTimeoutMillis`, `DEFAULT_DB_CHECKOUT_TIMEOUT_MS`) whose failure
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Filesystem CAS for fact bytes stored outside Postgres — the disk half of
3
3
  * `PgFactStore`, threaded over the injectable `runtime/*Deps` rather than raw
4
- * `node:fs`, so it runs unchanged under Node, Deno, and a mock runtime.
4
+ * `node:fs`, so it runs unchanged over `create_node_runtime` and the mock
5
+ * runtime.
5
6
  *
6
7
  * Large facts (over the embedded threshold) live on disk at the canonical
7
8
  * sharded layout `<facts_dir>/<shard>/<rest>` — `<shard>` is the first 2 hex
@@ -39,7 +40,7 @@ export declare const FACT_TMP_DIRNAME = ".tmp";
39
40
  export declare const FACT_TMP_ORPHAN_MAX_AGE_MS: number;
40
41
  /**
41
42
  * Filesystem capabilities the disk CAS needs, drawn from `runtime/deps.ts`. A
42
- * full `RuntimeDeps` (Node or Deno) satisfies this; each function below picks
43
+ * full `RuntimeDeps` satisfies this; each function below picks
43
44
  * the narrow subset it actually uses.
44
45
  */
45
46
  export type FactDiskStorageDeps = Pick<FsReadDeps, 'stat' | 'readdir' | 'read_file'> & Pick<FsWriteDeps, 'mkdir' | 'rename' | 'write_file' | 'fsync'> & Pick<FsStreamDeps, 'write_file_stream'> & Pick<FsRemoveDeps, 'remove'>;
@@ -1 +1 @@
1
- {"version":3,"file":"fact_disk_storage.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/db/fact_disk_storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAQH,OAAO,EAAoB,KAAK,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AACnF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE9F,OAAO,EAIN,KAAK,WAAW,EAChB,MAAM,oBAAoB,CAAC;AAO5B,sEAAsE;AACtE,eAAO,MAAM,gBAAgB,SAAS,CAAC;AAEvC,8EAA8E;AAC9E,eAAO,MAAM,0BAA0B,QAAiB,CAAC;AAEzD;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,WAAW,CAAC,GACnF,IAAI,CAAC,WAAW,EAAE,OAAO,GAAG,QAAQ,GAAG,YAAY,GAAG,OAAO,CAAC,GAC9D,IAAI,CAAC,YAAY,EAAE,mBAAmB,CAAC,GACvC,IAAI,CAAC,YAAY,EA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ACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,eAAe,CAAC;CAC3B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,mBAAmB,GAC/B,MAAM,IAAI,CAAC,mBAAmB,EAAE,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,mBAAmB,GAAG,OAAO,CAAC,EAC9F,WAAW,MAAM,GAAG,SAAS,EAC7B,QAAQ,cAAc,CAAC,UAAU,CAAC,EAClC,WAAW,MAAM,EACjB,oBAAoB,MAAM,KACxB,OAAO,CAAC,sBAAsB,CA0GhC,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,wBAAwB,GACpC,MAAM,IAAI,CAAC,mBAAmB,EAAE,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,YAAY,GAAG,OAAO,CAAC,EACvF,WAAW,MAAM,EACjB,MAAM,QAAQ,EACd,OAAO,UAAU,KACf,OAAO,CAAC,WAAW,CAgBrB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,yBAAyB,GACrC,MAAM,IAAI,CAAC,mBAAmB,EAAE,WAAW,CAAC,EAC5C,WAAW,MAAM,EACjB,KAAK,MAAM,KACT,OAAO,CAAC,UAAU,CAIpB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,kBAAkB,GAC9B,MAAM,IAAI,CAAC,mBAAmB,EAAE,SAAS,GAAG,MAAM,GAAG,QAAQ,CAAC,EAC9D,WAAW,MAAM,EACjB,UAAU;IAAE,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,KAC3D,OAAO,CAAC,MAAM,CA0BhB,CAAC"}
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Filesystem CAS for fact bytes stored outside Postgres — the disk half of
3
3
  * `PgFactStore`, threaded over the injectable `runtime/*Deps` rather than raw
4
- * `node:fs`, so it runs unchanged under Node, Deno, and a mock runtime.
4
+ * `node:fs`, so it runs unchanged over `create_node_runtime` and the mock
5
+ * runtime.
5
6
  *
6
7
  * Large facts (over the embedded threshold) live on disk at the canonical
7
8
  * sharded layout `<facts_dir>/<shard>/<rest>` — `<shard>` is the first 2 hex
@@ -0,0 +1,95 @@
1
+ /**
2
+ * The libpq side of a Postgres `DATABASE_URL`: the database it names, the
3
+ * maintenance connection a client program like `createdb` or `dropdb` reaches
4
+ * its server through, the environment that carries the URL's password, and the
5
+ * argv those programs get. Pure functions, shared by the dev setup helpers
6
+ * (`dev/setup.ts`) and the cross-backend test setup
7
+ * (`testing/cross_backend/postgres_database.ts`).
8
+ *
9
+ * URL rewrites edit the raw query pairs, never `URLSearchParams`, which
10
+ * reserializes every value form-encoded: a space becomes `+`, which libpq reads
11
+ * as a literal plus (`options=-c%20search_path%3Dapp` would arrive broken).
12
+ *
13
+ * @module
14
+ */
15
+ /**
16
+ * Rewrite a URL's query pair by pair, leaving every pair the rewrite keeps
17
+ * byte for byte as it was.
18
+ *
19
+ * @param url - the URL to rewrite
20
+ * @param rewrite - each raw pair and its decoded key to its replacement, or `null` to drop it
21
+ * @mutates url - replaces its `search`
22
+ */
23
+ export declare const rewrite_query_pairs: (url: URL, rewrite: (pair: string, key: string | null) => string | null) => void;
24
+ /**
25
+ * Extract the database name from a PostgreSQL URL, percent-decoded.
26
+ *
27
+ * @param url - a `DATABASE_URL`
28
+ * @returns the database name, or `null` if the URL isn't a valid `postgres://` / `postgresql://` URL or names no database
29
+ */
30
+ export declare const parse_db_name: (url: string) => string | null;
31
+ /**
32
+ * The maintenance database a client program connects to in order to create or
33
+ * drop `database`: `postgres`, or `template1` when `database` is `postgres`
34
+ * itself — a database can't be dropped over a connection to it, and `dropdb`
35
+ * falls back to `template1` the same way when given no maintenance database.
36
+ *
37
+ * @param database - the database being created or dropped
38
+ */
39
+ export declare const to_maintenance_database_name: (database: string) => string;
40
+ /**
41
+ * The URL a client program connects through to create or drop `database` on
42
+ * the server `server_url` names: `server_url` with its database replaced by
43
+ * the maintenance database (`to_maintenance_database_name`) and its password
44
+ * removed, from the userinfo and the `password` query parameter, and any
45
+ * fragment dropped. Host, port, role, and every other query parameter are kept
46
+ * as written. The password reaches the program in its environment instead
47
+ * (`to_postgres_password_env`), never its argv, which any local user can read
48
+ * from the process list.
49
+ *
50
+ * @param server_url - a `postgres://` URL on the target server; its database name is ignored
51
+ * @param database - the database being created or dropped
52
+ * @returns the maintenance URL, with no password
53
+ */
54
+ export declare const to_maintenance_database_url: (server_url: string, database: string) => string;
55
+ /**
56
+ * The environment that hands a client program the password `database_url`
57
+ * carries: `{PGPASSWORD}` when it has one, `{}` when it has none. libpq reads
58
+ * `PGPASSWORD` as it reads a `password` connection parameter, ahead of
59
+ * `~/.pgpass`. A `password` query parameter wins over the userinfo's, as in
60
+ * libpq, which applies query parameters after the URI's components.
61
+ *
62
+ * @param database_url - a `postgres://` URL
63
+ * @returns the environment to add, or `null` when the password has a malformed percent-escape
64
+ */
65
+ export declare const to_postgres_password_env: (database_url: string) => Record<string, string> | null;
66
+ /**
67
+ * Whether `createdb`'s stderr is its refusal of a database that already
68
+ * exists — Postgres's `database "<name>" already exists`, which `createdb`
69
+ * prints after `database creation failed: ERROR:`. The text is the server's,
70
+ * in its `lc_messages`, so only an English refusal is recognized.
71
+ *
72
+ * @param stderr - what `createdb` wrote to stderr
73
+ * @param database - the database it was asked to create
74
+ */
75
+ export declare const is_createdb_already_exists: (stderr: string, database: string) => boolean;
76
+ /**
77
+ * The argv for `createdb` to create `database`: never prompting
78
+ * (`--no-password`), on the server `server_url` names when given (through
79
+ * `--maintenance-db`, `to_maintenance_database_url`) or else the server the
80
+ * libpq environment names, with `--` ahead of the name so a name beginning
81
+ * with `-` stays an operand.
82
+ *
83
+ * @param database - the database to create
84
+ * @param server_url - a `postgres://` URL on the target server
85
+ */
86
+ export declare const to_createdb_args: (database: string, server_url?: string) => Array<string>;
87
+ /**
88
+ * The argv for `dropdb` to drop `database` if it exists, on the server
89
+ * `server_url` names, as `to_createdb_args` builds it.
90
+ *
91
+ * @param database - the database to drop
92
+ * @param server_url - a `postgres://` URL on the target server
93
+ */
94
+ export declare const to_dropdb_args: (database: string, server_url: string) => Array<string>;
95
+ //# sourceMappingURL=postgres_url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres_url.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/db/postgres_url.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAoBH;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,GAC/B,KAAK,GAAG,EACR,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,KAAK,MAAM,GAAG,IAAI,KAC1D,IAQF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,IAOpD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,4BAA4B,GAAI,UAAU,MAAM,KAAG,MACb,CAAC;AAEpD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,2BAA2B,GAAI,YAAY,MAAM,EAAE,UAAU,MAAM,KAAG,MAQlF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,wBAAwB,GAAI,cAAc,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAexF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,0BAA0B,GAAI,QAAQ,MAAM,EAAE,UAAU,MAAM,KAAG,OACrB,CAAC;AAE1D;;;;;;;;;GASG;AACH,eAAO,MAAM,gBAAgB,GAAI,UAAU,MAAM,EAAE,aAAa,MAAM,KAAG,KAAK,CAAC,MAAM,CAOpF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,UAAU,MAAM,EAAE,YAAY,MAAM,KAAG,KAAK,CAAC,MAAM,CAMjF,CAAC"}
@@ -0,0 +1,167 @@
1
+ /**
2
+ * The libpq side of a Postgres `DATABASE_URL`: the database it names, the
3
+ * maintenance connection a client program like `createdb` or `dropdb` reaches
4
+ * its server through, the environment that carries the URL's password, and the
5
+ * argv those programs get. Pure functions, shared by the dev setup helpers
6
+ * (`dev/setup.ts`) and the cross-backend test setup
7
+ * (`testing/cross_backend/postgres_database.ts`).
8
+ *
9
+ * URL rewrites edit the raw query pairs, never `URLSearchParams`, which
10
+ * reserializes every value form-encoded: a space becomes `+`, which libpq reads
11
+ * as a literal plus (`options=-c%20search_path%3Dapp` would arrive broken).
12
+ *
13
+ * @module
14
+ */
15
+ import { is_pg_database_url } from './create_db.js';
16
+ /** A raw query pair's key, percent-decoded as libpq decodes it, or `null` for a malformed escape. */
17
+ const to_query_key = (pair) => {
18
+ try {
19
+ return decodeURIComponent(pair.split('=', 1)[0]);
20
+ }
21
+ catch {
22
+ return null;
23
+ }
24
+ };
25
+ /** The raw pairs of a URL's query (`url.search` without its `?`), empty pairs dropped. */
26
+ const to_query_pairs = (url) => url.search
27
+ .slice(1)
28
+ .split('&')
29
+ .filter((pair) => pair !== '');
30
+ /**
31
+ * Rewrite a URL's query pair by pair, leaving every pair the rewrite keeps
32
+ * byte for byte as it was.
33
+ *
34
+ * @param url - the URL to rewrite
35
+ * @param rewrite - each raw pair and its decoded key to its replacement, or `null` to drop it
36
+ * @mutates url - replaces its `search`
37
+ */
38
+ export const rewrite_query_pairs = (url, rewrite) => {
39
+ if (!url.search)
40
+ return;
41
+ const pairs = [];
42
+ for (const pair of to_query_pairs(url)) {
43
+ const next = rewrite(pair, to_query_key(pair));
44
+ if (next !== null)
45
+ pairs.push(next);
46
+ }
47
+ url.search = pairs.join('&');
48
+ };
49
+ /**
50
+ * Extract the database name from a PostgreSQL URL, percent-decoded.
51
+ *
52
+ * @param url - a `DATABASE_URL`
53
+ * @returns the database name, or `null` if the URL isn't a valid `postgres://` / `postgresql://` URL or names no database
54
+ */
55
+ export const parse_db_name = (url) => {
56
+ if (!is_pg_database_url(url) || !URL.canParse(url))
57
+ return null;
58
+ try {
59
+ return decodeURIComponent(new URL(url).pathname.slice(1)) || null;
60
+ }
61
+ catch {
62
+ return null; // a malformed percent-escape
63
+ }
64
+ };
65
+ /**
66
+ * The maintenance database a client program connects to in order to create or
67
+ * drop `database`: `postgres`, or `template1` when `database` is `postgres`
68
+ * itself — a database can't be dropped over a connection to it, and `dropdb`
69
+ * falls back to `template1` the same way when given no maintenance database.
70
+ *
71
+ * @param database - the database being created or dropped
72
+ */
73
+ export const to_maintenance_database_name = (database) => database === 'postgres' ? 'template1' : 'postgres';
74
+ /**
75
+ * The URL a client program connects through to create or drop `database` on
76
+ * the server `server_url` names: `server_url` with its database replaced by
77
+ * the maintenance database (`to_maintenance_database_name`) and its password
78
+ * removed, from the userinfo and the `password` query parameter, and any
79
+ * fragment dropped. Host, port, role, and every other query parameter are kept
80
+ * as written. The password reaches the program in its environment instead
81
+ * (`to_postgres_password_env`), never its argv, which any local user can read
82
+ * from the process list.
83
+ *
84
+ * @param server_url - a `postgres://` URL on the target server; its database name is ignored
85
+ * @param database - the database being created or dropped
86
+ * @returns the maintenance URL, with no password
87
+ */
88
+ export const to_maintenance_database_url = (server_url, database) => {
89
+ const url = new URL(server_url);
90
+ url.pathname = `/${to_maintenance_database_name(database)}`;
91
+ url.password = '';
92
+ // libpq reads a fragment as part of the database name
93
+ url.hash = '';
94
+ rewrite_query_pairs(url, (pair, key) => (key === 'password' ? null : pair));
95
+ return url.href;
96
+ };
97
+ /**
98
+ * The environment that hands a client program the password `database_url`
99
+ * carries: `{PGPASSWORD}` when it has one, `{}` when it has none. libpq reads
100
+ * `PGPASSWORD` as it reads a `password` connection parameter, ahead of
101
+ * `~/.pgpass`. A `password` query parameter wins over the userinfo's, as in
102
+ * libpq, which applies query parameters after the URI's components.
103
+ *
104
+ * @param database_url - a `postgres://` URL
105
+ * @returns the environment to add, or `null` when the password has a malformed percent-escape
106
+ */
107
+ export const to_postgres_password_env = (database_url) => {
108
+ const url = new URL(database_url);
109
+ let raw = null;
110
+ for (const pair of to_query_pairs(url)) {
111
+ if (to_query_key(pair) !== 'password')
112
+ continue;
113
+ const eq = pair.indexOf('=');
114
+ raw = eq === -1 ? '' : pair.slice(eq + 1);
115
+ }
116
+ raw ??= url.password;
117
+ if (raw === '')
118
+ return {};
119
+ try {
120
+ return { PGPASSWORD: decodeURIComponent(raw) };
121
+ }
122
+ catch {
123
+ return null;
124
+ }
125
+ };
126
+ /**
127
+ * Whether `createdb`'s stderr is its refusal of a database that already
128
+ * exists — Postgres's `database "<name>" already exists`, which `createdb`
129
+ * prints after `database creation failed: ERROR:`. The text is the server's,
130
+ * in its `lc_messages`, so only an English refusal is recognized.
131
+ *
132
+ * @param stderr - what `createdb` wrote to stderr
133
+ * @param database - the database it was asked to create
134
+ */
135
+ export const is_createdb_already_exists = (stderr, database) => stderr.includes(`database "${database}" already exists`);
136
+ /**
137
+ * The argv for `createdb` to create `database`: never prompting
138
+ * (`--no-password`), on the server `server_url` names when given (through
139
+ * `--maintenance-db`, `to_maintenance_database_url`) or else the server the
140
+ * libpq environment names, with `--` ahead of the name so a name beginning
141
+ * with `-` stays an operand.
142
+ *
143
+ * @param database - the database to create
144
+ * @param server_url - a `postgres://` URL on the target server
145
+ */
146
+ export const to_createdb_args = (database, server_url) => [
147
+ '--no-password',
148
+ ...(server_url === undefined
149
+ ? []
150
+ : [`--maintenance-db=${to_maintenance_database_url(server_url, database)}`]),
151
+ '--',
152
+ database
153
+ ];
154
+ /**
155
+ * The argv for `dropdb` to drop `database` if it exists, on the server
156
+ * `server_url` names, as `to_createdb_args` builds it.
157
+ *
158
+ * @param database - the database to drop
159
+ * @param server_url - a `postgres://` URL on the target server
160
+ */
161
+ export const to_dropdb_args = (database, server_url) => [
162
+ '--if-exists',
163
+ '--no-password',
164
+ `--maintenance-db=${to_maintenance_database_url(server_url, database)}`,
165
+ '--',
166
+ database
167
+ ];