@lunora/container 1.0.0-alpha.1 → 1.0.0-alpha.100

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 (37) hide show
  1. package/LICENSE.md +26 -0
  2. package/README.md +118 -3
  3. package/__assets__/package-og.svg +1 -1
  4. package/dist/bridge.d.mts +60 -35
  5. package/dist/bridge.d.ts +60 -35
  6. package/dist/bridge.mjs +1 -76
  7. package/dist/do/index.d.mts +870 -32
  8. package/dist/do/index.d.ts +870 -32
  9. package/dist/do/index.mjs +1 -119
  10. package/dist/index.d.mts +634 -109
  11. package/dist/index.d.ts +634 -109
  12. package/dist/index.mjs +1 -2
  13. package/dist/otel.d.mts +217 -0
  14. package/dist/otel.d.ts +217 -0
  15. package/dist/otel.mjs +1 -0
  16. package/dist/packem_shared/CONTAINER_EXEC_PATH-7Hcd6-zs.mjs +1 -0
  17. package/dist/packem_shared/CUSTOM_INSTANCE_MIN_MEMORY_MIB_PER_VCPU-D8Hy9Rxv.mjs +1 -0
  18. package/dist/packem_shared/ContainerProxy-CfXjS6ZI.mjs +27 -0
  19. package/dist/packem_shared/DURABLE_OBJECT_POLICY_FORBIDDEN_KEYS-gYi8RBUO.mjs +1 -0
  20. package/dist/packem_shared/LunoraSandboxContainer-DPnhJUu1.mjs +1 -0
  21. package/dist/packem_shared/abort-deadline-jwbW9Ala.mjs +1 -0
  22. package/dist/packem_shared/createContainerContext-p6QxWjsm.mjs +1 -0
  23. package/dist/packem_shared/exec-BYLW70sR.mjs +1 -0
  24. package/dist/packem_shared/jurisdiction-DtE9s70w.mjs +1 -0
  25. package/dist/packem_shared/jurisdiction.d-BwpauCoI.d.mts +471 -0
  26. package/dist/packem_shared/jurisdiction.d-BwpauCoI.d.ts +471 -0
  27. package/dist/packem_shared/sampling-D21rpu_s.mjs +1 -0
  28. package/dist/packem_shared/sandbox-types.d-DXlHnsEg.d.mts +110 -0
  29. package/dist/packem_shared/sandbox-types.d-DXlHnsEg.d.ts +110 -0
  30. package/dist/sandbox/index.d.mts +66 -0
  31. package/dist/sandbox/index.d.ts +66 -0
  32. package/dist/sandbox/index.mjs +1 -0
  33. package/package.json +13 -3
  34. package/dist/packem_shared/containerBindingName-BGdSdFNA.mjs +0 -116
  35. package/dist/packem_shared/createContainerContext-ChDD53ys.mjs +0 -122
  36. package/dist/packem_shared/types.d-D2l2SYol.d.mts +0 -140
  37. package/dist/packem_shared/types.d-D2l2SYol.d.ts +0 -140
package/LICENSE.md CHANGED
@@ -103,3 +103,29 @@ Unless required by applicable law or agreed to in writing, software distributed
103
103
  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
104
104
  CONDITIONS OF ANY KIND, either express or implied. See the License for the
105
105
  specific language governing permissions and limitations under the License.
106
+
107
+ <!-- DEPENDENCIES -->
108
+
109
+ # Licenses of bundled dependencies
110
+ The published @lunora/container artifact additionally contains code with the following licenses:
111
+ MIT OR Apache-2.0
112
+
113
+ # Bundled dependencies:
114
+ ## @cloudflare/containers
115
+ License: MIT OR Apache-2.0
116
+ Repository: git+https://github.com/cloudflare/containers.git
117
+
118
+ <!-- /DEPENDENCIES -->
119
+
120
+ <!-- TYPE_DEPENDENCIES -->
121
+
122
+ # Licenses of bundled types
123
+ The published @lunora/container artifact additionally contains code with the following licenses:
124
+ MIT OR Apache-2.0
125
+
126
+ # Bundled types:
127
+ ## @cloudflare/containers
128
+ License: MIT OR Apache-2.0
129
+ Repository: git+https://github.com/cloudflare/containers.git
130
+
131
+ <!-- /TYPE_DEPENDENCIES -->
package/README.md CHANGED
@@ -10,6 +10,8 @@
10
10
 
11
11
  <!-- END_PACKAGE_OG_IMAGE_PLACEHOLDER -->
12
12
 
13
+ > **Experimental** — this package is outside the Lunora 1.0 stability promise: its API may change in any release, without a major version bump.
14
+
13
15
  <br />
14
16
 
15
17
  <div align="center">
@@ -66,6 +68,7 @@ export const transcoder = defineContainer({
66
68
  maxInstances: 5,
67
69
  sleepAfter: "5m",
68
70
  secrets: ["TRANSCODER_API_KEY"], // forwarded from Worker secrets / .dev.vars
71
+ labels: { team: "media" }, // metadata attached to every instance for metrics/observability
69
72
  });
70
73
  ```
71
74
 
@@ -89,10 +92,112 @@ export const transcode = action.input({ videoId: v.id("videos") }).action(async
89
92
  });
90
93
  ```
91
94
 
92
- `ctx.containers` is action-only (container calls are external I/O, like `ctx.fetch`); `.get(name)` handles also expose `start`/`stop`/`destroy`/`getState` lifecycle control.
95
+ `.get()` and `.any()` retry the **same** instance through a cold start — when a request lands while Cloudflare is still provisioning (a `503` "no instance", `500` "Failed to start", `429`, or "not listening"), they back off and retry (default 3 attempts) so the provisioning race never reaches your handler. Genuine app `5xx`s pass straight through. Tune or disable per call with `.get(id, { attempts, backoffMs })` (a pre-built `Request` is sent once and not retried, since its body may not be replayable).
96
+
97
+ `ctx.containers` is action-only (container calls are external I/O, like `ctx.fetch`); `.get(name)` handles also expose `start`/`stop`/`destroy`/`getState` lifecycle control plus `renewActivityTimeout()` and `egress.*` (adjust the allow/deny lists at runtime). HTTP requests and WebSocket frames already keep a busy container awake automatically; `renewActivityTimeout()` is the escape hatch for non-HTTP/non-WS activity.
93
98
 
94
99
  The config layer (`lunora dev` / `lunora deploy`) reconciles the wrangler `containers[]` entry, the `CONTAINER_*` Durable Object binding, and the SQLite-class migration automatically; `wrangler deploy` builds the Dockerfile with local Docker and pushes it to the Cloudflare Registry.
95
100
 
