-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathgetFilenameFromUrl.js
153 lines (125 loc) · 3.57 KB
/
getFilenameFromUrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const path = require("path");
const { parse } = require("url");
const querystring = require("querystring");
const getPaths = require("./getPaths");
/** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
/** @typedef {import("../index.js").ServerResponse} ServerResponse */
const cacheStore = new WeakMap();
/**
* @param {Function} fn
* @param {{ cache?: Map<any, any> }} [cache]
* @returns {any}
*/
// @ts-ignore
const mem = (fn, { cache = new Map() } = {}) => {
/**
* @param {any} arguments_
* @return {any}
*/
const memoized = (...arguments_) => {
const [key] = arguments_;
const cacheItem = cache.get(key);
if (cacheItem) {
return cacheItem.data;
}
const result = fn.apply(this, arguments_);
cache.set(key, {
data: result,
});
return result;
};
cacheStore.set(memoized, cache);
return memoized;
};
const memoizedParse = mem(parse);
/**
* @template {IncomingMessage} Request
* @template {ServerResponse} Response
* @param {import("../index.js").Context<Request, Response>} context
* @param {string} url
* @returns {string | undefined}
*/
function getFilenameFromUrl(context, url) {
const { options } = context;
const paths = getPaths(context);
let foundFilename;
let urlObject;
try {
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
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) {
// eslint-disable-next-line no-continue
continue;
}
if (
urlObject.pathname &&
urlObject.pathname.startsWith(publicPathObject.pathname)
) {
filename = outputPath;
// Strip the `pathname` property from the `publicPath` option from the start of requested url
// `/complex/foo.js` => `foo.js`
const pathname = urlObject.pathname.slice(
publicPathObject.pathname.length
);
if (pathname) {
filename = path.join(outputPath, querystring.unescape(pathname));
}
if (
options.historyApiFallback &&
!context.outputFileSystem.existsSync(filename)
) {
filename = path.join(outputPath);
}
let fsStats;
try {
fsStats =
/** @type {import("fs").statSync} */
(context.outputFileSystem.statSync)(filename);
} catch (_ignoreError) {
// eslint-disable-next-line no-continue
continue;
}
if (fsStats.isFile()) {
foundFilename = filename;
break;
} else if (
fsStats.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 {
fsStats =
/** @type {import("fs").statSync} */
(context.outputFileSystem.statSync)(filename);
} catch (__ignoreError) {
// eslint-disable-next-line no-continue
continue;
}
if (fsStats.isFile()) {
foundFilename = filename;
break;
}
}
}
}
// eslint-disable-next-line consistent-return
return foundFilename;
}
module.exports = getFilenameFromUrl;