@fastify/compress 6.1.1 → 6.2.1

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.
package/index.js CHANGED
@@ -16,7 +16,7 @@ const { isStream, isGzip, isDeflate } = require('./lib/utils')
16
16
  const InvalidRequestEncodingError = createError('FST_CP_ERR_INVALID_CONTENT_ENCODING', 'Unsupported Content-Encoding: %s', 415)
17
17
  const InvalidRequestCompressedPayloadError = createError('FST_CP_ERR_INVALID_CONTENT', 'Could not decompress the request payload using the provided encoding', 400)
18
18
 
19
- function compressPlugin (fastify, opts, next) {
19
+ function fastifyCompress (fastify, opts, next) {
20
20
  const globalCompressParams = processCompressParams(opts)
21
21
  const globalDecompressParams = processDecompressParams(opts)
22
22
 
@@ -142,7 +142,7 @@ function processCompressParams (opts) {
142
142
  params.encodings = Array.isArray(opts.encodings)
143
143
  ? supportedEncodings
144
144
  .filter(encoding => opts.encodings.includes(encoding))
145
- .sort((a, b) => opts.encodings.indexOf(a) - supportedEncodings.indexOf(b))
145
+ .sort((a, b) => opts.encodings.indexOf(a) - opts.encodings.indexOf(b))
146
146
  : supportedEncodings
147
147
 
148
148
  return params
@@ -174,7 +174,7 @@ function processDecompressParams (opts) {
174
174
  params.encodings = Array.isArray(opts.requestEncodings)
175
175
  ? supportedEncodings
176
176
  .filter(encoding => opts.requestEncodings.includes(encoding))
177
- .sort((a, b) => opts.requestEncodings.indexOf(a) - supportedEncodings.indexOf(b))
177
+ .sort((a, b) => opts.requestEncodings.indexOf(a) - opts.requestEncodings.indexOf(b))
178
178
  : supportedEncodings
179
179
 
180
180
  if (opts.forceRequestEncoding) {
@@ -548,7 +548,9 @@ function createError (code, message, statusCode) {
548
548
  return FastifyCompressError
549
549
  }
550
550
 
551
- module.exports = fp(compressPlugin, {
551
+ module.exports = fp(fastifyCompress, {
552
552
  fastify: '4.x',
553
553
  name: '@fastify/compress'
554
554
  })
555
+ module.exports.default = fastifyCompress
556
+ module.exports.fastifyCompress = fastifyCompress
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@fastify/compress",
3
- "version": "6.1.1",
3
+ "version": "6.2.1",
4
4
  "description": "Fastify compression utils",
5
5
  "main": "index.js",
6
- "types": "index.d.ts",
6
+ "types": "types/index.d.ts",
7
7
  "dependencies": {
8
8
  "@fastify/accept-negotiator": "^1.0.0",
9
9
  "fastify-plugin": "^4.0.0",
10
10
  "into-stream": "^6.0.0",
11
11
  "mime-db": "^1.51.0",
12
- "minipass": "^3.1.6",
12
+ "minipass": "^4.0.0",
13
13
  "peek-stream": "^1.1.3",
14
14
  "pump": "^3.0.0",
15
15
  "pumpify": "^2.0.1"
@@ -24,8 +24,8 @@
24
24
  "jsonstream": "^1.0.3",
25
25
  "standard": "^17.0.0",
26
26
  "tap": "^16.0.0",
27
- "tsd": "^0.22.0",
28
- "typescript": "^4.5.4"
27
+ "tsd": "^0.28.0",
28
+ "typescript": "^5.0.2"
29
29
  },
30
30
  "scripts": {
31
31
  "coverage": "npm run unit -- --cov",
@@ -2703,7 +2703,7 @@ test('It should sort and follow custom `encodings` options', async (t) => {
2703
2703
  const fastify = Fastify()
2704
2704
  await fastify.register(compressPlugin, {
2705
2705
  global: true,
2706
- encodings: ['gzip', 'br']
2706
+ encodings: ['br', 'gzip']
2707
2707
  })
2708
2708
 
2709
2709
  fastify.get('/', (request, reply) => {
@@ -2725,6 +2725,35 @@ test('It should sort and follow custom `encodings` options', async (t) => {
2725
2725
  t.equal(payload.toString('utf-8'), file)
2726
2726
  })
2727
2727
 
2728
+ test('It should sort and prefer the order of custom `encodings` options', async (t) => {
2729
+ t.plan(2)
2730
+
2731
+ const fastify = Fastify()
2732
+ await fastify.register(compressPlugin, {
2733
+ global: true,
2734
+ encodings: ['gzip', 'deflate', 'br']
2735
+ })
2736
+
2737
+ fastify.get('/', (request, reply) => {
2738
+ reply
2739
+ .type('text/plain')
2740
+ .compress(createReadStream('./package.json'))
2741
+ })
2742
+
2743
+ const response = await fastify.inject({
2744
+ url: '/',
2745
+ method: 'GET',
2746
+ headers: {
2747
+ 'accept-encoding': 'hello,gzip,br'
2748
+ }
2749
+ })
2750
+
2751
+ const file = readFileSync('./package.json', 'utf8')
2752
+ const payload = zlib.gunzipSync(response.rawPayload)
2753
+ t.equal(response.headers['content-encoding'], 'gzip')
2754
+ t.equal(payload.toString('utf-8'), file)
2755
+ })
2756
+
2728
2757
  test('It should sort and follow custom `requestEncodings` options', async (t) => {
2729
2758
  t.plan(2)
2730
2759
 
@@ -0,0 +1,100 @@
1
+ import {
2
+ FastifyPluginCallback,
3
+ FastifyReply,
4
+ FastifyRequest,
5
+ RawServerBase,
6
+ RawServerDefault,
7
+ RouteOptions as FastifyRouteOptions
8
+ } from 'fastify'
9
+ import { Input, InputObject } from 'into-stream'
10
+ import { Stream } from 'stream'
11
+ import { BrotliOptions, ZlibOptions } from 'zlib'
12
+
13
+ declare module 'fastify' {
14
+ export interface FastifyContextConfig {
15
+ /** @deprecated `config.compress` is deprecated, use `compress` shorthand option instead */
16
+ compress?: RouteCompressOptions | false;
17
+ /** @deprecated `config.decompress` is deprecated, use `decompress` shorthand option instead */
18
+ decompress?: RouteDecompressOptions | false;
19
+ }
20
+
21
+ export interface RouteShorthandOptions<
22
+ RawServer extends RawServerBase = RawServerDefault
23
+ > {
24
+ compress?: RouteCompressOptions | false;
25
+ decompress?: RouteDecompressOptions | false;
26
+ }
27
+
28
+ interface FastifyReply {
29
+ compress(input: Stream | Input | InputObject): void;
30
+ }
31
+
32
+ export interface RouteOptions {
33
+ compress?: RouteCompressOptions | false;
34
+ decompress?: RouteDecompressOptions | false;
35
+ }
36
+ }
37
+
38
+ type FastifyCompress = FastifyPluginCallback<fastifyCompress.FastifyCompressOptions>
39
+
40
+ type RouteCompressOptions = Pick<fastifyCompress.FastifyCompressOptions,
41
+ | 'brotliOptions'
42
+ | 'customTypes'
43
+ | 'encodings'
44
+ | 'inflateIfDeflated'
45
+ | 'onUnsupportedEncoding'
46
+ | 'removeContentLengthHeader'
47
+ | 'threshold'
48
+ | 'zlib'
49
+ | 'zlibOptions'
50
+ >
51
+
52
+ type RouteDecompressOptions = Pick<fastifyCompress.FastifyCompressOptions,
53
+ | 'forceRequestEncoding'
54
+ | 'onInvalidRequestPayload'
55
+ | 'onUnsupportedRequestEncoding'
56
+ | 'requestEncodings'
57
+ | 'zlib'
58
+ >
59
+
60
+ type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity';
61
+
62
+ declare namespace fastifyCompress {
63
+
64
+ export interface FastifyCompressOptions {
65
+ brotliOptions?: BrotliOptions;
66
+ customTypes?: RegExp;
67
+ encodings?: EncodingToken[];
68
+ forceRequestEncoding?: EncodingToken;
69
+ global?: boolean;
70
+ inflateIfDeflated?: boolean;
71
+ onInvalidRequestPayload?: (encoding: string, request: FastifyRequest, error: Error) => Error | undefined | null;
72
+ onUnsupportedEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => string | Buffer | Stream;
73
+ onUnsupportedRequestEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => Error | undefined | null;
74
+ removeContentLengthHeader?: boolean;
75
+ requestEncodings?: EncodingToken[];
76
+ threshold?: number;
77
+ zlib?: unknown;
78
+ zlibOptions?: ZlibOptions;
79
+ }
80
+
81
+ export interface FastifyCompressRouteOptions {
82
+ compress?: RouteCompressOptions | false;
83
+ decompress?: RouteDecompressOptions | false;
84
+ }
85
+
86
+ export interface RouteOptions extends FastifyRouteOptions, FastifyCompressRouteOptions { }
87
+
88
+ export interface RoutesConfigCompressOptions {
89
+ /** @deprecated `config.compress` is deprecated, use `compress` shorthand option instead */
90
+ compress?: RouteCompressOptions | false;
91
+ /** @deprecated `config.decompress` is deprecated, use `decompress` shorthand option instead */
92
+ decompress?: RouteDecompressOptions | false;
93
+ }
94
+
95
+ export const fastifyCompress: FastifyCompress
96
+ export { fastifyCompress as default }
97
+ }
98
+
99
+ declare function fastifyCompress(...params: Parameters<FastifyCompress>): ReturnType<FastifyCompress>
100
+ export = fastifyCompress
@@ -2,7 +2,7 @@ import fastify, { FastifyInstance } from 'fastify'
2
2
  import { createReadStream } from 'fs'
3
3
  import { expectError, expectType } from 'tsd'
4
4
  import * as zlib from 'zlib'
5
- import fastifyCompress, { FastifyCompressOptions } from '../..'
5
+ import fastifyCompress, { FastifyCompressOptions } from '..'
6
6
 
7
7
  const stream = createReadStream('./package.json')
8
8
 
package/index.d.ts DELETED
@@ -1,81 +0,0 @@
1
- import {
2
- FastifyPluginCallback,
3
- FastifyReply,
4
- FastifyRequest,
5
- RawServerBase,
6
- RawServerDefault
7
- } from 'fastify'
8
- import * as fastify from 'fastify'
9
- import { Input, InputObject } from 'into-stream'
10
- import { Stream } from 'stream'
11
- import { BrotliOptions, ZlibOptions } from 'zlib'
12
-
13
- type EncodingToken = 'br' | 'deflate' | 'gzip' | 'identity';
14
-
15
- export interface FastifyCompressOptions {
16
- brotliOptions?: BrotliOptions;
17
- customTypes?: RegExp;
18
- encodings?: EncodingToken[];
19
- forceRequestEncoding?: EncodingToken;
20
- global?: boolean;
21
- inflateIfDeflated?: boolean;
22
- onInvalidRequestPayload?: (encoding: string, request: FastifyRequest, error: Error) => Error | undefined | null;
23
- onUnsupportedEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => string | Buffer | Stream;
24
- onUnsupportedRequestEncoding?: (encoding: string, request: FastifyRequest, reply: FastifyReply) => Error | undefined | null;
25
- removeContentLengthHeader?: boolean;
26
- requestEncodings?: EncodingToken[];
27
- threshold?: number;
28
- zlib?: unknown;
29
- zlibOptions?: ZlibOptions;
30
- }
31
-
32
- interface RouteCompressOptions extends Pick<FastifyCompressOptions,
33
- | 'brotliOptions'
34
- | 'customTypes'
35
- | 'encodings'
36
- | 'inflateIfDeflated'
37
- | 'onUnsupportedEncoding'
38
- | 'removeContentLengthHeader'
39
- | 'threshold'
40
- | 'zlib'
41
- | 'zlibOptions'
42
- > {}
43
-
44
- interface RouteDecompressOptions extends Pick<FastifyCompressOptions,
45
- | 'forceRequestEncoding'
46
- | 'onInvalidRequestPayload'
47
- | 'onUnsupportedRequestEncoding'
48
- | 'requestEncodings'
49
- | 'zlib'
50
- > {}
51
-
52
- interface FastifyCompressRouteOptions {
53
- compress?: RouteCompressOptions | false;
54
- decompress?: RouteDecompressOptions | false;
55
- }
56
-
57
- export interface RouteOptions extends fastify.RouteOptions, FastifyCompressRouteOptions {}
58
-
59
- export interface RoutesConfigCompressOptions {
60
- /** @deprecated `config.compress` is deprecated, use `compress` shorthand option instead */
61
- compress?: RouteCompressOptions | false;
62
- /** @deprecated `config.decompress` is deprecated, use `decompress` shorthand option instead */
63
- decompress?: RouteDecompressOptions | false;
64
- }
65
-
66
- declare module 'fastify' {
67
- export interface FastifyContextConfig extends RoutesConfigCompressOptions {}
68
-
69
- export interface RouteShorthandOptions<
70
- RawServer extends RawServerBase = RawServerDefault
71
- > extends FastifyCompressRouteOptions {}
72
-
73
- interface FastifyReply {
74
- compress(input: Stream | Input | InputObject): void;
75
- }
76
-
77
- export interface RouteOptions extends FastifyCompressRouteOptions {}
78
- }
79
-
80
- declare const fastifyCompress: FastifyPluginCallback<FastifyCompressOptions>
81
- export default fastifyCompress