bare-fs 1.7.1 → 1.8.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 +29 -25
  2. package/binding.c +93 -7
  3. package/index.js +130 -2
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -8,59 +8,63 @@ 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
24
18
  fs.fstat
25
19
  fs.ftruncate
20
+ fs.lstat
26
21
  fs.mkdir
27
- fs.rmdir
28
- fs.unlink
29
- fs.rename
30
- fs.readlink
22
+ fs.open
31
23
  fs.opendir
24
+ fs.read
32
25
  fs.readdir
33
- fs.chmod
26
+ fs.readlink
27
+ fs.readv
28
+ fs.rename
29
+ fs.rmdir
30
+ fs.stat
31
+ fs.symlink
32
+ fs.unlink
33
+ fs.write
34
+ fs.writev
34
35
 
35
36
  fs.readFile
36
37
  fs.writeFile
37
38
 
38
- fs.promises.stat
39
+ fs.promises.chmod
39
40
  fs.promises.lstat
40
41
  fs.promises.mkdir
41
- fs.promises.rmdir
42
- fs.promises.unlink
43
- fs.promises.rename
44
- fs.promises.readlink
45
42
  fs.promises.opendir
46
- fs.promises.readdir
47
43
  fs.promises.readFile
44
+ fs.promises.readdir
45
+ fs.promises.readlink
46
+ fs.promises.rename
47
+ fs.promises.rmdir
48
+ fs.promises.stat
49
+ fs.promises.symlink
50
+ fs.promises.unlink
48
51
  fs.promises.writeFile
49
- fs.promises.chmod
50
52
 
51
53
  fs.createReadStream
52
54
  fs.createWriteStream
53
55
 
54
- fs.openSync
56
+ fs.chmodSync
55
57
  fs.closeSync
58
+ fs.fstatSync
59
+ fs.lstatSync
60
+ fs.openSync
56
61
  fs.readSync
57
- fs.writeSync
58
62
  fs.statSync
59
- fs.lstatSync
60
- fs.fstatSync
63
+ fs.symlinkSync
64
+ fs.writeSync
65
+
61
66
  fs.readFileSync
62
67
  fs.writeFileSync
