path.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // import {cwd} from './env.js'
  2. let cwd;
  3. // Copyright Joyent, Inc. and other Node contributors.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to permit
  10. // persons to whom the Software is furnished to do so, subject to the
  11. // following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included
  14. // in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  19. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  20. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //'use strict';
  24. const
  25. CHAR_UPPERCASE_A = 65,
  26. CHAR_LOWERCASE_A = 97,
  27. CHAR_UPPERCASE_Z = 90,
  28. CHAR_LOWERCASE_Z = 122,
  29. CHAR_DOT = 46,
  30. CHAR_FORWARD_SLASH = 47,
  31. CHAR_BACKWARD_SLASH = 92,
  32. CHAR_COLON = 58,
  33. CHAR_QUESTION_MARK = 63;
  34. function isPathSeparator(code) {
  35. return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
  36. }
  37. function isPosixPathSeparator(code) {
  38. return code === CHAR_FORWARD_SLASH;
  39. }
  40. // Resolves . and .. elements in a path with directory names
  41. function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
  42. let res = '';
  43. let lastSegmentLength = 0;
  44. let lastSlash = -1;
  45. let dots = 0;
  46. let code = 0;
  47. for (let i = 0; i <= path.length; ++i) {
  48. if (i < path.length)
  49. code = path.charCodeAt(i);
  50. else if (isPathSeparator(code))
  51. break;
  52. else
  53. code = CHAR_FORWARD_SLASH;
  54. if (isPathSeparator(code)) {
  55. if (lastSlash === i - 1 || dots === 1) {
  56. // NOOP
  57. } else if (dots === 2) {
  58. if (res.length < 2 || lastSegmentLength !== 2 ||
  59. res.charCodeAt( res.length - 1) !== CHAR_DOT ||
  60. res.charCodeAt(res.length - 2) !== CHAR_DOT) {
  61. if (res.length > 2) {
  62. const lastSlashIndex = res.lastIndexOf(separator);
  63. if (lastSlashIndex === -1) {
  64. res = '';
  65. lastSegmentLength = 0;
  66. } else {
  67. res = res.slice(0, lastSlashIndex);
  68. lastSegmentLength =
  69. res.length - 1 - res.lastIndexOf(res, separator);
  70. }
  71. lastSlash = i;
  72. dots = 0;
  73. continue;
  74. } else if (res.length !== 0) {
  75. res = '';
  76. lastSegmentLength = 0;
  77. lastSlash = i;
  78. dots = 0;
  79. continue;
  80. }
  81. }
  82. if (allowAboveRoot) {
  83. res += res.length > 0 ? `${separator}..` : '..';
  84. lastSegmentLength = 2;
  85. }
  86. } else {
  87. if (res.length > 0)
  88. res += `${separator}${path.slice(lastSlash + 1, i)}`;
  89. else
  90. res = path.slice(lastSlash + 1, i);
  91. lastSegmentLength = i - lastSlash - 1;
  92. }
  93. lastSlash = i;
  94. dots = 0;
  95. } else if (code === CHAR_DOT && dots !== -1) {
  96. ++dots;
  97. } else {
  98. dots = -1;
  99. }
  100. }
  101. return res;
  102. }
  103. const path = {
  104. // path.resolve([from ...], to)
  105. resolve(...args) {
  106. let resolvedPath = '';
  107. let resolvedAbsolute = false;
  108. for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
  109. // orig const path = i >= 0 ? args[i] : posixCwd();
  110. const path = i >= 0 ? args[i] : (cwd !== undefined ? cwd : '/');
  111. // const path = i >= 0 ? args[i] : '/';
  112. // Skip empty entries
  113. if (path.length === 0) {
  114. continue;
  115. }
  116. resolvedPath = `${path}/${resolvedPath}`;
  117. resolvedAbsolute =
  118. path.charCodeAt(0) === CHAR_FORWARD_SLASH;
  119. }
  120. // At this point the path should be resolved to a full absolute path, but
  121. // handle relative paths to be safe (might happen when process.cwd() fails)
  122. // Normalize the path
  123. resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
  124. isPosixPathSeparator);
  125. if (resolvedAbsolute) {
  126. return `/${resolvedPath}`;
  127. }
  128. return resolvedPath.length > 0 ? resolvedPath : '.';
  129. },
  130. normalize(path) {
  131. if (path.length === 0)
  132. return '.';
  133. const isAbsolute =
  134. path.charCodeAt(0) === CHAR_FORWARD_SLASH;
  135. const trailingSeparator =
  136. path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
  137. // Normalize the path
  138. path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
  139. if (path.length === 0) {
  140. if (isAbsolute)
  141. return '/';
  142. return trailingSeparator ? './' : '.';
  143. }
  144. if (trailingSeparator)
  145. path += '/';
  146. return isAbsolute ? `/${path}` : path;
  147. },
  148. isAbsolute(path) {
  149. return path.length > 0 &&
  150. path.charCodeAt(0) === CHAR_FORWARD_SLASH;
  151. },
  152. join(...args) {
  153. if (args.length === 0)
  154. return '.';
  155. let joined;
  156. for (let i = 0; i < args.length; ++i) {
  157. const arg = args[i];
  158. if (arg.length > 0) {
  159. if (joined === undefined)
  160. joined = arg;
  161. else
  162. joined += `/${arg}`;
  163. }
  164. }
  165. if (joined === undefined)
  166. return '.';
  167. return path.normalize(joined);
  168. },
  169. relative(from, to) {
  170. if (from === to)
  171. return '';
  172. // Trim leading forward slashes.
  173. from = path.resolve(from);
  174. to = path.resolve(to);
  175. if (from === to)
  176. return '';
  177. const fromStart = 1;
  178. const fromEnd = from.length;
  179. const fromLen = fromEnd - fromStart;
  180. const toStart = 1;
  181. const toLen = to.length - toStart;
  182. // Compare paths to find the longest common path from root
  183. const length = (fromLen < toLen ? fromLen : toLen);
  184. let lastCommonSep = -1;
  185. let i = 0;
  186. for (; i < length; i++) {
  187. const fromCode = from.charCodeAt(fromStart + i);
  188. if (fromCode !== to.charCodeAt(toStart + i))
  189. break;
  190. else if (fromCode === CHAR_FORWARD_SLASH)
  191. lastCommonSep = i;
  192. }
  193. if (i === length) {
  194. if (toLen > length) {
  195. if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
  196. // We get here if `from` is the exact base path for `to`.
  197. // For example: from='/foo/bar'; to='/foo/bar/baz'
  198. return to.slice(toStart + i + 1);
  199. }
  200. if (i === 0) {
  201. // We get here if `from` is the root
  202. // For example: from='/'; to='/foo'
  203. return to.slice(toStart + i);
  204. }
  205. } else if (fromLen > length) {
  206. if (from.charCodeAt(fromStart + i) ===
  207. CHAR_FORWARD_SLASH) {
  208. // We get here if `to` is the exact base path for `from`.
  209. // For example: from='/foo/bar/baz'; to='/foo/bar'
  210. lastCommonSep = i;
  211. } else if (i === 0) {
  212. // We get here if `to` is the root.
  213. // For example: from='/foo/bar'; to='/'
  214. lastCommonSep = 0;
  215. }
  216. }
  217. }
  218. let out = '';
  219. // Generate the relative path based on the path difference between `to`
  220. // and `from`.
  221. for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
  222. if (i === fromEnd ||
  223. from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
  224. out += out.length === 0 ? '..' : '/..';
  225. }
  226. }
  227. // Lastly, append the rest of the destination (`to`) path that comes after
  228. // the common path parts.
  229. return `${out}${to.slice(toStart + lastCommonSep)}`;
  230. },
  231. toNamespacedPath(path) {
  232. // Non-op on posix systems
  233. return path;
  234. },
  235. dirname(path) {
  236. if (path.length === 0)
  237. return '.';
  238. const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
  239. let end = -1;
  240. let matchedSlash = true;
  241. for (let i = path.length - 1; i >= 1; --i) {
  242. if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
  243. if (!matchedSlash) {
  244. end = i;
  245. break;
  246. }
  247. } else {
  248. // We saw the first non-path separator
  249. matchedSlash = false;
  250. }
  251. }
  252. if (end === -1)
  253. return hasRoot ? '/' : '.';
  254. if (hasRoot && end === 1)
  255. return '//';
  256. return path.slice(0, end);
  257. },
  258. basename(path, ext) {
  259. let start = 0;
  260. let end = -1;
  261. let matchedSlash = true;
  262. if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
  263. if (ext === path)
  264. return '';
  265. let extIdx = ext.length - 1;
  266. let firstNonSlashEnd = -1;
  267. for (let i = path.length - 1; i >= 0; --i) {
  268. const code = path.charCodeAt(i);
  269. if (code === CHAR_FORWARD_SLASH) {
  270. // If we reached a path separator that was not part of a set of path
  271. // separators at the end of the string, stop now
  272. if (!matchedSlash) {
  273. start = i + 1;
  274. break;
  275. }
  276. } else {
  277. if (firstNonSlashEnd === -1) {
  278. // We saw the first non-path separator, remember this index in case
  279. // we need it if the extension ends up not matching
  280. matchedSlash = false;
  281. firstNonSlashEnd = i + 1;
  282. }
  283. if (extIdx >= 0) {
  284. // Try to match the explicit extension
  285. if (code === ext.charCodeAt(extIdx)) {
  286. if (--extIdx === -1) {
  287. // We matched the extension, so mark this as the end of our path
  288. // component
  289. end = i;
  290. }
  291. } else {
  292. // Extension does not match, so our result is the entire path
  293. // component
  294. extIdx = -1;
  295. end = firstNonSlashEnd;
  296. }
  297. }
  298. }
  299. }
  300. if (start === end)
  301. end = firstNonSlashEnd;
  302. else if (end === -1)
  303. end = path.length;
  304. return path.slice(start, end);
  305. }
  306. for (let i = path.length - 1; i >= 0; --i) {
  307. if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
  308. // If we reached a path separator that was not part of a set of path
  309. // separators at the end of the string, stop now
  310. if (!matchedSlash) {
  311. start = i + 1;
  312. break;
  313. }
  314. } else if (end === -1) {
  315. // We saw the first non-path separator, mark this as the end of our
  316. // path component
  317. matchedSlash = false;
  318. end = i + 1;
  319. }
  320. }
  321. if (end === -1)
  322. return '';
  323. return path.slice(start, end);
  324. },
  325. extname(path) {
  326. let startDot = -1;
  327. let startPart = 0;
  328. let end = -1;
  329. let matchedSlash = true;
  330. // Track the state of characters (if any) we see before our first dot and
  331. // after any path separator we find
  332. let preDotState = 0;
  333. for (let i = path.length - 1; i >= 0; --i) {
  334. const code = path.charCodeAt(i);
  335. if (code === CHAR_FORWARD_SLASH) {
  336. // If we reached a path separator that was not part of a set of path
  337. // separators at the end of the string, stop now
  338. if (!matchedSlash) {
  339. startPart = i + 1;
  340. break;
  341. }
  342. continue;
  343. }
  344. if (end === -1) {
  345. // We saw the first non-path separator, mark this as the end of our
  346. // extension
  347. matchedSlash = false;
  348. end = i + 1;
  349. }
  350. if (code === CHAR_DOT) {
  351. // If this is our first dot, mark it as the start of our extension
  352. if (startDot === -1)
  353. startDot = i;
  354. else if (preDotState !== 1)
  355. preDotState = 1;
  356. } else if (startDot !== -1) {
  357. // We saw a non-dot and non-path separator before our dot, so we should
  358. // have a good chance at having a non-empty extension
  359. preDotState = -1;
  360. }
  361. }
  362. if (startDot === -1 ||
  363. end === -1 ||
  364. // We saw a non-dot character immediately before the dot
  365. preDotState === 0 ||
  366. // The (right-most) trimmed path component is exactly '..'
  367. (preDotState === 1 &&
  368. startDot === end - 1 &&
  369. startDot === startPart + 1)) {
  370. return '';
  371. }
  372. return path.slice(startDot, end);
  373. },
  374. format: _format.bind( null, '/'),
  375. parse(path) {
  376. const ret = { root: '', dir: '', base: '', ext: '', name: '' };
  377. if (path.length === 0)
  378. return ret;
  379. const isAbsolute =
  380. path.charCodeAt(0) === CHAR_FORWARD_SLASH;
  381. let start;
  382. if (isAbsolute) {
  383. ret.root = '/';
  384. start = 1;
  385. } else {
  386. start = 0;
  387. }
  388. let startDot = -1;
  389. let startPart = 0;
  390. let end = -1;
  391. let matchedSlash = true;
  392. let i = path.length - 1;
  393. // Track the state of characters (if any) we see before our first dot and
  394. // after any path separator we find
  395. let preDotState = 0;
  396. // Get non-dir info
  397. for (; i >= start; --i) {
  398. const code = path.charCodeAt(i);
  399. if (code === CHAR_FORWARD_SLASH) {
  400. // If we reached a path separator that was not part of a set of path
  401. // separators at the end of the string, stop now
  402. if (!matchedSlash) {
  403. startPart = i + 1;
  404. break;
  405. }
  406. continue;
  407. }
  408. if (end === -1) {
  409. // We saw the first non-path separator, mark this as the end of our
  410. // extension
  411. matchedSlash = false;
  412. end = i + 1;
  413. }
  414. if (code === CHAR_DOT) {
  415. // If this is our first dot, mark it as the start of our extension
  416. if (startDot === -1)
  417. startDot = i;
  418. else if (preDotState !== 1)
  419. preDotState = 1;
  420. } else if (startDot !== -1) {
  421. // We saw a non-dot and non-path separator before our dot, so we should
  422. // have a good chance at having a non-empty extension
  423. preDotState = -1;
  424. }
  425. }
  426. if (end !== -1) {
  427. const start = startPart === 0 && isAbsolute ? 1 : startPart;
  428. if (startDot === -1 ||
  429. // We saw a non-dot character immediately before the dot
  430. preDotState === 0 ||
  431. // The (right-most) trimmed path component is exactly '..'
  432. (preDotState === 1 &&
  433. startDot === end - 1 &&
  434. startDot === startPart + 1)) {
  435. ret.base = ret.name = path.slice(start, end);
  436. } else {
  437. ret.name = path.slice(start, startDot);
  438. ret.base = path.slice(start, end);
  439. ret.ext = path.slice(startDot, end);
  440. }
  441. }
  442. if (startPart > 0)
  443. ret.dir = path.slice(0, startPart - 1);
  444. else if (isAbsolute)
  445. ret.dir = '/';
  446. return ret;
  447. },
  448. sep: '/',
  449. delimiter: ':',
  450. win32: null,
  451. posix: null
  452. };
  453. function _format(sep, pathObject) {
  454. validateObject(pathObject, 'pathObject');
  455. const dir = pathObject.dir || pathObject.root;
  456. const base = pathObject.base ||
  457. `${pathObject.name || ''}${pathObject.ext || ''}`;
  458. if (!dir) {
  459. return base;
  460. }
  461. return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
  462. }
  463. export default path