node.js 14 KB

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