@fastify/compress 8.0.1 → 8.0.3

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.
@@ -251,7 +251,7 @@ describe('It should return the error returned by :', async () => {
251
251
 
252
252
  const fastify = Fastify()
253
253
  await fastify.register(compressPlugin, {
254
- onUnsupportedRequestEncoding (encoding, request) {
254
+ onUnsupportedRequestEncoding (encoding) {
255
255
  return {
256
256
  statusCode: 400,
257
257
  code: 'INVALID',
@@ -288,7 +288,7 @@ describe('It should return the error returned by :', async () => {
288
288
 
289
289
  const fastify = Fastify()
290
290
  await fastify.register(compressPlugin, {
291
- onInvalidRequestPayload (encoding, request, error) {
291
+ onInvalidRequestPayload (encoding, _request, error) {
292
292
  return {
293
293
  statusCode: 400,
294
294
  code: 'INVALID',
@@ -327,7 +327,7 @@ describe('It should return the default error :', async () => {
327
327
 
328
328
  const fastify = Fastify()
329
329
  await fastify.register(compressPlugin, {
330
- onUnsupportedRequestEncoding (encoding, request) {
330
+ onUnsupportedRequestEncoding () {
331
331
  throw new Error('Kaboom!')
332
332
  }
333
333
  })
@@ -359,7 +359,7 @@ describe('It should return the default error :', async () => {
359
359
 
360
360
  const fastify = Fastify()
361
361
  await fastify.register(compressPlugin, {
362
- onInvalidRequestPayload (encoding, request, error) {
362
+ onInvalidRequestPayload () {
363
363
  throw new Error('Kaboom!')
364
364
  }
365
365
  })
@@ -37,16 +37,16 @@ test('should not corrupt the file content', async (t) => {
37
37
  const fastify = new Fastify()
38
38
  t.after(() => fastify.close())
39
39
 
40
- fastify.register(async (instance, opts) => {
40
+ fastify.register(async (instance) => {
41
41
  await fastify.register(fastifyCompress)
42
42
  // compression
43
- instance.get('/compress', async (req, reply) => {
43
+ instance.get('/compress', async () => {
44
44
  return twoByteUnicodeContent
45
45
  })
46
46
  })
47
47
 
48
48
  // no compression
49
- fastify.get('/no-compress', async (req, reply) => {
49
+ fastify.get('/no-compress', async () => {
50
50
  return twoByteUnicodeContent
51
51
  })
52
52
 
@@ -6,7 +6,7 @@ const zlib = require('node:zlib')
6
6
  const Fastify = require('fastify')
7
7
  const compressPlugin = require('../index')
8
8
 
9
- describe('When using routes `compress` settings :', async (t) => {
9
+ describe('When using routes `compress` settings :', async () => {
10
10
  test('it should compress data using the route custom provided `createDeflate` method', async (t) => {
11
11
  t.plan(12)
12
12
  const equal = t.assert.equal
@@ -19,7 +19,7 @@ describe('When using routes `compress` settings :', async (t) => {
19
19
  const fastify = Fastify()
20
20
  await fastify.register(compressPlugin, { global: true, zlib: customZlibGlobal })
21
21
 
22
- fastify.get('/', (request, reply) => {
22
+ fastify.get('/', (_request, reply) => {
23
23
  reply
24
24
  .type('text/plain')
25
25
  .compress(createReadStream('./package.json'))
@@ -27,7 +27,7 @@ describe('When using routes `compress` settings :', async (t) => {
27
27
 
28
28
  fastify.get('/custom', {
29
29
  compress: { zlib: customZlib }
30
- }, (request, reply) => {
30
+ }, (_request, reply) => {
31
31
  reply
32
32
  .type('text/plain')
33
33
  .compress(createReadStream('./package.json'))
@@ -84,13 +84,13 @@ describe('When using routes `compress` settings :', async (t) => {
84
84
  const fastify = Fastify()
85
85
  await fastify.register(compressPlugin, { global: false, zlib: customZlibGlobal })
86
86
 
87
- fastify.get('/', (request, reply) => {
87
+ fastify.get('/', (_request, reply) => {
88
88
  reply
89
89
  .type('text/plain')
90
90
  .compress(createReadStream('./package.json'))
91
91
  })
92
92
 
93
- fastify.get('/custom', { compress: { zlib: customZlib } }, (request, reply) => {
93
+ fastify.get('/custom', { compress: { zlib: customZlib } }, (_request, reply) => {
94
94
  reply
95
95
  .type('text/plain')
96
96
  .compress(createReadStream('./package.json'))
@@ -143,7 +143,7 @@ describe('When using routes `compress` settings :', async (t) => {
143
143
  const fastify = Fastify()
144
144
  await fastify.register(compressPlugin, { global: false })
145
145
 
146
- fastify.get('/', (request, reply) => {
146
+ fastify.get('/', (_request, reply) => {
147
147
  // compress function should still be available
148
148
  t.assert.ok(typeof reply.compress === 'function')
149
149
 
@@ -152,7 +152,7 @@ describe('When using routes `compress` settings :', async (t) => {
152
152
 
153
153
  fastify.get('/custom', {
154
154
  compress: { zlib: customZlib }
155
- }, (request, reply) => {
155
+ }, (_request, reply) => {
156
156
  reply
157
157
  .type('text/plain')
158
158
  .compress(createReadStream('./package.json'))
@@ -160,7 +160,7 @@ describe('When using routes `compress` settings :', async (t) => {
160
160
 
161
161
  fastify.get('/standard', {
162
162
  compress: { threshold: 1 }
163
- }, (request, reply) => {
163
+ }, (_request, reply) => {
164
164
  reply.send({ foo: 1 })
165
165
  })
166
166
 
@@ -218,7 +218,7 @@ describe('When using routes `compress` settings :', async (t) => {
218
218
 
219
219
  fastify.get('/', {
220
220
  compress: false
221
- }, (request, reply) => {
221
+ }, (_request, reply) => {
222
222
  reply.send(content)
223
223
  })
224
224
 
@@ -245,7 +245,7 @@ describe('When using routes `compress` settings :', async (t) => {
245
245
  try {
246
246
  fastify.get('/', {
247
247
  compress: 'bad config'
248
- }, (request, reply) => {
248
+ }, (_request, reply) => {
249
249
  reply.send('')
250
250
  })
251
251
  } catch (err) {
@@ -264,7 +264,7 @@ describe('When `compress.removeContentLengthHeader` is `false`, it should not re
264
264
 
265
265
  fastify.get('/', {
266
266
  compress: { removeContentLengthHeader: false }
267
- }, (request, reply) => {
267
+ }, (_request, reply) => {
268
268
  readFile('./package.json', 'utf8', (err, data) => {
269
269
  if (err) {
270
270
  return reply.send(err)
@@ -301,7 +301,7 @@ describe('When `compress.removeContentLengthHeader` is `false`, it should not re
301
301
 
302
302
  fastify.get('/', {
303
303
  compress: { removeContentLengthHeader: false }
304
- }, (request, reply) => {
304
+ }, (_request, reply) => {
305
305
  readFile('./package.json', 'utf8', (err, data) => {
306
306
  if (err) {
307
307
  return reply.send(err)
@@ -343,7 +343,7 @@ describe('When using the old routes `{ config: compress }` option :', async () =
343
343
  const fastify = Fastify()
344
344
  await fastify.register(compressPlugin, { global: false, zlib: customZlibGlobal })
345
345
 
346
- fastify.get('/', (request, reply) => {
346
+ fastify.get('/', (_request, reply) => {
347
347
  reply
348
348
  .type('text/plain')
349
349
  .compress(createReadStream('./package.json'))
@@ -353,7 +353,7 @@ describe('When using the old routes `{ config: compress }` option :', async () =
353
353
  config: {
354
354
  compress: { zlib: customZlib }
355
355
  }
356
- }, (request, reply) => {
356
+ }, (_request, reply) => {
357
357
  reply
358
358
  .type('text/plain')
359
359
  .compress(createReadStream('./package.json'))
@@ -410,7 +410,7 @@ describe('When using the old routes `{ config: compress }` option :', async () =
410
410
  config: {
411
411
  compress: 'bad config'
412
412
  }
413
- }, (request, reply) => {
413
+ }, (_request, reply) => {
414
414
  reply.send('')
415
415
  })
416
416
  } catch (err) {
@@ -426,7 +426,7 @@ test('It should avoid to trigger `onSend` hook twice', async (t) => {
426
426
  await server.register(compressPlugin, { threshold: 0 })
427
427
 
428
428
  await server.register(async function (server) {
429
- server.get('/', async (request, _) => {
429
+ server.get('/', async () => {
430
430
  return { hi: true }
431
431
  })
432
432
  }, { prefix: '/test' })
@@ -18,7 +18,7 @@ function createPayload (compressor) {
18
18
  return payload
19
19
  }
20
20
 
21
- describe('When using routes `decompress` settings :', async (suite) => {
21
+ describe('When using routes `decompress` settings :', async () => {
22
22
  test('it should decompress data using the route custom provided `createInflate` method', async (t) => {
23
23
  t.plan(8)
24
24
  const equal = t.assert.equal
@@ -182,6 +182,31 @@ describe('When using routes `decompress` settings :', async (suite) => {
182
182
  })
183
183
  equal(usedCustomGlobal, false)
184
184
 
185
+ equal(response.statusCode, 400)
186
+ t.assert.match(response.json().message, /is not valid JSON/)
187
+ })
188
+
189
+ test('it should return FST_ERR_CTP_INVALID_CONTENT_LENGTH when Content-Length mismatches payload', async (t) => {
190
+ t.plan(2)
191
+ const equal = t.assert.equal
192
+
193
+ const fastify = Fastify()
194
+ await fastify.register(compressPlugin)
195
+
196
+ fastify.post('/length', (request, reply) => {
197
+ reply.send({ ok: true })
198
+ })
199
+
200
+ const response = await fastify.inject({
201
+ url: '/length',
202
+ method: 'POST',
203
+ headers: {
204
+ 'content-type': 'application/json',
205
+ 'content-length': '100',
206
+ },
207
+ payload: JSON.stringify({ hello: 'world' })
208
+ })
209
+
185
210
  equal(response.statusCode, 400)
186
211
  t.assert.deepEqual(response.json(), {
187
212
  statusCode: 400,
package/types/index.d.ts CHANGED
@@ -5,9 +5,9 @@ import {
5
5
  RouteOptions as FastifyRouteOptions,
6
6
  RawServerBase,
7
7
  RawServerDefault
8
- } from 'fastify';
9
- import { Stream } from 'stream';
10
- import { BrotliOptions, ZlibOptions } from 'zlib';
8
+ } from 'fastify'
9
+ import { Stream } from 'node:stream'
10
+ import { BrotliOptions, ZlibOptions } from 'node:zlib'
11
11
 
12
12
  declare module 'fastify' {
13
13
  export interface FastifyContextConfig {
@@ -18,6 +18,7 @@ declare module 'fastify' {
18
18
  }
19
19
 
20
20
  export interface RouteShorthandOptions<
21
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
21
22
  RawServer extends RawServerBase = RawServerDefault
22
23
  > {
23
24
  compress?: RouteCompressOptions | false;
@@ -56,9 +57,9 @@ type RouteDecompressOptions = Pick<fastifyCompress.FastifyCompressOptions,
56
57
  | 'zlib'
57
58
  >
58
59
 
59
- type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity';
60
+ type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity'
60
61
 
61
- type CompressibleContentTypeFunction = (contentType: string) => boolean;
62
+ type CompressibleContentTypeFunction = (contentType: string) => boolean
62
63
 
63
64
  type Input =
64
65
  | Buffer
@@ -66,7 +67,7 @@ type Input =
66
67
  | ArrayBuffer
67
68
  | string
68
69
  | Iterable<Buffer | string>
69
- | AsyncIterable<Buffer | string>;
70
+ | AsyncIterable<Buffer | string>
70
71
 
71
72
  declare namespace fastifyCompress {
72
73
 
@@ -105,5 +106,5 @@ declare namespace fastifyCompress {
105
106
  export { fastifyCompress as default }
106
107
  }
107
108
 
108
- declare function fastifyCompress(...params: Parameters<FastifyCompress>): ReturnType<FastifyCompress>
109
+ declare function fastifyCompress (...params: Parameters<FastifyCompress>): ReturnType<FastifyCompress>
109
110
  export = fastifyCompress
@@ -1,7 +1,7 @@
1
1
  import fastify, { FastifyInstance } from 'fastify'
2
- import { createReadStream } from 'fs'
2
+ import { createReadStream } from 'node:fs'
3
3
  import { expectError, expectType } from 'tsd'
4
- import * as zlib from 'zlib'
4
+ import * as zlib from 'node:zlib'
5
5
  import fastifyCompress, { FastifyCompressOptions } from '..'
6
6
 
7
7
  const stream = createReadStream('./package.json')
@@ -9,7 +9,7 @@ const stream = createReadStream('./package.json')
9
9
  const withGlobalOptions: FastifyCompressOptions = {
10
10
  global: true,
11
11
  threshold: 10,
12
- zlib: zlib,
12
+ zlib,
13
13
  brotliOptions: {
14
14
  params: {
15
15
  [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
@@ -32,11 +32,11 @@ app.register(fastifyCompress, {
32
32
  customTypes: value => value === 'application/json'
33
33
  })
34
34
 
35
- app.get('/test-one', async (request, reply) => {
35
+ app.get('/test-one', async (_request, reply) => {
36
36
  expectType<void>(reply.compress(stream))
37
37
  })
38
38
 
39
- app.get('/test-two', async (request, reply) => {
39
+ app.get('/test-two', async (_request, reply) => {
40
40
  expectError(reply.compress())
41
41
  })
42
42
 
@@ -57,7 +57,7 @@ appWithoutGlobal.get('/one', {
57
57
  createGunzip: () => zlib.createGunzip()
58
58
  }
59
59
  }
60
- }, (request, reply) => {
60
+ }, (_request, reply) => {
61
61
  expectType<void>(reply.type('text/plain').compress(stream))
62
62
  })
63
63
 
@@ -75,7 +75,7 @@ appWithoutGlobal.get('/two', {
75
75
  }
76
76
  }
77
77
  }
78
- }, (request, reply) => {
78
+ }, (_request, reply) => {
79
79
  expectType<void>(reply.type('text/plain').compress(stream))
80
80
  })
81
81
 
@@ -83,7 +83,7 @@ expectError(
83
83
  appWithoutGlobal.get('/throw-a-ts-arg-error-on-shorthand-route', {
84
84
  compress: 'bad compress route option value',
85
85
  decompress: 'bad decompress route option value'
86
- }, (request, reply) => {
86
+ }, (_request, reply) => {
87
87
  expectType<void>(reply.type('text/plain').compress(stream))
88
88
  })
89
89
  )
@@ -94,7 +94,7 @@ expectError(
94
94
  path: '/throw-a-ts-arg-error',
95
95
  compress: 'bad compress route option value',
96
96
  decompress: 'bad decompress route option value',
97
- handler: (request, reply) => { expectType<void>(reply.type('text/plain').compress(stream)) }
97
+ handler: (_request, reply) => { expectType<void>(reply.type('text/plain').compress(stream)) }
98
98
  })
99
99
  )
100
100