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