-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use XDomainRequest in browsers which can't CORS with XMLHttpRequest #55
base: master
Are you sure you want to change the base?
Changes from all commits
70bd1a1
488eabf
db0f1b3
7439d1d
600fade
32d57a5
8c914fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ var Request = module.exports = function (xhr, params) { | |
+ (params.port ? ':' + params.port : '') | ||
+ (params.path || '/') | ||
; | ||
|
||
if (typeof params.withCredentials === 'undefined') { | ||
params.withCredentials = true; | ||
} | ||
|
@@ -53,17 +53,59 @@ var Request = module.exports = function (xhr, params) { | |
self.emit('close'); | ||
}); | ||
|
||
res.on('ready', function () { | ||
res.once('ready', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ready should def not fire more than once, but this change is a little safety check |
||
self.emit('response', res); | ||
}); | ||
|
||
xhr.onreadystatechange = function () { | ||
// Fix for IE9 bug | ||
// SCRIPT575: Could not complete the operation due to error c00c023f | ||
// It happens when a request is aborted, calling the success callback anyway with readyState === 4 | ||
if (xhr.__aborted) return; | ||
if (xhr.readyState === 4 && xhr.status === 0) { | ||
// onerror will fire and its an error so there should be no res | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe that this actually fixes a completely different bug unrelated to XDR. If node, if you If needed I can make a separate pull for this. |
||
res.handle(xhr); | ||
}; | ||
|
||
xhr.onerror = function() { | ||
// http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute | ||
// there was an error making the request | ||
// In node, this means the request should emit 'error' and no response | ||
// should ever be created | ||
if (xhr.status === 0) { | ||
// error flag is set so no response should be gotten | ||
requestError(new Error("XHR is done (state 4) but error flag is set (status 0)")); | ||
return; | ||
} | ||
else if ( ! xhr.status) { | ||
// If falsy but not 0, it's an IE XDR | ||
requestError(new Error('XDomainRequest Error')); | ||
return; | ||
} | ||
|
||
xhr.error = xhr.error || 'Unknown error'; | ||
res.handle(xhr); | ||
|
||
function requestError(err) { | ||
self.emit('error', err); | ||
self.emit('close'); | ||
} | ||
}; | ||
|
||
if (window.XDomainRequest && xhr instanceof window.XDomainRequest) { | ||
// IE XDomainRequest has onload but not onreadystatechange | ||
// Raised when the object has been completely received from the server. | ||
// http://msdn.microsoft.com/en-us/library/ie/ms536942(v=vs.85).aspx | ||
xhr.onload = function() { // IE XDR | ||
res.handle(xhr); | ||
}; | ||
// IE sometimes fails if you don't specify every handler. | ||
// https://github.com/matthewwithanm/httpplease.js/blob/861b26a9916543ff34963192d734da4c06603d0b/lib/index.js#L94 | ||
xhr.onprogress = function () {}; | ||
xhr.ontimeout = function () {}; | ||
} | ||
}; | ||
|
||
inherits(Request, Stream); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an appropriate XHR-like thing is now chosen based on the params, not just once at eval time