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