bare-fs 1.7.0 → 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 +113 -11
  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
@@ -488,7 +488,8 @@ bare_fs_read (js_env_t *env, js_callback_info_t *info) {
488
488
  assert(err == 0);
489
489
 
490
490
  uint8_t *data;
491
- err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data, NULL, NULL, NULL);
491
+ size_t data_len;
492
+ err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data, &data_len, NULL, NULL);
492
493
  assert(err == 0);
493
494
 
494
495
  uint32_t offset;
@@ -499,6 +500,9 @@ bare_fs_read (js_env_t *env, js_callback_info_t *info) {
499
500
  err = js_get_value_uint32(env, argv[4], &len);
500
501
  assert(err == 0);
501
502
 
503
+ if (offset >= data_len) len = 0;
504
+ else if (offset + len >= data_len) len = data_len - offset;
505
+
502
506
  int64_t pos;
503
507
  err = js_get_value_int64(env, argv[5], &pos);
504
508
  assert(err == 0);
@@ -530,7 +534,8 @@ bare_fs_read_sync (js_env_t *env, js_callback_info_t *info) {
530
534
  assert(err == 0);
531
535
 
532
536
  uint8_t *data;
533
- err = js_get_typedarray_info(env, argv[1], NULL, (void **) &data, NULL, NULL, NULL);
537
+ size_t data_len;
538
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &data, &data_len, NULL, NULL);
534
539
  assert(err == 0);
535
540
 
536
541
  uint32_t offset;
@@ -541,6 +546,9 @@ bare_fs_read_sync (js_env_t *env, js_callback_info_t *info) {
541
546
  err = js_get_value_uint32(env, argv[3], &len);
542
547
  assert(err == 0);
543
548
 
549
+ if (offset >= data_len) len = 0;
550
+ else if (offset + len >= data_len) len = data_len - offset;
551
+
544
552
  int64_t pos;
545
553
  err = js_get_value_int64(env, argv[4], &pos);
546
554
  assert(err == 0);
@@ -638,7 +646,8 @@ bare_fs_write (js_env_t *env, js_callback_info_t *info) {
638
646
  assert(err == 0);
639
647
 
640
648
  uint8_t *data;
641
- err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data, NULL, NULL, NULL);
649
+ size_t data_len;
650
+ err = js_get_typedarray_info(env, argv[2], NULL, (void **) &data, &data_len, NULL, NULL);
642
651
  assert(err == 0);
643
652
 
644
653
  uint32_t offset;
@@ -649,6 +658,9 @@ bare_fs_write (js_env_t *env, js_callback_info_t *info) {
649
658
  err = js_get_value_uint32(env, argv[4], &len);
650
659
  assert(err == 0);
651
660
 
661
+ if (offset >= data_len) len = 0;
662
+ else if (offset + len >= data_len) len = data_len - offset;
663
+
652
664
  int64_t pos;
653
665
  err = js_get_value_int64(env, argv[5], &pos);
654
666
  assert(err == 0);
@@ -680,7 +692,8 @@ bare_fs_write_sync (js_env_t *env, js_callback_info_t *info) {
680
692
  assert(err == 0);
681
693
 
682
694
  uint8_t *data;
683
- err = js_get_typedarray_info(env, argv[1], NULL, (void **) &data, NULL, NULL, NULL);
695
+ size_t data_len;
696
+ err = js_get_typedarray_info(env, argv[1], NULL, (void **) &data, &data_len, NULL, NULL);
684
697
  assert(err == 0);
685
698
 
686
699
  uint32_t offset;
@@ -691,6 +704,9 @@ bare_fs_write_sync (js_env_t *env, js_callback_info_t *info) {
691
704
  err = js_get_value_uint32(env, argv[3], &len);
692
705
  assert(err == 0);
693
706
 
707
+ if (offset >= data_len) len = 0;
708
+ else if (offset + len >= data_len) len = data_len - offset;
709
+
694
710
  int64_t pos;
695
711
  err = js_get_value_int64(env, argv[4], &pos);
696
712
  assert(err == 0);
@@ -1354,29 +1370,102 @@ bare_fs_readlink_sync (js_env_t *env, js_callback_info_t *info) {
1354
1370
  err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
1355
1371
  assert(err == 0);
1356
1372
 
1357
- js_value_t *data = argv[1];
1358
-
1359
1373
  uv_loop_t *loop;
1360
1374
  js_get_env_loop(env, &loop);
1361
1375
 
1362
1376
  uv_fs_t req;
1363
1377
  uv_fs_readlink(loop, &req, (char *) path, NULL);
1364
1378
 
1365
- 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 {
1366
1384
  char *path;
1367
- 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);
1368
1386
  assert(err == 0);
1369
1387
 
1370
1388
  strncpy(path, req.ptr, sizeof(bare_fs_path_t));
1371
1389
  }
1372
1390
 
1373
- js_value_t *res;
1374
- 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);
1404
+ assert(err == 0);
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);
1375
1414
  assert(err == 0);
1376
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
+
1377
1466
  uv_fs_req_cleanup(&req);
1378
1467
 
1379
- return res;
1468
+ return NULL;
1380
1469
  }
1381
1470
 
1382
1471
  static js_value_t *
@@ -1649,6 +1738,16 @@ init (js_env_t *env, js_value_t *exports) {
1649
1738
  js_create_function(env, "readlinkSync", -1, bare_fs_readlink_sync, NULL, &fn);
1650
1739
  js_set_named_property(env, exports, "readlinkSync", fn);
1651
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
+ }
1652
1751
  {
1653
1752
  js_value_t *fn;
1654
1753
  js_create_function(env, "opendir", -1, bare_fs_opendir, NULL, &fn);
@@ -1728,6 +1827,9 @@ init (js_env_t *env, js_value_t *exports) {
1728
1827
  V(UV_DIRENT_SOCKET)
1729
1828
  V(UV_DIRENT_CHAR)
1730
1829
  V(UV_DIRENT_BLOCK)
1830
+
1831
+ V(UV_FS_SYMLINK_DIR)
1832
+ V(UV_FS_SYMLINK_JUNCTION)
1731
1833
  #undef V
1732
1834
 
1733
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.0",
3
+ "version": "1.8.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [