Skip to content
Merged
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
32 changes: 10 additions & 22 deletions src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ import path from "node:path";
import querystring from "node:querystring";

import getPaths from "./getPaths.js";
import memorize from "./memorize.js";

/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
/** @typedef {import("../index.js").OutputFileSystem} OutputFileSystem */
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
/** @typedef {import("fs").Stats} FSStats */

/**
* @param {string} input input
* @returns {string} unescape input
* @param {string} url url
* @returns {string} decoded pathname
*/
function decode(input) {
return querystring.unescape(input);
function getPathnameFromUrl(url) {
return querystring.unescape(new URL(url, "http://localhost").pathname);
}

const memoizedParse = memorize((url) => {
const urlObject = new URL(url, "http://localhost");

// We can't change pathname in URL directly because it won't decode correctly.
return { ...urlObject, pathname: decode(urlObject.pathname) };
});

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

/**
Expand Down Expand Up @@ -88,12 +80,11 @@ function getFilenameFromUrl(context, url) {
? ["index.html"]
: [options.index];

/** @type {URL} */
let urlObject;
/** @type {string} */
let pathname;

try {
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
urlObject = memoizedParse(url);
pathname = getPathnameFromUrl(url);
} catch {
return;
}
Expand All @@ -106,20 +97,17 @@ function getFilenameFromUrl(context, url) {
} of paths) {
/** @type {string | undefined} */
let filename;
/** @type {URL} */
let publicPathObject;
/** @type {string} */
let publicPathPathname;

try {
publicPathObject = memoizedParse(
publicPathPathname = getPathnameFromUrl(
publicPath !== "auto" && publicPath ? publicPath : "/",
);
} catch {
continue;
}

const { pathname } = urlObject;
const { pathname: publicPathPathname } = publicPathObject;

if (
pathname &&
publicPathPathname &&
Expand Down
Loading