diff --git a/index.js b/index.js index 72f30be..1e3024a 100644 --- a/index.js +++ b/index.js @@ -108,7 +108,7 @@ function fastifyCompress (fastify, opts, next) { next() } -const defaultCompressibleTypes = /^text\/(?!event-stream)|(\+|\/)json(;|$)|(\+|\/)text(;|$)|(\+|\/)xml(;|$)|octet-stream(;|$)/ +const defaultCompressibleTypes = /^text\/(?!event-stream)|(?:\+|\/)json(?:;|$)|(?:\+|\/)text(?:;|$)|(?:\+|\/)xml(?:;|$)|octet-stream(?:;|$)/u function processCompressParams (opts) { /* istanbul ignore next */ @@ -453,6 +453,8 @@ function onDecompressError (request, params, encoding, error) { Object.assign(error, errorPayload) } +const gzipAlias = /\*|x-gzip/gu + function getEncodingHeader (encodings, request) { let header = request.headers['accept-encoding'] if (header != null) { @@ -460,7 +462,7 @@ function getEncodingHeader (encodings, request) { // consider the no-preference token as gzip for downstream compat // and x-gzip as an alias of gzip // ref.: [HTTP/1.1 RFC 7230 section 4.2.3](https://datatracker.ietf.org/doc/html/rfc7230#section-4.2.3) - .replace(/\*|x-gzip/g, 'gzip') + .replace(gzipAlias, 'gzip') return encodingNegotiator.negotiate(header, encodings) } else { return undefined diff --git a/test/global-compress.test.js b/test/global-compress.test.js index cc971e5..659e2b4 100644 --- a/test/global-compress.test.js +++ b/test/global-compress.test.js @@ -766,7 +766,7 @@ test('It should not compress :', async (t) => { t.plan(2) const fastify = Fastify() - await fastify.register(compressPlugin, { customTypes: /x-user-header$/ }) + await fastify.register(compressPlugin, { customTypes: /x-user-header$/u }) fastify.get('/', (request, reply) => { reply @@ -1101,7 +1101,7 @@ test('It should not compress :', async (t) => { t.plan(2) const fastify = Fastify() - await fastify.register(compressPlugin, { customTypes: /x-user-header$/ }) + await fastify.register(compressPlugin, { customTypes: /x-user-header$/u }) fastify.get('/', (request, reply) => { reply @@ -2445,7 +2445,7 @@ test('`Accept-Encoding` request header values :', async (t) => { test('It should compress data if `customTypes` is set and matches `Content-Type` reply header value', async (t) => { t.plan(2) const fastify = Fastify() - await fastify.register(compressPlugin, { customTypes: /x-user-header$/ }) + await fastify.register(compressPlugin, { customTypes: /x-user-header$/u }) fastify.get('/', (request, reply) => { reply