Skip to content

Commit

Permalink
perf(index): convert unused capture groups to non-capture groups (#273)
Browse files Browse the repository at this point in the history
* perf(index): convert unused capture groups to non-capture groups

* test: add `u` unicode flag to regex

* chore(index): add `u` unicode flag to regex

* perf(index): cache gzip alias regex
  • Loading branch information
Fdawgs authored Oct 26, 2023
1 parent ae772a2 commit 670698a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -453,14 +453,16 @@ 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) {
header = header.toLowerCase()
// 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
Expand Down
6 changes: 3 additions & 3 deletions test/global-compress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 670698a

Please sign in to comment.