forked from xk/nodeSnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgzipper3.js
97 lines (81 loc) · 2.25 KB
/
gzipper3.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
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
//20100401 [email protected]
//still attempting to gzip afap
var sys= require("sys");
var http= require("http");
var spawn = require("child_process").spawn;
var port= 12345;
function gzipStr (str, callback, child, stdout, stderr) {
child= spawn("gzip", ["-c", "-f", "-n"]);
stdout= stderr= "";
child.stdout.addListener("data", function (chunk) {
if (chunk) stdout+= chunk;
});
child.stderr.addListener("data", function (chunk) {
if (chunk) stderr+= chunk;
});
child.addListener("exit", function (code) {
//sys.puts("input: \""+ str+ "\"\n-->"+ hexdump(stdout));
callback(code ? "" : stdout);
});
child.stdout.setEncoding('binary');
child.stdin.write(str, "utf8");
child.stdin.close();
};
http.createServer(function (request, response) {
if (request.url.indexOf("favicon") >= 0) {
response.writeHeader(404, {});
response.write("");
return response.close();
}
gzipStr(newLoremIpsum(8192), function (str) {
response.writeHeader(200, {
"Content-Type": "text/plain",
"server":"Node.js",
"Content-Encoding":"gzip",
"Connection":"close",
"Content-Length":str.length
});
response.write(str, "binary");
response.close();
});
}).listen(port);
sys.puts("Server running at http://localhost:"+ port+ "/");
// ************ Utilities
function hexdump (input, r, i) {
r= "";
if (typeof input === "string") {
i= 0;
while (i < input.length) {
r+= hexdump(input.charCodeAt(i++));
}
} else if (typeof input === "number") {
i= input.toString(16);
i= (i.length < 2) ? "0"+i : i;
r= i+ ".";
}
return r;
}
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;
}