All files / src/core Node.ts

93.65% Statements 118/126
84% Branches 42/50
94.87% Functions 37/39
94.06% Lines 111/118

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 31465x 65x 65x 65x               65x 21799x 21799x         65x 3068x           3068x 3068x   3068x 3068x 3068x       3068x         3068x           3068x 3068x       12062x       760x       4x 4x       858x       4x 4x       858x       15977x       749x       6671x 6671x       773x       18965x       1x 1x       5382x 5382x       6135x       138x 138x         10x 10x       544x 544x 544x       1x 1x       805x               573x       35512x         12905x       1x       90x 90x       1196x   1196x 1170x 1170x 1170x     1196x   1196x   1196x                   63x 63x 63x 47x 47x     47x 16x   47x 47x 47x       1182x   13x 13x 8x   5x 5x 5x 5x       1182x       157x 157x       3x 3x 3x       2549x 2549x       704x 686x     18x 18x         18x 18x 4x       14x       3584x 3548x     36x 36x         36x 36x 7x       29x       14443x 14382x     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;
 
/**
 * 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();
 
  // 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) {
    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.setBuffer(bufferFrom(str, 'utf8'));
    this.buf = bufferFrom(str, 'utf8');
    this.touch();
  }
 
  getBuffer(): Buffer {
    this.atime = new Date();
    if (!this.buf) this.buf = 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, buf.byteOffset, buf.byteLength);
    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.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(),
    };
  }
}