@fastify/compress 9.1.0 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,41 @@ const { Socket } = require('node:net')
5
5
  const { Duplex, PassThrough, Readable, Stream, Transform, Writable } = require('node:stream')
6
6
  const { finished } = require('node:stream/promises')
7
7
  const { test } = require('node:test')
8
- const { isStream, isZstd, isDeflate, isGzip, intoAsyncIterator, createPeekTransform } = require('../lib/utils')
8
+ const { availableParallelism } = require('node:os')
9
+ const { computeSyncThreshold, isStream, isZstd, isDeflate, isGzip, intoAsyncIterator, createPeekTransform } = require('../lib/utils')
10
+
11
+ test('computeSyncThreshold() utility should scale the default with the host parallelism', async (t) => {
12
+ t.plan(7)
13
+ const equal = t.assert.equal
14
+
15
+ // a single core has nothing to overlap stream compression with, so the whole range
16
+ // is worth compressing synchronously
17
+ equal(computeSyncThreshold(1), 65536)
18
+ equal(computeSyncThreshold(2), 8192)
19
+ // from three cores up the lower clamp takes over
20
+ equal(computeSyncThreshold(3), 4096)
21
+ equal(computeSyncThreshold(4), 4096)
22
+ // beyond libuv's threadpool size extra cores cannot compress any more in parallel
23
+ equal(computeSyncThreshold(8), 4096)
24
+ equal(computeSyncThreshold(16), 4096)
25
+ equal(computeSyncThreshold(1024), 4096)
26
+ })
27
+
28
+ test('computeSyncThreshold() utility should clamp the default to a sane range', async (t) => {
29
+ const parallelisms = [0, 1, 2, 3, 4, 7, 12, 64, 4096]
30
+ t.plan(parallelisms.length)
31
+
32
+ for (const parallelism of parallelisms) {
33
+ const threshold = computeSyncThreshold(parallelism)
34
+ t.assert.ok(threshold >= 4096 && threshold <= 65536, `${parallelism} cores yielded ${threshold}`)
35
+ }
36
+ })
37
+
38
+ test('computeSyncThreshold() utility should default to the host parallelism', async (t) => {
39
+ t.plan(1)
40
+
41
+ t.assert.equal(computeSyncThreshold(), computeSyncThreshold(availableParallelism()))
42
+ })
9
43
 
10
44
  test('isStream() utility should be able to detect Streams', async (t) => {
11
45
  t.plan(12)
package/types/index.d.ts CHANGED
@@ -37,6 +37,7 @@ type RouteCompressOptions = Pick<fastifyCompress.FastifyCompressOptions,
37
37
  | 'inflateIfDeflated'
38
38
  | 'onUnsupportedEncoding'
39
39
  | 'removeContentLengthHeader'
40
+ | 'syncThreshold'
40
41
  | 'threshold'
41
42
  | 'zlib'
42
43
  | 'zlibOptions'
@@ -78,6 +79,7 @@ declare namespace fastifyCompress {
78
79
  onUnsupportedRequestEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => Error | undefined | null;
79
80
  removeContentLengthHeader?: boolean;
80
81
  requestEncodings?: EncodingToken[];
82
+ syncThreshold?: number;
81
83
  threshold?: number;
82
84
  zlib?: unknown;
83
85
  zlibOptions?: ZlibOptions;
@@ -9,6 +9,7 @@ const stream = createReadStream('./package.json')
9
9
  const withGlobalOptions: FastifyCompressOptions = {
10
10
  global: true,
11
11
  threshold: 10,
12
+ syncThreshold: 32768,
12
13
  zlib,
13
14
  brotliOptions: {
14
15
  params: {
@@ -55,7 +56,8 @@ appWithoutGlobal.get('/one', {
55
56
  zlib: {
56
57
  createGzip: () => zlib.createGzip()
57
58
  },
58
- removeContentLengthHeader: false
59
+ removeContentLengthHeader: false,
60
+ syncThreshold: 0
59
61
  },
60
62
  decompress: {
61
63
  forceRequestEncoding: 'gzip',