Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(index): convert unused capture groups to non-capture groups #273

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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