Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
call: refactor abort() to handle differences in node versions
Browse files Browse the repository at this point in the history
On a call to request.abort() three different scenarios exist

 - node.js 0.10 emits error event
 - node.js 0.12 silently aborts
 - io.js 1.x emits abort event

This commit ...

 - silences errors after abort()
 - implements an abort event
 - implements an plugin-abort event
 - removes request.socket.destroy() code
  • Loading branch information
skenqbx committed Apr 24, 2015
1 parent 17b31fb commit 36af677
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
3 changes: 3 additions & 0 deletions doc/api.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ Emitted after the request object has been created and the send-buffer has been f

`function({Object} request)`

### Event 'abort'
Emitted when a pending connect or active request is aborted.

### Event 'response'
Emitted after the response headers have been received.

Expand Down
5 changes: 5 additions & 0 deletions doc/plugin-api.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Emitted directly after a request object has been created.

`function({Call} call, {Object} options, {Request} request)`

### Event: 'plugin-abort'
Emitted when a pending connect or active request is aborted.

`function({Call} call, {Object} options)`

### Event: 'plugin-response'
Emitted when response headers have been received.

Expand Down
49 changes: 28 additions & 21 deletions lib/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function Call(rail, opt_options) {
this.ended = false;
this.aborted = false; // call abort flag
this._aborted = false; // request abort flag (reset-able)
this._destroyed = false; // socket destroyed flag (reset-able)
this._reason = '';

// stack of request configurations
Expand Down Expand Up @@ -205,12 +204,11 @@ Call.prototype.__request = function(opt_callback) {
});

request.on('error', function(err) {
if (self._aborted || self._destroyed) {
err.reason = self._reason;
if (self._aborted) {
return; // ignore errors after a request has been aborted, see call.__abort()
}
self.__emit('error', err);

// TODO: clear when the actual error is emitted
self.request = null;
self.response = null;

Expand Down Expand Up @@ -245,6 +243,7 @@ Call.prototype.__request = function(opt_callback) {
Call.prototype.__emit = function(event, object) {
var listener;


if (this._interceptors[event] && this._interceptors[event].length) {
listener = this._interceptors[event].shift();
listener(this, this._stack[this._pointer], object);
Expand All @@ -268,28 +267,39 @@ Call.prototype.__intercept = function(event, interceptor) {

Call.prototype.__clear = function() {
this._aborted = false;
this._destroyed = false;
this._reason = '';

this._interceptors = {};
};


Call.prototype.__abort = function(opt_reason) {
Call.prototype.__abort = function() {
// request.abort()
// - node.js 0.10 emits error event
// - node.js 0.12 silently aborts
// - io.js 1.x emits abort event
var self = this;

if (this.request) {
if (typeof this.request.abort === 'function' &&
this.request.aborted === undefined) {

this._aborted = true;
this._reason = opt_reason || 'unknown';
this.request.abort();
self.__clear();

setImmediate(function() {
self.rail.emit('plugin-abort', self, self._stack[self._pointer]);

} else if (this.request.socket &&
typeof this.request.socket.destroy === 'function') {
if (self.aborted) {
self.emit('abort');

this._destroyed = true;
this._reason = opt_reason || 'unknown';
this.request.socket.destroy();
if (self._buffer) {
self._buffer.close();
self._buffer.dump();
self._buffer = null;
}
}
});
}
return true;
}
Expand All @@ -299,14 +309,10 @@ Call.prototype.__abort = function(opt_reason) {


Call.prototype.abort = function() {
this.aborted = true;

if (this._buffer) {
this._buffer.dump();
this._buffer = null;
if (!this.aborted) {
this.aborted = true;
this.__abort(true);
}

this.__abort('user');
};


Expand All @@ -324,6 +330,7 @@ Call.prototype.end = function(chunk, encoding, opt_callback) {
} else if (!encoding) {
encoding = null;
}

opt_callback = opt_callback || function() {};

if (this.ended) {
Expand Down Expand Up @@ -353,7 +360,7 @@ Call.prototype.end = function(chunk, encoding, opt_callback) {
self._buffer.close();
}
request.end();
opt_callback(err2);
opt_callback();
});
});

Expand Down

0 comments on commit 36af677

Please sign in to comment.