bare-fs 1.9.1 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +5 -0
  2. package/binding.c +240 -3
  3. package/index.js +302 -44
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -26,6 +26,7 @@ fs.read
26
26
  fs.readdir
27
27
  fs.readlink
28
28
  fs.readv
29
+ fs.realpath
29
30
  fs.rename
30
31
  fs.rmdir
31
32
  fs.stat
@@ -44,6 +45,7 @@ fs.promises.opendir
44
45
  fs.promises.readFile
45
46
  fs.promises.readdir
46
47
  fs.promises.readlink
48
+ fs.promises.realpath
47
49
  fs.promises.rename
48
50
  fs.promises.rmdir
49
51
  fs.promises.stat
@@ -61,8 +63,11 @@ fs.fstatSync
61
63
  fs.lstatSync
62
64
  fs.mkdirSync
63
65
  fs.openSync
66
+ fs.opendirSync
64
67
  fs.readSync
68
+ fs.readdirSync
65
69
  fs.readlinkSync
70
+ fs.realpathSync
66
71
  fs.renameSync
67
72
  fs.rmdirSync
68
73
  fs.statSync
package/binding.c CHANGED
@@ -153,6 +153,29 @@ on_fs_stat_response (uv_fs_t *uv_req) {
153
153
  on_fs_response(uv_req);
154
154
  }
155
155
 
156
+ static void
157
+ on_fs_realpath_response (uv_fs_t *uv_req) {
158
+ int err;
159
+
160
+ bare_fs_req_t *req = (bare_fs_req_t *) uv_req;
161
+
162
+ js_env_t *env = req->env;
163
+
164
+ if (uv_req->result == 0) {
165
+ js_value_t *data;
166
+ err = js_get_reference_value(env, req->data, &data);
167
+ assert(err == 0);
168
+
169
+ char *path;
170
+ err = js_get_typedarray_info(env, data, NULL, (void **) &path, NULL, NULL, NULL);
171
+ assert(err == 0);
172
+
173
+ strncpy(path, uv_req->ptr, sizeof(bare_fs_path_t));
174
+ }
175
+
176
+ on_fs_response(uv_req);
177
+ }
178
+
156
179
  static void
157
180
  on_fs_readlink_response (uv_fs_t *uv_req) {
158
181
  int err;
@@ -1522,6 +1545,76 @@ bare_fs_unlink_sync (js_env_t *env, js_callback_info_t *info) {
1522
1545
  return NULL;
1523
1546
  }
1524
1547
 
1548
+ static js_value_t *
1549
+ bare_fs_realpath (js_env_t *env, js_callback_info_t *info) {
1550
+ int err;
1551
+
1552
+ size_t argc = 3;
1553
+ js_value_t *argv[3];
1554
+
1555
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1556
+ assert(err == 0);
1557
+
1558
+ assert(argc == 3);
1559
+
1560
+ bare_fs_req_t *req;
1561
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
1562
+ assert(err == 0);
1563
+
1564
+ bare_fs_path_t path;
1565
+ err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
1566
+ assert(err == 0);
1567
+
1568
+ err = js_create_reference(env, argv[2], 1, &req->data);
1569
+ assert(err == 0);
1570
+
1571
+ uv_loop_t *loop;
1572
+ js_get_env_loop(env, &loop);
1573
+
1574
+ uv_fs_realpath(loop, (uv_fs_t *) req, (char *) path, on_fs_realpath_response);
1575
+
1576
+ return NULL;
1577
+ }
1578
+
1579
+ static js_value_t *
1580
+ bare_fs_realpath_sync (js_env_t *env, js_callback_info_t *info) {
1581
+ int err;
1582
+
1583
+ size_t argc = 2;
1584
+ js_value_t *argv[2];
1585
+
1586
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1587
+ assert(err == 0);
1588
+
1589
+ assert(argc == 2);
1590
+
1591
+ bare_fs_path_t path;
1592
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1593
+ assert(err == 0);
1594
+
1595
+ uv_loop_t *loop;
1596
+ js_get_env_loop(env, &loop);
1597
+
1598
+ uv_fs_t req;
1599
+ uv_fs_realpath(loop, &req, (char *) path, NULL);
1600
+
1601
+ js_value_t *res = NULL;
1602
+
1603
+ if (req.result < 0) {
1604
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1605
+ } else {
1606
+ char *path;
1607
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &path, NULL, NULL, NULL);
1608
+ assert(err == 0);
1609
+
1610
+ strncpy(path, req.ptr, sizeof(bare_fs_path_t));
1611
+ }
1612
+
1613
+ uv_fs_req_cleanup(&req);
1614
+
1615
+ return res;
1616
+ }
1617
+
1525
1618
  static js_value_t *
