bare-fs 4.4.6 → 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 +29 -40
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -742,24 +742,18 @@ async function mkdir(filepath, opts, cb) {
742
742
  try {
743
743
  await mkdir(filepath, { mode })
744
744
  } catch (err) {
745
- if (err.code !== 'ENOENT' && (await stat(filepath)).isDirectory()) {
746
- return ok(cb)
747
- }
748
-
749
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
750
- const i = filepath.lastIndexOf(path.sep)
751
- if (i <= 0) return fail(err, cb)
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
752
749
 
753
- await mkdir(filepath.slice(0, i), { mode, recursive: true })
750
+ await mkdir(filepath.slice(0, i), { mode, recursive: true })
754
751
 
755
- try {
756
- await mkdir(filepath, { mode })
757
- } catch (err) {
758
- if ((await stat(filepath)).isDirectory()) {
759
- return ok(cb)
752
+ try {
753
+ await mkdir(filepath, { mode })
754
+ } catch (err) {
755
+ if (!(await stat(filepath)).isDirectory()) throw err
760
756
  }
761
-
762
- return fail(err, cb)
763
757
  }
764
758
  }
765
759
  } catch (e) {
@@ -799,24 +793,18 @@ function mkdirSync(filepath, opts) {
799
793
  try {
800
794
  mkdirSync(filepath, { mode })
801
795
  } catch (err) {
802
- if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
803
- return
804
- }
805
-
806
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
807
- const i = filepath.lastIndexOf(path.sep)
808
- if (i <= 0) throw err
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
809
800
 
810
- mkdirSync(filepath.slice(0, i), { mode, recursive: true })
801
+ mkdirSync(filepath.slice(0, i), { mode, recursive: true })
811
802
 
812
- try {
813
- mkdirSync(filepath, { mode })
814
- } catch (err) {
815
- if (statSync(filepath).isDirectory()) {
816
- return
803
+ try {
804
+ mkdirSync(filepath, { mode })
805
+ } catch (err) {
806
+ if (!statSync(filepath).isDirectory()) throw err
817
807
  }
818
-
819
- throw err
820
808
  }
821
809
  }
822
810
 
@@ -1437,12 +1425,13 @@ async function readFile(filepath, opts, cb) {
1437
1425
  const { encoding = 'buffer' } = opts
1438
1426
 
1439
1427
  let fd = -1
1428
+ let buffer = null
1429
+ let err = null
1440
1430
  try {
1441
1431
  fd = await open(filepath, opts.flag || 'r')
1442
1432
 
1443
1433
  const st = await fstat(fd)
1444
1434
 
1445
- let buffer
1446
1435
  let len = 0
1447
1436
 
1448
1437
  if (st.size === 0) {
@@ -1470,13 +1459,13 @@ async function readFile(filepath, opts, cb) {
1470
1459
  }
1471
1460
 
1472
1461
  if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1473
-
1474
- return ok(buffer, cb)
1475
- } catch (err) {
1476
- fail(err, cb)
1462
+ } catch (e) {
1463
+ err = e
1477
1464
  } finally {
1478
1465
  if (fd !== -1) await close(fd)
1479
1466
  }
1467
+
1468
+ return done(err, buffer, cb)
1480
1469
  }
1481
1470
 
1482
1471
  function readFileSync(filepath, opts) {
@@ -1538,22 +1527,22 @@ async function writeFile(filepath, data, opts, cb) {
1538
1527
  if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
1539
1528
 
1540
1529
  let fd = -1
1530
+ let len = 0
1531
+ let err = null
1541
1532
  try {
1542
1533
  fd = await open(filepath, opts.flag || 'w', opts.mode || 0o666)
1543
1534
 
1544
- let len = 0
1545
-
1546
1535
  while (true) {
1547
1536
  len += await write(fd, len ? data.subarray(len) : data)
1548
1537
  if (len === data.byteLength) break
1549
1538
  }
1550
-
1551
- return ok(cb)
1552
1539
  } catch (err) {
1553
- fail(err, cb)
1540
+ err = e
1554
1541
  } finally {
1555
1542
  if (fd !== -1) await close(fd)
1556
1543
  }
1544
+
1545
+ return done(err, len, cb)
1557
1546
  }
1558
1547
 
1559
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.6",
3
+ "version": "4.4.7",
4
4
  "description": "Native file system operations for Javascript",
5
5
  "exports": {
6
6
  "./package": "./package.json",