Streaming Gzip Responses
Streaming Compressed Responses
Hello, compression pipeline builder! 🗜️ In the previous lesson, you preserved the fixture through a Node Readable → standard Web Response → Node Readable → native ServerResponse bridge. That bridge remains unchanged here. This lesson adds a separate gzip byte-stream pipeline between the existing fixture ReadStream and the native HTTP response, then proves the produced gzip bytes by decompressing them directly.
The completed endpoint is:
Its pipeline is:
Compression is a stream stage, not a one-time conversion of a fully loaded file. It accepts source bytes incrementally, emits encoded bytes incrementally, and leaves transfer, completion, and failure handling to the stream pipeline.
Locate the Existing Byte Source
The existing source factory in backend/src/streams/file-source.ts opens the supplied fixture path as a Node byte stream. Both the compressed route and the direct gzip test reuse it unchanged.
createReadStream(samplePath)produces a NodeReadStream; it does not load the fixture into a completeBufferfirst.- The route supplies
samplePath, keeping this source factory independent of HTTP concerns and fixture-location decisions. - Compression belongs after this source: the gzip stage transforms the bytes produced by the
ReadStream. - The gzip module must not reopen the fixture, parse NDJSON, or recreate its contents.
The completed bridge remains a separate composition. backend/src/streams/web-response.ts adapts a Node-readable source into a standard Response, then converts its body back only for a Node-native consumer.
Readable.toWeb()andReadable.fromWeb()change stream interfaces; they do not decode, parse, buffer, or alter the fixture bytes.- The
Responseowns the bridge endpoint’s status and metadata. - The
response.bodyguard preventsReadable.fromWeb()from receiving anullbody. - The gzip endpoint does not add compression to this bridge. It uses a direct Node stream pipeline instead.
backend/src/routes/web-stream.ts shows the unchanged bridge route at the native HTTP boundary.
- The bridge starts with the same
createSampleReadStream(samplePath)source used by the compressed route. - This route copies metadata from its internal standard
Responseto the nativeServerResponse. await pipeline(...)keeps the route pending until the stream completes or fails.backend/src/app.tsremains the runtime edge that creates the native Node request and response objects and delegates to the router.
The two endpoint shapes are deliberately different:
