1 | var FormData = require('form-data');
|
2 |
|
3 | (function () {
|
4 | var InvalidStateError, NetworkError, ProgressEvent, SecurityError, SyntaxError, XMLHttpRequest, XMLHttpRequestEventTarget, XMLHttpRequestUpload, http, https, os, url,
|
5 | extend = function (child, parent) {
|
6 | for (var key in parent) {
|
7 | if (hasProp.call(parent, key)) {
|
8 | child[key] = parent[key];
|
9 | }
|
10 | }
|
11 | function ctor() {
|
12 | this.constructor = child;
|
13 | }
|
14 |
|
15 | ctor.prototype = parent.prototype;
|
16 | child.prototype = new ctor();
|
17 | child.__super__ = parent.prototype;
|
18 | return child;
|
19 | },
|
20 | hasProp = {}.hasOwnProperty;
|
21 |
|
22 | XMLHttpRequestEventTarget = (function () {
|
23 | function XMLHttpRequestEventTarget() {
|
24 | this.onloadstart = null;
|
25 | this.onprogress = null;
|
26 | this.onabort = null;
|
27 | this.onerror = null;
|
28 | this.onload = null;
|
29 | this.ontimeout = null;
|
30 | this.onloadend = null;
|
31 | this._listeners = {};
|
32 | }
|
33 |
|
34 | XMLHttpRequestEventTarget.prototype.onloadstart = null;
|
35 |
|
36 | XMLHttpRequestEventTarget.prototype.onprogress = null;
|
37 |
|
38 | XMLHttpRequestEventTarget.prototype.onabort = null;
|
39 |
|
40 | XMLHttpRequestEventTarget.prototype.onerror = null;
|
41 |
|
42 | XMLHttpRequestEventTarget.prototype.onload = null;
|
43 |
|
44 | XMLHttpRequestEventTarget.prototype.ontimeout = null;
|
45 |
|
46 | XMLHttpRequestEventTarget.prototype.onloadend = null;
|
47 |
|
48 | XMLHttpRequestEventTarget.prototype.addEventListener = function (eventType, listener) {
|
49 | var base;
|
50 | eventType = eventType.toLowerCase();
|
51 | (base = this._listeners)[eventType] || (base[eventType] = []);
|
52 | this._listeners[eventType].push(listener);
|
53 | return void 0;
|
54 | };
|
55 |
|
56 | XMLHttpRequestEventTarget.prototype.removeEventListener = function (eventType, listener) {
|
57 | var index;
|
58 | eventType = eventType.toLowerCase();
|
59 | if (this._listeners[eventType]) {
|
60 | index = this._listeners[eventType].indexOf(listener);
|
61 | if (index !== -1) {
|
62 | this._listeners[eventType].splice(index, 1);
|
63 | }
|
64 | }
|
65 | return void 0;
|
66 | };
|
67 |
|
68 | XMLHttpRequestEventTarget.prototype.dispatchEvent = function (event) {
|
69 | var eventType, j, len, listener, listeners;
|
70 | event.currentTarget = event.target = this;
|
71 | eventType = event.type;
|
72 | if (listeners = this._listeners[eventType]) {
|
73 | for (j = 0, len = listeners.length; j < len; j++) {
|
74 | listener = listeners[j];
|
75 | listener.call(this, event);
|
76 | }
|
77 | }
|
78 | if (listener = this["on" + eventType]) {
|
79 | listener.call(this, event);
|
80 | }
|
81 | return void 0;
|
82 | };
|
83 |
|
84 | return XMLHttpRequestEventTarget;
|
85 |
|
86 | })();
|
87 |
|
88 | http = require('http');
|
89 |
|
90 | https = require('https');
|
91 |
|
92 | os = require('os');
|
93 |
|
94 | url = require('url');
|
95 |
|
96 | XMLHttpRequest = (function (superClass) {
|
97 | extend(XMLHttpRequest, superClass);
|
98 |
|
99 | function XMLHttpRequest(options) {
|
100 | XMLHttpRequest.__super__.constructor.call(this);
|
101 | this.onreadystatechange = null;
|
102 | this._anonymous = options && options.anon;
|
103 | this.readyState = XMLHttpRequest.UNSENT;
|
104 | this.response = null;
|
105 | this.responseText = '';
|
106 | this.responseType = '';
|
107 | this.status = 0;
|
108 | this.statusText = '';
|
109 | this.timeout = 0;
|
110 | this.upload = new XMLHttpRequestUpload(this);
|
111 | this._method = null;
|
112 | this._url = null;
|
113 | this._sync = false;
|
114 | this._headers = null;
|
115 | this._loweredHeaders = null;
|
116 | this._mimeOverride = null;
|
117 | this._request = null;
|
118 | this._response = null;
|
119 | this._responseParts = null;
|
120 | this._responseHeaders = null;
|
121 | this._aborting = null;
|
122 | this._error = null;
|
123 | this._loadedBytes = 0;
|
124 | this._totalBytes = 0;
|
125 | this._lengthComputable = false;
|
126 | }
|
127 |
|
128 | XMLHttpRequest.prototype.onreadystatechange = null;
|
129 |
|
130 | XMLHttpRequest.prototype.readyState = null;
|
131 |
|
132 | XMLHttpRequest.prototype.response = null;
|
133 |
|
134 | XMLHttpRequest.prototype.responseText = null;
|
135 |
|
136 | XMLHttpRequest.prototype.responseType = null;
|
137 |
|
138 | XMLHttpRequest.prototype.status = null;
|
139 |
|
140 | XMLHttpRequest.prototype.timeout = null;
|
141 |
|
142 | XMLHttpRequest.prototype.upload = null;
|
143 |
|
144 | XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
|
145 | var xhrUrl;
|
146 | method = method.toUpperCase();
|
147 | if (method in this._restrictedMethods) {
|
148 | throw new SecurityError("HTTP method " + method + " is not allowed in XHR");
|
149 | }
|
150 | xhrUrl = this._parseUrl(url);
|
151 | if (async === void 0) {
|
152 | async = true;
|
153 | }
|
154 | switch (this.readyState) {
|
155 | case XMLHttpRequest.UNSENT:
|
156 | case XMLHttpRequest.OPENED:
|
157 | case XMLHttpRequest.DONE:
|
158 | null;
|
159 | break;
|
160 | case XMLHttpRequest.HEADERS_RECEIVED:
|
161 | case XMLHttpRequest.LOADING:
|
162 | null;
|
163 | }
|
164 | this._method = method;
|
165 | this._url = xhrUrl;
|
166 | this._sync = !async;
|
167 | this._headers = {};
|
168 | this._loweredHeaders = {};
|
169 | this._mimeOverride = null;
|
170 | this._setReadyState(XMLHttpRequest.OPENED);
|
171 | this._request = null;
|
172 | this._response = null;
|
173 | this.status = 0;
|
174 | this.statusText = '';
|
175 | this._responseParts = [];
|
176 | this._responseHeaders = null;
|
177 | this._loadedBytes = 0;
|
178 | this._totalBytes = 0;
|
179 | this._lengthComputable = false;
|
180 | return void 0;
|
181 | };
|
182 |
|
183 | XMLHttpRequest.prototype.setRequestHeader = function (name, value) {
|
184 | var loweredName;
|
185 | if (this.readyState !== XMLHttpRequest.OPENED) {
|
186 | throw new InvalidStateError("XHR readyState must be OPENED");
|
187 | }
|
188 | loweredName = name.toLowerCase();
|
189 | if (this._restrictedHeaders[loweredName] || /^sec\-/.test(loweredName) || /^proxy-/.test(loweredName)) {
|
190 | return void 0;
|
191 | }
|
192 | value = value.toString();
|
193 | if (loweredName in this._loweredHeaders) {
|
194 | name = this._loweredHeaders[loweredName];
|
195 | this._headers[name] = this._headers[name] + ', ' + value;
|
196 | } else {
|
197 | this._loweredHeaders[loweredName] = name;
|
198 | this._headers[name] = value;
|
199 | }
|
200 | return void 0;
|
201 | };
|
202 |
|
203 | XMLHttpRequest.prototype.send = function (data) {
|
204 | if (this.readyState !== XMLHttpRequest.OPENED) {
|
205 | throw new InvalidStateError("XHR readyState must be OPENED");
|
206 | }
|
207 | if (this._request) {
|
208 | throw new InvalidStateError("send() already called");
|
209 | }
|
210 | switch (this._url.protocol) {
|
211 | case 'file:':
|
212 | this._sendFile(data);
|
213 | break;
|
214 | case 'http:':
|
215 | case 'https:':
|
216 | this._sendHttp(data);
|
217 | break;
|
218 | default:
|
219 | throw new NetworkError("Unsupported protocol " + this._url.protocol);
|
220 | }
|
221 | return void 0;
|
222 | };
|
223 |
|
224 | XMLHttpRequest.prototype.abort = function () {
|
225 | if (!this._request) {
|
226 | return;
|
227 | }
|
228 | this._request.abort();
|
229 | this._setError();
|
230 | this._dispatchProgress('abort');
|
231 | this._dispatchProgress('loadend');
|
232 | return void 0;
|
233 | };
|
234 |
|
235 | XMLHttpRequest.prototype.getResponseHeader = function (name) {
|
236 | var loweredName;
|
237 | if (!this._responseHeaders) {
|
238 | return null;
|
239 | }
|
240 | loweredName = name.toLowerCase();
|
241 | if (loweredName in this._responseHeaders) {
|
242 | return this._responseHeaders[loweredName];
|
243 | } else {
|
244 | return null;
|
245 | }
|
246 | };
|
247 |
|
248 | XMLHttpRequest.prototype.getAllResponseHeaders = function () {
|
249 | var lines, name, value;
|
250 | if (!this._responseHeaders) {
|
251 | return '';
|
252 | }
|
253 | lines = (function () {
|
254 | var ref, results;
|
255 | ref = this._responseHeaders;
|
256 | results = [];
|
257 | for (name in ref) {
|
258 | value = ref[name];
|
259 | results.push(name + ": " + value);
|
260 | }
|
261 | return results;
|
262 | }).call(this);
|
263 | return lines.join("\r\n");
|
264 | };
|
265 |
|
266 | XMLHttpRequest.prototype.overrideMimeType = function (newMimeType) {
|
267 | if (this.readyState === XMLHttpRequest.LOADING || this.readyState === XMLHttpRequest.DONE) {
|
268 | throw new InvalidStateError("overrideMimeType() not allowed in LOADING or DONE");
|
269 | }
|
270 | this._mimeOverride = newMimeType.toLowerCase();
|
271 | return void 0;
|
272 | };
|
273 |
|
274 | XMLHttpRequest.prototype.nodejsSet = function (options) {
|
275 | var baseUrl, parsedUrl;
|
276 | if ('httpAgent' in options) {
|
277 | this.nodejsHttpAgent = options.httpAgent;
|
278 | }
|
279 | if ('httpsAgent' in options) {
|
280 | this.nodejsHttpsAgent = options.httpsAgent;
|
281 | }
|
282 | if ('baseUrl' in options) {
|
283 | baseUrl = options.baseUrl;
|
284 | if (baseUrl !== null) {
|
285 | parsedUrl = url.parse(baseUrl, false, true);
|
286 | if (!parsedUrl.protocol) {
|
287 | throw new SyntaxError("baseUrl must be an absolute URL");
|
288 | }
|
289 | }
|
290 | this.nodejsBaseUrl = baseUrl;
|
291 | }
|
292 | return void 0;
|
293 | };
|
294 |
|
295 | XMLHttpRequest.nodejsSet = function (options) {
|
296 | XMLHttpRequest.prototype.nodejsSet(options);
|
297 | return void 0;
|
298 | };
|
299 |
|
300 | XMLHttpRequest.prototype.UNSENT = 0;
|
301 |
|
302 | XMLHttpRequest.UNSENT = 0;
|
303 |
|
304 | XMLHttpRequest.prototype.OPENED = 1;
|
305 |
|
306 | XMLHttpRequest.OPENED = 1;
|
307 |
|
308 | XMLHttpRequest.prototype.HEADERS_RECEIVED = 2;
|
309 |
|
310 | XMLHttpRequest.HEADERS_RECEIVED = 2;
|
311 |
|
312 | XMLHttpRequest.prototype.LOADING = 3;
|
313 |
|
314 | XMLHttpRequest.LOADING = 3;
|
315 |
|
316 | XMLHttpRequest.prototype.DONE = 4;
|
317 |
|
318 | XMLHttpRequest.DONE = 4;
|
319 |
|
320 | XMLHttpRequest.prototype.nodejsHttpAgent = http.globalAgent;
|
321 |
|
322 | XMLHttpRequest.prototype.nodejsHttpsAgent = https.globalAgent;
|
323 |
|
324 | XMLHttpRequest.prototype.nodejsBaseUrl = null;
|
325 |
|
326 | XMLHttpRequest.prototype._restrictedMethods = {
|
327 | CONNECT: true,
|
328 | TRACE: true,
|
329 | TRACK: true
|
330 | };
|
331 |
|
332 | XMLHttpRequest.prototype._restrictedHeaders = {
|
333 | 'accept-charset': true,
|
334 | 'accept-encoding': true,
|
335 | 'access-control-request-headers': true,
|
336 | 'access-control-request-method': true,
|
337 | connection: true,
|
338 | 'content-length': true,
|
339 | cookie: true,
|
340 | cookie2: true,
|
341 | date: true,
|
342 | dnt: true,
|
343 | expect: true,
|
344 | host: true,
|
345 | 'keep-alive': true,
|
346 | origin: true,
|
347 | referer: true,
|
348 | te: true,
|
349 | trailer: true,
|
350 | 'transfer-encoding': true,
|
351 | upgrade: true,
|
352 | 'user-agent': true,
|
353 | via: true
|
354 | };
|
355 |
|
356 | XMLHttpRequest.prototype._privateHeaders = {
|
357 | 'set-cookie': false,
|
358 | 'set-cookie2': false
|
359 | };
|
360 |
|
361 | XMLHttpRequest.prototype._userAgent = ("Mozilla/5.0 (" + (os.type()) + " " + (os.arch()) + ") ") + ("node.js/" + process.versions.node + " v8/" + process.versions.v8);
|
362 |
|
363 | XMLHttpRequest.prototype._setReadyState = function (newReadyState) {
|
364 | var event;
|
365 | this.readyState = newReadyState;
|
366 | event = new ProgressEvent('readystatechange');
|
367 | this.dispatchEvent(event);
|
368 | return void 0;
|
369 | };
|
370 |
|
371 | XMLHttpRequest.prototype._sendFile = function () {
|
372 | if (this._url.method !== 'GET') {
|
373 | throw new NetworkError('The file protocol only supports GET');
|
374 | }
|
375 | throw new Error("Protocol file: not implemented");
|
376 | };
|
377 |
|
378 | XMLHttpRequest.prototype._sendHttp = function (data) {
|
379 | if (this._sync) {
|
380 | throw new Error("Synchronous XHR processing not implemented");
|
381 | }
|
382 | if ((data != null) && (this._method === 'GET' || this._method === 'HEAD')) {
|
383 | data = null;
|
384 | } else {
|
385 | data || (data = '');
|
386 | }
|
387 | this.upload._setData(data);
|
388 | this._finalizeHeaders(data);
|
389 | this._sendHxxpRequest(data);
|
390 | return void 0;
|
391 | };
|
392 |
|
393 | XMLHttpRequest.prototype._sendHxxpRequest = function (data) {
|
394 | var agent, hxxp, request;
|
395 | if (this._url.protocol === 'http:') {
|
396 | hxxp = http;
|
397 | agent = this.nodejsHttpAgent;
|
398 | } else {
|
399 | hxxp = https;
|
400 | agent = this.nodejsHttpsAgent;
|
401 | }
|
402 | request = hxxp.request({
|
403 | hostname: this._url.hostname,
|
404 | port: this._url.port,
|
405 | path: this._url.path,
|
406 | auth: this._url.auth,
|
407 | method: this._method,
|
408 | headers: this._headers,
|
409 | agent: agent
|
410 | });
|
411 |
|
412 | if (typeof data === 'object' && data instanceof FormData) {
|
413 | data.pipe(request);
|
414 | }
|
415 |
|
416 | this._request = request;
|
417 | if (this.timeout) {
|
418 | request.setTimeout(this.timeout, (function (_this) {
|
419 | return function () {
|
420 | return _this._onHttpTimeout(request);
|
421 | };
|
422 | })(this));
|
423 | }
|
424 | request.on('response', (function (_this) {
|
425 | return function (response) {
|
426 | return _this._onHttpResponse(request, response);
|
427 | };
|
428 | })(this));
|
429 | request.on('error', (function (_this) {
|
430 | return function (error) {
|
431 | return _this._onHttpRequestError(request, error);
|
432 | };
|
433 | })(this));
|
434 | this.upload._startUpload(request);
|
435 | if (this._request === request) {
|
436 | this._dispatchProgress('loadstart');
|
437 | }
|
438 | return void 0;
|
439 | };
|
440 |
|
441 | XMLHttpRequest.prototype._finalizeHeaders = function (data) {
|
442 | if (typeof data === 'object' && data instanceof FormData) {
|
443 | Object.assign(this._headers, data.getHeaders());
|
444 | }
|
445 | this._headers['Connection'] = 'keep-alive';
|
446 | this._headers['Host'] = this._url.host;
|
447 | if (this._anonymous) {
|
448 | this._headers['Referer'] = 'about:blank';
|
449 | }
|
450 | this._headers['User-Agent'] = this._userAgent;
|
451 | this.upload._finalizeHeaders(this._headers, this._loweredHeaders);
|
452 | return void 0;
|
453 | };
|
454 |
|
455 | XMLHttpRequest.prototype._onHttpResponse = function (request, response) {
|
456 | var lengthString;
|
457 | if (this._request !== request) {
|
458 | return;
|
459 | }
|
460 | switch (response.statusCode) {
|
461 | case 301:
|
462 | case 302:
|
463 | case 303:
|
464 | case 307:
|
465 | case 308:
|
466 | this._url = this._parseUrl(response.headers['location']);
|
467 | this._method = 'GET';
|
468 | if ('content-type' in this._loweredHeaders) {
|
469 | delete this._headers[this._loweredHeaders['content-type']];
|
470 | delete this._loweredHeaders['content-type'];
|
471 | }
|
472 | if ('Content-Type' in this._headers) {
|
473 | delete this._headers['Content-Type'];
|
474 | }
|
475 | delete this._headers['Content-Length'];
|
476 | this.upload._reset();
|
477 | this._finalizeHeaders();
|
478 | this._sendHxxpRequest();
|
479 | return;
|
480 | }
|
481 | this._response = response;
|
482 | this._response.on('data', (function (_this) {
|
483 | return function (data) {
|
484 | return _this._onHttpResponseData(response, data);
|
485 | };
|
486 | })(this));
|
487 | this._response.on('end', (function (_this) {
|
488 | return function () {
|
489 | return _this._onHttpResponseEnd(response);
|
490 | };
|
491 | })(this));
|
492 | this._response.on('close', (function (_this) {
|
493 | return function () {
|
494 | return _this._onHttpResponseClose(response);
|
495 | };
|
496 | })(this));
|
497 | this.status = this._response.statusCode;
|
498 | this.statusText = http.STATUS_CODES[this.status];
|
499 | this._parseResponseHeaders(response);
|
500 | if (lengthString = this._responseHeaders['content-length']) {
|
501 | this._totalBytes = parseInt(lengthString);
|
502 | this._lengthComputable = true;
|
503 | } else {
|
504 | this._lengthComputable = false;
|
505 | }
|
506 | return this._setReadyState(XMLHttpRequest.HEADERS_RECEIVED);
|
507 | };
|
508 |
|
509 | XMLHttpRequest.prototype._onHttpResponseData = function (response, data) {
|
510 | if (this._response !== response) {
|
511 | return;
|
512 | }
|
513 | this._responseParts.push(data);
|
514 | this._loadedBytes += data.length;
|
515 | if (this.readyState !== XMLHttpRequest.LOADING) {
|
516 | this._setReadyState(XMLHttpRequest.LOADING);
|
517 | }
|
518 | return this._dispatchProgress('progress');
|
519 | };
|
520 |
|
521 | XMLHttpRequest.prototype._onHttpResponseEnd = function (response) {
|
522 | if (this._response !== response) {
|
523 | return;
|
524 | }
|
525 | this._parseResponse();
|
526 | this._request = null;
|
527 | this._response = null;
|
528 | this._setReadyState(XMLHttpRequest.DONE);
|
529 | this._dispatchProgress('load');
|
530 | return this._dispatchProgress('loadend');
|
531 | };
|
532 |
|
533 | XMLHttpRequest.prototype._onHttpResponseClose = function (response) {
|
534 | var request;
|
535 | if (this._response !== response) {
|
536 | return;
|
537 | }
|
538 | request = this._request;
|
539 | this._setError();
|
540 | request.abort();
|
541 | this._setReadyState(XMLHttpRequest.DONE);
|
542 | this._dispatchProgress('error');
|
543 | return this._dispatchProgress('loadend');
|
544 | };
|
545 |
|
546 | XMLHttpRequest.prototype._onHttpTimeout = function (request) {
|
547 | if (this._request !== request) {
|
548 | return;
|
549 | }
|
550 | this._setError();
|
551 | request.abort();
|
552 | this._setReadyState(XMLHttpRequest.DONE);
|
553 | this._dispatchProgress('timeout');
|
554 | return this._dispatchProgress('loadend');
|
555 | };
|
556 |
|
557 | XMLHttpRequest.prototype._onHttpRequestError = function (request, error) {
|
558 | if (this._request !== request) {
|
559 | return;
|
560 | }
|
561 | this._setError();
|
562 | request.abort();
|
563 | this._setReadyState(XMLHttpRequest.DONE);
|
564 | this._dispatchProgress('error');
|
565 | return this._dispatchProgress('loadend');
|
566 | };
|
567 |
|
568 | XMLHttpRequest.prototype._dispatchProgress = function (eventType) {
|
569 | var event;
|
570 | event = new ProgressEvent(eventType);
|
571 | event.lengthComputable = this._lengthComputable;
|
572 | event.loaded = this._loadedBytes;
|
573 | event.total = this._totalBytes;
|
574 | this.dispatchEvent(event);
|
575 | return void 0;
|
576 | };
|
577 |
|
578 | XMLHttpRequest.prototype._setError = function () {
|
579 | this._request = null;
|
580 | this._response = null;
|
581 | this._responseHeaders = null;
|
582 | this._responseParts = null;
|
583 | return void 0;
|
584 | };
|
585 |
|
586 | XMLHttpRequest.prototype._parseUrl = function (urlString) {
|
587 | var absoluteUrlString, index, password, user, xhrUrl;
|
588 | if (this.nodejsBaseUrl === null) {
|
589 | absoluteUrlString = urlString;
|
590 | } else {
|
591 | absoluteUrlString = url.resolve(this.nodejsBaseUrl, urlString);
|
592 | }
|
593 | xhrUrl = url.parse(absoluteUrlString, false, true);
|
594 | xhrUrl.hash = null;
|
595 | if (xhrUrl.auth && ((typeof user !== "undefined" && user !== null) || (typeof password !== "undefined" && password !== null))) {
|
596 | index = xhrUrl.auth.indexOf(':');
|
597 | if (index === -1) {
|
598 | if (!user) {
|
599 | user = xhrUrl.auth;
|
600 | }
|
601 | } else {
|
602 | if (!user) {
|
603 | user = xhrUrl.substring(0, index);
|
604 | }
|
605 | if (!password) {
|
606 | password = xhrUrl.substring(index + 1);
|
607 | }
|
608 | }
|
609 | }
|
610 | if (user || password) {
|
611 | xhrUrl.auth = user + ":" + password;
|
612 | }
|
613 | return xhrUrl;
|
614 | };
|
615 |
|
616 | XMLHttpRequest.prototype._parseResponseHeaders = function (response) {
|
617 | var loweredName, name, ref, value;
|
618 | this._responseHeaders = {};
|
619 | ref = response.headers;
|
620 | for (name in ref) {
|
621 | value = ref[name];
|
622 | loweredName = name.toLowerCase();
|
623 | if (this._privateHeaders[loweredName]) {
|
624 | continue;
|
625 | }
|
626 | if (this._mimeOverride !== null && loweredName === 'content-type') {
|
627 | value = this._mimeOverride;
|
628 | }
|
629 | this._responseHeaders[loweredName] = value;
|
630 | }
|
631 | if (this._mimeOverride !== null && !('content-type' in this._responseHeaders)) {
|
632 | this._responseHeaders['content-type'] = this._mimeOverride;
|
633 | }
|
634 | return void 0;
|
635 | };
|
636 |
|
637 | XMLHttpRequest.prototype._parseResponse = function () {
|
638 | var arrayBuffer, buffer, i, j, jsonError, ref, view;
|
639 | if (Buffer.concat) {
|
640 | buffer = Buffer.concat(this._responseParts);
|
641 | } else {
|
642 | buffer = this._concatBuffers(this._responseParts);
|
643 | }
|
644 |
|
645 | if (this._responseHeaders['content-encoding'] && this._responseHeaders['content-encoding'] === 'gzip') {
|
646 | buffer = require('zlib').gunzipSync(buffer);
|
647 | }
|
648 | this._responseParts = null;
|
649 | switch (this.responseType) {
|
650 | case 'text':
|
651 | this._parseTextResponse(buffer);
|
652 | break;
|
653 | case 'json':
|
654 | this.responseText = null;
|
655 | try {
|
656 | this.response = JSON.parse(buffer.toString('utf-8'));
|
657 | } catch (_error) {
|
658 | jsonError = _error;
|
659 | this.response = null;
|
660 | }
|
661 | break;
|
662 | case 'buffer':
|
663 | this.responseText = null;
|
664 | this.response = buffer;
|
665 | break;
|
666 | case 'arraybuffer':
|
667 | this.responseText = null;
|
668 | arrayBuffer = new ArrayBuffer(buffer.length);
|
669 | view = new Uint8Array(arrayBuffer);
|
670 | for (i = j = 0, ref = buffer.length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
|
671 | view[i] = buffer[i];
|
672 | }
|
673 | this.response = arrayBuffer;
|
674 | break;
|
675 | default:
|
676 | this._parseTextResponse(buffer);
|
677 | }
|
678 | return void 0;
|
679 | };
|
680 |
|
681 | XMLHttpRequest.prototype._parseTextResponse = function (buffer) {
|
682 | var e;
|
683 | try {
|
684 | this.responseText = buffer.toString(this._parseResponseEncoding());
|
685 | } catch (_error) {
|
686 | e = _error;
|
687 | this.responseText = buffer.toString('binary');
|
688 | }
|
689 | this.response = this.responseText;
|
690 | return void 0;
|
691 | };
|
692 |
|
693 | XMLHttpRequest.prototype._parseResponseEncoding = function () {
|
694 | var contentType, encoding, match;
|
695 | encoding = null;
|
696 | if (contentType = this._responseHeaders['content-type']) {
|
697 | if (match = /\;\s*charset\=(.*)$/.exec(contentType)) {
|
698 | return match[1];
|
699 | }
|
700 | }
|
701 | return 'utf-8';
|
702 | };
|
703 |
|
704 | XMLHttpRequest.prototype._concatBuffers = function (buffers) {
|
705 | var buffer, j, k, len, len1, length, target;
|
706 | if (buffers.length === 0) {
|
707 | return Buffer.alloc(0);
|
708 | }
|
709 | if (buffers.length === 1) {
|
710 | return buffers[0];
|
711 | }
|
712 | length = 0;
|
713 | for (j = 0, len = buffers.length; j < len; j++) {
|
714 | buffer = buffers[j];
|
715 | length += buffer.length;
|
716 | }
|
717 | target = Buffer.alloc(length);
|
718 | length = 0;
|
719 | for (k = 0, len1 = buffers.length; k < len1; k++) {
|
720 | buffer = buffers[k];
|
721 | buffer.copy(target, length);
|
722 | length += buffer.length;
|
723 | }
|
724 | return target;
|
725 | };
|
726 |
|
727 | return XMLHttpRequest;
|
728 |
|
729 | })(XMLHttpRequestEventTarget);
|
730 |
|
731 | module.exports = XMLHttpRequest;
|
732 |
|
733 | XMLHttpRequest.XMLHttpRequest = XMLHttpRequest;
|
734 |
|
735 | SecurityError = (function (superClass) {
|
736 | extend(SecurityError, superClass);
|
737 |
|
738 | function SecurityError() {
|
739 | SecurityError.__super__.constructor.apply(this, arguments);
|
740 | }
|
741 |
|
742 | return SecurityError;
|
743 |
|
744 | })(Error);
|
745 |
|
746 | XMLHttpRequest.SecurityError = SecurityError;
|
747 |
|
748 | InvalidStateError = (function (superClass) {
|
749 | extend(InvalidStateError, superClass);
|
750 |
|
751 | function InvalidStateError() {
|
752 | InvalidStateError.__super__.constructor.apply(this, arguments);
|
753 | }
|
754 |
|
755 | return InvalidStateError;
|
756 |
|
757 | })(Error);
|
758 |
|
759 | InvalidStateError = (function (superClass) {
|
760 | extend(InvalidStateError, superClass);
|
761 |
|
762 | function InvalidStateError() {
|
763 | return InvalidStateError.__super__.constructor.apply(this, arguments);
|
764 | }
|
765 |
|
766 | return InvalidStateError;
|
767 |
|
768 | })(Error);
|
769 |
|
770 | XMLHttpRequest.InvalidStateError = InvalidStateError;
|
771 |
|
772 | NetworkError = (function (superClass) {
|
773 | extend(NetworkError, superClass);
|
774 |
|
775 | function NetworkError() {
|
776 | NetworkError.__super__.constructor.apply(this, arguments);
|
777 | }
|
778 |
|
779 | return NetworkError;
|
780 |
|
781 | })(Error);
|
782 |
|
783 | XMLHttpRequest.SyntaxError = SyntaxError;
|
784 |
|
785 | SyntaxError = (function (superClass) {
|
786 | extend(SyntaxError, superClass);
|
787 |
|
788 | function SyntaxError() {
|
789 | SyntaxError.__super__.constructor.apply(this, arguments);
|
790 | }
|
791 |
|
792 | return SyntaxError;
|
793 |
|
794 | })(Error);
|
795 |
|
796 | ProgressEvent = (function () {
|
797 | function ProgressEvent(type) {
|
798 | this.type = type;
|
799 | this.target = null;
|
800 | this.currentTarget = null;
|
801 | this.lengthComputable = false;
|
802 | this.loaded = 0;
|
803 | this.total = 0;
|
804 | }
|
805 |
|
806 | ProgressEvent.prototype.bubbles = false;
|
807 |
|
808 | ProgressEvent.prototype.cancelable = false;
|
809 |
|
810 | ProgressEvent.prototype.target = null;
|
811 |
|
812 | ProgressEvent.prototype.loaded = null;
|
813 |
|
814 | ProgressEvent.prototype.lengthComputable = null;
|
815 |
|
816 | ProgressEvent.prototype.total = null;
|
817 |
|
818 | return ProgressEvent;
|
819 |
|
820 | })();
|
821 |
|
822 | XMLHttpRequest.ProgressEvent = ProgressEvent;
|
823 |
|
824 | XMLHttpRequestUpload = (function (superClass) {
|
825 | extend(XMLHttpRequestUpload, superClass);
|
826 |
|
827 | function XMLHttpRequestUpload(request) {
|
828 | XMLHttpRequestUpload.__super__.constructor.call(this);
|
829 | this._request = request;
|
830 | this._reset();
|
831 | }
|
832 |
|
833 | XMLHttpRequestUpload.prototype._reset = function () {
|
834 | this._contentType = null;
|
835 | this._body = null;
|
836 | return void 0;
|
837 | };
|
838 |
|
839 | XMLHttpRequestUpload.prototype._setData = function (data) {
|
840 | var body, i, j, k, offset, ref, ref1, view;
|
841 | if (typeof data === 'undefined' || data === null) {
|
842 | return;
|
843 | }
|
844 | if (typeof data === 'string') {
|
845 | if (data.length !== 0) {
|
846 | this._contentType = 'text/plain;charset=UTF-8';
|
847 | }
|
848 | this._body = Buffer.from(data, 'utf8');
|
849 | } else if (Buffer.isBuffer(data)) {
|
850 | this._body = data;
|
851 | } else if (data instanceof ArrayBuffer) {
|
852 | body = Buffer.alloc(data.byteLength);
|
853 | view = new Uint8Array(data);
|
854 | for (i = j = 0, ref = data.byteLength; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
|
855 | body[i] = view[i];
|
856 | }
|
857 | this._body = body;
|
858 | } else if (data.buffer && data.buffer instanceof ArrayBuffer) {
|
859 | body = Buffer.alloc(data.byteLength);
|
860 | offset = data.byteOffset;
|
861 | view = new Uint8Array(data.buffer);
|
862 | for (i = k = 0, ref1 = data.byteLength; 0 <= ref1 ? k < ref1 : k > ref1; i = 0 <= ref1 ? ++k : --k) {
|
863 | body[i] = view[i + offset];
|
864 | }
|
865 | this._body = body;
|
866 | } else if (typeof data === 'object' && data instanceof FormData) {
|
867 | this._body = null;
|
868 | } else {
|
869 | throw new Error("Unsupported send() data " + data);
|
870 | }
|
871 | return void 0;
|
872 | };
|
873 |
|
874 | XMLHttpRequestUpload.prototype._finalizeHeaders = function (headers, loweredHeaders) {
|
875 | if (this._contentType) {
|
876 | if (!('content-type' in loweredHeaders)) {
|
877 | headers['Content-Type'] = this._contentType;
|
878 | }
|
879 | }
|
880 | if (this._body) {
|
881 | headers['Content-Length'] = this._body.length.toString();
|
882 | }
|
883 | return void 0;
|
884 | };
|
885 |
|
886 | XMLHttpRequestUpload.prototype._startUpload = function (request) {
|
887 | if (this._body) {
|
888 | request.write(this._body);
|
889 | }
|
890 | request.end();
|
891 | return void 0;
|
892 | };
|
893 |
|
894 | return XMLHttpRequestUpload;
|
895 |
|
896 | })(XMLHttpRequestEventTarget);
|
897 |
|
898 | XMLHttpRequest.XMLHttpRequestUpload = XMLHttpRequestUpload;
|
899 |
|
900 | }).call(this);
|