@fuzdev/fuz_app 0.175.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 (84) hide show
  1. package/README.md +3 -2
  2. package/dist/actions/CLAUDE.md +10 -8
  3. package/dist/actions/ws_outbound_writer.d.ts +8 -8
  4. package/dist/actions/ws_outbound_writer.d.ts.map +1 -1
  5. package/dist/actions/ws_outbound_writer.js +8 -11
  6. package/dist/auth/keyring.d.ts +1 -1
  7. package/dist/auth/keyring.js +1 -1
  8. package/dist/db/CLAUDE.md +9 -0
  9. package/dist/db/fact_disk_storage.d.ts +3 -2
  10. package/dist/db/fact_disk_storage.d.ts.map +1 -1
  11. package/dist/db/fact_disk_storage.js +2 -1
  12. package/dist/db/postgres_url.d.ts +95 -0
  13. package/dist/db/postgres_url.d.ts.map +1 -0
  14. package/dist/db/postgres_url.js +167 -0
  15. package/dist/dev/setup.d.ts +101 -33
  16. package/dist/dev/setup.d.ts.map +1 -1
  17. package/dist/dev/setup.js +174 -71
  18. package/dist/env/dotenv.d.ts +1 -1
  19. package/dist/env/dotenv.d.ts.map +1 -1
  20. package/dist/env/dotenv.js +2 -3
  21. package/dist/http/CLAUDE.md +1 -1
  22. package/dist/http/body_limit.d.ts +10 -16
  23. package/dist/http/body_limit.d.ts.map +1 -1
  24. package/dist/http/body_limit.js +10 -16
  25. package/dist/runtime/deps.d.ts +81 -45
  26. package/dist/runtime/deps.d.ts.map +1 -1
  27. package/dist/runtime/deps.js +7 -2
  28. package/dist/runtime/mock.d.ts +63 -16
  29. package/dist/runtime/mock.d.ts.map +1 -1
  30. package/dist/runtime/mock.js +75 -35
  31. package/dist/runtime/node.d.ts +3 -2
  32. package/dist/runtime/node.d.ts.map +1 -1
  33. package/dist/runtime/node.js +20 -72
  34. package/dist/runtime/process_id.d.ts +24 -0
  35. package/dist/runtime/process_id.d.ts.map +1 -0
  36. package/dist/runtime/process_id.js +27 -0
  37. package/dist/runtime/secure_file.d.ts +8 -30
  38. package/dist/runtime/secure_file.d.ts.map +1 -1
  39. package/dist/runtime/secure_file.js +23 -51
  40. package/dist/server/app_server.d.ts +2 -2
  41. package/dist/server/static.d.ts +1 -1
  42. package/dist/testing/CLAUDE.md +28 -27
  43. package/dist/testing/cross_backend/backend_config.d.ts +7 -7
  44. package/dist/testing/cross_backend/backend_config.d.ts.map +1 -1
  45. package/dist/testing/cross_backend/bench/bench_report.d.ts +2 -2
  46. package/dist/testing/cross_backend/bench/bench_report.js +2 -2
  47. package/dist/testing/cross_backend/body_size_smuggling.d.ts.map +1 -1
  48. package/dist/testing/cross_backend/body_size_smuggling.js +1 -4
  49. package/dist/testing/cross_backend/bootstrap_backend.d.ts +1 -1
  50. package/dist/testing/cross_backend/capabilities.d.ts +2 -2
  51. package/dist/testing/cross_backend/create_cross_backend_global_setup.d.ts +2 -2
  52. package/dist/testing/cross_backend/create_cross_backend_global_setup.js +1 -1
  53. package/dist/testing/cross_backend/create_dual_spawn_global_setup.d.ts +2 -2
  54. package/dist/testing/cross_backend/default_backend_configs.d.ts +1 -1
  55. package/dist/testing/cross_backend/default_backend_configs.js +4 -4
  56. package/dist/testing/cross_backend/differential.d.ts +2 -2
  57. package/dist/testing/cross_backend/differential.js +6 -6
  58. package/dist/testing/cross_backend/fact_serving.js +1 -1
  59. package/dist/testing/cross_backend/make_cross_backend_project.d.ts +2 -2
  60. package/dist/testing/cross_backend/make_cross_backend_project.js +1 -1
  61. package/dist/testing/cross_backend/origin_conformance.json +1 -1
  62. package/dist/testing/cross_backend/postgres_database.d.ts +7 -21
  63. package/dist/testing/cross_backend/postgres_database.d.ts.map +1 -1
  64. package/dist/testing/cross_backend/postgres_database.js +24 -35
  65. package/dist/testing/cross_backend/testing_server_bun.d.ts.map +1 -1
  66. package/dist/testing/cross_backend/testing_server_bun.js +10 -12
  67. package/dist/testing/cross_backend/testing_server_core.d.ts +13 -17
  68. package/dist/testing/cross_backend/testing_server_core.d.ts.map +1 -1
  69. package/dist/testing/cross_backend/testing_server_core.js +1 -1
  70. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts +7 -17
  71. package/dist/testing/cross_backend/ts_spine_backend_config.d.ts.map +1 -1
  72. package/dist/testing/cross_backend/ts_spine_backend_config.js +0 -51
  73. package/dist/testing/cross_backend/ws_outbound_overflow.js +2 -2
  74. package/dist/testing/daemon_info.d.ts +11 -6
  75. package/dist/testing/daemon_info.d.ts.map +1 -1
  76. package/dist/testing/daemon_info.js +11 -8
  77. package/dist/testing/middleware_gate_probes.js +2 -2
  78. package/package.json +4 -9
  79. package/dist/runtime/deno.d.ts +0 -21
  80. package/dist/runtime/deno.d.ts.map +0 -1
  81. package/dist/runtime/deno.js +0 -191
  82. package/dist/testing/cross_backend/testing_server_deno.d.ts +0 -5
  83. package/dist/testing/cross_backend/testing_server_deno.d.ts.map +0 -1
  84. package/dist/testing/cross_backend/testing_server_deno.js +0 -38
