bare-fs 1.5.4 → 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 +859 -418
  3. package/index.js +712 -273
  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
+ }
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
+ }
259
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
+ }
379
+
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
+ }
325
398
 
326
- req.buffer = Buffer.allocUnsafe(16 * 8)
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
+ }
345
443
 
346
- req.buffer = Buffer.allocUnsafe(16 * 8)
444
+ if (typeof cb !== 'function') {
445
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
446
+ }
347
447
 
348
- req.callback = function (err, _, buf) {
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
+ }
466
+
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
+ }
511
+
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
+ }
365
515
 
366
- req.buffer = Buffer.allocUnsafe(16 * 8)
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
+ }
367
538
 
368
- req.callback = function (err, _, buf) {
539
+ const req = getReq()
540
+
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,128 +625,179 @@ 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
+ }
656
+
657
+ if (typeof cb !== 'function') {
658
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
659
+ }
436
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
+ }
443
670
 
671
+ if (typeof cb !== 'function') {
672
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
673
+ }
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
 
458
- const req = getReq()
717
+ const data = Buffer.allocUnsafe(binding.sizeofFSPath)
459
718
 
460
- req.buffer = Buffer.allocUnsafe(4097)
719
+ const req = getReq()
461
720
 
462
- req.callback = function (err, _, buf) {
721
+ req.callback = function (err, _) {
463
722
  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)
723
+ let path = data.subarray(0, data.indexOf(0))
724
+ if (encoding !== 'buffer') path = path.toString(encoding)
725
+ cb(null, path)
467
726
  }
468
727
 
469
- binding.readlink(req.handle, path, req.buffer)
728
+ binding.readlink(req.handle, path, data)
470
729
  }
471
730
 
472
731
  function opendir (path, opts, cb) {
473
- if (typeof opts === 'function') return opendir(path, null, opts)
474
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
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
+ }
742
+
475
743
  if (typeof opts === 'string') opts = { encoding: opts }
476
- if (!opts) opts = {}
744
+ else if (!opts) opts = {}
477
745
 
478
- const {
479
- encoding = 'utf8'
480
- } = opts
746
+ const data = Buffer.allocUnsafe(binding.sizeofFSDir)
481
747
 
482
748
  const req = getReq()
483
749
 
484
- req.buffer = Buffer.allocUnsafe(binding.sizeofFSDir)
485
-
486
- req.callback = function (err, _, buf) {
750
+ req.callback = function (err, _) {
487
751
  if (err) return cb(err, null)
488
- cb(null, new Dir(buf, encoding))
752
+ cb(null, new Dir(path, data, opts))
489
753
  }
490
754
 
491
- binding.opendir(req.handle, path, req.buffer)
755
+ binding.opendir(req.handle, path, data)
492
756
  }
493
757
 
494
- function readdir (dir, opts, cb) {
495
- if (typeof opts === 'function') return readdir(dir, null, opts)
496
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
497
- if (typeof opts === 'string') opts = { encoding: opts }
498
- if (!opts) opts = {}
499
-
500
- if (typeof dir === 'string') {
501
- return opendir(dir, opts, (err, dir) => {
502
- if (err) return cb(err, null)
503
- return readdir(dir, opts, cb)
504
- })
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 + ')')
505
761
  }
506
762
 
507
- const {
508
- encoding = 'utf8',
509
- bufferSize = 32
510
- } = opts
511
-
512
- const req = getReq()
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
+ }
513
769
 
514
- req.buffer = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
770
+ if (typeof opts === 'string') opts = { encoding: opts }
771
+ if (!opts) opts = {}
515
772
 
516
- req.callback = function (err, _, buf) {
773
+ opendir(path, opts, async (err, dir) => {
517
774
  if (err) return cb(err, null)
518
- binding.toDirent(dir._handle)
519
- }
520
-
521
- binding.readdir(req.handle, dir._handle, req.buffer)
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
+ })
522
781
  }
523
782
 
524
783
  function readFile (path, opts, cb) {
525
- if (typeof opts === 'function') return readFile(path, null, opts)
526
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
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
+
527
795
  if (typeof opts === 'string') opts = { encoding: opts }
528
- if (!opts) opts = {}
796
+ else if (!opts) opts = {}
797
+
798
+ const {
799
+ encoding = 'buffer'
800
+ } = opts
529
801
 
530
802
  open(path, opts.flag || 'r', function (err, fd) {
531
803
  if (err) return cb(err)
@@ -533,24 +805,24 @@ function readFile (path, opts, cb) {
533
805
  fstat(fd, function (err, st) {
534
806
  if (err) return closeAndError(err)
535
807
 
536
- let buf = Buffer.allocUnsafe(st.size)
808
+ let buffer = Buffer.allocUnsafe(st.size)
537
809
  let len = 0
538
810
 
539
- read(fd, buf, loop)
811
+ read(fd, buffer, loop)
540
812
 
541
813
  function loop (err, r) {
542
814
  if (err) return closeAndError(err)
543
815
  len += r
544
- if (r === 0 || len === buf.byteLength) return done()
545
- read(fd, buf.subarray(len), loop)
816
+ if (r === 0 || len === buffer.byteLength) return done()
817
+ read(fd, buffer.subarray(len), loop)
546
818
  }
547
819
 
548
820
  function done () {
549
- if (len !== buf.byteLength) buf = buf.subarray(0, len)
821
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
550
822
  close(fd, function (err) {
551
823
  if (err) return cb(err)
552
- if (opts.encoding) return cb(null, buf.toString(opts.encoding))
553
- return cb(null, buf)
824
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
825
+ cb(null, buffer)
554
826
  })
555
827
  }
556
828
  })
@@ -564,26 +836,34 @@ function readFile (path, opts, cb) {
564
836
  }
565
837
 
566
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
+
567
843
  if (typeof opts === 'string') opts = { encoding: opts }
568
- if (!opts) opts = {}
844
+ else if (!opts) opts = {}
845
+
846
+ const {
847
+ encoding = 'buffer'
848
+ } = opts
569
849
 
570
850
  const fd = openSync(path, opts.flag || 'r')
571
851
 
572
852
  try {
573
853
  const st = fstatSync(fd)
574
854
 
575
- let buf = Buffer.allocUnsafe(st.size)
855
+ let buffer = Buffer.allocUnsafe(st.size)
576
856
  let len = 0
577
857
 
578
858
  while (true) {
579
- const r = readSync(fd, len ? buf.subarray(len) : buf)
859
+ const r = readSync(fd, len ? buffer.subarray(len) : buffer)
580
860
  len += r
581
- if (r === 0 || len === buf.byteLength) break
861
+ if (r === 0 || len === buffer.byteLength) break
582
862
  }
583
- if (len !== buf.byteLength) buf = buf.subarray(0, len)
584
863
 
585
- if (opts.encoding) return Buffer.coerce(buf).toString(opts.encoding)
586
- return buf
864
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
865
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
866
+ return buffer
587
867
  } finally {
588
868
  try {
589
869
  closeSync(fd)
@@ -592,16 +872,26 @@ function readFileSync (path, opts) {
592
872
  }
593
873
 
594
874
  function writeFile (path, data, opts, cb) {
595
- if (typeof opts === 'function') return writeFile(path, data, null, opts)
596
- if (typeof cb !== 'function') throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function')
597
- if (typeof data !== 'string' && !Buffer.isBuffer(data)) throw typeError('ERR_INVALID_ARG_TYPE', 'The data argument must be of type string or buffer')
598
- if (typeof opts === 'string') opts = { encoding: opts }
599
- if (!opts) opts = {}
875
+ if (typeof path !== 'string') {
876
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
877
+ }
878
+
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 + ')')
881
+ }
600
882
 
601
- if (opts.encoding || typeof data === 'string') {
602
- data = Buffer.from(data, opts.encoding)
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 + ')')
603
888
  }
604
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
+
605
895
  open(path, opts.flag || 'w', opts.mode, function (err, fd) {
606
896
  if (err) return cb(err)
607
897
 
@@ -628,21 +918,28 @@ function writeFile (path, data, opts, cb) {
628
918
  })
629
919
  }
630
920
 
631
- function writeFileSync (path, buf, opts) {
632
- if (typeof opts === 'string') opts = { encoding: opts }
633
- 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
+ }
634
925
 
635
- if (opts.encoding || typeof buf === 'string') {
636
- 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 + ')')
637
928
  }
638
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
+
639
935
  const fd = openSync(path, opts.flag || 'w', opts.mode)
640
936
 
641
937
  try {
642
938
  let len = 0
939
+
643
940
  while (true) {
644
- len += writeSync(fd, len ? buf.subarray(len) : buf)
645
- if (len === buf.byteLength) break
941
+ len += writeSync(fd, len ? data.subarray(len) : data)
942
+ if (len === data.byteLength) break
646
943
  }
647
944
  } finally {
648
945
  try {
@@ -651,6 +948,59 @@ function writeFileSync (path, buf, opts) {
651
948
  }
652
949
  }
653
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
+
654
1004
  class FileWriteStream extends Writable {
655
1005
  constructor (path, opts = {}) {
656
1006
  super({ map })
@@ -755,15 +1105,100 @@ class FileReadStream extends Readable {
755
1105
  }
756
1106
 
757
1107
  class Dir extends Readable {
758
- constructor (handle, encoding) {
1108
+ constructor (path, handle, opts = {}) {
1109
+ const {
1110
+ encoding = 'utf8',
1111
+ bufferSize = 32
1112
+ } = opts
1113
+
759
1114
  super()
760
1115
 
761
1116
  this._handle = handle
1117
+ this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
762
1118
  this._encoding = encoding
1119
+ this._closed = false
1120
+
1121
+ this.path = path
763
1122
  }
764
1123
 
765
1124
  _read (cb) {
766
- cb(null)
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
767
1202
  }
768
1203
  }
769
1204
 
@@ -783,31 +1218,22 @@ exports.read = read
783
1218
  exports.readv = readv
784
1219
  exports.write = write
785
1220
  exports.writev = writev
786
- exports.ftruncate = ftruncate
787
1221
  exports.fstat = fstat
1222
+ exports.ftruncate = ftruncate
788
1223
 
789
1224
  exports.openSync = openSync
790
1225
  exports.closeSync = closeSync
791
1226
  exports.readSync = readSync
792
1227
  exports.writeSync = writeSync
793
- exports.fstatSync = fstatSync
794
1228
  exports.statSync = statSync
795
1229
  exports.lstatSync = lstatSync
1230
+ exports.fstatSync = fstatSync
796
1231
 
797
- exports.readFileSync = readFileSync
798
- exports.writeFileSync = writeFileSync
799
-
800
- exports.unlink = unlink
801
- exports.promises.unlink = promisify(unlink)
802
-
803
- exports.readFile = readFile
804
- exports.promises.readFile = promisify(readFile)
805
-
806
- exports.writeFile = writeFile
807
- exports.promises.writeFile = promisify(writeFile)
1232
+ exports.stat = stat
1233
+ exports.promises.stat = promisify(stat)
808
1234
 
809
- exports.rename = rename
810
- exports.promises.rename = promisify(rename)
1235
+ exports.lstat = lstat
1236
+ exports.promises.lstat = promisify(lstat)
811
1237
 
812
1238
  exports.mkdir = mkdir
813
1239
  exports.promises.mkdir = promisify(mkdir)
@@ -815,13 +1241,11 @@ exports.promises.mkdir = promisify(mkdir)
815
1241
  exports.rmdir = rmdir
816
1242
  exports.promises.rmdir = promisify(rmdir)
817
1243
 
818
- exports.Stats = Stats // for compat
819
-
820
- exports.stat = stat
821
- exports.promises.stat = promisify(stat)
1244
+ exports.unlink = unlink
1245
+ exports.promises.unlink = promisify(unlink)
822
1246
 
823
- exports.lstat = lstat
824
- exports.promises.lstat = promisify(lstat)
1247
+ exports.rename = rename
1248
+ exports.promises.rename = promisify(rename)
825
1249
 
826
1250
  exports.readlink = readlink
827
1251
  exports.promises.readlink = promisify(readlink)
@@ -832,11 +1256,26 @@ exports.promises.opendir = promisify(opendir)
832
1256
  exports.readdir = readdir
833
1257
  exports.promises.readdir = promisify(readdir)
834
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
+
835
1270
  exports.ReadStream = FileReadStream
836
- exports.createReadStream = (path, options) => new FileReadStream(path, options)
1271
+ exports.createReadStream = function createReadStream (path, opts) {
1272
+ return new FileReadStream(path, opts)
1273
+ }
837
1274
 
838
1275
  exports.WriteStream = FileWriteStream
839
- exports.createWriteStream = (path, options) => new FileWriteStream(path, options)
1276
+ exports.createWriteStream = function createWriteStream (path, opts) {
1277
+ return new FileWriteStream(path, opts)
1278
+ }
840
1279
 
841
1280
  function promisify (fn) {
842
1281
  return function (...args) {
@@ -849,6 +1288,6 @@ function promisify (fn) {
849
1288
  }
850
1289
  }
851
1290
 
852
- function map (s) {
853
- return typeof s === 'string' ? Buffer.from(s) : s
1291
+ function map (data) {
1292
+ return typeof data === 'string' ? Buffer.from(data) : data
854
1293
  }