UNPKG

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