mphttpx 2.1.1 → 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 -17
  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 -17
  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
package/README.md CHANGED
@@ -8,6 +8,7 @@ This allows web code to be reused in other environments (such as mini-programs).
8
8
 
9
9
  - [MPHTTPX](#mphttpx)
10
10
  - [Table of Contents](#table-of-contents)
11
+ - [Translations](#translations)
11
12
  - [Features](#features)
12
13
  - [Installation](#installation)
13
14
  - [Mini-Program Support](#mini-program-support)
@@ -62,6 +63,10 @@ This allows web code to be reused in other environments (such as mini-programs).
62
63
  - [Node.js](#nodejs)
63
64
  - [License](#license)
64
65
 
66
+ ## Translations
67
+
68
+ * [中文文档](README.zh-CN.md)
69
+
65
70
  ## Features
66
71
 
67
72
  - **TextEncoder**
@@ -3,20 +3,19 @@
3
3
  var utils = require('../utils.js');
4
4
  var isArrayBuffer = require('../helpers/isArrayBuffer.js');
5
5
 
6
+ const validLabels = ["utf-8", "utf8", "unicode-1-1-utf-8"];
6
7
  class TextDecoderP {
7
8
  constructor(label = "utf-8", options) {
8
- if (["utf-8", "utf8", "unicode-1-1-utf-8"].indexOf(("" + label).toLowerCase()) === -1) {
9
+ if (validLabels.indexOf((typeof label === "string" ? label : ("" + label)).toLowerCase()) === -1) {
9
10
  throw new RangeError(`Failed to construct 'TextDecoder': encoding ('${label}') not implemented.`);
10
11
  }
11
- utils.setState(this, "__TextDecoder__", new TextDecoderState());
12
- state(this).fatal = !!(options === null || options === void 0 ? void 0 : options.fatal);
13
- state(this).ignoreBOM = !!(options === null || options === void 0 ? void 0 : options.ignoreBOM);
12
+ utils.setState(this, "__TextDecoder__", new TextDecoderState(options));
14
13
  }
15
14
  get encoding() { return "utf-8"; }
16
15
  get fatal() { return state(this).fatal; }
17
16
  get ignoreBOM() { return state(this).ignoreBOM; }
18
17
  decode(input, options) {
19
- return decodeText(state(this), input, options);
18
+ return decodeText(input, options, state(this));
20
19
  }
21
20
  /** @internal */ toString() { return "[object TextDecoder]"; }
22
21
  /** @internal */ get [utils.SymbolP.toStringTag]() { return "TextDecoder"; }
@@ -24,30 +23,18 @@ class TextDecoderP {
24
23
  }
25
24
  /** @internal */
26
25
  class TextDecoderState {
27
- constructor() {
28
- this.fatal = false;
29
- this.ignoreBOM = false;
26
+ constructor({ fatal = false, ignoreBOM = false } = {}) {
30
27
  this.initial = 0;
31
28
  this.partial = [];
29
+ this.fatal = !!fatal;
30
+ this.ignoreBOM = !!ignoreBOM;
32
31
  }
33
32
  }
34
33
  function state(target) {
35
34
  return target.__TextDecoder__;
36
35
  }
37
- function decodeText(settings, input, options) {
38
- let bytes;
39
- if (input !== undefined) {
40
- if (isArrayBuffer.isArrayBuffer(input)) {
41
- bytes = new Uint8Array(input);
42
- }
43
- else if (ArrayBuffer.isView(input)) {
44
- bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
45
- }
46
- else {
47
- throw new TypeError("Input could not be converted to any of: ArrayBufferView, ArrayBuffer.");
48
- }
49
- }
50
- else {
36
+ function decodeText(input, { stream = false } = {}, settings = { fatal: false, initial: 0, partial: [] }) {
37
+ if (input === undefined) {
51
38
  if (settings.partial.length > 0) {
52
39
  if (settings.fatal) {
53
40
  settings.partial = [];
@@ -56,6 +43,11 @@ function decodeText(settings, input, options) {
56
43
  }
57
44
  return "";
58
45
  }
46
+ let bytes = isArrayBuffer.isArrayBuffer(input)
47
+ ? new Uint8Array(input)
48
+ : ArrayBuffer.isView(input)
49
+ ? new Uint8Array(input.buffer, input.byteOffset, input.byteLength)
50
+ : (() => { throw new TypeError("Input could not be converted to any of: ArrayBufferView, ArrayBuffer."); })();
59
51
  if (settings.initial < 3) {
60
52
  settings.initial += bytes.length;
61
53
  if (bytes.length >= 3) {
@@ -73,29 +65,32 @@ function decodeText(settings, input, options) {
73
65
  }
74
66
  let end = bytes.length;
75
67
  let res = [];
76
- if (!!(options === null || options === void 0 ? void 0 : options.stream) && bytes.length > 0) {
68
+ if (stream && bytes.length > 0) {
77
69
  let i = bytes.length;
78
70
  while (i > 0 && i > bytes.length - 4) {
79
71
  let byte = bytes[i - 1];
80
72
  if ((byte & 0b11000000) !== 0b10000000) {
81
- let len = getBytesPerSequence(byte);
73
+ let len = (byte > 0xEF) ? 4 : (byte > 0xDF) ? 3 : (byte > 0xBF) ? 2 : 1;
82
74
  if (len > bytes.length - (i - 1)) {
83
75
  end = i - 1;
84
76
  }
85
77
  break;
86
78
  }
87
- i--;
79
+ --i;
88
80
  }
89
81
  settings.partial = Array.from(bytes.slice(end)); // save tail // × WeChat 2.5.0
90
82
  bytes = bytes.slice(0, end); // × WeChat 2.5.0
91
83
  }
84
+ let codePoint = 0;
85
+ let tempCodePoint = 0;
86
+ let bytesPerSequence = 0;
87
+ let firstByte = 0, secondByte = 0, thirdByte = 0, fourthByte = 0;
92
88
  let i = 0;
93
89
  while (i < end) {
94
- let codePoint = null;
95
- let firstByte = bytes[i];
96
- let bytesPerSequence = getBytesPerSequence(firstByte);
90
+ codePoint = 0;
91
+ firstByte = bytes[i];
92
+ bytesPerSequence = (firstByte > 0xEF) ? 4 : (firstByte > 0xDF) ? 3 : (firstByte > 0xBF) ? 2 : 1;
97
93
  if (i + bytesPerSequence <= end) {
98
- let secondByte, thirdByte, fourthByte, tempCodePoint;
99
94
  switch (bytesPerSequence) {
100
95
  case 1:
101
96
  if (firstByte < 0x80) {
@@ -134,7 +129,7 @@ function decodeText(settings, input, options) {
134
129
  break;
135
130
  }
136
131
  }
137
- if (codePoint === null) {
132
+ if (codePoint === 0 && ((bytesPerSequence === 1 && firstByte !== 0) || bytesPerSequence > 1)) {
138
133
  if (settings.fatal) {
139
134
  settings.partial = [];
140
135
  throw new TypeError("Decoding failed.");
@@ -159,9 +154,6 @@ function decodeText(settings, input, options) {
159
154
  }
160
155
  return res.length > 0x4000 ? buildString(res) : concatString(res);
161
156
  }
162
- function getBytesPerSequence(byte) {
163
- return (byte > 0xEF) ? 4 : (byte > 0xDF) ? 3 : (byte > 0xBF) ? 2 : 1;
164
- }
165
157
  function buildString(val) {
166
158
  let arr = [];
167
159
  for (let i = 0, len = val.length; i < len; i += 0x1000) {
@@ -5,14 +5,14 @@ var utils = require('../utils.js');
5
5
  class TextEncoderP {
6
6
  get encoding() { return "utf-8"; }
7
7
  encode(input = "") {
8
- return encodeText("" + input).encoded;
8
+ return encodeText(typeof input === "string" ? input : ("" + input)).encoded;
9
9
  }
10
10
  encodeInto(source, destination) {
11
11
  utils.checkArgsLength(arguments.length, 2, "TextEncoder", "encodeInto");
12
12
  if (!(destination instanceof Uint8Array || utils.isObjectType("Uint8Array", destination) /* Mini Program */)) {
13
13
  throw new TypeError("Failed to execute 'encodeInto' on 'TextEncoder': parameter 2 is not of type 'Uint8Array'.");
14
14
  }
15
- let result = encodeText("" + source, destination);
15
+ let result = encodeText(typeof source === "string" ? source : ("" + source), destination);
16
16
  return { read: result.read, written: result.written };
17
17
  }
18
18
  /** @internal */ toString() { return "[object TextEncoder]"; }
@@ -27,9 +27,12 @@ function encodeText(input, destination) {
27
27
  let at = 0; // output position
28
28
  let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
29
29
  let target = HAS_DESTINATION ? destination : new Uint8Array((tlen >> 3) << 3); // ... but at 8 byte offset
30
+ let value = 0;
31
+ let codeUnitCount = 0;
32
+ let byteCount = 0;
30
33
  while (pos < len) {
31
- let value = input.charCodeAt(pos);
32
- let codeUnitCount = 1;
34
+ value = input.charCodeAt(pos);
35
+ codeUnitCount = 1;
33
36
  if (value >= 0xd800 && value <= 0xdbff) {
34
37
  // high surrogate
35
38
  if (pos + 1 < len) {
@@ -65,7 +68,6 @@ function encodeText(input, destination) {
65
68
  update.set(target);
66
69
  target = update;
67
70
  }
68
- let byteCount;
69
71
  if ((value & 0xffffff80) === 0) { // 1-byte
70
72
  byteCount = 1;
71
73
  }
@@ -8,9 +8,7 @@ class AbortControllerP {
8
8
  utils.setState(this, "__AbortController__", new AbortControllerState());
9
9
  }
10
10
  get signal() { return state(this).signal; }
11
- abort(reason) {
12
- AbortSignalP.AbortSignal_abort(this.signal, true, reason);
13
- }
11
+ abort(reason) { AbortSignalP.AbortSignal_abort(this.signal, true, reason); }
14
12
  /** @internal */ toString() { return "[object AbortController]"; }
15
13
  /** @internal */ get [utils.SymbolP.toStringTag]() { return "AbortController"; }
16
14
  /** @internal */ get __MPHTTPX__() { return { chain: ["AbortController"] }; }
@@ -19,13 +19,14 @@ class AbortSignalP extends EventTargetP.EventTargetP {
19
19
  throw new TypeError("Failed to execute 'any' on 'AbortSignal': The provided value cannot be converted to a sequence.");
20
20
  }
21
21
  let _signals = Array.isArray(signals) ? signals : Array.from(signals);
22
- _signals.forEach(sig => {
23
- if (!isEventTarget.isEventTarget(sig)) {
24
- throw new TypeError("Failed to execute 'any' on 'AbortSignal': Failed to convert value to 'AbortSignal'.");
25
- }
26
- });
22
+ _signals.forEach(sig => { if (!isEventTarget.isEventTarget(sig))
23
+ throw new TypeError("Failed to execute 'any' on 'AbortSignal': Failed to convert value to 'AbortSignal'."); });
27
24
  let signal = createAbortSignal();
28
- let abortedSignal = _signals.find(x => x.aborted);
25
+ let abortedSignal = (() => { for (let i = 0; i < _signals.length; ++i) {
26
+ let sig = _signals[i];
27
+ if (sig.aborted)
28
+ return sig;
29
+ } })();
29
30
  if (abortedSignal) {
30
31
  AbortSignal_abort(signal, false, abortedSignal.reason);
31
32
  }
@@ -50,8 +51,8 @@ class AbortSignalP extends EventTargetP.EventTargetP {
50
51
  throw new TypeError("Failed to execute 'timeout' on 'AbortSignal': Value is outside the 'unsigned long long' value range.");
51
52
  }
52
53
  const signal = createAbortSignal();
53
- const whenTimeout = () => AbortSignal_abort(signal, true, new utils.DOMExceptionP("signal timed out", "TimeoutError"));
54
- setTimeout(whenTimeout, milliseconds);
54
+ const execTimeout = () => AbortSignal_abort(signal, true, new utils.DOMExceptionP("signal timed out", "TimeoutError"));
55
+ setTimeout(execTimeout, milliseconds);
55
56
  return signal;
56
57
  }
57
58
  /** @internal */
@@ -95,16 +96,14 @@ function state(target) {
95
96
  /** @internal */
96
97
  function AbortSignal_abort(signal, notify = true, reason) {
97
98
  const s = state(signal);
98
- const whenAbort = () => {
99
+ const execAbort = () => {
99
100
  s.aborted = true;
100
- s.reason = reason !== null && reason !== void 0 ? reason : (new utils.DOMExceptionP("signal is aborted without reason", "AbortError"));
101
- if (notify) {
101
+ s.reason = reason !== undefined ? reason : new utils.DOMExceptionP("signal is aborted without reason", "AbortError");
102
+ if (notify)
102
103
  emitEvent.emitEvent(signal, "abort");
103
- }
104
104
  };
105
- if (!signal.aborted) {
106
- whenAbort();
107
- }
105
+ if (!signal.aborted)
106
+ execAbort();
108
107
  }
109
108
  /** @internal */
110
109
  function createAbortSignal() {
@@ -5,11 +5,10 @@ var utils = require('../utils.js');
5
5
 
6
6
  class CloseEventP extends EventP.EventP {
7
7
  constructor(type, eventInitDict) {
8
- var _a;
9
8
  super(type, eventInitDict);
10
9
  utils.setState(this, "__CloseEvent__", new CloseEventState());
11
10
  const s = state(this);
12
- let _code = Number((_a = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.code) !== null && _a !== void 0 ? _a : 0);
11
+ let _code = Number(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.code);
13
12
  s.code = isNaN(_code) ? 0 : _code;
14
13
  if ((eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.reason) !== undefined)
15
14
  s.reason = "" + eventInitDict.reason;
@@ -6,6 +6,7 @@ var utils = require('../utils.js');
6
6
  class CustomEventP extends EventP.EventP {
7
7
  constructor(type, eventInitDict) {
8
8
  var _a;
9
+ utils.checkArgsLength(arguments.length, 1, "CustomEvent");
9
10
  super(type, eventInitDict);
10
11
  utils.setState(this, "__CustomEvent__", new CustomEventState());
11
12
  state(this).detail = (_a = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.detail) !== null && _a !== void 0 ? _a : null;
@@ -10,13 +10,14 @@ class EventP {
10
10
  constructor(type, eventInitDict) {
11
11
  utils.checkArgsLength(arguments.length, 1, "Event");
12
12
  utils.setState(this, "__Event__", new EventState());
13
- state(this).type = "" + type;
14
- state(this).bubbles = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.bubbles);
15
- state(this).cancelable = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.cancelable);
16
- state(this).composed = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.composed);
13
+ const s = state(this);
14
+ s.type = "" + type;
15
+ s.bubbles = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.bubbles);
16
+ s.cancelable = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.cancelable);
17
+ s.composed = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.composed);
17
18
  Object.defineProperty(this, "isTrusted", {
18
19
  enumerable: true,
19
- get: (function isTrusted() { return state(this).isTrusted === "YES"; }).bind(this),
20
+ get: function isTrusted() { return s.isTrusted === "YES"; },
20
21
  });
21
22
  }
22
23
  get NONE() { return 0; }
@@ -49,19 +50,20 @@ class EventP {
49
50
  }
50
51
  initEvent(type, bubbles, cancelable) {
51
52
  utils.checkArgsLength(arguments.length, 1, utils.className(this), "initEvent");
52
- if (state(this).eventDispatched)
53
+ let s = state(this);
54
+ if (s.eventDispatched)
53
55
  return;
54
- state(this).type = "" + type;
55
- state(this).bubbles = !!bubbles;
56
- state(this).cancelable = !!cancelable;
56
+ s.type = "" + type;
57
+ s.bubbles = !!bubbles;
58
+ s.cancelable = !!cancelable;
57
59
  }
58
60
  preventDefault() {
59
- if (state(this).passive) {
61
+ let s = state(this);
62
+ if (s.passive)
60
63
  return console.warn(`Ignoring 'preventDefault()' call on event of type '${this.type}' from a listener registered as 'passive'.`);
61
- }
62
64
  if (this.cancelable) {
63
- state(this).defaultPrevented = true;
64
- state(this).returnValue = false;
65
+ s.defaultPrevented = true;
66
+ s.returnValue = false;
65
67
  }
66
68
  }
67
69
  stopImmediatePropagation() {
@@ -87,7 +89,7 @@ class EventState {
87
89
  this.eventPhase = 0 /* NONE */;
88
90
  this.returnValue = true;
89
91
  this.target = null;
90
- this.timeStamp = (new Date()).getTime() - EventState.timeStamp;
92
+ this.timeStamp = (new Date()).getTime() - timeStamp;
91
93
  this.type = "";
92
94
  this.passive = false;
93
95
  this.eventDispatched = false;
@@ -95,7 +97,7 @@ class EventState {
95
97
  Object.defineProperty(this, "isTrusted", createTrustedPropertyDescriptor());
96
98
  }
97
99
  }
98
- EventState.timeStamp = (new Date()).getTime();
100
+ const timeStamp = (new Date()).getTime();
99
101
  /** @internal */
100
102
  function state(target) {
101
103
  return target.__Event__;
@@ -10,24 +10,27 @@ class EventTargetP {
10
10
  }
11
11
  addEventListener(type, callback, options) {
12
12
  utils.checkArgsLength(arguments.length, 2, utils.className(this), "addEventListener");
13
- if (!(typeof callback === "function" || typeof callback === "object" || callback === undefined)) {
13
+ if (typeof callback !== "function" && typeof callback !== "object" && callback !== undefined) {
14
14
  throw new TypeError(`Failed to execute 'addEventListener' on '${utils.className(this)}': parameter 2 is not of type 'Object'.`);
15
15
  }
16
+ let s = state(this);
16
17
  let executor = new Executor(type, callback);
17
- executor.options.capture = typeof options === "boolean" ? options : !!(options === null || options === void 0 ? void 0 : options.capture);
18
- if (!state(this).executors.some(x => x.equals(executor))) {
18
+ let capture = executor.options.capture = typeof options === "boolean" ? options : !!(options === null || options === void 0 ? void 0 : options.capture);
19
+ if (!s.executors.some(x => x.equals(executor))) {
20
+ s.executors.push(executor);
19
21
  if (options && typeof options === "object") {
20
22
  executor.options.once = !!options.once;
21
23
  executor.options.passive = !!options.passive;
22
24
  const signal = options.signal;
23
- if (signal && isEventTarget.isEventTarget(signal)) {
25
+ if (signal && isEventTarget.isEventTarget(signal) && !signal.aborted) {
24
26
  executor.options.signal = signal;
25
27
  whenAbort(this, executor, signal);
26
28
  }
27
29
  }
28
- state(this).executors.push(executor);
29
- const f = (v) => !!v.options.capture ? 0 : 1;
30
- state(this).executors = state(this).executors.sort((a, b) => f(a) - f(b));
30
+ if (capture) {
31
+ let f = (v) => !!v.options.capture ? 0 : 1;
32
+ s.executors = s.executors.sort((a, b) => f(a) - f(b));
33
+ }
31
34
  }
32
35
  }
33
36
  dispatchEvent(event) {
@@ -45,13 +48,14 @@ class EventTargetP {
45
48
  }
46
49
  removeEventListener(type, callback, options) {
47
50
  utils.checkArgsLength(arguments.length, 2, utils.className(this), "removeEventListener");
48
- if (!(typeof callback === "function" || typeof callback === "object" || callback === undefined)) {
51
+ if (typeof callback !== "function" && typeof callback !== "object" && callback !== undefined) {
49
52
  throw new TypeError(`Failed to execute 'removeEventListener' on '${utils.className(this)}': parameter 2 is not of type 'Object'.`);
50
53
  }
54
+ let s = state(this);
51
55
  let executor = new Executor(type, callback);
52
56
  executor.options.capture = typeof options === "boolean" ? options : !!(options === null || options === void 0 ? void 0 : options.capture);
53
- if (state(this).executors.some(x => x.equals(executor))) {
54
- state(this).executors = state(this).executors.filter(x => !x.equals(executor));
57
+ if (s.executors.some(x => x.equals(executor))) {
58
+ s.executors = s.executors.filter(x => !x.equals(executor));
55
59
  }
56
60
  }
57
61
  /** @internal */ toString() { return "[object EventTarget]"; }
@@ -8,11 +8,12 @@ class ProgressEventP extends EventP.EventP {
8
8
  var _a, _b;
9
9
  super(type, eventInitDict);
10
10
  utils.setState(this, "__ProgressEvent__", new ProgressEventState());
11
- state(this).lengthComputable = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.lengthComputable);
12
- state(this).loaded = Number((_a = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.loaded) !== null && _a !== void 0 ? _a : 0);
13
- state(this).total = Number((_b = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.total) !== null && _b !== void 0 ? _b : 0);
14
- checkNumber("loaded", this.loaded);
15
- checkNumber("total", this.total);
11
+ const s = state(this);
12
+ s.lengthComputable = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.lengthComputable);
13
+ s.loaded = Number((_a = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.loaded) !== null && _a !== void 0 ? _a : 0);
14
+ s.total = Number((_b = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.total) !== null && _b !== void 0 ? _b : 0);
15
+ checkNumberField("loaded", this.loaded);
16
+ checkNumberField("total", this.total);
16
17
  }
17
18
  get lengthComputable() { return state(this).lengthComputable; }
18
19
  get loaded() { return state(this).loaded; }
@@ -32,7 +33,7 @@ class ProgressEventState {
32
33
  function state(target) {
33
34
  return target.__ProgressEvent__;
34
35
  }
35
- function checkNumber(field, value) {
36
+ function checkNumberField(field, value) {
36
37
  if (isNaN(value)) {
37
38
  throw new TypeError(`Failed to construct 'ProgressEvent': Failed to read the '${field}' property from 'ProgressEventInit': The provided double value is non-finite.`);
38
39
  }
@@ -68,10 +68,11 @@ class HeadersP {
68
68
  if (typeof callbackfn !== "function") {
69
69
  throw new TypeError("Failed to execute 'forEach' on 'Headers': parameter 1 is not of type 'Function'.");
70
70
  }
71
- let names = Object.getOwnPropertyNames(state(this).dict);
71
+ let dict = state(this).dict;
72
+ let names = Object.getOwnPropertyNames(dict);
72
73
  for (let i = 0; i < names.length; ++i) {
73
74
  let name = names[i];
74
- callbackfn.call(thisArg, state(this).dict[name], name, this);
75
+ callbackfn.call(thisArg, dict[name], name, this);
75
76
  }
76
77
  }
77
78
  entries() {
@@ -81,13 +82,13 @@ class HeadersP {
81
82
  }
82
83
  keys() {
83
84
  let array = [];
84
- this.forEach((value, name) => { array.push(name); });
85
- return array.values();
85
+ this.forEach((value, name) => { array.push([name, value]); });
86
+ return array.map(x => x[0]).values();
86
87
  }
87
88
  values() {
88
89
  let array = [];
89
- this.forEach((value, name) => { array.push(value); });
90
- return array.values();
90
+ this.forEach((value, name) => { array.push([name, value]); });
91
+ return array.map(x => x[1]).values();
91
92
  }
92
93
  // @ts-ignore
93
94
  /** @internal */ [utils.SymbolP.iterator]() {
@@ -26,7 +26,7 @@ class RequestP extends BodyImpl.BodyImpl {
26
26
  }
27
27
  s.cache = input.cache;
28
28
  s.credentials = input.credentials;
29
- if (!_init.headers) {
29
+ if (_init.headers === undefined) {
30
30
  state(this).headers = new HeadersP.HeadersP(input.headers);
31
31
  }
32
32
  s.method = input.method;
@@ -62,12 +62,14 @@ class RequestP extends BodyImpl.BodyImpl {
62
62
  else
63
63
  throw new TypeError("Failed to construct 'Request': Failed to read the 'signal' property from 'RequestInit': Failed to convert value to 'AbortSignal'.");
64
64
  }
65
- if ((this.method === "GET" || this.method === "HEAD") && body) {
66
- throw new TypeError("Failed to construct 'Request': Request with GET/HEAD method cannot have body.");
65
+ if (this.method === "GET" || this.method === "HEAD") {
66
+ if (body !== null && body !== undefined) {
67
+ throw new TypeError("Failed to construct 'Request': Request with GET/HEAD method cannot have body.");
68
+ }
67
69
  }
68
70
  BodyImpl.Body_init(this, bodyInited ? null : body);
69
71
  let payload = this.__Body__.payload;
70
- if (payload && payload.type && !this.headers.has("Content-Type")) {
72
+ if (!this.headers.has("Content-Type") && payload && payload.type) {
71
73
  this.headers.set("Content-Type", payload.type);
72
74
  }
73
75
  if (this.method === "GET" || this.method === "HEAD") {
@@ -101,9 +103,9 @@ class RequestP extends BodyImpl.BodyImpl {
101
103
  get url() { return state(this).url; }
102
104
  clone() {
103
105
  if (!this.bodyUsed) {
104
- let request = new RequestP(this);
106
+ let req = new RequestP(this);
105
107
  this.__Body__.bodyUsed = false;
106
- return request;
108
+ return req;
107
109
  }
108
110
  else {
109
111
  throw new TypeError("Failed to execute 'clone' on 'Request': Request body is already used");
@@ -36,14 +36,13 @@ class ResponseP extends BodyImpl.BodyImpl {
36
36
  state(this).headers = new HeadersP.HeadersP(_init.headers);
37
37
  }
38
38
  let status = _init.status === undefined ? 200 : _init.status;
39
- if (status < 200 || status > 500) {
39
+ if (status < 200 || status > 500)
40
40
  throw new RangeError(`Failed to construct 'Response': The status provided (${+status}) is outside the range [200, 599].`);
41
- }
42
41
  s.status = status;
43
42
  s.statusText = _init.statusText === undefined ? "" : "" + _init.statusText;
44
43
  BodyImpl.Body_init(this, body);
45
44
  let payload = this.__Body__.payload;
46
- if (payload && payload.type && !this.headers.has("Content-Type")) {
45
+ if (!this.headers.has("Content-Type") && payload && payload.type) {
47
46
  this.headers.set("Content-Type", payload.type);
48
47
  }
49
48
  }
@@ -22,7 +22,7 @@ function fetchP(input, init) {
22
22
  let xhr = new mp.XMLHttpRequest();
23
23
  let aborted = false;
24
24
  let payload = request.__Body__.payload;
25
- xhr.onload = function () {
25
+ xhr.onload = () => {
26
26
  let options = {
27
27
  headers: parseHeaders(xhr.getAllResponseHeaders() || ""),
28
28
  status: xhr.status,
@@ -39,21 +39,9 @@ function fetchP(input, init) {
39
39
  resolve(response);
40
40
  });
41
41
  };
42
- xhr.onerror = function () {
43
- setTimeout(function () {
44
- reject(new TypeError("Failed to fetch"));
45
- });
46
- };
47
- xhr.ontimeout = function () {
48
- setTimeout(function () {
49
- reject(new utils.DOMExceptionP("request:fail timeout", "TimeoutError"));
50
- });
51
- };
52
- xhr.onabort = function () {
53
- setTimeout(function () {
54
- reject(new utils.DOMExceptionP("The user aborted a request.", "AbortError"));
55
- });
56
- };
42
+ xhr.onerror = () => { setTimeout(() => { reject(new TypeError("Failed to fetch")); }); };
43
+ xhr.ontimeout = () => { setTimeout(() => { reject(new utils.DOMExceptionP("request:fail timeout", "TimeoutError")); }); };
44
+ xhr.onabort = () => { setTimeout(() => { reject(new utils.DOMExceptionP("The user aborted a request.", "AbortError")); }); };
57
45
  xhr.open(request.method, request.url, true);
58
46
  if (request.credentials === "include") {
59
47
  xhr.withCredentials = true;
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var isBlob = require('../helpers/isBlob.js');
4
- var isSequence = require('../helpers/isSequence.js');
5
- var utils = require('../utils.js');
6
3
  var encode = require('../helpers/encode.js');
7
4
  var decode = require('../helpers/decode.js');
5
+ var utils = require('../utils.js');
6
+ var isBlob = require('../helpers/isBlob.js');
7
+ var isSequence = require('../helpers/isSequence.js');
8
8
  var isArrayBuffer = require('../helpers/isArrayBuffer.js');
9
9
 
10
10
  class BlobP {
@@ -21,13 +21,10 @@ class BlobP {
21
21
  size += chunk.size;
22
22
  tasks.push(chunk.arrayBuffer().then(r => new Uint8Array(r)));
23
23
  }
24
- else if (isArrayBuffer.isArrayBuffer(chunk) || ArrayBuffer.isView(chunk)) {
25
- let bytes = BufferSource_toUint8Array(chunk);
26
- size += bytes.length;
27
- tasks.push(Promise.resolve(bytes));
28
- }
29
24
  else {
30
- let bytes = encode.encode("" + chunk);
25
+ let bytes = (isArrayBuffer.isArrayBuffer(chunk) || ArrayBuffer.isView(chunk))
26
+ ? BufferSource_toUint8Array(chunk)
27
+ : encode.encode(chunk);
31
28
  size += bytes.length;
32
29
  tasks.push(Promise.resolve(bytes));
33
30
  }
@@ -90,26 +87,22 @@ function clone(buf) {
90
87
  function concat(chunks) {
91
88
  let totalByteLength = chunks.reduce((acc, cur) => acc + cur.byteLength, 0);
92
89
  let result = new Uint8Array(totalByteLength);
93
- chunks.reduce((offset, chunk) => {
94
- result.set(chunk, offset);
95
- return offset + chunk.byteLength;
96
- }, 0);
90
+ chunks.reduce((offset, chunk) => { result.set(chunk, offset); return offset + chunk.byteLength; }, 0);
97
91
  return result;
98
92
  }
99
- function calcSlicedSize(originalSize, start, end) {
93
+ function calcSlicedSize(size, start, end) {
100
94
  const normalizeNumer = (n) => {
101
95
  let num = Number(n);
102
96
  if (isNaN(num))
103
97
  num = 0;
104
98
  if (num >= 0) {
105
- num = Math.min(num, originalSize);
99
+ num = Math.min(num, size);
106
100
  }
107
101
  else {
108
- num = Math.max(0, num + originalSize);
102
+ num = Math.max(0, num + size);
109
103
  }
110
104
  return num;
111
105
  };
112
- console.log(normalizeNumer(start), normalizeNumer(end));
113
106
  return Math.max(0, normalizeNumer(end) - normalizeNumer(start));
114
107
  }
115
108
  const BlobE = (typeof Blob !== "undefined" && Blob) || BlobP;