|
1 |
| -/* global XMLHttpRequest */ |
2 |
| -/* global process */ |
3 |
| -"use strict"; |
4 |
| - |
5 |
| -exports._ajax = function () { |
6 |
| - var platformSpecific = { }; |
7 |
| - if (typeof module !== "undefined" && module.require && !(typeof process !== "undefined" && process.versions["electron"])) { |
8 |
| - // We are on node.js |
9 |
| - platformSpecific.newXHR = function () { |
10 |
| - var XHR = module.require("xhr2"); |
11 |
| - return new XHR(); |
12 |
| - }; |
13 |
| - |
14 |
| - platformSpecific.fixupUrl = function (url, xhr) { |
15 |
| - if (xhr.nodejsBaseUrl === null) { |
16 |
| - var urllib = module.require("url"); |
17 |
| - var u = urllib.parse(url); |
18 |
| - u.protocol = u.protocol || "http:"; |
19 |
| - u.hostname = u.hostname || "localhost"; |
20 |
| - return urllib.format(u); |
21 |
| - } else { |
22 |
| - return url || "/"; |
| 1 | +export function _ajax(platformSpecificDriver, timeoutErrorMessageIdent, requestFailedMessageIdent, mkHeader, options) { |
| 2 | + return function (errback, callback) { |
| 3 | + var xhr = platformSpecificDriver.newXHR(); |
| 4 | + var fixedUrl = platformSpecificDriver.fixupUrl(options.url, xhr); |
| 5 | + xhr.open(options.method || "GET", fixedUrl, true, options.username, options.password); |
| 6 | + if (options.headers) { |
| 7 | + try { |
| 8 | + // eslint-disable-next-line no-eq-null,eqeqeq |
| 9 | + for (var i = 0, header; (header = options.headers[i]) != null; i++) { |
| 10 | + xhr.setRequestHeader(header.field, header.value); |
| 11 | + } |
| 12 | + } catch (e) { |
| 13 | + errback(e); |
23 | 14 | }
|
| 15 | + } |
| 16 | + var onerror = function (msgIdent) { |
| 17 | + return function () { |
| 18 | + errback(new Error(msgIdent)); |
| 19 | + }; |
24 | 20 | };
|
25 |
| - |
26 |
| - platformSpecific.getResponse = function (xhr) { |
27 |
| - return xhr.response; |
28 |
| - }; |
29 |
| - } else { |
30 |
| - // We are in the browser |
31 |
| - platformSpecific.newXHR = function () { |
32 |
| - return new XMLHttpRequest(); |
33 |
| - }; |
34 |
| - |
35 |
| - platformSpecific.fixupUrl = function (url) { |
36 |
| - return url || "/"; |
37 |
| - }; |
38 |
| - |
39 |
| - platformSpecific.getResponse = function (xhr) { |
40 |
| - return xhr.response; |
| 21 | + xhr.onerror = onerror(requestFailedMessageIdent); |
| 22 | + xhr.ontimeout = onerror(timeoutErrorMessageIdent); |
| 23 | + xhr.onload = function () { |
| 24 | + callback({ |
| 25 | + status: xhr.status, |
| 26 | + statusText: xhr.statusText, |
| 27 | + headers: xhr.getAllResponseHeaders().split("\r\n") |
| 28 | + .filter(function (header) { |
| 29 | + return header.length > 0; |
| 30 | + }) |
| 31 | + .map(function (header) { |
| 32 | + var i = header.indexOf(":"); |
| 33 | + return mkHeader(header.substring(0, i))(header.substring(i + 2)); |
| 34 | + }), |
| 35 | + body: xhr.response |
| 36 | + }); |
41 | 37 | };
|
42 |
| - } |
| 38 | + xhr.responseType = options.responseType; |
| 39 | + xhr.withCredentials = options.withCredentials; |
| 40 | + xhr.timeout = options.timeout; |
| 41 | + xhr.send(options.content); |
43 | 42 |
|
44 |
| - return function (timeoutErrorMessageIdent, requestFailedMessageIdent, mkHeader, options) { |
45 |
| - return function (errback, callback) { |
46 |
| - var xhr = platformSpecific.newXHR(); |
47 |
| - var fixedUrl = platformSpecific.fixupUrl(options.url, xhr); |
48 |
| - xhr.open(options.method || "GET", fixedUrl, true, options.username, options.password); |
49 |
| - if (options.headers) { |
50 |
| - try { |
51 |
| - // eslint-disable-next-line no-eq-null,eqeqeq |
52 |
| - for (var i = 0, header; (header = options.headers[i]) != null; i++) { |
53 |
| - xhr.setRequestHeader(header.field, header.value); |
54 |
| - } |
55 |
| - } catch (e) { |
56 |
| - errback(e); |
57 |
| - } |
| 43 | + return function (error, cancelErrback, cancelCallback) { |
| 44 | + try { |
| 45 | + xhr.abort(); |
| 46 | + } catch (e) { |
| 47 | + return cancelErrback(e); |
58 | 48 | }
|
59 |
| - var onerror = function (msgIdent) { |
60 |
| - return function () { |
61 |
| - errback(new Error(msgIdent)); |
62 |
| - }; |
63 |
| - }; |
64 |
| - xhr.onerror = onerror(requestFailedMessageIdent); |
65 |
| - xhr.ontimeout = onerror(timeoutErrorMessageIdent); |
66 |
| - xhr.onload = function () { |
67 |
| - callback({ |
68 |
| - status: xhr.status, |
69 |
| - statusText: xhr.statusText, |
70 |
| - headers: xhr.getAllResponseHeaders().split("\r\n") |
71 |
| - .filter(function (header) { |
72 |
| - return header.length > 0; |
73 |
| - }) |
74 |
| - .map(function (header) { |
75 |
| - var i = header.indexOf(":"); |
76 |
| - return mkHeader(header.substring(0, i))(header.substring(i + 2)); |
77 |
| - }), |
78 |
| - body: platformSpecific.getResponse(xhr) |
79 |
| - }); |
80 |
| - }; |
81 |
| - xhr.responseType = options.responseType; |
82 |
| - xhr.withCredentials = options.withCredentials; |
83 |
| - xhr.timeout = options.timeout; |
84 |
| - xhr.send(options.content); |
85 |
| - |
86 |
| - return function (error, cancelErrback, cancelCallback) { |
87 |
| - try { |
88 |
| - xhr.abort(); |
89 |
| - } catch (e) { |
90 |
| - return cancelErrback(e); |
91 |
| - } |
92 |
| - return cancelCallback(); |
93 |
| - }; |
| 49 | + return cancelCallback(); |
94 | 50 | };
|
95 | 51 | };
|
96 |
| -}(); |
| 52 | +} |
0 commit comments