UNPKG

17.1 kBJavaScriptView Raw
1var _a, _b, _c;
2import * as http from '../http';
3import * as types from '../utils/types';
4import { Trace } from '../trace';
5var 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 = {}));
13export class XMLHttpRequest {
14 constructor() {
15 this.UNSENT = 0;
16 this.OPENED = 1;
17 this.HEADERS_RECEIVED = 2;
18 this.LOADING = 3;
19 this.DONE = 4;
20 this._responseType = '';
21 this._listeners = new Map();
22 this._readyState = this.UNSENT;
23 }
24 get upload() {
25 return this;
26 }
27 get readyState() {
28 return this._readyState;
29 }
30 get responseType() {
31 return this._responseType;
32 }
33 set responseType(value) {
34 if (value === XMLHttpRequestResponseType.empty || value in XMLHttpRequestResponseType) {
35 this._responseType = value;
36 }
37 else {
38 throw new Error(`Response type of '${value}' not supported.`);
39 }
40 }
41 get responseText() {
42 if (this._responseType !== XMLHttpRequestResponseType.empty && this._responseType !== XMLHttpRequestResponseType.text) {
43 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}').`);
44 }
45 if (types.isFunction(this._responseTextReader)) {
46 return this._responseTextReader();
47 }
48 return '';
49 }
50 get response() {
51 if (this._responseType === XMLHttpRequestResponseType.empty || this._responseType === XMLHttpRequestResponseType.text) {
52 if (this._readyState !== this.LOADING && this._readyState !== this.DONE) {
53 return '';
54 }
55 else {
56 return this._response;
57 }
58 }
59 else {
60 if (this._readyState !== this.DONE) {
61 return null;
62 }
63 else {
64 return this._response;
65 }
66 }
67 }
68 get status() {
69 return this._status;
70 }
71 get statusText() {
72 if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._errorFlag) {
73 return '';
74 }
75 return statuses[this._status];
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 //The Android Java HTTP lib throws an exception if we provide a
183 //a request body for GET requests, so we avoid doing that.
184 //Browser implementations silently ignore it as well.
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}
248const 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};
290export 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}
305export class Blob {
306 constructor(chunks = [], opts = {}) {
307 this[_a] = 'Blob';
308 const dataChunks = [];
309 for (const chunk of chunks) {
310 if (chunk instanceof Blob) {
311 dataChunks.push(chunk._buffer);
312 }
313 else if (typeof chunk === 'string') {
314 const textEncoder = new TextEncoder();
315 dataChunks.push(textEncoder.encode(chunk));
316 }
317 else if (chunk instanceof DataView) {
318 dataChunks.push(new Uint8Array(chunk.buffer.slice(0)));
319 }
320 else if (chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk)) {
321 dataChunks.push(new Uint8Array(ArrayBuffer.isView(chunk) ? chunk.buffer.slice(0) : chunk.slice(0)));
322 }
323 else {
324 const textEncoder = new TextEncoder();
325 dataChunks.push(textEncoder.encode(String(chunk)));
326 }
327 }
328 const size = dataChunks.reduce((size, chunk) => size + chunk.byteLength, 0);
329 const buffer = new Uint8Array(size);
330 let offset = 0;
331 for (let i = 0; i < dataChunks.length; i++) {
332 const chunk = dataChunks[i];
333 buffer.set(chunk, offset);
334 offset += chunk.byteLength;
335 }
336 this._buffer = buffer;
337 this._size = this._buffer.byteLength;
338 this._type = opts.type || '';
339 if (/[^\u0020-\u007E]/.test(this._type)) {
340 this._type = '';
341 }
342 else {
343 this._type = this._type.toLowerCase();
344 }
345 }
346 get size() {
347 return this._size;
348 }
349 get type() {
350 return this._type;
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// Note: only for use by XHR
372Blob.InternalAccessor = class {
373 static getBuffer(blob) {
374 return blob._buffer;
375 }
376};
377export class File extends Blob {
378 constructor(chunks, name, opts = {}) {
379 super(chunks, opts);
380 this[_b] = 'File';
381 this._name = name.replace(/\//g, ':');
382 this._lastModified = opts.lastModified ? new Date(opts.lastModified).valueOf() : Date.now();
383 }
384 get name() {
385 return this._name;
386 }
387 get lastModified() {
388 return this._lastModified;
389 }
390 toString() {
391 return '[object File]';
392 }
393}
394_b = Symbol.toStringTag;
395export class FileReader {
396 constructor() {
397 this.EMPTY = 0;
398 this.LOADING = 1;
399 this.DONE = 2;
400 this._listeners = new Map();
401 this[_c] = 'FileReader';
402 //
403 }
404 get readyState() {
405 return this._readyState;
406 }
407 get result() {
408 return this._result;
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//# sourceMappingURL=index.js.map
\No newline at end of file