Content-Encoding:gzip

483 views
Skip to first unread message

Jorge

unread,
Mar 3, 2010, 6:34:38 AM3/3/10
to nodejs
Are there any JS APIs for that, or do I have to do it "by hand" ?

Are there any examples somewhere ?

TIA,
--
Jorge.

Akzhan Abdulin

unread,
Mar 3, 2010, 6:44:23 AM3/3/10
to nod...@googlegroups.com
I recommend to use nginx as http reverse proxy server to handle low level tasks like static files handling, gzip, routing, load balancing etc.

Simply use directive like: location / {  proxy_pass localhost:<NODEJS PORT>; }

2010/3/3 Jorge <jo...@jorgechamorro.com>

--
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.


Joran Greef

unread,
Mar 3, 2010, 7:04:46 AM3/3/10
to nodejs

Joran Greef

unread,
Mar 29, 2010, 10:46:35 AM3/29/10
to nodejs
Jorge, did you manage to gzip content?

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

unread,
Mar 30, 2010, 5:27:50 AM3/30/10
to nod...@googlegroups.com
On 29/03/2010, at 16:46, Joran Greef wrote:

Jorge, did you manage to gzip content?

No. I tried but I failed. Here's what I did:

************** gzipper.js

var sys= require("sys");
var http= require("http");
var port= 12345;


function rnd (n) {
  return (n* Math.random()) >>> 0;
}

function rndStr (len) {
  var s= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var l= s.length;
  var r= "";
  while (r.length < len) {
    r+= s[rnd(l)];
  }
  return r;

function gzipStr (str, cb) {
  var key= rndStr(16)+ "\n";
  var cmd= "gzip -c -f << "+ key+ str+ "\n"+ key;
  sys.exec(cmd, function (err, stdout, stderr) {
    sys.puts("str: "+str+"\nerr: "+err+"\nstdout: "+stdout+"\nstderr: "+stderr);
    cb(err ? "" : stdout);
  });
}

http.createServer(function (request, response) {
  
  if (request.url.indexOf("favicon") >= 0) {
    response.writeHeader(404, {});
    response.write("");
    response.close();
    return;
  }

  gzipStr("this is a test", 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+ "/");


******************* console telnet test:

$ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1

HTTP/1.1 200 OK
Content-Type: text/plain
server: Node.js
Content-Encoding: gzip
Connection: close
Content-Length: 33

v??K+??,V?D????.rConnection closed by foreign host.



******************* console gzip test:

$ gzip -c -f << EOF
> this is a test
> EOF
#??K+??,V?D????.r


******************* console char count test:

$ gzip -c -f << EOF | wc -c
> this is a test
> EOF
      33

******************

I have tried changing the response encoding to "utf-8" and "ascii" and "binary", but the browser always complains that "Safari can’t open the page “http://localhost:12345/”. The error is: “cannot decode raw data”"

I have no idea what I'm doing wrong.
-- 
Jorge.

Joran Greef

unread,
Mar 30, 2010, 5:46:29 AM3/30/10
to nodejs
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.

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+ "/");

Jorge

unread,
Mar 30, 2010, 11:43:31 AM3/30/10
to nod...@googlegroups.com
On 30/03/2010, at 11:46, Joran Greef wrote:

> 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.

Reply all
Reply to author
Forward
0 new messages