bare-fs 1.5.5 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +16 -10
  2. package/binding.c +905 -376
  3. package/index.js +746 -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,464 @@ 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
+ }
286
+
287
+ if (typeof position !== 'number') pos = -1
245
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
+ }
259
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
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
+ }
325
379
 
326
- req.buffer = Buffer.allocUnsafe(16 * 8)
380
+ const data = {
381
+ dev: 0,
382
+ mode: 0,
383
+ nlink: 0,
384
+ uid: 0,
385
+ gid: 0,
386
+ rdev: 0,
387
+ ino: 0,
388
+ size: 0,
389
+ blksize: 0,
390
+ blocks: 0,
391
+ flags: 0,
392
+ gen: 0,
393
+ atim: 0,
394
+ mtim: 0,
395
+ ctim: 0,
396
+ birthtim: 0
397
+ }
398
+
399
+ const req = getReq()
327
400
 
328
- req.callback = function (err, _, buf) {
401
+ req.callback = function (err, _) {
329
402
  if (err) cb(err, null)
330
- else cb(null, new Stats(buf))
403
+ else cb(null, new Stats(data))
331
404
  }
332
405
 
333
- binding.stat(req.handle, path, req.buffer)
406
+ binding.stat(req.handle, path, data)
334
407
  }
335
408
 
336
409
  function statSync (path) {
337
- const buffer = Buffer.allocUnsafe(16 * 8)
338
- const res = binding.statSync(path, buffer)
410
+ if (typeof path !== 'string') {
411
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
412
+ }
413
+
414
+ const data = {
415
+ dev: 0,
416
+ mode: 0,
417
+ nlink: 0,
418
+ uid: 0,
419
+ gid: 0,
420
+ rdev: 0,
421
+ ino: 0,
422
+ size: 0,
423
+ blksize: 0,
424
+ blocks: 0,
425
+ flags: 0,
426
+ gen: 0,
427
+ atim: 0,
428
+ mtim: 0,
429
+ ctim: 0,
430
+ birthtim: 0
431
+ }
432
+
433
+ const res = binding.statSync(path, data)
339
434
  if (res < 0) throw createError(res)
340
- return new Stats(buffer)
435
+
436
+ return new Stats(data)
341
437
  }
342
438
 
343
439
  function lstat (path, cb) {
344
- const req = getReq()
440
+ if (typeof path !== 'string') {
441
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
442
+ }
443
+
444
+ if (typeof cb !== 'function') {
445
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
446
+ }
345
447
 
346
- req.buffer = Buffer.allocUnsafe(16 * 8)
448
+ const data = {
449
+ dev: 0,
450
+ mode: 0,
451
+ nlink: 0,
452
+ uid: 0,
453
+ gid: 0,
454
+ rdev: 0,
455
+ ino: 0,
456
+ size: 0,
457
+ blksize: 0,
458
+ blocks: 0,
459
+ flags: 0,
460
+ gen: 0,
461
+ atim: 0,
462
+ mtim: 0,
463
+ ctim: 0,
464
+ birthtim: 0
465
+ }
347
466
 
348
- req.callback = function (err, _, buf) {
467
+ const req = getReq()
468
+
469
+ req.callback = function (err, _) {
349
470
  if (err) cb(err, null)
350
- else cb(null, new Stats(buf))
471
+ else cb(null, new Stats(data))
351
472
  }
352
473
 
353
- binding.lstat(req.handle, path, req.buffer)
474
+ binding.lstat(req.handle, path, data)
354
475
  }
355
476
 
356
477
  function lstatSync (path) {
357
- const buffer = Buffer.allocUnsafe(16 * 8)
358
- const res = binding.lstatSync(path, buffer)
478
+ if (typeof path !== 'string') {
479
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
480
+ }
481
+
482
+ const data = {
483
+ dev: 0,
484
+ mode: 0,
485
+ nlink: 0,
486
+ uid: 0,
487
+ gid: 0,
488
+ rdev: 0,
489
+ ino: 0,
490
+ size: 0,
491
+ blksize: 0,
492
+ blocks: 0,
493
+ flags: 0,
494
+ gen: 0,
495
+ atim: 0,
496
+ mtim: 0,
497
+ ctim: 0,
498
+ birthtim: 0
499
+ }
500
+
501
+ const res = binding.lstatSync(path, data)
359
502
  if (res < 0) throw createError(res)
360
- return new Stats(buffer)
503
+
504
+ return new Stats(data)
361
505
  }
362
506
 
363
507
  function fstat (fd, cb) {
364
- const req = getReq()
508
+ if (typeof fd !== 'number') {
509
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
510
+ }
365
511
 
366
- req.buffer = Buffer.allocUnsafe(16 * 8)
512
+ if (fd < 0 || fd > 0x7fffffff) {
513
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
514
+ }
515
+
516
+ if (typeof cb !== 'function') {
517
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
518
+ }
519
+
520
+ const data = {
521
+ dev: 0,
522
+ mode: 0,
523
+ nlink: 0,
524
+ uid: 0,
525
+ gid: 0,
526
+ rdev: 0,
527
+ ino: 0,
528
+ size: 0,
529
+ blksize: 0,
530
+ blocks: 0,
531
+ flags: 0,
532
+ gen: 0,
533
+ atim: 0,
534
+ mtim: 0,
535
+ ctim: 0,
536
+ birthtim: 0
537
+ }
538
+
539
+ const req = getReq()
367
540
 
368
- req.callback = function (err, _, buf) {
541
+ req.callback = function (err, _) {
369
542
  if (err) cb(err, null)
370
- else cb(null, new Stats(buf))
543
+ else cb(null, new Stats(data))
371
544
  }
372
545
 
373
- binding.fstat(req.handle, fd, req.buffer)
546
+ binding.fstat(req.handle, fd, data)
374
547
  }
375
548
 
376
549
  function fstatSync (fd) {
377
- const buffer = Buffer.allocUnsafe(16 * 8)
378
- const res = binding.fstatSync(fd, buffer)
550
+ if (typeof fd !== 'number') {
551
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
552
+ }
553
+
554
+ if (fd < 0 || fd > 0x7fffffff) {
555
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
556
+ }
557
+
558
+ const data = {
559
+ dev: 0,
560
+ mode: 0,
561
+ nlink: 0,
562
+ uid: 0,
563
+ gid: 0,
564
+ rdev: 0,
565
+ ino: 0,
566
+ size: 0,
567
+ blksize: 0,
568
+ blocks: 0,
569
+ flags: 0,
570
+ gen: 0,
571
+ atim: 0,
572
+ mtim: 0,
573
+ ctim: 0,
574
+ birthtim: 0
575
+ }
576
+
577
+ const res = binding.fstatSync(fd, data)
379
578
  if (res < 0) throw createError(res)
380
- return new Stats(buffer)
579
+
580
+ return new Stats(data)
581
+ }
582
+
583
+ function ftruncate (fd, len, cb) {
584
+ if (typeof fd !== 'number') {
585
+ throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
586
+ }
587
+
588
+ if (fd < 0 || fd > 0x7fffffff) {
589
+ throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
590
+ }
591
+
592
+ if (typeof len === 'function') {
593
+ cb = len
594
+ len = 0
595
+ } else if (typeof cb !== 'function') {
596
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
597
+ }
598
+
599
+ const req = getReq()
600
+ req.callback = cb
601
+ binding.ftruncate(req.handle, fd, len)
381
602
  }
382
603
 
383
604
  function mkdirp (path, mode, cb) {
@@ -404,101 +625,204 @@ function mkdirp (path, mode, cb) {
404
625
  })
405
626
  }
406
627
 
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
628
  function mkdir (path, opts, cb) {
629
+ if (typeof path !== 'string') {
630
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
631
+ }
632
+
415
633
  if (typeof opts === 'function') {
416
634
  cb = opts
417
635
  opts = { mode: 0o777 }
636
+ } else if (typeof cb !== 'function') {
637
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
418
638
  }
419
639
 
420
- if (!opts) opts = {}
640
+ if (typeof opts === 'number') opts = { mode: opts }
641
+ else if (!opts) opts = {}
421
642
 
422
643
  const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
423
644
 
424
- if (opts.recursive) {
425
- return mkdirp(path, mode, cb)
426
- }
645
+ if (opts.recursive) return mkdirp(path, mode, cb)
427
646
 
428
647
  const req = getReq()
429
-
430
648
  req.callback = cb
431
649
  binding.mkdir(req.handle, path, mode)
432
650
  }
433
651
 
434
652
  function rmdir (path, cb) {
435
- const req = getReq()
653
+ if (typeof path !== 'string') {
654
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
655
+ }
436
656
 
657
+ if (typeof cb !== 'function') {
658
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
659
+ }
660
+
661
+ const req = getReq()
437
662
  req.callback = cb
438
663
  binding.rmdir(req.handle, path)
439
664
  }
440
665
 
441
666
  function unlink (path, cb) {
442
- const req = getReq()
667
+ if (typeof path !== 'string') {
668
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
669
+ }
670
+
671
+ if (typeof cb !== 'function') {
672
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
673
+ }
443
674
 
675
+ const req = getReq()
444
676
  req.callback = cb
445
677
  binding.unlink(req.handle, path)
446
678
  }
447
679
 
680
+ function rename (src, dst, cb) {
681
+ if (typeof src !== 'string') {
682
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
683
+ }
684
+
685
+ if (typeof dst !== 'string') {
686
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
687
+ }
688
+
689
+ if (typeof cb !== 'function') {
690
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
691
+ }
692
+
693
+ const req = getReq()
694
+ req.callback = cb
695
+ binding.rename(req.handle, src, dst)
696
+ }
697
+
448
698
  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')
699
+ if (typeof path !== 'string') {
700
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
701
+ }
702
+
703
+ if (typeof opts === 'function') {
704
+ cb = opts
705
+ opts = {}
706
+ } else if (typeof cb !== 'function') {
707
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
708
+ }
709
+
451
710
  if (typeof opts === 'string') opts = { encoding: opts }
452
- if (!opts) opts = {}
711
+ else if (!opts) opts = {}
453
712
 
454
713
  const {
455
714
  encoding = 'utf8'
456
715
  } = opts
457
716
 
717
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
718
+
458
719
  const req = getReq()
459
720
 
460
- req.buffer = Buffer.allocUnsafe(4097)
721
+ req.callback = function (err, _) {
722
+ if (err) return cb(err, null)
723
+ let path = data.subarray(0, data.indexOf(0))
724
+ if (encoding !== 'buffer') path = path.toString(encoding)
725
+ cb(null, path)
726
+ }
727
+
728
+ binding.readlink(req.handle, path, data)
729
+ }
730
+
731
+ function opendir (path, opts, cb) {
732
+ if (typeof path !== 'string') {
733
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
734
+ }
735
+
736
+ if (typeof opts === 'function') {
737
+ cb = opts
738
+ opts = {}
739
+ } else if (typeof cb !== 'function') {
740
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
741
+ }
461
742
 
462
- req.callback = function (err, _, buf) {
743
+ if (typeof opts === 'string') opts = { encoding: opts }
744
+ else if (!opts) opts = {}
745
+
746
+ const data = Buffer.allocUnsafe(binding.sizeofFSDir)
747
+
748
+ const req = getReq()
749
+
750
+ req.callback = function (err, _) {
463
751
  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)
752
+ cb(null, new Dir(path, data, opts))
467
753
  }
468
754
 
469
- binding.readlink(req.handle, path, req.buffer)
755
+ binding.opendir(req.handle, path, data)
470
756
  }
471
757
 
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')
758
+ function readdir (path, opts, cb) {
759
+ if (typeof path !== 'string') {
760
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
761
+ }
762
+
763
+ if (typeof opts === 'function') {
764
+ cb = opts
765
+ opts = {}
766
+ } else if (typeof cb !== 'function') {
767
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
768
+ }
769
+
475
770
  if (typeof opts === 'string') opts = { encoding: opts }
476
771
  if (!opts) opts = {}
477
772
 
773
+ opendir(path, opts, async (err, dir) => {
774
+ if (err) return cb(err, null)
775
+ const result = []
776
+ dir
777
+ .on('data', (entry) => result.push(entry))
778
+ .on('error', (err) => cb(err, null))
779
+ .on('end', () => cb(null, result))
780
+ })
781
+ }
782
+
783
+ function readFile (path, opts, cb) {
784
+ if (typeof path !== 'string') {
785
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
786
+ }
787
+
788
+ if (typeof opts === 'function') {
789
+ cb = opts
790
+ opts = {}
791
+ } else if (typeof cb !== 'function') {
792
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
793
+ }
794
+
795
+ if (typeof opts === 'string') opts = { encoding: opts }
796
+ else if (!opts) opts = {}
797
+
798
+ const {
799
+ encoding = 'buffer'
800
+ } = opts
801
+
478
802
  open(path, opts.flag || 'r', function (err, fd) {
479
803
  if (err) return cb(err)
480
804
 
481
805
  fstat(fd, function (err, st) {
482
806
  if (err) return closeAndError(err)
483
807
 
484
- let buf = Buffer.allocUnsafe(st.size)
808
+ let buffer = Buffer.allocUnsafe(st.size)
485
809
  let len = 0
486
810
 
487
- read(fd, buf, loop)
811
+ read(fd, buffer, loop)
488
812
 
489
813
  function loop (err, r) {
490
814
  if (err) return closeAndError(err)
491
815
  len += r
492
- if (r === 0 || len === buf.byteLength) return done()
493
- read(fd, buf.subarray(len), loop)
816
+ if (r === 0 || len === buffer.byteLength) return done()
817
+ read(fd, buffer.subarray(len), loop)
494
818
  }
495
819
 
496
820
  function done () {
497
- if (len !== buf.byteLength) buf = buf.subarray(0, len)
821
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
498
822
  close(fd, function (err) {
499
823
  if (err) return cb(err)
500
- if (opts.encoding) return cb(null, buf.toString(opts.encoding))
501
- return cb(null, buf)
824
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
825
+ cb(null, buffer)
502
826
  })
503
827
  }
504
828
  })
@@ -512,26 +836,34 @@ function readFile (path, opts, cb) {
512
836
  }
513
837
 
514
838
  function readFileSync (path, opts) {
839
+ if (typeof path !== 'string') {
840
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
841
+ }
842
+
515
843
  if (typeof opts === 'string') opts = { encoding: opts }
516
- if (!opts) opts = {}
844
+ else if (!opts) opts = {}
845
+
846
+ const {
847
+ encoding = 'buffer'
848
+ } = opts
517
849
 
518
850
  const fd = openSync(path, opts.flag || 'r')
519
851
 
520
852
  try {
521
853
  const st = fstatSync(fd)
522
854
 
523
- let buf = Buffer.allocUnsafe(st.size)
855
+ let buffer = Buffer.allocUnsafe(st.size)
524
856
  let len = 0
525
857
 
526
858
  while (true) {
527
- const r = readSync(fd, len ? buf.subarray(len) : buf)
859
+ const r = readSync(fd, len ? buffer.subarray(len) : buffer)
528
860
  len += r
529
- if (r === 0 || len === buf.byteLength) break
861
+ if (r === 0 || len === buffer.byteLength) break
530
862
  }
531
- if (len !== buf.byteLength) buf = buf.subarray(0, len)
532
863
 
533
- if (opts.encoding) return Buffer.coerce(buf).toString(opts.encoding)
534
- return buf
864
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
865
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
866
+ return buffer
535
867
  } finally {
536
868
  try {
537
869
  closeSync(fd)
@@ -540,16 +872,26 @@ function readFileSync (path, opts) {
540
872
  }
541
873
 
542
874
  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 = {}
875
+ if (typeof path !== 'string') {
876
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
877
+ }
548
878
 
549
- if (opts.encoding || typeof data === 'string') {
550
- data = Buffer.from(data, opts.encoding)
879
+ if (typeof data !== 'string' && !Buffer.isBuffer(data)) {
880
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
551
881
  }
552
882
 
883
+ if (typeof opts === 'function') {
884
+ cb = opts
885
+ opts = {}
886
+ } else if (typeof cb !== 'function') {
887
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
888
+ }
889
+
890
+ if (typeof opts === 'string') opts = { encoding: opts }
891
+ else if (!opts) opts = {}
892
+
893
+ if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
894
+
553
895
  open(path, opts.flag || 'w', opts.mode, function (err, fd) {
554
896
  if (err) return cb(err)
555
897
 
@@ -576,21 +918,28 @@ function writeFile (path, data, opts, cb) {
576
918
  })
577
919
  }
578
920
 
579
- function writeFileSync (path, buf, opts) {
580
- if (typeof opts === 'string') opts = { encoding: opts }
581
- if (!opts) opts = {}
921
+ function writeFileSync (path, data, opts) {
922
+ if (typeof path !== 'string') {
923
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
924
+ }
582
925
 
583
- if (opts.encoding || typeof buf === 'string') {
584
- buf = Buffer.from(buf, opts.encoding)
926
+ if (typeof data !== 'string' && !Buffer.isBuffer(data)) {
927
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
585
928
  }
586
929
 
930
+ if (typeof opts === 'string') opts = { encoding: opts }
931
+ else if (!opts) opts = {}
932
+
933
+ if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
934
+
587
935
  const fd = openSync(path, opts.flag || 'w', opts.mode)
588
936
 
589
937
  try {
590
938
  let len = 0
939
+
591
940
  while (true) {
592
- len += writeSync(fd, len ? buf.subarray(len) : buf)
593
- if (len === buf.byteLength) break
941
+ len += writeSync(fd, len ? data.subarray(len) : data)
942
+ if (len === data.byteLength) break
594
943
  }
595
944
  } finally {
596
945
  try {
@@ -599,6 +948,59 @@ function writeFileSync (path, buf, opts) {
599
948
  }
600
949
  }
601
950
 
951
+ class Stats {
952
+ constructor (props) {
953
+ this.dev = props.dev
954
+ this.mode = props.mode
955
+ this.nlink = props.nlink
956
+ this.uid = props.uid
957
+ this.gid = props.gid
958
+ this.rdev = props.rdev
959
+ this.ino = props.ino
960
+ this.size = props.size
961
+ this.blksize = props.blksize
962
+ this.blocks = props.blocks
963
+ this.flags = props.flags
964
+ this.gen = props.gen
965
+ this.atimeMs = props.atim
966
+ this.mtimeMs = props.mtim
967
+ this.ctimeMs = props.ctim
968
+ this.birthtimeMs = props.birthtim
969
+ this.atime = new Date(props.atim)
970
+ this.mtime = new Date(props.mtim)
971
+ this.ctime = new Date(props.ctim)
972
+ this.birthtime = new Date(props.birthtim)
973
+ }
974
+
975
+ isDirectory () {
976
+ return (this.mode & constants.S_IFMT) === constants.S_IFDIR
977
+ }
978
+
979
+ isFile () {
980
+ return (this.mode & constants.S_IFMT) === constants.S_IFREG
981
+ }
982
+
983
+ isBlockDevice () {
984
+ return (this.mode & constants.S_IFMT) === constants.S_IFBLK
985
+ }
986
+
987
+ isCharacterDevice () {
988
+ return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
989
+ }
990
+
991
+ isFIFO () {
992
+ return (this.mode & constants.S_IFMT) === constants.S_IFIFO
993
+ }
994
+
995
+ isSymbolicLink () {
996
+ return (this.mode & constants.S_IFMT) === constants.S_IFLNK
997
+ }
998
+
999
+ isSocket () {
1000
+ return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
1001
+ }
1002
+ }
1003
+
602
1004
  class FileWriteStream extends Writable {
603
1005
  constructor (path, opts = {}) {
604
1006
  super({ map })
@@ -702,6 +1104,104 @@ class FileReadStream extends Readable {
702
1104
  }
703
1105
  }
704
1106
 
1107
+ class Dir extends Readable {
1108
+ constructor (path, handle, opts = {}) {
1109
+ const {
1110
+ encoding = 'utf8',
1111
+ bufferSize = 32
1112
+ } = opts
1113
+
1114
+ super()
1115
+
1116
+ this._handle = handle
1117
+ this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
1118
+ this._encoding = encoding
1119
+ this._closed = false
1120
+
1121
+ this.path = path
1122
+ }
1123
+
1124
+ _read (cb) {
1125
+ const self = this
1126
+ const data = []
1127
+
1128
+ const req = getReq()
1129
+
1130
+ req.callback = function (err, _) {
1131
+ if (err) return cb(err)
1132
+ if (data.length === 0) self.push(null)
1133
+ else {
1134
+ for (const entry of data) {
1135
+ let name = Buffer.from(entry.name)
1136
+ if (self._encoding !== 'buffer') name = name.toString(self._encoding)
1137
+ self.push(new Dirent(self.path, name, entry.type))
1138
+ }
1139
+ }
1140
+ cb(null)
1141
+ }
1142
+
1143
+ binding.readdir(req.handle, this._handle, this._dirents, data)
1144
+ }
1145
+
1146
+ _destroy (cb) {
1147
+ if (this._closed) cb(null)
1148
+ else this.close(cb)
1149
+ }
1150
+
1151
+ close (cb) {
1152
+ if (this._closed) return
1153
+
1154
+ const self = this
1155
+
1156
+ const req = getReq()
1157
+
1158
+ req.callback = function (err, _) {
1159
+ if (err) return cb(err)
1160
+ self._closed = true
1161
+ cb(null)
1162
+ }
1163
+
1164
+ binding.closedir(req.handle, this._handle)
1165
+ }
1166
+ }
1167
+
1168
+ class Dirent {
1169
+ constructor (path, name, type) {
1170
+ this._type = type
1171
+
1172
+ this.path = path
1173
+ this.name = name
1174
+ }
1175
+
1176
+ isFile () {
1177
+ return this._type === constants.UV_DIRENT_FILE
1178
+ }
1179
+
1180
+ isDirectory () {
1181
+ return this._type === constants.UV_DIRENT_DIR
1182
+ }
1183
+
1184
+ isSymbolicLink () {
1185
+ return this._type === constants.UV_DIRENT_LINK
1186
+ }
1187
+
1188
+ isFIFO () {
1189
+ return this._type === constants.UV_DIRENT_FIFO
1190
+ }
1191
+
1192
+ isSocket () {
1193
+ return this._type === constants.UV_DIRENT_SOCKET
1194
+ }
1195
+
1196
+ isCharacterDevice () {
1197
+ return this._type === constants.UV_DIRENT_CHAR
1198
+ }
1199
+
1200
+ isBlockDevice () {
1201
+ return this._type === constants.UV_DIRENT_BLOCK
1202
+ }
1203
+ }
1204
+
705
1205
  exports.promises = {}
706
1206
 
707
1207
  function typeError (code, message) {
@@ -718,31 +1218,22 @@ exports.read = read
718
1218
  exports.readv = readv
719
1219
  exports.write = write
720
1220
  exports.writev = writev
721
- exports.ftruncate = ftruncate
722
1221
  exports.fstat = fstat
1222
+ exports.ftruncate = ftruncate
723
1223
 
724
1224
  exports.openSync = openSync
725
1225
  exports.closeSync = closeSync
726
1226
  exports.readSync = readSync
727
1227
  exports.writeSync = writeSync
728
- exports.fstatSync = fstatSync
729
1228
  exports.statSync = statSync
730
1229
  exports.lstatSync = lstatSync
1230
+ exports.fstatSync = fstatSync
731
1231
 
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)
1232
+ exports.stat = stat
1233
+ exports.promises.stat = promisify(stat)
743
1234
 
744
- exports.rename = rename
745
- exports.promises.rename = promisify(rename)
1235
+ exports.lstat = lstat
1236
+ exports.promises.lstat = promisify(lstat)
746
1237
 
747
1238
  exports.mkdir = mkdir
748
1239
  exports.promises.mkdir = promisify(mkdir)
@@ -750,22 +1241,41 @@ exports.promises.mkdir = promisify(mkdir)
750
1241
  exports.rmdir = rmdir
751
1242
  exports.promises.rmdir = promisify(rmdir)
752
1243
 
753
- exports.Stats = Stats // for compat
754
-
755
- exports.stat = stat
756
- exports.promises.stat = promisify(stat)
1244
+ exports.unlink = unlink
1245
+ exports.promises.unlink = promisify(unlink)
757
1246
 
758
- exports.lstat = lstat
759
- exports.promises.lstat = promisify(lstat)
1247
+ exports.rename = rename
1248
+ exports.promises.rename = promisify(rename)
760
1249
 
761
1250
  exports.readlink = readlink
762
1251
  exports.promises.readlink = promisify(readlink)
763
1252
 
1253
+ exports.opendir = opendir
1254
+ exports.promises.opendir = promisify(opendir)
1255
+
1256
+ exports.readdir = readdir
1257
+ exports.promises.readdir = promisify(readdir)
1258
+
1259
+ exports.readFile = readFile
1260
+ exports.promises.readFile = promisify(readFile)
1261
+
1262
+ exports.writeFile = writeFile
1263
+ exports.promises.writeFile = promisify(writeFile)
1264
+
1265
+ exports.readFileSync = readFileSync
1266
+ exports.writeFileSync = writeFileSync
1267
+
1268
+ exports.Stats = Stats
1269
+
764
1270
  exports.ReadStream = FileReadStream
765
- exports.createReadStream = (path, options) => new FileReadStream(path, options)
1271
+ exports.createReadStream = function createReadStream (path, opts) {
1272
+ return new FileReadStream(path, opts)
1273
+ }
766
1274
 
767
1275
  exports.WriteStream = FileWriteStream
768
- exports.createWriteStream = (path, options) => new FileWriteStream(path, options)
1276
+ exports.createWriteStream = function createWriteStream (path, opts) {
1277
+ return new FileWriteStream(path, opts)
1278
+ }
769
1279
 
770
1280
  function promisify (fn) {
771
1281
  return function (...args) {
@@ -778,6 +1288,6 @@ function promisify (fn) {
778
1288
  }
779
1289
  }
780
1290
 
781
- function map (s) {
782
- return typeof s === 'string' ? Buffer.from(s) : s
1291
+ function map (data) {
1292
+ return typeof data === 'string' ? Buffer.from(data) : data
783
1293
  }