bare-fs 1.11.0 → 1.12.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 +3 -0
  2. package/binding.c +82 -0
  3. package/index.js +157 -108
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -13,6 +13,7 @@ const fs = require('bare-fs')
13
13
 
14
14
  // Currently supports:
15
15
 
16
+ fs.access
16
17
  fs.chmod
17
18
  fs.close
18
19
  fs.chmod
@@ -38,6 +39,7 @@ fs.writev
38
39
  fs.readFile
39
40
  fs.writeFile
40
41
 
42
+ fs.promises.access
41
43
  fs.promises.chmod
42
44
  fs.promises.lstat
43
45
  fs.promises.mkdir
@@ -56,6 +58,7 @@ fs.promises.writeFile
56
58
  fs.createReadStream
57
59
  fs.createWriteStream
58
60
 
61
+ fs.accessSync
59
62
  fs.chmodSync
60
63
  fs.closeSync
61
64
  fs.fchmodSync
package/binding.c CHANGED
@@ -490,6 +490,73 @@ bare_fs_close_sync (js_env_t *env, js_callback_info_t *info) {
490
490
  return NULL;
491
491
  }
492
492
 
493
+ static js_value_t *
494
+ bare_fs_access (js_env_t *env, js_callback_info_t *info) {
495
+ int err;
496
+
497
+ size_t argc = 3;
498
+ js_value_t *argv[3];
499
+
500
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
501
+ assert(err == 0);
502
+
503
+ assert(argc == 3);
504
+
505
+ bare_fs_req_t *req;
506
+ err = js_get_typedarray_info(env, argv[0], NULL, (void **) &req, NULL, NULL, NULL);
507
+ assert(err == 0);
508
+
509
+ bare_fs_path_t path;
510
+ err = js_get_value_string_utf8(env, argv[1], path, sizeof(bare_fs_path_t), NULL);
511
+ assert(err == 0);
512
+
513
+ int32_t mode;
514
+ err = js_get_value_int32(env, argv[2], &mode);
515
+ assert(err == 0);
516
+
517
+ uv_loop_t *loop;
518
+ js_get_env_loop(env, &loop);
519
+
520
+ uv_fs_access(loop, (uv_fs_t *) req, (char *) path, mode, on_fs_response);
521
+
522
+ return NULL;
523
+ }
524
+
525
+ static js_value_t *
526
+ bare_fs_access_sync (js_env_t *env, js_callback_info_t *info) {
527
+ int err;
528
+
529
+ size_t argc = 2;
530
+ js_value_t *argv[2];
531
+
532
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
533
+ assert(err == 0);
534
+
535
+ assert(argc == 2);
536
+
537
+ bare_fs_path_t path;
538
+ err = js_get_value_string_utf8(env, argv[0], path, sizeof(bare_fs_path_t), NULL);
539
+ assert(err == 0);
540
+
541
+ int32_t mode;
542
+ err = js_get_value_int32(env, argv[1], &mode);
543
+ assert(err == 0);
544
+
545
+ uv_loop_t *loop;
546
+ js_get_env_loop(env, &loop);
547
+
548
+ uv_fs_t req;
549
+ uv_fs_access(loop, &req, (char *) path, mode, NULL);
550
+
551
+ if (req.result < 0) {
552
+ js_throw_error(env, uv_err_name(req.result), uv_strerror(req.result));
553
+ }
554
+
555
+ uv_fs_req_cleanup(&req);
556
+
557
+ return NULL;
558
+ }
559
+
493
560
  static js_value_t *
494
561
  bare_fs_read (js_env_t *env, js_callback_info_t *info) {
495
562
  int err;
@@ -2047,6 +2114,8 @@ init (js_env_t *env, js_value_t *exports) {
2047
2114
  V("openSync", bare_fs_open_sync)
2048
2115
  V("close", bare_fs_close)
2049
2116
  V("closeSync", bare_fs_close_sync)
2117
+ V("access", bare_fs_access)
2118
+ V("accessSync", bare_fs_access_sync)
2050
2119
  V("read", bare_fs_read)
2051
2120
  V("readSync", bare_fs_read_sync)
2052
2121
  V("readv", bare_fs_readv)
@@ -2102,6 +2171,19 @@ init (js_env_t *env, js_value_t *exports) {
2102
2171
  V(O_TRUNC)
2103
2172
  V(O_APPEND)
2104
2173
 
2174
+ #ifdef F_OK
2175
+ V(F_OK)
2176
+ #endif
2177
+ #ifdef R_OK
2178
+ V(R_OK)
2179
+ #endif
2180
+ #ifdef W_OK
2181
+ V(W_OK)
2182
+ #endif
2183
+ #ifdef X_OK
2184
+ V(X_OK)
2185
+ #endif
2186
+
2105
2187
  V(S_IFMT)
2106
2188
  V(S_IFREG)
2107
2189
  V(S_IFDIR)
package/index.js CHANGED
@@ -12,6 +12,11 @@ const constants = exports.constants = {
12
12
  O_TRUNC: binding.O_TRUNC,
13
13
  O_APPEND: binding.O_APPEND,
14
14
 
15
+ F_OK: binding.F_OK || 0,
16
+ R_OK: binding.R_OK || 0,
17
+ W_OK: binding.W_OK || 0,
18
+ X_OK: binding.X_OK || 0,
19
+
15
20
  S_IFMT: binding.S_IFMT,
16
21
  S_IFREG: binding.S_IFREG,
17
22
  S_IFDIR: binding.S_IFDIR,
@@ -204,6 +209,33 @@ function closeSync (fd) {
204
209
  return binding.closeSync(fd)
205
210
  }
206
211
 
212
+ function access (path, mode, cb) {
213
+ if (typeof path !== 'string') {
214
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
215
+ }
216
+
217
+ if (typeof cb !== 'function') {
218
+ if (typeof mode === 'function') {
219
+ cb = mode
220
+ mode = constants.F_OK
221
+ } else {
222
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
223
+ }
224
+ }
225
+
226
+ const req = getReq()
227
+ req.callback = cb
228
+ binding.access(req.handle, path, mode)
229
+ }
230
+
231
+ function accessSync (path, mode = constants.F_OK) {
232
+ if (typeof path !== 'string') {
233
+ throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof path) + ' (' + path + ')')
234
+ }
235
+
236
+ binding.accessSync(path, mode)
237
+ }
238
+
207
239
  function read (fd, buffer, offset, len, pos, cb) {
208
240
  if (typeof fd !== 'number') {
209
241
  throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
@@ -1300,109 +1332,6 @@ class Stats {
1300
1332
  }
1301
1333
  }
1302
1334
 
1303
- class FileWriteStream extends Writable {
1304
- constructor (path, opts = {}) {
1305
- super({ map })
1306
-
1307
- this.path = path
1308
- this.fd = 0
1309
- this.flags = opts.flags || 'w'
1310
- this.mode = opts.mode || 0o666
1311
- }
1312
-
1313
- _open (cb) {
1314
- open(this.path, this.flags, this.mode, (err, fd) => {
1315
- if (err) return cb(err)
1316
- this.fd = fd
1317
- cb(null)
1318
- })
1319
- }
1320
-
1321
- _writev (datas, cb) {
1322
- writev(this.fd, datas, cb)
1323
- }
1324
-
1325
- _destroy (cb) {
1326
- if (!this.fd) return cb(null)
1327
- close(this.fd, () => cb(null))
1328
- }
1329
- }
1330
-
1331
- class FileReadStream extends Readable {
1332
- constructor (path, opts = {}) {
1333
- super()
1334
-
1335
- this.path = path
1336
- this.fd = 0
1337
-
1338
- this._offset = opts.start || 0
1339
- this._missing = 0
1340
-
1341
- if (opts.length) this._missing = opts.length
1342
- else if (typeof opts.end === 'number') this._missing = opts.end - this._offset + 1
1343
- else this._missing = -1
1344
- }
1345
-
1346
- _open (cb) {
1347
- open(this.path, constants.O_RDONLY, (err, fd) => {
1348
- if (err) return cb(err)
1349
-
1350
- const onerror = (err) => close(fd, () => cb(err))
1351
-
1352
- fstat(fd, (err, st) => {
1353
- if (err) return onerror(err)
1354
- if (!st.isFile()) return onerror(new Error(this.path + ' is not a file'))
1355
-
1356
- this.fd = fd
1357
- if (this._missing === -1) this._missing = st.size
1358
-
1359
- if (st.size < this._offset) {
1360
- this._offset = st.size
1361
- this._missing = 0
1362
- return cb(null)
1363
- }
1364
- if (st.size < this._offset + this._missing) {
1365
- this._missing = st.size - this._offset
1366
- return cb(null)
1367
- }
1368
-
1369
- cb(null)
1370
- })
1371
- })
1372
- }
1373
-
1374
- _read (cb) {
1375
- if (!this._missing) {
1376
- this.push(null)
1377
- return cb(null)
1378
- }
1379
-
1380
- const data = Buffer.allocUnsafe(Math.min(this._missing, 65536))
1381
-
1382
- read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
1383
- if (err) return cb(err)
1384
-
1385
- if (!read) {
1386
- this.push(null)
1387
- return cb(null)
1388
- }
1389
-
1390
- if (this._missing < read) read = this._missing
1391
- this.push(data.subarray(0, read))
1392
- this._missing -= read
1393
- this._offset += read
1394
- if (!this._missing) this.push(null)
1395
-
1396
- cb(null)
1397
- })
1398
- }
1399
-
1400
- _destroy (cb) {
1401
- if (!this.fd) return cb(null)
1402
- close(this.fd, () => cb(null))
1403
- }
1404
- }
1405
-
1406
1335
  class Dir {
1407
1336
  constructor (path, handle, opts = {}) {
1408
1337
  const {
@@ -1493,14 +1422,18 @@ class Dir {
1493
1422
  }
1494
1423
 
1495
1424
  if (this._ended) {
1496
- this.closeSync()
1497
-
1498
1425
  return { done: true }
1499
1426
  }
1500
1427
 
1501
1428
  const entry = this.readSync()
1502
1429
 
1503
- return { done: entry === null, value: entry }
1430
+ if (entry) {
1431
+ return { done: false, value: entry }
1432
+ }
1433
+
1434
+ this.closeSync()
1435
+
1436
+ return { done: true }
1504
1437
  }
1505
1438
  }
1506
1439
  }
@@ -1513,10 +1446,18 @@ class Dir {
1513
1446
  }
1514
1447
 
1515
1448
  if (this._ended) {
1516
- return this.close((err) => err ? reject(err) : resolve({ done: true }))
1449
+ return resolve({ done: true })
1517
1450
  }
1518
1451
 
1519
- this.read((err, entry) => err ? reject(err) : resolve({ done: entry === null, value: entry }))
1452
+ this.read((err, entry) => {
1453
+ if (err) return reject(err)
1454
+
1455
+ if (entry) {
1456
+ return resolve({ done: false, value: entry })
1457
+ }
1458
+
1459
+ this.close((err) => err ? reject(err) : resolve({ done: true }))
1460
+ })
1520
1461
  })
1521
1462
  }
1522
1463
  }
@@ -1558,6 +1499,109 @@ class Dirent {
1558
1499
  }
1559
1500
  }
1560
1501
 
1502
+ class FileWriteStream extends Writable {
1503
+ constructor (path, opts = {}) {
1504
+ super({ map })
1505
+
1506
+ this.path = path
1507
+ this.fd = 0
1508
+ this.flags = opts.flags || 'w'
1509
+ this.mode = opts.mode || 0o666
1510
+ }
1511
+
1512
+ _open (cb) {
1513
+ open(this.path, this.flags, this.mode, (err, fd) => {
1514
+ if (err) return cb(err)
1515
+ this.fd = fd
1516
+ cb(null)
1517
+ })
1518
+ }
1519
+
1520
+ _writev (datas, cb) {
1521
+ writev(this.fd, datas, cb)
1522
+ }
1523
+
1524
+ _destroy (cb) {
1525
+ if (!this.fd) return cb(null)
1526
+ close(this.fd, () => cb(null))
1527
+ }
1528
+ }
1529
+
1530
+ class FileReadStream extends Readable {
1531
+ constructor (path, opts = {}) {
1532
+ super()
1533
+
1534
+ this.path = path
1535
+ this.fd = 0
1536
+
1537
+ this._offset = opts.start || 0
1538
+ this._missing = 0
1539
+
1540
+ if (opts.length) this._missing = opts.length
1541
+ else if (typeof opts.end === 'number') this._missing = opts.end - this._offset + 1
1542
+ else this._missing = -1
1543
+ }
1544
+
1545
+ _open (cb) {
1546
+ open(this.path, constants.O_RDONLY, (err, fd) => {
1547
+ if (err) return cb(err)
1548
+
1549
+ const onerror = (err) => close(fd, () => cb(err))
1550
+
1551
+ fstat(fd, (err, st) => {
1552
+ if (err) return onerror(err)
1553
+ if (!st.isFile()) return onerror(new Error(this.path + ' is not a file'))
1554
+
1555
+ this.fd = fd
1556
+ if (this._missing === -1) this._missing = st.size
1557
+
1558
+ if (st.size < this._offset) {
1559
+ this._offset = st.size
1560
+ this._missing = 0
1561
+ return cb(null)
1562
+ }
1563
+ if (st.size < this._offset + this._missing) {
1564
+ this._missing = st.size - this._offset
1565
+ return cb(null)
1566
+ }
1567
+
1568
+ cb(null)
1569
+ })
1570
+ })
1571
+ }
1572
+
1573
+ _read (cb) {
1574
+ if (!this._missing) {
1575
+ this.push(null)
1576
+ return cb(null)
1577
+ }
1578
+
1579
+ const data = Buffer.allocUnsafe(Math.min(this._missing, 65536))
1580
+
1581
+ read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
1582
+ if (err) return cb(err)
1583
+
1584
+ if (!read) {
1585
+ this.push(null)
1586
+ return cb(null)
1587
+ }
1588
+
1589
+ if (this._missing < read) read = this._missing
1590
+ this.push(data.subarray(0, read))
1591
+ this._missing -= read
1592
+ this._offset += read
1593
+ if (!this._missing) this.push(null)
1594
+
1595
+ cb(null)
1596
+ })
1597
+ }
1598
+
1599
+ _destroy (cb) {
1600
+ if (!this.fd) return cb(null)
1601
+ close(this.fd, () => cb(null))
1602
+ }
1603
+ }
1604
+
1561
1605
  exports.promises = {}
1562
1606
 
1563
1607
  function typeError (code, message) {
@@ -1568,6 +1612,7 @@ function typeError (code, message) {
1568
1612
 
1569
1613
  function noop () {}
1570
1614
 
1615
+ exports.access = access
1571
1616
  exports.chmod = chmod
1572
1617
  exports.close = close
1573
1618
  exports.fchmod = fchmod
@@ -1593,6 +1638,7 @@ exports.write = write
1593
1638
  exports.writeFile = writeFile
1594
1639
  exports.writev = writev
1595
1640
 
1641
+ exports.accessSync = accessSync
1596
1642
  exports.chmodSync = chmodSync
1597
1643
  exports.closeSync = closeSync
1598
1644
  exports.fchmodSync = fchmodSync
@@ -1615,6 +1661,7 @@ exports.unlinkSync = unlinkSync
1615
1661
  exports.writeFileSync = writeFileSync
1616
1662
  exports.writeSync = writeSync
1617
1663
 
1664
+ exports.promises.access = promisify(access)
1618
1665
  exports.promises.chmod = promisify(chmod)
1619
1666
  exports.promises.lstat = promisify(lstat)
1620
1667
  exports.promises.mkdir = promisify(mkdir)
@@ -1632,6 +1679,8 @@ exports.promises.unlink = promisify(unlink)
1632
1679
  exports.promises.writeFile = promisify(writeFile)
1633
1680
 
1634
1681
  exports.Stats = Stats
1682
+ exports.Dir = Dir
1683
+ exports.Dirent = Dirent
1635
1684
 
1636
1685
  exports.ReadStream = FileReadStream
1637
1686
  exports.createReadStream = function createReadStream (path, opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-fs",
3
- "version": "1.11.0",
3
+ "version": "1.12.1",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [