@nativescript/canvas-polyfill 1.0.0-v6 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DOM/CharacterData.js +5 -10
- package/DOM/CharacterData.js.map +1 -1
- package/DOM/Document.d.ts +6 -4
- package/DOM/Document.js +58 -54
- package/DOM/Document.js.map +1 -1
- package/DOM/Element.d.ts +20 -18
- package/DOM/Element.js +64 -131
- package/DOM/Element.js.map +1 -1
- package/DOM/HTMLCanvasElement.d.ts +7 -1
- package/DOM/HTMLCanvasElement.js +35 -11
- package/DOM/HTMLCanvasElement.js.map +1 -1
- package/DOM/HTMLImageElement.d.ts +8 -4
- package/DOM/HTMLImageElement.js +154 -172
- package/DOM/HTMLImageElement.js.map +1 -1
- package/DOM/HTMLVideoElement.d.ts +21 -1
- package/DOM/HTMLVideoElement.js +103 -11
- package/DOM/HTMLVideoElement.js.map +1 -1
- package/DOM/Node.d.ts +7 -1
- package/DOM/Node.js +57 -42
- package/DOM/Node.js.map +1 -1
- package/DOM/Text.js +5 -10
- package/DOM/Text.js.map +1 -1
- package/DOM/XMLDocument.js +3 -11
- package/DOM/XMLDocument.js.map +1 -1
- package/DOM/svg/SVGCircleElement.d.ts +13 -0
- package/DOM/svg/SVGCircleElement.js +39 -0
- package/DOM/svg/SVGCircleElement.js.map +1 -0
- package/DOM/svg/SVGElement.d.ts +17 -0
- package/DOM/svg/SVGElement.js +59 -0
- package/DOM/svg/SVGElement.js.map +1 -0
- package/DOM/svg/SVGGGElement.d.ts +13 -0
- package/DOM/svg/SVGGGElement.js +39 -0
- package/DOM/svg/SVGGGElement.js.map +1 -0
- package/DOM/svg/SVGPathElement.d.ts +13 -0
- package/DOM/svg/SVGPathElement.js +39 -0
- package/DOM/svg/SVGPathElement.js.map +1 -0
- package/DOM/svg/SVGRectElement.d.ts +13 -0
- package/DOM/svg/SVGRectElement.js +39 -0
- package/DOM/svg/SVGRectElement.js.map +1 -0
- package/DOM/svg/index.d.ts +5 -0
- package/DOM/svg/index.js +6 -0
- package/DOM/svg/index.js.map +1 -0
- package/URL.android.d.ts +38 -0
- package/URL.android.js +266 -0
- package/URL.android.js.map +1 -0
- package/URL.ios.d.ts +37 -0
- package/URL.ios.js +209 -0
- package/URL.ios.js.map +1 -0
- package/{vendor/http-async → async}/async.d.ts +0 -0
- package/{vendor/http-async → async}/async.js +0 -0
- package/async/async.js.map +1 -0
- package/{vendor/http-async → async}/file/file.android.d.ts +0 -0
- package/{vendor/http-async → async}/file/file.android.js +8 -5
- package/async/file/file.android.js.map +1 -0
- package/{vendor/http-async → async}/file/file.ios.d.ts +0 -0
- package/async/file/file.ios.js +55 -0
- package/async/file/file.ios.js.map +1 -0
- package/{vendor/http-async → async}/http/http-request-common.d.ts +0 -0
- package/{vendor/http-async → async}/http/http-request-common.js +0 -0
- package/async/http/http-request-common.js.map +1 -0
- package/{vendor/http-async → async}/http/http.android.d.ts +0 -0
- package/{vendor/http-async → async}/http/http.android.js +30 -13
- package/async/http/http.android.js.map +1 -0
- package/{vendor/http-async → async}/http/http.ios.d.ts +0 -0
- package/{vendor/http-async → async}/http/http.ios.js +305 -6
- package/async/http/http.ios.js.map +1 -0
- package/{vendor/http-async → async}/xhr/TNSXMLHttpRequest.d.ts +1 -0
- package/async/xhr/TNSXMLHttpRequest.js +958 -0
- package/async/xhr/TNSXMLHttpRequest.js.map +1 -0
- package/index.js +64 -16
- package/index.js.map +1 -1
- package/package.json +10 -8
- package/performance.d.ts +0 -1
- package/performance.js +12 -15
- package/performance.js.map +1 -1
- package/platforms/android/async-release.aar +0 -0
- package/platforms/android/include.gradle +2 -1
- package/resize.js +13 -13
- package/resize.js.map +1 -1
- package/window.d.ts +1 -1
- package/window.js +56 -84
- package/window.js.map +1 -1
- package/vendor/http-async/async.js.map +0 -1
- package/vendor/http-async/file/file.android.js.map +0 -1
- package/vendor/http-async/file/file.d.ts +0 -8
- package/vendor/http-async/file/file.ios.js +0 -62
- package/vendor/http-async/file/file.ios.js.map +0 -1
- package/vendor/http-async/http/http-request-common.js.map +0 -1
- package/vendor/http-async/http/http.android.js.map +0 -1
- package/vendor/http-async/http/http.d.ts +0 -14
- package/vendor/http-async/http/http.ios.js.map +0 -1
- package/vendor/http-async/xhr/TNSXMLHttpRequest.js +0 -982
- package/vendor/http-async/xhr/TNSXMLHttpRequest.js.map +0 -1
|
@@ -1,982 +0,0 @@
|
|
|
1
|
-
import {Http} from '../http/http';
|
|
2
|
-
import {HttpError, ProgressEvent} from '../http/http-request-common';
|
|
3
|
-
import {FileManager} from '../file/file';
|
|
4
|
-
import {isNullOrUndefined, isObject, isFunction} from '@nativescript/core/utils/types';
|
|
5
|
-
import {knownFolders, path as filePath, File as fsFile, isIOS, isAndroid} from '@nativescript/core';
|
|
6
|
-
|
|
7
|
-
var XMLHttpRequestResponseType;
|
|
8
|
-
(function (XMLHttpRequestResponseType) {
|
|
9
|
-
XMLHttpRequestResponseType["empty"] = "";
|
|
10
|
-
XMLHttpRequestResponseType["text"] = "text";
|
|
11
|
-
XMLHttpRequestResponseType["json"] = "json";
|
|
12
|
-
XMLHttpRequestResponseType["document"] = "document";
|
|
13
|
-
XMLHttpRequestResponseType["arraybuffer"] = "arraybuffer";
|
|
14
|
-
XMLHttpRequestResponseType["blob"] = "blob";
|
|
15
|
-
})(XMLHttpRequestResponseType || (XMLHttpRequestResponseType = {}));
|
|
16
|
-
const statuses = {
|
|
17
|
-
100: 'Continue',
|
|
18
|
-
101: 'Switching Protocols',
|
|
19
|
-
200: 'OK',
|
|
20
|
-
201: 'Created',
|
|
21
|
-
202: 'Accepted',
|
|
22
|
-
203: 'Non - Authoritative Information',
|
|
23
|
-
204: 'No Content',
|
|
24
|
-
205: 'Reset Content',
|
|
25
|
-
206: 'Partial Content',
|
|
26
|
-
300: 'Multiple Choices',
|
|
27
|
-
301: 'Moved Permanently',
|
|
28
|
-
302: 'Found',
|
|
29
|
-
303: 'See Other',
|
|
30
|
-
304: 'Not Modified',
|
|
31
|
-
305: 'Use Proxy',
|
|
32
|
-
307: 'Temporary Redirect',
|
|
33
|
-
400: 'Bad Request',
|
|
34
|
-
401: 'Unauthorized',
|
|
35
|
-
402: 'Payment Required',
|
|
36
|
-
403: 'Forbidden',
|
|
37
|
-
404: 'Not Found',
|
|
38
|
-
405: 'Method Not Allowed',
|
|
39
|
-
406: 'Not Acceptable',
|
|
40
|
-
407: 'Proxy Authentication Required',
|
|
41
|
-
408: 'Request Timeout',
|
|
42
|
-
409: 'Conflict',
|
|
43
|
-
410: 'Gone',
|
|
44
|
-
411: 'Length Required',
|
|
45
|
-
412: 'Precondition Failed',
|
|
46
|
-
413: 'Request Entity Too Large',
|
|
47
|
-
414: 'Request - URI Too Long',
|
|
48
|
-
415: 'Unsupported Media Type',
|
|
49
|
-
416: 'Requested Range Not Satisfiable',
|
|
50
|
-
417: 'Expectation Failed',
|
|
51
|
-
500: 'Internal Server Error',
|
|
52
|
-
501: 'Not Implemented',
|
|
53
|
-
502: 'Bad Gateway',
|
|
54
|
-
503: 'Service Unavailable',
|
|
55
|
-
504: 'Gateway Timeout',
|
|
56
|
-
505: 'HTTP Version Not Supported'
|
|
57
|
-
};
|
|
58
|
-
var Status;
|
|
59
|
-
(function (Status) {
|
|
60
|
-
Status[Status["UNSENT"] = 0] = "UNSENT";
|
|
61
|
-
Status[Status["OPENED"] = 0] = "OPENED";
|
|
62
|
-
Status[Status["LOADING"] = 200] = "LOADING";
|
|
63
|
-
Status[Status["DONE"] = 200] = "DONE";
|
|
64
|
-
})(Status || (Status = {}));
|
|
65
|
-
|
|
66
|
-
export class TNSXMLHttpRequestUpload {
|
|
67
|
-
constructor(req) {
|
|
68
|
-
this._listeners = new Map();
|
|
69
|
-
this._request = req;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
addEventListener(eventName, handler) {
|
|
73
|
-
const handlers = this._listeners.get(eventName) || [];
|
|
74
|
-
handlers.push(handler);
|
|
75
|
-
this._listeners.set(eventName, handlers);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
removeEventListener(eventName, toDetach) {
|
|
79
|
-
let handlers = this._listeners.get(eventName) || [];
|
|
80
|
-
handlers = handlers.filter(handler => handler !== toDetach);
|
|
81
|
-
this._listeners.set(eventName, handlers);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
_emitEvent(eventName, ...args) {
|
|
85
|
-
const handlers = this._listeners.get(eventName) || [];
|
|
86
|
-
handlers.forEach(handler => {
|
|
87
|
-
handler.apply(this, ...args);
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export class TNSXMLHttpRequest {
|
|
93
|
-
constructor() {
|
|
94
|
-
this.UNSENT = 0;
|
|
95
|
-
this.OPENED = 1;
|
|
96
|
-
this.HEADERS_RECEIVED = 2;
|
|
97
|
-
this.LOADING = 3;
|
|
98
|
-
this.DONE = 4;
|
|
99
|
-
this.timeout = 0;
|
|
100
|
-
this._readyState = this.UNSENT;
|
|
101
|
-
this._response = '';
|
|
102
|
-
this._responseType = null;
|
|
103
|
-
this._responseText = null;
|
|
104
|
-
this._request = null;
|
|
105
|
-
this._lastProgress = {lengthComputable: false, loaded: 0, total: 0, target: this};
|
|
106
|
-
this._responseURL = '';
|
|
107
|
-
this._listeners = new Map();
|
|
108
|
-
this.textTypes = [
|
|
109
|
-
'text/plain',
|
|
110
|
-
'application/xml',
|
|
111
|
-
'application/rss+xml',
|
|
112
|
-
'text/html',
|
|
113
|
-
'text/xml',
|
|
114
|
-
];
|
|
115
|
-
this._status = Status.UNSENT;
|
|
116
|
-
this._http = new Http();
|
|
117
|
-
this._upload = new TNSXMLHttpRequestUpload(this);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
get readyState() {
|
|
121
|
-
return this._readyState;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
get response() {
|
|
125
|
-
return this._response;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
get responseType() {
|
|
129
|
-
return this._responseType;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
get responseText() {
|
|
133
|
-
if (this._responseType === XMLHttpRequestResponseType.text ||
|
|
134
|
-
this._responseType === XMLHttpRequestResponseType.json) {
|
|
135
|
-
return this._responseText;
|
|
136
|
-
}
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
get responseURL() {
|
|
141
|
-
return this._responseURL;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
get status() {
|
|
145
|
-
return this._status;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
get statusText() {
|
|
149
|
-
if (this._readyState === this.UNSENT ||
|
|
150
|
-
this._readyState === this.OPENED) {
|
|
151
|
-
return '';
|
|
152
|
-
}
|
|
153
|
-
return statuses[this.status];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
get upload() {
|
|
157
|
-
return this._upload;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
get responseXML() {
|
|
161
|
-
const header = this.getResponseHeader('Content-Type') ||
|
|
162
|
-
this.getResponseHeader('content-type');
|
|
163
|
-
const contentType = header && header.toLowerCase();
|
|
164
|
-
if (this.isTextContentType(contentType)) {
|
|
165
|
-
if (this._responseType === XMLHttpRequestResponseType.document) {
|
|
166
|
-
return this.responseText;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return '';
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
isTextContentType(contentType) {
|
|
173
|
-
let result = false;
|
|
174
|
-
for (let i = 0; i < this.textTypes.length; i++) {
|
|
175
|
-
if (contentType.toLowerCase().indexOf(this.textTypes[i]) >= 0) {
|
|
176
|
-
result = true;
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
return result;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
_setResponseType() {
|
|
184
|
-
const header = this.getResponseHeader('Content-Type') ||
|
|
185
|
-
this.getResponseHeader('content-type');
|
|
186
|
-
const contentType = header && header.toLowerCase();
|
|
187
|
-
if (contentType) {
|
|
188
|
-
if (contentType.indexOf('application/json') >= 0 ||
|
|
189
|
-
contentType.indexOf('+json') >= 0) {
|
|
190
|
-
this._responseType = XMLHttpRequestResponseType.json;
|
|
191
|
-
} else if (this.isTextContentType(contentType)) {
|
|
192
|
-
if (contentType.indexOf('text/html') ||
|
|
193
|
-
contentType.indexOf('text/xml')) {
|
|
194
|
-
this._responseType = XMLHttpRequestResponseType.document;
|
|
195
|
-
}
|
|
196
|
-
this._responseType = XMLHttpRequestResponseType.text;
|
|
197
|
-
}
|
|
198
|
-
} else {
|
|
199
|
-
this._responseType = XMLHttpRequestResponseType.text;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
getAllResponseHeaders() {
|
|
204
|
-
if (this._readyState < 2) {
|
|
205
|
-
return '';
|
|
206
|
-
}
|
|
207
|
-
let result = '';
|
|
208
|
-
if (typeof this._headers === 'object') {
|
|
209
|
-
const keys = Object.keys(this._headers);
|
|
210
|
-
for (let key of keys) {
|
|
211
|
-
result += key + ': ' + this._headers[key] + '\r\n';
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return result.substr(0, result.length - 2);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
getResponseHeader(header) {
|
|
218
|
-
if (typeof header === 'string' &&
|
|
219
|
-
this._readyState > 1 &&
|
|
220
|
-
this._headers) {
|
|
221
|
-
header = header.toLowerCase();
|
|
222
|
-
if (typeof this._headers === 'object') {
|
|
223
|
-
const keys = Object.keys(this._headers);
|
|
224
|
-
for (let key of keys) {
|
|
225
|
-
const item = key.toLowerCase();
|
|
226
|
-
if (item === header) {
|
|
227
|
-
return this._headers[key];
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
return null;
|
|
232
|
-
}
|
|
233
|
-
return null;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
overrideMimeType(mime) {
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
set responseType(value) {
|
|
240
|
-
if (value === XMLHttpRequestResponseType.empty ||
|
|
241
|
-
value in XMLHttpRequestResponseType) {
|
|
242
|
-
this._responseType = value;
|
|
243
|
-
} else {
|
|
244
|
-
throw new Error(`Response type of '${value}' not supported.`);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
_addToStringOnResponse() {
|
|
249
|
-
if (isNullOrUndefined(this.response)) {
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
if (this.response instanceof ArrayBuffer) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
if (isObject(this.response)) {
|
|
256
|
-
Object.defineProperty(this._response, 'toString', {
|
|
257
|
-
configurable: true,
|
|
258
|
-
enumerable: false,
|
|
259
|
-
writable: true,
|
|
260
|
-
value: () => this.responseText,
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
_toJSString(data) {
|
|
266
|
-
const encodings = ['UTF-8', 'US-ASCII', 'ISO-8859-1', null];
|
|
267
|
-
let value;
|
|
268
|
-
const count = encodings.length;
|
|
269
|
-
for (let i = 0; i < count; i++) {
|
|
270
|
-
let encodingType = encodings[i];
|
|
271
|
-
if (encodingType === null) {
|
|
272
|
-
value = (new java.lang.String(data)).toString();
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
try {
|
|
276
|
-
let encoding = java.nio.charset.Charset.forName(encodingType);
|
|
277
|
-
value = (new java.lang.String(data, encoding)).toString();
|
|
278
|
-
break;
|
|
279
|
-
} catch (e) {
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return value;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
open(method, url, async = true, username = null, password = null) {
|
|
286
|
-
this._headers = {};
|
|
287
|
-
this._responseURL = '';
|
|
288
|
-
this._httpContent = null;
|
|
289
|
-
this._request = {
|
|
290
|
-
method,
|
|
291
|
-
url,
|
|
292
|
-
async,
|
|
293
|
-
username,
|
|
294
|
-
password,
|
|
295
|
-
};
|
|
296
|
-
this._updateReadyStateChange(this.OPENED);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
setRequestHeader(header, value) {
|
|
300
|
-
if (this._readyState !== this.OPENED) {
|
|
301
|
-
throw new Error("Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.");
|
|
302
|
-
}
|
|
303
|
-
if (typeof this._headers === 'object') {
|
|
304
|
-
this._headers[header] = value;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
send(body = null) {
|
|
309
|
-
if (this._readyState !== this.OPENED) {
|
|
310
|
-
throw new Error("Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED");
|
|
311
|
-
}
|
|
312
|
-
if (!this._headers['Accept']) {
|
|
313
|
-
this._headers['Accept'] = '*/*';
|
|
314
|
-
}
|
|
315
|
-
if (typeof this._request.method === 'string' &&
|
|
316
|
-
this._request.method.toLowerCase() === 'get' &&
|
|
317
|
-
typeof this._request.url === 'string' && !this._request.url.startsWith('http')) {
|
|
318
|
-
let path;
|
|
319
|
-
if (this._request.url.startsWith('file://')) {
|
|
320
|
-
path = this._request.url.replace('file://', '');
|
|
321
|
-
} else if (this._request.url.startsWith('~/')) {
|
|
322
|
-
path = filePath.join(knownFolders.currentApp().path, this._request.url.replace('~/', ''));
|
|
323
|
-
} else if (this._request.url.startsWith('/')) {
|
|
324
|
-
path = this._request.url;
|
|
325
|
-
}
|
|
326
|
-
const responseURL = `file://${path}`;
|
|
327
|
-
let contentLength = -1;
|
|
328
|
-
const file = fsFile.fromPath(path);
|
|
329
|
-
if (fsFile.exists(path)) {
|
|
330
|
-
contentLength = file.size;
|
|
331
|
-
}
|
|
332
|
-
this._lastProgress = {
|
|
333
|
-
lengthComputable: contentLength > -1,
|
|
334
|
-
loaded: 0,
|
|
335
|
-
total: contentLength,
|
|
336
|
-
target: this,
|
|
337
|
-
};
|
|
338
|
-
const startEvent = new ProgressEvent('loadstart', this._lastProgress);
|
|
339
|
-
if (this.onloadstart) {
|
|
340
|
-
this.onloadstart(startEvent);
|
|
341
|
-
}
|
|
342
|
-
this.emitEvent('loadstart', startEvent);
|
|
343
|
-
this._updateReadyStateChange(this.LOADING);
|
|
344
|
-
FileManager.readFile(path, {}, (error, data) => {
|
|
345
|
-
if (error) {
|
|
346
|
-
const errorEvent = new ProgressEvent('error', this._lastProgress);
|
|
347
|
-
this._responseText = error.message;
|
|
348
|
-
if (this.onerror) {
|
|
349
|
-
this.onerror(errorEvent);
|
|
350
|
-
}
|
|
351
|
-
this.emitEvent('error', errorEvent);
|
|
352
|
-
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
|
|
353
|
-
if (this.onloadend) {
|
|
354
|
-
this.onloadend(loadendEvent);
|
|
355
|
-
}
|
|
356
|
-
this.emitEvent('loadend', loadendEvent);
|
|
357
|
-
this._updateReadyStateChange(this.DONE);
|
|
358
|
-
} else {
|
|
359
|
-
if (!this.responseType) {
|
|
360
|
-
this._setResponseType();
|
|
361
|
-
}
|
|
362
|
-
this._status = 200;
|
|
363
|
-
this._httpContent = data;
|
|
364
|
-
this._responseURL = responseURL;
|
|
365
|
-
if (this.responseType === XMLHttpRequestResponseType.json) {
|
|
366
|
-
try {
|
|
367
|
-
if (isAndroid) {
|
|
368
|
-
this._responseText = this._toJSString(data);
|
|
369
|
-
this._response = JSON.parse(this._responseText);
|
|
370
|
-
} else {
|
|
371
|
-
this._responseText = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding);
|
|
372
|
-
if (!this._responseText) {
|
|
373
|
-
this._responseText = NSString.alloc().initWithDataEncoding(data, NSISOLatin1StringEncoding);
|
|
374
|
-
}
|
|
375
|
-
this._response = JSON.parse(this._responseText);
|
|
376
|
-
}
|
|
377
|
-
} catch (e) {
|
|
378
|
-
console.log('json parse error', e);
|
|
379
|
-
}
|
|
380
|
-
} else if (this.responseType === XMLHttpRequestResponseType.text) {
|
|
381
|
-
if (isIOS) {
|
|
382
|
-
let code = NSUTF8StringEncoding;
|
|
383
|
-
let encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
|
384
|
-
if (!encodedString) {
|
|
385
|
-
code = NSISOLatin1StringEncoding;
|
|
386
|
-
encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
|
387
|
-
}
|
|
388
|
-
this._responseText = this._response = encodedString.toString();
|
|
389
|
-
} else {
|
|
390
|
-
const response = this._toJSString(data);
|
|
391
|
-
this._responseText = this._response = response
|
|
392
|
-
? response.toString()
|
|
393
|
-
: '';
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
} else if (this.responseType === XMLHttpRequestResponseType.document) {
|
|
397
|
-
if (isIOS) {
|
|
398
|
-
let code = NSUTF8StringEncoding;
|
|
399
|
-
let encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
|
400
|
-
if (!encodedString) {
|
|
401
|
-
code = NSISOLatin1StringEncoding;
|
|
402
|
-
encodedString = NSString.alloc().initWithDataEncoding(data, code);
|
|
403
|
-
}
|
|
404
|
-
this._responseText = this._response = encodedString.toString();
|
|
405
|
-
} else {
|
|
406
|
-
let response = this._toJSString(data);
|
|
407
|
-
this._responseText = this._response = response
|
|
408
|
-
? response
|
|
409
|
-
: '';
|
|
410
|
-
}
|
|
411
|
-
} else if (this.responseType === XMLHttpRequestResponseType.arraybuffer) {
|
|
412
|
-
if (isIOS) {
|
|
413
|
-
this._response = interop.bufferFromData(data);
|
|
414
|
-
} else {
|
|
415
|
-
this._response = ArrayBuffer.from(java.nio.ByteBuffer.wrap(data));
|
|
416
|
-
}
|
|
417
|
-
} else if (this.responseType === XMLHttpRequestResponseType.blob) {
|
|
418
|
-
let buffer;
|
|
419
|
-
if (isIOS) {
|
|
420
|
-
buffer = interop.bufferFromData(data);
|
|
421
|
-
} else {
|
|
422
|
-
buffer = ArrayBuffer.from(java.nio.ByteBuffer.wrap(data));
|
|
423
|
-
}
|
|
424
|
-
this._response = new Blob([buffer]);
|
|
425
|
-
}
|
|
426
|
-
let size = 0;
|
|
427
|
-
if (isIOS) {
|
|
428
|
-
if (data instanceof NSData) {
|
|
429
|
-
size = data.length;
|
|
430
|
-
}
|
|
431
|
-
} else {
|
|
432
|
-
size = data ? data.length : 0;
|
|
433
|
-
}
|
|
434
|
-
this._lastProgress = {
|
|
435
|
-
lengthComputable: contentLength > -1,
|
|
436
|
-
loaded: size,
|
|
437
|
-
total: contentLength,
|
|
438
|
-
target: this,
|
|
439
|
-
};
|
|
440
|
-
const progressEvent = new ProgressEvent('progress', this._lastProgress);
|
|
441
|
-
if (this.onprogress) {
|
|
442
|
-
this.onprogress(progressEvent);
|
|
443
|
-
}
|
|
444
|
-
this.emitEvent('progress', progressEvent);
|
|
445
|
-
this._addToStringOnResponse();
|
|
446
|
-
const loadEvent = new ProgressEvent('load', this._lastProgress);
|
|
447
|
-
if (this.onload) {
|
|
448
|
-
this.onload(loadEvent);
|
|
449
|
-
}
|
|
450
|
-
this.emitEvent('load', loadEvent);
|
|
451
|
-
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
|
|
452
|
-
if (this.onloadend) {
|
|
453
|
-
this.onloadend(loadendEvent);
|
|
454
|
-
}
|
|
455
|
-
this.emitEvent('loadend', loadendEvent);
|
|
456
|
-
this._updateReadyStateChange(this.DONE);
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
return;
|
|
460
|
-
}
|
|
461
|
-
const method = this._request.method.toLowerCase();
|
|
462
|
-
const request = {
|
|
463
|
-
content: body,
|
|
464
|
-
method: this._request.method,
|
|
465
|
-
url: this._request.url,
|
|
466
|
-
headers: this._headers,
|
|
467
|
-
onLoading: () => {
|
|
468
|
-
if (this.onloadstart) {
|
|
469
|
-
this.onloadstart();
|
|
470
|
-
}
|
|
471
|
-
let contentLength = -1;
|
|
472
|
-
if (typeof this._headers === 'object') {
|
|
473
|
-
if (this._headers['Content-Length']) {
|
|
474
|
-
contentLength =
|
|
475
|
-
parseInt(this._headers['Content-Length'], 10) || -1;
|
|
476
|
-
}
|
|
477
|
-
if (this._headers['content-length']) {
|
|
478
|
-
contentLength =
|
|
479
|
-
parseInt(this._headers['content-length'], 10) || -1;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
this._lastProgress = {
|
|
483
|
-
lengthComputable: contentLength > -1,
|
|
484
|
-
loaded: 0,
|
|
485
|
-
total: contentLength,
|
|
486
|
-
target: this,
|
|
487
|
-
};
|
|
488
|
-
const loadStartEvent = new ProgressEvent('loadstart', this._lastProgress);
|
|
489
|
-
if (this._upload && (method === 'post' || method === 'put')) {
|
|
490
|
-
this._upload._emitEvent('loadstart', loadStartEvent);
|
|
491
|
-
}
|
|
492
|
-
this.emitEvent('loadstart', loadStartEvent);
|
|
493
|
-
this._updateReadyStateChange(this.LOADING);
|
|
494
|
-
},
|
|
495
|
-
onHeaders: (event) => {
|
|
496
|
-
if (!isNaN(event.status)) {
|
|
497
|
-
this._status = event.status;
|
|
498
|
-
}
|
|
499
|
-
if (event.headers) {
|
|
500
|
-
this._headers = event.headers;
|
|
501
|
-
}
|
|
502
|
-
this._updateReadyStateChange(this.HEADERS_RECEIVED);
|
|
503
|
-
},
|
|
504
|
-
};
|
|
505
|
-
if (method === 'post' || method === 'put') {
|
|
506
|
-
request.onProgress = (event) => {
|
|
507
|
-
this._lastProgress = Object.assign(Object.assign({}, (this._lastProgress || {})), event);
|
|
508
|
-
if (event.loaded > 0) {
|
|
509
|
-
const progressEvent = new ProgressEvent('progress', this._lastProgress);
|
|
510
|
-
if (this._upload && (method === 'post' || method === 'put')) {
|
|
511
|
-
this._upload._emitEvent('progress', progressEvent);
|
|
512
|
-
}
|
|
513
|
-
this.emitEvent('progress', progressEvent);
|
|
514
|
-
}
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
if (this.timeout > 0) {
|
|
518
|
-
request['timeout'] = this.timeout;
|
|
519
|
-
}
|
|
520
|
-
this._currentRequest = this._http.request(request);
|
|
521
|
-
this._currentRequest
|
|
522
|
-
.then((res) => {
|
|
523
|
-
this._setResponseType();
|
|
524
|
-
this._status = res.statusCode;
|
|
525
|
-
this._httpContent = res.content;
|
|
526
|
-
this._responseURL = res.url;
|
|
527
|
-
if (this.responseType === XMLHttpRequestResponseType.json) {
|
|
528
|
-
if (typeof res.content === 'string') {
|
|
529
|
-
this._responseText = res.content;
|
|
530
|
-
try {
|
|
531
|
-
this._response = JSON.parse(this.responseText);
|
|
532
|
-
} catch (err) {
|
|
533
|
-
}
|
|
534
|
-
} else if (typeof res.content === 'object') {
|
|
535
|
-
this._response = res.content;
|
|
536
|
-
this._responseText = res.responseText;
|
|
537
|
-
} else {
|
|
538
|
-
if (isIOS) {
|
|
539
|
-
if (res.content instanceof NSData) {
|
|
540
|
-
let code = NSUTF8StringEncoding;
|
|
541
|
-
let encodedString = NSString.alloc().initWithDataEncoding(res.content, code);
|
|
542
|
-
if (!encodedString) {
|
|
543
|
-
code = NSISOLatin1StringEncoding;
|
|
544
|
-
encodedString = NSString.alloc().initWithDataEncoding(res.content, code);
|
|
545
|
-
}
|
|
546
|
-
this._responseText = encodedString.toString();
|
|
547
|
-
this._response = JSON.parse(this._responseText);
|
|
548
|
-
}
|
|
549
|
-
} else {
|
|
550
|
-
if (res.content instanceof java.nio.ByteBuffer) {
|
|
551
|
-
this._responseText = this._toJSString(res.content.array());
|
|
552
|
-
this._response = JSON.parse(this._responseText);
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
} else if (this.responseType === XMLHttpRequestResponseType.text) {
|
|
557
|
-
if (typeof res.content === 'string') {
|
|
558
|
-
this._responseText = res.content;
|
|
559
|
-
} else if (typeof res.content === 'object') {
|
|
560
|
-
this._responseText = JSON.stringify(res.content);
|
|
561
|
-
} else {
|
|
562
|
-
if (isIOS) {
|
|
563
|
-
if (res.content instanceof NSData) {
|
|
564
|
-
let code = NSUTF8StringEncoding;
|
|
565
|
-
let encodedString = NSString.alloc().initWithDataEncoding(res.content, code);
|
|
566
|
-
if (!encodedString) {
|
|
567
|
-
code = NSISOLatin1StringEncoding;
|
|
568
|
-
encodedString = NSString.alloc().initWithDataEncoding(res.content, code);
|
|
569
|
-
}
|
|
570
|
-
this._responseText = this._response = encodedString.toString();
|
|
571
|
-
}
|
|
572
|
-
} else {
|
|
573
|
-
if (res.content instanceof java.nio.ByteBuffer) {
|
|
574
|
-
this._responseText = this._response = this._toJSString(res.content.array());
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
this._response = this._responseText;
|
|
579
|
-
} else if (this.responseType === XMLHttpRequestResponseType.document) {
|
|
580
|
-
if (typeof res.content === 'string') {
|
|
581
|
-
this._responseText = res.content;
|
|
582
|
-
} else {
|
|
583
|
-
if (isIOS) {
|
|
584
|
-
if (res.content instanceof NSData) {
|
|
585
|
-
let code = NSUTF8StringEncoding;
|
|
586
|
-
let encodedString = NSString.alloc().initWithDataEncoding(res.content, code);
|
|
587
|
-
if (!encodedString) {
|
|
588
|
-
code = NSISOLatin1StringEncoding;
|
|
589
|
-
encodedString = NSString.alloc().initWithDataEncoding(res.content, code);
|
|
590
|
-
}
|
|
591
|
-
this._responseText = this._response = encodedString.toString();
|
|
592
|
-
}
|
|
593
|
-
} else {
|
|
594
|
-
if (res.content instanceof java.nio.ByteBuffer) {
|
|
595
|
-
this._responseText = this._response = this._toJSString(res.content.array());
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
} else if (this.responseType === XMLHttpRequestResponseType.arraybuffer) {
|
|
600
|
-
if (isIOS) {
|
|
601
|
-
this._response = interop.bufferFromData(res.content);
|
|
602
|
-
} else {
|
|
603
|
-
this._response = ArrayBuffer.from(res.content);
|
|
604
|
-
}
|
|
605
|
-
} else if (this.responseType === XMLHttpRequestResponseType.blob) {
|
|
606
|
-
const buffer = ArrayBuffer.from(res.content);
|
|
607
|
-
this._response = new Blob([buffer]);
|
|
608
|
-
}
|
|
609
|
-
this._addToStringOnResponse();
|
|
610
|
-
if (this.onload) {
|
|
611
|
-
this.onload();
|
|
612
|
-
}
|
|
613
|
-
const loadEvent = new ProgressEvent('load', this._lastProgress);
|
|
614
|
-
if (this._upload && (method === 'post' || method === 'put')) {
|
|
615
|
-
this._upload._emitEvent('load', loadEvent);
|
|
616
|
-
}
|
|
617
|
-
this.emitEvent('load', loadEvent);
|
|
618
|
-
if (this.onloadend) {
|
|
619
|
-
this.onloadend();
|
|
620
|
-
}
|
|
621
|
-
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
|
|
622
|
-
if (this._upload && (method === 'post' || method === 'put')) {
|
|
623
|
-
this._upload._emitEvent('loadend', loadendEvent);
|
|
624
|
-
}
|
|
625
|
-
this.emitEvent('loadend', loadendEvent);
|
|
626
|
-
this._updateReadyStateChange(this.DONE);
|
|
627
|
-
})
|
|
628
|
-
.catch((error) => {
|
|
629
|
-
const type = error.type;
|
|
630
|
-
const method = this._request.method.toLowerCase();
|
|
631
|
-
switch (type) {
|
|
632
|
-
case HttpError.Cancelled:
|
|
633
|
-
if (this.onabort) {
|
|
634
|
-
this.onabort();
|
|
635
|
-
}
|
|
636
|
-
const abortEvent = new ProgressEvent('abort', this._lastProgress);
|
|
637
|
-
if (this._upload &&
|
|
638
|
-
(method === 'post' || method === 'put')) {
|
|
639
|
-
this._upload._emitEvent('abort', abortEvent);
|
|
640
|
-
}
|
|
641
|
-
this.emitEvent('abort', abortEvent);
|
|
642
|
-
if (this.onloadend) {
|
|
643
|
-
this.onloadend();
|
|
644
|
-
}
|
|
645
|
-
const _loadendEvent = new ProgressEvent('loadend', this._lastProgress);
|
|
646
|
-
if (this._upload &&
|
|
647
|
-
(method === 'post' || method === 'put')) {
|
|
648
|
-
this._upload._emitEvent('loadend', _loadendEvent);
|
|
649
|
-
}
|
|
650
|
-
this.emitEvent('loadend', _loadendEvent);
|
|
651
|
-
if (this._readyState === this.UNSENT ||
|
|
652
|
-
this._readyState === this.OPENED ||
|
|
653
|
-
this._readyState === this.DONE) {
|
|
654
|
-
this._updateReadyStateChange(this.UNSENT);
|
|
655
|
-
} else {
|
|
656
|
-
this._updateReadyStateChange(this.DONE);
|
|
657
|
-
}
|
|
658
|
-
this._currentRequest = null;
|
|
659
|
-
break;
|
|
660
|
-
case HttpError.Timeout:
|
|
661
|
-
if (this.ontimeout) {
|
|
662
|
-
this.ontimeout();
|
|
663
|
-
}
|
|
664
|
-
const timeoutEvent = new ProgressEvent('timeout', this._lastProgress);
|
|
665
|
-
if (this._upload &&
|
|
666
|
-
(method === 'post' || method === 'put')) {
|
|
667
|
-
this._upload._emitEvent('timeout', timeoutEvent);
|
|
668
|
-
}
|
|
669
|
-
this.emitEvent('timeout', timeoutEvent);
|
|
670
|
-
break;
|
|
671
|
-
case HttpError.Error:
|
|
672
|
-
if (this.onerror) {
|
|
673
|
-
this.onerror(error.message);
|
|
674
|
-
}
|
|
675
|
-
const errorEvent = new ProgressEvent('error', this._lastProgress);
|
|
676
|
-
if (this._upload &&
|
|
677
|
-
(method === 'post' || method === 'put')) {
|
|
678
|
-
this._upload._emitEvent('error', errorEvent);
|
|
679
|
-
}
|
|
680
|
-
this.emitEvent('error', errorEvent);
|
|
681
|
-
if (this.onloadend) {
|
|
682
|
-
this.onloadend();
|
|
683
|
-
}
|
|
684
|
-
const loadendEvent = new ProgressEvent('loadend', this._lastProgress);
|
|
685
|
-
if (this._upload &&
|
|
686
|
-
(method === 'post' || method === 'put')) {
|
|
687
|
-
this._upload._emitEvent('loadend', loadendEvent);
|
|
688
|
-
}
|
|
689
|
-
this.emitEvent('loadend', loadendEvent);
|
|
690
|
-
break;
|
|
691
|
-
}
|
|
692
|
-
this._updateReadyStateChange(this.DONE);
|
|
693
|
-
});
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
abort() {
|
|
697
|
-
if (this._currentRequest) {
|
|
698
|
-
this._currentRequest.cancel();
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
addEventListener(eventName, handler) {
|
|
703
|
-
const handlers = this._listeners.get(eventName) || [];
|
|
704
|
-
handlers.push(handler);
|
|
705
|
-
this._listeners.set(eventName, handlers);
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
removeEventListener(eventName, toDetach) {
|
|
709
|
-
let handlers = this._listeners.get(eventName) || [];
|
|
710
|
-
handlers = handlers.filter((handler) => handler !== toDetach);
|
|
711
|
-
this._listeners.set(eventName, handlers);
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
emitEvent(eventName, ...args) {
|
|
715
|
-
const handlers = this._listeners.get(eventName) || [];
|
|
716
|
-
handlers.forEach((handler) => {
|
|
717
|
-
handler.apply(this, ...args);
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
dispatchEvent(event) {
|
|
722
|
-
return false;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
_updateReadyStateChange(state) {
|
|
726
|
-
this._readyState = state;
|
|
727
|
-
if (this.onreadystatechange) {
|
|
728
|
-
this.onreadystatechange();
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
export class FormData {
|
|
734
|
-
constructor() {
|
|
735
|
-
this._data = new Map();
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
append(name, value) {
|
|
739
|
-
this._data.set(name, value);
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
toString() {
|
|
743
|
-
let arr = [];
|
|
744
|
-
this._data.forEach(function (value, name, map) {
|
|
745
|
-
arr.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
|
|
746
|
-
});
|
|
747
|
-
return arr.join("&");
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
export class Blob {
|
|
752
|
-
constructor(chunks = [], opts = {}) {
|
|
753
|
-
this[Symbol.toStringTag] = "Blob";
|
|
754
|
-
const dataChunks = [];
|
|
755
|
-
for (const chunk of chunks) {
|
|
756
|
-
if (chunk instanceof Blob) {
|
|
757
|
-
dataChunks.push(chunk._buffer);
|
|
758
|
-
} else if (typeof chunk === "string") {
|
|
759
|
-
const textEncoder = new TextEncoder();
|
|
760
|
-
dataChunks.push(textEncoder.encode(chunk));
|
|
761
|
-
} else if (chunk instanceof DataView) {
|
|
762
|
-
dataChunks.push(new Uint8Array(chunk.buffer.slice(0)));
|
|
763
|
-
} else if (chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk)) {
|
|
764
|
-
dataChunks.push(new Uint8Array(ArrayBuffer.isView(chunk)
|
|
765
|
-
? chunk.buffer.slice(0)
|
|
766
|
-
: chunk.slice(0)));
|
|
767
|
-
} else {
|
|
768
|
-
const textEncoder = new TextEncoder();
|
|
769
|
-
dataChunks.push(textEncoder.encode(String(chunk)));
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
const size = dataChunks.reduce((size, chunk) => size + chunk.byteLength, 0);
|
|
773
|
-
const buffer = new Uint8Array(size);
|
|
774
|
-
let offset = 0;
|
|
775
|
-
for (let i = 0; i < dataChunks.length; i++) {
|
|
776
|
-
const chunk = dataChunks[i];
|
|
777
|
-
buffer.set(chunk, offset);
|
|
778
|
-
offset += chunk.byteLength;
|
|
779
|
-
}
|
|
780
|
-
this._buffer = buffer;
|
|
781
|
-
this._size = this._buffer.byteLength;
|
|
782
|
-
this._type = opts.type || "";
|
|
783
|
-
|
|
784
|
-
if(this._type === ''){
|
|
785
|
-
var arr = buffer.subarray(0, 4);
|
|
786
|
-
var header = '';
|
|
787
|
-
for (var i = 0; i < arr.length; i++) {
|
|
788
|
-
header += arr[i].toString(16);
|
|
789
|
-
}
|
|
790
|
-
// Check the file signature against known types
|
|
791
|
-
var type = '';
|
|
792
|
-
switch (header) {
|
|
793
|
-
case '89504e47':
|
|
794
|
-
this._type = 'image/png';
|
|
795
|
-
break;
|
|
796
|
-
case '47494638':
|
|
797
|
-
this._type = 'image/gif';
|
|
798
|
-
break;
|
|
799
|
-
case 'ffd8ffe0':
|
|
800
|
-
case 'ffd8ffe1':
|
|
801
|
-
case 'ffd8ffdb':
|
|
802
|
-
this._type = 'image/jpg';
|
|
803
|
-
break;
|
|
804
|
-
case 'ffd8ffe2':
|
|
805
|
-
this._type = 'image/jpeg';
|
|
806
|
-
break;
|
|
807
|
-
case '25504446':
|
|
808
|
-
this._type = 'application/pdf';
|
|
809
|
-
break;
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
if (/[^\u0020-\u007E]/.test(this._type)) {
|
|
813
|
-
this._type = "";
|
|
814
|
-
} else {
|
|
815
|
-
this._type = this._type.toLowerCase();
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
get size() {
|
|
820
|
-
return this._size;
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
get type() {
|
|
824
|
-
return this._type;
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
arrayBuffer() {
|
|
828
|
-
return Promise.resolve(this._buffer);
|
|
829
|
-
}
|
|
830
|
-
|
|
831
|
-
text() {
|
|
832
|
-
const textDecoder = new TextDecoder();
|
|
833
|
-
return Promise.resolve(textDecoder.decode(this._buffer));
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
slice(start, end, type) {
|
|
837
|
-
const slice = this._buffer.slice(start || 0, end || this._buffer.length);
|
|
838
|
-
return new Blob([slice], {type: type});
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
stream() {
|
|
842
|
-
throw new Error("stream is currently not supported");
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
toString() {
|
|
846
|
-
return "[object Blob]";
|
|
847
|
-
}
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
Blob.InternalAccessor = class {
|
|
851
|
-
static getBuffer(blob) {
|
|
852
|
-
return blob._buffer;
|
|
853
|
-
}
|
|
854
|
-
};
|
|
855
|
-
|
|
856
|
-
export class File extends Blob {
|
|
857
|
-
constructor(chunks, name, opts = {}) {
|
|
858
|
-
super(chunks, opts);
|
|
859
|
-
this[Symbol.toStringTag] = "File";
|
|
860
|
-
this._name = name.replace(/\//g, ":");
|
|
861
|
-
this._lastModified =
|
|
862
|
-
opts.lastModified
|
|
863
|
-
? new Date(opts.lastModified).valueOf()
|
|
864
|
-
: Date.now();
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
get name() {
|
|
868
|
-
return this._name;
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
get lastModified() {
|
|
872
|
-
return this._lastModified;
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
toString() {
|
|
876
|
-
return "[object File]";
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
export class FileReader {
|
|
881
|
-
constructor() {
|
|
882
|
-
this.EMPTY = 0;
|
|
883
|
-
this.LOADING = 1;
|
|
884
|
-
this.DONE = 2;
|
|
885
|
-
this._listeners = new Map();
|
|
886
|
-
this[Symbol.toStringTag] = "FileReader";
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
get readyState() {
|
|
890
|
-
return this._readyState;
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
get result() {
|
|
894
|
-
return this._result;
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
_array2base64(input) {
|
|
898
|
-
var byteToCharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
899
|
-
var output = [];
|
|
900
|
-
for (var i = 0; i < input.length; i += 3) {
|
|
901
|
-
var byte1 = input[i];
|
|
902
|
-
var haveByte2 = i + 1 < input.length;
|
|
903
|
-
var byte2 = haveByte2 ? input[i + 1] : 0;
|
|
904
|
-
var haveByte3 = i + 2 < input.length;
|
|
905
|
-
var byte3 = haveByte3 ? input[i + 2] : 0;
|
|
906
|
-
var outByte1 = byte1 >> 2;
|
|
907
|
-
var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
|
|
908
|
-
var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6);
|
|
909
|
-
var outByte4 = byte3 & 0x3F;
|
|
910
|
-
if (!haveByte3) {
|
|
911
|
-
outByte4 = 64;
|
|
912
|
-
if (!haveByte2) {
|
|
913
|
-
outByte3 = 64;
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
output.push(byteToCharMap[outByte1], byteToCharMap[outByte2], byteToCharMap[outByte3], byteToCharMap[outByte4]);
|
|
917
|
-
}
|
|
918
|
-
return output.join("");
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
_read(blob, kind) {
|
|
922
|
-
if (!(blob instanceof Blob)) {
|
|
923
|
-
throw new TypeError(`Failed to execute '${kind}' on 'FileReader': parameter 1 is not of type 'Blob'.`);
|
|
924
|
-
}
|
|
925
|
-
this._result = "";
|
|
926
|
-
setTimeout(() => {
|
|
927
|
-
this._readyState = this.LOADING;
|
|
928
|
-
this.emitEvent("load");
|
|
929
|
-
this.emitEvent("loadend");
|
|
930
|
-
});
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
emitEvent(eventName, ...args) {
|
|
934
|
-
if (isFunction(this["on" + eventName])) {
|
|
935
|
-
this["on" + eventName](...args);
|
|
936
|
-
}
|
|
937
|
-
let handlers = this._listeners.get(eventName) || [];
|
|
938
|
-
handlers.forEach((handler) => {
|
|
939
|
-
handler(this, ...args);
|
|
940
|
-
});
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
addEventListener(eventName, handler) {
|
|
944
|
-
if (["abort", "error", "load", "loadend", "loadstart", "progress"].indexOf(eventName) === -1) {
|
|
945
|
-
throw new Error("Event not supported: " + eventName);
|
|
946
|
-
}
|
|
947
|
-
let handlers = this._listeners.get(eventName) || [];
|
|
948
|
-
handlers.push(handler);
|
|
949
|
-
this._listeners.set(eventName, handlers);
|
|
950
|
-
}
|
|
951
|
-
|
|
952
|
-
removeEventListener(eventName, toDetach) {
|
|
953
|
-
let handlers = this._listeners.get(eventName) || [];
|
|
954
|
-
handlers = handlers.filter((handler) => handler !== toDetach);
|
|
955
|
-
this._listeners.set(eventName, handlers);
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
readAsDataURL(blob) {
|
|
959
|
-
this._read(blob, "readAsDataURL");
|
|
960
|
-
this._result = `data:${blob.type};base64,${this._array2base64(Blob.InternalAccessor.getBuffer(blob))}`;
|
|
961
|
-
}
|
|
962
|
-
|
|
963
|
-
readAsText(blob) {
|
|
964
|
-
this._read(blob, "readAsText");
|
|
965
|
-
const textDecoder = new TextDecoder();
|
|
966
|
-
this._result = textDecoder.decode(Blob.InternalAccessor.getBuffer(blob));
|
|
967
|
-
}
|
|
968
|
-
|
|
969
|
-
readAsArrayBuffer(blob) {
|
|
970
|
-
this._read(blob, "readAsArrayBuffer");
|
|
971
|
-
this._result = Blob.InternalAccessor.getBuffer(blob).buffer.slice(0);
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
abort() {
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
toString() {
|
|
978
|
-
return "[object FileReader]";
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
//# sourceMappingURL=TNSXMLHttpRequest.js.map
|