bare-fs 4.3.3 → 4.4.1

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.
package/index.js CHANGED
@@ -12,9 +12,20 @@ const isWindows = Bare.platform === 'win32'
12
12
  exports.constants = constants
13
13
 
14
14
  class FileRequest {
15
+ static _free = []
16
+
17
+ static borrow() {
18
+ if (this._free.length > 0) return this._free.pop()
19
+ return new FileRequest()
20
+ }
21
+
22
+ static return(req) {
23
+ if (this._free.length < 32) this._free.push(req.reset())
24
+ else req.destroy()
25
+ }
26
+
15
27
  constructor() {
16
28
  this._reset()
17
-
18
29
  this._handle = binding.requestInit(this, this._onresult)
19
30
  }
20
31
 
@@ -27,19 +38,40 @@ class FileRequest {
27
38
  }
28
39
 
29
40
  reset() {
30
- this._reset()
41
+ if (this._handle === null) return this
31
42
 
32
43
  binding.requestReset(this._handle)
44
+
45
+ this._reset()
46
+
47
+ return this
33
48
  }
34
49
 
35
50
  destroy() {
36
- this._reset()
51
+ if (this._handle === null) return this
37
52
 
38
53
  binding.requestDestroy(this._handle)
54
+
55
+ this._reset()
56
+ this._handle = null
57
+
58
+ return this
39
59
  }
40
60
 
41
61
  then(resolve, reject) {
42
- this._promise.then(resolve, reject)
62
+ return this._promise.then(resolve, reject)
63
+ }
64
+
65
+ return() {
66
+ if (this._handle === null) return this
67
+
68
+ FileRequest.return(this)
69
+
70
+ return this
71
+ }
72
+
73
+ [Symbol.dispose]() {
74
+ this.return()
43
75
  }
44
76
 
45
77
  _reset() {
@@ -97,7 +129,7 @@ async function open(filepath, flags = 'r', mode = 0o666, cb) {
97
129
 
98
130
  filepath = toNamespacedPath(filepath)
99
131
 
100
- const req = new FileRequest()
132
+ using req = FileRequest.borrow()
101
133
 
102
134
  let fd
103
135
  let err = null
@@ -111,8 +143,6 @@ async function open(filepath, flags = 'r', mode = 0o666, cb) {
111
143
  code: e.code,
112
144
  path: filepath
113
145
  })
114
- } finally {
115
- req.destroy()
116
146
  }
117
147
 
118
148
  return done(err, fd, cb)
@@ -124,7 +154,7 @@ function openSync(filepath, flags = 'r', mode = 0o666) {
124
154
 
125
155
  filepath = toNamespacedPath(filepath)
126
156
 
127
- const req = new FileRequest()
157
+ using req = FileRequest.borrow()
128
158
 
129
159
  try {
130
160
  return binding.openSync(req.handle, filepath, flags, mode)
@@ -134,13 +164,11 @@ function openSync(filepath, flags = 'r', mode = 0o666) {
134
164
  code: e.code,
135
165
  path: filepath
136
166
  })
137
- } finally {
138
- req.destroy()
139
167
  }
140
168
  }
141
169
 
142
170
  async function close(fd, cb) {
143
- const req = new FileRequest()
171
+ using req = FileRequest.borrow()
144
172
 
145
173
  let err = null
146
174
  try {
@@ -149,22 +177,18 @@ async function close(fd, cb) {
149
177
  await req
150
178
  } catch (e) {
151
179
  err = new FileError(e.message, { operation: 'close', code: e.code, fd })
152
- } finally {
153
- req.destroy()
154
180
  }
155
181
 
156
182
  return done(err, cb)
157
183
  }
158
184
 
159
185
  function closeSync(fd) {
160
- const req = new FileRequest()
186
+ using req = FileRequest.borrow()
161
187
 
162
188
  try {
163
189
  binding.closeSync(req.handle, fd)
164
190
  } catch (e) {
165
191
  throw new FileError(e.message, { operation: 'close', code: e.code, fd })
166
- } finally {
167
- req.destroy()
168
192
  }
169
193
  }
170
194
 
@@ -176,7 +200,7 @@ async function access(filepath, mode = constants.F_OK, cb) {
176
200
 
177
201
  filepath = toNamespacedPath(filepath)
178
202
 
179
- const req = new FileRequest()
203
+ using req = FileRequest.borrow()
180
204
 
181
205
  let err = null
182
206
  try {
@@ -189,15 +213,13 @@ async function access(filepath, mode = constants.F_OK, cb) {
189
213
  code: e.code,
190
214
  path: filepath
191
215
  })
192
- } finally {
193
- req.destroy()
194
216
  }
195
217
 
196
218
  return done(err, cb)
197
219
  }
198
220
 
199
221
  function accessSync(filepath, mode = constants.F_OK) {
200
- const req = new FileRequest()
222
+ using req = FileRequest.borrow()
201
223
 
202
224
  filepath = toNamespacedPath(filepath)
203
225
 
@@ -209,8 +231,6 @@ function accessSync(filepath, mode = constants.F_OK) {
209
231
  code: e.code,
210
232
  path: filepath
211
233
  })
