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