All files / src/core Node.ts

87.26% Statements 137/157
75.47% Branches 40/53
95% Functions 38/40
90.78% Lines 128/141

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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 34266x 66x 66x 66x               66x 21895x 21895x 66x         66x 3095x           3095x 3095x   3095x 3095x 3095x   3095x     3095x     3095x   3095x         3095x           3095x 3095x       12244x       760x       4x 4x       858x       4x 4x       858x       16051x       749x       6841x 6841x       773x       19034x       1x 1x       5394x 5394x       6147x       139x 139x       11x       557x 557x 557x       2x 2x       13x 13x 13x 13x 13x       823x               573x       35625x         12941x       1x       90x 90x       1337x 1337x 1337x 1337x 1337x 1164x 1164x 1164x 1164x 1164x 1164x   1337x 1337x 1337x 1337x 1337x                                   66x 66x 50x 50x 50x 50x 50x 50x 50x       1194x 1179x 1179x 1179x 1179x 1179x   15x   6x               6x 6x   15x       157x 157x       3x 3x 3x       2704x 2704x       708x 690x     18x 18x         18x 18x 4x       14x       3604x 3568x     36x 36x         36x 36x 7x       29x       14488x 14427x     61x 61x         61x 61x         61x       9x                                      
import { FanOut } from 'thingies/lib/fanout';
import process from '../process';
import { Buffer, bufferAllocUnsafe, bufferFrom } from '../vendor/node/internal/buffer';
import { constants, S } from '../constants';
 
export type NodeEventModify = [type: 'modify'];
 
export type NodeEventDelete = [type: 'delete'];
 
export type NodeEvent = NodeEventModify | NodeEventDelete;
 
const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, S_IFCHR } = constants;
const getuid = (): number => process.getuid?.() ?? 0;
const getgid = (): number => process.getgid?.() ?? 0;
const EMPTY_BUFFER = bufferAllocUnsafe(0);
 
/**
 * Node in a file system (like i-node, v-node).
 */
export class Node {
  public readonly changes = new FanOut<NodeEvent>();
 
  // 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();
 
  buf: Buffer = EMPTY_BUFFER;
 
  /** Total allocated memory capacity for this node. */
  private capacity: number = 0;
 
  /** Actually used bytes to store content. */
  private size: number = 0;
 
  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) {
    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;
  }
 
  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: BufferEncoding = 'utf8'): string {
    this.atime = new Date();
    return this.getBuffer().toString(encoding);
  }
 
  setString(str: string) {
    this._setBuf(bufferFrom(str, 'utf8'));
  }
 
  getBuffer(): Buffer {
    this.atime = new Date();
    Iif (!this.buf) this.buf = bufferAllocUnsafe(0);
    return bufferFrom(this.buf.subarray(0, this.size)); // Return a copy of used portion.
  }
 
  setBuffer(buf: Buffer) {
    const copy = bufferFrom(buf); // Creates a copy of data.
    this._setBuf(copy);
  }
 
  private _setBuf(buf: Buffer): void {
    const size = buf.length;
    this.buf = buf;
    this.capacity = size;
    this.size = size;
    this.touch();
  }
 
  getSize(): number {
    return this.size;
  }
 
  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 {
    const bufLength = buf.length;
    Iif (off + len > bufLength) len = bufLength - off;
    Iif (len <= 0) return 0;
    const requiredSize = pos + len;
    if (requiredSize > this.capacity) {
      let newCapacity = Math.max(this.capacity * 2, 64);
      while (newCapacity < requiredSize) newCapacity *= 2;
      const newBuf = bufferAllocUnsafe(newCapacity);
      if (this.size > 0) this.buf.copy(newBuf, 0, 0, this.size);
      this.buf = newBuf;
      this.capacity = newCapacity;
    }
    if (pos > this.size) this.buf.fill(0, this.size, pos);
    buf.copy(this.buf, pos, off, off + len);
    if (requiredSize > this.size) this.size = requiredSize;
    this.touch();
    return len;
  }
 
  /**
   * Read data from the file.
   *
   * @param buf Buffer to read data into.
   * @param off Offset int the `buf` where to start writing data.
   * @param len How many bytes to read. Equals to `buf.byteLength` by default.
   * @param pos Position offset in file where to start reading. Defaults to `0`.
   * @returns 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 (pos >= this.size) return 0;
    let actualLen = len;
    Iif (actualLen > buf.byteLength) actualLen = buf.byteLength;
    if (actualLen + pos > this.size) actualLen = this.size - pos;
    Iif (actualLen <= 0) return 0;
    const buf2 = buf instanceof Buffer ? buf : Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
    this.buf.copy(buf2, off, pos, pos + actualLen);
    return actualLen;
  }
 
  truncate(len: number = 0) {
    if (!len) {
      this.buf = EMPTY_BUFFER;
      this.capacity = 0;
      this.size = 0;
      this.touch();
      return;
    }
    if (len <= this.size) this.size = len;
    else {
      Iif (len > this.capacity) {
        let newCapacity = Math.max(this.capacity * 2, 64);
        while (newCapacity < len) newCapacity *= 2;
        const buf = bufferAllocUnsafe(newCapacity);
        Iif (this.size > 0) this.buf.copy(buf, 0, 0, this.size);
        buf.fill(0, this.size, len);
        this.buf = buf;
        this.capacity = newCapacity;
      } else this.buf.fill(0, this.size, len);
      this.size = len;
    }
    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.changes.emit(['modify']);
  }
 
  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) {
      if (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.changes.emit(['delete']);
  }
 
  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(),
    };
  }
}