Skip to content

Commit a133076

Browse files
committed
Minor refactoring.
1 parent 2c3f94c commit a133076

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

packages/service-core/src/routes/compression.ts

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { PassThrough, pipeline, Readable, Transform } from 'node:stream';
21
import type Negotiator from 'negotiator';
2+
import { PassThrough, pipeline, Readable, Transform } from 'node:stream';
33
import * as zlib from 'node:zlib';
44
import { RequestTracker } from '../sync/RequestTracker.js';
55

@@ -19,54 +19,51 @@ export function maybeCompressResponseStream(
1919
tracker: RequestTracker
2020
): { stream: Readable; encodingHeaders: { 'content-encoding'?: string } } {
2121
const encoding = (negotiator as any).encoding(['identity', 'gzip', 'zstd'], { preferred: 'zstd' });
22-
if (encoding == 'zstd') {
23-
tracker.setCompressed(encoding);
24-
return {
25-
stream: transform(
26-
stream,
27-
// Available since Node v23.8.0, v22.15.0
28-
// This does the actual compression in a background thread pool.
29-
zlib.createZstdCompress({
30-
// We need to flush the frame after every new input chunk, to avoid delaying data
31-
// in the output stream.
32-
flush: zlib.constants.ZSTD_e_flush,
33-
params: {
34-
// Default compression level is 3. We reduce this slightly to limit CPU overhead
35-
[zlib.constants.ZSTD_c_compressionLevel]: 2
36-
}
37-
}),
38-
tracker
39-
),
40-
encodingHeaders: { 'content-encoding': 'zstd' }
41-
};
42-
} else if (encoding == 'gzip') {
43-
tracker.setCompressed(encoding);
22+
const transform = createCompressionTransform(encoding);
23+
if (transform == null) {
24+
// No matching compression supported - leave stream as-is
4425
return {
45-
stream: transform(
46-
stream,
47-
zlib.createGzip({
48-
// We need to flush the frame after every new input chunk, to avoid delaying data
49-
// in the output stream.
50-
flush: zlib.constants.Z_SYNC_FLUSH
51-
}),
52-
tracker
53-
),
54-
encodingHeaders: { 'content-encoding': 'gzip' }
26+
stream,
27+
encodingHeaders: {}
5528
};
5629
} else {
30+
tracker.setCompressed(encoding);
5731
return {
58-
stream: stream,
59-
encodingHeaders: {}
32+
stream: transformStream(stream, transform, tracker),
33+
encodingHeaders: { 'content-encoding': encoding }
6034
};
6135
}
6236
}
6337

64-
function transform(source: Readable, transform: Transform, tracker: RequestTracker) {
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) {
6562
// pipe does not forward error events automatically, resulting in unhandled error
6663
// events. This forwards it.
6764
const out = new PassThrough();
6865
const trackingTransform = new Transform({
69-
transform(chunk, encoding, callback) {
66+
transform(chunk, _encoding, callback) {
7067
tracker.addCompressedDataSent(chunk.length);
7168
callback(null, chunk);
7269
}

0 commit comments

Comments
 (0)