Skip to content

Commit

Permalink
fix: improve perf (#1777)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Mar 20, 2024
1 parent 1a34bc4 commit 5b47c92
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
27 changes: 21 additions & 6 deletions src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const getPaths = require("./getPaths");
const cacheStore = new WeakMap();

/**
* @template T
* @param {Function} fn
* @param {{ cache?: Map<any, any> }} [cache]
* @param {{ cache?: Map<string, { data: T }> } | undefined} cache
* @param {(value: T) => T} callback
* @returns {any}
*/
// @ts-ignore
const mem = (fn, { cache = new Map() } = {}) => {
const mem = (fn, { cache = new Map() } = {}, callback) => {
/**
* @param {any} arguments_
* @return {any}
Expand All @@ -28,7 +30,8 @@ const mem = (fn, { cache = new Map() } = {}) => {
return cacheItem.data;
}

const result = fn.apply(this, arguments_);
let result = fn.apply(this, arguments_);
result = callback(result);

cache.set(key, {
data: result,
Expand All @@ -41,7 +44,15 @@ const mem = (fn, { cache = new Map() } = {}) => {

return memoized;
};
const memoizedParse = mem(parse);
// eslint-disable-next-line no-undefined
const memoizedParse = mem(parse, undefined, (value) => {
if (value.pathname) {
// eslint-disable-next-line no-param-reassign
value.pathname = decode(value.pathname);
}

return value;
});

const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;

Expand Down Expand Up @@ -77,7 +88,9 @@ function getFilenameFromUrl(context, url, extra = {}) {
const { options } = context;
const paths = getPaths(context);

/** @type {string | undefined} */
let foundFilename;
/** @type {URL} */
let urlObject;

try {
Expand All @@ -88,7 +101,9 @@ function getFilenameFromUrl(context, url, extra = {}) {
}

for (const { publicPath, outputPath } of paths) {
/** @type {string | undefined} */
let filename;
/** @type {URL} */
let publicPathObject;

try {
Expand All @@ -102,8 +117,8 @@ function getFilenameFromUrl(context, url, extra = {}) {
continue;
}

const pathname = decode(urlObject.pathname);
const publicPathPathname = decode(publicPathObject.pathname);
const { pathname } = urlObject;
const { pathname: publicPathPathname } = publicPathObject;

if (pathname && pathname.startsWith(publicPathPathname)) {
// Null byte(s)
Expand Down
114 changes: 52 additions & 62 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,39 @@ describe.each([
},
],
},
{
file: "windows.txt",
data: "windows.txt content",
urls: [
{
value: "windows.txt",
contentType: "text/plain; charset=utf-8",
code: 200,
},
],
},
{
file: "windows 2.txt",
data: "windows 2.txt content",
urls: [
{
value: "windows%202.txt",
contentType: "text/plain; charset=utf-8",
code: 200,
},
],
},
{
file: "test & test & %20.txt",
data: "test & test & %20.txt content",
urls: [
{
value: "test%20%26%20test%20%26%20%2520.txt",
contentType: "text/plain; charset=utf-8",
code: 200,
},
],
},
];

const configurations = [
Expand Down Expand Up @@ -934,71 +967,28 @@ describe.each([
},
publicPathForRequest: "/",
},
];

const isWindows = process.platform === "win32";

if (isWindows) {
fixtures.push(
{
file: "windows.txt",
data: "windows.txt content",
urls: [
{
value: "windows.txt",
contentType: "text/plain; charset=utf-8",
code: 200,
},
],
},
{
file: "windows 2.txt",
data: "windows 2.txt content",
urls: [
{
value: "windows%202.txt",
contentType: "text/plain; charset=utf-8",
code: 200,
},
],
},
{
file: "test & test & %20.txt",
data: "test & test & %20.txt content",
urls: [
{
value: "test%20%26%20test%20%26%20%2520.txt",
contentType: "text/plain; charset=utf-8",
code: 200,
},
],
},
);

configurations.push(
{
output: {
path: path.join(basicOutputPath, "my static"),
publicPath: "/static/",
},
publicPathForRequest: "/static/",
{
output: {
path: path.join(basicOutputPath, "my static"),
publicPath: "/static/",
},
{
output: {
path: path.join(basicOutputPath, "my%20static"),
publicPath: "/static/",
},
publicPathForRequest: "/static/",
publicPathForRequest: "/static/",
},
{
output: {
path: path.join(basicOutputPath, "my%20static"),
publicPath: "/static/",
},
{
output: {
path: path.join(basicOutputPath, "my %20 static"),
publicPath: "/my%20static/",
},
publicPathForRequest: "/my%20static/",
publicPathForRequest: "/static/",
},
{
output: {
path: path.join(basicOutputPath, "my %20 static"),
publicPath: "/my%20static/",
},
);
}
publicPathForRequest: "/my%20static/",
},
];

for (const configuration of configurations) {
// eslint-disable-next-line no-loop-func
Expand Down

0 comments on commit 5b47c92

Please sign in to comment.