@@ -1,14 +1,17 @@
1
1
  /**
2
2
  * Dev workflow helpers for setup, reset, and database management.
3
3
  *
4
- * Composable functions that consumer projects (tx, visiones, etc.) use in their
5
- * `scripts/dev_setup.ts`, `scripts/dev_reset.ts`, etc. All functions accept narrow
6
- * `*Deps` interfaces from `runtime/deps.ts` — pass a `RuntimeDeps` instance
7
- * created by `create_deno_runtime()`.
4
+ * Composable functions that consumer projects use in their dev setup and
5
+ * reset tasks. File and env access come through narrow `*Deps` interfaces
6
+ * from `runtime/deps.ts`, which a `RuntimeDeps` from `create_node_runtime()`
7
+ * satisfies. The Postgres client programs the database helpers run come
8
+ * through `PostgresProgramDeps`, each a factory the task root writes over its
9
+ * spawn (with gro, `(env) => spawn.find('createdb', {pass_env: ['PG*'], env})`),
10
+ * so the root decides which binary runs and which environment it sees.
8
11
  *
9
12
  * @module
10
13
  */
11
- import type { CommandDeps, CommandResult, EnvDeps, FsReadDeps, FsRemoveDeps, FsWriteDeps } from '../runtime/deps.js';
14
+ import type { EnvDeps, FsReadDeps, FsRemoveDeps, FsWriteDeps, ProgramFactory } from '../runtime/deps.js';
12
15
  import type { QueryDeps } from '../db/query_deps.js';
13
16
  /**
14
17
  * Optional logger for setup helpers.
@@ -39,14 +42,43 @@ export interface SetupTokenResult {
39
42
  /** The token file path. */
40
43
  token_path: string;
41
44
  }
45
+ /**
46
+ * The kind of database a `DATABASE_URL` names, by the scheme `create_db`
47
+ * routes on: `postgres://` / `postgresql://`, `file://` (PGlite), `memory://`
48
+ * (pglet), or `'none'` for an empty or unsupported URL.
49
+ */
50
+ export type SetupDbType = 'postgres' | 'pglite' | 'memory' | 'none';
51
+ /** Result of `create_database`. */
52
+ export type CreateDatabaseResult =
53
+ /** `createdb` created the database. */
54
+ {
55
+ kind: 'created';
56
+ database: string;
57
+ }
58
+ /** The database already existed. */
59
+ | {
60
+ kind: 'exists';
61
+ database: string;
62
+ }
63
+ /** Nothing to create: a URL that isn't Postgres, or no `createdb` installed. */
64
+ | {
65
+ kind: 'skipped';
66
+ db_type: SetupDbType;
67
+ message: string;
68
+ }
69
+ /** The database was not created; `message` is what was logged. */
70
+ | {
71
+ kind: 'failed';
72
+ message: string;
73
+ };
42
74
  /** Result of `reset_database`. */
43
75
  export interface ResetDbResult {
44
76
  /** Whether the database was actually reset. */
45
77
  reset: boolean;
46
- /** Whether the operation was skipped (e.g. pglite with no data dir). */
78
+ /** Whether there was nothing to reset (no URL, an in-memory URL, PGlite with no data dir). */
47
79
  skipped: boolean;
48
- /** What type of database was detected. */
49
- db_type: 'postgres' | 'pglite' | 'none';
80
+ /** What type of database the URL names. */
81
+ db_type: SetupDbType;
50
82
  }
51
83
  /** Options for `setup_env_file`. */
52
84
  export interface SetupEnvOptions {
@@ -63,7 +95,7 @@ export interface SetupEnvOptions {
63
95
  }
64
96
  /** Options for `setup_bootstrap_token`. */
65
97
  export interface SetupBootstrapTokenOptions {
66
- /** State directory override. Defaults to `~/.{app_name}`. */
98
+ /** State directory override. Defaults to `~/.{app_name}`, which needs `$HOME`. */
67
99
  state_dir?: string;
68
100
  log?: SetupLogger;
69
101
  }
@@ -73,16 +105,36 @@ export interface CreateDatabaseOptions {
73
105
  }
74
106
  /** Options for `reset_database`. */
75
107
  export interface ResetDatabaseOptions {
76
- /** Directory to remove for file-based pglite. */
108
+ /**
109
+ * The PGlite data directory to remove for a `file://` URL. Named by the
110
+ * caller, never derived from the URL, so a mistyped `DATABASE_URL` can't
111
+ * aim a recursive remove; without it a `file://` reset is skipped.
112
+ */
77
113
  pglite_data_dir?: string;
78
114
  log?: SetupLogger;
79
115
  }
80
116
  /**
81
- * Extract the database name from a PostgreSQL URL.
117
+ * The Postgres client programs the database helpers run, each as a factory
118
+ * that makes the program with extra environment, `null` when it isn't
119
+ * installed. A helper calls each factory once per run with the environment
120
+ * that carries `DATABASE_URL`'s password (`{PGPASSWORD}`, or `{}` for a URL
121
+ * with none), so the password reaches the program the way libpq reads it and
122
+ * never its argv. At a gro task root:
123
+ * `(env) => spawn.find('createdb', {pass_env: ['PG*'], env})` — `env` wins over
124
+ * a host `PGPASSWORD` that `PG*` forwards, and gro's spawn base passes `HOME`
125
+ * for `~/.pgpass`.
82
126
  *
83
- * @returns the database name, or `null` if the URL is invalid or has no path
127
+ * Each program connects to the server `DATABASE_URL` names: host, port, role,
128
+ * and query parameters (`sslmode`, a socket `host`) all come from the URL,
129
+ * through `--maintenance-db` (`to_maintenance_database_url`). With no host,
130
+ * as in `postgres:///app`, libpq uses its default socket.
84
131
  */
85
- export declare const parse_db_name: (url: string) => string | null;
132
+ export interface PostgresProgramDeps {
133
+ /** Makes `createdb`, or `null` when it isn't installed. */
134
+ createdb: ProgramFactory;
135
+ /** Makes `dropdb`, or `null` when it isn't installed. */
136
+ dropdb: ProgramFactory;
137
+ }
86
138
  /**
87
139
  * Generate a cookie signing key in-process: 32 random bytes, hex-encoded.
88
140
  *
@@ -106,7 +158,7 @@ export declare const generate_cookie_key: () => string;
106
158
  * @param env_path - path to the .env file
107
159
  * @param name - the variable name to read
108
160
  * @returns the value, or `undefined` if the file or variable doesn't exist
109
- * @throws Error if reading fails for any reason other than `ENOENT` / `NotFound`
161
+ * @throws Error if reading fails for any reason other than `ENOENT`
110
162
  */
111
163
  export declare const read_env_var: (deps: Pick<FsReadDeps, "read_text_file">, env_path: string, name: string) => Promise<string | undefined>;
112
164
  /**
@@ -129,8 +181,9 @@ export declare const setup_env_file: (deps: FsReadDeps & FsWriteDeps, env_path:
129
181
  *
130
182
  * The token is a one-shot secret used to create the first admin account:
131
183
  * 32 random bytes as 64 lowercase hex characters plus one LF, the file
132
- * `openssl rand -hex 32` writes. Stored at
133
- * `~/.{app_name}/secret_bootstrap_token` by default.
184
+ * `openssl rand -hex 32` writes. Stored at `{state_dir}/secret_bootstrap_token`,
185
+ * `state_dir` defaulting to `~/.{app_name}`; skipped only when no `state_dir`
186
+ * is given and `$HOME` is unset.
134
187
  *
135
188
  * @param deps - file and env capabilities
136
189
  * @param app_name - application name (used for default state directory)
@@ -140,7 +193,8 @@ export declare const setup_env_file: (deps: FsReadDeps & FsWriteDeps, env_path:
140
193
  */
141
194
  export declare const setup_bootstrap_token: (deps: FsReadDeps & FsWriteDeps & EnvDeps, app_name: string, options?: SetupBootstrapTokenOptions) => Promise<SetupTokenResult>;
142
195
  /**
143
- * Remove an existing bootstrap token and create a new one.
196
+ * Remove an existing bootstrap token and create a new one, in the state
197
+ * directory `setup_bootstrap_token` uses.
144
198
  *
145
199
  * @param deps - file, env, and remove capabilities
146
200
  * @param app_name - application name
@@ -150,33 +204,47 @@ export declare const setup_bootstrap_token: (deps: FsReadDeps & FsWriteDeps & En
150
204
  */
151
205
  export declare const reset_bootstrap_token: (deps: FsReadDeps & FsWriteDeps & FsRemoveDeps & EnvDeps, app_name: string, options?: SetupBootstrapTokenOptions) => Promise<SetupTokenResult>;
152
206
  /**
153
- * Create a PostgreSQL database if `createdb` is available.
207
+ * Create the PostgreSQL database `database_url` names, on the server it
208
+ * names, when it's missing.
154
209
  *
155
- * Does not throw — returns the underlying command result so callers can
156
- * decide how to react to a missing `createdb` or an "already exists" failure.
210
+ * Runs `createdb --no-password --maintenance-db=<maintenance URL> -- <name>`
211
+ * (`to_createdb_args`) through the program `deps.createdb` makes, with the
212
+ * URL's password in its environment (see `PostgresProgramDeps`). A URL that
213
+ * isn't Postgres (`file://`, `memory://`) has nothing to create and is
214
+ * skipped, as is a missing `createdb`. Does not throw on a failed `createdb` —
215
+ * the result says what happened, and the log says why; a `createdb` that
216
+ * can't start throws.
157
217
  *
158
- * @param deps - command execution capability
159
- * @param db_name - database name to create
218
+ * On a server whose `lc_messages` isn't English, an existing database is
219
+ * reported as `failed`, since only Postgres's English refusal is recognized.
220
+ * A capped (truncated) output is read as it came: a clean exit is still
221
+ * success, and a cut stderr is still searched for the refusal.
222
+ *
223
+ * @param deps - the `createdb` factory
224
+ * @param database_url - the `DATABASE_URL` value
160
225
  * @param options - logger
161
- * @returns the command result
162
- * @mutates external database - invokes `createdb` to create `db_name` when available
226
+ * @returns what happened
227
+ * @mutates external database - creates the database on the server `database_url` names
163
228
  */
164
- export declare const create_database: (deps: CommandDeps, db_name: string, options?: CreateDatabaseOptions) => Promise<CommandResult>;
229
+ export declare const create_database: (deps: Pick<PostgresProgramDeps, "createdb">, database_url: string, options?: CreateDatabaseOptions) => Promise<CreateDatabaseResult>;
165
230
  /**
166
231
  * Reset a database to a clean slate.
167
232
  *
168
- * For PostgreSQL: drops and recreates the database.
169
- * For pglite: removes the data directory if `pglite_data_dir` is provided.
170
- * For empty/missing URLs: skips.
233
+ * For PostgreSQL: drops and recreates the database `database_url` names, on
234
+ * the server it names, through the programs `deps.dropdb` and `deps.createdb`
235
+ * make (see `PostgresProgramDeps`; argv from `to_dropdb_args` /
236
+ * `to_createdb_args`). For PGlite (`file://`): removes
237
+ * `options.pglite_data_dir` when given. For an in-memory (`memory://`), empty,
238
+ * or missing URL: skips. An unsupported URL is an error, never dropped.
171
239
  *
172
- * @param deps - command and file capabilities
240
+ * @param deps - the Postgres program factories and file capabilities
173
241
  * @param database_url - the `DATABASE_URL` value
174
242
  * @param options - `pglite_data_dir`, logger
175
243
  * @returns result describing what happened
176
- * @mutates external database - drops and recreates the PostgreSQL database, or removes the PGlite data directory
177
- * @mutates filesystem - removes `options.pglite_data_dir` recursively for PGlite URLs
244
+ * @mutates external database - drops and recreates the PostgreSQL database on the server `database_url` names
245
+ * @mutates filesystem - removes `options.pglite_data_dir` recursively for `file://` URLs
178
246
  */
179
- export declare const reset_database: (deps: CommandDeps & FsReadDeps & FsRemoveDeps, database_url: string, options?: ResetDatabaseOptions) => Promise<ResetDbResult>;
247
+ export declare const reset_database: (deps: PostgresProgramDeps & Pick<FsReadDeps, "stat"> & FsRemoveDeps, database_url: string, options?: ResetDatabaseOptions) => Promise<ResetDbResult>;
180
248
  /** Input to `seed_dev_account`. */
181
249
  export interface SeedDevAccountInput {
182
250
  /**
@@ -212,7 +280,7 @@ export interface SeedDevAccountDeps extends QueryDeps {
212
280
  * requested role grants. Never updates an existing password (rerun would
213
281
  * silently rotate it).
214
282
  *
215
- * Intended for `scripts/dev_setup.ts` — do not call in production.
283
+ * Intended for a dev setup task — do not call in production.
216
284
  *
217
285
  * @mutates database - inserts an account/actor pair when missing and grants any requested role role_grants
218
286
  * @throws Error if an existing account is found without an associated actor row
@@ -1 +1 @@
1
- {"version":3,"file":"setup.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/dev/setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EACX,WAAW,EACX,aAAa,EACb,OAAO,EACP,UAAU,EACV,YAAY,EACZ,WAAW,EACX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAWrD;;;;;GAKG;AACH,MAAM,WAAW,WAAW;IAC3B,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1B,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5B,KAAK,E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,eAAO,MAAM,eAAe,GAC3B,MAAM,IAAI,CAAC,mBAAmB,EAAE,UAAU,CAAC,EAC3C,cAAc,MAAM,EACpB,UAAU,qBAAqB,KAC7B,OAAO,CAAC,oBAAoB,CA8C9B,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,cAAc,GAC1B,MAAM,mBAAmB,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG,YAAY,EACnE,cAAc,MAAM,EACpB,UAAU,oBAAoB,KAC5B,OAAO,CAAC,aAAa,CA4EvB,CAAC;AAIF,mCAAmC;AACnC,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,CAAC;IACjB,kDAAkD;IAClD,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CAC9B;AAED,oCAAoC;AACpC,MAAM,WAAW,oBAAoB;IACpC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,OAAO,EAAE,OAAO,CAAC;CACjB;AAED,2CAA2C;AAC3C,MAAM,WAAW,kBAAmB,SAAQ,SAAS;IACpD,oEAAoE;IACpE,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CACrD;AAED;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,gBAAgB,GAC5B,MAAM,kBAAkB,EACxB,OAAO,mBAAmB,EAC1B,UAAU;IAAE,GAAG,CAAC,EAAE,WAAW,CAAA;CAAE,KAC7B,OAAO,CAAC,oBAAoB,CAyC9B,CAAC"}
package/dist/dev/setup.js CHANGED
@@ -1,14 +1,19 @@
1
1
  /**
2
2
  * Dev workflow helpers for setup, reset, and database management.
3
3
  *
4
- * Composable functions that consumer projects (tx, visiones, etc.) use in their
5
- * `scripts/dev_setup.ts`, `scripts/dev_reset.ts`, etc. All functions accept narrow
6
- * `*Deps` interfaces from `runtime/deps.ts` — pass a `RuntimeDeps` instance
7
- * created by `create_deno_runtime()`.
4
+ * Composable functions that consumer projects use in their dev setup and
5
+ * reset tasks. File and env access come through narrow `*Deps` interfaces
6
+ * from `runtime/deps.ts`, which a `RuntimeDeps` from `create_node_runtime()`
7
+ * satisfies. The Postgres client programs the database helpers run come
8
+ * through `PostgresProgramDeps`, each a factory the task root writes over its
9
+ * spawn (with gro, `(env) => spawn.find('createdb', {pass_env: ['PG*'], env})`),
10
+ * so the root decides which binary runs and which environment it sees.
8
11
  *
9
12
  * @module
10
13
  */
11
14
  import { to_hex } from '@fuzdev/fuz_util/hex.ts';
15
+ import { is_pg_database_url } from '../db/create_db.js';
16
+ import { is_createdb_already_exists, parse_db_name, to_createdb_args, to_dropdb_args, to_postgres_password_env } from '../db/postgres_url.js';
12
17
  import { load_env_file, parse_dotenv } from '../env/dotenv.js';
13
18
  import { update_env_content } from '../env/update_env_variable.js';
14
19
  import { write_file_atomic } from '../runtime/fs.js';
@@ -21,20 +26,49 @@ export const default_setup_logger = {
21
26
  error: (msg) => console.error(` [error] ${msg}`)
22
27
  };
23
28
  // === Pure utilities ===
29
+ /** What kind of database `database_url` names, by `create_db`'s schemes. */
30
+ const to_setup_db_type = (database_url) => {
31
+ if (is_pg_database_url(database_url))
32
+ return 'postgres';
33
+ if (database_url.startsWith('file://'))
34
+ return 'pglite';
35
+ if (database_url.startsWith('memory://'))
36
+ return 'memory';
37
+ return 'none';
38
+ };
39
+ /** A program output's failure, as text: its stderr, or how it ended. */
40
+ const describe_program_failure = (output) => {
41
+ const stderr = output.stderr.trim();
42
+ if (stderr)
43
+ return stderr;
44
+ switch (output.kind) {
45
+ case 'exited':
46
+ return `exited with code ${output.code}`;
47
+ case 'signaled':
48
+ return `ended by ${output.signal}`;
49
+ default:
50
+ return output.kind === 'timed_out' ? 'timed out' : 'aborted';
51
+ }
52
+ };
53
+ /** Whether a program output is a clean exit. */
54
+ const is_program_success = (output) => output.kind === 'exited' && output.code === 0;
24
55
  /**
25
- * Extract the database name from a PostgreSQL URL.
26
- *
27
- * @returns the database name, or `null` if the URL is invalid or has no path
56
+ * The hint a failed Postgres client gets when the server wanted a password
57
+ * `DATABASE_URL` doesn't carry: where else the program can read one.
28
58
  */
29
- export const parse_db_name = (url) => {
30
- try {
31
- const u = new URL(url);
32
- const name = u.pathname.slice(1); // remove leading /
33
- return name || null;
34
- }
35
- catch {
36
- return null;
37
- }
59
+ const password_hint = (output, password_env) => output.stderr.includes('password') && password_env.PGPASSWORD === undefined
60
+ ? ' (DATABASE_URL has no password: put it in the URL, or give the program PGPASSWORD ' +
61
+ 'in its environment or a ~/.pgpass entry)'
62
+ : '';
63
+ /**
64
+ * The URL's password as program environment, or a failure message when its
65
+ * percent-escape is malformed — never naming the password.
66
+ */
67
+ const read_password_env = (database_url) => {
68
+ const env = to_postgres_password_env(database_url);
69
+ return env === null
70
+ ? { ok: false, message: "DATABASE_URL's password has a malformed percent-escape" }
71
+ : { ok: true, env };
38
72
  };
39
73
  /**
40
74
  * Mint an operator-facing secret: 32 random bytes as lowercase hex, the shape
@@ -66,7 +100,7 @@ export const generate_cookie_key = () => generate_secret_hex();
66
100
  * @param env_path - path to the .env file
67
101
  * @param name - the variable name to read
68
102
  * @returns the value, or `undefined` if the file or variable doesn't exist
69
- * @throws Error if reading fails for any reason other than `ENOENT` / `NotFound`
103
+ * @throws Error if reading fails for any reason other than `ENOENT`
70
104
  */
71
105
  export const read_env_var = async (deps, env_path, name) => {
72
106
  const env = await load_env_file(deps, env_path);
@@ -135,13 +169,24 @@ export const setup_env_file = async (deps, env_path, example_path, options) => {
135
169
  log.ok(`Created ${env_path} with generated secrets`);
136
170
  return { created: true, updated: true, path: env_path };
137
171
  };
172
+ /**
173
+ * The bootstrap token's state directory: `state_dir` when given, else
174
+ * `~/.{app_name}`, or `null` when neither is available.
175
+ */
176
+ const to_token_state_dir = (deps, app_name, state_dir) => {
177
+ if (state_dir !== undefined)
178
+ return state_dir;
179
+ const home = deps.env_get('HOME');
180
+ return home ? `${home}/.${app_name}` : null;
181
+ };
138
182
  /**
139
183
  * Create a bootstrap token file if it doesn't exist.
140
184
  *
141
185
  * The token is a one-shot secret used to create the first admin account:
142
186
  * 32 random bytes as 64 lowercase hex characters plus one LF, the file
143
- * `openssl rand -hex 32` writes. Stored at
144
- * `~/.{app_name}/secret_bootstrap_token` by default.
187
+ * `openssl rand -hex 32` writes. Stored at `{state_dir}/secret_bootstrap_token`,
188
+ * `state_dir` defaulting to `~/.{app_name}`; skipped only when no `state_dir`
189
+ * is given and `$HOME` is unset.
145
190
  *
146
191
  * @param deps - file and env capabilities
147
192
  * @param app_name - application name (used for default state directory)
@@ -151,16 +196,15 @@ export const setup_env_file = async (deps, env_path, example_path, options) => {
151
196
  */
152
197
  export const setup_bootstrap_token = async (deps, app_name, options) => {
153
198
  const log = options?.log ?? default_setup_logger;
154
- const home = deps.env_get('HOME');
155
- if (!home) {
156
- log.skip('$HOME not set, skipping bootstrap token');
199
+ const state_dir = to_token_state_dir(deps, app_name, options?.state_dir);
200
+ if (state_dir === null) {
201
+ log.skip('$HOME not set and no state_dir given, skipping bootstrap token');
157
202
  return { created: false, token_path: '' };
158
203
  }
159
- const state_dir = options?.state_dir ?? `${home}/.${app_name}`;
160
204
  const token_path = `${state_dir}/secret_bootstrap_token`;
161
205
  const stat = await deps.stat(token_path);
162
206
  if (stat?.is_file) {
163
- log.skip(`~/.${app_name}/secret_bootstrap_token already exists`);
207
+ log.skip(`${token_path} already exists`);
164
208
  return { created: false, token_path };
165
209
  }
166
210
  // Modes apply at creation — an already-existing state dir keeps its mode.
@@ -170,11 +214,12 @@ export const setup_bootstrap_token = async (deps, app_name, options) => {
170
214
  await deps.mkdir(state_dir, { recursive: true, mode: 0o700 });
171
215
  const token = generate_secret_hex();
172
216
  await write_file_atomic(deps, token_path, token + '\n', { mode: 0o600 });
173
- log.ok(`Created ~/.${app_name}/secret_bootstrap_token (one-shot, deleted after first use)`);
217
+ log.ok(`Created ${token_path} (one-shot, deleted after first use)`);
174
218
  return { created: true, token_path };
175
219
  };
176
220
  /**
177
- * Remove an existing bootstrap token and create a new one.
221
+ * Remove an existing bootstrap token and create a new one, in the state
222
+ * directory `setup_bootstrap_token` uses.
178
223
  *
179
224
  * @param deps - file, env, and remove capabilities
180
225
  * @param app_name - application name
@@ -184,62 +229,99 @@ export const setup_bootstrap_token = async (deps, app_name, options) => {
184
229
  */
185
230
  export const reset_bootstrap_token = async (deps, app_name, options) => {
186
231
  const log = options?.log ?? default_setup_logger;
187
- const home = deps.env_get('HOME');
188
- if (!home) {
189
- log.skip('$HOME not set');
232
+ const state_dir = to_token_state_dir(deps, app_name, options?.state_dir);
233
+ if (state_dir === null) {
234
+ log.skip('$HOME not set and no state_dir given, skipping bootstrap token');
190
235
  return { created: false, token_path: '' };
191
236
  }
192
- const state_dir = options?.state_dir ?? `${home}/.${app_name}`;
193
237
  const token_path = `${state_dir}/secret_bootstrap_token`;
194
238
  const stat = await deps.stat(token_path);
195
239
  if (stat?.is_file) {
196
240
  await deps.remove(token_path);
197
- log.ok('Removed existing bootstrap token');
241
+ log.ok(`Removed existing ${token_path}`);
198
242
  }
199
243
  return setup_bootstrap_token(deps, app_name, { state_dir, log });
200
244
  };
201
245
  // === Database helpers ===
202
246
  /**
203
- * Create a PostgreSQL database if `createdb` is available.
247
+ * Create the PostgreSQL database `database_url` names, on the server it
248
+ * names, when it's missing.
249
+ *
250
+ * Runs `createdb --no-password --maintenance-db=<maintenance URL> -- <name>`
251
+ * (`to_createdb_args`) through the program `deps.createdb` makes, with the
252
+ * URL's password in its environment (see `PostgresProgramDeps`). A URL that
253
+ * isn't Postgres (`file://`, `memory://`) has nothing to create and is
254
+ * skipped, as is a missing `createdb`. Does not throw on a failed `createdb` —
255
+ * the result says what happened, and the log says why; a `createdb` that
256
+ * can't start throws.
204
257
  *
205
- * Does not throw — returns the underlying command result so callers can
206
- * decide how to react to a missing `createdb` or an "already exists" failure.
258
+ * On a server whose `lc_messages` isn't English, an existing database is
259
+ * reported as `failed`, since only Postgres's English refusal is recognized.
260
+ * A capped (truncated) output is read as it came: a clean exit is still
261
+ * success, and a cut stderr is still searched for the refusal.
207
262
  *
208
- * @param deps - command execution capability
209
- * @param db_name - database name to create
263
+ * @param deps - the `createdb` factory
264
+ * @param database_url - the `DATABASE_URL` value
210
265
  * @param options - logger
211
- * @returns the command result
212
- * @mutates external database - invokes `createdb` to create `db_name` when available
266
+ * @returns what happened
267
+ * @mutates external database - creates the database on the server `database_url` names
213
268
  */
214
- export const create_database = async (deps, db_name, options) => {
269
+ export const create_database = async (deps, database_url, options) => {
215
270
  const log = options?.log ?? default_setup_logger;
216
- const check = await deps.run_command('which', ['createdb']);
217
- if (!check.success) {
218
- log.skip('createdb not found — install PostgreSQL or use pglite');
219
- return check;
271
+ const db_type = to_setup_db_type(database_url);
272
+ if (db_type !== 'postgres') {
273
+ const message = db_type === 'none'
274
+ ? 'No Postgres DATABASE_URL, skipping database creation'
275
+ : `A ${db_type} DATABASE_URL needs no createdb`;
276
+ log.skip(message);
277
+ return { kind: 'skipped', db_type, message };
278
+ }
279
+ const database = parse_db_name(database_url);
280
+ if (database === null) {
281
+ const message = 'Could not parse database name from DATABASE_URL';
282
+ log.error(message);
283
+ return { kind: 'failed', message };
284
+ }
285
+ const password = read_password_env(database_url);
286
+ if (!password.ok) {
287
+ log.error(password.message);
288
+ return { kind: 'failed', message: password.message };
220
289
  }
221
- const result = await deps.run_command('createdb', [db_name]);
222
- if (result.success) {
223
- log.ok(`Created ${db_name} database`);
290
+ const createdb = deps.createdb(password.env);
291
+ if (createdb === null) {
292
+ const message = 'createdb not found — install PostgreSQL, or use a file:// (PGlite) or memory:// DATABASE_URL';
293
+ log.skip(message);
294
+ return { kind: 'skipped', db_type, message };
224
295
  }
225
- else {
226
- log.skip(`${db_name} database already exists`);
296
+ const output = await createdb.out(to_createdb_args(database, database_url));
297
+ if (is_program_success(output)) {
298
+ log.ok(`Created ${database} database`);
299
+ return { kind: 'created', database };
227
300
  }
228
- return result;
301
+ if (is_createdb_already_exists(output.stderr, database)) {
302
+ log.skip(`${database} database already exists`);
303
+ return { kind: 'exists', database };
304
+ }
305
+ const message = `Failed to create database ${database}: ${describe_program_failure(output)}${password_hint(output, password.env)}`;
306
+ log.error(message);
307
+ return { kind: 'failed', message };
229
308
  };
230
309
  /**
231
310
  * Reset a database to a clean slate.
232
311
  *
233
- * For PostgreSQL: drops and recreates the database.
234
- * For pglite: removes the data directory if `pglite_data_dir` is provided.
235
- * For empty/missing URLs: skips.
312
+ * For PostgreSQL: drops and recreates the database `database_url` names, on
313
+ * the server it names, through the programs `deps.dropdb` and `deps.createdb`
314
+ * make (see `PostgresProgramDeps`; argv from `to_dropdb_args` /
315
+ * `to_createdb_args`). For PGlite (`file://`): removes
316
+ * `options.pglite_data_dir` when given. For an in-memory (`memory://`), empty,
317
+ * or missing URL: skips. An unsupported URL is an error, never dropped.
236
318
  *
237
- * @param deps - command and file capabilities
319
+ * @param deps - the Postgres program factories and file capabilities
238
320
  * @param database_url - the `DATABASE_URL` value
239
321
  * @param options - `pglite_data_dir`, logger
240
322
  * @returns result describing what happened
241
- * @mutates external database - drops and recreates the PostgreSQL database, or removes the PGlite data directory
242
- * @mutates filesystem - removes `options.pglite_data_dir` recursively for PGlite URLs
323
+ * @mutates external database - drops and recreates the PostgreSQL database on the server `database_url` names
324
+ * @mutates filesystem - removes `options.pglite_data_dir` recursively for `file://` URLs
243
325
  */
244
326
  export const reset_database = async (deps, database_url, options) => {
245
327
  const log = options?.log ?? default_setup_logger;
@@ -248,39 +330,60 @@ export const reset_database = async (deps, database_url, options) => {
248
330
  log.skip('No DATABASE_URL, skipping database reset');
249
331
  return { reset: false, skipped: true, db_type: 'none' };
250
332
  }
251
- // pglite
252
- if (database_url === 'pglite' || database_url.startsWith('pglite:')) {
333
+ const db_type = to_setup_db_type(database_url);
334
+ if (db_type === 'none') {
335
+ log.error('Unsupported DATABASE_URL scheme — expected postgres://, file://, or memory://');
336
+ return { reset: false, skipped: false, db_type };
337
+ }
338
+ if (db_type === 'memory') {
339
+ log.skip('An in-memory database has nothing to reset');
340
+ return { reset: false, skipped: true, db_type };
341
+ }
342
+ if (db_type === 'pglite') {
253
343
  const pglite_dir = options?.pglite_data_dir;
254
344
  if (pglite_dir) {
255
345
  const stat = await deps.stat(pglite_dir);
256
346
  if (stat?.is_directory) {
257
347
  await deps.remove(pglite_dir, { recursive: true });
258
348
  log.ok(`Removed pglite directory: ${pglite_dir}`);
259
- return { reset: true, skipped: false, db_type: 'pglite' };
349
+ return { reset: true, skipped: false, db_type };
260
350
  }
261
351
  }
262
352
  log.skip('No pglite directory to remove');
263
- return { reset: false, skipped: true, db_type: 'pglite' };
353
+ return { reset: false, skipped: true, db_type };
264
354
  }
265
- // PostgreSQL
266
355
  const db_name = parse_db_name(database_url);
267
356
  if (!db_name) {
268
357
  log.error('Could not parse database name from DATABASE_URL');
269
- return { reset: false, skipped: true, db_type: 'postgres' };
358
+ return { reset: false, skipped: false, db_type };
359
+ }
360
+ const password = read_password_env(database_url);
361
+ if (!password.ok) {
362
+ log.error(password.message);
363
+ return { reset: false, skipped: false, db_type };
364
+ }
365
+ const dropdb = deps.dropdb(password.env);
366
+ const createdb = deps.createdb(password.env);
367
+ if (dropdb === null || createdb === null) {
368
+ const missing = [dropdb === null && 'dropdb', createdb === null && 'createdb']
369
+ .filter(Boolean)
370
+ .join(' and ');
371
+ log.error(`${missing} not found — install PostgreSQL to reset a Postgres database`);
372
+ return { reset: false, skipped: false, db_type };
270
373
  }
271
- const drop = await deps.run_command('dropdb', ['--if-exists', db_name]);
272
- if (!drop.success) {
273
- log.error(`Failed to drop database: ${drop.stderr}`);
274
- return { reset: false, skipped: false, db_type: 'postgres' };
374
+ const drop = await dropdb.out(to_dropdb_args(db_name, database_url));
375
+ if (!is_program_success(drop)) {
376
+ log.error(`Failed to drop database: ${describe_program_failure(drop)}${password_hint(drop, password.env)}`);
377
+ return { reset: false, skipped: false, db_type };
275
378
  }
276
379
  log.ok(`Dropped database: ${db_name}`);
277
- const create = await deps.run_command('createdb', [db_name]);
278
- if (!create.success) {
279
- log.error(`Failed to create database: ${create.stderr}`);
280
- return { reset: false, skipped: false, db_type: 'postgres' };
380
+ const create = await createdb.out(to_createdb_args(db_name, database_url));
381
+ if (!is_program_success(create)) {
382
+ log.error(`Failed to create database: ${describe_program_failure(create)}${password_hint(create, password.env)}`);
383
+ return { reset: false, skipped: false, db_type };
281
384
  }
282
385
  log.ok(`Created database: ${db_name}`);
283
- return { reset: true, skipped: false, db_type: 'postgres' };
386
+ return { reset: true, skipped: false, db_type };
284
387
  };
285
388
  /**
286
389
  * Seed a development test account, bypassing the username and password
@@ -293,7 +396,7 @@ export const reset_database = async (deps, database_url, options) => {
293
396
  * requested role grants. Never updates an existing password (rerun would
294
397
  * silently rotate it).
295
398
  *
296
- * Intended for `scripts/dev_setup.ts` — do not call in production.
399
+ * Intended for a dev setup task — do not call in production.
297
400
  *
298
401
  * @mutates database - inserts an account/actor pair when missing and grants any requested role role_grants
299
402
  * @throws Error if an existing account is found without an associated actor row
@@ -40,7 +40,7 @@ export declare const parse_dotenv: (content: string) => Record<string, string>;
40
40
  * @param runtime - runtime with `read_text_file` capability
41
41
  * @param path - path to env file
42
42
  * @returns parsed env record, or `null` if file doesn't exist
43
- * @throws Error if reading fails for any reason other than `ENOENT` / `NotFound`
43
+ * @throws Error if reading fails for any reason other than `ENOENT`
44
44
  */
45
45
  export declare const load_env_file: (runtime: Pick<FsReadDeps, "read_text_file">, path: string) => Promise<Record<string, string> | null>;
46
46
  //# sourceMappingURL=dotenv.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dotenv.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/env/dotenv.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,eAAO,MAAM,YAAY,GAAI,SAAS,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAqCnE,CAAC;AAiEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,GACzB,SAAS,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,EAC3C,MAAM,MAAM,KACV,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CASvC,CAAC"}
1
+ {"version":3,"file":"dotenv.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/env/dotenv.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,eAAO,MAAM,YAAY,GAAI,SAAS,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAqCnE,CAAC;AAiEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,aAAa,GACzB,SAAS,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,EAC3C,MAAM,MAAM,KACV,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAQvC,CAAC"}