Skip to content

Commit 50941a7

Browse files
committed
adding features to test/zip.js to reproduce #9.
$ node --stack-size=100 --stack-trace-limit=200 test/zip.js index.js --buffer $(for i in {1..1000}; do echo index.js; done) -o /dev/null
1 parent cc7d74c commit 50941a7

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

test/zip.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
var usage = "node " + __filename.replace(/.*[\/\\]/, "") + " " +
2-
"[FILE | --compress | --no-compress]... -o OUTPUT.zip";
2+
"[FILE | --compress | --no-compress | --buffer | --no-buffer]... -o OUTPUT.zip" + "\n" +
3+
"\n" +
4+
"all arguments and switches are processed in order. for example:" + "\n" +
5+
" node zip.js --compress a.txt --no-compress b.txt -o out.zip" + "\n" +
6+
"would result in compression for a.txt, but not for b.txt.";
37
var yazl = require("../");
48
var fs = require("fs");
59

610
var zipfile = new yazl.ZipFile();
711
var options = {compress: false};
12+
var use_buffer = false;
813

914
var args = process.argv.slice(2);
10-
if (Math.max(args.indexOf("-h"), args.indexOf("--help")) !== -1) throw new Error("usage: " + usage);
11-
var outputFileIndex = args.indexOf("-o");
12-
if (outputFileIndex === -1) throw new Error("missing -o");
13-
zipfile.outputStream.pipe(fs.createWriteStream(args[outputFileIndex + 1]));
14-
args.splice(outputFileIndex, 2);
15+
if (Math.max(args.indexOf("-h"), args.indexOf("--help")) !== -1) {
16+
console.log("usage: " + usage);
17+
process.exit(1);
18+
}
19+
// this one's important
20+
if (args.indexOf("-o") === -1) throw new Error("missing -o");
21+
if (args.indexOf("-o") + 1 >= args.length) throw new Error("missing argument after -o");
22+
23+
var its_the_dash_o = false;
1524
args.forEach(function(arg) {
16-
if (/--compress/.test(arg)) {
25+
if (its_the_dash_o) {
26+
its_the_dash_o = false;
27+
zipfile.outputStream.pipe(fs.createWriteStream(arg));
28+
} else if (arg === "--compress") {
1729
options.compress = true;
18-
} else if (/--no-compress/.test(arg)) {
30+
} else if (arg === "--no-compress") {
1931
options.compress = false;
32+
} else if (arg === "--buffer") {
33+
use_buffer = true;
34+
} else if (arg === "--no-buffer") {
35+
use_buffer = false;
36+
} else if (arg === "-o") {
37+
its_the_dash_o = true;
2038
} else {
2139
// file thing
2240
var stats = fs.statSync(arg);
2341
if (stats.isFile()) {
24-
zipfile.addFile(arg, arg, options);
42+
if (use_buffer) {
43+
zipfile.addBuffer(fs.readFileSync(arg), arg, options);
44+
} else {
45+
zipfile.addFile(arg, arg, options);
46+
}
2547
} else if (stats.isDirectory()) {
2648
zipfile.addEmptyDirectory(arg);
2749
} else {

0 commit comments

Comments
 (0)