bare-fs 4.3.2 → 4.4.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.
package/index.d.ts CHANGED
@@ -1110,12 +1110,19 @@ export function watch(
1110
1110
 
1111
1111
  export function write(
1112
1112
  fd: number,
1113
- data: string | Buffer | ArrayBufferView,
1113
+ data: Buffer | ArrayBufferView,
1114
1114
  offset?: number,
1115
1115
  len?: number,
1116
1116
  pos?: number
1117
1117
  ): Promise<number>
1118
1118
 
1119
+ export function write(
1120
+ fd: number,
1121
+ data: string,
1122
+ pos?: number,
1123
+ encoding?: BufferEncoding
1124
+ ): Promise<number>
1125
+
1119
1126
  export function write(
1120
1127
  fd: number,
1121
1128
  data: Buffer | ArrayBufferView,
@@ -1136,7 +1143,7 @@ export function write(
1136
1143
  export function write(
1137
1144
  fd: number,
1138
1145
  data: string,
1139
- pos: string | number,
1146
+ pos: number,
1140
1147
  encoding: BufferEncoding,
1141
1148
  cb: Callback<[len: number]>
1142
1149
  ): void
@@ -1151,7 +1158,7 @@ export function write(
1151
1158
  export function write(
1152
1159
  fd: number,
1153
1160
  data: string,
1154
- pos: string | number,
1161
+ pos: number,
1155
1162
  cb: Callback<[len: number]>
1156
1163
  ): void
1157
1164
 
@@ -1169,12 +1176,19 @@ export function write(
1169
1176
 
1170
1177
  export function writeSync(
1171
1178
  fd: number,
1172
- data: string | Buffer | ArrayBufferView,
1179
+ data: Buffer | ArrayBufferView,
1173
1180
  offset?: number,
1174
1181
  len?: number,
1175
1182
  pos?: number
1176
1183
  ): number
1177
1184
 
1185
+ export function writeSync(
1186
+ fd: number,
1187
+ data: string,
1188
+ pos?: number,
1189
+ encoding?: BufferEncoding
1190
+ ): number
1191
+
1178
1192
  export interface WriteFileOptions {
1179
1193
  encoding?: BufferEncoding
1180
1194
  flag?: Flag
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ const FastFIFO = require('fast-fifo')
1
2
  const EventEmitter = require('bare-events')
2
3
  const path = require('bare-path')
3
4
  const { fileURLToPath } = require('bare-url')
@@ -11,9 +12,20 @@ const isWindows = Bare.platform === 'win32'
11
12
  exports.constants = constants
12
13
 
13
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
+
14
27
  constructor() {
15
28
  this._reset()
16
-
17
29
  this._handle = binding.requestInit(this, this._onresult)
18
30
  }
19
31
 
@@ -26,19 +38,40 @@ class FileRequest {
26
38
  }
27
39
 
28
40
  reset() {
29
- this._reset()
41
+ if (this._handle === null) return this
30
42
 
31
43
  binding.requestReset(this._handle)
44
+
45
+ this._reset()
46
+
47
+ return this
32
48
  }
33
49
 
34
50
  destroy() {
35
- this._reset()
51
+ if (this._handle === null) return this
36
52
 
37
53
  binding.requestDestroy(this._handle)
54
+
55
+ this._reset()
56
+ this._handle = null
57
+
58
+ return this
38
59
  }
39
60
 
40
61
  then(resolve, reject) {
41
- 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()
42
75
  }
43
76
 
44
77
  _reset() {
@@ -96,7 +129,7 @@ async function open(filepath, flags = 'r', mode = 0o666, cb) {
96
129
 
97
130
  filepath = toNamespacedPath(filepath)
98
131
 
99
- const req = new FileRequest()
132
+ using req = FileRequest.borrow()
100
133
 
101
134
  let fd
102
135
  let err = null
@@ -110,8 +143,6 @@ async function open(filepath, flags = 'r', mode = 0o666, cb) {
110
143
  code: e.code,
111
144
  path: filepath
112
145
  })
113
- } finally {
114
- req.destroy()
115
146
  }
116
147
 
117
148
  return done(err, fd, cb)
@@ -123,7 +154,7 @@ function openSync(filepath, flags = 'r', mode = 0o666) {
123
154
 
124
155
  filepath = toNamespacedPath(filepath)
125
156
 
126
- const req = new FileRequest()
157
+ using req = FileRequest.borrow()
127
158
 
128
159
  try {
129
160
  return binding.openSync(req.handle, filepath, flags, mode)
@@ -133,13 +164,11 @@ function openSync(filepath, flags = 'r', mode = 0o666) {
133
164
  code: e.code,
134
165
  path: filepath
135
166
  })
136
- } finally {
137
- req.destroy()
138
167
  }
139
168
  }
140
169
 
141
170
  async function close(fd, cb) {
142
- const req = new FileRequest()
171
+ using req = FileRequest.borrow()
143
172
 
144
173
  let err = null
145
174
  try {
@@ -148,22 +177,18 @@ async function close(fd, cb) {
148
177
  await req
149
178
  } catch (e) {
150
179
  err = new FileError(e.message, { operation: 'close', code: e.code, fd })
151
- } finally {
152
- req.destroy()
153
180
  }
154
181
 
155
182
  return done(err, cb)
156
183
  }
157
184
 
158
185
  function closeSync(fd) {
159
- const req = new FileRequest()
186
+ using req = FileRequest.borrow()
160
187
 
161
188
  try {
162
189
  binding.closeSync(req.handle, fd)
163
190
  } catch (e) {
164
191
  throw new FileError(e.message, { operation: 'close', code: e.code, fd })
165
- } finally {
166
- req.destroy()
167
192
  }
168
193
  }
169
194
 
@@ -175,7 +200,7 @@ async function access(filepath, mode = constants.F_OK, cb) {
175
200
 
176
201
  filepath = toNamespacedPath(filepath)
177
202
 
178
- const req = new FileRequest()
203
+ using req = FileRequest.borrow()
179
204
 
180
205
  let err = null
181
206
  try {
@@ -188,15 +213,13 @@ async function access(filepath, mode = constants.F_OK, cb) {
188
213
  code: e.code,
189
214
  path: filepath
190
215
  })
191
- } finally {
192
- req.destroy()
193
216
  }
194
217
 
195
218
  return done(err, cb)
196
219
  }
197
220
 
198
221
  function accessSync(filepath, mode = constants.F_OK) {
199
- const req = new FileRequest()
222
+ using req = FileRequest.borrow()
200
223
 
201
224
  filepath = toNamespacedPath(filepath)
202
225
 
@@ -208,8 +231,6 @@ function accessSync(filepath, mode = constants.F_OK) {
208
231
  code: e.code,
209
232
  path: filepath
210
233
  })
211
- } finally {
212
- req.destroy()
213
234
  }
214
235
  }
215
236
 
@@ -258,7 +279,7 @@ async function read(
258
279
 
259
280
  if (typeof pos !== 'number') pos = -1
260
281
 
261
- const req = new FileRequest()
282
+ using req = FileRequest.borrow()
262
283
 
263
284
  let bytes
264
285
  let err = null
@@ -268,8 +289,6 @@ async function read(
268
289
  bytes = await req
269
290
  } catch (e) {
270
291
  err = new FileError(e.message, { operation: 'read', code: e.code, fd })
271
- } finally {
272
- req.destroy()
273
292
  }
274
293
 
275
294
  return done(err, bytes, cb)
@@ -282,14 +301,12 @@ function readSync(
282
301
  len = buffer.byteLength - offset,
283
302
  pos = -1
284
303
  ) {
285
- const req = new FileRequest()
304
+ using req = FileRequest.borrow()
286
305
 
287
306
  try {
288
307
  return binding.readSync(req.handle, fd, buffer, offset, len, pos)
289
308
  } catch (e) {
290
309
  throw new FileError(e.message, { operation: 'read', code: e.code, fd })
291
- } finally {
292
- req.destroy()
293
310
  }
294
311
  }
295
312
 
@@ -301,7 +318,7 @@ async function readv(fd, buffers, pos = -1, cb) {
301
318
 
302
319
  if (typeof pos !== 'number') pos = -1
303
320
 
304
- const req = new FileRequest()
321
+ using req = FileRequest.borrow()
305
322
 
306
323
  let bytes
307
324
  let err = null
@@ -311,8 +328,6 @@ async function readv(fd, buffers, pos = -1, cb) {
311
328
  bytes = await req
312
329
  } catch (e) {
313
330
  err = new FileError(e.message, { operation: 'readv', code: e.code, fd })
314
- } finally {
315
- req.destroy()
316
331
  }
317
332
 
318
333
  return done(err, bytes, cb)
@@ -321,14 +336,12 @@ async function readv(fd, buffers, pos = -1, cb) {
321
336
  function readvSync(fd, buffers, pos = -1) {
322
337
  if (typeof pos !== 'number') pos = -1
323
338
 
324
- const req = new FileRequest()
339
+ using req = FileRequest.borrow()
325
340
 
326
341
  try {
327
342
  return binding.readvSync(req.handle, fd, buffers, pos)
328
343
  } catch (e) {
329
344
  throw new FileError(e.message, { operation: 'readv', code: e.code, fd })
330
- } finally {
331
- req.destroy()
332
345
  }
333
346
  }
334
347
 
@@ -372,7 +385,7 @@ async function write(fd, data, offset = 0, len, pos = -1, cb) {
372
385
  if (typeof len !== 'number') len = data.byteLength - offset
373
386
  if (typeof pos !== 'number') pos = -1
374
387
 
375
- const req = new FileRequest()
388
+ using req = FileRequest.borrow()
376
389
 
377
390
  let bytes
378
391
  let err = null
@@ -382,26 +395,35 @@ async function write(fd, data, offset = 0, len, pos = -1, cb) {
382
395
  bytes = await req
383
396
  } catch (e) {
384
397
  err = new FileError(e.message, { operation: 'write', code: e.code, fd })
385
- } finally {
386
- req.destroy()
387
398
  }
388
399
 
389
400
  return done(err, bytes, cb)
390
401
  }
391
402
 
392
403
  function writeSync(fd, data, offset = 0, len, pos = -1) {
393
- if (typeof data === 'string') data = Buffer.from(data)
404
+ if (typeof data === 'string') {
405
+ let encoding = len
406
+ pos = offset
407
+
408
+ if (typeof pos === 'string') {
409
+ encoding = pos
410
+ pos = -1
411
+ }
412
+
413
+ data = Buffer.from(data, encoding)
414
+ offset = 0
415
+ len = data.byteLength
416
+ }
417
+
394
418
  if (typeof len !== 'number') len = data.byteLength - offset
395
419
  if (typeof pos !== 'number') pos = -1
396
420
 
397
- const req = new FileRequest()
421
+ using req = FileRequest.borrow()
398
422
 
399
423
  try {
400
424
  return binding.writeSync(req.handle, fd, data, offset, len, pos)
401
425
  } catch (e) {
402
426
  throw new FileError(e.message, { operation: 'write', code: e.code, fd })
403
- } finally {
404
- req.destroy()
405
427
  }
406
428
  }
407
429
 
@@ -413,7 +435,7 @@ async function writev(fd, buffers, pos = -1, cb) {
413
435
 
414
436
  if (typeof pos !== 'number') pos = -1
415
437
 
416
- const req = new FileRequest()
438
+ using req = FileRequest.borrow()
417
439
 
418
440
  let bytes
419
441
  let err = null
@@ -423,8 +445,6 @@ async function writev(fd, buffers, pos = -1, cb) {
423
445
  bytes = await req
424
446
  } catch (e) {
425
447
  err = new FileError(e.message, { operation: 'writev', code: e.code, fd })
426
- } finally {
427
- req.destroy()
428
448
  }
429
449
 
430
450
  return done(err, bytes, cb)
@@ -433,21 +453,19 @@ async function writev(fd, buffers, pos = -1, cb) {
433
453
  function writevSync(fd, buffers, pos = -1) {
434
454
  if (typeof pos !== 'number') pos = -1
435
455
 
436
- const req = new FileRequest()
456
+ using req = FileRequest.borrow()
437
457
 
438
458
  try {
439
459
  return binding.writevSync(req.handle, fd, buffers, pos)
440
460
  } catch (e) {
441
461
  throw new FileError(e.message, { operation: 'writev', code: e.code, fd })
442
- } finally {
443
- req.destroy()
444
462
  }
445
463
  }
446
464
 
447
465
  async function stat(filepath, cb) {
448
466
  filepath = toNamespacedPath(filepath)
449
467
 
450
- const req = new FileRequest()
468
+ using req = FileRequest.borrow()
451
469
 
452
470
  let st
453
471
  let err = null
@@ -463,8 +481,6 @@ async function stat(filepath, cb) {
463
481
  code: e.code,
464
482
  path: filepath
465
483
  })
466
- } finally {
467
- req.destroy()
468
484
  }
469
485
 
470
486
  return done(err, st, cb)
@@ -473,7 +489,7 @@ async function stat(filepath, cb) {
473
489
  function statSync(filepath) {
474
490
  filepath = toNamespacedPath(filepath)
475
491
 
476
- const req = new FileRequest()
492
+ using req = FileRequest.borrow()
477
493
 
478
494
  try {
479
495
  binding.statSync(req.handle, filepath)
@@ -485,15 +501,13 @@ function statSync(filepath) {
485
501
  code: e.code,
486
502
  path: filepath
487
503
  })
488
- } finally {
489
- req.destroy()
490
504
  }
491
505
  }
492
506
 
493
507
  async function lstat(filepath, cb) {
494
508
  filepath = toNamespacedPath(filepath)
495
509
 
496
- const req = new FileRequest()
510
+ using req = FileRequest.borrow()
497
511
 
498
512
  let st
499
513
  let err = null
@@ -509,8 +523,6 @@ async function lstat(filepath, cb) {
509
523
  code: e.code,
510
524
  path: filepath
511
525
  })
512
- } finally {
513
- req.destroy()
514
526
  }
515
527
 
516
528
  return done(err, st, cb)
@@ -519,7 +531,7 @@ async function lstat(filepath, cb) {
519
531
  function lstatSync(filepath) {
520
532
  filepath = toNamespacedPath(filepath)
521
533
 
522
- const req = new FileRequest()
534
+ using req = FileRequest.borrow()
523
535
 
524
536
  try {
525
537
  binding.lstatSync(req.handle, filepath)
@@ -531,13 +543,11 @@ function lstatSync(filepath) {
531
543
  code: e.code,
532
544
  path: filepath
533
545
  })
534
- } finally {
535
- req.destroy()
536
546
  }
537
547
  }
538
548
 
539
549
  async function fstat(fd, cb) {
540
- const req = new FileRequest()
550
+ using req = FileRequest.borrow()
541
551
 
542
552
  let st
543
553
  let err = null
@@ -549,15 +559,13 @@ async function fstat(fd, cb) {
549
559
  st = new Stats(...binding.requestResultStat(req.handle))
550
560
  } catch (e) {
551
561
  err = new FileError(e.message, { operation: 'fstat', code: e.code, fd })
552
- } finally {
553
- req.destroy()
554
562
  }
555
563
 
556
564
  return done(err, st, cb)
557
565
  }
558
566
 
559
567
  function fstatSync(fd) {
560
- const req = new FileRequest()
568
+ using req = FileRequest.borrow()
561
569
 
562
570
  try {
563
571
  binding.fstatSync(req.handle, fd)
@@ -565,8 +573,6 @@ function fstatSync(fd) {
565
573
  return new Stats(...binding.requestResultStat(req.handle))
566
574
  } catch (e) {
567
575
  throw new FileError(e.message, { operation: 'fstat', code: e.code, fd })
568
- } finally {
569
- req.destroy()
570
576
  }
571
577
  }
572
578
 
@@ -578,7 +584,7 @@ async function ftruncate(fd, len = 0, cb) {
578
584
 
579
585
  if (typeof len !== 'number') len = 0
580
586
 
581
- const req = new FileRequest()
587
+ using req = FileRequest.borrow()
582
588
 
583
589
  let err = null
584
590
  try {
@@ -587,8 +593,6 @@ async function ftruncate(fd, len = 0, cb) {
587
593
  await req
588
594
  } catch (e) {
589
595
  err = new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
590
- } finally {
591
- req.destroy()
592
596
  }
593
597
 
594
598
  return done(err, cb)
@@ -597,14 +601,12 @@ async function ftruncate(fd, len = 0, cb) {
597
601
  function ftruncateSync(fd, len = 0) {
598
602
  if (typeof len !== 'number') len = 0
599
603
 
600
- const req = new FileRequest()
604
+ using req = FileRequest.borrow()
601
605
 
602
606
  try {
603
607
  binding.ftruncateSync(req.handle, fd, len)
604
608
  } catch (e) {
605
609
  throw new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
606
- } finally {
607
- req.destroy()
608
610
  }
609
611
  }
610
612
 
@@ -613,7 +615,7 @@ async function chmod(filepath, mode, cb) {
613
615
 
614
616
  filepath = toNamespacedPath(filepath)
615
617
 
616
- const req = new FileRequest()
618
+ using req = FileRequest.borrow()
617
619
 
618
620
  let err = null
619
621
  try {
@@ -626,8 +628,6 @@ async function chmod(filepath, mode, cb) {
626
628
  code: e.code,
627
629
  path: filepath
628
630
  })
629
- } finally {
630
- req.destroy()
631
631
  }
632
632
 
633
633
  return done(err, cb)
@@ -638,7 +638,7 @@ function chmodSync(filepath, mode) {
638
638
 
639
639
  filepath = toNamespacedPath(filepath)
640
640
 
641
- const req = new FileRequest()
641
+ using req = FileRequest.borrow()
642
642
 
643
643
  try {
644
644
  binding.chmodSync(req.handle, filepath, mode)
@@ -648,15 +648,13 @@ function chmodSync(filepath, mode) {
648
648
  code: e.code,
649
649
  path: filepath
650
650
  })
651
- } finally {
652
- req.destroy()
653
651
  }
654
652
  }
655
653
 
656
654
  async function fchmod(fd, mode, cb) {
657
655
  if (typeof mode === 'string') mode = toMode(mode)
658
656
 
659
- const req = new FileRequest()
657
+ using req = FileRequest.borrow()
660
658
 
661
659
  let err = null
662
660
  try {
@@ -665,8 +663,6 @@ async function fchmod(fd, mode, cb) {
665
663
  await req
666
664
  } catch (e) {
667
665
  err = new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
668
- } finally {
669
- req.destroy()
670
666
  }
671
667
 
672
668
  return done(err, cb)
@@ -675,14 +671,12 @@ async function fchmod(fd, mode, cb) {
675
671
  function fchmodSync(fd, mode) {
676
672
  if (typeof mode === 'string') mode = toMode(mode)
677
673
 
678
- const req = new FileRequest()
674
+ using req = FileRequest.borrow()
679
675
 
680
676
  try {
681
677
  binding.fchmodSync(req.handle, fd, mode)
682
678
  } catch (e) {
683
679
  throw new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
684
- } finally {
685
- req.destroy()
686
680
  }
687
681
  }
688
682
 
@@ -692,7 +686,7 @@ async function utimes(filepath, atime, mtime, cb) {
692
686
 
693
687
  filepath = toNamespacedPath(filepath)
694
688
 
695
- const req = new FileRequest()
689
+ using req = FileRequest.borrow()
696
690
 
697
691
  let err = null
698
692
  try {
@@ -705,8 +699,6 @@ async function utimes(filepath, atime, mtime, cb) {
705
699
  code: e.code,
706
700
  path: filepath
707
701
  })
708
- } finally {
709
- req.destroy()
710
702
  }
711
703
 
712
704
  return done(err, cb)
@@ -718,7 +710,7 @@ function utimesSync(filepath, atime, mtime) {
718
710
 
719
711
  filepath = toNamespacedPath(filepath)
720
712
 
721
- const req = new FileRequest()
713
+ using req = FileRequest.borrow()
722
714
 
723
715
  try {
724
716
  binding.utimesSync(req.handle, filepath, atime, mtime)
@@ -728,8 +720,6 @@ function utimesSync(filepath, atime, mtime) {
728
720
  code: e.code,
729
721
  path: filepath
730
722
  })
731
- } finally {
732
- req.destroy()
733
723
  }
734
724
  }
735
725
 
@@ -774,7 +764,7 @@ async function mkdir(filepath, opts, cb) {
774
764
  return ok(cb)
775
765
  }
776
766
 
777
- const req = new FileRequest()
767
+ using req = FileRequest.borrow()
778
768
 
779
769
  let err = null
780
770
  try {
@@ -787,8 +777,6 @@ async function mkdir(filepath, opts, cb) {
787
777
  code: e.code,
788
778
  path: filepath
789
779
  })
790
- } finally {
791
- req.destroy()
792
780
  }
793
781
 
794
782
  return done(err, cb)
@@ -830,7 +818,7 @@ function mkdirSync(filepath, opts) {
830
818
  return
831
819
  }
832
820
 
833
- const req = new FileRequest()
821
+ using req = FileRequest.borrow()
834
822
 
835
823
  try {
836
824
  binding.mkdirSync(req.handle, filepath, mode)
@@ -840,15 +828,13 @@ function mkdirSync(filepath, opts) {
840
828
  code: e.code,
841
829
  path: filepath
842
830
  })
843
- } finally {
844
- req.destroy()
845
831
  }
846
832
  }
847
833
 
848
834
  async function rmdir(filepath, cb) {
849
835
  filepath = toNamespacedPath(filepath)
850
836
 
851
- const req = new FileRequest()
837
+ using req = FileRequest.borrow()
852
838
 
853
839
  let err = null
854
840
  try {
@@ -861,8 +847,6 @@ async function rmdir(filepath, cb) {
861
847
  code: e.code,
862
848
  path: filepath
863
849
  })
864
- } finally {
865
- req.destroy()
866
850
  }
867
851
 
868
852
  return done(err, cb)
@@ -871,7 +855,7 @@ async function rmdir(filepath, cb) {
871
855
  function rmdirSync(filepath) {
872
856
  filepath = toNamespacedPath(filepath)
873
857
 
874
- const req = new FileRequest()
858
+ using req = FileRequest.borrow()
875
859
 
876
860
  try {
877
861
  binding.rmdirSync(req.handle, filepath)
@@ -881,8 +865,6 @@ function rmdirSync(filepath) {
881
865
  code: e.code,
882
866
  path: filepath
883
867
  })
884
- } finally {
885
- req.destroy()
886
868
  }
887
869
  }
888
870
 
@@ -973,7 +955,7 @@ function rmSync(filepath, opts) {
973
955
  async function unlink(filepath, cb) {
974
956
  filepath = toNamespacedPath(filepath)
975
957
 
976
- const req = new FileRequest()
958
+ using req = FileRequest.borrow()
977
959
 
978
960
  let err = null
979
961
  try {
@@ -986,8 +968,6 @@ async function unlink(filepath, cb) {
986
968
  code: e.code,
987
969
  path: filepath
988
970
  })
989
- } finally {
990
- req.destroy()
991
971
  }
992
972
 
993
973
  return done(err, cb)
@@ -996,7 +976,7 @@ async function unlink(filepath, cb) {
996
976
  function unlinkSync(filepath) {
997
977
  filepath = toNamespacedPath(filepath)
998
978
 
999
- const req = new FileRequest()
979
+ using req = FileRequest.borrow()
1000
980
 
1001
981
  try {
1002
982
  binding.unlinkSync(req.handle, filepath)
@@ -1006,8 +986,6 @@ function unlinkSync(filepath) {
1006
986
  code: e.code,
1007
987
  path: filepath
1008
988
  })
1009
- } finally {
1010
- req.destroy()
1011
989
  }
1012
990
  }
1013
991
 
@@ -1015,7 +993,7 @@ async function rename(src, dst, cb) {
1015
993
  src = toNamespacedPath(src)
1016
994
  dst = toNamespacedPath(dst)
1017
995
 
1018
- const req = new FileRequest()
996
+ using req = FileRequest.borrow()
1019
997
 
1020
998
  let err = null
1021
999
  try {
@@ -1029,8 +1007,6 @@ async function rename(src, dst, cb) {
1029
1007
  path: src,
1030
1008
  destination: dst
1031
1009
  })
1032
- } finally {
1033
- req.destroy()
1034
1010
  }
1035
1011
 
1036
1012
  return done(err, cb)
@@ -1040,7 +1016,7 @@ function renameSync(src, dst) {
1040
1016
  src = toNamespacedPath(src)
1041
1017
  dst = toNamespacedPath(dst)
1042
1018
 
1043
- const req = new FileRequest()
1019
+ using req = FileRequest.borrow()
1044
1020
 
1045
1021
  try {
1046
1022
  binding.renameSync(req.handle, src, dst)
@@ -1051,8 +1027,6 @@ function renameSync(src, dst) {
1051
1027
  path: src,
1052
1028
  destination: dst
1053
1029
  })
1054
- } finally {
1055
- req.destroy()
1056
1030
  }
1057
1031
  }
1058
1032
 
@@ -1065,7 +1039,7 @@ async function copyFile(src, dst, mode = 0, cb) {
1065
1039
  src = toNamespacedPath(src)
1066
1040
  dst = toNamespacedPath(dst)
1067
1041
 
1068
- const req = new FileRequest()
1042
+ using req = FileRequest.borrow()
1069
1043
 
1070
1044
  let err = null
1071
1045
  try {
@@ -1079,8 +1053,6 @@ async function copyFile(src, dst, mode = 0, cb) {
1079
1053
  path: src,
1080
1054
  destination: dst
1081
1055
  })
1082
- } finally {
1083
- req.destroy()
1084
1056
  }
1085
1057
 
1086
1058
  return done(err, cb)
@@ -1090,7 +1062,7 @@ function copyFileSync(src, dst, mode = 0) {
1090
1062
  src = toNamespacedPath(src)
1091
1063
  dst = toNamespacedPath(dst)
1092
1064
 
1093
- const req = new FileRequest()
1065
+ using req = FileRequest.borrow()
1094
1066
 
1095
1067
  try {
1096
1068
  binding.copyfileSync(req.handle, src, dst, mode)
@@ -1101,8 +1073,6 @@ function copyFileSync(src, dst, mode = 0) {
1101
1073
  path: src,
1102
1074
  destination: dst
1103
1075
  })
1104
- } finally {
1105
- req.destroy()
1106
1076
  }
1107
1077
  }
1108
1078
 
@@ -1119,7 +1089,7 @@ async function realpath(filepath, opts, cb) {
1119
1089
 
1120
1090
  filepath = toNamespacedPath(filepath)
1121
1091
 
1122
- const req = new FileRequest()
1092
+ using req = FileRequest.borrow()
1123
1093
 
1124
1094
  let res
1125
1095
  let err = null
@@ -1137,8 +1107,6 @@ async function realpath(filepath, opts, cb) {
1137
1107
  code: e.code,
1138
1108
  path: filepath
1139
1109
  })
1140
- } finally {
1141
- req.destroy()
1142
1110
  }
1143
1111
 
1144
1112
  return done(err, res, cb)
@@ -1152,7 +1120,7 @@ function realpathSync(filepath, opts) {
1152
1120
 
1153
1121
  filepath = toNamespacedPath(filepath)
1154
1122
 
1155
- const req = new FileRequest()
1123
+ using req = FileRequest.borrow()
1156
1124
 
1157
1125
  try {
1158
1126
  binding.realpathSync(req.handle, filepath)
@@ -1168,8 +1136,6 @@ function realpathSync(filepath, opts) {
1168
1136
  code: e.code,
1169
1137
  path: filepath
1170
1138
  })
1171
- } finally {
1172
- req.destroy()
1173
1139
  }
1174
1140
  }
1175
1141
 
@@ -1186,7 +1152,7 @@ async function readlink(filepath, opts, cb) {
1186
1152
 
1187
1153
  filepath = toNamespacedPath(filepath)
1188
1154
 
1189
- const req = new FileRequest()
1155
+ using req = FileRequest.borrow()
1190
1156
 
1191
1157
  let res
1192
1158
  let err = null
@@ -1204,8 +1170,6 @@ async function readlink(filepath, opts, cb) {
1204
1170
  code: e.code,
1205
1171
  path: filepath
1206
1172
  })
1207
- } finally {
1208
- req.destroy()
1209
1173
  }
1210
1174
 
1211
1175
  return done(err, res, cb)
@@ -1219,7 +1183,7 @@ function readlinkSync(filepath, opts) {
1219
1183
 
1220
1184
  filepath = toNamespacedPath(filepath)
1221
1185
 
1222
- const req = new FileRequest()
1186
+ using req = FileRequest.borrow()
1223
1187
 
1224
1188
  try {
1225
1189
  binding.readlinkSync(req.handle, filepath)
@@ -1235,8 +1199,6 @@ function readlinkSync(filepath, opts) {
1235
1199
  code: e.code,
1236
1200
  path: filepath
1237
1201
  })
1238
- } finally {
1239
- req.destroy()
1240
1202
  }
1241
1203
  }
1242
1204
 
@@ -1291,7 +1253,7 @@ async function symlink(target, filepath, type, cb) {
1291
1253
 
1292
1254
  target = normalizeSymlinkTarget(target)
1293
1255
 
1294
- const req = new FileRequest()
1256
+ using req = FileRequest.borrow()
1295
1257
 
1296
1258
  let err = null
1297
1259
  try {
@@ -1305,8 +1267,6 @@ async function symlink(target, filepath, type, cb) {
1305
1267
  path: target,
1306
1268
  destination: filepath
1307
1269
  })
1308
- } finally {
1309
- req.destroy()
1310
1270
  }
1311
1271
 
1312
1272
  return done(err, cb)
@@ -1346,7 +1306,7 @@ function symlinkSync(target, filepath, type) {
1346
1306
 
1347
1307
  target = normalizeSymlinkTarget(target)
1348
1308
 
1349
- const req = new FileRequest()
1309
+ using req = FileRequest.borrow()
1350
1310
 
1351
1311
  try {
1352
1312
  binding.symlinkSync(req.handle, target, filepath, type)
@@ -1357,8 +1317,6 @@ function symlinkSync(target, filepath, type) {
1357
1317
  path: target,
1358
1318
  destination: filepath
1359
1319
  })
1360
- } finally {
1361
- req.destroy()
1362
1320
  }
1363
1321
  }
1364
1322
 
@@ -1373,7 +1331,7 @@ async function opendir(filepath, opts, cb) {
1373
1331
 
1374
1332
  filepath = toNamespacedPath(filepath)
1375
1333
 
1376
- const req = new FileRequest()
1334
+ using req = FileRequest.borrow()
1377
1335
 
1378
1336
  let dir
1379
1337
  let err = null
@@ -1389,8 +1347,6 @@ async function opendir(filepath, opts, cb) {
1389
1347
  code: e.code,
1390
1348
  path: filepath
1391
1349
  })
1392
- } finally {
1393
- req.destroy()
1394
1350
  }
1395
1351
 
1396
1352
  return done(err, dir, cb)
@@ -1402,7 +1358,7 @@ function opendirSync(filepath, opts) {
1402
1358
 
1403
1359
  filepath = toNamespacedPath(filepath)
1404
1360
 
1405
- const req = new FileRequest()
1361
+ using req = FileRequest.borrow()
1406
1362
 
1407
1363
  try {
1408
1364
  binding.opendirSync(req.handle, filepath)
@@ -1414,8 +1370,6 @@ function opendirSync(filepath, opts) {
1414
1370
  code: e.code,
1415
1371
  path: filepath
1416
1372
  })
1417
- } finally {
1418
- req.destroy()
1419
1373
  }
1420
1374
  }
1421
1375
 
@@ -1741,7 +1695,7 @@ class Dir {
1741
1695
 
1742
1696
  this._encoding = encoding
1743
1697
  this._capacity = bufferSize
1744
- this._buffer = []
1698
+ this._buffer = new FastFIFO()
1745
1699
  this._ended = false
1746
1700
  this._handle = handle
1747
1701
  }
@@ -1750,7 +1704,7 @@ class Dir {
1750
1704
  if (this._buffer.length) return ok(this._buffer.shift(), cb)
1751
1705
  if (this._ended) return ok(null, cb)
1752
1706
 
1753
- const req = new FileRequest()
1707
+ using req = FileRequest.borrow()
1754
1708
 
1755
1709
  let entries
1756
1710
  let err = null
@@ -1766,8 +1720,6 @@ class Dir {
1766
1720
  code: e.code,
1767
1721
  path: this.path
1768
1722
  })
1769
- } finally {
1770
- req.destroy()
1771
1723
  }
1772
1724
 
1773
1725
  if (err) return fail(err, cb)
@@ -1793,7 +1745,7 @@ class Dir {
1793
1745
  if (this._buffer.length) return this._buffer.shift()
1794
1746
  if (this._ended) return null
1795
1747
 
1796
- const req = new FileRequest()
1748
+ using req = FileRequest.borrow()
1797
1749
 
1798
1750
  let entries
1799
1751
  try {
@@ -1806,8 +1758,6 @@ class Dir {
1806
1758
  code: e.code,
1807
1759
  path: this.path
1808
1760
  })
1809
- } finally {
1810
- req.destroy()
1811
1761
  }
1812
1762
 
1813
1763
  if (entries.length === 0) {
@@ -1828,7 +1778,7 @@ class Dir {
1828
1778
  }
1829
1779
 
1830
1780
  async close(cb) {
1831
- const req = new FileRequest()
1781
+ using req = FileRequest.borrow()
1832
1782
 
1833
1783
  let err = null
1834
1784
  try {
@@ -1841,8 +1791,6 @@ class Dir {
1841
1791
  code: e.code,
1842
1792
  path: this.path
1843
1793
  })
1844
- } finally {
1845
- req.destroy()
1846
1794
  }
1847
1795
 
1848
1796
  this._handle = null
@@ -1851,7 +1799,7 @@ class Dir {
1851
1799
  }
1852
1800
 
1853
1801
  closeSync() {
1854
- const req = new FileRequest()
1802
+ using req = FileRequest.borrow()
1855
1803
 
1856
1804
  try {
1857
1805
  binding.closedirSync(req.handle, this._handle)
@@ -1861,59 +1809,37 @@ class Dir {
1861
1809
  code: e.code,
1862
1810
  path: this.path
1863
1811
  })
1864
- } finally {
1865
- req.destroy()
1866
1812
  }
1867
1813
 
1868
1814
  this._handle = null
1869
1815
  }
1870
1816
 
1871
- [Symbol.iterator]() {
1872
- return {
1873
- next: () => {
1874
- if (this._buffer.length) {
1875
- return { done: false, value: this._buffer.shift() }
1876
- }
1877
-
1878
- if (this._ended) {
1879
- return { done: true }
1880
- }
1881
-
1882
- const entry = this.readSync()
1883
-
1884
- if (entry) {
1885
- return { done: false, value: entry }
1886
- }
1887
-
1888
- this.closeSync()
1889
-
1890
- return { done: true }
1891
- }
1892
- }
1817
+ [Symbol.dispose]() {
1818
+ this.closeSync()
1893
1819
  }
1894
1820
 
1895
- [Symbol.asyncIterator]() {
1896
- return {
1897
- next: async () => {
1898
- if (this._buffer.length) {
1899
- return { done: false, value: this._buffer.shift() }
1900
- }
1901
-
1902
- if (this._ended) {
1903
- return { done: true }
1904
- }
1905
-
1906
- const entry = await this.read()
1821
+ async [Symbol.asyncDispose]() {
1822
+ await this.close()
1823
+ }
1907
1824
 
1908
- if (entry) {
1909
- return { done: false, value: entry }
1910
- }
1825
+ *[Symbol.iterator]() {
1826
+ while (true) {
1827
+ const entry = this.readSync()
1828
+ if (entry === null) break
1829
+ yield entry
1830
+ }
1911
1831
 
1912
- await this.close()
1832
+ this.closeSync()
1833
+ }
1913
1834
 
1914
- return { done: true }
1915
- }
1835
+ async *[Symbol.asyncIterator]() {
1836
+ while (true) {
1837
+ const entry = await this.read()
1838
+ if (entry === null) break
1839
+ yield entry
1916
1840
  }
1841
+
1842
+ await this.close()
1917
1843
  }
1918
1844
  }
1919
1845
 
@@ -2222,14 +2148,24 @@ class Watcher extends EventEmitter {
2222
2148
  exports.access = access
2223
2149
  exports.appendFile = appendFile
2224
2150
  exports.chmod = chmod
2151
+ // exports.chown = chown TODO
2225
2152
  exports.close = close
2226
2153
  exports.copyFile = copyFile
2227
2154
  exports.exists = exists
2228
2155
  exports.fchmod = fchmod
2156
+ // exports.fchown = fchown TODO
2157
+ // exports.fdatasync = fdatasync TODO
2229
2158
  exports.fstat = fstat
2159
+ // exports.fsync = fsync TODO
2230
2160
  exports.ftruncate = ftruncate
2161
+ // exports.futimes = futimes TODO
2162
+ // exports.lchmod = lchmod TODO
2163
+ // exports.lchown = lchown TODO
2164
+ // exports.lutimes = lutimes TODO
2165
+ // exports.link = link TODO
2231
2166
  exports.lstat = lstat
2232
2167
  exports.mkdir = mkdir
2168
+ // exports.mkdtemp = mkdtemp TODO
2233
2169
  exports.open = open
2234
2170
  exports.opendir = opendir
2235
2171
  exports.read = read
@@ -2242,7 +2178,9 @@ exports.rename = rename
2242
2178
  exports.rm = rm
2243
2179
  exports.rmdir = rmdir
2244
2180
  exports.stat = stat
2181
+ // exports.statfs = statfs TODO
2245
2182
  exports.symlink = symlink
2183
+ // exports.truncate = truncate TODO
2246
2184
  exports.unlink = unlink
2247
2185
  exports.utimes = utimes
2248
2186
  exports.watch = watch
@@ -2253,14 +2191,24 @@ exports.writev = writev
2253
2191
  exports.accessSync = accessSync
2254
2192
  exports.appendFileSync = appendFileSync
2255
2193
  exports.chmodSync = chmodSync
2194
+ // exports.chownSync = chownSync TODO
2256
2195
  exports.closeSync = closeSync
2257
2196
  exports.copyFileSync = copyFileSync
2258
2197
  exports.existsSync = existsSync
2259
2198
  exports.fchmodSync = fchmodSync
2199
+ // exports.fchownSync = fchownSync TODO
2200
+ // exports.fdatasyncSync = fdatasyncSync TODO
2260
2201
  exports.fstatSync = fstatSync
2202
+ // exports.fsyncSync = fsyncSync TODO
2261
2203
  exports.ftruncateSync = ftruncateSync
2204
+ // exports.futimesSync = futimesSync TODO
2205
+ // exports.lchmodSync = lchmodSync TODO
2206
+ // exports.lchownSync = lchownSync TODO
2207
+ // exports.lutimesSync = lutimesSync TODO
2208
+ // exports.linkSync = linkSync TODO
2262
2209
  exports.lstatSync = lstatSync
2263
2210
  exports.mkdirSync = mkdirSync
2211
+ // exports.mkdtempSync = mkdtempSync TODO
2264
2212
  exports.openSync = openSync
2265
2213
  exports.opendirSync = opendirSync
2266
2214
  exports.readFileSync = readFileSync
@@ -2273,7 +2221,9 @@ exports.renameSync = renameSync
2273
2221
  exports.rmSync = rmSync
2274
2222
  exports.rmdirSync = rmdirSync
2275
2223
  exports.statSync = statSync
2224
+ // exports.statfsSync = statfsSync TODO
2276
2225
  exports.symlinkSync = symlinkSync
2226
+ // exports.truncateSync = truncateSync TODO
2277
2227
  exports.unlinkSync = unlinkSync
2278
2228
  exports.utimesSync = utimesSync
2279
2229
  exports.writeFileSync = writeFileSync
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "4.3.2",
3
+ "version": "4.4.0",
4
4
  "description": "Native file system operations for Javascript",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -49,7 +49,8 @@
49
49
  "bare-events": "^2.5.4",
50
50
  "bare-path": "^3.0.0",
51
51
  "bare-stream": "^2.6.4",
52
- "bare-url": "^2.2.2"
52
+ "bare-url": "^2.2.2",
53
+ "fast-fifo": "^1.3.2"
53
54
  },
54
55
  "devDependencies": {
55
56
  "bare-buffer": "^3.0.2",
@@ -59,15 +60,11 @@
59
60
  "prettier-config-standard": "^7.0.0"
60
61
  },
61
62
  "peerDependencies": {
62
- "bare-buffer": "*",
63
- "bare-url": "*"
63
+ "bare-buffer": "*"
64
64
  },
65
65
  "peerDependenciesMeta": {
66
66
  "bare-buffer": {
67
67
  "optional": true
68
- },
69
- "bare-url": {
70
- "optional": true
71
68
  }
72
69
  }
73
70
  }
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