@lunora/container 0.0.0 → 1.0.0-alpha.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.
@@ -0,0 +1,189 @@
1
+ import { a as ContainerConfig, C as ContainerDefinition, b as ContainerImageSource, N as NormalizedContainerImage } from "./packem_shared/types.d-D2l2SYol.js";
2
+ export type { B as BuildImageSource, c as ContainerInstanceType, d as ContainerRollout, e as CustomContainerInstanceType, f as NamedContainerInstanceType, R as RegistryImageSource } from "./packem_shared/types.d-D2l2SYol.js";
3
+ /**
4
+ * The `ctx.containers` action surface: typed handles over the `CONTAINER_*`
5
+ * Durable Object namespace bindings the config layer reconciles.
6
+ *
7
+ * Deliberately structural (no `@cloudflare/containers` import): a Durable
8
+ * Object namespace stub is all that is needed to route a request to a
9
+ * container-enabled DO, so this module stays Node-safe and the test double
10
+ * below can satisfy the exact same shape without a workerd runtime.
11
+ */
12
+ /** Options for explicitly starting an instance (mirrors `@cloudflare/containers`). */
13
+ interface ContainerStartOptions {
14
+ /** Override outbound internet access for this start. */
15
+ enableInternet?: boolean;
16
+ /** Override the container entrypoint. */
17
+ entrypoint?: string[];
18
+ /** Per-instance environment, merged over the definition's `env`/secrets. */
19
+ envVars?: Record<string, string>;
20
+ /** Metadata labels attached for metrics/observability. */
21
+ labels?: Record<string, string>;
22
+ }
23
+ /** A container instance's runtime state, as returned by `getState()`. Structural — the platform adds fields over time. */
24
+ interface ContainerInstanceState {
25
+ [key: string]: unknown;
26
+ lastChange?: number;
27
+ }
28
+ /** What a handle needs from a Durable Object stub — `fetch` plus the optional lifecycle RPCs the container DO exposes. */
29
+ interface ContainerStubLike {
30
+ destroy?: () => Promise<void>;
31
+ fetch: (input: Request) => Promise<Response>;
32
+ getState?: () => Promise<ContainerInstanceState>;
33
+ start?: (options?: ContainerStartOptions) => Promise<void>;
34
+ stop?: (signal?: number | string) => Promise<void>;
35
+ }
36
+ /** What the client needs from a Durable Object namespace binding. */
37
+ interface ContainerNamespaceLike {
38
+ get: (id: unknown) => ContainerStubLike;
39
+ idFromName: (name: string) => unknown;
40
+ }
41
+ /** A handle on one container instance (one Durable Object). */
42
+ interface ContainerHandle {
43
+ /**
44
+ * Send an HTTP (or WebSocket-upgrade) request to the container. A path
45
+ * string (`"/transcode"`) is resolved against a synthetic origin; a full
46
+ * `Request`/URL passes through unchanged.
47
+ */
48
+ fetch: (input: Request | string, init?: RequestInit) => Promise<Response>;
49
+ }
50
+ /**
51
+ * A handle on a *named* instance (from `.get(name)`) — `fetch` plus explicit
52
+ * lifecycle control. The per-entity pattern (a sandbox per user, a room per
53
+ * game, a job runner per id) often needs to tear down or inspect the instance
54
+ * rather than wait for `sleepAfter`, so these wrap the container DO's
55
+ * `start`/`stop`/`destroy`/`getState`.
56
+ */
57
+ interface ContainerInstanceHandle extends ContainerHandle {
58
+ /** Stop and discard the instance (its ephemeral disk is lost). */
59
+ destroy: () => Promise<void>;
60
+ /** Read the instance's current runtime state. */
61
+ getState: () => Promise<ContainerInstanceState>;
62
+ /** Explicitly start the instance, optionally with per-instance env/entrypoint. */
63
+ start: (options?: ContainerStartOptions) => Promise<void>;
64
+ /** Stop the instance (optionally with a signal); it can start again on the next request. */
65
+ stop: (signal?: number | string) => Promise<void>;
66
+ }
67
+ /** The per-definition accessor exposed as `ctx.containers.&lt;exportName>`. */
68
+ interface ContainerAccessor {
69
+ /**
70
+ * A random instance from a fixed pool of `count` (defaults to the
71
+ * definition's `maxInstances`, else 3 — mirroring `getRandom` from
72
+ * `@cloudflare/containers`). For stateless, interchangeable workloads.
73
+ */
74
+ any: (count?: number) => ContainerHandle;
75
+ /** The instance for `name` — one container per entity (user, room, job…), with lifecycle control. */
76
+ get: (name: string) => ContainerInstanceHandle;
77
+ /**
78
+ * A resilient handle over the pool: each `fetch` picks a random instance and,
79
+ * on a thrown error or a retryable response (5xx by default), retries on a
80
+ * freshly-picked instance with exponential backoff. Until Cloudflare ships
81
+ * native autoscaling + health-aware routing this is the recommended way to
82
+ * call a stateless container pool — it rides over a single cold/unhealthy
83
+ * instance instead of failing the whole request.
84
+ *
85
+ * Because a retry re-issues the request, pass a **replayable** body — a path
86
+ * string plus an `init.body` string/`ArrayBuffer` (re-created each attempt).
87
+ * A pre-built `Request` carrying a stream body can only be sent once, so it
88
+ * is not retry-safe here; use `.get()`/`.any()` for those.
89
+ */
90
+ pool: (options?: PoolOptions) => ContainerHandle;
91
+ }
92
+ /** Tuning for a pooled, retrying container handle. See {@link ContainerAccessor.pool}. */
93
+ interface PoolOptions {
94
+ /** Total attempts before giving up (each on a freshly-picked instance). Default 3. */
95
+ attempts?: number;
96
+ /** Base backoff in ms between attempts; doubles each retry (0 disables the wait). Default 100. */
97
+ backoffMs?: number;
98
+ /**
99
+ * Upper bound on a single backoff sleep, in ms. The doubling delay is clamped
100
+ * to this ceiling so a large `attempts` count can't produce an unboundedly
101
+ * long wait. Default {@link DEFAULT_MAX_BACKOFF_MS} (30s).
102
+ */
103
+ maxBackoffMs?: number;
104
+ /**
105
+ * Whether a *returned* response should be retried on another instance.
106
+ * Defaults to retrying any `5xx`. A thrown error (network/start failure) is
107
+ * always retried regardless of this predicate.
108
+ */
109
+ retryOn?: (response: Response) => boolean;
110
+ /** Pool size to spread picks across. Defaults to the definition's `maxInstances`, else 3. */
111
+ size?: number;
112
+ }
113
+ /** Wiring info for one definition, emitted by codegen into the generated DO. */
114
+ interface ContainerBindingSpec {
115
+ /** Durable Object binding name, e.g. `CONTAINER_TRANSCODER`. */
116
+ binding: string;
117
+ /** The `lunora/containers.ts` export name, e.g. `transcoder`. */
118
+ exportName: string;
119
+ /** Pool size default for `.any()`. */
120
+ maxInstances?: number;
121
+ }
122
+ /**
123
+ * Build the `ctx.containers` record from the Worker `env`. Called by the
124
+ * generated ShardDO with the specs codegen derived from
125
+ * `lunora/containers.ts`. A missing binding doesn't throw here — only when the
126
+ * handle is actually used — so one unprovisioned container never breaks
127
+ * unrelated functions.
128
+ */
129
+ declare const createContainerContext: (env: Record<string, unknown>, specs: ReadonlyArray<ContainerBindingSpec>) => Record<string, ContainerAccessor>;
130
+ /** A test handler: receives the request plus the targeted instance name. */
131
+ type ContainerTestHandler = (request: Request, instance: {
132
+ name: string;
133
+ }) => Promise<Response> | Response;
134
+ /**
135
+ * Docker-free test double for `ctx.containers`: each export name maps to a
136
+ * fetch handler that plays the container. Mirrors the real shape exactly, so
137
+ * action handlers under test can't tell the difference.
138
+ *
139
+ * ```ts
140
+ * const containers = createContainerTestContext({
141
+ * transcoder: (request) => new Response("ok"),
142
+ * });
143
+ * ```
144
+ */
145
+ declare const createContainerTestContext: (handlers: Record<string, ContainerTestHandler>) => Record<string, ContainerAccessor>;
146
+ /**
147
+ * Normalize a `ContainerImageSource` into the shape wrangler wants: a
148
+ * Dockerfile path + build context for local builds, or a fully-qualified
149
+ * reference for pre-built images.
150
+ *
151
+ * A local-path string whose basename starts with `Dockerfile` (so
152
+ * `Dockerfile.dev` also counts) is used as-is with its directory as the build
153
+ * context; any other path is treated as the build-context directory and the
154
+ * Dockerfile is expected at `&lt;dir>/Dockerfile`.
155
+ */
156
+ declare const normalizeContainerImage: (image: ContainerImageSource) => NormalizedContainerImage;
157
+ /**
158
+ * The generated Container DO class name for a `lunora/containers.ts` export:
159
+ * `transcoder` → `TranscoderContainer`. wrangler's `containers[].class_name`
160
+ * and the Durable Object binding's `class_name` both reference it, so codegen
161
+ * and the config layer MUST derive it identically — always via this helper.
162
+ */
163
+ declare const containerClassName: (exportName: string) => string;
164
+ /**
165
+ * The Durable Object binding name for a container export: `transcoder` →
166
+ * `CONTAINER_TRANSCODER`, `imageResizer` → `CONTAINER_IMAGE_RESIZER`. The
167
+ * `CONTAINER_` prefix namespaces these away from `SHARD`/`SESSION`/`SCHEDULER`
168
+ * so a container export can never collide with the built-in bindings.
169
+ */
170
+ declare const containerBindingName: (exportName: string) => string;
171
+ /**
172
+ * The local image tag a Railpack `{ build }` container is built and pushed
173
+ * under: `transcoder` → `lunora-transcoder:build`. The config reconciler writes
174
+ * it as the wrangler `containers[].image`, and `lunora deploy` builds that tag
175
+ * with Railpack and `wrangler containers push`es it before deploying — so all
176
+ * three derive the tag from this one helper and can never disagree.
177
+ */
178
+ declare const containerBuildTag: (exportName: string) => string;
179
+ declare const defineContainer: (config: ContainerConfig) => ContainerDefinition;
180
+ /** True when a value is a `defineContainer` result (the runtime brand check). */
181
+ declare const isContainerDefinition: (value: unknown) => value is ContainerDefinition;
182
+ /**
183
+ * The container's full environment at instance start: the static `env` block
184
+ * plus every declared secret resolved from the Worker `env`. A declared secret
185
+ * missing from the Worker env fails fast — starting the container without a
186
+ * credential it was promised yields far worse errors downstream.
187
+ */
188
+ declare const resolveContainerEnvVariables: (definition: ContainerDefinition, workerEnv: Record<string, unknown>, exportName?: string) => Record<string, string>;
189
+ export { type ContainerAccessor, type ContainerBindingSpec, type ContainerConfig, type ContainerDefinition, type ContainerHandle, type ContainerImageSource, type ContainerInstanceHandle, type ContainerInstanceState, type ContainerNamespaceLike, type ContainerStartOptions, type ContainerTestHandler, type NormalizedContainerImage, type PoolOptions, containerBindingName, containerBuildTag, containerClassName, createContainerContext, createContainerTestContext, defineContainer, isContainerDefinition, normalizeContainerImage, resolveContainerEnvVariables as resolveContainerEnvVars };
package/dist/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export { createContainerContext, createContainerTestContext } from './packem_shared/createContainerContext-ChDD53ys.mjs';
2
+ export { containerBindingName, containerBuildTag, containerClassName, defineContainer, isContainerDefinition, normalizeContainerImage, resolveContainerEnvVars } from './packem_shared/containerBindingName-BGdSdFNA.mjs';
@@ -0,0 +1,116 @@
1
+ const NAMED_INSTANCE_TYPES = /* @__PURE__ */ new Set(["basic", "lite", "standard-1", "standard-2", "standard-3", "standard-4"]);
2
+ const ENV_NAME_PATTERN = /^[A-Z_]\w*$/i;
3
+ const SLEEP_AFTER_PATTERN = /^\d+[smh]$/;
4
+ const basename = (path) => {
5
+ const trimmed = path.endsWith("/") ? path.slice(0, -1) : path;
6
+ const separatorIndex = trimmed.lastIndexOf("/");
7
+ return separatorIndex === -1 ? trimmed : trimmed.slice(separatorIndex + 1);
8
+ };
9
+ const dirname = (path) => {
10
+ const separatorIndex = path.lastIndexOf("/");
11
+ return separatorIndex === -1 ? "." : path.slice(0, separatorIndex) || "/";
12
+ };
13
+ const normalizeContainerImage = (image) => {
14
+ if (typeof image !== "string") {
15
+ if ("build" in image) {
16
+ const buildDirectory = image.build.endsWith("/") ? image.build.slice(0, -1) : image.build;
17
+ return { buildDir: buildDirectory, kind: "build" };
18
+ }
19
+ return { kind: "registry", reference: image.registry };
20
+ }
21
+ if (basename(image).startsWith("Dockerfile")) {
22
+ return { buildContext: dirname(image), dockerfilePath: image, kind: "dockerfile" };
23
+ }
24
+ const context = image.endsWith("/") ? image.slice(0, -1) : image;
25
+ return { buildContext: context, dockerfilePath: `${context}/Dockerfile`, kind: "dockerfile" };
26
+ };
27
+ const containerClassName = (exportName) => `${exportName.charAt(0).toUpperCase()}${exportName.slice(1)}Container`;
28
+ const containerBindingName = (exportName) => `CONTAINER_${exportName.replaceAll(/(?<=[a-z0-9])(?=[A-Z])/g, "_").toUpperCase()}`;
29
+ const containerBuildTag = (exportName) => `lunora-${exportName.replaceAll(/(?<=[a-z0-9])(?=[A-Z])/g, "-").toLowerCase()}:build`;
30
+ const assertValidImage = (image) => {
31
+ if (typeof image === "string") {
32
+ if (image.length === 0) {
33
+ throw new TypeError("defineContainer: `image` must be a non-empty path or a { registry } reference");
34
+ }
35
+ if (image.includes(":")) {
36
+ throw new TypeError(
37
+ `defineContainer: \`image\` string "${image}" looks like a registry reference — pass it as { registry: "${image}" } instead. Plain strings are local Dockerfile paths.`
38
+ );
39
+ }
40
+ return;
41
+ }
42
+ if ("build" in image) {
43
+ if (typeof image.build !== "string" || image.build.length === 0) {
44
+ throw new TypeError("defineContainer: `image.build` must be a non-empty source directory for Railpack to build");
45
+ }
46
+ return;
47
+ }
48
+ if (typeof image.registry !== "string" || image.registry.length === 0) {
49
+ throw new TypeError("defineContainer: `image.registry` must be a non-empty fully-qualified image reference");
50
+ }
51
+ };
52
+ const assertValidEnvAndSecrets = (config) => {
53
+ for (const name of Object.keys(config.env ?? {})) {
54
+ if (!ENV_NAME_PATTERN.test(name)) {
55
+ throw new TypeError(`defineContainer: env variable name "${name}" is not a valid environment variable name`);
56
+ }
57
+ }
58
+ for (const name of Object.keys(config.buildArgs ?? {})) {
59
+ if (!ENV_NAME_PATTERN.test(name)) {
60
+ throw new TypeError(`defineContainer: buildArg name "${name}" is not a valid environment variable name`);
61
+ }
62
+ }
63
+ const envNames = new Set(Object.keys(config.env ?? {}));
64
+ for (const secret of config.secrets ?? []) {
65
+ if (!ENV_NAME_PATTERN.test(secret)) {
66
+ throw new TypeError(`defineContainer: secret name "${secret}" is not a valid environment variable name`);
67
+ }
68
+ if (envNames.has(secret)) {
69
+ throw new TypeError(
70
+ `defineContainer: "${secret}" is declared in both \`env\` and \`secrets\` — a secret would silently overwrite the static env value; pick one`
71
+ );
72
+ }
73
+ }
74
+ };
75
+ const defineContainer = (config) => {
76
+ assertValidImage(config.image);
77
+ if (config.defaultPort !== void 0 && (!Number.isInteger(config.defaultPort) || config.defaultPort < 1 || config.defaultPort > 65535)) {
78
+ throw new TypeError(`defineContainer: \`defaultPort\` must be an integer in 1–65535 (got ${String(config.defaultPort)})`);
79
+ }
80
+ const stepPercentage = config.rollout?.stepPercentage;
81
+ if (stepPercentage !== void 0 && (!Number.isInteger(stepPercentage) || stepPercentage < 1 || stepPercentage > 100)) {
82
+ throw new TypeError(`defineContainer: \`rollout.stepPercentage\` must be an integer in 1–100 (got ${String(stepPercentage)})`);
83
+ }
84
+ if (config.maxInstances !== void 0 && (!Number.isInteger(config.maxInstances) || config.maxInstances < 1)) {
85
+ throw new TypeError(`defineContainer: \`maxInstances\` must be a positive integer (got ${String(config.maxInstances)})`);
86
+ }
87
+ if (typeof config.instanceType === "string" && !NAMED_INSTANCE_TYPES.has(config.instanceType)) {
88
+ throw new TypeError(
89
+ `defineContainer: unknown \`instanceType\` "${config.instanceType}" — use one of ${[...NAMED_INSTANCE_TYPES].join(", ")}, or a custom { vcpu, memoryMib, diskMb } object`
90
+ );
91
+ }
92
+ if (typeof config.sleepAfter === "string" && !SLEEP_AFTER_PATTERN.test(config.sleepAfter)) {
93
+ throw new TypeError(
94
+ `defineContainer: \`sleepAfter\` string "${config.sleepAfter}" must be a number of seconds followed by a unit, e.g. "30s", "5m", or "1h"`
95
+ );
96
+ }
97
+ assertValidEnvAndSecrets(config);
98
+ return { ...config, isLunoraContainer: true };
99
+ };
100
+ const isContainerDefinition = (value) => typeof value === "object" && value !== null && value.isLunoraContainer === true;
101
+ const resolveContainerEnvVariables = (definition, workerEnv, exportName) => {
102
+ const resolved = { ...definition.env };
103
+ for (const secret of definition.secrets ?? []) {
104
+ const value = workerEnv[secret];
105
+ if (typeof value !== "string") {
106
+ const label = exportName === void 0 ? "container" : `container "${exportName}"`;
107
+ throw new Error(
108
+ `${label}: declared secret "${secret}" is not set on the Worker environment. Add it to .dev.vars for local dev and run \`wrangler secret put ${secret}\` for production.`
109
+ );
110
+ }
111
+ resolved[secret] = value;
112
+ }
113
+ return resolved;
114
+ };
115
+
116
+ export { containerBindingName, containerBuildTag, containerClassName, defineContainer, isContainerDefinition, normalizeContainerImage, resolveContainerEnvVariables as resolveContainerEnvVars };
@@ -0,0 +1,122 @@
1
+ const DEFAULT_POOL_SIZE = 3;
2
+ const DEFAULT_MAX_BACKOFF_MS = 3e4;
3
+ const toRequest = (input, init) => {
4
+ if (typeof input === "string" && input.startsWith("/")) {
5
+ return new Request(`http://container${input}`, init);
6
+ }
7
+ return new Request(input, init);
8
+ };
9
+ const handleFor = (namespace, instanceName) => {
10
+ return {
11
+ fetch: async (input, init) => namespace.get(namespace.idFromName(instanceName)).fetch(toRequest(input, init))
12
+ };
13
+ };
14
+ const lifecycleCall = async (stub, method, binding, argument) => {
15
+ const rpc = stub[method];
16
+ if (typeof rpc !== "function") {
17
+ throw new TypeError(`ctx.containers: the "${binding}" container DO does not expose ${method}() — is @lunora/container/do up to date?`);
18
+ }
19
+ return rpc(argument);
20
+ };
21
+ const instanceHandleFor = (namespace, spec, instanceName) => {
22
+ const stub = () => namespace.get(namespace.idFromName(instanceName));
23
+ return {
24
+ destroy: async () => lifecycleCall(stub(), "destroy", spec.binding),
25
+ fetch: async (input, init) => stub().fetch(toRequest(input, init)),
26
+ getState: async () => lifecycleCall(stub(), "getState", spec.binding),
27
+ start: async (options) => lifecycleCall(stub(), "start", spec.binding, options),
28
+ stop: async (signal) => lifecycleCall(stub(), "stop", spec.binding, signal)
29
+ };
30
+ };
31
+ const randomPoolName = (size) => (
32
+ // eslint-disable-next-line sonarjs/pseudo-random -- load-balancing pick across interchangeable instances, not a security decision
33
+ `pool-${String(Math.floor(Math.random() * size))}`
34
+ );
35
+ const sleep = async (ms) => {
36
+ if (ms <= 0) {
37
+ return;
38
+ }
39
+ await new Promise((resolve) => {
40
+ setTimeout(resolve, ms);
41
+ });
42
+ };
43
+ const retryOnServerError = (response) => response.status >= 500;
44
+ const poolHandleFor = (namespace, spec, options = {}) => {
45
+ const size = options.size ?? spec.maxInstances ?? DEFAULT_POOL_SIZE;
46
+ const attempts = Math.max(1, options.attempts ?? 3);
47
+ const baseBackoff = options.backoffMs ?? 100;
48
+ const maxBackoff = options.maxBackoffMs ?? DEFAULT_MAX_BACKOFF_MS;
49
+ const shouldRetry = options.retryOn ?? retryOnServerError;
50
+ return {
51
+ fetch: async (input, init) => {
52
+ let lastError;
53
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
54
+ if (attempt > 0) {
55
+ await sleep(Math.min(baseBackoff * 2 ** (attempt - 1), maxBackoff));
56
+ }
57
+ const request = toRequest(input, init);
58
+ try {
59
+ const response = await namespace.get(namespace.idFromName(randomPoolName(size))).fetch(request);
60
+ if (attempt === attempts - 1 || !shouldRetry(response)) {
61
+ return response;
62
+ }
63
+ } catch (error) {
64
+ lastError = error;
65
+ }
66
+ }
67
+ throw lastError instanceof Error ? lastError : new Error(`ctx.containers.${spec.exportName}.pool(): all ${String(attempts)} attempts failed`);
68
+ }
69
+ };
70
+ };
71
+ const accessorFor = (namespace, spec) => {
72
+ return {
73
+ any: (count) => handleFor(namespace, randomPoolName(count ?? spec.maxInstances ?? DEFAULT_POOL_SIZE)),
74
+ get: (name) => instanceHandleFor(namespace, spec, name),
75
+ pool: (options) => poolHandleFor(namespace, spec, options)
76
+ };
77
+ };
78
+ const missingBindingAccessor = (spec) => {
79
+ const fail = () => {
80
+ throw new Error(
81
+ `ctx.containers.${spec.exportName}: no "${spec.binding}" Durable Object binding found. Run \`lunora dev\` (or \`lunora deploy\`) to reconcile wrangler.jsonc, and make sure the worker entry re-exports the generated container classes.`
82
+ );
83
+ };
84
+ return { any: fail, get: fail, pool: fail };
85
+ };
86
+ const createContainerContext = (env, specs) => {
87
+ const containers = {};
88
+ for (const spec of specs) {
89
+ const namespace = env[spec.binding];
90
+ containers[spec.exportName] = namespace && typeof namespace.idFromName === "function" && typeof namespace.get === "function" ? accessorFor(namespace, spec) : missingBindingAccessor(spec);
91
+ }
92
+ return containers;
93
+ };
94
+ const createContainerTestContext = (handlers) => {
95
+ const containers = {};
96
+ for (const [exportName, handler] of Object.entries(handlers)) {
97
+ const testHandleFor = (instanceName) => {
98
+ return {
99
+ fetch: async (input, init) => handler(toRequest(input, init), { name: instanceName })
100
+ };
101
+ };
102
+ const testInstanceHandleFor = (instanceName) => {
103
+ return {
104
+ ...testHandleFor(instanceName),
105
+ destroy: () => Promise.resolve(),
106
+ getState: () => Promise.resolve({ lastChange: 0 }),
107
+ start: () => Promise.resolve(),
108
+ stop: () => Promise.resolve()
109
+ };
110
+ };
111
+ containers[exportName] = {
112
+ any: () => testHandleFor("pool-0"),
113
+ get: (name) => testInstanceHandleFor(name),
114
+ // The double doesn't simulate failure/retry — pool() just routes to
115
+ // the handler like any other call, so tests stay deterministic.
116
+ pool: () => testHandleFor("pool-0")
117
+ };
118
+ }
119
+ return containers;
120
+ };
121
+
122
+ export { createContainerContext, createContainerTestContext };
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Public configuration types for `@lunora/container`.
3
+ *
4
+ * Everything in this module is pure data — no Cloudflare runtime imports — so
5
+ * it is safe to import from Node tooling (codegen, the config layer) as well
6
+ * as from worker code.
7
+ */
8
+ /** Named instance types Cloudflare Containers provides. */
9
+ type NamedContainerInstanceType = "basic" | "lite" | "standard-1" | "standard-2" | "standard-3" | "standard-4";
10
+ /**
11
+ * A custom instance type. Cloudflare's bounds at the time of writing: up to
12
+ * 4 vCPU, 12 GiB memory, 20 GB disk, ≥ 3 GiB memory per vCPU and ≤ 2 GB disk
13
+ * per GiB memory. The config-layer validator enforces the documented ranges.
14
+ */
15
+ interface CustomContainerInstanceType {
16
+ /** Disk in MB. Cloudflare's default is 2000 (2 GB). */
17
+ diskMb?: number;
18
+ /** Memory in MiB. Cloudflare's default is 256. */
19
+ memoryMib?: number;
20
+ /** vCPU count. Cloudflare's default is 0.0625 (1/16 vCPU). */
21
+ vcpu?: number;
22
+ }
23
+ type ContainerInstanceType = CustomContainerInstanceType | NamedContainerInstanceType;
24
+ /** Rolling-deploy tuning for a container. */
25
+ interface ContainerRollout {
26
+ /** Seconds an active instance runs before it's eligible for update (wrangler `rollout_active_grace_period`). */
27
+ gracePeriodSeconds?: number;
28
+ /** Percentage of instances updated per rollout step, 1–100 (wrangler `rollout_step_percentage`). */
29
+ stepPercentage?: number;
30
+ }
31
+ /**
32
+ * A pre-built image pulled from a registry — the Cloudflare Registry, Docker
33
+ * Hub, or Amazon ECR (the registries `wrangler deploy` supports). The
34
+ * reference must be fully qualified, e.g. `docker.io/acme/transcoder:1.4`.
35
+ */
36
+ interface RegistryImageSource {
37
+ registry: string;
38
+ }
39
+ /**
40
+ * A Dockerfile-less build via [Railpack](https://railpack.com): point at a
41
+ * source directory and `lunora deploy` builds an OCI image with Railpack
42
+ * (needs a BuildKit instance) and pushes it to the Cloudflare Registry before
43
+ * wrangler runs. Opt-in — the Dockerfile path is the zero-extra-deps default.
44
+ */
45
+ interface BuildImageSource {
46
+ build: string;
47
+ }
48
+ /**
49
+ * Where the container image comes from. A `string` is a **local path** —
50
+ * either a directory containing a `Dockerfile` (normalized to
51
+ * `&lt;dir>/Dockerfile` with the directory as the build context) or a path to
52
+ * the Dockerfile itself — while `{ registry }` is a pre-built image reference.
53
+ */
54
+ type ContainerImageSource = BuildImageSource | RegistryImageSource | string;
55
+ interface ContainerConfig {
56
+ /**
57
+ * Build-time variables for a Dockerfile/Railpack image — wrangler's
58
+ * `image_vars` (equivalent to `docker build --build-arg`). For *runtime*
59
+ * values use {@link ContainerConfig.env} / {@link ContainerConfig.secrets}.
60
+ * Ignored for a pre-built `{ registry }` image.
61
+ */
62
+ buildArgs?: Readonly<Record<string, string>>;
63
+ /**
64
+ * The port the container listens on. Worker → container requests target
65
+ * this port. Locally the Dockerfile must also `EXPOSE` it.
66
+ */
67
+ defaultPort?: number;
68
+ /**
69
+ * Whether the container may open outbound internet connections. Defaults
70
+ * to `true` — the platform default. Note that container egress is billed
71
+ * per GB by Cloudflare.
72
+ */
73
+ enableInternet?: boolean;
74
+ /**
75
+ * Static environment variables passed to the container on every start.
76
+ * For secret values use {@link ContainerConfig.secrets} instead so they
77
+ * flow through Worker Secrets rather than source code.
78
+ */
79
+ env?: Readonly<Record<string, string>>;
80
+ /** Image source — a local Dockerfile path/directory or a registry reference. */
81
+ image: ContainerImageSource;
82
+ /**
83
+ * Resource class for each instance: a named Cloudflare instance type or a
84
+ * custom `{ vcpu, memoryMib, diskMb }` object.
85
+ */
86
+ instanceType?: ContainerInstanceType;
87
+ /**
88
+ * Maximum number of concurrently *running* instances. Stopped (slept)
89
+ * containers don't count. Also the default pool size for `.any()`.
90
+ */
91
+ maxInstances?: number;
92
+ /**
93
+ * Override for the wrangler `containers[].name` identifier. Defaults to
94
+ * wrangler's own default (worker name + class name + environment).
95
+ */
96
+ name?: string;
97
+ /**
98
+ * Rolling-deploy tuning. `stepPercentage` is the share of instances updated
99
+ * per rollout step (wrangler `rollout_step_percentage`); `gracePeriodSeconds`
100
+ * is how long an active instance is left running before it's eligible for
101
+ * update (wrangler `rollout_active_grace_period`).
102
+ */
103
+ rollout?: ContainerRollout;
104
+ /**
105
+ * Names of Worker secrets (from `wrangler secret` / `.dev.vars`) forwarded
106
+ * into the container's environment at instance start. Each declared name
107
+ * must exist on the Worker `env` — a missing one fails fast with a
108
+ * directed error instead of starting the container without it.
109
+ */
110
+ secrets?: ReadonlyArray<string>;
111
+ /**
112
+ * Idle timeout after which the instance is put to sleep, e.g. `"5m"`,
113
+ * `"30s"`, or a number of seconds. Cloudflare's default is `"10m"`.
114
+ */
115
+ sleepAfter?: number | string;
116
+ }
117
+ /**
118
+ * The value `defineContainer` returns: the validated config plus a brand the
119
+ * codegen discovery and the generated Container DO class key on.
120
+ */
121
+ interface ContainerDefinition extends ContainerConfig {
122
+ /** Brand marking a value as a Lunora container definition. */
123
+ readonly isLunoraContainer: true;
124
+ }
125
+ /** A normalized image source, as written into `wrangler.jsonc`. */
126
+ type NormalizedContainerImage = {
127
+ /** Build context directory (wrangler `image_build_context`). */
128
+ buildContext: string;
129
+ /** Path to the Dockerfile (wrangler `image`). */
130
+ dockerfilePath: string;
131
+ kind: "dockerfile";
132
+ } | {
133
+ /** Railpack source directory built + pushed at deploy time. */
134
+ buildDir: string;
135
+ kind: "build";
136
+ } | {
137
+ kind: "registry"; /** Fully-qualified image reference (wrangler `image`). */
138
+ reference: string;
139
+ };
140
+ export { BuildImageSource as B, ContainerDefinition as C, NormalizedContainerImage as N, RegistryImageSource as R, ContainerConfig as a, ContainerImageSource as b, ContainerInstanceType as c, ContainerRollout as d, CustomContainerInstanceType as e, NamedContainerInstanceType as f };