1tube 0.1.10 → 0.1.11
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.
- package/README.md +376 -2
- package/deno.json +9 -4
- package/deno.lock +359 -1
- package/package.json +1 -1
- package/src/backends/workerd/backend.ts +1808 -0
- package/src/backends/workerd/bundler.ts +618 -0
- package/src/backends/workerd/capnp.ts +375 -0
- package/src/backends/workerd/hot-reloader.ts +330 -0
- package/src/backends/workerd/prebuilt.ts +153 -0
- package/src/backends/workerd/proc-stats.ts +147 -0
- package/src/backends/workerd/process.ts +495 -0
- package/src/backends/workerd/shared-runtime.ts +97 -0
- package/src/backends/workerd/watchdog.ts +282 -0
- package/src/cli/build.ts +449 -0
- package/src/cli/envelope.ts +271 -0
- package/src/cli/package.ts +547 -0
- package/src/cli.ts +51 -0
- package/src/gateway/cors.ts +57 -11
- package/src/gateway/logging.ts +104 -1
- package/src/gateway/rate-limit.ts +15 -0
- package/src/health.ts +49 -3
- package/src/registry.ts +42 -16
- package/src/server.ts +702 -40
package/README.md
CHANGED
|
@@ -58,8 +58,371 @@ All knobs default to safe-but-backwards-compatible values. The TS gateway (`src/
|
|
|
58
58
|
| `1TUBE_TRUSTED_PROXIES` | empty | Comma-separated list of remote IPs whose `X-Forwarded-For` is honored. Anything else uses the raw socket address — XFF spoofing no longer mints fresh rate-limit buckets. |
|
|
59
59
|
| `1TUBE_SHUTDOWN_GRACE_MS` | `10000` | SIGINT/SIGTERM drain budget. |
|
|
60
60
|
| `INTERNAL_KEY` | unset | Required to read detailed `/health` and `/metrics`. Header-only: `Authorization: Bearer $INTERNAL_KEY`. |
|
|
61
|
+
| `--backend` / `1TUBE_BACKEND` | `deno` | Function execution engine. `deno` (default) imports each function as a module in this process — same as 1tube has always done. `workerd` bundles each function and serves it from a Cloudflare-style workerd subprocess for hard isolation. See [Workerd backend](#workerd-backend) below. |
|
|
62
|
+
| `--workerd-bin` / `1TUBE_WORKERD_BIN` | `workerd` (PATH) | Path to the workerd binary. Only consulted with `--backend workerd`. |
|
|
63
|
+
| `--workerd-env` / `1TUBE_WORKERD_ENV` | unset (= forward all) | Comma-separated allowlist of env var names to forward to bundled functions under `--backend workerd`. Each name reaches the bundle via `Deno.env.get(name)` and the worker's `env` binding. Values stay in the gateway's process env — they are NEVER written into `config.capnp`. **When unset (or `*`), every env var the gateway can see is forwarded** — bundled functions inherit their parent's environment like a regular `deno run` child. Pass an explicit list to narrow the surface for shared/multi-tenant workerds. |
|
|
64
|
+
| `--kill-stale-workerd` / `1TUBE_KILL_STALE_WORKERD` | off | When the boot-time port preflight finds one of workerd's socket ports already busy, automatically run `taskkill /F /IM workerd.exe` (Windows) or `pkill -9 -x workerd` (Unix), wait briefly, and re-probe. Only ever targets processes named `workerd` — non-workerd processes holding the port still surface the normal preflight error. Off by default; recommended for dev where leftover workerds from prior runs are the common cause of flaky boots. |
|
|
65
|
+
| `--inspector` / `--inspector-addr=ADDR` / `1TUBE_INSPECTOR` | off | Launch workerd with the V8 inspector bound to `ADDR` (default `127.0.0.1:9229` when `--inspector` has no value). Lets Chrome DevTools / Node-style debuggers attach for breakpoints inside bundled functions. **Local dev only** — opens an unauthenticated debug port. See [V8 inspector / debugger access](#v8-inspector--debugger-access) below. |
|
|
66
|
+
| `1TUBE_SHUTDOWN_GRACE_MS` | `10000` | Total wall-clock budget (ms) for shutdown, split between (1) draining gateway in-flight requests and (2) tearing down the workerd subprocess. See [Graceful shutdown](#graceful-shutdown) below. |
|
|
67
|
+
| `1TUBE_DEFAULT_RPM` | `120` | Override the gateway-wide default rate limit (requests per minute, per IP, per function). Per-function `1tube.json#rpm` still wins. |
|
|
68
|
+
| `1TUBE_DISABLE_RATE_LIMIT` | unset | Set to `1` to bypass rate limiting entirely. **Load-test / dev only** — production deployments must keep the limiter on. The gateway prints a clear warning at boot when this is enabled. |
|
|
61
69
|
```
|
|
62
70
|
|
|
71
|
+
## Workerd backend
|
|
72
|
+
|
|
73
|
+
1tube can optionally run each function inside a [workerd](https://github.com/cloudflare/workerd) subprocess instead of importing it directly into the gateway. This gives:
|
|
74
|
+
|
|
75
|
+
- **Hard isolation** between functions and the gateway — a runaway function cannot wedge the gateway, leak memory into peer functions, or read peer functions' globals.
|
|
76
|
+
- **Cloudflare-Workers-style runtime** — `nodejs_compat`, `process.env`, `node:fs`/`node:os`/`node:http`, `AsyncLocalStorage`, the modern `fetch`/`Streams`/`URL` spec fixes, etc.
|
|
77
|
+
- **Zero changes to function code** — the same `serve(handler, { public: ... })` shim works on both backends. `Deno.env.get(...)` still works (we shim it onto the `env` binding). The bundler resolves `npm:`, `jsr:`, `https:`, and `file:` specifiers via Deno's own loader.
|
|
78
|
+
|
|
79
|
+
Per-function manifests, the circuit breaker, and per-function timeouts apply on the workerd path too — see [Per-function manifests](#per-function-manifests-on-workerd) below. Live HMR (`--hmr`) also works: a save triggers a re-bundle of just the changed function, a *new* workerd is spawned on shifted ports, the gateway atomically swaps to it, and the old workerd is torn down — all without dropping in-flight requests. See [HMR on workerd](#hmr-on-workerd) below for details and limits.
|
|
80
|
+
|
|
81
|
+
### Run it
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Make sure workerd is on PATH, or set 1TUBE_WORKERD_BIN.
|
|
85
|
+
workerd --version
|
|
86
|
+
|
|
87
|
+
# Boot 1tube with the workerd backend pointed at the playground.
|
|
88
|
+
deno run --allow-all src/server.ts \
|
|
89
|
+
--backend workerd \
|
|
90
|
+
--functions ./playground \
|
|
91
|
+
--port 3100 \
|
|
92
|
+
--dev
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
You should see something like:
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
[1tube] mode=dev hmr=off lazy=off backend=workerd ...
|
|
99
|
+
[1tube] Bundling functions from: ./playground
|
|
100
|
+
[1tube] workerd backend ready (v1.20260415.0) · 3 function(s) in 740ms
|
|
101
|
+
[1tube] Functions: echo, hello, whoami
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Then verify end-to-end:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# Public hello
|
|
108
|
+
curl http://127.0.0.1:3100/functions/v1/hello
|
|
109
|
+
# → {"message":"hello, hello",...}
|
|
110
|
+
|
|
111
|
+
# Public echo with body + query + custom header
|
|
112
|
+
curl -X POST 'http://127.0.0.1:3100/functions/v1/echo?x=1' \
|
|
113
|
+
-H 'content-type: application/json' \
|
|
114
|
+
-H 'x-test: abc' \
|
|
115
|
+
-d '{"hello":"world"}'
|
|
116
|
+
|
|
117
|
+
# Unknown function fast-fails before auth/rate-limit
|
|
118
|
+
curl -i http://127.0.0.1:3100/functions/v1/no-such-fn
|
|
119
|
+
# → HTTP/1.1 404 Not Found
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The full e2e test (`tests/workerd-e2e.test.ts`) automates exactly the same checks against a real workerd subprocess; run `deno test --allow-all tests/workerd-e2e.test.ts` for one-shot verification.
|
|
123
|
+
|
|
124
|
+
### Compatibility date
|
|
125
|
+
|
|
126
|
+
Workerd applies a `compatibilityDate` to enable behaviour-change opt-ins. 1tube defaults to today's date, then **clamps it down** to whatever the installed workerd binary actually accepts — derived from the binary's version string (`1.YYYYMMDD.N`), since workerd refuses dates later than its build date. You only see a clamp warning if you explicitly pinned a date the binary can't honour.
|
|
127
|
+
|
|
128
|
+
### Forwarding env vars (secrets, API keys)
|
|
129
|
+
|
|
130
|
+
**Default: every env var the gateway sees is forwarded.** Bundled functions inherit their parent's environment the same way a `deno run ./functions/foo/index.ts` child would — so `Deno.env.get("OPENAI_API_KEY")` Just Works without any extra wiring. This is the developer-friendly default; use it for solo projects and trusted code.
|
|
131
|
+
|
|
132
|
+
Pass an explicit allowlist when you care about isolation (shared multi-tenant workerds, reproducible build artifacts, etc.):
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
# CLI flag — narrows to exactly these names
|
|
136
|
+
deno run --allow-all src/server.ts \
|
|
137
|
+
--backend workerd --functions ./functions \
|
|
138
|
+
--workerd-env=OPENAI_API_KEY,POSTHOG_HOST,STRIPE_SECRET_KEY
|
|
139
|
+
|
|
140
|
+
# Env var (handy when 1tube is launched by a process manager)
|
|
141
|
+
1TUBE_WORKERD_ENV=OPENAI_API_KEY,POSTHOG_HOST,STRIPE_SECRET_KEY \
|
|
142
|
+
deno run --allow-all src/server.ts --backend workerd --functions ./functions
|
|
143
|
+
|
|
144
|
+
# `*` is the explicit form of the default (forward everything)
|
|
145
|
+
--workerd-env=*
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Each listed name becomes a `(name = X, fromEnvironment = X)` binding in the generated `config.capnp`. Workerd reads the **value** from its own process env at boot; the on-disk capnp file contains nothing but the var names. This means:
|
|
149
|
+
|
|
150
|
+
- Operators can rotate secrets by restarting the gateway with new env values; no cache rebuild needed.
|
|
151
|
+
- A leaked `config.capnp` cannot leak secrets — it never had them.
|
|
152
|
+
- Vars listed but absent from the gateway env get a single grouped warning at boot, then are simply not exposed (workerd would otherwise refuse to start).
|
|
153
|
+
|
|
154
|
+
> **A note on the default.** Earlier versions of 1tube defaulted to *deny-all* and required explicit opt-in. That was friction for the 90%-case (single-app deploys where the gateway and the functions trust each other) and surprised people whose `.env` already worked everywhere else. The deny-all stance is still one keystroke away — `--workerd-env=A,B,C` — and the per-function `permissions.env` manifest knob still narrows further under `1TUBE_ENFORCE_MANIFEST=1`.
|
|
155
|
+
|
|
156
|
+
### Bundle sizes at boot
|
|
157
|
+
|
|
158
|
+
Workerd boot prints a sorted table so unintended npm bloat is visible immediately:
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
[1tube] workerd bundle sizes (sorted, largest first):
|
|
162
|
+
zod-deps 443.4KB (612ms)
|
|
163
|
+
stream 12.0KB (89ms)
|
|
164
|
+
echo 9.9KB (74ms)
|
|
165
|
+
...
|
|
166
|
+
total 492.6KB (1131ms across 7 fns)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
If a bundle is much bigger than expected (50MB+), look for an unintended deep import — `nodejs_compat` shims most of `node:*` automatically and you rarely need to import a "browser polyfill" version of an npm package.
|
|
170
|
+
|
|
171
|
+
### Compatibility date
|
|
172
|
+
|
|
173
|
+
Workerd applies a `compatibilityDate` to enable behaviour-change opt-ins. 1tube defaults to today's date, then **clamps it down** to whatever the installed workerd binary actually accepts — derived from the binary's version string (`1.YYYYMMDD.N`), since workerd refuses dates later than its build date. You only see a clamp warning if you explicitly pinned a date the binary can't honour.
|
|
174
|
+
|
|
175
|
+
### Per-function manifests on workerd
|
|
176
|
+
|
|
177
|
+
Each `<function>/1tube.json` is loaded at workerd boot and applied the same way the Deno backend uses it:
|
|
178
|
+
|
|
179
|
+
- **`timeoutMs`** — per-function dispatch timeout. The gateway aborts the request and returns 504 if the bundled handler doesn't respond within this window. Falls back to `--timeout` when unset.
|
|
180
|
+
- **`rpm`** — per-function rate limit. The gateway-wide rate limiter consults the workerd manifest the same way it consults Deno-side ones; manifest values override `1TUBE_DEFAULT_RPM`.
|
|
181
|
+
- **`recycle`** — circuit breaker. After `errorWindow` requests with at least `errorRate` of them returning 5xx (or aborting), the breaker opens for `cooldownMs`. While open, requests short-circuit with `503 Retry-After: <seconds>` and a `{"reason": "circuit_breaker_open"}` body — workerd is never touched. The breaker half-opens automatically once the cool-down elapses.
|
|
182
|
+
- **`permissions.env`** — per-function env scoping. Default behaviour: every function sees the full `--workerd-env` allowlist (manifest entries are documentation only). Set `1TUBE_ENFORCE_MANIFEST=1` to switch to strict mode, where each function only sees the *intersection* of the gateway allowlist and its declared `permissions.env`. Use `["*"]` to opt back into the full surface under enforcement.
|
|
183
|
+
|
|
184
|
+
Functions without a `1tube.json` get the defaults from `defaultManifest()` — wide-open env, gateway-default timeout/rpm, errorRate=0.5 over a 20-request window with a 30s cool-down.
|
|
185
|
+
|
|
186
|
+
#### Example — tight breaker
|
|
187
|
+
|
|
188
|
+
```json
|
|
189
|
+
// playground/boom/1tube.json
|
|
190
|
+
{
|
|
191
|
+
"permissions": { "net": [], "env": [], "read": [], "write": [] },
|
|
192
|
+
"timeoutMs": 5000,
|
|
193
|
+
"recycle": { "errorRate": 1.0, "errorWindow": 3, "cooldownMs": 5000 }
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Three consecutive 500s trip the breaker; the gateway logs:
|
|
198
|
+
|
|
199
|
+
```
|
|
200
|
+
[1tube] Circuit breaker OPEN for "boom" (workerd) — refusing requests for 5000ms (errorRate >= 1).
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
The 4th request returns 503 with `Retry-After: 5` without ever reaching the workerd subprocess. Verified end-to-end in `tests/workerd-e2e.test.ts`.
|
|
204
|
+
|
|
205
|
+
### HMR on workerd
|
|
206
|
+
|
|
207
|
+
`--hmr` (or `1TUBE_HMR=1`) enables live reload on the workerd backend. The gateway watches the functions directory and on each save:
|
|
208
|
+
|
|
209
|
+
1. Classifies the changed path. A change inside a function dir (e.g. `playground/hello/index.ts` or `playground/hello/1tube.json`) only re-bundles that one function; a change to anything starting with `_` (`_shared/`, `_internal/`, etc.) triggers a full re-bundle since shared code may be imported by any function.
|
|
210
|
+
2. Coalesces a 200ms burst of save events (editor format-on-save typically writes the file 2–3 times within ~50ms).
|
|
211
|
+
3. Re-bundles the affected function(s) using the long-lived esbuild worker. Bundles for unchanged functions stay on disk.
|
|
212
|
+
4. Generates a fresh `config.gen-{0|1}.capnp` on shifted ports (alternating between `basePort` and `basePort+500`) so the new workerd never collides with the still-serving old one.
|
|
213
|
+
5. Spawns the new workerd, waits for TCP readiness, then atomically swaps the gateway's route table.
|
|
214
|
+
6. Stops the old workerd. In-flight requests against the old process keep their captured origin URL and complete naturally; only NEW requests go to the new workerd. There is no request-drop window.
|
|
215
|
+
|
|
216
|
+
A typical incremental swap completes in **200–800ms** on a warm cache (single-function rebundle + workerd cold start). Manifests (`1tube.json`) are re-read on every reload and propagated into the registry / supervisor before the new workerd starts serving, so an `rpm` or `timeoutMs` edit applies to the very first post-reload request.
|
|
217
|
+
|
|
218
|
+
Failures are non-fatal: a syntax error in your source code aborts the swap, the old workerd keeps serving, and the next save retries. The reloader logs:
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
[1tube] HMR 1 function(s) changed: hello
|
|
222
|
+
[1tube] HMR reload ok in 412ms (gen=3; rebundled=hello)
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
or, on failure:
|
|
226
|
+
|
|
227
|
+
```
|
|
228
|
+
[1tube] HMR reload FAILED — keeping previous workerd alive. Error: …
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
End-to-end coverage in `tests/workerd-e2e.test.ts` (`HMR rebundles + swaps without dropping requests`).
|
|
232
|
+
|
|
233
|
+
### Memory enforcement & crash recovery
|
|
234
|
+
|
|
235
|
+
**The honest version up front**: workerd's open-source binary does **not** support per-isolate memory limits in its capnp schema — that's a Cloudflare-platform feature, not something the OSS runtime exposes. So 1tube cannot kill an individual misbehaving function; it can only recycle the whole workerd process. Two mechanisms keep things stable:
|
|
236
|
+
|
|
237
|
+
1. **Memory watchdog** — periodically reads the workerd PID's resident set size (RSS) and recycles the entire process when it stays over budget for `N` consecutive samples.
|
|
238
|
+
2. **Crash auto-recovery** — if workerd dies unexpectedly (OOM, SIGKILL, segfault), the gateway re-bundles and respawns it automatically.
|
|
239
|
+
|
|
240
|
+
Both reuse the M4 dual-process swap, so recovery is non-disruptive: in-flight requests against the old workerd complete on their existing socket, the gateway atomically points new requests at the new process, and only then is the dead/over-budget process torn down.
|
|
241
|
+
|
|
242
|
+
#### Watchdog
|
|
243
|
+
|
|
244
|
+
Opt-in via `1TUBE_WORKERD_MAX_RSS_MB` (an absolute cap in MB). When unset, the gateway derives a recommended budget from `sum(manifest.memoryMB) × 1.5 + 64MB` and uses that automatically. If no manifest declares `memoryMB` either, the watchdog stays off — no surprises for existing deployments.
|
|
245
|
+
|
|
246
|
+
| Env var | Default | Notes |
|
|
247
|
+
|---|---|---|
|
|
248
|
+
| `1TUBE_WORKERD_MAX_RSS_MB` | unset | Hard RSS cap in MB. |
|
|
249
|
+
| `1TUBE_WORKERD_RSS_INTERVAL_MS` | `5000` | Poll interval. |
|
|
250
|
+
|
|
251
|
+
Hysteresis is fixed at **3 consecutive over-budget samples** before tripping (a single GC pause spike won't recycle), and a **10-second cooldown** runs after each recycle to let the new process settle. Watchdog stats (last RSS, configured budget, total recycles, current generation) are surfaced on the authenticated `/health` endpoint under a `workerd` block:
|
|
252
|
+
|
|
253
|
+
```json
|
|
254
|
+
{
|
|
255
|
+
"status": "ok",
|
|
256
|
+
"workerd": {
|
|
257
|
+
"pid": 1234,
|
|
258
|
+
"generation": 2,
|
|
259
|
+
"recycles": 1,
|
|
260
|
+
"rss_bytes": 134217728,
|
|
261
|
+
"budget_bytes": 268435456
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
RSS reading is cross-platform: Linux reads `/proc/{pid}/status`, macOS shells out to `ps`, Windows to `tasklist`. The numbers approximate (working set vs resident vs incl. shared pages differ by platform) — treat the budget as a rough cap, not a precise quota.
|
|
267
|
+
|
|
268
|
+
#### Crash recovery
|
|
269
|
+
|
|
270
|
+
`WorkerdProcess.onExit` is wired to trigger an automatic recycle. A bounded retry budget — **5 crashes in 30 seconds** — protects against hot-looping on a fundamentally broken bundle:
|
|
271
|
+
|
|
272
|
+
```
|
|
273
|
+
[1tube] workerd crashed (gen=2, code=137, crashes=1/5 in 30s) — auto-recycling...
|
|
274
|
+
[1tube] workerd crashed (gen=3, code=137, crashes=2/5 in 30s) — auto-recycling...
|
|
275
|
+
…
|
|
276
|
+
[1tube] workerd crashed (gen=6, code=137, crashes=6/5 in 30s) — GIVING UP, manual restart required.
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
After the budget is exhausted the gateway returns `502 workerd backend error` for every request until the operator restarts it (or, in dev, an HMR save which clears the counter and resumes recovery). End-to-end coverage in `tests/workerd-e2e.test.ts` (`gateway auto-recovers when workerd is killed externally`).
|
|
280
|
+
|
|
281
|
+
### Observability on workerd
|
|
282
|
+
|
|
283
|
+
Both `/health` (JSON) and `/metrics` (Prometheus exposition) report workerd-specific state when the backend is active. Both endpoints require `Authorization: Bearer $INTERNAL_KEY`.
|
|
284
|
+
|
|
285
|
+
`/health` adds a `workerd` block:
|
|
286
|
+
|
|
287
|
+
```json
|
|
288
|
+
{
|
|
289
|
+
"workerd": {
|
|
290
|
+
"pid": 4242,
|
|
291
|
+
"generation": 3,
|
|
292
|
+
"recycles": 0,
|
|
293
|
+
"rss_bytes": 134217728,
|
|
294
|
+
"budget_bytes": 268435456,
|
|
295
|
+
"last_reload_duration_ms": 412.5,
|
|
296
|
+
"bundle_bytes": { "hello": 1024, "echo": 998 }
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
`/metrics` exposes the same data as scrapeable gauges, plus per-function circuit-breaker view that mirrors `supervisor.allStats()`:
|
|
302
|
+
|
|
303
|
+
| Gauge | Meaning |
|
|
304
|
+
|---|---|
|
|
305
|
+
| `onetube_workerd_up` | `1` if workerd is currently running, `0` between processes (mid-recycle, post-crash gap). |
|
|
306
|
+
| `onetube_workerd_pid` | PID of the live workerd, `0` when not running. |
|
|
307
|
+
| `onetube_workerd_generation` | Increments on every reload (HMR / watchdog / crash recovery). |
|
|
308
|
+
| `onetube_workerd_recycles_total` | Number of memory-watchdog-triggered recycles. |
|
|
309
|
+
| `onetube_workerd_rss_bytes` | Most recent RSS sample. |
|
|
310
|
+
| `onetube_workerd_budget_bytes` | RSS budget enforced by the watchdog (when set). |
|
|
311
|
+
| `onetube_workerd_last_reload_duration_ms` | Wall-clock duration of the most recent successful reload. |
|
|
312
|
+
| `onetube_workerd_bundle_bytes{function="..."}` | esbuild output size per function. |
|
|
313
|
+
| `onetube_function_breaker_open{function="..."}` | `1` while the supervisor's breaker is open for this function. |
|
|
314
|
+
| `onetube_function_error_rate{function="..."}` | Rolling error rate inside the supervisor's window (0..1). |
|
|
315
|
+
| `onetube_function_recycle_recommended{function="..."}` | `1` once `recycle.maxRequests` has been reached. |
|
|
316
|
+
|
|
317
|
+
The breaker gauges work on the Deno backend too — the supervisor is shared between paths.
|
|
318
|
+
|
|
319
|
+
### V8 inspector / debugger access
|
|
320
|
+
|
|
321
|
+
Pass `--inspector` (or `--inspector-addr=host:port`, `1TUBE_INSPECTOR=1`) to launch workerd with `--inspector-addr=...`. Default address is `127.0.0.1:9229`:
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
deno run --allow-all src/server.ts --backend workerd --inspector
|
|
325
|
+
# → [1tube] workerd V8 inspector enabled at 127.0.0.1:9229 (open chrome://inspect or attach via DevTools)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Then open `chrome://inspect` in a Chromium browser; the workerd target appears under "Remote Target" once a function is invoked. Set breakpoints, step through bundled code, inspect locals — everything you'd expect from V8.
|
|
329
|
+
|
|
330
|
+
Caveats:
|
|
331
|
+
|
|
332
|
+
- **Unauthenticated debug port.** `--inspector` opens an unauthenticated V8 protocol port. The gateway warns loudly when the bind address is non-loopback. Don't enable in production.
|
|
333
|
+
- **HMR port shift.** When `--hmr` is also enabled, every reload spawns a *new* workerd on the next port slot. The inspector port shifts by one (`9229` → `9230` → `9229` → …) to avoid colliding with the still-listening predecessor. The boot log calls out the actual port for each generation.
|
|
334
|
+
- **Workerd-only.** The flag is a no-op on `--backend deno`; debug Deno-side functions with the standard `--inspect` Deno flag instead.
|
|
335
|
+
|
|
336
|
+
### Graceful shutdown
|
|
337
|
+
|
|
338
|
+
On `SIGINT` / `SIGTERM` the gateway runs a three-phase shutdown bounded by `1TUBE_SHUTDOWN_GRACE_MS` (default `10000`):
|
|
339
|
+
|
|
340
|
+
1. **Stop watchers** — memory watchdog, hot reloader. Prevents a poll racing the shutdown from triggering a doomed recycle.
|
|
341
|
+
2. **Drain the listener** — `Deno.serve`'s abort signal stops accepting new TCP connections. Already-accepted requests keep running against the still-live workerd until they complete or grace expires.
|
|
342
|
+
3. **Stop workerd** — only after the drain has finished (or grace expired). Workerd's own SIGTERM-then-SIGKILL ladder gets the larger of `(remaining_grace, 1000ms)` so its log pumps can flush a final line.
|
|
343
|
+
|
|
344
|
+
Previous releases fired-and-forgot `workerd.stop()` in parallel with the drain, which killed workerd while requests were still forwarding through it (502s on the way out). The current ordering ensures in-flight requests keep their original socket until they complete naturally — same property HMR's dual-process swap relies on.
|
|
345
|
+
|
|
346
|
+
If grace is too tight (rare), bump `1TUBE_SHUTDOWN_GRACE_MS=30000`. The boot log records the actual drain + workerd-stop durations so you can size it from real numbers.
|
|
347
|
+
|
|
348
|
+
### Prebuilt artifacts (`1tube build` + `--prebuilt`)
|
|
349
|
+
|
|
350
|
+
For prod deploys you usually don't want esbuild on the box. `1tube build` produces a sealed artifact directory that `1tube serve --prebuilt` can boot with **zero bundling on the critical path** — just load, sha-256-verify, hand to workerd.
|
|
351
|
+
|
|
352
|
+
```bash
|
|
353
|
+
# Build in CI (or locally)
|
|
354
|
+
deno task build --functions ./supabase/functions --out ./dist
|
|
355
|
+
# → dist/manifest.json + hello.js + echo.js + ... + .gitignore
|
|
356
|
+
|
|
357
|
+
# Ship `dist/` and the workerd binary to prod, then:
|
|
358
|
+
deno run --allow-all src/cli.ts --backend workerd --prebuilt ./dist
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Build flags worth knowing:
|
|
362
|
+
|
|
363
|
+
| Flag | Default | Purpose |
|
|
364
|
+
|---|---|---|
|
|
365
|
+
| `--functions <path>` | `./supabase/functions` | Source dir to bundle. |
|
|
366
|
+
| `--out <path>` | `./dist` | Output artifact directory. |
|
|
367
|
+
| `--only A,B,C` | all | Build a subset (smoke tests, partial deploys). |
|
|
368
|
+
| `--sourcemap MODE` | `linked` | `none` / `linked` / `inline`. |
|
|
369
|
+
| `--minify` | off | Minify output. |
|
|
370
|
+
| `--compat-date YYYY-MM-DD` | (workerd's default) | Bake a workerd compatibility date into the manifest. |
|
|
371
|
+
| `--compat-flag FLAG` | `nodejs_compat` | Repeatable; appends a workerd compatibility flag. |
|
|
372
|
+
| `--workerd-env A,B,C` | empty | Bake env-var allowlist into the manifest. Values are still read from process env at serve time — only names are persisted. |
|
|
373
|
+
|
|
374
|
+
`dist/manifest.json` carries:
|
|
375
|
+
|
|
376
|
+
- Schema version (`schema: 1`) — serve refuses newer schemas instead of mis-parsing.
|
|
377
|
+
- 1tube version + ISO build timestamp (diagnostic).
|
|
378
|
+
- Per-function `bundleFile` + `bundleBytes` + `bundleSha256` (lower-case hex). The serve loader recomputes the digest at boot and **fails fast** if a bundle has been modified or corrupted.
|
|
379
|
+
- Per-function `1tube.json` parsed in-line, so a prebuilt deploy doesn't need the source tree at all.
|
|
380
|
+
|
|
381
|
+
Prebuilt-mode behaviour at serve time:
|
|
382
|
+
|
|
383
|
+
- `--functions` is ignored (warned about). `1TUBE_PREBUILT=path` works as an env var equivalent.
|
|
384
|
+
- `--prebuilt` implies `--backend workerd`; coerced with a warning if you forget.
|
|
385
|
+
- `--hmr` is rejected with a one-line warning — sealed artifacts can't reload. Re-run `1tube build` and restart.
|
|
386
|
+
- `backend.reload()` is rejected. Crash-recovery (re-spawning workerd against the same on-disk bundles) still works — it doesn't need to re-bundle.
|
|
387
|
+
- The artifact directory must be **writable at boot** because workerd's generated `config.gen-*.capnp` is written next to the bundles. If your prod filesystem is read-only, `cp -r dist /tmp/dist-rw && --prebuilt /tmp/dist-rw`. (Capnp generation is microseconds; we don't bake it at build time so `--port`, `--host`, and `--workerd-env` overrides keep working at serve.)
|
|
388
|
+
|
|
389
|
+
What you gain by separating build and serve:
|
|
390
|
+
|
|
391
|
+
- Reproducible deploys — the artifact is byte-identical for fixed inputs (esbuild output is deterministic; manifest sorts by function name).
|
|
392
|
+
- Tamper-evident — sha-256 verification at boot catches both corruption and silent edits.
|
|
393
|
+
- No esbuild / `@deno/esbuild-plugin` / npm fetch on the prod box.
|
|
394
|
+
- Faster boot — esbuild (the slow part) runs once in CI, not once per restart.
|
|
395
|
+
|
|
396
|
+
### Cache layout
|
|
397
|
+
|
|
398
|
+
Bundles, sourcemaps, and the generated `config.gen-{0,1}.capnp` files live in `node_modules/.cache/1tube-workerd/` (or `.1tube-cache/workerd/` if there's no `node_modules/`). The directory is auto-gitignored. Safe to delete; it gets rebuilt on next boot.
|
|
399
|
+
|
|
400
|
+
### Benchmarking
|
|
401
|
+
|
|
402
|
+
A self-contained bench script under `scripts/bench.ts` boots a gateway with each backend, hammers `/functions/v1/hello` (GET) and `/functions/v1/echo` (POST with a 256-byte body), and reports `RPS / p50 / p95 / p99` per (backend, route) pair. It auto-disables rate limiting via `1TUBE_DISABLE_RATE_LIMIT=1` so per-function manifest caps don't skew the numbers.
|
|
403
|
+
|
|
404
|
+
```bash
|
|
405
|
+
# Default sweep (5000 reqs at 64 concurrency, both backends, ~30s).
|
|
406
|
+
deno run --allow-all scripts/bench.ts
|
|
407
|
+
|
|
408
|
+
# Single backend + heavier load.
|
|
409
|
+
deno run --allow-all scripts/bench.ts --backend workerd -n 20000 -c 128
|
|
410
|
+
|
|
411
|
+
# Quick smoke (won't catch tail latency but boots fast).
|
|
412
|
+
deno run --allow-all scripts/bench.ts -n 500 -c 16 --warmup 50
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
Order-of-magnitude numbers on a recent laptop, loopback, both backends warm:
|
|
416
|
+
|
|
417
|
+
| Backend | Route | RPS | p50 | p95 | p99 |
|
|
418
|
+
|---------|-------|----:|----:|----:|----:|
|
|
419
|
+
| Deno | GET hello | 3180 | 8.8ms | 16.7ms | 26.0ms |
|
|
420
|
+
| Deno | POST echo | 1819 | 16.4ms | 27.2ms | 36.3ms |
|
|
421
|
+
| Workerd | GET hello | 1377 | 22.6ms | 32.9ms | 38.0ms |
|
|
422
|
+
| Workerd | POST echo | 1034 | 29.7ms | 47.2ms | 57.2ms |
|
|
423
|
+
|
|
424
|
+
Workerd costs roughly 2× the latency of in-process Deno calls because every request takes one extra HTTP hop (gateway → workerd loopback socket). For LLM/streaming workloads where the upstream call dwarfs proxy overhead, that delta is invisible.
|
|
425
|
+
|
|
63
426
|
## .NET integration
|
|
64
427
|
|
|
65
428
|
Add the `OneTube` NuGet package to your ASP.NET project:
|
|
@@ -67,8 +430,17 @@ Add the `OneTube` NuGet package to your ASP.NET project:
|
|
|
67
430
|
```csharp
|
|
68
431
|
services.AddOneTube(options =>
|
|
69
432
|
{
|
|
70
|
-
|
|
71
|
-
|
|
433
|
+
// Path to the edge functions directory. Absolute, or relative
|
|
434
|
+
// to AppContext.BaseDirectory (the host's bin/).
|
|
435
|
+
options.FunctionsPath = "supabase/functions";
|
|
436
|
+
|
|
437
|
+
// Binaries — absolute, relative to AppContext.BaseDirectory,
|
|
438
|
+
// or bare names (PATH lookup). On servers, drop deno/workerd
|
|
439
|
+
// next to the published host and use relative paths so the
|
|
440
|
+
// deploy is reproducible.
|
|
441
|
+
options.DenoBinary = "onetube-bin/deno.exe";
|
|
442
|
+
options.WorkerdBinary = "onetube-bin/workerd.exe";
|
|
443
|
+
|
|
72
444
|
options.Port = 3100;
|
|
73
445
|
options.EnvVars = new()
|
|
74
446
|
{
|
|
@@ -81,4 +453,6 @@ services.AddOneTube(options =>
|
|
|
81
453
|
app.MapOneTube(port: 3100);
|
|
82
454
|
```
|
|
83
455
|
|
|
456
|
+
The OneTube package ships the gateway TypeScript sources alongside the DLL (under `OneTubeGateway/` in the host's output directory), so you do **not** need a 1tube checkout on the production host — only the `deno` and `workerd` binaries you point at via `DenoBinary` / `WorkerdBinary`.
|
|
457
|
+
|
|
84
458
|
This spawns the Deno gateway as a managed child process with health monitoring and auto-restart, and forwards `/functions/v1/*` via YARP zero-copy proxying.
|
package/deno.json
CHANGED
|
@@ -4,15 +4,20 @@
|
|
|
4
4
|
"lib": ["deno.window"]
|
|
5
5
|
},
|
|
6
6
|
"tasks": {
|
|
7
|
-
"dev": "deno run --allow-all --watch src/
|
|
8
|
-
"serve": "deno run --allow-all src/
|
|
9
|
-
"play": "deno run --allow-all --watch src/
|
|
7
|
+
"dev": "deno run --allow-all --watch src/cli.ts",
|
|
8
|
+
"serve": "deno run --allow-all src/cli.ts",
|
|
9
|
+
"play": "deno run --allow-all --watch src/cli.ts --dev --hmr --functions ./playground --port 3199",
|
|
10
|
+
"play:workerd": "deno run --allow-all src/cli.ts --backend workerd --dev --hmr --functions ./playground --port 3199",
|
|
11
|
+
"build": "deno run --allow-all src/cli.ts build",
|
|
10
12
|
"test": "deno test --allow-all tests/"
|
|
11
13
|
},
|
|
12
14
|
"imports": {
|
|
13
15
|
"@std/path": "jsr:@std/path@^1",
|
|
14
16
|
"@std/assert": "jsr:@std/assert@^1",
|
|
15
|
-
"hono": "npm:hono@4"
|
|
17
|
+
"hono": "npm:hono@4",
|
|
18
|
+
"esbuild": "npm:esbuild@^0.28",
|
|
19
|
+
"@deno/esbuild-plugin": "jsr:@deno/esbuild-plugin@^1.2",
|
|
20
|
+
"@zip-js/zip-js": "jsr:@zip-js/zip-js@^2.8"
|
|
16
21
|
},
|
|
17
22
|
"lint": {
|
|
18
23
|
"rules": {
|