Skip to content

Commit

Permalink
destroy() and "close" events
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 5, 2012
1 parent 44702c3 commit 7071d4b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/request.js
Expand Up @@ -30,6 +30,10 @@ var Request = module.exports = function (xhr, params) {
}

var res = new Response;
res.on('close', function () {
self.emit('close');
});

res.on('ready', function () {
self.emit('response', res);
});
Expand Down Expand Up @@ -57,6 +61,11 @@ Request.prototype.write = function (s) {
this.body.write(s);
};

Request.prototype.destroy = function (s) {
this.xhr.abort();
this.emit('close');
};

Request.prototype.end = function (s) {
if (s !== undefined) this.body.write(s);
this.body.end()
Expand Down
12 changes: 7 additions & 5 deletions lib/response.js
Expand Up @@ -45,8 +45,8 @@ function parseHeaders (res) {

Response.prototype.getResponse = function (xhr) {
var respType = xhr.responseType.toLowerCase();
if (respType === "blob") return xhr.responseBlob;
if (respType === "arraybuffer") return xhr.response;
if (respType === 'blob') return xhr.responseBlob;
if (respType === 'arraybuffer') return xhr.response;
return xhr.responseText;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ Response.prototype.handle = function (res) {
catch (err) {}

try {
this.write(res);
this._emitData(res);
}
catch (err) {
capable.streaming = false;
Expand All @@ -90,16 +90,18 @@ Response.prototype.handle = function (res) {
this.statusCode = res.status;
this.emit('ready');
}
this.write(res);
this._emitData(res);

if (res.error) {
this.emit('error', this.getResponse(res));
}
else this.emit('end');

this.emit('close');
}
};

Response.prototype.write = function (res) {
Response.prototype._emitData = function (res) {
var respBody = this.getResponse(res);
if (respBody.toString().match(/ArrayBuffer/)) {
this.emit('data', new Uint8Array(respBody, this.offset));
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name" : "http-browserify",
"version" : "0.1.3",
"version" : "0.1.4",
"description" : "http module compatability for browserify",
"main" : "index.js",
"browserify" : "index.js",
Expand Down

0 comments on commit 7071d4b

Please sign in to comment.