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