Skip to content

Commit

Permalink
Headers now passed to res._clientRes (also comment out logs)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Dec 15, 2014
1 parent af12034 commit d97bb71
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
2 changes: 2 additions & 0 deletions lib/app/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ module.exports = function request( /* address, body, cb */ ) {
var clientRes = new MockClientResponse();
clientRes.on('finish', function() {

// console.log('clientRes finished. Headers:',clientRes.headers);

// Only dump the buffer if a callback was supplied
if (cb) {
clientRes.body = Buffer.concat(clientRes._readableState.buffer).toString();
Expand Down
10 changes: 5 additions & 5 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Router.prototype.route = function(req, res) {
return res.send(400, err && err.stack);
}

console.log('Ran cookie parser');
// console.log('Ran cookie parser');
// console.log('res.writeHead= ',res.writeHead);

// Load session (if relevant)
Expand All @@ -184,9 +184,9 @@ Router.prototype.route = function(req, res) {
return res.send(400, err && err.stack);
}
// console.log('res is now:\n',res);
console.log('\n\n');
console.log('Ran session middleware. Now req.session= ',req.session);
console.log('req.sessionID= ',req.sessionID);
// console.log('\n\n');
// console.log('Ran session middleware. Now req.session= ',req.session);
// console.log('req.sessionID= ',req.sessionID);

sails.log.silly('Handling virtual request :: Running virtual body parser...');
bodyParser(req,res, function (err) {
Expand Down Expand Up @@ -421,7 +421,7 @@ function parseCookies (req, res, next){

// TODO: this line of code does not need to be run for every request
req._sails.log.verbose('Parsing cookie:',req.headers.cookie);
console.log('Parsing cookie:',req.headers.cookie);
// console.log('Parsing cookie:',req.headers.cookie);
var _cookieParser = Express.cookieParser(req._sails && req._sails.config.session && req._sails.config.session.secret);

// Run the middleware
Expand Down
63 changes: 33 additions & 30 deletions lib/router/res.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = function buildResponse (req, _res) {

// Ensure res.headers and res.locals exist.
// res = _.merge(res, {locals: {}, headers: {}}, _res);
res = _.extend(res, {locals: {}, headers: {}});
res = _.extend(res, {locals: {}, headers: {}, _headers: {}});
res = _.extend(res, _res);

// Now that we're sure `res` is a Transform stream, we'll handle the two different
Expand Down Expand Up @@ -101,7 +101,7 @@ module.exports = function buildResponse (req, _res) {
// res.writeHead() is wrapped in closure by the `on-header` module,
// but it still needs the underlying impl
res.writeHead = function ( /* statusCode, [reasonPhrase], headers */) {
console.log('res.writeHead(%s)', Array.prototype.slice.call(arguments));
// console.log('\n\n• res.writeHead(%s)', Array.prototype.slice.call(arguments));
var statusCode = +arguments[0];
var reasonPhrase = (function(){
if (arguments[2] && _.isString(arguments[1])) {
Expand All @@ -122,6 +122,8 @@ module.exports = function buildResponse (req, _res) {
// Set status code
res.statusCode = statusCode;

// Ensure `._headers` have been merged into `.headers`
_.extend(res.headers, res._headers);

if (newHeaders) {
if (!_.isObject(newHeaders)) {
Expand All @@ -131,6 +133,16 @@ module.exports = function buildResponse (req, _res) {
_.extend(res.headers, newHeaders);
}

// Set status code and headers on the `_clientRes` stream so they are accessible
// to the provider of that stream.
// (this has to happen in `send()` because the code/headers might have just changed)
if (res._clientRes) {
// console.log('Setting headers on clientRes- res.headers = ',res.headers);
res._clientRes.headers = res.headers;
res._clientRes.statusCode = res.statusCode;
}


// Flag headers as written.
headersWritten = true;

Expand All @@ -139,20 +151,21 @@ module.exports = function buildResponse (req, _res) {

var prevSetHeader = res.setHeader;
res.setHeader = function (){
console.log('called res.setHeader() w/ args=',arguments);
// console.log('called res.setHeader() w/ args=',arguments);
prevSetHeader.apply(res, Array.prototype.slice.call(arguments));
};

// Wrap res.write() and res.end() to get them to call writeHead()
var prevWrite = res.write;
res.write = function (){
res.writeHead(res.statusCode, res._headers);
console.log('res.write():: res.headers=',res.headers);
res.writeHead(res.statusCode, _.extend(res._headers,res.headers));
// console.log('res.write():: called writeHead with headers=',_.extend(res._headers,res.headers));
prevWrite.apply(res, Array.prototype.slice.call(arguments));
};
var prevEnd = res.end;
res.end = function (){
res.writeHead(res.statusCode, res._headers);
res.writeHead(res.statusCode, _.extend(res._headers,res.headers));
// console.log('res.end():: called writeHead with headers=',_.extend(res._headers,res.headers));
prevEnd.apply(res, Array.prototype.slice.call(arguments));
};

Expand Down Expand Up @@ -186,6 +199,7 @@ module.exports = function buildResponse (req, _res) {
req._sails.log.error(e);
return;
}
// TODO: use debug()
console.error(e);
return;
}
Expand All @@ -195,7 +209,7 @@ module.exports = function buildResponse (req, _res) {
res.charset = res.charset || 'utf-8';

// Ensure headers are set
res.headers = _.extend(res.headers || {}, res._headers || {});
_.extend(res.headers, res._headers);

// Ensure statusCode is set
// (override `this.statusCode` if `statusCode` argument specified)
Expand All @@ -205,29 +219,25 @@ module.exports = function buildResponse (req, _res) {
if (res._clientCallback) {

// Manually plug in res.body.
res.body = args.other;
res._clientRes.body = res.body;
// (but don't include body if it is empty)
if (!res.body) delete res.body;
if (!res._clientRes.body) delete res._clientRes.body;

// Set status code and headers on the `_clientRes` stream so they are accessible
// to the provider of that stream.
// (this has to happen in `send()` because the code/headers might have just changed)
if (res._clientRes) {

// TODO: try `res.emit('header')` to trigger .on('header') listeners
res._clientRes.headers = res.headers;
res._clientRes.statusCode = res.statusCode;
if (res.body) {
res.body = args.other;
res._clientRes.body = res.body;
}

// End the `res` stream (which will in turn end the `res._clientRes` stream)
res.end();
return;
}

//
// Otherwise, the hook using the interpreter must have provided us with a `res._clientRes` stream,
// so we'll need to serialize everything to work w/ that stream.
//

// console.log('\n---\nwriting to clientRes stream...');
// console.log('res.headers =>',res.headers);
// console.log('res._headers =>',res._headers);

// Write body to `res` stream
if (args.other) {
Expand All @@ -238,19 +248,20 @@ module.exports = function buildResponse (req, _res) {
try {
toWrite = JSON.stringify(args.other);

// original method:
// original way:
// toWrite = util.inspect(toWrite);
}
catch(e) {
var failedStringify = new Error(
'Failed to stringify specified JSON response body :: ' + util.inspect(args.other) +
'\nError:\n' + util.inspect(e)
);
console.log('failed to stringify!');
// console.log('failed to stringify!');
if (req._sails && req._sails.log && req._sails.log.error) {
req._sails.log.error(failedToStringify);
}
else {
// todo: use debug()
console.error(failedToStringify);
}
toWrite = failedStringify.message;
Expand All @@ -260,14 +271,6 @@ module.exports = function buildResponse (req, _res) {
res.write(toWrite);
}

// Set status code and headers on the `_clientRes` stream so they are accessible
// to the provider of that stream.
// (this has to happen in `send()` because the code/headers might have just changed)
if (res._clientRes) {
res._clientRes.headers = res.headers;
res._clientRes.statusCode = res.statusCode;
}

// End the `res` stream.
res.end();
};
Expand Down

0 comments on commit d97bb71

Please sign in to comment.