index.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  1. import { LRUCache } from 'lru-cache';
  2. import { posix, win32 } from 'node:path';
  3. import { fileURLToPath } from 'node:url';
  4. import { lstatSync, readdir as readdirCB, readdirSync, readlinkSync, realpathSync as rps, } from 'fs';
  5. import * as actualFS from 'node:fs';
  6. const realpathSync = rps.native;
  7. // TODO: test perf of fs/promises realpath vs realpathCB,
  8. // since the promises one uses realpath.native
  9. import { lstat, readdir, readlink, realpath } from 'node:fs/promises';
  10. import { Minipass } from 'minipass';
  11. const defaultFS = {
  12. lstatSync,
  13. readdir: readdirCB,
  14. readdirSync,
  15. readlinkSync,
  16. realpathSync,
  17. promises: {
  18. lstat,
  19. readdir,
  20. readlink,
  21. realpath,
  22. },
  23. };
  24. // if they just gave us require('fs') then use our default
  25. const fsFromOption = (fsOption) => !fsOption || fsOption === defaultFS || fsOption === actualFS ?
  26. defaultFS
  27. : {
  28. ...defaultFS,
  29. ...fsOption,
  30. promises: {
  31. ...defaultFS.promises,
  32. ...(fsOption.promises || {}),
  33. },
  34. };
  35. // turn something like //?/c:/ into c:\
  36. const uncDriveRegexp = /^\\\\\?\\([a-z]:)\\?$/i;
  37. const uncToDrive = (rootPath) => rootPath.replace(/\//g, '\\').replace(uncDriveRegexp, '$1\\');
  38. // windows paths are separated by either / or \
  39. const eitherSep = /[\\\/]/;
  40. const UNKNOWN = 0; // may not even exist, for all we know
  41. const IFIFO = 0b0001;
  42. const IFCHR = 0b0010;
  43. const IFDIR = 0b0100;
  44. const IFBLK = 0b0110;
  45. const IFREG = 0b1000;
  46. const IFLNK = 0b1010;
  47. const IFSOCK = 0b1100;
  48. const IFMT = 0b1111;
  49. // mask to unset low 4 bits
  50. const IFMT_UNKNOWN = ~IFMT;
  51. // set after successfully calling readdir() and getting entries.
  52. const READDIR_CALLED = 0b0000_0001_0000;
  53. // set after a successful lstat()
  54. const LSTAT_CALLED = 0b0000_0010_0000;
  55. // set if an entry (or one of its parents) is definitely not a dir
  56. const ENOTDIR = 0b0000_0100_0000;
  57. // set if an entry (or one of its parents) does not exist
  58. // (can also be set on lstat errors like EACCES or ENAMETOOLONG)
  59. const ENOENT = 0b0000_1000_0000;
  60. // cannot have child entries -- also verify &IFMT is either IFDIR or IFLNK
  61. // set if we fail to readlink
  62. const ENOREADLINK = 0b0001_0000_0000;
  63. // set if we know realpath() will fail
  64. const ENOREALPATH = 0b0010_0000_0000;
  65. const ENOCHILD = ENOTDIR | ENOENT | ENOREALPATH;
  66. const TYPEMASK = 0b0011_1111_1111;
  67. const entToType = (s) => s.isFile() ? IFREG
  68. : s.isDirectory() ? IFDIR
  69. : s.isSymbolicLink() ? IFLNK
  70. : s.isCharacterDevice() ? IFCHR
  71. : s.isBlockDevice() ? IFBLK
  72. : s.isSocket() ? IFSOCK
  73. : s.isFIFO() ? IFIFO
  74. : UNKNOWN;
  75. // normalize unicode path names
  76. const normalizeCache = new Map();
  77. const normalize = (s) => {
  78. const c = normalizeCache.get(s);
  79. if (c)
  80. return c;
  81. const n = s.normalize('NFKD');
  82. normalizeCache.set(s, n);
  83. return n;
  84. };
  85. const normalizeNocaseCache = new Map();
  86. const normalizeNocase = (s) => {
  87. const c = normalizeNocaseCache.get(s);
  88. if (c)
  89. return c;
  90. const n = normalize(s.toLowerCase());
  91. normalizeNocaseCache.set(s, n);
  92. return n;
  93. };
  94. /**
  95. * An LRUCache for storing resolved path strings or Path objects.
  96. * @internal
  97. */
  98. export class ResolveCache extends LRUCache {
  99. constructor() {
  100. super({ max: 256 });
  101. }
  102. }
  103. // In order to prevent blowing out the js heap by allocating hundreds of
  104. // thousands of Path entries when walking extremely large trees, the "children"
  105. // in this tree are represented by storing an array of Path entries in an
  106. // LRUCache, indexed by the parent. At any time, Path.children() may return an
  107. // empty array, indicating that it doesn't know about any of its children, and
  108. // thus has to rebuild that cache. This is fine, it just means that we don't
  109. // benefit as much from having the cached entries, but huge directory walks
  110. // don't blow out the stack, and smaller ones are still as fast as possible.
  111. //
  112. //It does impose some complexity when building up the readdir data, because we
  113. //need to pass a reference to the children array that we started with.
  114. /**
  115. * an LRUCache for storing child entries.
  116. * @internal
  117. */
  118. export class ChildrenCache extends LRUCache {
  119. constructor(maxSize = 16 * 1024) {
  120. super({
  121. maxSize,
  122. // parent + children
  123. sizeCalculation: a => a.length + 1,
  124. });
  125. }
  126. }
  127. const setAsCwd = Symbol('PathScurry setAsCwd');
  128. /**
  129. * Path objects are sort of like a super-powered
  130. * {@link https://nodejs.org/docs/latest/api/fs.html#class-fsdirent fs.Dirent}
  131. *
  132. * Each one represents a single filesystem entry on disk, which may or may not
  133. * exist. It includes methods for reading various types of information via
  134. * lstat, readlink, and readdir, and caches all information to the greatest
  135. * degree possible.
  136. *
  137. * Note that fs operations that would normally throw will instead return an
  138. * "empty" value. This is in order to prevent excessive overhead from error
  139. * stack traces.
  140. */
  141. export class PathBase {
  142. /**
  143. * the basename of this path
  144. *
  145. * **Important**: *always* test the path name against any test string
  146. * usingthe {@link isNamed} method, and not by directly comparing this
  147. * string. Otherwise, unicode path strings that the system sees as identical
  148. * will not be properly treated as the same path, leading to incorrect
  149. * behavior and possible security issues.
  150. */
  151. name;
  152. /**
  153. * the Path entry corresponding to the path root.
  154. *
  155. * @internal
  156. */
  157. root;
  158. /**
  159. * All roots found within the current PathScurry family
  160. *
  161. * @internal
  162. */
  163. roots;
  164. /**
  165. * a reference to the parent path, or undefined in the case of root entries
  166. *
  167. * @internal
  168. */
  169. parent;
  170. /**
  171. * boolean indicating whether paths are compared case-insensitively
  172. * @internal
  173. */
  174. nocase;
  175. /**
  176. * boolean indicating that this path is the current working directory
  177. * of the PathScurry collection that contains it.
  178. */
  179. isCWD = false;
  180. // potential default fs override
  181. #fs;
  182. // Stats fields
  183. #dev;
  184. get dev() {
  185. return this.#dev;
  186. }
  187. #mode;
  188. get mode() {
  189. return this.#mode;
  190. }
  191. #nlink;
  192. get nlink() {
  193. return this.#nlink;
  194. }
  195. #uid;
  196. get uid() {
  197. return this.#uid;
  198. }
  199. #gid;
  200. get gid() {
  201. return this.#gid;
  202. }
  203. #rdev;
  204. get rdev() {
  205. return this.#rdev;
  206. }
  207. #blksize;
  208. get blksize() {
  209. return this.#blksize;
  210. }
  211. #ino;
  212. get ino() {
  213. return this.#ino;
  214. }
  215. #size;
  216. get size() {
  217. return this.#size;
  218. }
  219. #blocks;
  220. get blocks() {
  221. return this.#blocks;
  222. }
  223. #atimeMs;
  224. get atimeMs() {
  225. return this.#atimeMs;
  226. }
  227. #mtimeMs;
  228. get mtimeMs() {
  229. return this.#mtimeMs;
  230. }
  231. #ctimeMs;
  232. get ctimeMs() {
  233. return this.#ctimeMs;
  234. }
  235. #birthtimeMs;
  236. get birthtimeMs() {
  237. return this.#birthtimeMs;
  238. }
  239. #atime;
  240. get atime() {
  241. return this.#atime;
  242. }
  243. #mtime;
  244. get mtime() {
  245. return this.#mtime;
  246. }
  247. #ctime;
  248. get ctime() {
  249. return this.#ctime;
  250. }
  251. #birthtime;
  252. get birthtime() {
  253. return this.#birthtime;
  254. }
  255. #matchName;
  256. #depth;
  257. #fullpath;
  258. #fullpathPosix;
  259. #relative;
  260. #relativePosix;
  261. #type;
  262. #children;
  263. #linkTarget;
  264. #realpath;
  265. /**
  266. * This property is for compatibility with the Dirent class as of
  267. * Node v20, where Dirent['parentPath'] refers to the path of the
  268. * directory that was passed to readdir. For root entries, it's the path
  269. * to the entry itself.
  270. */
  271. get parentPath() {
  272. return (this.parent || this).fullpath();
  273. }
  274. /**
  275. * Deprecated alias for Dirent['parentPath'] Somewhat counterintuitively,
  276. * this property refers to the *parent* path, not the path object itself.
  277. *
  278. * @deprecated
  279. */
  280. get path() {
  281. return this.parentPath;
  282. }
  283. /**
  284. * Do not create new Path objects directly. They should always be accessed
  285. * via the PathScurry class or other methods on the Path class.
  286. *
  287. * @internal
  288. */
  289. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  290. this.name = name;
  291. this.#matchName = nocase ? normalizeNocase(name) : normalize(name);
  292. this.#type = type & TYPEMASK;
  293. this.nocase = nocase;
  294. this.roots = roots;
  295. this.root = root || this;
  296. this.#children = children;
  297. this.#fullpath = opts.fullpath;
  298. this.#relative = opts.relative;
  299. this.#relativePosix = opts.relativePosix;
  300. this.parent = opts.parent;
  301. if (this.parent) {
  302. this.#fs = this.parent.#fs;
  303. }
  304. else {
  305. this.#fs = fsFromOption(opts.fs);
  306. }
  307. }
  308. /**
  309. * Returns the depth of the Path object from its root.
  310. *
  311. * For example, a path at `/foo/bar` would have a depth of 2.
  312. */
  313. depth() {
  314. if (this.#depth !== undefined)
  315. return this.#depth;
  316. if (!this.parent)
  317. return (this.#depth = 0);
  318. return (this.#depth = this.parent.depth() + 1);
  319. }
  320. /**
  321. * @internal
  322. */
  323. childrenCache() {
  324. return this.#children;
  325. }
  326. /**
  327. * Get the Path object referenced by the string path, resolved from this Path
  328. */
  329. resolve(path) {
  330. if (!path) {
  331. return this;
  332. }
  333. const rootPath = this.getRootString(path);
  334. const dir = path.substring(rootPath.length);
  335. const dirParts = dir.split(this.splitSep);
  336. const result = rootPath ?
  337. this.getRoot(rootPath).#resolveParts(dirParts)
  338. : this.#resolveParts(dirParts);
  339. return result;
  340. }
  341. #resolveParts(dirParts) {
  342. let p = this;
  343. for (const part of dirParts) {
  344. p = p.child(part);
  345. }
  346. return p;
  347. }
  348. /**
  349. * Returns the cached children Path objects, if still available. If they
  350. * have fallen out of the cache, then returns an empty array, and resets the
  351. * READDIR_CALLED bit, so that future calls to readdir() will require an fs
  352. * lookup.
  353. *
  354. * @internal
  355. */
  356. children() {
  357. const cached = this.#children.get(this);
  358. if (cached) {
  359. return cached;
  360. }
  361. const children = Object.assign([], { provisional: 0 });
  362. this.#children.set(this, children);
  363. this.#type &= ~READDIR_CALLED;
  364. return children;
  365. }
  366. /**
  367. * Resolves a path portion and returns or creates the child Path.
  368. *
  369. * Returns `this` if pathPart is `''` or `'.'`, or `parent` if pathPart is
  370. * `'..'`.
  371. *
  372. * This should not be called directly. If `pathPart` contains any path
  373. * separators, it will lead to unsafe undefined behavior.
  374. *
  375. * Use `Path.resolve()` instead.
  376. *
  377. * @internal
  378. */
  379. child(pathPart, opts) {
  380. if (pathPart === '' || pathPart === '.') {
  381. return this;
  382. }
  383. if (pathPart === '..') {
  384. return this.parent || this;
  385. }
  386. // find the child
  387. const children = this.children();
  388. const name = this.nocase ? normalizeNocase(pathPart) : normalize(pathPart);
  389. for (const p of children) {
  390. if (p.#matchName === name) {
  391. return p;
  392. }
  393. }
  394. // didn't find it, create provisional child, since it might not
  395. // actually exist. If we know the parent isn't a dir, then
  396. // in fact it CAN'T exist.
  397. const s = this.parent ? this.sep : '';
  398. const fullpath = this.#fullpath ? this.#fullpath + s + pathPart : undefined;
  399. const pchild = this.newChild(pathPart, UNKNOWN, {
  400. ...opts,
  401. parent: this,
  402. fullpath,
  403. });
  404. if (!this.canReaddir()) {
  405. pchild.#type |= ENOENT;
  406. }
  407. // don't have to update provisional, because if we have real children,
  408. // then provisional is set to children.length, otherwise a lower number
  409. children.push(pchild);
  410. return pchild;
  411. }
  412. /**
  413. * The relative path from the cwd. If it does not share an ancestor with
  414. * the cwd, then this ends up being equivalent to the fullpath()
  415. */
  416. relative() {
  417. if (this.isCWD)
  418. return '';
  419. if (this.#relative !== undefined) {
  420. return this.#relative;
  421. }
  422. const name = this.name;
  423. const p = this.parent;
  424. if (!p) {
  425. return (this.#relative = this.name);
  426. }
  427. const pv = p.relative();
  428. return pv + (!pv || !p.parent ? '' : this.sep) + name;
  429. }
  430. /**
  431. * The relative path from the cwd, using / as the path separator.
  432. * If it does not share an ancestor with
  433. * the cwd, then this ends up being equivalent to the fullpathPosix()
  434. * On posix systems, this is identical to relative().
  435. */
  436. relativePosix() {
  437. if (this.sep === '/')
  438. return this.relative();
  439. if (this.isCWD)
  440. return '';
  441. if (this.#relativePosix !== undefined)
  442. return this.#relativePosix;
  443. const name = this.name;
  444. const p = this.parent;
  445. if (!p) {
  446. return (this.#relativePosix = this.fullpathPosix());
  447. }
  448. const pv = p.relativePosix();
  449. return pv + (!pv || !p.parent ? '' : '/') + name;
  450. }
  451. /**
  452. * The fully resolved path string for this Path entry
  453. */
  454. fullpath() {
  455. if (this.#fullpath !== undefined) {
  456. return this.#fullpath;
  457. }
  458. const name = this.name;
  459. const p = this.parent;
  460. if (!p) {
  461. return (this.#fullpath = this.name);
  462. }
  463. const pv = p.fullpath();
  464. const fp = pv + (!p.parent ? '' : this.sep) + name;
  465. return (this.#fullpath = fp);
  466. }
  467. /**
  468. * On platforms other than windows, this is identical to fullpath.
  469. *
  470. * On windows, this is overridden to return the forward-slash form of the
  471. * full UNC path.
  472. */
  473. fullpathPosix() {
  474. if (this.#fullpathPosix !== undefined)
  475. return this.#fullpathPosix;
  476. if (this.sep === '/')
  477. return (this.#fullpathPosix = this.fullpath());
  478. if (!this.parent) {
  479. const p = this.fullpath().replace(/\\/g, '/');
  480. if (/^[a-z]:\//i.test(p)) {
  481. return (this.#fullpathPosix = `//?/${p}`);
  482. }
  483. else {
  484. return (this.#fullpathPosix = p);
  485. }
  486. }
  487. const p = this.parent;
  488. const pfpp = p.fullpathPosix();
  489. const fpp = pfpp + (!pfpp || !p.parent ? '' : '/') + this.name;
  490. return (this.#fullpathPosix = fpp);
  491. }
  492. /**
  493. * Is the Path of an unknown type?
  494. *
  495. * Note that we might know *something* about it if there has been a previous
  496. * filesystem operation, for example that it does not exist, or is not a
  497. * link, or whether it has child entries.
  498. */
  499. isUnknown() {
  500. return (this.#type & IFMT) === UNKNOWN;
  501. }
  502. isType(type) {
  503. return this[`is${type}`]();
  504. }
  505. getType() {
  506. return (this.isUnknown() ? 'Unknown'
  507. : this.isDirectory() ? 'Directory'
  508. : this.isFile() ? 'File'
  509. : this.isSymbolicLink() ? 'SymbolicLink'
  510. : this.isFIFO() ? 'FIFO'
  511. : this.isCharacterDevice() ? 'CharacterDevice'
  512. : this.isBlockDevice() ? 'BlockDevice'
  513. : /* c8 ignore start */ this.isSocket() ? 'Socket'
  514. : 'Unknown');
  515. /* c8 ignore stop */
  516. }
  517. /**
  518. * Is the Path a regular file?
  519. */
  520. isFile() {
  521. return (this.#type & IFMT) === IFREG;
  522. }
  523. /**
  524. * Is the Path a directory?
  525. */
  526. isDirectory() {
  527. return (this.#type & IFMT) === IFDIR;
  528. }
  529. /**
  530. * Is the path a character device?
  531. */
  532. isCharacterDevice() {
  533. return (this.#type & IFMT) === IFCHR;
  534. }
  535. /**
  536. * Is the path a block device?
  537. */
  538. isBlockDevice() {
  539. return (this.#type & IFMT) === IFBLK;
  540. }
  541. /**
  542. * Is the path a FIFO pipe?
  543. */
  544. isFIFO() {
  545. return (this.#type & IFMT) === IFIFO;
  546. }
  547. /**
  548. * Is the path a socket?
  549. */
  550. isSocket() {
  551. return (this.#type & IFMT) === IFSOCK;
  552. }
  553. /**
  554. * Is the path a symbolic link?
  555. */
  556. isSymbolicLink() {
  557. return (this.#type & IFLNK) === IFLNK;
  558. }
  559. /**
  560. * Return the entry if it has been subject of a successful lstat, or
  561. * undefined otherwise.
  562. *
  563. * Does not read the filesystem, so an undefined result *could* simply
  564. * mean that we haven't called lstat on it.
  565. */
  566. lstatCached() {
  567. return this.#type & LSTAT_CALLED ? this : undefined;
  568. }
  569. /**
  570. * Return the cached link target if the entry has been the subject of a
  571. * successful readlink, or undefined otherwise.
  572. *
  573. * Does not read the filesystem, so an undefined result *could* just mean we
  574. * don't have any cached data. Only use it if you are very sure that a
  575. * readlink() has been called at some point.
  576. */
  577. readlinkCached() {
  578. return this.#linkTarget;
  579. }
  580. /**
  581. * Returns the cached realpath target if the entry has been the subject
  582. * of a successful realpath, or undefined otherwise.
  583. *
  584. * Does not read the filesystem, so an undefined result *could* just mean we
  585. * don't have any cached data. Only use it if you are very sure that a
  586. * realpath() has been called at some point.
  587. */
  588. realpathCached() {
  589. return this.#realpath;
  590. }
  591. /**
  592. * Returns the cached child Path entries array if the entry has been the
  593. * subject of a successful readdir(), or [] otherwise.
  594. *
  595. * Does not read the filesystem, so an empty array *could* just mean we
  596. * don't have any cached data. Only use it if you are very sure that a
  597. * readdir() has been called recently enough to still be valid.
  598. */
  599. readdirCached() {
  600. const children = this.children();
  601. return children.slice(0, children.provisional);
  602. }
  603. /**
  604. * Return true if it's worth trying to readlink. Ie, we don't (yet) have
  605. * any indication that readlink will definitely fail.
  606. *
  607. * Returns false if the path is known to not be a symlink, if a previous
  608. * readlink failed, or if the entry does not exist.
  609. */
  610. canReadlink() {
  611. if (this.#linkTarget)
  612. return true;
  613. if (!this.parent)
  614. return false;
  615. // cases where it cannot possibly succeed
  616. const ifmt = this.#type & IFMT;
  617. return !((ifmt !== UNKNOWN && ifmt !== IFLNK) ||
  618. this.#type & ENOREADLINK ||
  619. this.#type & ENOENT);
  620. }
  621. /**
  622. * Return true if readdir has previously been successfully called on this
  623. * path, indicating that cachedReaddir() is likely valid.
  624. */
  625. calledReaddir() {
  626. return !!(this.#type & READDIR_CALLED);
  627. }
  628. /**
  629. * Returns true if the path is known to not exist. That is, a previous lstat
  630. * or readdir failed to verify its existence when that would have been
  631. * expected, or a parent entry was marked either enoent or enotdir.
  632. */
  633. isENOENT() {
  634. return !!(this.#type & ENOENT);
  635. }
  636. /**
  637. * Return true if the path is a match for the given path name. This handles
  638. * case sensitivity and unicode normalization.
  639. *
  640. * Note: even on case-sensitive systems, it is **not** safe to test the
  641. * equality of the `.name` property to determine whether a given pathname
  642. * matches, due to unicode normalization mismatches.
  643. *
  644. * Always use this method instead of testing the `path.name` property
  645. * directly.
  646. */
  647. isNamed(n) {
  648. return !this.nocase ?
  649. this.#matchName === normalize(n)
  650. : this.#matchName === normalizeNocase(n);
  651. }
  652. /**
  653. * Return the Path object corresponding to the target of a symbolic link.
  654. *
  655. * If the Path is not a symbolic link, or if the readlink call fails for any
  656. * reason, `undefined` is returned.
  657. *
  658. * Result is cached, and thus may be outdated if the filesystem is mutated.
  659. */
  660. async readlink() {
  661. const target = this.#linkTarget;
  662. if (target) {
  663. return target;
  664. }
  665. if (!this.canReadlink()) {
  666. return undefined;
  667. }
  668. /* c8 ignore start */
  669. // already covered by the canReadlink test, here for ts grumples
  670. if (!this.parent) {
  671. return undefined;
  672. }
  673. /* c8 ignore stop */
  674. try {
  675. const read = await this.#fs.promises.readlink(this.fullpath());
  676. const linkTarget = (await this.parent.realpath())?.resolve(read);
  677. if (linkTarget) {
  678. return (this.#linkTarget = linkTarget);
  679. }
  680. }
  681. catch (er) {
  682. this.#readlinkFail(er.code);
  683. return undefined;
  684. }
  685. }
  686. /**
  687. * Synchronous {@link PathBase.readlink}
  688. */
  689. readlinkSync() {
  690. const target = this.#linkTarget;
  691. if (target) {
  692. return target;
  693. }
  694. if (!this.canReadlink()) {
  695. return undefined;
  696. }
  697. /* c8 ignore start */
  698. // already covered by the canReadlink test, here for ts grumples
  699. if (!this.parent) {
  700. return undefined;
  701. }
  702. /* c8 ignore stop */
  703. try {
  704. const read = this.#fs.readlinkSync(this.fullpath());
  705. const linkTarget = this.parent.realpathSync()?.resolve(read);
  706. if (linkTarget) {
  707. return (this.#linkTarget = linkTarget);
  708. }
  709. }
  710. catch (er) {
  711. this.#readlinkFail(er.code);
  712. return undefined;
  713. }
  714. }
  715. #readdirSuccess(children) {
  716. // succeeded, mark readdir called bit
  717. this.#type |= READDIR_CALLED;
  718. // mark all remaining provisional children as ENOENT
  719. for (let p = children.provisional; p < children.length; p++) {
  720. const c = children[p];
  721. if (c)
  722. c.#markENOENT();
  723. }
  724. }
  725. #markENOENT() {
  726. // mark as UNKNOWN and ENOENT
  727. if (this.#type & ENOENT)
  728. return;
  729. this.#type = (this.#type | ENOENT) & IFMT_UNKNOWN;
  730. this.#markChildrenENOENT();
  731. }
  732. #markChildrenENOENT() {
  733. // all children are provisional and do not exist
  734. const children = this.children();
  735. children.provisional = 0;
  736. for (const p of children) {
  737. p.#markENOENT();
  738. }
  739. }
  740. #markENOREALPATH() {
  741. this.#type |= ENOREALPATH;
  742. this.#markENOTDIR();
  743. }
  744. // save the information when we know the entry is not a dir
  745. #markENOTDIR() {
  746. // entry is not a directory, so any children can't exist.
  747. // this *should* be impossible, since any children created
  748. // after it's been marked ENOTDIR should be marked ENOENT,
  749. // so it won't even get to this point.
  750. /* c8 ignore start */
  751. if (this.#type & ENOTDIR)
  752. return;
  753. /* c8 ignore stop */
  754. let t = this.#type;
  755. // this could happen if we stat a dir, then delete it,
  756. // then try to read it or one of its children.
  757. if ((t & IFMT) === IFDIR)
  758. t &= IFMT_UNKNOWN;
  759. this.#type = t | ENOTDIR;
  760. this.#markChildrenENOENT();
  761. }
  762. #readdirFail(code = '') {
  763. // markENOTDIR and markENOENT also set provisional=0
  764. if (code === 'ENOTDIR' || code === 'EPERM') {
  765. this.#markENOTDIR();
  766. }
  767. else if (code === 'ENOENT') {
  768. this.#markENOENT();
  769. }
  770. else {
  771. this.children().provisional = 0;
  772. }
  773. }
  774. #lstatFail(code = '') {
  775. // Windows just raises ENOENT in this case, disable for win CI
  776. /* c8 ignore start */
  777. if (code === 'ENOTDIR') {
  778. // already know it has a parent by this point
  779. const p = this.parent;
  780. p.#markENOTDIR();
  781. }
  782. else if (code === 'ENOENT') {
  783. /* c8 ignore stop */
  784. this.#markENOENT();
  785. }
  786. }
  787. #readlinkFail(code = '') {
  788. let ter = this.#type;
  789. ter |= ENOREADLINK;
  790. if (code === 'ENOENT')
  791. ter |= ENOENT;
  792. // windows gets a weird error when you try to readlink a file
  793. if (code === 'EINVAL' || code === 'UNKNOWN') {
  794. // exists, but not a symlink, we don't know WHAT it is, so remove
  795. // all IFMT bits.
  796. ter &= IFMT_UNKNOWN;
  797. }
  798. this.#type = ter;
  799. // windows just gets ENOENT in this case. We do cover the case,
  800. // just disabled because it's impossible on Windows CI
  801. /* c8 ignore start */
  802. if (code === 'ENOTDIR' && this.parent) {
  803. this.parent.#markENOTDIR();
  804. }
  805. /* c8 ignore stop */
  806. }
  807. #readdirAddChild(e, c) {
  808. return (this.#readdirMaybePromoteChild(e, c) ||
  809. this.#readdirAddNewChild(e, c));
  810. }
  811. #readdirAddNewChild(e, c) {
  812. // alloc new entry at head, so it's never provisional
  813. const type = entToType(e);
  814. const child = this.newChild(e.name, type, { parent: this });
  815. const ifmt = child.#type & IFMT;
  816. if (ifmt !== IFDIR && ifmt !== IFLNK && ifmt !== UNKNOWN) {
  817. child.#type |= ENOTDIR;
  818. }
  819. c.unshift(child);
  820. c.provisional++;
  821. return child;
  822. }
  823. #readdirMaybePromoteChild(e, c) {
  824. for (let p = c.provisional; p < c.length; p++) {
  825. const pchild = c[p];
  826. const name = this.nocase ? normalizeNocase(e.name) : normalize(e.name);
  827. if (name !== pchild.#matchName) {
  828. continue;
  829. }
  830. return this.#readdirPromoteChild(e, pchild, p, c);
  831. }
  832. }
  833. #readdirPromoteChild(e, p, index, c) {
  834. const v = p.name;
  835. // retain any other flags, but set ifmt from dirent
  836. p.#type = (p.#type & IFMT_UNKNOWN) | entToType(e);
  837. // case sensitivity fixing when we learn the true name.
  838. if (v !== e.name)
  839. p.name = e.name;
  840. // just advance provisional index (potentially off the list),
  841. // otherwise we have to splice/pop it out and re-insert at head
  842. if (index !== c.provisional) {
  843. if (index === c.length - 1)
  844. c.pop();
  845. else
  846. c.splice(index, 1);
  847. c.unshift(p);
  848. }
  849. c.provisional++;
  850. return p;
  851. }
  852. /**
  853. * Call lstat() on this Path, and update all known information that can be
  854. * determined.
  855. *
  856. * Note that unlike `fs.lstat()`, the returned value does not contain some
  857. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  858. * information is required, you will need to call `fs.lstat` yourself.
  859. *
  860. * If the Path refers to a nonexistent file, or if the lstat call fails for
  861. * any reason, `undefined` is returned. Otherwise the updated Path object is
  862. * returned.
  863. *
  864. * Results are cached, and thus may be out of date if the filesystem is
  865. * mutated.
  866. */
  867. async lstat() {
  868. if ((this.#type & ENOENT) === 0) {
  869. try {
  870. this.#applyStat(await this.#fs.promises.lstat(this.fullpath()));
  871. return this;
  872. }
  873. catch (er) {
  874. this.#lstatFail(er.code);
  875. }
  876. }
  877. }
  878. /**
  879. * synchronous {@link PathBase.lstat}
  880. */
  881. lstatSync() {
  882. if ((this.#type & ENOENT) === 0) {
  883. try {
  884. this.#applyStat(this.#fs.lstatSync(this.fullpath()));
  885. return this;
  886. }
  887. catch (er) {
  888. this.#lstatFail(er.code);
  889. }
  890. }
  891. }
  892. #applyStat(st) {
  893. const { atime, atimeMs, birthtime, birthtimeMs, blksize, blocks, ctime, ctimeMs, dev, gid, ino, mode, mtime, mtimeMs, nlink, rdev, size, uid, } = st;
  894. this.#atime = atime;
  895. this.#atimeMs = atimeMs;
  896. this.#birthtime = birthtime;
  897. this.#birthtimeMs = birthtimeMs;
  898. this.#blksize = blksize;
  899. this.#blocks = blocks;
  900. this.#ctime = ctime;
  901. this.#ctimeMs = ctimeMs;
  902. this.#dev = dev;
  903. this.#gid = gid;
  904. this.#ino = ino;
  905. this.#mode = mode;
  906. this.#mtime = mtime;
  907. this.#mtimeMs = mtimeMs;
  908. this.#nlink = nlink;
  909. this.#rdev = rdev;
  910. this.#size = size;
  911. this.#uid = uid;
  912. const ifmt = entToType(st);
  913. // retain any other flags, but set the ifmt
  914. this.#type = (this.#type & IFMT_UNKNOWN) | ifmt | LSTAT_CALLED;
  915. if (ifmt !== UNKNOWN && ifmt !== IFDIR && ifmt !== IFLNK) {
  916. this.#type |= ENOTDIR;
  917. }
  918. }
  919. #onReaddirCB = [];
  920. #readdirCBInFlight = false;
  921. #callOnReaddirCB(children) {
  922. this.#readdirCBInFlight = false;
  923. const cbs = this.#onReaddirCB.slice();
  924. this.#onReaddirCB.length = 0;
  925. cbs.forEach(cb => cb(null, children));
  926. }
  927. /**
  928. * Standard node-style callback interface to get list of directory entries.
  929. *
  930. * If the Path cannot or does not contain any children, then an empty array
  931. * is returned.
  932. *
  933. * Results are cached, and thus may be out of date if the filesystem is
  934. * mutated.
  935. *
  936. * @param cb The callback called with (er, entries). Note that the `er`
  937. * param is somewhat extraneous, as all readdir() errors are handled and
  938. * simply result in an empty set of entries being returned.
  939. * @param allowZalgo Boolean indicating that immediately known results should
  940. * *not* be deferred with `queueMicrotask`. Defaults to `false`. Release
  941. * zalgo at your peril, the dark pony lord is devious and unforgiving.
  942. */
  943. readdirCB(cb, allowZalgo = false) {
  944. if (!this.canReaddir()) {
  945. if (allowZalgo)
  946. cb(null, []);
  947. else
  948. queueMicrotask(() => cb(null, []));
  949. return;
  950. }
  951. const children = this.children();
  952. if (this.calledReaddir()) {
  953. const c = children.slice(0, children.provisional);
  954. if (allowZalgo)
  955. cb(null, c);
  956. else
  957. queueMicrotask(() => cb(null, c));
  958. return;
  959. }
  960. // don't have to worry about zalgo at this point.
  961. this.#onReaddirCB.push(cb);
  962. if (this.#readdirCBInFlight) {
  963. return;
  964. }
  965. this.#readdirCBInFlight = true;
  966. // else read the directory, fill up children
  967. // de-provisionalize any provisional children.
  968. const fullpath = this.fullpath();
  969. this.#fs.readdir(fullpath, { withFileTypes: true }, (er, entries) => {
  970. if (er) {
  971. this.#readdirFail(er.code);
  972. children.provisional = 0;
  973. }
  974. else {
  975. // if we didn't get an error, we always get entries.
  976. //@ts-ignore
  977. for (const e of entries) {
  978. this.#readdirAddChild(e, children);
  979. }
  980. this.#readdirSuccess(children);
  981. }
  982. this.#callOnReaddirCB(children.slice(0, children.provisional));
  983. return;
  984. });
  985. }
  986. #asyncReaddirInFlight;
  987. /**
  988. * Return an array of known child entries.
  989. *
  990. * If the Path cannot or does not contain any children, then an empty array
  991. * is returned.
  992. *
  993. * Results are cached, and thus may be out of date if the filesystem is
  994. * mutated.
  995. */
  996. async readdir() {
  997. if (!this.canReaddir()) {
  998. return [];
  999. }
  1000. const children = this.children();
  1001. if (this.calledReaddir()) {
  1002. return children.slice(0, children.provisional);
  1003. }
  1004. // else read the directory, fill up children
  1005. // de-provisionalize any provisional children.
  1006. const fullpath = this.fullpath();
  1007. if (this.#asyncReaddirInFlight) {
  1008. await this.#asyncReaddirInFlight;
  1009. }
  1010. else {
  1011. /* c8 ignore start */
  1012. let resolve = () => { };
  1013. /* c8 ignore stop */
  1014. this.#asyncReaddirInFlight = new Promise(res => (resolve = res));
  1015. try {
  1016. for (const e of await this.#fs.promises.readdir(fullpath, {
  1017. withFileTypes: true,
  1018. })) {
  1019. this.#readdirAddChild(e, children);
  1020. }
  1021. this.#readdirSuccess(children);
  1022. }
  1023. catch (er) {
  1024. this.#readdirFail(er.code);
  1025. children.provisional = 0;
  1026. }
  1027. this.#asyncReaddirInFlight = undefined;
  1028. resolve();
  1029. }
  1030. return children.slice(0, children.provisional);
  1031. }
  1032. /**
  1033. * synchronous {@link PathBase.readdir}
  1034. */
  1035. readdirSync() {
  1036. if (!this.canReaddir()) {
  1037. return [];
  1038. }
  1039. const children = this.children();
  1040. if (this.calledReaddir()) {
  1041. return children.slice(0, children.provisional);
  1042. }
  1043. // else read the directory, fill up children
  1044. // de-provisionalize any provisional children.
  1045. const fullpath = this.fullpath();
  1046. try {
  1047. for (const e of this.#fs.readdirSync(fullpath, {
  1048. withFileTypes: true,
  1049. })) {
  1050. this.#readdirAddChild(e, children);
  1051. }
  1052. this.#readdirSuccess(children);
  1053. }
  1054. catch (er) {
  1055. this.#readdirFail(er.code);
  1056. children.provisional = 0;
  1057. }
  1058. return children.slice(0, children.provisional);
  1059. }
  1060. canReaddir() {
  1061. if (this.#type & ENOCHILD)
  1062. return false;
  1063. const ifmt = IFMT & this.#type;
  1064. // we always set ENOTDIR when setting IFMT, so should be impossible
  1065. /* c8 ignore start */
  1066. if (!(ifmt === UNKNOWN || ifmt === IFDIR || ifmt === IFLNK)) {
  1067. return false;
  1068. }
  1069. /* c8 ignore stop */
  1070. return true;
  1071. }
  1072. shouldWalk(dirs, walkFilter) {
  1073. return ((this.#type & IFDIR) === IFDIR &&
  1074. !(this.#type & ENOCHILD) &&
  1075. !dirs.has(this) &&
  1076. (!walkFilter || walkFilter(this)));
  1077. }
  1078. /**
  1079. * Return the Path object corresponding to path as resolved
  1080. * by realpath(3).
  1081. *
  1082. * If the realpath call fails for any reason, `undefined` is returned.
  1083. *
  1084. * Result is cached, and thus may be outdated if the filesystem is mutated.
  1085. * On success, returns a Path object.
  1086. */
  1087. async realpath() {
  1088. if (this.#realpath)
  1089. return this.#realpath;
  1090. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  1091. return undefined;
  1092. try {
  1093. const rp = await this.#fs.promises.realpath(this.fullpath());
  1094. return (this.#realpath = this.resolve(rp));
  1095. }
  1096. catch (_) {
  1097. this.#markENOREALPATH();
  1098. }
  1099. }
  1100. /**
  1101. * Synchronous {@link realpath}
  1102. */
  1103. realpathSync() {
  1104. if (this.#realpath)
  1105. return this.#realpath;
  1106. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  1107. return undefined;
  1108. try {
  1109. const rp = this.#fs.realpathSync(this.fullpath());
  1110. return (this.#realpath = this.resolve(rp));
  1111. }
  1112. catch (_) {
  1113. this.#markENOREALPATH();
  1114. }
  1115. }
  1116. /**
  1117. * Internal method to mark this Path object as the scurry cwd,
  1118. * called by {@link PathScurry#chdir}
  1119. *
  1120. * @internal
  1121. */
  1122. [setAsCwd](oldCwd) {
  1123. if (oldCwd === this)
  1124. return;
  1125. oldCwd.isCWD = false;
  1126. this.isCWD = true;
  1127. const changed = new Set([]);
  1128. let rp = [];
  1129. let p = this;
  1130. while (p && p.parent) {
  1131. changed.add(p);
  1132. p.#relative = rp.join(this.sep);
  1133. p.#relativePosix = rp.join('/');
  1134. p = p.parent;
  1135. rp.push('..');
  1136. }
  1137. // now un-memoize parents of old cwd
  1138. p = oldCwd;
  1139. while (p && p.parent && !changed.has(p)) {
  1140. p.#relative = undefined;
  1141. p.#relativePosix = undefined;
  1142. p = p.parent;
  1143. }
  1144. }
  1145. }
  1146. /**
  1147. * Path class used on win32 systems
  1148. *
  1149. * Uses `'\\'` as the path separator for returned paths, either `'\\'` or `'/'`
  1150. * as the path separator for parsing paths.
  1151. */
  1152. export class PathWin32 extends PathBase {
  1153. /**
  1154. * Separator for generating path strings.
  1155. */
  1156. sep = '\\';
  1157. /**
  1158. * Separator for parsing path strings.
  1159. */
  1160. splitSep = eitherSep;
  1161. /**
  1162. * Do not create new Path objects directly. They should always be accessed
  1163. * via the PathScurry class or other methods on the Path class.
  1164. *
  1165. * @internal
  1166. */
  1167. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  1168. super(name, type, root, roots, nocase, children, opts);
  1169. }
  1170. /**
  1171. * @internal
  1172. */
  1173. newChild(name, type = UNKNOWN, opts = {}) {
  1174. return new PathWin32(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  1175. }
  1176. /**
  1177. * @internal
  1178. */
  1179. getRootString(path) {
  1180. return win32.parse(path).root;
  1181. }
  1182. /**
  1183. * @internal
  1184. */
  1185. getRoot(rootPath) {
  1186. rootPath = uncToDrive(rootPath.toUpperCase());
  1187. if (rootPath === this.root.name) {
  1188. return this.root;
  1189. }
  1190. // ok, not that one, check if it matches another we know about
  1191. for (const [compare, root] of Object.entries(this.roots)) {
  1192. if (this.sameRoot(rootPath, compare)) {
  1193. return (this.roots[rootPath] = root);
  1194. }
  1195. }
  1196. // otherwise, have to create a new one.
  1197. return (this.roots[rootPath] = new PathScurryWin32(rootPath, this).root);
  1198. }
  1199. /**
  1200. * @internal
  1201. */
  1202. sameRoot(rootPath, compare = this.root.name) {
  1203. // windows can (rarely) have case-sensitive filesystem, but
  1204. // UNC and drive letters are always case-insensitive, and canonically
  1205. // represented uppercase.
  1206. rootPath = rootPath
  1207. .toUpperCase()
  1208. .replace(/\//g, '\\')
  1209. .replace(uncDriveRegexp, '$1\\');
  1210. return rootPath === compare;
  1211. }
  1212. }
  1213. /**
  1214. * Path class used on all posix systems.
  1215. *
  1216. * Uses `'/'` as the path separator.
  1217. */
  1218. export class PathPosix extends PathBase {
  1219. /**
  1220. * separator for parsing path strings
  1221. */
  1222. splitSep = '/';
  1223. /**
  1224. * separator for generating path strings
  1225. */
  1226. sep = '/';
  1227. /**
  1228. * Do not create new Path objects directly. They should always be accessed
  1229. * via the PathScurry class or other methods on the Path class.
  1230. *
  1231. * @internal
  1232. */
  1233. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  1234. super(name, type, root, roots, nocase, children, opts);
  1235. }
  1236. /**
  1237. * @internal
  1238. */
  1239. getRootString(path) {
  1240. return path.startsWith('/') ? '/' : '';
  1241. }
  1242. /**
  1243. * @internal
  1244. */
  1245. getRoot(_rootPath) {
  1246. return this.root;
  1247. }
  1248. /**
  1249. * @internal
  1250. */
  1251. newChild(name, type = UNKNOWN, opts = {}) {
  1252. return new PathPosix(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  1253. }
  1254. }
  1255. /**
  1256. * The base class for all PathScurry classes, providing the interface for path
  1257. * resolution and filesystem operations.
  1258. *
  1259. * Typically, you should *not* instantiate this class directly, but rather one
  1260. * of the platform-specific classes, or the exported {@link PathScurry} which
  1261. * defaults to the current platform.
  1262. */
  1263. export class PathScurryBase {
  1264. /**
  1265. * The root Path entry for the current working directory of this Scurry
  1266. */
  1267. root;
  1268. /**
  1269. * The string path for the root of this Scurry's current working directory
  1270. */
  1271. rootPath;
  1272. /**
  1273. * A collection of all roots encountered, referenced by rootPath
  1274. */
  1275. roots;
  1276. /**
  1277. * The Path entry corresponding to this PathScurry's current working directory.
  1278. */
  1279. cwd;
  1280. #resolveCache;
  1281. #resolvePosixCache;
  1282. #children;
  1283. /**
  1284. * Perform path comparisons case-insensitively.
  1285. *
  1286. * Defaults true on Darwin and Windows systems, false elsewhere.
  1287. */
  1288. nocase;
  1289. #fs;
  1290. /**
  1291. * This class should not be instantiated directly.
  1292. *
  1293. * Use PathScurryWin32, PathScurryDarwin, PathScurryPosix, or PathScurry
  1294. *
  1295. * @internal
  1296. */
  1297. constructor(cwd = process.cwd(), pathImpl, sep, { nocase, childrenCacheSize = 16 * 1024, fs = defaultFS, } = {}) {
  1298. this.#fs = fsFromOption(fs);
  1299. if (cwd instanceof URL || cwd.startsWith('file://')) {
  1300. cwd = fileURLToPath(cwd);
  1301. }
  1302. // resolve and split root, and then add to the store.
  1303. // this is the only time we call path.resolve()
  1304. const cwdPath = pathImpl.resolve(cwd);
  1305. this.roots = Object.create(null);
  1306. this.rootPath = this.parseRootPath(cwdPath);
  1307. this.#resolveCache = new ResolveCache();
  1308. this.#resolvePosixCache = new ResolveCache();
  1309. this.#children = new ChildrenCache(childrenCacheSize);
  1310. const split = cwdPath.substring(this.rootPath.length).split(sep);
  1311. // resolve('/') leaves '', splits to [''], we don't want that.
  1312. if (split.length === 1 && !split[0]) {
  1313. split.pop();
  1314. }
  1315. /* c8 ignore start */
  1316. if (nocase === undefined) {
  1317. throw new TypeError('must provide nocase setting to PathScurryBase ctor');
  1318. }
  1319. /* c8 ignore stop */
  1320. this.nocase = nocase;
  1321. this.root = this.newRoot(this.#fs);
  1322. this.roots[this.rootPath] = this.root;
  1323. let prev = this.root;
  1324. let len = split.length - 1;
  1325. const joinSep = pathImpl.sep;
  1326. let abs = this.rootPath;
  1327. let sawFirst = false;
  1328. for (const part of split) {
  1329. const l = len--;
  1330. prev = prev.child(part, {
  1331. relative: new Array(l).fill('..').join(joinSep),
  1332. relativePosix: new Array(l).fill('..').join('/'),
  1333. fullpath: (abs += (sawFirst ? '' : joinSep) + part),
  1334. });
  1335. sawFirst = true;
  1336. }
  1337. this.cwd = prev;
  1338. }
  1339. /**
  1340. * Get the depth of a provided path, string, or the cwd
  1341. */
  1342. depth(path = this.cwd) {
  1343. if (typeof path === 'string') {
  1344. path = this.cwd.resolve(path);
  1345. }
  1346. return path.depth();
  1347. }
  1348. /**
  1349. * Return the cache of child entries. Exposed so subclasses can create
  1350. * child Path objects in a platform-specific way.
  1351. *
  1352. * @internal
  1353. */
  1354. childrenCache() {
  1355. return this.#children;
  1356. }
  1357. /**
  1358. * Resolve one or more path strings to a resolved string
  1359. *
  1360. * Same interface as require('path').resolve.
  1361. *
  1362. * Much faster than path.resolve() when called multiple times for the same
  1363. * path, because the resolved Path objects are cached. Much slower
  1364. * otherwise.
  1365. */
  1366. resolve(...paths) {
  1367. // first figure out the minimum number of paths we have to test
  1368. // we always start at cwd, but any absolutes will bump the start
  1369. let r = '';
  1370. for (let i = paths.length - 1; i >= 0; i--) {
  1371. const p = paths[i];
  1372. if (!p || p === '.')
  1373. continue;
  1374. r = r ? `${p}/${r}` : p;
  1375. if (this.isAbsolute(p)) {
  1376. break;
  1377. }
  1378. }
  1379. const cached = this.#resolveCache.get(r);
  1380. if (cached !== undefined) {
  1381. return cached;
  1382. }
  1383. const result = this.cwd.resolve(r).fullpath();
  1384. this.#resolveCache.set(r, result);
  1385. return result;
  1386. }
  1387. /**
  1388. * Resolve one or more path strings to a resolved string, returning
  1389. * the posix path. Identical to .resolve() on posix systems, but on
  1390. * windows will return a forward-slash separated UNC path.
  1391. *
  1392. * Same interface as require('path').resolve.
  1393. *
  1394. * Much faster than path.resolve() when called multiple times for the same
  1395. * path, because the resolved Path objects are cached. Much slower
  1396. * otherwise.
  1397. */
  1398. resolvePosix(...paths) {
  1399. // first figure out the minimum number of paths we have to test
  1400. // we always start at cwd, but any absolutes will bump the start
  1401. let r = '';
  1402. for (let i = paths.length - 1; i >= 0; i--) {
  1403. const p = paths[i];
  1404. if (!p || p === '.')
  1405. continue;
  1406. r = r ? `${p}/${r}` : p;
  1407. if (this.isAbsolute(p)) {
  1408. break;
  1409. }
  1410. }
  1411. const cached = this.#resolvePosixCache.get(r);
  1412. if (cached !== undefined) {
  1413. return cached;
  1414. }
  1415. const result = this.cwd.resolve(r).fullpathPosix();
  1416. this.#resolvePosixCache.set(r, result);
  1417. return result;
  1418. }
  1419. /**
  1420. * find the relative path from the cwd to the supplied path string or entry
  1421. */
  1422. relative(entry = this.cwd) {
  1423. if (typeof entry === 'string') {
  1424. entry = this.cwd.resolve(entry);
  1425. }
  1426. return entry.relative();
  1427. }
  1428. /**
  1429. * find the relative path from the cwd to the supplied path string or
  1430. * entry, using / as the path delimiter, even on Windows.
  1431. */
  1432. relativePosix(entry = this.cwd) {
  1433. if (typeof entry === 'string') {
  1434. entry = this.cwd.resolve(entry);
  1435. }
  1436. return entry.relativePosix();
  1437. }
  1438. /**
  1439. * Return the basename for the provided string or Path object
  1440. */
  1441. basename(entry = this.cwd) {
  1442. if (typeof entry === 'string') {
  1443. entry = this.cwd.resolve(entry);
  1444. }
  1445. return entry.name;
  1446. }
  1447. /**
  1448. * Return the dirname for the provided string or Path object
  1449. */
  1450. dirname(entry = this.cwd) {
  1451. if (typeof entry === 'string') {
  1452. entry = this.cwd.resolve(entry);
  1453. }
  1454. return (entry.parent || entry).fullpath();
  1455. }
  1456. async readdir(entry = this.cwd, opts = {
  1457. withFileTypes: true,
  1458. }) {
  1459. if (typeof entry === 'string') {
  1460. entry = this.cwd.resolve(entry);
  1461. }
  1462. else if (!(entry instanceof PathBase)) {
  1463. opts = entry;
  1464. entry = this.cwd;
  1465. }
  1466. const { withFileTypes } = opts;
  1467. if (!entry.canReaddir()) {
  1468. return [];
  1469. }
  1470. else {
  1471. const p = await entry.readdir();
  1472. return withFileTypes ? p : p.map(e => e.name);
  1473. }
  1474. }
  1475. readdirSync(entry = this.cwd, opts = {
  1476. withFileTypes: true,
  1477. }) {
  1478. if (typeof entry === 'string') {
  1479. entry = this.cwd.resolve(entry);
  1480. }
  1481. else if (!(entry instanceof PathBase)) {
  1482. opts = entry;
  1483. entry = this.cwd;
  1484. }
  1485. const { withFileTypes = true } = opts;
  1486. if (!entry.canReaddir()) {
  1487. return [];
  1488. }
  1489. else if (withFileTypes) {
  1490. return entry.readdirSync();
  1491. }
  1492. else {
  1493. return entry.readdirSync().map(e => e.name);
  1494. }
  1495. }
  1496. /**
  1497. * Call lstat() on the string or Path object, and update all known
  1498. * information that can be determined.
  1499. *
  1500. * Note that unlike `fs.lstat()`, the returned value does not contain some
  1501. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  1502. * information is required, you will need to call `fs.lstat` yourself.
  1503. *
  1504. * If the Path refers to a nonexistent file, or if the lstat call fails for
  1505. * any reason, `undefined` is returned. Otherwise the updated Path object is
  1506. * returned.
  1507. *
  1508. * Results are cached, and thus may be out of date if the filesystem is
  1509. * mutated.
  1510. */
  1511. async lstat(entry = this.cwd) {
  1512. if (typeof entry === 'string') {
  1513. entry = this.cwd.resolve(entry);
  1514. }
  1515. return entry.lstat();
  1516. }
  1517. /**
  1518. * synchronous {@link PathScurryBase.lstat}
  1519. */
  1520. lstatSync(entry = this.cwd) {
  1521. if (typeof entry === 'string') {
  1522. entry = this.cwd.resolve(entry);
  1523. }
  1524. return entry.lstatSync();
  1525. }
  1526. async readlink(entry = this.cwd, { withFileTypes } = {
  1527. withFileTypes: false,
  1528. }) {
  1529. if (typeof entry === 'string') {
  1530. entry = this.cwd.resolve(entry);
  1531. }
  1532. else if (!(entry instanceof PathBase)) {
  1533. withFileTypes = entry.withFileTypes;
  1534. entry = this.cwd;
  1535. }
  1536. const e = await entry.readlink();
  1537. return withFileTypes ? e : e?.fullpath();
  1538. }
  1539. readlinkSync(entry = this.cwd, { withFileTypes } = {
  1540. withFileTypes: false,
  1541. }) {
  1542. if (typeof entry === 'string') {
  1543. entry = this.cwd.resolve(entry);
  1544. }
  1545. else if (!(entry instanceof PathBase)) {
  1546. withFileTypes = entry.withFileTypes;
  1547. entry = this.cwd;
  1548. }
  1549. const e = entry.readlinkSync();
  1550. return withFileTypes ? e : e?.fullpath();
  1551. }
  1552. async realpath(entry = this.cwd, { withFileTypes } = {
  1553. withFileTypes: false,
  1554. }) {
  1555. if (typeof entry === 'string') {
  1556. entry = this.cwd.resolve(entry);
  1557. }
  1558. else if (!(entry instanceof PathBase)) {
  1559. withFileTypes = entry.withFileTypes;
  1560. entry = this.cwd;
  1561. }
  1562. const e = await entry.realpath();
  1563. return withFileTypes ? e : e?.fullpath();
  1564. }
  1565. realpathSync(entry = this.cwd, { withFileTypes } = {
  1566. withFileTypes: false,
  1567. }) {
  1568. if (typeof entry === 'string') {
  1569. entry = this.cwd.resolve(entry);
  1570. }
  1571. else if (!(entry instanceof PathBase)) {
  1572. withFileTypes = entry.withFileTypes;
  1573. entry = this.cwd;
  1574. }
  1575. const e = entry.realpathSync();
  1576. return withFileTypes ? e : e?.fullpath();
  1577. }
  1578. async walk(entry = this.cwd, opts = {}) {
  1579. if (typeof entry === 'string') {
  1580. entry = this.cwd.resolve(entry);
  1581. }
  1582. else if (!(entry instanceof PathBase)) {
  1583. opts = entry;
  1584. entry = this.cwd;
  1585. }
  1586. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1587. const results = [];
  1588. if (!filter || filter(entry)) {
  1589. results.push(withFileTypes ? entry : entry.fullpath());
  1590. }
  1591. const dirs = new Set();
  1592. const walk = (dir, cb) => {
  1593. dirs.add(dir);
  1594. dir.readdirCB((er, entries) => {
  1595. /* c8 ignore start */
  1596. if (er) {
  1597. return cb(er);
  1598. }
  1599. /* c8 ignore stop */
  1600. let len = entries.length;
  1601. if (!len)
  1602. return cb();
  1603. const next = () => {
  1604. if (--len === 0) {
  1605. cb();
  1606. }
  1607. };
  1608. for (const e of entries) {
  1609. if (!filter || filter(e)) {
  1610. results.push(withFileTypes ? e : e.fullpath());
  1611. }
  1612. if (follow && e.isSymbolicLink()) {
  1613. e.realpath()
  1614. .then(r => (r?.isUnknown() ? r.lstat() : r))
  1615. .then(r => r?.shouldWalk(dirs, walkFilter) ? walk(r, next) : next());
  1616. }
  1617. else {
  1618. if (e.shouldWalk(dirs, walkFilter)) {
  1619. walk(e, next);
  1620. }
  1621. else {
  1622. next();
  1623. }
  1624. }
  1625. }
  1626. }, true); // zalgooooooo
  1627. };
  1628. const start = entry;
  1629. return new Promise((res, rej) => {
  1630. walk(start, er => {
  1631. /* c8 ignore start */
  1632. if (er)
  1633. return rej(er);
  1634. /* c8 ignore stop */
  1635. res(results);
  1636. });
  1637. });
  1638. }
  1639. walkSync(entry = this.cwd, opts = {}) {
  1640. if (typeof entry === 'string') {
  1641. entry = this.cwd.resolve(entry);
  1642. }
  1643. else if (!(entry instanceof PathBase)) {
  1644. opts = entry;
  1645. entry = this.cwd;
  1646. }
  1647. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1648. const results = [];
  1649. if (!filter || filter(entry)) {
  1650. results.push(withFileTypes ? entry : entry.fullpath());
  1651. }
  1652. const dirs = new Set([entry]);
  1653. for (const dir of dirs) {
  1654. const entries = dir.readdirSync();
  1655. for (const e of entries) {
  1656. if (!filter || filter(e)) {
  1657. results.push(withFileTypes ? e : e.fullpath());
  1658. }
  1659. let r = e;
  1660. if (e.isSymbolicLink()) {
  1661. if (!(follow && (r = e.realpathSync())))
  1662. continue;
  1663. if (r.isUnknown())
  1664. r.lstatSync();
  1665. }
  1666. if (r.shouldWalk(dirs, walkFilter)) {
  1667. dirs.add(r);
  1668. }
  1669. }
  1670. }
  1671. return results;
  1672. }
  1673. /**
  1674. * Support for `for await`
  1675. *
  1676. * Alias for {@link PathScurryBase.iterate}
  1677. *
  1678. * Note: As of Node 19, this is very slow, compared to other methods of
  1679. * walking. Consider using {@link PathScurryBase.stream} if memory overhead
  1680. * and backpressure are concerns, or {@link PathScurryBase.walk} if not.
  1681. */
  1682. [Symbol.asyncIterator]() {
  1683. return this.iterate();
  1684. }
  1685. iterate(entry = this.cwd, options = {}) {
  1686. // iterating async over the stream is significantly more performant,
  1687. // especially in the warm-cache scenario, because it buffers up directory
  1688. // entries in the background instead of waiting for a yield for each one.
  1689. if (typeof entry === 'string') {
  1690. entry = this.cwd.resolve(entry);
  1691. }
  1692. else if (!(entry instanceof PathBase)) {
  1693. options = entry;
  1694. entry = this.cwd;
  1695. }
  1696. return this.stream(entry, options)[Symbol.asyncIterator]();
  1697. }
  1698. /**
  1699. * Iterating over a PathScurry performs a synchronous walk.
  1700. *
  1701. * Alias for {@link PathScurryBase.iterateSync}
  1702. */
  1703. [Symbol.iterator]() {
  1704. return this.iterateSync();
  1705. }
  1706. *iterateSync(entry = this.cwd, opts = {}) {
  1707. if (typeof entry === 'string') {
  1708. entry = this.cwd.resolve(entry);
  1709. }
  1710. else if (!(entry instanceof PathBase)) {
  1711. opts = entry;
  1712. entry = this.cwd;
  1713. }
  1714. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1715. if (!filter || filter(entry)) {
  1716. yield withFileTypes ? entry : entry.fullpath();
  1717. }
  1718. const dirs = new Set([entry]);
  1719. for (const dir of dirs) {
  1720. const entries = dir.readdirSync();
  1721. for (const e of entries) {
  1722. if (!filter || filter(e)) {
  1723. yield withFileTypes ? e : e.fullpath();
  1724. }
  1725. let r = e;
  1726. if (e.isSymbolicLink()) {
  1727. if (!(follow && (r = e.realpathSync())))
  1728. continue;
  1729. if (r.isUnknown())
  1730. r.lstatSync();
  1731. }
  1732. if (r.shouldWalk(dirs, walkFilter)) {
  1733. dirs.add(r);
  1734. }
  1735. }
  1736. }
  1737. }
  1738. stream(entry = this.cwd, opts = {}) {
  1739. if (typeof entry === 'string') {
  1740. entry = this.cwd.resolve(entry);
  1741. }
  1742. else if (!(entry instanceof PathBase)) {
  1743. opts = entry;
  1744. entry = this.cwd;
  1745. }
  1746. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1747. const results = new Minipass({ objectMode: true });
  1748. if (!filter || filter(entry)) {
  1749. results.write(withFileTypes ? entry : entry.fullpath());
  1750. }
  1751. const dirs = new Set();
  1752. const queue = [entry];
  1753. let processing = 0;
  1754. const process = () => {
  1755. let paused = false;
  1756. while (!paused) {
  1757. const dir = queue.shift();
  1758. if (!dir) {
  1759. if (processing === 0)
  1760. results.end();
  1761. return;
  1762. }
  1763. processing++;
  1764. dirs.add(dir);
  1765. const onReaddir = (er, entries, didRealpaths = false) => {
  1766. /* c8 ignore start */
  1767. if (er)
  1768. return results.emit('error', er);
  1769. /* c8 ignore stop */
  1770. if (follow && !didRealpaths) {
  1771. const promises = [];
  1772. for (const e of entries) {
  1773. if (e.isSymbolicLink()) {
  1774. promises.push(e
  1775. .realpath()
  1776. .then((r) => r?.isUnknown() ? r.lstat() : r));
  1777. }
  1778. }
  1779. if (promises.length) {
  1780. Promise.all(promises).then(() => onReaddir(null, entries, true));
  1781. return;
  1782. }
  1783. }
  1784. for (const e of entries) {
  1785. if (e && (!filter || filter(e))) {
  1786. if (!results.write(withFileTypes ? e : e.fullpath())) {
  1787. paused = true;
  1788. }
  1789. }
  1790. }
  1791. processing--;
  1792. for (const e of entries) {
  1793. const r = e.realpathCached() || e;
  1794. if (r.shouldWalk(dirs, walkFilter)) {
  1795. queue.push(r);
  1796. }
  1797. }
  1798. if (paused && !results.flowing) {
  1799. results.once('drain', process);
  1800. }
  1801. else if (!sync) {
  1802. process();
  1803. }
  1804. };
  1805. // zalgo containment
  1806. let sync = true;
  1807. dir.readdirCB(onReaddir, true);
  1808. sync = false;
  1809. }
  1810. };
  1811. process();
  1812. return results;
  1813. }
  1814. streamSync(entry = this.cwd, opts = {}) {
  1815. if (typeof entry === 'string') {
  1816. entry = this.cwd.resolve(entry);
  1817. }
  1818. else if (!(entry instanceof PathBase)) {
  1819. opts = entry;
  1820. entry = this.cwd;
  1821. }
  1822. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1823. const results = new Minipass({ objectMode: true });
  1824. const dirs = new Set();
  1825. if (!filter || filter(entry)) {
  1826. results.write(withFileTypes ? entry : entry.fullpath());
  1827. }
  1828. const queue = [entry];
  1829. let processing = 0;
  1830. const process = () => {
  1831. let paused = false;
  1832. while (!paused) {
  1833. const dir = queue.shift();
  1834. if (!dir) {
  1835. if (processing === 0)
  1836. results.end();
  1837. return;
  1838. }
  1839. processing++;
  1840. dirs.add(dir);
  1841. const entries = dir.readdirSync();
  1842. for (const e of entries) {
  1843. if (!filter || filter(e)) {
  1844. if (!results.write(withFileTypes ? e : e.fullpath())) {
  1845. paused = true;
  1846. }
  1847. }
  1848. }
  1849. processing--;
  1850. for (const e of entries) {
  1851. let r = e;
  1852. if (e.isSymbolicLink()) {
  1853. if (!(follow && (r = e.realpathSync())))
  1854. continue;
  1855. if (r.isUnknown())
  1856. r.lstatSync();
  1857. }
  1858. if (r.shouldWalk(dirs, walkFilter)) {
  1859. queue.push(r);
  1860. }
  1861. }
  1862. }
  1863. if (paused && !results.flowing)
  1864. results.once('drain', process);
  1865. };
  1866. process();
  1867. return results;
  1868. }
  1869. chdir(path = this.cwd) {
  1870. const oldCwd = this.cwd;
  1871. this.cwd = typeof path === 'string' ? this.cwd.resolve(path) : path;
  1872. this.cwd[setAsCwd](oldCwd);
  1873. }
  1874. }
  1875. /**
  1876. * Windows implementation of {@link PathScurryBase}
  1877. *
  1878. * Defaults to case insensitve, uses `'\\'` to generate path strings. Uses
  1879. * {@link PathWin32} for Path objects.
  1880. */
  1881. export class PathScurryWin32 extends PathScurryBase {
  1882. /**
  1883. * separator for generating path strings
  1884. */
  1885. sep = '\\';
  1886. constructor(cwd = process.cwd(), opts = {}) {
  1887. const { nocase = true } = opts;
  1888. super(cwd, win32, '\\', { ...opts, nocase });
  1889. this.nocase = nocase;
  1890. for (let p = this.cwd; p; p = p.parent) {
  1891. p.nocase = this.nocase;
  1892. }
  1893. }
  1894. /**
  1895. * @internal
  1896. */
  1897. parseRootPath(dir) {
  1898. // if the path starts with a single separator, it's not a UNC, and we'll
  1899. // just get separator as the root, and driveFromUNC will return \
  1900. // In that case, mount \ on the root from the cwd.
  1901. return win32.parse(dir).root.toUpperCase();
  1902. }
  1903. /**
  1904. * @internal
  1905. */
  1906. newRoot(fs) {
  1907. return new PathWin32(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  1908. }
  1909. /**
  1910. * Return true if the provided path string is an absolute path
  1911. */
  1912. isAbsolute(p) {
  1913. return (p.startsWith('/') || p.startsWith('\\') || /^[a-z]:(\/|\\)/i.test(p));
  1914. }
  1915. }
  1916. /**
  1917. * {@link PathScurryBase} implementation for all posix systems other than Darwin.
  1918. *
  1919. * Defaults to case-sensitive matching, uses `'/'` to generate path strings.
  1920. *
  1921. * Uses {@link PathPosix} for Path objects.
  1922. */
  1923. export class PathScurryPosix extends PathScurryBase {
  1924. /**
  1925. * separator for generating path strings
  1926. */
  1927. sep = '/';
  1928. constructor(cwd = process.cwd(), opts = {}) {
  1929. const { nocase = false } = opts;
  1930. super(cwd, posix, '/', { ...opts, nocase });
  1931. this.nocase = nocase;
  1932. }
  1933. /**
  1934. * @internal
  1935. */
  1936. parseRootPath(_dir) {
  1937. return '/';
  1938. }
  1939. /**
  1940. * @internal
  1941. */
  1942. newRoot(fs) {
  1943. return new PathPosix(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  1944. }
  1945. /**
  1946. * Return true if the provided path string is an absolute path
  1947. */
  1948. isAbsolute(p) {
  1949. return p.startsWith('/');
  1950. }
  1951. }
  1952. /**
  1953. * {@link PathScurryBase} implementation for Darwin (macOS) systems.
  1954. *
  1955. * Defaults to case-insensitive matching, uses `'/'` for generating path
  1956. * strings.
  1957. *
  1958. * Uses {@link PathPosix} for Path objects.
  1959. */
  1960. export class PathScurryDarwin extends PathScurryPosix {
  1961. constructor(cwd = process.cwd(), opts = {}) {
  1962. const { nocase = true } = opts;
  1963. super(cwd, { ...opts, nocase });
  1964. }
  1965. }
  1966. /**
  1967. * Default {@link PathBase} implementation for the current platform.
  1968. *
  1969. * {@link PathWin32} on Windows systems, {@link PathPosix} on all others.
  1970. */
  1971. export const Path = process.platform === 'win32' ? PathWin32 : PathPosix;
  1972. /**
  1973. * Default {@link PathScurryBase} implementation for the current platform.
  1974. *
  1975. * {@link PathScurryWin32} on Windows systems, {@link PathScurryDarwin} on
  1976. * Darwin (macOS) systems, {@link PathScurryPosix} on all others.
  1977. */
  1978. export const PathScurry = process.platform === 'win32' ? PathScurryWin32
  1979. : process.platform === 'darwin' ? PathScurryDarwin
  1980. : PathScurryPosix;
  1981. //# sourceMappingURL=index.js.map