Description
After this tweet from @mjackson:
Somebody please stop me from building an actual fetch-based HTTP server for Node.js
— MJ (@mjackson) September 9, 2024
I started thinking about what it would take to actually replace node:http
with an http server that directly implements Web APIs as described in README.md:
What exactly we mean by "web standards"? It means we use:
The Web Streams API instead of Node.js streams
Uint8Array instead of Node.js Buffers
The Web Crypto API instead of the Node.js crypto library
Blob and File instead of some bespoke runtime-specific API
Took a deep dive into the source code of node:http
and node:net
to see what we would be working on and here are my thoughts:
No soup for you!
The files that make up node:http
rely heavily on Node internal objects and functions that are not available to userland JS.
- the global
primordials
object - the
internalBinding('MODULE_ID
)` function - Make compile time macros in C++
- the
http_parser
module (internal binding) - functions, constants, and symbols from
internal/http
,internal/async_hooks
,internal/errors
, etc
Replicating the functionality of node:http
while still relying on node:net
is then gonna require adding a new built-in module to Node and getting it merged. It won't be possible directly from userland
Enter the multi-(language)-verse
One alternative I thought of is building a native addon that bundles a different http server and exposes a Web API-based interface to JS, while staying as close to node:http
as possible:
There are viable options in Rust (https://hyper.rs/, https://actix.rs/) and in Go (https://gofiber.io/, https://github.com/go-chi/chi, https://gorilla.github.io/) that can be compiled as Node native modules.
Encore.ts seems to be doing something like this but they've gone the "batteries included" route where the value prop only makes sense if you switch your entire runtime and app architecture to their library. Ideally whatever I would build would only aim to provide an alternative to node:http
and not become an entire stack.
This path would be a lot of work so before I keep going down the rabbit hole your feedback would be greatly appreciated 🤓
Activity