From 2a6fba63a3173bf807df98f67090c0ccd8645d4a Mon Sep 17 00:00:00 2001 From: James Halliday Date: Fri, 19 Aug 2011 18:18:41 -0700 Subject: [PATCH] some more try/catches around readyStates 2 and 3, now works properly in opera --- example/streaming/index.html | 8 +++ main.js => example/streaming/main.js | 0 server.js => example/streaming/server.js | 0 xhr.js => index.js | 0 request.js => lib/request.js | 9 ++-- lib/response.js | 62 ++++++++++++++++++++++++ response.js | 34 ------------- 7 files changed, 75 insertions(+), 38 deletions(-) create mode 100644 example/streaming/index.html rename main.js => example/streaming/main.js (100%) rename server.js => example/streaming/server.js (100%) rename xhr.js => index.js (100%) rename request.js => lib/request.js (89%) create mode 100644 lib/response.js delete mode 100644 response.js diff --git a/example/streaming/index.html b/example/streaming/index.html new file mode 100644 index 0000000..6263e48 --- /dev/null +++ b/example/streaming/index.html @@ -0,0 +1,8 @@ + + + xhr + + + + + diff --git a/main.js b/example/streaming/main.js similarity index 100% rename from main.js rename to example/streaming/main.js diff --git a/server.js b/example/streaming/server.js similarity index 100% rename from server.js rename to example/streaming/server.js diff --git a/xhr.js b/index.js similarity index 100% rename from xhr.js rename to index.js diff --git a/request.js b/lib/request.js similarity index 89% rename from request.js rename to lib/request.js index 36481e8..20e617f 100644 --- a/request.js +++ b/lib/request.js @@ -24,11 +24,12 @@ var Request = module.exports = function (xhr, params) { xhr.open(params.method || 'GET', 'http://' + uri, true); var res = new Response; + res.on('ready', function () { + self.emit('response', res); + }); + xhr.onreadystatechange = function () { - if (this.readyState === 2) { - self.emit('response', res); - } - res.response(this); + res.handle(this); }; }; diff --git a/lib/response.js b/lib/response.js new file mode 100644 index 0000000..7f04305 --- /dev/null +++ b/lib/response.js @@ -0,0 +1,62 @@ +var EventEmitter = require('events').EventEmitter; + +var Response = module.exports = function (res) { + this.offset = 0; +}; + +Response.prototype = new EventEmitter; + +var capable = { + streaming : true, + status2 : true +}; + +Response.prototype.handle = function (res) { + if (res.readyState === 2 && capable.status2) { + try { + this.statusCode = res.status; + } + catch (err) { + capable.status2 = false; + } + + if (capable.status2) { + this.emit('ready'); + } + } + else if (capable.streaming && res.readyState === 3) { + try { + if (!this.statusCode) { + this.statusCode = res.status; + this.emit('ready'); + } + } + catch (err) {} + + try { + this.write(res); + } + catch (err) { + capable.streaming = false; + } + } + else if (res.readyState === 4) { + if (!this.statusCode) { + this.statusCode = res.status; + this.emit('ready'); + } + this.write(res); + + if (res.error) { + this.emit('error', res.responseText); + } + else this.emit('end'); + } +}; + +Response.prototype.write = function (res) { + if (res.responseText.length > this.offset) { + this.emit('data', res.responseText.slice(this.offset)); + this.offset = res.responseText.length; + } +}; diff --git a/response.js b/response.js deleted file mode 100644 index c1629b4..0000000 --- a/response.js +++ /dev/null @@ -1,34 +0,0 @@ -var EventEmitter = require('events').EventEmitter; - -var Response = module.exports = function (res) { - this.offset = 0; -}; - -Response.prototype = new EventEmitter; - -Response.prototype.response = function (res) { - this.statusCode = res.status; - if (res.readyState === 3) { - this.write(res); - } - else if (res.readyState === 4) { - this.write(res); - - if (res.error) { - this.emit('error', res.responseText); - } - else this.emit('end'); - } -}; - -Response.prototype.write = function (res) { - if (res.responseText.length > this.offset) { - this.emit('data', res.responseText.slice(this.offset)); - this.offset = res.responseText.length; - this.emit('end'); - } -}; - -Response.prototype.end = function () { - this.emit('end'); -};