mphttpx 1.1.0 → 1.2.0-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.
- package/README.zh-CN.md +982 -0
- package/dist/cjs/AbortControllerP.js +31 -0
- package/dist/cjs/AbortSignalP.js +126 -0
- package/dist/cjs/BlobP.js +131 -0
- package/dist/cjs/BodyImpl.js +142 -0
- package/dist/cjs/CloseEventP.js +38 -0
- package/dist/cjs/CustomEventP.js +35 -0
- package/dist/cjs/EventP.js +173 -0
- package/dist/cjs/EventTargetP.js +176 -0
- package/dist/cjs/FileP.js +36 -0
- package/dist/cjs/FileReaderP.js +151 -0
- package/dist/cjs/FormDataP.js +250 -0
- package/dist/cjs/HeadersP.js +176 -0
- package/dist/cjs/MessageEventP.js +64 -0
- package/dist/cjs/ProgressEventP.js +69 -0
- package/dist/cjs/RequestP.js +158 -0
- package/dist/cjs/ResponseP.js +102 -0
- package/dist/cjs/TextDecoderP.js +186 -0
- package/dist/cjs/TextEncoderP.js +122 -0
- package/dist/cjs/URLSearchParamsP.js +230 -0
- package/dist/cjs/WebSocketP.js +238 -0
- package/dist/cjs/XMLHttpRequestP.js +567 -0
- package/dist/cjs/convertor.js +83 -0
- package/dist/cjs/fetchP.js +115 -0
- package/dist/cjs/index.js +80 -0
- package/dist/cjs/isPolyfill.js +56 -0
- package/dist/cjs/platform.js +33 -0
- package/dist/esm/AbortControllerP.js +28 -0
- package/dist/esm/AbortSignalP.js +121 -0
- package/dist/esm/BlobP.js +124 -0
- package/dist/esm/BodyImpl.js +137 -0
- package/dist/esm/CloseEventP.js +35 -0
- package/dist/esm/CustomEventP.js +32 -0
- package/dist/esm/EventP.js +165 -0
- package/dist/esm/EventTargetP.js +168 -0
- package/dist/esm/FileP.js +33 -0
- package/dist/esm/FileReaderP.js +148 -0
- package/dist/esm/FormDataP.js +245 -0
- package/dist/esm/HeadersP.js +170 -0
- package/dist/esm/MessageEventP.js +61 -0
- package/dist/esm/ProgressEventP.js +65 -0
- package/dist/esm/RequestP.js +153 -0
- package/dist/esm/ResponseP.js +98 -0
- package/dist/esm/TextDecoderP.js +183 -0
- package/dist/esm/TextEncoderP.js +119 -0
- package/dist/esm/URLSearchParamsP.js +227 -0
- package/dist/esm/WebSocketP.js +234 -0
- package/dist/esm/XMLHttpRequestP.js +563 -0
- package/dist/esm/convertor.js +80 -0
- package/dist/esm/fetchP.js +111 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/isPolyfill.js +48 -0
- package/dist/esm/platform.js +31 -0
- package/dist/index.d.ts +33 -37
- package/package.json +7 -9
- package/dist/index.cjs.js +0 -3291
- package/dist/index.cjs.min.js +0 -1
- package/dist/index.esm.js +0 -3251
- package/dist/index.esm.min.js +0 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { HeadersP } from './HeadersP.js';
|
|
2
|
+
import { AbortControllerP } from './AbortControllerP.js';
|
|
3
|
+
import { g, checkArgsLength, isPolyfillType, polyfill } from './isPolyfill.js';
|
|
4
|
+
import { BodyImpl, bodyState as state$1, Body_toPayload, Body_init } from './BodyImpl.js';
|
|
5
|
+
import './AbortSignalP.js';
|
|
6
|
+
import './EventP.js';
|
|
7
|
+
import './EventTargetP.js';
|
|
8
|
+
import './convertor.js';
|
|
9
|
+
import './BlobP.js';
|
|
10
|
+
import './TextEncoderP.js';
|
|
11
|
+
import './TextDecoderP.js';
|
|
12
|
+
import './FormDataP.js';
|
|
13
|
+
import './FileP.js';
|
|
14
|
+
|
|
15
|
+
/** @internal */ const state = Symbol( /* "RequestState" */);
|
|
16
|
+
class RequestP extends BodyImpl {
|
|
17
|
+
constructor(...args) {
|
|
18
|
+
const [input, init] = args;
|
|
19
|
+
checkArgsLength(args, 1, "Request");
|
|
20
|
+
super();
|
|
21
|
+
this[state$1].name = "Request";
|
|
22
|
+
this[state] = new RequestState();
|
|
23
|
+
const s = this[state];
|
|
24
|
+
let _init = init !== null && init !== void 0 ? init : {};
|
|
25
|
+
if (typeof _init !== "object") {
|
|
26
|
+
throw new TypeError("Failed to construct 'Request': The provided value is not of type 'RequestInit'.");
|
|
27
|
+
}
|
|
28
|
+
let body = _init.body;
|
|
29
|
+
if (isPolyfillType("Request", input)) {
|
|
30
|
+
if (input.bodyUsed) {
|
|
31
|
+
throw new TypeError("Failed to construct 'Request': Cannot construct a Request with a Request object that has already been used.");
|
|
32
|
+
}
|
|
33
|
+
s.cache = input.cache;
|
|
34
|
+
s.credentials = input.credentials;
|
|
35
|
+
if (!_init.headers) {
|
|
36
|
+
s.headers = new HeadersP(input.headers);
|
|
37
|
+
}
|
|
38
|
+
s.method = input.method;
|
|
39
|
+
s.mode = input.mode;
|
|
40
|
+
let inputSignal = input[state].signal;
|
|
41
|
+
if (inputSignal) {
|
|
42
|
+
s.signal = inputSignal;
|
|
43
|
+
}
|
|
44
|
+
s.url = input.url;
|
|
45
|
+
let payload = Body_toPayload(input);
|
|
46
|
+
if (!body && payload !== "") {
|
|
47
|
+
body = payload;
|
|
48
|
+
input[state$1].bodyUsed = true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
s.url = "" + input;
|
|
53
|
+
}
|
|
54
|
+
if (_init.cache) {
|
|
55
|
+
s.cache = _init.cache;
|
|
56
|
+
}
|
|
57
|
+
if (_init.credentials) {
|
|
58
|
+
s.credentials = _init.credentials;
|
|
59
|
+
}
|
|
60
|
+
if (_init.headers) {
|
|
61
|
+
s.headers = new HeadersP(_init.headers);
|
|
62
|
+
}
|
|
63
|
+
if (_init.method) {
|
|
64
|
+
s.method = normalizeMethod(_init.method);
|
|
65
|
+
}
|
|
66
|
+
if (_init.mode) {
|
|
67
|
+
s.mode = _init.mode;
|
|
68
|
+
}
|
|
69
|
+
if (_init.signal) {
|
|
70
|
+
s.signal = _init.signal;
|
|
71
|
+
}
|
|
72
|
+
if ((this.method === "GET" || this.method === "HEAD") && body) {
|
|
73
|
+
throw new TypeError("Failed to construct 'Request': Request with GET/HEAD method cannot have body.");
|
|
74
|
+
}
|
|
75
|
+
Body_init(this, body);
|
|
76
|
+
if (this.method === "GET" || this.method === "HEAD") {
|
|
77
|
+
if (_init.cache === "no-store" || _init.cache === "no-cache") {
|
|
78
|
+
// Search for a '_' parameter in the query string
|
|
79
|
+
let reParamSearch = /([?&])_=[^&]*/;
|
|
80
|
+
if (reParamSearch.test(this.url)) {
|
|
81
|
+
// If it already exists then set the value with the current time
|
|
82
|
+
s.url = this.url.replace(reParamSearch, "$1_=" + (new Date()).getTime());
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
// Otherwise add a new '_' parameter to the end with the current time
|
|
86
|
+
let reQueryString = /\?/;
|
|
87
|
+
s.url += (reQueryString.test(this.url) ? "&" : "?") + "_=" + (new Date()).getTime();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
get cache() { return this[state].cache; }
|
|
93
|
+
get credentials() { return this[state].credentials; }
|
|
94
|
+
get destination() { return this[state].destination; }
|
|
95
|
+
get headers() {
|
|
96
|
+
const s = this[state];
|
|
97
|
+
if (!s.headers) {
|
|
98
|
+
s.headers = new HeadersP();
|
|
99
|
+
}
|
|
100
|
+
return s.headers;
|
|
101
|
+
}
|
|
102
|
+
get integrity() { return this[state].integrity; }
|
|
103
|
+
get keepalive() { return this[state].keepalive; }
|
|
104
|
+
get method() { return this[state].method; }
|
|
105
|
+
get mode() { return this[state].mode; }
|
|
106
|
+
get redirect() { return this[state].redirect; }
|
|
107
|
+
get referrer() { return this[state].referrer; }
|
|
108
|
+
get referrerPolicy() { return this[state].referrerPolicy; }
|
|
109
|
+
get signal() {
|
|
110
|
+
const s = this[state];
|
|
111
|
+
if (!s.signal) {
|
|
112
|
+
s.signal = (new AbortControllerP()).signal;
|
|
113
|
+
}
|
|
114
|
+
return s.signal;
|
|
115
|
+
}
|
|
116
|
+
get url() { return this[state].url; }
|
|
117
|
+
clone() {
|
|
118
|
+
var _a;
|
|
119
|
+
if (this.bodyUsed) {
|
|
120
|
+
throw new TypeError("Failed to execute 'clone' on 'Request': Request body is already used");
|
|
121
|
+
}
|
|
122
|
+
return new RequestP(this, { body: (_a = Body_toPayload(this)) !== null && _a !== void 0 ? _a : null });
|
|
123
|
+
}
|
|
124
|
+
/** @internal */ toString() { return "[object Request]"; }
|
|
125
|
+
/** @internal */ get [Symbol.toStringTag]() { return "Request"; }
|
|
126
|
+
/** @internal */ get isPolyfill() { return { symbol: polyfill, hierarchy: ["Request"] }; }
|
|
127
|
+
}
|
|
128
|
+
/** @internal */
|
|
129
|
+
class RequestState {
|
|
130
|
+
constructor() {
|
|
131
|
+
this.cache = "default";
|
|
132
|
+
this.credentials = "same-origin";
|
|
133
|
+
this.destination = "";
|
|
134
|
+
this.integrity = "";
|
|
135
|
+
this.keepalive = false;
|
|
136
|
+
this.method = "GET";
|
|
137
|
+
this.mode = "cors";
|
|
138
|
+
this.redirect = "follow";
|
|
139
|
+
this.referrer = "about:client";
|
|
140
|
+
this.referrerPolicy = "";
|
|
141
|
+
this.url = "";
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// HTTP methods whose capitalization should be normalized
|
|
145
|
+
const methods = ["CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"];
|
|
146
|
+
/** @internal */
|
|
147
|
+
function normalizeMethod(method) {
|
|
148
|
+
let upcased = method.toUpperCase();
|
|
149
|
+
return methods.indexOf(upcased) > -1 ? upcased : method;
|
|
150
|
+
}
|
|
151
|
+
const RequestE = g["Request"] || RequestP;
|
|
152
|
+
|
|
153
|
+
export { RequestE as Request, RequestP, normalizeMethod, state as requestState };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { HeadersP } from './HeadersP.js';
|
|
2
|
+
import { g, checkArgsLength, polyfill } from './isPolyfill.js';
|
|
3
|
+
import { BodyImpl, bodyState as state$1, Body_init, Body_toPayload } from './BodyImpl.js';
|
|
4
|
+
import './convertor.js';
|
|
5
|
+
import './BlobP.js';
|
|
6
|
+
import './TextEncoderP.js';
|
|
7
|
+
import './TextDecoderP.js';
|
|
8
|
+
import './FormDataP.js';
|
|
9
|
+
import './FileP.js';
|
|
10
|
+
|
|
11
|
+
/** @internal */ const state = Symbol( /* "ResponseState" */);
|
|
12
|
+
class ResponseP extends BodyImpl {
|
|
13
|
+
constructor(body, init) {
|
|
14
|
+
super();
|
|
15
|
+
this[state$1].name = "Response";
|
|
16
|
+
this[state] = new ResponseState();
|
|
17
|
+
const s = this[state];
|
|
18
|
+
let _init = init !== null && init !== void 0 ? init : {};
|
|
19
|
+
if (typeof _init !== "object") {
|
|
20
|
+
throw new TypeError("Failed to construct 'Response': The provided value is not of type 'ResponseInit'.");
|
|
21
|
+
}
|
|
22
|
+
let status = _init.status === undefined ? 200 : _init.status;
|
|
23
|
+
if (status < 200 || status > 500) {
|
|
24
|
+
throw new RangeError(`Failed to construct 'Response': The status provided (${+status}) is outside the range [200, 599].`);
|
|
25
|
+
}
|
|
26
|
+
if (_init.headers) {
|
|
27
|
+
s.headers = new HeadersP(_init.headers);
|
|
28
|
+
}
|
|
29
|
+
s.ok = status >= 200 && status < 300;
|
|
30
|
+
s.status = status;
|
|
31
|
+
s.statusText = _init.statusText === undefined ? "" : "" + _init.statusText;
|
|
32
|
+
Body_init(this, body);
|
|
33
|
+
}
|
|
34
|
+
get headers() {
|
|
35
|
+
const s = this[state];
|
|
36
|
+
if (!s.headers) {
|
|
37
|
+
s.headers = new HeadersP();
|
|
38
|
+
}
|
|
39
|
+
return s.headers;
|
|
40
|
+
}
|
|
41
|
+
get ok() { return this[state].ok; }
|
|
42
|
+
get redirected() { return this[state].redirected; }
|
|
43
|
+
get status() { return this[state].status; }
|
|
44
|
+
get statusText() { return this[state].statusText; }
|
|
45
|
+
get type() { return this[state].type; }
|
|
46
|
+
get url() { return this[state].url; }
|
|
47
|
+
clone() {
|
|
48
|
+
if (this.bodyUsed) {
|
|
49
|
+
throw new TypeError("Failed to execute 'clone' on 'Response': Response body is already used");
|
|
50
|
+
}
|
|
51
|
+
let response = new ResponseP(Body_toPayload(this), {
|
|
52
|
+
headers: new HeadersP(this.headers),
|
|
53
|
+
status: this.status,
|
|
54
|
+
statusText: this.statusText,
|
|
55
|
+
});
|
|
56
|
+
response[state].url = this.url;
|
|
57
|
+
return response;
|
|
58
|
+
}
|
|
59
|
+
static json(...args) {
|
|
60
|
+
const [data, init] = args;
|
|
61
|
+
checkArgsLength(args, 1, "Response", "json");
|
|
62
|
+
let response = new ResponseP(typeof data === "string" ? data : JSON.stringify(data), init);
|
|
63
|
+
response.headers.set("Content-Type", "application/json");
|
|
64
|
+
return response;
|
|
65
|
+
}
|
|
66
|
+
static error() {
|
|
67
|
+
let response = new ResponseP(null, { status: 200, statusText: "" });
|
|
68
|
+
response[state].ok = false;
|
|
69
|
+
response[state].status = 0;
|
|
70
|
+
response[state].type = "error";
|
|
71
|
+
return response;
|
|
72
|
+
}
|
|
73
|
+
static redirect(...args) {
|
|
74
|
+
const [url, status = 301] = args;
|
|
75
|
+
checkArgsLength(args, 1, "Response", "redirect");
|
|
76
|
+
if ([301, 302, 303, 307, 308].indexOf(status) === -1) {
|
|
77
|
+
throw new RangeError("Failed to execute 'redirect' on 'Response': Invalid status code");
|
|
78
|
+
}
|
|
79
|
+
return new ResponseP(null, { status, headers: { location: "" + url } });
|
|
80
|
+
}
|
|
81
|
+
/** @internal */ toString() { return "[object Response]"; }
|
|
82
|
+
/** @internal */ get [Symbol.toStringTag]() { return "Response"; }
|
|
83
|
+
/** @internal */ get isPolyfill() { return { symbol: polyfill, hierarchy: ["Response"] }; }
|
|
84
|
+
}
|
|
85
|
+
/** @internal */
|
|
86
|
+
class ResponseState {
|
|
87
|
+
constructor() {
|
|
88
|
+
this.ok = true;
|
|
89
|
+
this.redirected = false;
|
|
90
|
+
this.status = 200;
|
|
91
|
+
this.statusText = "";
|
|
92
|
+
this.type = "default";
|
|
93
|
+
this.url = "";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const ResponseE = g["Response"] || ResponseP;
|
|
97
|
+
|
|
98
|
+
export { ResponseE as Response, ResponseP, state as responseState };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { g, isArrayBuffer, isObjectType, polyfill } from './isPolyfill.js';
|
|
2
|
+
|
|
3
|
+
var _a, _b;
|
|
4
|
+
/** @internal */
|
|
5
|
+
const state = Symbol( /* "TextDecoderState" */);
|
|
6
|
+
class TextDecoderP {
|
|
7
|
+
constructor(label = "utf-8", { fatal = false, ignoreBOM = false } = {}) {
|
|
8
|
+
let _label = "" + label;
|
|
9
|
+
if (["utf-8", "utf8", "unicode-1-1-utf-8"].indexOf(_label.toLowerCase()) === -1) {
|
|
10
|
+
throw new RangeError(`Failed to construct 'TextDecoder': encoding ('${_label}') not implemented.`);
|
|
11
|
+
}
|
|
12
|
+
this[state] = new TextDecoderState();
|
|
13
|
+
this[state].fatal = !!fatal;
|
|
14
|
+
this[state].ignoreBOM = !!ignoreBOM;
|
|
15
|
+
}
|
|
16
|
+
get encoding() { return "utf-8"; }
|
|
17
|
+
get fatal() { return this[state].fatal; }
|
|
18
|
+
get ignoreBOM() { return this[state].ignoreBOM; }
|
|
19
|
+
decode(input, { stream = false } = {}) {
|
|
20
|
+
const s = this[state];
|
|
21
|
+
let bytes;
|
|
22
|
+
if (input !== undefined) {
|
|
23
|
+
if (isArrayBuffer(input)) {
|
|
24
|
+
bytes = new Uint8Array(input);
|
|
25
|
+
}
|
|
26
|
+
else if (input instanceof Uint8Array || isObjectType("Uint8Array", input)) {
|
|
27
|
+
bytes = input;
|
|
28
|
+
}
|
|
29
|
+
else if (ArrayBuffer.isView(input)) {
|
|
30
|
+
bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
throw new TypeError("Failed to execute 'decode' on 'TextDecoder': parameter 1 is not of type 'ArrayBuffer'.");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (s[_partial].length > 0) {
|
|
38
|
+
if (this.fatal) {
|
|
39
|
+
s[_partial] = [];
|
|
40
|
+
throw new TypeError("TextDecoder: Incomplete UTF-8 sequence.");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
if (s[_bomDone] < 3) {
|
|
46
|
+
s[_bomDone] += bytes.length;
|
|
47
|
+
if (bytes.length >= 3) {
|
|
48
|
+
if (bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf) {
|
|
49
|
+
bytes = bytes.subarray(3); // × WeChat 2.5.0
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (s[_partial].length > 0) {
|
|
54
|
+
let merged = new Uint8Array(s[_partial].length + bytes.length);
|
|
55
|
+
merged.set(s[_partial], 0);
|
|
56
|
+
merged.set(bytes, s[_partial].length);
|
|
57
|
+
bytes = merged;
|
|
58
|
+
s[_partial] = [];
|
|
59
|
+
}
|
|
60
|
+
let end = bytes.length;
|
|
61
|
+
let res = [];
|
|
62
|
+
if (stream && bytes.length > 0) {
|
|
63
|
+
let i = bytes.length;
|
|
64
|
+
while (i > 0 && i > bytes.length - 4) {
|
|
65
|
+
let byte = bytes[i - 1];
|
|
66
|
+
if ((byte & 0b11000000) !== 0b10000000) {
|
|
67
|
+
let len = getBytesPerSequence(byte);
|
|
68
|
+
if (len > bytes.length - (i - 1)) {
|
|
69
|
+
end = i - 1;
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
i--;
|
|
74
|
+
}
|
|
75
|
+
s[_partial] = Array.from(bytes.slice(end)); // save tail // × WeChat 2.5.0
|
|
76
|
+
bytes = bytes.slice(0, end); // × WeChat 2.5.0
|
|
77
|
+
}
|
|
78
|
+
let i = 0;
|
|
79
|
+
while (i < end) {
|
|
80
|
+
let codePoint = null;
|
|
81
|
+
let firstByte = bytes[i];
|
|
82
|
+
let bytesPerSequence = getBytesPerSequence(firstByte);
|
|
83
|
+
if (i + bytesPerSequence <= end) {
|
|
84
|
+
let secondByte, thirdByte, fourthByte, tempCodePoint;
|
|
85
|
+
switch (bytesPerSequence) {
|
|
86
|
+
case 1:
|
|
87
|
+
if (firstByte < 0x80) {
|
|
88
|
+
codePoint = firstByte;
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case 2:
|
|
92
|
+
secondByte = bytes[i + 1];
|
|
93
|
+
if ((secondByte & 0xC0) === 0x80) {
|
|
94
|
+
tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F);
|
|
95
|
+
if (tempCodePoint > 0x7F) {
|
|
96
|
+
codePoint = tempCodePoint;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
case 3:
|
|
101
|
+
secondByte = bytes[i + 1];
|
|
102
|
+
thirdByte = bytes[i + 2];
|
|
103
|
+
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
|
|
104
|
+
tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F);
|
|
105
|
+
if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
|
|
106
|
+
codePoint = tempCodePoint;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case 4:
|
|
111
|
+
secondByte = bytes[i + 1];
|
|
112
|
+
thirdByte = bytes[i + 2];
|
|
113
|
+
fourthByte = bytes[i + 3];
|
|
114
|
+
if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
|
|
115
|
+
tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F);
|
|
116
|
+
if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
|
|
117
|
+
codePoint = tempCodePoint;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (codePoint === null) {
|
|
124
|
+
if (this.fatal) {
|
|
125
|
+
s[_partial] = [];
|
|
126
|
+
throw new TypeError("TextDecoder.decode: Decoding failed.");
|
|
127
|
+
}
|
|
128
|
+
let skip = 1;
|
|
129
|
+
while (i + skip < end && (bytes[i + skip] & 0b11000000) === 0b10000000) {
|
|
130
|
+
skip += 1;
|
|
131
|
+
}
|
|
132
|
+
// we did not generate a valid codePoint so insert a replacement char (U+FFFD)
|
|
133
|
+
res.push(0xFFFD);
|
|
134
|
+
i += skip;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
else if (codePoint > 0xFFFF) {
|
|
138
|
+
// encode to utf16 (surrogate pair dance)
|
|
139
|
+
codePoint -= 0x10000;
|
|
140
|
+
res.push(codePoint >>> 10 & 0x3FF | 0xD800);
|
|
141
|
+
codePoint = 0xDC00 | codePoint & 0x3FF;
|
|
142
|
+
}
|
|
143
|
+
res.push(codePoint);
|
|
144
|
+
i += bytesPerSequence;
|
|
145
|
+
}
|
|
146
|
+
return res.length > 0x4000 ? buildString(res) : concatString(res);
|
|
147
|
+
}
|
|
148
|
+
/** @internal */ toString() { return "[object TextDecoder]"; }
|
|
149
|
+
/** @internal */ get [Symbol.toStringTag]() { return "TextDecoder"; }
|
|
150
|
+
/** @internal */ get isPolyfill() { return { symbol: polyfill, hierarchy: ["TextDecoder"] }; }
|
|
151
|
+
}
|
|
152
|
+
/** @internal */ const _bomDone = Symbol();
|
|
153
|
+
/** @internal */ const _partial = Symbol();
|
|
154
|
+
/** @internal */
|
|
155
|
+
class TextDecoderState {
|
|
156
|
+
constructor() {
|
|
157
|
+
this.fatal = false;
|
|
158
|
+
this.ignoreBOM = false;
|
|
159
|
+
this[_a] = 0;
|
|
160
|
+
this[_b] = [];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
_a = _bomDone, _b = _partial;
|
|
164
|
+
function getBytesPerSequence(byte) {
|
|
165
|
+
return (byte > 0xEF) ? 4 : (byte > 0xDF) ? 3 : (byte > 0xBF) ? 2 : 1;
|
|
166
|
+
}
|
|
167
|
+
const buildString = (res) => {
|
|
168
|
+
let arr = [];
|
|
169
|
+
for (let i = 0, len = res.length; i < len; i += 0x1000) {
|
|
170
|
+
arr.push(String.fromCharCode.apply(String, res.slice(i, i + 0x1000)));
|
|
171
|
+
}
|
|
172
|
+
return arr.join("");
|
|
173
|
+
};
|
|
174
|
+
const concatString = (res) => {
|
|
175
|
+
let str = "";
|
|
176
|
+
for (let i = 0, len = res.length; i < len; i += 0x1000) {
|
|
177
|
+
str += String.fromCharCode.apply(String, res.slice(i, i + 0x1000));
|
|
178
|
+
}
|
|
179
|
+
return str;
|
|
180
|
+
};
|
|
181
|
+
const TextDecoderE = g["TextDecoder"] || TextDecoderP;
|
|
182
|
+
|
|
183
|
+
export { TextDecoderE as TextDecoder, TextDecoderP };
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { g, checkArgsLength, isObjectType, polyfill } from './isPolyfill.js';
|
|
2
|
+
|
|
3
|
+
class TextEncoderP {
|
|
4
|
+
get encoding() { return "utf-8"; }
|
|
5
|
+
encode(input = "") {
|
|
6
|
+
let _input = "" + input;
|
|
7
|
+
return encodeText(_input).encoded;
|
|
8
|
+
}
|
|
9
|
+
encodeInto(...args) {
|
|
10
|
+
const [source, destination] = args;
|
|
11
|
+
checkArgsLength(args, 2, "TextEncoder", "encodeInto");
|
|
12
|
+
let _source = "" + source;
|
|
13
|
+
if (!(destination instanceof Uint8Array || isObjectType("Uint8Array", destination))) {
|
|
14
|
+
throw new TypeError("Failed to execute 'encodeInto' on 'TextEncoder': parameter 2 is not of type 'Uint8Array'.");
|
|
15
|
+
}
|
|
16
|
+
let result = encodeText(_source, destination);
|
|
17
|
+
return { read: result.read, written: result.written };
|
|
18
|
+
}
|
|
19
|
+
/** @internal */ toString() { return "[object TextEncoder]"; }
|
|
20
|
+
/** @internal */ get [Symbol.toStringTag]() { return "TextEncoder"; }
|
|
21
|
+
/** @internal */ get isPolyfill() { return { symbol: polyfill, hierarchy: ["TextEncoder"] }; }
|
|
22
|
+
}
|
|
23
|
+
function encodeText(input, destination) {
|
|
24
|
+
const HAS_DESTINATION = typeof destination !== "undefined";
|
|
25
|
+
let pos = 0;
|
|
26
|
+
let read = 0;
|
|
27
|
+
let len = input.length;
|
|
28
|
+
let at = 0; // output position
|
|
29
|
+
let tlen = Math.max(32, len + (len >> 1) + 7); // 1.5x size
|
|
30
|
+
let target = HAS_DESTINATION ? destination : new Uint8Array((tlen >> 3) << 3); // ... but at 8 byte offset
|
|
31
|
+
while (pos < len) {
|
|
32
|
+
let value = input.charCodeAt(pos);
|
|
33
|
+
let codeUnitCount = 1;
|
|
34
|
+
if (value >= 0xd800 && value <= 0xdbff) {
|
|
35
|
+
// high surrogate
|
|
36
|
+
if (pos + 1 < len) {
|
|
37
|
+
let extra = input.charCodeAt(pos + 1);
|
|
38
|
+
if ((extra & 0xfc00) === 0xdc00) {
|
|
39
|
+
codeUnitCount = 2;
|
|
40
|
+
pos += 2;
|
|
41
|
+
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
pos += 1;
|
|
45
|
+
value = 0xfffd;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
pos += 1;
|
|
50
|
+
value = 0xfffd;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (value >= 0xdc00 && value <= 0xdfff) {
|
|
54
|
+
pos += 1;
|
|
55
|
+
value = 0xfffd;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
pos += 1;
|
|
59
|
+
}
|
|
60
|
+
// expand the buffer if we couldn't write 4 bytes
|
|
61
|
+
if (!HAS_DESTINATION && at + 4 > target.length) {
|
|
62
|
+
tlen += 8; // minimum extra
|
|
63
|
+
tlen *= (1.0 + (pos / input.length) * 2); // take 2x the remaining
|
|
64
|
+
tlen = (tlen >> 3) << 3; // 8 byte offset
|
|
65
|
+
let update = new Uint8Array(tlen);
|
|
66
|
+
update.set(target);
|
|
67
|
+
target = update;
|
|
68
|
+
}
|
|
69
|
+
let byteCount;
|
|
70
|
+
if ((value & 0xffffff80) === 0) { // 1-byte
|
|
71
|
+
byteCount = 1;
|
|
72
|
+
}
|
|
73
|
+
else if ((value & 0xfffff800) === 0) { // 2-byte
|
|
74
|
+
byteCount = 2;
|
|
75
|
+
}
|
|
76
|
+
else if ((value & 0xffff0000) === 0) { // 3-byte
|
|
77
|
+
byteCount = 3;
|
|
78
|
+
}
|
|
79
|
+
else if ((value & 0xffe00000) === 0) { // 4-byte
|
|
80
|
+
byteCount = 4;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
value = 0xfffd;
|
|
84
|
+
byteCount = 3;
|
|
85
|
+
}
|
|
86
|
+
if (HAS_DESTINATION && at + byteCount > target.length) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
switch (byteCount) {
|
|
90
|
+
case 1: // 1-byte
|
|
91
|
+
target[at++] = value; // ASCII
|
|
92
|
+
break;
|
|
93
|
+
case 2: // 2-byte
|
|
94
|
+
target[at++] = ((value >> 6) & 0x1f) | 0xc0;
|
|
95
|
+
target[at++] = (value & 0x3f) | 0x80;
|
|
96
|
+
break;
|
|
97
|
+
case 3: // 3-byte
|
|
98
|
+
target[at++] = ((value >> 12) & 0x0f) | 0xe0;
|
|
99
|
+
target[at++] = ((value >> 6) & 0x3f) | 0x80;
|
|
100
|
+
target[at++] = (value & 0x3f) | 0x80;
|
|
101
|
+
break;
|
|
102
|
+
case 4: // 4-byte
|
|
103
|
+
target[at++] = ((value >> 18) & 0x07) | 0xf0;
|
|
104
|
+
target[at++] = ((value >> 12) & 0x3f) | 0x80;
|
|
105
|
+
target[at++] = ((value >> 6) & 0x3f) | 0x80;
|
|
106
|
+
target[at++] = (value & 0x3f) | 0x80;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
read += codeUnitCount;
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
encoded: !HAS_DESTINATION ? target.slice(0, at) : destination,
|
|
113
|
+
read: read,
|
|
114
|
+
written: at,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
const TextEncoderE = g["TextEncoder"] || TextEncoderP;
|
|
118
|
+
|
|
119
|
+
export { TextEncoderE as TextEncoder, TextEncoderP };
|