Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node gzip #475

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Enable gzip response compression in node file handlers
  • Loading branch information
jacksonic committed Nov 24, 2015
commit 295b8baf23769553d59312862c6336c832a60630
29 changes: 25 additions & 4 deletions js/foam/node/handlers/FileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ CLASS({
name: 'path',
hidden: true,
},
{
name: 'zlib',
factory: function() { return require('zlib'); }
},
{
name: 'stream',
factory: function() { return require('stream'); }
},
{
model_: 'foam.node.NodeRequireProperty',
name: 'fs',
Expand All @@ -47,10 +55,23 @@ CLASS({
if ( this.url.parse(req.url).pathname !== this.pathname ) return false;

this.fs.readFile(this.file, function(err, data) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better to restructure this as

this.fs.createReadStream(this.file) similar to the StaticFileHandler

if ( err ) {
this.send(res, 404, 'File not found.');
} else {
this.send(res, 200, data.toString());
if ( err ) {
this.send(res, 404, 'File not found.');
} else {
res.statusCode = 200;

if ( req.headers["accept-encoding"] &&
req.headers["accept-encoding"].indexOf("gzip") !== -1 ) {
res.setHeader('Content-encoding', 'gzip');
var src = new this.stream.Readable();
src._read = function noop() {}
src.push(data.toString());
src.push(null);
//src.write( 'utf8');
src.pipe(this.zlib.createGzip()).pipe(res);
} else {
this.send(res, 200, data.toString());
}
}
}.bind(this));

Expand Down
14 changes: 12 additions & 2 deletions js/foam/node/handlers/StaticFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ CLASS({
name: 'fs',
factory: function() { return require('fs'); }
},
{
name: 'zlib',
factory: function() { return require('zlib'); }
},
{
name: 'dir',
documentation: 'Directory under which to serve files.',
Expand Down Expand Up @@ -123,7 +127,6 @@ CLASS({
body += '</ul></body></html>';
res.setHeader('Content-type', 'text/html');
res.statusCode = 200;
res.write(body, 'utf8');
res.end();
this.log('200 OK (dir) ' + target);
} else {
Expand All @@ -138,7 +141,14 @@ CLASS({
// Open the file as a stream.
this.log('200 OK ' + target);
var stream = this.fs.createReadStream(target);
stream.pipe(res);

if ( req.headers["accept-encoding"] &&
req.headers["accept-encoding"].indexOf("gzip") !== -1 ) {
res.setHeader('Content-encoding', 'gzip');
stream.pipe(this.zlib.createGzip()).pipe(res);
} else {
stream.pipe(res);
}
}
return true;
}
Expand Down