Node.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Node = void 0;
  4. const process_1 = require("../process");
  5. const buffer_1 = require("../internal/buffer");
  6. const constants_1 = require("../constants");
  7. const events_1 = require("events");
  8. const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, S_IFCHR } = constants_1.constants;
  9. const getuid = () => { var _a, _b; return (_b = (_a = process_1.default.getuid) === null || _a === void 0 ? void 0 : _a.call(process_1.default)) !== null && _b !== void 0 ? _b : 0; };
  10. const getgid = () => { var _a, _b; return (_b = (_a = process_1.default.getgid) === null || _a === void 0 ? void 0 : _a.call(process_1.default)) !== null && _b !== void 0 ? _b : 0; };
  11. /**
  12. * Node in a file system (like i-node, v-node).
  13. */
  14. class Node extends events_1.EventEmitter {
  15. constructor(ino, mode = 0o666) {
  16. super();
  17. // User ID and group ID.
  18. this._uid = getuid();
  19. this._gid = getgid();
  20. this._atime = new Date();
  21. this._mtime = new Date();
  22. this._ctime = new Date();
  23. this.rdev = 0;
  24. // Number of hard links pointing at this Node.
  25. this._nlink = 1;
  26. this.mode = mode;
  27. this.ino = ino;
  28. }
  29. set ctime(ctime) {
  30. this._ctime = ctime;
  31. }
  32. get ctime() {
  33. return this._ctime;
  34. }
  35. set uid(uid) {
  36. this._uid = uid;
  37. this.ctime = new Date();
  38. }
  39. get uid() {
  40. return this._uid;
  41. }
  42. set gid(gid) {
  43. this._gid = gid;
  44. this.ctime = new Date();
  45. }
  46. get gid() {
  47. return this._gid;
  48. }
  49. set atime(atime) {
  50. this._atime = atime;
  51. this.ctime = new Date();
  52. }
  53. get atime() {
  54. return this._atime;
  55. }
  56. set mtime(mtime) {
  57. this._mtime = mtime;
  58. this.ctime = new Date();
  59. }
  60. get mtime() {
  61. return this._mtime;
  62. }
  63. get perm() {
  64. return this.mode & ~S_IFMT;
  65. }
  66. set perm(perm) {
  67. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  68. this.ctime = new Date();
  69. }
  70. set nlink(nlink) {
  71. this._nlink = nlink;
  72. this.ctime = new Date();
  73. }
  74. get nlink() {
  75. return this._nlink;
  76. }
  77. getString(encoding = 'utf8') {
  78. this.atime = new Date();
  79. return this.getBuffer().toString(encoding);
  80. }
  81. setString(str) {
  82. // this.setBuffer(bufferFrom(str, 'utf8'));
  83. this.buf = (0, buffer_1.bufferFrom)(str, 'utf8');
  84. this.touch();
  85. }
  86. getBuffer() {
  87. this.atime = new Date();
  88. if (!this.buf)
  89. this.setBuffer((0, buffer_1.bufferAllocUnsafe)(0));
  90. return (0, buffer_1.bufferFrom)(this.buf); // Return a copy.
  91. }
  92. setBuffer(buf) {
  93. this.buf = (0, buffer_1.bufferFrom)(buf); // Creates a copy of data.
  94. this.touch();
  95. }
  96. getSize() {
  97. return this.buf ? this.buf.length : 0;
  98. }
  99. setModeProperty(property) {
  100. this.mode = property;
  101. }
  102. isFile() {
  103. return (this.mode & S_IFMT) === S_IFREG;
  104. }
  105. isDirectory() {
  106. return (this.mode & S_IFMT) === S_IFDIR;
  107. }
  108. isSymlink() {
  109. // return !!this.symlink;
  110. return (this.mode & S_IFMT) === S_IFLNK;
  111. }
  112. isCharacterDevice() {
  113. return (this.mode & S_IFMT) === S_IFCHR;
  114. }
  115. makeSymlink(symlink) {
  116. this.mode = S_IFLNK | 0o666;
  117. this.symlink = symlink;
  118. }
  119. write(buf, off = 0, len = buf.length, pos = 0) {
  120. if (!this.buf)
  121. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  122. if (pos + len > this.buf.length) {
  123. const newBuf = (0, buffer_1.bufferAllocUnsafe)(pos + len);
  124. this.buf.copy(newBuf, 0, 0, this.buf.length);
  125. this.buf = newBuf;
  126. }
  127. buf.copy(this.buf, pos, off, off + len);
  128. this.touch();
  129. return len;
  130. }
  131. // Returns the number of bytes read.
  132. read(buf, off = 0, len = buf.byteLength, pos = 0) {
  133. this.atime = new Date();
  134. if (!this.buf)
  135. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  136. if (pos >= this.buf.length)
  137. return 0;
  138. let actualLen = len;
  139. if (actualLen > buf.byteLength) {
  140. actualLen = buf.byteLength;
  141. }
  142. if (actualLen + pos > this.buf.length) {
  143. actualLen = this.buf.length - pos;
  144. }
  145. const buf2 = buf instanceof buffer_1.Buffer ? buf : buffer_1.Buffer.from(buf.buffer);
  146. this.buf.copy(buf2, off, pos, pos + actualLen);
  147. return actualLen;
  148. }
  149. truncate(len = 0) {
  150. if (!len)
  151. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  152. else {
  153. if (!this.buf)
  154. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  155. if (len <= this.buf.length) {
  156. this.buf = this.buf.slice(0, len);
  157. }
  158. else {
  159. const buf = (0, buffer_1.bufferAllocUnsafe)(len);
  160. this.buf.copy(buf);
  161. buf.fill(0, this.buf.length);
  162. this.buf = buf;
  163. }
  164. }
  165. this.touch();
  166. }
  167. chmod(perm) {
  168. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  169. this.touch();
  170. }
  171. chown(uid, gid) {
  172. this.uid = uid;
  173. this.gid = gid;
  174. this.touch();
  175. }
  176. touch() {
  177. this.mtime = new Date();
  178. this.emit('change', this);
  179. }
  180. canRead(uid = getuid(), gid = getgid()) {
  181. if (this.perm & 4 /* S.IROTH */) {
  182. return true;
  183. }
  184. if (gid === this.gid) {
  185. if (this.perm & 32 /* S.IRGRP */) {
  186. return true;
  187. }
  188. }
  189. if (uid === this.uid) {
  190. if (this.perm & 256 /* S.IRUSR */) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. canWrite(uid = getuid(), gid = getgid()) {
  197. if (this.perm & 2 /* S.IWOTH */) {
  198. return true;
  199. }
  200. if (gid === this.gid) {
  201. if (this.perm & 16 /* S.IWGRP */) {
  202. return true;
  203. }
  204. }
  205. if (uid === this.uid) {
  206. if (this.perm & 128 /* S.IWUSR */) {
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. canExecute(uid = getuid(), gid = getgid()) {
  213. if (this.perm & 1 /* S.IXOTH */) {
  214. return true;
  215. }
  216. if (gid === this.gid) {
  217. if (this.perm & 8 /* S.IXGRP */) {
  218. return true;
  219. }
  220. }
  221. if (uid === this.uid) {
  222. if (this.perm & 64 /* S.IXUSR */) {
  223. return true;
  224. }
  225. }
  226. return false;
  227. }
  228. del() {
  229. this.emit('delete', this);
  230. }
  231. toJSON() {
  232. return {
  233. ino: this.ino,
  234. uid: this.uid,
  235. gid: this.gid,
  236. atime: this.atime.getTime(),
  237. mtime: this.mtime.getTime(),
  238. ctime: this.ctime.getTime(),
  239. perm: this.perm,
  240. mode: this.mode,
  241. nlink: this.nlink,
  242. symlink: this.symlink,
  243. data: this.getString(),
  244. };
  245. }
  246. }
  247. exports.Node = Node;
  248. //# sourceMappingURL=Node.js.map