-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from Shopify/ie9-cors-support
[FIX] Support the network layer in IE9
- Loading branch information
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
function authToUrl(url, opts) { | ||
let authorization; | ||
|
||
if (opts.headers) { | ||
Object.keys(opts.headers).forEach(key => { | ||
if (key.toLowerCase() === 'authorization') { | ||
authorization = opts.headers[key]; | ||
} | ||
}); | ||
} | ||
|
||
if (authorization) { | ||
const hashedKey = authorization.split(' ').slice(-1)[0]; | ||
|
||
try { | ||
const plainKey = atob(hashedKey); | ||
|
||
let newUrl; | ||
|
||
if (url.indexOf('?') > -1) { | ||
newUrl = `${url}&_x_http_authorization=${plainKey}`; | ||
} else { | ||
newUrl = `${url}?_x_http_authorization=${plainKey}`; | ||
} | ||
|
||
return newUrl; | ||
} catch (e) { | ||
// atob choked on non-encoded data. Therefore, not a form of auth we | ||
// support. | ||
// | ||
// NOOP | ||
// | ||
} | ||
} | ||
|
||
return url; | ||
} | ||
|
||
function ie9Ajax(method, url, opts) { | ||
return new Promise(function (resolve, reject) { | ||
const xdr = new XDomainRequest(); | ||
|
||
xdr.onload = function () { | ||
try { | ||
const json = JSON.parse(xdr.responseText); | ||
|
||
resolve({ json, originalResponse: xdr, isJSON: true }); | ||
} catch (e) { | ||
resolve({ text: xdr.responseText, originalResponse: xdr, isText: true }); | ||
} | ||
}; | ||
|
||
function handleError() { | ||
reject(new Error('There was an error with the XDR')); | ||
} | ||
|
||
xdr.onerror = handleError; | ||
xdr.ontimeout = handleError; | ||
|
||
xdr.open(method, authToUrl(url, opts)); | ||
xdr.send(opts.data); | ||
}); | ||
} | ||
|
||
export default ie9Ajax; |