mphttpx 2.1.2 → 2.1.5-beta.1

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.
Files changed (41) hide show
  1. package/README.md +5 -0
  2. package/dist/cjs/encoding/TextDecoderP.js +25 -33
  3. package/dist/cjs/encoding/TextEncoderP.js +7 -5
  4. package/dist/cjs/event-system/AbortControllerP.js +1 -3
  5. package/dist/cjs/event-system/AbortSignalP.js +14 -15
  6. package/dist/cjs/event-system/CloseEventP.js +1 -2
  7. package/dist/cjs/event-system/CustomEventP.js +1 -0
  8. package/dist/cjs/event-system/EventP.js +17 -15
  9. package/dist/cjs/event-system/EventTargetP.js +14 -10
  10. package/dist/cjs/event-system/ProgressEventP.js +7 -6
  11. package/dist/cjs/fetch-api/HeadersP.js +7 -6
  12. package/dist/cjs/fetch-api/RequestP.js +8 -6
  13. package/dist/cjs/fetch-api/ResponseP.js +2 -3
  14. package/dist/cjs/fetch-api/fetchP.js +4 -16
  15. package/dist/cjs/file-system/BlobP.js +10 -16
  16. package/dist/cjs/file-system/FileReaderP.js +43 -46
  17. package/dist/cjs/helpers/handlers.js +4 -2
  18. package/dist/cjs/mini-program/WebSocketImpl.js +27 -25
  19. package/dist/cjs/mini-program/XMLHttpRequestImpl.js +193 -237
  20. package/dist/cjs/network/XMLHttpRequestEventTargetP.js +1 -3
  21. package/dist/esm/encoding/TextDecoderP.js +25 -33
  22. package/dist/esm/encoding/TextEncoderP.js +7 -5
  23. package/dist/esm/event-system/AbortControllerP.js +1 -3
  24. package/dist/esm/event-system/AbortSignalP.js +14 -15
  25. package/dist/esm/event-system/CloseEventP.js +1 -2
  26. package/dist/esm/event-system/CustomEventP.js +2 -1
  27. package/dist/esm/event-system/EventP.js +17 -15
  28. package/dist/esm/event-system/EventTargetP.js +14 -10
  29. package/dist/esm/event-system/ProgressEventP.js +7 -6
  30. package/dist/esm/fetch-api/HeadersP.js +7 -6
  31. package/dist/esm/fetch-api/RequestP.js +8 -6
  32. package/dist/esm/fetch-api/ResponseP.js +2 -3
  33. package/dist/esm/fetch-api/fetchP.js +4 -16
  34. package/dist/esm/file-system/BlobP.js +10 -16
  35. package/dist/esm/file-system/FileReaderP.js +43 -46
  36. package/dist/esm/helpers/handlers.js +4 -2
  37. package/dist/esm/mini-program/WebSocketImpl.js +27 -25
  38. package/dist/esm/mini-program/XMLHttpRequestImpl.js +193 -237
  39. package/dist/esm/network/XMLHttpRequestEventTargetP.js +1 -3
  40. package/dist/index.d.ts +5 -5
  41. package/package.json +1 -1
@@ -37,50 +37,55 @@ class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP {
37
37
  get responseText() { return state(this).responseText; }
38
38
  get responseType() { return state(this).responseType; }
39
39
  set responseType(value) {
40
- if (state(this).pos > 15 /* XHRCycle.RESPONSE_BODY_WAITING */) {
40
+ if (state(this).pos > 9 /* XHRCycle.HEADERS_RECEIVED */)
41
41
  throw new DOMExceptionP("Failed to set the 'responseType' property on 'XMLHttpRequest': The response type cannot be set if the object's state is LOADING or DONE.", "InvalidStateError");
42
- }
43
- state(this).responseType = XMLHttpRequestState.responseTypes.indexOf(value) > -1 ? value : "";
42
+ if (responseTypes.indexOf(value) > -1)
43
+ state(this).responseType = value;
44
44
  }
45
45
  get responseURL() { return state(this).responseURL; }
46
46
  get responseXML() { return null; }
47
47
  get status() { return state(this).status; }
48
48
  get statusText() {
49
- if (state(this).pos < 14 /* XHRCycle.RESPONSE_HEADERS_RECEIVED */)
49
+ if (state(this).pos < 9 /* XHRCycle.HEADERS_RECEIVED */)
50
50
  return "";
51
51
  return state(this).statusText || statusTextMap[this.status] || "unknown";
52
52
  }
53
53
  get timeout() { return state(this).timeout; }
54
54
  set timeout(value) { state(this).timeout = value >= 0 ? value : 0; }
55
55
  get upload() {
56
- if (!state(this).upload) {
56
+ if (!state(this).upload)
57
57
  state(this).upload = createXMLHttpRequestUpload();
58
- }
59
58
  return state(this).upload;
60
59
  }
61
60
  get withCredentials() { return state(this).withCredentials; }
62
- set withCredentials(value) { state(this).withCredentials = !!value; }
61
+ set withCredentials(value) {
62
+ if (state(this).pos < 2 /* XHRCycle.LOADSTART */)
63
+ state(this).withCredentials = !!value;
64
+ else
65
+ throw new TypeError("Failed to set the 'withCredentials' property on 'XMLHttpRequest': The value may only be set if the object's state is UNSENT or OPENED.");
66
+ }
63
67
  abort() {
64
- const requestTask = state(this).requestTask;
65
- clearRequest(state(this));
66
- switch (state(this).pos) {
67
- case 3 /* XHRCycle.SUSPEND */:
68
- state(this).requestHeaders = new HeadersP();
68
+ const s = state(this);
69
+ const requestTask = s.requestTask;
70
+ switch (s.pos) {
71
+ case 1 /* XHRCycle.OPENED */:
72
+ s.requestHeaders = new HeadersP();
69
73
  break;
70
- case 6 /* XHRCycle.UPLOAD_UPLOADING */:
74
+ case 2 /* XHRCycle.LOADSTART */:
75
+ case 3 /* XHRCycle.UPLOAD_LOADSTART */:
71
76
  execUploadAbort(this);
72
- execIdle(this);
77
+ execUnset(this);
73
78
  break;
74
- case 11 /* XHRCycle.UPLOAD_FINISHING */:
75
- case 13 /* XHRCycle.RESPONSE_HEADERS_WAITING */:
76
- case 15 /* XHRCycle.RESPONSE_BODY_WAITING */:
77
- case 17 /* XHRCycle.RESPONSE_BODY_RECEIVING */:
79
+ case 4 /* XHRCycle.UPLOAD_LOAD */:
80
+ case 8 /* XHRCycle.UPLOAD_LOADEND */:
81
+ case 9 /* XHRCycle.HEADERS_RECEIVED */:
82
+ case 10 /* XHRCycle.LOADING */:
78
83
  execAbort(this);
79
- case 19 /* XHRCycle.REQUEST_FINISHING */:
80
- case 25 /* XHRCycle.END */:
81
- execIdle(this);
84
+ case 11 /* XHRCycle.DONE */:
85
+ execUnset(this);
82
86
  break;
83
87
  }
88
+ clearRequest(s);
84
89
  safeAbort(requestTask);
85
90
  }
86
91
  getAllResponseHeaders() {
@@ -97,7 +102,7 @@ class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP {
97
102
  return null;
98
103
  return state(this).responseHeaders.get(name);
99
104
  }
100
- open(method, url, async, username, password) {
105
+ open(method, url, async = true, username = null, password = null) {
101
106
  checkArgsLength(arguments.length, 2, "XMLHttpRequest", "open");
102
107
  if (!async) {
103
108
  console.warn("Synchronous XMLHttpRequest is not supported because of its detrimental effects to the end user's experience.");
@@ -115,18 +120,18 @@ class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP {
115
120
  let auth = `Basic ${Uint8Array_toBase64(encode(_username + ":" + _password))}`;
116
121
  this.setRequestHeader("Authorization", auth);
117
122
  }
118
- execRequestOpened(this);
123
+ execOpened(this);
119
124
  }
120
125
  overrideMimeType(mime) {
121
126
  checkArgsLength(arguments.length, 1, "XMLHttpRequest", "overrideMimeType");
122
- if (state(this).pos === 3 /* XHRCycle.SUSPEND */) {
127
+ if (state(this).pos === 1 /* XHRCycle.OPENED */) {
123
128
  console.error(`TypeError: Failed to execute 'overrideMimeType' on 'XMLHttpRequest': mimeType ('${mime}') not implemented.`);
124
129
  }
125
130
  }
126
131
  send(body) {
127
132
  const s = state(this);
128
133
  const requestId = s.requestId;
129
- if (s.pos !== 3 /* XHRCycle.SUSPEND */) {
134
+ if (s.pos !== 1 /* XHRCycle.OPENED */) {
130
135
  throw new DOMExceptionP("Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.", "InvalidStateError");
131
136
  }
132
137
  const options = {
@@ -139,29 +144,30 @@ class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP {
139
144
  success: requestSuccess.bind(this, requestId),
140
145
  fail: requestFail.bind(this, requestId),
141
146
  };
142
- const launch = (function (data) {
147
+ const payload = (s.method !== "GET" && s.method !== "HEAD" && body !== null && body !== undefined) && new Payload(body);
148
+ if (payload && payload.type && !s.requestHeaders.has("Content-Type")) {
149
+ options.header["Content-Type"] = payload.type;
150
+ }
151
+ const request = (function (data) {
143
152
  if (requestId !== s.requestId)
144
153
  return;
154
+ if (s.pos !== 2 /* XHRCycle.LOADSTART */ && s.pos !== 8 /* XHRCycle.UPLOAD_LOADEND */)
155
+ return;
145
156
  options.data = data !== "" ? data : undefined;
146
157
  options.headers = options.header; // Alipay Mini Program
147
158
  s.requestTask = mp.request(options);
148
159
  }).bind(this);
149
- execLoadstart(this);
150
- if (s.method !== "GET" && s.method !== "HEAD" && body !== null && body !== undefined) {
151
- const payload = new Payload(body);
152
- if (!s.requestHeaders.has("Content-Type") && payload.type) {
153
- options.header["Content-Type"] = payload.type;
154
- }
155
- execUploadLoadstart(this, payload, launch);
156
- }
157
- else {
158
- thenExec(() => execResponseHeadersWaiting(this, "", launch));
159
- }
160
- checkRequestTimeout(this, requestId);
160
+ checkRequestTimeout(this);
161
+ const task = execLoadstart(this, payload || undefined);
162
+ if (!task)
163
+ request();
164
+ else
165
+ task.then(data => { if (data !== undefined)
166
+ request(data); });
161
167
  }
162
168
  setRequestHeader(name, value) {
163
169
  checkArgsLength(arguments.length, 2, "XMLHttpRequest", "setRequestHeader");
164
- if (state(this).pos !== 3 /* XHRCycle.SUSPEND */) {
170
+ if (state(this).pos !== 1 /* XHRCycle.OPENED */) {
165
171
  throw new DOMExceptionP("Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.", "InvalidStateError");
166
172
  }
167
173
  let _name = "" + name;
@@ -181,7 +187,7 @@ class XMLHttpRequestImpl extends XMLHttpRequestEventTargetP {
181
187
  }
182
188
  class XMLHttpRequestState {
183
189
  constructor(target) {
184
- this.pos = 1 /* XHRCycle.IDLE */;
190
+ this.pos = 0 /* XHRCycle.UNSET */;
185
191
  this.readyState = 0 /* UNSENT */;
186
192
  this.response = "";
187
193
  this.responseText = "";
@@ -202,7 +208,6 @@ class XMLHttpRequestState {
202
208
  this.attach = attachFn(target, getHandlers(target));
203
209
  }
204
210
  }
205
- XMLHttpRequestState.responseTypes = ["", "arraybuffer", "blob", "document", "json", "text"];
206
211
  function getHandlers(t) {
207
212
  return {
208
213
  onreadystatechange: (ev) => { executeFn(t, t.onreadystatechange, ev); },
@@ -216,38 +221,10 @@ function Headers_toDict(headers) {
216
221
  headers.forEach((value, name) => { dict[name] = value; });
217
222
  return dict;
218
223
  }
224
+ const responseTypes = ["", "arraybuffer", "blob", "document", "json", "text"];
219
225
  function normalizeDataType(responseType) {
220
226
  return (responseType === "blob" || responseType === "arraybuffer") ? "arraybuffer" : "text";
221
227
  }
222
- // Alipay Mini Program
223
- function safeAbort(task) {
224
- if (task && typeof task === "object") {
225
- if ("abort" in task && typeof task.abort === "function")
226
- task.abort();
227
- }
228
- }
229
- function checkRequestTimeout(xhr, requestId) {
230
- const whenTimeout = () => {
231
- if (state(xhr).requestId !== requestId)
232
- return;
233
- const requestTask = state(xhr).requestTask;
234
- switch (state(xhr).pos) {
235
- case 6 /* XHRCycle.UPLOAD_UPLOADING */:
236
- execUploadTimeout(xhr);
237
- break;
238
- case 11 /* XHRCycle.UPLOAD_FINISHING */:
239
- case 13 /* XHRCycle.RESPONSE_HEADERS_WAITING */:
240
- case 15 /* XHRCycle.RESPONSE_BODY_WAITING */:
241
- case 17 /* XHRCycle.RESPONSE_BODY_RECEIVING */:
242
- execTimeout(xhr);
243
- break;
244
- }
245
- safeAbort(requestTask);
246
- };
247
- if (xhr.timeout) {
248
- state(xhr).timeoutId = setTimeout(() => { whenTimeout(); }, xhr.timeout);
249
- }
250
- }
251
228
  function clearRequest(state) {
252
229
  state.requestTask = null;
253
230
  clearRequestTimeout(state);
@@ -265,14 +242,29 @@ function clearRequestTimeout(state) {
265
242
  state.timeoutId = 0;
266
243
  }
267
244
  }
245
+ function checkRequestTimeout(xhr) {
246
+ if (!xhr.timeout)
247
+ return;
248
+ const s = state(xhr);
249
+ const whenTimeout = () => {
250
+ switch (s.pos) {
251
+ case 2 /* XHRCycle.LOADSTART */:
252
+ case 3 /* XHRCycle.UPLOAD_LOADSTART */:
253
+ execUploadTimeout(xhr);
254
+ break;
255
+ case 8 /* XHRCycle.UPLOAD_LOADEND */:
256
+ execTimeout(xhr);
257
+ break;
258
+ }
259
+ };
260
+ s.timeoutId = setTimeout(() => { let task = s.requestTask; whenTimeout(); safeAbort(task); }, xhr.timeout);
261
+ }
268
262
  function requestSuccess(requestId, res) {
269
263
  if (requestId !== state(this).requestId)
270
264
  return;
271
- execResponseHeadersReceived(this, res);
265
+ execHeadersReceived(this, res);
272
266
  }
273
267
  function requestFail(requestId, err) {
274
- if (requestId !== state(this).requestId)
275
- return;
276
268
  // Alipay Mini Program
277
269
  if (("header" in err && "statusCode" in err) || ("headers" in err && "status" in err)) {
278
270
  return requestSuccess.call(this, requestId, {
@@ -281,6 +273,8 @@ function requestFail(requestId, err) {
281
273
  data: "data" in err ? err.data || "" : "",
282
274
  });
283
275
  }
276
+ if (requestId !== state(this).requestId)
277
+ return;
284
278
  execError(this, err);
285
279
  }
286
280
  function setReadyStateAndNotify(xhr, value) {
@@ -290,243 +284,205 @@ function setReadyStateAndNotify(xhr, value) {
290
284
  }
291
285
  }
292
286
  function getResponse(type, data) {
293
- if (type === "json") {
294
- return typeof data === "string"
295
- ? JSON.parse(data)
296
- : isArrayBuffer(data)
297
- ? JSON.parse(convertBack("text", data))
298
- : data;
287
+ let temp = (typeof data === "string" || isArrayBuffer(data))
288
+ ? data
289
+ : JSON.stringify(data);
290
+ switch (type) {
291
+ case "":
292
+ case "text":
293
+ return typeof temp === "string" ? temp : decode(temp);
294
+ case "arraybuffer":
295
+ return isArrayBuffer(temp) ? temp : encode(temp).buffer;
296
+ case "json":
297
+ return typeof data === "string"
298
+ ? JSON.parse(data)
299
+ : isArrayBuffer(data)
300
+ ? JSON.parse(decode(data))
301
+ : data;
302
+ case "blob":
303
+ return new BlobP([temp]);
304
+ default:
305
+ return temp;
299
306
  }
300
- return convertBack(type, (typeof data === "string" || isArrayBuffer(data)) ? data : JSON.stringify(data));
301
307
  }
302
- function convertBack(type, data) {
303
- if (!type || type === "text") {
304
- return typeof data === "string" ? data : decode(data);
305
- }
306
- else if (type === "arraybuffer") {
307
- return isArrayBuffer(data) ? data : encode(data).buffer;
308
- }
309
- else if (type === "blob") {
310
- return new BlobP([data !== null && data !== void 0 ? data : ""]);
311
- }
312
- else {
313
- return data !== null && data !== void 0 ? data : "";
308
+ // Alipay Mini Program
309
+ function safeAbort(task) {
310
+ if (task && typeof task === "object") {
311
+ if ("abort" in task && typeof task.abort === "function")
312
+ task.abort();
314
313
  }
315
314
  }
316
- function thenExec(fn) {
317
- Promise.resolve().then(fn);
315
+ function execUnset(xhr) {
316
+ state(xhr).pos = 0 /* XHRCycle.UNSET */;
317
+ state(xhr).readyState = 0 /* UNSENT */;
318
318
  }
319
- function execIdle(xhr) {
320
- state(xhr).pos = 1 /* XHRCycle.IDLE */;
321
- state(xhr).readyState = 0 /* UNSET */;
322
- }
323
- function execRequestOpened(xhr) {
324
- state(xhr).pos = 2 /* XHRCycle.REQUEST_OPENED */;
319
+ function execOpened(xhr) {
320
+ state(xhr).pos = 1 /* XHRCycle.OPENED */;
325
321
  state(xhr).requestId += 1;
326
322
  setReadyStateAndNotify(xhr, 1 /* OPENED */);
327
- execSuspend(xhr);
328
- }
329
- function execSuspend(xhr) {
330
- state(xhr).pos = 3 /* XHRCycle.SUSPEND */;
331
323
  }
332
- // external call
333
- function execLoadstart(xhr) {
334
- state(xhr).pos = 4 /* XHRCycle.LOADSTART */;
324
+ function execLoadstart(xhr, payload) {
325
+ state(xhr).pos = 2 /* XHRCycle.LOADSTART */;
335
326
  emitProgressEvent(xhr, "loadstart");
327
+ return payload && execUploadLoadstart(xhr, payload);
336
328
  }
337
- // external call
338
- function execUploadLoadstart(xhr, payload, callback) {
339
- state(xhr).pos = 5 /* XHRCycle.UPLOAD_LOADSTART */;
340
- execUploadUploading(xhr, payload, callback);
341
- if (state(xhr).upload)
329
+ function execUploadLoadstart(xhr, payload) {
330
+ let s = state(xhr);
331
+ let requestId = s.requestId;
332
+ if (s.pos !== 2 /* XHRCycle.LOADSTART */)
333
+ return;
334
+ s.pos = 3 /* XHRCycle.UPLOAD_LOADSTART */;
335
+ if (s.upload)
342
336
  emitProgressEvent(xhr.upload, "loadstart", 0, payload.size);
343
- }
344
- function execUploadUploading(xhr, payload, callback) {
345
- state(xhr).pos = 6 /* XHRCycle.UPLOAD_UPLOADING */;
346
- const requestId = state(xhr).requestId;
347
- payload.promise
348
- .then(data => {
349
- if (requestId !== state(xhr).requestId)
350
- return;
351
- execUploadLoad(xhr, payload, data, callback);
352
- })
353
- .catch(e => {
354
- if (requestId !== state(xhr).requestId)
355
- return;
337
+ return payload.promise
338
+ .then(r => { if (requestId === s.requestId) {
339
+ return execUploadLoad(xhr, payload, r);
340
+ } })
341
+ .catch(e => { if (requestId === s.requestId) {
356
342
  execUploadError(xhr);
357
343
  console.error(e);
358
- });
344
+ } });
359
345
  }
360
- function execUploadLoad(xhr, payload, data, callback) {
361
- if (state(xhr).pos !== 6 /* XHRCycle.UPLOAD_UPLOADING */)
346
+ function execUploadLoad(xhr, payload, data) {
347
+ let s = state(xhr);
348
+ if (s.pos !== 3 /* XHRCycle.UPLOAD_LOADSTART */)
362
349
  return;
363
- state(xhr).pos = 7 /* XHRCycle.UPLOAD_LOAD */;
364
- execUploadFinishing(xhr, payload, data, callback);
365
- if (state(xhr).upload && payload.size > 0)
350
+ s.pos = 4 /* XHRCycle.UPLOAD_LOAD */;
351
+ if (s.upload && payload.size > 0)
366
352
  emitProgressEvent(xhr.upload, "load", payload.size, payload.size);
353
+ return execUploadLoadend(xhr, "load", { payload, data });
367
354
  }
368
355
  function execUploadAbort(xhr) {
369
- state(xhr).pos = 8 /* XHRCycle.UPLOAD_ABORT */;
356
+ state(xhr).pos = 5 /* XHRCycle.UPLOAD_ABORT */;
370
357
  setReadyStateAndNotify(xhr, 4 /* DONE */);
371
358
  if (state(xhr).upload)
372
359
  emitProgressEvent(xhr.upload, "abort");
373
360
  execUploadLoadend(xhr, "abort");
374
361
  }
375
362
  function execUploadError(xhr) {
376
- if (state(xhr).pos !== 6 /* XHRCycle.UPLOAD_UPLOADING */)
363
+ let s = state(xhr);
364
+ if (s.pos !== 3 /* XHRCycle.UPLOAD_LOADSTART */)
377
365
  return;
378
- state(xhr).pos = 9 /* XHRCycle.UPLOAD_ERROR */;
366
+ s.pos = 6 /* XHRCycle.UPLOAD_ERROR */;
379
367
  setReadyStateAndNotify(xhr, 4 /* DONE */);
380
- if (state(xhr).upload)
368
+ if (s.upload)
381
369
  emitProgressEvent(xhr.upload, "error");
382
370
  execUploadLoadend(xhr, "error");
383
371
  }
384
372
  function execUploadTimeout(xhr) {
385
- state(xhr).pos = 10 /* XHRCycle.UPLOAD_TIMEOUT */;
373
+ state(xhr).pos = 7 /* XHRCycle.UPLOAD_TIMEOUT */;
386
374
  setReadyStateAndNotify(xhr, 4 /* DONE */);
387
375
  if (state(xhr).upload)
388
376
  emitProgressEvent(xhr.upload, "timeout");
389
377
  execUploadLoadend(xhr, "timeout");
390
378
  }
391
- function execUploadFinishing(xhr, payload, data, callback) {
392
- state(xhr).pos = 11 /* XHRCycle.UPLOAD_FINISHING */;
393
- thenExec(() => execUploadLoadend(xhr, "load", payload, data, callback));
394
- }
395
- function execUploadLoadend(xhr, type, payload, data, callback) {
396
- if (type === "load") {
397
- if (state(xhr).pos === 11 /* XHRCycle.UPLOAD_FINISHING */) {
398
- state(xhr).pos = 12 /* XHRCycle.UPLOAD_LOADEND */;
399
- execResponseHeadersWaiting(xhr, data, callback);
400
- }
401
- if (state(xhr).upload && payload.size > 0) {
402
- emitProgressEvent(xhr.upload, "loadend", payload.size, payload.size);
403
- }
404
- }
405
- else {
406
- state(xhr).pos = 12 /* XHRCycle.UPLOAD_LOADEND */;
407
- if (state(xhr).upload) {
408
- emitProgressEvent(xhr.upload, "loadend");
409
- }
410
- switch (type) {
411
- case "abort":
412
- execAbort(xhr);
413
- break;
414
- case "error":
415
- execError(xhr);
416
- break;
417
- case "timeout":
418
- execTimeout(xhr);
419
- break;
420
- }
421
- }
422
- }
423
- function execResponseHeadersWaiting(xhr, data, callback) {
424
- if (state(xhr).pos !== 4 /* XHRCycle.LOADSTART */ && state(xhr).pos !== 12 /* XHRCycle.UPLOAD_LOADEND */)
425
- return;
426
- state(xhr).pos = 13 /* XHRCycle.RESPONSE_HEADERS_WAITING */;
427
- try {
428
- callback(data);
379
+ function execUploadLoadend(xhr, type, ctx) {
380
+ let s = state(xhr);
381
+ if ((type === "load" && s.pos === 4 /* XHRCycle.UPLOAD_LOAD */) || type !== "load")
382
+ s.pos = 8 /* XHRCycle.UPLOAD_LOADEND */;
383
+ if (s.upload) {
384
+ let l = ctx ? ctx.payload.size : 0;
385
+ emitProgressEvent(xhr.upload, "loadend", l, l);
429
386
  }
430
- catch (e) {
431
- execError(xhr);
432
- console.error(e);
387
+ switch (type) {
388
+ case "load": return ctx && ctx.data;
389
+ case "abort":
390
+ execAbort(xhr);
391
+ break;
392
+ case "error":
393
+ execError(xhr);
394
+ break;
395
+ case "timeout":
396
+ execTimeout(xhr);
397
+ break;
433
398
  }
434
399
  }
435
- // external call
436
- function execResponseHeadersReceived(xhr, res) {
437
- if (state(xhr).pos !== 13 /* XHRCycle.RESPONSE_HEADERS_WAITING */)
400
+ function execHeadersReceived(xhr, res) {
401
+ let s = state(xhr);
402
+ if (s.pos !== 2 /* XHRCycle.LOADSTART */ && s.pos !== 8 /* XHRCycle.UPLOAD_LOADEND */)
438
403
  return;
439
- state(xhr).pos = 14 /* XHRCycle.RESPONSE_HEADERS_RECEIVED */;
440
- state(xhr).responseURL = state(xhr).requestURL;
441
- state(xhr).status = "statusCode" in res ? res.statusCode : "status" in res ? res.status : 200;
442
- state(xhr).responseHeaders = new HeadersP(("header" in res ? res.header : "headers" in res ? res.headers : {}));
443
- execResponseBodyWaiting(xhr, res);
404
+ s.pos = 9 /* XHRCycle.HEADERS_RECEIVED */;
405
+ s.responseURL = s.requestURL;
406
+ s.status = "statusCode" in res ? res.statusCode : "status" in res ? res.status : 200;
407
+ s.responseHeaders = new HeadersP(("header" in res ? res.header : "headers" in res ? res.headers : {}));
444
408
  setReadyStateAndNotify(xhr, 2 /* HEADERS_RECEIVED */);
409
+ execLoading(xhr, res);
445
410
  }
446
- function execResponseBodyWaiting(xhr, res) {
447
- state(xhr).pos = 15 /* XHRCycle.RESPONSE_BODY_WAITING */;
448
- thenExec(() => execResponseBodyLoading(xhr, res));
449
- }
450
- function execResponseBodyLoading(xhr, res) {
451
- if (state(xhr).pos !== 15 /* XHRCycle.RESPONSE_BODY_WAITING */)
411
+ function execLoading(xhr, res) {
412
+ if (state(xhr).pos !== 9 /* XHRCycle.HEADERS_RECEIVED */)
452
413
  return;
453
- state(xhr).pos = 16 /* XHRCycle.RESPONSE_BODY_LOADING */;
454
- execResponseBodyReceiving(xhr, res);
414
+ state(xhr).pos = 10 /* XHRCycle.LOADING */;
455
415
  setReadyStateAndNotify(xhr, 3 /* LOADING */);
416
+ execDone(xhr, res);
456
417
  }
457
- function execResponseBodyReceiving(xhr, res) {
458
- state(xhr).pos = 17 /* XHRCycle.RESPONSE_BODY_RECEIVING */;
418
+ function execDone(xhr, res) {
419
+ let s = state(xhr);
420
+ if (s.pos !== 10 /* XHRCycle.LOADING */)
421
+ return;
422
+ s.pos = 11 /* XHRCycle.DONE */;
459
423
  const setResponse = () => {
460
424
  var _a;
461
- if (state(xhr).pos !== 17 /* XHRCycle.RESPONSE_BODY_RECEIVING */)
462
- return;
463
- state(xhr).response = getResponse(xhr.responseType, (_a = res.data) !== null && _a !== void 0 ? _a : "");
425
+ s.response = getResponse(xhr.responseType, (_a = res.data) !== null && _a !== void 0 ? _a : "");
464
426
  if (!xhr.responseType || xhr.responseType === "text")
465
- state(xhr).responseText = xhr.response;
466
- thenExec(() => execRequestDone(xhr));
427
+ s.responseText = xhr.response;
428
+ setReadyStateAndNotify(xhr, 4 /* DONE */);
429
+ execLoad(xhr);
467
430
  };
468
- thenExec(() => {
469
- try {
470
- setResponse();
471
- }
472
- catch (e) {
473
- execError(xhr);
474
- console.error(e);
475
- }
476
- });
477
- }
478
- function execRequestDone(xhr) {
479
- if (state(xhr).pos !== 17 /* XHRCycle.RESPONSE_BODY_RECEIVING */)
480
- return;
481
- state(xhr).pos = 18 /* XHRCycle.REQUEST_DONE */;
482
- execRequestFinishing(xhr);
483
- setReadyStateAndNotify(xhr, 4 /* DONE */);
484
- }
485
- function execRequestFinishing(xhr) {
486
- state(xhr).pos = 19 /* XHRCycle.REQUEST_FINISHING */;
487
- thenExec(() => execLoad(xhr));
431
+ try {
432
+ setResponse();
433
+ }
434
+ catch (e) {
435
+ execError(xhr);
436
+ console.error(e);
437
+ }
488
438
  }
489
439
  function execLoad(xhr) {
490
- if (state(xhr).pos !== 19 /* XHRCycle.REQUEST_FINISHING */)
440
+ let s = state(xhr);
441
+ if (s.pos !== 11 /* XHRCycle.DONE */)
491
442
  return;
492
- state(xhr).pos = 20 /* XHRCycle.LOAD */;
493
- let contentLength = parseInt(state(xhr).responseHeaders.get("Content-Length") || "0");
443
+ s.pos = 12 /* XHRCycle.LOAD */;
444
+ let lstr = s.responseHeaders.get("Content-Length");
445
+ let contentLength = lstr ? (parseInt(lstr) || 0) : 0;
494
446
  emitProgressEvent(xhr, "load", contentLength, contentLength);
495
447
  execLoadend(xhr, contentLength);
496
448
  }
497
449
  function execAbort(xhr) {
498
- state(xhr).pos = 21 /* XHRCycle.ABORT */;
450
+ state(xhr).pos = 13 /* XHRCycle.ABORT */;
499
451
  setReadyStateAndNotify(xhr, 4 /* DONE */);
500
452
  emitProgressEvent(xhr, "abort");
501
453
  execLoadend(xhr);
502
454
  }
503
455
  function execError(xhr, err) {
504
- if (state(xhr).pos !== 12 /* XHRCycle.UPLOAD_LOADEND */ && state(xhr).pos !== 13 /* XHRCycle.RESPONSE_HEADERS_WAITING */ && state(xhr).pos !== 17 /* XHRCycle.RESPONSE_BODY_RECEIVING */)
505
- return;
506
- state(xhr).pos = 22 /* XHRCycle.ERROR */;
456
+ let s = state(xhr);
457
+ switch (s.pos) {
458
+ case 2 /* XHRCycle.LOADSTART */:
459
+ case 8 /* XHRCycle.UPLOAD_LOADEND */:
460
+ case 11 /* XHRCycle.DONE */:
461
+ break;
462
+ default:
463
+ return;
464
+ }
465
+ s.pos = 14 /* XHRCycle.ERROR */;
507
466
  if (err) {
508
- state(xhr).status = 0;
509
- state(xhr).statusText = "errMsg" in err ? err.errMsg : "errorMessage" in err ? err.errorMessage : "";
467
+ s.status = 0;
468
+ s.statusText = "errMsg" in err ? err.errMsg : "errorMessage" in err ? err.errorMessage : "";
510
469
  }
511
470
  setReadyStateAndNotify(xhr, 4 /* DONE */);
512
471
  emitProgressEvent(xhr, "error");
513
472
  execLoadend(xhr);
514
473
  }
515
474
  function execTimeout(xhr) {
516
- state(xhr).pos = 23 /* XHRCycle.TIMEOUT */;
475
+ state(xhr).pos = 15 /* XHRCycle.TIMEOUT */;
517
476
  setReadyStateAndNotify(xhr, 4 /* DONE */);
518
477
  emitProgressEvent(xhr, "timeout");
519
478
  execLoadend(xhr);
520
479
  }
521
480
  function execLoadend(xhr, contentLength = 0) {
522
- state(xhr).pos = 24 /* XHRCycle.LOADEND */;
481
+ let s = state(xhr);
482
+ s.pos = 16 /* XHRCycle.LOADEND */;
483
+ s.requestTask = null;
484
+ clearRequestTimeout(s);
523
485
  emitProgressEvent(xhr, "loadend", contentLength, contentLength);
524
- execEnd(xhr);
525
- }
526
- function execEnd(xhr) {
527
- state(xhr).pos = 25 /* XHRCycle.END */;
528
- state(xhr).requestTask = null;
529
- clearRequestTimeout(state(xhr));
530
486
  }
531
487
  const XMLHttpRequestE = (typeof XMLHttpRequest !== "undefined" && XMLHttpRequest) || XMLHttpRequestImpl;
532
488
 
@@ -31,9 +31,7 @@ class XMLHttpRequestEventTargetP extends EventTargetP {
31
31
  }
32
32
  /** @internal */
33
33
  class XMLHttpRequestEventTargetState {
34
- /**
35
- * @param target XMLHttpRequestEventTarget
36
- */
34
+ /** @param target XMLHttpRequestEventTarget */
37
35
  constructor(target) {
38
36
  this.onabort = null;
39
37
  this.onerror = null;
package/dist/index.d.ts CHANGED
@@ -65,7 +65,7 @@ declare class FileReaderP extends EventTargetP implements FileReader {
65
65
  get EMPTY(): 0;
66
66
  get LOADING(): 1;
67
67
  get DONE(): 2;
68
- get error(): (DOMException | null);
68
+ get error(): DOMException | null;
69
69
  get readyState(): 0 | 1 | 2;
70
70
  get result(): string | ArrayBuffer | null;
71
71
  abort(): void;
@@ -105,9 +105,9 @@ declare class URLSearchParamsP implements URLSearchParams {
105
105
  set(name: string, value: string): void;
106
106
  sort(): void;
107
107
  forEach(callbackfn: (value: string, key: string, parent: URLSearchParams) => void, thisArg?: any): void;
108
- entries(): ArrayIterator<[string, string]>;
109
- keys(): ArrayIterator<string>;
110
- values(): ArrayIterator<string>;
108
+ entries(): URLSearchParamsIterator<[string, string]>;
109
+ keys(): URLSearchParamsIterator<string>;
110
+ values(): URLSearchParamsIterator<string>;
111
111
  [Symbol.iterator]: () => URLSearchParamsIterator<[string, string]>;
112
112
  toString(): string;
113
113
  }
@@ -264,7 +264,7 @@ declare class EventP implements Event {
264
264
  set returnValue(value: boolean);
265
265
  get srcElement(): EventTarget | null;
266
266
  get target(): EventTarget | null;
267
- get timeStamp(): number;
267
+ get timeStamp(): DOMHighResTimeStamp;
268
268
  get type(): string;
269
269
  composedPath(): EventTarget[];
270
270
  initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void;