All files / src/core Node.ts

92.91% Statements 118/127
82.25% Branches 51/62
94.87% Functions 37/39
93.27% Lines 111/119

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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 30866x 66x 66x 66x   66x 62064x 62064x         66x         4950x 4950x   4950x 4950x 4950x       4950x         4950x           4950x 4950x 4950x       65992x       6544x       4x 4x       6628x       4x 4x       6628x       1556x 1556x       6533x       56931x 56931x       6557x       57320x       1x 1x       7496x 7496x       14283x       167x 167x         10x 10x       1293x 1293x 1293x       8x 8x       7312x               745x       101859x         40120x       1x       90x 90x       1979x   1979x 1681x 1681x 1681x     1979x   1979x   1979x                   62x 62x 62x 46x 46x     46x 16x   46x 46x 46x       1912x   13x 13x 8x   5x 5x 5x 5x       1912x       147x 147x       3x 3x 3x       4059x 4059x       1435x 1426x     9x 9x         9x 9x         9x       9210x 9176x     34x 34x         34x 34x 8x       26x       46469x 46411x     58x 58x         58x 58x         58x       259x                                      
import process from '../process';
import { Buffer, bufferAllocUnsafe, bufferFrom } from '../internal/buffer';
import { constants, S } from '../constants';
import { EventEmitter } from 'events';
 
const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, S_IFCHR } = constants;
const getuid = (): number => process.getuid?.() ?? 0;
const getgid = (): number => process.getgid?.() ?? 0;
 
/**
 * Node in a file system (like i-node, v-node).
 */
export class Node extends EventEmitter {
  // i-node number.
  ino: number;
 
  // User ID and group ID.
  private _uid: number = getuid();
  private _gid: number = getgid();
 
  private _atime = new Date();
  private _mtime = new Date();
  private _ctime = new Date();
 
  // data: string = '';
  buf: Buffer;
  rdev: number = 0;
 
  mode: number; // S_IFDIR, S_IFREG, etc..
 
  // Number of hard links pointing at this Node.
  private _nlink = 1;
 
  // Path to another node, if this is a symlink.
  symlink: string;
 
  constructor(ino: number, mode: number = 0o666) {
    super();
    this.mode = mode;
    this.ino = ino;
  }
 
  public set ctime(ctime: Date) {
    this._ctime = ctime;
  }
 
  public get ctime(): Date {
    return this._ctime;
  }
 
  public set uid(uid: number) {
    this._uid = uid;
    this.ctime = new Date();
  }
 
  public get uid(): number {
    return this._uid;
  }
 
  public set gid(gid: number) {
    this._gid = gid;
    this.ctime = new Date();
  }
 
  public get gid(): number {
    return this._gid;
  }
 
  public set atime(atime: Date) {
    this._atime = atime;
    this.ctime = new Date();
  }
 
  public get atime(): Date {
    return this._atime;
  }
 
  public set mtime(mtime: Date) {
    this._mtime = mtime;
    this.ctime = new Date();
  }
 
  public get mtime(): Date {
    return this._mtime;
  }
 
  public get perm(): number {
    return this.mode & ~S_IFMT;
  }
 
  public set perm(perm: number) {
    this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
    this.ctime = new Date();
  }
 
  public set nlink(nlink: number) {
    this._nlink = nlink;
    this.ctime = new Date();
  }
 
  public get nlink(): number {
    return this._nlink;
  }
 
  getString(encoding = 'utf8'): string {
    this.atime = new Date();
    return this.getBuffer().toString(encoding);
  }
 
  setString(str: string) {
    // this.setBuffer(bufferFrom(str, 'utf8'));
    this.buf = bufferFrom(str, 'utf8');
    this.touch();
  }
 
  getBuffer(): Buffer {
    this.atime = new Date();
    if (!this.buf) this.setBuffer(bufferAllocUnsafe(0));
    return bufferFrom(this.buf); // Return a copy.
  }
 
  setBuffer(buf: Buffer) {
    this.buf = bufferFrom(buf); // Creates a copy of data.
    this.touch();
  }
 
  getSize(): number {
    return this.buf ? this.buf.length : 0;
  }
 
  setModeProperty(property: number) {
    this.mode = property;
  }
 
  isFile() {
    return (this.mode & S_IFMT) === S_IFREG;
  }
 
  isDirectory() {
    return (this.mode & S_IFMT) === S_IFDIR;
  }
 
  isSymlink() {
    // return !!this.symlink;
    return (this.mode & S_IFMT) === S_IFLNK;
  }
 
  isCharacterDevice() {
    return (this.mode & S_IFMT) === S_IFCHR;
  }
 
  makeSymlink(symlink: string) {
    this.mode = S_IFLNK | 0o666;
    this.symlink = symlink;
  }
 
  write(buf: Buffer, off: number = 0, len: number = buf.length, pos: number = 0): number {
    if (!this.buf) this.buf = bufferAllocUnsafe(0);
 
    if (pos + len > this.buf.length) {
      const newBuf = bufferAllocUnsafe(pos + len);
      this.buf.copy(newBuf, 0, 0, this.buf.length);
      this.buf = newBuf;
    }
 
    buf.copy(this.buf, pos, off, off + len);
 
    this.touch();
 
    return len;
  }
 
  // Returns the number of bytes read.
  read(
    buf: Buffer | ArrayBufferView | DataView,
    off: number = 0,
    len: number = buf.byteLength,
    pos: number = 0,
  ): number {
    this.atime = new Date();
    if (!this.buf) this.buf = bufferAllocUnsafe(0);
    if (pos >= this.buf.length) return 0;
    let actualLen = len;
    Iif (actualLen > buf.byteLength) {
      actualLen = buf.byteLength;
    }
    if (actualLen + pos > this.buf.length) {
      actualLen = this.buf.length - pos;
    }
    const buf2 = buf instanceof Buffer ? buf : Buffer.from(buf.buffer);
    this.buf.copy(buf2, off, pos, pos + actualLen);
    return actualLen;
  }
 
  truncate(len: number = 0) {
    if (!len) this.buf = bufferAllocUnsafe(0);
    else {
      Iif (!this.buf) this.buf = bufferAllocUnsafe(0);
      if (len <= this.buf.length) {
        this.buf = this.buf.slice(0, len);
      } else {
        const buf = bufferAllocUnsafe(len);
        this.buf.copy(buf);
        buf.fill(0, this.buf.length);
        this.buf = buf;
      }
    }
 
    this.touch();
  }
 
  chmod(perm: number) {
    this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
    this.touch();
  }
 
  chown(uid: number, gid: number) {
    this.uid = uid;
    this.gid = gid;
    this.touch();
  }
 
  touch() {
    this.mtime = new Date();
    this.emit('change', this);
  }
 
  canRead(uid: number = getuid(), gid: number = getgid()): boolean {
    if (this.perm & S.IROTH) {
      return true;
    }
 
    if (gid === this.gid) {
      Iif (this.perm & S.IRGRP) {
        return true;
      }
    }
 
    if (uid === this.uid) {
      Iif (this.perm & S.IRUSR) {
        return true;
      }
    }
 
    return false;
  }
 
  canWrite(uid: number = getuid(), gid: number = getgid()): boolean {
    if (this.perm & S.IWOTH) {
      return true;
    }
 
    if (gid === this.gid) {
      Iif (this.perm & S.IWGRP) {
        return true;
      }
    }
 
    if (uid === this.uid) {
      if (this.perm & S.IWUSR) {
        return true;
      }
    }
 
    return false;
  }
 
  canExecute(uid: number = getuid(), gid: number = getgid()): boolean {
    if (this.perm & S.IXOTH) {
      return true;
    }
 
    if (gid === this.gid) {
      Iif (this.perm & S.IXGRP) {
        return true;
      }
    }
 
    if (uid === this.uid) {
      Iif (this.perm & S.IXUSR) {
        return true;
      }
    }
 
    return false;
  }
 
  del() {
    this.emit('delete', this);
  }
 
  toJSON() {
    return {
      ino: this.ino,
      uid: this.uid,
      gid: this.gid,
      atime: this.atime.getTime(),
      mtime: this.mtime.getTime(),
      ctime: this.ctime.getTime(),
      perm: this.perm,
      mode: this.mode,
      nlink: this.nlink,
      symlink: this.symlink,
      data: this.getString(),
    };
  }
}