-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathZipFS.test.ts
1028 lines (727 loc) Β· 33 KB
/
ZipFS.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {Filename, PortablePath, constants, ppath, statUtils, xfs} from '@yarnpkg/fslib';
import {makeEmptyArchive, ZipFS} from '@yarnpkg/libzip';
import {S_IFREG} from 'constants';
import fs from 'fs';
const isNotWin32 = process.platform !== `win32`;
const ifNotWin32It = isNotWin32
? it
: it.skip;
afterEach(() => {
jest.useRealTimers();
});
describe(`ZipFS`, () => {
it(`should handle symlink correctly`, () => {
const expectSameStats = (a: fs.Stats, b: fs.Stats) => {
expect(a.ino).toEqual(b.ino);
expect(a.size).toEqual(b.size);
expect(a.mode).toEqual(b.mode);
expect(a.atimeMs).toEqual(b.atimeMs);
expect(a.mtimeMs).toEqual(b.mtimeMs);
expect(a.ctimeMs).toEqual(b.ctimeMs);
expect(a.birthtimeMs).toEqual(b.birthtimeMs);
expect(a.isFile()).toEqual(a.isFile());
expect(a.isDirectory()).toEqual(a.isDirectory());
expect(a.isSymbolicLink()).toEqual(a.isSymbolicLink());
};
const asserts = (zipFs: ZipFS) => {
const dir = zipFs.statSync(`/dir` as PortablePath);
expect(dir.isFile()).toBeFalsy();
expect(dir.isDirectory()).toBeTruthy();
expect(dir.isSymbolicLink()).toBeFalsy();
const file = zipFs.statSync(`/dir/file` as PortablePath);
expect(file.isFile()).toBeTruthy();
expect(file.isDirectory()).toBeFalsy();
expect(file.isSymbolicLink()).toBeFalsy();
expectSameStats(zipFs.lstatSync(`/dir/file` as PortablePath), file);
expectSameStats(zipFs.lstatSync(`/dir` as PortablePath), dir);
expectSameStats(zipFs.statSync(`/linkToFileA` as PortablePath), file);
expectSameStats(zipFs.statSync(`/linkToFileB` as PortablePath), file);
expectSameStats(zipFs.statSync(`/linkToDirA/file` as PortablePath), file);
expectSameStats(zipFs.statSync(`/linkToDirB/file` as PortablePath), file);
expectSameStats(zipFs.statSync(`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/dir/file` as PortablePath), file);
expectSameStats(zipFs.statSync(`/linkToDirA` as PortablePath), dir);
expectSameStats(zipFs.statSync(`/linkToDirB` as PortablePath), dir);
expectSameStats(zipFs.statSync(`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/linkToDirA` as PortablePath), dir);
expectSameStats(zipFs.lstatSync(`/linkToDirA/file` as PortablePath), file);
expectSameStats(zipFs.lstatSync(`/linkToDirB/file` as PortablePath), file);
expectSameStats(zipFs.lstatSync(`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/dir/file` as PortablePath), file);
const linkToDirA = zipFs.lstatSync(`/linkToDirA` as PortablePath);
expect(linkToDirA.isFile()).toBeFalsy();
expect(linkToDirA.isDirectory()).toBeFalsy();
expect(linkToDirA.isSymbolicLink()).toBeTruthy();
const linkToDirB = zipFs.lstatSync(`/linkToDirB` as PortablePath);
expect(linkToDirB.isFile()).toBeFalsy();
expect(linkToDirB.isDirectory()).toBeFalsy();
expect(linkToDirB.isSymbolicLink()).toBeTruthy();
for (const path of [
`/linkToFileA`,
`/linkToFileB`,
`/linkToDirA/file`,
`/linkToDirB/file`,
`/dir/file`,
`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/dir/file`,
])
expect(zipFs.readFileSync(path as PortablePath, `utf8`)).toEqual(`file content`);
for (const path of [
`/linkToDirA`,
`/linkToDirB`,
`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/dir`,
`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/linkToDirA`,
`/linkToCwd/linkToCwd/linkToCwd/linkToCwd/linkToDirB`,
]) {
expect(zipFs.readdirSync(path as PortablePath)).toContain(`file`);
}
};
const tmpfile = ppath.resolve(xfs.mktempSync(), `test.zip`);
const zipFs = new ZipFS(tmpfile, {create: true});
zipFs.mkdirSync(`/dir` as PortablePath);
zipFs.writeFileSync(`/dir/file` as PortablePath, `file content`);
zipFs.symlinkSync(`dir/file` as PortablePath, `linkToFileA` as PortablePath);
zipFs.symlinkSync(`./dir/file` as PortablePath, `linkToFileB` as PortablePath);
zipFs.symlinkSync(`dir` as PortablePath, `linkToDirA` as PortablePath);
zipFs.symlinkSync(`./dir` as PortablePath, `linkToDirB` as PortablePath);
zipFs.symlinkSync(`.` as PortablePath, `linkToCwd` as PortablePath);
// asserts(zipFs);
zipFs.saveAndClose();
const zipFs2 = new ZipFS(tmpfile);
asserts(zipFs2);
zipFs2.discardAndClose();
});
it(`should readSync file contents`, async () => {
const readFileContents = function (zipFs: ZipFS, p: PortablePath, position: number | null) {
const fd = zipFs.openSync(p, `r`);
const buffer = Buffer.alloc(8192);
try {
let size = 0;
let read = 0;
while ((read = zipFs.readSync(fd, buffer, 0, buffer.length, position)) !== 0)
size += read;
return buffer.toString(`utf-8`, 0, size);
} finally {
zipFs.closeSync(fd);
}
};
const readSyncAsserts = (zipFs: ZipFS) => {
const p = `/dir/file` as PortablePath;
expect(readFileContents(zipFs, p, -1)).toEqual(`file content`);
expect(readFileContents(zipFs, p, null)).toEqual(`file content`);
};
const tmpfile = ppath.resolve(xfs.mktempSync(), `test2.zip`);
const zipFs = new ZipFS(tmpfile, {create: true});
await zipFs.mkdirPromise(`/dir` as PortablePath);
zipFs.writeFileSync(`/dir/file` as PortablePath, `file content`);
zipFs.saveAndClose();
const zipFs2 = new ZipFS(tmpfile);
readSyncAsserts(zipFs2);
zipFs2.discardAndClose();
});
it(`defaults the readSync read length to the buffer size`, async () => {
const p = `/dir/file` as PortablePath;
const zipFs = new ZipFS();
await zipFs.mkdirPromise(`/dir` as PortablePath);
zipFs.writeFileSync(p, `file content`);
const buffer = Buffer.alloc(8192);
const fd = zipFs.openSync(p, `r`);
try {
zipFs.readSync(fd, buffer);
expect(buffer.slice(0, buffer.indexOf(`\0`)).toString()).toEqual(`file content`);
} finally {
zipFs.closeSync(fd);
}
zipFs.discardAndClose();
});
it(`can create a zip file in memory`, () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, `Test`);
const zipContent = zipFs.getBufferAndClose();
const zipFs2 = new ZipFS(zipContent);
expect(zipFs2.readFileSync(`/foo.txt` as PortablePath, `utf8`)).toEqual(`Test`);
});
it(`can handle nested symlinks`, () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, `Test`);
zipFs.symlinkSync(`/foo.txt` as PortablePath, `/linkA` as PortablePath);
zipFs.symlinkSync(`/linkA` as PortablePath, `/linkB` as PortablePath);
const zipFs2 = new ZipFS(zipFs.getBufferAndClose());
expect(zipFs2.readFileSync(`/linkA` as PortablePath, `utf8`)).toEqual(`Test`);
expect(zipFs2.readFileSync(`/linkB` as PortablePath, `utf8`)).toEqual(`Test`);
zipFs2.discardAndClose();
});
it(`returns the same content for sync and async reads`, async () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, `Test`);
const zipFs2 = new ZipFS(zipFs.getBufferAndClose());
expect(await zipFs2.readFilePromise(`/foo.txt` as PortablePath, `utf8`)).toEqual(`Test`);
expect(zipFs2.readFileSync(`/foo.txt` as PortablePath, `utf8`)).toEqual(`Test`);
});
it(`should support unlinking files`, () => {
const zipFs = new ZipFS();
const dir = `/foo` as PortablePath;
zipFs.mkdirSync(dir);
const file = `/foo/bar.txt` as PortablePath;
zipFs.writeFileSync(file, `Test`);
expect(zipFs.existsSync(dir)).toBeTruthy();
expect(zipFs.existsSync(file)).toBeTruthy();
zipFs.unlinkSync(file);
expect(zipFs.existsSync(dir)).toBeTruthy();
expect(zipFs.existsSync(file)).toBeFalsy();
zipFs.discardAndClose();
});
it(`should support removing empty directories`, () => {
const zipFs = new ZipFS();
const dir = `/foo` as PortablePath;
const subdir = `/foo/bar` as PortablePath;
zipFs.mkdirpSync(subdir);
expect(zipFs.existsSync(dir)).toBeTruthy();
expect(zipFs.existsSync(subdir)).toBeTruthy();
zipFs.rmdirSync(subdir);
expect(zipFs.existsSync(dir)).toBeTruthy();
expect(zipFs.existsSync(subdir)).toBeFalsy();
zipFs.discardAndClose();
});
it(`should not support removing non-empty directories`, () => {
const zipFs = new ZipFS();
const dir = `/foo` as PortablePath;
zipFs.mkdirSync(dir);
const file = `/foo/bar.txt` as PortablePath;
zipFs.writeFileSync(file, `Test`);
expect(() => zipFs.rmdirSync(dir)).toThrowError(`ENOTEMPTY`);
zipFs.discardAndClose();
});
it(`should support removing non-empty directories via zipFs.removeSync`, () => {
const zipFs = new ZipFS();
const dir = `/foo` as PortablePath;
const subdir = `/foo/bar` as PortablePath;
zipFs.mkdirpSync(subdir);
const file = `/foo/bar/baz.txt` as PortablePath;
zipFs.writeFileSync(file, `Test`);
expect(zipFs.existsSync(dir)).toBeTruthy();
expect(zipFs.existsSync(subdir)).toBeTruthy();
expect(zipFs.existsSync(file)).toBeTruthy();
zipFs.removeSync(subdir);
expect(zipFs.existsSync(dir)).toBeTruthy();
expect(zipFs.existsSync(subdir)).toBeFalsy();
expect(zipFs.existsSync(file)).toBeFalsy();
});
it(`should support read after write`, () => {
const zipFs = new ZipFS();
const file = `/foo.txt` as PortablePath;
zipFs.writeFileSync(file, `Test`);
expect(zipFs.readFileSync(file, `utf8`)).toStrictEqual(`Test`);
zipFs.discardAndClose();
});
it(`should support write after read`, () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
const zipFs = new ZipFS(archive, {create: true});
const file = `/foo.txt` as PortablePath;
zipFs.writeFileSync(file, `Hello World`);
zipFs.saveAndClose();
const zipFs2 = new ZipFS(archive);
expect(zipFs2.readFileSync(file, `utf8`)).toStrictEqual(`Hello World`);
expect(() => zipFs2.writeFileSync(file, `Goodbye World`)).not.toThrow();
zipFs2.discardAndClose();
});
it(`should support write after write`, () => {
const zipFs = new ZipFS();
const file = `/foo.txt` as PortablePath;
zipFs.writeFileSync(file, `Hello World`);
expect(() => zipFs.writeFileSync(file, `Goodbye World`)).not.toThrow();
zipFs.discardAndClose();
});
it(`should support read after read`, () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
const zipFs = new ZipFS(archive, {create: true});
const file = `/foo.txt` as PortablePath;
zipFs.writeFileSync(file, `Hello World`);
zipFs.saveAndClose();
const zipFs2 = new ZipFS(archive);
expect(zipFs2.readFileSync(file, `utf8`)).toStrictEqual(`Hello World`);
expect(zipFs2.readFileSync(file, `utf8`)).toStrictEqual(`Hello World`);
zipFs2.discardAndClose();
});
it(`should support truncate`, () => {
const zipFs = new ZipFS();
const file = `/foo.txt` as PortablePath;
zipFs.writeFileSync(file, `1234567890`);
zipFs.truncateSync(file, 5);
expect(zipFs.readFileSync(file, `utf8`)).toStrictEqual(`12345`);
zipFs.truncateSync(file, 10);
expect(zipFs.readFileSync(file, `utf8`)).toStrictEqual(`12345${`\u0000`.repeat(5)}`);
zipFs.truncateSync(file);
expect(zipFs.readFileSync(file, `utf8`)).toStrictEqual(``);
zipFs.discardAndClose();
});
it(`should support ftruncate`, async () => {
const zipFs = new ZipFS();
const fd = zipFs.openSync(`/foo.txt` as PortablePath, `r+`);
zipFs.writeFileSync(fd, `1234567890`);
zipFs.ftruncateSync(fd, 5);
expect(zipFs.readFileSync(fd, `utf8`)).toStrictEqual(`12345`);
await zipFs.ftruncatePromise(fd, 4);
expect(zipFs.readFileSync(fd, `utf8`)).toStrictEqual(`1234`);
zipFs.closeSync(fd);
zipFs.discardAndClose();
});
it(`should support watchFile and unwatchFile`, () => {
const zipFs = new ZipFS();
const file = `/foo.txt` as PortablePath;
const emptyStats = statUtils.makeEmptyStats();
const changeListener = jest.fn();
const stopListener = jest.fn();
jest.useFakeTimers();
const statWatcher = zipFs.watchFile(file, {interval: 1000}, changeListener);
statWatcher.on(`stop`, stopListener);
// The listener should be initially called with empty stats if the path doesn't exist,
// but only after 3 milliseconds, so that other listeners can be registered in that timespan
// (That's what Node does)
expect(changeListener).not.toHaveBeenCalled();
jest.advanceTimersByTime(3);
expect(changeListener).toHaveBeenCalledTimes(1);
expect(changeListener).toHaveBeenCalledWith(emptyStats, emptyStats);
// The watcher should pick up changes in content
zipFs.writeFileSync(file, `Hello World`);
const first = zipFs.statSync(file);
jest.advanceTimersByTime(1000);
expect(changeListener).toHaveBeenCalledTimes(2);
expect(changeListener).toHaveBeenCalledWith(first, emptyStats);
// The watcher should only pick up the last changes in an interval
zipFs.writeFileSync(file, `This shouldn't be picked up`);
zipFs.writeFileSync(file, `Goodbye World`);
const second = zipFs.statSync(file);
jest.advanceTimersByTime(1000);
expect(changeListener).toHaveBeenCalledTimes(3);
expect(changeListener).toHaveBeenCalledWith(second, first);
// The watcher should pick up deletions
zipFs.unlinkSync(file);
jest.advanceTimersByTime(1000);
expect(changeListener).toHaveBeenCalledTimes(4);
expect(changeListener).toHaveBeenCalledWith(emptyStats, second);
// unwatchFile should work
expect(stopListener).not.toHaveBeenCalled();
zipFs.unwatchFile(file, changeListener);
// The stop event should be emitted when there are no remaining change listeners
expect(stopListener).toHaveBeenCalledTimes(1);
// The listener shouldn't be called after the file is unwatched
zipFs.writeFileSync(file, `Test`);
jest.advanceTimersByTime(1000);
expect(changeListener).toHaveBeenCalledTimes(4);
zipFs.discardAndClose();
// The watcher shouldn't keep the process running after the file is unwatched
});
it(`should accept invalid paths on watchFile (ENOTDIR)`, async () => {
const zipFs = new ZipFS();
const file = `/foo.txt/package.json` as PortablePath;
// Should cause a ENOTDIR error to trigger, but watchFile doesn't care
zipFs.writeFileSync(ppath.dirname(file), ``);
const emptyStats = statUtils.makeEmptyStats();
const changeListener = jest.fn();
const stopListener = jest.fn();
jest.useFakeTimers();
const statWatcher = zipFs.watchFile(file, {interval: 1000}, changeListener);
statWatcher.on(`stop`, stopListener);
expect(changeListener).not.toHaveBeenCalled();
jest.advanceTimersByTime(3);
expect(changeListener).toHaveBeenCalledTimes(1);
expect(changeListener).toHaveBeenCalledWith(emptyStats, emptyStats);
zipFs.discardAndClose();
});
it(`closes the fd created in createReadStream when the stream is closed early`, async () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as Filename, `foo`.repeat(10000));
expect(zipFs.hasOpenFileHandles()).toBe(false);
const stream = zipFs.createReadStream(`/foo.txt` as Filename);
expect(zipFs.hasOpenFileHandles()).toBe(true);
await new Promise<void>((resolve, reject) => {
stream.on(`data`, () => {
reject(new Error(`Should not be called`));
});
stream.on(`close`, () => {
resolve();
});
stream.on(`error`, error => {
reject(error);
});
stream.close();
});
expect(zipFs.hasOpenFileHandles()).toBe(false);
zipFs.discardAndClose();
});
it(`should close the createWriteStream when destroyed`, async () => {
const zipFs = new ZipFS();
const writeStream = zipFs.createWriteStream(`/foo.txt` as Filename);
await new Promise<void>((resolve, reject) => {
writeStream.write(`foo`, err => {
if (err) {
reject(err);
} else {
writeStream.destroy();
resolve();
}
});
});
expect(zipFs.hasOpenFileHandles()).toBe(false);
expect(zipFs.readFileSync(`/foo.txt` as Filename, `utf8`)).toBe(`foo`);
zipFs.discardAndClose();
});
it(`should stop the watcher on closing the archive`, async () => {
jest.useFakeTimers();
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, `foo`);
zipFs.watchFile(`/foo.txt` as PortablePath, (current, previous) => {});
zipFs.discardAndClose();
// If the watcher wasn't stopped this will trigger `EBUSY: archive closed`
jest.advanceTimersByTime(100);
});
it(`should support opendir`, async () => {
const zipFs = new ZipFS();
const folder = `/foo` as PortablePath;
zipFs.mkdirSync(folder);
const firstFile = `/foo/1.txt` as PortablePath;
const secondFile = `/foo/2.txt` as PortablePath;
const thirdFile = `/foo/3.txt` as PortablePath;
zipFs.writeFileSync(firstFile, ``);
zipFs.writeFileSync(secondFile, ``);
zipFs.writeFileSync(thirdFile, ``);
const dir = zipFs.opendirSync(folder);
expect(dir.path).toStrictEqual(folder);
const iter = dir[Symbol.asyncIterator]();
expect((await iter.next()).value.name).toStrictEqual(ppath.basename(firstFile));
expect(dir.readSync()!.name).toStrictEqual(ppath.basename(secondFile));
expect((await dir.read())!.name).toStrictEqual(ppath.basename(thirdFile));
expect((await iter.next()).value).toBeUndefined();
// Consuming the iterator should cause the Dir instance to close
// FIXME: This assertion fails
// await expect(() => iter.next()).rejects.toThrow(`Directory handle was closed`);
expect(() => dir.readSync()).toThrow(`Directory handle was closed`);
// It's important that this function throws synchronously, because that's what Node does
expect(() => dir.read()).toThrow(`Directory handle was closed`);
zipFs.discardAndClose();
});
it(`closes the fd created in opendir when the Dir is closed early`, () => {
const zipFs = new ZipFS();
zipFs.mkdirSync(`/foo` as PortablePath);
expect(zipFs.hasOpenFileHandles()).toBe(false);
const dir = zipFs.opendirSync(`/foo` as Filename);
expect(zipFs.hasOpenFileHandles()).toBe(true);
dir.closeSync();
expect(zipFs.hasOpenFileHandles()).toBe(false);
zipFs.discardAndClose();
});
it(`should emit the 'end' event from large reads in createReadStream`, async () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as Filename, `foo`.repeat(10000));
const stream = zipFs.createReadStream(`/foo.txt` as Filename);
let endEmitted = false;
await new Promise<void>((resolve, reject) => {
stream.on(`end`, () => {
endEmitted = true;
});
stream.on(`close`, () => {
if (!endEmitted) {
setTimeout(() => {
resolve();
}, 1000);
}
});
const nullStream = fs.createWriteStream(process.platform === `win32` ? `\\\\.\\NUL` : `/dev/null`);
const piped = stream.pipe(nullStream);
piped.on(`finish`, () => {
resolve();
});
stream.on(`error`, error => reject(error));
piped.on(`error`, error => reject(error));
});
expect(endEmitted).toBe(true);
zipFs.discardAndClose();
});
it(`should return bigint stats`, () => {
const zipFs = new ZipFS();
zipFs.mkdirSync(`/foo` as PortablePath);
expect(
statUtils.areStatsEqual(
zipFs.statSync(`/foo` as PortablePath, {bigint: true}),
zipFs.statSync(`/foo` as PortablePath, {bigint: true}),
),
).toBe(true);
expect(
statUtils.areStatsEqual(
zipFs.statSync(`/foo` as PortablePath, {bigint: false}),
zipFs.statSync(`/foo` as PortablePath, {bigint: true}),
),
).toBe(false);
zipFs.discardAndClose();
});
it(`should support saving an empty zip archive`, () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
const zipFs = new ZipFS(archive, {create: true});
zipFs.saveAndClose();
expect(xfs.existsSync(archive)).toStrictEqual(true);
expect(new ZipFS(archive).readdirSync(PortablePath.root)).toHaveLength(0);
});
it(`should support saving an empty zip archive (unlink after write)`, () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
const zipFs = new ZipFS(archive, {create: true});
zipFs.writeFileSync(`/foo.txt` as PortablePath, `foo`);
zipFs.unlinkSync(`/foo.txt` as PortablePath);
zipFs.saveAndClose();
expect(xfs.existsSync(archive)).toStrictEqual(true);
expect(new ZipFS(archive).readdirSync(PortablePath.root)).toHaveLength(0);
});
it(`should support getting the buffer from an empty in-memory zip archive`, () => {
const zipFs = new ZipFS();
const buffer = zipFs.getBufferAndClose();
expect(buffer).toStrictEqual(makeEmptyArchive());
expect(new ZipFS(buffer).readdirSync(PortablePath.root)).toHaveLength(0);
});
it(`should support getting the buffer from an empty in-memory zip archive (unlink after write)`, () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, `foo`);
zipFs.unlinkSync(`/foo.txt` as PortablePath);
const buffer = zipFs.getBufferAndClose();
expect(buffer).toStrictEqual(makeEmptyArchive());
expect(new ZipFS(buffer).readdirSync(PortablePath.root)).toHaveLength(0);
});
ifNotWin32It(`should preserve the umask`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
await xfs.writeFilePromise(archive, makeEmptyArchive(), {mode: 0o754});
const zipFs = new ZipFS(archive);
await zipFs.writeFilePromise(`/foo.txt` as PortablePath, `foo`);
zipFs.saveAndClose();
expect((await xfs.statPromise(archive)).mode & 0o777).toStrictEqual(0o754);
});
ifNotWin32It(`should preserve the umask (empty archive)`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
await xfs.writeFilePromise(archive, makeEmptyArchive(), {mode: 0o754});
const zipFs = new ZipFS(archive);
zipFs.saveAndClose();
expect((await xfs.statPromise(archive)).mode & 0o777).toStrictEqual(0o754);
});
ifNotWin32It(`should preserve the umask if the archive is unlinked before being closed`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
await xfs.writeFilePromise(archive, makeEmptyArchive(), {mode: 0o754});
const zipFs = new ZipFS(archive);
await zipFs.writeFilePromise(`/foo.txt` as PortablePath, `foo`);
await xfs.unlinkPromise(archive);
zipFs.saveAndClose();
expect((await xfs.statPromise(archive)).mode & 0o777).toStrictEqual(0o754);
});
ifNotWin32It(`should preserve the umask if the archive is unlinked before being closed (empty archive)`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
await xfs.writeFilePromise(archive, makeEmptyArchive(), {mode: 0o754});
const zipFs = new ZipFS(archive);
await xfs.unlinkPromise(archive);
zipFs.saveAndClose();
expect((await xfs.statPromise(archive)).mode & 0o777).toStrictEqual(0o754);
});
ifNotWin32It(`should create archives with -rw-r--r--`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
const zipFs = new ZipFS(archive, {create: true});
await zipFs.writeFilePromise(`/foo.txt` as PortablePath, `foo`);
zipFs.saveAndClose();
expect((await xfs.statPromise(archive)).mode).toStrictEqual(S_IFREG | 0o644);
});
ifNotWin32It(`should create archives with -rw-r--r-- (empty archive)`, async () => {
const tmpdir = xfs.mktempSync();
const archive = `${tmpdir}/archive.zip` as PortablePath;
const zipFs = new ZipFS(archive, {create: true});
zipFs.saveAndClose();
expect((await xfs.statPromise(archive)).mode).toStrictEqual(S_IFREG | 0o644);
});
it(`should support chmod`, async () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as Filename, `foo`);
zipFs.chmodSync(`/foo.txt` as Filename, 0o754);
expect(zipFs.statSync(`/foo.txt` as Filename).mode & 0o777).toBe(0o754);
await zipFs.writeFilePromise(`/bar.txt` as Filename, `bar`);
await zipFs.chmodPromise(`/bar.txt` as Filename, 0o754);
expect((await zipFs.statPromise(`/bar.txt` as Filename)).mode & 0o777).toBe(0o754);
zipFs.discardAndClose();
});
it(`should support fchmodSync`, () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as Filename, `foo`);
const fd = zipFs.openSync(`/foo.txt` as Filename, `rw`);
zipFs.fchmodSync(fd, 0o754);
zipFs.closeSync(fd);
expect(zipFs.statSync(`/foo.txt` as Filename).mode & 0o777).toBe(0o754);
zipFs.discardAndClose();
});
it(`should support writeFile mode`, async () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as Filename, `foo`, {mode: 0o754});
expect(zipFs.statSync(`/foo.txt` as Filename).mode & 0o777).toBe(0o754);
await zipFs.writeFilePromise(`/bar.txt` as Filename, `bar`, {mode: 0o754});
expect((await zipFs.statPromise(`/bar.txt` as Filename)).mode & 0o777).toBe(0o754);
zipFs.discardAndClose();
});
it(`should support appendFile mode`, async () => {
const zipFs = new ZipFS();
zipFs.appendFileSync(`/foo.txt` as Filename, `foo`, {mode: 0o754});
expect(zipFs.statSync(`/foo.txt` as Filename).mode & 0o777).toBe(0o754);
await zipFs.appendFilePromise(`/bar.txt` as Filename, `bar`, {mode: 0o754});
expect((await zipFs.statPromise(`/bar.txt` as Filename)).mode & 0o777).toBe(0o754);
zipFs.discardAndClose();
});
it(`should support mkdir mode`, async () => {
const zipFs = new ZipFS();
zipFs.mkdirSync(`/foo` as Filename, {mode: 0o754});
expect(zipFs.statSync(`/foo` as Filename).mode & 0o777).toBe(0o754);
await zipFs.mkdirPromise(`/bar` as Filename, {mode: 0o754});
expect((await zipFs.statPromise(`/bar` as Filename)).mode & 0o777).toBe(0o754);
zipFs.discardAndClose();
});
it(`should support fd in writeFile and readFile`, async () => {
const zipFs = new ZipFS();
zipFs.mkdirSync(`/dir` as PortablePath);
zipFs.writeFileSync(`/dir/file` as PortablePath, `file content`);
const fd = zipFs.openSync(`/dir/file` as PortablePath, `r`);
zipFs.writeFileSync(fd, `new content`);
await expect(zipFs.readFilePromise(fd, `utf8`)).resolves.toEqual(`new content`);
await zipFs.writeFilePromise(fd, `new new content`);
expect(zipFs.readFileSync(fd, `utf8`)).toEqual(`new new content`);
zipFs.discardAndClose();
});
it(`should throw ENOTDIR when trying to stat a file as a directory`, () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, ``);
expect(() => zipFs.statSync(`/foo.txt/` as PortablePath)).toThrowError(`ENOTDIR`);
zipFs.symlinkSync(`/foo.txt` as PortablePath, `/bar.txt` as PortablePath);
expect(() => zipFs.lstatSync(`/bar.txt/` as PortablePath)).toThrowError(`ENOTDIR`);
zipFs.discardAndClose();
});
it(`should throw ENOTDIR when trying to create a file when the dirname is a file`, () => {
const zipFs = new ZipFS();
zipFs.writeFileSync(`/foo.txt` as PortablePath, ``);
expect(() => zipFs.writeFileSync(`/foo.txt/bar.txt` as PortablePath, ``)).toThrowError(`ENOTDIR`);
zipFs.symlinkSync(`/foo.txt` as PortablePath, `/bar.txt` as PortablePath);
expect(() => zipFs.writeFileSync(`/bar.txt/baz.txt` as PortablePath, ``)).toThrowError(`ENOTDIR`);
zipFs.discardAndClose();
});
it(`should throw ENOENT when reading a file that doesn't exist`, () => {
const zipFs = new ZipFS();
// File doesn't exist
expect(() => zipFs.readFileSync(`/foo` as PortablePath)).toThrowError(`ENOENT`);
// Parent entry doesn't exist
expect(() => zipFs.readFileSync(`/foo/bar` as PortablePath)).toThrowError(`ENOENT`);
zipFs.discardAndClose();
});
it(`should return the first created directory in mkdir recursive`, () => {
const zipFs = new ZipFS();
expect(zipFs.mkdirSync(`/foo` as PortablePath, {recursive: true})).toEqual(`/foo` as PortablePath);
expect(zipFs.mkdirSync(`/foo` as PortablePath, {recursive: true})).toEqual(undefined);
expect(zipFs.mkdirSync(`/foo/bar/baz` as PortablePath, {recursive: true})).toEqual(`/foo/bar` as PortablePath);
expect(zipFs.mkdirSync(`/foo/bar/baz` as PortablePath, {recursive: true})).toEqual(undefined);
zipFs.discardAndClose();
});
it(`should return the first created directory in mkdirp`, () => {
const zipFs = new ZipFS();
expect(zipFs.mkdirpSync(`/foo` as PortablePath)).toEqual(`/foo` as PortablePath);
expect(zipFs.mkdirpSync(`/foo` as PortablePath)).toEqual(undefined);
expect(zipFs.mkdirpSync(`/foo/bar/baz` as PortablePath)).toEqual(`/foo/bar` as PortablePath);
expect(zipFs.mkdirpSync(`/foo/bar/baz` as PortablePath)).toEqual(undefined);
zipFs.discardAndClose();
});
it(`should support the recursive flag in readdir`, () => {
const zipFs = new ZipFS();
zipFs.mkdirSync(`/foo` as PortablePath);
zipFs.mkdirSync(`/foo/bar` as PortablePath);
zipFs.mkdirSync(`/bar` as PortablePath);
zipFs.writeFileSync(`/foo/file.txt` as PortablePath, `Test`);
zipFs.writeFileSync(`/foo/bar/file.txt` as PortablePath, `Test`);
zipFs.writeFileSync(`/bar/file.txt` as PortablePath, `Test`);
zipFs.writeFileSync(`/file.txt` as PortablePath, `Test`);
const zipContent = zipFs.getBufferAndClose();
const zipFs2 = new ZipFS(zipContent);
expect(zipFs2.readdirSync(`/` as PortablePath, {recursive: true}).sort()).toEqual([
`bar`,
`bar/file.txt`,
`file.txt`,
`foo`,
`foo/bar`,
`foo/bar/file.txt`,
`foo/file.txt`,
]);
expect(zipFs2.readdirSync(`/foo` as PortablePath, {recursive: true}).sort()).toEqual([
`bar`,
`bar/file.txt`,
`file.txt`,
]);
});
it(`should support the combination of recursive and withFileTypes in readdir`, () => {
const zipFs = new ZipFS();
zipFs.mkdirSync(`/foo` as PortablePath);
zipFs.mkdirSync(`/foo/bar` as PortablePath);
zipFs.mkdirSync(`/bar` as PortablePath);
zipFs.writeFileSync(`/foo/file.txt` as PortablePath, `Test`);
zipFs.writeFileSync(`/foo/bar/file.txt` as PortablePath, `Test`);
zipFs.writeFileSync(`/bar/file.txt` as PortablePath, `Test`);
zipFs.writeFileSync(`/file.txt` as PortablePath, `Test`);
const zipContent = zipFs.getBufferAndClose();
const readdir = (p: PortablePath) => {
return zipFs2.readdirSync(p, {recursive: true, withFileTypes: true}).sort((a, b) => {
return a.path.localeCompare(b.path) || a.name.localeCompare(b.name);
}).map(({name, path}) => {
return {name, path};
});
};
const zipFs2 = new ZipFS(zipContent);
expect(readdir(PortablePath.root)).toEqual([
{name: `bar`, path: `.`},
{name: `file.txt`, path: `.`},
{name: `foo`, path: `.`},
{name: `file.txt`, path: `bar`},
{name: `bar`, path: `foo`},
{name: `file.txt`, path: `foo`},
{name: `file.txt`, path: `foo/bar`},
]);
expect(readdir(ppath.join(PortablePath.root, `foo`))).toEqual([
{name: `bar`, path: `.`},
{name: `file.txt`, path: `.`},
{name: `file.txt`, path: `bar`},
]);
});
it(`should support throwIfNoEntry`, async () => {
const zipFs = new ZipFS();
expect(zipFs.statSync(`/foo` as PortablePath, {throwIfNoEntry: false})).toEqual(undefined);
expect(zipFs.statSync(`/foo/bar` as PortablePath, {throwIfNoEntry: false})).toEqual(undefined);
expect(zipFs.lstatSync(`/foo` as PortablePath, {throwIfNoEntry: false})).toEqual(undefined);
expect(zipFs.lstatSync(`/foo/bar` as PortablePath, {throwIfNoEntry: false})).toEqual(undefined);
await expect(
zipFs.statPromise(`/foo` as PortablePath, {
// @ts-expect-error throwIfNoEntry is not a valid option but statPromise
// calls statSync which does support it, this checks that it's ignored.
throwIfNoEntry: false,
}),