this.writable to check if the socket is writable

because node sucks haha
master
Jonathan Ong 2014-02-15 02:06:08 -08:00
parent e56f442222
commit cb532b7bef
4 changed files with 28 additions and 2 deletions

View File

@ -152,7 +152,7 @@ function *respond(next){
if (false === this.respond) return;
var res = this.res;
if (res.headersSent || !res.socket.writable) return;
if (res.headersSent || !this.writable) return;
var body = this.body;
var status = this.status = this.status || 404;

View File

@ -106,7 +106,7 @@ var proto = module.exports = {
// nothing we can do here other
// than delegate to the app-level
// handler and log.
if (this.headerSent || !this.socket.writable) {
if (this.headerSent || !this.writable) {
err.headerSent = true;
this.app.emit('error', err, this);
return;
@ -153,6 +153,7 @@ delegate(proto, 'response')
.access('type')
.access('charset')
.getter('headerSent')
.getter('writable')
.setter('lastModified')
.setter('etag');

View File

@ -434,6 +434,21 @@ module.exports = {
this.set(field, list.join(', '));
},
/**
* Checks if the request is writable.
* Tests for the existence of the socket
* as node sometimes does not set it.
*
* @return {Boolean}
* @api private
*/
get writable() {
var socket = this.res.socket;
if (!socket) return false;
return socket.writable;
},
/**
* Inspect implementation.
*

10
test/context/writable.js Normal file
View File

@ -0,0 +1,10 @@
var context = require('../context');
describe('ctx.writable', function(){
it('should not crash when the socket does not exist', function(){
var ctx = context();
ctx.socket = null;
ctx.writable.should.equal(false);
})
})