-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgzipper2.js
86 lines (68 loc) · 2.03 KB
/
gzipper2.js
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
#!/usr/bin/env node
//20100330 [email protected]
//gzipStr(str) returns str gzipped to cb(str)
var sys= require("sys");
var http= require("http");
var fs= require("fs");
var port= 12345;
function gzipStr (str, cb, key, file, cmd) {
do {
key= rndStr(16);
} while (str.indexOf(key) >= 0);
file= "/tmp/NodeGzipTmpFile_"+ key+ "_"+ (+new Date())+ ".gz";
cmd= "gzip -n << "+ key+ " > "+ file +"\n"+ str+ "\n"+ key+ "\n";
sys.exec(cmd, function (err, stdout, stderr) {
if (err) return cb("");
fs.readFile(file, "binary", function (err, data) {
fs.unlink(file, function () {});
return cb(err ? "" : data);
});
});
}
//***** utility f()s
function rndStr (len) {
var s= "abcdefghijklmnopqrstuvwxyz1234567890";
var l= s.length;
var r= "";
while (r.length < len) {
r+= s[rnd(l)];
}
return r;
}
function rnd (n) {
return (n* Math.random()) >>> 0;
}
var words= "Lorem ipsum dolor sit amet consectetur adipiscing elit Suspendisse nunc ante ut tincidunt fringilla id risus pulvinar metus nec scelerisque pellentesque".toLowerCase().split(" ");
function newLoremIpsum (length, r, curr, prev) {
r= "";
while (r.length < length) {
do {
curr= words[rnd(words.length)];
} while (curr === prev);
r+= (prev= curr)+ " ";
}
return r;
}
http.createServer(function (request, response) {
if (request.url.indexOf("favicon") >= 0) {
response.writeHeader(404, {});
response.write("");
return response.close();
}
var str= "this is a test";
var now= +new Date();
gzipStr(str, function (binaryString) {
if (binaryString) {
response.writeHeader(200, {
"Content-Type": "text/plain; charset=UTF-8;",
"server":"Node.js",
"Content-Encoding":"gzip",
"Connection":"close"
});
response.write(binaryString, "binary");
}
response.close();
//sys.puts("before:"+ str.length+ ", after:"+ binaryString.length+ ", time:"+ (new Date()- now)+ "ms");
});
}).listen(port);
sys.puts("Server running at http://localhost:"+ port+ "/");