bare-fs 1.5.3

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 ADDED
@@ -0,0 +1,854 @@
1
+ const { Readable, Writable } = require('streamx')
2
+ const binding = require('./binding')
3
+
4
+ const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff
5
+
6
+ const sep = process.platform === 'win32' ? '\\' : '/'
7
+
8
+ const constants = exports.constants = {
9
+ O_RDWR: binding.O_RDWR,
10
+ O_RDONLY: binding.O_RDONLY,
11
+ O_WRONLY: binding.O_WRONLY,
12
+ O_CREAT: binding.O_CREAT,
13
+ O_TRUNC: binding.O_TRUNC,
14
+ O_APPEND: binding.O_APPEND,
15
+ S_IFMT: binding.S_IFMT,
16
+ S_IFREG: binding.S_IFREG,
17
+ S_IFDIR: binding.S_IFDIR,
18
+ S_IFCHR: binding.S_IFCHR,
19
+ S_IFLNK: binding.S_IFLNK,
20
+ S_IFBLK: binding.S_IFBLK || 0,
21
+ S_IFIFO: binding.S_IFIFO || 0,
22
+ S_IFSOCK: binding.S_IFSOCK || 0
23
+ }
24
+
25
+ const reqs = []
26
+ let used = 0
27
+
28
+ const fs = {
29
+ handle: Buffer.allocUnsafe(binding.sizeofFS)
30
+ }
31
+
32
+ binding.init(fs.handle, fs, onfsresponse)
33
+
34
+ process.on('exit', () => binding.destroy(fs.handle))
35
+
36
+ // Lightly-modified from the Node FS internal utils.
37
+ function flagsToNumber (flags) {
38
+ switch (flags) {
39
+ case 'r' : return constants.O_RDONLY
40
+ case 'rs' : // Fall through.
41
+ case 'sr' : return constants.O_RDONLY | constants.O_SYNC
42
+ case 'r+' : return constants.O_RDWR
43
+ case 'rs+' : // Fall through.
44
+ case 'sr+' : return constants.O_RDWR | constants.O_SYNC
45
+
46
+ case 'w' : return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
47
+ case 'wx' : // Fall through.
48
+ case 'xw' : return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
49
+
50
+ case 'w+' : return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
51
+ case 'wx+': // Fall through.
52
+ case 'xw+': return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
53
+
54
+ case 'a' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
55
+ case 'ax' : // Fall through.
56
+ case 'xa' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
57
+ case 'as' : // Fall through.
58
+ case 'sa' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_SYNC
59
+
60
+ case 'a+' : return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
61
+ case 'ax+': // Fall through.
62
+ case 'xa+': return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
63
+ case 'as+': // Fall through.
64
+ case 'sa+': return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_SYNC
65
+ }
66
+
67
+ throw typeError('ERR_INVALID_ARG_VALUE', `Invalid value in flags: ${flags}`)
68
+ }
69
+
70
+ function modeToNumber (mode) {
71
+ mode = parseInt(mode, 8)
72
+ if (isNaN(mode)) throw typeError('ERR_INVALID_ARG_VALUE', 'Mode must be a number or octal string')
73
+ return mode
74
+ }
75
+
76
+ function alloc () {
77
+ const handle = Buffer.alloc(binding.sizeofFSReq)
78
+
79
+ binding.initReq(fs.handle, handle)
80
+
81
+ const view = new Uint32Array(handle.buffer, handle.byteOffset + binding.offsetofFSReqID, 1)
82
+
83
+ view[0] = reqs.length
84
+
85
+ const req = {
86
+ handle,
87
+ view,
88
+ type: 0,
89
+ buffer: null,
90
+ buffers: null,
91
+ callback: null
92
+ }
93
+
94
+ used++
95
+ reqs.push(req)
96
+ return req
97
+ }
98
+
99
+ function getReq () {
100
+ return used === reqs.length ? alloc() : reqs[used++]
101
+ }
102
+
103
+ function onfsresponse (id, result) {
104
+ const req = reqs[id]
105
+ used--
106
+
107
+ if (used !== id) {
108
+ const u = reqs[used]
109
+ reqs[u.view[0] = id] = u
110
+ reqs[req.view[0] = used] = req
111
+ }
112
+
113
+ const callback = req.callback
114
+ const buffer = req.buffer
115
+ const buffers = req.buffers
116
+
117
+ req.callback = null
118
+ req.buffer = null
119
+ req.buffers = null
120
+
121
+ if (result < 0) {
122
+ callback(createError(result), result, null)
123
+ } else {
124
+ callback(null, result, buffer || buffers)
125
+ }
126
+ }
127
+
128
+ function createError (errno) {
129
+ const [code, message] = process.errnos.get(errno)
130
+ const err = new Error(code + ': ' + message)
131
+
132
+ err.errno = errno
133
+ err.code = code
134
+
135
+ return err
136
+ }
137
+
138
+ function write (fd, buf, offset, len, pos, cb) {
139
+ if (typeof cb === 'function') {
140
+ const req = getReq()
141
+
142
+ req.buffer = buf
143
+ req.callback = cb
144
+
145
+ binding.write(req.handle, fd, buf, offset, len, pos)
146
+ return
147
+ }
148
+
149
+ if (typeof offset === 'function') return write(fd, buf, 0, buf.byteLength, null, offset)
150
+ if (typeof len === 'function') return write(fd, buf, offset, buf.byteLength - offset, null, len)
151
+ if (typeof pos === 'function') return write(fd, buf, offset, len, null, pos)
152
+
153
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received ' + cb)
154
+ }
155
+
156
+ function writeSync (fd, buf, offset = 0, len = buf.byteLength, pos = 0) {
157
+ return binding.writeSync(fd, buf, offset, len, pos)
158
+ }
159
+
160
+ function writev (fd, buffers, pos, cb) {
161
+ if (typeof pos === 'function') {
162
+ cb = pos
163
+ pos = 0
164
+ }
165
+
166
+ const req = getReq()
167
+
168
+ req.buffers = buffers
169
+ req.callback = cb
170
+
171
+ binding.writev(req.handle, fd, buffers, pos)
172
+ }
173
+
174
+ function read (fd, buf, offset, len, pos, cb) {
175
+ if (typeof cb === 'function') {
176
+ const req = getReq()
177
+
178
+ req.buffer = buf
179
+ req.callback = cb
180
+
181
+ binding.read(req.handle, fd, buf, offset, len, pos)
182
+ return
183
+ }
184
+
185
+ if (typeof offset === 'function') return read(fd, buf, 0, buf.byteLength, null, offset)
186
+ if (typeof len === 'function') return read(fd, buf, offset, buf.byteLength - offset, null, len)
187
+ if (typeof pos === 'function') return read(fd, buf, offset, len, null, pos)
188
+
189
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received ' + cb)
190
+ }
191
+
192
+ function readSync (fd, buf, offset = 0, len = buf.byteLength, pos = 0) {
193
+ return binding.readSync(fd, buf, offset, len, pos)
194
+ }
195
+
196
+ function readv (fd, buffers, pos, cb) {
197
+ if (typeof pos === 'function') {
198
+ cb = pos
199
+ pos = 0
200
+ }
201
+
202
+ const req = getReq()
203
+
204
+ req.buffers = buffers
205
+ req.callback = cb
206
+
207
+ binding.readv(req.handle, fd, buffers, pos)
208
+ }
209
+
210
+ function open (filename, flags = 'r', mode = 0o666, cb) {
211
+ if (typeof filename !== 'string') throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received ' + filename)
212
+
213
+ if (typeof flags === 'function') return open(filename, undefined, undefined, flags)
214
+ if (typeof mode === 'function') return open(filename, flags, undefined, mode)
215
+
216
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received ' + cb)
217
+
218
+ if (typeof flags === 'string') flags = flagsToNumber(flags)
219
+ if (typeof mode === 'string') mode = modeToNumber(mode)
220
+
221
+ const req = getReq()
222
+
223
+ req.callback = cb
224
+ binding.open(req.handle, filename, flags, mode)
225
+ }
226
+
227
+ function openSync (filename, flags = 'r', mode = 0o666) {
228
+ if (typeof filename !== 'string') throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received ' + filename)
229
+
230
+ if (typeof flags === 'string') flags = flagsToNumber(flags)
231
+ if (typeof mode === 'string') mode = modeToNumber(mode)
232
+
233
+ const res = binding.openSync(filename, flags, mode)
234
+
235
+ if (res < 0) throw createError(res)
236
+ return res
237
+ }
238
+
239
+ function close (fd, cb = noop) {
240
+ if (typeof fd !== 'number') throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
241
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
242
+ if (!(fd >= 0 && fd <= 0x7fffffff)) throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
243
+
244
+ const req = getReq()
245
+
246
+ req.callback = cb
247
+ binding.close(req.handle, fd)
248
+ }
249
+
250
+ function closeSync (fd) {
251
+ const res = binding.closeSync(fd)
252
+
253
+ if (res < 0) throw createError(res)
254
+ return res
255
+ }
256
+
257
+ function ftruncate (fd, len, cb) {
258
+ const req = getReq()
259
+
260
+ req.callback = cb
261
+ binding.ftruncate(req.handle, fd, len)
262
+ }
263
+
264
+ class Stats {
265
+ constructor (buf) {
266
+ const view = new Uint32Array(buf.buffer, buf.byteOffset, 32)
267
+
268
+ this.dev = toNumber(view, 0)
269
+ this.mode = toNumber(view, 2)
270
+ this.nlink = toNumber(view, 4)
271
+ this.uid = toNumber(view, 6)
272
+ this.gid = toNumber(view, 8)
273
+ this.rdev = toNumber(view, 10)
274
+ this.ino = toNumber(view, 12)
275
+ this.size = toNumber(view, 14)
276
+ this.blksize = toNumber(view, 16)
277
+ this.blocks = toNumber(view, 18)
278
+ this.flags = toNumber(view, 20)
279
+ this.gen = toNumber(view, 22)
280
+ this.atimeMs = toNumber(view, 24)
281
+ this.mtimeMs = toNumber(view, 26)
282
+ this.ctimeMs = toNumber(view, 28)
283
+ this.birthtimeMs = toNumber(view, 30)
284
+ this.atime = new Date(this.atimeMs)
285
+ this.mtime = new Date(this.mtimeMs)
286
+ this.ctime = new Date(this.ctimeMs)
287
+ this.birthtime = new Date(this.birthtimeMs)
288
+ }
289
+
290
+ isDirectory () {
291
+ return (this.mode & constants.S_IFMT) === constants.S_IFDIR
292
+ }
293
+
294
+ isFile () {
295
+ return (this.mode & constants.S_IFMT) === constants.S_IFREG
296
+ }
297
+
298
+ isBlockDevice () {
299
+ return (this.mode & constants.S_IFMT) === constants.S_IFBLK
300
+ }
301
+
302
+ isCharacterDevice () {
303
+ return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
304
+ }
305
+
306
+ isFIFO () {
307
+ return (this.mode & constants.S_IFMT) === constants.S_IFIFO
308
+ }
309
+
310
+ isSymbolicLink () {
311
+ return (this.mode & constants.S_IFMT) === constants.S_IFLNK
312
+ }
313
+
314
+ isSocket () {
315
+ return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
316
+ }
317
+ }
318
+
319
+ function toNumber (view, n) {
320
+ return LE ? view[n] + view[n + 1] * 0x100000000 : view[n] * 0x100000000 + view[n + 1]
321
+ }
322
+
323
+ function stat (path, cb) {
324
+ const req = getReq()
325
+
326
+ req.buffer = Buffer.allocUnsafe(16 * 8)
327
+
328
+ req.callback = function (err, _, buf) {
329
+ if (err) cb(err, null)
330
+ else cb(null, new Stats(buf))
331
+ }
332
+
333
+ binding.stat(req.handle, path, req.buffer)
334
+ }
335
+
336
+ function statSync (path) {
337
+ const buffer = Buffer.allocUnsafe(16 * 8)
338
+ const res = binding.statSync(path, buffer)
339
+ if (res < 0) throw createError(res)
340
+ return new Stats(buffer)
341
+ }
342
+
343
+ function lstat (path, cb) {
344
+ const req = getReq()
345
+
346
+ req.buffer = Buffer.allocUnsafe(16 * 8)
347
+
348
+ req.callback = function (err, _, buf) {
349
+ if (err) cb(err, null)
350
+ else cb(null, new Stats(buf))
351
+ }
352
+
353
+ binding.lstat(req.handle, path, req.buffer)
354
+ }
355
+
356
+ function lstatSync (path) {
357
+ const buffer = Buffer.allocUnsafe(16 * 8)
358
+ const res = binding.lstatSync(path, buffer)
359
+ if (res < 0) throw createError(res)
360
+ return new Stats(buffer)
361
+ }
362
+
363
+ function fstat (fd, cb) {
364
+ const req = getReq()
365
+
366
+ req.buffer = Buffer.allocUnsafe(16 * 8)
367
+
368
+ req.callback = function (err, _, buf) {
369
+ if (err) cb(err, null)
370
+ else cb(null, new Stats(buf))
371
+ }
372
+
373
+ binding.fstat(req.handle, fd, req.buffer)
374
+ }
375
+
376
+ function fstatSync (fd) {
377
+ const buffer = Buffer.allocUnsafe(16 * 8)
378
+ const res = binding.fstatSync(fd, buffer)
379
+ if (res < 0) throw createError(res)
380
+ return new Stats(buffer)
381
+ }
382
+
383
+ function mkdirp (path, mode, cb) {
384
+ mkdir(path, { mode }, function (err) {
385
+ if (err === null) return cb(null, 0, null)
386
+
387
+ if (err.code !== 'ENOENT') {
388
+ stat(path, function (e, st) {
389
+ if (e) return cb(e, e.errno, null)
390
+ if (st.isDirectory()) return cb(null, 0, null)
391
+ cb(err, err.errno, null)
392
+ })
393
+ return
394
+ }
395
+
396
+ while (path.endsWith(sep)) path = path.slice(0, -1)
397
+ const i = path.lastIndexOf(sep)
398
+ if (i <= 0) return cb(err, err.errno, null)
399
+
400
+ mkdirp(path.slice(0, i), mode, function (err) {
401
+ if (err) return cb(err, err.errno, null)
402
+ mkdir(path, { mode }, cb)
403
+ })
404
+ })
405
+ }
406
+
407
+ function rename (src, dst, cb) {
408
+ const req = getReq()
409
+
410
+ req.callback = cb
411
+ binding.rename(req.handle, src, dst)
412
+ }
413
+
414
+ function mkdir (path, opts, cb) {
415
+ if (typeof opts === 'function') {
416
+ cb = opts
417
+ opts = { mode: 0o777 }
418
+ }
419
+
420
+ if (!opts) opts = {}
421
+
422
+ const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
423
+
424
+ if (opts.recursive) {
425
+ return mkdirp(path, mode, cb)
426
+ }
427
+
428
+ const req = getReq()
429
+
430
+ req.callback = cb
431
+ binding.mkdir(req.handle, path, mode)
432
+ }
433
+
434
+ function rmdir (path, cb) {
435
+ const req = getReq()
436
+
437
+ req.callback = cb
438
+ binding.rmdir(req.handle, path)
439
+ }
440
+
441
+ function unlink (path, cb) {
442
+ const req = getReq()
443
+
444
+ req.callback = cb
445
+ binding.unlink(req.handle, path)
446
+ }
447
+
448
+ function readlink (path, opts, cb) {
449
+ if (typeof opts === 'function') return readlink(path, null, opts)
450
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
451
+ if (typeof opts === 'string') opts = { encoding: opts }
452
+ if (!opts) opts = {}
453
+
454
+ const {
455
+ encoding = 'utf8'
456
+ } = opts
457
+
458
+ const req = getReq()
459
+
460
+ req.buffer = Buffer.allocUnsafe(4097)
461
+
462
+ req.callback = function (err, _, buf) {
463
+ if (err) return cb(err, null)
464
+ buf = buf.subarray(0, buf.indexOf(0))
465
+ if (encoding !== 'buffer') return cb(null, Buffer.coerce(buf).toString(encoding))
466
+ cb(null, buf)
467
+ }
468
+
469
+ binding.readlink(req.handle, path, req.buffer)
470
+ }
471
+
472
+ function opendir (path, opts, cb) {
473
+ if (typeof opts === 'function') return opendir(path, null, opts)
474
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
475
+ if (typeof opts === 'string') opts = { encoding: opts }
476
+ if (!opts) opts = {}
477
+
478
+ const {
479
+ encoding = 'utf8'
480
+ } = opts
481
+
482
+ const req = getReq()
483
+
484
+ req.buffer = Buffer.allocUnsafe(binding.sizeofFSDir)
485
+
486
+ req.callback = function (err, _, buf) {
487
+ if (err) return cb(err, null)
488
+ cb(null, new Dir(buf, encoding))
489
+ }
490
+
491
+ binding.opendir(req.handle, path, req.buffer)
492
+ }
493
+
494
+ function readdir (dir, opts, cb) {
495
+ if (typeof opts === 'function') return readdir(dir, null, opts)
496
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
497
+ if (typeof opts === 'string') opts = { encoding: opts }
498
+ if (!opts) opts = {}
499
+
500
+ if (typeof dir === 'string') {
501
+ return opendir(dir, opts, (err, dir) => {
502
+ if (err) return cb(err, null)
503
+ return readdir(dir, opts, cb)
504
+ })
505
+ }
506
+
507
+ const {
508
+ encoding = 'utf8',
509
+ bufferSize = 32
510
+ } = opts
511
+
512
+ const req = getReq()
513
+
514
+ req.buffer = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
515
+
516
+ req.callback = function (err, _, buf) {
517
+ if (err) return cb(err, null)
518
+ binding.toDirent(dir._handle)
519
+ }
520
+
521
+ binding.readdir(req.handle, dir._handle, req.buffer)
522
+ }
523
+
524
+ function readFile (path, opts, cb) {
525
+ if (typeof opts === 'function') return readFile(path, null, opts)
526
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
527
+ if (typeof opts === 'string') opts = { encoding: opts }
528
+ if (!opts) opts = {}
529
+
530
+ open(path, opts.flag || 'r', function (err, fd) {
531
+ if (err) return cb(err)
532
+
533
+ fstat(fd, function (err, st) {
534
+ if (err) return closeAndError(err)
535
+
536
+ let buf = Buffer.allocUnsafe(st.size)
537
+ let len = 0
538
+
539
+ read(fd, buf, loop)
540
+
541
+ function loop (err, r) {
542
+ if (err) return closeAndError(err)
543
+ len += r
544
+ if (r === 0 || len === buf.byteLength) return done()
545
+ read(fd, buf.subarray(len), loop)
546
+ }
547
+
548
+ function done () {
549
+ if (len !== buf.byteLength) buf = buf.subarray(0, len)
550
+ close(fd, function (err) {
551
+ if (err) return cb(err)
552
+ if (opts.encoding) return cb(null, buf.toString(opts.encoding))
553
+ return cb(null, buf)
554
+ })
555
+ }
556
+ })
557
+
558
+ function closeAndError (err) {
559
+ close(fd, function () {
560
+ cb(err)
561
+ })
562
+ }
563
+ })
564
+ }
565
+
566
+ function readFileSync (path, opts) {
567
+ if (typeof opts === 'string') opts = { encoding: opts }
568
+ if (!opts) opts = {}
569
+
570
+ const fd = openSync(path, opts.flag || 'r')
571
+
572
+ try {
573
+ const st = fstatSync(fd)
574
+
575
+ let buf = Buffer.allocUnsafe(st.size)
576
+ let len = 0
577
+
578
+ while (true) {
579
+ const r = readSync(fd, len ? buf.subarray(len) : buf)
580
+ len += r
581
+ if (r === 0 || len === buf.byteLength) break
582
+ }
583
+ if (len !== buf.byteLength) buf = buf.subarray(0, len)
584
+
585
+ if (opts.encoding) return Buffer.coerce(buf).toString(opts.encoding)
586
+ return buf
587
+ } finally {
588
+ try {
589
+ closeSync(fd)
590
+ } catch {}
591
+ }
592
+ }
593
+
594
+ function writeFile (path, data, opts, cb) {
595
+ if (typeof opts === 'function') return writeFile(path, data, null, opts)
596
+ if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
597
+ if (typeof data !== 'string' && !Buffer.isBuffer(data)) throw typeError('ERR_INVALID_ARG_TYPE', 'The data argument must be of type string or buffer')
598
+ if (typeof opts === 'string') opts = { encoding: opts }
599
+ if (!opts) opts = {}
600
+
601
+ if (opts.encoding || typeof data === 'string') {
602
+ data = Buffer.from(data, opts.encoding)
603
+ }
604
+
605
+ open(path, opts.flag || 'w', opts.mode, function (err, fd) {
606
+ if (err) return cb(err)
607
+
608
+ write(fd, data, loop)
609
+
610
+ function loop (err, w, data) {
611
+ if (err) return closeAndError(err)
612
+ if (w === data.byteLength) return done()
613
+ write(fd, data.subarray(w), loop)
614
+ }
615
+
616
+ function done () {
617
+ close(fd, function (err) {
618
+ if (err) return cb(err)
619
+ return cb(null)
620
+ })
621
+ }
622
+
623
+ function closeAndError (err) {
624
+ close(fd, function () {
625
+ cb(err)
626
+ })
627
+ }
628
+ })
629
+ }
630
+
631
+ function writeFileSync (path, buf, opts) {
632
+ if (typeof opts === 'string') opts = { encoding: opts }
633
+ if (!opts) opts = {}
634
+
635
+ if (opts.encoding || typeof buf === 'string') {
636
+ buf = Buffer.from(buf, opts.encoding)
637
+ }
638
+
639
+ const fd = openSync(path, opts.flag || 'w', opts.mode)
640
+
641
+ try {
642
+ let len = 0
643
+ while (true) {
644
+ len += writeSync(fd, len ? buf.subarray(len) : buf)
645
+ if (len === buf.byteLength) break
646
+ }
647
+ } finally {
648
+ try {
649
+ closeSync(fd)
650
+ } catch {}
651
+ }
652
+ }
653
+
654
+ class FileWriteStream extends Writable {
655
+ constructor (path, opts = {}) {
656
+ super({ map })
657
+
658
+ this.path = path
659
+ this.fd = 0
660
+ this.flags = opts.flags || 'w'
661
+ this.mode = opts.mode || 0o666
662
+ }
663
+
664
+ _open (cb) {
665
+ open(this.path, this.flags, this.mode, (err, fd) => {
666
+ if (err) return cb(err)
667
+ this.fd = fd
668
+ cb(null)
669
+ })
670
+ }
671
+
672
+ _writev (datas, cb) {
673
+ writev(this.fd, datas, cb)
674
+ }
675
+
676
+ _destroy (cb) {
677
+ if (!this.fd) return cb(null)
678
+ close(this.fd, () => cb(null))
679
+ }
680
+ }
681
+
682
+ class FileReadStream extends Readable {
683
+ constructor (path, opts = {}) {
684
+ super()
685
+
686
+ this.path = path
687
+ this.fd = 0
688
+
689
+ this._offset = opts.start || 0
690
+ this._missing = 0
691
+
692
+ if (opts.length) this._missing = opts.length
693
+ else if (typeof opts.end === 'number') this._missing = opts.end - this._offset + 1
694
+ else this._missing = -1
695
+ }
696
+
697
+ _open (cb) {
698
+ open(this.path, constants.O_RDONLY, (err, fd) => {
699
+ if (err) return cb(err)
700
+
701
+ const onerror = (err) => close(fd, () => cb(err))
702
+
703
+ fstat(fd, (err, st) => {
704
+ if (err) return onerror(err)
705
+ if (!st.isFile()) return onerror(new Error(this.path + ' is not a file'))
706
+
707
+ this.fd = fd
708
+ if (this._missing === -1) this._missing = st.size
709
+
710
+ if (st.size < this._offset) {
711
+ this._offset = st.size
712
+ this._missing = 0
713
+ return cb(null)
714
+ }
715
+ if (st.size < this._offset + this._missing) {
716
+ this._missing = st.size - this._offset
717
+ return cb(null)
718
+ }
719
+
720
+ cb(null)
721
+ })
722
+ })
723
+ }
724
+
725
+ _read (cb) {
726
+ if (!this._missing) {
727
+ this.push(null)
728
+ return cb(null)
729
+ }
730
+
731
+ const data = Buffer.allocUnsafe(Math.min(this._missing, 65536))
732
+
733
+ read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
734
+ if (err) return cb(err)
735
+
736
+ if (!read) {
737
+ this.push(null)
738
+ return cb(null)
739
+ }
740
+
741
+ if (this._missing < read) read = this._missing
742
+ this.push(data.subarray(0, read))
743
+ this._missing -= read
744
+ this._offset += read
745
+ if (!this._missing) this.push(null)
746
+
747
+ cb(null)
748
+ })
749
+ }
750
+
751
+ _destroy (cb) {
752
+ if (!this.fd) return cb(null)
753
+ close(this.fd, () => cb(null))
754
+ }
755
+ }
756
+
757
+ class Dir extends Readable {
758
+ constructor (handle, encoding) {
759
+ super()
760
+
761
+ this._handle = handle
762
+ this._encoding = encoding
763
+ }
764
+
765
+ _read (cb) {
766
+ cb(null)
767
+ }
768
+ }
769
+
770
+ exports.promises = {}
771
+
772
+ function typeError (code, message) {
773
+ const error = new TypeError(message)
774
+ error.code = code
775
+ return error
776
+ }
777
+
778
+ function noop () {}
779
+
780
+ exports.open = open
781
+ exports.close = close
782
+ exports.read = read
783
+ exports.readv = readv
784
+ exports.write = write
785
+ exports.writev = writev
786
+ exports.ftruncate = ftruncate
787
+ exports.fstat = fstat
788
+
789
+ exports.openSync = openSync
790
+ exports.closeSync = closeSync
791
+ exports.readSync = readSync
792
+ exports.writeSync = writeSync
793
+ exports.fstatSync = fstatSync
794
+ exports.statSync = statSync
795
+ exports.lstatSync = lstatSync
796
+
797
+ exports.readFileSync = readFileSync
798
+ exports.writeFileSync = writeFileSync
799
+
800
+ exports.unlink = unlink
801
+ exports.promises.unlink = promisify(unlink)
802
+
803
+ exports.readFile = readFile
804
+ exports.promises.readFile = promisify(readFile)
805
+
806
+ exports.writeFile = writeFile
807
+ exports.promises.writeFile = promisify(writeFile)
808
+
809
+ exports.rename = rename
810
+ exports.promises.rename = promisify(rename)
811
+
812
+ exports.mkdir = mkdir
813
+ exports.promises.mkdir = promisify(mkdir)
814
+
815
+ exports.rmdir = rmdir
816
+ exports.promises.rmdir = promisify(rmdir)
817
+
818
+ exports.Stats = Stats // for compat
819
+
820
+ exports.stat = stat
821
+ exports.promises.stat = promisify(stat)
822
+
823
+ exports.lstat = lstat
824
+ exports.promises.lstat = promisify(lstat)
825
+
826
+ exports.readlink = readlink
827
+ exports.promises.readlink = promisify(readlink)
828
+
829
+ exports.opendir = opendir
830
+ exports.promises.opendir = promisify(opendir)
831
+
832
+ exports.readdir = readdir
833
+ exports.promises.readdir = promisify(readdir)
834
+
835
+ exports.ReadStream = FileReadStream
836
+ exports.createReadStream = (path, options) => new FileReadStream(path, options)
837
+
838
+ exports.WriteStream = FileWriteStream
839
+ exports.createWriteStream = (path, options) => new FileWriteStream(path, options)
840
+
841
+ function promisify (fn) {
842
+ return function (...args) {
843
+ return new Promise((resolve, reject) => {
844
+ fn(...args, function (err, res) {
845
+ if (err) return reject(err)
846
+ resolve(res)
847
+ })
848
+ })
849
+ }
850
+ }
851
+
852
+ function map (s) {
853
+ return typeof s === 'string' ? Buffer.from(s) : s
854
+ }