123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- "use strict";
- const path = require("path");
- const {
- parse
- } = require("url");
- const querystring = require("querystring");
- const getPaths = require("./getPaths");
- const cacheStore = new WeakMap();
- const mem = (fn, {
- cache = new Map()
- } = {}, callback) => {
-
- const memoized = (...arguments_) => {
- const [key] = arguments_;
- const cacheItem = cache.get(key);
- if (cacheItem) {
- return cacheItem.data;
- }
- let result = fn.apply(void 0, arguments_);
- result = callback(result);
- cache.set(key, {
- data: result
- });
- return result;
- };
- cacheStore.set(memoized, cache);
- return memoized;
- };
- const memoizedParse = mem(parse, undefined, value => {
- if (value.pathname) {
-
- value.pathname = decode(value.pathname);
- }
- return value;
- });
- const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
- function decode(input) {
- return querystring.unescape(input);
- }
- function getFilenameFromUrl(context, url, extra = {}) {
- const {
- options
- } = context;
- const paths = getPaths(context);
-
- let foundFilename;
-
- let urlObject;
- try {
-
- urlObject = memoizedParse(url, false, true);
- } catch (_ignoreError) {
- return;
- }
- for (const {
- publicPath,
- outputPath
- } of paths) {
-
- let filename;
-
- let publicPathObject;
- try {
- publicPathObject = memoizedParse(publicPath !== "auto" && publicPath ? publicPath : "/", false, true);
- } catch (_ignoreError) {
-
- continue;
- }
- const {
- pathname
- } = urlObject;
- const {
- pathname: publicPathPathname
- } = publicPathObject;
- if (pathname && pathname.startsWith(publicPathPathname)) {
-
- if (pathname.includes("\0")) {
-
- extra.errorCode = 400;
- return;
- }
- if (UP_PATH_REGEXP.test(path.normalize(`./${pathname}`))) {
-
- extra.errorCode = 403;
- return;
- }
-
-
-
- filename = path.join(outputPath, pathname.slice(publicPathPathname.length));
- try {
-
- extra.stats =
-
- context.outputFileSystem.statSync(filename);
- } catch (_ignoreError) {
-
- continue;
- }
- if (extra.stats.isFile()) {
- foundFilename = filename;
- break;
- } else if (extra.stats.isDirectory() && (typeof options.index === "undefined" || options.index)) {
- const indexValue = typeof options.index === "undefined" || typeof options.index === "boolean" ? "index.html" : options.index;
- filename = path.join(filename, indexValue);
- try {
-
- extra.stats =
-
- context.outputFileSystem.statSync(filename);
- } catch (__ignoreError) {
-
- continue;
- }
- if (extra.stats.isFile()) {
- foundFilename = filename;
- break;
- }
- }
- }
- }
- return foundFilename;
- }
- module.exports = getFilenameFromUrl;
|