Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

delay opening of XHR object until next tick #39

Open
wants to merge 1 commit into from

1 participant

Stephen Sugden
Stephen Sugden

The use case for this is to allow attaching event listeners to "progress" events. Listeners that are attached to this event after open has been called (when the request is in readyState === 1) are ignored, so receiving progress events with requests made via require('http').request was impossible.

Stephen Sugden grncdr commented on the diff
lib/request.js
((47 lines not shown))
+ if (params.auth) {
+ //basic auth
+ self.setHeader(
+ 'Authorization',
+ 'Basic ' + Base64.btoa(params.auth)
+ );
+ }
+
+ self.emit('open', xhr)
Stephen Sugden
grncdr added a note

Pretty sure this is the wrong event name. It might make more sense to do something like 'xhr.open' or even re-use the 'socket' event from node core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 5, 2014
  1. Stephen Sugden
This page is out of date. Refresh to see the latest.
Showing with 35 additions and 22 deletions.
  1. +35 −22 lib/request.js
57 lib/request.js
View
@@ -18,32 +18,39 @@ var Request = module.exports = function (xhr, params) {
try { xhr.withCredentials = true }
catch (e) {}
- xhr.open(
- params.method || 'GET',
- self.uri,
- true
- );
-
- if (params.headers) {
- var keys = objectKeys(params.headers);
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i];
- if (!self.isSafeRequestHeader(key)) continue;
- var value = params.headers[key];
- if (isArray(value)) {
- for (var j = 0; j < value.length; j++) {
- xhr.setRequestHeader(key, value[j]);
+ self._open = function () {
+ xhr.open(
+ params.method || 'GET',
+ self.uri,
+ true
+ );
+
+ if (params.headers) {
+ var keys = objectKeys(params.headers);
+ for (var i = 0; i < keys.length; i++) {
+ var key = keys[i];
+ if (!self.isSafeRequestHeader(key)) continue;
+ var value = params.headers[key];
+ if (isArray(value)) {
+ for (var j = 0; j < value.length; j++) {
+ xhr.setRequestHeader(key, value[j]);
+ }
}
+ else xhr.setRequestHeader(key, value)
}
- else xhr.setRequestHeader(key, value)
}
- }
-
- if (params.auth) {
- //basic auth
- this.setHeader('Authorization', 'Basic ' + Base64.btoa(params.auth));
- }
+ if (params.auth) {
+ //basic auth
+ self.setHeader(
+ 'Authorization',
+ 'Basic ' + Base64.btoa(params.auth)
+ );
+ }
+
+ self.emit('open', xhr)
Stephen Sugden
grncdr added a note

Pretty sure this is the wrong event name. It might make more sense to do something like 'xhr.open' or even re-use the 'socket' event from node core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ };
+
var res = new Response;
res.on('close', function () {
self.emit('close');
@@ -72,6 +79,9 @@ Request.prototype.setHeader = function (key, value) {
};
Request.prototype.write = function (s) {
+ if (this.xhr.readyState === 0) {
+ this._open();
+ }
this.body.push(s);
};
@@ -82,6 +92,9 @@ Request.prototype.destroy = function (s) {
Request.prototype.end = function (s) {
if (s !== undefined) this.body.push(s);
+ if (this.xhr.readyState === 0) {
+ this._open();
+ }
if (this.body.length === 0) {
this.xhr.send('');
}
Something went wrong with that request. Please try again.