node.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.File = exports.Link = exports.Node = exports.SEP = void 0;
  19. var process_1 = require("./process");
  20. var buffer_1 = require("./internal/buffer");
  21. var constants_1 = require("./constants");
  22. var events_1 = require("events");
  23. var Stats_1 = require("./Stats");
  24. var S_IFMT = constants_1.constants.S_IFMT, S_IFDIR = constants_1.constants.S_IFDIR, S_IFREG = constants_1.constants.S_IFREG, S_IFLNK = constants_1.constants.S_IFLNK, O_APPEND = constants_1.constants.O_APPEND;
  25. var getuid = function () { 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; };
  26. var getgid = function () { 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; };
  27. exports.SEP = '/';
  28. /**
  29. * Node in a file system (like i-node, v-node).
  30. */
  31. var Node = /** @class */ (function (_super) {
  32. __extends(Node, _super);
  33. function Node(ino, perm) {
  34. if (perm === void 0) { perm = 438; }
  35. var _this = _super.call(this) || this;
  36. // User ID and group ID.
  37. _this._uid = getuid();
  38. _this._gid = getgid();
  39. _this._atime = new Date();
  40. _this._mtime = new Date();
  41. _this._ctime = new Date();
  42. _this._perm = 438; // Permissions `chmod`, `fchmod`
  43. _this.mode = S_IFREG; // S_IFDIR, S_IFREG, etc.. (file by default?)
  44. // Number of hard links pointing at this Node.
  45. _this._nlink = 1;
  46. _this._perm = perm;
  47. _this.mode |= perm;
  48. _this.ino = ino;
  49. return _this;
  50. }
  51. Object.defineProperty(Node.prototype, "ctime", {
  52. get: function () {
  53. return this._ctime;
  54. },
  55. set: function (ctime) {
  56. this._ctime = ctime;
  57. },
  58. enumerable: false,
  59. configurable: true
  60. });
  61. Object.defineProperty(Node.prototype, "uid", {
  62. get: function () {
  63. return this._uid;
  64. },
  65. set: function (uid) {
  66. this._uid = uid;
  67. this.ctime = new Date();
  68. },
  69. enumerable: false,
  70. configurable: true
  71. });
  72. Object.defineProperty(Node.prototype, "gid", {
  73. get: function () {
  74. return this._gid;
  75. },
  76. set: function (gid) {
  77. this._gid = gid;
  78. this.ctime = new Date();
  79. },
  80. enumerable: false,
  81. configurable: true
  82. });
  83. Object.defineProperty(Node.prototype, "atime", {
  84. get: function () {
  85. return this._atime;
  86. },
  87. set: function (atime) {
  88. this._atime = atime;
  89. this.ctime = new Date();
  90. },
  91. enumerable: false,
  92. configurable: true
  93. });
  94. Object.defineProperty(Node.prototype, "mtime", {
  95. get: function () {
  96. return this._mtime;
  97. },
  98. set: function (mtime) {
  99. this._mtime = mtime;
  100. this.ctime = new Date();
  101. },
  102. enumerable: false,
  103. configurable: true
  104. });
  105. Object.defineProperty(Node.prototype, "perm", {
  106. get: function () {
  107. return this._perm;
  108. },
  109. set: function (perm) {
  110. this._perm = perm;
  111. this.ctime = new Date();
  112. },
  113. enumerable: false,
  114. configurable: true
  115. });
  116. Object.defineProperty(Node.prototype, "nlink", {
  117. get: function () {
  118. return this._nlink;
  119. },
  120. set: function (nlink) {
  121. this._nlink = nlink;
  122. this.ctime = new Date();
  123. },
  124. enumerable: false,
  125. configurable: true
  126. });
  127. Node.prototype.getString = function (encoding) {
  128. if (encoding === void 0) { encoding = 'utf8'; }
  129. this.atime = new Date();
  130. return this.getBuffer().toString(encoding);
  131. };
  132. Node.prototype.setString = function (str) {
  133. // this.setBuffer(bufferFrom(str, 'utf8'));
  134. this.buf = (0, buffer_1.bufferFrom)(str, 'utf8');
  135. this.touch();
  136. };
  137. Node.prototype.getBuffer = function () {
  138. this.atime = new Date();
  139. if (!this.buf)
  140. this.setBuffer((0, buffer_1.bufferAllocUnsafe)(0));
  141. return (0, buffer_1.bufferFrom)(this.buf); // Return a copy.
  142. };
  143. Node.prototype.setBuffer = function (buf) {
  144. this.buf = (0, buffer_1.bufferFrom)(buf); // Creates a copy of data.
  145. this.touch();
  146. };
  147. Node.prototype.getSize = function () {
  148. return this.buf ? this.buf.length : 0;
  149. };
  150. Node.prototype.setModeProperty = function (property) {
  151. this.mode = (this.mode & ~S_IFMT) | property;
  152. };
  153. Node.prototype.setIsFile = function () {
  154. this.setModeProperty(S_IFREG);
  155. };
  156. Node.prototype.setIsDirectory = function () {
  157. this.setModeProperty(S_IFDIR);
  158. };
  159. Node.prototype.setIsSymlink = function () {
  160. this.setModeProperty(S_IFLNK);
  161. };
  162. Node.prototype.isFile = function () {
  163. return (this.mode & S_IFMT) === S_IFREG;
  164. };
  165. Node.prototype.isDirectory = function () {
  166. return (this.mode & S_IFMT) === S_IFDIR;
  167. };
  168. Node.prototype.isSymlink = function () {
  169. // return !!this.symlink;
  170. return (this.mode & S_IFMT) === S_IFLNK;
  171. };
  172. Node.prototype.makeSymlink = function (steps) {
  173. this.symlink = steps;
  174. this.setIsSymlink();
  175. };
  176. Node.prototype.write = function (buf, off, len, pos) {
  177. if (off === void 0) { off = 0; }
  178. if (len === void 0) { len = buf.length; }
  179. if (pos === void 0) { pos = 0; }
  180. if (!this.buf)
  181. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  182. if (pos + len > this.buf.length) {
  183. var newBuf = (0, buffer_1.bufferAllocUnsafe)(pos + len);
  184. this.buf.copy(newBuf, 0, 0, this.buf.length);
  185. this.buf = newBuf;
  186. }
  187. buf.copy(this.buf, pos, off, off + len);
  188. this.touch();
  189. return len;
  190. };
  191. // Returns the number of bytes read.
  192. Node.prototype.read = function (buf, off, len, pos) {
  193. if (off === void 0) { off = 0; }
  194. if (len === void 0) { len = buf.byteLength; }
  195. if (pos === void 0) { pos = 0; }
  196. this.atime = new Date();
  197. if (!this.buf)
  198. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  199. var actualLen = len;
  200. if (actualLen > buf.byteLength) {
  201. actualLen = buf.byteLength;
  202. }
  203. if (actualLen + pos > this.buf.length) {
  204. actualLen = this.buf.length - pos;
  205. }
  206. this.buf.copy(buf, off, pos, pos + actualLen);
  207. return actualLen;
  208. };
  209. Node.prototype.truncate = function (len) {
  210. if (len === void 0) { len = 0; }
  211. if (!len)
  212. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  213. else {
  214. if (!this.buf)
  215. this.buf = (0, buffer_1.bufferAllocUnsafe)(0);
  216. if (len <= this.buf.length) {
  217. this.buf = this.buf.slice(0, len);
  218. }
  219. else {
  220. var buf = (0, buffer_1.bufferAllocUnsafe)(len);
  221. this.buf.copy(buf);
  222. buf.fill(0, this.buf.length);
  223. this.buf = buf;
  224. }
  225. }
  226. this.touch();
  227. };
  228. Node.prototype.chmod = function (perm) {
  229. this.perm = perm;
  230. this.mode = (this.mode & ~511) | perm;
  231. this.touch();
  232. };
  233. Node.prototype.chown = function (uid, gid) {
  234. this.uid = uid;
  235. this.gid = gid;
  236. this.touch();
  237. };
  238. Node.prototype.touch = function () {
  239. this.mtime = new Date();
  240. this.emit('change', this);
  241. };
  242. Node.prototype.canRead = function (uid, gid) {
  243. if (uid === void 0) { uid = getuid(); }
  244. if (gid === void 0) { gid = getgid(); }
  245. if (this.perm & 4 /* S.IROTH */) {
  246. return true;
  247. }
  248. if (gid === this.gid) {
  249. if (this.perm & 32 /* S.IRGRP */) {
  250. return true;
  251. }
  252. }
  253. if (uid === this.uid) {
  254. if (this.perm & 256 /* S.IRUSR */) {
  255. return true;
  256. }
  257. }
  258. return false;
  259. };
  260. Node.prototype.canWrite = function (uid, gid) {
  261. if (uid === void 0) { uid = getuid(); }
  262. if (gid === void 0) { gid = getgid(); }
  263. if (this.perm & 2 /* S.IWOTH */) {
  264. return true;
  265. }
  266. if (gid === this.gid) {
  267. if (this.perm & 16 /* S.IWGRP */) {
  268. return true;
  269. }
  270. }
  271. if (uid === this.uid) {
  272. if (this.perm & 128 /* S.IWUSR */) {
  273. return true;
  274. }
  275. }
  276. return false;
  277. };
  278. Node.prototype.del = function () {
  279. this.emit('delete', this);
  280. };
  281. Node.prototype.toJSON = function () {
  282. return {
  283. ino: this.ino,
  284. uid: this.uid,
  285. gid: this.gid,
  286. atime: this.atime.getTime(),
  287. mtime: this.mtime.getTime(),
  288. ctime: this.ctime.getTime(),
  289. perm: this.perm,
  290. mode: this.mode,
  291. nlink: this.nlink,
  292. symlink: this.symlink,
  293. data: this.getString(),
  294. };
  295. };
  296. return Node;
  297. }(events_1.EventEmitter));
  298. exports.Node = Node;
  299. /**
  300. * Represents a hard link that points to an i-node `node`.
  301. */
  302. var Link = /** @class */ (function (_super) {
  303. __extends(Link, _super);
  304. function Link(vol, parent, name) {
  305. var _this = _super.call(this) || this;
  306. _this.children = {};
  307. // Path to this node as Array: ['usr', 'bin', 'node'].
  308. _this._steps = [];
  309. // "i-node" number of the node.
  310. _this.ino = 0;
  311. // Number of children.
  312. _this.length = 0;
  313. _this.vol = vol;
  314. _this.parent = parent;
  315. _this.name = name;
  316. _this.syncSteps();
  317. return _this;
  318. }
  319. Object.defineProperty(Link.prototype, "steps", {
  320. get: function () {
  321. return this._steps;
  322. },
  323. // Recursively sync children steps, e.g. in case of dir rename
  324. set: function (val) {
  325. this._steps = val;
  326. for (var _i = 0, _a = Object.entries(this.children); _i < _a.length; _i++) {
  327. var _b = _a[_i], child = _b[0], link = _b[1];
  328. if (child === '.' || child === '..') {
  329. continue;
  330. }
  331. link === null || link === void 0 ? void 0 : link.syncSteps();
  332. }
  333. },
  334. enumerable: false,
  335. configurable: true
  336. });
  337. Link.prototype.setNode = function (node) {
  338. this.node = node;
  339. this.ino = node.ino;
  340. };
  341. Link.prototype.getNode = function () {
  342. return this.node;
  343. };
  344. Link.prototype.createChild = function (name, node) {
  345. if (node === void 0) { node = this.vol.createNode(); }
  346. var link = new Link(this.vol, this, name);
  347. link.setNode(node);
  348. if (node.isDirectory()) {
  349. link.children['.'] = link;
  350. link.getNode().nlink++;
  351. }
  352. this.setChild(name, link);
  353. return link;
  354. };
  355. Link.prototype.setChild = function (name, link) {
  356. if (link === void 0) { link = new Link(this.vol, this, name); }
  357. this.children[name] = link;
  358. link.parent = this;
  359. this.length++;
  360. var node = link.getNode();
  361. if (node.isDirectory()) {
  362. link.children['..'] = this;
  363. this.getNode().nlink++;
  364. }
  365. this.getNode().mtime = new Date();
  366. this.emit('child:add', link, this);
  367. return link;
  368. };
  369. Link.prototype.deleteChild = function (link) {
  370. var node = link.getNode();
  371. if (node.isDirectory()) {
  372. delete link.children['..'];
  373. this.getNode().nlink--;
  374. }
  375. delete this.children[link.getName()];
  376. this.length--;
  377. this.getNode().mtime = new Date();
  378. this.emit('child:delete', link, this);
  379. };
  380. Link.prototype.getChild = function (name) {
  381. this.getNode().mtime = new Date();
  382. if (Object.hasOwnProperty.call(this.children, name)) {
  383. return this.children[name];
  384. }
  385. };
  386. Link.prototype.getPath = function () {
  387. return this.steps.join(exports.SEP);
  388. };
  389. Link.prototype.getName = function () {
  390. return this.steps[this.steps.length - 1];
  391. };
  392. // del() {
  393. // const parent = this.parent;
  394. // if(parent) {
  395. // parent.deleteChild(link);
  396. // }
  397. // this.parent = null;
  398. // this.vol = null;
  399. // }
  400. /**
  401. * Walk the tree path and return the `Link` at that location, if any.
  402. * @param steps {string[]} Desired location.
  403. * @param stop {number} Max steps to go into.
  404. * @param i {number} Current step in the `steps` array.
  405. *
  406. * @return {Link|null}
  407. */
  408. Link.prototype.walk = function (steps, stop, i) {
  409. if (stop === void 0) { stop = steps.length; }
  410. if (i === void 0) { i = 0; }
  411. if (i >= steps.length)
  412. return this;
  413. if (i >= stop)
  414. return this;
  415. var step = steps[i];
  416. var link = this.getChild(step);
  417. if (!link)
  418. return null;
  419. return link.walk(steps, stop, i + 1);
  420. };
  421. Link.prototype.toJSON = function () {
  422. return {
  423. steps: this.steps,
  424. ino: this.ino,
  425. children: Object.keys(this.children),
  426. };
  427. };
  428. Link.prototype.syncSteps = function () {
  429. this.steps = this.parent ? this.parent.steps.concat([this.name]) : [this.name];
  430. };
  431. return Link;
  432. }(events_1.EventEmitter));
  433. exports.Link = Link;
  434. /**
  435. * Represents an open file (file descriptor) that points to a `Link` (Hard-link) and a `Node`.
  436. */
  437. var File = /** @class */ (function () {
  438. /**
  439. * Open a Link-Node pair. `node` is provided separately as that might be a different node
  440. * rather the one `link` points to, because it might be a symlink.
  441. * @param link
  442. * @param node
  443. * @param flags
  444. * @param fd
  445. */
  446. function File(link, node, flags, fd) {
  447. /**
  448. * A cursor/offset position in a file, where data will be written on write.
  449. * User can "seek" this position.
  450. */
  451. this.position = 0;
  452. this.link = link;
  453. this.node = node;
  454. this.flags = flags;
  455. this.fd = fd;
  456. }
  457. File.prototype.getString = function (encoding) {
  458. if (encoding === void 0) { encoding = 'utf8'; }
  459. return this.node.getString();
  460. };
  461. File.prototype.setString = function (str) {
  462. this.node.setString(str);
  463. };
  464. File.prototype.getBuffer = function () {
  465. return this.node.getBuffer();
  466. };
  467. File.prototype.setBuffer = function (buf) {
  468. this.node.setBuffer(buf);
  469. };
  470. File.prototype.getSize = function () {
  471. return this.node.getSize();
  472. };
  473. File.prototype.truncate = function (len) {
  474. this.node.truncate(len);
  475. };
  476. File.prototype.seekTo = function (position) {
  477. this.position = position;
  478. };
  479. File.prototype.stats = function () {
  480. return Stats_1.default.build(this.node);
  481. };
  482. File.prototype.write = function (buf, offset, length, position) {
  483. if (offset === void 0) { offset = 0; }
  484. if (length === void 0) { length = buf.length; }
  485. if (typeof position !== 'number')
  486. position = this.position;
  487. if (this.flags & O_APPEND)
  488. position = this.getSize();
  489. var bytes = this.node.write(buf, offset, length, position);
  490. this.position = position + bytes;
  491. return bytes;
  492. };
  493. File.prototype.read = function (buf, offset, length, position) {
  494. if (offset === void 0) { offset = 0; }
  495. if (length === void 0) { length = buf.byteLength; }
  496. if (typeof position !== 'number')
  497. position = this.position;
  498. var bytes = this.node.read(buf, offset, length, position);
  499. this.position = position + bytes;
  500. return bytes;
  501. };
  502. File.prototype.chmod = function (perm) {
  503. this.node.chmod(perm);
  504. };
  505. File.prototype.chown = function (uid, gid) {
  506. this.node.chown(uid, gid);
  507. };
  508. return File;
  509. }());
  510. exports.File = File;