bare-fs 1.11.0 → 1.12.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 +3 -0
  2. package/binding.c +82 -0
  3. package/index.js +140 -103
  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 {
@@ -1558,6 +1487,109 @@ class Dirent {
1558
1487
  }
1559
1488
  }
1560
1489
 
1490
+ class FileWriteStream extends Writable {
1491
+ constructor (path, opts = {}) {
1492
+ super({ map })
1493
+
1494
+ this.path = path
1495
+ this.fd = 0
1496
+ this.flags = opts.flags || 'w'
1497
+ this.mode = opts.mode || 0o666
1498
+ }
1499
+
1500
+ _open (cb) {
1501
+ open(this.path, this.flags, this.mode, (err, fd) => {
1502
+ if (err) return cb(err)
1503
+ this.fd = fd
1504
+ cb(null)
1505
+ })
1506
+ }
1507
+
1508
+ _writev (datas, cb) {
1509
+ writev(this.fd, datas, cb)
1510
+ }
1511
+
1512
+ _destroy (cb) {
1513
+ if (!this.fd) return cb(null)
1514
+ close(this.fd, () => cb(null))
1515
+ }
1516
+ }
1517
+
1518
+ class FileReadStream extends Readable {
1519
+ constructor (path, opts = {}) {
1520
+ super()
1521
+
1522
+ this.path = path
1523
+ this.fd = 0
1524
+
1525
+ this._offset = opts.start || 0
1526
+ this._missing = 0
1527
+
1528
+ if (opts.length) this._missing = opts.length
1529
+ else if (typeof opts.end === 'number') this._missing = opts.end - this._offset + 1
1530
+ else this._missing = -1
1531
+ }
1532
+
1533
+ _open (cb) {
1534
+ open(this.path, constants.O_RDONLY, (err, fd) => {
1535
+ if (err) return cb(err)
1536
+
1537
+ const onerror = (err) => close(fd, () => cb(err))
1538
+
1539
+ fstat(fd, (err, st) => {
1540
+ if (err) return onerror(err)
1541
+ if (!st.isFile()) return onerror(new Error(this.path + ' is not a file'))
1542
+
1543
+ this.fd = fd
1544
+ if (this._missing === -1) this._missing = st.size
1545
+
1546
+ if (st.size < this._offset) {
1547
+ this._offset = st.size
1548
+ this._missing = 0
1549
+ return cb(null)
1550
+ }
1551
+ if (st.size < this._offset + this._missing) {
1552
+ this._missing = st.size - this._offset
1553
+ return cb(null)
1554
+ }
1555
+
1556
+ cb(null)
1557
+ })
1558
+ })
1559
+ }
1560
+
1561
+ _read (cb) {
1562
+ if (!this._missing) {
1563
+ this.push(null)
1564
+ return cb(null)
1565
+ }
1566
+
1567
+ const data = Buffer.allocUnsafe(Math.min(this._missing, 65536))
1568
+
1569
+ read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
1570
+ if (err) return cb(err)
1571
+
1572
+ if (!read) {
1573
+ this.push(null)
1574
+ return cb(null)
1575
+ }
1576
+
1577
+ if (this._missing < read) read = this._missing
1578
+ this.push(data.subarray(0, read))
1579
+ this._missing -= read
1580
+ this._offset += read
1581
+ if (!this._missing) this.push(null)
1582
+
1583
+ cb(null)
1584
+ })
1585
+ }
1586
+
1587
+ _destroy (cb) {
1588
+ if (!this.fd) return cb(null)
1589
+ close(this.fd, () => cb(null))
1590
+ }
1591
+ }
1592
+
1561
1593
  exports.promises = {}
1562
1594
 
1563
1595
  function typeError (code, message) {
@@ -1568,6 +1600,7 @@ function typeError (code, message) {
1568
1600
 
1569
1601
  function noop () {}
1570
1602
 
1603
+ exports.access = access
1571
1604
  exports.chmod = chmod
1572
1605
  exports.close = close
1573
1606
  exports.fchmod = fchmod
@@ -1593,6 +1626,7 @@ exports.write = write
1593
1626
  exports.writeFile = writeFile
1594
1627
  exports.writev = writev
1595
1628
 
1629
+ exports.accessSync = accessSync
1596
1630
  exports.chmodSync = chmodSync
1597
1631
  exports.closeSync = closeSync
1598
1632
  exports.fchmodSync = fchmodSync
@@ -1615,6 +1649,7 @@ exports.unlinkSync = unlinkSync
1615
1649
  exports.writeFileSync = writeFileSync
1616
1650
  exports.writeSync = writeSync
1617
1651
 
1652
+ exports.promises.access = promisify(access)
1618
1653
  exports.promises.chmod = promisify(chmod)
1619
1654
  exports.promises.lstat = promisify(lstat)
1620
1655
  exports.promises.mkdir = promisify(mkdir)
@@ -1632,6 +1667,8 @@ exports.promises.unlink = promisify(unlink)
1632
1667
  exports.promises.writeFile = promisify(writeFile)
1633
1668
 
1634
1669
  exports.Stats = Stats
1670
+ exports.Dir = Dir
1671
+ exports.Dirent = Dirent
1635
1672
 
1636
1673
  exports.ReadStream = FileReadStream
1637
1674
  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.0",
4
4
  "description": "Native file system for Javascript",
5
5
  "main": "index.js",
6
6
  "files": [