Bridging Node Web Streams
Bridging Node Streams Through a Standard `Response`
Hello, stream bridge builder! 🌉 The project already serves a deterministic NDJSON fixture from a native Node HTTP server. This lesson adds a standard Web Response interoperability boundary around that existing Node stream while preserving fixture bytes and keeping ServerResponse as the final runtime HTTP edge.
The completed endpoint is GET /api/lab/web-stream:
This bridge does not parse NDJSON, decode text, buffer the body, transform records, or reserialize anything. It changes stream interfaces at a narrow interoperability boundary while preserving the original bytes.
Locate the Existing Ownership Boundaries
The fixture source is already a Node stream producer. backend/src/streams/file-source.ts establishes the source side of the bridge and remains unchanged.
createReadStream(samplePath)opens the fixture as a NodeReadStream, which is a NodeReadable.- The function does not inspect fixture contents or create JavaScript objects from NDJSON records.
- Returning the
ReadStreamdirectly lets the adapter receive a stream rather than a complete in-memory file buffer. - This is the first representation in the bridge: Node-native bytes flowing from the file system.
The existing buffered baseline in backend/src/routes/sample.ts serves the same fixture through a deliberately different ownership model.
readBufferedSample(samplePath)produces a completeBufferbefore the route begins the HTTP response.- The route can set
content-lengthbecause it already owns the complete body and knows its byte length. response.end(body)writes that buffered value directly to Node’s nativeServerResponse.- The bridge endpoint serves the same deterministic three-line fixture, but it must not copy this buffering approach.
A standard Response also does not replace the native server in this project. backend/src/app.ts is the runtime boundary that receives Node request and response objects.
createServer()provides nativeIncomingMessageandServerResponseobjects to the router.- The standard
Responseused by the bridge is an internal, fetch-compatible interoperability object rather than the final HTTP response object. - If route code fails after headers are already sent, the application destroys the native response instead of attempting to write a second HTTP response.
- The bridge therefore fits inside the existing Node HTTP lifecycle rather than replacing it.