63
- fs.chmodSync
64
68
  ```
65
69
 
66
70
  ## License
package/binding.c CHANGED
@@ -1370,29 +1370,102 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1370
1370
  err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1371
1371
  assert(err == 0);
1372
1372
 
1373
- js_value_t *data = argv[1];
1374
-
1375
1373
  uv_loop_t *loop;
1376
1374
  js_get_env_loop(env, &loop);
1377
1375
 
1378
1376
  uv_fs_t req;
1379
1377
  uv_fs_readlink(loop, &req, (char *) path, NULL);
1380
1378
 
1381
- if (req.result == 0) {
1379
+ js_value_t *res = NULL;
1380
+
1381
+ if (req.result < 0) {
1382
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1383
+ } else {
1382
1384
  char *path;
1383
- err = js_get_typedarray_info(env, data, NULL, (void **) &path, NULL, NULL, NULL);
1385
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &path, NULL, NULL, NULL);
1384
1386
  assert(err == 0);
1385
1387
 
1386
1388
  strncpy(path, req.ptr, sizeof(bare_fs_path_t));
1387
1389
  }
1388
1390
 
1389
- js_value_t *res;
1390
- err = js_create_int32(env, req.result, &res);
1391
+ uv_fs_req_cleanup(&req);
1392
+
1393
+ return res;
1394
+ }
1395
+
1396
+ static js_value_t *
1397
+ bare_fs_symlink (js_env_t *env, js_callback_info_t *info) {
1398
+ int err;
1399
+
1400
+ size_t argc = 4;
1401
+ js_value_t *argv[4];
1402
+
1403
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1391
1404
  assert(err == 0);
1392
1405
 
1406
+ assert(argc == 4);
1407
+
1408
+ bare_fs_req_t *req;
1409
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
1410
+ assert(err == 0);
1411
+
1412
+ bare_fs_path_t target;
1413
+ err = js_get_value_string_utf8(env, argv[1], target, sizeof(bare_fs_path_t), NULL);
1414
+ assert(err == 0);
1415
+
1416
+ bare_fs_path_t path;
1417
+ err = js_get_value_string_utf8(env, argv[2], path, sizeof(bare_fs_path_t), NULL);
1418
+ assert(err == 0);
1419
+
1420
+ int32_t flags;
1421
+ err = js_get_value_int32(env, argv[3], &flags);
1422
+ assert(err == 0);
1423
+
1424
+ uv_loop_t *loop;
1425
+ js_get_env_loop(env, &loop);
1426
+
1427
+ uv_fs_symlink(loop, (uv_fs_t *) req, (char *) target, (char *) path, flags, on_fs_response);
1428
+
1429
+ return NULL;
1430
+ }
1431
+
1432
+ static js_value_t *
1433
+ bare_fs_symlink_sync (js_env_t *env, js_callback_info_t *info) {
1434
+ int err;
1435
+
1436
+ size_t argc = 3;
1437
+ js_value_t *argv[3];
1438
+
1439
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
1440
+ assert(err == 0);
1441
+
1442
+ assert(argc == 3);
1443
+
1444
+ bare_fs_path_t target;
1445
+ err = js_get_value_string_utf8(env, argv[0], target, sizeof(bare_fs_path_t), NULL);
1446
+ assert(err == 0);
1447
+
1448
+ bare_fs_path_t path;
1449
+ err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
1450
+ assert(err == 0);
1451
+
1452
+ int32_t flags;
1453
+ err = js_get_value_int32(env, argv[2], &flags);
1454
+ assert(err == 0);
1455
+
1456
+ uv_loop_t *loop;
1457
+ js_get_env_loop(env, &loop);
1458
+
1459
+ uv_fs_t req;
1460
+ uv_fs_symlink(loop, &req, (char *) target, (char *) path, flags, NULL);
1461
+
1462
+ if (req.result < 0) {
1463
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
1464
+ }
1465
+
1393
1466
  uv_fs_req_cleanup(&req);
1394
1467
 
1395
- return res;
1468
+ return NULL;
1396
1469
  }
1397
1470
 
1398
1471
  static js_value_t *
@@ -1665,6 +1738,16 @@ init (js_env_t *env, js_value_t *exports) {
1665
1738
  js_create_function(env, "readlinkSync", -1, bare_fs_readlink_sync, NULL, &fn);
1666
1739
  js_set_named_property(env, exports, "readlinkSync", fn);
1667
1740
  }
1741
+ {
1742
+ js_value_t *fn;
1743
+ js_create_function(env, "symlink", -1, bare_fs_symlink, NULL, &fn);
1744
+ js_set_named_property(env, exports, "symlink", fn);
1745
+ }
1746
+ {
1747
+ js_value_t *fn;
1748
+ js_create_function(env, "symlinkSync", -1, bare_fs_symlink_sync, NULL, &fn);
1749
+ js_set_named_property(env, exports, "symlinkSync", fn);
1750
+ }
1668
1751
  {
1669
1752
  js_value_t *fn;
1670
1753
  js_create_function(env, "opendir", -1, bare_fs_opendir, NULL, &fn);
@@ -1744,6 +1827,9 @@ init (js_env_t *env, js_value_t *exports) {
1744
1827
  V(UV_DIRENT_SOCKET)
1745
1828
  V(UV_DIRENT_CHAR)
1746
1829
  V(UV_DIRENT_BLOCK)
1830
+
1831
+ V(UV_FS_SYMLINK_DIR)
1832
+ V(UV_FS_SYMLINK_JUNCTION)
1747
1833
  #undef V
1748
1834
 
1749
1835
  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 = []
@@ -623,6 +627,126 @@ function readlink (path, opts, cb) {
623
627
  binding.readlink(req.handle, path, data)
624
628
  }
625
629
 
630
+ function readlinkSync (path, opts) {
631
+ if (typeof path !== 'string') {
632
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
633
+ }
634
+
635
+ if (typeof opts === 'string') opts = { encoding: opts }
636
+ else if (!opts) opts = {}
637
+
638
+ const {
639
+ encoding = 'utf8'
640
+ } = opts
641
+
642
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
643
+
644
+ binding.readlinkSync(path, data)
645
+
646
+ path = data.subarray(0, data.indexOf(0))
647
+ if (encoding !== 'buffer') path = path.toString(encoding)
648
+ return path
649
+ }
650
+
651
+ function normalizeSymlinkTarget (target, type, path) {
652
+ if (isWindows) {
653
+ if (type === 'junction') target = resolve(path, '..', target)
654
+
655
+ if (isAbsolute(target)) return toNamespacedPath(target)
656
+
657
+ return target.replace(/\//g, sep)
658
+ }
659
+
660
+ return target
661
+ }
662
+
663
+ function symlink (target, path, type, cb) {
664
+ if (typeof target !== 'string') {
665
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof path) + ' (' + path + ')')
666
+ }
667
+
668
+ if (typeof path !== 'string') {
669
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
670
+ }
671
+
672
+ if (typeof type === 'function') {
673
+ cb = type
674
+ type = null
675
+ } else if (typeof cb !== 'function') {
676
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
677
+ }
678
+
679
+ if (typeof type === 'string') {
680
+ switch (type) {
681
+ case 'file':
682
+ type = 0
683
+ break
684
+ case 'dir':
685
+ type = constants.UV_FS_SYMLINK_DIR
686
+ break
687
+ case 'junction':
688
+ type = constants.UV_FS_SYMLINK_JUNCTION
689
+ break
690
+ default:
691
+ throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
692
+ }
693
+ } else if (typeof type !== 'number') {
694
+ if (isWindows) {
695
+ target = resolve(path, '..', target)
696
+
697
+ stat(target, (err, st) => {
698
+ type = err === null && st.isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
699
+
700
+ symlink(target, path, type, path, cb)
701
+ })
702
+
703
+ return
704
+ }
705
+
706
+ type = 0
707
+ }
708
+
709
+ const req = getReq()
710
+ req.callback = cb
711
+ binding.symlink(req.handle, normalizeSymlinkTarget(target), toNamespacedPath(path), type)
712
+ }
713
+
714
+ function symlinkSync (target, path, type) {
715
+ if (typeof target !== 'string') {
716
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof path) + ' (' + path + ')')
717
+ }
718
+
719
+ if (typeof path !== 'string') {
720
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
721
+ }
722
+
723
+ if (typeof type === 'string') {
724
+ switch (type) {
725
+ case 'file':
726
+ type = 0
727
+ break
728
+ case 'dir':
729
+ type = constants.UV_FS_SYMLINK_DIR
730
+ break
731
+ case 'junction':
732
+ type = constants.UV_FS_SYMLINK_JUNCTION
733
+ break
734
+ default:
735
+ throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
736
+ }
737
+ } else if (typeof type !== 'number') {
738
+ if (isWindows) {
739
+ target = resolve(path, '..', target)
740
+
741
+ type = statSync(target).isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
742
+ } else {
743
+ type = 0
744
+ }
745
+ }
746
+
747
+ binding.symlinkSync(normalizeSymlinkTarget(target), toNamespacedPath(path), type)
748
+ }
749
+
626
750
  function opendir (path, opts, cb) {
627
751
  if (typeof path !== 'string') {
628
752
  throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
@@ -1120,6 +1244,7 @@ exports.readv = readv
1120
1244
  exports.rename = rename
1121
1245
  exports.rmdir = rmdir
1122
1246
  exports.stat = stat
1247
+ exports.symlink = symlink
1123
1248
  exports.unlink = unlink
1124
1249
  exports.write = write
1125
1250
  exports.writeFile = writeFile
@@ -1132,7 +1257,9 @@ exports.lstatSync = lstatSync
1132
1257
  exports.openSync = openSync
1133
1258
  exports.readFileSync = readFileSync
1134
1259
  exports.readSync = readSync
1260
+ exports.readlinkSync = readlinkSync
1135
1261
  exports.statSync = statSync
1262
+ exports.symlinkSync = symlinkSync
1136
1263
  exports.writeFileSync = writeFileSync
1137
1264
  exports.writeSync = writeSync
1138
1265
 
@@ -1146,6 +1273,7 @@ exports.promises.readlink = promisify(readlink)
1146
1273
  exports.promises.rename = promisify(rename)
1147
1274
  exports.promises.rmdir = promisify(rmdir)
1148
1275
  exports.promises.stat = promisify(stat)
1276
+ exports.promises.symlink = promisify(symlink)
1149
1277
  exports.promises.unlink = promisify(unlink)
1150
1278
  exports.promises.writeFile = promisify(writeFile)
1151
1279
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "1.7.1",
3
+ "version": "1.8.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [