identifier.js 12 KB

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