Skip to content

Commit 162d9bc

Browse files
authored
fix: preserve mtime when zipping with the node zipper (#539)
This means it's possible for code to retrieve the correct mtime, for example to return in a `Last-Modified` header. Since we have a test which verifies a checksum of a zip file created by this function, we can be sure the results are deterministic. To support this, update the test inputs to have specified mtimes.
1 parent 081242f commit 162d9bc

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/tests/util.test.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@ const nodeVersion = parseInt(process.versions.node.split('.')[0]!, 10);
2323
const itNode18 = nodeVersion > 18 ? it.skip : it;
2424

2525
describe('utils/zip', () => {
26+
const mtime = new Date(2024, 0, 1, 0, 0, 0, 0);
27+
2628
beforeEach(() => {
2729
mockFs({
28-
'/src': {
29-
'test.txt': 'lorem ipsum',
30-
modules: {
31-
'module.txt': 'lorem ipsum 2',
30+
'/src': mockFs.directory({
31+
mtime,
32+
items: {
33+
'test.txt': mockFs.file({
34+
mtime,
35+
content: 'lorem ipsum',
36+
}),
37+
modules: mockFs.directory({
38+
mtime,
39+
items: {
40+
'module.txt': mockFs.file({
41+
mtime,
42+
content: 'lorem ipsum 2',
43+
}),
44+
},
45+
}),
3246
},
33-
},
34-
'/dist': {},
47+
}),
48+
'/dist': mockFs.directory({ mtime }),
3549
});
3650
});
3751

@@ -70,7 +84,17 @@ describe('utils/zip', () => {
7084
},
7185
];
7286

73-
await zip(zipPath, filesPathList, useNativeZip);
87+
// Check the mtimes are set correctly
88+
const sourceStat = fs.statSync(source);
89+
expect(sourceStat.mtime).toEqual(mtime);
90+
91+
const testStat = fs.statSync('/src/test.txt');
92+
expect(testStat.mtime).toEqual(mtime);
93+
94+
const moduleStat = fs.statSync('/src/modules/module.txt');
95+
expect(moduleStat.mtime).toEqual(mtime);
96+
97+
await expect(zip(zipPath, filesPathList, useNativeZip)).resolves.toBeUndefined();
7498

7599
expect(fs.existsSync(zipPath)).toEqual(true);
76100

@@ -84,7 +108,7 @@ describe('utils/zip', () => {
84108
if (!useNativeZip) {
85109
const data = fs.readFileSync(zipPath);
86110
const fileHash = crypto.createHash('sha256').update(data).digest('base64');
87-
expect(fileHash).toEqual('iCZdyHJ7ON2LLwBIE6gQmRvBTzXBogSqJTMvHSenzGk=');
111+
expect(fileHash).toEqual('PHu2gv7OIMv+lAOCXYPNd30X8/7EKYTuV7KYJjw3Qd4=');
88112
}
89113
}
90114
);

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function nodeZip(zipPath: string, filesPathList: IFiles): Promise<void> {
112112
zipArchive.append(fs.readFileSync(file.rootPath), {
113113
name: file.localPath,
114114
mode: stats.mode,
115-
date: new Date(0), // necessary to get the same hash when zipping the same content
115+
date: new Date(stats.mtime),
116116
});
117117
});
118118

0 commit comments

Comments
 (0)