bare-fs 1.5.5 → 1.6.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.
Files changed (4) hide show
  1. package/README.md +16 -10
  2. package/binding.c +905 -376
  3. package/index.js +646 -236
  4. package/package.json +1 -1
package/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  const { Readable, Writable } = require('streamx')
2
2
  const binding = require('./binding')
3
3
 
4
- const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff
5
-
6
4
  const sep = process.platform === 'win32' ? '\\' : '/'
7
5
 
8
6
  const constants = exports.constants = {
@@ -12,6 +10,7 @@ const constants = exports.constants = {
12
10
  O_CREAT: binding.O_CREAT,
13
11
  O_TRUNC: binding.O_TRUNC,
14
12
  O_APPEND: binding.O_APPEND,
13
+
15
14
  S_IFMT: binding.S_IFMT,
16
15
  S_IFREG: binding.S_IFREG,
17
16
  S_IFDIR: binding.S_IFDIR,
@@ -19,7 +18,16 @@ const constants = exports.constants = {
19
18
  S_IFLNK: binding.S_IFLNK,
20
19
  S_IFBLK: binding.S_IFBLK || 0,
21
20
  S_IFIFO: binding.S_IFIFO || 0,
22
- S_IFSOCK: binding.S_IFSOCK || 0
21
+ S_IFSOCK: binding.S_IFSOCK || 0,
22
+
23
+ UV_DIRENT_UNKNOWN: binding.UV_DIRENT_UNKNOWN,
24
+ UV_DIRENT_FILE: binding.UV_DIRENT_FILE,
25
+ UV_DIRENT_DIR: binding.UV_DIRENT_DIR,
26
+ UV_DIRENT_LINK: binding.UV_DIRENT_LINK,
27
+ UV_DIRENT_FIFO: binding.UV_DIRENT_FIFO,
28
+ UV_DIRENT_SOCKET: binding.UV_DIRENT_SOCKET,
29
+ UV_DIRENT_CHAR: binding.UV_DIRENT_CHAR,
30
+ UV_DIRENT_BLOCK: binding.UV_DIRENT_BLOCK
23
31
  }
24
32
 
25
33
  const reqs = []
@@ -29,10 +37,14 @@ const fs = {
29
37
  handle: Buffer.allocUnsafe(binding.sizeofFS)
30
38
  }
31
39
 
32
- binding.init(fs.handle, fs, onfsresponse)
40
+ binding.init(fs.handle, fs, onresponse)
33
41
 
34
42
  process.on('exit', () => binding.destroy(fs.handle))
35
43
 
44
+ if (process.thread) {
45
+ process.thread.on('exit', () => binding.destroy(fs.handle))
46
+ }
47
+
36
48
  // Lightly-modified from the Node FS internal utils.
37
49
  function flagsToNumber (flags) {
38
50
  switch (flags) {
@@ -86,8 +98,6 @@ function alloc () {
86
98
  handle,
87
99
  view,
88
100
  type: 0,
89
- buffer: null,
90
- buffers: null,
91
101
  callback: null
92
102
  }
93
103
 
@@ -100,7 +110,7 @@ function getReq () {
100
110
  return used === reqs.length ? alloc() : reqs[used++]
101
111
  }
102
112
 
103
- function onfsresponse (id, result) {
113
+ function onresponse (id, result) {
104
114
  const req = reqs[id]
105
115
  used--
106
116
 
@@ -111,17 +121,13 @@ function onfsresponse (id, result) {
111
121
  }
112
122
 
113
123
  const callback = req.callback
114
- const buffer = req.buffer
115
- const buffers = req.buffers
116
124
 
117
125
  req.callback = null
118
- req.buffer = null
119
- req.buffers = null
120
126
 
121
127
  if (result < 0) {
122
128
  callback(createError(result), result, null)
123
129
  } else {
124
- callback(null, result, buffer || buffers)
130
+ callback(null, result)
125
131
  }
126
132
  }
127
133
 
@@ -135,249 +141,362 @@ function createError (errno) {
135
141
  return err
136
142
  }
137
143
 
138
- function write (fd, buf, offset, len, pos, cb) {
139
- if (typeof cb === 'function') {
140
- const req = getReq()
141
-
142
- req.buffer = buf
143
- req.callback = cb
144
-
145
- binding.write(req.handle, fd, buf, offset, len, pos)
146
- return
144
+ function open (path, flags, mode, cb) {
145
+ if (typeof path !== 'string') {
146
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
147
147
  }
148
148
 
149
- if (typeof offset === 'function') return write(fd, buf, 0, buf.byteLength, null, offset)
150
- if (typeof len === 'function') return write(fd, buf, offset, buf.byteLength - offset, null, len)
151
- if (typeof pos === 'function') return write(fd, buf, offset, len, null, pos)
149
+ if (typeof cb !== 'function') {
150
+ if (typeof flags === 'function') {
151
+ cb = flags
152
+ flags = 'r'
153
+ mode = 0o666
154
+ } else if (typeof mode === 'function') {
155
+ cb = mode
156
+ mode = 0o666
157
+ } else {
158
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
159
+ }
160
+ }
152
161
 
153
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received ' + cb)
154
- }
162
+ if (typeof flags === 'string') flags = flagsToNumber(flags)
163
+ if (typeof mode === 'string') mode = modeToNumber(mode)
155
164
 
156
- function writeSync (fd, buf, offset = 0, len = buf.byteLength, pos = 0) {
157
- return binding.writeSync(fd, buf, offset, len, pos)
165
+ const req = getReq()
166
+ req.callback = cb
167
+ binding.open(req.handle, path, flags, mode)
158
168
  }
159
169
 
160
- function writev (fd, buffers, pos, cb) {
161
- if (typeof pos === 'function') {
162
- cb = pos
163
- pos = 0
170
+ function openSync (path, flags = 'r', mode = 0o666) {
171
+ if (typeof path !== 'string') {
172
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
164
173
  }
165
174
 
166
- const req = getReq()
175
+ if (typeof flags === 'string') flags = flagsToNumber(flags)
176
+ if (typeof mode === 'string') mode = modeToNumber(mode)
167
177
 
168
- req.buffers = buffers
169
- req.callback = cb
178
+ const res = binding.openSync(path, flags, mode)
179
+ if (res < 0) throw createError(res)
170
180
 
171
- binding.writev(req.handle, fd, buffers, pos)
181
+ return res
172
182
  }
173
183
 
174
- function read (fd, buf, offset, len, pos, cb) {
175
- if (typeof cb === 'function') {
176
- const req = getReq()
177
-
178
- req.buffer = buf
179
- req.callback = cb
180
-
181
- binding.read(req.handle, fd, buf, offset, len, pos)
182
- return
184
+ function close (fd, cb = noop) {
185
+ if (typeof fd !== 'number') {
186
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
183
187
  }
184
188
 
185
- if (typeof offset === 'function') return read(fd, buf, 0, buf.byteLength, null, offset)
186
- if (typeof len === 'function') return read(fd, buf, offset, buf.byteLength - offset, null, len)
187
- if (typeof pos === 'function') return read(fd, buf, offset, len, null, pos)
189
+ if (fd < 0 || fd > 0x7fffffff) {
190
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
191
+ }
188
192
 
189
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received ' + cb)
190
- }
193
+ if (typeof cb !== 'function') {
194
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
195
+ }
191
196
 
192
- function readSync (fd, buf, offset = 0, len = buf.byteLength, pos = 0) {
193
- return binding.readSync(fd, buf, offset, len, pos)
197
+ const req = getReq()
198
+ req.callback = cb
199
+ binding.close(req.handle, fd)
194
200
  }
195
201
 
196
- function readv (fd, buffers, pos, cb) {
197
- if (typeof pos === 'function') {
198
- cb = pos
199
- pos = 0
202
+ function closeSync (fd) {
203
+ if (typeof fd !== 'number') {
204
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
200
205
  }
201
206
 
202
- const req = getReq()
207
+ if (fd < 0 || fd > 0x7fffffff) {
208
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
209
+ }
203
210
 
204
- req.buffers = buffers
205
- req.callback = cb
211
+ const res = binding.closeSync(fd)
212
+ if (res < 0) throw createError(res)
206
213
 
207
- binding.readv(req.handle, fd, buffers, pos)
214
+ return res
208
215
  }
209
216
 
210
- function open (filename, flags = 'r', mode = 0o666, cb) {
211
- if (typeof filename !== 'string') throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received ' + filename)
217
+ function read (fd, buffer, offset, len, pos, cb) {
218
+ if (typeof fd !== 'number') {
219
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
220
+ }
212
221
 
213
- if (typeof flags === 'function') return open(filename, undefined, undefined, flags)
214
- if (typeof mode === 'function') return open(filename, flags, undefined, mode)
222
+ if (fd < 0 || fd > 0x7fffffff) {
223
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
224
+ }
215
225
 
216
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received ' + cb)
226
+ if (!Buffer.isBuffer(buffer)) {
227
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
228
+ }
217
229
 
218
- if (typeof flags === 'string') flags = flagsToNumber(flags)
219
- if (typeof mode === 'string') mode = modeToNumber(mode)
230
+ if (typeof cb !== 'function') {
231
+ if (typeof offset === 'function') {
232
+ cb = offset
233
+ offset = 0
234
+ len = buffer.byteLength
235
+ pos = -1
236
+ } else if (typeof len === 'function') {
237
+ cb = len
238
+ len = buffer.byteLength - offset
239
+ pos = -1
240
+ } else if (typeof pos === 'function') {
241
+ cb = pos
242
+ pos = -1
243
+ } else {
244
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
245
+ }
246
+ }
220
247
 
221
- const req = getReq()
248
+ if (typeof position !== 'number') pos = -1
222
249
 
250
+ const req = getReq()
223
251
  req.callback = cb
224
- binding.open(req.handle, filename, flags, mode)
252
+ binding.read(req.handle, fd, buffer, offset, len, pos)
225
253
  }
226
254
 
227
- function openSync (filename, flags = 'r', mode = 0o666) {
228
- if (typeof filename !== 'string') throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received ' + filename)
255
+ function readSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
256
+ if (typeof fd !== 'number') {
257
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
258
+ }
229
259
 
230
- if (typeof flags === 'string') flags = flagsToNumber(flags)
231
- if (typeof mode === 'string') mode = modeToNumber(mode)
260
+ if (fd < 0 || fd > 0x7fffffff) {
261
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
262
+ }
232
263
 
233
- const res = binding.openSync(filename, flags, mode)
264
+ if (!Buffer.isBuffer(buffer)) {
265
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
266
+ }
234
267
 
235
- if (res < 0) throw createError(res)
236
- return res
268
+ return binding.readSync(fd, buffer, offset, len, pos)
237
269
  }
238
270
 
239
- function close (fd, cb = noop) {
240
- if (typeof fd !== 'number') throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
241
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
242
- if (!(fd >= 0 && fd <= 0x7fffffff)) throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
271
+ function readv (fd, buffers, pos, cb) {
272
+ if (typeof fd !== 'number') {
273
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
274
+ }
243
275
 
244
- const req = getReq()
276
+ if (fd < 0 || fd > 0x7fffffff) {
277
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
278
+ }
279
+
280
+ if (typeof pos === 'function') {
281
+ cb = pos
282
+ pos = -1
283
+ } else if (typeof cb !== 'function') {
284
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
285
+ }
245
286
 
287
+ if (typeof position !== 'number') pos = -1
288
+
289
+ const req = getReq()
246
290
  req.callback = cb
247
- binding.close(req.handle, fd)
291
+ binding.readv(req.handle, fd, buffers, pos)
248
292
  }
249
293
 
250
- function closeSync (fd) {
251
- const res = binding.closeSync(fd)
294
+ function write (fd, buffer, offset, len, pos, cb) {
295
+ if (typeof fd !== 'number') {
296
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
297
+ }
252
298
 
253
- if (res < 0) throw createError(res)
254
- return res
255
- }
299
+ if (fd < 0 || fd > 0x7fffffff) {
300
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
301
+ }
256
302
 
257
- function ftruncate (fd, len, cb) {
258
- const req = getReq()
303
+ if (!Buffer.isBuffer(buffer)) {
304
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
305
+ }
306
+
307
+ if (typeof cb !== 'function') {
308
+ if (typeof offset === 'function') {
309
+ cb = offset
310
+ offset = 0
311
+ len = buffer.byteLength
312
+ pos = -1
313
+ } else if (typeof len === 'function') {
314
+ cb = len
315
+ len = buffer.byteLength - offset
316
+ pos = -1
317
+ } else if (typeof pos === 'function') {
318
+ cb = pos
319
+ pos = -1
320
+ } else {
321
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
322
+ }
323
+ }
324
+
325
+ if (typeof position !== 'number') pos = -1
259
326
 
327
+ const req = getReq()
260
328
  req.callback = cb
261
- binding.ftruncate(req.handle, fd, len)
329
+ binding.write(req.handle, fd, buffer, offset, len, pos)
262
330
  }
263
331
 
264
- class Stats {
265
- constructor (buf) {
266
- const view = new Uint32Array(buf.buffer, buf.byteOffset, 32)
267
-
268
- this.dev = toNumber(view, 0)
269
- this.mode = toNumber(view, 2)
270
- this.nlink = toNumber(view, 4)
271
- this.uid = toNumber(view, 6)
272
- this.gid = toNumber(view, 8)
273
- this.rdev = toNumber(view, 10)
274
- this.ino = toNumber(view, 12)
275
- this.size = toNumber(view, 14)
276
- this.blksize = toNumber(view, 16)
277
- this.blocks = toNumber(view, 18)
278
- this.flags = toNumber(view, 20)
279
- this.gen = toNumber(view, 22)
280
- this.atimeMs = toNumber(view, 24)
281
- this.mtimeMs = toNumber(view, 26)
282
- this.ctimeMs = toNumber(view, 28)
283
- this.birthtimeMs = toNumber(view, 30)
284
- this.atime = new Date(this.atimeMs)
285
- this.mtime = new Date(this.mtimeMs)
286
- this.ctime = new Date(this.ctimeMs)
287
- this.birthtime = new Date(this.birthtimeMs)
332
+ function writeSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
333
+ if (typeof fd !== 'number') {
334
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
288
335
  }
289
336
 
290
- isDirectory () {
291
- return (this.mode & constants.S_IFMT) === constants.S_IFDIR
337
+ if (fd < 0 || fd > 0x7fffffff) {
338
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
292
339
  }
293
340
 
294
- isFile () {
295
- return (this.mode & constants.S_IFMT) === constants.S_IFREG
341
+ if (!Buffer.isBuffer(buffer)) {
342
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
296
343
  }
297
344
 
298
- isBlockDevice () {
299
- return (this.mode & constants.S_IFMT) === constants.S_IFBLK
300
- }
345
+ return binding.writeSync(fd, buffer, offset, len, pos)
346
+ }
301
347
 
302
- isCharacterDevice () {
303
- return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
348
+ function writev (fd, buffers, pos, cb) {
349
+ if (typeof fd !== 'number') {
350
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
304
351
  }
305
352
 
306
- isFIFO () {
307
- return (this.mode & constants.S_IFMT) === constants.S_IFIFO
353
+ if (fd < 0 || fd > 0x7fffffff) {
354
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
308
355
  }
309
356
 
310
- isSymbolicLink () {
311
- return (this.mode & constants.S_IFMT) === constants.S_IFLNK
357
+ if (typeof pos === 'function') {
358
+ cb = pos
359
+ pos = -1
360
+ } else if (typeof cb !== 'function') {
361
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
312
362
  }
313
363
 
314
- isSocket () {
315
- return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
316
- }
317
- }
364
+ if (typeof position !== 'number') pos = -1
318
365
 
319
- function toNumber (view, n) {
320
- return LE ? view[n] + view[n + 1] * 0x100000000 : view[n] * 0x100000000 + view[n + 1]
366
+ const req = getReq()
367
+ req.callback = cb
368
+ binding.writev(req.handle, fd, buffers, pos)
321
369
  }
322
370
 
323
371
  function stat (path, cb) {
324
- const req = getReq()
372
+ if (typeof path !== 'string') {
373
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
374
+ }
375
+
376
+ if (typeof cb !== 'function') {
377
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
378
+ }
379
+
380
+ const data = new Array(Stats.length)
325
381
 
326
- req.buffer = Buffer.allocUnsafe(16 * 8)
382
+ const req = getReq()
327
383
 
328
- req.callback = function (err, _, buf) {
384
+ req.callback = function (err, _) {
329
385
  if (err) cb(err, null)
330
- else cb(null, new Stats(buf))
386
+ else cb(null, new Stats(...data))
331
387
  }
332
388
 
333
- binding.stat(req.handle, path, req.buffer)
389
+ binding.stat(req.handle, path, data)
334
390
  }
335
391
 
336
392
  function statSync (path) {
337
- const buffer = Buffer.allocUnsafe(16 * 8)
338
- const res = binding.statSync(path, buffer)
393
+ if (typeof path !== 'string') {
394
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
395
+ }
396
+
397
+ const data = new Array(Stats.length)
398
+
399
+ const res = binding.statSync(path, data)
339
400
  if (res < 0) throw createError(res)
340
- return new Stats(buffer)
401
+
402
+ return new Stats(...data)
341
403
  }
342
404
 
343
405
  function lstat (path, cb) {
344
- const req = getReq()
406
+ if (typeof path !== 'string') {
407
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
408
+ }
409
+
410
+ if (typeof cb !== 'function') {
411
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
412
+ }
345
413
 
346
- req.buffer = Buffer.allocUnsafe(16 * 8)
414
+ const data = new Array(Stats.length)
347
415
 
348
- req.callback = function (err, _, buf) {
416
+ const req = getReq()
417
+
418
+ req.callback = function (err, _) {
349
419
  if (err) cb(err, null)
350
- else cb(null, new Stats(buf))
420
+ else cb(null, new Stats(...data))
351
421
  }
352
422
 
353
- binding.lstat(req.handle, path, req.buffer)
423
+ binding.lstat(req.handle, path, data)
354
424
  }
355
425
 
356
426
  function lstatSync (path) {
357
- const buffer = Buffer.allocUnsafe(16 * 8)
358
- const res = binding.lstatSync(path, buffer)
427
+ if (typeof path !== 'string') {
428
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
429
+ }
430
+
431
+ const data = new Array(Stats.length)
432
+
433
+ const res = binding.lstatSync(path, data)
359
434
  if (res < 0) throw createError(res)
360
- return new Stats(buffer)
435
+
436
+ return new Stats(...data)
361
437
  }
362
438
 
363
439
  function fstat (fd, cb) {
364
- const req = getReq()
440
+ if (typeof fd !== 'number') {
441
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
442
+ }
365
443
 
366
- req.buffer = Buffer.allocUnsafe(16 * 8)
444
+ if (fd < 0 || fd > 0x7fffffff) {
445
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
446
+ }
367
447
 
368
- req.callback = function (err, _, buf) {
448
+ if (typeof cb !== 'function') {
449
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
450
+ }
451
+
452
+ const data = new Array(Stats.length)
453
+
454
+ const req = getReq()
455
+
456
+ req.callback = function (err, _) {
369
457
  if (err) cb(err, null)
370
- else cb(null, new Stats(buf))
458
+ else cb(null, new Stats(...data))
371
459
  }
372
460
 
373
- binding.fstat(req.handle, fd, req.buffer)
461
+ binding.fstat(req.handle, fd, data)
374
462
  }
375
463
 
376
464
  function fstatSync (fd) {
377
- const buffer = Buffer.allocUnsafe(16 * 8)
378
- const res = binding.fstatSync(fd, buffer)
465
+ if (typeof fd !== 'number') {
466
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
467
+ }
468
+
469
+ if (fd < 0 || fd > 0x7fffffff) {
470
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
471
+ }
472
+
473
+ const data = new Array(Stats.length)
474
+
475
+ const res = binding.fstatSync(fd, data)
379
476
  if (res < 0) throw createError(res)
380
- return new Stats(buffer)
477
+
478
+ return new Stats(...data)
479
+ }
480
+
481
+ function ftruncate (fd, len, cb) {
482
+ if (typeof fd !== 'number') {
483
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
484
+ }
485
+
486
+ if (fd < 0 || fd > 0x7fffffff) {
487
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
488
+ }
489
+
490
+ if (typeof len === 'function') {
491
+ cb = len
492
+ len = 0
493
+ } else if (typeof cb !== 'function') {
494
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
495
+ }
496
+
497
+ const req = getReq()
498
+ req.callback = cb
499
+ binding.ftruncate(req.handle, fd, len)
381
500
  }
382
501
 
383
502
  function mkdirp (path, mode, cb) {
@@ -404,101 +523,208 @@ function mkdirp (path, mode, cb) {
404
523
  })
405
524
  }
406
525
 
407
- function rename (src, dst, cb) {
408
- const req = getReq()
409
-
410
- req.callback = cb
411
- binding.rename(req.handle, src, dst)
412
- }
413
-
414
526
  function mkdir (path, opts, cb) {
527
+ if (typeof path !== 'string') {
528
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
529
+ }
530
+
415
531
  if (typeof opts === 'function') {
416
532
  cb = opts
417
533
  opts = { mode: 0o777 }
534
+ } else if (typeof cb !== 'function') {
535
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
418
536
  }
419
537
 
420
- if (!opts) opts = {}
538
+ if (typeof opts === 'number') opts = { mode: opts }
539
+ else if (!opts) opts = {}
421
540
 
422
541
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
423
542
 
424
- if (opts.recursive) {
425
- return mkdirp(path, mode, cb)
426
- }
543
+ if (opts.recursive) return mkdirp(path, mode, cb)
427
544
 
428
545
  const req = getReq()
429
-
430
546
  req.callback = cb
431
547
  binding.mkdir(req.handle, path, mode)
432
548
  }
433
549
 
434
550
  function rmdir (path, cb) {
435
- const req = getReq()
551
+ if (typeof path !== 'string') {
552
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
553
+ }
554
+
555
+ if (typeof cb !== 'function') {
556
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
557
+ }
436
558
 
559
+ const req = getReq()
437
560
  req.callback = cb
438
561
  binding.rmdir(req.handle, path)
439
562
  }
440
563
 
441
564
  function unlink (path, cb) {
442
- const req = getReq()
565
+ if (typeof path !== 'string') {
566
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
567
+ }
443
568
 
569
+ if (typeof cb !== 'function') {
570
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
571
+ }
572
+
573
+ const req = getReq()
444
574
  req.callback = cb
445
575
  binding.unlink(req.handle, path)
446
576
  }
447
577
 
578
+ function rename (src, dst, cb) {
579
+ if (typeof src !== 'string') {
580
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
581
+ }
582
+
583
+ if (typeof dst !== 'string') {
584
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
585
+ }
586
+
587
+ if (typeof cb !== 'function') {
588
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
589
+ }
590
+
591
+ const req = getReq()
592
+ req.callback = cb
593
+ binding.rename(req.handle, src, dst)
594
+ }
595
+
448
596
  function readlink (path, opts, cb) {
449
- if (typeof opts === 'function') return readlink(path, null, opts)
450
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
597
+ if (typeof path !== 'string') {
598
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
599
+ }
600
+
601
+ if (typeof opts === 'function') {
602
+ cb = opts
603
+ opts = {}
604
+ } else if (typeof cb !== 'function') {
605
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
606
+ }
607
+
451
608
  if (typeof opts === 'string') opts = { encoding: opts }
452
- if (!opts) opts = {}
609
+ else if (!opts) opts = {}
453
610
 
454
611
  const {
455
612
  encoding = 'utf8'
456
613
  } = opts
457
614
 
615
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
616
+
458
617
  const req = getReq()
459
618
 
460
- req.buffer = Buffer.allocUnsafe(4097)
619
+ req.callback = function (err, _) {
620
+ if (err) return cb(err, null)
621
+ let path = data.subarray(0, data.indexOf(0))
622
+ if (encoding !== 'buffer') path = path.toString(encoding)
623
+ cb(null, path)
624
+ }
625
+
626
+ binding.readlink(req.handle, path, data)
627
+ }
628
+
629
+ function opendir (path, opts, cb) {
630
+ if (typeof path !== 'string') {
631
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
632
+ }
633
+
634
+ if (typeof opts === 'function') {
635
+ cb = opts
636
+ opts = {}
637
+ } else if (typeof cb !== 'function') {
638
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
639
+ }
640
+
641
+ if (typeof opts === 'string') opts = { encoding: opts }
642
+ else if (!opts) opts = {}
643
+
644
+ const data = Buffer.allocUnsafe(binding.sizeofFSDir)
645
+
646
+ const req = getReq()
461
647
 
462
- req.callback = function (err, _, buf) {
648
+ req.callback = function (err, _) {
463
649
  if (err) return cb(err, null)
464
- buf = buf.subarray(0, buf.indexOf(0))
465
- if (encoding !== 'buffer') return cb(null, Buffer.coerce(buf).toString(encoding))
466
- cb(null, buf)
650
+ cb(null, new Dir(path, data, opts))
467
651
  }
468
652
 
469
- binding.readlink(req.handle, path, req.buffer)
653
+ binding.opendir(req.handle, path, data)
470
654
  }
471
655
 
472
- function readFile (path, opts, cb) {
473
- if (typeof opts === 'function') return readFile(path, null, opts)
474
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
656
+ function readdir (path, opts, cb) {
657
+ if (typeof path !== 'string') {
658
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
659
+ }
660
+
661
+ if (typeof opts === 'function') {
662
+ cb = opts
663
+ opts = {}
664
+ } else if (typeof cb !== 'function') {
665
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
666
+ }
667
+
475
668
  if (typeof opts === 'string') opts = { encoding: opts }
476
669
  if (!opts) opts = {}
477
670
 
671
+ const {
672
+ withFileTypes = false
673
+ } = opts
674
+
675
+ opendir(path, opts, async (err, dir) => {
676
+ if (err) return cb(err, null)
677
+ const result = []
678
+ dir
679
+ .on('data', (entry) => result.push(withFileTypes ? entry : entry.name))
680
+ .on('error', (err) => cb(err, null))
681
+ .on('end', () => cb(null, result))
682
+ })
683
+ }
684
+
685
+ function readFile (path, opts, cb) {
686
+ if (typeof path !== 'string') {
687
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
688
+ }
689
+
690
+ if (typeof opts === 'function') {
691
+ cb = opts
692
+ opts = {}
693
+ } else if (typeof cb !== 'function') {
694
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
695
+ }
696
+
697
+ if (typeof opts === 'string') opts = { encoding: opts }
698
+ else if (!opts) opts = {}
699
+
700
+ const {
701
+ encoding = 'buffer'
702
+ } = opts
703
+
478
704
  open(path, opts.flag || 'r', function (err, fd) {
479
705
  if (err) return cb(err)
480
706
 
481
707
  fstat(fd, function (err, st) {
482
708
  if (err) return closeAndError(err)
483
709
 
484
- let buf = Buffer.allocUnsafe(st.size)
710
+ let buffer = Buffer.allocUnsafe(st.size)
485
711
  let len = 0
486
712
 
487
- read(fd, buf, loop)
713
+ read(fd, buffer, loop)
488
714
 
489
715
  function loop (err, r) {
490
716
  if (err) return closeAndError(err)
491
717
  len += r
492
- if (r === 0 || len === buf.byteLength) return done()
493
- read(fd, buf.subarray(len), loop)
718
+ if (r === 0 || len === buffer.byteLength) return done()
719
+ read(fd, buffer.subarray(len), loop)
494
720
  }
495
721
 
496
722
  function done () {
497
- if (len !== buf.byteLength) buf = buf.subarray(0, len)
723
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
498
724
  close(fd, function (err) {
499
725
  if (err) return cb(err)
500
- if (opts.encoding) return cb(null, buf.toString(opts.encoding))
501
- return cb(null, buf)
726
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
727
+ cb(null, buffer)
502
728
  })
503
729
  }
504
730
  })
@@ -512,26 +738,34 @@ function readFile (path, opts, cb) {
512
738
  }
513
739
 
514
740
  function readFileSync (path, opts) {
741
+ if (typeof path !== 'string') {
742
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
743
+ }
744
+
515
745
  if (typeof opts === 'string') opts = { encoding: opts }
516
- if (!opts) opts = {}
746
+ else if (!opts) opts = {}
747
+
748
+ const {
749
+ encoding = 'buffer'
750
+ } = opts
517
751
 
518
752
  const fd = openSync(path, opts.flag || 'r')
519
753
 
520
754
  try {
521
755
  const st = fstatSync(fd)
522
756
 
523
- let buf = Buffer.allocUnsafe(st.size)
757
+ let buffer = Buffer.allocUnsafe(st.size)
524
758
  let len = 0
525
759
 
526
760
  while (true) {
527
- const r = readSync(fd, len ? buf.subarray(len) : buf)
761
+ const r = readSync(fd, len ? buffer.subarray(len) : buffer)
528
762
  len += r
529
- if (r === 0 || len === buf.byteLength) break
763
+ if (r === 0 || len === buffer.byteLength) break
530
764
  }
531
- if (len !== buf.byteLength) buf = buf.subarray(0, len)
532
765
 
533
- if (opts.encoding) return Buffer.coerce(buf).toString(opts.encoding)
534
- return buf
766
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
767
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
768
+ return buffer
535
769
  } finally {
536
770
  try {
537
771
  closeSync(fd)
@@ -540,16 +774,26 @@ function readFileSync (path, opts) {
540
774
  }
541
775
 
542
776
  function writeFile (path, data, opts, cb) {
543
- if (typeof opts === 'function') return writeFile(path, data, null, opts)
544
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
545
- if (typeof data !== 'string' && !Buffer.isBuffer(data)) throw typeError('ERR_INVALID_ARG_TYPE', 'The data argument must be of type string or buffer')
546
- if (typeof opts === 'string') opts = { encoding: opts }
547
- if (!opts) opts = {}
777
+ if (typeof path !== 'string') {
778
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
779
+ }
548
780
 
549
- if (opts.encoding || typeof data === 'string') {
550
- data = Buffer.from(data, opts.encoding)
781
+ if (typeof data !== 'string' && !Buffer.isBuffer(data)) {
782
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
551
783
  }
552
784
 
785
+ if (typeof opts === 'function') {
786
+ cb = opts
787
+ opts = {}
788
+ } else if (typeof cb !== 'function') {
789
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
790
+ }
791
+
792
+ if (typeof opts === 'string') opts = { encoding: opts }
793
+ else if (!opts) opts = {}
794
+
795
+ if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
796
+
553
797
  open(path, opts.flag || 'w', opts.mode, function (err, fd) {
554
798
  if (err) return cb(err)
555
799
 
@@ -576,21 +820,28 @@ function writeFile (path, data, opts, cb) {
576
820
  })
577
821
  }
578
822
 
579
- function writeFileSync (path, buf, opts) {
580
- if (typeof opts === 'string') opts = { encoding: opts }
581
- if (!opts) opts = {}
823
+ function writeFileSync (path, data, opts) {
824
+ if (typeof path !== 'string') {
825
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
826
+ }
582
827
 
583
- if (opts.encoding || typeof buf === 'string') {
584
- buf = Buffer.from(buf, opts.encoding)
828
+ if (typeof data !== 'string' && !Buffer.isBuffer(data)) {
829
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
585
830
  }
586
831
 
832
+ if (typeof opts === 'string') opts = { encoding: opts }
833
+ else if (!opts) opts = {}
834
+
835
+ if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
836
+
587
837
  const fd = openSync(path, opts.flag || 'w', opts.mode)
588
838
 
589
839
  try {
590
840
  let len = 0
841
+
591
842
  while (true) {
592
- len += writeSync(fd, len ? buf.subarray(len) : buf)
593
- if (len === buf.byteLength) break
843
+ len += writeSync(fd, len ? data.subarray(len) : data)
844
+ if (len === data.byteLength) break
594
845
  }
595
846
  } finally {
596
847
  try {
@@ -599,6 +850,57 @@ function writeFileSync (path, buf, opts) {
599
850
  }
600
851
  }
601
852
 
853
+ class Stats {
854
+ constructor (dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeMs, mtimeMs, ctimeMs, birthtimeMs) {
855
+ this.dev = dev
856
+ this.mode = mode
857
+ this.nlink = nlink
858
+ this.uid = uid
859
+ this.gid = gid
860
+ this.rdev = rdev
861
+ this.blksize = blksize
862
+ this.ino = ino
863
+ this.size = size
864
+ this.blocks = blocks
865
+ this.atimeMs = atimeMs
866
+ this.mtimeMs = mtimeMs
867
+ this.ctimeMs = ctimeMs
868
+ this.birthtimeMs = birthtimeMs
869
+ this.atime = new Date(atimeMs)
870
+ this.mtime = new Date(mtimeMs)
871
+ this.ctime = new Date(ctimeMs)
872
+ this.birthtime = new Date(birthtimeMs)
873
+ }
874
+
875
+ isDirectory () {
876
+ return (this.mode & constants.S_IFMT) === constants.S_IFDIR
877
+ }
878
+
879
+ isFile () {
880
+ return (this.mode & constants.S_IFMT) === constants.S_IFREG
881
+ }
882
+
883
+ isBlockDevice () {
884
+ return (this.mode & constants.S_IFMT) === constants.S_IFBLK
885
+ }
886
+
887
+ isCharacterDevice () {
888
+ return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
889
+ }
890
+
891
+ isFIFO () {
892
+ return (this.mode & constants.S_IFMT) === constants.S_IFIFO
893
+ }
894
+
895
+ isSymbolicLink () {
896
+ return (this.mode & constants.S_IFMT) === constants.S_IFLNK
897
+ }
898
+
899
+ isSocket () {
900
+ return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
901
+ }
902
+ }
903
+
602
904
  class FileWriteStream extends Writable {
603
905
  constructor (path, opts = {}) {
604
906
  super({ map })
@@ -702,6 +1004,104 @@ class FileReadStream extends Readable {
702
1004
  }
703
1005
  }
704
1006
 
1007
+ class Dir extends Readable {
1008
+ constructor (path, handle, opts = {}) {
1009
+ const {
1010
+ encoding = 'utf8',
1011
+ bufferSize = 32
1012
+ } = opts
1013
+
1014
+ super()
1015
+
1016
+ this._handle = handle
1017
+ this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
1018
+ this._encoding = encoding
1019
+ this._closed = false
1020
+
1021
+ this.path = path
1022
+ }
1023
+
1024
+ _read (cb) {
1025
+ const self = this
1026
+ const data = []
1027
+
1028
+ const req = getReq()
1029
+
1030
+ req.callback = function (err, _) {
1031
+ if (err) return cb(err)
1032
+ if (data.length === 0) self.push(null)
1033
+ else {
1034
+ for (const entry of data) {
1035
+ let name = Buffer.from(entry.name)
1036
+ if (self._encoding !== 'buffer') name = name.toString(self._encoding)
1037
+ self.push(new Dirent(self.path, name, entry.type))
1038
+ }
1039
+ }
1040
+ cb(null)
1041
+ }
1042
+
1043
+ binding.readdir(req.handle, this._handle, this._dirents, data)
1044
+ }
1045
+
1046
+ _destroy (cb) {
1047
+ if (this._closed) cb(null)
1048
+ else this.close(cb)
1049
+ }
1050
+
1051
+ close (cb = noop) {
1052
+ if (this._closed) return
1053
+
1054
+ const self = this
1055
+
1056
+ const req = getReq()
1057
+
1058
+ req.callback = function (err, _) {
1059
+ if (err) return cb(err)
1060
+ self._closed = true
1061
+ cb(null)
1062
+ }
1063
+
1064
+ binding.closedir(req.handle, this._handle)
1065
+ }
1066
+ }
1067
+
1068
+ class Dirent {
1069
+ constructor (path, name, type) {
1070
+ this._type = type
1071
+
1072
+ this.path = path
1073
+ this.name = name
1074
+ }
1075
+
1076
+ isFile () {
1077
+ return this._type === constants.UV_DIRENT_FILE
1078
+ }
1079
+
1080
+ isDirectory () {
1081
+ return this._type === constants.UV_DIRENT_DIR
1082
+ }
1083
+
1084
+ isSymbolicLink () {
1085
+ return this._type === constants.UV_DIRENT_LINK
1086
+ }
1087
+
1088
+ isFIFO () {
1089
+ return this._type === constants.UV_DIRENT_FIFO
1090
+ }
1091
+
1092
+ isSocket () {
1093
+ return this._type === constants.UV_DIRENT_SOCKET
1094
+ }
1095
+
1096
+ isCharacterDevice () {
1097
+ return this._type === constants.UV_DIRENT_CHAR
1098
+ }
1099
+
1100
+ isBlockDevice () {
1101
+ return this._type === constants.UV_DIRENT_BLOCK
1102
+ }
1103
+ }
1104
+
705
1105
  exports.promises = {}
706
1106
 
707
1107
  function typeError (code, message) {
@@ -718,31 +1118,22 @@ exports.read = read
718
1118
  exports.readv = readv
719
1119
  exports.write = write
720
1120
  exports.writev = writev
721
- exports.ftruncate = ftruncate
722
1121
  exports.fstat = fstat
1122
+ exports.ftruncate = ftruncate
723
1123
 
724
1124
  exports.openSync = openSync
725
1125
  exports.closeSync = closeSync
726
1126
  exports.readSync = readSync
727
1127
  exports.writeSync = writeSync
728
- exports.fstatSync = fstatSync
729
1128
  exports.statSync = statSync
730
1129
  exports.lstatSync = lstatSync
1130
+ exports.fstatSync = fstatSync
731
1131
 
732
- exports.readFileSync = readFileSync
733
- exports.writeFileSync = writeFileSync
734
-
735
- exports.unlink = unlink
736
- exports.promises.unlink = promisify(unlink)
737
-
738
- exports.readFile = readFile
739
- exports.promises.readFile = promisify(readFile)
740
-
741
- exports.writeFile = writeFile
742
- exports.promises.writeFile = promisify(writeFile)
1132
+ exports.stat = stat
1133
+ exports.promises.stat = promisify(stat)
743
1134
 
744
- exports.rename = rename
745
- exports.promises.rename = promisify(rename)
1135
+ exports.lstat = lstat
1136
+ exports.promises.lstat = promisify(lstat)
746
1137
 
747
1138
  exports.mkdir = mkdir
748
1139
  exports.promises.mkdir = promisify(mkdir)
@@ -750,22 +1141,41 @@ exports.promises.mkdir = promisify(mkdir)
750
1141
  exports.rmdir = rmdir
751
1142
  exports.promises.rmdir = promisify(rmdir)
752
1143
 
753
- exports.Stats = Stats // for compat
754
-
755
- exports.stat = stat
756
- exports.promises.stat = promisify(stat)
1144
+ exports.unlink = unlink
1145
+ exports.promises.unlink = promisify(unlink)
757
1146
 
758
- exports.lstat = lstat
759
- exports.promises.lstat = promisify(lstat)
1147
+ exports.rename = rename
1148
+ exports.promises.rename = promisify(rename)
760
1149
 
761
1150
  exports.readlink = readlink
762
1151
  exports.promises.readlink = promisify(readlink)
763
1152
 
1153
+ exports.opendir = opendir
1154
+ exports.promises.opendir = promisify(opendir)
1155
+
1156
+ exports.readdir = readdir
1157
+ exports.promises.readdir = promisify(readdir)
1158
+
1159
+ exports.readFile = readFile
1160
+ exports.promises.readFile = promisify(readFile)
1161
+
1162
+ exports.writeFile = writeFile
1163
+ exports.promises.writeFile = promisify(writeFile)
1164
+
1165
+ exports.readFileSync = readFileSync
1166
+ exports.writeFileSync = writeFileSync
1167
+
1168
+ exports.Stats = Stats
1169
+
764
1170
  exports.ReadStream = FileReadStream
765
- exports.createReadStream = (path, options) => new FileReadStream(path, options)
1171
+ exports.createReadStream = function createReadStream (path, opts) {
1172
+ return new FileReadStream(path, opts)
1173
+ }
766
1174
 
767
1175
  exports.WriteStream = FileWriteStream
768
- exports.createWriteStream = (path, options) => new FileWriteStream(path, options)
1176
+ exports.createWriteStream = function createWriteStream (path, opts) {
1177
+ return new FileWriteStream(path, opts)
1178
+ }
769
1179
 
770
1180
  function promisify (fn) {
771
1181
  return function (...args) {
@@ -778,6 +1188,6 @@ function promisify (fn) {
778
1188
  }
779
1189
  }
780
1190
 
781
- function map (s) {
782
- return typeof s === 'string' ? Buffer.from(s) : s
1191
+ function map (data) {
1192
+ return typeof data === 'string' ? Buffer.from(data) : data
783
1193
  }