@atlaspack/fs 2.15.11-dev-eae8c193d.0 → 2.15.11-typescript-de860656a.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,829 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.MemoryFS = exports.File = exports.FSError = void 0;
7
+ exports.makeShared = makeShared;
8
+ function _path() {
9
+ const data = _interopRequireDefault(require("path"));
10
+ _path = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ function _stream() {
16
+ const data = require("stream");
17
+ _stream = function () {
18
+ return data;
19
+ };
20
+ return data;
21
+ }
22
+ function _buildCache() {
23
+ const data = require("@atlaspack/build-cache");
24
+ _buildCache = function () {
25
+ return data;
26
+ };
27
+ return data;
28
+ }
29
+ function _utils() {
30
+ const data = require("@atlaspack/utils");
31
+ _utils = function () {
32
+ return data;
33
+ };
34
+ return data;
35
+ }
36
+ var _package = _interopRequireDefault(require("../package.json"));
37
+ function _workers() {
38
+ const data = _interopRequireWildcard(require("@atlaspack/workers"));
39
+ _workers = function () {
40
+ return data;
41
+ };
42
+ return data;
43
+ }
44
+ function _nullthrows() {
45
+ const data = _interopRequireDefault(require("nullthrows"));
46
+ _nullthrows = function () {
47
+ return data;
48
+ };
49
+ return data;
50
+ }
51
+ function _events() {
52
+ const data = _interopRequireDefault(require("events"));
53
+ _events = function () {
54
+ return data;
55
+ };
56
+ return data;
57
+ }
58
+ var _find = require("./find");
59
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
60
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
61
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
62
+ const instances = new Map();
63
+ let id = 0;
64
+ class MemoryFS {
65
+ _numWorkerInstances = 0;
66
+ _workerRegisterResolves = [];
67
+ _emitter = new (_events().default)();
68
+ constructor(workerFarm) {
69
+ this.farm = workerFarm;
70
+ this._cwd = _path().default.resolve(_path().default.sep);
71
+ this.dirs = new Map([[this._cwd, new Directory()]]);
72
+ this.files = new Map();
73
+ this.symlinks = new Map();
74
+ this.watchers = new Map();
75
+ this.events = [];
76
+ this.id = id++;
77
+ this._workerHandles = [];
78
+ this._eventQueue = [];
79
+ instances.set(this.id, this);
80
+ this._emitter.on('allWorkersRegistered', () => {
81
+ for (let resolve of this._workerRegisterResolves) {
82
+ resolve();
83
+ }
84
+ this._workerRegisterResolves = [];
85
+ });
86
+ }
87
+ static deserialize(opts) {
88
+ let existing = instances.get(opts.id);
89
+ if (existing != null) {
90
+ // Correct the count of worker instances since serialization assumes a new instance is created
91
+ _workers().default.getWorkerApi().runHandle(opts.handle, ['decrementWorkerInstance', []]);
92
+ return existing;
93
+ }
94
+ let fs = new WorkerFS(opts.id, (0, _nullthrows().default)(opts.handle));
95
+ fs.dirs = opts.dirs;
96
+ fs.files = opts.files;
97
+ fs.symlinks = opts.symlinks;
98
+ return fs;
99
+ }
100
+ serialize() {
101
+ if (!this.handle) {
102
+ this.handle = this.farm.createReverseHandle((fn, args) => {
103
+ // $FlowFixMe
104
+ return this[fn](...args);
105
+ });
106
+ }
107
+
108
+ // If a worker instance already exists, it will decrement this number
109
+ this._numWorkerInstances++;
110
+ return {
111
+ $$raw: false,
112
+ id: this.id,
113
+ handle: this.handle,
114
+ dirs: this.dirs,
115
+ files: this.files,
116
+ symlinks: this.symlinks
117
+ };
118
+ }
119
+ decrementWorkerInstance() {
120
+ this._numWorkerInstances--;
121
+ if (this._numWorkerInstances === this._workerHandles.length) {
122
+ this._emitter.emit('allWorkersRegistered');
123
+ }
124
+ }
125
+ cwd() {
126
+ return this._cwd;
127
+ }
128
+ chdir(dir) {
129
+ this._cwd = dir;
130
+ }
131
+ _normalizePath(filePath, realpath = true) {
132
+ filePath = _path().default.normalize(filePath);
133
+ if (!filePath.startsWith(this.cwd())) {
134
+ filePath = _path().default.resolve(this.cwd(), filePath);
135
+ }
136
+
137
+ // get realpath by following symlinks
138
+ if (realpath) {
139
+ let {
140
+ root,
141
+ dir,
142
+ base
143
+ } = _path().default.parse(filePath);
144
+ let parts = dir.slice(root.length).split(_path().default.sep).concat(base);
145
+ let res = root;
146
+ for (let part of parts) {
147
+ res = _path().default.join(res, part);
148
+ let symlink = this.symlinks.get(res);
149
+ if (symlink) {
150
+ res = symlink;
151
+ }
152
+ }
153
+ return res;
154
+ }
155
+ return filePath;
156
+ }
157
+ async writeFile(filePath, contents, options) {
158
+ filePath = this._normalizePath(filePath);
159
+ if (this.dirs.has(filePath)) {
160
+ throw new FSError('EISDIR', filePath, 'is a directory');
161
+ }
162
+ let dir = _path().default.dirname(filePath);
163
+ if (!this.dirs.has(dir)) {
164
+ throw new FSError('ENOENT', dir, 'does not exist');
165
+ }
166
+ let buffer = makeShared(contents);
167
+ let file = this.files.get(filePath);
168
+ let mode = options && options.mode || 0o666;
169
+ if (file) {
170
+ file.write(buffer, mode);
171
+ this.files.set(filePath, file);
172
+ } else {
173
+ this.files.set(filePath, new File(buffer, mode));
174
+ }
175
+ await this._sendWorkerEvent({
176
+ type: 'writeFile',
177
+ path: filePath,
178
+ entry: this.files.get(filePath)
179
+ });
180
+ this._triggerEvent({
181
+ type: file ? 'update' : 'create',
182
+ path: filePath
183
+ });
184
+ }
185
+
186
+ // eslint-disable-next-line require-await
187
+ async readFile(filePath, encoding) {
188
+ return this.readFileSync(filePath, encoding);
189
+ }
190
+ readFileSync(filePath, encoding) {
191
+ filePath = this._normalizePath(filePath);
192
+ let file = this.files.get(filePath);
193
+ if (file == null) {
194
+ throw new FSError('ENOENT', filePath, 'does not exist');
195
+ }
196
+ let buffer = file.read();
197
+ if (encoding) {
198
+ return buffer.toString(encoding);
199
+ }
200
+ return buffer;
201
+ }
202
+ async copyFile(source, destination) {
203
+ let contents = await this.readFile(source);
204
+ await this.writeFile(destination, contents);
205
+ }
206
+ statSync(filePath) {
207
+ filePath = this._normalizePath(filePath);
208
+ let dir = this.dirs.get(filePath);
209
+ if (dir) {
210
+ return dir.stat();
211
+ }
212
+ let file = this.files.get(filePath);
213
+ if (file == null) {
214
+ throw new FSError('ENOENT', filePath, 'does not exist');
215
+ }
216
+ return file.stat();
217
+ }
218
+
219
+ // eslint-disable-next-line require-await
220
+ async stat(filePath) {
221
+ return this.statSync(filePath);
222
+ }
223
+ readdirSync(dir, opts) {
224
+ dir = this._normalizePath(dir);
225
+ if (!this.dirs.has(dir)) {
226
+ throw new FSError('ENOENT', dir, 'does not exist');
227
+ }
228
+ if (!dir.endsWith(_path().default.sep)) {
229
+ dir += _path().default.sep;
230
+ }
231
+ let res = [];
232
+ for (let [filePath, entry] of this.dirs) {
233
+ if (filePath === dir) {
234
+ continue;
235
+ }
236
+ if (filePath.startsWith(dir) && filePath.indexOf(_path().default.sep, dir.length) === -1) {
237
+ let name = filePath.slice(dir.length);
238
+ if (opts !== null && opts !== void 0 && opts.withFileTypes) {
239
+ res.push(new Dirent(name, entry));
240
+ } else {
241
+ res.push(name);
242
+ }
243
+ }
244
+ }
245
+ for (let [filePath, entry] of this.files) {
246
+ if (filePath.startsWith(dir) && filePath.indexOf(_path().default.sep, dir.length) === -1) {
247
+ let name = filePath.slice(dir.length);
248
+ if (opts !== null && opts !== void 0 && opts.withFileTypes) {
249
+ res.push(new Dirent(name, entry));
250
+ } else {
251
+ res.push(name);
252
+ }
253
+ }
254
+ }
255
+ for (let [from] of this.symlinks) {
256
+ if (from.startsWith(dir) && from.indexOf(_path().default.sep, dir.length) === -1) {
257
+ let name = from.slice(dir.length);
258
+ if (opts !== null && opts !== void 0 && opts.withFileTypes) {
259
+ res.push(new Dirent(name, {
260
+ mode: S_IFLNK
261
+ }));
262
+ } else {
263
+ res.push(name);
264
+ }
265
+ }
266
+ }
267
+ return res;
268
+ }
269
+
270
+ // eslint-disable-next-line require-await
271
+ async readdir(dir, opts) {
272
+ return this.readdirSync(dir, opts);
273
+ }
274
+ async unlink(filePath) {
275
+ filePath = this._normalizePath(filePath);
276
+ if (!this.files.has(filePath) && !this.dirs.has(filePath)) {
277
+ throw new FSError('ENOENT', filePath, 'does not exist');
278
+ }
279
+ this.files.delete(filePath);
280
+ this.dirs.delete(filePath);
281
+ this.watchers.delete(filePath);
282
+ await this._sendWorkerEvent({
283
+ type: 'unlink',
284
+ path: filePath
285
+ });
286
+ this._triggerEvent({
287
+ type: 'delete',
288
+ path: filePath
289
+ });
290
+ return Promise.resolve();
291
+ }
292
+ async mkdirp(dir) {
293
+ dir = this._normalizePath(dir);
294
+ if (this.dirs.has(dir)) {
295
+ return Promise.resolve();
296
+ }
297
+ if (this.files.has(dir)) {
298
+ throw new FSError('ENOENT', dir, 'is not a directory');
299
+ }
300
+ let root = _path().default.parse(dir).root;
301
+ while (dir !== root) {
302
+ if (this.dirs.has(dir)) {
303
+ break;
304
+ }
305
+ this.dirs.set(dir, new Directory());
306
+ await this._sendWorkerEvent({
307
+ type: 'mkdir',
308
+ path: dir
309
+ });
310
+ this._triggerEvent({
311
+ type: 'create',
312
+ path: dir
313
+ });
314
+ dir = _path().default.dirname(dir);
315
+ }
316
+ return Promise.resolve();
317
+ }
318
+ async rimraf(filePath) {
319
+ filePath = this._normalizePath(filePath);
320
+ if (this.dirs.has(filePath)) {
321
+ let dir = filePath + _path().default.sep;
322
+ for (let filePath of this.files.keys()) {
323
+ if (filePath.startsWith(dir)) {
324
+ this.files.delete(filePath);
325
+ await this._sendWorkerEvent({
326
+ type: 'unlink',
327
+ path: filePath
328
+ });
329
+ this._triggerEvent({
330
+ type: 'delete',
331
+ path: filePath
332
+ });
333
+ }
334
+ }
335
+ for (let dirPath of this.dirs.keys()) {
336
+ if (dirPath.startsWith(dir)) {
337
+ this.dirs.delete(dirPath);
338
+ this.watchers.delete(dirPath);
339
+ await this._sendWorkerEvent({
340
+ type: 'unlink',
341
+ path: filePath
342
+ });
343
+ this._triggerEvent({
344
+ type: 'delete',
345
+ path: dirPath
346
+ });
347
+ }
348
+ }
349
+ for (let filePath of this.symlinks.keys()) {
350
+ if (filePath.startsWith(dir)) {
351
+ this.symlinks.delete(filePath);
352
+ await this._sendWorkerEvent({
353
+ type: 'unlink',
354
+ path: filePath
355
+ });
356
+ }
357
+ }
358
+ this.dirs.delete(filePath);
359
+ await this._sendWorkerEvent({
360
+ type: 'unlink',
361
+ path: filePath
362
+ });
363
+ this._triggerEvent({
364
+ type: 'delete',
365
+ path: filePath
366
+ });
367
+ } else if (this.files.has(filePath)) {
368
+ this.files.delete(filePath);
369
+ await this._sendWorkerEvent({
370
+ type: 'unlink',
371
+ path: filePath
372
+ });
373
+ this._triggerEvent({
374
+ type: 'delete',
375
+ path: filePath
376
+ });
377
+ }
378
+ return Promise.resolve();
379
+ }
380
+ async ncp(source, destination) {
381
+ source = this._normalizePath(source);
382
+ if (this.dirs.has(source)) {
383
+ if (!this.dirs.has(destination)) {
384
+ this.dirs.set(destination, new Directory());
385
+ await this._sendWorkerEvent({
386
+ type: 'mkdir',
387
+ path: destination
388
+ });
389
+ this._triggerEvent({
390
+ type: 'create',
391
+ path: destination
392
+ });
393
+ }
394
+ let dir = source + _path().default.sep;
395
+ for (let dirPath of this.dirs.keys()) {
396
+ if (dirPath.startsWith(dir)) {
397
+ let destName = _path().default.join(destination, dirPath.slice(dir.length));
398
+ if (!this.dirs.has(destName)) {
399
+ this.dirs.set(destName, new Directory());
400
+ await this._sendWorkerEvent({
401
+ type: 'mkdir',
402
+ path: destination
403
+ });
404
+ this._triggerEvent({
405
+ type: 'create',
406
+ path: destName
407
+ });
408
+ }
409
+ }
410
+ }
411
+ for (let [filePath, file] of this.files) {
412
+ if (filePath.startsWith(dir)) {
413
+ let destName = _path().default.join(destination, filePath.slice(dir.length));
414
+ let exists = this.files.has(destName);
415
+ this.files.set(destName, file);
416
+ await this._sendWorkerEvent({
417
+ type: 'writeFile',
418
+ path: destName,
419
+ entry: file
420
+ });
421
+ this._triggerEvent({
422
+ type: exists ? 'update' : 'create',
423
+ path: destName
424
+ });
425
+ }
426
+ }
427
+ } else {
428
+ await this.copyFile(source, destination);
429
+ }
430
+ }
431
+ createReadStream(filePath) {
432
+ return new ReadStream(this, filePath);
433
+ }
434
+ createWriteStream(filePath, options) {
435
+ this.mkdirp(_path().default.dirname(filePath));
436
+ return new WriteStream(this, filePath, options);
437
+ }
438
+ realpathSync(filePath) {
439
+ return this._normalizePath(filePath);
440
+ }
441
+
442
+ // eslint-disable-next-line require-await
443
+ async realpath(filePath) {
444
+ return this.realpathSync(filePath);
445
+ }
446
+ async symlink(target, path) {
447
+ target = this._normalizePath(target);
448
+ path = this._normalizePath(path);
449
+ this.symlinks.set(path, target);
450
+ await this._sendWorkerEvent({
451
+ type: 'symlink',
452
+ path,
453
+ target
454
+ });
455
+ }
456
+ existsSync(filePath) {
457
+ filePath = this._normalizePath(filePath);
458
+ return this.files.has(filePath) || this.dirs.has(filePath);
459
+ }
460
+
461
+ // eslint-disable-next-line require-await
462
+ async exists(filePath) {
463
+ return this.existsSync(filePath);
464
+ }
465
+ _triggerEvent(event) {
466
+ this.events.push(event);
467
+ if (this.watchers.size === 0) {
468
+ return;
469
+ }
470
+
471
+ // Batch events
472
+ this._eventQueue.push(event);
473
+ clearTimeout(this._watcherTimer);
474
+ this._watcherTimer = setTimeout(() => {
475
+ let events = this._eventQueue;
476
+ this._eventQueue = [];
477
+ for (let [dir, watchers] of this.watchers) {
478
+ if (!dir.endsWith(_path().default.sep)) {
479
+ dir += _path().default.sep;
480
+ }
481
+ const relevantEvents = events.filter(event => event.path.startsWith(dir));
482
+ if (relevantEvents.length > 0) {
483
+ for (let watcher of watchers) {
484
+ watcher.trigger(relevantEvents);
485
+ }
486
+ }
487
+ }
488
+ }, 50);
489
+ }
490
+ _registerWorker(handle) {
491
+ this._workerHandles.push(handle);
492
+ if (this._numWorkerInstances === this._workerHandles.length) {
493
+ this._emitter.emit('allWorkersRegistered');
494
+ }
495
+ }
496
+ async _sendWorkerEvent(event) {
497
+ // Wait for worker instances to register their handles
498
+ while (this._workerHandles.length < this._numWorkerInstances) {
499
+ await new Promise(resolve => this._workerRegisterResolves.push(resolve));
500
+ }
501
+ await Promise.all(this._workerHandles.map(workerHandle => this.farm.workerApi.runHandle(workerHandle, [event])));
502
+ }
503
+ watch(dir, fn, opts) {
504
+ dir = this._normalizePath(dir);
505
+ let watcher = new Watcher(fn, opts);
506
+ let watchers = this.watchers.get(dir);
507
+ if (!watchers) {
508
+ watchers = new Set();
509
+ this.watchers.set(dir, watchers);
510
+ }
511
+ watchers.add(watcher);
512
+ return Promise.resolve({
513
+ unsubscribe: () => {
514
+ watchers = (0, _nullthrows().default)(watchers);
515
+ watchers.delete(watcher);
516
+ if (watchers.size === 0) {
517
+ this.watchers.delete(dir);
518
+ }
519
+ return Promise.resolve();
520
+ }
521
+ });
522
+ }
523
+ async getEventsSince(dir, snapshot, opts) {
524
+ let contents = await this.readFile(snapshot, 'utf8');
525
+ let len = Number(contents);
526
+ let events = this.events.slice(len);
527
+ let ignore = opts.ignore;
528
+ if (ignore) {
529
+ events = events.filter(event => !ignore.some(i => event.path.startsWith(i + _path().default.sep)));
530
+ }
531
+ return events;
532
+ }
533
+ async writeSnapshot(dir, snapshot) {
534
+ await this.mkdirp(_path().default.dirname(snapshot));
535
+ await this.writeFile(snapshot, '' + this.events.length);
536
+ }
537
+ findAncestorFile(fileNames, fromDir, root) {
538
+ return (0, _find.findAncestorFile)(this, fileNames, fromDir, root);
539
+ }
540
+ findNodeModule(moduleName, fromDir) {
541
+ return (0, _find.findNodeModule)(this, moduleName, fromDir);
542
+ }
543
+ findFirstFile(filePaths) {
544
+ return (0, _find.findFirstFile)(this, filePaths);
545
+ }
546
+ }
547
+ exports.MemoryFS = MemoryFS;
548
+ class Watcher {
549
+ constructor(fn, options) {
550
+ this.fn = fn;
551
+ this.options = options;
552
+ }
553
+ trigger(events) {
554
+ let ignore = this.options.ignore;
555
+ if (ignore) {
556
+ events = events.filter(event => !ignore.some(i => event.path.startsWith(i + _path().default.sep)));
557
+ }
558
+ if (events.length > 0) {
559
+ this.fn(null, events);
560
+ }
561
+ }
562
+ }
563
+ class FSError extends Error {
564
+ constructor(code, path, message) {
565
+ var _Error$captureStackTr;
566
+ super(`${code}: ${path} ${message}`);
567
+ this.name = 'FSError';
568
+ this.code = code;
569
+ this.path = path;
570
+ (_Error$captureStackTr = Error.captureStackTrace) === null || _Error$captureStackTr === void 0 || _Error$captureStackTr.call(Error, this, this.constructor);
571
+ }
572
+ }
573
+ exports.FSError = FSError;
574
+ class ReadStream extends _stream().Readable {
575
+ constructor(fs, filePath) {
576
+ super();
577
+ this.fs = fs;
578
+ this.filePath = filePath;
579
+ this.reading = false;
580
+ this.bytesRead = 0;
581
+ }
582
+ _read() {
583
+ if (this.reading) {
584
+ return;
585
+ }
586
+ this.reading = true;
587
+ this.fs.readFile(this.filePath).then(res => {
588
+ this.bytesRead += res.byteLength;
589
+ this.push(res);
590
+ this.push(null);
591
+ }, err => {
592
+ this.emit('error', err);
593
+ });
594
+ }
595
+ }
596
+ class WriteStream extends _stream().Writable {
597
+ constructor(fs, filePath, options) {
598
+ super({
599
+ emitClose: true,
600
+ autoDestroy: true
601
+ });
602
+ this.fs = fs;
603
+ this.filePath = filePath;
604
+ this.options = options;
605
+ this.buffer = Buffer.alloc(0);
606
+ }
607
+ _write(chunk, encoding, callback) {
608
+ let c = typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk;
609
+ this.buffer = Buffer.concat([this.buffer, c]);
610
+ callback();
611
+ }
612
+ _final(callback) {
613
+ this.fs.writeFile(this.filePath, this.buffer, this.options).then(callback).catch(callback);
614
+ }
615
+ }
616
+ const S_IFREG = 0o100000;
617
+ const S_IFDIR = 0o040000;
618
+ const S_IFLNK = 0o120000;
619
+ const S_IFMT = 0o170000;
620
+ class Entry {
621
+ constructor(mode) {
622
+ this.mode = mode;
623
+ let now = Date.now();
624
+ this.atime = now;
625
+ this.mtime = now;
626
+ this.ctime = now;
627
+ this.birthtime = now;
628
+ }
629
+ access() {
630
+ let now = Date.now();
631
+ this.atime = now;
632
+ this.ctime = now;
633
+ }
634
+ modify(mode) {
635
+ let now = Date.now();
636
+ this.mtime = now;
637
+ this.ctime = now;
638
+ this.mode = mode;
639
+ }
640
+ getSize() {
641
+ return 0;
642
+ }
643
+ stat() {
644
+ return new Stat(this);
645
+ }
646
+ }
647
+ class Stat {
648
+ dev = 0;
649
+ ino = 0;
650
+ nlink = 0;
651
+ uid = 0;
652
+ gid = 0;
653
+ rdev = 0;
654
+ blksize = 0;
655
+ blocks = 0;
656
+ constructor(entry) {
657
+ this.mode = entry.mode;
658
+ this.size = entry.getSize();
659
+ this.atimeMs = entry.atime;
660
+ this.mtimeMs = entry.mtime;
661
+ this.ctimeMs = entry.ctime;
662
+ this.birthtimeMs = entry.birthtime;
663
+ this.atime = new Date(entry.atime);
664
+ this.mtime = new Date(entry.mtime);
665
+ this.ctime = new Date(entry.ctime);
666
+ this.birthtime = new Date(entry.birthtime);
667
+ }
668
+ isFile() {
669
+ return Boolean(this.mode & S_IFREG);
670
+ }
671
+ isDirectory() {
672
+ return Boolean(this.mode & S_IFDIR);
673
+ }
674
+ isBlockDevice() {
675
+ return false;
676
+ }
677
+ isCharacterDevice() {
678
+ return false;
679
+ }
680
+ isSymbolicLink() {
681
+ return false;
682
+ }
683
+ isFIFO() {
684
+ return false;
685
+ }
686
+ isSocket() {
687
+ return false;
688
+ }
689
+ }
690
+ class Dirent {
691
+ #mode;
692
+ constructor(name, entry) {
693
+ this.name = name;
694
+ this.#mode = entry.mode;
695
+ }
696
+ isFile() {
697
+ return (this.#mode & S_IFMT) === S_IFREG;
698
+ }
699
+ isDirectory() {
700
+ return (this.#mode & S_IFMT) === S_IFDIR;
701
+ }
702
+ isBlockDevice() {
703
+ return false;
704
+ }
705
+ isCharacterDevice() {
706
+ return false;
707
+ }
708
+ isSymbolicLink() {
709
+ return (this.#mode & S_IFMT) === S_IFLNK;
710
+ }
711
+ isFIFO() {
712
+ return false;
713
+ }
714
+ isSocket() {
715
+ return false;
716
+ }
717
+ }
718
+ class File extends Entry {
719
+ constructor(buffer, mode) {
720
+ super(S_IFREG | mode);
721
+ this.buffer = buffer;
722
+ }
723
+ read() {
724
+ super.access();
725
+ return Buffer.from(this.buffer);
726
+ }
727
+ write(buffer, mode) {
728
+ super.modify(S_IFREG | mode);
729
+ this.buffer = buffer;
730
+ }
731
+ getSize() {
732
+ return this.buffer.byteLength;
733
+ }
734
+ }
735
+ exports.File = File;
736
+ class Directory extends Entry {
737
+ constructor() {
738
+ super(S_IFDIR);
739
+ }
740
+ }
741
+ function makeShared(contents) {
742
+ if (typeof contents !== 'string' && contents.buffer instanceof _utils().SharedBuffer) {
743
+ return contents;
744
+ }
745
+ let contentsBuffer = contents;
746
+ // $FlowFixMe
747
+ if (process.browser) {
748
+ // For the polyfilled buffer module, it's faster to always convert once so that the subsequent
749
+ // operations are fast (.byteLength and using .set instead of .write)
750
+ contentsBuffer = contentsBuffer instanceof Buffer ? contentsBuffer : Buffer.from(contentsBuffer);
751
+ }
752
+ let length = Buffer.byteLength(contentsBuffer);
753
+ let shared = new (_utils().SharedBuffer)(length);
754
+ let buffer = Buffer.from(shared);
755
+ if (length > 0) {
756
+ if (typeof contentsBuffer === 'string') {
757
+ buffer.write(contentsBuffer);
758
+ } else {
759
+ buffer.set(contentsBuffer);
760
+ }
761
+ }
762
+ return buffer;
763
+ }
764
+ class WorkerFS extends MemoryFS {
765
+ constructor(id, handle) {
766
+ // TODO Make this not a subclass
767
+ // $FlowFixMe
768
+ super();
769
+ this.id = id;
770
+ this.handleFn = (methodName, args) => _workers().default.getWorkerApi().runHandle(handle, [methodName, args]);
771
+ this.handleFn('_registerWorker', [_workers().default.getWorkerApi().createReverseHandle(event => {
772
+ switch (event.type) {
773
+ case 'writeFile':
774
+ this.files.set(event.path, event.entry);
775
+ break;
776
+ case 'unlink':
777
+ this.files.delete(event.path);
778
+ this.dirs.delete(event.path);
779
+ this.symlinks.delete(event.path);
780
+ break;
781
+ case 'mkdir':
782
+ this.dirs.set(event.path, new Directory());
783
+ break;
784
+ case 'symlink':
785
+ this.symlinks.set(event.path, event.target);
786
+ break;
787
+ }
788
+ })]);
789
+ }
790
+ static deserialize(opts) {
791
+ return (0, _nullthrows().default)(instances.get(opts.id));
792
+ }
793
+ serialize() {
794
+ // $FlowFixMe
795
+ return {
796
+ id: this.id
797
+ };
798
+ }
799
+ writeFile(filePath, contents, options) {
800
+ super.writeFile(filePath, contents, options);
801
+ let buffer = makeShared(contents);
802
+ return this.handleFn('writeFile', [filePath, buffer, options]);
803
+ }
804
+ unlink(filePath) {
805
+ super.unlink(filePath);
806
+ return this.handleFn('unlink', [filePath]);
807
+ }
808
+ mkdirp(dir) {
809
+ super.mkdirp(dir);
810
+ return this.handleFn('mkdirp', [dir]);
811
+ }
812
+ rimraf(filePath) {
813
+ super.rimraf(filePath);
814
+ return this.handleFn('rimraf', [filePath]);
815
+ }
816
+ ncp(source, destination) {
817
+ super.ncp(source, destination);
818
+ return this.handleFn('ncp', [source, destination]);
819
+ }
820
+ symlink(target, path) {
821
+ super.symlink(target, path);
822
+ return this.handleFn('symlink', [target, path]);
823
+ }
824
+ }
825
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:MemoryFS`, MemoryFS);
826
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:WorkerFS`, WorkerFS);
827
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:Stat`, Stat);
828
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:File`, File);
829
+ (0, _buildCache().registerSerializableClass)(`${_package.default.version}:Directory`, Directory);