identifier.js 11 KB

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