|
| 1 | +import type Negotiator from 'negotiator'; |
| 2 | +import { PassThrough, pipeline, Readable, Transform } from 'node:stream'; |
| 3 | +import * as zlib from 'node:zlib'; |
| 4 | +import { RequestTracker } from '../sync/RequestTracker.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Compress a streamed response. |
| 8 | + * |
| 9 | + * `@fastify/compress` can do something similar, but does not appear to work as well on streamed responses. |
| 10 | + * The manual implementation is simple enough, and gives us more control over the low-level details. |
| 11 | + * |
| 12 | + * @param negotiator Negotiator from the request, to negotiate response encoding |
| 13 | + * @param stream plain-text stream |
| 14 | + * @returns |
| 15 | + */ |
| 16 | +export function maybeCompressResponseStream( |
| 17 | + negotiator: Negotiator, |
| 18 | + stream: Readable, |
| 19 | + tracker: RequestTracker |
| 20 | +): { stream: Readable; encodingHeaders: { 'content-encoding'?: string } } { |
| 21 | + const encoding = (negotiator as any).encoding(['identity', 'gzip', 'zstd'], { preferred: 'zstd' }); |
| 22 | + const transform = createCompressionTransform(encoding); |
| 23 | + if (transform == null) { |
| 24 | + // No matching compression supported - leave stream as-is |
| 25 | + return { |
| 26 | + stream, |
| 27 | + encodingHeaders: {} |
| 28 | + }; |
| 29 | + } else { |
| 30 | + tracker.setCompressed(encoding); |
| 31 | + return { |
| 32 | + stream: transformStream(stream, transform, tracker), |
| 33 | + encodingHeaders: { 'content-encoding': encoding } |
| 34 | + }; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function createCompressionTransform(encoding: string | undefined): Transform | null { |
| 39 | + if (encoding == 'zstd') { |
| 40 | + // Available since Node v23.8.0, v22.15.0 |
| 41 | + // This does the actual compression in a background thread pool. |
| 42 | + return zlib.createZstdCompress({ |
| 43 | + // We need to flush the frame after every new input chunk, to avoid delaying data |
| 44 | + // in the output stream. |
| 45 | + flush: zlib.constants.ZSTD_e_flush, |
| 46 | + params: { |
| 47 | + // Default compression level is 3. We reduce this slightly to limit CPU overhead |
| 48 | + [zlib.constants.ZSTD_c_compressionLevel]: 2 |
| 49 | + } |
| 50 | + }); |
| 51 | + } else if (encoding == 'gzip') { |
| 52 | + return zlib.createGzip({ |
| 53 | + // We need to flush the frame after every new input chunk, to avoid delaying data |
| 54 | + // in the output stream. |
| 55 | + flush: zlib.constants.Z_SYNC_FLUSH |
| 56 | + }); |
| 57 | + } |
| 58 | + return null; |
| 59 | +} |
| 60 | + |
| 61 | +function transformStream(source: Readable, transform: Transform, tracker: RequestTracker) { |
| 62 | + // pipe does not forward error events automatically, resulting in unhandled error |
| 63 | + // events. This forwards it. |
| 64 | + const out = new PassThrough(); |
| 65 | + const trackingTransform = new Transform({ |
| 66 | + transform(chunk, _encoding, callback) { |
| 67 | + tracker.addCompressedDataSent(chunk.length); |
| 68 | + callback(null, chunk); |
| 69 | + } |
| 70 | + }); |
| 71 | + pipeline(source, transform, trackingTransform, out, (err) => { |
| 72 | + if (err) out.destroy(err); |
| 73 | + }); |
| 74 | + return out; |
| 75 | +} |
0 commit comments