bare-fs 4.4.5 → 4.4.7

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 (2) hide show
  1. package/index.js +46 -52
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -737,31 +737,30 @@ async function mkdir(filepath, opts, cb) {
737
737
  filepath = toNamespacedPath(filepath)
738
738
 
739
739
  if (opts.recursive) {
740
+ let err = null
740
741
  try {
741
- await mkdir(filepath, { mode })
742
- } catch (err) {
743
- if (err.code !== 'ENOENT' && (await stat(filepath)).isDirectory()) {
744
- return ok(cb)
745
- }
746
-
747
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
748
- const i = filepath.lastIndexOf(path.sep)
749
- if (i <= 0) return fail(err, cb)
750
-
751
- await mkdir(filepath.slice(0, i), { mode, recursive: true })
752
-
753
742
  try {
754
743
  await mkdir(filepath, { mode })
755
744
  } catch (err) {
756
- if ((await stat(filepath)).isDirectory()) {
757
- return ok(cb)
758
- }
745
+ if (err.code === 'ENOENT' || !(await stat(filepath)).isDirectory()) {
746
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
747
+ const i = filepath.lastIndexOf(path.sep)
748
+ if (i <= 0) throw err
749
+
750
+ await mkdir(filepath.slice(0, i), { mode, recursive: true })
759
751
 
760
- return fail(err, cb)
752
+ try {
753
+ await mkdir(filepath, { mode })
754
+ } catch (err) {
755
+ if (!(await stat(filepath)).isDirectory()) throw err
756
+ }
757
+ }
761
758
  }
759
+ } catch (e) {
760
+ err = e
762
761
  }
763
762
 
764
- return ok(cb)
763
+ return done(err, cb)
765
764
  }
766
765
 
767
766
  using req = FileRequest.borrow()
@@ -794,24 +793,18 @@ function mkdirSync(filepath, opts) {
794
793
  try {
795
794
  mkdirSync(filepath, { mode })
796
795
  } catch (err) {
797
- if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
798
- return
799
- }
796
+ if (err.code === 'ENOENT' || !statSync(filepath).isDirectory()) {
797
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
798
+ const i = filepath.lastIndexOf(path.sep)
799
+ if (i <= 0) throw err
800
800
 
801
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
802
- const i = filepath.lastIndexOf(path.sep)
803
- if (i <= 0) throw err
801
+ mkdirSync(filepath.slice(0, i), { mode, recursive: true })
804
802
 
805
- mkdirSync(filepath.slice(0, i), { mode, recursive: true })
806
-
807
- try {
808
- mkdirSync(filepath, { mode })
809
- } catch (err) {
810
- if (statSync(filepath).isDirectory()) {
811
- return
803
+ try {
804
+ mkdirSync(filepath, { mode })
805
+ } catch (err) {
806
+ if (!statSync(filepath).isDirectory()) throw err
812
807
  }
813
-
814
- throw err
815
808
  }
816
809
  }
817
810
 
@@ -878,6 +871,7 @@ async function rm(filepath, opts, cb) {
878
871
 
879
872
  filepath = toNamespacedPath(filepath)
880
873
 
874
+ let err = null
881
875
  try {
882
876
  const st = await lstat(filepath)
883
877
 
@@ -897,21 +891,20 @@ async function rm(filepath, opts, cb) {
897
891
  await rmdir(filepath)
898
892
  }
899
893
  } else {
900
- fail(
901
- new FileError('is a directory', {
902
- operation: 'rm',
903
- code: 'EISDIR',
904
- path: filepath
905
- }),
906
- cb
907
- )
894
+ throw new FileError('is a directory', {
895
+ operation: 'rm',
896
+ code: 'EISDIR',
897
+ path: filepath
898
+ })
908
899
  }
909
900
  } else {
910
901
  await unlink(filepath)
911
902
  }
912
- } catch (err) {
913
- if (err.code !== 'ENOENT' || !opts.force) fail(err, cb)
903
+ } catch (e) {
904
+ if (e.code !== 'ENOENT' || !opts.force) err = e
914
905
  }
906
+
907
+ return done(err, cb)
915
908
  }
916
909
 
917
910
  function rmSync(filepath, opts) {
@@ -1432,12 +1425,13 @@ async function readFile(filepath, opts, cb) {
1432
1425
  const { encoding = 'buffer' } = opts
1433
1426
 
1434
1427
  let fd = -1
1428
+ let buffer = null
1429
+ let err = null
1435
1430
  try {
1436
1431
  fd = await open(filepath, opts.flag || 'r')
1437
1432
 
1438
1433
  const st = await fstat(fd)
1439
1434
 
1440
- let buffer
1441
1435
  let len = 0
1442
1436
 
1443
1437
  if (st.size === 0) {
@@ -1465,13 +1459,13 @@ async function readFile(filepath, opts, cb) {
1465
1459
  }
1466
1460
 
1467
1461
  if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1468
-
1469
- return ok(buffer, cb)
1470
- } catch (err) {
1471
- fail(err, cb)
1462
+ } catch (e) {
1463
+ err = e
1472
1464
  } finally {
1473
1465
  if (fd !== -1) await close(fd)
1474
1466
  }
1467
+
1468
+ return done(err, buffer, cb)
1475
1469
  }
1476
1470
 
1477
1471
  function readFileSync(filepath, opts) {
@@ -1533,22 +1527,22 @@ async function writeFile(filepath, data, opts, cb) {
1533
1527
  if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
1534
1528
 
1535
1529
  let fd = -1
1530
+ let len = 0
1531
+ let err = null
1536
1532
  try {
1537
1533
  fd = await open(filepath, opts.flag || 'w', opts.mode || 0o666)
1538
1534
 
1539
- let len = 0
1540
-
1541
1535
  while (true) {
1542
1536
  len += await write(fd, len ? data.subarray(len) : data)
1543
1537
  if (len === data.byteLength) break
1544
1538
  }
1545
-
1546
- return ok(cb)
1547
1539
  } catch (err) {
1548
- fail(err, cb)
1540
+ err = e
1549
1541
  } finally {
1550
1542
  if (fd !== -1) await close(fd)
1551
1543
  }
1544
+
1545
+ return done(err, len, cb)
1552
1546
  }
1553
1547
 
1554
1548
  function writeFileSync(filepath, data, opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "4.4.5",
3
+ "version": "4.4.7",
4
4
  "description": "Native file system operations for Javascript",
5
5
  "exports": {
6
6
  "./package": "./package.json",