@molecule/api-staging-docker-compose 1.0.0 → 1.0.1

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 (2) hide show
  1. package/README.md +319 -0
  2. package/package.json +5 -4
package/README.md ADDED
@@ -0,0 +1,319 @@
1
+ <!--
2
+ AUTO-GENERATED — DO NOT EDIT THIS FILE.
3
+ Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
4
+ Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
5
+ To change this document, edit the module-level JSDoc in src/index.ts.
6
+ Generated: 2026-08-04T01:49:36.498Z
7
+ -->
8
+
9
+ # @molecule/api-staging-docker-compose
10
+
11
+ > **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
12
+ > It is written to be read by coding agents as much as by people, and is generated from this
13
+ > package's source — edit `src/index.ts` JSDoc, not this file.
14
+
15
+ Docker Compose staging driver for molecule.dev.
16
+
17
+ Manages ephemeral staging environments using Docker Compose.
18
+ Each feature branch gets isolated API, App, and database containers
19
+ with unique port allocations and networking.
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import { setProvider } from '@molecule/api-staging'
25
+ import { provider } from '@molecule/api-staging-docker-compose'
26
+
27
+ setProvider(provider)
28
+ ```
29
+
30
+ ## Type
31
+
32
+ `provider`
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ npm install @molecule/api-staging-docker-compose @molecule/api-staging
38
+ ```
39
+
40
+ ## API
41
+
42
+ ### Interfaces
43
+
44
+ #### `ComposeContainerStatus`
45
+
46
+ A single service row parsed from `docker compose ps --format json`.
47
+
48
+ ```typescript
49
+ interface ComposeContainerStatus {
50
+ Service: string
51
+ State: string
52
+ Health: string
53
+ }
54
+ ```
55
+
56
+ #### `ComposeGeneratorConfig`
57
+
58
+ Configuration for Docker Compose file generation.
59
+
60
+ ```typescript
61
+ interface ComposeGeneratorConfig {
62
+ /** Allocated API port on the host. */
63
+ apiPort: number
64
+
65
+ /** Allocated App port on the host. */
66
+ appPort: number
67
+
68
+ /** Allocated database port on the host. */
69
+ dbPort: number
70
+
71
+ /** Relative path from compose file to the API project root. */
72
+ apiContext?: string
73
+
74
+ /** Relative path from compose file to the App project root. */
75
+ appContext?: string
76
+
77
+ /** Path to generated Dockerfiles directory, relative to the compose file. */
78
+ dockerfilePath?: string
79
+ }
80
+ ```
81
+
82
+ ### Functions
83
+
84
+ #### `containerStatus(container)`
85
+
86
+ Returns the human-readable status for a parsed `docker compose ps`
87
+ container row — the `Health` value when a healthcheck is defined,
88
+ otherwise the raw container `State`, or `'not found'` when the service
89
+ isn't running at all.
90
+
91
+ ```typescript
92
+ function containerStatus(container?: ComposeContainerStatus): string
93
+ ```
94
+
95
+ - `container` — The parsed container row, or `undefined` if the service wasn't found.
96
+
97
+ **Returns:** The status string.
98
+
99
+ #### `fallbackPort(base, slug, range)`
100
+
101
+ Deterministically derives a fallback host port from an environment slug so
102
+ that TWO environments falling back to this path (no `driverMeta` — direct
103
+ provider callers only; `mlcl stage up` always allocates real ports via
104
+ `allocatePort()`) don't collide on the SAME fixed port. Uses FNV-1a, a
105
+ stable, dependency-free 32-bit hash — the same slug always maps to the same
106
+ port across repeated `up()` calls, so redeploying an existing slug doesn't
107
+ relocate it.
108
+
109
+ ```typescript
110
+ function fallbackPort(base: number, slug: string, range?: number): number
111
+ ```
112
+
113
+ - `base` — The base port for this role (api/app/db).
114
+ - `slug` — The environment slug to derive an offset from.
115
+ - `range` — Width of the offset window (default 100 — kept narrow enough that api/app/db's offset windows never overlap each other).
116
+
117
+ **Returns:** `base` plus a slug-derived offset in `[0, range)`.
118
+
119
+ #### `generateApiDockerfile()`
120
+
121
+ Generates a multi-stage Dockerfile for the API server.
122
+
123
+ Stage 1 (build): installs all deps, compiles TypeScript.
124
+ Stage 2 (runtime): copies compiled output, installs production deps only.
125
+
126
+ ```typescript
127
+ function generateApiDockerfile(): string
128
+ ```
129
+
130
+ **Returns:** Dockerfile content as a string.
131
+
132
+ #### `generateAppDockerfile(envVars)`
133
+
134
+ Generates a multi-stage Dockerfile for the frontend app.
135
+
136
+ Stage 1 (build): installs deps, injects Vite env vars via Docker ARG,
137
+ then runs the Vite build. ARG → ENV ensures \`process.env.VITE_*\` is
138
+ available to Vite at build time (takes precedence over .env files).
139
+
140
+ Stage 2 (serve): copies built static assets into Nginx. The nginx.conf is
141
+ copied from the \`staging\` named build context (the \`.molecule/staging/\`
142
+ directory, wired via \`additional_contexts\` in the generated compose file) —
143
+ NOT from the app build context: the app project has no nginx.conf of its
144
+ own, so a plain \`COPY nginx.conf\` would fail every build with
145
+ "not found in build context".
146
+
147
+ ```typescript
148
+ function generateAppDockerfile(envVars?: Record<string, string>): string
149
+ ```
150
+
151
+ - `envVars` — Build-time environment variables to inject (e.g. \`{ VITE_API_URL: '...' }\`).
152
+
153
+ **Returns:** Dockerfile content as a string.
154
+
155
+ #### `generateComposeFile(env, config)`
156
+
157
+ Generates a Docker Compose YAML string for a staging environment.
158
+
159
+ The API service uses runtime env_file layering (Node.js reads process.env).
160
+ The App service uses Docker build args for VITE_* variables (Vite inlines
161
+ them at build time; runtime env vars have no effect on the served bundle).
162
+
163
+ Requires Docker Compose v2.24+ (long-syntax `env_file` with `required:
164
+ false` — the base `.env` / `.env.staging` layers are OPTIONAL; short syntax
165
+ hard-fails `up` on any project that doesn't have both files) and BuildKit
166
+ (`additional_contexts`, which lets the app image copy the generated
167
+ `nginx.conf` from the staging directory — it is NOT inside the app build
168
+ context).
169
+
170
+ The `api` and `app` services each define a `healthcheck:` (previously only
171
+ `db` did) so `health()` can read the real `Health` field from `docker
172
+ compose ps` instead of treating container `State === 'running'` as
173
+ healthy — a process that is up but never came up serving (or is
174
+ crash-looping between polls) used to read as a fully healthy environment.
175
+ `api`'s probe hits the `/health` route every molecule-scaffolded API
176
+ server exposes (`server.ts` — this driver already assumes that shape via
177
+ the hardcoded `node dist/server.js` Dockerfile CMD); `app`'s probe hits the
178
+ Nginx root, which always serves the built `index.html`.
179
+
180
+ ```typescript
181
+ function generateComposeFile(env: StagingEnvironment, config: ComposeGeneratorConfig): string
182
+ ```
183
+
184
+ - `env` — The staging environment descriptor.
185
+ - `config` — Port and path configuration.
186
+
187
+ **Returns:** A Docker Compose YAML string.
188
+
189
+ #### `generateNginxConf()`
190
+
191
+ Generates an SPA-compatible Nginx configuration.
192
+
193
+ Uses \`try_files\` to serve \`index.html\` for all routes, which is
194
+ required for client-side routing (React Router, Vue Router, etc.).
195
+
196
+ ```typescript
197
+ function generateNginxConf(): string
198
+ ```
199
+
200
+ **Returns:** Nginx config content as a string.
201
+
202
+ #### `isComposeVersionSufficient(version)`
203
+
204
+ Checks a parsed Compose version against {@link REQUIRED_COMPOSE_VERSION}.
205
+
206
+ ```typescript
207
+ function isComposeVersionSufficient(version: { major: number; minor: number }): boolean
208
+ ```
209
+
210
+ - `version` — A parsed `{ major, minor }` version.
211
+
212
+ **Returns:** `true` if the version meets or exceeds the minimum this driver requires.
213
+
214
+ #### `isContainerHealthy(container)`
215
+
216
+ Determines whether a parsed `docker compose ps` container row counts as
217
+ healthy. `generateComposeFile()` now defines a `healthcheck:` for the `api`
218
+ AND `app` services (it always did for `db`), so `Health` is populated for
219
+ both — `State === 'running'` alone used to be treated as healthy, which is
220
+ also true of a process that is up but never came up serving (or is
221
+ crash-looping between polls). Falls back to `State === 'running'` only for
222
+ a container with NO healthcheck (an environment staged with a compose file
223
+ generated before this fix, or a hand-edited one) — an empty `Health`
224
+ string is how `docker compose ps` reports "no healthcheck defined".
225
+
226
+ ```typescript
227
+ function isContainerHealthy(container?: ComposeContainerStatus): boolean
228
+ ```
229
+
230
+ - `container` — The parsed container row, or `undefined` if the service wasn't found.
231
+
232
+ **Returns:** `true` if the container is healthy.
233
+
234
+ #### `parseComposeVersion(output)`
235
+
236
+ Parses a major.minor pair out of `docker compose version` output (e.g.
237
+ `'Docker Compose version v2.24.5'` or the bare `'2.24.5'` from `--short`).
238
+ Exported for testing — no need to shell out to `docker` to verify the
239
+ parsing logic against every version-string shape Compose has printed.
240
+
241
+ ```typescript
242
+ function parseComposeVersion(output: string): { major: number; minor: number } | null
243
+ ```
244
+
245
+ - `output` — Raw stdout from `docker compose version`.
246
+
247
+ **Returns:** The parsed `{ major, minor }`, or `null` if no version substring is found.
248
+
249
+ ### Constants
250
+
251
+ #### `provider`
252
+
253
+ Docker Compose staging driver implementation.
254
+
255
+ ```typescript
256
+ const provider: StagingDriver
257
+ ```
258
+
259
+ ## Core Interface
260
+
261
+ Implements `@molecule/api-staging` interface.
262
+
263
+ ## Bond Wiring
264
+
265
+ Setup function to register this provider with the core interface:
266
+
267
+ ```typescript
268
+ import { setProvider } from '@molecule/api-staging'
269
+ import { provider } from '@molecule/api-staging-docker-compose'
270
+
271
+ export function setupStagingDockerCompose(): void {
272
+ setProvider(provider)
273
+ }
274
+ ```
275
+
276
+ ## Injection Notes
277
+
278
+ ### Requirements
279
+
280
+ Peer dependencies:
281
+
282
+ - `@molecule/api-staging` ^1.0.1
283
+
284
+ ### Runtime Dependencies
285
+
286
+ - `@molecule/api-staging`
287
+
288
+ - Requires Docker Compose v2.24+ and BuildKit: the generated compose file
289
+ uses long-syntax `env_file` (`required: false` — a project without `.env`
290
+ or `.env.staging` must still stage) and a named `additional_contexts`
291
+ build context (the generated `nginx.conf` lives in `.molecule/staging/`,
292
+ outside the app build context). `checkPrerequisites()` parses `docker
293
+ compose version` and names `'docker-compose >= 2.24 (found X.Y)'` in
294
+ `missing` when the installed engine predates this — before `up()` hits
295
+ the engine's opaque compose parse error.
296
+ - Inside the containers the API always listens on port 4000 and Postgres on
297
+ 5432; the allocated `driverMeta` ports (`apiPort`, `appPort`, `dbPort`)
298
+ are HOST-side mappings only. The branch env file
299
+ (`.env.staging.<slug>`) intentionally holds the host-side values for
300
+ tooling run outside Docker — the compose `environment:` block overrides
301
+ `PORT`/`DATABASE_URL` back to the in-container values.
302
+ - When `env.driverMeta` has no port overrides (direct provider callers
303
+ only — `mlcl stage up` always allocates real ports via `allocatePort()`),
304
+ `up()` derives `apiPort`/`appPort`/`dbPort` deterministically from
305
+ `env.slug` (`fallbackPort()`) instead of three fixed ports, so two
306
+ concurrently-staged slugs don't collide on identical ports.
307
+ - `health()` reads the REAL `Health` field from `docker compose ps` for the
308
+ `api` and `app` services (both now define a `healthcheck:` hitting
309
+ `/health` and `/` respectively) — a booted-but-not-yet-serving or
310
+ crash-looping container no longer reads as healthy just because its
311
+ process state is `running`. `logs()` still needs a look for WHY an
312
+ unhealthy service is unhealthy.
313
+ - `logs({ follow: true })` throws — this provider returns a single
314
+ `Promise<EnvironmentLogs>` snapshot, not a stream, so `follow` cannot be
315
+ honored; it is rejected explicitly instead of silently returning a
316
+ static tail.
317
+ - Environments live on the machine that ran `up()` (containers + state are
318
+ local) — running this driver on an ephemeral CI runner produces an
319
+ environment that dies with the job.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@molecule/api-staging-docker-compose",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Docker Compose staging driver for local ephemeral environments in molecule.dev",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -17,7 +17,8 @@
17
17
  }
18
18
  },
19
19
  "files": [
20
- "dist"
20
+ "dist",
21
+ "README.md"
21
22
  ],
22
23
  "keywords": [
23
24
  "molecule",
@@ -27,10 +28,10 @@
27
28
  ],
28
29
  "license": "Apache-2.0",
29
30
  "peerDependencies": {
30
- "@molecule/api-staging": "^1.0.0"
31
+ "@molecule/api-staging": "^1.0.1"
31
32
  },
32
33
  "devDependencies": {
33
- "@molecule/api-staging": "1.0.0",
34
+ "@molecule/api-staging": "1.0.1",
34
35
  "@types/node": "26.1.2",
35
36
  "typescript": "6.0.3",
36
37
  "vitest": "4.1.10"