Are there any examples somewhere ?
TIA,
--
Jorge.
--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
And see http://groups.google.com/group/nodejs/t/f6c735eb747a89dd for
help with installation.
On Mar 3, 2:04 pm, Joran Greef <jorangr...@gmail.com> wrote:
> See:http://github.com/waveto/node-compress
>
> And seehttp://groups.google.com/group/nodejs/t/f6c735eb747a89ddfor
> help with installation.
Jorge, did you manage to gzip content?
Then, using node-compress, this code will work:
var Gzip = {
deflate: function(content, encoding) {
var gzip = new this._compress.Gzip;
gzip.init();
var data = gzip.deflate(content, encoding || 'utf8');
return data + gzip.end();
},
inflate: function(data, encoding) {
var gunzip = new this._compress.Gunzip;
gunzip.init();
var content = gunzip.inflate(data, encoding || 'utf8');
return content + gunzip.end();
},
_compress: require('./compress')
};
I tried this yesterday but have noticed that node-compress appears to
be leaking memory which is not being collected at all by V8.
> sys.puts("Server running athttp://localhost:"+ port+ "/");
> I tried out gzipStr in an REPL to see if I could zip something with it
> and then use something else to unzip and match the results. What
> encoding is sys.exec using for stdout? That could be the problem.
It *is* the problem. Any 0xff >= char > 0x7f is converted to 0xfffd.
> Then, using node-compress, this code will work:
>
> var Gzip = {
> deflate: function(content, encoding) {
> var gzip = new this._compress.Gzip;
> gzip.init();
> var data = gzip.deflate(content, encoding || 'utf8');
> return data + gzip.end();
> },
>
> inflate: function(data, encoding) {
> var gunzip = new this._compress.Gunzip;
> gunzip.init();
> var content = gunzip.inflate(data, encoding || 'utf8');
> return content + gunzip.end();
> },
>
> _compress: require('./compress')
> };
>
> I tried this yesterday but have noticed that node-compress appears to
> be leaking memory which is not being collected at all by V8.
I've made it work by gzipping to a tmp file: when reading a file the encoding can be set to "binary":
//20100330 jo...@jorgechamorro.com
//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);
});
});
}
http.createServer(function (request, response) {
if (request.url.indexOf("favicon") >= 0) {
response.writeHeader(404, {});
response.write("");
return response.close();
}
var str= newLoremIpsum(4096);
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+ "/");
//***** 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;
}
--
Jorge.