101
+ ### Multi-port containers
102
+
103
+ Declare every port the container must be listening on with `requiredPorts` (start-up waits for all of them); `defaultPort` is the target when a request doesn't pick one. Route a single request to another port with `.port(n)` — it composes with `.get()`, `.any()`, and `.pool()`:
104
+
105
+ ```ts
106
+ export const app = defineContainer({
107
+ image: "./containers/app",
108
+ defaultPort: 8080,
109
+ requiredPorts: [8080, 9090], // app + admin
110
+ });
111
+
112
+ // in an action:
113
+ await ctx.containers.app.get(tenantId).fetch("/work"); // → 8080
114
+ await ctx.containers.app.get(tenantId).port(9090).fetch("/admin"); // → 9090
115
+ ```
116
+
117
+ ### Build-time args
118
+
119
+ `env` and `secrets` are runtime values; for build-time `docker build --build-arg` values (wrangler `image_vars`, exposed to the Dockerfile as `ARG`) use `buildArgs`. They apply only to an image Lunora builds and are ignored for a pre-built `{ registry }` image.
120
+
121
+ ```ts
122
+ export const worker = defineContainer({
123
+ image: "./containers/worker",
124
+ buildArgs: { NODE_VERSION: "22", BUILD_TARGET: "production" },
125
+ });
126
+ ```
127
+
128
+ ### Secrets and Secrets Store
129
+
130
+ `secrets` forwards plain Worker secrets into the container env; `secretsStore` maps a _container env-var name → Cloudflare [Secrets Store](https://developers.cloudflare.com/secrets-store/) binding name_ and resolves each with its async `.get()` at first start (memoised). A collision with `env`/`secrets` is rejected at authoring time; a missing binding fails the start — the same fail-closed stance as `secrets`. A per-instance `start({ envVars })` replaces the env set wholesale (and skips Secrets Store resolution entirely), and is persisted for that instance: later starts, including implicit restarts, reuse it until `destroy()`. The override cannot change while the container is running or still starting: a differing `start({ envVars })` is rejected with `CONFLICT` in either case, so `stop()` it (and let any start in flight finish) first.
131
+
132
+ ```ts
133
+ export const worker = defineContainer({
134
+ image: "./containers/worker",
135
+ secrets: ["TRANSCODER_API_KEY"], // plain Worker secret → same-named env var
136
+ secretsStore: { STRIPE_KEY: "STRIPE_SECRET" }, // env.STRIPE_SECRET.get() → STRIPE_KEY
137
+ });
138
+ ```
139
+
140
+ ### Egress firewall
141
+
142
+ Pair `enableInternet: false` with an `allowedHosts` allow-list (or layer a `deniedHosts` deny-list that overrides everything) to constrain a container's outbound traffic; `interceptHttps: true` extends the lists to TLS connections (the image must trust the Cloudflare CA). Codegen re-exports the `ContainerProxy` worker entrypoint the interception path needs automatically.
143
+
144
+ ```ts
145
+ export const fetcher = defineContainer({
146
+ image: "./containers/fetcher",
147
+ enableInternet: false,
148
+ allowedHosts: ["*.stripe.com", "api.github.com"],
149
+ deniedHosts: ["*.evil.com"],
150
+ });
151
+
152
+ // tighten or relax one running instance at runtime:
153
+ await ctx.containers.fetcher.get(tenantId).egress.allow("hooks.slack.com");
154
+ ```
155
+
156
+ For advanced egress rewriting in worker code, `@lunora/container/do` re-exports Cloudflare's custom outbound-handler types (`OutboundHandler`, `OutboundHandlers`, `outboundParams`) — wire them onto a hand-authored `LunoraContainer` subclass to inject auth, route, or mock a container's outbound calls.
157
+
158
+ ### Readiness gating
159
+
160
+ The platform health check waits for an open port, not necessarily a _ready_ app. `readyOn` adds application-level probes that gate request proxying: a `ctx.containers.<name>` fetch holds until every probe responds with its expected status, so callers never hit a container still applying migrations or warming caches. Probes are declarative data (path + optional `port`/`status`), run in parallel at start, and probe the container's TCP port directly.
161
+
162
+ ```ts
163
+ export const api = defineContainer({
164
+ image: "./containers/api",
165
+ defaultPort: 8080,
166
+ readyOn: [
167
+ { path: "/ready" }, // expect 200 on defaultPort
168
+ { path: "/live", port: 9090, status: 204 }, // own port + expected status
169
+ ],
170
+ });
171
+ ```
172
+
173
+ ### Hard timeout
174
+
175
+ `sleepAfter` caps _idle_ time; `hardTimeout` caps _total_ lifetime — a runaway-cost backstop measured from start, regardless of activity (same grammar as `sleepAfter`). When it elapses the generated class's `onHardTimeoutExpired` hook runs (default: `stop()`, i.e. SIGTERM with no escalation — a container that ignores SIGTERM outlives its cap unless you override the hook and follow up with `destroy()`); the timer is run-generation-stamped so a stale timer from a slept/crashed run can't kill a fresh one.
176
+
177
+ ```ts
178
+ export const job = defineContainer({
179
+ image: "./containers/job",
180
+ hardTimeout: "1h", // stopped after an hour, busy or not
181
+ });
182
+ ```
183
+
184
+ ### Per-instance images and snapshots
185
+
186
+ `schedulingPolicy: "durable_object"` (Cloudflare public beta) lets each instance pick its image and size at start, and save and restore its filesystem:
187
+
188
+ ```ts
189
+ export const agentComputer = defineContainer({
190
+ schedulingPolicy: "durable_object",
191
+ images: { base: "./containers/base" },
192
+ image: "base",
193
+ });
194
+
195
+ const computer = ctx.containers.agentComputer.get(taskId);
196
+ await computer.start({ image: "base", instanceType: "standard-2" });
197
+ const saved = await computer.snapshot();
198
+ await ctx.containers.agentComputer.get(otherId).start({ snapshot: saved });
199
+ ```
200
+
96
201
  ### Calling Lunora from inside a container
97
202
 
98
203
  Container code calls back into your app's functions with the bridge client (any JS runtime), over the Worker's HTTP RPC endpoint:
@@ -108,7 +213,7 @@ await lunora.mutation("jobs:markDone", { id: pending[0].id });
108
213
 
109
214
  The token is a bearer your Worker's `resolveIdentity` recognizes — pass it to the container as a `secret`. Non-JS containers can `POST /_lunora/rpc` with `{ functionPath, args }` directly.
110
215
 
111
- Secure the bridge in `resolveIdentity`: read `request.headers.get("authorization")`, strip the `Bearer ` prefix, and compare the token against a Worker secret (e.g. `env.LUNORA_CONTAINER_TOKEN`) you also forward to the container. Return a `{ userId }` identity only on a match and `null` otherwise — an unrecognised request then runs anonymously and is rejected by your functions' own authorization checks. See [Securing the bridge](https://lunora.sh/docs/addons/containers#securing-the-bridge) for the full example.
216
+ Secure the bridge in `resolveIdentity`: read `request.headers.get("authorization")`, strip the `Bearer ` prefix, and compare the token against a Worker secret (e.g. `env.LUNORA_CONTAINER_TOKEN`) you also forward to the container. Return a `{ userId }` identity only on a match and `null` otherwise — an unrecognised request then runs anonymously and is rejected by your functions' own authorization checks. See [Securing the bridge](https://lunora.sh/docs/packages/container#securing-the-bridge) for the full example.
112
217
 
113
218
  ### Entry points
114
219
 
@@ -116,7 +221,17 @@ Secure the bridge in `resolveIdentity`: read `request.headers.get("authorization
116
221
  - `@lunora/container/do` — workerd-only: the `LunoraContainer` base class the generated DO classes extend (pulls in `@cloudflare/containers`).
117
222
  - `@lunora/container/bridge` — runtime-agnostic: `createContainerBridge` for calling Lunora functions from inside a container.
118
223
 
119
- > This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/addons/containers)**.
224
+ > This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/packages/container)**.
225
+
226
+ ### Known platform limitations
227
+
228
+ Some constraints live in Cloudflare Containers itself (open issues on [`cloudflare/containers`](https://github.com/cloudflare/containers/issues)). Lunora papers over what it can — cold-start retry and WebSocket keep-alive — and surfaces the rest:
229
+
230
+ - **No autoscaling / location-aware routing** — pools are fixed-size and pick uniformly at random ([#226](https://github.com/cloudflare/containers/issues/226)).
231
+ - **Ephemeral disk; no FUSE / tmpfs / some `node:net` modes** — persist to [`@lunora/storage`](https://www.npmjs.com/package/@lunora/storage) (R2) ([#112](https://github.com/cloudflare/containers/issues/112), [#160](https://github.com/cloudflare/containers/issues/160), [#67](https://github.com/cloudflare/containers/issues/67)).
232
+ - **Egress interception is HTTP-first** — HTTPS needs `interceptHttps`; raw gRPC isn't interceptable yet ([#195](https://github.com/cloudflare/containers/issues/195)).
233
+ - **Long jobs can be terminated on rollout** — use `hardTimeout` and make work resumable ([#138](https://github.com/cloudflare/containers/issues/138)).
234
+ - **Local dev can't pull from the Cloudflare Registry** — build from a local Dockerfile ([#155](https://github.com/cloudflare/containers/issues/155)).
120
235
 
121
236
  ## Related
122
237