From 6aa766e5e92688dc8946902907dbcd3ee9278767 Mon Sep 17 00:00:00 2001 From: mzabriskie Date: Mon, 28 Sep 2015 18:33:46 -0600 Subject: [PATCH] Adding support for web workers, and react-native closes #70, closes #98 --- lib/adapters/xhr.js | 21 ++++++++++++++------- lib/helpers/cookies.js | 6 ++++++ lib/helpers/urlIsSameOrigin.js | 6 ++++++ lib/utils.js | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index e3de30e213..1a3ac77da1 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -5,10 +5,8 @@ var defaults = require('./../defaults'); var utils = require('./../utils'); var buildUrl = require('./../helpers/buildUrl'); -var cookies = require('./../helpers/cookies'); var parseHeaders = require('./../helpers/parseHeaders'); var transformData = require('./../helpers/transformData'); -var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin'); module.exports = function xhrAdapter(resolve, reject, config) { // Transform request data @@ -65,11 +63,20 @@ module.exports = function xhrAdapter(resolve, reject, config) { }; // Add xsrf header - var xsrfValue = urlIsSameOrigin(config.url) ? - cookies.read(config.xsrfCookieName || defaults.xsrfCookieName) : - undefined; - if (xsrfValue) { - requestHeaders[config.xsrfHeaderName || defaults.xsrfHeaderName] = xsrfValue; + // This is only done if running in a standard browser environment. + // Specifically not if we're in a web worker, or react-native. + if (utils.isStandardBrowserEnv()) { + var cookies = require('./../helpers/cookies'); + var urlIsSameOrigin = require('./../helpers/urlIsSameOrigin'); + + // Add xsrf header + var xsrfValue = urlIsSameOrigin(config.url) ? + cookies.read(config.xsrfCookieName || defaults.xsrfCookieName) : + undefined; + + if (xsrfValue) { + requestHeaders[config.xsrfHeaderName || defaults.xsrfHeaderName] = xsrfValue; + } } // Add headers to the request diff --git a/lib/helpers/cookies.js b/lib/helpers/cookies.js index c42b17731b..fc06e0d9cf 100644 --- a/lib/helpers/cookies.js +++ b/lib/helpers/cookies.js @@ -1,5 +1,11 @@ 'use strict'; +/** + * WARNING: + * This file makes references to objects that aren't safe in all environments. + * Please see lib/utils.isStandardBrowserEnv before including this file. + */ + var utils = require('./../utils'); module.exports = { diff --git a/lib/helpers/urlIsSameOrigin.js b/lib/helpers/urlIsSameOrigin.js index 962802249c..a10a26ce06 100644 --- a/lib/helpers/urlIsSameOrigin.js +++ b/lib/helpers/urlIsSameOrigin.js @@ -1,5 +1,11 @@ 'use strict'; +/** + * WARNING: + * This file makes references to objects that aren't safe in all environments. + * Please see lib/utils.isStandardBrowserEnv before including this file. + */ + var utils = require('./../utils'); var msie = /(msie|trident)/i.test(navigator.userAgent); var urlParsingNode = document.createElement('a'); diff --git a/lib/utils.js b/lib/utils.js index 3123b4158c..9dd728755e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -140,6 +140,27 @@ function isArguments(val) { return toString.call(val) === '[object Arguments]'; } +/** + * Determine if we're running in a standard browser environment + * + * This allows axios to run in a web worker, and react-native. + * Both environments support XMLHttpRequest, but not fully standard globals. + * + * web workers: + * typeof window -> undefined + * typeof document -> undefined + * + * react-native: + * typeof document.createelement -> undefined + */ +function isStandardBrowserEnv() { + return ( + typeof window !== 'undefined' && + typeof document !== 'undefined' && + typeof document.createElement === 'function' + ); +} + /** * Iterate over an Array or an Object invoking a function for each item. * @@ -221,6 +242,7 @@ module.exports = { isDate: isDate, isFile: isFile, isBlob: isBlob, + isStandardBrowserEnv: isStandardBrowserEnv, forEach: forEach, merge: merge, trim: trim