From 7f34353b1758a3dedac3dc718b35d82d4602bd45 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 10:54:49 +0100 Subject: [PATCH 1/4] perf(index): convert unused capture groups to non-capture groups --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 72f30be..e0144a5 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(?:;|$)/ function processCompressParams (opts) { /* istanbul ignore next */ @@ -460,7 +460,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(/\*|x-gzip/gu, 'gzip') return encodingNegotiator.negotiate(header, encodings) } else { return undefined From 86ceff0f8133530faf6b69113123d27e91ca5813 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 10:55:11 +0100 Subject: [PATCH 2/4] test: add `u` unicode flag to regex --- test/global-compress.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From 4df738dcb646def73da870f182c270d01cb84431 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 10:56:27 +0100 Subject: [PATCH 3/4] chore(index): add `u` unicode flag to regex --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e0144a5..4512b78 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 */ From 73cfbad5e41eef17b9b0cb4649493503cc9a3d96 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 21 Oct 2023 15:33:42 +0100 Subject: [PATCH 4/4] perf(index): cache gzip alias regex --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4512b78..1e3024a 100644 --- a/index.js +++ b/index.js @@ -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/gu, 'gzip') + .replace(gzipAlias, 'gzip') return encodingNegotiator.negotiate(header, encodings) } else { return undefined