identifier.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const path = require("path");
  6. const WINDOWS_ABS_PATH_REGEXP = /^[a-zA-Z]:[\\/]/;
  7. const SEGMENTS_SPLIT_REGEXP = /([|!])/;
  8. const WINDOWS_PATH_SEPARATOR_REGEXP = /\\/g;
  9. /**
  10. * @param {string} relativePath relative path
  11. * @returns {string} request
  12. */
  13. const relativePathToRequest = relativePath => {
  14. if (relativePath === "") return "./.";
  15. if (relativePath === "..") return "../.";
  16. if (relativePath.startsWith("../")) return relativePath;
  17. return `./${relativePath}`;
  18. };
  19. /**
  20. * @param {string} context context for relative path
  21. * @param {string} maybeAbsolutePath path to make relative
  22. * @returns {string} relative path in request style
  23. */
  24. const absoluteToRequest = (context, maybeAbsolutePath) => {
  25. if (maybeAbsolutePath[0] === "/") {
  26. if (
  27. maybeAbsolutePath.length > 1 &&
  28. maybeAbsolutePath[maybeAbsolutePath.length - 1] === "/"
  29. ) {
  30. // this 'path' is actually a regexp generated by dynamic requires.
  31. // Don't treat it as an absolute path.
  32. return maybeAbsolutePath;
  33. }
  34. const querySplitPos = maybeAbsolutePath.indexOf("?");
  35. let resource =
  36. querySplitPos === -1
  37. ? maybeAbsolutePath
  38. : maybeAbsolutePath.slice(0, querySplitPos);
  39. resource = relativePathToRequest(path.posix.relative(context, resource));
  40. return querySplitPos === -1
  41. ? resource
  42. : resource + maybeAbsolutePath.slice(querySplitPos);
  43. }
  44. if (WINDOWS_ABS_PATH_REGEXP.test(maybeAbsolutePath)) {
  45. const querySplitPos = maybeAbsolutePath.indexOf("?");
  46. let resource =
  47. querySplitPos === -1
  48. ? maybeAbsolutePath
  49. : maybeAbsolutePath.slice(0, querySplitPos);
  50. resource = path.win32.relative(context, resource);
  51. if (!WINDOWS_ABS_PATH_REGEXP.test(resource)) {
  52. resource = relativePathToRequest(
  53. resource.replace(WINDOWS_PATH_SEPARATOR_REGEXP, "/")
  54. );
  55. }
  56. return querySplitPos === -1
  57. ? resource
  58. : resource + maybeAbsolutePath.slice(querySplitPos);
  59. }
  60. // not an absolute path
  61. return maybeAbsolutePath;
  62. };
  63. /**
  64. * @param {string} context context for relative path
  65. * @param {string} relativePath path
  66. * @returns {string} absolute path
  67. */
  68. const requestToAbsolute = (context, relativePath) => {
  69. if (relativePath.startsWith("./") || relativePath.startsWith("../"))
  70. return path.join(context, relativePath);
  71. return relativePath;
  72. };
  73. /** @typedef {EXPECTED_OBJECT} AssociatedObjectForCache */
  74. /**
  75. * @template T
  76. * @typedef {(value: string, cache?: AssociatedObjectForCache) => T} MakeCacheableResult
  77. */
  78. /**
  79. * @template T
  80. * @typedef {(value: string) => T} BindCacheResultFn
  81. */
  82. /**
  83. * @template T
  84. * @typedef {(cache: AssociatedObjectForCache) => BindCacheResultFn<T>} BindCache
  85. */
  86. /**
  87. * @template T
  88. * @param {((value: string) => T)} realFn real function
  89. * @returns {MakeCacheableResult<T> & { bindCache: BindCache<T> }} cacheable function
  90. */
  91. const makeCacheable = realFn => {
  92. /**
  93. * @template T
  94. * @typedef {Map<string, T>} CacheItem
  95. */
  96. /** @type {WeakMap<AssociatedObjectForCache, CacheItem<T>>} */
  97. const cache = new WeakMap();
  98. /**
  99. * @param {AssociatedObjectForCache} associatedObjectForCache an object to which the cache will be attached
  100. * @returns {CacheItem<T>} cache item
  101. */
  102. const getCache = associatedObjectForCache => {
  103. const entry = cache.get(associatedObjectForCache);
  104. if (entry !== undefined) return entry;
  105. /** @type {Map<string, T>} */
  106. const map = new Map();
  107. cache.set(associatedObjectForCache, map);
  108. return map;
  109. };
  110. /** @type {MakeCacheableResult<T> & { bindCache: BindCache<T> }} */
  111. const fn = (str, associatedObjectForCache) => {
  112. if (!associatedObjectForCache) return realFn(str);
  113. const cache = getCache(associatedObjectForCache);
  114. const entry = cache.get(str);
  115. if (entry !== undefined) return entry;
  116. const result = realFn(str);
  117. cache.set(str, result);
  118. return result;
  119. };
  120. /** @type {BindCache<T>} */
  121. fn.bindCache = associatedObjectForCache => {
  122. const cache = getCache(associatedObjectForCache);
  123. /**
  124. * @param {string} str string
  125. * @returns {T} value
  126. */
  127. return str => {
  128. const entry = cache.get(str);
  129. if (entry !== undefined) return entry;
  130. const result = realFn(str);
  131. cache.set(str, result);
  132. return result;
  133. };
  134. };
  135. return fn;
  136. };
  137. /** @typedef {(context: string, value: string, associatedObjectForCache?: AssociatedObjectForCache) => string} MakeCacheableWithContextResult */
  138. /** @typedef {(context: string, value: string) => string} BindCacheForContextResultFn */
  139. /** @typedef {(value: string) => string} BindContextCacheForContextResultFn */
  140. /** @typedef {(associatedObjectForCache?: AssociatedObjectForCache) => BindCacheForContextResultFn} BindCacheForContext */
  141. /** @typedef {(value: string, associatedObjectForCache?: AssociatedObjectForCache) => BindContextCacheForContextResultFn} BindContextCacheForContext */
  142. /**
  143. * @param {(context: string, identifier: string) => string} fn function
  144. * @returns {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} cacheable function with context
  145. */
  146. const makeCacheableWithContext = fn => {
  147. /** @type {WeakMap<AssociatedObjectForCache, Map<string, Map<string, string>>>} */
  148. const cache = new WeakMap();
  149. /** @type {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} */
  150. const cachedFn = (context, identifier, associatedObjectForCache) => {
  151. if (!associatedObjectForCache) return fn(context, identifier);
  152. let innerCache = cache.get(associatedObjectForCache);
  153. if (innerCache === undefined) {
  154. innerCache = new Map();
  155. cache.set(associatedObjectForCache, innerCache);
  156. }
  157. let cachedResult;
  158. let innerSubCache = innerCache.get(context);
  159. if (innerSubCache === undefined) {
  160. innerCache.set(context, (innerSubCache = new Map()));
  161. } else {
  162. cachedResult = innerSubCache.get(identifier);
  163. }
  164. if (cachedResult !== undefined) {
  165. return cachedResult;
  166. }
  167. const result = fn(context, identifier);
  168. innerSubCache.set(identifier, result);
  169. return result;
  170. };
  171. /** @type {BindCacheForContext} */
  172. cachedFn.bindCache = associatedObjectForCache => {
  173. let innerCache;
  174. if (associatedObjectForCache) {
  175. innerCache = cache.get(associatedObjectForCache);
  176. if (innerCache === undefined) {
  177. innerCache = new Map();
  178. cache.set(associatedObjectForCache, innerCache);
  179. }
  180. } else {
  181. innerCache = new Map();
  182. }
  183. /**
  184. * @param {string} context context used to create relative path
  185. * @param {string} identifier identifier used to create relative path
  186. * @returns {string} the returned relative path
  187. */
  188. const boundFn = (context, identifier) => {
  189. let cachedResult;
  190. let innerSubCache = innerCache.get(context);
  191. if (innerSubCache === undefined) {
  192. innerCache.set(context, (innerSubCache = new Map()));
  193. } else {
  194. cachedResult = innerSubCache.get(identifier);
  195. }
  196. if (cachedResult !== undefined) {
  197. return cachedResult;
  198. }
  199. const result = fn(context, identifier);
  200. innerSubCache.set(identifier, result);
  201. return result;
  202. };
  203. return boundFn;
  204. };
  205. /** @type {BindContextCacheForContext} */
  206. cachedFn.bindContextCache = (context, associatedObjectForCache) => {
  207. let innerSubCache;
  208. if (associatedObjectForCache) {
  209. let innerCache = cache.get(associatedObjectForCache);
  210. if (innerCache === undefined) {
  211. innerCache = new Map();
  212. cache.set(associatedObjectForCache, innerCache);
  213. }
  214. innerSubCache = innerCache.get(context);
  215. if (innerSubCache === undefined) {
  216. innerCache.set(context, (innerSubCache = new Map()));
  217. }
  218. } else {
  219. innerSubCache = new Map();
  220. }
  221. /**
  222. * @param {string} identifier identifier used to create relative path
  223. * @returns {string} the returned relative path
  224. */
  225. const boundFn = identifier => {
  226. const cachedResult = innerSubCache.get(identifier);
  227. if (cachedResult !== undefined) {
  228. return cachedResult;
  229. }
  230. const result = fn(context, identifier);
  231. innerSubCache.set(identifier, result);
  232. return result;
  233. };
  234. return boundFn;
  235. };
  236. return cachedFn;
  237. };
  238. /**
  239. * @param {string} context context for relative path
  240. * @param {string} identifier identifier for path
  241. * @returns {string} a converted relative path
  242. */
  243. const _makePathsRelative = (context, identifier) =>
  244. identifier
  245. .split(SEGMENTS_SPLIT_REGEXP)
  246. .map(str => absoluteToRequest(context, str))
  247. .join("");
  248. module.exports.makePathsRelative = makeCacheableWithContext(_makePathsRelative);
  249. /**
  250. * @param {string} context context for relative path
  251. * @param {string} identifier identifier for path
  252. * @returns {string} a converted relative path
  253. */
  254. const _makePathsAbsolute = (context, identifier) =>
  255. identifier
  256. .split(SEGMENTS_SPLIT_REGEXP)
  257. .map(str => requestToAbsolute(context, str))
  258. .join("");
  259. module.exports.makePathsAbsolute = makeCacheableWithContext(_makePathsAbsolute);
  260. /**
  261. * @param {string} context absolute context path
  262. * @param {string} request any request string may containing absolute paths, query string, etc.
  263. * @returns {string} a new request string avoiding absolute paths when possible
  264. */
  265. const _contextify = (context, request) =>
  266. request
  267. .split("!")
  268. .map(r => absoluteToRequest(context, r))
  269. .join("!");
  270. const contextify = makeCacheableWithContext(_contextify);
  271. module.exports.contextify = contextify;
  272. /**
  273. * @param {string} context absolute context path
  274. * @param {string} request any request string
  275. * @returns {string} a new request string using absolute paths when possible
  276. */
  277. const _absolutify = (context, request) =>
  278. request
  279. .split("!")
  280. .map(r => requestToAbsolute(context, r))
  281. .join("!");
  282. const absolutify = makeCacheableWithContext(_absolutify);
  283. module.exports.absolutify = absolutify;
  284. const PATH_QUERY_FRAGMENT_REGEXP =
  285. /^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
  286. const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
  287. /** @typedef {{ resource: string, path: string, query: string, fragment: string }} ParsedResource */
  288. /** @typedef {{ resource: string, path: string, query: string }} ParsedResourceWithoutFragment */
  289. /**
  290. * @param {string} str the path with query and fragment
  291. * @returns {ParsedResource} parsed parts
  292. */
  293. const _parseResource = str => {
  294. const match =
  295. /** @type {[string, string, string | undefined, string | undefined]} */
  296. (/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
  297. return {
  298. resource: str,
  299. path: match[1].replace(/\0(.)/g, "$1"),
  300. query: match[2] ? match[2].replace(/\0(.)/g, "$1") : "",
  301. fragment: match[3] || ""
  302. };
  303. };
  304. module.exports.parseResource = makeCacheable(_parseResource);
  305. /**
  306. * Parse resource, skips fragment part
  307. * @param {string} str the path with query and fragment
  308. * @returns {ParsedResourceWithoutFragment} parsed parts
  309. */
  310. const _parseResourceWithoutFragment = str => {
  311. const match =
  312. /** @type {[string, string, string | undefined]} */
  313. (/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
  314. return {
  315. resource: str,
  316. path: match[1].replace(/\0(.)/g, "$1"),
  317. query: match[2] ? match[2].replace(/\0(.)/g, "$1") : ""
  318. };
  319. };
  320. module.exports.parseResourceWithoutFragment = makeCacheable(
  321. _parseResourceWithoutFragment
  322. );
  323. /**
  324. * @param {string} filename the filename which should be undone
  325. * @param {string} outputPath the output path that is restored (only relevant when filename contains "..")
  326. * @param {boolean} enforceRelative true returns ./ for empty paths
  327. * @returns {string} repeated ../ to leave the directory of the provided filename to be back on output dir
  328. */
  329. module.exports.getUndoPath = (filename, outputPath, enforceRelative) => {
  330. let depth = -1;
  331. let append = "";
  332. outputPath = outputPath.replace(/[\\/]$/, "");
  333. for (const part of filename.split(/[/\\]+/)) {
  334. if (part === "..") {
  335. if (depth > -1) {
  336. depth--;
  337. } else {
  338. const i = outputPath.lastIndexOf("/");
  339. const j = outputPath.lastIndexOf("\\");
  340. const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
  341. if (pos < 0) return `${outputPath}/`;
  342. append = `${outputPath.slice(pos + 1)}/${append}`;
  343. outputPath = outputPath.slice(0, pos);
  344. }
  345. } else if (part !== ".") {
  346. depth++;
  347. }
  348. }
  349. return depth > 0
  350. ? `${"../".repeat(depth)}${append}`
  351. : enforceRelative
  352. ? `./${append}`
  353. : append;
  354. };