source: trip-planner-front/node_modules/memfs/lib/node.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 13.2 KB
Line 
1"use strict";
2var __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})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.File = exports.Link = exports.Node = exports.SEP = void 0;
19var process_1 = require("./process");
20var buffer_1 = require("./internal/buffer");
21var constants_1 = require("./constants");
22var events_1 = require("events");
23var Stats_1 = require("./Stats");
24var 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;
25exports.SEP = '/';
26/**
27 * Node in a file system (like i-node, v-node).
28 */
29var 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));
216exports.Node = Node;
217/**
218 * Represents a hard link that points to an i-node `node`.
219 */
220var 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.steps = parent ? parent.steps.concat([name]) : [name];
234 return _this;
235 }
236 Link.prototype.setNode = function (node) {
237 this.node = node;
238 this.ino = node.ino;
239 };
240 Link.prototype.getNode = function () {
241 return this.node;
242 };
243 Link.prototype.createChild = function (name, node) {
244 if (node === void 0) { node = this.vol.createNode(); }
245 var link = new Link(this.vol, this, name);
246 link.setNode(node);
247 if (node.isDirectory()) {
248 // link.setChild('.', link);
249 // link.getNode().nlink++;
250 // link.setChild('..', this);
251 // this.getNode().nlink++;
252 }
253 this.setChild(name, link);
254 return link;
255 };
256 Link.prototype.setChild = function (name, link) {
257 if (link === void 0) { link = new Link(this.vol, this, name); }
258 this.children[name] = link;
259 link.parent = this;
260 this.length++;
261 this.emit('child:add', link, this);
262 return link;
263 };
264 Link.prototype.deleteChild = function (link) {
265 delete this.children[link.getName()];
266 this.length--;
267 this.emit('child:delete', link, this);
268 };
269 Link.prototype.getChild = function (name) {
270 if (Object.hasOwnProperty.call(this.children, name)) {
271 return this.children[name];
272 }
273 };
274 Link.prototype.getPath = function () {
275 return this.steps.join(exports.SEP);
276 };
277 Link.prototype.getName = function () {
278 return this.steps[this.steps.length - 1];
279 };
280 // del() {
281 // const parent = this.parent;
282 // if(parent) {
283 // parent.deleteChild(link);
284 // }
285 // this.parent = null;
286 // this.vol = null;
287 // }
288 /**
289 * Walk the tree path and return the `Link` at that location, if any.
290 * @param steps {string[]} Desired location.
291 * @param stop {number} Max steps to go into.
292 * @param i {number} Current step in the `steps` array.
293 *
294 * @return {Link|null}
295 */
296 Link.prototype.walk = function (steps, stop, i) {
297 if (stop === void 0) { stop = steps.length; }
298 if (i === void 0) { i = 0; }
299 if (i >= steps.length)
300 return this;
301 if (i >= stop)
302 return this;
303 var step = steps[i];
304 var link = this.getChild(step);
305 if (!link)
306 return null;
307 return link.walk(steps, stop, i + 1);
308 };
309 Link.prototype.toJSON = function () {
310 return {
311 steps: this.steps,
312 ino: this.ino,
313 children: Object.keys(this.children),
314 };
315 };
316 return Link;
317}(events_1.EventEmitter));
318exports.Link = Link;
319/**
320 * Represents an open file (file descriptor) that points to a `Link` (Hard-link) and a `Node`.
321 */
322var File = /** @class */ (function () {
323 /**
324 * Open a Link-Node pair. `node` is provided separately as that might be a different node
325 * rather the one `link` points to, because it might be a symlink.
326 * @param link
327 * @param node
328 * @param flags
329 * @param fd
330 */
331 function File(link, node, flags, fd) {
332 /**
333 * A cursor/offset position in a file, where data will be written on write.
334 * User can "seek" this position.
335 */
336 this.position = 0;
337 this.link = link;
338 this.node = node;
339 this.flags = flags;
340 this.fd = fd;
341 }
342 File.prototype.getString = function (encoding) {
343 if (encoding === void 0) { encoding = 'utf8'; }
344 return this.node.getString();
345 };
346 File.prototype.setString = function (str) {
347 this.node.setString(str);
348 };
349 File.prototype.getBuffer = function () {
350 return this.node.getBuffer();
351 };
352 File.prototype.setBuffer = function (buf) {
353 this.node.setBuffer(buf);
354 };
355 File.prototype.getSize = function () {
356 return this.node.getSize();
357 };
358 File.prototype.truncate = function (len) {
359 this.node.truncate(len);
360 };
361 File.prototype.seekTo = function (position) {
362 this.position = position;
363 };
364 File.prototype.stats = function () {
365 return Stats_1.default.build(this.node);
366 };
367 File.prototype.write = function (buf, offset, length, position) {
368 if (offset === void 0) { offset = 0; }
369 if (length === void 0) { length = buf.length; }
370 if (typeof position !== 'number')
371 position = this.position;
372 if (this.flags & O_APPEND)
373 position = this.getSize();
374 var bytes = this.node.write(buf, offset, length, position);
375 this.position = position + bytes;
376 return bytes;
377 };
378 File.prototype.read = function (buf, offset, length, position) {
379 if (offset === void 0) { offset = 0; }
380 if (length === void 0) { length = buf.byteLength; }
381 if (typeof position !== 'number')
382 position = this.position;
383 var bytes = this.node.read(buf, offset, length, position);
384 this.position = position + bytes;
385 return bytes;
386 };
387 File.prototype.chmod = function (perm) {
388 this.node.chmod(perm);
389 };
390 File.prototype.chown = function (uid, gid) {
391 this.node.chown(uid, gid);
392 };
393 return File;
394}());
395exports.File = File;
Note: See TracBrowser for help on using the repository browser.