Permalink
Browse files

support blob/arraybuffer requests and responses

  • Loading branch information...
1 parent 10019e0 commit c5ffd1d02048b8d723d2809537f29de4104eb2fd @maxogden maxogden committed Aug 4, 2012
Showing with 25 additions and 22 deletions.
  1. +6 −18 lib/request.js
  2. +16 −4 lib/response.js
  3. +3 −0 package.json
View
@@ -1,10 +1,11 @@
var EventEmitter = require('events').EventEmitter;
var Response = require('./response');
+var concatStream = require('concat-stream')
var Request = module.exports = function (xhr, params) {
var self = this;
self.xhr = xhr;
- self.body = [];
+ self.body = concatStream()
var uri = params.host + ':' + params.port + (params.path || '/');
@@ -52,26 +53,13 @@ Request.prototype.setHeader = function (key, value) {
};
Request.prototype.write = function (s) {
- this.body.push(s);
-};
-
-Request.prototype.getBody = function () {
- if (this.body.length === 0) return;
- if (typeof(this.body[0]) === "string") return this.body.join('');
- if (this.body[0].toString().match(/Array/)) {
- var first = false;
- this.body.forEach(function(ary) {
- if (!first) return first = ary;
- first.concat(ary);
- })
- return first;
- }
- return this.body;
+ this.body.write(s);
};
Request.prototype.end = function (s) {
- if (s !== undefined) this.write(s);
- this.xhr.send(this.getBody());
+ if (s !== undefined) this.body.write(s);
+ this.body.end()
+ this.xhr.send(this.body.getBody());
};
// Taken from http://dxr.mozilla.org/mozilla/mozilla-central/content/base/src/nsXMLHttpRequest.cpp.html
View
@@ -42,6 +42,13 @@ function parseHeaders (res) {
return headers;
}
+Response.prototype.getResponse = function (xhr) {
+ var respType = xhr.responseType.toLowerCase();
+ if (respType === "blob") return xhr.responseBlob;
+ if (respType === "arraybuffer") return xhr.response;
+ return xhr.responseText;
+}
+
Response.prototype.getHeader = function (key) {
return this.headers[key.toLowerCase()];
};
@@ -85,15 +92,20 @@ Response.prototype.handle = function (res) {
this.write(res);
if (res.error) {
- this.emit('error', res.responseText);
+ this.emit('error', this.getResponse(res));
}
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;
+ var respBody = this.getResponse(res);
+ if (respBody.toString().match(/ArrayBuffer/)) {
+ var ui8a = new Uint8Array(respBody);
+ respBody = String.fromCharCode.apply(null, ui8a);
+ }
+ if (respBody.length > this.offset) {
+ this.emit('data', respBody.slice(this.offset));
+ this.offset = respBody.length;
}
};
View
@@ -9,6 +9,9 @@
"example" : "example",
"test" : "test"
},
+ "dependencies": {
+ "concat-stream": "0.0.8"
@Raynos
Raynos Aug 5, 2012

this murdered 0.6 support on http-browserify and browserify

@maxogden
maxogden Aug 5, 2012

why cant you upgrade to 0.8?

@Raynos
Raynos Aug 5, 2012

I can. It's just that all modules I have with 0.6 support on travis fall over. So anything that uses browserify becomes 0.8 only. browserify has a long reach on a lot of my modules.

I basically have to remote browserify from my package.json for my 0.6 travis CI tests to pass

@ferentchak
ferentchak Oct 17, 2012

Plus c9.io is still on version 0.6. As node becomes more mature the solution cannot always be instantly upgrade to the newest version :)

+ },
"devDependencies" : {
"express" : "2.5.x",
"browserify" : "1.9.x"

0 comments on commit c5ffd1d

Please sign in to comment.