xhr.js
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Implements XMLHttpRequest.
// See http://www.w3.org/TR/XMLHttpRequest/#the-abort()-method
'use strict';
var _createClass = require('babel-runtime/helpers/create-class')['default'];
var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
var DOM = require('./dom');
var Fetch = require('./fetch');
var ms = require('ms');
var URL = require('url');
var Utils = require('jsdom/lib/jsdom/utils');
var EventTarget = require('jsdom/lib/jsdom/living/generated/EventTarget');
var DOMException = DOM.DOMException;
var XMLHttpRequest = (function () {
//class XMLHttpRequest extends EventTarget {
function XMLHttpRequest(window) {
_classCallCheck(this, XMLHttpRequest);
//super();
EventTarget.setup(this);
for (var method in EventTarget['interface'].prototype) {
this[method] = EventTarget['interface'].prototype[method];
}this._window = window;
this._browser = window.browser;
// Pending request
this._pending = null;
// Response headers
this.readyState = XMLHttpRequest.UNSENT;
this.onreadystatechange = null;
this.timeout = 0;
// XHR events need the first to dispatch, the second to propagate up to window
this._ownerDocument = window.document;
}
// Lifecycle states
// Aborts the request if it has already been sent.
_createClass(XMLHttpRequest, [{
key: 'abort',
value: function abort() {
var request = this._pending;
var sent = !!request;
if (this.readyState === XMLHttpRequest.UNSENT || this.readyState === XMLHttpRequest.OPENED && !sent) {
this.readyState = XMLHttpRequest.UNSENT;
return;
}
// Aborting a done request sets its readyState to UNSENT and does not trigger a readystatechange event
// https://xhr.spec.whatwg.org/#the-abort()-method
if (this.readyState === XMLHttpRequest.DONE) {
this.readyState = XMLHttpRequest.UNSENT;
} else {
// Tell any pending request it has been aborted.
request.aborted = true;
}
this._response = null;
this._error = null;
this._pending = null;
}
// Initializes a request.
//
// Calling this method an already active request (one for which open()or
// openRequest()has already been called) is the equivalent of calling abort().
}, {
key: 'open',
value: function open(method, url, useAsync, user, password) {
// jshint ignore:line
if (useAsync === false) throw new DOMException(DOMException.NOT_SUPPORTED_ERR, 'Zombie does not support synchronous XHR requests');
// Abort any pending request.
this.abort();
// Check supported HTTP method
this._method = method.toUpperCase();
if (/^(CONNECT|TRACE|TRACK)$/.test(this._method)) throw new DOMException(DOMException.SECURITY_ERR, 'Unsupported HTTP method');
if (!/^(DELETE|GET|HEAD|OPTIONS|POST|PUT)$/.test(this._method)) throw new DOMException(DOMException.SYNTAX_ERR, 'Unsupported HTTP method');
var headers = new Fetch.Headers();
// Normalize the URL and check security
url = URL.parse(Utils.resolveHref(this._window.location.href, url));
// Don't consider port if they are standard for http and https
if (url.protocol === 'https:' && url.port === '443' || url.protocol === 'http:' && url.port === '80') delete url.port;
if (!/^https?:$/i.test(url.protocol)) throw new DOMException(DOMException.NOT_SUPPORTED_ERR, 'Only HTTP/S protocol supported');
url.hostname = url.hostname || this._window.location.hostname;
url.host = url.port ? url.hostname + ':' + url.port : url.hostname;
if (url.host !== this._window.location.host) {
headers.set('Origin', this._window.location.protocol + '//' + this._window.location.host);
this._cors = headers.get('Origin');
}
url.hash = null;
if (user) url.auth = user + ':' + password;
// Used for logging requests
this._url = URL.format(url);
this._headers = headers;
// Reset response status
this._stateChanged(XMLHttpRequest.OPENED);
}
// Sets the value of an HTTP request header.You must call setRequestHeader()
// after open(), but before send().
}, {
key: 'setRequestHeader',
value: function setRequestHeader(header, value) {
if (this.readyState !== XMLHttpRequest.OPENED) throw new DOMException(DOMException.INVALID_STATE_ERR, 'Invalid state');
this._headers.set(header, value);
}
// Sends the request. If the request is asynchronous (which is the default),
// this method returns as soon as the request is sent. If the request is
// synchronous, this method doesn't return until the response has arrived.
}, {
key: 'send',
value: function send(data) {
var _this = this;
// Request must be opened.
if (this.readyState !== XMLHttpRequest.OPENED) throw new DOMException(DOMException.INVALID_STATE_ERR, 'Invalid state');
var request = new Fetch.Request(this._url, {
method: this._method,
headers: this._headers,
body: data
});
this._pending = request;
this._fire('loadstart');
var timeout = setTimeout(function () {
if (_this._pending === request) _this._pending = null;
request.timedOut = true;
_this._stateChanged(XMLHttpRequest.DONE);
_this._fire('progress');
_this._error = new DOMException(DOMException.TIMEOUT_ERR, 'The request timed out');
_this._fire('timeout', _this._error);
_this._fire('loadend');
_this._browser.errors.push(_this._error);
}, this.timeout || ms('2m'));
this._window._eventQueue.http(request, function (error, response) {
// Request already timed-out, nothing to do
if (request.timedOut) return;
clearTimeout(timeout);
if (_this._pending === request) _this._pending = null;
// Request aborted
if (request.aborted) {
_this._stateChanged(XMLHttpRequest.DONE);
_this._fire('progress');
_this._error = new DOMException(DOMException.ABORT_ERR, 'Request aborted');
_this._fire('abort', _this._error);
return;
}
// If not aborted, then we look at networking error
if (error) {
_this._stateChanged(XMLHttpRequest.DONE);
_this._fire('progress');
_this._error = new DOMException(DOMException.NETWORK_ERR);
_this._fire('error', _this._error);
_this._fire('loadend');
_this._browser.errors.push(_this._error);
return;
}
// CORS request, check origin, may lead to new error
if (_this._cors) {
var allowedOrigin = response.headers.get('Access-Control-Allow-Origin');
if (!(allowedOrigin === '*' || allowedOrigin === _this._cors)) {
_this._error = new DOMException(DOMException.SECURITY_ERR, 'Cannot make request to different domain');
_this._browser.errors.push(_this._error);
_this._stateChanged(XMLHttpRequest.DONE);
_this._fire('progress');
_this._fire('error', _this._error);
_this._fire('loadend');
_this.raise('error', _this._error.message, { exception: _this._error });
return;
}
}
// Store the response so getters have acess access it
_this._response = response;
// We have a one-stop implementation that goes through all the state
// transitions
_this._stateChanged(XMLHttpRequest.HEADERS_RECEIVED);
_this._stateChanged(XMLHttpRequest.LOADING);
var done = _this._window._eventQueue.waitForCompletion();
response.text().then(function (text) {
_this.responseText = text;
_this._stateChanged(XMLHttpRequest.DONE);
_this._fire('progress');
_this._fire('load');
_this._fire('loadend');
done();
});
});
request.sent = true;
}
}, {
key: 'getResponseHeader',
value: function getResponseHeader(name) {
// Returns the string containing the text of the specified header, or null if
// either the response has not yet been received or the header doesn't exist in
// the response.
return this._response && this._response.headers.get(name) || null;
}
}, {
key: 'getAllResponseHeaders',
value: function getAllResponseHeaders() {
// Returns all the response headers as a string, or null if no response has
// been received. Note: For multipart requests, this returns the headers from
// the current part of the request, not from the original channel.
if (this._response)
// XHR's getAllResponseHeaders, against all reason, returns a multi-line
// string. See http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method
return this._response.headers.toString();else return null;
}
// Fire onreadystatechange event
}, {
key: '_stateChanged',
value: function _stateChanged(newState) {
this.readyState = newState;
this._fire('readystatechange');
}
// Fire the named event on this object
}, {
key: '_fire',
value: function _fire(eventName, error) {
var event = new DOM.Event('xhr');
event.initEvent(eventName, true, true);
event.error = error;
this.dispatchEvent(event);
this._browser.emit('xhr', eventName, this._url);
}
// Raise error coming from jsdom
}, {
key: 'raise',
value: function raise(type, message, data) {
this._ownerDocument.raise(type, message, data);
}
}, {
key: 'status',
get: function get() {
// Status code/headers available immediately, 0 if request errored
return this._response ? this._response.status : this._error ? 0 : null;
}
}, {
key: 'statusText',
get: function get() {
// Status code/headers available immediately, '' if request errored
return this._response ? this._response.statusText : this._error ? '' : null;
}
}, {
key: 'responseXML',
get: function get() {
// Not implemented yet
return null;
}
}]);
return XMLHttpRequest;
})();
XMLHttpRequest.UNSENT = 0;
XMLHttpRequest.OPENED = 1;
XMLHttpRequest.HEADERS_RECEIVED = 2;
XMLHttpRequest.LOADING = 3;
XMLHttpRequest.DONE = 4;
module.exports = XMLHttpRequest;
//# sourceMappingURL=xhr.js.map