mphttpx 2.1.2 → 2.1.5-beta.2

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