Idea: deno-handler preset for library-mode embedding (sibling of deno-server) #4286
Replies: 2 comments 5 replies
-
|
I guess you can just use |
Beta Was this translation helpful? Give feedback.
-
|
Hey @pi0 — yeah, v3's Would a v2 backport of Happy to retarget this PR or open a fresh one — whichever's cleaner. Use case: multi-tenant Nitro hosting — one Deno process spawns a Web Worker per site, imports each site's |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
The current
deno-serverpreset callsDeno.serve()as a module-level side effect, so importing the builtindex.mjsimmediately binds a port. There's no way to embed a Nitro build as a library in a parent Deno process.Concrete use case: hosting many tenants in a single Deno process where a parent dispatcher routes by Host header. We work around the missing library mode today by having each Nitro build bind its own loopback port (
127.0.0.1:400N) and the dispatcher doesfetch("http://127.0.0.1:port/...")between them — adding TCP overhead, port-allocation logic, andDeno.env-race serialization to in-process communication that ought to be a function call.Proposed solution
Add a sibling preset
deno-handler(preservingdeno-serveras-is) that exports the handler instead of side-effecting a listen — mirroring how the Cloudflare preset family has bothcloudflare-workerandcloudflare-module.Consumer pattern:
No per-build port binding, no localhost TCP. Same primitive Cloudflare Workers solves via
isolate.fetch().Implementation prototype
Draft PR: #4285 (against
v2)deno-serverminus theDeno.serve()callfetch(named) anddefault { fetch }matchingcloudflare-module's conventiontest/presets/deno-handler.test.ts: 43/43 pass, identical to thedeno-serverv2 baselinemainport also ready locally — same shape, 49/49 conformance pass — happy to file as a follow-up PR once direction is agreedQuestions for maintainers
deno-serverthat conditionally skips the listen?deno-handlerfollows the contract ("exports a handler"). Alternatives considered:deno-module(mirrorscloudflare-module),deno-library. Preference?info(Deno.ServeHandlerInfo) as the 2nd arg, throws clearly if missing. Acceptable?hooks.compiled— nodeno.jsonemitted (embedding host owns it). OK?Beta Was this translation helpful? Give feedback.
All reactions