path_CH3auf61.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. globalThis.process ??= {}; globalThis.process.env ??= {};
  2. function appendForwardSlash(path) {
  3. return path.endsWith("/") ? path : path + "/";
  4. }
  5. function prependForwardSlash(path) {
  6. return path[0] === "/" ? path : "/" + path;
  7. }
  8. const MANY_TRAILING_SLASHES = /\/{2,}$/g;
  9. function collapseDuplicateTrailingSlashes(path, trailingSlash) {
  10. if (!path) {
  11. return path;
  12. }
  13. return path.replace(MANY_TRAILING_SLASHES, trailingSlash ? "/" : "") || "/";
  14. }
  15. function removeTrailingForwardSlash(path) {
  16. return path.endsWith("/") ? path.slice(0, path.length - 1) : path;
  17. }
  18. function removeLeadingForwardSlash(path) {
  19. return path.startsWith("/") ? path.substring(1) : path;
  20. }
  21. function trimSlashes(path) {
  22. return path.replace(/^\/|\/$/g, "");
  23. }
  24. function isString(path) {
  25. return typeof path === "string" || path instanceof String;
  26. }
  27. const INTERNAL_PREFIXES = /* @__PURE__ */ new Set(["/_", "/@", "/.", "//"]);
  28. const JUST_SLASHES = /^\/{2,}$/;
  29. function isInternalPath(path) {
  30. return INTERNAL_PREFIXES.has(path.slice(0, 2)) && !JUST_SLASHES.test(path);
  31. }
  32. function joinPaths(...paths) {
  33. return paths.filter(isString).map((path, i) => {
  34. if (i === 0) {
  35. return removeTrailingForwardSlash(path);
  36. } else if (i === paths.length - 1) {
  37. return removeLeadingForwardSlash(path);
  38. } else {
  39. return trimSlashes(path);
  40. }
  41. }).join("/");
  42. }
  43. function isRemotePath(src) {
  44. if (!src) return false;
  45. const trimmed = src.trim();
  46. if (!trimmed) return false;
  47. let decoded = trimmed;
  48. let previousDecoded = "";
  49. let maxIterations = 10;
  50. while (decoded !== previousDecoded && maxIterations > 0) {
  51. previousDecoded = decoded;
  52. try {
  53. decoded = decodeURIComponent(decoded);
  54. } catch {
  55. break;
  56. }
  57. maxIterations--;
  58. }
  59. if (/^[a-zA-Z]:/.test(decoded)) {
  60. return false;
  61. }
  62. if (decoded[0] === "/" && decoded[1] !== "/" && decoded[1] !== "\\") {
  63. return false;
  64. }
  65. if (decoded[0] === "\\") {
  66. return true;
  67. }
  68. if (decoded.startsWith("//")) {
  69. return true;
  70. }
  71. try {
  72. const url = new URL(decoded, "http://n");
  73. if (url.username || url.password) {
  74. return true;
  75. }
  76. if (decoded.includes("@") && !url.pathname.includes("@") && !url.search.includes("@")) {
  77. return true;
  78. }
  79. if (url.origin !== "http://n") {
  80. const protocol = url.protocol.toLowerCase();
  81. if (protocol === "file:") {
  82. return false;
  83. }
  84. return true;
  85. }
  86. if (URL.canParse(decoded)) {
  87. return true;
  88. }
  89. return false;
  90. } catch {
  91. return true;
  92. }
  93. }
  94. function slash(path) {
  95. return path.replace(/\\/g, "/");
  96. }
  97. function fileExtension(path) {
  98. const ext = path.split(".").pop();
  99. return ext !== path ? `.${ext}` : "";
  100. }
  101. const WITH_FILE_EXT = /\/[^/]+\.\w+$/;
  102. function hasFileExtension(path) {
  103. return WITH_FILE_EXT.test(path);
  104. }
  105. export { appendForwardSlash as a, isInternalPath as b, collapseDuplicateTrailingSlashes as c, fileExtension as f, hasFileExtension as h, isRemotePath as i, joinPaths as j, prependForwardSlash as p, removeTrailingForwardSlash as r, slash as s, trimSlashes as t };