1526
1619
  bare_fs_readlink (js_env_t *env, js_callback_info_t *info) {
1527
1620
  int err;
@@ -1575,8 +1668,6 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1575
1668
  uv_fs_t req;
1576
1669
  uv_fs_readlink(loop, &req, (char *) path, NULL);
1577
1670
 
1578
- js_value_t *res = NULL;
1579
-
1580
1671
  if (req.result < 0) {
1581
1672
  js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1582
1673
  } else {
@@ -1589,7 +1680,7 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1589
1680
 
1590
1681
  uv_fs_req_cleanup(&req);
1591
1682
 
1592
- return res;
1683
+ return NULL;
1593
1684
  }
1594
1685
 
1595
1686
  static js_value_t *
@@ -1698,6 +1789,45 @@ bare_fs_opendir (js_env_t *env, js_callback_info_t *info) {
1698
1789
  return NULL;
1699
1790
  }
1700
1791
 
1792
+ static js_value_t *
1793
+ bare_fs_opendir_sync (js_env_t *env, js_callback_info_t *info) {
1794
+ int err;
1795
+
1796
+ size_t argc = 2;
1797
+ js_value_t *argv[2];
1798
+
1799
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1800
+ assert(err == 0);
1801
+
1802
+ assert(argc == 2);
1803
+
1804
+ bare_fs_path_t path;
1805
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1806
+ assert(err == 0);
1807
+
1808
+ js_value_t *data = argv[1];
1809
+
1810
+ uv_loop_t *loop;
1811
+ js_get_env_loop(env, &loop);
1812
+
1813
+ uv_fs_t req;
1814
+ uv_fs_opendir(loop, &req, (char *) path, NULL);
1815
+
1816
+ if (req.result < 0) {
1817
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1818
+ } else {
1819
+ bare_fs_dir_t *dir;
1820
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dir, NULL, NULL, NULL);
1821
+ assert(err == 0);
1822
+
1823
+ dir->dir = req.ptr;
1824
+ }
1825
+
1826
+ uv_fs_req_cleanup(&req);
1827
+
1828
+ return NULL;
1829
+ }
1830
+
1701
1831
  static js_value_t *
1702
1832
  bare_fs_readdir (js_env_t *env, js_callback_info_t *info) {
1703
1833
  int err;
@@ -1737,6 +1867,77 @@ bare_fs_readdir (js_env_t *env, js_callback_info_t *info) {
1737
1867
  return NULL;
1738
1868
  }
1739
1869
 
1870
+ static js_value_t *
1871
+ bare_fs_readdir_sync (js_env_t *env, js_callback_info_t *info) {
1872
+ int err;
1873
+
1874
+ size_t argc = 3;
1875
+ js_value_t *argv[3];
1876
+
1877
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1878
+ assert(err == 0);
1879
+
1880
+ assert(argc == 3);
1881
+
1882
+ bare_fs_dir_t *dir;
1883
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &dir, NULL, NULL, NULL);
1884
+ assert(err == 0);
1885
+
1886
+ bare_fs_dirent_t *dirents;
1887
+ size_t dirents_len;
1888
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dirents, &dirents_len, NULL, NULL);
1889
+ assert(err == 0);
1890
+
1891
+ uv_loop_t *loop;
1892
+ js_get_env_loop(env, &loop);
1893
+
1894
+ dir->dir->dirents = dirents;
1895
+ dir->dir->nentries = dirents_len / sizeof(bare_fs_dirent_t);
1896
+
1897
+ uv_fs_t req;
1898
+ uv_fs_readdir(loop, &req, dir->dir, NULL);
1899
+
1900
+ if (req.result < 0) {
1901
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1902
+ } else {
1903
+ uv_dir_t *dir = req.ptr;
1904
+
1905
+ for (size_t i = 0, n = req.result; i < n; i++) {
1906
+ uv_dirent_t *dirent = &dir->dirents[i];
1907
+
1908
+ js_value_t *entry;
1909
+ err = js_create_object(env, &entry);
1910
+ assert(err == 0);
1911
+
1912
+ err = js_set_element(env, argv[2], i, entry);
1913
+ assert(err == 0);
1914
+
1915
+ size_t name_len = strlen(dirent->name);
1916
+
1917
+ js_value_t *name;
1918
+ void *data;
1919
+ err = js_create_arraybuffer(env, name_len, &data, &name);
1920
+ assert(err == 0);
1921
+
1922
+ memcpy(data, dirent->name, name_len);
1923
+
1924
+ err = js_set_named_property(env, entry, "name", name);
1925
+ assert(err == 0);
1926
+
1927
+ js_value_t *type;
1928
+ err = js_create_uint32(env, dirent->type, &type);
1929
+ assert(err == 0);
1930
+
1931
+ err = js_set_named_property(env, entry, "type", type);
1932
+ assert(err == 0);
1933
+ }
1934
+ }
1935
+
1936
+ uv_fs_req_cleanup(&req);
1937
+
1938
+ return NULL;
1939
+ }
1940
+
1740
1941
  static js_value_t *
1741
1942
  bare_fs_closedir (js_env_t *env, js_callback_info_t *info) {
1742
1943
  int err;
@@ -1765,6 +1966,37 @@ bare_fs_closedir (js_env_t *env, js_callback_info_t *info) {
1765
1966
  return NULL;
1766
1967
  }
1767
1968
 
1969
+ static js_value_t *
1970
+ bare_fs_closedir_sync (js_env_t *env, js_callback_info_t *info) {
1971
+ int err;
1972
+
1973
+ size_t argc = 1;
1974
+ js_value_t *argv[1];
1975
+
1976
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1977
+ assert(err == 0);
1978
+
1979
+ assert(argc == 1);
1980
+
1981
+ bare_fs_dir_t *dir;
1982
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &dir, NULL, NULL, NULL);
1983
+ assert(err == 0);
1984
+
1985
+ uv_loop_t *loop;
1986
+ js_get_env_loop(env, &loop);
1987
+
1988
+ uv_fs_t req;
1989
+ uv_fs_closedir(loop, &req, dir->dir, NULL);
1990
+
1991
+ if (req.result < 0) {
1992
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1993
+ }
1994
+
1995
+ uv_fs_req_cleanup(&req);
1996
+
1997
+ return NULL;
1998
+ }
1999
+
1768
2000
  static js_value_t *
