bare-fs 1.7.1 → 1.9.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 +36 -25
  2. package/binding.c +338 -163
  3. package/index.js +226 -2
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -8,59 +8,70 @@ npm i bare-fs
8
8
 
9
9
  ## Usage
10
10
 
11
- ``` js
11
+ ```js
12
12
  const fs = require('bare-fs')
13
13
 
14
14
  // Currently supports:
15
15
 
16
- fs.open
16
+ fs.chmod
17
17
  fs.close
18
- fs.read
19
- fs.readv
20
- fs.write
21
- fs.writev
22
- fs.stat
23
- fs.lstat
18
+ fs.chmod
24
19
  fs.fstat
25
20
  fs.ftruncate
21
+ fs.lstat
26
22
  fs.mkdir
27
- fs.rmdir
28
- fs.unlink
29
- fs.rename
30
- fs.readlink
23
+ fs.open
31
24
  fs.opendir
25
+ fs.read
32
26
  fs.readdir
33
- fs.chmod
27
+ fs.readlink
28
+ fs.readv
29
+ fs.rename
30
+ fs.rmdir
31
+ fs.stat
32
+ fs.symlink
33
+ fs.unlink
34
+ fs.write
35
+ fs.writev
34
36
 
35
37
  fs.readFile
36
38
  fs.writeFile
37
39
 
38
- fs.promises.stat
40
+ fs.promises.chmod
39
41
  fs.promises.lstat
40
42
  fs.promises.mkdir
41
- fs.promises.rmdir
42
- fs.promises.unlink
43
- fs.promises.rename
44
- fs.promises.readlink
45
43
  fs.promises.opendir
46
- fs.promises.readdir
47
44
  fs.promises.readFile
45
+ fs.promises.readdir
46
+ fs.promises.readlink
47
+ fs.promises.rename
48
+ fs.promises.rmdir
49
+ fs.promises.stat
50
+ fs.promises.symlink
51
+ fs.promises.unlink
48
52
  fs.promises.writeFile
49
- fs.promises.chmod
50
53
 
51
54
  fs.createReadStream
52
55
  fs.createWriteStream
53
56
 
54
- fs.openSync
57
+ fs.chmodSync
55
58
  fs.closeSync
59
+ fs.fchmodSync
60
+ fs.fstatSync
61
+ fs.lstatSync
62
+ fs.mkdirSync
63
+ fs.openSync
56
64
  fs.readSync
57
- fs.writeSync
65
+ fs.readlinkSync
66
+ fs.renameSync
67
+ fs.rmdirSync
58
68
  fs.statSync
59
- fs.lstatSync
60
- fs.fstatSync
69
+ fs.symlinkSync
70
+ fs.unlinkSync
71
+ fs.writeSync
72
+
61
73
  fs.readFileSync
62
74
  fs.writeFileSync
63
- fs.chmodSync
64
75
  ```
65
76
 
66
77
  ## License
package/binding.c CHANGED
@@ -882,6 +882,73 @@ bare_fs_chmod_sync (js_env_t *env, js_callback_info_t *info) {
882
882
  return NULL;
883
883
  }
884
884
 
885
+ static js_value_t *
886
+ bare_fs_fchmod (js_env_t *env, js_callback_info_t *info) {
887
+ int err;
888
+
889
+ size_t argc = 3;
890
+ js_value_t *argv[3];
891
+
892
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
893
+ assert(err == 0);
894
+
895
+ assert(argc == 3);
896
+
897
+ bare_fs_req_t *req;
898
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
899
+ assert(err == 0);
900
+
901
+ uint32_t fd;
902
+ err = js_get_value_uint32(env, argv[1], &fd);
903
+ assert(err == 0);
904
+
905
+ int32_t mode;
906
+ err = js_get_value_int32(env, argv[2], &mode);
907
+ assert(err == 0);
908
+
909
+ uv_loop_t *loop;
910
+ js_get_env_loop(env, &loop);
911
+
912
+ uv_fs_fchmod(loop, (uv_fs_t *) req, fd, mode, on_fs_response);
913
+
914
+ return NULL;
915
+ }
916
+
917
+ static js_value_t *
918
+ bare_fs_fchmod_sync (js_env_t *env, js_callback_info_t *info) {
919
+ int err;
920
+
921
+ size_t argc = 2;
922
+ js_value_t *argv[2];
923
+
924
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
925
+ assert(err == 0);
926
+
927
+ assert(argc == 2);
928
+
929
+ uint32_t fd;
930
+ err = js_get_value_uint32(env, argv[0], &fd);
931
+ assert(err == 0);
932
+
933
+ int32_t mode;
934
+ err = js_get_value_int32(env, argv[1], &mode);
935
+ assert(err == 0);
936
+
937
+ uv_loop_t *loop;
938
+ js_get_env_loop(env, &loop);
939
+
940
+ uv_fs_t req;
941
+ uv_fs_fchmod(loop, &req, fd, mode, NULL);
942
+
943
+ if (req.result < 0) {
944
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
945
+ }
946
+
947
+ uv_fs_req_cleanup(&req);
948
+
949
+ return NULL;
950
+ }
951
+
885
952
  static js_value_t *
886
953
  bare_fs_rename (js_env_t *env, js_callback_info_t *info) {
887
954
  int err;
@@ -914,6 +981,41 @@ bare_fs_rename (js_env_t *env, js_callback_info_t *info) {
914
981
  return NULL;
915
982
  }
916
983
 
984
+ static js_value_t *
985
+ bare_fs_rename_sync (js_env_t *env, js_callback_info_t *info) {
986
+ int err;
987
+
988
+ size_t argc = 2;
989
+ js_value_t *argv[2];
990
+
991
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
992
+ assert(err == 0);
993
+
994
+ assert(argc == 2);
995
+
996
+ bare_fs_path_t src;
997
+ err = js_get_value_string_utf8(env, argv[0], src, sizeof(bare_fs_path_t), NULL);
998
+ assert(err == 0);
999
+
1000
+ bare_fs_path_t dest;
1001
+ err = js_get_value_string_utf8(env, argv[1], dest, sizeof(bare_fs_path_t), NULL);
1002
+ assert(err == 0);
1003
+
1004
+ uv_loop_t *loop;
1005
+ js_get_env_loop(env, &loop);
1006
+
1007
+ uv_fs_t req;
1008
+ uv_fs_rename(loop, &req, (char *) src, (char *) dest, on_fs_response);
1009
+
1010
+ if (req.result < 0) {
1011
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1012
+ }
1013
+
1014
+ uv_fs_req_cleanup(&req);
1015
+
1016
+ return NULL;
1017
+ }
1018
+
917
1019
  static js_value_t *
918
1020
  bare_fs_mkdir (js_env_t *env, js_callback_info_t *info) {
919
1021
  int err;
@@ -946,6 +1048,41 @@ bare_fs_mkdir (js_env_t *env, js_callback_info_t *info) {
946
1048
  return NULL;
947
1049
  }
948
1050
 
1051
+ static js_value_t *
1052
+ bare_fs_mkdir_sync (js_env_t *env, js_callback_info_t *info) {
1053
+ int err;
1054
+
1055
+ size_t argc = 2;
1056
+ js_value_t *argv[2];
1057
+
1058
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1059
+ assert(err == 0);
1060
+
1061
+ assert(argc == 2);
1062
+
1063
+ bare_fs_path_t path;
1064
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1065
+ assert(err == 0);
1066
+
1067
+ int32_t mode;
1068
+ err = js_get_value_int32(env, argv[1], &mode);
1069
+ assert(err == 0);
1070
+
1071
+ uv_loop_t *loop;
1072
+ js_get_env_loop(env, &loop);
1073
+
1074
+ uv_fs_t req;
1075
+ uv_fs_mkdir(loop, &req, (char *) path, mode, on_fs_response);
1076
+
1077
+ if (req.result < 0) {
1078
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1079
+ }
1080
+
1081
+ uv_fs_req_cleanup(&req);
1082
+
1083
+ return NULL;
1084
+ }
1085
+
949
1086
  static js_value_t *
950
1087
  bare_fs_rmdir (js_env_t *env, js_callback_info_t *info) {
951
1088
  int err;
@@ -974,6 +1111,37 @@ bare_fs_rmdir (js_env_t *env, js_callback_info_t *info) {
974
1111
  return NULL;
975
1112
  }
976
1113
 
1114
+ static js_value_t *
1115
+ bare_fs_rmdir_sync (js_env_t *env, js_callback_info_t *info) {
1116
+ int err;
1117
+
1118
+ size_t argc = 1;
1119
+ js_value_t *argv[1];
1120
+
1121
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1122
+ assert(err == 0);
1123
+
1124
+ assert(argc == 1);
1125
+
1126
+ bare_fs_path_t path;
1127
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1128
+ assert(err == 0);
1129
+
1130
+ uv_loop_t *loop;
1131
+ js_get_env_loop(env, &loop);
1132
+
1133
+ uv_fs_t req;
1134
+ uv_fs_rmdir(loop, &req, (char *) path, on_fs_response);
1135
+
1136
+ if (req.result < 0) {
1137
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1138
+ }
1139
+
1140
+ uv_fs_req_cleanup(&req);
1141
+
1142
+ return NULL;
1143
+ }
1144
+
977
1145
  static js_value_t *
978
1146
  bare_fs_stat (js_env_t *env, js_callback_info_t *info) {
979
1147
  int err;
@@ -1323,6 +1491,37 @@ bare_fs_unlink (js_env_t *env, js_callback_info_t *info) {
1323
1491
  return NULL;
1324
1492
  }
1325
1493
 
1494
+ static js_value_t *
1495
+ bare_fs_unlink_sync (js_env_t *env, js_callback_info_t *info) {
1496
+ int err;
1497
+
1498
+ size_t argc = 1;
1499
+ js_value_t *argv[1];
1500
+
1501
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1502
+ assert(err == 0);
1503
+
1504
+ assert(argc == 1);
1505
+
1506
+ bare_fs_path_t path;
1507
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1508
+ assert(err == 0);
1509
+
1510
+ uv_loop_t *loop;
1511
+ js_get_env_loop(env, &loop);
1512
+
1513
+ uv_fs_t req;
1514
+ uv_fs_unlink(loop, &req, (char *) path, on_fs_response);
1515
+
1516
+ if (req.result < 0) {
1517
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1518
+ }
1519
+
1520
+ uv_fs_req_cleanup(&req);
1521
+
1522
+ return NULL;
1523
+ }
1524
+
1326
1525
  static js_value_t *
1327
1526
  bare_fs_readlink (js_env_t *env, js_callback_info_t *info) {
1328
1527
  int err;
@@ -1370,29 +1569,102 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1370
1569
  err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1371
1570
  assert(err == 0);
1372
1571
 
1373
- js_value_t *data = argv[1];
1374
-
1375
1572
  uv_loop_t *loop;
1376
1573
  js_get_env_loop(env, &loop);
1377
1574
 
1378
1575
  uv_fs_t req;
1379
1576
  uv_fs_readlink(loop, &req, (char *) path, NULL);
1380
1577
 
1381
- if (req.result == 0) {
1578
+ js_value_t *res = NULL;
1579
+
1580
+ if (req.result < 0) {
1581
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1582
+ } else {
1382
1583
  char *path;
1383
- err = js_get_typedarray_info(env, data, NULL, (void **) &path, NULL, NULL, NULL);
1584
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &path, NULL, NULL, NULL);
1384
1585
  assert(err == 0);
1385
1586
 
1386
1587
  strncpy(path, req.ptr, sizeof(bare_fs_path_t));
1387
1588
  }
1388
1589
 
1389
- js_value_t *res;
1390
- err = js_create_int32(env, req.result, &res);
1590
+ uv_fs_req_cleanup(&req);
1591
+
1592
+ return res;
1593
+ }
1594
+
1595
+ static js_value_t *
1596
+ bare_fs_symlink (js_env_t *env, js_callback_info_t *info) {
1597
+ int err;
1598
+
1599
+ size_t argc = 4;
1600
+ js_value_t *argv[4];
1601
+
1602
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1391
1603
  assert(err == 0);
1392
1604
 
1605
+ assert(argc == 4);
1606
+
1607
+ bare_fs_req_t *req;
1608
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
1609
+ assert(err == 0);
1610
+
1611
+ bare_fs_path_t target;
1612
+ err = js_get_value_string_utf8(env, argv[1], target, sizeof(bare_fs_path_t), NULL);
1613
+ assert(err == 0);
1614
+
1615
+ bare_fs_path_t path;
1616
+ err = js_get_value_string_utf8(env, argv[2], path, sizeof(bare_fs_path_t), NULL);
1617
+ assert(err == 0);
1618
+
1619
+ int32_t flags;
1620
+ err = js_get_value_int32(env, argv[3], &flags);
1621
+ assert(err == 0);
1622
+
1623
+ uv_loop_t *loop;
1624
+ js_get_env_loop(env, &loop);
1625
+
1626
+ uv_fs_symlink(loop, (uv_fs_t *) req, (char *) target, (char *) path, flags, on_fs_response);
1627
+
1628
+ return NULL;
1629
+ }
1630
+
1631
+ static js_value_t *
1632
+ bare_fs_symlink_sync (js_env_t *env, js_callback_info_t *info) {
1633
+ int err;
1634
+
1635
+ size_t argc = 3;
1636
+ js_value_t *argv[3];
1637
+
1638
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1639
+ assert(err == 0);
1640
+
1641
+ assert(argc == 3);
1642
+
1643
+ bare_fs_path_t target;
1644
+ err = js_get_value_string_utf8(env, argv[0], target, sizeof(bare_fs_path_t), NULL);
1645
+ assert(err == 0);
1646
+
1647
+ bare_fs_path_t path;
1648
+ err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
1649
+ assert(err == 0);
1650
+
1651
+ int32_t flags;
1652
+ err = js_get_value_int32(env, argv[2], &flags);
1653
+ assert(err == 0);
1654
+
1655
+ uv_loop_t *loop;
1656
+ js_get_env_loop(env, &loop);
1657
+
1658
+ uv_fs_t req;
1659
+ uv_fs_symlink(loop, &req, (char *) target, (char *) path, flags, NULL);
1660
+
1661
+ if (req.result < 0) {
1662
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1663
+ }
1664
+
1393
1665
  uv_fs_req_cleanup(&req);
1394
1666
 
1395
- return res;
1667
+ return NULL;
1396
1668
  }
1397
1669
 
1398
1670
  static js_value_t *
@@ -1495,6 +1767,8 @@ bare_fs_closedir (js_env_t *env, js_callback_info_t *info) {
1495
1767
 
1496
1768
  static js_value_t *
1497
1769
  init (js_env_t *env, js_value_t *exports) {
1770
+ int err;
1771
+
1498
1772
  {
1499
1773
  js_value_t *val;
1500
1774
  js_create_uint32(env, sizeof(bare_fs_t), &val);
@@ -1525,167 +1799,65 @@ init (js_env_t *env, js_value_t *exports) {
1525
1799
  js_create_uint32(env, sizeof(bare_fs_path_t), &val);
1526
1800
  js_set_named_property(env, exports, "sizeofFSPath", val);
1527
1801
  }
1528
- {
1529
- js_value_t *fn;
1530
- js_create_function(env, "init", -1, bare_fs_init, NULL, &fn);
1531
- js_set_named_property(env, exports, "init", fn);
1532
- }
1533
- {
1534
- js_value_t *fn;
1535
- js_create_function(env, "destroy", -1, bare_fs_destroy, NULL, &fn);
1536
- js_set_named_property(env, exports, "destroy", fn);
1537
- }
1538
- {
1539
- js_value_t *fn;
1540
- js_create_function(env, "initReq", -1, bare_fs_req_init, NULL, &fn);
1541
- js_set_named_property(env, exports, "initReq", fn);
1542
- }
1543
- {
1544
- js_value_t *fn;
1545
- js_create_function(env, "open", -1, bare_fs_open, NULL, &fn);
1546
- js_set_named_property(env, exports, "open", fn);
1547
- }
1548
- {
1549
- js_value_t *fn;
1550
- js_create_function(env, "openSync", -1, bare_fs_open_sync, NULL, &fn);
1551
- js_set_named_property(env, exports, "openSync", fn);
1552
- }
1553
- {
1554
- js_value_t *fn;
1555
- js_create_function(env, "close", -1, bare_fs_close, NULL, &fn);
1556
- js_set_named_property(env, exports, "close", fn);
1557
- }
1558
- {
1559
- js_value_t *fn;
1560
- js_create_function(env, "closeSync", -1, bare_fs_close_sync, NULL, &fn);
1561
- js_set_named_property(env, exports, "closeSync", fn);
1562
- }
1563
- {
1564
- js_value_t *fn;
1565
- js_create_function(env, "read", -1, bare_fs_read, NULL, &fn);
1566
- js_set_named_property(env, exports, "read", fn);
1567
- }
1568
- {
1569
- js_value_t *fn;
1570
- js_create_function(env, "readSync", -1, bare_fs_read_sync, NULL, &fn);
1571
- js_set_named_property(env, exports, "readSync", fn);
1572
- }
1573
- {
1574
- js_value_t *fn;
1575
- js_create_function(env, "readv", -1, bare_fs_readv, NULL, &fn);
1576
- js_set_named_property(env, exports, "readv", fn);
1577
- }
1578
- {
1579
- js_value_t *fn;
1580
- js_create_function(env, "write", -1, bare_fs_write, NULL, &fn);
1581
- js_set_named_property(env, exports, "write", fn);
1582
- }
1583
- {
1584
- js_value_t *fn;
1585
- js_create_function(env, "writeSync", -1, bare_fs_write_sync, NULL, &fn);
1586
- js_set_named_property(env, exports, "writeSync", fn);
1587
- }
1588
- {
1589
- js_value_t *fn;
1590
- js_create_function(env, "writev", -1, bare_fs_writev, NULL, &fn);
1591
- js_set_named_property(env, exports, "writev", fn);
1592
- }
1593
- {
1594
- js_value_t *fn;
1595
- js_create_function(env, "ftruncate", -1, bare_fs_ftruncate, NULL, &fn);
1596
- js_set_named_property(env, exports, "ftruncate", fn);
1597
- }
1598
- {
1599
- js_value_t *fn;
1600
- js_create_function(env, "chmod", -1, bare_fs_chmod, NULL, &fn);
1601
- js_set_named_property(env, exports, "chmod", fn);
1602
- }
1603
- {
1604
- js_value_t *fn;
1605
- js_create_function(env, "chmodSync", -1, bare_fs_chmod_sync, NULL, &fn);
1606
- js_set_named_property(env, exports, "chmodSync", fn);
1607
- }
1608
- {
1609
- js_value_t *fn;
1610
- js_create_function(env, "rename", -1, bare_fs_rename, NULL, &fn);
1611
- js_set_named_property(env, exports, "rename", fn);
1612
- }
1613
- {
1614
- js_value_t *fn;
1615
- js_create_function(env, "mkdir", -1, bare_fs_mkdir, NULL, &fn);
1616
- js_set_named_property(env, exports, "mkdir", fn);
1617
- }
1618
- {
1619
- js_value_t *fn;
1620
- js_create_function(env, "rmdir", -1, bare_fs_rmdir, NULL, &fn);
1621
- js_set_named_property(env, exports, "rmdir", fn);
1622
- }
1623
- {
1624
- js_value_t *fn;
1625
- js_create_function(env, "stat", -1, bare_fs_stat, NULL, &fn);
1626
- js_set_named_property(env, exports, "stat", fn);
1627
- }
1628
- {
1629
- js_value_t *fn;
1630
- js_create_function(env, "statSync", -1, bare_fs_stat_sync, NULL, &fn);
1631
- js_set_named_property(env, exports, "statSync", fn);
1632
- }
1633
- {
1634
- js_value_t *fn;
1635
- js_create_function(env, "lstat", -1, bare_fs_lstat, NULL, &fn);
1636
- js_set_named_property(env, exports, "lstat", fn);
1637
- }
1638
- {
1639
- js_value_t *fn;
1640
- js_create_function(env, "lstatSync", -1, bare_fs_lstat_sync, NULL, &fn);
1641
- js_set_named_property(env, exports, "lstatSync", fn);
1642
- }
1643
- {
1644
- js_value_t *fn;
1645
- js_create_function(env, "fstat", -1, bare_fs_fstat, NULL, &fn);
1646
- js_set_named_property(env, exports, "fstat", fn);
1647
- }
1648
- {
1649
- js_value_t *fn;
1650
- js_create_function(env, "fstatSync", -1, bare_fs_fstat_sync, NULL, &fn);
1651
- js_set_named_property(env, exports, "fstatSync", fn);
1652
- }
1653
- {
1654
- js_value_t *fn;
1655
- js_create_function(env, "unlink", -1, bare_fs_unlink, NULL, &fn);
1656
- js_set_named_property(env, exports, "unlink", fn);
1657
- }
1658
- {
1659
- js_value_t *fn;
1660
- js_create_function(env, "readlink", -1, bare_fs_readlink, NULL, &fn);
1661
- js_set_named_property(env, exports, "readlink", fn);
1662
- }
1663
- {
1664
- js_value_t *fn;
1665
- js_create_function(env, "readlinkSync", -1, bare_fs_readlink_sync, NULL, &fn);
1666
- js_set_named_property(env, exports, "readlinkSync", fn);
1667
- }
1668
- {
1669
- js_value_t *fn;
1670
- js_create_function(env, "opendir", -1, bare_fs_opendir, NULL, &fn);
1671
- js_set_named_property(env, exports, "opendir", fn);
1672
- }
1673
- {
1674
- js_value_t *fn;
1675
- js_create_function(env, "readdir", -1, bare_fs_readdir, NULL, &fn);
1676
- js_set_named_property(env, exports, "readdir", fn);
1677
- }
1678
- {
1679
- js_value_t *fn;
1680
- js_create_function(env, "closedir", -1, bare_fs_closedir, NULL, &fn);
1681
- js_set_named_property(env, exports, "closedir", fn);
1802
+ #define V(name, fn) \
1803
+ { \
1804
+ js_value_t *val; \
1805
+ err = js_create_function(env, name, -1, fn, NULL, &val); \
1806
+ assert(err == 0); \
1807
+ js_set_named_property(env, exports, name, val); \
1808
+ assert(err == 0); \
1682
1809
  }
1810
+
1811
+ V("init", bare_fs_init)
1812
+ V("destroy", bare_fs_destroy)
1813
+ V("initReq", bare_fs_req_init)
1814
+ V("open", bare_fs_open)
1815
+ V("openSync", bare_fs_open_sync)
1816
+ V("close", bare_fs_close)
1817
+ V("closeSync", bare_fs_close_sync)
1818
+ V("read", bare_fs_read)
1819
+ V("readSync", bare_fs_read_sync)
1820
+ V("readv", bare_fs_readv)
1821
+ V("write", bare_fs_write)
1822
+ V("writeSync", bare_fs_write_sync)
1823
+ V("writev", bare_fs_writev)
1824
+ V("ftruncate", bare_fs_ftruncate)
1825
+ V("chmod", bare_fs_chmod)
1826
+ V("chmodSync", bare_fs_chmod_sync)
1827
+ V("fchmod", bare_fs_fchmod)
1828
+ V("fchmodSync", bare_fs_fchmod_sync)
1829
+ V("rename", bare_fs_rename)
1830
+ V("renameSync", bare_fs_rename_sync)
1831
+ V("mkdir", bare_fs_mkdir)
1832
+ V("mkdirSync", bare_fs_mkdir_sync)
1833
+ V("rmdir", bare_fs_rmdir)
1834
+ V("rmdirSync", bare_fs_rmdir_sync)
1835
+ V("stat", bare_fs_stat)
1836
+ V("statSync", bare_fs_stat_sync)
1837
+ V("lstat", bare_fs_lstat)
1838
+ V("lstatSync", bare_fs_lstat_sync)
1839
+ V("fstat", bare_fs_fstat)
1840
+ V("fstatSync", bare_fs_fstat_sync)
1841
+ V("unlink", bare_fs_unlink)
1842
+ V("unlinkSync", bare_fs_unlink_sync)
1843
+ V("readlink", bare_fs_readlink)
1844
+ V("readlinkSync", bare_fs_readlink_sync)
1845
+ V("symlink", bare_fs_symlink)
1846
+ V("symlinkSync", bare_fs_symlink_sync)
1847
+ V("opendir", bare_fs_opendir)
1848
+ V("readdir", bare_fs_readdir)
1849
+ V("closedir", bare_fs_closedir)
1850
+ #undef V
1851
+
1683
1852
  #define V(name) \
1684
1853
  { \
1685
1854
  js_value_t *val; \
1686
- js_create_uint32(env, name, &val); \
1687
- js_set_named_property(env, exports, #name, val); \
1855
+ err = js_create_uint32(env, name, &val); \
1856
+ assert(err == 0); \
1857
+ err = js_set_named_property(env, exports, #name, val); \
1858
+ assert(err == 0); \
1688
1859
  }
1860
+
1689
1861
  V(O_RDWR)
1690
1862
  V(O_RDONLY)
1691
1863
  V(O_WRONLY)
@@ -1744,6 +1916,9 @@ init (js_env_t *env, js_value_t *exports) {
1744
1916
  V(UV_DIRENT_SOCKET)
1745
1917
  V(UV_DIRENT_CHAR)
1746
1918
  V(UV_DIRENT_BLOCK)
1919
+
1920
+ V(UV_FS_SYMLINK_DIR)
1921
+ V(UV_FS_SYMLINK_JUNCTION)
1747
1922
  #undef V
1748
1923
 
1749
1924
  return exports;
package/index.js CHANGED
@@ -1,7 +1,8 @@
1
+ const { sep, resolve, isAbsolute, toNamespacedPath } = require('path')
1
2
  const { Readable, Writable, getStreamError } = require('streamx')
2
3
  const binding = require('./binding')
3
4
 
4
- const sep = process.platform === 'win32' ? '\\' : '/'
5
+ const isWindows = process.platform === 'win32'
5
6
 
6
7
  const constants = exports.constants = {
7
8
  O_RDWR: binding.O_RDWR,
@@ -37,7 +38,10 @@ const constants = exports.constants = {
37
38
  UV_DIRENT_FIFO: binding.UV_DIRENT_FIFO,
38
39
  UV_DIRENT_SOCKET: binding.UV_DIRENT_SOCKET,
39
40
  UV_DIRENT_CHAR: binding.UV_DIRENT_CHAR,
40
- UV_DIRENT_BLOCK: binding.UV_DIRENT_BLOCK
41
+ UV_DIRENT_BLOCK: binding.UV_DIRENT_BLOCK,
42
+
43
+ UV_FS_SYMLINK_DIR: binding.UV_FS_SYMLINK_DIR,
44
+ UV_FS_SYMLINK_JUNCTION: binding.UV_FS_SYMLINK_JUNCTION
41
45
  }
42
46
 
43
47
  const reqs = []
@@ -496,6 +500,36 @@ function chmodSync (path, mode) {
496
500
  binding.chmodSync(path, mode)
497
501
  }
498
502
 
503
+ function fchmod (fd, mode, cb) {
504
+ if (typeof fd !== 'number') {
505
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
506
+ }
507
+
508
+ if (fd < 0 || fd > 0x7fffffff) {
509
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
510
+ }
511
+
512
+ if (typeof mode === 'string') mode = modeToNumber(mode)
513
+
514
+ const req = getReq()
515
+ req.callback = cb
516
+ binding.fchmod(req.handle, fd, mode)
517
+ }
518
+
519
+ function fchmodSync (fd, mode) {
520
+ if (typeof fd !== 'number') {
521
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
522
+ }
523
+
524
+ if (fd < 0 || fd > 0x7fffffff) {
525
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
526
+ }
527
+
528
+ if (typeof mode === 'string') mode = modeToNumber(mode)
529
+
530
+ binding.fchmodSync(fd, mode)
531
+ }
532
+
499
533
  function mkdirp (path, mode, cb) {
500
534
  mkdir(path, { mode }, function (err) {
501
535
  if (err === null) return cb(null, 0, null)
@@ -544,6 +578,38 @@ function mkdir (path, opts, cb) {
544
578
  binding.mkdir(req.handle, path, mode)
545
579
  }
546
580
 
581
+ function mkdirpSync (path, mode) {
582
+ try {
583
+ mkdirSync(path, { mode })
584
+ } catch (err) {
585
+ if (err.code !== 'ENOENT' && statSync(path).isDirectory()) {
586
+ return
587
+ }
588
+
589
+ while (path.endsWith(sep)) path = path.slice(0, -1)
590
+ const i = path.lastIndexOf(sep)
591
+ if (i <= 0) throw err
592
+
593
+ mkdirpSync(path.slice(0, i), { mode })
594
+ mkdirSync(path, { mode })
595
+ }
596
+ }
597
+
598
+ function mkdirSync (path, opts) {
599
+ if (typeof path !== 'string') {
600
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
601
+ }
602
+
603
+ if (typeof opts === 'number') opts = { mode: opts }
604
+ else if (!opts) opts = {}
605
+
606
+ const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
607
+
608
+ if (opts.recursive) return mkdirpSync(path, mode)
609
+
610
+ binding.mkdirSync(path, mode)
611
+ }
612
+
547
613
  function rmdir (path, cb) {
548
614
  if (typeof path !== 'string') {
549
615
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -558,6 +624,14 @@ function rmdir (path, cb) {
558
624
  binding.rmdir(req.handle, path)
559
625
  }
560
626
 
627
+ function rmdirSync (path) {
628
+ if (typeof path !== 'string') {
629
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
630
+ }
631
+
632
+ binding.rmdirSync(path)
633
+ }
634
+
561
635
  function unlink (path, cb) {
562
636
  if (typeof path !== 'string') {
563
637
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -572,6 +646,14 @@ function unlink (path, cb) {
572
646
  binding.unlink(req.handle, path)
573
647
  }
574
648
 
649
+ function unlinkSync (path) {
650
+ if (typeof path !== 'string') {
651
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
652
+ }
653
+
654
+ binding.unlinkSync(path)
655
+ }
656
+
575
657
  function rename (src, dst, cb) {
576
658
  if (typeof src !== 'string') {
577
659
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
@@ -590,6 +672,18 @@ function rename (src, dst, cb) {
590
672
  binding.rename(req.handle, src, dst)
591
673
  }
592
674
 
675
+ function renameSync (src, dst) {
676
+ if (typeof src !== 'string') {
677
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
678
+ }
679
+
680
+ if (typeof dst !== 'string') {
681
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
682
+ }
683
+
684
+ binding.renameSync(src, dst)
685
+ }
686
+
593
687
  function readlink (path, opts, cb) {
594
688
  if (typeof path !== 'string') {
595
689
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -623,6 +717,126 @@ function readlink (path, opts, cb) {
623
717
  binding.readlink(req.handle, path, data)
624
718
  }
625
719
 
720
+ function readlinkSync (path, opts) {
721
+ if (typeof path !== 'string') {
722
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
723
+ }
724
+
725
+ if (typeof opts === 'string') opts = { encoding: opts }
726
+ else if (!opts) opts = {}
727
+
728
+ const {
729
+ encoding = 'utf8'
730
+ } = opts
731
+
732
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
733
+
734
+ binding.readlinkSync(path, data)
735
+
736
+ path = data.subarray(0, data.indexOf(0))
737
+ if (encoding !== 'buffer') path = path.toString(encoding)
738
+ return path
739
+ }
740
+
741
+ function normalizeSymlinkTarget (target, type, path) {
742
+ if (isWindows) {
743
+ if (type === 'junction') target = resolve(path, '..', target)
744
+
745
+ if (isAbsolute(target)) return toNamespacedPath(target)
746
+
747
+ return target.replace(/\//g, sep)
748
+ }
749
+
750
+ return target
751
+ }
752
+
753
+ function symlink (target, path, type, cb) {
754
+ if (typeof target !== 'string') {
755
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof path) + ' (' + path + ')')
756
+ }
757
+
758
+ if (typeof path !== 'string') {
759
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
760
+ }
761
+
762
+ if (typeof type === 'function') {
763
+ cb = type
764
+ type = null
765
+ } else if (typeof cb !== 'function') {
766
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
767
+ }
768
+
769
+ if (typeof type === 'string') {
770
+ switch (type) {
771
+ case 'file':
772
+ type = 0
773
+ break
774
+ case 'dir':
775
+ type = constants.UV_FS_SYMLINK_DIR
776
+ break
777
+ case 'junction':
778
+ type = constants.UV_FS_SYMLINK_JUNCTION
779
+ break
780
+ default:
781
+ throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
782
+ }
783
+ } else if (typeof type !== 'number') {
784
+ if (isWindows) {
785
+ target = resolve(path, '..', target)
786
+
787
+ stat(target, (err, st) => {
788
+ type = err === null && st.isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
789
+
790
+ symlink(target, path, type, path, cb)
791
+ })
792
+
793
+ return
794
+ }
795
+
796
+ type = 0
797
+ }
798
+
799
+ const req = getReq()
800
+ req.callback = cb
801
+ binding.symlink(req.handle, normalizeSymlinkTarget(target), toNamespacedPath(path), type)
802
+ }
803
+
804
+ function symlinkSync (target, path, type) {
805
+ if (typeof target !== 'string') {
806
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof path) + ' (' + path + ')')
807
+ }
808
+
809
+ if (typeof path !== 'string') {
810
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
811
+ }
812
+
813
+ if (typeof type === 'string') {
814
+ switch (type) {
815
+ case 'file':
816
+ type = 0
817
+ break
818
+ case 'dir':
819
+ type = constants.UV_FS_SYMLINK_DIR
820
+ break
821
+ case 'junction':
822
+ type = constants.UV_FS_SYMLINK_JUNCTION
823
+ break
824
+ default:
825
+ throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
826
+ }
827
+ } else if (typeof type !== 'number') {
828
+ if (isWindows) {
829
+ target = resolve(path, '..', target)
830
+
831
+ type = statSync(target).isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
832
+ } else {
833
+ type = 0
834
+ }
835
+ }
836
+
837
+ binding.symlinkSync(normalizeSymlinkTarget(target), toNamespacedPath(path), type)
838
+ }
839
+
626
840
  function opendir (path, opts, cb) {
627
841
  if (typeof path !== 'string') {
628
842
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -1106,6 +1320,7 @@ function noop () {}
1106
1320
 
1107
1321
  exports.chmod = chmod
1108
1322
  exports.close = close
1323
+ exports.fchmod = fchmod
1109
1324
  exports.fstat = fstat
1110
1325
  exports.ftruncate = ftruncate
1111
1326
  exports.lstat = lstat
@@ -1120,6 +1335,7 @@ exports.readv = readv
1120
1335
  exports.rename = rename
1121
1336
  exports.rmdir = rmdir
1122
1337
  exports.stat = stat
1338
+ exports.symlink = symlink
1123
1339
  exports.unlink = unlink
1124
1340
  exports.write = write
1125
1341
  exports.writeFile = writeFile
@@ -1127,12 +1343,19 @@ exports.writev = writev
1127
1343
 
1128
1344
  exports.chmodSync = chmodSync
1129
1345
  exports.closeSync = closeSync
1346
+ exports.fchmodSync = fchmodSync
1130
1347
  exports.fstatSync = fstatSync
1131
1348
  exports.lstatSync = lstatSync
1349
+ exports.mkdirSync = mkdirSync
1132
1350
  exports.openSync = openSync
1133
1351
  exports.readFileSync = readFileSync
1134
1352
  exports.readSync = readSync
1353
+ exports.readlinkSync = readlinkSync
1354
+ exports.renameSync = renameSync
1355
+ exports.rmdirSync = rmdirSync
1135
1356
  exports.statSync = statSync
1357
+ exports.symlinkSync = symlinkSync
1358
+ exports.unlinkSync = unlinkSync
1136
1359
  exports.writeFileSync = writeFileSync
1137
1360
  exports.writeSync = writeSync
1138
1361
 
@@ -1146,6 +1369,7 @@ exports.promises.readlink = promisify(readlink)
1146
1369
  exports.promises.rename = promisify(rename)
1147
1370
  exports.promises.rmdir = promisify(rmdir)
1148
1371
  exports.promises.stat = promisify(stat)
1372
+ exports.promises.symlink = promisify(symlink)
1149
1373
  exports.promises.unlink = promisify(unlink)
1150
1374
  exports.promises.writeFile = promisify(writeFile)
1151
1375
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "1.7.1",
3
+ "version": "1.9.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [