Stats.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Stats = void 0;
  4. const constants_1 = require("./constants");
  5. const getBigInt_1 = require("./getBigInt");
  6. const { S_IFMT, S_IFDIR, S_IFREG, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, S_IFSOCK } = constants_1.constants;
  7. /**
  8. * Statistics about a file/directory, like `fs.Stats`.
  9. */
  10. class Stats {
  11. static build(node, bigint = false) {
  12. const stats = new Stats();
  13. const { uid, gid, atime, mtime, ctime } = node;
  14. const getStatNumber = !bigint ? number => number : getBigInt_1.default;
  15. // Copy all values on Stats from Node, so that if Node values
  16. // change, values on Stats would still be the old ones,
  17. // just like in Node fs.
  18. stats.uid = getStatNumber(uid);
  19. stats.gid = getStatNumber(gid);
  20. stats.rdev = getStatNumber(0);
  21. stats.blksize = getStatNumber(4096);
  22. stats.ino = getStatNumber(node.ino);
  23. stats.size = getStatNumber(node.getSize());
  24. stats.blocks = getStatNumber(1);
  25. stats.atime = atime;
  26. stats.mtime = mtime;
  27. stats.ctime = ctime;
  28. stats.birthtime = ctime;
  29. stats.atimeMs = getStatNumber(atime.getTime());
  30. stats.mtimeMs = getStatNumber(mtime.getTime());
  31. const ctimeMs = getStatNumber(ctime.getTime());
  32. stats.ctimeMs = ctimeMs;
  33. stats.birthtimeMs = ctimeMs;
  34. stats.dev = getStatNumber(0);
  35. stats.mode = getStatNumber(node.mode);
  36. stats.nlink = getStatNumber(node.nlink);
  37. return stats;
  38. }
  39. _checkModeProperty(property) {
  40. return (Number(this.mode) & S_IFMT) === property;
  41. }
  42. isDirectory() {
  43. return this._checkModeProperty(S_IFDIR);
  44. }
  45. isFile() {
  46. return this._checkModeProperty(S_IFREG);
  47. }
  48. isBlockDevice() {
  49. return this._checkModeProperty(S_IFBLK);
  50. }
  51. isCharacterDevice() {
  52. return this._checkModeProperty(S_IFCHR);
  53. }
  54. isSymbolicLink() {
  55. return this._checkModeProperty(S_IFLNK);
  56. }
  57. isFIFO() {
  58. return this._checkModeProperty(S_IFIFO);
  59. }
  60. isSocket() {
  61. return this._checkModeProperty(S_IFSOCK);
  62. }
  63. }
  64. exports.Stats = Stats;
  65. exports.default = Stats;