Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toolkit): ensure consistent zip files #2931

Merged
merged 7 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions packages/aws-cdk/lib/archive.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
import archiver = require('archiver');
import crypto = require('crypto');
import fs = require('fs-extra');
import glob = require('glob');
import path = require('path');

export function zipDirectory(directory: string, outputFile: string): Promise<void> {
return new Promise((ok, fail) => {
const output = fs.createWriteStream(outputFile);
const archive = archiver('zip');

// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
const globOptions = {
dot: true,
nodir: true,
follow: true,
cwd: directory
cwd: directory,
};
archive.glob('**', globOptions);
archive.pipe(output);
archive.finalize();
const files = glob.sync('**', globOptions); // The output here is already sorted

output.on('open', async () => {
archive.pipe(output);

const contents = await Promise.all(files.map(async (file) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will load all files into memory concurrently. Not sure I like that for very big zips (tens to hundreds of megabytes).

Also, don't see the use of doing this in parallel since this task should be I/O bound, as far as I can estimate.

Can you test and verify whether the speed makes a difference? If it does, then I would prefer some kind of batching (chop the list into batches of 10 and do those in parallel) to keep memory consumption under control.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understand, will have a look at it

const data = await fs.readFile(path.join(directory, file));
return {
data,
name: file
};
}));

contents.forEach(content => { // Append files serially to ensure file order
archive.append(content.data, {
name: content.name,
date: new Date(0), // reset dates to get the same hash for the same content
});
});

archive.finalize();
});

archive.on('warning', fail);
archive.on('error', fail);
Expand Down
1 change: 1 addition & 0 deletions packages/aws-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"colors": "^1.3.3",
"decamelize": "^3.2.0",
"fs-extra": "^8.0.1",
"glob": "^7.1.4",
"json-diff": "^0.5.4",
"minimatch": ">=3.0",
"promptly": "^3.0.3",
Expand Down