engine-wrapper.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /******/ (function(modules) { // webpackBootstrap
  2. /******/ // The module cache
  3. /******/ var installedModules = {};
  4. /******/
  5. /******/ // The require function
  6. /******/ function __webpack_require__(moduleId) {
  7. /******/
  8. /******/ // Check if module is in cache
  9. /******/ if(installedModules[moduleId]) {
  10. /******/ return installedModules[moduleId].exports;
  11. /******/ }
  12. /******/ // Create a new module (and put it into the cache)
  13. /******/ var module = installedModules[moduleId] = {
  14. /******/ i: moduleId,
  15. /******/ l: false,
  16. /******/ exports: {}
  17. /******/ };
  18. /******/
  19. /******/ // Execute the module function
  20. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  21. /******/
  22. /******/ // Flag the module as loaded
  23. /******/ module.l = true;
  24. /******/
  25. /******/ // Return the exports of the module
  26. /******/ return module.exports;
  27. /******/ }
  28. /******/
  29. /******/
  30. /******/ // expose the modules object (__webpack_modules__)
  31. /******/ __webpack_require__.m = modules;
  32. /******/
  33. /******/ // expose the module cache
  34. /******/ __webpack_require__.c = installedModules;
  35. /******/
  36. /******/ // identity function for calling harmony imports with the correct context
  37. /******/ __webpack_require__.i = function(value) { return value; };
  38. /******/
  39. /******/ // define getter function for harmony exports
  40. /******/ __webpack_require__.d = function(exports, name, getter) {
  41. /******/ if(!__webpack_require__.o(exports, name)) {
  42. /******/ Object.defineProperty(exports, name, {
  43. /******/ configurable: false,
  44. /******/ enumerable: true,
  45. /******/ get: getter
  46. /******/ });
  47. /******/ }
  48. /******/ };
  49. /******/
  50. /******/ // getDefaultExport function for compatibility with non-harmony modules
  51. /******/ __webpack_require__.n = function(module) {
  52. /******/ var getter = module && module.__esModule ?
  53. /******/ function getDefault() { return module['default']; } :
  54. /******/ function getModuleExports() { return module; };
  55. /******/ __webpack_require__.d(getter, 'a', getter);
  56. /******/ return getter;
  57. /******/ };
  58. /******/
  59. /******/ // Object.prototype.hasOwnProperty.call
  60. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  61. /******/
  62. /******/ // __webpack_public_path__
  63. /******/ __webpack_require__.p = "";
  64. /******/
  65. /******/ // Load entry module and return exports
  66. /******/ return __webpack_require__(__webpack_require__.s = 4);
  67. /******/ })
  68. /************************************************************************/
  69. /******/ ([
  70. /* 0 */,
  71. /* 1 */
  72. /***/ (function(module, exports, __webpack_require__) {
  73. "use strict";
  74. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  75. module.exports = {
  76. type: function type(ob) {
  77. return Object.prototype.toString.call(ob).slice(8, -1).toLowerCase();
  78. },
  79. isObject: function isObject(ob, real) {
  80. if (real) {
  81. return this.type(ob) === "object";
  82. } else {
  83. return ob && (typeof ob === 'undefined' ? 'undefined' : _typeof(ob)) === 'object';
  84. }
  85. },
  86. isFormData: function isFormData(val) {
  87. return typeof FormData !== 'undefined' && val instanceof FormData;
  88. },
  89. trim: function trim(str) {
  90. return str.replace(/(^\s*)|(\s*$)/g, '');
  91. },
  92. encode: function encode(val) {
  93. return encodeURIComponent(val).replace(/%40/gi, '@').replace(/%3A/gi, ':').replace(/%24/g, '$').replace(/%2C/gi, ',').replace(/%20/g, '+').replace(/%5B/gi, '[').replace(/%5D/gi, ']');
  94. },
  95. formatParams: function formatParams(data) {
  96. var str = "";
  97. var first = true;
  98. var that = this;
  99. if (!this.isObject(data)) {
  100. return data;
  101. }
  102. function _encode(sub, path) {
  103. var encode = that.encode;
  104. var type = that.type(sub);
  105. if (type == "array") {
  106. sub.forEach(function (e, i) {
  107. if (!that.isObject(e)) i = "";
  108. _encode(e, path + ('%5B' + i + '%5D'));
  109. });
  110. } else if (type == "object") {
  111. for (var key in sub) {
  112. if (path) {
  113. _encode(sub[key], path + "%5B" + encode(key) + "%5D");
  114. } else {
  115. _encode(sub[key], encode(key));
  116. }
  117. }
  118. } else {
  119. if (!first) {
  120. str += "&";
  121. }
  122. first = false;
  123. str += path + "=" + encode(sub);
  124. }
  125. }
  126. _encode(data, "");
  127. return str;
  128. },
  129. // Do not overwrite existing attributes
  130. merge: function merge(a, b) {
  131. for (var key in b) {
  132. if (!a.hasOwnProperty(key)) {
  133. a[key] = b[key];
  134. } else if (this.isObject(b[key], 1) && this.isObject(a[key], 1)) {
  135. this.merge(a[key], b[key]);
  136. }
  137. }
  138. return a;
  139. }
  140. };
  141. /***/ }),
  142. /* 2 */,
  143. /* 3 */,
  144. /* 4 */
  145. /***/ (function(module, exports, __webpack_require__) {
  146. function KEEP(_,cb){cb();}
  147. "use strict";
  148. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  149. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  150. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  151. /*
  152. * author: wendu
  153. * email: 824783146@qq.com
  154. **/
  155. var util = __webpack_require__(1);
  156. var isBrowser = typeof document !== "undefined";
  157. //EngineWrapper can help generating a http engine quickly through a adapter
  158. function EngineWrapper(adapter) {
  159. var AjaxEngine = function () {
  160. function AjaxEngine() {
  161. _classCallCheck(this, AjaxEngine);
  162. this.requestHeaders = {};
  163. this.readyState = 0;
  164. this.timeout = 0; // 0 stands for no timeout
  165. this.responseURL = "";
  166. this.responseHeaders = {};
  167. }
  168. _createClass(AjaxEngine, [{
  169. key: "_call",
  170. value: function _call(name) {
  171. this[name] && this[name].apply(this, [].splice.call(arguments, 1));
  172. }
  173. }, {
  174. key: "_changeReadyState",
  175. value: function _changeReadyState(state) {
  176. this.readyState = state;
  177. this._call("onreadystatechange");
  178. }
  179. }, {
  180. key: "open",
  181. value: function open(method, url) {
  182. this.method = method;
  183. if (!url) {
  184. url = location.href;
  185. } else {
  186. url = util.trim(url);
  187. if (url.indexOf("http") !== 0) {
  188. // Normalize the request url
  189. if (isBrowser) {
  190. var t = document.createElement("a");
  191. t.href = url;
  192. url = t.href;
  193. }
  194. }
  195. }
  196. this.responseURL = url;
  197. this._changeReadyState(1);
  198. }
  199. }, {
  200. key: "send",
  201. value: function send(arg) {
  202. var _this = this;
  203. arg = arg || null;
  204. var self = this;
  205. if (adapter) {
  206. var request = {
  207. method: self.method,
  208. url: self.responseURL,
  209. headers: self.requestHeaders || {},
  210. body: arg
  211. };
  212. util.merge(request, self._options || {});
  213. if (request.method === "GET") {
  214. request.body = null;
  215. }
  216. self._changeReadyState(3);
  217. var timer = void 0;
  218. self.timeout = self.timeout || 0;
  219. if (self.timeout > 0) {
  220. timer = setTimeout(function () {
  221. if (self.readyState === 3) {
  222. _this._call("onloadend");
  223. self._changeReadyState(0);
  224. self._call("ontimeout");
  225. }
  226. }, self.timeout);
  227. }
  228. request.timeout = self.timeout;
  229. adapter(request, function (response) {
  230. function getAndDelete(key) {
  231. var t = response[key];
  232. delete response[key];
  233. return t;
  234. }
  235. // If the request has already timeout, return
  236. if (self.readyState !== 3) return;
  237. clearTimeout(timer);
  238. // Make sure the type of status is integer
  239. self.status = getAndDelete("statusCode") - 0;
  240. var responseText = getAndDelete("responseText");
  241. var statusMessage = getAndDelete("statusMessage");
  242. // Network error, set the status code 0
  243. if (!self.status) {
  244. self.statusText = responseText;
  245. self._call("onerror", { msg: statusMessage });
  246. } else {
  247. // Parsing the response headers to array in a object, because
  248. // there may be multiple values with the same header name
  249. var responseHeaders = getAndDelete("headers");
  250. var headers = {};
  251. for (var field in responseHeaders) {
  252. var value = responseHeaders[field];
  253. var key = field.toLowerCase();
  254. // Is array
  255. if ((typeof value === "undefined" ? "undefined" : _typeof(value)) === "object") {
  256. headers[key] = value;
  257. } else {
  258. headers[key] = headers[key] || [];
  259. headers[key].push(value);
  260. }
  261. }
  262. var cookies = headers["set-cookie"];
  263. if (isBrowser && cookies) {
  264. cookies.forEach(function (e) {
  265. // Remove the http-Only property of the cookie
  266. // so that JavaScript can operate it.
  267. document.cookie = e.replace(/;\s*httpOnly/ig, "");
  268. });
  269. }
  270. self.responseHeaders = headers;
  271. // Set the fields of engine from response
  272. self.statusText = statusMessage || "";
  273. self.response = self.responseText = responseText;
  274. self._response = response;
  275. self._changeReadyState(4);
  276. self._call("onload");
  277. }
  278. self._call("onloadend");
  279. });
  280. } else {
  281. console.error("Ajax require adapter");
  282. }
  283. }
  284. }, {
  285. key: "setRequestHeader",
  286. value: function setRequestHeader(key, value) {
  287. this.requestHeaders[util.trim(key)] = value;
  288. }
  289. }, {
  290. key: "getResponseHeader",
  291. value: function getResponseHeader(key) {
  292. return (this.responseHeaders[key.toLowerCase()] || "").toString() || null;
  293. }
  294. }, {
  295. key: "getAllResponseHeaders",
  296. value: function getAllResponseHeaders() {
  297. var str = "";
  298. for (var key in this.responseHeaders) {
  299. str += key + ":" + this.getResponseHeader(key) + "\r\n";
  300. }
  301. return str || null;
  302. }
  303. }, {
  304. key: "abort",
  305. value: function abort(msg) {
  306. this._changeReadyState(0);
  307. this._call("onerror", { msg: msg });
  308. this._call("onloadend");
  309. }
  310. }], [{
  311. key: "setAdapter",
  312. value: function setAdapter(requestAdapter) {
  313. adapter = requestAdapter;
  314. }
  315. }]);
  316. return AjaxEngine;
  317. }();
  318. return AjaxEngine;
  319. }
  320. // learn more about keep-loader: https://github.com/wendux/keep-loader
  321. KEEP("cdn||cdn-min", function () {
  322. // This code block will be removed besides the "CDN" and "cdn-min" build environment
  323. window.EngineWrapper = EngineWrapper;
  324. });
  325. module.exports = EngineWrapper;
  326. /***/ })
  327. /******/ ]);