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