path.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. // Slightly modified (no IE8 support, ES6) and transcribed to TypeScript
  3. // https://github.com/calvinmetcalf/rollup-plugin-node-builtins/blob/63ab8aacd013767445ca299e468d9a60a95328d7/src/es6/path.js
  4. //
  5. // Copyright Joyent, Inc.and other Node contributors.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a
  8. // copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to permit
  12. // persons to whom the Software is furnished to do so, subject to the
  13. // following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included
  16. // in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  21. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  22. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. /** JSDoc */
  26. function normalizeArray(parts, allowAboveRoot) {
  27. // if the path tries to go above the root, `up` ends up > 0
  28. let up = 0;
  29. for (let i = parts.length - 1; i >= 0; i--) {
  30. const last = parts[i];
  31. if (last === '.') {
  32. parts.splice(i, 1);
  33. } else if (last === '..') {
  34. parts.splice(i, 1);
  35. up++;
  36. } else if (up) {
  37. parts.splice(i, 1);
  38. up--;
  39. }
  40. }
  41. // if the path is allowed to go above the root, restore leading ..s
  42. if (allowAboveRoot) {
  43. for (; up--; up) {
  44. parts.unshift('..');
  45. }
  46. }
  47. return parts;
  48. }
  49. // Split a filename into [root, dir, basename, ext], unix version
  50. // 'root' is just a slash, or nothing.
  51. const splitPathRe = /^(\S+:\\|\/?)([\s\S]*?)((?:\.{1,2}|[^/\\]+?|)(\.[^./\\]*|))(?:[/\\]*)$/;
  52. /** JSDoc */
  53. function splitPath(filename) {
  54. // Truncate files names greater than 1024 characters to avoid regex dos
  55. // https://github.com/getsentry/sentry-javascript/pull/8737#discussion_r1285719172
  56. const truncated = filename.length > 1024 ? `<truncated>${filename.slice(-1024)}` : filename;
  57. const parts = splitPathRe.exec(truncated);
  58. return parts ? parts.slice(1) : [];
  59. }
  60. // path.resolve([from ...], to)
  61. // posix version
  62. /** JSDoc */
  63. function resolve(...args) {
  64. let resolvedPath = '';
  65. let resolvedAbsolute = false;
  66. for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  67. const path = i >= 0 ? args[i] : '/';
  68. // Skip empty entries
  69. if (!path) {
  70. continue;
  71. }
  72. resolvedPath = `${path}/${resolvedPath}`;
  73. resolvedAbsolute = path.charAt(0) === '/';
  74. }
  75. // At this point the path should be resolved to a full absolute path, but
  76. // handle relative paths to be safe (might happen when process.cwd() fails)
  77. // Normalize the path
  78. resolvedPath = normalizeArray(
  79. resolvedPath.split('/').filter(p => !!p),
  80. !resolvedAbsolute,
  81. ).join('/');
  82. return (resolvedAbsolute ? '/' : '') + resolvedPath || '.';
  83. }
  84. /** JSDoc */
  85. function trim(arr) {
  86. let start = 0;
  87. for (; start < arr.length; start++) {
  88. if (arr[start] !== '') {
  89. break;
  90. }
  91. }
  92. let end = arr.length - 1;
  93. for (; end >= 0; end--) {
  94. if (arr[end] !== '') {
  95. break;
  96. }
  97. }
  98. if (start > end) {
  99. return [];
  100. }
  101. return arr.slice(start, end - start + 1);
  102. }
  103. // path.relative(from, to)
  104. // posix version
  105. /** JSDoc */
  106. function relative(from, to) {
  107. /* eslint-disable no-param-reassign */
  108. from = resolve(from).slice(1);
  109. to = resolve(to).slice(1);
  110. /* eslint-enable no-param-reassign */
  111. const fromParts = trim(from.split('/'));
  112. const toParts = trim(to.split('/'));
  113. const length = Math.min(fromParts.length, toParts.length);
  114. let samePartsLength = length;
  115. for (let i = 0; i < length; i++) {
  116. if (fromParts[i] !== toParts[i]) {
  117. samePartsLength = i;
  118. break;
  119. }
  120. }
  121. let outputParts = [];
  122. for (let i = samePartsLength; i < fromParts.length; i++) {
  123. outputParts.push('..');
  124. }
  125. outputParts = outputParts.concat(toParts.slice(samePartsLength));
  126. return outputParts.join('/');
  127. }
  128. // path.normalize(path)
  129. // posix version
  130. /** JSDoc */
  131. function normalizePath(path) {
  132. const isPathAbsolute = isAbsolute(path);
  133. const trailingSlash = path.slice(-1) === '/';
  134. // Normalize the path
  135. let normalizedPath = normalizeArray(
  136. path.split('/').filter(p => !!p),
  137. !isPathAbsolute,
  138. ).join('/');
  139. if (!normalizedPath && !isPathAbsolute) {
  140. normalizedPath = '.';
  141. }
  142. if (normalizedPath && trailingSlash) {
  143. normalizedPath += '/';
  144. }
  145. return (isPathAbsolute ? '/' : '') + normalizedPath;
  146. }
  147. // posix version
  148. /** JSDoc */
  149. function isAbsolute(path) {
  150. return path.charAt(0) === '/';
  151. }
  152. // posix version
  153. /** JSDoc */
  154. function join(...args) {
  155. return normalizePath(args.join('/'));
  156. }
  157. /** JSDoc */
  158. function dirname(path) {
  159. const result = splitPath(path);
  160. const root = result[0];
  161. let dir = result[1];
  162. if (!root && !dir) {
  163. // No dirname whatsoever
  164. return '.';
  165. }
  166. if (dir) {
  167. // It has a dirname, strip trailing slash
  168. dir = dir.slice(0, dir.length - 1);
  169. }
  170. return root + dir;
  171. }
  172. /** JSDoc */
  173. function basename(path, ext) {
  174. let f = splitPath(path)[2];
  175. if (ext && f.slice(ext.length * -1) === ext) {
  176. f = f.slice(0, f.length - ext.length);
  177. }
  178. return f;
  179. }
  180. exports.basename = basename;
  181. exports.dirname = dirname;
  182. exports.isAbsolute = isAbsolute;
  183. exports.join = join;
  184. exports.normalizePath = normalizePath;
  185. exports.relative = relative;
  186. exports.resolve = resolve;
  187. //# sourceMappingURL=path.js.map