Permalink
Browse files

using response-stream for the http server response method passing

  • Loading branch information...
1 parent fe77042 commit c5b20abd92724d72d74794c63de88e49368902a7 @substack committed Aug 29, 2012
Showing with 24 additions and 70 deletions.
  1. +23 −70 index.js
  2. +1 −0 package.json
View
@@ -1,90 +1,43 @@
var Negotiator = require('negotiator');
var zlib = require('zlib');
var through = require('through');
+var responseStream = require('response-stream');
module.exports = function (req) {
var negotiator = new Negotiator(req);
var enc = negotiator.preferredEncodings([ 'gzip', 'compress', 'identity' ]);
if (Array.isArray(enc)) enc = enc[0];
+
var createStream = {
gzip : zlib.createGzip,
compress : zlib.createDeflate,
identity : through,
- }[enc];
-
- if (!createStream) return unacceptable();
- var stream = createStream();
-
- var pipe = stream.pipe;
- stream.pipe = function (target) {
- if (target && target.writeHead && target.setHeader) {
- if (target.statusCode === 200) {
- target.statusCode = stream.statusCode;
- }
-
- if (!target._headers
- || target._headers['content-encoding'] === undefined) {
- target.setHeader('content-encoding', enc);
- }
-
- proxied.forEach(function (p) {
- target[p.name].apply(target, p.arguments);
- });
-
- target.removeHeader('content-length');
- proxyResponse(stream, target, enc !== 'identity');
- }
- return pipe.apply(this, arguments);
- };
+ }[enc] || through;
- stream.statusCode = 200;
- var proxied = proxyResponse(stream, null, enc !== 'identity');
- return stream;
-};
-
-function unacceptable () {
- // as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
- var stream = through();
- var proxied = proxyResponse(stream);
- stream.pipe = function (target) {
- if (target.statusCode && target.write && target.end) {
- target.statusCode = 406;
- target.write('encoding not accepted');
- target.end();
+ var stream = responseStream(createStream());
+ stream.on('setHeader', function (args, prevent) {
+ if (String(args[0]).toLowerCase() === 'content-length') {
+ prevent();
}
- else stream.emit('error', 'encoding not accepted');
- };
- return stream;
-}
-
-function proxyResponse (stream, dst, doIntercept) {
- // proxy calls through so this module works with request and filed-style
- // streaming pipe http response hijacking
- var proxied = [];
- [ 'writeContinue', 'writeHead', 'setHeader', 'sendDate', 'getHeader',
- 'removeHeader', 'addTrailers' ].forEach(function (name) {
- stream[name] = function () {
- if (doIntercept && intercept(name, arguments)) return;
- if (dst) return dst[name].apply(dst, arguments);
-
- // hopefully the return value isn't important...
- proxied.push({ name : name, arguments : arguments });
- };
});
- return proxied;
-}
-
-function intercept (name, args) {
- if (name === 'setHeader'
- && String(args[0].toLowerCase()) === 'content-length') {
-
- return true;
- }
- if (name === 'writeHead' && typeof args[1] === 'object') {
+
+ stream.on('writeHead', function (args, prevent) {
+ if (!args[0] || typeof args[0] !== 'object') return;
+
Object.keys(args[1]).forEach(function (key) {
if (String(key).toLowerCase() === 'content-length') {
delete args[0][key];
}
});
- }
-}
+ });
+
+ stream.on('response', function (res) {
+ if (!res._headers
+ || res._headers['content-encoding'] === undefined) {
+ res.setHeader('content-encoding', enc);
+ }
+ res.removeHeader('content-length');
+ });
+
+ return stream;
+};
View
@@ -9,6 +9,7 @@
},
"dependencies" : {
"through" : "~0.1.4",
+ "response-stream" : "~0.0.0",
"negotiator" : "~0.2.5"
},
"devDependencies" : {

0 comments on commit c5b20ab

Please sign in to comment.