1769
2001
  init (js_env_t *env, js_value_t *exports) {
1770
2002
  int err;
@@ -1840,13 +2072,18 @@ init (js_env_t *env, js_value_t *exports) {
1840
2072
  V("fstatSync", bare_fs_fstat_sync)
1841
2073
  V("unlink", bare_fs_unlink)
1842
2074
  V("unlinkSync", bare_fs_unlink_sync)
2075
+ V("realpath", bare_fs_realpath)
2076
+ V("realpathSync", bare_fs_realpath_sync)
1843
2077
  V("readlink", bare_fs_readlink)
1844
2078
  V("readlinkSync", bare_fs_readlink_sync)
1845
2079
  V("symlink", bare_fs_symlink)
1846
2080
  V("symlinkSync", bare_fs_symlink_sync)
1847
2081
  V("opendir", bare_fs_opendir)
2082
+ V("opendirSync", bare_fs_opendir_sync)
1848
2083
  V("readdir", bare_fs_readdir)
2084
+ V("readdirSync", bare_fs_readdir_sync)
1849
2085
  V("closedir", bare_fs_closedir)
2086
+ V("closedirSync", bare_fs_closedir_sync)
1850
2087
  #undef V
1851
2088
 
1852
2089
  #define V(name) \
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { sep, resolve, isAbsolute, toNamespacedPath } = require('path')
2
- const { Readable, Writable, getStreamError } = require('streamx')
2
+ const { Readable, Writable } = require('streamx')
3
3
  const binding = require('./binding')
4
4
 
5
5
  const isWindows = process.platform === 'win32'
@@ -55,10 +55,6 @@ binding.init(fs.handle, fs, onresponse)
55
55
 
56
56
  process.on('exit', () => binding.destroy(fs.handle))
57
57
 
58
- if (process.thread) {
59
- process.thread.on('exit', () => binding.destroy(fs.handle))
60
- }
61
-
62
58
  // Lightly-modified from the Node FS internal utils.
63
59
  function flagsToNumber (flags) {
64
60
  switch (flags) {
@@ -530,7 +526,7 @@ function fchmodSync (fd, mode) {
530
526
  binding.fchmodSync(fd, mode)
531
527
  }
532
528
 
533
- function mkdirp (path, mode, cb) {
529
+ function mkdirRecursive (path, mode, cb) {
534
530
  mkdir(path, { mode }, function (err) {
535
531
  if (err === null) return cb(null, 0, null)
536
532
 
@@ -547,7 +543,7 @@ function mkdirp (path, mode, cb) {
547
543
  const i = path.lastIndexOf(sep)
548
544
  if (i <= 0) return cb(err, err.errno, null)
549
545
 
550
- mkdirp(path.slice(0, i), mode, function (err) {
546
+ mkdirRecursive(path.slice(0, i), mode, function (err) {
551
547
  if (err) return cb(err, err.errno, null)
552
548
  mkdir(path, { mode }, cb)
553
549
  })
@@ -571,14 +567,14 @@ function mkdir (path, opts, cb) {
571
567
 
572
568
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
573
569
 
574
- if (opts.recursive) return mkdirp(path, mode, cb)
570
+ if (opts.recursive) return mkdirRecursive(path, mode, cb)
575
571
 
576
572
  const req = getReq()
577
573
  req.callback = cb
578
574
  binding.mkdir(req.handle, path, mode)
579
575
  }
580
576
 
581
- function mkdirpSync (path, mode) {
577
+ function mkdirResursiveSync (path, mode) {
582
578
  try {
583
579
  mkdirSync(path, { mode })
584
580
  } catch (err) {
@@ -590,7 +586,7 @@ function mkdirpSync (path, mode) {
590
586
  const i = path.lastIndexOf(sep)
591
587
  if (i <= 0) throw err
592
588
 
593
- mkdirpSync(path.slice(0, i), { mode })
589
+ mkdirResursiveSync(path.slice(0, i), { mode })
594
590
  mkdirSync(path, { mode })
595
591
  }
596
592
  }
@@ -605,7 +601,7 @@ function mkdirSync (path, opts) {
605
601
 
606
602
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
607
603
 
608
- if (opts.recursive) return mkdirpSync(path, mode)
604
+ if (opts.recursive) return mkdirResursiveSync(path, mode)
609
605
 
610
606
  binding.mkdirSync(path, mode)
611
607
  }
@@ -632,6 +628,107 @@ function rmdirSync (path) {
632
628
  binding.rmdirSync(path)
633
629
  }
634
630
 
631
+ function rmRecursive (path, opts, cb) {
632
+ rmdir(path, function (err) {
633
+ if (err === null) return cb(null)
634
+
635
+ if (err.code !== 'ENOTEMPTY') return cb(err)
636
+
637
+ readdir(path, function (err, files) {
638
+ if (err) return cb(err)
639
+
640
+ if (files.length === 0) return rmdir(path, cb)
641
+
642
+ let missing = files.length
643
+ let done = false
644
+
645
+ for (const file of files) {
646
+ rm(path + sep + file, opts, function (err) {
647
+ if (done) return
648
+
649
+ if (err) {
650
+ done = true
651
+ return cb(err)
652
+ }
653
+
654
+ if (--missing === 0) rmdir(path, cb)
655
+ })
656
+ }
657
+ })
658
+ })
659
+ }
660
+
661
+ function rm (path, opts, cb) {
662
+ if (typeof path !== 'string') {
663
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
664
+ }
665
+
666
+ if (typeof opts === 'function') {
667
+ cb = opts
668
+ opts = {}
669
+ } else if (typeof cb !== 'function') {
670
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
671
+ }
672
+
673
+ if (!opts) opts = {}
674
+
675
+ lstat(path, function (err, st) {
676
+ if (err) {
677
+ return cb(err.code === 'ENOENT' && opts.force ? null : err)
678
+ }
679
+
680
+ if (st.isDirectory()) {
681
+ if (opts.recursive) return rmRecursive(path, opts, cb)
682
+
683
+ const err = new Error('is a directory')
684
+ err.code = 'EISDIR'
685
+ return cb(err)
686
+ }
687
+
688
+ unlink(path, cb)
689
+ })
690
+ }
691
+
692
+ function rmRecursiveSync (path, opts) {
693
+ try {
694
+ rmdirSync(path)
695
+ } catch (err) {
696
+ if (err.code !== 'ENOTEMPTY') throw err
697
+
698
+ const files = readdirSync(path)
699
+
700
+ for (const file of files) {
701
+ rmSync(path + sep + file, opts)
702
+ }
703
+
704
+ rmdirSync(path)
705
+ }
706
+ }
707
+
708
+ function rmSync (path, opts) {
709
+ if (typeof path !== 'string') {
710
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
711
+ }
712
+
713
+ if (!opts) opts = {}
714
+
715
+ try {
716
+ const st = lstatSync(path)
717
+
718
+ if (st.isDirectory()) {
719
+ if (opts.recursive) return rmRecursiveSync(path, opts)
720
+
721
+ const err = new Error('is a directory')
722
+ err.code = 'EISDIR'
723
+ throw err
724
+ }
725
+
726
+ unlinkSync(path)
727
+ } catch (err) {
728
+ if (err.code !== 'ENOENT' || !opts.force) throw err
729
+ }
730
+ }
731
+
635
732
  function unlink (path, cb) {
636
733
  if (typeof path !== 'string') {
637
734
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -684,6 +781,60 @@ function renameSync (src, dst) {
684
781
  binding.renameSync(src, dst)
685
782
  }
686
783
 
784
+ function realpath (path, opts, cb) {
785
+ if (typeof path !== 'string') {
786
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
787
+ }
788
+
789
+ if (typeof opts === 'function') {
790
+ cb = opts
791
+ opts = {}
792
+ } else if (typeof cb !== 'function') {
793
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
794
+ }
795
+
796
+ if (typeof opts === 'string') opts = { encoding: opts }
797
+ else if (!opts) opts = {}
798
+
799
+ const {
800
+ encoding = 'utf8'
801
+ } = opts
802
+
803
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
804
+
805
+ const req = getReq()
806
+
807
+ req.callback = function (err, _) {
808
+ if (err) return cb(err, null)
809
+ let path = data.subarray(0, data.indexOf(0))
810
+ if (encoding !== 'buffer') path = path.toString(encoding)
811
+ cb(null, path)
812
+ }
813
+
814
+ binding.realpath(req.handle, path, data)
815
+ }
816
+
817
+ function realpathSync (path, opts) {
818
+ if (typeof path !== 'string') {
819
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
820
+ }
821
+
822
+ if (typeof opts === 'string') opts = { encoding: opts }
823
+ else if (!opts) opts = {}
824
+
825
+ const {
826
+ encoding = 'utf8'
827
+ } = opts
828
+
829
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
830
+
831
+ binding.realpathSync(path, data)
832
+
833
+ path = data.subarray(0, data.indexOf(0))
834
+ if (encoding !== 'buffer') path = path.toString(encoding)
835
+ return path
836
+ }
837
+
687
838
  function readlink (path, opts, cb) {
688
839
  if (typeof path !== 'string') {
689
840
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -864,6 +1015,19 @@ function opendir (path, opts, cb) {
864
1015
  binding.opendir(req.handle, path, data)
865
1016
  }
866
1017
 
1018
+ function opendirSync (path, opts) {
1019
+ if (typeof path !== 'string') {
1020
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1021
+ }
1022
+
1023
+ if (typeof opts === 'string') opts = { encoding: opts }
1024
+ else if (!opts) opts = {}
1025
+
1026
+ const data = Buffer.allocUnsafe(binding.sizeofFSDir)
1027
+ binding.opendirSync(path, data)
1028
+ return new Dir(path, data, opts)
1029
+ }
1030
+
867
1031
  function readdir (path, opts, cb) {
868
1032
  if (typeof path !== 'string') {
869
1033
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -877,7 +1041,7 @@ function readdir (path, opts, cb) {
877
1041
  }
878
1042
 
879
1043
  if (typeof opts === 'string') opts = { encoding: opts }
880
- if (!opts) opts = {}
1044
+ else if (!opts) opts = {}
881
1045
 
882
1046
  const {
883
1047
  withFileTypes = false
@@ -886,13 +1050,37 @@ function readdir (path, opts, cb) {
886
1050
  opendir(path, opts, async (err, dir) => {
887
1051
  if (err) return cb(err, null)
888
1052
  const result = []
889
- dir
890
- .on('data', (entry) => result.push(withFileTypes ? entry : entry.name))
891
- .on('error', (err) => cb(err, null))
892
- .on('end', () => cb(null, result))
1053
+ for await (const entry of dir) {
1054
+ result.push(withFileTypes ? entry : entry.name)
1055
+ }
1056
+ cb(null, result)
893
1057
  })
894
1058
  }
895
1059
 
1060
+ function readdirSync (path, opts) {
1061
+ if (typeof path !== 'string') {
1062
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1063
+ }
1064
+
1065
+ if (typeof opts === 'string') opts = { encoding: opts }
1066
+ else if (!opts) opts = {}
1067
+
1068
+ const {
1069
+ withFileTypes = false
1070
+ } = opts
1071
+
1072
+ const dir = opendirSync(path, opts)
1073
+ const result = []
1074
+
1075
+ while (true) {
1076
+ const entry = dir.readSync()
1077
+ if (entry === null) break
1078
+ result.push(withFileTypes ? entry : entry.name)
1079
+ }
1080
+
1081
+ return result
1082
+ }
1083
+
896
1084
  function readFile (path, opts, cb) {
897
1085
  if (typeof path !== 'string') {
898
1086
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -1215,96 +1403,158 @@ class FileReadStream extends Readable {
1215
1403
  }
1216
1404
  }
1217
1405
 
1218
- class Dir extends Readable {
1406
+ class Dir {
1219
1407
  constructor (path, handle, opts = {}) {
1220
1408
  const {
1221
1409
  encoding = 'utf8',
1222
1410
  bufferSize = 32
1223
1411
  } = opts
1224
1412
 
1225
- super()
1226
-
1227
1413
  this._handle = handle
1228
1414
  this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
1229
1415
  this._encoding = encoding
1416
+ this._buffer = []
1417
+ this._ended = false
1230
1418
 
1231
1419
  this.path = path
1232
1420
  }
1233
1421
 
1234
- _read (cb) {
1235
- const self = this
1236
- const data = []
1422
+ read (cb) {
1423
+ if (!cb) return promisify(this.read.bind(this))
1237
1424
 
1425
+ if (this._buffer.length) return queueMicrotask(() => cb(null, this._buffer.shift()))
1426
+ if (this._ended) return queueMicrotask(() => cb(null, null))
1427
+
1428
+ const data = []
1238
1429
  const req = getReq()
1239
1430
 
1240
- req.callback = function (err, _) {
1241
- if (err) return cb(err)
1242
- if (data.length === 0) self.push(null)
1431
+ req.callback = (err, _) => {
1432
+ if (err) return cb(err, null)
1433
+ if (data.length === 0) this._ended = true
1243
1434
  else {
1244
1435
  for (const entry of data) {
1245
1436
  let name = Buffer.from(entry.name)
1246
- if (self._encoding !== 'buffer') name = name.toString(self._encoding)
1247
- self.push(new Dirent(self.path, name, entry.type))
1437
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1438
+ this._buffer.push(new Dirent(this.path, name, entry.type))
1248
1439
  }
1249
1440
  }
1250
- cb(null)
1441
+
1442
+ if (this._ended) return cb(null, null)
1443
+ cb(null, this._buffer.shift())
1251
1444
  }
1252
1445
 
1253
1446
  binding.readdir(req.handle, this._handle, this._dirents, data)
1254
1447
  }
1255
1448
 
1256
- _destroy (cb) {
1449
+ readSync () {
1450
+ if (this._buffer.length) return this._buffer.shift()
1451
+ if (this._ended) return null
1452
+
1453
+ const data = []
1454
+
1455
+ binding.readdirSync(this._handle, this._dirents, data)
1456
+
1457
+ if (data.length === 0) this._ended = true
1458
+ else {
1459
+ for (const entry of data) {
1460
+ let name = Buffer.from(entry.name)
1461
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1462
+ this._buffer.push(new Dirent(this.path, name, entry.type))
1463
+ }
1464
+ }
1465
+
1466
+ if (this._ended) return null
1467
+ return this._buffer.shift()
1468
+ }
1469
+
1470
+ close (cb) {
1471
+ if (!cb) return promisify(this.close.bind(this))
1472
+
1257
1473
  const req = getReq()
1258
1474
 
1259
- req.callback = function (err, _) {
1475
+ req.callback = (err, _) => {
1476
+ this._handle = null
1260
1477
  cb(err)
1261
1478
  }
1262
1479
 
1263
1480
  binding.closedir(req.handle, this._handle)
1264
1481
  }
1265
1482
 
1266
- close (cb = noop) {
1267
- if (this.destroyed) return cb(null)
1268
- this
1269
- .once('close', () => cb(getStreamError(this)))
1270
- .destroy()
1483
+ closeSync () {
1484
+ binding.closedirSync(this._handle)
1485
+ this._handle = null
1486
+ }
1487
+
1488
+ [Symbol.iterator] () {
1489
+ return {
1490
+ next: () => {
1491
+ if (this._buffer.length) {
1492
+ return { done: false, value: this._buffer.shift() }
1493
+ }
1494
+
1495
+ if (this._ended) {
1496
+ this.closeSync()
1497
+
1498
+ return { done: true }
1499
+ }
1500
+
1501
+ const entry = this.readSync()
1502
+
1503
+ return { done: entry === null, value: entry }
1504
+ }
1505
+ }
1506
+ }
1507
+
1508
+ [Symbol.asyncIterator] () {
1509
+ return {
1510
+ next: () => new Promise((resolve, reject) => {
1511
+ if (this._buffer.length) {
1512
+ return resolve({ done: false, value: this._buffer.shift() })
1513
+ }
1514
+
1515
+ if (this._ended) {
1516
+ return this.close((err) => err ? reject(err) : resolve({ done: true }))
1517
+ }
1518
+
1519
+ this.read((err, entry) => err ? reject(err) : resolve({ done: entry === null, value: entry }))
1520
+ })
1521
+ }
1271
1522
  }
1272
1523
  }
1273
1524
 
1274
1525
  class Dirent {
1275
1526
  constructor (path, name, type) {
1276
- this._type = type
1277
-
1527
+ this.type = type
1278
1528
  this.path = path
1279
1529
  this.name = name
1280
1530
  }
1281
1531
 
1282
1532
  isFile () {
1283
- return this._type === constants.UV_DIRENT_FILE
1533
+ return this.type === constants.UV_DIRENT_FILE
1284
1534
  }
1285
1535
 
1286
1536
  isDirectory () {
1287
- return this._type === constants.UV_DIRENT_DIR
1537
+ return this.type === constants.UV_DIRENT_DIR
1288
1538
  }
1289
1539
 
1290
1540
  isSymbolicLink () {
1291
- return this._type === constants.UV_DIRENT_LINK
1541
+ return this.type === constants.UV_DIRENT_LINK
1292
1542
  }
1293
1543
 
1294
1544
  isFIFO () {
1295
- return this._type === constants.UV_DIRENT_FIFO
1545
+ return this.type === constants.UV_DIRENT_FIFO
1296
1546
  }
1297
1547
 
1298
1548
  isSocket () {
1299
- return this._type === constants.UV_DIRENT_SOCKET
1549
+ return this.type === constants.UV_DIRENT_SOCKET
1300
1550
  }
1301
1551
 
1302
1552
  isCharacterDevice () {
1303
- return this._type === constants.UV_DIRENT_CHAR
1553
+ return this.type === constants.UV_DIRENT_CHAR
1304
1554
  }
1305
1555
 
1306
1556
  isBlockDevice () {
1307
- return this._type === constants.UV_DIRENT_BLOCK
1557
+ return this.type === constants.UV_DIRENT_BLOCK
1308
1558
  }
1309
1559
  }
1310
1560
 
@@ -1332,7 +1582,9 @@ exports.readFile = readFile
1332
1582
  exports.readdir = readdir
1333
1583
  exports.readlink = readlink
1334
1584
  exports.readv = readv
1585
+ exports.realpath = realpath
1335
1586
  exports.rename = rename
1587
+ exports.rm = rm
1336
1588
  exports.rmdir = rmdir
1337
1589
  exports.stat = stat
1338
1590
  exports.symlink = symlink
@@ -1348,10 +1600,14 @@ exports.fstatSync = fstatSync
1348
1600
  exports.lstatSync = lstatSync
1349
1601
  exports.mkdirSync = mkdirSync
1350
1602
  exports.openSync = openSync
1603
+ exports.opendirSync = opendirSync
1351
1604
  exports.readFileSync = readFileSync
1352
1605
  exports.readSync = readSync
1606
+ exports.readdirSync = readdirSync
1353
1607
  exports.readlinkSync = readlinkSync
1608
+ exports.realpathSync = realpathSync
1354
1609
  exports.renameSync = renameSync
1610
+ exports.rmSync = rmSync
1355
1611
  exports.rmdirSync = rmdirSync
1356
1612
  exports.statSync = statSync
1357
1613
  exports.symlinkSync = symlinkSync
@@ -1366,7 +1622,9 @@ exports.promises.opendir = promisify(opendir)
1366
1622
  exports.promises.readFile = promisify(readFile)
1367
1623
  exports.promises.readdir = promisify(readdir)
1368
1624
  exports.promises.readlink = promisify(readlink)
1625
+ exports.promises.realpath = promisify(realpath)
1369
1626
  exports.promises.rename = promisify(rename)
1627
+ exports.promises.rm = promisify(rm)
1370
1628
  exports.promises.rmdir = promisify(rmdir)
1371
1629
  exports.promises.stat = promisify(stat)
1372
1630
  exports.promises.symlink = promisify(symlink)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "1.9.1",
3
+ "version": "1.11.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [