node.js 14 KB

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