identifier.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. const makeCacheable = realFn => {
  78. /** @type {WeakMap<object, Map<string, ParsedResource>>} */
  79. const cache = new WeakMap();
  80. const getCache = associatedObjectForCache => {
  81. const entry = cache.get(associatedObjectForCache);
  82. if (entry !== undefined) return entry;
  83. /** @type {Map<string, ParsedResource>} */
  84. const map = new Map();
  85. cache.set(associatedObjectForCache, map);
  86. return map;
  87. };
  88. /**
  89. * @param {string} str the path with query and fragment
  90. * @param {object=} associatedObjectForCache an object to which the cache will be attached
  91. * @returns {ParsedResource} parsed parts
  92. */
  93. const fn = (str, associatedObjectForCache) => {
  94. if (!associatedObjectForCache) return realFn(str);
  95. const cache = getCache(associatedObjectForCache);
  96. const entry = cache.get(str);
  97. if (entry !== undefined) return entry;
  98. const result = realFn(str);
  99. cache.set(str, result);
  100. return result;
  101. };
  102. fn.bindCache = associatedObjectForCache => {
  103. const cache = getCache(associatedObjectForCache);
  104. return str => {
  105. const entry = cache.get(str);
  106. if (entry !== undefined) return entry;
  107. const result = realFn(str);
  108. cache.set(str, result);
  109. return result;
  110. };
  111. };
  112. return fn;
  113. };
  114. const makeCacheableWithContext = fn => {
  115. /** @type {WeakMap<object, Map<string, Map<string, string>>>} */
  116. const cache = new WeakMap();
  117. /**
  118. * @param {string} context context used to create relative path
  119. * @param {string} identifier identifier used to create relative path
  120. * @param {object=} associatedObjectForCache an object to which the cache will be attached
  121. * @returns {string} the returned relative path
  122. */
  123. const cachedFn = (context, identifier, associatedObjectForCache) => {
  124. if (!associatedObjectForCache) return fn(context, identifier);
  125. let innerCache = cache.get(associatedObjectForCache);
  126. if (innerCache === undefined) {
  127. innerCache = new Map();
  128. cache.set(associatedObjectForCache, innerCache);
  129. }
  130. let cachedResult;
  131. let innerSubCache = innerCache.get(context);
  132. if (innerSubCache === undefined) {
  133. innerCache.set(context, (innerSubCache = new Map()));
  134. } else {
  135. cachedResult = innerSubCache.get(identifier);
  136. }
  137. if (cachedResult !== undefined) {
  138. return cachedResult;
  139. } else {
  140. const result = fn(context, identifier);
  141. innerSubCache.set(identifier, result);
  142. return result;
  143. }
  144. };
  145. /**
  146. * @param {object=} associatedObjectForCache an object to which the cache will be attached
  147. * @returns {function(string, string): string} cached function
  148. */
  149. cachedFn.bindCache = associatedObjectForCache => {
  150. let innerCache;
  151. if (associatedObjectForCache) {
  152. innerCache = cache.get(associatedObjectForCache);
  153. if (innerCache === undefined) {
  154. innerCache = new Map();
  155. cache.set(associatedObjectForCache, innerCache);
  156. }
  157. } else {
  158. innerCache = new Map();
  159. }
  160. /**
  161. * @param {string} context context used to create relative path
  162. * @param {string} identifier identifier used to create relative path
  163. * @returns {string} the returned relative path
  164. */
  165. const boundFn = (context, identifier) => {
  166. let cachedResult;
  167. let innerSubCache = innerCache.get(context);
  168. if (innerSubCache === undefined) {
  169. innerCache.set(context, (innerSubCache = new Map()));
  170. } else {
  171. cachedResult = innerSubCache.get(identifier);
  172. }
  173. if (cachedResult !== undefined) {
  174. return cachedResult;
  175. } else {
  176. const result = fn(context, identifier);
  177. innerSubCache.set(identifier, result);
  178. return result;
  179. }
  180. };
  181. return boundFn;
  182. };
  183. /**
  184. * @param {string} context context used to create relative path
  185. * @param {object=} associatedObjectForCache an object to which the cache will be attached
  186. * @returns {function(string): string} cached function
  187. */
  188. cachedFn.bindContextCache = (context, associatedObjectForCache) => {
  189. let innerSubCache;
  190. if (associatedObjectForCache) {
  191. let innerCache = cache.get(associatedObjectForCache);
  192. if (innerCache === undefined) {
  193. innerCache = new Map();
  194. cache.set(associatedObjectForCache, innerCache);
  195. }
  196. innerSubCache = innerCache.get(context);
  197. if (innerSubCache === undefined) {
  198. innerCache.set(context, (innerSubCache = new Map()));
  199. }
  200. } else {
  201. innerSubCache = new Map();
  202. }
  203. /**
  204. * @param {string} identifier identifier used to create relative path
  205. * @returns {string} the returned relative path
  206. */
  207. const boundFn = identifier => {
  208. const cachedResult = innerSubCache.get(identifier);
  209. if (cachedResult !== undefined) {
  210. return cachedResult;
  211. } else {
  212. const result = fn(context, identifier);
  213. innerSubCache.set(identifier, result);
  214. return result;
  215. }
  216. };
  217. return boundFn;
  218. };
  219. return cachedFn;
  220. };
  221. /**
  222. *
  223. * @param {string} context context for relative path
  224. * @param {string} identifier identifier for path
  225. * @returns {string} a converted relative path
  226. */
  227. const _makePathsRelative = (context, identifier) => {
  228. return identifier
  229. .split(SEGMENTS_SPLIT_REGEXP)
  230. .map(str => absoluteToRequest(context, str))
  231. .join("");
  232. };
  233. exports.makePathsRelative = makeCacheableWithContext(_makePathsRelative);
  234. /**
  235. *
  236. * @param {string} context context for relative path
  237. * @param {string} identifier identifier for path
  238. * @returns {string} a converted relative path
  239. */
  240. const _makePathsAbsolute = (context, identifier) => {
  241. return identifier
  242. .split(SEGMENTS_SPLIT_REGEXP)
  243. .map(str => requestToAbsolute(context, str))
  244. .join("");
  245. };
  246. exports.makePathsAbsolute = makeCacheableWithContext(_makePathsAbsolute);
  247. /**
  248. * @param {string} context absolute context path
  249. * @param {string} request any request string may containing absolute paths, query string, etc.
  250. * @returns {string} a new request string avoiding absolute paths when possible
  251. */
  252. const _contextify = (context, request) => {
  253. return request
  254. .split("!")
  255. .map(r => absoluteToRequest(context, r))
  256. .join("!");
  257. };
  258. const contextify = makeCacheableWithContext(_contextify);
  259. exports.contextify = contextify;
  260. /**
  261. * @param {string} context absolute context path
  262. * @param {string} request any request string
  263. * @returns {string} a new request string using absolute paths when possible
  264. */
  265. const _absolutify = (context, request) => {
  266. return request
  267. .split("!")
  268. .map(r => requestToAbsolute(context, r))
  269. .join("!");
  270. };
  271. const absolutify = makeCacheableWithContext(_absolutify);
  272. exports.absolutify = absolutify;
  273. const PATH_QUERY_FRAGMENT_REGEXP =
  274. /^((?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
  275. const PATH_QUERY_REGEXP = /^((?:\0.|[^?\0])*)(\?.*)?$/;
  276. /** @typedef {{ resource: string, path: string, query: string, fragment: string }} ParsedResource */
  277. /** @typedef {{ resource: string, path: string, query: string }} ParsedResourceWithoutFragment */
  278. /**
  279. * @param {string} str the path with query and fragment
  280. * @returns {ParsedResource} parsed parts
  281. */
  282. const _parseResource = str => {
  283. const match = PATH_QUERY_FRAGMENT_REGEXP.exec(str);
  284. return {
  285. resource: str,
  286. path: match[1].replace(/\0(.)/g, "$1"),
  287. query: match[2] ? match[2].replace(/\0(.)/g, "$1") : "",
  288. fragment: match[3] || ""
  289. };
  290. };
  291. exports.parseResource = makeCacheable(_parseResource);
  292. /**
  293. * Parse resource, skips fragment part
  294. * @param {string} str the path with query and fragment
  295. * @returns {ParsedResourceWithoutFragment} parsed parts
  296. */
  297. const _parseResourceWithoutFragment = str => {
  298. const match = PATH_QUERY_REGEXP.exec(str);
  299. return {
  300. resource: str,
  301. path: match[1].replace(/\0(.)/g, "$1"),
  302. query: match[2] ? match[2].replace(/\0(.)/g, "$1") : ""
  303. };
  304. };
  305. exports.parseResourceWithoutFragment = makeCacheable(
  306. _parseResourceWithoutFragment
  307. );
  308. /**
  309. * @param {string} filename the filename which should be undone
  310. * @param {string} outputPath the output path that is restored (only relevant when filename contains "..")
  311. * @param {boolean} enforceRelative true returns ./ for empty paths
  312. * @returns {string} repeated ../ to leave the directory of the provided filename to be back on output dir
  313. */
  314. exports.getUndoPath = (filename, outputPath, enforceRelative) => {
  315. let depth = -1;
  316. let append = "";
  317. outputPath = outputPath.replace(/[\\/]$/, "");
  318. for (const part of filename.split(/[/\\]+/)) {
  319. if (part === "..") {
  320. if (depth > -1) {
  321. depth--;
  322. } else {
  323. const i = outputPath.lastIndexOf("/");
  324. const j = outputPath.lastIndexOf("\\");
  325. const pos = i < 0 ? j : j < 0 ? i : Math.max(i, j);
  326. if (pos < 0) return outputPath + "/";
  327. append = outputPath.slice(pos + 1) + "/" + append;
  328. outputPath = outputPath.slice(0, pos);
  329. }
  330. } else if (part !== ".") {
  331. depth++;
  332. }
  333. }
  334. return depth > 0
  335. ? `${"../".repeat(depth)}${append}`
  336. : enforceRelative
  337. ? `./${append}`
  338. : append;
  339. };