vitecompose 0.0.0-stage → 0.1.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.
@@ -0,0 +1,292 @@
1
+ import { ChildProcess } from "node:child_process";
2
+ type RunOptions = {
3
+ cwd?: string;
4
+ env?: NodeJS.ProcessEnv;
5
+ /** Written to the child's stdin, which is then closed. */
6
+ input?: string | Uint8Array;
7
+ /** Upper bound for the whole command. The child is killed when it passes. */
8
+ timeoutMs: number;
9
+ /** Cancels the command and ends the child. */
10
+ signal?: AbortSignal;
11
+ /** Keeps arguments and output out of every error this command produces. */
12
+ private?: boolean;
13
+ };
14
+ type RunResult = {
15
+ /** Real exit status, or null when the child was ended by a signal. */
16
+ status: number | null;
17
+ signal: NodeJS.Signals | null;
18
+ /** Decoded as UTF-8 and untrimmed. Use the byte fields when exact bytes matter. */
19
+ stdout: string;
20
+ stderr: string;
21
+ stdoutBytes: Buffer;
22
+ stderrBytes: Buffer;
23
+ };
24
+ /** Ends every child this process started and has not yet seen exit. Safe to call from a signal handler. */
25
+ declare const killOwnedChildren: (signal?: NodeJS.Signals) => void;
26
+ declare class CommandStartError extends Error {
27
+ name: string;
28
+ constructor(command: string, args: readonly string[], cause: Error, isPrivate?: boolean);
29
+ }
30
+ declare class CommandTimeoutError extends Error {
31
+ name: string;
32
+ constructor(command: string, args: readonly string[], timeoutMs: number, isPrivate?: boolean);
33
+ }
34
+ declare class CommandCanceledError extends Error {
35
+ name: string;
36
+ constructor(command: string, args: readonly string[], isPrivate?: boolean);
37
+ }
38
+ declare class CommandFailedError extends Error {
39
+ name: string;
40
+ readonly status: number | null;
41
+ /** Absent for private commands, because the output could contain what they protect. */
42
+ readonly result?: RunResult;
43
+ constructor(command: string, args: readonly string[], result: RunResult, isPrivate?: boolean);
44
+ }
45
+ /**
46
+ * Runs a command without blocking the event loop, so Vitest timeouts can still fire.
47
+ * A nonzero exit is a result, not an error. Failure to start, timeouts and cancellation reject.
48
+ */
49
+ declare const run: (command: string, args: readonly string[], options: RunOptions) => Promise<RunResult>;
50
+ /** Like `run`, but any exit other than status 0 rejects with `CommandFailedError`. */
51
+ declare const runChecked: (command: string, args: readonly string[], options: RunOptions) => Promise<RunResult>;
52
+ type BackgroundProcess = {
53
+ child: ChildProcess;
54
+ /** Resolves when the process has exited, whatever the reason. */
55
+ exited: Promise<void>;
56
+ isRunning: () => boolean;
57
+ stop: () => Promise<void>;
58
+ };
59
+ /** Starts a long-lived child that this process owns. It is ended on process exit if not stopped first. */
60
+ declare const startBackground: (command: string, args: readonly string[], options?: {
61
+ env?: NodeJS.ProcessEnv;
62
+ }) => BackgroundProcess;
63
+ type WaitOptions = {
64
+ timeoutMs: number;
65
+ intervalMs?: number;
66
+ signal?: AbortSignal;
67
+ };
68
+ declare class WaitTimeoutError extends Error {
69
+ name: string;
70
+ readonly lastState: unknown;
71
+ constructor(what: string, timeoutMs: number, lastState: unknown);
72
+ }
73
+ type WaitForOptions<T> = WaitOptions & {
74
+ /** Named in the timeout error. */
75
+ describe: string;
76
+ /**
77
+ * Produces what the timeout error reports. It is opt-in because observed values can hold secrets,
78
+ * such as a container's environment, so return only fields that are safe to print.
79
+ */
80
+ summarize?: (value: T) => unknown;
81
+ };
82
+ /**
83
+ * Polls `read` until `predicate` accepts the value, and returns that value.
84
+ * An error from `read` or `predicate` ends the wait: broken access must not look like "not ready yet",
85
+ * and a predicate can throw to report a terminal failure immediately.
86
+ */
87
+ declare const waitFor: <T>(read: () => Promise<T>, predicate: (value: T) => boolean, options: WaitForOptions<T>) => Promise<T>;
88
+ type ComposeTarget = {
89
+ projectName: string;
90
+ /** Absolute paths to Compose files (user files plus vitecompose's ownership override when managed). */
91
+ composeFiles: readonly string[];
92
+ cwd: string;
93
+ envFile?: string;
94
+ };
95
+ type ComposePublisher = {
96
+ url: string;
97
+ targetPort: number;
98
+ publishedPort: number;
99
+ protocol: string;
100
+ };
101
+ type ContainerStateName = "created" | "running" | "paused" | "restarting" | "removing" | "exited" | "dead" | "unknown";
102
+ type ContainerHealthName = "starting" | "healthy" | "unhealthy" | "none" | "";
103
+ type ComposeContainer = {
104
+ id: string;
105
+ name: string;
106
+ service: string;
107
+ state: ContainerStateName;
108
+ status: string;
109
+ health: ContainerHealthName;
110
+ exitCode: number;
111
+ publishers: ComposePublisher[];
112
+ };
113
+ type ComposeContainerSummary = {
114
+ name: string;
115
+ service: string;
116
+ state: ContainerStateName;
117
+ status: string;
118
+ health?: ContainerHealthName;
119
+ exitCode?: number;
120
+ };
121
+ type ComposeClientOptions = {
122
+ rootDir?: string;
123
+ signal?: AbortSignal;
124
+ /** Called when an operation damaged shared stack state and could not restore it. */
125
+ onEnvironmentDamaged?: (reason: string) => Promise<void> | void;
126
+ };
127
+ type ComposeUpOptions = {
128
+ services?: readonly string[];
129
+ profiles?: readonly string[];
130
+ build?: boolean;
131
+ forceRecreate?: boolean;
132
+ noDeps?: boolean;
133
+ wait?: boolean;
134
+ timeoutMs?: number;
135
+ env?: NodeJS.ProcessEnv;
136
+ };
137
+ type ComposeDownOptions = {
138
+ volumes?: boolean;
139
+ removeOrphans?: boolean;
140
+ profiles?: readonly string[];
141
+ timeoutMs?: number;
142
+ };
143
+ type ComposeExecOptions = {
144
+ user?: string;
145
+ workdir?: string;
146
+ env?: Record<string, string>;
147
+ input?: string | Uint8Array;
148
+ timeoutMs?: number;
149
+ private?: boolean;
150
+ };
151
+ type ComposeRunOptions = {
152
+ rm?: boolean;
153
+ noDeps?: boolean;
154
+ user?: string;
155
+ workdir?: string;
156
+ env?: Record<string, string>;
157
+ input?: string | Uint8Array;
158
+ timeoutMs?: number;
159
+ private?: boolean;
160
+ };
161
+ type ComposeLogsOptions = {
162
+ tailLines?: number;
163
+ since?: string;
164
+ timestamps?: boolean;
165
+ timeoutMs?: number;
166
+ };
167
+ type TemporaryOutage = {
168
+ /** Restores the service early. Safe to call twice. */
169
+ restore: () => Promise<void>;
170
+ [Symbol.asyncDispose]: () => Promise<void>;
171
+ };
172
+ type StopTemporarilyOptions = {
173
+ /** Send SIGKILL (`docker compose kill`) instead of graceful `docker compose stop`. */
174
+ kill?: boolean;
175
+ signal?: string;
176
+ /** Wait for the service to come back as `running` or `healthy` on restore. Defaults to `"healthy"`. */
177
+ waitForRestore?: "running" | "healthy" | "none";
178
+ timeoutMs?: number;
179
+ restoreTimeoutMs?: number;
180
+ };
181
+ type PauseTemporarilyOptions = {
182
+ timeoutMs?: number;
183
+ restoreTimeoutMs?: number;
184
+ };
185
+ type ComposeClient = {
186
+ readonly target: ComposeTarget;
187
+ readonly rootDir: string;
188
+ up: (options?: ComposeUpOptions) => Promise<void>;
189
+ down: (options?: ComposeDownOptions) => Promise<void>;
190
+ ps: (options?: {
191
+ services?: readonly string[];
192
+ all?: boolean;
193
+ profiles?: readonly string[];
194
+ }) => Promise<ComposeContainer[]>;
195
+ inspect: (service: string) => Promise<ComposeContainer | null>;
196
+ port: (service: string, containerPort: number, options?: {
197
+ protocol?: "tcp" | "udp";
198
+ index?: number;
199
+ }) => Promise<{
200
+ host: string;
201
+ port: number;
202
+ }>;
203
+ url: (service: string, containerPort: number, options?: {
204
+ protocol?: "http" | "https";
205
+ path?: string;
206
+ index?: number;
207
+ }) => Promise<string>;
208
+ exec: (service: string, command: readonly string[], options?: ComposeExecOptions) => Promise<RunResult>;
209
+ run: (service: string, command?: readonly string[], options?: ComposeRunOptions) => Promise<RunResult>;
210
+ logs: (service: string, options?: ComposeLogsOptions) => Promise<string>;
211
+ start: (services: string | readonly string[], options?: {
212
+ timeoutMs?: number;
213
+ }) => Promise<void>;
214
+ stop: (services: string | readonly string[], options?: {
215
+ timeoutMs?: number;
216
+ }) => Promise<void>;
217
+ kill: (services: string | readonly string[], options?: {
218
+ signal?: string;
219
+ timeoutMs?: number;
220
+ }) => Promise<void>;
221
+ restart: (services: string | readonly string[], options?: {
222
+ timeoutMs?: number;
223
+ }) => Promise<void>;
224
+ pause: (services: string | readonly string[], options?: {
225
+ timeoutMs?: number;
226
+ }) => Promise<void>;
227
+ unpause: (services: string | readonly string[], options?: {
228
+ timeoutMs?: number;
229
+ }) => Promise<void>;
230
+ waitForRunning: (service: string, options?: Partial<WaitOptions>) => Promise<ComposeContainer>;
231
+ waitForHealthy: (service: string, options?: Partial<WaitOptions>) => Promise<ComposeContainer>;
232
+ waitForStopped: (service: string, options?: Partial<WaitOptions>) => Promise<ComposeContainer | null>;
233
+ waitForExit: (service: string, options?: Partial<WaitOptions>) => Promise<ComposeContainer>;
234
+ waitForLogs: (service: string, patterns: string | RegExp | readonly (string | RegExp)[], options?: Partial<WaitOptions> & {
235
+ tailLines?: number;
236
+ }) => Promise<string>;
237
+ waitForHttp: (service: string, containerPort: number, options?: Partial<WaitOptions> & {
238
+ path?: string;
239
+ protocol?: "http" | "https";
240
+ expectedStatus?: number | ((status: number) => boolean);
241
+ }) => Promise<string>;
242
+ stopTemporarily: (service: string, options?: StopTemporarilyOptions) => Promise<TemporaryOutage>;
243
+ pauseTemporarily: (service: string, options?: PauseTemporarilyOptions) => Promise<TemporaryOutage>;
244
+ };
245
+ /**
246
+ * Parses `docker compose ps --format json`, which emits either NDJSON (one JSON object per line)
247
+ * or a single JSON array depending on the Docker Compose version.
248
+ */
249
+ declare const parseComposePsJson: (stdout: string) => ComposeContainer[];
250
+ /**
251
+ * Parses `docker compose port <service> <port>` output (for example `0.0.0.0:54321` or `[::]:54321`).
252
+ * Wildcard bind addresses are normalized to `127.0.0.1` so HTTP clients and DB drivers can connect directly.
253
+ */
254
+ declare const parseComposePort: (stdout: string) => {
255
+ host: string;
256
+ port: number;
257
+ };
258
+ /** Safe summary for timeout errors and failure evidence — never includes container env vars. */
259
+ declare const summarizeContainer: (container: ComposeContainer | null) => ComposeContainerSummary | null;
260
+ declare const composeBaseArgs: (target: ComposeTarget, profiles?: readonly string[]) => string[];
261
+ declare const createComposeClient: (target: ComposeTarget, options?: ComposeClientOptions) => ComposeClient;
262
+ type DeferOptions = {
263
+ /** Named in errors, so a failed or hung cleanup can be identified. */
264
+ label?: string;
265
+ timeoutMs?: number;
266
+ };
267
+ declare class DisposalError extends AggregateError {
268
+ name: string;
269
+ constructor(errors: unknown[]);
270
+ }
271
+ type Scope = {
272
+ /** Register a cleanup immediately after acquiring what it releases. */
273
+ defer: (cleanup: () => unknown, options?: DeferOptions) => void;
274
+ /** Runs cleanups in reverse order, all of them even if some fail. Safe to call twice. */
275
+ dispose: () => Promise<void>;
276
+ [Symbol.asyncDispose]: () => Promise<void>;
277
+ };
278
+ /**
279
+ * Vitest does not time out fixture teardown, so each cleanup carries its own limit.
280
+ * A hung cleanup is reported and the remaining ones still run.
281
+ */
282
+ declare const createScope: (options?: {
283
+ cleanupTimeoutMs?: number;
284
+ }) => Scope;
285
+ /**
286
+ * Runs `body` with a scope and always disposes it. If `body` fails, its error is the one thrown,
287
+ * and cleanup failures are logged. A failed cleanup must not replace the failure that explains the run.
288
+ */
289
+ declare const withScope: <T>(body: (scope: Scope) => Promise<T>, options?: {
290
+ cleanupTimeoutMs?: number;
291
+ }) => Promise<T>;
292
+ export { waitFor as A, runChecked as B, createComposeClient as C, WaitForOptions as D, summarizeContainer as E, CommandTimeoutError as F, RunOptions as I, RunResult as L, CommandCanceledError as M, CommandFailedError as N, WaitOptions as O, CommandStartError as P, killOwnedChildren as R, composeBaseArgs as S, parseComposePsJson as T, startBackground as V, ContainerHealthName as _, withScope as a, StopTemporarilyOptions as b, ComposeContainer as c, ComposeExecOptions as d, ComposeLogsOptions as f, ComposeUpOptions as g, ComposeTarget as h, createScope as i, BackgroundProcess as j, WaitTimeoutError as k, ComposeContainerSummary as l, ComposeRunOptions as m, DisposalError as n, ComposeClient as o, ComposePublisher as p, Scope as r, ComposeClientOptions as s, DeferOptions as t, ComposeDownOptions as u, ContainerStateName as v, parseComposePort as w, TemporaryOutage as x, PauseTemporarilyOptions as y, run as z };