Created
May 29, 2010 12:52
-
-
Save dvv/418264 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
middleware capable (not true so far :) of gzipping Buffers: | |
======================================= | |
function dir(x){var sys=require('sys');sys.debug(sys.inspect(x));} | |
exports.Compress = function(nextApp){ | |
return function(request){ | |
var encoding = 'gzip'; | |
if ((request.headers['accept-encoding']||'').indexOf(encoding) >= 0) { | |
//dir('gzipping'); | |
// so far only node.js provides compression module | |
var Gzip = require('compress').Gzip; | |
if (true && Gzip) { | |
return require('promise').when(nextApp(request), function(response){ | |
var zipper = new Gzip; | |
zipper.init(); | |
var data = response.body; | |
response.body = { | |
encoding: 'binary', | |
forEach: function(write){ | |
var r = data.forEach(function(chunk){ | |
//if (chunk instanceof Buffer) chunk = chunk.toString('binary', 0, chunk.length); | |
return write(zipper.deflate(chunk, 'binary')); | |
}); | |
write(zipper.end()); | |
return r; | |
} | |
}; | |
/*response.body = { | |
encoding: 'binary', | |
forEach: function(write){ | |
var Buffer = require('buffer').Buffer; | |
dir('begin'); | |
var r = data.forEach(function(chunk){ | |
return require('promise').when(chunk, function(chunk){ | |
//if (chunk instanceof Buffer) chunk = chunk.toString('binary', 0, chunk.length); | |
return write(zipper.deflate(chunk, 'binary')); | |
}); | |
}); | |
dir('end'); | |
write(zipper.end()); | |
dir('end1'); | |
return r; | |
} | |
};*/ | |
/*response.body = { | |
encoding: 'binary', | |
forEach: function(write){ | |
return data.forEach(function(chunk){ | |
write(chunk.toString('binary')); | |
}); | |
} | |
};*/ | |
//response.headers['content-encoding'] = encoding; | |
//dir('gzipped'); | |
return response; | |
}); | |
} | |
} | |
return nextApp(request); | |
}; | |
}; | |
separate middleware version: | |
========================== | |
/** | |
* JSGI app that GZIPs the response | |
*/ | |
exports.Compress = function(nextApp){ | |
return function(request){ | |
var encoding = 'gzip'; | |
if ((request.headers['accept-encoding']||'').indexOf(encoding) >= 0) { | |
// so far only node.js provides compression module | |
var Gzip = require('compress').Gzip; | |
if (true && Gzip) { | |
return require('promise').when(nextApp(request), function(response){ | |
var zipper = new Gzip; | |
zipper.init(); | |
var data = response.body; | |
response.body = { | |
encoding: 'binary', | |
forEach: function(write){ | |
data.forEach(function(chunk){ | |
write(zipper.deflate(chunk, 'binary')); | |
}); | |
write(zipper.end()); | |
} | |
}; | |
response.headers['content-encoding'] = encoding; | |
return response; | |
}); | |
} | |
} | |
return nextApp(request); | |
}; | |
}; | |
jsgi/media inline version: | |
========================== | |
// compress the body | |
var encoding = 'gzip'; | |
if ((request.headers['accept-encoding']||'').indexOf(encoding) >= 0) { | |
// so far only node.js provides compression module | |
var Gzip = require('compress').Gzip; | |
if (Gzip) { | |
var zipper = new Gzip; | |
zipper.init(); | |
var data = response.body; | |
response.body = { | |
encoding: 'binary', | |
forEach: function(write){ | |
data.forEach(function(chunk){ | |
write(zipper.deflate(chunk, 'binary')); | |
}); | |
write(zipper.end()); | |
} | |
}; | |
response.headers['content-encoding'] = encoding; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment