Two halves of the same "mesh off Node" gap; they touch the same file and landed
together.
Control channel (Task 6): meshControl existed only inside listen(), passed to
createHttpServer, so a teapot could only export its scopes from Node. app.upgrade
— the path serveDeno/serveBun drive — had never heard of it, which would have left
"mesh on three runtimes" meaning consume-only. It is now built in the boot gate
(it captures the finalized ordered nodes, so it cannot exist before finalize) and
the reserved path is routed from app.upgrade. Task 3 already made handle() take a
neutral WsSocket and upgrade holds one, so this is a direct call, not a second
implementation. handle() returns the connection's lifetime like runWsConnection,
so upgrade awaits it instead of returning while the peer is still talking. An app
with nothing exported refuses the reserved path explicitly rather than falling
through to "no matching ws route".
Link lifecycle (Task 7): links were reaped by server.on('close'), reachable only
through listen(), so a mesh app booted through app.fetch had links and no server
and leaked every teapot connection at `if (!server) return`. close() now closes
them first either way; that aborts each socket, which rejects its in-flight RPCs.
Also fixes a race the new tests exposed: upgrade awaited the boot *before* handing
the socket to a consumer, but `channel` is fan-out, so frames arriving during the
boot were dropped with no subscriber attached. For the control channel that is
`hello` itself — against a teapot with slow providers the handshake would hang
until timeout. Inbound is now subscribed synchronously on entry and the buffered
iterator handed down, closing the same gap for regular @Ws routes.
Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Tasks 6, 7
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Only listen() spliced remote scopes and finalized the graph, and listen() builds
a Node http.Server. Deno/Bun/edge serve through app.fetch and can never call it,
so mesh was Node-only by construction: fetch() threw "unavailable before
listen()" forever on those runtimes, telling operators to call something they
had no way to call. buildAppFetch also hardcoded remoteRoutes to [], so even past
the guard it would not have served a remote route.
Move splice+finalize into the memoized boot gate that fetch, upgrade and listen
already share, ahead of the providers — remote nodes must join before the topo
sort. Await it before building the route table rather than after, since for a
mesh app the table is not knowable until the splice has run. listen() keeps
working by awaiting the same gate; the memoization means a fetch followed by a
listen boots once, and concurrent first requests splice once.
The fetch/upgrade guards are gone rather than reworded: they are now unreachable.
inspect()/graph()/explain() keep theirs — they are synchronous and cannot await
network I/O — but their message no longer claims listen() is the only way in.
test/fetch.test.ts asserted the old contract (fetch throws until listen), i.e.
the trap itself; it now pins that a mesh app serves without listen() and that
listen() afterwards does not re-run provider factories.
Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 5
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
connectLink constructed `require('ws')` directly and drove its EventEmitter API,
which is why a teacup could only run on Node. It now goes through the neutral
WsSocket like every other consumer, over whichever client the runtime provides:
the platform global (Deno, Bun, Node >= 22) or the `ws` peer for Node 18-21,
which has no global. `ws` stays optional and is only reached for when the global
is absent.
One bridge covers both because `ws` also implements the browser-compatible event
API and delivers text frames as `event.data` strings — probed against the real
package rather than taken on faith.
Both implementations are exercised in one run: `ws` is always in the matrix, so
the Node 18-21 path cannot rot unseen on CI that only runs a modern Node, which
is how a fallback quietly stops working. connectLink takes an optional Ctor to
make that possible; it doubles as a bring-your-own-WebSocket hook for runtimes
we do not ship an adapter for.
Verified the edge bundle is unaffected: `require('ws')` tree-shakes out of the
workerd build entirely.
Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 4
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
The mesh control channel was the last consumer speaking the `ws` EventEmitter
API directly: http/ws.ts handed meshControl.handle a raw socket, while every
@Ws route already went through nodeSocket() to get a neutral WsSocket. Mesh did
not lack an abstraction — it skipped the one the codebase already had.
handle() now takes a WsSocket and consumes `inbound`, so both feeds work: Node
via the existing nodeSocket, and Deno/Bun via app.upgrade, where the socket is
already neutral. That is what unblocks mounting the control channel off Node.
RPCs are still dispatched without awaiting. The old `ws.on('message')` listener
overlapped them, and the peer's id-keyed pending map exists for exactly that —
awaiting inside the receive loop would have silently serialized every mesh call.
A test now pins the overlap.
The unused `http` import in http/types.ts fell out with MeshControl's req
parameter: nothing in that module is Node-shaped any more.
Node behaviour is unchanged — the teapot↔teacup integration tests pass
untouched. teapot.test.ts drives handle() directly, so its fake moved from a raw
EventEmitter to a fake WsSocket.
Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 3
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
safeEqual guards the mesh shared secret and reached for Buffer + timingSafeEqual
through a bare 'crypto' specifier, leaving resolution to Deno/Bun node-compat
heuristics. Name the builtin explicitly — the authentication path should not
depend on which compat layer happens to answer.
Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 2
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Cross-runtime mesh means peers are separate processes on separate deploy
cadences, so the frame union is a contract between green-tea versions rather
than an internal detail. There was no version field, and decode validated only
that `type` was a known tag before casting `parsed as Frame` — every other
field was trusted, so a skewed or malformed peer failed somewhere downstream
instead of at the boundary.
Add MESH_PROTOCOL_VERSION to the hello/manifest frames. Both ends refuse a
mismatch: the teapot checks the version *before* the secret, since a skewed
peer is not an auth failure and "wrong secret" would send its operator hunting
the wrong bug.
Validate fields per frame type via a Record keyed on the type tag, so a new
frame type must add a row. The lookup uses an own-property check: `type` is
peer-controlled and a bare lookup would resolve SHAPE['constructor'] up the
prototype chain to a truthy non-check.
This is a breaking wire change, done now because it is free exactly once —
mesh is alpha behind experimental:true and has no deployed peers. Adding a
version later would break every peer already in the field.
Refs .specs/2026-07-15-green-tea-mesh-cross-runtime-plan.md Task 1
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
- OpenApiRoute.transport was typed `string`, so the compiler could not check the
transport branching at all. Type it Transport and key the documented 200 off a
Record, so a new transport fails the build instead of silently going undocumented.
- pickEncoder fell through to ndjson for unknown transports; make the mapping
exhaustive so a new transport cannot inherit the wrong wire format by accident.
- HttpError carries optional headers, removing the instanceof Redirect special case
that coupled the error renderer to one subclass. Custom errors needing headers
(retry-after, etag) no longer require touching transformers.
- Drop the module-global connSeq in mesh/link: `pending` is already per-connection,
so a per-link counter suffices and connection ids stop depending on test order.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
toWsRequest, the neutral-socket wrapper and the upgrade error guard were
copy-pasted across all three runtime adapters, comments included. A change to
the upgrade close code meant remembering to touch three files.
Extract the shared parts to http/ws-adapter (kept separate from ws-core: that
module runs a connection, this one builds one from a host runtime's socket).
Bun keeps its own socket wrapper — its events land on a server-level handler
rather than the socket, so the lifecycle genuinely differs.
Verified against real runtimes: deno test, bun test, workerd via miniflare.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Steps were executed by three separate hand-rolled loops: runPipeline (HTTP),
buildWsRoutes (ws) and resolveScope (mesh). Only the HTTP one emitted
request:step:enter/leave, so a logging plugin silently observed nothing on ws
routes — a gap in a documented public API, not just redundancy.
Route every transport through a single runSteps() so observability is uniform
by construction rather than by remembering to copy the emit calls.
Also fixes the plugins guide, which documented a request:step:exit event that
has never existed (the bus emits request:step:leave).
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
feat(pipeline): transport is declared, not inferred (BREAKING)
A route's declared transport is now the ground truth for what its handler
may return — the old implicit "any handler returning an AsyncIterable
streams" behavior (spec #2) is removed.
- @Get/@Post/@Put/@Patch/@Delete (buffer) MUST return a value; returning an
AsyncIterable -> 500 TransportMismatchError.
- @Sse/@Ws MUST return an AsyncIterable; returning a value -> 500.
- @Stream (negotiate) still tolerates both (client negotiates via Accept).
- Extensible TRANSPORT_RETURN map (Record<Transport, ReturnContract>);
runPipeline gains a required `transport` arg, wired at both call sites.
- @Html (buffer) is covered by the general rule; its return contract
(bare->string, template->object, file->void) is documented.
BREAKING (pre-1.0): a return value no longer changes a route's wire
contract. Declare @Sse/@Stream/@Ws to stream. 0 examples affected.
269 tests, tsc/lint/complexity green. Per-task + final opus review.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Boots the actual workerd engine locally (miniflare devDep, no CF account
needed) and round-trips HTTP (app.fetch) + WebSocket (WebSocketPair +
app.upgrade) through edgeHandler's real request path, mirroring the Deno/Bun
adapter tests' assertion shape.
workerd's nodejs_compat genuinely does not implement node:http/node:https
(confirmed in isolation against the real runtime; node:crypto, node:stream,
node:net, node:tls, node:events, node:util, node:buffer all load fine under
the same flags). The barrel statically imports them for the Node listen()
path even though edgeHandler never touches it, so the bundler stubs those
two modules at test-bundle time (inert createServer that throws if ever
called) purely to let the worker load — edgeHandler itself is untouched.
See project-docs/.superpowers/sdd/task-2-report.md for the full writeup and
the suggested core follow-up (lazy-defer those imports behind listen()).
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Also fixes buildWsRoutes (app/index.ts) to read the pre-derived
protocol/ip off the neutral WsRequest instead of req.socket, which no
longer exists now that WsOpenCtx.req is WsRequest — without this every
real @Ws route throws on open() and the connection hangs.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Record the app.fetch Web-Standards handler and the app-scope provider
memoization behaviour change in the changelog, and note on the runtimes
guide that app.fetch reads oversized bodies fully before rejecting them
(unlike Node's mid-stream abort) so non-Node deployments should also
bound request size at the platform layer.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
The Web-Standards fetch adapter read x-forwarded-for unconditionally to
seed ctx.ip, unlike Node's deriveIp which only honours it when
trustProxy is enabled. A client could spoof ctx.ip on any handler
relying on it. Mirror deriveIp's first-hop semantics and only trust
the header when trustProxy is set; otherwise ctx.ip is ''.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
Add a Node-vs-app.fetch parity suite that drives GET/POST/404/CORS-preflight/SSE
through both the real Node server and app.fetch and asserts identical status,
headers, and body. It caught a real divergence: buildFetch's preflight branch
returned only the CORS-specific headers, skipping the security headers
(x-content-type-options, x-frame-options, referrer-policy, x-dns-prefetch-control)
that the Node adapter's writeHead patch applies to every response including the
204 preflight. Fix buildFetch to merge `injected` into the preflight response the
same way, so both paths agree.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
app.fetch's 413/400/501 body-acquisition failures were returned without
the injected security/CORS headers Node's writeHead patch always
applies, breaking byte-identical parity with the listen() path. Extract
computeInjected() (shared by handle() and buildFetch()) and merge it
into every error response, not just handle()'s outcomes.
Also, buildAppFetch captured `booted` by value at createApp() time, so
mesh apps (whose finalize() is deferred into listen()) had app.fetch
permanently throw even after a successful listen(). Read booted live
via a getter, mirroring explain()/graph()/inspect(), and lazily
build/cache the fetch handler on first call after boot.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>