identifier.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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-z]:[\\/]/i;
  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. }
  72. return relativePath;
  73. };
  74. /** @typedef {EXPECTED_OBJECT} AssociatedObjectForCache */
  75. /**
  76. * @template T
  77. * @typedef {(value: string, cache?: AssociatedObjectForCache) => T} MakeCacheableResult
  78. */
  79. /**
  80. * @template T
  81. * @typedef {(value: string) => T} BindCacheResultFn
  82. */
  83. /**
  84. * @template T
  85. * @typedef {(cache: AssociatedObjectForCache) => BindCacheResultFn<T>} BindCache
  86. */
  87. /**
  88. * @template T
  89. * @param {((value: string) => T)} realFn real function
  90. * @returns {MakeCacheableResult<T> & { bindCache: BindCache<T> }} cacheable function
  91. */
  92. const makeCacheable = (realFn) => {
  93. /**
  94. * @template T
  95. * @typedef {Map<string, T>} CacheItem
  96. */
  97. /** @type {WeakMap<AssociatedObjectForCache, CacheItem<T>>} */
  98. const cache = new WeakMap();
  99. /**
  100. * @param {AssociatedObjectForCache} associatedObjectForCache an object to which the cache will be attached
  101. * @returns {CacheItem<T>} cache item
  102. */
  103. const getCache = (associatedObjectForCache) => {
  104. const entry = cache.get(associatedObjectForCache);
  105. if (entry !== undefined) return entry;
  106. /** @type {Map<string, T>} */
  107. const map = new Map();
  108. cache.set(associatedObjectForCache, map);
  109. return map;
  110. };
  111. /** @type {MakeCacheableResult<T> & { bindCache: BindCache<T> }} */
  112. const fn = (str, associatedObjectForCache) => {
  113. if (!associatedObjectForCache) return realFn(str);
  114. const cache = getCache(associatedObjectForCache);
  115. const entry = cache.get(str);
  116. if (entry !== undefined) return entry;
  117. const result = realFn(str);
  118. cache.set(str, result);
  119. return result;
  120. };
  121. /** @type {BindCache<T>} */
  122. fn.bindCache = (associatedObjectForCache) => {
  123. const cache = getCache(associatedObjectForCache);
  124. /**
  125. * @param {string} str string
  126. * @returns {T} value
  127. */
  128. return (str) => {
  129. const entry = cache.get(str);
  130. if (entry !== undefined) return entry;
  131. const result = realFn(str);
  132. cache.set(str, result);
  133. return result;
  134. };
  135. };
  136. return fn;
  137. };
  138. /** @typedef {(context: string, value: string, associatedObjectForCache?: AssociatedObjectForCache) => string} MakeCacheableWithContextResult */
  139. /** @typedef {(context: string, value: string) => string} BindCacheForContextResultFn */
  140. /** @typedef {(value: string) => string} BindContextCacheForContextResultFn */
  141. /** @typedef {(associatedObjectForCache?: AssociatedObjectForCache) => BindCacheForContextResultFn} BindCacheForContext */
  142. /** @typedef {(value: string, associatedObjectForCache?: AssociatedObjectForCache) => BindContextCacheForContextResultFn} BindContextCacheForContext */
  143. /**
  144. * @param {(context: string, identifier: string) => string} fn function
  145. * @returns {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} cacheable function with context
  146. */
  147. const makeCacheableWithContext = (fn) => {
  148. /** @typedef {Map<string, Map<string, string>>} InnerCache */
  149. /** @type {WeakMap<AssociatedObjectForCache, InnerCache>} */
  150. const cache = new WeakMap();
  151. /** @type {MakeCacheableWithContextResult & { bindCache: BindCacheForContext, bindContextCache: BindContextCacheForContext }} */
  152. const cachedFn = (context, identifier, associatedObjectForCache) => {
  153. if (!associatedObjectForCache) return fn(context, identifier);
  154. let innerCache = cache.get(associatedObjectForCache);
  155. if (innerCache === undefined) {
  156. innerCache = new Map();
  157. cache.set(associatedObjectForCache, innerCache);
  158. }
  159. /** @type {undefined | string} */
  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. /** @type {undefined | InnerCache} */
  177. let innerCache;
  178. if (associatedObjectForCache) {
  179. innerCache = cache.get(associatedObjectForCache);
  180. if (innerCache === undefined) {
  181. innerCache = new Map();
  182. cache.set(associatedObjectForCache, innerCache);
  183. }
  184. } else {
  185. innerCache = new Map();
  186. }
  187. /**
  188. * @param {string} context context used to create relative path
  189. * @param {string} identifier identifier used to create relative path
  190. * @returns {string} the returned relative path
  191. */
  192. const boundFn = (context, identifier) => {
  193. /** @type {undefined | string} */
  194. let cachedResult;
  195. let innerSubCache = innerCache.get(context);
  196. if (innerSubCache === undefined) {
  197. innerCache.set(context, (innerSubCache = new Map()));
  198. } else {
  199. cachedResult = innerSubCache.get(identifier);
  200. }
  201. if (cachedResult !== undefined) {
  202. return cachedResult;
  203. }
  204. const result = fn(context, identifier);
  205. innerSubCache.set(identifier, result);
  206. return result;
  207. };
  208. return boundFn;
  209. };
  210. /** @type {BindContextCacheForContext} */
  211. cachedFn.bindContextCache = (context, associatedObjectForCache) => {
  212. /** @type {undefined | Map<string, string>} */
  213. let innerSubCache;
  214. if (associatedObjectForCache) {
  215. let innerCache = cache.get(associatedObjectForCache);
  216. if (innerCache === undefined) {
  217. innerCache = new Map();
  218. cache.set(associatedObjectForCache, innerCache);
  219. }
  220. innerSubCache = innerCache.get(context);
  221. if (innerSubCache === undefined) {
  222. innerCache.set(context, (innerSubCache = new Map()));
  223. }
  224. } else {
  225. innerSubCache = new Map();
  226. }
  227. /**
  228. * @param {string} identifier identifier used to create relative path
  229. * @returns {string} the returned relative path
  230. */
  231. const boundFn = (identifier) => {
  232. const cachedResult = innerSubCache.get(identifier);
  233. if (cachedResult !== undefined) {
  234. return cachedResult;
  235. }
  236. const result = fn(context, identifier);
  237. innerSubCache.set(identifier, result);
  238. return result;
  239. };
  240. return boundFn;
  241. };
  242. return cachedFn;
  243. };
  244. /**
  245. * @param {string} context context for relative path
  246. * @param {string} identifier identifier for path
  247. * @returns {string} a converted relative path
  248. */
  249. const _makePathsRelative = (context, identifier) =>
  250. identifier
  251. .split(SEGMENTS_SPLIT_REGEXP)
  252. .map((str) => absoluteToRequest(context, str))
  253. .join("");
  254. /**
  255. * @param {string} context context for relative path
  256. * @param {string} identifier identifier for path
  257. * @returns {string} a converted relative path
  258. */
  259. const _makePathsAbsolute = (context, identifier) =>
  260. identifier
  261. .split(SEGMENTS_SPLIT_REGEXP)
  262. .map((str) => requestToAbsolute(context, str))
  263. .join("");
  264. /**
  265. * @param {string} context absolute context path
  266. * @param {string} request any request string may containing absolute paths, query string, etc.
  267. * @returns {string} a new request string avoiding absolute paths when possible
  268. */
  269. const _contextify = (context, request) =>
  270. request
  271. .split("!")
  272. .map((r) => absoluteToRequest(context, r))
  273. .join("!");
  274. const contextify = makeCacheableWithContext(_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. const PATH_QUERY_FRAGMENT_REGEXP =
  287. /^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
  288. const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
  289. const ZERO_ESCAPE_REGEXP = /\0(.)/g;
  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 firstEscape = str.indexOf("\0");
  298. // Handle `\0`
  299. if (firstEscape !== -1) {
  300. const match =
  301. /** @type {[string, string, string | undefined, string | undefined]} */
  302. (/** @type {unknown} */ (PATH_QUERY_FRAGMENT_REGEXP.exec(str)));
  303. return {
  304. resource: str,
  305. path: match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
  306. query: match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : "",
  307. fragment: match[3] || ""
  308. };
  309. }
  310. /** @type {ParsedResource} */
  311. const result = { resource: str, path: "", query: "", fragment: "" };
  312. const queryStart = str.indexOf("?");
  313. const fragmentStart = str.indexOf("#");
  314. if (fragmentStart < 0) {
  315. if (queryStart < 0) {
  316. result.path = result.resource;
  317. // No fragment, no query
  318. return result;
  319. }
  320. result.path = str.slice(0, queryStart);
  321. result.query = str.slice(queryStart);
  322. // Query, no fragment
  323. return result;
  324. }
  325. if (queryStart < 0 || fragmentStart < queryStart) {
  326. result.path = str.slice(0, fragmentStart);
  327. result.fragment = str.slice(fragmentStart);
  328. // Fragment, no query
  329. return result;
  330. }
  331. result.path = str.slice(0, queryStart);
  332. result.query = str.slice(queryStart, fragmentStart);
  333. result.fragment = str.slice(fragmentStart);
  334. // Query and fragment
  335. return result;
  336. };
  337. /**
  338. * Parse resource, skips fragment part
  339. * @param {string} str the path with query and fragment
  340. * @returns {ParsedResourceWithoutFragment} parsed parts
  341. */
  342. const _parseResourceWithoutFragment = (str) => {
  343. const firstEscape = str.indexOf("\0");
  344. // Handle `\0`
  345. if (firstEscape !== -1) {
  346. const match =
  347. /** @type {[string, string, string | undefined]} */
  348. (/** @type {unknown} */ (PATH_QUERY_REGEXP.exec(str)));
  349. return {
  350. resource: str,
  351. path: match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
  352. query: match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : ""
  353. };
  354. }
  355. /** @type {ParsedResourceWithoutFragment} */
  356. const result = { resource: str, path: "", query: "" };
  357. const queryStart = str.indexOf("?");
  358. if (queryStart < 0) {
  359. result.path = result.resource;
  360. // No query
  361. return result;
  362. }
  363. result.path = str.slice(0, queryStart);
  364. result.query = str.slice(queryStart);
  365. // Query
  366. return result;
  367. };
  368. /**
  369. * @param {string} filename the filename which should be undone
  370. * @param {string} outputPath the output path that is restored (only relevant when filename contains "..")
  371. * @param {boolean} enforceRelative true returns ./ for empty paths
  372. * @returns {string} repeated ../ to leave the directory of the provided filename to be back on output dir
  373. */
  374. const getUndoPath = (filename, outputPath, enforceRelative) => {
  375. let depth = -1;
  376. let append = "";
  377. outputPath = outputPath.replace(/[\\/]$/, "");
  378. for (const part of filename.split(/[/\\]+/)) {
  379. if (part === "..") {
  380. if (depth > -1) {
  381. depth--;
  382. } else {
  383. const i = outputPath.lastIndexOf("/");
  384. const j = outputPath.lastIndexOf("\\");
  385. const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
  386. if (pos < 0) return `${outputPath}/`;
  387. append = `${outputPath.slice(pos + 1)}/${append}`;
  388. outputPath = outputPath.slice(0, pos);
  389. }
  390. } else if (part !== ".") {
  391. depth++;
  392. }
  393. }
  394. return depth > 0
  395. ? `${"../".repeat(depth)}${append}`
  396. : enforceRelative
  397. ? `./${append}`
  398. : append;
  399. };
  400. module.exports.absolutify = absolutify;
  401. module.exports.contextify = contextify;
  402. module.exports.getUndoPath = getUndoPath;
  403. module.exports.makePathsAbsolute = makeCacheableWithContext(_makePathsAbsolute);
  404. module.exports.makePathsRelative = makeCacheableWithContext(_makePathsRelative);
  405. module.exports.parseResource = makeCacheable(_parseResource);
  406. module.exports.parseResourceWithoutFragment = makeCacheable(
  407. _parseResourceWithoutFragment
  408. );