Skip to content

Commit 5b47c92

Browse files
authoredMar 20, 2024
fix: improve perf (#1777)
1 parent 1a34bc4 commit 5b47c92

File tree

2 files changed

+73
-68
lines changed

2 files changed

+73
-68
lines changed
 

‎src/utils/getFilenameFromUrl.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ const getPaths = require("./getPaths");
1010
const cacheStore = new WeakMap();
1111

1212
/**
13+
* @template T
1314
* @param {Function} fn
14-
* @param {{ cache?: Map<any, any> }} [cache]
15+
* @param {{ cache?: Map<string, { data: T }> } | undefined} cache
16+
* @param {(value: T) => T} callback
1517
* @returns {any}
1618
*/
1719
// @ts-ignore
18-
const mem = (fn, { cache = new Map() } = {}) => {
20+
const mem = (fn, { cache = new Map() } = {}, callback) => {
1921
/**
2022
* @param {any} arguments_
2123
* @return {any}
@@ -28,7 +30,8 @@ const mem = (fn, { cache = new Map() } = {}) => {
2830
return cacheItem.data;
2931
}
3032

31-
const result = fn.apply(this, arguments_);
33+
let result = fn.apply(this, arguments_);
34+
result = callback(result);
3235

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

4245
return memoized;
4346
};
44-
const memoizedParse = mem(parse);
47+
// eslint-disable-next-line no-undefined
48+
const memoizedParse = mem(parse, undefined, (value) => {
49+
if (value.pathname) {
50+
// eslint-disable-next-line no-param-reassign
51+
value.pathname = decode(value.pathname);
52+
}
53+
54+
return value;
55+
});
4556

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

@@ -77,7 +88,9 @@ function getFilenameFromUrl(context, url, extra = {}) {
7788
const { options } = context;
7889
const paths = getPaths(context);
7990

91+
/** @type {string | undefined} */
8092
let foundFilename;
93+
/** @type {URL} */
8194
let urlObject;
8295

8396
try {
@@ -88,7 +101,9 @@ function getFilenameFromUrl(context, url, extra = {}) {
88101
}
89102

90103
for (const { publicPath, outputPath } of paths) {
104+
/** @type {string | undefined} */
91105
let filename;
106+
/** @type {URL} */
92107
let publicPathObject;
93108

94109
try {
@@ -102,8 +117,8 @@ function getFilenameFromUrl(context, url, extra = {}) {
102117
continue;
103118
}
104119

105-
const pathname = decode(urlObject.pathname);
106-
const publicPathPathname = decode(publicPathObject.pathname);
120+
const { pathname } = urlObject;
121+
const { pathname: publicPathPathname } = publicPathObject;
107122

108123
if (pathname && pathname.startsWith(publicPathPathname)) {
109124
// Null byte(s)

‎test/middleware.test.js

+52-62
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,39 @@ describe.each([
859859
},
860860
],
861861
},
862+
{
863+
file: "windows.txt",
864+
data: "windows.txt content",
865+
urls: [
866+
{
867+
value: "windows.txt",
868+
contentType: "text/plain; charset=utf-8",
869+
code: 200,
870+
},
871+
],
872+
},
873+
{
874+
file: "windows 2.txt",
875+
data: "windows 2.txt content",
876+
urls: [
877+
{
878+
value: "windows%202.txt",
879+
contentType: "text/plain; charset=utf-8",
880+
code: 200,
881+
},
882+
],
883+
},
884+
{
885+
file: "test & test & %20.txt",
886+
data: "test & test & %20.txt content",
887+
urls: [
888+
{
889+
value: "test%20%26%20test%20%26%20%2520.txt",
890+
contentType: "text/plain; charset=utf-8",
891+
code: 200,
892+
},
893+
],
894+
},
862895
];
863896

864897
const configurations = [
@@ -934,71 +967,28 @@ describe.each([
934967
},
935968
publicPathForRequest: "/",
936969
},
937-
];
938-
939-
const isWindows = process.platform === "win32";
940-
941-
if (isWindows) {
942-
fixtures.push(
943-
{
944-
file: "windows.txt",
945-
data: "windows.txt content",
946-
urls: [
947-
{
948-
value: "windows.txt",
949-
contentType: "text/plain; charset=utf-8",
950-
code: 200,
951-
},
952-
],
953-
},
954-
{
955-
file: "windows 2.txt",
956-
data: "windows 2.txt content",
957-
urls: [
958-
{
959-
value: "windows%202.txt",
960-
contentType: "text/plain; charset=utf-8",
961-
code: 200,
962-
},
963-
],
964-
},
965-
{
966-
file: "test & test & %20.txt",
967-
data: "test & test & %20.txt content",
968-
urls: [
969-
{
970-
value: "test%20%26%20test%20%26%20%2520.txt",
971-
contentType: "text/plain; charset=utf-8",
972-
code: 200,
973-
},
974-
],
975-
},
976-
);
977-
978-
configurations.push(
979-
{
980-
output: {
981-
path: path.join(basicOutputPath, "my static"),
982-
publicPath: "/static/",
983-
},
984-
publicPathForRequest: "/static/",
970+
{
971+
output: {
972+
path: path.join(basicOutputPath, "my static"),
973+
publicPath: "/static/",
985974
},
986-
{
987-
output: {
988-
path: path.join(basicOutputPath, "my%20static"),
989-
publicPath: "/static/",
990-
},
991-
publicPathForRequest: "/static/",
975+
publicPathForRequest: "/static/",
976+
},
977+
{
978+
output: {
979+
path: path.join(basicOutputPath, "my%20static"),
980+
publicPath: "/static/",
992981
},
993-
{
994-
output: {
995-
path: path.join(basicOutputPath, "my %20 static"),
996-
publicPath: "/my%20static/",
997-
},
998-
publicPathForRequest: "/my%20static/",
982+
publicPathForRequest: "/static/",
983+
},
984+
{
985+
output: {
986+
path: path.join(basicOutputPath, "my %20 static"),
987+
publicPath: "/my%20static/",
999988
},
1000-
);
1001-
}
989+
publicPathForRequest: "/my%20static/",
990+
},
991+
];
1002992

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

0 commit comments

Comments
 (0)