bare-fs 1.10.0 → 1.12.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 +8 -0
  2. package/binding.c +322 -3
  3. package/index.js +404 -105
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -13,6 +13,7 @@ const fs = require('bare-fs')
13
13
 
14
14
  // Currently supports:
15
15
 
16
+ fs.access
16
17
  fs.chmod
17
18
  fs.close
18
19
  fs.chmod
@@ -26,6 +27,7 @@ fs.read
26
27
  fs.readdir
27
28
  fs.readlink
28
29
  fs.readv
30
+ fs.realpath
29
31
  fs.rename
30
32
  fs.rmdir
31
33
  fs.stat
@@ -37,6 +39,7 @@ fs.writev
37
39
  fs.readFile
38
40
  fs.writeFile
39
41
 
42
+ fs.promises.access
40
43
  fs.promises.chmod
41
44
  fs.promises.lstat
42
45
  fs.promises.mkdir
@@ -44,6 +47,7 @@ fs.promises.opendir
44
47
  fs.promises.readFile
45
48
  fs.promises.readdir
46
49
  fs.promises.readlink
50
+ fs.promises.realpath
47
51
  fs.promises.rename
48
52
  fs.promises.rmdir
49
53
  fs.promises.stat
@@ -54,6 +58,7 @@ fs.promises.writeFile
54
58
  fs.createReadStream
55
59
  fs.createWriteStream
56
60
 
61
+ fs.accessSync
57
62
  fs.chmodSync
58
63
  fs.closeSync
59
64
  fs.fchmodSync
@@ -61,8 +66,11 @@ fs.fstatSync
61
66
  fs.lstatSync
62
67
  fs.mkdirSync
63
68
  fs.openSync
69
+ fs.opendirSync
64
70
  fs.readSync
71
+ fs.readdirSync
65
72
  fs.readlinkSync
73
+ fs.realpathSync
66
74
  fs.renameSync
67
75
  fs.rmdirSync
68
76
  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;
@@ -467,6 +490,73 @@ bare_fs_close_sync (js_env_t *env, js_callback_info_t *info) {
467
490
  return NULL;
468
491
  }
469
492
 
493
+ static js_value_t *
494
+ bare_fs_access (js_env_t *env, js_callback_info_t *info) {
495
+ int err;
496
+
497
+ size_t argc = 3;
498
+ js_value_t *argv[3];
499
+
500
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
501
+ assert(err == 0);
502
+
503
+ assert(argc == 3);
504
+
505
+ bare_fs_req_t *req;
506
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
507
+ assert(err == 0);
508
+
509
+ bare_fs_path_t path;
510
+ err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
511
+ assert(err == 0);
512
+
513
+ int32_t mode;
514
+ err = js_get_value_int32(env, argv[2], &mode);
515
+ assert(err == 0);
516
+
517
+ uv_loop_t *loop;
518
+ js_get_env_loop(env, &loop);
519
+
520
+ uv_fs_access(loop, (uv_fs_t *) req, (char *) path, mode, on_fs_response);
521
+
522
+ return NULL;
523
+ }
524
+
525
+ static js_value_t *
526
+ bare_fs_access_sync (js_env_t *env, js_callback_info_t *info) {
527
+ int err;
528
+
529
+ size_t argc = 2;
530
+ js_value_t *argv[2];
531
+
532
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
533
+ assert(err == 0);
534
+
535
+ assert(argc == 2);
536
+
537
+ bare_fs_path_t path;
538
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
539
+ assert(err == 0);
540
+
541
+ int32_t mode;
542
+ err = js_get_value_int32(env, argv[1], &mode);
543
+ assert(err == 0);
544
+
545
+ uv_loop_t *loop;
546
+ js_get_env_loop(env, &loop);
547
+
548
+ uv_fs_t req;
549
+ uv_fs_access(loop, &req, (char *) path, mode, NULL);
550
+
551
+ if (req.result < 0) {
552
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
553
+ }
554
+
555
+ uv_fs_req_cleanup(&req);
556
+
557
+ return NULL;
558
+ }
559
+
470
560
  static js_value_t *
471
561
  bare_fs_read (js_env_t *env, js_callback_info_t *info) {
472
562
  int err;
@@ -1522,6 +1612,76 @@ bare_fs_unlink_sync (js_env_t *env, js_callback_info_t *info) {
1522
1612
  return NULL;
1523
1613
  }
1524
1614
 
1615
+ static js_value_t *
1616
+ bare_fs_realpath (js_env_t *env, js_callback_info_t *info) {
1617
+ int err;
1618
+
1619
+ size_t argc = 3;
1620
+ js_value_t *argv[3];
1621
+
1622
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1623
+ assert(err == 0);
1624
+
1625
+ assert(argc == 3);
1626
+
1627
+ bare_fs_req_t *req;
1628
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
1629
+ assert(err == 0);
1630
+
1631
+ bare_fs_path_t path;
1632
+ err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
1633
+ assert(err == 0);
1634
+
1635
+ err = js_create_reference(env, argv[2], 1, &req->data);
1636
+ assert(err == 0);
1637
+
1638
+ uv_loop_t *loop;
1639
+ js_get_env_loop(env, &loop);
1640
+
1641
+ uv_fs_realpath(loop, (uv_fs_t *) req, (char *) path, on_fs_realpath_response);
1642
+
1643
+ return NULL;
1644
+ }
1645
+
1646
+ static js_value_t *
1647
+ bare_fs_realpath_sync (js_env_t *env, js_callback_info_t *info) {
1648
+ int err;
1649
+
1650
+ size_t argc = 2;
1651
+ js_value_t *argv[2];
1652
+
1653
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1654
+ assert(err == 0);
1655
+
1656
+ assert(argc == 2);
1657
+
1658
+ bare_fs_path_t path;
1659
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1660
+ assert(err == 0);
1661
+
1662
+ uv_loop_t *loop;
1663
+ js_get_env_loop(env, &loop);
1664
+
1665
+ uv_fs_t req;
1666
+ uv_fs_realpath(loop, &req, (char *) path, NULL);
1667
+
1668
+ js_value_t *res = NULL;
1669
+
1670
+ if (req.result < 0) {
1671
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1672
+ } else {
1673
+ char *path;
1674
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &path, NULL, NULL, NULL);
1675
+ assert(err == 0);
1676
+
1677
+ strncpy(path, req.ptr, sizeof(bare_fs_path_t));
1678
+ }
1679
+
1680
+ uv_fs_req_cleanup(&req);
1681
+
1682
+ return res;
1683
+ }
1684
+
1525
1685
  static js_value_t *
1526
1686
  bare_fs_readlink (js_env_t *env, js_callback_info_t *info) {
1527
1687
  int err;
@@ -1575,8 +1735,6 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1575
1735
  uv_fs_t req;
1576
1736
  uv_fs_readlink(loop, &req, (char *) path, NULL);
1577
1737
 
1578
- js_value_t *res = NULL;
1579
-
1580
1738
  if (req.result < 0) {
1581
1739
  js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1582
1740
  } else {
@@ -1589,7 +1747,7 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1589
1747
 
1590
1748
  uv_fs_req_cleanup(&req);
1591
1749
 
1592
- return res;
1750
+ return NULL;
1593
1751
  }
1594
1752
 
1595
1753
  static js_value_t *
@@ -1698,6 +1856,45 @@ bare_fs_opendir (js_env_t *env, js_callback_info_t *info) {
1698
1856
  return NULL;
1699
1857
  }
1700
1858
 
1859
+ static js_value_t *
1860
+ bare_fs_opendir_sync (js_env_t *env, js_callback_info_t *info) {
1861
+ int err;
1862
+
1863
+ size_t argc = 2;
1864
+ js_value_t *argv[2];
1865
+
1866
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1867
+ assert(err == 0);
1868
+
1869
+ assert(argc == 2);
1870
+
1871
+ bare_fs_path_t path;
1872
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1873
+ assert(err == 0);
1874
+
1875
+ js_value_t *data = argv[1];
1876
+
1877
+ uv_loop_t *loop;
1878
+ js_get_env_loop(env, &loop);
1879
+
1880
+ uv_fs_t req;
1881
+ uv_fs_opendir(loop, &req, (char *) path, NULL);
1882
+
1883
+ if (req.result < 0) {
1884
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1885
+ } else {
1886
+ bare_fs_dir_t *dir;
1887
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dir, NULL, NULL, NULL);
1888
+ assert(err == 0);
1889
+
1890
+ dir->dir = req.ptr;
1891
+ }
1892
+
1893
+ uv_fs_req_cleanup(&req);
1894
+
1895
+ return NULL;
1896
+ }
1897
+
1701
1898
  static js_value_t *
1702
1899
  bare_fs_readdir (js_env_t *env, js_callback_info_t *info) {
1703
1900
  int err;
@@ -1737,6 +1934,77 @@ bare_fs_readdir (js_env_t *env, js_callback_info_t *info) {
1737
1934
  return NULL;
1738
1935
  }
1739
1936
 
1937
+ static js_value_t *
1938
+ bare_fs_readdir_sync (js_env_t *env, js_callback_info_t *info) {
1939
+ int err;
1940
+
1941
+ size_t argc = 3;
1942
+ js_value_t *argv[3];
1943
+
1944
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1945
+ assert(err == 0);
1946
+
1947
+ assert(argc == 3);
1948
+
1949
+ bare_fs_dir_t *dir;
1950
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &dir, NULL, NULL, NULL);
1951
+ assert(err == 0);
1952
+
1953
+ bare_fs_dirent_t *dirents;
1954
+ size_t dirents_len;
1955
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &dirents, &dirents_len, NULL, NULL);
1956
+ assert(err == 0);
1957
+
1958
+ uv_loop_t *loop;
1959
+ js_get_env_loop(env, &loop);
1960
+
1961
+ dir->dir->dirents = dirents;
1962
+ dir->dir->nentries = dirents_len / sizeof(bare_fs_dirent_t);
1963
+
1964
+ uv_fs_t req;
1965
+ uv_fs_readdir(loop, &req, dir->dir, NULL);
1966
+
1967
+ if (req.result < 0) {
1968
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1969
+ } else {
1970
+ uv_dir_t *dir = req.ptr;
1971
+
1972
+ for (size_t i = 0, n = req.result; i < n; i++) {
1973
+ uv_dirent_t *dirent = &dir->dirents[i];
1974
+
1975
+ js_value_t *entry;
1976
+ err = js_create_object(env, &entry);
1977
+ assert(err == 0);
1978
+
1979
+ err = js_set_element(env, argv[2], i, entry);
1980
+ assert(err == 0);
1981
+
1982
+ size_t name_len = strlen(dirent->name);
1983
+
1984
+ js_value_t *name;
1985
+ void *data;
1986
+ err = js_create_arraybuffer(env, name_len, &data, &name);
1987
+ assert(err == 0);
1988
+
1989
+ memcpy(data, dirent->name, name_len);
1990
+
1991
+ err = js_set_named_property(env, entry, "name", name);
1992
+ assert(err == 0);
1993
+
1994
+ js_value_t *type;
1995
+ err = js_create_uint32(env, dirent->type, &type);
1996
+ assert(err == 0);
1997
+
1998
+ err = js_set_named_property(env, entry, "type", type);
1999
+ assert(err == 0);
2000
+ }
2001
+ }
2002
+
2003
+ uv_fs_req_cleanup(&req);
2004
+
2005
+ return NULL;
2006
+ }
2007
+
1740
2008
  static js_value_t *
1741
2009
  bare_fs_closedir (js_env_t *env, js_callback_info_t *info) {
1742
2010
  int err;
@@ -1765,6 +2033,37 @@ bare_fs_closedir (js_env_t *env, js_callback_info_t *info) {
1765
2033
  return NULL;
1766
2034
  }
1767
2035
 
2036
+ static js_value_t *
2037
+ bare_fs_closedir_sync (js_env_t *env, js_callback_info_t *info) {
2038
+ int err;
2039
+
2040
+ size_t argc = 1;
2041
+ js_value_t *argv[1];
2042
+
2043
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
2044
+ assert(err == 0);
2045
+
2046
+ assert(argc == 1);
2047
+
2048
+ bare_fs_dir_t *dir;
2049
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &dir, NULL, NULL, NULL);
2050
+ assert(err == 0);
2051
+
2052
+ uv_loop_t *loop;
2053
+ js_get_env_loop(env, &loop);
2054
+
2055
+ uv_fs_t req;
2056
+ uv_fs_closedir(loop, &req, dir->dir, NULL);
2057
+
2058
+ if (req.result < 0) {
2059
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
2060
+ }
2061
+
2062
+ uv_fs_req_cleanup(&req);
2063
+
2064
+ return NULL;
2065
+ }
2066
+
1768
2067
  static js_value_t *
1769
2068
  init (js_env_t *env, js_value_t *exports) {
1770
2069
  int err;
@@ -1815,6 +2114,8 @@ init (js_env_t *env, js_value_t *exports) {
1815
2114
  V("openSync", bare_fs_open_sync)
1816
2115
  V("close", bare_fs_close)
1817
2116
  V("closeSync", bare_fs_close_sync)
2117
+ V("access", bare_fs_access)
2118
+ V("accessSync", bare_fs_access_sync)
1818
2119
  V("read", bare_fs_read)
1819
2120
  V("readSync", bare_fs_read_sync)
1820
2121
  V("readv", bare_fs_readv)
@@ -1840,13 +2141,18 @@ init (js_env_t *env, js_value_t *exports) {
1840
2141
  V("fstatSync", bare_fs_fstat_sync)
1841
2142
  V("unlink", bare_fs_unlink)
1842
2143
  V("unlinkSync", bare_fs_unlink_sync)
2144
+ V("realpath", bare_fs_realpath)
2145
+ V("realpathSync", bare_fs_realpath_sync)
1843
2146
  V("readlink", bare_fs_readlink)
1844
2147
  V("readlinkSync", bare_fs_readlink_sync)
1845
2148
  V("symlink", bare_fs_symlink)
1846
2149
  V("symlinkSync", bare_fs_symlink_sync)
1847
2150
  V("opendir", bare_fs_opendir)
2151
+ V("opendirSync", bare_fs_opendir_sync)
1848
2152
  V("readdir", bare_fs_readdir)
2153
+ V("readdirSync", bare_fs_readdir_sync)
1849
2154
  V("closedir", bare_fs_closedir)
2155
+ V("closedirSync", bare_fs_closedir_sync)
1850
2156
  #undef V
1851
2157
 
1852
2158
  #define V(name) \
@@ -1865,6 +2171,19 @@ init (js_env_t *env, js_value_t *exports) {
1865
2171
  V(O_TRUNC)
1866
2172
  V(O_APPEND)
1867
2173
 
2174
+ #ifdef F_OK
2175
+ V(F_OK)
2176
+ #endif
2177
+ #ifdef R_OK
2178
+ V(R_OK)
2179
+ #endif
2180
+ #ifdef W_OK
2181
+ V(W_OK)
2182
+ #endif
2183
+ #ifdef X_OK
2184
+ V(X_OK)
2185
+ #endif
2186
+
1868
2187
  V(S_IFMT)
1869
2188
  V(S_IFREG)
1870
2189
  V(S_IFDIR)
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'
@@ -12,6 +12,11 @@ const constants = exports.constants = {
12
12
  O_TRUNC: binding.O_TRUNC,
13
13
  O_APPEND: binding.O_APPEND,
14
14
 
15
+ F_OK: binding.F_OK || 0,
16
+ R_OK: binding.R_OK || 0,
17
+ W_OK: binding.W_OK || 0,
18
+ X_OK: binding.X_OK || 0,
19
+
15
20
  S_IFMT: binding.S_IFMT,
16
21
  S_IFREG: binding.S_IFREG,
17
22
  S_IFDIR: binding.S_IFDIR,
@@ -204,6 +209,33 @@ function closeSync (fd) {
204
209
  return binding.closeSync(fd)
205
210
  }
206
211
 
212
+ function access (path, mode, cb) {
213
+ if (typeof path !== 'string') {
214
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
215
+ }
216
+
217
+ if (typeof cb !== 'function') {
218
+ if (typeof mode === 'function') {
219
+ cb = mode
220
+ mode = constants.F_OK
221
+ } else {
222
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
223
+ }
224
+ }
225
+
226
+ const req = getReq()
227
+ req.callback = cb
228
+ binding.access(req.handle, path, mode)
229
+ }
230
+
231
+ function accessSync (path, mode = constants.F_OK) {
232
+ if (typeof path !== 'string') {
233
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
234
+ }
235
+
236
+ binding.accessSync(path, mode)
237
+ }
238
+
207
239
  function read (fd, buffer, offset, len, pos, cb) {
208
240
  if (typeof fd !== 'number') {
209
241
  throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
@@ -526,7 +558,7 @@ function fchmodSync (fd, mode) {
526
558
  binding.fchmodSync(fd, mode)
527
559
  }
528
560
 
529
- function mkdirp (path, mode, cb) {
561
+ function mkdirRecursive (path, mode, cb) {
530
562
  mkdir(path, { mode }, function (err) {
531
563
  if (err === null) return cb(null, 0, null)
532
564
 
@@ -543,7 +575,7 @@ function mkdirp (path, mode, cb) {
543
575
  const i = path.lastIndexOf(sep)
544
576
  if (i <= 0) return cb(err, err.errno, null)
545
577
 
546
- mkdirp(path.slice(0, i), mode, function (err) {
578
+ mkdirRecursive(path.slice(0, i), mode, function (err) {
547
579
  if (err) return cb(err, err.errno, null)
548
580
  mkdir(path, { mode }, cb)
549
581
  })
@@ -567,14 +599,14 @@ function mkdir (path, opts, cb) {
567
599
 
568
600
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
569
601
 
570
- if (opts.recursive) return mkdirp(path, mode, cb)
602
+ if (opts.recursive) return mkdirRecursive(path, mode, cb)
571
603
 
572
604
  const req = getReq()
573
605
  req.callback = cb
574
606
  binding.mkdir(req.handle, path, mode)
575
607
  }
576
608
 
577
- function mkdirpSync (path, mode) {
609
+ function mkdirResursiveSync (path, mode) {
578
610
  try {
579
611
  mkdirSync(path, { mode })
580
612
  } catch (err) {
@@ -586,7 +618,7 @@ function mkdirpSync (path, mode) {
586
618
  const i = path.lastIndexOf(sep)
587
619
  if (i <= 0) throw err
588
620
 
589
- mkdirpSync(path.slice(0, i), { mode })
621
+ mkdirResursiveSync(path.slice(0, i), { mode })
590
622
  mkdirSync(path, { mode })
591
623
  }
592
624
  }
@@ -601,7 +633,7 @@ function mkdirSync (path, opts) {
601
633
 
602
634
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
603
635
 
604
- if (opts.recursive) return mkdirpSync(path, mode)
636
+ if (opts.recursive) return mkdirResursiveSync(path, mode)
605
637
 
606
638
  binding.mkdirSync(path, mode)
607
639
  }
@@ -628,6 +660,107 @@ function rmdirSync (path) {
628
660
  binding.rmdirSync(path)
629
661
  }
630
662
 
663
+ function rmRecursive (path, opts, cb) {
664
+ rmdir(path, function (err) {
665
+ if (err === null) return cb(null)
666
+
667
+ if (err.code !== 'ENOTEMPTY') return cb(err)
668
+
669
+ readdir(path, function (err, files) {
670
+ if (err) return cb(err)
671
+
672
+ if (files.length === 0) return rmdir(path, cb)
673
+
674
+ let missing = files.length
675
+ let done = false
676
+
677
+ for (const file of files) {
678
+ rm(path + sep + file, opts, function (err) {
679
+ if (done) return
680
+
681
+ if (err) {
682
+ done = true
683
+ return cb(err)
684
+ }
685
+
686
+ if (--missing === 0) rmdir(path, cb)
687
+ })
688
+ }
689
+ })
690
+ })
691
+ }
692
+
693
+ function rm (path, opts, cb) {
694
+ if (typeof path !== 'string') {
695
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
696
+ }
697
+
698
+ if (typeof opts === 'function') {
699
+ cb = opts
700
+ opts = {}
701
+ } else if (typeof cb !== 'function') {
702
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
703
+ }
704
+
705
+ if (!opts) opts = {}
706
+
707
+ lstat(path, function (err, st) {
708
+ if (err) {
709
+ return cb(err.code === 'ENOENT' && opts.force ? null : err)
710
+ }
711
+
712
+ if (st.isDirectory()) {
713
+ if (opts.recursive) return rmRecursive(path, opts, cb)
714
+
715
+ const err = new Error('is a directory')
716
+ err.code = 'EISDIR'
717
+ return cb(err)
718
+ }
719
+
720
+ unlink(path, cb)
721
+ })
722
+ }
723
+
724
+ function rmRecursiveSync (path, opts) {
725
+ try {
726
+ rmdirSync(path)
727
+ } catch (err) {
728
+ if (err.code !== 'ENOTEMPTY') throw err
729
+
730
+ const files = readdirSync(path)
731
+
732
+ for (const file of files) {
733
+ rmSync(path + sep + file, opts)
734
+ }
735
+
736
+ rmdirSync(path)
737
+ }
738
+ }
739
+
740
+ function rmSync (path, opts) {
741
+ if (typeof path !== 'string') {
742
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
743
+ }
744
+
745
+ if (!opts) opts = {}
746
+
747
+ try {
748
+ const st = lstatSync(path)
749
+
750
+ if (st.isDirectory()) {
751
+ if (opts.recursive) return rmRecursiveSync(path, opts)
752
+
753
+ const err = new Error('is a directory')
754
+ err.code = 'EISDIR'
755
+ throw err
756
+ }
757
+
758
+ unlinkSync(path)
759
+ } catch (err) {
760
+ if (err.code !== 'ENOENT' || !opts.force) throw err
761
+ }
762
+ }
763
+
631
764
  function unlink (path, cb) {
632
765
  if (typeof path !== 'string') {
633
766
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -680,6 +813,60 @@ function renameSync (src, dst) {
680
813
  binding.renameSync(src, dst)
681
814
  }
682
815
 
816
+ function realpath (path, opts, cb) {
817
+ if (typeof path !== 'string') {
818
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
819
+ }
820
+
821
+ if (typeof opts === 'function') {
822
+ cb = opts
823
+ opts = {}
824
+ } else if (typeof cb !== 'function') {
825
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
826
+ }
827
+
828
+ if (typeof opts === 'string') opts = { encoding: opts }
829
+ else if (!opts) opts = {}
830
+
831
+ const {
832
+ encoding = 'utf8'
833
+ } = opts
834
+
835
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
836
+
837
+ const req = getReq()
838
+
839
+ req.callback = function (err, _) {
840
+ if (err) return cb(err, null)
841
+ let path = data.subarray(0, data.indexOf(0))
842
+ if (encoding !== 'buffer') path = path.toString(encoding)
843
+ cb(null, path)
844
+ }
845
+
846
+ binding.realpath(req.handle, path, data)
847
+ }
848
+
849
+ function realpathSync (path, opts) {
850
+ if (typeof path !== 'string') {
851
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
852
+ }
853
+
854
+ if (typeof opts === 'string') opts = { encoding: opts }
855
+ else if (!opts) opts = {}
856
+
857
+ const {
858
+ encoding = 'utf8'
859
+ } = opts
860
+
861
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
862
+
863
+ binding.realpathSync(path, data)
864
+
865
+ path = data.subarray(0, data.indexOf(0))
866
+ if (encoding !== 'buffer') path = path.toString(encoding)
867
+ return path
868
+ }
869
+
683
870
  function readlink (path, opts, cb) {
684
871
  if (typeof path !== 'string') {
685
872
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -860,6 +1047,19 @@ function opendir (path, opts, cb) {
860
1047
  binding.opendir(req.handle, path, data)
861
1048
  }
862
1049
 
1050
+ function opendirSync (path, opts) {
1051
+ if (typeof path !== 'string') {
1052
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1053
+ }
1054
+
1055
+ if (typeof opts === 'string') opts = { encoding: opts }
1056
+ else if (!opts) opts = {}
1057
+
1058
+ const data = Buffer.allocUnsafe(binding.sizeofFSDir)
1059
+ binding.opendirSync(path, data)
1060
+ return new Dir(path, data, opts)
1061
+ }
1062
+
863
1063
  function readdir (path, opts, cb) {
864
1064
  if (typeof path !== 'string') {
865
1065
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -873,7 +1073,7 @@ function readdir (path, opts, cb) {
873
1073
  }
874
1074
 
875
1075
  if (typeof opts === 'string') opts = { encoding: opts }
876
- if (!opts) opts = {}
1076
+ else if (!opts) opts = {}
877
1077
 
878
1078
  const {
879
1079
  withFileTypes = false
@@ -882,13 +1082,37 @@ function readdir (path, opts, cb) {
882
1082
  opendir(path, opts, async (err, dir) => {
883
1083
  if (err) return cb(err, null)
884
1084
  const result = []
885
- dir
886
- .on('data', (entry) => result.push(withFileTypes ? entry : entry.name))
887
- .on('error', (err) => cb(err, null))
888
- .on('end', () => cb(null, result))
1085
+ for await (const entry of dir) {
1086
+ result.push(withFileTypes ? entry : entry.name)
1087
+ }
1088
+ cb(null, result)
889
1089
  })
890
1090
  }
891
1091
 
1092
+ function readdirSync (path, opts) {
1093
+ if (typeof path !== 'string') {
1094
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1095
+ }
1096
+
1097
+ if (typeof opts === 'string') opts = { encoding: opts }
1098
+ else if (!opts) opts = {}
1099
+
1100
+ const {
1101
+ withFileTypes = false
1102
+ } = opts
1103
+
1104
+ const dir = opendirSync(path, opts)
1105
+ const result = []
1106
+
1107
+ while (true) {
1108
+ const entry = dir.readSync()
1109
+ if (entry === null) break
1110
+ result.push(withFileTypes ? entry : entry.name)
1111
+ }
1112
+
1113
+ return result
1114
+ }
1115
+
892
1116
  function readFile (path, opts, cb) {
893
1117
  if (typeof path !== 'string') {
894
1118
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -1108,6 +1332,161 @@ class Stats {
1108
1332
  }
1109
1333
  }
1110
1334
 
1335
+ class Dir {
1336
+ constructor (path, handle, opts = {}) {
1337
+ const {
1338
+ encoding = 'utf8',
1339
+ bufferSize = 32
1340
+ } = opts
1341
+
1342
+ this._handle = handle
1343
+ this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
1344
+ this._encoding = encoding
1345
+ this._buffer = []
1346
+ this._ended = false
1347
+
1348
+ this.path = path
1349
+ }
1350
+
1351
+ read (cb) {
1352
+ if (!cb) return promisify(this.read.bind(this))
1353
+
1354
+ if (this._buffer.length) return queueMicrotask(() => cb(null, this._buffer.shift()))
1355
+ if (this._ended) return queueMicrotask(() => cb(null, null))
1356
+
1357
+ const data = []
1358
+ const req = getReq()
1359
+
1360
+ req.callback = (err, _) => {
1361
+ if (err) return cb(err, null)
1362
+ if (data.length === 0) this._ended = true
1363
+ else {
1364
+ for (const entry of data) {
1365
+ let name = Buffer.from(entry.name)
1366
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1367
+ this._buffer.push(new Dirent(this.path, name, entry.type))
1368
+ }
1369
+ }
1370
+
1371
+ if (this._ended) return cb(null, null)
1372
+ cb(null, this._buffer.shift())
1373
+ }
1374
+
1375
+ binding.readdir(req.handle, this._handle, this._dirents, data)
1376
+ }
1377
+
1378
+ readSync () {
1379
+ if (this._buffer.length) return this._buffer.shift()
1380
+ if (this._ended) return null
1381
+
1382
+ const data = []
1383
+
1384
+ binding.readdirSync(this._handle, this._dirents, data)
1385
+
1386
+ if (data.length === 0) this._ended = true
1387
+ else {
1388
+ for (const entry of data) {
1389
+ let name = Buffer.from(entry.name)
1390
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1391
+ this._buffer.push(new Dirent(this.path, name, entry.type))
1392
+ }
1393
+ }
1394
+
1395
+ if (this._ended) return null
1396
+ return this._buffer.shift()
1397
+ }
1398
+
1399
+ close (cb) {
1400
+ if (!cb) return promisify(this.close.bind(this))
1401
+
1402
+ const req = getReq()
1403
+
1404
+ req.callback = (err, _) => {
1405
+ this._handle = null
1406
+ cb(err)
1407
+ }
1408
+
1409
+ binding.closedir(req.handle, this._handle)
1410
+ }
1411
+
1412
+ closeSync () {
1413
+ binding.closedirSync(this._handle)
1414
+ this._handle = null
1415
+ }
1416
+
1417
+ [Symbol.iterator] () {
1418
+ return {
1419
+ next: () => {
1420
+ if (this._buffer.length) {
1421
+ return { done: false, value: this._buffer.shift() }
1422
+ }
1423
+
1424
+ if (this._ended) {
1425
+ this.closeSync()
1426
+
1427
+ return { done: true }
1428
+ }
1429
+
1430
+ const entry = this.readSync()
1431
+
1432
+ return { done: entry === null, value: entry }
1433
+ }
1434
+ }
1435
+ }
1436
+
1437
+ [Symbol.asyncIterator] () {
1438
+ return {
1439
+ next: () => new Promise((resolve, reject) => {
1440
+ if (this._buffer.length) {
1441
+ return resolve({ done: false, value: this._buffer.shift() })
1442
+ }
1443
+
1444
+ if (this._ended) {
1445
+ return this.close((err) => err ? reject(err) : resolve({ done: true }))
1446
+ }
1447
+
1448
+ this.read((err, entry) => err ? reject(err) : resolve({ done: entry === null, value: entry }))
1449
+ })
1450
+ }
1451
+ }
1452
+ }
1453
+
1454
+ class Dirent {
1455
+ constructor (path, name, type) {
1456
+ this.type = type
1457
+ this.path = path
1458
+ this.name = name
1459
+ }
1460
+
1461
+ isFile () {
1462
+ return this.type === constants.UV_DIRENT_FILE
1463
+ }
1464
+
1465
+ isDirectory () {
1466
+ return this.type === constants.UV_DIRENT_DIR
1467
+ }
1468
+
1469
+ isSymbolicLink () {
1470
+ return this.type === constants.UV_DIRENT_LINK
1471
+ }
1472
+
1473
+ isFIFO () {
1474
+ return this.type === constants.UV_DIRENT_FIFO
1475
+ }
1476
+
1477
+ isSocket () {
1478
+ return this.type === constants.UV_DIRENT_SOCKET
1479
+ }
1480
+
1481
+ isCharacterDevice () {
1482
+ return this.type === constants.UV_DIRENT_CHAR
1483
+ }
1484
+
1485
+ isBlockDevice () {
1486
+ return this.type === constants.UV_DIRENT_BLOCK
1487
+ }
1488
+ }
1489
+
1111
1490
  class FileWriteStream extends Writable {
1112
1491
  constructor (path, opts = {}) {
1113
1492
  super({ map })
@@ -1211,99 +1590,6 @@ class FileReadStream extends Readable {
1211
1590
  }
1212
1591
  }
1213
1592
 
1214
- class Dir extends Readable {
1215
- constructor (path, handle, opts = {}) {
1216
- const {
1217
- encoding = 'utf8',
1218
- bufferSize = 32
1219
- } = opts
1220
-
1221
- super()
1222
-
1223
- this._handle = handle
1224
- this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
1225
- this._encoding = encoding
1226
-
1227
- this.path = path
1228
- }
1229
-
1230
- _read (cb) {
1231
- const self = this
1232
- const data = []
1233
-
1234
- const req = getReq()
1235
-
1236
- req.callback = function (err, _) {
1237
- if (err) return cb(err)
1238
- if (data.length === 0) self.push(null)
1239
- else {
1240
- for (const entry of data) {
1241
- let name = Buffer.from(entry.name)
1242
- if (self._encoding !== 'buffer') name = name.toString(self._encoding)
1243
- self.push(new Dirent(self.path, name, entry.type))
1244
- }
1245
- }
1246
- cb(null)
1247
- }
1248
-
1249
- binding.readdir(req.handle, this._handle, this._dirents, data)
1250
- }
1251
-
1252
- _destroy (cb) {
1253
- const req = getReq()
1254
-
1255
- req.callback = function (err, _) {
1256
- cb(err)
1257
- }
1258
-
1259
- binding.closedir(req.handle, this._handle)
1260
- }
1261
-
1262
- close (cb = noop) {
1263
- if (this.destroyed) return cb(null)
1264
- this
1265
- .once('close', () => cb(getStreamError(this)))
1266
- .destroy()
1267
- }
1268
- }
1269
-
1270
- class Dirent {
1271
- constructor (path, name, type) {
1272
- this._type = type
1273
-
1274
- this.path = path
1275
- this.name = name
1276
- }
1277
-
1278
- isFile () {
1279
- return this._type === constants.UV_DIRENT_FILE
1280
- }
1281
-
1282
- isDirectory () {
1283
- return this._type === constants.UV_DIRENT_DIR
1284
- }
1285
-
1286
- isSymbolicLink () {
1287
- return this._type === constants.UV_DIRENT_LINK
1288
- }
1289
-
1290
- isFIFO () {
1291
- return this._type === constants.UV_DIRENT_FIFO
1292
- }
1293
-
1294
- isSocket () {
1295
- return this._type === constants.UV_DIRENT_SOCKET
1296
- }
1297
-
1298
- isCharacterDevice () {
1299
- return this._type === constants.UV_DIRENT_CHAR
1300
- }
1301
-
1302
- isBlockDevice () {
1303
- return this._type === constants.UV_DIRENT_BLOCK
1304
- }
1305
- }
1306
-
1307
1593
  exports.promises = {}
1308
1594
 
1309
1595
  function typeError (code, message) {
@@ -1314,6 +1600,7 @@ function typeError (code, message) {
1314
1600
 
1315
1601
  function noop () {}
1316
1602
 
1603
+ exports.access = access
1317
1604
  exports.chmod = chmod
1318
1605
  exports.close = close
1319
1606
  exports.fchmod = fchmod
@@ -1328,7 +1615,9 @@ exports.readFile = readFile
1328
1615
  exports.readdir = readdir
1329
1616
  exports.readlink = readlink
1330
1617
  exports.readv = readv
1618
+ exports.realpath = realpath
1331
1619
  exports.rename = rename
1620
+ exports.rm = rm
1332
1621
  exports.rmdir = rmdir
1333
1622
  exports.stat = stat
1334
1623
  exports.symlink = symlink
@@ -1337,6 +1626,7 @@ exports.write = write
1337
1626
  exports.writeFile = writeFile
1338
1627
  exports.writev = writev
1339
1628
 
1629
+ exports.accessSync = accessSync
1340
1630
  exports.chmodSync = chmodSync
1341
1631
  exports.closeSync = closeSync
1342
1632
  exports.fchmodSync = fchmodSync
@@ -1344,10 +1634,14 @@ exports.fstatSync = fstatSync
1344
1634
  exports.lstatSync = lstatSync
1345
1635
  exports.mkdirSync = mkdirSync
1346
1636
  exports.openSync = openSync
1637
+ exports.opendirSync = opendirSync
1347
1638
  exports.readFileSync = readFileSync
1348
1639
  exports.readSync = readSync
1640
+ exports.readdirSync = readdirSync
1349
1641
  exports.readlinkSync = readlinkSync
1642
+ exports.realpathSync = realpathSync
1350
1643
  exports.renameSync = renameSync
1644
+ exports.rmSync = rmSync
1351
1645
  exports.rmdirSync = rmdirSync
1352
1646
  exports.statSync = statSync
1353
1647
  exports.symlinkSync = symlinkSync
@@ -1355,6 +1649,7 @@ exports.unlinkSync = unlinkSync
1355
1649
  exports.writeFileSync = writeFileSync
1356
1650
  exports.writeSync = writeSync
1357
1651
 
1652
+ exports.promises.access = promisify(access)
1358
1653
  exports.promises.chmod = promisify(chmod)
1359
1654
  exports.promises.lstat = promisify(lstat)
1360
1655
  exports.promises.mkdir = promisify(mkdir)
@@ -1362,7 +1657,9 @@ exports.promises.opendir = promisify(opendir)
1362
1657
  exports.promises.readFile = promisify(readFile)
1363
1658
  exports.promises.readdir = promisify(readdir)
1364
1659
  exports.promises.readlink = promisify(readlink)
1660
+ exports.promises.realpath = promisify(realpath)
1365
1661
  exports.promises.rename = promisify(rename)
1662
+ exports.promises.rm = promisify(rm)
1366
1663
  exports.promises.rmdir = promisify(rmdir)
1367
1664
  exports.promises.stat = promisify(stat)
1368
1665
  exports.promises.symlink = promisify(symlink)
@@ -1370,6 +1667,8 @@ exports.promises.unlink = promisify(unlink)
1370
1667
  exports.promises.writeFile = promisify(writeFile)
1371
1668
 
1372
1669
  exports.Stats = Stats
1670
+ exports.Dir = Dir
1671
+ exports.Dirent = Dirent
1373
1672
 
1374
1673
  exports.ReadStream = FileReadStream
1375
1674
  exports.createReadStream = function createReadStream (path, opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "1.10.0",
3
+ "version": "1.12.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [