node.js 14 KB

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