Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
perf: cache regex literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Oct 29, 2023
1 parent e2a8a06 commit 84c7132
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
27 changes: 18 additions & 9 deletions src/routes/admin/access/bearer-token/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const {
accessPost,
} = require("./query");

// Cache immutable regex as they are expensive to create and garbage collect
const sqlLikePercRegex = /%/gu;
const sqlLikeUnderRegex = /_/gu;
const sqlLikeWildRegex = /\*/gu;
const keyDashRegex = /-/gu;

/**
* @author Frazer Smith
* @description Builds bearer token object from database result.
Expand Down Expand Up @@ -158,9 +164,9 @@ async function route(server, options) {
escSq`(LOWER(name) LIKE LOWER('${req.query[
"access.name"
]
.replace(/%/gu, "!%")
.replace(/_/gu, "!_")
.replace(/\*/gu, "%")}') ESCAPE '!')`
.replace(sqlLikePercRegex, "!%")
.replace(sqlLikeUnderRegex, "!_")
.replace(sqlLikeWildRegex, "%")}') ESCAPE '!')`
);
}

Expand All @@ -171,9 +177,9 @@ async function route(server, options) {
escSq`(LOWER(email) LIKE LOWER('${req.query[
"access.email"
]
.replace(/%/gu, "!%")
.replace(/_/gu, "!_")
.replace(/\*/gu, "%")}') ESCAPE '!')`
.replace(sqlLikePercRegex, "!%")
.replace(sqlLikeUnderRegex, "!_")
.replace(sqlLikeWildRegex, "%")}') ESCAPE '!')`
);
}

Expand All @@ -196,8 +202,11 @@ async function route(server, options) {
// _ and % act as wildcards in SQL LIKE clauses, so need to be escaped
whereArray.push(
escSq`(scopes LIKE '%${scopesValue
.replace(/%/gu, "!%")
.replace(/_/gu, "!_")}%' ESCAPE '!')`
.replace(sqlLikePercRegex, "!%")
.replace(
sqlLikeUnderRegex,
"!_"
)}%' ESCAPE '!')`
);
break;
}
Expand Down Expand Up @@ -351,7 +360,7 @@ async function route(server, options) {
* Underscores are also good as they allow for the whole token to be selected
* when double-clicking on it, as opposed to dashes
*/
const key = `ydhcc_${randomUUID().replace(/-/gu, "_")}`;
const key = `ydhcc_${randomUUID().replace(keyDashRegex, "_")}`;

const hash = await bcryptHash(key, 10);

Expand Down
17 changes: 11 additions & 6 deletions src/routes/contact/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const {
contactPut,
} = require("./query");

// Cache immutable regex as they are expensive to create and garbage collect
const sqlLikePercRegex = /%/gu;
const sqlLikeUnderRegex = /_/gu;
const sqlLikeWildRegex = /\*/gu;

/**
* @author Frazer Smith
* @description Builds contact object from database results.
Expand Down Expand Up @@ -219,9 +224,9 @@ async function route(server, options) {
escSq`(LOWER(match_value) LIKE LOWER('${req.query[
"match.value"
]
.replace(/%/gu, "!%")
.replace(/_/gu, "!_")
.replace(/\*/gu, "%")}') ESCAPE '!')`
.replace(sqlLikePercRegex, "!%")
.replace(sqlLikeUnderRegex, "!_")
.replace(sqlLikeWildRegex, "%")}') ESCAPE '!')`
);
}

Expand All @@ -232,9 +237,9 @@ async function route(server, options) {
escSq`(LOWER(match_receiver) LIKE LOWER('${req.query[
"match.receiver"
]
.replace(/%/gu, "!%")
.replace(/_/gu, "!_")
.replace(/\*/gu, "%")}') ESCAPE '!')`
.replace(sqlLikePercRegex, "!%")
.replace(sqlLikeUnderRegex, "!_")
.replace(sqlLikeWildRegex, "%")}') ESCAPE '!')`
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/routes/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const { docsGetSchema } = require("./schema");

const accepts = ["text/html"];

// Cache immutable regex as they are expensive to create and garbage collect
const pathRegex = /\/redoc\.standalone\.js(?:.map)?/u;

/**
* @author Frazer Smith
* @description Sets routing options for server.
Expand All @@ -31,8 +34,7 @@ async function route(server) {
"redoc",
"bundles"
),
allowedPath: (pathName) =>
pathName.match(/\/redoc\.standalone\.js(?:.map)?/u) !== null,
allowedPath: (pathName) => pathName.match(pathRegex) !== null,
decorateReply: false,
maxAge: "1 day",
prefix: "/redoc/",
Expand Down
5 changes: 4 additions & 1 deletion src/utils/escape-single-quotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

"use strict";

// Cache immutable regex as they are expensive to create and garbage collect
const quoteRegex = /'/gu;

/**
* @author Frazer Smith
* @description Tagged template function to replace single-quote characters
Expand All @@ -19,7 +22,7 @@ function escapeSingleQuote(strings, ...expressions) {
expressions.forEach((value, i) => {
result += raw[i];
result +=
typeof value === "string" ? value.replace(/'/gu, "''") : value;
typeof value === "string" ? value.replace(quoteRegex, "''") : value;
});

// Add last literal section
Expand Down

0 comments on commit 84c7132

Please sign in to comment.