bare-fs 1.13.2 → 2.0.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 (3) hide show
  1. package/binding.js +1 -1
  2. package/index.js +188 -184
  3. package/package.json +9 -3
package/binding.js CHANGED
@@ -1 +1 @@
1
- module.exports = process.addon(__dirname)
1
+ module.exports = require.addon()
package/index.js CHANGED
@@ -1,9 +1,11 @@
1
- const EventEmitter = require('events')
2
- const { sep, resolve, isAbsolute, toNamespacedPath } = require('path')
1
+ /* global Bare */
2
+ const EventEmitter = require('bare-events')
3
+ const os = require('bare-os')
4
+ const path = require('bare-path')
3
5
  const { Readable, Writable } = require('streamx')
4
6
  const binding = require('./binding')
5
7
 
6
- const isWindows = process.platform === 'win32'
8
+ const isWindows = os.platform() === 'win32'
7
9
 
8
10
  const constants = exports.constants = {
9
11
  O_RDWR: binding.O_RDWR,
@@ -59,7 +61,7 @@ const fs = {
59
61
 
60
62
  binding.init(fs.handle, fs, onresponse)
61
63
 
62
- process.on('exit', () => binding.destroy(fs.handle))
64
+ Bare.on('exit', () => binding.destroy(fs.handle))
63
65
 
64
66
  // Lightly-modified from the Node FS internal utils.
65
67
  function flagsToNumber (flags) {
@@ -143,9 +145,9 @@ function onresponse (id, err, result) {
143
145
  callback(err, result)
144
146
  }
145
147
 
146
- function open (path, flags, mode, cb) {
147
- if (typeof path !== 'string') {
148
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
148
+ function open (filepath, flags, mode, cb) {
149
+ if (typeof filepath !== 'string') {
150
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
149
151
  }
150
152
 
151
153
  if (typeof cb !== 'function') {
@@ -166,18 +168,18 @@ function open (path, flags, mode, cb) {
166
168
 
167
169
  const req = getReq()
168
170
  req.callback = cb
169
- binding.open(req.handle, path, flags, mode)
171
+ binding.open(req.handle, filepath, flags, mode)
170
172
  }
171
173
 
172
- function openSync (path, flags = 'r', mode = 0o666) {
173
- if (typeof path !== 'string') {
174
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
174
+ function openSync (filepath, flags = 'r', mode = 0o666) {
175
+ if (typeof filepath !== 'string') {
176
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
175
177
  }
176
178
 
177
179
  if (typeof flags === 'string') flags = flagsToNumber(flags)
178
180
  if (typeof mode === 'string') mode = modeToNumber(mode)
179
181
 
180
- return binding.openSync(path, flags, mode)
182
+ return binding.openSync(filepath, flags, mode)
181
183
  }
182
184
 
183
185
  function close (fd, cb = noop) {
@@ -210,9 +212,9 @@ function closeSync (fd) {
210
212
  return binding.closeSync(fd)
211
213
  }
212
214
 
213
- function access (path, mode, cb) {
214
- if (typeof path !== 'string') {
215
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
215
+ function access (filepath, mode, cb) {
216
+ if (typeof filepath !== 'string') {
217
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
216
218
  }
217
219
 
218
220
  if (typeof cb !== 'function') {
@@ -226,15 +228,15 @@ function access (path, mode, cb) {
226
228
 
227
229
  const req = getReq()
228
230
  req.callback = cb
229
- binding.access(req.handle, path, mode)
231
+ binding.access(req.handle, filepath, mode)
230
232
  }
231
233
 
232
- function accessSync (path, mode = constants.F_OK) {
233
- if (typeof path !== 'string') {
234
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
234
+ function accessSync (filepath, mode = constants.F_OK) {
235
+ if (typeof filepath !== 'string') {
236
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
235
237
  }
236
238
 
237
- binding.accessSync(path, mode)
239
+ binding.accessSync(filepath, mode)
238
240
  }
239
241
 
240
242
  function read (fd, buffer, offset, len, pos, cb) {
@@ -391,9 +393,9 @@ function writev (fd, buffers, pos, cb) {
391
393
  binding.writev(req.handle, fd, buffers, pos)
392
394
  }
393
395
 
394
- function stat (path, cb) {
395
- if (typeof path !== 'string') {
396
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
396
+ function stat (filepath, cb) {
397
+ if (typeof filepath !== 'string') {
398
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
397
399
  }
398
400
 
399
401
  if (typeof cb !== 'function') {
@@ -409,20 +411,20 @@ function stat (path, cb) {
409
411
  else cb(null, new Stats(...data))
410
412
  }
411
413
 
412
- binding.stat(req.handle, path, data)
414
+ binding.stat(req.handle, filepath, data)
413
415
  }
414
416
 
415
- function statSync (path) {
416
- if (typeof path !== 'string') {
417
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
417
+ function statSync (filepath) {
418
+ if (typeof filepath !== 'string') {
419
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
418
420
  }
419
421
 
420
- return new Stats(...binding.statSync(path))
422
+ return new Stats(...binding.statSync(filepath))
421
423
  }
422
424
 
423
- function lstat (path, cb) {
424
- if (typeof path !== 'string') {
425
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
425
+ function lstat (filepath, cb) {
426
+ if (typeof filepath !== 'string') {
427
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
426
428
  }
427
429
 
428
430
  if (typeof cb !== 'function') {
@@ -438,15 +440,15 @@ function lstat (path, cb) {
438
440
  else cb(null, new Stats(...data))
439
441
  }
440
442
 
441
- binding.lstat(req.handle, path, data)
443
+ binding.lstat(req.handle, filepath, data)
442
444
  }
443
445
 
444
- function lstatSync (path) {
445
- if (typeof path !== 'string') {
446
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
446
+ function lstatSync (filepath) {
447
+ if (typeof filepath !== 'string') {
448
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
447
449
  }
448
450
 
449
- return new Stats(...binding.lstatSync(path))
451
+ return new Stats(...binding.lstatSync(filepath))
450
452
  }
451
453
 
452
454
  function fstat (fd, cb) {
@@ -507,26 +509,26 @@ function ftruncate (fd, len, cb) {
507
509
  binding.ftruncate(req.handle, fd, len)
508
510
  }
509
511
 
510
- function chmod (path, mode, cb) {
511
- if (typeof path !== 'string') {
512
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
512
+ function chmod (filepath, mode, cb) {
513
+ if (typeof filepath !== 'string') {
514
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
513
515
  }
514
516
 
515
517
  if (typeof mode === 'string') mode = modeToNumber(mode)
516
518
 
517
519
  const req = getReq()
518
520
  req.callback = cb
519
- binding.chmod(req.handle, path, mode)
521
+ binding.chmod(req.handle, filepath, mode)
520
522
  }
521
523
 
522
- function chmodSync (path, mode) {
523
- if (typeof path !== 'string') {
524
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
524
+ function chmodSync (filepath, mode) {
525
+ if (typeof filepath !== 'string') {
526
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
525
527
  }
526
528
 
527
529
  if (typeof mode === 'string') mode = modeToNumber(mode)
528
530
 
529
- binding.chmodSync(path, mode)
531
+ binding.chmodSync(filepath, mode)
530
532
  }
531
533
 
532
534
  function fchmod (fd, mode, cb) {
@@ -559,12 +561,12 @@ function fchmodSync (fd, mode) {
559
561
  binding.fchmodSync(fd, mode)
560
562
  }
561
563
 
562
- function mkdirRecursive (path, mode, cb) {
563
- mkdir(path, { mode }, function (err) {
564
+ function mkdirRecursive (filepath, mode, cb) {
565
+ mkdir(filepath, { mode }, function (err) {
564
566
  if (err === null) return cb(null, 0, null)
565
567
 
566
568
  if (err.code !== 'ENOENT') {
567
- stat(path, function (e, st) {
569
+ stat(filepath, function (e, st) {
568
570
  if (e) return cb(e, e.errno, null)
569
571
  if (st.isDirectory()) return cb(null, 0, null)
570
572
  cb(err, err.errno, null)
@@ -572,20 +574,20 @@ function mkdirRecursive (path, mode, cb) {
572
574
  return
573
575
  }
574
576
 
575
- while (path.endsWith(sep)) path = path.slice(0, -1)
576
- const i = path.lastIndexOf(sep)
577
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
578
+ const i = filepath.lastIndexOf(path.sep)
577
579
  if (i <= 0) return cb(err, err.errno, null)
578
580
 
579
- mkdirRecursive(path.slice(0, i), mode, function (err) {
581
+ mkdirRecursive(filepath.slice(0, i), mode, function (err) {
580
582
  if (err) return cb(err, err.errno, null)
581
- mkdir(path, { mode }, cb)
583
+ mkdir(filepath, { mode }, cb)
582
584
  })
583
585
  })
584
586
  }
585
587
 
586
- function mkdir (path, opts, cb) {
587
- if (typeof path !== 'string') {
588
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
588
+ function mkdir (filepath, opts, cb) {
589
+ if (typeof filepath !== 'string') {
590
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
589
591
  }
590
592
 
591
593
  if (typeof opts === 'function') {
@@ -600,33 +602,33 @@ function mkdir (path, opts, cb) {
600
602
 
601
603
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
602
604
 
603
- if (opts.recursive) return mkdirRecursive(path, mode, cb)
605
+ if (opts.recursive) return mkdirRecursive(filepath, mode, cb)
604
606
 
605
607
  const req = getReq()
606
608
  req.callback = cb
607
- binding.mkdir(req.handle, path, mode)
609
+ binding.mkdir(req.handle, filepath, mode)
608
610
  }
609
611
 
610
- function mkdirResursiveSync (path, mode) {
612
+ function mkdirResursiveSync (filepath, mode) {
611
613
  try {
612
- mkdirSync(path, { mode })
614
+ mkdirSync(filepath, { mode })
613
615
  } catch (err) {
614
- if (err.code !== 'ENOENT' && statSync(path).isDirectory()) {
616
+ if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
615
617
  return
616
618
  }
617
619
 
618
- while (path.endsWith(sep)) path = path.slice(0, -1)
619
- const i = path.lastIndexOf(sep)
620
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
621
+ const i = filepath.lastIndexOf(path.sep)
620
622
  if (i <= 0) throw err
621
623
 
622
- mkdirResursiveSync(path.slice(0, i), { mode })
623
- mkdirSync(path, { mode })
624
+ mkdirResursiveSync(filepath.slice(0, i), { mode })
625
+ mkdirSync(filepath, { mode })
624
626
  }
625
627
  }
626
628
 
627
- function mkdirSync (path, opts) {
628
- if (typeof path !== 'string') {
629
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
629
+ function mkdirSync (filepath, opts) {
630
+ if (typeof filepath !== 'string') {
631
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
630
632
  }
631
633
 
632
634
  if (typeof opts === 'number') opts = { mode: opts }
@@ -634,14 +636,14 @@ function mkdirSync (path, opts) {
634
636
 
635
637
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
636
638
 
637
- if (opts.recursive) return mkdirResursiveSync(path, mode)
639
+ if (opts.recursive) return mkdirResursiveSync(filepath, mode)
638
640
 
639
- binding.mkdirSync(path, mode)
641
+ binding.mkdirSync(filepath, mode)
640
642
  }
641
643
 
642
- function rmdir (path, cb) {
643
- if (typeof path !== 'string') {
644
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
644
+ function rmdir (filepath, cb) {
645
+ if (typeof filepath !== 'string') {
646
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
645
647
  }
646
648
 
647
649
  if (typeof cb !== 'function') {
@@ -650,33 +652,33 @@ function rmdir (path, cb) {
650
652
 
651
653
  const req = getReq()
652
654
  req.callback = cb
653
- binding.rmdir(req.handle, path)
655
+ binding.rmdir(req.handle, filepath)
654
656
  }
655
657
 
656
- function rmdirSync (path) {
657
- if (typeof path !== 'string') {
658
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
658
+ function rmdirSync (filepath) {
659
+ if (typeof filepath !== 'string') {
660
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
659
661
  }
660
662
 
661
- binding.rmdirSync(path)
663
+ binding.rmdirSync(filepath)
662
664
  }
663
665
 
664
- function rmRecursive (path, opts, cb) {
665
- rmdir(path, function (err) {
666
+ function rmRecursive (filepath, opts, cb) {
667
+ rmdir(filepath, function (err) {
666
668
  if (err === null) return cb(null)
667
669
 
668
670
  if (err.code !== 'ENOTEMPTY') return cb(err)
669
671
 
670
- readdir(path, function (err, files) {
672
+ readdir(filepath, function (err, files) {
671
673
  if (err) return cb(err)
672
674
 
673
- if (files.length === 0) return rmdir(path, cb)
675
+ if (files.length === 0) return rmdir(filepath, cb)
674
676
 
675
677
  let missing = files.length
676
678
  let done = false
677
679
 
678
680
  for (const file of files) {
679
- rm(path + sep + file, opts, function (err) {
681
+ rm(filepath + path.sep + file, opts, function (err) {
680
682
  if (done) return
681
683
 
682
684
  if (err) {
@@ -684,16 +686,16 @@ function rmRecursive (path, opts, cb) {
684
686
  return cb(err)
685
687
  }
686
688
 
687
- if (--missing === 0) rmdir(path, cb)
689
+ if (--missing === 0) rmdir(filepath, cb)
688
690
  })
689
691
  }
690
692
  })
691
693
  })
692
694
  }
693
695
 
694
- function rm (path, opts, cb) {
695
- if (typeof path !== 'string') {
696
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
696
+ function rm (filepath, opts, cb) {
697
+ if (typeof filepath !== 'string') {
698
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
697
699
  }
698
700
 
699
701
  if (typeof opts === 'function') {
@@ -705,66 +707,66 @@ function rm (path, opts, cb) {
705
707
 
706
708
  if (!opts) opts = {}
707
709
 
708
- lstat(path, function (err, st) {
710
+ lstat(filepath, function (err, st) {
709
711
  if (err) {
710
712
  return cb(err.code === 'ENOENT' && opts.force ? null : err)
711
713
  }
712
714
 
713
715
  if (st.isDirectory()) {
714
- if (opts.recursive) return rmRecursive(path, opts, cb)
716
+ if (opts.recursive) return rmRecursive(filepath, opts, cb)
715
717
 
716
718
  const err = new Error('is a directory')
717
719
  err.code = 'EISDIR'
718
720
  return cb(err)
719
721
  }
720
722
 
721
- unlink(path, cb)
723
+ unlink(filepath, cb)
722
724
  })
723
725
  }
724
726
 
725
- function rmRecursiveSync (path, opts) {
727
+ function rmRecursiveSync (filepath, opts) {
726
728
  try {
727
- rmdirSync(path)
729
+ rmdirSync(filepath)
728
730
  } catch (err) {
729
731
  if (err.code !== 'ENOTEMPTY') throw err
730
732
 
731
- const files = readdirSync(path)
733
+ const files = readdirSync(filepath)
732
734
 
733
735
  for (const file of files) {
734
- rmSync(path + sep + file, opts)
736
+ rmSync(filepath + path.sep + file, opts)
735
737
  }
736
738
 
737
- rmdirSync(path)
739
+ rmdirSync(filepath)
738
740
  }
739
741
  }
740
742
 
741
- function rmSync (path, opts) {
742
- if (typeof path !== 'string') {
743
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
743
+ function rmSync (filepath, opts) {
744
+ if (typeof filepath !== 'string') {
745
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
744
746
  }
745
747
 
746
748
  if (!opts) opts = {}
747
749
 
748
750
  try {
749
- const st = lstatSync(path)
751
+ const st = lstatSync(filepath)
750
752
 
751
753
  if (st.isDirectory()) {
752
- if (opts.recursive) return rmRecursiveSync(path, opts)
754
+ if (opts.recursive) return rmRecursiveSync(filepath, opts)
753
755
 
754
756
  const err = new Error('is a directory')
755
757
  err.code = 'EISDIR'
756
758
  throw err
757
759
  }
758
760
 
759
- unlinkSync(path)
761
+ unlinkSync(filepath)
760
762
  } catch (err) {
761
763
  if (err.code !== 'ENOENT' || !opts.force) throw err
762
764
  }
763
765
  }
764
766
 
765
- function unlink (path, cb) {
766
- if (typeof path !== 'string') {
767
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
767
+ function unlink (filepath, cb) {
768
+ if (typeof filepath !== 'string') {
769
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
768
770
  }
769
771
 
770
772
  if (typeof cb !== 'function') {
@@ -773,15 +775,15 @@ function unlink (path, cb) {
773
775
 
774
776
  const req = getReq()
775
777
  req.callback = cb
776
- binding.unlink(req.handle, path)
778
+ binding.unlink(req.handle, filepath)
777
779
  }
778
780
 
779
- function unlinkSync (path) {
780
- if (typeof path !== 'string') {
781
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
781
+ function unlinkSync (filepath) {
782
+ if (typeof filepath !== 'string') {
783
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
782
784
  }
783
785
 
784
- binding.unlinkSync(path)
786
+ binding.unlinkSync(filepath)
785
787
  }
786
788
 
787
789
  function rename (src, dst, cb) {
@@ -814,9 +816,9 @@ function renameSync (src, dst) {
814
816
  binding.renameSync(src, dst)
815
817
  }
816
818
 
817
- function realpath (path, opts, cb) {
818
- if (typeof path !== 'string') {
819
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
819
+ function realpath (filepath, opts, cb) {
820
+ if (typeof filepath !== 'string') {
821
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
820
822
  }
821
823
 
822
824
  if (typeof opts === 'function') {
@@ -844,12 +846,12 @@ function realpath (path, opts, cb) {
844
846
  cb(null, path)
845
847
  }
846
848
 
847
- binding.realpath(req.handle, path, data)
849
+ binding.realpath(req.handle, filepath, data)
848
850
  }
849
851
 
850
- function realpathSync (path, opts) {
851
- if (typeof path !== 'string') {
852
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
852
+ function realpathSync (filepath, opts) {
853
+ if (typeof filepath !== 'string') {
854
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
853
855
  }
854
856
 
855
857
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -861,16 +863,16 @@ function realpathSync (path, opts) {
861
863
 
862
864
  const data = Buffer.allocUnsafe(binding.sizeofFSPath)
863
865
 
864
- binding.realpathSync(path, data)
866
+ binding.realpathSync(filepath, data)
865
867
 
866
- path = data.subarray(0, data.indexOf(0))
867
- if (encoding !== 'buffer') path = path.toString(encoding)
868
- return path
868
+ filepath = data.subarray(0, data.indexOf(0))
869
+ if (encoding !== 'buffer') filepath = filepath.toString(encoding)
870
+ return filepath
869
871
  }
870
872
 
871
- function readlink (path, opts, cb) {
872
- if (typeof path !== 'string') {
873
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
873
+ function readlink (filepath, opts, cb) {
874
+ if (typeof filepath !== 'string') {
875
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
874
876
  }
875
877
 
876
878
  if (typeof opts === 'function') {
@@ -898,12 +900,12 @@ function readlink (path, opts, cb) {
898
900
  cb(null, path)
899
901
  }
900
902
 
901
- binding.readlink(req.handle, path, data)
903
+ binding.readlink(req.handle, filepath, data)
902
904
  }
903
905
 
904
- function readlinkSync (path, opts) {
905
- if (typeof path !== 'string') {
906
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
906
+ function readlinkSync (filepath, opts) {
907
+ if (typeof filepath !== 'string') {
908
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
907
909
  }
908
910
 
909
911
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -915,32 +917,32 @@ function readlinkSync (path, opts) {
915
917
 
916
918
  const data = Buffer.allocUnsafe(binding.sizeofFSPath)
917
919
 
918
- binding.readlinkSync(path, data)
920
+ binding.readlinkSync(filepath, data)
919
921
 
920
- path = data.subarray(0, data.indexOf(0))
921
- if (encoding !== 'buffer') path = path.toString(encoding)
922
- return path
922
+ filepath = data.subarray(0, data.indexOf(0))
923
+ if (encoding !== 'buffer') filepath = filepath.toString(encoding)
924
+ return filepath
923
925
  }
924
926
 
925
- function normalizeSymlinkTarget (target, type, path) {
927
+ function normalizeSymlinkTarget (target, type, filepath) {
926
928
  if (isWindows) {
927
- if (type === 'junction') target = resolve(path, '..', target)
929
+ if (type === 'junction') target = path.resolve(filepath, '..', target)
928
930
 
929
- if (isAbsolute(target)) return toNamespacedPath(target)
931
+ if (path.isAbsolute(target)) return path.toNamespacedPath(target)
930
932
 
931
- return target.replace(/\//g, sep)
933
+ return target.replace(/\//g, path.sep)
932
934
  }
933
935
 
934
936
  return target
935
937
  }
936
938
 
937
- function symlink (target, path, type, cb) {
939
+ function symlink (target, filepath, type, cb) {
938
940
  if (typeof target !== 'string') {
939
- throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof path) + ' (' + path + ')')
941
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
940
942
  }
941
943
 
942
- if (typeof path !== 'string') {
943
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
944
+ if (typeof filepath !== 'string') {
945
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
944
946
  }
945
947
 
946
948
  if (typeof type === 'function') {
@@ -966,12 +968,12 @@ function symlink (target, path, type, cb) {
966
968
  }
967
969
  } else if (typeof type !== 'number') {
968
970
  if (isWindows) {
969
- target = resolve(path, '..', target)
971
+ target = path.resolve(filepath, '..', target)
970
972
 
971
973
  stat(target, (err, st) => {
972
974
  type = err === null && st.isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
973
975
 
974
- symlink(target, path, type, path, cb)
976
+ symlink(target, filepath, type, filepath, cb)
975
977
  })
976
978
 
977
979
  return
@@ -982,16 +984,16 @@ function symlink (target, path, type, cb) {
982
984
 
983
985
  const req = getReq()
984
986
  req.callback = cb
985
- binding.symlink(req.handle, normalizeSymlinkTarget(target), toNamespacedPath(path), type)
987
+ binding.symlink(req.handle, normalizeSymlinkTarget(target), path.toNamespacedPath(filepath), type)
986
988
  }
987
989
 
988
- function symlinkSync (target, path, type) {
990
+ function symlinkSync (target, filepath, type) {
989
991
  if (typeof target !== 'string') {
990
- throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof path) + ' (' + path + ')')
992
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
991
993
  }
992
994
 
993
- if (typeof path !== 'string') {
994
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
995
+ if (typeof filepath !== 'string') {
996
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
995
997
  }
996
998
 
997
999
  if (typeof type === 'string') {
@@ -1010,7 +1012,7 @@ function symlinkSync (target, path, type) {
1010
1012
  }
1011
1013
  } else if (typeof type !== 'number') {
1012
1014
  if (isWindows) {
1013
- target = resolve(path, '..', target)
1015
+ target = path.resolve(filepath, '..', target)
1014
1016
 
1015
1017
  type = statSync(target).isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
1016
1018
  } else {
@@ -1018,12 +1020,12 @@ function symlinkSync (target, path, type) {
1018
1020
  }
1019
1021
  }
1020
1022
 
1021
- binding.symlinkSync(normalizeSymlinkTarget(target), toNamespacedPath(path), type)
1023
+ binding.symlinkSync(normalizeSymlinkTarget(target), path.toNamespacedPath(filepath), type)
1022
1024
  }
1023
1025
 
1024
- function opendir (path, opts, cb) {
1025
- if (typeof path !== 'string') {
1026
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1026
+ function opendir (filepath, opts, cb) {
1027
+ if (typeof filepath !== 'string') {
1028
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1027
1029
  }
1028
1030
 
1029
1031
  if (typeof opts === 'function') {
@@ -1042,28 +1044,28 @@ function opendir (path, opts, cb) {
1042
1044
 
1043
1045
  req.callback = function (err, _) {
1044
1046
  if (err) return cb(err, null)
1045
- cb(null, new Dir(path, data, opts))
1047
+ cb(null, new Dir(filepath, data, opts))
1046
1048
  }
1047
1049
 
1048
- binding.opendir(req.handle, path, data)
1050
+ binding.opendir(req.handle, filepath, data)
1049
1051
  }
1050
1052
 
1051
- function opendirSync (path, opts) {
1052
- if (typeof path !== 'string') {
1053
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1053
+ function opendirSync (filepath, opts) {
1054
+ if (typeof filepath !== 'string') {
1055
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1054
1056
  }
1055
1057
 
1056
1058
  if (typeof opts === 'string') opts = { encoding: opts }
1057
1059
  else if (!opts) opts = {}
1058
1060
 
1059
1061
  const data = Buffer.allocUnsafe(binding.sizeofFSDir)
1060
- binding.opendirSync(path, data)
1061
- return new Dir(path, data, opts)
1062
+ binding.opendirSync(filepath, data)
1063
+ return new Dir(filepath, data, opts)
1062
1064
  }
1063
1065
 
1064
- function readdir (path, opts, cb) {
1065
- if (typeof path !== 'string') {
1066
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1066
+ function readdir (filepath, opts, cb) {
1067
+ if (typeof filepath !== 'string') {
1068
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1067
1069
  }
1068
1070
 
1069
1071
  if (typeof opts === 'function') {
@@ -1080,7 +1082,7 @@ function readdir (path, opts, cb) {
1080
1082
  withFileTypes = false
1081
1083
  } = opts
1082
1084
 
1083
- opendir(path, opts, async (err, dir) => {
1085
+ opendir(filepath, opts, async (err, dir) => {
1084
1086
  if (err) return cb(err, null)
1085
1087
  const result = []
1086
1088
  for await (const entry of dir) {
@@ -1090,9 +1092,9 @@ function readdir (path, opts, cb) {
1090
1092
  })
1091
1093
  }
1092
1094
 
1093
- function readdirSync (path, opts) {
1094
- if (typeof path !== 'string') {
1095
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1095
+ function readdirSync (filepath, opts) {
1096
+ if (typeof filepath !== 'string') {
1097
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1096
1098
  }
1097
1099
 
1098
1100
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1102,7 +1104,7 @@ function readdirSync (path, opts) {
1102
1104
  withFileTypes = false
1103
1105
  } = opts
1104
1106
 
1105
- const dir = opendirSync(path, opts)
1107
+ const dir = opendirSync(filepath, opts)
1106
1108
  const result = []
1107
1109
 
1108
1110
  while (true) {
@@ -1114,9 +1116,9 @@ function readdirSync (path, opts) {
1114
1116
  return result
1115
1117
  }
1116
1118
 
1117
- function readFile (path, opts, cb) {
1118
- if (typeof path !== 'string') {
1119
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1119
+ function readFile (filepath, opts, cb) {
1120
+ if (typeof filepath !== 'string') {
1121
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1120
1122
  }
1121
1123
 
1122
1124
  if (typeof opts === 'function') {
@@ -1133,7 +1135,7 @@ function readFile (path, opts, cb) {
1133
1135
  encoding = 'buffer'
1134
1136
  } = opts
1135
1137
 
1136
- open(path, opts.flag || 'r', function (err, fd) {
1138
+ open(filepath, opts.flag || 'r', function (err, fd) {
1137
1139
  if (err) return cb(err)
1138
1140
 
1139
1141
  fstat(fd, function (err, st) {
@@ -1169,9 +1171,9 @@ function readFile (path, opts, cb) {
1169
1171
  })
1170
1172
  }
1171
1173
 
1172
- function readFileSync (path, opts) {
1173
- if (typeof path !== 'string') {
1174
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1174
+ function readFileSync (filepath, opts) {
1175
+ if (typeof filepath !== 'string') {
1176
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1175
1177
  }
1176
1178
 
1177
1179
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1181,7 +1183,7 @@ function readFileSync (path, opts) {
1181
1183
  encoding = 'buffer'
1182
1184
  } = opts
1183
1185
 
1184
- const fd = openSync(path, opts.flag || 'r')
1186
+ const fd = openSync(filepath, opts.flag || 'r')
1185
1187
 
1186
1188
  try {
1187
1189
  const st = fstatSync(fd)
@@ -1205,9 +1207,9 @@ function readFileSync (path, opts) {
1205
1207
  }
1206
1208
  }
1207
1209
 
1208
- function writeFile (path, data, opts, cb) {
1209
- if (typeof path !== 'string') {
1210
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1210
+ function writeFile (filepath, data, opts, cb) {
1211
+ if (typeof filepath !== 'string') {
1212
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1211
1213
  }
1212
1214
 
1213
1215
  if (typeof data !== 'string' && !Buffer.isBuffer(data) && !ArrayBuffer.isView(data)) {
@@ -1226,7 +1228,7 @@ function writeFile (path, data, opts, cb) {
1226
1228
 
1227
1229
  if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
1228
1230
 
1229
- open(path, opts.flag || 'w', opts.mode || 0o666, function (err, fd) {
1231
+ open(filepath, opts.flag || 'w', opts.mode || 0o666, function (err, fd) {
1230
1232
  if (err) return cb(err)
1231
1233
 
1232
1234
  write(fd, data, loop)
@@ -1252,9 +1254,9 @@ function writeFile (path, data, opts, cb) {
1252
1254
  })
1253
1255
  }
1254
1256
 
1255
- function writeFileSync (path, data, opts) {
1256
- if (typeof path !== 'string') {
1257
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1257
+ function writeFileSync (filepath, data, opts) {
1258
+ if (typeof filepath !== 'string') {
1259
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1258
1260
  }
1259
1261
 
1260
1262
  if (typeof data !== 'string' && !Buffer.isBuffer(data) && !ArrayBuffer.isView(data)) {
@@ -1266,7 +1268,7 @@ function writeFileSync (path, data, opts) {
1266
1268
 
1267
1269
  if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
1268
1270
 
1269
- const fd = openSync(path, opts.flag || 'w', opts.mode)
1271
+ const fd = openSync(filepath, opts.flag || 'w', opts.mode)
1270
1272
 
1271
1273
  try {
1272
1274
  let len = 0
@@ -1282,9 +1284,9 @@ function writeFileSync (path, data, opts) {
1282
1284
  }
1283
1285
  }
1284
1286
 
1285
- function watch (path, opts, cb) {
1286
- if (typeof path !== 'string') {
1287
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
1287
+ function watch (filepath, opts, cb) {
1288
+ if (typeof filepath !== 'string') {
1289
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
1288
1290
  }
1289
1291
 
1290
1292
  if (typeof opts === 'function') {
@@ -1295,7 +1297,7 @@ function watch (path, opts, cb) {
1295
1297
  if (typeof opts === 'string') opts = { encoding: opts }
1296
1298
  else if (!opts) opts = {}
1297
1299
 
1298
- const watcher = new Watcher(path, opts)
1300
+ const watcher = new Watcher(filepath, opts)
1299
1301
  if (cb) watcher.on('change', cb)
1300
1302
  return watcher
1301
1303
  }
@@ -1658,6 +1660,8 @@ class Watcher extends EventEmitter {
1658
1660
  }
1659
1661
 
1660
1662
  _onclose () {
1663
+ this._handle = null
1664
+
1661
1665
  this.emit('close')
1662
1666
  }
1663
1667
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "1.13.2",
3
+ "version": "2.0.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -11,12 +11,15 @@
11
11
  "CMakeLists.txt",
12
12
  "prebuilds"
13
13
  ],
14
+ "imports": {
15
+ "events": "bare-events"
16
+ },
14
17
  "scripts": {
15
- "test": "standard && bare test.js"
18
+ "test": "standard && bare -m package.json test.js"
16
19
  },
17
20
  "repository": {
18
21
  "type": "git",
19
- "url": "https://github.com/holepunchto/bare-fs.git"
22
+ "url": "git+https://github.com/holepunchto/bare-fs.git"
20
23
  },
21
24
  "author": "Holepunch",
22
25
  "license": "Apache-2.0",
@@ -25,6 +28,9 @@
25
28
  },
26
29
  "homepage": "https://github.com/holepunchto/bare-fs#readme",
27
30
  "dependencies": {
31
+ "bare-events": "^2.0.0",
32
+ "bare-os": "^2.0.0",
33
+ "bare-path": "^2.0.0",
28
34
  "streamx": "^2.13.0"
29
35
  },
30
36
  "devDependencies": {