bare-fs 4.2.3 → 4.3.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
@@ -1,393 +1,278 @@
1
1
  const EventEmitter = require('bare-events')
2
2
  const path = require('bare-path')
3
+ const { fileURLToPath } = require('bare-url')
3
4
  const { Readable, Writable } = require('bare-stream')
4
5
  const binding = require('./binding')
5
6
  const constants = require('./lib/constants')
7
+ const FileError = require('./lib/errors')
6
8
 
7
9
  const isWindows = Bare.platform === 'win32'
8
10
 
9
11
  exports.constants = constants
10
12
 
11
- // Lightly-modified from the Node FS internal utils.
12
- function flagsToNumber(flags) {
13
- switch (flags) {
14
- case 'r':
15
- return constants.O_RDONLY
16
- case 'rs': // Fall through.
17
- case 'sr':
18
- return constants.O_RDONLY | constants.O_SYNC
19
- case 'r+':
20
- return constants.O_RDWR
21
- case 'rs+': // Fall through.
22
- case 'sr+':
23
- return constants.O_RDWR | constants.O_SYNC
13
+ class FileRequest {
14
+ constructor() {
15
+ this._reset()
24
16
 
25
- case 'w':
26
- return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
27
- case 'wx': // Fall through.
28
- case 'xw':
29
- return (
30
- constants.O_TRUNC |
31
- constants.O_CREAT |
32
- constants.O_WRONLY |
33
- constants.O_EXCL
34
- )
17
+ this._handle = binding.requestInit(this, this._onresult)
18
+ }
35
19
 
36
- case 'w+':
37
- return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
38
- case 'wx+': // Fall through.
39
- case 'xw+':
40
- return (
41
- constants.O_TRUNC |
42
- constants.O_CREAT |
43
- constants.O_RDWR |
44
- constants.O_EXCL
45
- )
20
+ get handle() {
21
+ return this._handle
22
+ }
46
23
 
47
- case 'a':
48
- return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
49
- case 'ax': // Fall through.
50
- case 'xa':
51
- return (
52
- constants.O_APPEND |
53
- constants.O_CREAT |
54
- constants.O_WRONLY |
55
- constants.O_EXCL
56
- )
57
- case 'as': // Fall through.
58
- case 'sa':
59
- return (
60
- constants.O_APPEND |
61
- constants.O_CREAT |
62
- constants.O_WRONLY |
63
- constants.O_SYNC
64
- )
24
+ retain(value) {
25
+ this._retain = value // Tie the lifetime of `value` to the lifetime of `this`
26
+ }
65
27
 
66
- case 'a+':
67
- return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
68
- case 'ax+': // Fall through.
69
- case 'xa+':
70
- return (
71
- constants.O_APPEND |
72
- constants.O_CREAT |
73
- constants.O_RDWR |
74
- constants.O_EXCL
75
- )
76
- case 'as+': // Fall through.
77
- case 'sa+':
78
- return (
79
- constants.O_APPEND |
80
- constants.O_CREAT |
81
- constants.O_RDWR |
82
- constants.O_SYNC
83
- )
28
+ reset() {
29
+ this._reset()
30
+
31
+ binding.requestReset(this._handle)
84
32
  }
85
33
 
86
- throw typeError('ERR_INVALID_ARG_VALUE', `Invalid value in flags: ${flags}`)
87
- }
34
+ destroy() {
35
+ this._reset()
88
36
 
89
- function modeToNumber(mode) {
90
- mode = parseInt(mode, 8)
91
- if (isNaN(mode))
92
- throw typeError(
93
- 'ERR_INVALID_ARG_VALUE',
94
- 'Mode must be a number or octal string'
95
- )
96
- return mode
97
- }
37
+ binding.requestDestroy(this._handle)
38
+ }
39
+
40
+ then(resolve, reject) {
41
+ this._promise.then(resolve, reject)
42
+ }
98
43
 
99
- const free = []
44
+ _reset() {
45
+ const { promise, resolve, reject } = Promise.withResolvers()
100
46
 
101
- function alloc() {
102
- const req = { handle: null, callback: null }
103
- req.handle = binding.init(req, onresponse)
104
- return req
47
+ this._promise = promise
48
+ this._resolve = resolve
49
+ this._reject = reject
50
+ this._retain = null
51
+ }
52
+
53
+ _onresult(err, status) {
54
+ if (err) this._reject(err)
55
+ else this._resolve(status)
56
+ }
105
57
  }
106
58
 
107
- function getReq() {
108
- return free.length ? free.pop() : alloc()
59
+ function ok(result, cb) {
60
+ if (typeof result === 'function') {
61
+ cb = result
62
+ result = undefined
63
+ }
64
+
65
+ if (cb) cb(null, result)
66
+ else return result
109
67
  }
110
68
 
111
- function onresponse(err, result) {
112
- const req = this
113
- const cb = req.callback
114
- req.callback = null
115
- free.push(req)
116
- cb(err, result)
69
+ function fail(err, cb) {
70
+ if (cb) cb(err)
71
+ else throw err
117
72
  }
118
73
 
119
- function open(filepath, flags, mode, cb) {
120
- if (typeof filepath !== 'string') {
121
- throw typeError(
122
- 'ERR_INVALID_ARG_TYPE',
123
- 'Path must be a string. Received type ' +
124
- typeof filepath +
125
- ' (' +
126
- filepath +
127
- ')'
128
- )
74
+ function done(err, result, cb) {
75
+ if (typeof result === 'function') {
76
+ cb = result
77
+ result = undefined
129
78
  }
130
79
 
131
- if (typeof cb !== 'function') {
132
- if (typeof flags === 'function') {
133
- cb = flags
134
- flags = 'r'
135
- mode = 0o666
136
- } else if (typeof mode === 'function') {
137
- cb = mode
138
- mode = 0o666
139
- } else {
140
- throw typeError(
141
- 'ERR_INVALID_ARG_TYPE',
142
- 'Callback must be a function. Received type ' +
143
- typeof cb +
144
- ' (' +
145
- cb +
146
- ')'
147
- )
148
- }
80
+ if (err) fail(err, cb)
81
+ else return ok(result, cb)
82
+ }
83
+
84
+ async function open(filepath, flags = 'r', mode = 0o666, cb) {
85
+ if (typeof flags === 'function') {
86
+ cb = flags
87
+ flags = 'r'
88
+ mode = 0o666
89
+ } else if (typeof mode === 'function') {
90
+ cb = mode
91
+ mode = 0o666
149
92
  }
150
93
 
151
- if (typeof flags === 'string') flags = flagsToNumber(flags)
152
- if (typeof mode === 'string') mode = modeToNumber(mode)
94
+ if (typeof flags === 'string') flags = toFlags(flags)
95
+ if (typeof mode === 'string') mode = toMode(mode)
153
96
 
154
- const req = getReq()
155
- req.callback = cb
156
- binding.open(req.handle, toNamespacedPath(filepath), flags, mode)
97
+ filepath = toNamespacedPath(filepath)
98
+
99
+ const req = new FileRequest()
100
+
101
+ let fd
102
+ let err = null
103
+ try {
104
+ binding.open(req.handle, filepath, flags, mode)
105
+
106
+ fd = await req
107
+ } catch (e) {
108
+ err = new FileError(e.message, {
109
+ operation: 'open',
110
+ code: e.code,
111
+ path: filepath
112
+ })
113
+ } finally {
114
+ req.destroy()
115
+ }
116
+
117
+ return done(err, fd, cb)
157
118
  }
158
119
 
159
120
  function openSync(filepath, flags = 'r', mode = 0o666) {
160
- if (typeof filepath !== 'string') {
161
- throw typeError(
162
- 'ERR_INVALID_ARG_TYPE',
163
- 'Path must be a string. Received type ' +
164
- typeof filepath +
165
- ' (' +
166
- filepath +
167
- ')'
168
- )
169
- }
121
+ if (typeof flags === 'string') flags = toFlags(flags)
122
+ if (typeof mode === 'string') mode = toMode(mode)
123
+
124
+ filepath = toNamespacedPath(filepath)
170
125
 
171
- if (typeof flags === 'string') flags = flagsToNumber(flags)
172
- if (typeof mode === 'string') mode = modeToNumber(mode)
126
+ const req = new FileRequest()
173
127
 
174
- return binding.openSync(toNamespacedPath(filepath), flags, mode)
128
+ try {
129
+ return binding.openSync(req.handle, filepath, flags, mode)
130
+ } catch (e) {
131
+ throw new FileError(e.message, {
132
+ operation: 'open',
133
+ code: e.code,
134
+ path: filepath
135
+ })
136
+ } finally {
137
+ req.destroy()
138
+ }
175
139
  }
176
140
 
177
- function close(fd, cb = noop) {
178
- if (typeof fd !== 'number') {
179
- throw typeError(
180
- 'ERR_INVALID_ARG_TYPE',
181
- 'File descriptor must be a number. Received type ' +
182
- typeof fd +
183
- ' (' +
184
- fd +
185
- ')'
186
- )
187
- }
141
+ async function close(fd, cb) {
142
+ const req = new FileRequest()
188
143
 
189
- if (fd < 0 || fd > 0x7fffffff) {
190
- throw typeError(
191
- 'ERR_OUT_OF_RANGE',
192
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
193
- fd
194
- )
195
- }
144
+ let err = null
145
+ try {
146
+ binding.close(req.handle, fd)
196
147
 
197
- if (typeof cb !== 'function') {
198
- throw typeError(
199
- 'ERR_INVALID_ARG_TYPE',
200
- 'Callback must be a function. Received type ' +
201
- typeof cb +
202
- ' (' +
203
- cb +
204
- ')'
205
- )
148
+ await req
149
+ } catch (e) {
150
+ err = new FileError(e.message, { operation: 'close', code: e.code, fd })
151
+ } finally {
152
+ req.destroy()
206
153
  }
207
154
 
208
- const req = getReq()
209
- req.callback = cb
210
- binding.close(req.handle, fd)
155
+ return done(err, cb)
211
156
  }
212
157
 
213
158
  function closeSync(fd) {
214
- if (typeof fd !== 'number') {
215
- throw typeError(
216
- 'ERR_INVALID_ARG_TYPE',
217
- 'File descriptor must be a number. Received type ' +
218
- typeof fd +
219
- ' (' +
220
- fd +
221
- ')'
222
- )
223
- }
159
+ const req = new FileRequest()
224
160
 
225
- if (fd < 0 || fd > 0x7fffffff) {
226
- throw typeError(
227
- 'ERR_OUT_OF_RANGE',
228
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
229
- fd
230
- )
161
+ try {
162
+ binding.closeSync(req.handle, fd)
163
+ } catch (e) {
164
+ throw new FileError(e.message, { operation: 'close', code: e.code, fd })
165
+ } finally {
166
+ req.destroy()
231
167
  }
232
-
233
- return binding.closeSync(fd)
234
168
  }
235
169
 
236
- function access(filepath, mode, cb) {
237
- if (typeof filepath !== 'string') {
238
- throw typeError(
239
- 'ERR_INVALID_ARG_TYPE',
240
- 'Path must be a string. Received type ' +
241
- typeof filepath +
242
- ' (' +
243
- filepath +
244
- ')'
245
- )
170
+ async function access(filepath, mode = constants.F_OK, cb) {
171
+ if (typeof mode === 'function') {
172
+ cb = mode
173
+ mode = constants.F_OK
246
174
  }
247
175
 
248
- if (typeof cb !== 'function') {
249
- if (typeof mode === 'function') {
250
- cb = mode
251
- mode = constants.F_OK
252
- } else {
253
- throw typeError(
254
- 'ERR_INVALID_ARG_TYPE',
255
- 'Callback must be a function. Received type ' +
256
- typeof cb +
257
- ' (' +
258
- cb +
259
- ')'
260
- )
261
- }
176
+ filepath = toNamespacedPath(filepath)
177
+
178
+ const req = new FileRequest()
179
+
180
+ let err = null
181
+ try {
182
+ binding.access(req.handle, filepath, mode)
183
+
184
+ await req
185
+ } catch (e) {
186
+ err = new FileError(e.message, {
187
+ operation: 'access',
188
+ code: e.code,
189
+ path: filepath
190
+ })
191
+ } finally {
192
+ req.destroy()
262
193
  }
263
194
 
264
- const req = getReq()
265
- req.callback = cb
266
- binding.access(req.handle, toNamespacedPath(filepath), mode)
195
+ return done(err, cb)
267
196
  }
268
197
 
269
198
  function accessSync(filepath, mode = constants.F_OK) {
270
- if (typeof filepath !== 'string') {
271
- throw typeError(
272
- 'ERR_INVALID_ARG_TYPE',
273
- 'Path must be a string. Received type ' +
274
- typeof filepath +
275
- ' (' +
276
- filepath +
277
- ')'
278
- )
279
- }
199
+ const req = new FileRequest()
280
200
 
281
- binding.accessSync(toNamespacedPath(filepath), mode)
282
- }
201
+ filepath = toNamespacedPath(filepath)
283
202
 
284
- function exists(filepath, cb) {
285
- if (typeof filepath !== 'string') {
286
- throw typeError(
287
- 'ERR_INVALID_ARG_TYPE',
288
- 'Path must be a string. Received type ' +
289
- typeof filepath +
290
- ' (' +
291
- filepath +
292
- ')'
293
- )
203
+ try {
204
+ binding.accessSync(req.handle, filepath, mode)
205
+ } catch (e) {
206
+ throw new FileError(e.message, {
207
+ operation: 'access',
208
+ code: e.code,
209
+ path: filepath
210
+ })
211
+ } finally {
212
+ req.destroy()
294
213
  }
214
+ }
295
215
 
296
- if (typeof cb !== 'function') {
297
- throw typeError(
298
- 'ERR_INVALID_ARG_TYPE',
299
- 'Callback must be a function. Received type ' +
300
- typeof cb +
301
- ' (' +
302
- cb +
303
- ')'
304
- )
216
+ async function exists(filepath, cb) {
217
+ let ok = true
218
+ try {
219
+ await access(filepath)
220
+ } catch {
221
+ ok = false
305
222
  }
306
223
 
307
- return access(toNamespacedPath(filepath), (err) => cb(!!err))
224
+ return done(null, ok, cb)
308
225
  }
309
226
 
310
227
  function existsSync(filepath) {
311
- if (typeof filepath !== 'string') {
312
- throw typeError(
313
- 'ERR_INVALID_ARG_TYPE',
314
- 'Path must be a string. Received type ' +
315
- typeof filepath +
316
- ' (' +
317
- filepath +
318
- ')'
319
- )
320
- }
321
-
322
228
  try {
323
- accessSync(toNamespacedPath(filepath))
324
- return true
229
+ accessSync(filepath)
325
230
  } catch {
326
231
  return false
327
232
  }
233
+
234
+ return true
328
235
  }
329
236
 
330
- function read(fd, buffer, offset, len, pos, cb) {
331
- if (typeof fd !== 'number') {
332
- throw typeError(
333
- 'ERR_INVALID_ARG_TYPE',
334
- 'File descriptor must be a number. Received type ' +
335
- typeof fd +
336
- ' (' +
337
- fd +
338
- ')'
339
- )
237
+ async function read(
238
+ fd,
239
+ buffer,
240
+ offset = 0,
241
+ len = buffer.byteLength - offset,
242
+ pos = -1,
243
+ cb
244
+ ) {
245
+ if (typeof offset === 'function') {
246
+ cb = offset
247
+ offset = 0
248
+ len = buffer.byteLength
249
+ pos = -1
250
+ } else if (typeof len === 'function') {
251
+ cb = len
252
+ len = buffer.byteLength - offset
253
+ pos = -1
254
+ } else if (typeof pos === 'function') {
255
+ cb = pos
256
+ pos = -1
340
257
  }
341
258
 
342
- if (fd < 0 || fd > 0x7fffffff) {
343
- throw typeError(
344
- 'ERR_OUT_OF_RANGE',
345
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
346
- fd
347
- )
348
- }
259
+ if (typeof pos !== 'number') pos = -1
349
260
 
350
- if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
351
- throw typeError(
352
- 'ERR_INVALID_ARG_TYPE',
353
- 'Buffer must be a buffer. Received type ' +
354
- typeof buffer +
355
- ' (' +
356
- buffer +
357
- ')'
358
- )
359
- }
261
+ const req = new FileRequest()
360
262
 
361
- if (typeof cb !== 'function') {
362
- if (typeof offset === 'function') {
363
- cb = offset
364
- offset = 0
365
- len = buffer.byteLength
366
- pos = -1
367
- } else if (typeof len === 'function') {
368
- cb = len
369
- len = buffer.byteLength - offset
370
- pos = -1
371
- } else if (typeof pos === 'function') {
372
- cb = pos
373
- pos = -1
374
- } else {
375
- throw typeError(
376
- 'ERR_INVALID_ARG_TYPE',
377
- 'Callback must be a function. Received type ' +
378
- typeof cb +
379
- ' (' +
380
- cb +
381
- ')'
382
- )
383
- }
384
- }
263
+ let bytes
264
+ let err = null
265
+ try {
266
+ binding.read(req.handle, fd, buffer, offset, len, pos)
385
267
 
386
- if (typeof pos !== 'number') pos = -1
268
+ bytes = await req
269
+ } catch (e) {
270
+ err = new FileError(e.message, { operation: 'read', code: e.code, fd })
271
+ } finally {
272
+ req.destroy()
273
+ }
387
274
 
388
- const req = getReq()
389
- req.callback = cb
390
- binding.read(req.handle, fd, buffer, offset, len, pos)
275
+ return done(err, bytes, cb)
391
276
  }
392
277
 
393
278
  function readSync(
@@ -397,134 +282,69 @@ function readSync(
397
282
  len = buffer.byteLength - offset,
398
283
  pos = -1
399
284
  ) {
400
- if (typeof fd !== 'number') {
401
- throw typeError(
402
- 'ERR_INVALID_ARG_TYPE',
403
- 'File descriptor must be a number. Received type ' +
404
- typeof fd +
405
- ' (' +
406
- fd +
407
- ')'
408
- )
409
- }
285
+ const req = new FileRequest()
410
286
 
411
- if (fd < 0 || fd > 0x7fffffff) {
412
- throw typeError(
413
- 'ERR_OUT_OF_RANGE',
414
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
415
- fd
416
- )
417
- }
418
-
419
- if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
420
- throw typeError(
421
- 'ERR_INVALID_ARG_TYPE',
422
- 'Buffer must be a buffer. Received type ' +
423
- typeof buffer +
424
- ' (' +
425
- buffer +
426
- ')'
427
- )
287
+ try {
288
+ return binding.readSync(req.handle, fd, buffer, offset, len, pos)
289
+ } catch (e) {
290
+ throw new FileError(e.message, { operation: 'read', code: e.code, fd })
291
+ } finally {
292
+ req.destroy()
428
293
  }
429
-
430
- return binding.readSync(fd, buffer, offset, len, pos)
431
294
  }
432
295
 
433
- function readv(fd, buffers, pos, cb) {
434
- if (typeof fd !== 'number') {
435
- throw typeError(
436
- 'ERR_INVALID_ARG_TYPE',
437
- 'File descriptor must be a number. Received type ' +
438
- typeof fd +
439
- ' (' +
440
- fd +
441
- ')'
442
- )
443
- }
444
-
445
- if (fd < 0 || fd > 0x7fffffff) {
446
- throw typeError(
447
- 'ERR_OUT_OF_RANGE',
448
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
449
- fd
450
- )
451
- }
452
-
296
+ async function readv(fd, buffers, pos = -1, cb) {
453
297
  if (typeof pos === 'function') {
454
298
  cb = pos
455
299
  pos = -1
456
- } else if (typeof cb !== 'function') {
457
- throw typeError(
458
- 'ERR_INVALID_ARG_TYPE',
459
- 'Callback must be a function. Received type ' +
460
- typeof cb +
461
- ' (' +
462
- cb +
463
- ')'
464
- )
465
300
  }
466
301
 
467
302
  if (typeof pos !== 'number') pos = -1
468
303
 
469
- const req = getReq()
470
- req.callback = cb
471
- binding.readv(req.handle, fd, buffers, pos)
472
- }
304
+ const req = new FileRequest()
473
305
 
474
- function write(fd, data, offset, len, pos, cb) {
475
- if (typeof fd !== 'number') {
476
- throw typeError(
477
- 'ERR_INVALID_ARG_TYPE',
478
- 'File descriptor must be a number. Received type ' +
479
- typeof fd +
480
- ' (' +
481
- fd +
482
- ')'
483
- )
484
- }
306
+ let bytes
307
+ let err = null
308
+ try {
309
+ binding.readv(req.handle, fd, buffers, pos)
485
310
 
486
- if (fd < 0 || fd > 0x7fffffff) {
487
- throw typeError(
488
- 'ERR_OUT_OF_RANGE',
489
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
490
- fd
491
- )
311
+ bytes = await req
312
+ } catch (e) {
313
+ err = new FileError(e.message, { operation: 'readv', code: e.code, fd })
314
+ } finally {
315
+ req.destroy()
492
316
  }
493
317
 
494
- if (
495
- typeof data !== 'string' &&
496
- !Buffer.isBuffer(data) &&
497
- !ArrayBuffer.isView(data)
498
- ) {
499
- throw typeError(
500
- 'ERR_INVALID_ARG_TYPE',
501
- 'Data must be a string or buffer. Received type ' + typeof data
502
- )
318
+ return done(err, bytes, cb)
319
+ }
320
+
321
+ function readvSync(fd, buffers, pos = -1) {
322
+ if (typeof pos !== 'number') pos = -1
323
+
324
+ const req = new FileRequest()
325
+
326
+ try {
327
+ return binding.readvSync(req.handle, fd, buffers, pos)
328
+ } catch (e) {
329
+ throw new FileError(e.message, { operation: 'readv', code: e.code, fd })
330
+ } finally {
331
+ req.destroy()
503
332
  }
333
+ }
504
334
 
335
+ async function write(fd, data, offset = 0, len, pos = -1, cb) {
505
336
  if (typeof data === 'string') {
506
337
  let encoding = len
507
338
  cb = pos
508
339
  pos = offset
509
340
 
510
- if (typeof cb !== 'function') {
511
- if (typeof pos === 'function') {
512
- cb = pos
513
- pos = -1
514
- encoding = 'utf8'
515
- } else if (typeof encoding === 'function') {
516
- cb = encoding
517
- encoding = 'utf8'
518
- } else {
519
- throw typeError(
520
- 'ERR_INVALID_ARG_TYPE',
521
- 'Callback must be a function. Received type ' +
522
- typeof cb +
523
- ' (' +
524
- cb +
525
- ')'
526
- )
527
- }
341
+ if (typeof pos === 'function') {
342
+ cb = pos
343
+ pos = -1
344
+ encoding = 'utf8'
345
+ } else if (typeof encoding === 'function') {
346
+ cb = encoding
347
+ encoding = 'utf8'
528
348
  }
529
349
 
530
350
  if (typeof pos === 'string') {
@@ -535,536 +355,388 @@ function write(fd, data, offset, len, pos, cb) {
535
355
  data = Buffer.from(data, encoding)
536
356
  offset = 0
537
357
  len = data.byteLength
538
- } else if (typeof cb !== 'function') {
539
- if (typeof offset === 'function') {
540
- cb = offset
541
- offset = 0
542
- len = data.byteLength
543
- pos = -1
544
- } else if (typeof len === 'function') {
545
- cb = len
546
- len = data.byteLength - offset
547
- pos = -1
548
- } else if (typeof pos === 'function') {
549
- cb = pos
550
- pos = -1
551
- } else {
552
- throw typeError(
553
- 'ERR_INVALID_ARG_TYPE',
554
- 'Callback must be a function. Received type ' +
555
- typeof cb +
556
- ' (' +
557
- cb +
558
- ')'
559
- )
560
- }
358
+ } else if (typeof offset === 'function') {
359
+ cb = offset
360
+ offset = 0
361
+ len = data.byteLength
362
+ pos = -1
363
+ } else if (typeof len === 'function') {
364
+ cb = len
365
+ len = data.byteLength - offset
366
+ pos = -1
367
+ } else if (typeof pos === 'function') {
368
+ cb = pos
369
+ pos = -1
561
370
  }
562
371
 
372
+ if (typeof len !== 'number') len = data.byteLength - offset
563
373
  if (typeof pos !== 'number') pos = -1
564
374
 
565
- const req = getReq()
566
- req.callback = cb
567
- binding.write(req.handle, fd, data, offset, len, pos)
568
- }
375
+ const req = new FileRequest()
569
376
 
570
- function writeSync(fd, data, offset = 0, len, pos) {
571
- if (typeof fd !== 'number') {
572
- throw typeError(
573
- 'ERR_INVALID_ARG_TYPE',
574
- 'File descriptor must be a number. Received type ' +
575
- typeof fd +
576
- ' (' +
577
- fd +
578
- ')'
579
- )
580
- }
377
+ let bytes
378
+ let err = null
379
+ try {
380
+ binding.write(req.handle, fd, data, offset, len, pos)
581
381
 
582
- if (fd < 0 || fd > 0x7fffffff) {
583
- throw typeError(
584
- 'ERR_OUT_OF_RANGE',
585
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
586
- fd
587
- )
382
+ bytes = await req
383
+ } catch (e) {
384
+ err = new FileError(e.message, { operation: 'write', code: e.code, fd })
385
+ } finally {
386
+ req.destroy()
588
387
  }
589
388
 
590
- if (
591
- typeof data !== 'string' &&
592
- !Buffer.isBuffer(data) &&
593
- !ArrayBuffer.isView(data)
594
- ) {
595
- throw typeError(
596
- 'ERR_INVALID_ARG_TYPE',
597
- 'Data must be a string or buffer. Received type ' + typeof data
598
- )
599
- }
389
+ return done(err, bytes, cb)
390
+ }
600
391
 
392
+ function writeSync(fd, data, offset = 0, len, pos = -1) {
601
393
  if (typeof data === 'string') data = Buffer.from(data)
602
-
603
394
  if (typeof len !== 'number') len = data.byteLength - offset
604
-
605
395
  if (typeof pos !== 'number') pos = -1
606
396
 
607
- return binding.writeSync(fd, data, offset, len, pos)
608
- }
397
+ const req = new FileRequest()
609
398
 
610
- function writev(fd, buffers, pos, cb) {
611
- if (typeof fd !== 'number') {
612
- throw typeError(
613
- 'ERR_INVALID_ARG_TYPE',
614
- 'File descriptor must be a number. Received type ' +
615
- typeof fd +
616
- ' (' +
617
- fd +
618
- ')'
619
- )
620
- }
621
-
622
- if (fd < 0 || fd > 0x7fffffff) {
623
- throw typeError(
624
- 'ERR_OUT_OF_RANGE',
625
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
626
- fd
627
- )
399
+ try {
400
+ return binding.writeSync(req.handle, fd, data, offset, len, pos)
401
+ } catch (e) {
402
+ throw new FileError(e.message, { operation: 'write', code: e.code, fd })
403
+ } finally {
404
+ req.destroy()
628
405
  }
406
+ }
629
407
 
408
+ async function writev(fd, buffers, pos = -1, cb) {
630
409
  if (typeof pos === 'function') {
631
410
  cb = pos
632
411
  pos = -1
633
- } else if (typeof cb !== 'function') {
634
- throw typeError(
635
- 'ERR_INVALID_ARG_TYPE',
636
- 'Callback must be a function. Received type ' +
637
- typeof cb +
638
- ' (' +
639
- cb +
640
- ')'
641
- )
642
412
  }
643
413
 
644
414
  if (typeof pos !== 'number') pos = -1
645
415
 
646
- const req = getReq()
647
- req.callback = cb
648
- binding.writev(req.handle, fd, buffers, pos)
649
- }
416
+ const req = new FileRequest()
650
417
 
651
- function stat(filepath, cb) {
652
- if (typeof filepath !== 'string') {
653
- throw typeError(
654
- 'ERR_INVALID_ARG_TYPE',
655
- 'Path must be a string. Received type ' +
656
- typeof filepath +
657
- ' (' +
658
- filepath +
659
- ')'
660
- )
418
+ let bytes
419
+ let err = null
420
+ try {
421
+ binding.writev(req.handle, fd, buffers, pos)
422
+
423
+ bytes = await req
424
+ } catch (e) {
425
+ err = new FileError(e.message, { operation: 'writev', code: e.code, fd })
426
+ } finally {
427
+ req.destroy()
661
428
  }
662
429
 
663
- if (typeof cb !== 'function') {
664
- throw typeError(
665
- 'ERR_INVALID_ARG_TYPE',
666
- 'Callback must be a function. Received type ' +
667
- typeof cb +
668
- ' (' +
669
- cb +
670
- ')'
671
- )
430
+ return done(err, bytes, cb)
431
+ }
432
+
433
+ function writevSync(fd, buffers, pos = -1) {
434
+ if (typeof pos !== 'number') pos = -1
435
+
436
+ const req = new FileRequest()
437
+
438
+ try {
439
+ return binding.writevSync(req.handle, fd, buffers, pos)
440
+ } catch (e) {
441
+ throw new FileError(e.message, { operation: 'writev', code: e.code, fd })
442
+ } finally {
443
+ req.destroy()
672
444
  }
445
+ }
446
+
447
+ async function stat(filepath, cb) {
448
+ filepath = toNamespacedPath(filepath)
449
+
450
+ const req = new FileRequest()
673
451
 
674
- const data = new Array(Stats.length)
452
+ let st
453
+ let err = null
454
+ try {
455
+ binding.stat(req.handle, filepath)
675
456
 
676
- const req = getReq()
457
+ await req
677
458
 
678
- req.callback = function (err, _) {
679
- if (err) cb(err, null)
680
- else cb(null, new Stats(...data))
459
+ st = new Stats(...binding.requestResultStat(req.handle))
460
+ } catch (e) {
461
+ err = new FileError(e.message, {
462
+ operation: 'stat',
463
+ code: e.code,
464
+ path: filepath
465
+ })
466
+ } finally {
467
+ req.destroy()
681
468
  }
682
469
 
683
- binding.stat(req.handle, toNamespacedPath(filepath), data)
470
+ return done(err, st, cb)
684
471
  }
685
472
 
686
473
  function statSync(filepath) {
687
- if (typeof filepath !== 'string') {
688
- throw typeError(
689
- 'ERR_INVALID_ARG_TYPE',
690
- 'Path must be a string. Received type ' +
691
- typeof filepath +
692
- ' (' +
693
- filepath +
694
- ')'
695
- )
696
- }
474
+ filepath = toNamespacedPath(filepath)
697
475
 
698
- return new Stats(...binding.statSync(toNamespacedPath(filepath)))
699
- }
476
+ const req = new FileRequest()
700
477
 
701
- function lstat(filepath, cb) {
702
- if (typeof filepath !== 'string') {
703
- throw typeError(
704
- 'ERR_INVALID_ARG_TYPE',
705
- 'Path must be a string. Received type ' +
706
- typeof filepath +
707
- ' (' +
708
- filepath +
709
- ')'
710
- )
478
+ try {
479
+ binding.statSync(req.handle, filepath)
480
+
481
+ return new Stats(...binding.requestResultStat(req.handle))
482
+ } catch (e) {
483
+ throw new FileError(e.message, {
484
+ operation: 'stat',
485
+ code: e.code,
486
+ path: filepath
487
+ })
488
+ } finally {
489
+ req.destroy()
711
490
  }
491
+ }
712
492
 
713
- if (typeof cb !== 'function') {
714
- throw typeError(
715
- 'ERR_INVALID_ARG_TYPE',
716
- 'Callback must be a function. Received type ' +
717
- typeof cb +
718
- ' (' +
719
- cb +
720
- ')'
721
- )
722
- }
493
+ async function lstat(filepath, cb) {
494
+ filepath = toNamespacedPath(filepath)
495
+
496
+ const req = new FileRequest()
723
497
 
724
- const data = new Array(Stats.length)
498
+ let st
499
+ let err = null
500
+ try {
501
+ binding.lstat(req.handle, filepath)
725
502
 
726
- const req = getReq()
503
+ await req
727
504
 
728
- req.callback = function (err, _) {
729
- if (err) cb(err, null)
730
- else cb(null, new Stats(...data))
505
+ st = new Stats(...binding.requestResultStat(req.handle))
506
+ } catch (e) {
507
+ err = new FileError(e.message, {
508
+ operation: 'lstat',
509
+ code: e.code,
510
+ path: filepath
511
+ })
512
+ } finally {
513
+ req.destroy()
731
514
  }
732
515
 
733
- binding.lstat(req.handle, toNamespacedPath(filepath), data)
516
+ return done(err, st, cb)
734
517
  }
735
518
 
736
519
  function lstatSync(filepath) {
737
- if (typeof filepath !== 'string') {
738
- throw typeError(
739
- 'ERR_INVALID_ARG_TYPE',
740
- 'Path must be a string. Received type ' +
741
- typeof filepath +
742
- ' (' +
743
- filepath +
744
- ')'
745
- )
746
- }
747
-
748
- return new Stats(...binding.lstatSync(toNamespacedPath(filepath)))
749
- }
520
+ filepath = toNamespacedPath(filepath)
750
521
 
751
- function fstat(fd, cb) {
752
- if (typeof fd !== 'number') {
753
- throw typeError(
754
- 'ERR_INVALID_ARG_TYPE',
755
- 'File descriptor must be a number. Received type ' +
756
- typeof fd +
757
- ' (' +
758
- fd +
759
- ')'
760
- )
761
- }
522
+ const req = new FileRequest()
762
523
 
763
- if (fd < 0 || fd > 0x7fffffff) {
764
- throw typeError(
765
- 'ERR_OUT_OF_RANGE',
766
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
767
- fd
768
- )
524
+ try {
525
+ binding.lstatSync(req.handle, filepath)
526
+
527
+ return new Stats(...binding.requestResultStat(req.handle))
528
+ } catch (e) {
529
+ throw new FileError(e.message, {
530
+ operation: 'lstat',
531
+ code: e.code,
532
+ path: filepath
533
+ })
534
+ } finally {
535
+ req.destroy()
769
536
  }
537
+ }
770
538
 
771
- if (typeof cb !== 'function') {
772
- throw typeError(
773
- 'ERR_INVALID_ARG_TYPE',
774
- 'Callback must be a function. Received type ' +
775
- typeof cb +
776
- ' (' +
777
- cb +
778
- ')'
779
- )
780
- }
539
+ async function fstat(fd, cb) {
540
+ const req = new FileRequest()
781
541
 
782
- const data = new Array(Stats.length)
542
+ let st
543
+ let err = null
544
+ try {
545
+ binding.fstat(req.handle, fd)
783
546
 
784
- const req = getReq()
547
+ await req
785
548
 
786
- req.callback = function (err, _) {
787
- if (err) cb(err, null)
788
- else cb(null, new Stats(...data))
549
+ st = new Stats(...binding.requestResultStat(req.handle))
550
+ } catch (e) {
551
+ err = new FileError(e.message, { operation: 'fstat', code: e.code, fd })
552
+ } finally {
553
+ req.destroy()
789
554
  }
790
555
 
791
- binding.fstat(req.handle, fd, data)
556
+ return done(err, st, cb)
792
557
  }
793
558
 
794
559
  function fstatSync(fd) {
795
- if (typeof fd !== 'number') {
796
- throw typeError(
797
- 'ERR_INVALID_ARG_TYPE',
798
- 'File descriptor must be a number. Received type ' +
799
- typeof fd +
800
- ' (' +
801
- fd +
802
- ')'
803
- )
804
- }
560
+ const req = new FileRequest()
805
561
 
806
- if (fd < 0 || fd > 0x7fffffff) {
807
- throw typeError(
808
- 'ERR_OUT_OF_RANGE',
809
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
810
- fd
811
- )
812
- }
813
-
814
- return new Stats(...binding.fstatSync(fd))
815
- }
816
-
817
- function ftruncate(fd, len, cb) {
818
- if (typeof fd !== 'number') {
819
- throw typeError(
820
- 'ERR_INVALID_ARG_TYPE',
821
- 'File descriptor must be a number. Received type ' +
822
- typeof fd +
823
- ' (' +
824
- fd +
825
- ')'
826
- )
827
- }
562
+ try {
563
+ binding.fstatSync(req.handle, fd)
828
564
 
829
- if (fd < 0 || fd > 0x7fffffff) {
830
- throw typeError(
831
- 'ERR_OUT_OF_RANGE',
832
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
833
- fd
834
- )
565
+ return new Stats(...binding.requestResultStat(req.handle))
566
+ } catch (e) {
567
+ throw new FileError(e.message, { operation: 'fstat', code: e.code, fd })
568
+ } finally {
569
+ req.destroy()
835
570
  }
571
+ }
836
572
 
573
+ async function ftruncate(fd, len = 0, cb) {
837
574
  if (typeof len === 'function') {
838
575
  cb = len
839
576
  len = 0
840
- } else if (typeof cb !== 'function') {
841
- throw typeError(
842
- 'ERR_INVALID_ARG_TYPE',
843
- 'Callback must be a function. Received type ' +
844
- typeof cb +
845
- ' (' +
846
- cb +
847
- ')'
848
- )
849
577
  }
850
578
 
851
579
  if (typeof len !== 'number') len = 0
852
580
 
853
- const req = getReq()
854
- req.callback = cb
855
- binding.ftruncate(req.handle, fd, len)
856
- }
581
+ const req = new FileRequest()
857
582
 
858
- function ftruncateSync(fd, len) {
859
- if (typeof fd !== 'number') {
860
- throw typeError(
861
- 'ERR_INVALID_ARG_TYPE',
862
- 'File descriptor must be a number. Received type ' +
863
- typeof fd +
864
- ' (' +
865
- fd +
866
- ')'
867
- )
868
- }
583
+ let err = null
584
+ try {
585
+ binding.ftruncate(req.handle, fd, len)
869
586
 
870
- if (fd < 0 || fd > 0x7fffffff) {
871
- throw typeError(
872
- 'ERR_OUT_OF_RANGE',
873
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
874
- fd
875
- )
587
+ await req
588
+ } catch (e) {
589
+ err = new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
590
+ } finally {
591
+ req.destroy()
876
592
  }
877
593
 
594
+ return done(err, cb)
595
+ }
596
+
597
+ function ftruncateSync(fd, len = 0) {
878
598
  if (typeof len !== 'number') len = 0
879
599
 
880
- binding.ftruncateSync(fd, len)
881
- }
600
+ const req = new FileRequest()
882
601
 
883
- function chmod(filepath, mode, cb) {
884
- if (typeof filepath !== 'string') {
885
- throw typeError(
886
- 'ERR_INVALID_ARG_TYPE',
887
- 'Path must be a string. Received type ' +
888
- typeof filepath +
889
- ' (' +
890
- filepath +
891
- ')'
892
- )
602
+ try {
603
+ binding.ftruncateSync(req.handle, fd, len)
604
+ } catch (e) {
605
+ throw new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
606
+ } finally {
607
+ req.destroy()
893
608
  }
609
+ }
610
+
611
+ async function chmod(filepath, mode, cb) {
612
+ if (typeof mode === 'string') mode = toMode(mode)
613
+
614
+ filepath = toNamespacedPath(filepath)
615
+
616
+ const req = new FileRequest()
894
617
 
895
- if (typeof mode === 'string') mode = modeToNumber(mode)
618
+ let err = null
619
+ try {
620
+ binding.chmod(req.handle, filepath, mode)
621
+
622
+ await req
623
+ } catch (e) {
624
+ err = new FileError(e.message, {
625
+ operation: 'chmod',
626
+ code: e.code,
627
+ path: filepath
628
+ })
629
+ } finally {
630
+ req.destroy()
631
+ }
896
632
 
897
- const req = getReq()
898
- req.callback = cb
899
- binding.chmod(req.handle, toNamespacedPath(filepath), mode)
633
+ return done(err, cb)
900
634
  }
901
635
 
902
636
  function chmodSync(filepath, mode) {
903
- if (typeof filepath !== 'string') {
904
- throw typeError(
905
- 'ERR_INVALID_ARG_TYPE',
906
- 'Path must be a string. Received type ' +
907
- typeof filepath +
908
- ' (' +
909
- filepath +
910
- ')'
911
- )
912
- }
637
+ if (typeof mode === 'string') mode = toMode(mode)
913
638
 
914
- if (typeof mode === 'string') mode = modeToNumber(mode)
639
+ filepath = toNamespacedPath(filepath)
915
640
 
916
- binding.chmodSync(toNamespacedPath(filepath), mode)
917
- }
641
+ const req = new FileRequest()
918
642
 
919
- function fchmod(fd, mode, cb) {
920
- if (typeof fd !== 'number') {
921
- throw typeError(
922
- 'ERR_INVALID_ARG_TYPE',
923
- 'File descriptor must be a number. Received type ' +
924
- typeof fd +
925
- ' (' +
926
- fd +
927
- ')'
928
- )
643
+ try {
644
+ binding.chmodSync(req.handle, filepath, mode)
645
+ } catch (e) {
646
+ throw new FileError(e.message, {
647
+ operation: 'chmod',
648
+ code: e.code,
649
+ path: filepath
650
+ })
651
+ } finally {
652
+ req.destroy()
929
653
  }
654
+ }
930
655
 
931
- if (fd < 0 || fd > 0x7fffffff) {
932
- throw typeError(
933
- 'ERR_OUT_OF_RANGE',
934
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
935
- fd
936
- )
937
- }
656
+ async function fchmod(fd, mode, cb) {
657
+ if (typeof mode === 'string') mode = toMode(mode)
938
658
 
939
- if (typeof mode === 'string') mode = modeToNumber(mode)
659
+ const req = new FileRequest()
940
660
 
941
- const req = getReq()
942
- req.callback = cb
943
- binding.fchmod(req.handle, fd, mode)
944
- }
661
+ let err = null
662
+ try {
663
+ binding.fchmod(req.handle, fd, mode)
945
664
 
946
- function fchmodSync(fd, mode) {
947
- if (typeof fd !== 'number') {
948
- throw typeError(
949
- 'ERR_INVALID_ARG_TYPE',
950
- 'File descriptor must be a number. Received type ' +
951
- typeof fd +
952
- ' (' +
953
- fd +
954
- ')'
955
- )
665
+ await req
666
+ } catch (e) {
667
+ err = new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
668
+ } finally {
669
+ req.destroy()
956
670
  }
957
671
 
958
- if (fd < 0 || fd > 0x7fffffff) {
959
- throw typeError(
960
- 'ERR_OUT_OF_RANGE',
961
- 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' +
962
- fd
963
- )
964
- }
672
+ return done(err, cb)
673
+ }
965
674
 
966
- if (typeof mode === 'string') mode = modeToNumber(mode)
675
+ function fchmodSync(fd, mode) {
676
+ if (typeof mode === 'string') mode = toMode(mode)
967
677
 
968
- binding.fchmodSync(fd, mode)
969
- }
678
+ const req = new FileRequest()
970
679
 
971
- function utimes(filepath, atime, mtime, cb) {
972
- if (typeof filepath !== 'string') {
973
- throw typeError(
974
- 'ERR_INVALID_ARG_TYPE',
975
- 'Path must be a string. Received type ' +
976
- typeof filepath +
977
- ' (' +
978
- filepath +
979
- ')'
980
- )
680
+ try {
681
+ binding.fchmodSync(req.handle, fd, mode)
682
+ } catch (e) {
683
+ throw new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
684
+ } finally {
685
+ req.destroy()
981
686
  }
687
+ }
982
688
 
689
+ async function utimes(filepath, atime, mtime, cb) {
983
690
  if (typeof atime !== 'number') atime = atime.getTime() / 1000
984
691
  if (typeof mtime !== 'number') mtime = mtime.getTime() / 1000
985
692
 
986
- const req = getReq()
987
- req.callback = cb
988
- binding.utimes(req.handle, toNamespacedPath(filepath), atime, mtime)
989
- }
693
+ filepath = toNamespacedPath(filepath)
990
694
 
991
- function utimesSync(filepath, atime, mtime) {
992
- if (typeof filepath !== 'string') {
993
- throw typeError(
994
- 'ERR_INVALID_ARG_TYPE',
995
- 'Path must be a string. Received type ' +
996
- typeof filepath +
997
- ' (' +
998
- filepath +
999
- ')'
1000
- )
695
+ const req = new FileRequest()
696
+
697
+ let err = null
698
+ try {
699
+ binding.utimes(req.handle, filepath, atime, mtime)
700
+
701
+ await req
702
+ } catch (e) {
703
+ err = new FileError(e.message, {
704
+ operation: 'utimes',
705
+ code: e.code,
706
+ path: filepath
707
+ })
708
+ } finally {
709
+ req.destroy()
1001
710
  }
1002
711
 
712
+ return done(err, cb)
713
+ }
714
+
715
+ function utimesSync(filepath, atime, mtime) {
1003
716
  if (typeof atime !== 'number') atime = atime.getTime() / 1000
1004
717
  if (typeof mtime !== 'number') mtime = mtime.getTime() / 1000
1005
718
 
1006
- binding.utimesSync(toNamespacedPath(filepath), atime, mtime)
1007
- }
1008
-
1009
- function mkdirRecursive(filepath, mode, cb) {
1010
719
  filepath = toNamespacedPath(filepath)
1011
720
 
1012
- mkdir(filepath, { mode }, function (err) {
1013
- if (err === null) return cb(null, 0, null)
1014
-
1015
- if (err.code !== 'ENOENT') {
1016
- stat(filepath, function (e, st) {
1017
- if (e) return cb(e, e.errno, null)
1018
- if (st.isDirectory()) return cb(null, 0, null)
1019
- cb(err, err.errno, null)
1020
- })
1021
- return
1022
- }
1023
-
1024
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
1025
- const i = filepath.lastIndexOf(path.sep)
1026
- if (i <= 0) return cb(err, err.errno, null)
1027
-
1028
- mkdirRecursive(filepath.slice(0, i), mode, function (err) {
1029
- if (err) return cb(err, err.errno, null)
721
+ const req = new FileRequest()
1030
722
 
1031
- mkdir(filepath, { mode }, function (err) {
1032
- if (err === null) return cb(null, 0, null)
1033
-
1034
- stat(filepath, function (e, st) {
1035
- if (e) return cb(e, e.errno, null)
1036
- if (st.isDirectory()) return cb(null, 0, null)
1037
- cb(err, err.errno, null)
1038
- })
1039
- })
723
+ try {
724
+ binding.utimesSync(req.handle, filepath, atime, mtime)
725
+ } catch (e) {
726
+ throw new FileError(e.message, {
727
+ operation: 'utimes',
728
+ code: e.code,
729
+ path: filepath
1040
730
  })
1041
- })
1042
- }
1043
-
1044
- function mkdir(filepath, opts, cb) {
1045
- if (typeof filepath !== 'string') {
1046
- throw typeError(
1047
- 'ERR_INVALID_ARG_TYPE',
1048
- 'Path must be a string. Received type ' +
1049
- typeof filepath +
1050
- ' (' +
1051
- filepath +
1052
- ')'
1053
- )
731
+ } finally {
732
+ req.destroy()
1054
733
  }
734
+ }
1055
735
 
736
+ async function mkdir(filepath, opts, cb) {
1056
737
  if (typeof opts === 'function') {
1057
738
  cb = opts
1058
739
  opts = { mode: 0o777 }
1059
- } else if (typeof cb !== 'function') {
1060
- throw typeError(
1061
- 'ERR_INVALID_ARG_TYPE',
1062
- 'Callback must be a function. Received type ' +
1063
- typeof cb +
1064
- ' (' +
1065
- cb +
1066
- ')'
1067
- )
1068
740
  }
1069
741
 
1070
742
  if (typeof opts === 'number') opts = { mode: opts }
@@ -1074,53 +746,55 @@ function mkdir(filepath, opts, cb) {
1074
746
 
1075
747
  filepath = toNamespacedPath(filepath)
1076
748
 
1077
- if (opts.recursive) return mkdirRecursive(filepath, mode, cb)
749
+ if (opts.recursive) {
750
+ try {
751
+ await mkdir(filepath, { mode })
752
+ } catch (err) {
753
+ if (err.code !== 'ENOENT' && (await stat(filepath)).isDirectory()) {
754
+ return ok(cb)
755
+ }
1078
756
 
1079
- const req = getReq()
1080
- req.callback = cb
1081
- binding.mkdir(req.handle, filepath, mode)
1082
- }
757
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
758
+ const i = filepath.lastIndexOf(path.sep)
759
+ if (i <= 0) return fail(err, cb)
1083
760
 
1084
- function mkdirRecursiveSync(filepath, mode) {
1085
- filepath = toNamespacedPath(filepath)
761
+ await mkdir(filepath.slice(0, i), { mode, recursive: true })
1086
762
 
1087
- try {
1088
- mkdirSync(filepath, { mode })
1089
- } catch (err) {
1090
- if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
1091
- return
1092
- }
763
+ try {
764
+ await mkdir(filepath, { mode })
765
+ } catch (err) {
766
+ if ((await stat(filepath)).isDirectory()) {
767
+ return ok(cb)
768
+ }
1093
769
 
1094
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
1095
- const i = filepath.lastIndexOf(path.sep)
1096
- if (i <= 0) throw err
770
+ return fail(err, cb)
771
+ }
772
+ }
1097
773
 
1098
- mkdirRecursiveSync(filepath.slice(0, i), { mode })
774
+ return ok(cb)
775
+ }
1099
776
 
1100
- try {
1101
- mkdirSync(filepath, { mode })
1102
- } catch (err) {
1103
- if (statSync(filepath).isDirectory()) {
1104
- return
1105
- }
777
+ const req = new FileRequest()
1106
778
 
1107
- throw err
1108
- }
779
+ let err = null
780
+ try {
781
+ binding.mkdir(req.handle, filepath, mode)
782
+
783
+ await req
784
+ } catch (e) {
785
+ err = new FileError(e.message, {
786
+ operation: 'mkdir',
787
+ code: e.code,
788
+ path: filepath
789
+ })
790
+ } finally {
791
+ req.destroy()
1109
792
  }
793
+
794
+ return done(err, cb)
1110
795
  }
1111
796
 
1112
797
  function mkdirSync(filepath, opts) {
1113
- if (typeof filepath !== 'string') {
1114
- throw typeError(
1115
- 'ERR_INVALID_ARG_TYPE',
1116
- 'Path must be a string. Received type ' +
1117
- typeof filepath +
1118
- ' (' +
1119
- filepath +
1120
- ')'
1121
- )
1122
- }
1123
-
1124
798
  if (typeof opts === 'number') opts = { mode: opts }
1125
799
  else if (!opts) opts = {}
1126
800
 
@@ -1128,163 +802,137 @@ function mkdirSync(filepath, opts) {
1128
802
 
1129
803
  filepath = toNamespacedPath(filepath)
1130
804
 
1131
- if (opts.recursive) return mkdirRecursiveSync(filepath, mode)
805
+ if (opts.recursive) {
806
+ try {
807
+ mkdirSync(filepath, { mode })
808
+ } catch (err) {
809
+ if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
810
+ return
811
+ }
812
+
813
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
814
+ const i = filepath.lastIndexOf(path.sep)
815
+ if (i <= 0) throw err
1132
816
 
1133
- binding.mkdirSync(filepath, mode)
1134
- }
817
+ mkdirSync(filepath.slice(0, i), { mode, recursive: true })
1135
818
 
1136
- function rmdir(filepath, cb) {
1137
- if (typeof filepath !== 'string') {
1138
- throw typeError(
1139
- 'ERR_INVALID_ARG_TYPE',
1140
- 'Path must be a string. Received type ' +
1141
- typeof filepath +
1142
- ' (' +
1143
- filepath +
1144
- ')'
1145
- )
1146
- }
819
+ try {
820
+ mkdirSync(filepath, { mode })
821
+ } catch (err) {
822
+ if (statSync(filepath).isDirectory()) {
823
+ return
824
+ }
1147
825
 
1148
- if (typeof cb !== 'function') {
1149
- throw typeError(
1150
- 'ERR_INVALID_ARG_TYPE',
1151
- 'Callback must be a function. Received type ' +
1152
- typeof cb +
1153
- ' (' +
1154
- cb +
1155
- ')'
1156
- )
826
+ throw err
827
+ }
828
+ }
829
+
830
+ return
1157
831
  }
1158
832
 
1159
- const req = getReq()
1160
- req.callback = cb
1161
- binding.rmdir(req.handle, toNamespacedPath(filepath))
1162
- }
833
+ const req = new FileRequest()
1163
834
 
1164
- function rmdirSync(filepath) {
1165
- if (typeof filepath !== 'string') {
1166
- throw typeError(
1167
- 'ERR_INVALID_ARG_TYPE',
1168
- 'Path must be a string. Received type ' +
1169
- typeof filepath +
1170
- ' (' +
1171
- filepath +
1172
- ')'
1173
- )
835
+ try {
836
+ binding.mkdirSync(req.handle, filepath, mode)
837
+ } catch (e) {
838
+ throw new FileError(e.message, {
839
+ operation: 'mkdir',
840
+ code: e.code,
841
+ path: filepath
842
+ })
843
+ } finally {
844
+ req.destroy()
1174
845
  }
1175
-
1176
- binding.rmdirSync(toNamespacedPath(filepath))
1177
846
  }
1178
847
 
1179
- function rmRecursive(filepath, opts, cb) {
848
+ async function rmdir(filepath, cb) {
1180
849
  filepath = toNamespacedPath(filepath)
1181
850
 
1182
- rmdir(filepath, function (err) {
1183
- if (err === null) return cb(null)
851
+ const req = new FileRequest()
1184
852
 
1185
- if (err.code !== 'ENOTEMPTY') return cb(err)
1186
-
1187
- readdir(filepath, function (err, files) {
1188
- if (err) return cb(err)
1189
-
1190
- if (files.length === 0) return rmdir(filepath, cb)
853
+ let err = null
854
+ try {
855
+ binding.rmdir(req.handle, filepath)
856
+
857
+ await req
858
+ } catch (e) {
859
+ err = new FileError(e.message, {
860
+ operation: 'rmdir',
861
+ code: e.code,
862
+ path: filepath
863
+ })
864
+ } finally {
865
+ req.destroy()
866
+ }
1191
867
 
1192
- let missing = files.length
1193
- let done = false
868
+ return done(err, cb)
869
+ }
1194
870
 
1195
- for (const file of files) {
1196
- rm(filepath + path.sep + file, opts, function (err) {
1197
- if (done) return
871
+ function rmdirSync(filepath) {
872
+ filepath = toNamespacedPath(filepath)
1198
873
 
1199
- if (err) {
1200
- done = true
1201
- return cb(err)
1202
- }
874
+ const req = new FileRequest()
1203
875
 
1204
- if (--missing === 0) rmdir(filepath, cb)
1205
- })
1206
- }
876
+ try {
877
+ binding.rmdirSync(req.handle, filepath)
878
+ } catch (e) {
879
+ throw new FileError(e.message, {
880
+ operation: 'rmdir',
881
+ code: e.code,
882
+ path: filepath
1207
883
  })
1208
- })
1209
- }
1210
-
1211
- function rm(filepath, opts, cb) {
1212
- if (typeof filepath !== 'string') {
1213
- throw typeError(
1214
- 'ERR_INVALID_ARG_TYPE',
1215
- 'Path must be a string. Received type ' +
1216
- typeof filepath +
1217
- ' (' +
1218
- filepath +
1219
- ')'
1220
- )
884
+ } finally {
885
+ req.destroy()
1221
886
  }
887
+ }
1222
888
 
889
+ async function rm(filepath, opts, cb) {
1223
890
  if (typeof opts === 'function') {
1224
891
  cb = opts
1225
892
  opts = {}
1226
- } else if (typeof cb !== 'function') {
1227
- throw typeError(
1228
- 'ERR_INVALID_ARG_TYPE',
1229
- 'Callback must be a function. Received type ' +
1230
- typeof cb +
1231
- ' (' +
1232
- cb +
1233
- ')'
1234
- )
1235
893
  }
1236
894
 
1237
895
  if (!opts) opts = {}
1238
896
 
1239
897
  filepath = toNamespacedPath(filepath)
1240
898
 
1241
- lstat(filepath, function (err, st) {
1242
- if (err) {
1243
- return cb(err.code === 'ENOENT' && opts.force ? null : err)
1244
- }
899
+ try {
900
+ const st = await lstat(filepath)
1245
901
 
1246
902
  if (st.isDirectory()) {
1247
- if (opts.recursive) return rmRecursive(filepath, opts, cb)
1248
-
1249
- const err = new Error('is a directory')
1250
- err.code = 'EISDIR'
1251
- return cb(err)
1252
- }
1253
-
1254
- unlink(filepath, cb)
1255
- })
1256
- }
903
+ if (opts.recursive) {
904
+ try {
905
+ await rmdir(filepath)
906
+ } catch (err) {
907
+ if (err.code !== 'ENOTEMPTY') throw err
1257
908
 
1258
- function rmRecursiveSync(filepath, opts) {
1259
- filepath = toNamespacedPath(filepath)
1260
-
1261
- try {
1262
- rmdirSync(filepath)
1263
- } catch (err) {
1264
- if (err.code !== 'ENOTEMPTY') throw err
909
+ const files = await readdir(filepath)
1265
910
 
1266
- const files = readdirSync(filepath)
911
+ for (const file of files) {
912
+ await rm(filepath + path.sep + file, opts)
913
+ }
1267
914
 
1268
- for (const file of files) {
1269
- rmSync(filepath + path.sep + file, opts)
915
+ await rmdir(filepath)
916
+ }
917
+ } else {
918
+ fail(
919
+ new FileError('is a directory', {
920
+ operation: 'rm',
921
+ code: 'EISDIR',
922
+ path: filepath
923
+ }),
924
+ cb
925
+ )
926
+ }
927
+ } else {
928
+ await unlink(filepath)
1270
929
  }
1271
-
1272
- rmdirSync(filepath)
930
+ } catch (err) {
931
+ if (err.code !== 'ENOENT' || !opts.force) fail(err, cb)
1273
932
  }
1274
933
  }
1275
934
 
1276
935
  function rmSync(filepath, opts) {
1277
- if (typeof filepath !== 'string') {
1278
- throw typeError(
1279
- 'ERR_INVALID_ARG_TYPE',
1280
- 'Path must be a string. Received type ' +
1281
- typeof filepath +
1282
- ' (' +
1283
- filepath +
1284
- ')'
1285
- )
1286
- }
1287
-
1288
936
  if (!opts) opts = {}
1289
937
 
1290
938
  filepath = toNamespacedPath(filepath)
@@ -1293,178 +941,175 @@ function rmSync(filepath, opts) {
1293
941
  const st = lstatSync(filepath)
1294
942
 
1295
943
  if (st.isDirectory()) {
1296
- if (opts.recursive) return rmRecursiveSync(filepath, opts)
944
+ if (opts.recursive) {
945
+ try {
946
+ rmdirSync(filepath)
947
+ } catch (err) {
948
+ if (err.code !== 'ENOTEMPTY') throw err
1297
949
 
1298
- const err = new Error('is a directory')
1299
- err.code = 'EISDIR'
1300
- throw err
1301
- }
950
+ const files = readdirSync(filepath)
951
+
952
+ for (const file of files) {
953
+ rmSync(filepath + path.sep + file, opts)
954
+ }
1302
955
 
1303
- unlinkSync(filepath)
956
+ rmdirSync(filepath)
957
+ }
958
+ } else {
959
+ throw new FileError('is a directory', {
960
+ operation: 'rm',
961
+ code: 'EISDIR',
962
+ path: filepath
963
+ })
964
+ }
965
+ } else {
966
+ unlinkSync(filepath)
967
+ }
1304
968
  } catch (err) {
1305
969
  if (err.code !== 'ENOENT' || !opts.force) throw err
1306
970
  }
1307
971
  }
1308
972
 
1309
- function unlink(filepath, cb) {
1310
- if (typeof filepath !== 'string') {
1311
- throw typeError(
1312
- 'ERR_INVALID_ARG_TYPE',
1313
- 'Path must be a string. Received type ' +
1314
- typeof filepath +
1315
- ' (' +
1316
- filepath +
1317
- ')'
1318
- )
1319
- }
973
+ async function unlink(filepath, cb) {
974
+ filepath = toNamespacedPath(filepath)
1320
975
 
1321
- if (typeof cb !== 'function') {
1322
- throw typeError(
1323
- 'ERR_INVALID_ARG_TYPE',
1324
- 'Callback must be a function. Received type ' +
1325
- typeof cb +
1326
- ' (' +
1327
- cb +
1328
- ')'
1329
- )
976
+ const req = new FileRequest()
977
+
978
+ let err = null
979
+ try {
980
+ binding.unlink(req.handle, filepath)
981
+
982
+ await req
983
+ } catch (e) {
984
+ err = new FileError(e.message, {
985
+ operation: 'unlink',
986
+ code: e.code,
987
+ path: filepath
988
+ })
989
+ } finally {
990
+ req.destroy()
1330
991
  }
1331
992
 
1332
- const req = getReq()
1333
- req.callback = cb
1334
- binding.unlink(req.handle, toNamespacedPath(filepath))
993
+ return done(err, cb)
1335
994
  }
1336
995
 
1337
996
  function unlinkSync(filepath) {
1338
- if (typeof filepath !== 'string') {
1339
- throw typeError(
1340
- 'ERR_INVALID_ARG_TYPE',
1341
- 'Path must be a string. Received type ' +
1342
- typeof filepath +
1343
- ' (' +
1344
- filepath +
1345
- ')'
1346
- )
1347
- }
997
+ filepath = toNamespacedPath(filepath)
1348
998
 
1349
- binding.unlinkSync(toNamespacedPath(filepath))
1350
- }
999
+ const req = new FileRequest()
1351
1000
 
1352
- function rename(src, dst, cb) {
1353
- if (typeof src !== 'string') {
1354
- throw typeError(
1355
- 'ERR_INVALID_ARG_TYPE',
1356
- 'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
1357
- )
1001
+ try {
1002
+ binding.unlinkSync(req.handle, filepath)
1003
+ } catch (e) {
1004
+ throw new FileError(e.message, {
1005
+ operation: 'unlink',
1006
+ code: e.code,
1007
+ path: filepath
1008
+ })
1009
+ } finally {
1010
+ req.destroy()
1358
1011
  }
1012
+ }
1359
1013
 
1360
- if (typeof dst !== 'string') {
1361
- throw typeError(
1362
- 'ERR_INVALID_ARG_TYPE',
1363
- 'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
1364
- )
1365
- }
1014
+ async function rename(src, dst, cb) {
1015
+ src = toNamespacedPath(src)
1016
+ dst = toNamespacedPath(dst)
1366
1017
 
1367
- if (typeof cb !== 'function') {
1368
- throw typeError(
1369
- 'ERR_INVALID_ARG_TYPE',
1370
- 'Callback must be a function. Received type ' +
1371
- typeof cb +
1372
- ' (' +
1373
- cb +
1374
- ')'
1375
- )
1018
+ const req = new FileRequest()
1019
+
1020
+ let err = null
1021
+ try {
1022
+ binding.rename(req.handle, src, dst)
1023
+
1024
+ await req
1025
+ } catch (e) {
1026
+ err = new FileError(e.message, {
1027
+ operation: 'rename',
1028
+ code: e.code,
1029
+ path: src,
1030
+ destination: dst
1031
+ })
1032
+ } finally {
1033
+ req.destroy()
1376
1034
  }
1377
1035
 
1378
- const req = getReq()
1379
- req.callback = cb
1380
- binding.rename(req.handle, src, dst)
1036
+ return done(err, cb)
1381
1037
  }
1382
1038
 
1383
1039
  function renameSync(src, dst) {
1384
- if (typeof src !== 'string') {
1385
- throw typeError(
1386
- 'ERR_INVALID_ARG_TYPE',
1387
- 'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
1388
- )
1389
- }
1040
+ src = toNamespacedPath(src)
1041
+ dst = toNamespacedPath(dst)
1390
1042
 
1391
- if (typeof dst !== 'string') {
1392
- throw typeError(
1393
- 'ERR_INVALID_ARG_TYPE',
1394
- 'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
1395
- )
1396
- }
1043
+ const req = new FileRequest()
1397
1044
 
1398
- binding.renameSync(src, dst)
1045
+ try {
1046
+ binding.renameSync(req.handle, src, dst)
1047
+ } catch (e) {
1048
+ throw new FileError(e.message, {
1049
+ operation: 'rename',
1050
+ code: e.code,
1051
+ path: src,
1052
+ destination: dst
1053
+ })
1054
+ } finally {
1055
+ req.destroy()
1056
+ }
1399
1057
  }
1400
1058
 
1401
- function copyFile(src, dst, mode, cb) {
1059
+ async function copyFile(src, dst, mode = 0, cb) {
1402
1060
  if (typeof mode === 'function') {
1403
1061
  cb = mode
1404
1062
  mode = 0
1405
1063
  }
1406
1064
 
1407
- if (typeof src !== 'string') {
1408
- throw typeError(
1409
- 'ERR_INVALID_ARG_TYPE',
1410
- 'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
1411
- )
1412
- }
1065
+ src = toNamespacedPath(src)
1066
+ dst = toNamespacedPath(dst)
1413
1067
 
1414
- if (typeof dst !== 'string') {
1415
- throw typeError(
1416
- 'ERR_INVALID_ARG_TYPE',
1417
- 'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
1418
- )
1068
+ const req = new FileRequest()
1069
+
1070
+ let err = null
1071
+ try {
1072
+ binding.copyfile(req.handle, src, dst, mode)
1073
+
1074
+ await req
1075
+ } catch (e) {
1076
+ err = new FileError(e.message, {
1077
+ operation: 'copyfile',
1078
+ code: e.code,
1079
+ path: src,
1080
+ destination: dst
1081
+ })
1082
+ } finally {
1083
+ req.destroy()
1419
1084
  }
1420
1085
 
1421
- const req = getReq()
1422
- req.callback = cb
1423
- binding.copyfile(req.handle, src, dst, mode)
1086
+ return done(err, cb)
1424
1087
  }
1425
1088
 
1426
1089
  function copyFileSync(src, dst, mode = 0) {
1427
- if (typeof src !== 'string') {
1428
- throw typeError(
1429
- 'ERR_INVALID_ARG_TYPE',
1430
- 'Path must be a string. Received type ' + typeof src + ' (' + src + ')'
1431
- )
1432
- }
1090
+ src = toNamespacedPath(src)
1091
+ dst = toNamespacedPath(dst)
1433
1092
 
1434
- if (typeof dst !== 'string') {
1435
- throw typeError(
1436
- 'ERR_INVALID_ARG_TYPE',
1437
- 'Path must be a string. Received type ' + typeof dst + ' (' + dst + ')'
1438
- )
1439
- }
1440
-
1441
- binding.copyfileSync(src, dst, mode)
1442
- }
1093
+ const req = new FileRequest()
1443
1094
 
1444
- function realpath(filepath, opts, cb) {
1445
- if (typeof filepath !== 'string') {
1446
- throw typeError(
1447
- 'ERR_INVALID_ARG_TYPE',
1448
- 'Path must be a string. Received type ' +
1449
- typeof filepath +
1450
- ' (' +
1451
- filepath +
1452
- ')'
1453
- )
1095
+ try {
1096
+ binding.copyfileSync(req.handle, src, dst, mode)
1097
+ } catch (e) {
1098
+ throw new FileError(e.message, {
1099
+ operation: 'copyfile',
1100
+ code: e.code,
1101
+ path: src,
1102
+ destination: dst
1103
+ })
1104
+ } finally {
1105
+ req.destroy()
1454
1106
  }
1107
+ }
1455
1108
 
1109
+ async function realpath(filepath, opts, cb) {
1456
1110
  if (typeof opts === 'function') {
1457
1111
  cb = opts
1458
1112
  opts = {}
1459
- } else if (typeof cb !== 'function') {
1460
- throw typeError(
1461
- 'ERR_INVALID_ARG_TYPE',
1462
- 'Callback must be a function. Received type ' +
1463
- typeof cb +
1464
- ' (' +
1465
- cb +
1466
- ')'
1467
- )
1468
1113
  }
1469
1114
 
1470
1115
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1472,70 +1117,66 @@ function realpath(filepath, opts, cb) {
1472
1117
 
1473
1118
  const { encoding = 'utf8' } = opts
1474
1119
 
1475
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
1120
+ filepath = toNamespacedPath(filepath)
1121
+
1122
+ const req = new FileRequest()
1123
+
1124
+ let res
1125
+ let err = null
1126
+ try {
1127
+ binding.realpath(req.handle, filepath)
1128
+
1129
+ await req
1476
1130
 
1477
- const req = getReq()
1131
+ res = Buffer.from(binding.requestResultString(req.handle))
1478
1132
 
1479
- req.callback = function (err, _) {
1480
- if (err) return cb(err, null)
1481
- let path = data.subarray(0, data.indexOf(0))
1482
- if (encoding !== 'buffer') path = path.toString(encoding)
1483
- cb(null, path)
1133
+ if (encoding !== 'buffer') res = res.toString(encoding)
1134
+ } catch (e) {
1135
+ err = new FileError(e.message, {
1136
+ operation: 'realpath',
1137
+ code: e.code,
1138
+ path: filepath
1139
+ })
1140
+ } finally {
1141
+ req.destroy()
1484
1142
  }
1485
1143
 
1486
- binding.realpath(req.handle, toNamespacedPath(filepath), data)
1144
+ return done(err, res, cb)
1487
1145
  }
1488
1146
 
1489
1147
  function realpathSync(filepath, opts) {
1490
- if (typeof filepath !== 'string') {
1491
- throw typeError(
1492
- 'ERR_INVALID_ARG_TYPE',
1493
- 'Path must be a string. Received type ' +
1494
- typeof filepath +
1495
- ' (' +
1496
- filepath +
1497
- ')'
1498
- )
1499
- }
1500
-
1501
1148
  if (typeof opts === 'string') opts = { encoding: opts }
1502
1149
  else if (!opts) opts = {}
1503
1150
 
1504
1151
  const { encoding = 'utf8' } = opts
1505
1152
 
1506
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
1153
+ filepath = toNamespacedPath(filepath)
1507
1154
 
1508
- binding.realpathSync(toNamespacedPath(filepath), data)
1155
+ const req = new FileRequest()
1509
1156
 
1510
- filepath = data.subarray(0, data.indexOf(0))
1511
- if (encoding !== 'buffer') filepath = filepath.toString(encoding)
1512
- return filepath
1513
- }
1157
+ try {
1158
+ binding.realpathSync(req.handle, filepath)
1514
1159
 
1515
- function readlink(filepath, opts, cb) {
1516
- if (typeof filepath !== 'string') {
1517
- throw typeError(
1518
- 'ERR_INVALID_ARG_TYPE',
1519
- 'Path must be a string. Received type ' +
1520
- typeof filepath +
1521
- ' (' +
1522
- filepath +
1523
- ')'
1524
- )
1160
+ let res = Buffer.from(binding.requestResultString(req.handle))
1161
+
1162
+ if (encoding !== 'buffer') res = res.toString(encoding)
1163
+
1164
+ return res
1165
+ } catch (e) {
1166
+ throw new FileError(e.message, {
1167
+ operation: 'realpath',
1168
+ code: e.code,
1169
+ path: filepath
1170
+ })
1171
+ } finally {
1172
+ req.destroy()
1525
1173
  }
1174
+ }
1526
1175
 
1176
+ async function readlink(filepath, opts, cb) {
1527
1177
  if (typeof opts === 'function') {
1528
1178
  cb = opts
1529
1179
  opts = {}
1530
- } else if (typeof cb !== 'function') {
1531
- throw typeError(
1532
- 'ERR_INVALID_ARG_TYPE',
1533
- 'Callback must be a function. Received type ' +
1534
- typeof cb +
1535
- ' (' +
1536
- cb +
1537
- ')'
1538
- )
1539
1180
  }
1540
1181
 
1541
1182
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1543,44 +1184,60 @@ function readlink(filepath, opts, cb) {
1543
1184
 
1544
1185
  const { encoding = 'utf8' } = opts
1545
1186
 
1546
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
1187
+ filepath = toNamespacedPath(filepath)
1188
+
1189
+ const req = new FileRequest()
1190
+
1191
+ let res
1192
+ let err = null
1193
+ try {
1194
+ binding.readlink(req.handle, filepath)
1195
+
1196
+ await req
1547
1197
 
1548
- const req = getReq()
1198
+ res = Buffer.from(binding.requestResultString(req.handle))
1549
1199
 
1550
- req.callback = function (err, _) {
1551
- if (err) return cb(err, null)
1552
- let path = data.subarray(0, data.indexOf(0))
1553
- if (encoding !== 'buffer') path = path.toString(encoding)
1554
- cb(null, path)
1200
+ if (encoding !== 'buffer') res = res.toString(encoding)
1201
+ } catch (e) {
1202
+ err = new FileError(e.message, {
1203
+ operation: 'readlink',
1204
+ code: e.code,
1205
+ path: filepath
1206
+ })
1207
+ } finally {
1208
+ req.destroy()
1555
1209
  }
1556
1210
 
1557
- binding.readlink(req.handle, toNamespacedPath(filepath), data)
1211
+ return done(err, res, cb)
1558
1212
  }
1559
1213
 
1560
1214
  function readlinkSync(filepath, opts) {
1561
- if (typeof filepath !== 'string') {
1562
- throw typeError(
1563
- 'ERR_INVALID_ARG_TYPE',
1564
- 'Path must be a string. Received type ' +
1565
- typeof filepath +
1566
- ' (' +
1567
- filepath +
1568
- ')'
1569
- )
1570
- }
1571
-
1572
1215
  if (typeof opts === 'string') opts = { encoding: opts }
1573
1216
  else if (!opts) opts = {}
1574
1217
 
1575
1218
  const { encoding = 'utf8' } = opts
1576
1219
 
1577
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
1220
+ filepath = toNamespacedPath(filepath)
1221
+
1222
+ const req = new FileRequest()
1223
+
1224
+ try {
1225
+ binding.readlinkSync(req.handle, filepath)
1578
1226
 
1579
- binding.readlinkSync(toNamespacedPath(filepath), data)
1227
+ let res = Buffer.from(binding.requestResultString(req.handle))
1580
1228
 
1581
- filepath = data.subarray(0, data.indexOf(0))
1582
- if (encoding !== 'buffer') filepath = filepath.toString(encoding)
1583
- return filepath
1229
+ if (encoding !== 'buffer') res = res.toString(encoding)
1230
+
1231
+ return res
1232
+ } catch (e) {
1233
+ throw new FileError(e.message, {
1234
+ operation: 'readlink',
1235
+ code: e.code,
1236
+ path: filepath
1237
+ })
1238
+ } finally {
1239
+ req.destroy()
1240
+ }
1584
1241
  }
1585
1242
 
1586
1243
  function normalizeSymlinkTarget(target, type, filepath) {
@@ -1595,41 +1252,10 @@ function normalizeSymlinkTarget(target, type, filepath) {
1595
1252
  return target
1596
1253
  }
1597
1254
 
1598
- function symlink(target, filepath, type, cb) {
1599
- if (typeof target !== 'string') {
1600
- throw typeError(
1601
- 'ERR_INVALID_ARG_TYPE',
1602
- 'Target must be a string. Received type ' +
1603
- typeof filepath +
1604
- ' (' +
1605
- filepath +
1606
- ')'
1607
- )
1608
- }
1609
-
1610
- if (typeof filepath !== 'string') {
1611
- throw typeError(
1612
- 'ERR_INVALID_ARG_TYPE',
1613
- 'Path must be a string. Received type ' +
1614
- typeof filepath +
1615
- ' (' +
1616
- filepath +
1617
- ')'
1618
- )
1619
- }
1620
-
1255
+ async function symlink(target, filepath, type, cb) {
1621
1256
  if (typeof type === 'function') {
1622
1257
  cb = type
1623
1258
  type = null
1624
- } else if (typeof cb !== 'function') {
1625
- throw typeError(
1626
- 'ERR_INVALID_ARG_TYPE',
1627
- 'Callback must be a function. Received type ' +
1628
- typeof cb +
1629
- ' (' +
1630
- cb +
1631
- ')'
1632
- )
1633
1259
  }
1634
1260
 
1635
1261
  filepath = toNamespacedPath(filepath)
@@ -1637,6 +1263,7 @@ function symlink(target, filepath, type, cb) {
1637
1263
  if (typeof type === 'string') {
1638
1264
  switch (type) {
1639
1265
  case 'file':
1266
+ default:
1640
1267
  type = 0
1641
1268
  break
1642
1269
  case 'dir':
@@ -1645,66 +1272,53 @@ function symlink(target, filepath, type, cb) {
1645
1272
  case 'junction':
1646
1273
  type = constants.UV_FS_SYMLINK_JUNCTION
1647
1274
  break
1648
- default:
1649
- throw typeError(
1650
- 'ERR_FS_INVALID_SYMLINK_TYPE',
1651
- 'Symlink type must be one of "dir", "file", or "junction". Received "' +
1652
- type +
1653
- '"'
1654
- )
1655
1275
  }
1656
1276
  } else if (typeof type !== 'number') {
1657
1277
  if (isWindows) {
1658
1278
  target = path.resolve(filepath, '..', target)
1659
1279
 
1660
- stat(target, (err, st) => {
1661
- type =
1662
- err === null && st.isDirectory()
1663
- ? constants.UV_FS_SYMLINK_DIR
1664
- : constants.UV_FS_SYMLINK_JUNCTION
1280
+ try {
1281
+ type = (await stat(target)).isDirectory()
1282
+ ? constants.UV_FS_SYMLINK_DIR
1283
+ : constants.UV_FS_SYMLINK_JUNCTION
1284
+ } catch {
1285
+ type = 0
1286
+ }
1287
+ } else {
1288
+ type = 0
1289
+ }
1290
+ }
1665
1291
 
1666
- symlink(target, filepath, type, cb)
1667
- })
1292
+ target = normalizeSymlinkTarget(target)
1668
1293
 
1669
- return
1670
- }
1294
+ const req = new FileRequest()
1671
1295
 
1672
- type = 0
1296
+ let err = null
1297
+ try {
1298
+ binding.symlink(req.handle, target, filepath, type)
1299
+
1300
+ await req
1301
+ } catch (e) {
1302
+ err = new FileError(e.message, {
1303
+ operation: 'symlink',
1304
+ code: e.code,
1305
+ path: target,
1306
+ destination: filepath
1307
+ })
1308
+ } finally {
1309
+ req.destroy()
1673
1310
  }
1674
1311
 
1675
- const req = getReq()
1676
- req.callback = cb
1677
- binding.symlink(req.handle, normalizeSymlinkTarget(target), filepath, type)
1312
+ return done(err, cb)
1678
1313
  }
1679
1314
 
1680
1315
  function symlinkSync(target, filepath, type) {
1681
- if (typeof target !== 'string') {
1682
- throw typeError(
1683
- 'ERR_INVALID_ARG_TYPE',
1684
- 'Target must be a string. Received type ' +
1685
- typeof filepath +
1686
- ' (' +
1687
- filepath +
1688
- ')'
1689
- )
1690
- }
1691
-
1692
- if (typeof filepath !== 'string') {
1693
- throw typeError(
1694
- 'ERR_INVALID_ARG_TYPE',
1695
- 'Path must be a string. Received type ' +
1696
- typeof filepath +
1697
- ' (' +
1698
- filepath +
1699
- ')'
1700
- )
1701
- }
1702
-
1703
1316
  filepath = toNamespacedPath(filepath)
1704
1317
 
1705
1318
  if (typeof type === 'string') {
1706
1319
  switch (type) {
1707
1320
  case 'file':
1321
+ default:
1708
1322
  type = 0
1709
1323
  break
1710
1324
  case 'dir':
@@ -1713,53 +1327,45 @@ function symlinkSync(target, filepath, type) {
1713
1327
  case 'junction':
1714
1328
  type = constants.UV_FS_SYMLINK_JUNCTION
1715
1329
  break
1716
- default:
1717
- throw typeError(
1718
- 'ERR_FS_INVALID_SYMLINK_TYPE',
1719
- 'Symlink type must be one of "dir", "file", or "junction". Received "' +
1720
- type +
1721
- '"'
1722
- )
1723
1330
  }
1724
1331
  } else if (typeof type !== 'number') {
1725
1332
  if (isWindows) {
1726
1333
  target = path.resolve(filepath, '..', target)
1727
1334
 
1728
- type = statSync(target).isDirectory()
1729
- ? constants.UV_FS_SYMLINK_DIR
1730
- : constants.UV_FS_SYMLINK_JUNCTION
1335
+ try {
1336
+ type = statSync(target).isDirectory()
1337
+ ? constants.UV_FS_SYMLINK_DIR
1338
+ : constants.UV_FS_SYMLINK_JUNCTION
1339
+ } catch {
1340
+ type = 0
1341
+ }
1731
1342
  } else {
1732
1343
  type = 0
1733
1344
  }
1734
1345
  }
1735
1346
 
1736
- binding.symlinkSync(normalizeSymlinkTarget(target), filepath, type)
1737
- }
1347
+ target = normalizeSymlinkTarget(target)
1738
1348
 
1739
- function opendir(filepath, opts, cb) {
1740
- if (typeof filepath !== 'string') {
1741
- throw typeError(
1742
- 'ERR_INVALID_ARG_TYPE',
1743
- 'Path must be a string. Received type ' +
1744
- typeof filepath +
1745
- ' (' +
1746
- filepath +
1747
- ')'
1748
- )
1349
+ const req = new FileRequest()
1350
+
1351
+ try {
1352
+ binding.symlinkSync(req.handle, target, filepath, type)
1353
+ } catch (e) {
1354
+ throw new FileError(e.message, {
1355
+ operation: 'symlink',
1356
+ code: e.code,
1357
+ path: target,
1358
+ destination: filepath
1359
+ })
1360
+ } finally {
1361
+ req.destroy()
1749
1362
  }
1363
+ }
1750
1364
 
1365
+ async function opendir(filepath, opts, cb) {
1751
1366
  if (typeof opts === 'function') {
1752
1367
  cb = opts
1753
1368
  opts = {}
1754
- } else if (typeof cb !== 'function') {
1755
- throw typeError(
1756
- 'ERR_INVALID_ARG_TYPE',
1757
- 'Callback must be a function. Received type ' +
1758
- typeof cb +
1759
- ' (' +
1760
- cb +
1761
- ')'
1762
- )
1763
1369
  }
1764
1370
 
1765
1371
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1767,64 +1373,56 @@ function opendir(filepath, opts, cb) {
1767
1373
 
1768
1374
  filepath = toNamespacedPath(filepath)
1769
1375
 
1770
- const data = Buffer.allocUnsafe(binding.sizeofFSDir)
1376
+ const req = new FileRequest()
1377
+
1378
+ let dir
1379
+ let err = null
1380
+ try {
1381
+ binding.opendir(req.handle, filepath)
1771
1382
 
1772
- const req = getReq()
1383
+ await req
1773
1384
 
1774
- req.callback = function (err, _) {
1775
- if (err) return cb(err, null)
1776
- cb(null, new Dir(filepath, data, opts))
1385
+ dir = new Dir(filepath, binding.requestResultDir(req.handle), opts)
1386
+ } catch (e) {
1387
+ err = new FileError(e.message, {
1388
+ operation: 'opendir',
1389
+ code: e.code,
1390
+ path: filepath
1391
+ })
1392
+ } finally {
1393
+ req.destroy()
1777
1394
  }
1778
1395
 
1779
- binding.opendir(req.handle, filepath, data)
1396
+ return done(err, dir, cb)
1780
1397
  }
1781
1398
 
1782
1399
  function opendirSync(filepath, opts) {
1783
- if (typeof filepath !== 'string') {
1784
- throw typeError(
1785
- 'ERR_INVALID_ARG_TYPE',
1786
- 'Path must be a string. Received type ' +
1787
- typeof filepath +
1788
- ' (' +
1789
- filepath +
1790
- ')'
1791
- )
1792
- }
1793
-
1794
1400
  if (typeof opts === 'string') opts = { encoding: opts }
1795
1401
  else if (!opts) opts = {}
1796
1402
 
1797
1403
  filepath = toNamespacedPath(filepath)
1798
1404
 
1799
- const data = Buffer.allocUnsafe(binding.sizeofFSDir)
1800
- binding.opendirSync(filepath, data)
1801
- return new Dir(filepath, data, opts)
1802
- }
1405
+ const req = new FileRequest()
1803
1406
 
1804
- function readdir(filepath, opts, cb) {
1805
- if (typeof filepath !== 'string') {
1806
- throw typeError(
1807
- 'ERR_INVALID_ARG_TYPE',
1808
- 'Path must be a string. Received type ' +
1809
- typeof filepath +
1810
- ' (' +
1811
- filepath +
1812
- ')'
1813
- )
1407
+ try {
1408
+ binding.opendirSync(req.handle, filepath)
1409
+
1410
+ return new Dir(filepath, binding.requestResultDir(req.handle), opts)
1411
+ } catch (e) {
1412
+ throw new FileError(e.message, {
1413
+ operation: 'opendir',
1414
+ code: e.code,
1415
+ path: filepath
1416
+ })
1417
+ } finally {
1418
+ req.destroy()
1814
1419
  }
1420
+ }
1815
1421
 
1422
+ async function readdir(filepath, opts, cb) {
1816
1423
  if (typeof opts === 'function') {
1817
1424
  cb = opts
1818
1425
  opts = {}
1819
- } else if (typeof cb !== 'function') {
1820
- throw typeError(
1821
- 'ERR_INVALID_ARG_TYPE',
1822
- 'Callback must be a function. Received type ' +
1823
- typeof cb +
1824
- ' (' +
1825
- cb +
1826
- ')'
1827
- )
1828
1426
  }
1829
1427
 
1830
1428
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1832,69 +1430,46 @@ function readdir(filepath, opts, cb) {
1832
1430
 
1833
1431
  const { withFileTypes = false } = opts
1834
1432
 
1835
- opendir(toNamespacedPath(filepath), opts, async (err, dir) => {
1836
- if (err) return cb(err, null)
1837
- const result = []
1433
+ filepath = toNamespacedPath(filepath)
1434
+
1435
+ let result = []
1436
+ let err = null
1437
+ try {
1438
+ const dir = await opendir(filepath)
1439
+
1838
1440
  for await (const entry of dir) {
1839
1441
  result.push(withFileTypes ? entry : entry.name)
1840
1442
  }
1841
- cb(null, result)
1842
- })
1443
+ } catch (e) {
1444
+ result = []
1445
+ err = e
1446
+ }
1447
+
1448
+ return done(err, result, cb)
1843
1449
  }
1844
1450
 
1845
1451
  function readdirSync(filepath, opts) {
1846
- if (typeof filepath !== 'string') {
1847
- throw typeError(
1848
- 'ERR_INVALID_ARG_TYPE',
1849
- 'Path must be a string. Received type ' +
1850
- typeof filepath +
1851
- ' (' +
1852
- filepath +
1853
- ')'
1854
- )
1855
- }
1856
-
1857
1452
  if (typeof opts === 'string') opts = { encoding: opts }
1858
1453
  else if (!opts) opts = {}
1859
1454
 
1860
1455
  const { withFileTypes = false } = opts
1861
1456
 
1862
- const dir = opendirSync(toNamespacedPath(filepath), opts)
1457
+ filepath = toNamespacedPath(filepath)
1458
+
1459
+ const dir = opendirSync(filepath, opts)
1863
1460
  const result = []
1864
1461
 
1865
- while (true) {
1866
- const entry = dir.readSync()
1867
- if (entry === null) break
1462
+ for (const entry of dir) {
1868
1463
  result.push(withFileTypes ? entry : entry.name)
1869
1464
  }
1870
1465
 
1871
1466
  return result
1872
1467
  }
1873
1468
 
1874
- function readFile(filepath, opts, cb) {
1875
- if (typeof filepath !== 'string') {
1876
- throw typeError(
1877
- 'ERR_INVALID_ARG_TYPE',
1878
- 'Path must be a string. Received type ' +
1879
- typeof filepath +
1880
- ' (' +
1881
- filepath +
1882
- ')'
1883
- )
1884
- }
1885
-
1469
+ async function readFile(filepath, opts, cb) {
1886
1470
  if (typeof opts === 'function') {
1887
1471
  cb = opts
1888
1472
  opts = {}
1889
- } else if (typeof cb !== 'function') {
1890
- throw typeError(
1891
- 'ERR_INVALID_ARG_TYPE',
1892
- 'Callback must be a function. Received type ' +
1893
- typeof cb +
1894
- ' (' +
1895
- cb +
1896
- ')'
1897
- )
1898
1473
  }
1899
1474
 
1900
1475
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -1902,62 +1477,62 @@ function readFile(filepath, opts, cb) {
1902
1477
 
1903
1478
  const { encoding = 'buffer' } = opts
1904
1479
 
1905
- open(filepath, opts.flag || 'r', function (err, fd) {
1906
- if (err) return cb(err)
1480
+ let fd = -1
1481
+ try {
1482
+ fd = await open(filepath, opts.flag || 'r')
1907
1483
 
1908
- fstat(fd, function (err, st) {
1909
- if (err) return closeAndError(err)
1484
+ const st = await fstat(fd)
1910
1485
 
1911
- let buffer = Buffer.allocUnsafe(st.size)
1912
- let len = 0
1486
+ let buffer
1487
+ let len = 0
1913
1488
 
1914
- read(fd, buffer, loop)
1489
+ if (st.size === 0) {
1490
+ const buffers = []
1915
1491
 
1916
- function loop(err, r) {
1917
- if (err) return closeAndError(err)
1492
+ while (true) {
1493
+ buffer = Buffer.allocUnsafe(8192)
1494
+ const r = await read(fd, buffer, 0, 8192)
1918
1495
  len += r
1919
- if (r === 0 || len === buffer.byteLength) return done()
1920
- read(fd, buffer.subarray(len), loop)
1496
+ if (r === 0) break
1497
+ buffers.push(buffer.subarray(0, r))
1921
1498
  }
1922
1499
 
1923
- function done() {
1924
- if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
1925
- close(fd, function (err) {
1926
- if (err) return cb(err)
1927
- if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1928
- cb(null, buffer)
1929
- })
1500
+ buffer = Buffer.concat(buffers)
1501
+ } else {
1502
+ buffer = Buffer.allocUnsafe(st.size)
1503
+
1504
+ while (true) {
1505
+ const r = await read(fd, len ? buffer.subarray(len) : buffer)
1506
+ len += r
1507
+ if (r === 0 || len === buffer.byteLength) break
1930
1508
  }
1931
- })
1932
1509
 
1933
- function closeAndError(err) {
1934
- close(fd, function () {
1935
- cb(err)
1936
- })
1510
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
1937
1511
  }
1938
- })
1939
- }
1940
1512
 
1941
- function readFileSync(filepath, opts) {
1942
- if (typeof filepath !== 'string') {
1943
- throw typeError(
1944
- 'ERR_INVALID_ARG_TYPE',
1945
- 'Path must be a string. Received type ' +
1946
- typeof filepath +
1947
- ' (' +
1948
- filepath +
1949
- ')'
1950
- )
1513
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1514
+
1515
+ return ok(buffer, cb)
1516
+ } catch (err) {
1517
+ fail(err, cb)
1518
+ } finally {
1519
+ if (fd === -1) return
1520
+ try {
1521
+ await close(fd)
1522
+ } catch {}
1951
1523
  }
1524
+ }
1952
1525
 
1526
+ function readFileSync(filepath, opts) {
1953
1527
  if (typeof opts === 'string') opts = { encoding: opts }
1954
1528
  else if (!opts) opts = {}
1955
1529
 
1956
1530
  const { encoding = 'buffer' } = opts
1957
1531
 
1958
- const fd = openSync(filepath, opts.flag || 'r')
1959
-
1532
+ let fd = -1
1960
1533
  try {
1534
+ fd = openSync(filepath, opts.flag || 'r')
1535
+
1961
1536
  const st = fstatSync(fd)
1962
1537
 
1963
1538
  let buffer
@@ -1988,49 +1563,20 @@ function readFileSync(filepath, opts) {
1988
1563
  }
1989
1564
 
1990
1565
  if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1566
+
1991
1567
  return buffer
1992
1568
  } finally {
1569
+ if (fd === -1) return
1993
1570
  try {
1994
1571
  closeSync(fd)
1995
1572
  } catch {}
1996
1573
  }
1997
1574
  }
1998
1575
 
1999
- function writeFile(filepath, data, opts, cb) {
2000
- if (typeof filepath !== 'string') {
2001
- throw typeError(
2002
- 'ERR_INVALID_ARG_TYPE',
2003
- 'Path must be a string. Received type ' +
2004
- typeof filepath +
2005
- ' (' +
2006
- filepath +
2007
- ')'
2008
- )
2009
- }
2010
-
2011
- if (
2012
- typeof data !== 'string' &&
2013
- !Buffer.isBuffer(data) &&
2014
- !ArrayBuffer.isView(data)
2015
- ) {
2016
- throw typeError(
2017
- 'ERR_INVALID_ARG_TYPE',
2018
- 'Data must be a string or buffer. Received type ' + typeof data
2019
- )
2020
- }
2021
-
1576
+ async function writeFile(filepath, data, opts, cb) {
2022
1577
  if (typeof opts === 'function') {
2023
1578
  cb = opts
2024
1579
  opts = {}
2025
- } else if (typeof cb !== 'function') {
2026
- throw typeError(
2027
- 'ERR_INVALID_ARG_TYPE',
2028
- 'Callback must be a function. Received type ' +
2029
- typeof cb +
2030
- ' (' +
2031
- cb +
2032
- ')'
2033
- )
2034
1580
  }
2035
1581
 
2036
1582
  if (typeof opts === 'string') opts = { encoding: opts }
@@ -2038,63 +1584,38 @@ function writeFile(filepath, data, opts, cb) {
2038
1584
 
2039
1585
  if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
2040
1586
 
2041
- open(filepath, opts.flag || 'w', opts.mode || 0o666, function (err, fd) {
2042
- if (err) return cb(err)
2043
-
2044
- write(fd, data, loop)
1587
+ let fd = -1
1588
+ try {
1589
+ fd = await open(filepath, opts.flag || 'w', opts.mode || 0o666)
2045
1590
 
2046
- function loop(err, w) {
2047
- if (err) return closeAndError(err)
2048
- if (w === data.byteLength) return done()
2049
- write(fd, data.subarray(w), loop)
2050
- }
1591
+ let len = 0
2051
1592
 
2052
- function done() {
2053
- close(fd, function (err) {
2054
- if (err) return cb(err)
2055
- return cb(null)
2056
- })
1593
+ while (true) {
1594
+ len += await write(fd, len ? data.subarray(len) : data)
1595
+ if (len === data.byteLength) break
2057
1596
  }
2058
1597
 
2059
- function closeAndError(err) {
2060
- close(fd, function () {
2061
- cb(err)
2062
- })
2063
- }
2064
- })
1598
+ return ok(cb)
1599
+ } catch (err) {
1600
+ fail(err, cb)
1601
+ } finally {
1602
+ if (fd === -1) return
1603
+ try {
1604
+ await close(fd)
1605
+ } catch {}
1606
+ }
2065
1607
  }
2066
1608
 
2067
1609
  function writeFileSync(filepath, data, opts) {
2068
- if (typeof filepath !== 'string') {
2069
- throw typeError(
2070
- 'ERR_INVALID_ARG_TYPE',
2071
- 'Path must be a string. Received type ' +
2072
- typeof filepath +
2073
- ' (' +
2074
- filepath +
2075
- ')'
2076
- )
2077
- }
2078
-
2079
- if (
2080
- typeof data !== 'string' &&
2081
- !Buffer.isBuffer(data) &&
2082
- !ArrayBuffer.isView(data)
2083
- ) {
2084
- throw typeError(
2085
- 'ERR_INVALID_ARG_TYPE',
2086
- 'Data must be a string or buffer. Received type ' + typeof data
2087
- )
2088
- }
2089
-
2090
1610
  if (typeof opts === 'string') opts = { encoding: opts }
2091
1611
  else if (!opts) opts = {}
2092
1612
 
2093
1613
  if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
2094
1614
 
2095
- const fd = openSync(filepath, opts.flag || 'w', opts.mode)
2096
-
1615
+ let fd = -1
2097
1616
  try {
1617
+ fd = openSync(filepath, opts.flag || 'w', opts.mode || 0o666)
1618
+
2098
1619
  let len = 0
2099
1620
 
2100
1621
  while (true) {
@@ -2102,6 +1623,7 @@ function writeFileSync(filepath, data, opts) {
2102
1623
  if (len === data.byteLength) break
2103
1624
  }
2104
1625
  } finally {
1626
+ if (fd === -1) return
2105
1627
  try {
2106
1628
  closeSync(fd)
2107
1629
  } catch {}
@@ -2109,97 +1631,29 @@ function writeFileSync(filepath, data, opts) {
2109
1631
  }
2110
1632
 
2111
1633
  function appendFile(filepath, data, opts, cb) {
2112
- if (typeof filepath !== 'string') {
2113
- throw typeError(
2114
- 'ERR_INVALID_ARG_TYPE',
2115
- 'Path must be a string. Received type ' +
2116
- typeof filepath +
2117
- ' (' +
2118
- filepath +
2119
- ')'
2120
- )
2121
- }
2122
-
2123
- if (
2124
- typeof data !== 'string' &&
2125
- !Buffer.isBuffer(data) &&
2126
- !ArrayBuffer.isView(data)
2127
- ) {
2128
- throw typeError(
2129
- 'ERR_INVALID_ARG_TYPE',
2130
- 'Data must be a string or buffer. Received type ' + typeof data
2131
- )
2132
- }
2133
-
2134
1634
  if (typeof opts === 'function') {
2135
1635
  cb = opts
2136
1636
  opts = {}
2137
- } else if (typeof cb !== 'function') {
2138
- throw typeError(
2139
- 'ERR_INVALID_ARG_TYPE',
2140
- 'Callback must be a function. Received type ' +
2141
- typeof cb +
2142
- ' (' +
2143
- cb +
2144
- ')'
2145
- )
2146
1637
  }
2147
1638
 
2148
1639
  if (typeof opts === 'string') opts = { encoding: opts }
2149
1640
  else if (!opts) opts = {}
2150
1641
 
2151
- opts = { ...opts }
2152
-
2153
- if (!opts.flags) opts.flag = 'a'
1642
+ if (!opts.flag) opts = { ...opts, flag: 'a' }
2154
1643
 
2155
1644
  return writeFile(filepath, data, opts, cb)
2156
1645
  }
2157
1646
 
2158
1647
  function appendFileSync(filepath, data, opts) {
2159
- if (typeof filepath !== 'string') {
2160
- throw typeError(
2161
- 'ERR_INVALID_ARG_TYPE',
2162
- 'Path must be a string. Received type ' +
2163
- typeof filepath +
2164
- ' (' +
2165
- filepath +
2166
- ')'
2167
- )
2168
- }
2169
-
2170
- if (
2171
- typeof data !== 'string' &&
2172
- !Buffer.isBuffer(data) &&
2173
- !ArrayBuffer.isView(data)
2174
- ) {
2175
- throw typeError(
2176
- 'ERR_INVALID_ARG_TYPE',
2177
- 'Data must be a string or buffer. Received type ' + typeof data
2178
- )
2179
- }
2180
-
2181
1648
  if (typeof opts === 'string') opts = { encoding: opts }
2182
1649
  else if (!opts) opts = {}
2183
1650
 
2184
- opts = { ...opts }
2185
-
2186
- if (!opts.flags) opts.flag = 'a'
1651
+ if (!opts.flag) opts = { ...opts, flag: 'a' }
2187
1652
 
2188
1653
  return writeFileSync(filepath, data, opts)
2189
1654
  }
2190
1655
 
2191
1656
  function watch(filepath, opts, cb) {
2192
- if (typeof filepath !== 'string') {
2193
- throw typeError(
2194
- 'ERR_INVALID_ARG_TYPE',
2195
- 'Path must be a string. Received type ' +
2196
- typeof filepath +
2197
- ' (' +
2198
- filepath +
2199
- ')'
2200
- )
2201
- }
2202
-
2203
1657
  if (typeof opts === 'function') {
2204
1658
  cb = opts
2205
1659
  opts = {}
@@ -2208,9 +1662,9 @@ function watch(filepath, opts, cb) {
2208
1662
  if (typeof opts === 'string') opts = { encoding: opts }
2209
1663
  else if (!opts) opts = {}
2210
1664
 
2211
- const watcher = new Watcher(toNamespacedPath(filepath), opts)
2212
- if (cb) watcher.on('change', cb)
2213
- return watcher
1665
+ filepath = toNamespacedPath(filepath)
1666
+
1667
+ return new Watcher(filepath, opts, cb)
2214
1668
  }
2215
1669
 
2216
1670
  class Stats {
@@ -2283,79 +1737,134 @@ class Dir {
2283
1737
  constructor(path, handle, opts = {}) {
2284
1738
  const { encoding = 'utf8', bufferSize = 32 } = opts
2285
1739
 
2286
- this._handle = handle
2287
- this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
1740
+ this.path = path
1741
+
2288
1742
  this._encoding = encoding
1743
+ this._capacity = bufferSize
2289
1744
  this._buffer = []
2290
1745
  this._ended = false
1746
+ this._handle = handle
1747
+ }
1748
+
1749
+ async read(cb) {
1750
+ if (this._buffer.length) return ok(this._buffer.shift(), cb)
1751
+ if (this._ended) return ok(null, cb)
1752
+
1753
+ const req = new FileRequest()
1754
+
1755
+ let entries
1756
+ let err = null
1757
+ try {
1758
+ req.retain(binding.readdir(req.handle, this._handle, this._capacity))
1759
+
1760
+ await req
1761
+
1762
+ entries = binding.requestResultDirents(req.handle)
1763
+ } catch (e) {
1764
+ err = new FileError(e.message, {
1765
+ operation: 'readdir',
1766
+ code: e.code,
1767
+ path: this.path
1768
+ })
1769
+ } finally {
1770
+ req.destroy()
1771
+ }
2291
1772
 
2292
- this.path = path
2293
- }
1773
+ if (err) return fail(err, cb)
2294
1774
 
2295
- read(cb) {
2296
- if (!cb) return promisify(this.read.bind(this))
1775
+ if (entries.length === 0) {
1776
+ this._ended = true
2297
1777
 
2298
- if (this._buffer.length)
2299
- return queueMicrotask(() => cb(null, this._buffer.shift()))
2300
- if (this._ended) return queueMicrotask(() => cb(null, null))
1778
+ return ok(null, cb)
1779
+ }
2301
1780
 
2302
- const data = []
2303
- const req = getReq()
1781
+ for (const entry of entries) {
1782
+ let name = Buffer.from(entry.name)
2304
1783
 
2305
- req.callback = (err, _) => {
2306
- if (err) return cb(err, null)
2307
- if (data.length === 0) this._ended = true
2308
- else {
2309
- for (const entry of data) {
2310
- let name = Buffer.from(entry.name)
2311
- if (this._encoding !== 'buffer') name = name.toString(this._encoding)
2312
- this._buffer.push(new Dirent(this.path, name, entry.type))
2313
- }
2314
- }
1784
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
2315
1785
 
2316
- if (this._ended) return cb(null, null)
2317
- cb(null, this._buffer.shift())
1786
+ this._buffer.push(new Dirent(this.path, name, entry.type))
2318
1787
  }
2319
1788
 
2320
- binding.readdir(req.handle, this._handle, this._dirents, data)
1789
+ return ok(this._buffer.shift(), cb)
2321
1790
  }
2322
1791
 
2323
1792
  readSync() {
2324
1793
  if (this._buffer.length) return this._buffer.shift()
2325
1794
  if (this._ended) return null
2326
1795
 
2327
- const data = []
1796
+ const req = new FileRequest()
1797
+
1798
+ let entries
1799
+ try {
1800
+ req.retain(binding.readdirSync(req.handle, this._handle, this._capacity))
1801
+
1802
+ entries = binding.requestResultDirents(req.handle)
1803
+ } catch (e) {
1804
+ throw new FileError(e.message, {
1805
+ operation: 'readdir',
1806
+ code: e.code,
1807
+ path: this.path
1808
+ })
1809
+ } finally {
1810
+ req.destroy()
1811
+ }
1812
+
1813
+ if (entries.length === 0) {
1814
+ this._ended = true
2328
1815
 
2329
- binding.readdirSync(this._handle, this._dirents, data)
1816
+ return null
1817
+ }
2330
1818
 
2331
- if (data.length === 0) this._ended = true
2332
- else {
2333
- for (const entry of data) {
2334
- let name = Buffer.from(entry.name)
2335
- if (this._encoding !== 'buffer') name = name.toString(this._encoding)
2336
- this._buffer.push(new Dirent(this.path, name, entry.type))
2337
- }
1819
+ for (const entry of entries) {
1820
+ let name = Buffer.from(entry.name)
1821
+
1822
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1823
+
1824
+ this._buffer.push(new Dirent(this.path, name, entry.type))
2338
1825
  }
2339
1826
 
2340
- if (this._ended) return null
2341
1827
  return this._buffer.shift()
2342
1828
  }
2343
1829
 
2344
- close(cb) {
2345
- if (!cb) return promisify(this.close.bind(this))
2346
-
2347
- const req = getReq()
1830
+ async close(cb) {
1831
+ const req = new FileRequest()
2348
1832
 
2349
- req.callback = (err, _) => {
2350
- this._handle = null
2351
- cb(err)
1833
+ let err = null
1834
+ try {
1835
+ binding.closedir(req.handle, this._handle)
1836
+
1837
+ await req
1838
+ } catch (e) {
1839
+ err = new FileError(e.message, {
1840
+ operation: 'closedir',
1841
+ code: e.code,
1842
+ path: this.path
1843
+ })
1844
+ } finally {
1845
+ req.destroy()
2352
1846
  }
2353
1847
 
2354
- binding.closedir(req.handle, this._handle)
1848
+ this._handle = null
1849
+
1850
+ return done(err, cb)
2355
1851
  }
2356
1852
 
2357
1853
  closeSync() {
2358
- binding.closedirSync(this._handle)
1854
+ const req = new FileRequest()
1855
+
1856
+ try {
1857
+ binding.closedirSync(req.handle, this._handle)
1858
+ } catch (e) {
1859
+ throw new FileError(e.message, {
1860
+ operation: 'closedir',
1861
+ code: e.code,
1862
+ path: this.path
1863
+ })
1864
+ } finally {
1865
+ req.destroy()
1866
+ }
1867
+
2359
1868
  this._handle = null
2360
1869
  }
2361
1870
 
@@ -2385,26 +1894,25 @@ class Dir {
2385
1894
 
2386
1895
  [Symbol.asyncIterator]() {
2387
1896
  return {
2388
- next: () =>
2389
- new Promise((resolve, reject) => {
2390
- if (this._buffer.length) {
2391
- return resolve({ done: false, value: this._buffer.shift() })
2392
- }
1897
+ next: async () => {
1898
+ if (this._buffer.length) {
1899
+ return { done: false, value: this._buffer.shift() }
1900
+ }
2393
1901
 
2394
- if (this._ended) {
2395
- return resolve({ done: true })
2396
- }
1902
+ if (this._ended) {
1903
+ return { done: true }
1904
+ }
1905
+
1906
+ const entry = await this.read()
2397
1907
 
2398
- this.read((err, entry) => {
2399
- if (err) return reject(err)
1908
+ if (entry) {
1909
+ return { done: false, value: entry }
1910
+ }
2400
1911
 
2401
- if (entry) {
2402
- return resolve({ done: false, value: entry })
2403
- }
1912
+ await this.close()
2404
1913
 
2405
- this.close((err) => (err ? reject(err) : resolve({ done: true })))
2406
- })
2407
- })
1914
+ return { done: true }
1915
+ }
2408
1916
  }
2409
1917
  }
2410
1918
  }
@@ -2445,44 +1953,14 @@ class Dirent {
2445
1953
  }
2446
1954
  }
2447
1955
 
2448
- class FileWriteStream extends Writable {
2449
- constructor(path, opts = {}) {
2450
- super({ map })
2451
-
2452
- this.path = path
2453
- this.fd = 0
2454
- this.flags = opts.flags || 'w'
2455
- this.mode = opts.mode || 0o666
2456
- }
2457
-
2458
- _open(cb) {
2459
- open(this.path, this.flags, this.mode, (err, fd) => {
2460
- if (err) return cb(err)
2461
- this.fd = fd
2462
- cb(null)
2463
- })
2464
- }
2465
-
2466
- _writev(batch, cb) {
2467
- writev(
2468
- this.fd,
2469
- batch.map(({ chunk }) => chunk),
2470
- cb
2471
- )
2472
- }
2473
-
2474
- _destroy(err, cb) {
2475
- if (!this.fd) return cb(err)
2476
- close(this.fd, () => cb(err))
2477
- }
2478
- }
2479
-
2480
1956
  class FileReadStream extends Readable {
2481
1957
  constructor(path, opts = {}) {
2482
1958
  super()
2483
1959
 
2484
1960
  this.path = path
2485
- this.fd = 0
1961
+ this.fd = typeof opts.fd === 'number' ? opts.fd : -1
1962
+ this.flags = opts.flags || 'r'
1963
+ this.mode = opts.mode || 0o666
2486
1964
 
2487
1965
  this._offset = opts.start || 0
2488
1966
  this._missing = 0
@@ -2496,67 +1974,141 @@ class FileReadStream extends Readable {
2496
1974
  }
2497
1975
  }
2498
1976
 
2499
- _open(cb) {
2500
- open(this.path, constants.O_RDONLY, (err, fd) => {
1977
+ async _open(cb) {
1978
+ let err
1979
+
1980
+ if (this.fd === -1) {
1981
+ err = null
1982
+ try {
1983
+ this.fd = await open(this.path, this.flags, this.mode)
1984
+ } catch (e) {
1985
+ err = e
1986
+ }
1987
+
2501
1988
  if (err) return cb(err)
1989
+ }
2502
1990
 
2503
- const onerror = (err) => close(fd, () => cb(err))
1991
+ let st
1992
+ err = null
1993
+ try {
1994
+ st = await fstat(this.fd)
1995
+ } catch (e) {
1996
+ err = e
1997
+ }
2504
1998
 
2505
- fstat(fd, (err, st) => {
2506
- if (err) return onerror(err)
2507
- if (!st.isFile())
2508
- return onerror(new Error(this.path + ' is not a file'))
1999
+ if (err) return cb(err)
2509
2000
 
2510
- this.fd = fd
2511
- if (this._missing === -1) this._missing = st.size
2001
+ if (this._missing === -1) this._missing = st.size
2512
2002
 
2513
- if (st.size < this._offset) {
2514
- this._offset = st.size
2515
- this._missing = 0
2516
- return cb(null)
2517
- }
2518
- if (st.size < this._offset + this._missing) {
2519
- this._missing = st.size - this._offset
2520
- return cb(null)
2521
- }
2003
+ if (st.size < this._offset) {
2004
+ this._offset = st.size
2005
+ this._missing = 0
2006
+ } else if (st.size < this._offset + this._missing) {
2007
+ this._missing = st.size - this._offset
2008
+ }
2522
2009
 
2523
- cb(null)
2524
- })
2525
- })
2010
+ cb(null)
2526
2011
  }
2527
2012
 
2528
- _read(size) {
2529
- if (!this._missing) {
2530
- this.push(null)
2531
- return
2532
- }
2013
+ async _read(size) {
2014
+ if (this._missing <= 0) return this.push(null)
2533
2015
 
2534
2016
  const data = Buffer.allocUnsafe(Math.min(this._missing, size))
2535
2017
 
2536
- read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
2537
- if (err) return this.destroy(err)
2018
+ let len
2019
+ let err = null
2020
+ try {
2021
+ len = await read(this.fd, data, 0, data.byteLength, this._offset)
2022
+ } catch (e) {
2023
+ err = e
2024
+ }
2538
2025
 
2539
- if (!read) {
2540
- this.push(null)
2541
- return
2542
- }
2026
+ if (err) return cb(err)
2543
2027
 
2544
- if (this._missing < read) read = this._missing
2545
- this.push(data.subarray(0, read))
2546
- this._missing -= read
2547
- this._offset += read
2548
- if (!this._missing) this.push(null)
2549
- })
2028
+ if (len === 0) return this.push(null)
2029
+
2030
+ if (this._missing < len) len = this._missing
2031
+
2032
+ this.push(data.subarray(0, len))
2033
+
2034
+ this._missing -= len
2035
+ this._offset += read
2036
+ }
2037
+
2038
+ async _destroy(err, cb) {
2039
+ if (this.fd === -1) return cb(err)
2040
+
2041
+ err = null
2042
+ try {
2043
+ await close(this.fd)
2044
+ } catch (e) {
2045
+ err = e
2046
+ }
2047
+
2048
+ cb(err)
2049
+ }
2050
+ }
2051
+
2052
+ class FileWriteStream extends Writable {
2053
+ constructor(path, opts = {}) {
2054
+ super({ map })
2055
+
2056
+ this.path = path
2057
+ this.fd = typeof opts.fd === 'number' ? opts.fd : -1
2058
+ this.flags = opts.flags || 'w'
2059
+ this.mode = opts.mode || 0o666
2060
+ }
2061
+
2062
+ async _open(cb) {
2063
+ if (this.fd !== -1) return cb(null)
2064
+
2065
+ let err = null
2066
+ try {
2067
+ this.fd = await open(this.path, this.flags, this.mode)
2068
+ } catch (e) {
2069
+ err = e
2070
+ }
2071
+
2072
+ cb(err)
2073
+ }
2074
+
2075
+ async _writev(batch, cb) {
2076
+ let err = null
2077
+ try {
2078
+ await writev(
2079
+ this.fd,
2080
+ batch.map(({ chunk }) => chunk)
2081
+ )
2082
+ } catch (e) {
2083
+ err = e
2084
+ }
2085
+
2086
+ cb(err)
2550
2087
  }
2551
2088
 
2552
- _destroy(err, cb) {
2553
- if (!this.fd) return cb(err)
2554
- close(this.fd, () => cb(err))
2089
+ async _destroy(err, cb) {
2090
+ if (this.fd === -1) return cb(err)
2091
+
2092
+ err = null
2093
+ try {
2094
+ await close(this.fd)
2095
+ } catch (e) {
2096
+ err = e
2097
+ }
2098
+
2099
+ cb(err)
2555
2100
  }
2556
2101
  }
2557
2102
 
2558
2103
  class Watcher extends EventEmitter {
2559
- constructor(path, opts) {
2104
+ constructor(path, opts, onchange) {
2105
+ if (typeof opts === 'function') {
2106
+ onchange = opts
2107
+ opts = {}
2108
+ }
2109
+
2110
+ if (!opts) opts = {}
2111
+
2560
2112
  const { persistent = true, recursive = false, encoding = 'utf8' } = opts
2561
2113
 
2562
2114
  super()
@@ -2572,32 +2124,8 @@ class Watcher extends EventEmitter {
2572
2124
  )
2573
2125
 
2574
2126
  if (!persistent) this.unref()
2575
- }
2576
-
2577
- _onevent(err, events, filename) {
2578
- if (err) {
2579
- this.close()
2580
- this.emit('error', err)
2581
- } else {
2582
- const path =
2583
- this._encoding === 'buffer'
2584
- ? Buffer.from(filename)
2585
- : Buffer.from(filename).toString(this._encoding)
2586
-
2587
- if (events & binding.UV_RENAME) {
2588
- this.emit('change', 'rename', path)
2589
- }
2590
-
2591
- if (events & binding.UV_CHANGE) {
2592
- this.emit('change', 'change', path)
2593
- }
2594
- }
2595
- }
2596
2127
 
2597
- _onclose() {
2598
- this._handle = null
2599
-
2600
- this.emit('close')
2128
+ if (onchange) this.on('change', onchange)
2601
2129
  }
2602
2130
 
2603
2131
  close() {
@@ -2663,17 +2191,33 @@ class Watcher extends EventEmitter {
2663
2191
  })
2664
2192
  }
2665
2193
  }
2666
- }
2667
2194
 
2668
- exports.promises = {}
2195
+ _onevent(err, events, filename) {
2196
+ if (err) {
2197
+ this.close()
2198
+ this.emit('error', err)
2199
+ } else {
2200
+ const path =
2201
+ this._encoding === 'buffer'
2202
+ ? Buffer.from(filename)
2203
+ : Buffer.from(filename).toString(this._encoding)
2204
+
2205
+ if (events & binding.UV_RENAME) {
2206
+ this.emit('change', 'rename', path)
2207
+ }
2669
2208
 
2670
- function typeError(code, message) {
2671
- const error = new TypeError(message)
2672
- error.code = code
2673
- return error
2674
- }
2209
+ if (events & binding.UV_CHANGE) {
2210
+ this.emit('change', 'change', path)
2211
+ }
2212
+ }
2213
+ }
2675
2214
 
2676
- function noop() {}
2215
+ _onclose() {
2216
+ this._handle = null
2217
+
2218
+ this.emit('close')
2219
+ }
2220
+ }
2677
2221
 
2678
2222
  exports.access = access
2679
2223
  exports.appendFile = appendFile
@@ -2723,6 +2267,7 @@ exports.readFileSync = readFileSync
2723
2267
  exports.readSync = readSync
2724
2268
  exports.readdirSync = readdirSync
2725
2269
  exports.readlinkSync = readlinkSync
2270
+ exports.readvSync = readvSync
2726
2271
  exports.realpathSync = realpathSync
2727
2272
  exports.renameSync = renameSync
2728
2273
  exports.rmSync = rmSync
@@ -2733,28 +2278,9 @@ exports.unlinkSync = unlinkSync
2733
2278
  exports.utimesSync = utimesSync
2734
2279
  exports.writeFileSync = writeFileSync
2735
2280
  exports.writeSync = writeSync
2281
+ exports.writevSync = writevSync
2736
2282
 
2737
- exports.promises.access = promisify(access)
2738
- exports.promises.appendFile = promisify(appendFile)
2739
- exports.promises.chmod = promisify(chmod)
2740
- exports.promises.copyFile = promisify(copyFile)
2741
- exports.promises.lstat = promisify(lstat)
2742
- exports.promises.mkdir = promisify(mkdir)
2743
- exports.promises.opendir = promisify(opendir)
2744
- exports.promises.readFile = promisify(readFile)
2745
- exports.promises.readdir = promisify(readdir)
2746
- exports.promises.readlink = promisify(readlink)
2747
- exports.promises.realpath = promisify(realpath)
2748
- exports.promises.rename = promisify(rename)
2749
- exports.promises.rm = promisify(rm)
2750
- exports.promises.rmdir = promisify(rmdir)
2751
- exports.promises.stat = promisify(stat)
2752
- exports.promises.symlink = promisify(symlink)
2753
- exports.promises.unlink = promisify(unlink)
2754
- exports.promises.utimes = promisify(utimes)
2755
- exports.promises.writeFile = promisify(writeFile)
2756
-
2757
- exports.promises.watch = watch // Already async iterable
2283
+ exports.promises = require('./promises')
2758
2284
 
2759
2285
  exports.Stats = Stats
2760
2286
  exports.Dir = Dir
@@ -2762,30 +2288,107 @@ exports.Dirent = Dirent
2762
2288
  exports.Watcher = Watcher
2763
2289
 
2764
2290
  exports.ReadStream = FileReadStream
2291
+
2765
2292
  exports.createReadStream = function createReadStream(path, opts) {
2766
2293
  return new FileReadStream(path, opts)
2767
2294
  }
2768
2295
 
2769
2296
  exports.WriteStream = FileWriteStream
2297
+
2770
2298
  exports.createWriteStream = function createWriteStream(path, opts) {
2771
2299
  return new FileWriteStream(path, opts)
2772
2300
  }
2773
2301
 
2774
- function promisify(fn) {
2775
- return function (...args) {
2776
- return new Promise((resolve, reject) => {
2777
- fn(...args, function (err, res) {
2778
- if (err) return reject(err)
2779
- resolve(res)
2780
- })
2781
- })
2782
- }
2783
- }
2784
-
2785
2302
  function map(data) {
2786
2303
  return typeof data === 'string' ? Buffer.from(data) : data
2787
2304
  }
2788
2305
 
2789
2306
  function toNamespacedPath(filepath) {
2307
+ if (typeof filepath !== 'string') {
2308
+ if (URL.isURL(filepath)) filepath = fileURLToPath(filepath)
2309
+ else filepath = filepath.toString()
2310
+ }
2311
+
2790
2312
  return path.toNamespacedPath(filepath)
2791
2313
  }
2314
+
2315
+ function toFlags(flags) {
2316
+ switch (flags) {
2317
+ case 'r':
2318
+ return constants.O_RDONLY
2319
+ case 'rs': // Fall through.
2320
+ case 'sr':
2321
+ return constants.O_RDONLY | constants.O_SYNC
2322
+ case 'r+':
2323
+ return constants.O_RDWR
2324
+ case 'rs+': // Fall through.
2325
+ case 'sr+':
2326
+ return constants.O_RDWR | constants.O_SYNC
2327
+
2328
+ case 'w':
2329
+ return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
2330
+ case 'wx': // Fall through.
2331
+ case 'xw':
2332
+ return (
2333
+ constants.O_TRUNC |
2334
+ constants.O_CREAT |
2335
+ constants.O_WRONLY |
2336
+ constants.O_EXCL
2337
+ )
2338
+
2339
+ case 'w+':
2340
+ return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
2341
+ case 'wx+': // Fall through.
2342
+ case 'xw+':
2343
+ return (
2344
+ constants.O_TRUNC |
2345
+ constants.O_CREAT |
2346
+ constants.O_RDWR |
2347
+ constants.O_EXCL
2348
+ )
2349
+
2350
+ case 'a':
2351
+ return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
2352
+ case 'ax': // Fall through.
2353
+ case 'xa':
2354
+ return (
2355
+ constants.O_APPEND |
2356
+ constants.O_CREAT |
2357
+ constants.O_WRONLY |
2358
+ constants.O_EXCL
2359
+ )
2360
+ case 'as': // Fall through.
2361
+ case 'sa':
2362
+ return (
2363
+ constants.O_APPEND |
2364
+ constants.O_CREAT |
2365
+ constants.O_WRONLY |
2366
+ constants.O_SYNC
2367
+ )
2368
+
2369
+ case 'a+':
2370
+ return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
2371
+ case 'ax+': // Fall through.
2372
+ case 'xa+':
2373
+ return (
2374
+ constants.O_APPEND |
2375
+ constants.O_CREAT |
2376
+ constants.O_RDWR |
2377
+ constants.O_EXCL
2378
+ )
2379
+ case 'as+': // Fall through.
2380
+ case 'sa+':
2381
+ return (
2382
+ constants.O_APPEND |
2383
+ constants.O_CREAT |
2384
+ constants.O_RDWR |
2385
+ constants.O_SYNC
2386
+ )
2387
+ default:
2388
+ return 0
2389
+ }
2390
+ }
2391
+
2392
+ function toMode(mode) {
2393
+ return parseInt(mode, 8)
2394
+ }