212
- } finally {
213
- req.destroy()
214
234
  }
215
235
  }
216
236
 
@@ -259,7 +279,7 @@ async function read(
259
279
 
260
280
  if (typeof pos !== 'number') pos = -1
261
281
 
262
- const req = new FileRequest()
282
+ using req = FileRequest.borrow()
263
283
 
264
284
  let bytes
265
285
  let err = null
@@ -269,8 +289,6 @@ async function read(
269
289
  bytes = await req
270
290
  } catch (e) {
271
291
  err = new FileError(e.message, { operation: 'read', code: e.code, fd })
272
- } finally {
273
- req.destroy()
274
292
  }
275
293
 
276
294
  return done(err, bytes, cb)
@@ -283,14 +301,12 @@ function readSync(
283
301
  len = buffer.byteLength - offset,
284
302
  pos = -1
285
303
  ) {
286
- const req = new FileRequest()
304
+ using req = FileRequest.borrow()
287
305
 
288
306
  try {
289
307
  return binding.readSync(req.handle, fd, buffer, offset, len, pos)
290
308
  } catch (e) {
291
309
  throw new FileError(e.message, { operation: 'read', code: e.code, fd })
292
- } finally {
293
- req.destroy()
294
310
  }
295
311
  }
296
312
 
@@ -302,7 +318,7 @@ async function readv(fd, buffers, pos = -1, cb) {
302
318
 
303
319
  if (typeof pos !== 'number') pos = -1
304
320
 
305
- const req = new FileRequest()
321
+ using req = FileRequest.borrow()
306
322
 
307
323
  let bytes
308
324
  let err = null
@@ -312,8 +328,6 @@ async function readv(fd, buffers, pos = -1, cb) {
312
328
  bytes = await req
313
329
  } catch (e) {
314
330
  err = new FileError(e.message, { operation: 'readv', code: e.code, fd })
315
- } finally {
316
- req.destroy()
317
331
  }
318
332
 
319
333
  return done(err, bytes, cb)
@@ -322,14 +336,12 @@ async function readv(fd, buffers, pos = -1, cb) {
322
336
  function readvSync(fd, buffers, pos = -1) {
323
337
  if (typeof pos !== 'number') pos = -1
324
338
 
325
- const req = new FileRequest()
339
+ using req = FileRequest.borrow()
326
340
 
327
341
  try {
328
342
  return binding.readvSync(req.handle, fd, buffers, pos)
329
343
  } catch (e) {
330
344
  throw new FileError(e.message, { operation: 'readv', code: e.code, fd })
331
- } finally {
332
- req.destroy()
333
345
  }
334
346
  }
335
347
 
@@ -373,7 +385,7 @@ async function write(fd, data, offset = 0, len, pos = -1, cb) {
373
385
  if (typeof len !== 'number') len = data.byteLength - offset
374
386
  if (typeof pos !== 'number') pos = -1
375
387
 
376
- const req = new FileRequest()
388
+ using req = FileRequest.borrow()
377
389
 
378
390
  let bytes
379
391
  let err = null
@@ -383,8 +395,6 @@ async function write(fd, data, offset = 0, len, pos = -1, cb) {
383
395
  bytes = await req
384
396
  } catch (e) {
385
397
  err = new FileError(e.message, { operation: 'write', code: e.code, fd })
386
- } finally {
387
- req.destroy()
388
398
  }
389
399
 
390
400
  return done(err, bytes, cb)
@@ -408,14 +418,12 @@ function writeSync(fd, data, offset = 0, len, pos = -1) {
408
418
  if (typeof len !== 'number') len = data.byteLength - offset
409
419
  if (typeof pos !== 'number') pos = -1
410
420
 
411
- const req = new FileRequest()
421
+ using req = FileRequest.borrow()
412
422
 
413
423
  try {
414
424
  return binding.writeSync(req.handle, fd, data, offset, len, pos)
415
425
  } catch (e) {
416
426
  throw new FileError(e.message, { operation: 'write', code: e.code, fd })
417
- } finally {
418
- req.destroy()
419
427
  }
420
428
  }
421
429
 
@@ -427,7 +435,7 @@ async function writev(fd, buffers, pos = -1, cb) {
427
435
 
428
436
  if (typeof pos !== 'number') pos = -1
429
437
 
430
- const req = new FileRequest()
438
+ using req = FileRequest.borrow()
431
439
 
432
440
  let bytes
433
441
  let err = null
@@ -437,8 +445,6 @@ async function writev(fd, buffers, pos = -1, cb) {
437
445
  bytes = await req
438
446
  } catch (e) {
439
447
  err = new FileError(e.message, { operation: 'writev', code: e.code, fd })
440
- } finally {
441
- req.destroy()
442
448
  }
443
449
 
444
450
  return done(err, bytes, cb)
@@ -447,21 +453,19 @@ async function writev(fd, buffers, pos = -1, cb) {
447
453
  function writevSync(fd, buffers, pos = -1) {
448
454
  if (typeof pos !== 'number') pos = -1
449
455
 
450
- const req = new FileRequest()
456
+ using req = FileRequest.borrow()
451
457
 
452
458
  try {
453
459
  return binding.writevSync(req.handle, fd, buffers, pos)
454
460
  } catch (e) {
455
461
  throw new FileError(e.message, { operation: 'writev', code: e.code, fd })
456
- } finally {
457
- req.destroy()
458
462
  }
459
463
  }
460
464
 
461
465
  async function stat(filepath, cb) {
462
466
  filepath = toNamespacedPath(filepath)
463
467
 
464
- const req = new FileRequest()
468
+ using req = FileRequest.borrow()
465
469
 
466
470
  let st
467
471
  let err = null
@@ -477,8 +481,6 @@ async function stat(filepath, cb) {
477
481
  code: e.code,
478
482
  path: filepath
479
483
  })
480
- } finally {
481
- req.destroy()
482
484
  }
483
485
 
484
486
  return done(err, st, cb)
@@ -487,7 +489,7 @@ async function stat(filepath, cb) {
487
489
  function statSync(filepath) {
488
490
  filepath = toNamespacedPath(filepath)
489
491
 
490
- const req = new FileRequest()
492
+ using req = FileRequest.borrow()
491
493
 
492
494
  try {
493
495
  binding.statSync(req.handle, filepath)
@@ -499,15 +501,13 @@ function statSync(filepath) {
499
501
  code: e.code,
500
502
  path: filepath
501
503
  })
502
- } finally {
503
- req.destroy()
504
504
  }
505
505
  }
506
506
 
507
507
  async function lstat(filepath, cb) {
508
508
  filepath = toNamespacedPath(filepath)
509
509
 
510
- const req = new FileRequest()
510
+ using req = FileRequest.borrow()
511
511
 
512
512
  let st
513
513
  let err = null
@@ -523,8 +523,6 @@ async function lstat(filepath, cb) {
523
523
  code: e.code,
524
524
  path: filepath
525
525
  })
526
- } finally {
527
- req.destroy()
528
526
  }
529
527
 
530
528
  return done(err, st, cb)
@@ -533,7 +531,7 @@ async function lstat(filepath, cb) {
533
531
  function lstatSync(filepath) {
534
532
  filepath = toNamespacedPath(filepath)
535
533
 
536
- const req = new FileRequest()
534
+ using req = FileRequest.borrow()
537
535
 
538
536
  try {
539
537
  binding.lstatSync(req.handle, filepath)
@@ -545,13 +543,11 @@ function lstatSync(filepath) {
545
543
  code: e.code,
546
544
  path: filepath
547
545
  })
548
- } finally {
549
- req.destroy()
550
546
  }
551
547
  }
552
548
 
553
549
  async function fstat(fd, cb) {
554
- const req = new FileRequest()
550
+ using req = FileRequest.borrow()
555
551
 
556
552
  let st
557
553
  let err = null
@@ -563,15 +559,13 @@ async function fstat(fd, cb) {
563
559
  st = new Stats(...binding.requestResultStat(req.handle))
564
560
  } catch (e) {
565
561
  err = new FileError(e.message, { operation: 'fstat', code: e.code, fd })
566
- } finally {
567
- req.destroy()
568
562
  }
569
563
 
570
564
  return done(err, st, cb)
571
565
  }
572
566
 
573
567
  function fstatSync(fd) {
574
- const req = new FileRequest()
568
+ using req = FileRequest.borrow()
575
569
 
576
570
  try {
577
571
  binding.fstatSync(req.handle, fd)
@@ -579,8 +573,6 @@ function fstatSync(fd) {
579
573
  return new Stats(...binding.requestResultStat(req.handle))
580
574
  } catch (e) {
581
575
  throw new FileError(e.message, { operation: 'fstat', code: e.code, fd })
582
- } finally {
583
- req.destroy()
584
576
  }
585
577
  }
586
578
 
@@ -592,7 +584,7 @@ async function ftruncate(fd, len = 0, cb) {
592
584
 
593
585
  if (typeof len !== 'number') len = 0
594
586
 
595
- const req = new FileRequest()
587
+ using req = FileRequest.borrow()
596
588
 
597
589
  let err = null
598
590
  try {
@@ -601,8 +593,6 @@ async function ftruncate(fd, len = 0, cb) {
601
593
  await req
602
594
  } catch (e) {
603
595
  err = new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
604
- } finally {
605
- req.destroy()
606
596
  }
607
597
 
608
598
  return done(err, cb)
@@ -611,14 +601,12 @@ async function ftruncate(fd, len = 0, cb) {
611
601
  function ftruncateSync(fd, len = 0) {
612
602
  if (typeof len !== 'number') len = 0
613
603
 
614
- const req = new FileRequest()
604
+ using req = FileRequest.borrow()
615
605
 
616
606
  try {
617
607
  binding.ftruncateSync(req.handle, fd, len)
618
608
  } catch (e) {
619
609
  throw new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
620
- } finally {
621
- req.destroy()
622
610
  }
623
611
  }
624
612
 
@@ -627,7 +615,7 @@ async function chmod(filepath, mode, cb) {
627
615
 
628
616
  filepath = toNamespacedPath(filepath)
629
617
 
630
- const req = new FileRequest()
618
+ using req = FileRequest.borrow()
631
619
 
632
620
  let err = null
633
621
  try {
@@ -640,8 +628,6 @@ async function chmod(filepath, mode, cb) {
640
628
  code: e.code,
641
629
  path: filepath
642
630
  })
643
- } finally {
644
- req.destroy()
645
631
  }
646
632
 
647
633
  return done(err, cb)
@@ -652,7 +638,7 @@ function chmodSync(filepath, mode) {
652
638
 
653
639
  filepath = toNamespacedPath(filepath)
654
640
 
655
- const req = new FileRequest()
641
+ using req = FileRequest.borrow()
656
642
 
657
643
  try {
658
644
  binding.chmodSync(req.handle, filepath, mode)
@@ -662,15 +648,13 @@ function chmodSync(filepath, mode) {
662
648
  code: e.code,
663
649
  path: filepath
664
650
  })
665
- } finally {
666
- req.destroy()
667
651
  }
668
652
  }
669
653
 
670
654
  async function fchmod(fd, mode, cb) {
671
655
  if (typeof mode === 'string') mode = toMode(mode)
672
656
 
673
- const req = new FileRequest()
657
+ using req = FileRequest.borrow()
674
658
 
675
659
  let err = null
676
660
  try {
@@ -679,8 +663,6 @@ async function fchmod(fd, mode, cb) {
679
663
  await req
680
664
  } catch (e) {
681
665
  err = new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
682
- } finally {
683
- req.destroy()
684
666
  }
685
667
 
686
668
  return done(err, cb)
@@ -689,14 +671,12 @@ async function fchmod(fd, mode, cb) {
689
671
  function fchmodSync(fd, mode) {
690
672
  if (typeof mode === 'string') mode = toMode(mode)
691
673
 
692
- const req = new FileRequest()
674
+ using req = FileRequest.borrow()
693
675
 
694
676
  try {
695
677
  binding.fchmodSync(req.handle, fd, mode)
696
678
  } catch (e) {
697
679
  throw new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
698
- } finally {
699
- req.destroy()
700
680
  }
701
681
  }
702
682
 
@@ -706,7 +686,7 @@ async function utimes(filepath, atime, mtime, cb) {
706
686
 
707
687
  filepath = toNamespacedPath(filepath)
708
688
 
709
- const req = new FileRequest()
689
+ using req = FileRequest.borrow()
710
690
 
711
691
  let err = null
712
692
  try {
@@ -719,8 +699,6 @@ async function utimes(filepath, atime, mtime, cb) {
719
699
  code: e.code,
720
700
  path: filepath
721
701
  })
722
- } finally {
723
- req.destroy()
724
702
  }
725
703
 
726
704
  return done(err, cb)
@@ -732,7 +710,7 @@ function utimesSync(filepath, atime, mtime) {
732
710
 
733
711
  filepath = toNamespacedPath(filepath)
734
712
 
735
- const req = new FileRequest()
713
+ using req = FileRequest.borrow()
736
714
 
737
715
  try {
738
716
  binding.utimesSync(req.handle, filepath, atime, mtime)
@@ -742,8 +720,6 @@ function utimesSync(filepath, atime, mtime) {
742
720
  code: e.code,
743
721
  path: filepath
744
722
  })
745
- } finally {
746
- req.destroy()
747
723
  }
748
724
  }
749
725
 
@@ -788,7 +764,7 @@ async function mkdir(filepath, opts, cb) {
788
764
  return ok(cb)
789
765
  }
790
766
 
791
- const req = new FileRequest()
767
+ using req = FileRequest.borrow()
792
768
 
793
769
  let err = null
794
770
  try {
@@ -801,8 +777,6 @@ async function mkdir(filepath, opts, cb) {
801
777
  code: e.code,
802
778
  path: filepath
803
779
  })
804
- } finally {
805
- req.destroy()
806
780
  }
807
781
 
808
782
  return done(err, cb)
@@ -844,7 +818,7 @@ function mkdirSync(filepath, opts) {
844
818
  return
845
819
  }
846
820
 
847
- const req = new FileRequest()
821
+ using req = FileRequest.borrow()
848
822
 
849
823
  try {
850
824
  binding.mkdirSync(req.handle, filepath, mode)
@@ -854,15 +828,13 @@ function mkdirSync(filepath, opts) {
854
828
  code: e.code,
855
829
  path: filepath
856
830
  })
857
- } finally {
858
- req.destroy()
859
831
  }
860
832
  }
861
833
 
862
834
  async function rmdir(filepath, cb) {
863
835
  filepath = toNamespacedPath(filepath)
864
836
 
865
- const req = new FileRequest()
837
+ using req = FileRequest.borrow()
866
838
 
867
839
  let err = null
868
840
  try {
@@ -875,8 +847,6 @@ async function rmdir(filepath, cb) {
875
847
  code: e.code,
876
848
  path: filepath
877
849
  })
878
- } finally {
879
- req.destroy()
880
850
  }
881
851
 
882
852
  return done(err, cb)
@@ -885,7 +855,7 @@ async function rmdir(filepath, cb) {
885
855
  function rmdirSync(filepath) {
886
856
  filepath = toNamespacedPath(filepath)
887
857
 
888
- const req = new FileRequest()
858
+ using req = FileRequest.borrow()
889
859
 
890
860
  try {
891
861
  binding.rmdirSync(req.handle, filepath)
@@ -895,8 +865,6 @@ function rmdirSync(filepath) {
895
865
  code: e.code,
896
866
  path: filepath
897
867
  })
898
- } finally {
899
- req.destroy()
900
868
  }
901
869
  }
902
870
 
@@ -987,7 +955,7 @@ function rmSync(filepath, opts) {
987
955
  async function unlink(filepath, cb) {
988
956
  filepath = toNamespacedPath(filepath)
989
957
 
990
- const req = new FileRequest()
958
+ using req = FileRequest.borrow()
991
959
 
992
960
  let err = null
993
961
  try {
@@ -1000,8 +968,6 @@ async function unlink(filepath, cb) {
1000
968
  code: e.code,
1001
969
  path: filepath
1002
970
  })
1003
- } finally {
1004
- req.destroy()
1005
971
  }
1006
972
 
1007
973
  return done(err, cb)
@@ -1010,7 +976,7 @@ async function unlink(filepath, cb) {
1010
976
  function unlinkSync(filepath) {
1011
977
  filepath = toNamespacedPath(filepath)
1012
978
 
1013
- const req = new FileRequest()
979
+ using req = FileRequest.borrow()
1014
980
 
1015
981
  try {
1016
982
  binding.unlinkSync(req.handle, filepath)
@@ -1020,8 +986,6 @@ function unlinkSync(filepath) {
1020
986
  code: e.code,
1021
987
  path: filepath
1022
988
  })
1023
- } finally {
1024
- req.destroy()
1025
989
  }
1026
990
  }
1027
991
 
@@ -1029,7 +993,7 @@ async function rename(src, dst, cb) {
1029
993
  src = toNamespacedPath(src)
1030
994
  dst = toNamespacedPath(dst)
1031
995
 
1032
- const req = new FileRequest()
996
+ using req = FileRequest.borrow()
1033
997
 
1034
998
  let err = null
1035
999
  try {
@@ -1043,8 +1007,6 @@ async function rename(src, dst, cb) {
1043
1007
  path: src,
1044
1008
  destination: dst
1045
1009
  })
1046
- } finally {
1047
- req.destroy()
1048
1010
  }
1049
1011
 
1050
1012
  return done(err, cb)
@@ -1054,7 +1016,7 @@ function renameSync(src, dst) {
1054
1016
  src = toNamespacedPath(src)
1055
1017
  dst = toNamespacedPath(dst)
1056
1018
 
1057
- const req = new FileRequest()
1019
+ using req = FileRequest.borrow()
1058
1020
 
1059
1021
  try {
1060
1022
  binding.renameSync(req.handle, src, dst)
@@ -1065,8 +1027,6 @@ function renameSync(src, dst) {
1065
1027
  path: src,
1066
1028
  destination: dst
1067
1029
  })
1068
- } finally {
1069
- req.destroy()
1070
1030
  }
1071
1031
  }
1072
1032
 
@@ -1079,7 +1039,7 @@ async function copyFile(src, dst, mode = 0, cb) {
1079
1039
  src = toNamespacedPath(src)
1080
1040
  dst = toNamespacedPath(dst)
1081
1041
 
1082
- const req = new FileRequest()
1042
+ using req = FileRequest.borrow()
1083
1043
 
1084
1044
  let err = null
1085
1045
  try {
@@ -1093,8 +1053,6 @@ async function copyFile(src, dst, mode = 0, cb) {
1093
1053
  path: src,
1094
1054
  destination: dst
1095
1055
  })
1096
- } finally {
1097
- req.destroy()
1098
1056
  }
1099
1057
 
1100
1058
  return done(err, cb)
@@ -1104,7 +1062,7 @@ function copyFileSync(src, dst, mode = 0) {
1104
1062
  src = toNamespacedPath(src)
1105
1063
  dst = toNamespacedPath(dst)
1106
1064
 
1107
- const req = new FileRequest()
1065
+ using req = FileRequest.borrow()
1108
1066
 
1109
1067
  try {
1110
1068
  binding.copyfileSync(req.handle, src, dst, mode)
@@ -1115,8 +1073,6 @@ function copyFileSync(src, dst, mode = 0) {
1115
1073
  path: src,
1116
1074
  destination: dst
1117
1075
  })
1118
- } finally {
1119
- req.destroy()
1120
1076
  }
1121
1077
  }
1122
1078
 
@@ -1133,7 +1089,7 @@ async function realpath(filepath, opts, cb) {
1133
1089
 
1134
1090
  filepath = toNamespacedPath(filepath)
1135
1091
 
1136
- const req = new FileRequest()
1092
+ using req = FileRequest.borrow()
1137
1093
 
1138
1094
  let res
1139
1095
  let err = null
@@ -1151,8 +1107,6 @@ async function realpath(filepath, opts, cb) {
1151
1107
  code: e.code,
1152
1108
  path: filepath
1153
1109
  })
1154
- } finally {
1155
- req.destroy()
1156
1110
  }
1157
1111
 
1158
1112
  return done(err, res, cb)
@@ -1166,7 +1120,7 @@ function realpathSync(filepath, opts) {
1166
1120
 
1167
1121
  filepath = toNamespacedPath(filepath)
1168
1122
 
1169
- const req = new FileRequest()
1123
+ using req = FileRequest.borrow()
1170
1124
 
1171
1125
  try {
1172
1126
  binding.realpathSync(req.handle, filepath)
@@ -1182,8 +1136,6 @@ function realpathSync(filepath, opts) {
1182
1136
  code: e.code,
1183
1137
  path: filepath
1184
1138
  })
1185
- } finally {
1186
- req.destroy()
1187
1139
  }
1188
1140
  }
1189
1141
 
@@ -1200,7 +1152,7 @@ async function readlink(filepath, opts, cb) {
1200
1152
 
1201
1153
  filepath = toNamespacedPath(filepath)
1202
1154
 
1203
- const req = new FileRequest()
1155
+ using req = FileRequest.borrow()
1204
1156
 
1205
1157
  let res
1206
1158
  let err = null
@@ -1218,8 +1170,6 @@ async function readlink(filepath, opts, cb) {
1218
1170
  code: e.code,
1219
1171
  path: filepath
1220
1172
  })
1221
- } finally {
1222
- req.destroy()
1223
1173
  }
1224
1174
 
1225
1175
  return done(err, res, cb)
@@ -1233,7 +1183,7 @@ function readlinkSync(filepath, opts) {
1233
1183
 
1234
1184
  filepath = toNamespacedPath(filepath)
1235
1185
 
1236
- const req = new FileRequest()
1186
+ using req = FileRequest.borrow()
1237
1187
 
1238
1188
  try {
1239
1189
  binding.readlinkSync(req.handle, filepath)
@@ -1249,8 +1199,6 @@ function readlinkSync(filepath, opts) {
1249
1199
  code: e.code,
1250
1200
  path: filepath
1251
1201
  })
1252
- } finally {
1253
- req.destroy()
1254
1202
  }
1255
1203
  }
1256
1204
 
@@ -1305,7 +1253,7 @@ async function symlink(target, filepath, type, cb) {
1305
1253
 
1306
1254
  target = normalizeSymlinkTarget(target)
1307
1255
 
1308
- const req = new FileRequest()
1256
+ using req = FileRequest.borrow()
1309
1257
 
1310
1258
  let err = null
1311
1259
  try {
@@ -1319,8 +1267,6 @@ async function symlink(target, filepath, type, cb) {
1319
1267
  path: target,
1320
1268
  destination: filepath
1321
1269
  })
1322
- } finally {
1323
- req.destroy()
1324
1270
  }
1325
1271
 
1326
1272
  return done(err, cb)
@@ -1360,7 +1306,7 @@ function symlinkSync(target, filepath, type) {
1360
1306
 
1361
1307
  target = normalizeSymlinkTarget(target)
1362
1308
 
1363
- const req = new FileRequest()
1309
+ using req = FileRequest.borrow()
1364
1310
 
1365
1311
  try {
1366
1312
  binding.symlinkSync(req.handle, target, filepath, type)
@@ -1371,8 +1317,6 @@ function symlinkSync(target, filepath, type) {
1371
1317
  path: target,
1372
1318
  destination: filepath
1373
1319
  })
1374
- } finally {
1375
- req.destroy()
1376
1320
  }
1377
1321
  }
1378
1322
 
@@ -1387,7 +1331,7 @@ async function opendir(filepath, opts, cb) {
1387
1331
 
1388
1332
  filepath = toNamespacedPath(filepath)
1389
1333
 
1390
- const req = new FileRequest()
1334
+ using req = FileRequest.borrow()
1391
1335
 
1392
1336
  let dir
1393
1337
  let err = null
@@ -1403,8 +1347,6 @@ async function opendir(filepath, opts, cb) {
1403
1347
  code: e.code,
1404
1348
  path: filepath
1405
1349
  })
1406
- } finally {
1407
- req.destroy()
1408
1350
  }
1409
1351
 
1410
1352
  return done(err, dir, cb)
@@ -1416,7 +1358,7 @@ function opendirSync(filepath, opts) {
1416
1358
 
1417
1359
  filepath = toNamespacedPath(filepath)
1418
1360
 
1419
- const req = new FileRequest()
1361
+ using req = FileRequest.borrow()
1420
1362
 
1421
1363
  try {
1422
1364
  binding.opendirSync(req.handle, filepath)
@@ -1428,8 +1370,6 @@ function opendirSync(filepath, opts) {
1428
1370
  code: e.code,
1429
1371
  path: filepath
1430
1372
  })
1431
- } finally {
1432
- req.destroy()
1433
1373
  }
1434
1374
  }
1435
1375
 
@@ -1530,10 +1470,7 @@ async function readFile(filepath, opts, cb) {
1530
1470
  } catch (err) {
1531
1471
  fail(err, cb)
1532
1472
  } finally {
1533
- if (fd === -1) return
1534
- try {
1535
- await close(fd)
1536
- } catch {}
1473
+ if (fd !== -1) await close(fd)
1537
1474
  }
1538
1475
  }
1539
1476
 
@@ -1580,10 +1517,7 @@ function readFileSync(filepath, opts) {
1580
1517
 
1581
1518
  return buffer
1582
1519
  } finally {
1583
- if (fd === -1) return
1584
- try {
1585
- closeSync(fd)
1586
- } catch {}
1520
+ if (fd !== -1) closeSync(fd)
1587
1521
  }
1588
1522
  }
1589
1523
 
@@ -1613,10 +1547,7 @@ async function writeFile(filepath, data, opts, cb) {
1613
1547
  } catch (err) {
1614
1548
  fail(err, cb)
1615
1549
  } finally {
1616
- if (fd === -1) return
1617
- try {
1618
- await close(fd)
1619
- } catch {}
1550
+ if (fd !== -1) await close(fd)
1620
1551
  }
1621
1552
  }
1622
1553
 
@@ -1637,10 +1568,7 @@ function writeFileSync(filepath, data, opts) {
1637
1568
  if (len === data.byteLength) break
1638
1569
  }
1639
1570
  } finally {
1640
- if (fd === -1) return
1641
- try {
1642
- closeSync(fd)
1643
- } catch {}
1571
+ if (fd !== -1) closeSync(fd)
1644
1572
  }
1645
1573
  }
1646
1574
 
@@ -1764,7 +1692,7 @@ class Dir {
1764
1692
  if (this._buffer.length) return ok(this._buffer.shift(), cb)
1765
1693
  if (this._ended) return ok(null, cb)
1766
1694
 
1767
- const req = new FileRequest()
1695
+ using req = FileRequest.borrow()
1768
1696
 
1769
1697
  let entries
1770
1698
  let err = null
@@ -1780,8 +1708,6 @@ class Dir {
1780
1708
  code: e.code,
1781
1709
  path: this.path
1782
1710
  })
1783
- } finally {
1784
- req.destroy()
1785
1711
  }
1786
1712
 
1787
1713
  if (err) return fail(err, cb)
@@ -1807,7 +1733,7 @@ class Dir {
1807
1733
  if (this._buffer.length) return this._buffer.shift()
1808
1734
  if (this._ended) return null
1809
1735
 
1810
- const req = new FileRequest()
1736
+ using req = FileRequest.borrow()
1811
1737
 
1812
1738
  let entries
1813
1739
  try {
@@ -1820,8 +1746,6 @@ class Dir {
1820
1746
  code: e.code,
1821
1747
  path: this.path
1822
1748
  })
1823
- } finally {
1824
- req.destroy()
1825
1749
  }
1826
1750
 
1827
1751
  if (entries.length === 0) {
@@ -1842,7 +1766,7 @@ class Dir {
1842
1766
  }
1843
1767
 
1844
1768
  async close(cb) {
1845
- const req = new FileRequest()
1769
+ using req = FileRequest.borrow()
1846
1770
 
1847
1771
  let err = null
1848
1772
  try {
@@ -1855,8 +1779,6 @@ class Dir {
1855
1779
  code: e.code,
1856
1780
  path: this.path
1857
1781
  })
1858
- } finally {
1859
- req.destroy()
1860
1782
  }
1861
1783
 
1862
1784
  this._handle = null
@@ -1865,7 +1787,7 @@ class Dir {
1865
1787
  }
1866
1788
 
1867
1789
  closeSync() {
1868
- const req = new FileRequest()
1790
+ using req = FileRequest.borrow()
1869
1791
 
1870
1792
  try {
1871
1793
  binding.closedirSync(req.handle, this._handle)
@@ -1875,8 +1797,6 @@ class Dir {
1875
1797
  code: e.code,
1876
1798
  path: this.path
1877
1799
  })
1878
- } finally {
1879
- req.destroy()
1880
1800
  }
1881
1801
 
1882
1802
  this._handle = null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "4.3.3",
3
+ "version": "4.4.1",
4
4
  "description": "Native file system operations for Javascript",
5
5
  "exports": {
6
6
  "./package": "./package.json",
package/promises.d.ts CHANGED
@@ -1,11 +1,15 @@
1
+ import EventEmitter, { EventMap } from 'bare-events'
1
2
  import Buffer, { BufferEncoding } from 'bare-buffer'
2
3
  import {
4
+ constants,
3
5
  AppendFileOptions,
4
6
  Dir,
5
7
  MkdirOptions,
6
8
  OpendirOptions,
7
9
  Path,
8
10
  ReadFileOptions,
11
+ ReadStream,
12
+ ReadStreamOptions,
9
13
  ReaddirOptions,
10
14
  ReadlinkOptions,
11
15
  RealpathOptions,
@@ -13,9 +17,57 @@ import {
13
17
  Stats,
14
18
  Watcher,
15
19
  WatcherOptions,
16
- WriteFileOptions
20
+ WriteFileOptions,
21
+ WriteStream,
22
+ WriteStreamOptions
17
23
  } from '.'
18
24
 
25
+ export { constants }
26
+
27
+ interface FileHandleEvents extends EventMap {
28
+ close: []
29
+ }
30
+
31
+ interface FileHandle extends EventEmitter<FileHandleEvents>, AsyncDisposable {
32
+ close(): Promise<void>
33
+
34
+ read(
35
+ buffer: Buffer | ArrayBufferView,
36
+ offset?: number,
37
+ len?: number,
38
+ pos?: number
39
+ ): Promise<number>
40
+
41
+ readv(buffers: ArrayBufferView[], position?: number): Promise<number>
42
+
43
+ write(
44
+ data: Buffer | ArrayBufferView,
45
+ offset?: number,
46
+ len?: number,
47
+ pos?: number
48
+ ): Promise<number>
49
+
50
+ write(data: string, pos?: number, encoding?: BufferEncoding): Promise<number>
51
+
52
+ stat(): Promise<Stats>
53
+
54
+ chmod(mode: string | number): Promise<void>
55
+
56
+ createReadStream(opts?: ReadStreamOptions): ReadStream
57
+
58
+ createWriteStream(opts?: WriteStreamOptions): WriteStream
59
+ }
60
+
61
+ declare class FileHandle {
62
+ private constructor(fd: number)
63
+ }
64
+
65
+ export function open(
66
+ filepath: Path,
67
+ flags?: Flag | number,
68
+ mode?: string | number
69
+ ): Promise<FileHandle>
70
+
19
71
  export function access(filepath: Path, mode?: number): Promise<void>
20
72
 
21
73
  export function appendFile(
package/promises.js CHANGED
@@ -1,8 +1,75 @@
1
+ const EventEmitter = require('bare-events')
1
2
  const fs = require('.')
2
3
 
4
+ class FileHandle extends EventEmitter {
5
+ constructor(fd) {
6
+ this.fd = fd
7
+ }
8
+
9
+ async close() {
10
+ await fs.close(fd)
11
+
12
+ this.fd = -1
13
+ this.emit('close')
14
+ }
15
+
16
+ async read(buffer, ...args) {
17
+ return {
18
+ bytesRead: await fs.read(this.fd, buffer, ...args),
19
+ buffer
20
+ }
21
+ }
22
+
23
+ async readv(buffers, ...args) {
24
+ return {
25
+ bytesRead: await fs.readv(this.fd, buffers, ...args),
26
+ buffers
27
+ }
28
+ }
29
+
30
+ async write(buffer, ...args) {
31
+ return {
32
+ bytesWritten: await fs.write(this.fd, buffer, ...args),
33
+ buffer
34
+ }
35
+ }
36
+
37
+ async writev(buffers, ...args) {
38
+ return {
39
+ bytesWritten: await fs.writev(this.fd, buffers, ...args),
40
+ buffers
41
+ }
42
+ }
43
+
44
+ async stat() {
45
+ return fs.fstat(this.fd)
46
+ }
47
+
48
+ async chmod(mode) {
49
+ await fs.fchmod(this.fd, mode)
50
+ }
51
+
52
+ createReadStream(opts) {
53
+ return fs.createReadStream(null, { ...opts, fd: this.fd })
54
+ }
55
+
56
+ createWriteStream(opts) {
57
+ return fs.createWriteStream(null, { ...opts, fd: this.fd })
58
+ }
59
+
60
+ async [Symbol.asyncDispose]() {
61
+ await this.close()
62
+ }
63
+ }
64
+
65
+ exports.open = async function open(filepath, flags, mode) {
66
+ return new FileHandle(await fs.open(filepath, flags, mode))
67
+ }
68
+
3
69
  exports.access = fs.access
4
70
  exports.appendFile = fs.appendFile
5
71
  exports.chmod = fs.chmod
72
+ exports.constants = fs.constants
6
73
  exports.copyFile = fs.copyFile
7
74
  exports.lstat = fs.lstat
8
75
  exports.mkdir = fs.mkdir
@@ -18,5 +85,5 @@ exports.stat = fs.stat
18
85
  exports.symlink = fs.symlink
19
86
  exports.unlink = fs.unlink
20
87
  exports.utimes = fs.utimes
21
- exports.writeFile = fs.writeFile
22
88
  exports.watch = fs.watch
89
+ exports.writeFile = fs.writeFile