Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added gzip compression support

  • Loading branch information...
commit 76ccf0aca83a5eee659849b30d908948d0246653 1 parent db88686
@timw4mail timw4mail authored
View
2  README.md
@@ -8,6 +8,8 @@ All configuration is specified in the ``app/config.js`` file. This object is a g
Routing defaults to a ``controller/method`` url structure, and can be customized using regex matches in the route object of the ``config.js`` file.
+Gzip compression is available if you have the `gzip` module installed from npm.
+
The following objects are global:
* ``template``: the template object
View
41 core/template.js
@@ -132,13 +132,44 @@ function _do_parse(tmp, data, response)
console.timeEnd('total');
//Finally, return the value
- response.writeHead(200, {
- "Content-Type": "text/html",
- });
- response.write(tmp);
- response.end();
+ _output(tmp, response);
}
+/**
+ * Output
+ *
+ * Outputs the content to the client's browser
+ * @param string html
+ * @param object response
+ */
+function _output(html, response)
+{
+ //Gzip if possible
+ try{
+ var gzip = require('gzip');
+
+ gzip(html, function(err, data){
+
+ response.writeHead(200, {
+ "Content-Type": "text/html",
+ "Content-Encoding":"gzip",
+ "Vary": "Accept-Encoding"
+ });
+
+ response.write(data);
+ response.end();
+ });
+ }
+ catch(e)
+ {
+ response.writeHead(200, {
+ "Content-Type": "text/html",
+ });
+ response.write(html);
+ response.end();
+ }
+}
+
var template = {
/**
* Parse
View
0  node_modules/gzip/.gitignore
No changes.
View
19 node_modules/gzip/LICENSE
@@ -0,0 +1,19 @@
+
+Copyright 2009, 2010 Fedor Indutny. All rights reserved.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
View
20 node_modules/gzip/README.rdoc
@@ -0,0 +1,20 @@
+===Installation
+ npm install gzip
+
+===Usage
+ var gzip = require('gzip');
+
+ gzip('some text', function(err, data){
+ // By default:
+ // compression = 8
+ // encoding = utf8
+ console.log(data);
+ });
+ gzip('some text', 'ascii', function(err, data){
+ // Change encoding
+ console.log(data);
+ });
+ gzip(buffer, 4, function(err, data){
+ // Gzip buffer with compression rate = 4
+ console.log(data);
+ });
View
63 node_modules/gzip/lib/gzip/index.js
@@ -0,0 +1,63 @@
+var spawn = require('child_process').spawn,
+ Buffer = require('buffer').Buffer;
+
+module.exports = function(data) {
+ var rate = 8,
+ enc = 'utf8',
+ isBuffer = Buffer.isBuffer(data),
+ args = Array.prototype.slice.call(arguments, 1),
+ callback;
+
+ if (!isBuffer && typeof args[0] === 'string') {
+ enc = args.shift();
+ }
+
+ if (typeof args[0] === 'number') {
+ rate = args.shift() - 0;
+ }
+ callback = args[0];
+
+ var gzip = spawn('gzip', ['-' + (rate-0),'-c', '-']);
+
+ var promise = new process.EventEmitter,
+ output = [],
+ output_len = 0;
+
+ // No need to use buffer if no callback was provided
+ if (callback) {
+ gzip.stdout.on('data', function(data) {
+ output.push(data);
+ output_len += data.length;
+ });
+
+ gzip.on('exit', function(code) {
+ var buf = new Buffer(output_len);
+
+ for (var a = 0, p = 0; p < output_len; p += output[a++].length) {
+ output[a].copy(buf, p, 0);
+
+ }
+
+ callback(code, buf);
+ });
+ }
+
+ // Promise events
+ gzip.stdout.on('data', function(data) {
+ promise.emit('data', data);
+ });
+ gzip.on('exit', function(code) {
+ promise.emit('end');
+ });
+
+ if (isBuffer) {
+ gzip.stdin.encoding = 'binary';
+ gzip.stdin.end(data.length ? data: '');
+ } else {
+ gzip.stdin.end(data ? data.toString() : '', enc);
+ }
+
+ // Return EventEmitter, so node.gzip can be used for streaming
+ // (thx @indexzero for that tip)
+ return promise;
+};
View
25 node_modules/gzip/package.json
@@ -0,0 +1,25 @@
+{
+ "name" : "gzip",
+ "description" : "Gzip for node",
+ "version" : "0.1.0",
+ "homepage" : "http://github.com/donnerjack13589/node.gzip",
+ "author" : "Fedor Indutny <fedor.indutny@gmail.com> (http://github.com/donnerjack13589)",
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/donnerjack13589/node.gzip.git"
+ },
+ "bugs" :
+ {
+ "mail" : "fedor.indutny@gmail.com",
+ "web" : "http://github.com/donnerjack13589/node.gzip/issues"
+ },
+ "directories" : { "lib" : "./lib/gzip" },
+ "main": "lib/gzip/index",
+ "engines" : { "node" : ">=v0.2.0" },
+ "licenses" :
+ [ {
+ "type" : "MIT",
+ "url" : "http://github.com/donnerjack13589/node.gzip/raw/master/LICENSE"
+ }
+ ]
+}
View
21 node_modules/gzip/test/test-basic.js
@@ -0,0 +1,21 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ gzip = require('../lib/gzip'),
+ Buffer = require('buffer').Buffer;
+
+vows.describe('node-gzip').addBatch({
+ 'String compressed by node-gzip' : {
+ topic: function() {
+ gzip('some-string', this.callback);
+ },
+ 'should be not empty': function(compressed) {
+ assert.ok(compressed);
+ },
+ 'should be Buffer': function(compressed) {
+ assert.ok(Buffer.isBuffer(compressed));
+ },
+ 'should have non-zero length': function(compressed) {
+ assert.ok(compressed.length > 0);
+ }
+ }
+}).export(module);
View
44 node_modules/gzip/test/test-streaming.js
@@ -0,0 +1,44 @@
+var vows = require('vows'),
+ assert = require('assert'),
+ gzip = require('../lib/gzip'),
+ Buffer = require('buffer').Buffer;
+
+var promise;
+
+vows.describe('node-gzip').addBatch({
+ 'When executed node-gzip' : {
+ topic: function() {
+ this.callback(null, gzip('some-string'));
+ },
+ 'should return EventEmitter': function(result) {
+ assert.instanceOf(result, process.EventEmitter);
+ promise = result;
+ }
+ }
+}).addBatch({
+ 'A "promise"': {
+ 'should receive "data" event': {
+ topic: function() {
+ var callback = this.callback;
+ promise.on('data', function(data) {
+ callback(null, data);
+ });
+ },
+ 'with Buffer argument': function(data) {
+ assert.instanceOf(data, Buffer);
+ }
+ },
+ 'should receive "end" event': {
+ topic: function() {
+ var callback = this.callback;
+ promise.on('end', function(data) {
+ callback(null, data);
+ });
+ },
+ 'with no arguments!': function(data) {
+ assert.ok(!data);
+ }
+ }
+ }
+}).export(module);
+
Please sign in to comment.
Something went wrong with that request. Please try again.