diff --git a/History.md b/History.md index 3de7366..97d4f0a 100644 --- a/History.md +++ b/History.md @@ -2,6 +2,7 @@ HEAD ================== * update co + * use on-socket-error 0.1.1 / 2013-12-19 ================== diff --git a/lib/application.js b/lib/application.js index 00a63d8..be1a6b2 100644 --- a/lib/application.js +++ b/lib/application.js @@ -3,6 +3,7 @@ */ var debug = require('debug')('koa:application'); +var onSocketError = require('on-socket-error'); var Emitter = require('events').EventEmitter; var compose = require('koa-compose'); var context = require('./context'); @@ -99,7 +100,7 @@ app.callback = function(){ return function(req, res){ var ctx = self.createContext(req, res); - ctx.onSocketError(ctx.onerror); + onSocketError(ctx, ctx.onerror); fn.call(ctx, ctx.onerror); } }; diff --git a/lib/context.js b/lib/context.js index b379754..050ea1a 100644 --- a/lib/context.js +++ b/lib/context.js @@ -488,20 +488,4 @@ module.exports = { attachment: function() { return this.response.attachment.apply(this.response, arguments); }, - - /** - * Invoke `fn` on socket errors and - * handle cleanup on response. - * - * @param {Function} fn - * @api private - */ - - onSocketError: function(fn){ - var sock = this.socket; - sock.once('error', fn); - this.res.on('finish', function(){ - sock.removeListener('error', fn); - }); - } }; diff --git a/package.json b/package.json index 2c1f00b..ed4489e 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ ], "license": "MIT", "dependencies": { + "on-socket-error": "~1.0.0", "co": "~3.0.0", "debug": "*", "mime": "~1.2.11",