1 | var _a, _b, _c;
|
2 | import * as http from '../http';
|
3 | import * as types from '../utils/types';
|
4 | import { Trace } from '../trace';
|
5 | var XMLHttpRequestResponseType;
|
6 | (function (XMLHttpRequestResponseType) {
|
7 | XMLHttpRequestResponseType.empty = '';
|
8 | XMLHttpRequestResponseType.text = 'text';
|
9 | XMLHttpRequestResponseType.json = 'json';
|
10 | XMLHttpRequestResponseType.blob = 'blob';
|
11 | XMLHttpRequestResponseType.arraybuffer = 'arraybuffer';
|
12 | })(XMLHttpRequestResponseType || (XMLHttpRequestResponseType = {}));
|
13 | export class XMLHttpRequest {
|
14 | get upload() {
|
15 | return this;
|
16 | }
|
17 | get readyState() {
|
18 | return this._readyState;
|
19 | }
|
20 | get responseType() {
|
21 | return this._responseType;
|
22 | }
|
23 | set responseType(value) {
|
24 | if (value === XMLHttpRequestResponseType.empty || value in XMLHttpRequestResponseType) {
|
25 | this._responseType = value;
|
26 | }
|
27 | else {
|
28 | throw new Error(`Response type of '${value}' not supported.`);
|
29 | }
|
30 | }
|
31 | get responseText() {
|
32 | if (this._responseType !== XMLHttpRequestResponseType.empty && this._responseType !== XMLHttpRequestResponseType.text) {
|
33 | throw new Error("Failed to read the 'responseText' property from 'XMLHttpRequest': " + "The value is only accessible if the object's 'responseType' is '' or 'text' " + `(was '${this._responseType}').`);
|
34 | }
|
35 | if (types.isFunction(this._responseTextReader)) {
|
36 | return this._responseTextReader();
|
37 | }
|
38 | return '';
|
39 | }
|
40 | get response() {
|
41 | if (this._responseType === XMLHttpRequestResponseType.empty || this._responseType === XMLHttpRequestResponseType.text) {
|
42 | if (this._readyState !== this.LOADING && this._readyState !== this.DONE) {
|
43 | return '';
|
44 | }
|
45 | else {
|
46 | return this._response;
|
47 | }
|
48 | }
|
49 | else {
|
50 | if (this._readyState !== this.DONE) {
|
51 | return null;
|
52 | }
|
53 | else {
|
54 | return this._response;
|
55 | }
|
56 | }
|
57 | }
|
58 | get status() {
|
59 | return this._status;
|
60 | }
|
61 | get statusText() {
|
62 | if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._errorFlag) {
|
63 | return '';
|
64 | }
|
65 | return statuses[this._status];
|
66 | }
|
67 | constructor() {
|
68 | this.UNSENT = 0;
|
69 | this.OPENED = 1;
|
70 | this.HEADERS_RECEIVED = 2;
|
71 | this.LOADING = 3;
|
72 | this.DONE = 4;
|
73 | this._responseType = '';
|
74 | this._listeners = new Map();
|
75 | this._readyState = this.UNSENT;
|
76 | }
|
77 | _loadResponse(r) {
|
78 | this._status = r.statusCode;
|
79 | this._headers = r.headers;
|
80 | this._setReadyState(this.HEADERS_RECEIVED);
|
81 | this._setReadyState(this.LOADING);
|
82 | this._responseTextReader = () => r.content.toString();
|
83 | const contentType = this.getResponseHeader('Content-Type');
|
84 | const mimeType = (contentType && contentType.toLowerCase()) || 'text/xml';
|
85 | const finalMimeType = this._overrideMimeType || mimeType;
|
86 | if (this._responseType === XMLHttpRequestResponseType.json) {
|
87 | this._response = r.content.toJSON();
|
88 | }
|
89 | else if (this._responseType === XMLHttpRequestResponseType.text || this._responseType === XMLHttpRequestResponseType.empty) {
|
90 | this._response = this.responseText;
|
91 | }
|
92 | else if (this._responseType === XMLHttpRequestResponseType.arraybuffer) {
|
93 | this._response = r.content.toArrayBuffer();
|
94 | }
|
95 | else if (this._responseType === XMLHttpRequestResponseType.blob) {
|
96 | this._response = new Blob([r.content.toArrayBuffer()], {
|
97 | type: finalMimeType,
|
98 | });
|
99 | }
|
100 | this.emitEvent('progress');
|
101 | this._sendFlag = false;
|
102 | this._setReadyState(this.DONE);
|
103 | }
|
104 | emitEvent(eventName, ...args) {
|
105 | if (types.isFunction(this['on' + eventName])) {
|
106 | this['on' + eventName](...args);
|
107 | }
|
108 | const handlers = this._listeners.get(eventName) || [];
|
109 | handlers.forEach((handler) => {
|
110 | handler(...args);
|
111 | });
|
112 | }
|
113 | _setReadyState(value) {
|
114 | if (this._readyState !== value) {
|
115 | this._readyState = value;
|
116 | this.emitEvent('readystatechange');
|
117 | }
|
118 | if (this._readyState === this.DONE) {
|
119 | this.emitEvent('load');
|
120 | this.emitEvent('loadend');
|
121 | }
|
122 | }
|
123 | _setRequestError(eventName, error) {
|
124 | this._readyState = this.DONE;
|
125 | this._response = error;
|
126 | this.emitEvent('readystatechange');
|
127 | this.emitEvent(eventName, error);
|
128 | this.emitEvent('loadend');
|
129 | }
|
130 | addEventListener(eventName, handler) {
|
131 | if (['abort', 'error', 'load', 'loadend', 'loadstart', 'progress', 'readystatechange'].indexOf(eventName) === -1) {
|
132 | if (Trace.isEnabled()) {
|
133 | Trace.write('XHR Event not supported: ' + eventName, Trace.categories.Debug, Trace.messageType.warn);
|
134 | }
|
135 | }
|
136 | const handlers = this._listeners.get(eventName) || [];
|
137 | handlers.push(handler);
|
138 | this._listeners.set(eventName, handlers);
|
139 | }
|
140 | removeEventListener(eventName, toDetach) {
|
141 | let handlers = this._listeners.get(eventName) || [];
|
142 | handlers = handlers.filter((handler) => handler !== toDetach);
|
143 | this._listeners.set(eventName, handlers);
|
144 | }
|
145 | open(method, url, async, user, password) {
|
146 | if (types.isString(method) && types.isString(url)) {
|
147 | this._options = { url: url, method: method };
|
148 | this._options.headers = {};
|
149 | if (types.isString(user)) {
|
150 | this._options.headers['user'] = user;
|
151 | }
|
152 | if (types.isString(password)) {
|
153 | this._options.headers['password'] = password;
|
154 | }
|
155 | this._setReadyState(this.OPENED);
|
156 | }
|
157 | }
|
158 | abort() {
|
159 | this._response = null;
|
160 | this._responseTextReader = null;
|
161 | this._headers = null;
|
162 | this._status = null;
|
163 | if ((this._readyState === this.OPENED && this._sendFlag) || this._readyState === this.HEADERS_RECEIVED || this._readyState === this.LOADING) {
|
164 | this._errorFlag = true;
|
165 | this._sendFlag = false;
|
166 | this._setRequestError('abort');
|
167 | }
|
168 | if (this._readyState === this.DONE) {
|
169 | this._readyState = this.UNSENT;
|
170 | }
|
171 | }
|
172 | send(data) {
|
173 | this._errorFlag = false;
|
174 | this._response = null;
|
175 | this._responseTextReader = null;
|
176 | this._headers = null;
|
177 | this._status = null;
|
178 | if (this._readyState !== this.OPENED || this._sendFlag) {
|
179 | throw new Error("Failed to execute 'send' on 'XMLHttpRequest': " + "The object's state must be OPENED.");
|
180 | }
|
181 | if (types.isString(data) && this._options.method !== 'GET') {
|
182 |
|
183 |
|
184 |
|
185 | this._options.content = data;
|
186 | }
|
187 | else if (data instanceof FormData) {
|
188 | this._options.content = data.toString();
|
189 | }
|
190 | else if (data instanceof Blob) {
|
191 | this.setRequestHeader('Content-Type', data.type);
|
192 | this._options.content = Blob.InternalAccessor.getBuffer(data);
|
193 | }
|
194 | else if (data instanceof ArrayBuffer) {
|
195 | this._options.content = data;
|
196 | }
|
197 | this._sendFlag = true;
|
198 | this.emitEvent('loadstart');
|
199 | http
|
200 | .request(this._options)
|
201 | .then((r) => {
|
202 | if (!this._errorFlag && this._sendFlag) {
|
203 | this._loadResponse(r);
|
204 | }
|
205 | })
|
206 | .catch((e) => {
|
207 | this._errorFlag = true;
|
208 | this._sendFlag = false;
|
209 | this._setRequestError('error', e);
|
210 | });
|
211 | }
|
212 | setRequestHeader(header, value) {
|
213 | if (this._readyState !== this.OPENED || this._sendFlag) {
|
214 | throw new Error("Failed to execute 'setRequestHeader' on 'XMLHttpRequest': " + "The object's state must be OPENED.");
|
215 | }
|
216 | if (types.isString(header) && types.isString(value)) {
|
217 | this._options.headers[header] = value;
|
218 | }
|
219 | }
|
220 | getAllResponseHeaders() {
|
221 | if (this._readyState < 2 || this._errorFlag) {
|
222 | return '';
|
223 | }
|
224 | let result = '';
|
225 | for (const i in this._headers) {
|
226 | result += i + ': ' + this._headers[i] + '\r\n';
|
227 | }
|
228 | return result.substr(0, result.length - 2);
|
229 | }
|
230 | getResponseHeader(header) {
|
231 | if (types.isString(header) && this._readyState > 1 && this._headers && !this._errorFlag) {
|
232 | header = header.toLowerCase();
|
233 | for (const i in this._headers) {
|
234 | if (i.toLowerCase() === header) {
|
235 | return this._headers[i];
|
236 | }
|
237 | }
|
238 | }
|
239 | return null;
|
240 | }
|
241 | overrideMimeType(mime) {
|
242 | if (this._readyState === this.LOADING || this._readyState === this.DONE) {
|
243 | throw new Error("Failed to execute 'overrideMimeType' on 'XMLHttpRequest': " + 'MimeType cannot be overridden when the state is LOADING or DONE.');
|
244 | }
|
245 | this._overrideMimeType = mime;
|
246 | }
|
247 | }
|
248 | const statuses = {
|
249 | 100: 'Continue',
|
250 | 101: 'Switching Protocols',
|
251 | 200: 'OK',
|
252 | 201: 'Created',
|
253 | 202: 'Accepted',
|
254 | 203: 'Non - Authoritative Information',
|
255 | 204: 'No Content',
|
256 | 205: 'Reset Content',
|
257 | 206: 'Partial Content',
|
258 | 300: 'Multiple Choices',
|
259 | 301: 'Moved Permanently',
|
260 | 302: 'Found',
|
261 | 303: 'See Other',
|
262 | 304: 'Not Modified',
|
263 | 305: 'Use Proxy',
|
264 | 307: 'Temporary Redirect',
|
265 | 400: 'Bad Request',
|
266 | 401: 'Unauthorized',
|
267 | 402: 'Payment Required',
|
268 | 403: 'Forbidden',
|
269 | 404: 'Not Found',
|
270 | 405: 'Method Not Allowed',
|
271 | 406: 'Not Acceptable',
|
272 | 407: 'Proxy Authentication Required',
|
273 | 408: 'Request Timeout',
|
274 | 409: 'Conflict',
|
275 | 410: 'Gone',
|
276 | 411: 'Length Required',
|
277 | 412: 'Precondition Failed',
|
278 | 413: 'Request Entity Too Large',
|
279 | 414: 'Request - URI Too Long',
|
280 | 415: 'Unsupported Media Type',
|
281 | 416: 'Requested Range Not Satisfiable',
|
282 | 417: 'Expectation Failed',
|
283 | 500: 'Internal Server Error',
|
284 | 501: 'Not Implemented',
|
285 | 502: 'Bad Gateway',
|
286 | 503: 'Service Unavailable',
|
287 | 504: 'Gateway Timeout',
|
288 | 505: 'HTTP Version Not Supported',
|
289 | };
|
290 | export class FormData {
|
291 | constructor() {
|
292 | this._data = new Map();
|
293 | }
|
294 | append(name, value) {
|
295 | this._data.set(name, value);
|
296 | }
|
297 | toString() {
|
298 | const arr = new Array();
|
299 | this._data.forEach(function (value, name, map) {
|
300 | arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
|
301 | });
|
302 | return arr.join('&');
|
303 | }
|
304 | }
|
305 | export class Blob {
|
306 | get size() {
|
307 | return this._size;
|
308 | }
|
309 | get type() {
|
310 | return this._type;
|
311 | }
|
312 | constructor(chunks = [], opts = {}) {
|
313 | this[_a] = 'Blob';
|
314 | const dataChunks = [];
|
315 | for (const chunk of chunks) {
|
316 | if (chunk instanceof Blob) {
|
317 | dataChunks.push(chunk._buffer);
|
318 | }
|
319 | else if (typeof chunk === 'string') {
|
320 | const textEncoder = new TextEncoder();
|
321 | dataChunks.push(textEncoder.encode(chunk));
|
322 | }
|
323 | else if (chunk instanceof DataView) {
|
324 | dataChunks.push(new Uint8Array(chunk.buffer.slice(0)));
|
325 | }
|
326 | else if (chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk)) {
|
327 | dataChunks.push(new Uint8Array(ArrayBuffer.isView(chunk) ? chunk.buffer.slice(0) : chunk.slice(0)));
|
328 | }
|
329 | else {
|
330 | const textEncoder = new TextEncoder();
|
331 | dataChunks.push(textEncoder.encode(String(chunk)));
|
332 | }
|
333 | }
|
334 | const size = dataChunks.reduce((size, chunk) => size + chunk.byteLength, 0);
|
335 | const buffer = new Uint8Array(size);
|
336 | let offset = 0;
|
337 | for (let i = 0; i < dataChunks.length; i++) {
|
338 | const chunk = dataChunks[i];
|
339 | buffer.set(chunk, offset);
|
340 | offset += chunk.byteLength;
|
341 | }
|
342 | this._buffer = buffer;
|
343 | this._size = this._buffer.byteLength;
|
344 | this._type = opts.type || '';
|
345 | if (/[^\u0020-\u007E]/.test(this._type)) {
|
346 | this._type = '';
|
347 | }
|
348 | else {
|
349 | this._type = this._type.toLowerCase();
|
350 | }
|
351 | }
|
352 | arrayBuffer() {
|
353 | return Promise.resolve(this._buffer);
|
354 | }
|
355 | text() {
|
356 | const textDecoder = new TextDecoder();
|
357 | return Promise.resolve(textDecoder.decode(this._buffer));
|
358 | }
|
359 | slice(start, end, type) {
|
360 | const slice = this._buffer.slice(start || 0, end || this._buffer.length);
|
361 | return new Blob([slice], { type: type });
|
362 | }
|
363 | stream() {
|
364 | throw new Error('stream is currently not supported');
|
365 | }
|
366 | toString() {
|
367 | return '[object Blob]';
|
368 | }
|
369 | }
|
370 | _a = Symbol.toStringTag;
|
371 |
|
372 | Blob.InternalAccessor = class {
|
373 | static getBuffer(blob) {
|
374 | return blob._buffer;
|
375 | }
|
376 | };
|
377 | export class File extends Blob {
|
378 | get name() {
|
379 | return this._name;
|
380 | }
|
381 | get lastModified() {
|
382 | return this._lastModified;
|
383 | }
|
384 | constructor(chunks, name, opts = {}) {
|
385 | super(chunks, opts);
|
386 | this[_b] = 'File';
|
387 | this._name = name.replace(/\//g, ':');
|
388 | this._lastModified = opts.lastModified ? new Date(opts.lastModified).valueOf() : Date.now();
|
389 | }
|
390 | toString() {
|
391 | return '[object File]';
|
392 | }
|
393 | }
|
394 | _b = Symbol.toStringTag;
|
395 | export class FileReader {
|
396 | get readyState() {
|
397 | return this._readyState;
|
398 | }
|
399 | get result() {
|
400 | return this._result;
|
401 | }
|
402 | constructor() {
|
403 | this.EMPTY = 0;
|
404 | this.LOADING = 1;
|
405 | this.DONE = 2;
|
406 | this._listeners = new Map();
|
407 | this[_c] = 'FileReader';
|
408 |
|
409 | }
|
410 | _array2base64(input) {
|
411 | const byteToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
412 | const output = [];
|
413 | for (let i = 0; i < input.length; i += 3) {
|
414 | const byte1 = input[i];
|
415 | const haveByte2 = i + 1 < input.length;
|
416 | const byte2 = haveByte2 ? input[i + 1] : 0;
|
417 | const haveByte3 = i + 2 < input.length;
|
418 | const byte3 = haveByte3 ? input[i + 2] : 0;
|
419 | const outByte1 = byte1 >> 2;
|
420 | const outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
|
421 | let outByte3 = ((byte2 & 0x0f) << 2) | (byte3 >> 6);
|
422 | let outByte4 = byte3 & 0x3f;
|
423 | if (!haveByte3) {
|
424 | outByte4 = 64;
|
425 | if (!haveByte2) {
|
426 | outByte3 = 64;
|
427 | }
|
428 | }
|
429 | output.push(byteToCharMap[outByte1], byteToCharMap[outByte2], byteToCharMap[outByte3], byteToCharMap[outByte4]);
|
430 | }
|
431 | return output.join('');
|
432 | }
|
433 | _read(blob, kind) {
|
434 | if (!(blob instanceof Blob)) {
|
435 | throw new TypeError(`Failed to execute '${kind}' on 'FileReader': parameter 1 is not of type 'Blob'.`);
|
436 | }
|
437 | this._result = '';
|
438 | setTimeout(() => {
|
439 | this._readyState = this.LOADING;
|
440 | this.emitEvent('load');
|
441 | this.emitEvent('loadend');
|
442 | });
|
443 | }
|
444 | emitEvent(eventName, ...args) {
|
445 | if (types.isFunction(this['on' + eventName])) {
|
446 | this['on' + eventName](...args);
|
447 | }
|
448 | const handlers = this._listeners.get(eventName) || [];
|
449 | handlers.forEach((handler) => {
|
450 | handler(...args);
|
451 | });
|
452 | }
|
453 | addEventListener(eventName, handler) {
|
454 | if (['abort', 'error', 'load', 'loadend', 'loadstart', 'progress'].indexOf(eventName) === -1) {
|
455 | throw new Error('Event not supported: ' + eventName);
|
456 | }
|
457 | const handlers = this._listeners.get(eventName) || [];
|
458 | handlers.push(handler);
|
459 | this._listeners.set(eventName, handlers);
|
460 | }
|
461 | removeEventListener(eventName, toDetach) {
|
462 | let handlers = this._listeners.get(eventName) || [];
|
463 | handlers = handlers.filter((handler) => handler !== toDetach);
|
464 | this._listeners.set(eventName, handlers);
|
465 | }
|
466 | readAsDataURL(blob) {
|
467 | this._read(blob, 'readAsDataURL');
|
468 | this._result = `data:${blob.type};base64,${this._array2base64(Blob.InternalAccessor.getBuffer(blob))}`;
|
469 | }
|
470 | readAsText(blob) {
|
471 | this._read(blob, 'readAsText');
|
472 | const textDecoder = new TextDecoder();
|
473 | this._result = textDecoder.decode(Blob.InternalAccessor.getBuffer(blob));
|
474 | }
|
475 | readAsArrayBuffer(blob) {
|
476 | this._read(blob, 'readAsArrayBuffer');
|
477 | this._result = Blob.InternalAccessor.getBuffer(blob).buffer.slice(0);
|
478 | }
|
479 | abort() {
|
480 |
|
481 | }
|
482 | toString() {
|
483 | return '[object FileReader]';
|
484 | }
|
485 | }
|
486 | _c = Symbol.toStringTag;
|
487 |
|
\ | No newline at end of file |