All files / src Stats.ts

90.69% Statements 39/43
75% Branches 3/4
63.63% Functions 7/11
90.24% Lines 37/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123  46x   46x             46x         6371x 6371x   82784x           6371x 6371x   6371x 6371x 6371x 6371x 6371x   6371x 6371x 6371x 6371x   6371x 6371x 6371x 6371x 6371x   6371x 3x 3x 3x 3x 3x     6371x 6371x 6371x   6371x                                                                 5980x       3924x       2030x                       26x                       46x  
import { Node } from './node';
import { constants } from './constants';
 
const { S_IFMT, S_IFDIR, S_IFREG, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO, S_IFSOCK } = constants;
 
export type TStatNumber = number | bigint;
 
/**
 * Statistics about a file/directory, like `fs.Stats`.
 */
export class Stats<T = TStatNumber> {
  static build(node: Node, bigint: false): Stats<number>;
  static build(node: Node, bigint: true): Stats<bigint>;
  static build(node: Node, bigint?: boolean): Stats<TStatNumber>;
  static build(node: Node, bigint: boolean = false): Stats<TStatNumber> {
    const stats = new Stats<TStatNumber>();
    const { uid, gid, atime, mtime, ctime } = node;
 
    const getStatNumber = !bigint ? number => number : number => BigInt(number);
 
    // Copy all values on Stats from Node, so that if Node values
    // change, values on Stats would still be the old ones,
    // just like in Node fs.
 
    stats.uid = getStatNumber(uid);
    stats.gid = getStatNumber(gid);
 
    stats.rdev = getStatNumber(0);
    stats.blksize = getStatNumber(4096);
    stats.ino = getStatNumber(node.ino);
    stats.size = getStatNumber(node.getSize());
    stats.blocks = getStatNumber(1);
 
    stats.atime = atime;
    stats.mtime = mtime;
    stats.ctime = ctime;
    stats.birthtime = ctime;
 
    stats.atimeMs = getStatNumber(atime.getTime());
    stats.mtimeMs = getStatNumber(mtime.getTime());
    const ctimeMs = getStatNumber(ctime.getTime());
    stats.ctimeMs = ctimeMs;
    stats.birthtimeMs = ctimeMs;
 
    if (bigint) {
      stats.atimeNs = BigInt(atime.getTime()) * BigInt(1000000);
      stats.mtimeNs = BigInt(mtime.getTime()) * BigInt(1000000);
      const ctimeNs = BigInt(ctime.getTime()) * BigInt(1000000);
      stats.ctimeNs = ctimeNs;
      stats.birthtimeNs = ctimeNs;
    }
 
    stats.dev = getStatNumber(0);
    stats.mode = getStatNumber(node.mode);
    stats.nlink = getStatNumber(node.nlink);
 
    return stats;
  }
 
  uid: T;
  gid: T;
 
  rdev: T;
  blksize: T;
  ino: T;
  size: T;
  blocks: T;
 
  atime: Date;
  mtime: Date;
  ctime: Date;
  birthtime: Date;
 
  atimeMs: T;
  mtimeMs: T;
  ctimeMs: T;
  birthtimeMs: T;
 
  // additional properties that exist when bigint is true
  atimeNs: T extends bigint ? T : undefined;
  mtimeNs: T extends bigint ? T : undefined;
  ctimeNs: T extends bigint ? T : undefined;
  birthtimeNs: T extends bigint ? T : undefined;
 
  dev: T;
  mode: T;
  nlink: T;
 
  private _checkModeProperty(property: number): boolean {
    return (Number(this.mode) & S_IFMT) === property;
  }
 
  isDirectory(): boolean {
    return this._checkModeProperty(S_IFDIR);
  }
 
  isFile(): boolean {
    return this._checkModeProperty(S_IFREG);
  }
 
  isBlockDevice(): boolean {
    return this._checkModeProperty(S_IFBLK);
  }
 
  isCharacterDevice(): boolean {
    return this._checkModeProperty(S_IFCHR);
  }
 
  isSymbolicLink(): boolean {
    return this._checkModeProperty(S_IFLNK);
  }
 
  isFIFO(): boolean {
    return this._checkModeProperty(S_IFIFO);
  }
 
  isSocket(): boolean {
    return this._checkModeProperty(S_IFSOCK);
  }
}
 
export default Stats;