Skip to content

Commit

Permalink
fix: preserve mtime when zipping with the node zipper (#539)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
iainlane authored Sep 24, 2024
1 parent 081242f commit 162d9bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
40 changes: 32 additions & 8 deletions src/tests/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ const nodeVersion = parseInt(process.versions.node.split('.')[0]!, 10);
const itNode18 = nodeVersion > 18 ? it.skip : it;

describe('utils/zip', () => {
const mtime = new Date(2024, 0, 1, 0, 0, 0, 0);

beforeEach(() => {
mockFs({
'/src': {
'test.txt': 'lorem ipsum',
modules: {
'module.txt': 'lorem ipsum 2',
'/src': mockFs.directory({
mtime,
items: {
'test.txt': mockFs.file({
mtime,
content: 'lorem ipsum',
}),
modules: mockFs.directory({
mtime,
items: {
'module.txt': mockFs.file({
mtime,
content: 'lorem ipsum 2',
}),
},
}),
},
},
'/dist': {},
}),
'/dist': mockFs.directory({ mtime }),
});
});

Expand Down Expand Up @@ -70,7 +84,17 @@ describe('utils/zip', () => {
},
];

await zip(zipPath, filesPathList, useNativeZip);
// Check the mtimes are set correctly
const sourceStat = fs.statSync(source);
expect(sourceStat.mtime).toEqual(mtime);

const testStat = fs.statSync('/src/test.txt');
expect(testStat.mtime).toEqual(mtime);

const moduleStat = fs.statSync('/src/modules/module.txt');
expect(moduleStat.mtime).toEqual(mtime);

await expect(zip(zipPath, filesPathList, useNativeZip)).resolves.toBeUndefined();

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

Expand All @@ -84,7 +108,7 @@ describe('utils/zip', () => {
if (!useNativeZip) {
const data = fs.readFileSync(zipPath);
const fileHash = crypto.createHash('sha256').update(data).digest('base64');
expect(fileHash).toEqual('iCZdyHJ7ON2LLwBIE6gQmRvBTzXBogSqJTMvHSenzGk=');
expect(fileHash).toEqual('PHu2gv7OIMv+lAOCXYPNd30X8/7EKYTuV7KYJjw3Qd4=');
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function nodeZip(zipPath: string, filesPathList: IFiles): Promise<void> {
zipArchive.append(fs.readFileSync(file.rootPath), {
name: file.localPath,
mode: stats.mode,
date: new Date(0), // necessary to get the same hash when zipping the same content
date: new Date(stats.mtime),
});
});

Expand Down

0 comments on commit 162d9bc

Please sign in to comment.