Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 | 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 16766x 45x 45x 15315x 15315x 15315x 15243x 45x 45x 45x 12x 12x 8x 4x 4x 4x 6x 6x 553x 732x 632x 632x 632x 302x 330x 179x 151x 553x 553x 45x 45x 92x 92x 92x 552x 552x 552x 45x 807x 807x 807x 807x 807x 807x 807x 807x 664x 664x 807x 807x 807x 807x 807x 2x 807x 807x 1x 807x 807x 1x 807x 6x 807x 807x 807x 807x 807x 2796x 809x 1987x 1987x 321x 321x 321x 321x 3790x 3790x 3640x 3640x 3318x 3318x 3790x 3790x 3790x 3790x 257x 257x 257x 1419x 85x 85x 85x 81x 17742x 17742x 17742x 17742x 32856x 32856x 32856x 29341x 29341x 31x 31x 31x 31x 29310x 14227x 2x 2x 1x 3319x 51x 50x 50x 1113x 287x 287x 287x 286x 286x 5870x 5862x 5862x 5862x 5848x 12632x 12632x 12631x 12631x 1789x 1789x 10842x 320x 320x 320x 1x 1x 320x 990x 634x 356x 356x 356x 356x 356x 158x 158x 158x 198x 198x 320x 320x 320x 45x 320x 122x 122x 7x 7x 8x 8x 8x 7x 115x 122x 122x 121x 696x 654x 654x 654x 485x 485x 484x 169x 553x 2x 2x 2x 2x 2x 2x 2x 2x 3322x 3322x 3322x 3322x 3322x 3322x 8x 3314x 3318x 1088x 3318x 3318x 3318x 3318x 3318x 3318x 3591x 3591x 3591x 3341x 1981x 1981x 1980x 1980x 3340x 18x 3591x 3318x 3318x 1102x 1100x 1099x 1098x 1064x 1064x 1064x 8x 8x 1064x 1064x 1064x 1063x 1062x 3261x 3261x 3261x 3261x 3264x 3263x 3261x 799x 799x 16x 2x 14x 13x 1x 12x 13x 13x 4x 4x 4x 3x 3x 2x 1x 5x 4x 4x 4x 4x 11x 11x 11x 11x 4x 5x 5x 5x 1x 1x 5x 5x 5x 5x 4x 1x 1029x 1029x 1029x 1025x 1025x 1025x 1025x 1016x 1016x 1024x 1019x 1019x 1019x 1015x 1018x 395x 394x 394x 635x 635x 635x 1522x 1519x 1x 1518x 748x 748x 774x 774x 774x 774x 773x 772x 1x 1x 3x 2x 2x 2x 2x 5x 5x 5x 5x 5x 2x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1440x 1440x 1429x 1432x 1432x 1432x 1432x 1432x 733x 732x 732x 732x 1432x 574x 574x 574x 574x 574x 640x 640x 640x 1x 1x 640x 639x 639x 639x 639x 639x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 231x 230x 3x 2x 228x 1x 227x 9x 5x 5x 226x 226x 226x 3x 3x 223x 223x 226x 226x 3x 3x 3x 2x 2x 2x 262x 262x 262x 257x 257x 257x 257x 257x 257x 262x 262x 45x 45x 45x 45x 45x 44x 44x 44x 39x 39x 39x 6x 6x 6x 6x 8x 8x 8x 7x 4x 4x 4x 4x 281x 281x 272x 9x 2x 7x 150x 150x 131x 131x 7591x 7591x 6092x 1499x 2x 1497x 55x 55x 7519x 7519x 8x 8x 7x 6x 2x 2x 778x 778x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 12x 11x 9x 769x 769x 769x 17x 16x 16x 10x 4x 3x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 6x 6x 196x 196x 196x 195x 195x 195x 195x 781x 781x 391x 391x 6x 6x 6x 195x 195x 465x 255x 2x 195x 147x 147x 147x 298x 10x 10x 10x 288x 90x 90x 90x 100x 100x 100x 24x 23x 23x 23x 23x 12x 12x 12x 12x 12x 12x 3x 2x 1x 2x 2x 16x 15x 7x 9x 9x 9x 6x 5x 5x 5x 4x 3x 3x 3x 3x 3x 5x 4x 4x 4x 2x 4x 3x 3x 3x 2x 2x 288x 288x 1x 287x 286x 286x 284x 797x 797x 797x 797x 797x 797x 1171x 1171x 1171x 1171x 515x 1x 656x 656x 796x 59x 59x 59x 59x 27x 369x 369x 369x 369x 369x 258x 3x 3x 3x 2x 1x 1x 3x 3x 2x 1x 4x 4x 3x 2x 51x 51x 50x 46x 2x 49x 49x 49x 26x 26x 4x 2x 22x 16x 4x 18x 15x 11x 11x 8x 7x 2x 6x 5x 5x 5x 4x 4x 4x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 807x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 807x 807x 807x 807x 807x 807x 807x 807x 807x 2x 45x 3x 3x 3x 1x 3x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 45x 45x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 1x 1x 1x 1x 1x 1x 45x 3x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 45x 1x 45x 45x 45x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 1x 1x 1x 1x 1x 45x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 2x 2x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 6x 6x 6x 6x 6x 6x 6x 5x 6x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 18x 18x 18x 6x 6x 2x 6x 18x 18x 18x 18x 6x 15x 15x 5x 5x 5x 5x 15x 5x 5x 5x 5x 6x 4x 5x 1x 5x 5x 15x 25x 7x 15x 15x 15x 15x 13x 13x 15x 9x 14x 4x 6x 6x 6x 6x 6x 6x 6x 6x 6x 23x 6x 6x 6x 6x | import * as pathModule from 'path'; import { Node, Link, File } from './node'; import Stats from './Stats'; import Dirent from './Dirent'; import { Buffer, bufferAllocUnsafe, bufferFrom } from './internal/buffer'; import setImmediate from './setImmediate'; import queueMicrotask from './queueMicrotask'; import process from './process'; import setTimeoutUnref, { TSetTimeout } from './setTimeoutUnref'; import { Readable, Writable } from 'stream'; import { constants } from './constants'; import { EventEmitter } from 'events'; import { TEncodingExtended, TDataOut, strToEncoding, ENCODING_UTF8 } from './encoding'; import { FileHandle } from './node/FileHandle'; import * as util from 'util'; import * as misc from './node/types/misc'; import * as opts from './node/types/options'; import { FsCallbackApi, WritevCallback } from './node/types/FsCallbackApi'; import { FsPromises } from './node/FsPromises'; import { ToTreeOptions, toTreeSync } from './print'; import { ERRSTR, FLAGS, MODE } from './node/constants'; import { getDefaultOpts, getDefaultOptsAndCb, getMkdirOptions, getOptions, getReadFileOptions, getReaddirOptions, getReaddirOptsAndCb, getRmOptsAndCb, getRmdirOptions, optsAndCbGenerator, getAppendFileOptsAndCb, getAppendFileOpts, getStatOptsAndCb, getStatOptions, getRealpathOptsAndCb, getRealpathOptions, getWriteFileOptions, writeFileDefaults, } from './node/options'; import { validateCallback, modeToNumber, pathToFilename, nullCheck, createError, genRndStr6, flagsToNumber, validateFd, isFd, isWin, dataToBuffer, getWriteArgs, bufferToEncoding, getWriteSyncArgs, unixify, } from './node/util'; import type { PathLike, symlink } from 'fs'; import type { FsPromisesApi, FsSynchronousApi } from './node/types'; import { fsSynchronousApiList } from './node/lists/fsSynchronousApiList'; const resolveCrossPlatform = pathModule.resolve; const { O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, O_DIRECTORY, O_SYMLINK, F_OK, COPYFILE_EXCL, COPYFILE_FICLONE_FORCE, } = constants; const { sep, relative, join, dirname } = pathModule.posix ? pathModule.posix : pathModule; // ---------------------------------------- Types // Node-style errors with a `code` property. export interface IError extends Error { code?: string; } export type TFileId = PathLike | number; // Number is used as a file descriptor. export type TData = TDataOut | ArrayBufferView | DataView; // Data formats users can give us. export type TFlags = string | number; export type TMode = string | number; // Mode can be a String, although docs say it should be a Number. export type TTime = number | string | Date; export type TCallback<TData> = (error?: IError | null, data?: TData) => void; // ---------------------------------------- Constants const kMinPoolSpace = 128; // ---------------------------------------- Error messages const EPERM = 'EPERM'; const ENOENT = 'ENOENT'; const EBADF = 'EBADF'; const EINVAL = 'EINVAL'; const EEXIST = 'EEXIST'; const ENOTDIR = 'ENOTDIR'; const EMFILE = 'EMFILE'; const EACCES = 'EACCES'; const EISDIR = 'EISDIR'; const ENOTEMPTY = 'ENOTEMPTY'; const ENOSYS = 'ENOSYS'; const ERR_FS_EISDIR = 'ERR_FS_EISDIR'; const ERR_OUT_OF_RANGE = 'ERR_OUT_OF_RANGE'; // ---------------------------------------- Flags export type TFlagsCopy = | typeof constants.COPYFILE_EXCL | typeof constants.COPYFILE_FICLONE | typeof constants.COPYFILE_FICLONE_FORCE; // ---------------------------------------- Options // Options for `fs.appendFile` and `fs.appendFileSync` export interface IAppendFileOptions extends opts.IFileOptions {} // Options for `fs.watchFile` export interface IWatchFileOptions { persistent?: boolean; interval?: number; } // Options for `fs.watch` export interface IWatchOptions extends opts.IOptions { persistent?: boolean; recursive?: boolean; } // ---------------------------------------- Utility functions type TResolve = (filename: string, base?: string) => string; let resolve: TResolve = (filename, base = process.cwd()) => resolveCrossPlatform(base, filename); Iif (isWin) { const _resolve = resolve; resolve = (filename, base) => unixify(_resolve(filename, base)); } export function filenameToSteps(filename: string, base?: string): string[] { const fullPath = resolve(filename, base); const fullPathSansSlash = fullPath.substring(1); if (!fullPathSansSlash) return []; return fullPathSansSlash.split(sep); } export function pathToSteps(path: PathLike): string[] { return filenameToSteps(pathToFilename(path)); } export function dataToStr(data: TData, encoding: string = ENCODING_UTF8): string { if (Buffer.isBuffer(data)) return data.toString(encoding); else if (data instanceof Uint8Array) return bufferFrom(data).toString(encoding); else return String(data); } // converts Date or number to a fractional UNIX timestamp export function toUnixTimestamp(time) { // tslint:disable-next-line triple-equals Iif (typeof time === 'string' && +time == (time as any)) { return +time; } if (time instanceof Date) { return time.getTime() / 1000; } if (isFinite(time)) { Iif (time < 0) { return Date.now() / 1000; } return time; } throw new Error('Cannot parse time: ' + time); } function validateUid(uid: number) { Iif (typeof uid !== 'number') throw TypeError(ERRSTR.UID); } function validateGid(gid: number) { Iif (typeof gid !== 'number') throw TypeError(ERRSTR.GID); } // ---------------------------------------- Volume type DirectoryContent = string | Buffer | null; export interface DirectoryJSON<T extends DirectoryContent = DirectoryContent> { [key: string]: T; } export interface NestedDirectoryJSON<T extends DirectoryContent = DirectoryContent> { [key: string]: T | NestedDirectoryJSON; } function flattenJSON(nestedJSON: NestedDirectoryJSON): DirectoryJSON { const flatJSON: DirectoryJSON = {}; function flatten(pathPrefix: string, node: NestedDirectoryJSON) { for (const path in node) { const contentOrNode = node[path]; const joinedPath = join(pathPrefix, path); if (typeof contentOrNode === 'string' || contentOrNode instanceof Buffer) { flatJSON[joinedPath] = contentOrNode; } else if (typeof contentOrNode === 'object' && contentOrNode !== null && Object.keys(contentOrNode).length > 0) { // empty directories need an explicit entry and therefore get handled in `else`, non-empty ones are implicitly considered flatten(joinedPath, contentOrNode); } else { // without this branch null, empty-object or non-object entries would not be handled in the same way // by both fromJSON() and fromNestedJSON() flatJSON[joinedPath] = null; } } } flatten('', nestedJSON); return flatJSON; } const notImplemented: (...args: any[]) => any = () => { throw new Error('Not implemented'); }; /** * `Volume` represents a file system. */ export class Volume implements FsCallbackApi, FsSynchronousApi { static fromJSON(json: DirectoryJSON, cwd?: string): Volume { const vol = new Volume(); vol.fromJSON(json, cwd); return vol; } static fromNestedJSON(json: NestedDirectoryJSON, cwd?: string): Volume { const vol = new Volume(); vol.fromNestedJSON(json, cwd); return vol; } /** * Global file descriptor counter. UNIX file descriptors start from 0 and go sequentially * up, so here, in order not to conflict with them, we choose some big number and descrease * the file descriptor of every new opened file. * @type {number} * @todo This should not be static, right? */ static fd: number = 0x7fffffff; // Constructor function used to create new nodes. // NodeClass: new (...args) => TNode = Node as new (...args) => TNode; // Hard link to the root of this volume. // root: Node = new (this.NodeClass)(null, '', true); root: Link; // I-node number counter. ino: number = 0; // A mapping for i-node numbers to i-nodes (`Node`); inodes: { [ino: number]: Node } = {}; // List of released i-node numbers, for reuse. releasedInos: number[] = []; // A mapping for file descriptors to `File`s. fds: { [fd: number]: File } = {}; // A list of reusable (opened and closed) file descriptors, that should be // used first before creating a new file descriptor. releasedFds: number[] = []; // Max number of open files. maxFiles = 10000; // Current number of open files. openFiles = 0; StatWatcher: new () => StatWatcher; ReadStream: new (...args) => misc.IReadStream; WriteStream: new (...args) => IWriteStream; FSWatcher: new () => FSWatcher; props: { Node: new (...args) => Node; Link: new (...args) => Link; File: new (...args) => File; }; private promisesApi = new FsPromises(this, FileHandle); get promises(): FsPromisesApi { Iif (this.promisesApi === null) throw new Error('Promise is not supported in this environment.'); return this.promisesApi; } constructor(props = {}) { this.props = Object.assign({ Node, Link, File }, props); const root = this.createLink(); root.setNode(this.createNode(true)); const self = this; // tslint:disable-line no-this-assignment this.StatWatcher = class extends StatWatcher { constructor() { super(self); } }; const _ReadStream: new (...args) => misc.IReadStream = FsReadStream as any; this.ReadStream = class extends _ReadStream { constructor(...args) { super(self, ...args); } } as any as new (...args) => misc.IReadStream; const _WriteStream: new (...args) => IWriteStream = FsWriteStream as any; this.WriteStream = class extends _WriteStream { constructor(...args) { super(self, ...args); } } as any as new (...args) => IWriteStream; this.FSWatcher = class extends FSWatcher { constructor() { super(self); } }; root.setChild('.', root); root.getNode().nlink++; root.setChild('..', root); root.getNode().nlink++; this.root = root; } createLink(): Link; createLink(parent: Link, name: string, isDirectory?: boolean, perm?: number): Link; createLink(parent?: Link, name?: string, isDirectory: boolean = false, perm?: number): Link { if (!parent) { return new this.props.Link(this, null, ''); } Iif (!name) { throw new Error('createLink: name cannot be empty'); } return parent.createChild(name, this.createNode(isDirectory, perm)); } deleteLink(link: Link): boolean { const parent = link.parent; if (parent) { parent.deleteChild(link); return true; } return false; } private newInoNumber(): number { const releasedFd = this.releasedInos.pop(); if (releasedFd) return releasedFd; else { this.ino = (this.ino + 1) % 0xffffffff; return this.ino; } } private newFdNumber(): number { const releasedFd = this.releasedFds.pop(); return typeof releasedFd === 'number' ? releasedFd : Volume.fd--; } createNode(isDirectory: boolean = false, perm?: number): Node { const node = new this.props.Node(this.newInoNumber(), perm); if (isDirectory) node.setIsDirectory(); this.inodes[node.ino] = node; return node; } private deleteNode(node: Node) { node.del(); delete this.inodes[node.ino]; this.releasedInos.push(node.ino); } // Returns a `Link` (hard link) referenced by path "split" into steps. getLink(steps: string[]): Link | null { return this.root.walk(steps); } // Just link `getLink`, but throws a correct user error, if link to found. getLinkOrThrow(filename: string, funcName?: string): Link { const steps = filenameToSteps(filename); const link = this.getLink(steps); if (!link) throw createError(ENOENT, funcName, filename); return link; } // Just like `getLink`, but also dereference/resolves symbolic links. getResolvedLink(filenameOrSteps: string | string[]): Link | null { let steps: string[] = typeof filenameOrSteps === 'string' ? filenameToSteps(filenameOrSteps) : filenameOrSteps; let link: Link | undefined = this.root; let i = 0; while (i < steps.length) { const step = steps[i]; link = link.getChild(step); if (!link) return null; const node = link.getNode(); if (node.isSymlink()) { steps = node.symlink.concat(steps.slice(i + 1)); link = this.root; i = 0; continue; } i++; } return link; } // Just like `getLinkOrThrow`, but also dereference/resolves symbolic links. getResolvedLinkOrThrow(filename: string, funcName?: string): Link { const link = this.getResolvedLink(filename); if (!link) throw createError(ENOENT, funcName, filename); return link; } resolveSymlinks(link: Link): Link | null { // let node: Node = link.getNode(); // while(link && node.isSymlink()) { // link = this.getLink(node.symlink); // if(!link) return null; // node = link.getNode(); // } // return link; return this.getResolvedLink(link.steps.slice(1)); } // Just like `getLinkOrThrow`, but also verifies that the link is a directory. private getLinkAsDirOrThrow(filename: string, funcName?: string): Link { const link = this.getLinkOrThrow(filename, funcName); Iif (!link.getNode().isDirectory()) throw createError(ENOTDIR, funcName, filename); return link; } // Get the immediate parent directory of the link. private getLinkParent(steps: string[]): Link | null { return this.root.walk(steps, steps.length - 1); } private getLinkParentAsDirOrThrow(filenameOrSteps: string | string[], funcName?: string): Link { const steps = filenameOrSteps instanceof Array ? filenameOrSteps : filenameToSteps(filenameOrSteps); const link = this.getLinkParent(steps); if (!link) throw createError(ENOENT, funcName, sep + steps.join(sep)); Iif (!link.getNode().isDirectory()) throw createError(ENOTDIR, funcName, sep + steps.join(sep)); return link; } private getFileByFd(fd: number): File { return this.fds[String(fd)]; } private getFileByFdOrThrow(fd: number, funcName?: string): File { Iif (!isFd(fd)) throw TypeError(ERRSTR.FD); const file = this.getFileByFd(fd); if (!file) throw createError(EBADF, funcName); return file; } /** * @todo This is not used anymore. Remove. */ /* private getNodeByIdOrCreate(id: TFileId, flags: number, perm: number): Node { if (typeof id === 'number') { const file = this.getFileByFd(id); if (!file) throw Error('File nto found'); return file.node; } else { const steps = pathToSteps(id as PathLike); let link = this.getLink(steps); if (link) return link.getNode(); // Try creating a node if not found. if (flags & O_CREAT) { const dirLink = this.getLinkParent(steps); if (dirLink) { const name = steps[steps.length - 1]; link = this.createLink(dirLink, name, false, perm); return link.getNode(); } } throw createError(ENOENT, 'getNodeByIdOrCreate', pathToFilename(id)); } } */ private wrapAsync(method: (...args) => void, args: any[], callback: TCallback<any>) { validateCallback(callback); setImmediate(() => { let result; try { result = method.apply(this, args); } catch (err) { callback(err); return; } callback(null, result); }); } private _toJSON(link = this.root, json = {}, path?: string, asBuffer?: boolean): DirectoryJSON<string | null> { let isEmpty = true; let children = link.children; if (link.getNode().isFile()) { children = new Map([[link.getName(), link.parent.getChild(link.getName())]]); link = link.parent; } for (const name of children.keys()) { if (name === '.' || name === '..') { continue; } isEmpty = false; const child = link.getChild(name); Iif (!child) { throw new Error('_toJSON: unexpected undefined'); } const node = child.getNode(); if (node.isFile()) { let filename = child.getPath(); Iif (path) filename = relative(path, filename); json[filename] = asBuffer ? node.getBuffer() : node.getString(); } else if (node.isDirectory()) { this._toJSON(child, json, path, asBuffer); } } let dirPath = link.getPath(); Iif (path) dirPath = relative(path, dirPath); if (dirPath && isEmpty) { json[dirPath] = null; } return json; } toJSON(paths?: PathLike | PathLike[], json = {}, isRelative = false, asBuffer = false): DirectoryJSON<string | null> { const links: Link[] = []; if (paths) { if (!Array.isArray(paths)) paths = [paths]; for (const path of paths) { const filename = pathToFilename(path); const link = this.getResolvedLink(filename); if (!link) continue; links.push(link); } } else { links.push(this.root); } if (!links.length) return json; for (const link of links) this._toJSON(link, json, isRelative ? link.getPath() : '', asBuffer); return json; } // TODO: `cwd` should probably not invoke `process.cwd()`. fromJSON(json: DirectoryJSON, cwd: string = process.cwd()) { for (let filename in json) { const data = json[filename]; filename = resolve(filename, cwd); if (typeof data === 'string' || data instanceof Buffer) { const dir = dirname(filename); this.mkdirpBase(dir, MODE.DIR); this.writeFileSync(filename, data); } else { this.mkdirpBase(filename, MODE.DIR); } } } fromNestedJSON(json: NestedDirectoryJSON, cwd?: string) { this.fromJSON(flattenJSON(json), cwd); } public toTree(opts: ToTreeOptions = { separator: <'/' | '\\'>sep }): string { return toTreeSync(this, opts); } reset() { this.ino = 0; this.inodes = {}; this.releasedInos = []; this.fds = {}; this.releasedFds = []; this.openFiles = 0; this.root = this.createLink(); this.root.setNode(this.createNode(true)); } // Legacy interface mountSync(mountpoint: string, json: DirectoryJSON) { this.fromJSON(json, mountpoint); } private openLink(link: Link, flagsNum: number, resolveSymlinks: boolean = true): File { Iif (this.openFiles >= this.maxFiles) { // Too many open files. throw createError(EMFILE, 'open', link.getPath()); } // Resolve symlinks. let realLink: Link | null = link; if (resolveSymlinks) realLink = this.resolveSymlinks(link); Iif (!realLink) throw createError(ENOENT, 'open', link.getPath()); const node = realLink.getNode(); // Check whether node is a directory if (node.isDirectory()) { if ((flagsNum & (O_RDONLY | O_RDWR | O_WRONLY)) !== O_RDONLY) throw createError(EISDIR, 'open', link.getPath()); } else { Iif (flagsNum & O_DIRECTORY) throw createError(ENOTDIR, 'open', link.getPath()); } // Check node permissions if (!(flagsNum & O_WRONLY)) { Iif (!node.canRead()) { throw createError(EACCES, 'open', link.getPath()); } } if (flagsNum & O_RDWR) { } const file = new this.props.File(link, node, flagsNum, this.newFdNumber()); this.fds[file.fd] = file; this.openFiles++; if (flagsNum & O_TRUNC) file.truncate(); return file; } private openFile( filename: string, flagsNum: number, modeNum: number | undefined, resolveSymlinks: boolean = true, ): File { const steps = filenameToSteps(filename); let link: Link | null = resolveSymlinks ? this.getResolvedLink(steps) : this.getLink(steps); if (link && flagsNum & O_EXCL) throw createError(EEXIST, 'open', filename); // Try creating a new file, if it does not exist. if (!link && flagsNum & O_CREAT) { // const dirLink: Link = this.getLinkParent(steps); const dirLink: Link | null = this.getResolvedLink(steps.slice(0, steps.length - 1)); // if(!dirLink) throw createError(ENOENT, 'open', filename); if (!dirLink) throw createError(ENOENT, 'open', sep + steps.join(sep)); if (flagsNum & O_CREAT && typeof modeNum === 'number') { link = this.createLink(dirLink, steps[steps.length - 1], false, modeNum); } } if (link) return this.openLink(link, flagsNum, resolveSymlinks); throw createError(ENOENT, 'open', filename); } private openBase(filename: string, flagsNum: number, modeNum: number, resolveSymlinks: boolean = true): number { const file = this.openFile(filename, flagsNum, modeNum, resolveSymlinks); Iif (!file) throw createError(ENOENT, 'open', filename); return file.fd; } openSync(path: PathLike, flags: TFlags, mode: TMode = MODE.DEFAULT): number { // Validate (1) mode; (2) path; (3) flags - in that order. const modeNum = modeToNumber(mode); const fileName = pathToFilename(path); const flagsNum = flagsToNumber(flags); return this.openBase(fileName, flagsNum, modeNum, !(flagsNum & O_SYMLINK)); } open(path: PathLike, flags: TFlags, /* ... */ callback: TCallback<number>); open(path: PathLike, flags: TFlags, mode: TMode, callback: TCallback<number>); open(path: PathLike, flags: TFlags, a: TMode | TCallback<number>, b?: TCallback<number>) { let mode: TMode = a as TMode; let callback: TCallback<number> = b as TCallback<number>; if (typeof a === 'function') { mode = MODE.DEFAULT; callback = a; } mode = mode || MODE.DEFAULT; const modeNum = modeToNumber(mode); const fileName = pathToFilename(path); const flagsNum = flagsToNumber(flags); this.wrapAsync(this.openBase, [fileName, flagsNum, modeNum, !(flagsNum & O_SYMLINK)], callback); } private closeFile(file: File) { Iif (!this.fds[file.fd]) return; this.openFiles--; delete this.fds[file.fd]; this.releasedFds.push(file.fd); } closeSync(fd: number) { validateFd(fd); const file = this.getFileByFdOrThrow(fd, 'close'); this.closeFile(file); } close(fd: number, callback: TCallback<void>) { validateFd(fd); this.wrapAsync(this.closeSync, [fd], callback); } private openFileOrGetById(id: TFileId, flagsNum: number, modeNum?: number): File { if (typeof id === 'number') { const file = this.fds[id]; Iif (!file) throw createError(ENOENT); return file; } else { return this.openFile(pathToFilename(id), flagsNum, modeNum); } } private readBase( fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number | null, ): number { if (buffer.byteLength < length) { throw createError(ERR_OUT_OF_RANGE, 'read', undefined, undefined, RangeError); } const file = this.getFileByFdOrThrow(fd); if (file.node.isSymlink()) { throw createError(EPERM, 'read', file.link.getPath()); } return file.read( buffer, Number(offset), Number(length), position === -1 || typeof position !== 'number' ? undefined : position, ); } readSync( fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number | null, ): number { validateFd(fd); return this.readBase(fd, buffer, offset, length, position); } read( fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number | null, callback: (err?: Error | null, bytesRead?: number, buffer?: Buffer | ArrayBufferView | DataView) => void, ) { validateCallback(callback); // This `if` branch is from Node.js Iif (length === 0) { return queueMicrotask(() => { Iif (callback) callback(null, 0, buffer); }); } setImmediate(() => { try { const bytes = this.readBase(fd, buffer, offset, length, position); callback(null, bytes, buffer); } catch (err) { callback(err); } }); } private readvBase(fd: number, buffers: ArrayBufferView[], position: number | null): number { const file = this.getFileByFdOrThrow(fd); let p = position ?? undefined; Iif (p === -1) { p = undefined; } let bytesRead = 0; for (const buffer of buffers) { const bytes = file.read(buffer, 0, buffer.byteLength, p); p = undefined; bytesRead += bytes; if (bytes < buffer.byteLength) break; } return bytesRead; } readv(fd: number, buffers: ArrayBufferView[], callback: misc.TCallback2<number, ArrayBufferView[]>): void; readv( fd: number, buffers: ArrayBufferView[], position: number | null, callback: misc.TCallback2<number, ArrayBufferView[]>, ): void; readv( fd: number, buffers: ArrayBufferView[], a: number | null | misc.TCallback2<number, ArrayBufferView[]>, b?: misc.TCallback2<number, ArrayBufferView[]>, ): void { let position: number | null = a as number | null; let callback: misc.TCallback2<number, ArrayBufferView[]> = b as misc.TCallback2<number, ArrayBufferView[]>; if (typeof a === 'function') { position = null; callback = a; } validateCallback(callback); setImmediate(() => { try { const bytes = this.readvBase(fd, buffers, position); callback(null, bytes, buffers); } catch (err) { callback(err); } }); } readvSync(fd: number, buffers: ArrayBufferView[], position: number | null): number { validateFd(fd); return this.readvBase(fd, buffers, position); } private readFileBase(id: TFileId, flagsNum: number, encoding: BufferEncoding): Buffer | string { let result: Buffer | string; const isUserFd = typeof id === 'number'; const userOwnsFd: boolean = isUserFd && isFd(id); let fd: number; if (userOwnsFd) fd = id as number; else { const filename = pathToFilename(id as PathLike); const steps = filenameToSteps(filename); const link: Link | null = this.getResolvedLink(steps); if (link) { const node = link.getNode(); if (node.isDirectory()) throw createError(EISDIR, 'open', link.getPath()); } fd = this.openSync(id as PathLike, flagsNum); } try { result = bufferToEncoding(this.getFileByFdOrThrow(fd).getBuffer(), encoding); } finally { if (!userOwnsFd) { this.closeSync(fd); } } return result; } readFileSync(file: TFileId, options?: opts.IReadFileOptions | string): TDataOut { const opts = getReadFileOptions(options); const flagsNum = flagsToNumber(opts.flag); return this.readFileBase(file, flagsNum, opts.encoding as BufferEncoding); } readFile(id: TFileId, callback: TCallback<TDataOut>); readFile(id: TFileId, options: opts.IReadFileOptions | string, callback: TCallback<TDataOut>); readFile(id: TFileId, a: TCallback<TDataOut> | opts.IReadFileOptions | string, b?: TCallback<TDataOut>) { const [opts, callback] = optsAndCbGenerator<opts.IReadFileOptions, TCallback<TDataOut>>(getReadFileOptions)(a, b); const flagsNum = flagsToNumber(opts.flag); this.wrapAsync(this.readFileBase, [id, flagsNum, opts.encoding], callback); } private writeBase(fd: number, buf: Buffer, offset?: number, length?: number, position?: number | null): number { const file = this.getFileByFdOrThrow(fd, 'write'); if (file.node.isSymlink()) { throw createError(EBADF, 'write', file.link.getPath()); } return file.write(buf, offset, length, position === -1 || typeof position !== 'number' ? undefined : position); } writeSync( fd: number, buffer: Buffer | ArrayBufferView | DataView, offset?: number, length?: number, position?: number, ): number; writeSync(fd: number, str: string, position?: number, encoding?: BufferEncoding): number; writeSync( fd: number, a: string | Buffer | ArrayBufferView | DataView, b?: number, c?: number | BufferEncoding, d?: number, ): number { const [, buf, offset, length, position] = getWriteSyncArgs(fd, a, b, c, d); return this.writeBase(fd, buf, offset, length, position); } write(fd: number, buffer: Buffer | ArrayBufferView | DataView, callback: (...args) => void); write(fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, callback: (...args) => void); write( fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, callback: (...args) => void, ); write( fd: number, buffer: Buffer | ArrayBufferView | DataView, offset: number, length: number, position: number, callback: (...args) => void, ); write(fd: number, str: string, callback: (...args) => void); write(fd: number, str: string, position: number, callback: (...args) => void); write(fd: number, str: string, position: number, encoding: BufferEncoding, callback: (...args) => void); write(fd: number, a?, b?, c?, d?, e?) { const [, asStr, buf, offset, length, position, cb] = getWriteArgs(fd, a, b, c, d, e); setImmediate(() => { try { const bytes = this.writeBase(fd, buf, offset, length, position); if (!asStr) { cb(null, bytes, buf); } else { cb(null, bytes, a); } } catch (err) { cb(err); } }); } private writevBase(fd: number, buffers: ArrayBufferView[], position: number | null): number { const file = this.getFileByFdOrThrow(fd); let p = position ?? undefined; Iif (p === -1) { p = undefined; } let bytesWritten = 0; for (const buffer of buffers) { const nodeBuf = Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength); const bytes = file.write(nodeBuf, 0, nodeBuf.byteLength, p); p = undefined; bytesWritten += bytes; Iif (bytes < nodeBuf.byteLength) break; } return bytesWritten; } writev(fd: number, buffers: ArrayBufferView[], callback: WritevCallback): void; writev(fd: number, buffers: ArrayBufferView[], position: number | null, callback: WritevCallback): void; writev(fd: number, buffers: ArrayBufferView[], a: number | null | WritevCallback, b?: WritevCallback): void { let position: number | null = a as number | null; let callback: WritevCallback = b as WritevCallback; Iif (typeof a === 'function') { position = null; callback = a; } validateCallback(callback); setImmediate(() => { try { const bytes = this.writevBase(fd, buffers, position); callback(null, bytes, buffers); } catch (err) { callback(err); } }); } writevSync(fd: number, buffers: ArrayBufferView[], position: number | null): number { validateFd(fd); return this.writevBase(fd, buffers, position); } private writeFileBase(id: TFileId, buf: Buffer, flagsNum: number, modeNum: number) { // console.log('writeFileBase', id, buf, flagsNum, modeNum); // const node = this.getNodeByIdOrCreate(id, flagsNum, modeNum); // node.setBuffer(buf); const isUserFd = typeof id === 'number'; let fd: number; if (isUserFd) fd = id as number; else { fd = this.openBase(pathToFilename(id as PathLike), flagsNum, modeNum); // fd = this.openSync(id as PathLike, flagsNum, modeNum); } let offset = 0; let length = buf.length; let position = flagsNum & O_APPEND ? undefined : 0; try { while (length > 0) { const written = this.writeSync(fd, buf, offset, length, position); offset += written; length -= written; if (position !== undefined) position += written; } } finally { if (!isUserFd) this.closeSync(fd); } } writeFileSync(id: TFileId, data: TData, options?: opts.IWriteFileOptions): void { const opts = getWriteFileOptions(options); const flagsNum = flagsToNumber(opts.flag); const modeNum = modeToNumber(opts.mode); const buf = dataToBuffer(data, opts.encoding); this.writeFileBase(id, buf, flagsNum, modeNum); } writeFile(id: TFileId, data: TData, callback: TCallback<void>); writeFile(id: TFileId, data: TData, options: opts.IWriteFileOptions | string, callback: TCallback<void>); writeFile(id: TFileId, data: TData, a: TCallback<void> | opts.IWriteFileOptions | string, b?: TCallback<void>) { let options: opts.IWriteFileOptions | string = a as opts.IWriteFileOptions; let callback: TCallback<void> | undefined = b; if (typeof a === 'function') { options = writeFileDefaults; callback = a; } const cb = validateCallback(callback); const opts = getWriteFileOptions(options); const flagsNum = flagsToNumber(opts.flag); const modeNum = modeToNumber(opts.mode); const buf = dataToBuffer(data, opts.encoding); this.wrapAsync(this.writeFileBase, [id, buf, flagsNum, modeNum], cb); } private linkBase(filename1: string, filename2: string) { const steps1 = filenameToSteps(filename1); const link1 = this.getLink(steps1); if (!link1) throw createError(ENOENT, 'link', filename1, filename2); const steps2 = filenameToSteps(filename2); // Check new link directory exists. const dir2 = this.getLinkParent(steps2); Iif (!dir2) throw createError(ENOENT, 'link', filename1, filename2); const name = steps2[steps2.length - 1]; // Check if new file already exists. Iif (dir2.getChild(name)) throw createError(EEXIST, 'link', filename1, filename2); const node = link1.getNode(); node.nlink++; dir2.createChild(name, node); } private copyFileBase(src: string, dest: string, flags: number) { const buf = this.readFileSync(src) as Buffer; if (flags & COPYFILE_EXCL) { if (this.existsSync(dest)) { throw createError(EEXIST, 'copyFile', src, dest); } } if (flags & COPYFILE_FICLONE_FORCE) { throw createError(ENOSYS, 'copyFile', src, dest); } this.writeFileBase(dest, buf, FLAGS.w, MODE.DEFAULT); } copyFileSync(src: PathLike, dest: PathLike, flags?: TFlagsCopy) { const srcFilename = pathToFilename(src); const destFilename = pathToFilename(dest); return this.copyFileBase(srcFilename, destFilename, (flags || 0) | 0); } copyFile(src: PathLike, dest: PathLike, callback: TCallback<void>); copyFile(src: PathLike, dest: PathLike, flags: TFlagsCopy, callback: TCallback<void>); copyFile(src: PathLike, dest: PathLike, a, b?) { const srcFilename = pathToFilename(src); const destFilename = pathToFilename(dest); let flags: TFlagsCopy; let callback: TCallback<void>; if (typeof a === 'function') { flags = 0; callback = a; } else { flags = a; callback = b; } validateCallback(callback); this.wrapAsync(this.copyFileBase, [srcFilename, destFilename, flags], callback); } linkSync(existingPath: PathLike, newPath: PathLike) { const existingPathFilename = pathToFilename(existingPath); const newPathFilename = pathToFilename(newPath); this.linkBase(existingPathFilename, newPathFilename); } link(existingPath: PathLike, newPath: PathLike, callback: TCallback<void>) { const existingPathFilename = pathToFilename(existingPath); const newPathFilename = pathToFilename(newPath); this.wrapAsync(this.linkBase, [existingPathFilename, newPathFilename], callback); } private unlinkBase(filename: string) { const steps = filenameToSteps(filename); const link = this.getLink(steps); if (!link) throw createError(ENOENT, 'unlink', filename); // TODO: Check if it is file, dir, other... Iif (link.length) throw Error('Dir not empty...'); this.deleteLink(link); const node = link.getNode(); node.nlink--; // When all hard links to i-node are deleted, remove the i-node, too. if (node.nlink <= 0) { this.deleteNode(node); } } unlinkSync(path: PathLike) { const filename = pathToFilename(path); this.unlinkBase(filename); } unlink(path: PathLike, callback: TCallback<void>) { const filename = pathToFilename(path); this.wrapAsync(this.unlinkBase, [filename], callback); } private symlinkBase(targetFilename: string, pathFilename: string): Link { const pathSteps = filenameToSteps(pathFilename); // Check if directory exists, where we about to create a symlink. const dirLink = this.getLinkParent(pathSteps); Iif (!dirLink) throw createError(ENOENT, 'symlink', targetFilename, pathFilename); const name = pathSteps[pathSteps.length - 1]; // Check if new file already exists. if (dirLink.getChild(name)) throw createError(EEXIST, 'symlink', targetFilename, pathFilename); // Create symlink. const symlink: Link = dirLink.createChild(name); symlink.getNode().makeSymlink(filenameToSteps(targetFilename)); return symlink; } // `type` argument works only on Windows. symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type) { const targetFilename = pathToFilename(target); const pathFilename = pathToFilename(path); this.symlinkBase(targetFilename, pathFilename); } symlink(target: PathLike, path: PathLike, callback: TCallback<void>); symlink(target: PathLike, path: PathLike, type: symlink.Type, callback: TCallback<void>); symlink(target: PathLike, path: PathLike, a: symlink.Type | TCallback<void>, b?: TCallback<void>) { const callback: TCallback<void> = validateCallback(typeof a === 'function' ? a : b); const targetFilename = pathToFilename(target); const pathFilename = pathToFilename(path); this.wrapAsync(this.symlinkBase, [targetFilename, pathFilename], callback); } private realpathBase(filename: string, encoding: TEncodingExtended | undefined): TDataOut { const steps = filenameToSteps(filename); const realLink = this.getResolvedLink(steps); if (!realLink) throw createError(ENOENT, 'realpath', filename); return strToEncoding(realLink.getPath() || '/', encoding); } realpathSync(path: PathLike, options?: opts.IRealpathOptions | string): TDataOut { return this.realpathBase(pathToFilename(path), getRealpathOptions(options).encoding); } realpath(path: PathLike, callback: TCallback<TDataOut>); realpath(path: PathLike, options: opts.IRealpathOptions | string, callback: TCallback<TDataOut>); realpath(path: PathLike, a: TCallback<TDataOut> | opts.IRealpathOptions | string, b?: TCallback<TDataOut>) { const [opts, callback] = getRealpathOptsAndCb(a, b); const pathFilename = pathToFilename(path); this.wrapAsync(this.realpathBase, [pathFilename, opts.encoding], callback); } private lstatBase(filename: string, bigint: false, throwIfNoEntry: true): Stats<number>; private lstatBase(filename: string, bigint: true, throwIfNoEntry: true): Stats<bigint>; private lstatBase(filename: string, bigint: true, throwIfNoEntry: false): Stats<bigint> | undefined; private lstatBase(filename: string, bigint: false, throwIfNoEntry: false): Stats<number> | undefined; private lstatBase(filename: string, bigint = false, throwIfNoEntry = false): Stats | undefined { const link = this.getLink(filenameToSteps(filename)); if (link) { return Stats.build(link.getNode(), bigint); } else if (!throwIfNoEntry) { return undefined; } else { throw createError(ENOENT, 'lstat', filename); } } lstatSync(path: PathLike): Stats<number>; lstatSync(path: PathLike, options: { throwIfNoEntry?: true | undefined }): Stats<number>; lstatSync(path: PathLike, options: { bigint: false; throwIfNoEntry?: true | undefined }): Stats<number>; lstatSync(path: PathLike, options: { bigint: true; throwIfNoEntry?: true | undefined }): Stats<bigint>; lstatSync(path: PathLike, options: { throwIfNoEntry: false }): Stats<number> | undefined; lstatSync(path: PathLike, options: { bigint: false; throwIfNoEntry: false }): Stats<number> | undefined; lstatSync(path: PathLike, options: { bigint: true; throwIfNoEntry: false }): Stats<bigint> | undefined; lstatSync(path: PathLike, options?: opts.IStatOptions): Stats | undefined { const { throwIfNoEntry = true, bigint = false } = getStatOptions(options); return this.lstatBase(pathToFilename(path), bigint as any, throwIfNoEntry as any); } lstat(path: PathLike, callback: TCallback<Stats>): void; lstat(path: PathLike, options: opts.IStatOptions, callback: TCallback<Stats>): void; lstat(path: PathLike, a: TCallback<Stats> | opts.IStatOptions, b?: TCallback<Stats>): void { const [{ throwIfNoEntry = true, bigint = false }, callback] = getStatOptsAndCb(a, b); this.wrapAsync(this.lstatBase, [pathToFilename(path), bigint, throwIfNoEntry], callback); } private statBase(filename: string): Stats<number>; private statBase(filename: string, bigint: false, throwIfNoEntry: true): Stats<number>; private statBase(filename: string, bigint: true, throwIfNoEntry: true): Stats<bigint>; private statBase(filename: string, bigint: true, throwIfNoEntry: false): Stats<bigint> | undefined; private statBase(filename: string, bigint: false, throwIfNoEntry: false): Stats<number> | undefined; private statBase(filename: string, bigint = false, throwIfNoEntry = true): Stats | undefined { const link = this.getResolvedLink(filenameToSteps(filename)); if (link) { return Stats.build(link.getNode(), bigint); } else if (!throwIfNoEntry) { return undefined; } else { throw createError(ENOENT, 'stat', filename); } } statSync(path: PathLike): Stats<number>; statSync(path: PathLike, options: { throwIfNoEntry?: true }): Stats<number>; statSync(path: PathLike, options: { throwIfNoEntry: false }): Stats<number> | undefined; statSync(path: PathLike, options: { bigint: false; throwIfNoEntry?: true }): Stats<number>; statSync(path: PathLike, options: { bigint: true; throwIfNoEntry?: true }): Stats<bigint>; statSync(path: PathLike, options: { bigint: false; throwIfNoEntry: false }): Stats<number> | undefined; statSync(path: PathLike, options: { bigint: true; throwIfNoEntry: false }): Stats<bigint> | undefined; statSync(path: PathLike, options?: opts.IStatOptions): Stats | undefined { const { bigint = true, throwIfNoEntry = true } = getStatOptions(options); return this.statBase(pathToFilename(path), bigint as any, throwIfNoEntry as any); } stat(path: PathLike, callback: TCallback<Stats>): void; stat(path: PathLike, options: opts.IStatOptions, callback: TCallback<Stats>): void; stat(path: PathLike, a: TCallback<Stats> | opts.IStatOptions, b?: TCallback<Stats>): void { const [{ bigint = false, throwIfNoEntry = true }, callback] = getStatOptsAndCb(a, b); this.wrapAsync(this.statBase, [pathToFilename(path), bigint, throwIfNoEntry], callback); } private fstatBase(fd: number): Stats<number>; private fstatBase(fd: number, bigint: false): Stats<number>; private fstatBase(fd: number, bigint: true): Stats<bigint>; private fstatBase(fd: number, bigint: boolean = false): Stats { const file = this.getFileByFd(fd); if (!file) throw createError(EBADF, 'fstat'); return Stats.build(file.node, bigint); } fstatSync(fd: number): Stats<number>; fstatSync(fd: number, options: { bigint: false }): Stats<number>; fstatSync(fd: number, options: { bigint: true }): Stats<bigint>; fstatSync(fd: number, options?: opts.IFStatOptions): Stats { return this.fstatBase(fd, getStatOptions(options).bigint as any); } fstat(fd: number, callback: TCallback<Stats>): void; fstat(fd: number, options: opts.IFStatOptions, callback: TCallback<Stats>): void; fstat(fd: number, a: TCallback<Stats> | opts.IFStatOptions, b?: TCallback<Stats>): void { const [opts, callback] = getStatOptsAndCb(a, b); this.wrapAsync(this.fstatBase, [fd, opts.bigint], callback); } private renameBase(oldPathFilename: string, newPathFilename: string) { const link = this.getLink(filenameToSteps(oldPathFilename)); if (!link) throw createError(ENOENT, 'rename', oldPathFilename, newPathFilename); // TODO: Check if it is directory, if non-empty, we cannot move it, right? const newPathSteps = filenameToSteps(newPathFilename); // Check directory exists for the new location. const newPathDirLink = this.getLinkParent(newPathSteps); Iif (!newPathDirLink) throw createError(ENOENT, 'rename', oldPathFilename, newPathFilename); // TODO: Also treat cases with directories and symbolic links. // TODO: See: http://man7.org/linux/man-pages/man2/rename.2.html // Remove hard link from old folder. const oldLinkParent = link.parent; if (oldLinkParent) { oldLinkParent.deleteChild(link); } // Rename should overwrite the new path, if that exists. const name = newPathSteps[newPathSteps.length - 1]; link.name = name; link.steps = [...newPathDirLink.steps, name]; newPathDirLink.setChild(link.getName(), link); } renameSync(oldPath: PathLike, newPath: PathLike) { const oldPathFilename = pathToFilename(oldPath); const newPathFilename = pathToFilename(newPath); this.renameBase(oldPathFilename, newPathFilename); } rename(oldPath: PathLike, newPath: PathLike, callback: TCallback<void>) { const oldPathFilename = pathToFilename(oldPath); const newPathFilename = pathToFilename(newPath); this.wrapAsync(this.renameBase, [oldPathFilename, newPathFilename], callback); } private existsBase(filename: string): boolean { return !!this.statBase(filename); } existsSync(path: PathLike): boolean { try { return this.existsBase(pathToFilename(path)); } catch (err) { return false; } } exists(path: PathLike, callback: (exists: boolean) => void) { const filename = pathToFilename(path); if (typeof callback !== 'function') throw Error(ERRSTR.CB); setImmediate(() => { try { callback(this.existsBase(filename)); } catch (err) { callback(false); } }); } private accessBase(filename: string, mode: number) { const link = this.getLinkOrThrow(filename, 'access'); // TODO: Verify permissions } accessSync(path: PathLike, mode: number = F_OK) { const filename = pathToFilename(path); mode = mode | 0; this.accessBase(filename, mode); } access(path: PathLike, callback: TCallback<void>); access(path: PathLike, mode: number, callback: TCallback<void>); access(path: PathLike, a: TCallback<void> | number, b?: TCallback<void>) { let mode: number = F_OK; let callback: TCallback<void>; Iif (typeof a !== 'function') { mode = a | 0; // cast to number callback = validateCallback(b); } else { callback = a; } const filename = pathToFilename(path); this.wrapAsync(this.accessBase, [filename, mode], callback); } appendFileSync(id: TFileId, data: TData, options?: IAppendFileOptions | string) { const opts = getAppendFileOpts(options); // force append behavior when using a supplied file descriptor Iif (!opts.flag || isFd(id)) opts.flag = 'a'; this.writeFileSync(id, data, opts); } appendFile(id: TFileId, data: TData, callback: TCallback<void>); appendFile(id: TFileId, data: TData, options: IAppendFileOptions | string, callback: TCallback<void>); appendFile(id: TFileId, data: TData, a, b?) { const [opts, callback] = getAppendFileOptsAndCb(a, b); // force append behavior when using a supplied file descriptor if (!opts.flag || isFd(id)) opts.flag = 'a'; this.writeFile(id, data, opts, callback); } private readdirBase(filename: string, options: opts.IReaddirOptions): TDataOut[] | Dirent[] { const steps = filenameToSteps(filename); const link: Link | null = this.getResolvedLink(steps); if (!link) throw createError(ENOENT, 'readdir', filename); const node = link.getNode(); Iif (!node.isDirectory()) throw createError(ENOTDIR, 'scandir', filename); const list: Dirent[] = []; // output list for (const name of link.children.keys()) { const child = link.getChild(name); if (!child || name === '.' || name === '..') continue; list.push(Dirent.build(child, options.encoding)); // recursion if (options.recursive && child.children.size) { const recurseOptions = { ...options, recursive: true, withFileTypes: true }; const childList = this.readdirBase(child.getPath(), recurseOptions) as Dirent[]; list.push(...childList); } } if (!isWin && options.encoding !== 'buffer') list.sort((a, b) => { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; }); if (options.withFileTypes) return list; let filename2 = filename; Iif (isWin) { filename2 = filename2.replace(/\\/g, '/'); } return list.map(dirent => { if (options.recursive) { let fullPath = pathModule.join(dirent.path, dirent.name.toString()); Iif (isWin) { fullPath = fullPath.replace(/\\/g, '/'); } return fullPath.replace(filename2 + pathModule.posix.sep, ''); } return dirent.name; }); } readdirSync(path: PathLike, options?: opts.IReaddirOptions | string): TDataOut[] | Dirent[] { const opts = getReaddirOptions(options); const filename = pathToFilename(path); return this.readdirBase(filename, opts); } readdir(path: PathLike, callback: TCallback<TDataOut[] | Dirent[]>); readdir(path: PathLike, options: opts.IReaddirOptions | string, callback: TCallback<TDataOut[] | Dirent[]>); readdir(path: PathLike, a?, b?) { const [options, callback] = getReaddirOptsAndCb(a, b); const filename = pathToFilename(path); this.wrapAsync(this.readdirBase, [filename, options], callback); } private readlinkBase(filename: string, encoding: TEncodingExtended | undefined): TDataOut { const link = this.getLinkOrThrow(filename, 'readlink'); const node = link.getNode(); Iif (!node.isSymlink()) throw createError(EINVAL, 'readlink', filename); const str = sep + node.symlink.join(sep); return strToEncoding(str, encoding); } readlinkSync(path: PathLike, options?: opts.IOptions): TDataOut { const opts = getDefaultOpts(options); const filename = pathToFilename(path); return this.readlinkBase(filename, opts.encoding); } readlink(path: PathLike, callback: TCallback<TDataOut>); readlink(path: PathLike, options: opts.IOptions, callback: TCallback<TDataOut>); readlink(path: PathLike, a: TCallback<TDataOut> | opts.IOptions, b?: TCallback<TDataOut>) { const [opts, callback] = getDefaultOptsAndCb(a, b); const filename = pathToFilename(path); this.wrapAsync(this.readlinkBase, [filename, opts.encoding], callback); } private fsyncBase(fd: number) { this.getFileByFdOrThrow(fd, 'fsync'); } fsyncSync(fd: number) { this.fsyncBase(fd); } fsync(fd: number, callback: TCallback<void>) { this.wrapAsync(this.fsyncBase, [fd], callback); } private fdatasyncBase(fd: number) { this.getFileByFdOrThrow(fd, 'fdatasync'); } fdatasyncSync(fd: number) { this.fdatasyncBase(fd); } fdatasync(fd: number, callback: TCallback<void>) { this.wrapAsync(this.fdatasyncBase, [fd], callback); } private ftruncateBase(fd: number, len?: number) { const file = this.getFileByFdOrThrow(fd, 'ftruncate'); file.truncate(len); } ftruncateSync(fd: number, len?: number) { this.ftruncateBase(fd, len); } ftruncate(fd: number, callback: TCallback<void>); ftruncate(fd: number, len: number, callback: TCallback<void>); ftruncate(fd: number, a: TCallback<void> | number, b?: TCallback<void>) { const len: number = typeof a === 'number' ? a : 0; const callback: TCallback<void> = validateCallback(typeof a === 'number' ? b : a); this.wrapAsync(this.ftruncateBase, [fd, len], callback); } private truncateBase(path: PathLike, len?: number) { const fd = this.openSync(path, 'r+'); try { this.ftruncateSync(fd, len); } finally { this.closeSync(fd); } } /** * `id` should be a file descriptor or a path. `id` as file descriptor will * not be supported soon. */ truncateSync(id: TFileId, len?: number) { if (isFd(id)) return this.ftruncateSync(id as number, len); this.truncateBase(id as PathLike, len); } truncate(id: TFileId, callback: TCallback<void>); truncate(id: TFileId, len: number, callback: TCallback<void>); truncate(id: TFileId, a: TCallback<void> | number, b?: TCallback<void>) { const len: number = typeof a === 'number' ? a : 0; const callback: TCallback<void> = validateCallback(typeof a === 'number' ? b : a); Iif (isFd(id)) return this.ftruncate(id as number, len, callback); this.wrapAsync(this.truncateBase, [id, len], callback); } private futimesBase(fd: number, atime: number, mtime: number) { const file = this.getFileByFdOrThrow(fd, 'futimes'); const node = file.node; node.atime = new Date(atime * 1000); node.mtime = new Date(mtime * 1000); } futimesSync(fd: number, atime: TTime, mtime: TTime) { this.futimesBase(fd, toUnixTimestamp(atime), toUnixTimestamp(mtime)); } futimes(fd: number, atime: TTime, mtime: TTime, callback: TCallback<void>) { this.wrapAsync(this.futimesBase, [fd, toUnixTimestamp(atime), toUnixTimestamp(mtime)], callback); } private utimesBase(filename: string, atime: number, mtime: number) { const fd = this.openSync(filename, 'r'); try { this.futimesBase(fd, atime, mtime); } finally { this.closeSync(fd); } } utimesSync(path: PathLike, atime: TTime, mtime: TTime) { this.utimesBase(pathToFilename(path), toUnixTimestamp(atime), toUnixTimestamp(mtime)); } utimes(path: PathLike, atime: TTime, mtime: TTime, callback: TCallback<void>) { this.wrapAsync(this.utimesBase, [pathToFilename(path), toUnixTimestamp(atime), toUnixTimestamp(mtime)], callback); } private mkdirBase(filename: string, modeNum: number) { const steps = filenameToSteps(filename); // This will throw if user tries to create root dir `fs.mkdirSync('/')`. if (!steps.length) { throw createError(EEXIST, 'mkdir', filename); } const dir = this.getLinkParentAsDirOrThrow(filename, 'mkdir'); // Check path already exists. const name = steps[steps.length - 1]; if (dir.getChild(name)) throw createError(EEXIST, 'mkdir', filename); dir.createChild(name, this.createNode(true, modeNum)); } /** * Creates directory tree recursively. * @param filename * @param modeNum */ private mkdirpBase(filename: string, modeNum: number) { const fullPath = resolve(filename); const fullPathSansSlash = fullPath.substring(1); const steps = !fullPathSansSlash ? [] : fullPathSansSlash.split(sep); let link = this.root; let created = false; for (let i = 0; i < steps.length; i++) { const step = steps[i]; Iif (!link.getNode().isDirectory()) throw createError(ENOTDIR, 'mkdir', link.getPath()); const child = link.getChild(step); if (child) { if (child.getNode().isDirectory()) link = child; else throw createError(ENOTDIR, 'mkdir', child.getPath()); } else { link = link.createChild(step, this.createNode(true, modeNum)); created = true; } } return created ? fullPath : undefined; } mkdirSync(path: PathLike, options: opts.IMkdirOptions & { recursive: true }): string | undefined; mkdirSync(path: PathLike, options?: TMode | (opts.IMkdirOptions & { recursive?: false })): void; mkdirSync(path: PathLike, options?: TMode | opts.IMkdirOptions): string | undefined; mkdirSync(path: PathLike, options?: TMode | opts.IMkdirOptions) { const opts = getMkdirOptions(options); const modeNum = modeToNumber(opts.mode, 0o777); const filename = pathToFilename(path); if (opts.recursive) return this.mkdirpBase(filename, modeNum); this.mkdirBase(filename, modeNum); } mkdir(path: PathLike, callback: TCallback<void>); mkdir(path: PathLike, mode: TMode | (opts.IMkdirOptions & { recursive?: false }), callback: TCallback<void>); mkdir(path: PathLike, mode: opts.IMkdirOptions & { recursive: true }, callback: TCallback<string>); mkdir(path: PathLike, mode: TMode | opts.IMkdirOptions, callback: TCallback<string>); mkdir(path: PathLike, a: TCallback<void> | TMode | opts.IMkdirOptions, b?: TCallback<string> | TCallback<void>) { const opts: TMode | opts.IMkdirOptions = getMkdirOptions(a); const callback = validateCallback(typeof a === 'function' ? a : b!); const modeNum = modeToNumber(opts.mode, 0o777); const filename = pathToFilename(path); if (opts.recursive) this.wrapAsync(this.mkdirpBase, [filename, modeNum], callback); else this.wrapAsync(this.mkdirBase, [filename, modeNum], callback); } private mkdtempBase(prefix: string, encoding?: TEncodingExtended, retry: number = 5): TDataOut { const filename = prefix + genRndStr6(); try { this.mkdirBase(filename, MODE.DIR); return strToEncoding(filename, encoding); } catch (err) { Iif (err.code === EEXIST) { if (retry > 1) return this.mkdtempBase(prefix, encoding, retry - 1); else throw Error('Could not create temp dir.'); } else throw err; } } mkdtempSync(prefix: string, options?: opts.IOptions): TDataOut { const { encoding } = getDefaultOpts(options); if (!prefix || typeof prefix !== 'string') throw new TypeError('filename prefix is required'); nullCheck(prefix); return this.mkdtempBase(prefix, encoding); } mkdtemp(prefix: string, callback: TCallback<string>); mkdtemp(prefix: string, options: opts.IOptions, callback: TCallback<string>); mkdtemp(prefix: string, a: TCallback<string> | opts.IOptions, b?: TCallback<string>) { const [{ encoding }, callback] = getDefaultOptsAndCb(a, b); if (!prefix || typeof prefix !== 'string') throw new TypeError('filename prefix is required'); Iif (!nullCheck(prefix)) return; this.wrapAsync(this.mkdtempBase, [prefix, encoding], callback); } private rmdirBase(filename: string, options?: opts.IRmdirOptions) { const opts = getRmdirOptions(options); const link = this.getLinkAsDirOrThrow(filename, 'rmdir'); // Check directory is empty. if (link.length && !opts.recursive) throw createError(ENOTEMPTY, 'rmdir', filename); this.deleteLink(link); } rmdirSync(path: PathLike, options?: opts.IRmdirOptions) { this.rmdirBase(pathToFilename(path), options); } rmdir(path: PathLike, callback: TCallback<void>); rmdir(path: PathLike, options: opts.IRmdirOptions, callback: TCallback<void>); rmdir(path: PathLike, a: TCallback<void> | opts.IRmdirOptions, b?: TCallback<void>) { const opts: opts.IRmdirOptions = getRmdirOptions(a); const callback: TCallback<void> = validateCallback(typeof a === 'function' ? a : b); this.wrapAsync(this.rmdirBase, [pathToFilename(path), opts], callback); } private rmBase(filename: string, options: opts.IRmOptions = {}): void { const link = this.getResolvedLink(filename); if (!link) { // "stat" is used to match Node's native error message. if (!options.force) throw createError(ENOENT, 'stat', filename); return; } if (link.getNode().isDirectory()) { if (!options.recursive) { throw createError(ERR_FS_EISDIR, 'rm', filename); } } this.deleteLink(link); } public rmSync(path: PathLike, options?: opts.IRmOptions): void { this.rmBase(pathToFilename(path), options); } public rm(path: PathLike, callback: TCallback<void>): void; public rm(path: PathLike, options: opts.IRmOptions, callback: TCallback<void>): void; public rm(path: PathLike, a: TCallback<void> | opts.IRmOptions, b?: TCallback<void>): void { const [opts, callback] = getRmOptsAndCb(a, b); this.wrapAsync(this.rmBase, [pathToFilename(path), opts], callback); } private fchmodBase(fd: number, modeNum: number) { const file = this.getFileByFdOrThrow(fd, 'fchmod'); file.chmod(modeNum); } fchmodSync(fd: number, mode: TMode) { this.fchmodBase(fd, modeToNumber(mode)); } fchmod(fd: number, mode: TMode, callback: TCallback<void>) { this.wrapAsync(this.fchmodBase, [fd, modeToNumber(mode)], callback); } private chmodBase(filename: string, modeNum: number) { const fd = this.openSync(filename, 'r'); try { this.fchmodBase(fd, modeNum); } finally { this.closeSync(fd); } } chmodSync(path: PathLike, mode: TMode) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.chmodBase(filename, modeNum); } chmod(path: PathLike, mode: TMode, callback: TCallback<void>) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.wrapAsync(this.chmodBase, [filename, modeNum], callback); } private lchmodBase(filename: string, modeNum: number) { const fd = this.openBase(filename, O_RDWR, 0, false); try { this.fchmodBase(fd, modeNum); } finally { this.closeSync(fd); } } lchmodSync(path: PathLike, mode: TMode) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.lchmodBase(filename, modeNum); } lchmod(path: PathLike, mode: TMode, callback: TCallback<void>) { const modeNum = modeToNumber(mode); const filename = pathToFilename(path); this.wrapAsync(this.lchmodBase, [filename, modeNum], callback); } private fchownBase(fd: number, uid: number, gid: number) { this.getFileByFdOrThrow(fd, 'fchown').chown(uid, gid); } fchownSync(fd: number, uid: number, gid: number) { validateUid(uid); validateGid(gid); this.fchownBase(fd, uid, gid); } fchown(fd: number, uid: number, gid: number, callback: TCallback<void>) { validateUid(uid); validateGid(gid); this.wrapAsync(this.fchownBase, [fd, uid, gid], callback); } private chownBase(filename: string, uid: number, gid: number) { const link = this.getResolvedLinkOrThrow(filename, 'chown'); const node = link.getNode(); node.chown(uid, gid); // if(node.isFile() || node.isSymlink()) { // // } else if(node.isDirectory()) { // // } else { // TODO: What do we do here? // } } chownSync(path: PathLike, uid: number, gid: number) { validateUid(uid); validateGid(gid); this.chownBase(pathToFilename(path), uid, gid); } chown(path: PathLike, uid: number, gid: number, callback: TCallback<void>) { validateUid(uid); validateGid(gid); this.wrapAsync(this.chownBase, [pathToFilename(path), uid, gid], callback); } private lchownBase(filename: string, uid: number, gid: number) { this.getLinkOrThrow(filename, 'lchown').getNode().chown(uid, gid); } lchownSync(path: PathLike, uid: number, gid: number) { validateUid(uid); validateGid(gid); this.lchownBase(pathToFilename(path), uid, gid); } lchown(path: PathLike, uid: number, gid: number, callback: TCallback<void>) { validateUid(uid); validateGid(gid); this.wrapAsync(this.lchownBase, [pathToFilename(path), uid, gid], callback); } private statWatchers: Record<string, StatWatcher> = {}; watchFile(path: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher; watchFile(path: PathLike, options: IWatchFileOptions, listener: (curr: Stats, prev: Stats) => void): StatWatcher; watchFile(path: PathLike, a, b?): StatWatcher { const filename = pathToFilename(path); let options: IWatchFileOptions | null = a; let listener: (curr: Stats, prev: Stats) => void = b; Iif (typeof options === 'function') { listener = a; options = null; } Iif (typeof listener !== 'function') { throw Error('"watchFile()" requires a listener function'); } let interval = 5007; let persistent = true; if (options && typeof options === 'object') { if (typeof options.interval === 'number') interval = options.interval; Iif (typeof options.persistent === 'boolean') persistent = options.persistent; } let watcher: StatWatcher = this.statWatchers[filename]; if (!watcher) { watcher = new this.StatWatcher(); watcher.start(filename, persistent, interval); this.statWatchers[filename] = watcher; } watcher.addListener('change', listener); return watcher; } unwatchFile(path: PathLike, listener?: (curr: Stats, prev: Stats) => void) { const filename = pathToFilename(path); const watcher = this.statWatchers[filename]; Iif (!watcher) return; Iif (typeof listener === 'function') { watcher.removeListener('change', listener); } else { watcher.removeAllListeners('change'); } if (watcher.listenerCount('change') === 0) { watcher.stop(); delete this.statWatchers[filename]; } } createReadStream(path: misc.PathLike, options?: opts.IReadStreamOptions | string): misc.IReadStream { return new this.ReadStream(path, options); } createWriteStream(path: PathLike, options?: opts.IWriteStreamOptions | string): IWriteStream { return new this.WriteStream(path, options); } // watch(path: PathLike): FSWatcher; // watch(path: PathLike, options?: IWatchOptions | string): FSWatcher; watch( path: PathLike, options?: IWatchOptions | string, listener?: (eventType: string, filename: string) => void, ): FSWatcher { const filename = pathToFilename(path); let givenOptions: typeof options | null = options; if (typeof options === 'function') { listener = options; givenOptions = null; } // tslint:disable-next-line prefer-const let { persistent, recursive, encoding }: IWatchOptions = getDefaultOpts(givenOptions); if (persistent === undefined) persistent = true; if (recursive === undefined) recursive = false; const watcher = new this.FSWatcher(); watcher.start(filename, persistent, recursive, encoding as BufferEncoding); if (listener) { watcher.addListener('change', listener); } return watcher; } public cpSync: FsSynchronousApi['cpSync'] = notImplemented; public lutimesSync: FsSynchronousApi['lutimesSync'] = notImplemented; public statfsSync: FsSynchronousApi['statfsSync'] = notImplemented; public opendirSync: FsSynchronousApi['opendirSync'] = notImplemented; public cp: FsCallbackApi['cp'] = notImplemented; public lutimes: FsCallbackApi['lutimes'] = notImplemented; public statfs: FsCallbackApi['statfs'] = notImplemented; public openAsBlob: FsCallbackApi['openAsBlob'] = notImplemented; public opendir: FsCallbackApi['opendir'] = notImplemented; } function emitStop(self) { self.emit('stop'); } export class StatWatcher extends EventEmitter { vol: Volume; filename: string; interval: number; timeoutRef?; setTimeout: TSetTimeout; prev: Stats; constructor(vol: Volume) { super(); this.vol = vol; } private loop() { this.timeoutRef = this.setTimeout(this.onInterval, this.interval); } private hasChanged(stats: Stats): boolean { // if(!this.prev) return false; if (stats.mtimeMs > this.prev.mtimeMs) return true; Iif (stats.nlink !== this.prev.nlink) return true; return false; } private onInterval = () => { try { const stats = this.vol.statSync(this.filename); if (this.hasChanged(stats)) { this.emit('change', stats, this.prev); this.prev = stats; } } finally { this.loop(); } }; start(path: string, persistent: boolean = true, interval: number = 5007) { this.filename = pathToFilename(path); this.setTimeout = persistent ? setTimeout.bind(typeof globalThis !== 'undefined' ? globalThis : global) : setTimeoutUnref; this.interval = interval; this.prev = this.vol.statSync(this.filename); this.loop(); } stop() { clearTimeout(this.timeoutRef); queueMicrotask(() => { emitStop.call(this, this); }); } } /* tslint:disable no-var-keyword prefer-const */ // ---------------------------------------- ReadStream var pool; function allocNewPool(poolSize) { pool = bufferAllocUnsafe(poolSize); pool.used = 0; } util.inherits(FsReadStream, Readable); exports.ReadStream = FsReadStream; function FsReadStream(vol, path, options) { Iif (!(this instanceof FsReadStream)) return new (FsReadStream as any)(vol, path, options); this._vol = vol; // a little bit bigger buffer and water marks by default options = Object.assign({}, getOptions(options, {})); if (options.highWaterMark === undefined) options.highWaterMark = 64 * 1024; Readable.call(this, options); this.path = pathToFilename(path); this.fd = options.fd === undefined ? null : options.fd; this.flags = options.flags === undefined ? 'r' : options.flags; this.mode = options.mode === undefined ? 0o666 : options.mode; this.start = options.start; this.end = options.end; this.autoClose = options.autoClose === undefined ? true : options.autoClose; this.pos = undefined; this.bytesRead = 0; Iif (this.start !== undefined) { Iif (typeof this.start !== 'number') { throw new TypeError('"start" option must be a Number'); } if (this.end === undefined) { this.end = Infinity; } else Iif (typeof this.end !== 'number') { throw new TypeError('"end" option must be a Number'); } Iif (this.start > this.end) { throw new Error('"start" option must be <= "end" option'); } this.pos = this.start; } if (typeof this.fd !== 'number') this.open(); this.on('end', function () { Iif (this.autoClose) { Iif (this.destroy) this.destroy(); } }); } FsReadStream.prototype.open = function () { var self = this; // tslint:disable-line no-this-assignment this._vol.open(this.path, this.flags, this.mode, (er, fd) => { Iif (er) { Iif (self.autoClose) { Iif (self.destroy) self.destroy(); } self.emit('error', er); return; } self.fd = fd; self.emit('open', fd); // start the flow of data. self.read(); }); }; FsReadStream.prototype._read = function (n) { if (typeof this.fd !== 'number') { return this.once('open', function () { this._read(n); }); } Iif (this.destroyed) return; if (!pool || pool.length - pool.used < kMinPoolSpace) { // discard the old pool. allocNewPool(this._readableState.highWaterMark); } // Grab another reference to the pool in the case that while we're // in the thread pool another read() finishes up the pool, and // allocates a new one. var thisPool = pool; var toRead = Math.min(pool.length - pool.used, n); var start = pool.used; Iif (this.pos !== undefined) toRead = Math.min(this.end - this.pos + 1, toRead); // already read everything we were supposed to read! // treat as EOF. Iif (toRead <= 0) return this.push(null); // the actual read. var self = this; // tslint:disable-line no-this-assignment this._vol.read(this.fd, pool, pool.used, toRead, this.pos, onread); // move the pool positions, and internal position for reading. Iif (this.pos !== undefined) this.pos += toRead; pool.used += toRead; function onread(er, bytesRead) { Iif (er) { Iif (self.autoClose && self.destroy) { self.destroy(); } self.emit('error', er); } else { var b = null; if (bytesRead > 0) { self.bytesRead += bytesRead; b = thisPool.slice(start, start + bytesRead); } self.push(b); } } }; FsReadStream.prototype._destroy = function (err, cb) { this.close(err2 => { cb(err || err2); }); }; FsReadStream.prototype.close = function (cb) { Iif (cb) this.once('close', cb); Iif (this.closed || typeof this.fd !== 'number') { Iif (typeof this.fd !== 'number') { this.once('open', closeOnOpen); return; } return queueMicrotask(() => this.emit('close')); } // Since Node 18, there is only a getter for '.closed'. // The first branch mimics other setters from Readable. // See https://github.com/nodejs/node/blob/v18.0.0/lib/internal/streams/readable.js#L1243 if (typeof this._readableState?.closed === 'boolean') { this._readableState.closed = true; } else { this.closed = true; } this._vol.close(this.fd, er => { if (er) this.emit('error', er); else this.emit('close'); }); this.fd = null; }; // needed because as it will be called with arguments // that does not match this.close() signature function closeOnOpen(fd) { this.close(); } // ---------------------------------------- WriteStream export interface IWriteStream extends Writable { bytesWritten: number; path: string; pending: boolean; new (path: PathLike, options: opts.IWriteStreamOptions); open(); close(); } util.inherits(FsWriteStream, Writable); exports.WriteStream = FsWriteStream; function FsWriteStream(vol, path, options) { Iif (!(this instanceof FsWriteStream)) return new (FsWriteStream as any)(vol, path, options); this._vol = vol; options = Object.assign({}, getOptions(options, {})); Writable.call(this, options); this.path = pathToFilename(path); this.fd = options.fd === undefined ? null : options.fd; this.flags = options.flags === undefined ? 'w' : options.flags; this.mode = options.mode === undefined ? 0o666 : options.mode; this.start = options.start; this.autoClose = options.autoClose === undefined ? true : !!options.autoClose; this.pos = undefined; this.bytesWritten = 0; this.pending = true; Iif (this.start !== undefined) { Iif (typeof this.start !== 'number') { throw new TypeError('"start" option must be a Number'); } Iif (this.start < 0) { throw new Error('"start" must be >= zero'); } this.pos = this.start; } Iif (options.encoding) this.setDefaultEncoding(options.encoding); if (typeof this.fd !== 'number') this.open(); // dispose on finish. this.once('finish', function () { if (this.autoClose) { this.close(); } }); } FsWriteStream.prototype.open = function () { this._vol.open( this.path, this.flags, this.mode, function (er, fd) { Iif (er) { Iif (this.autoClose && this.destroy) { this.destroy(); } this.emit('error', er); return; } this.fd = fd; this.pending = false; this.emit('open', fd); }.bind(this), ); }; FsWriteStream.prototype._write = function (data, encoding, cb) { Iif (!(data instanceof Buffer || data instanceof Uint8Array)) return this.emit('error', new Error('Invalid data')); if (typeof this.fd !== 'number') { return this.once('open', function () { this._write(data, encoding, cb); }); } var self = this; // tslint:disable-line no-this-assignment this._vol.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { Iif (er) { Iif (self.autoClose && self.destroy) { self.destroy(); } return cb(er); } self.bytesWritten += bytes; cb(); }); Iif (this.pos !== undefined) this.pos += data.length; }; FsWriteStream.prototype._writev = function (data, cb) { Iif (typeof this.fd !== 'number') { return this.once('open', function () { this._writev(data, cb); }); } const self = this; // tslint:disable-line no-this-assignment const len = data.length; const chunks = new Array(len); var size = 0; for (var i = 0; i < len; i++) { var chunk = data[i].chunk; chunks[i] = chunk; size += chunk.length; } const buf = Buffer.concat(chunks); this._vol.write(this.fd, buf, 0, buf.length, this.pos, (er, bytes) => { Iif (er) { Iif (self.destroy) self.destroy(); return cb(er); } self.bytesWritten += bytes; cb(); }); Iif (this.pos !== undefined) this.pos += size; }; FsWriteStream.prototype.close = function (cb) { if (cb) this.once('close', cb); if (this.closed || typeof this.fd !== 'number') { if (typeof this.fd !== 'number') { this.once('open', closeOnOpen); return; } return queueMicrotask(() => this.emit('close')); } // Since Node 18, there is only a getter for '.closed'. // The first branch mimics other setters from Writable. // See https://github.com/nodejs/node/blob/v18.0.0/lib/internal/streams/writable.js#L766 if (typeof this._writableState?.closed === 'boolean') { this._writableState.closed = true; } else E{ this.closed = true; } this._vol.close(this.fd, er => { if (er) this.emit('error', er); else this.emit('close'); }); this.fd = null; }; FsWriteStream.prototype._destroy = FsReadStream.prototype._destroy; // There is no shutdown() for files. FsWriteStream.prototype.destroySoon = FsWriteStream.prototype.end; // ---------------------------------------- FSWatcher export class FSWatcher extends EventEmitter { _vol: Volume; _filename: string = ''; _steps: string[]; _filenameEncoded: TDataOut = ''; // _persistent: boolean = true; _recursive: boolean = false; _encoding: BufferEncoding = ENCODING_UTF8; _link: Link; _timer; // Timer that keeps this task persistent. // inode -> removers private _listenerRemovers = new Map<number, Array<() => void>>(); constructor(vol: Volume) { super(); this._vol = vol; // TODO: Emit "error" messages when watching. // this._handle.onchange = function(status, eventType, filename) { // if (status < 0) { // self._handle.close(); // const error = !filename ? // errnoException(status, 'Error watching file for changes:') : // errnoException(status, `Error watching file ${filename} for changes:`); // error.filename = filename; // self.emit('error', error); // } else { // self.emit('change', eventType, filename); // } // }; } private _getName(): string { return this._steps[this._steps.length - 1]; } private _onParentChild = (link: Link) => { Iif (link.getName() === this._getName()) { this._emit('rename'); } }; private _emit = (type: 'change' | 'rename') => { this.emit('change', type, this._filenameEncoded); }; private _persist = () => { this._timer = setTimeout(this._persist, 1e6); }; start( path: PathLike, persistent: boolean = true, recursive: boolean = false, encoding: BufferEncoding = ENCODING_UTF8, ) { this._filename = pathToFilename(path); this._steps = filenameToSteps(this._filename); this._filenameEncoded = strToEncoding(this._filename); // this._persistent = persistent; this._recursive = recursive; this._encoding = encoding; try { this._link = this._vol.getLinkOrThrow(this._filename, 'FSWatcher'); } catch (err) { const error = new Error(`watch ${this._filename} ${err.code}`); (error as any).code = err.code; (error as any).errno = err.code; throw error; } const watchLinkNodeChanged = (link: Link) => { const filepath = link.getPath(); const node = link.getNode(); const onNodeChange = () => { let filename = relative(this._filename, filepath); if (!filename) { filename = this._getName(); } return this.emit('change', 'change', filename); }; node.on('change', onNodeChange); const removers = this._listenerRemovers.get(node.ino) ?? []; removers.push(() => node.removeListener('change', onNodeChange)); this._listenerRemovers.set(node.ino, removers); }; const watchLinkChildrenChanged = (link: Link) => { const node = link.getNode(); // when a new link added const onLinkChildAdd = (l: Link) => { this.emit('change', 'rename', relative(this._filename, l.getPath())); setTimeout(() => { // 1. watch changes of the new link-node watchLinkNodeChanged(l); // 2. watch changes of the new link-node's children watchLinkChildrenChanged(l); }); }; // when a new link deleted const onLinkChildDelete = (l: Link) => { // remove the listeners of the children nodes const removeLinkNodeListeners = (curLink: Link) => { const ino = curLink.getNode().ino; const removers = this._listenerRemovers.get(ino); if (removers) { removers.forEach(r => r()); this._listenerRemovers.delete(ino); } for (const [name, childLink] of curLink.children.entries()) { Iif (childLink && name !== '.' && name !== '..') { removeLinkNodeListeners(childLink); } } }; removeLinkNodeListeners(l); this.emit('change', 'rename', relative(this._filename, l.getPath())); }; // children nodes changed for (const [name, childLink] of link.children.entries()) { if (childLink && name !== '.' && name !== '..') { watchLinkNodeChanged(childLink); } } // link children add/remove link.on('child:add', onLinkChildAdd); link.on('child:delete', onLinkChildDelete); const removers = this._listenerRemovers.get(node.ino) ?? []; removers.push(() => { link.removeListener('child:add', onLinkChildAdd); link.removeListener('child:delete', onLinkChildDelete); }); if (recursive) { for (const [name, childLink] of link.children.entries()) { if (childLink && name !== '.' && name !== '..') { watchLinkChildrenChanged(childLink); } } } }; watchLinkNodeChanged(this._link); watchLinkChildrenChanged(this._link); const parent = this._link.parent; if (parent) { // parent.on('child:add', this._onParentChild); parent.setMaxListeners(parent.getMaxListeners() + 1); parent.on('child:delete', this._onParentChild); } if (persistent) this._persist(); } close() { clearTimeout(this._timer); this._listenerRemovers.forEach(removers => { removers.forEach(r => r()); }); this._listenerRemovers.clear(); const parent = this._link.parent; if (parent) { // parent.removeListener('child:add', this._onParentChild); parent.removeListener('child:delete', this._onParentChild); } } } |