node.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.File = exports.Link = exports.Node = exports.SEP = 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 Stats_1 = require("./Stats");
  9. const { S_IFMT, S_IFDIR, S_IFREG, S_IFLNK, S_IFCHR, O_APPEND } = constants_1.constants;
  10. 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; };
  11. 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; };
  12. exports.SEP = '/';
  13. /**
  14. * Node in a file system (like i-node, v-node).
  15. */
  16. class Node extends events_1.EventEmitter {
  17. constructor(ino, mode = 0o666) {
  18. super();
  19. // User ID and group ID.
  20. this._uid = getuid();
  21. this._gid = getgid();
  22. this._atime = new Date();
  23. this._mtime = new Date();
  24. this._ctime = new Date();
  25. // Number of hard links pointing at this Node.
  26. this._nlink = 1;
  27. this.mode = mode;
  28. this.ino = ino;
  29. }
  30. set ctime(ctime) {
  31. this._ctime = ctime;
  32. }
  33. get ctime() {
  34. return this._ctime;
  35. }
  36. set uid(uid) {
  37. this._uid = uid;
  38. this.ctime = new Date();
  39. }
  40. get uid() {
  41. return this._uid;
  42. }
  43. set gid(gid) {
  44. this._gid = gid;
  45. this.ctime = new Date();
  46. }
  47. get gid() {
  48. return this._gid;
  49. }
  50. set atime(atime) {
  51. this._atime = atime;
  52. this.ctime = new Date();
  53. }
  54. get atime() {
  55. return this._atime;
  56. }
  57. set mtime(mtime) {
  58. this._mtime = mtime;
  59. this.ctime = new Date();
  60. }
  61. get mtime() {
  62. return this._mtime;
  63. }
  64. get perm() {
  65. return this.mode & ~S_IFMT;
  66. }
  67. set perm(perm) {
  68. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  69. this.ctime = new Date();
  70. }
  71. set nlink(nlink) {
  72. this._nlink = nlink;
  73. this.ctime = new Date();
  74. }
  75. get nlink() {
  76. return this._nlink;
  77. }
  78. getString(encoding = 'utf8') {
  79. this.atime = new Date();
  80. return this.getBuffer().toString(encoding);
  81. }
  82. setString(str) {
  83. // this.setBuffer(bufferFrom(str, 'utf8'));
  84. this.buf = (0, buffer_1.bufferFrom)(str, 'utf8');
  85. this.touch();
  86. }
  87. getBuffer() {
  88. this.atime = new Date();
  89. if (!this.buf)
  90. this.setBuffer((0, buffer_1.bufferAllocUnsafe)(0));
  91. return (0, buffer_1.bufferFrom)(this.buf); // Return a copy.
  92. }
  93. setBuffer(buf) {
  94. this.buf = (0, buffer_1.bufferFrom)(buf); // Creates a copy of data.
  95. this.touch();
  96. }
  97. getSize() {
  98. return this.buf ? this.buf.length : 0;
  99. }
  100. setModeProperty(property) {
  101. this.mode = property;
  102. }
  103. isFile() {
  104. return (this.mode & S_IFMT) === S_IFREG;
  105. }
  106. isDirectory() {
  107. return (this.mode & S_IFMT) === S_IFDIR;
  108. }
  109. isSymlink() {
  110. // return !!this.symlink;
  111. return (this.mode & S_IFMT) === S_IFLNK;
  112. }
  113. isCharacterDevice() {
  114. return (this.mode & S_IFMT) === S_IFCHR;
  115. }
  116. makeSymlink(symlink) {
  117. this.mode = S_IFLNK | 0o666;
  118. this.symlink = symlink;
  119. }
  120. write(buf, off = 0, len = buf.length, pos = 0) {
  121. if (!this.buf)
  122. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  123. if (pos + len > this.buf.length) {
  124. const newBuf = (0, buffer_1.bufferAllocUnsafe)(pos + len);
  125. this.buf.copy(newBuf, 0, 0, this.buf.length);
  126. this.buf = newBuf;
  127. }
  128. buf.copy(this.buf, pos, off, off + len);
  129. this.touch();
  130. return len;
  131. }
  132. // Returns the number of bytes read.
  133. read(buf, off = 0, len = buf.byteLength, pos = 0) {
  134. this.atime = new Date();
  135. if (!this.buf)
  136. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  137. let actualLen = len;
  138. if (actualLen > buf.byteLength) {
  139. actualLen = buf.byteLength;
  140. }
  141. if (actualLen + pos > this.buf.length) {
  142. actualLen = this.buf.length - pos;
  143. }
  144. const buf2 = buf instanceof buffer_1.Buffer ? buf : buffer_1.Buffer.from(buf.buffer);
  145. this.buf.copy(buf2, off, pos, pos + actualLen);
  146. return actualLen;
  147. }
  148. truncate(len = 0) {
  149. if (!len)
  150. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  151. else {
  152. if (!this.buf)
  153. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  154. if (len <= this.buf.length) {
  155. this.buf = this.buf.slice(0, len);
  156. }
  157. else {
  158. const buf = (0, buffer_1.bufferAllocUnsafe)(len);
  159. this.buf.copy(buf);
  160. buf.fill(0, this.buf.length);
  161. this.buf = buf;
  162. }
  163. }
  164. this.touch();
  165. }
  166. chmod(perm) {
  167. this.mode = (this.mode & S_IFMT) | (perm & ~S_IFMT);
  168. this.touch();
  169. }
  170. chown(uid, gid) {
  171. this.uid = uid;
  172. this.gid = gid;
  173. this.touch();
  174. }
  175. touch() {
  176. this.mtime = new Date();
  177. this.emit('change', this);
  178. }
  179. canRead(uid = getuid(), gid = getgid()) {
  180. if (this.perm & 4 /* S.IROTH */) {
  181. return true;
  182. }
  183. if (gid === this.gid) {
  184. if (this.perm & 32 /* S.IRGRP */) {
  185. return true;
  186. }
  187. }
  188. if (uid === this.uid) {
  189. if (this.perm & 256 /* S.IRUSR */) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. canWrite(uid = getuid(), gid = getgid()) {
  196. if (this.perm & 2 /* S.IWOTH */) {
  197. return true;
  198. }
  199. if (gid === this.gid) {
  200. if (this.perm & 16 /* S.IWGRP */) {
  201. return true;
  202. }
  203. }
  204. if (uid === this.uid) {
  205. if (this.perm & 128 /* S.IWUSR */) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. canExecute(uid = getuid(), gid = getgid()) {
  212. if (this.perm & 1 /* S.IXOTH */) {
  213. return true;
  214. }
  215. if (gid === this.gid) {
  216. if (this.perm & 8 /* S.IXGRP */) {
  217. return true;
  218. }
  219. }
  220. if (uid === this.uid) {
  221. if (this.perm & 64 /* S.IXUSR */) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. del() {
  228. this.emit('delete', this);
  229. }
  230. toJSON() {
  231. return {
  232. ino: this.ino,
  233. uid: this.uid,
  234. gid: this.gid,
  235. atime: this.atime.getTime(),
  236. mtime: this.mtime.getTime(),
  237. ctime: this.ctime.getTime(),
  238. perm: this.perm,
  239. mode: this.mode,
  240. nlink: this.nlink,
  241. symlink: this.symlink,
  242. data: this.getString(),
  243. };
  244. }
  245. }
  246. exports.Node = Node;
  247. /**
  248. * Represents a hard link that points to an i-node `node`.
  249. */
  250. class Link extends events_1.EventEmitter {
  251. get steps() {
  252. return this._steps;
  253. }
  254. // Recursively sync children steps, e.g. in case of dir rename
  255. set steps(val) {
  256. this._steps = val;
  257. for (const [child, link] of this.children.entries()) {
  258. if (child === '.' || child === '..') {
  259. continue;
  260. }
  261. link === null || link === void 0 ? void 0 : link.syncSteps();
  262. }
  263. }
  264. constructor(vol, parent, name) {
  265. super();
  266. this.children = new Map();
  267. // Path to this node as Array: ['usr', 'bin', 'node'].
  268. this._steps = [];
  269. // "i-node" number of the node.
  270. this.ino = 0;
  271. // Number of children.
  272. this.length = 0;
  273. this.vol = vol;
  274. this.parent = parent;
  275. this.name = name;
  276. this.syncSteps();
  277. }
  278. setNode(node) {
  279. this.node = node;
  280. this.ino = node.ino;
  281. }
  282. getNode() {
  283. return this.node;
  284. }
  285. createChild(name, node = this.vol.createNode(S_IFREG | 0o666)) {
  286. const link = new Link(this.vol, this, name);
  287. link.setNode(node);
  288. if (node.isDirectory()) {
  289. link.children.set('.', link);
  290. link.getNode().nlink++;
  291. }
  292. this.setChild(name, link);
  293. return link;
  294. }
  295. setChild(name, link = new Link(this.vol, this, name)) {
  296. this.children.set(name, link);
  297. link.parent = this;
  298. this.length++;
  299. const node = link.getNode();
  300. if (node.isDirectory()) {
  301. link.children.set('..', this);
  302. this.getNode().nlink++;
  303. }
  304. this.getNode().mtime = new Date();
  305. this.emit('child:add', link, this);
  306. return link;
  307. }
  308. deleteChild(link) {
  309. const node = link.getNode();
  310. if (node.isDirectory()) {
  311. link.children.delete('..');
  312. this.getNode().nlink--;
  313. }
  314. this.children.delete(link.getName());
  315. this.length--;
  316. this.getNode().mtime = new Date();
  317. this.emit('child:delete', link, this);
  318. }
  319. getChild(name) {
  320. this.getNode().mtime = new Date();
  321. return this.children.get(name);
  322. }
  323. getPath() {
  324. return this.steps.join(exports.SEP);
  325. }
  326. getParentPath() {
  327. return this.steps.slice(0, -1).join(exports.SEP);
  328. }
  329. getName() {
  330. return this.steps[this.steps.length - 1];
  331. }
  332. // del() {
  333. // const parent = this.parent;
  334. // if(parent) {
  335. // parent.deleteChild(link);
  336. // }
  337. // this.parent = null;
  338. // this.vol = null;
  339. // }
  340. toJSON() {
  341. return {
  342. steps: this.steps,
  343. ino: this.ino,
  344. children: Array.from(this.children.keys()),
  345. };
  346. }
  347. syncSteps() {
  348. this.steps = this.parent ? this.parent.steps.concat([this.name]) : [this.name];
  349. }
  350. }
  351. exports.Link = Link;
  352. /**
  353. * Represents an open file (file descriptor) that points to a `Link` (Hard-link) and a `Node`.
  354. */
  355. class File {
  356. /**
  357. * Open a Link-Node pair. `node` is provided separately as that might be a different node
  358. * rather the one `link` points to, because it might be a symlink.
  359. * @param link
  360. * @param node
  361. * @param flags
  362. * @param fd
  363. */
  364. constructor(link, node, flags, fd) {
  365. this.link = link;
  366. this.node = node;
  367. this.flags = flags;
  368. this.fd = fd;
  369. this.position = 0;
  370. if (this.flags & O_APPEND)
  371. this.position = this.getSize();
  372. }
  373. getString(encoding = 'utf8') {
  374. return this.node.getString();
  375. }
  376. setString(str) {
  377. this.node.setString(str);
  378. }
  379. getBuffer() {
  380. return this.node.getBuffer();
  381. }
  382. setBuffer(buf) {
  383. this.node.setBuffer(buf);
  384. }
  385. getSize() {
  386. return this.node.getSize();
  387. }
  388. truncate(len) {
  389. this.node.truncate(len);
  390. }
  391. seekTo(position) {
  392. this.position = position;
  393. }
  394. stats() {
  395. return Stats_1.default.build(this.node);
  396. }
  397. write(buf, offset = 0, length = buf.length, position) {
  398. if (typeof position !== 'number')
  399. position = this.position;
  400. const bytes = this.node.write(buf, offset, length, position);
  401. this.position = position + bytes;
  402. return bytes;
  403. }
  404. read(buf, offset = 0, length = buf.byteLength, position) {
  405. if (typeof position !== 'number')
  406. position = this.position;
  407. const bytes = this.node.read(buf, offset, length, position);
  408. this.position = position + bytes;
  409. return bytes;
  410. }
  411. chmod(perm) {
  412. this.node.chmod(perm);
  413. }
  414. chown(uid, gid) {
  415. this.node.chown(uid, gid);
  416. }
  417. }
  418. exports.File = File;
  419. //# sourceMappingURL=node.js.map