Skip to content

Node server fetch#6

Draft
mcollina wants to merge 2 commits into
mainfrom
node-server-fetch
Draft

Node server fetch#6
mcollina wants to merge 2 commits into
mainfrom
node-server-fetch

Conversation

@mcollina

Copy link
Copy Markdown
Owner

No description provided.

Add a modern HTTP server API that uses the Fetch API's Request and
Response objects, bypassing the legacy IncomingMessage/ServerResponse
stream-based model.

Features:
- serve(options, handler) creates HTTP or HTTPS servers
- Handler receives Request, returns Response (sync or async)
- getRemoteMetadata(request) retrieves connection info
- Automatic chunked transfer encoding when no Content-Length
- Keep-alive connection support
- Graceful shutdown via AbortSignal
- Custom error handling via onError callback

The implementation reuses the battle-tested HTTPParser but creates
Fetch API objects directly from parser output. Requests are serialized
per connection (no HTTP pipelining) - parser is paused after headers
and resumed after response is written.

Example usage:
  const { serve, getRemoteMetadata } = require('http');

  const server = serve({}, async (request) => {
    const body = await request.json();
    return Response.json({ received: body });
  });
  server.listen(3000);
- Enable TCP_NODELAY (noDelay: true) to prevent Nagle's algorithm
  from batching small writes, which was causing 40ms delays
- Batch header writes into a single socket.write() call
- Use cork()/uncork() to batch chunked body writes

These optimizations improve hello-world throughput from ~24 req/sec
to ~13,600 req/sec (567x improvement). The remaining 2.3x gap vs
createServer() is due to Response body streaming overhead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant