identifier.js 14 KB

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