fetch-xhr-shim 0.0.1-beta

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 (51) hide show
  1. package/dist/cjs/dev.js +55 -0
  2. package/dist/cjs/encoding/TextDecoderP.js +179 -0
  3. package/dist/cjs/encoding/TextEncoderP.js +121 -0
  4. package/dist/cjs/event-system/AbortControllerP.js +28 -0
  5. package/dist/cjs/event-system/AbortSignalP.js +117 -0
  6. package/dist/cjs/event-system/CustomEventP.js +35 -0
  7. package/dist/cjs/event-system/EventP.js +125 -0
  8. package/dist/cjs/event-system/EventTargetP.js +181 -0
  9. package/dist/cjs/event-system/ProgressEventP.js +54 -0
  10. package/dist/cjs/fetch-api/BodyImpl.js +176 -0
  11. package/dist/cjs/fetch-api/HeadersP.js +184 -0
  12. package/dist/cjs/fetch-api/RequestP.js +188 -0
  13. package/dist/cjs/fetch-api/ResponseP.js +93 -0
  14. package/dist/cjs/fetch-api/fetchP.js +129 -0
  15. package/dist/cjs/file-system/BlobP.js +171 -0
  16. package/dist/cjs/file-system/FileP.js +53 -0
  17. package/dist/cjs/file-system/FileReaderP.js +201 -0
  18. package/dist/cjs/fixes.js +243 -0
  19. package/dist/cjs/index.js +59 -0
  20. package/dist/cjs/network/FormDataP.js +308 -0
  21. package/dist/cjs/network/URLSearchParamsP.js +235 -0
  22. package/dist/cjs/polyfill.js +87 -0
  23. package/dist/cjs/utils.js +70 -0
  24. package/dist/dev.d.ts +145 -0
  25. package/dist/esm/dev.js +13 -0
  26. package/dist/esm/encoding/TextDecoderP.js +175 -0
  27. package/dist/esm/encoding/TextEncoderP.js +118 -0
  28. package/dist/esm/event-system/AbortControllerP.js +25 -0
  29. package/dist/esm/event-system/AbortSignalP.js +112 -0
  30. package/dist/esm/event-system/CustomEventP.js +32 -0
  31. package/dist/esm/event-system/EventP.js +121 -0
  32. package/dist/esm/event-system/EventTargetP.js +174 -0
  33. package/dist/esm/event-system/ProgressEventP.js +51 -0
  34. package/dist/esm/fetch-api/BodyImpl.js +172 -0
  35. package/dist/esm/fetch-api/HeadersP.js +176 -0
  36. package/dist/esm/fetch-api/RequestP.js +184 -0
  37. package/dist/esm/fetch-api/ResponseP.js +90 -0
  38. package/dist/esm/fetch-api/fetchP.js +125 -0
  39. package/dist/esm/file-system/BlobP.js +164 -0
  40. package/dist/esm/file-system/FileP.js +50 -0
  41. package/dist/esm/file-system/FileReaderP.js +197 -0
  42. package/dist/esm/fixes.js +239 -0
  43. package/dist/esm/index.js +17 -0
  44. package/dist/esm/network/FormDataP.js +301 -0
  45. package/dist/esm/network/URLSearchParamsP.js +231 -0
  46. package/dist/esm/polyfill.js +85 -0
  47. package/dist/esm/utils.js +60 -0
  48. package/dist/fetch-xhr-shim.cjs.min.js +1 -0
  49. package/dist/fetch-xhr-shim.esm.min.js +1 -0
  50. package/dist/index.d.ts +300 -0
  51. package/package.json +59 -0
@@ -0,0 +1,184 @@
1
+ import { createHeaders, Headers as HeadersE } from './HeadersP.js';
2
+ import { BodyImpl, initBody, Payload } from './BodyImpl.js';
3
+ import { AbortController as AbortControllerE } from '../event-system/AbortControllerP.js';
4
+ import { isEventTarget } from '../event-system/EventTargetP.js';
5
+ import { checkArgsLength, setState, _Symbol, isPolyfillType } from '../utils.js';
6
+
7
+ class RequestP extends BodyImpl {
8
+ constructor(input, init) {
9
+ checkArgsLength(arguments.length, 1, "Request");
10
+ super();
11
+ setState(this, "__Request__", new RequestState());
12
+ const s = state(this);
13
+ let _init = init !== null && init !== void 0 ? init : {};
14
+ if (typeof _init !== "object") {
15
+ throw new TypeError("Failed to construct 'Request': The provided value is not of type 'RequestInit'.");
16
+ }
17
+ let body = _init.body;
18
+ let bodyInited = false;
19
+ if (isRequest(input)) {
20
+ if (input.bodyUsed) {
21
+ throw new TypeError("Failed to construct 'Request': Cannot construct a Request with a Request object that has already been used.");
22
+ }
23
+ s.cache = input.cache;
24
+ s.credentials = input.credentials;
25
+ if (_init.headers === undefined) {
26
+ state(this).headers = createHeaders(input.headers);
27
+ }
28
+ s.method = input.method;
29
+ s.mode = input.mode;
30
+ s.signal = input.signal;
31
+ s.url = input.url;
32
+ if (!body) {
33
+ this.__Body__.payload = createPhonyPayload(input);
34
+ bodyInited = true;
35
+ }
36
+ }
37
+ else {
38
+ s.url = "" + input;
39
+ }
40
+ if (_init.cache) {
41
+ s.cache = _init.cache;
42
+ }
43
+ if (_init.credentials) {
44
+ s.credentials = _init.credentials;
45
+ }
46
+ if (_init.headers !== undefined) {
47
+ state(this).headers = createHeaders(_init.headers);
48
+ }
49
+ if (_init.method) {
50
+ s.method = normalizeMethod(_init.method);
51
+ }
52
+ if (_init.mode) {
53
+ s.mode = _init.mode;
54
+ }
55
+ if (_init.signal !== null && _init.signal !== undefined) {
56
+ if (isEventTarget(_init.signal))
57
+ s.signal = _init.signal;
58
+ else
59
+ throw new TypeError("Failed to construct 'Request': Failed to read the 'signal' property from 'RequestInit': Failed to convert value to 'AbortSignal'.");
60
+ }
61
+ if (this.method === "GET" || this.method === "HEAD") {
62
+ if (body !== null && body !== undefined) {
63
+ throw new TypeError("Failed to construct 'Request': Request with GET/HEAD method cannot have body.");
64
+ }
65
+ }
66
+ initBody(this, bodyInited ? null : body);
67
+ let payload = this.__Body__.payload;
68
+ if (!this.headers.has("Content-Type") && payload && payload.type) {
69
+ this.headers.set("Content-Type", payload.type);
70
+ }
71
+ if (this.method === "GET" || this.method === "HEAD") {
72
+ if (this.cache === "no-cache" || this.cache === "no-store") {
73
+ clearCache(this);
74
+ }
75
+ }
76
+ }
77
+ get cache() { return state(this).cache; }
78
+ get credentials() { return state(this).credentials; }
79
+ get destination() { return ""; }
80
+ get headers() {
81
+ if (!state(this).headers) {
82
+ state(this).headers = new HeadersE();
83
+ }
84
+ return state(this).headers;
85
+ }
86
+ get integrity() { return ""; }
87
+ get keepalive() { return false; }
88
+ get method() { return state(this).method; }
89
+ get mode() { return state(this).mode; }
90
+ get redirect() { return "follow"; }
91
+ get referrer() { return state(this).referrer; }
92
+ get referrerPolicy() { return ""; }
93
+ get signal() {
94
+ if (!state(this).signal) {
95
+ state(this).signal = (new AbortControllerE()).signal;
96
+ }
97
+ return state(this).signal;
98
+ }
99
+ get url() { return state(this).url; }
100
+ clone() {
101
+ if (!this.bodyUsed) {
102
+ let req = new RequestP(this);
103
+ this.__Body__.bodyUsed = false;
104
+ return req;
105
+ }
106
+ else {
107
+ throw new TypeError("Failed to execute 'clone' on 'Request': Request body is already used");
108
+ }
109
+ }
110
+ /** @internal */ toString() { return "[object Request]"; }
111
+ /** @internal */ get [_Symbol.toStringTag]() { return "Request"; }
112
+ /** @internal */ get __MPHTTPX__() { return { chain: ["Request", "Body"] }; }
113
+ }
114
+ /** @internal */
115
+ class RequestState {
116
+ constructor() {
117
+ this.cache = "default";
118
+ this.credentials = "same-origin";
119
+ this.method = "GET";
120
+ this.mode = "cors";
121
+ this.referrer = "about:client";
122
+ this.url = "";
123
+ }
124
+ }
125
+ function state(target) {
126
+ return target.__Request__;
127
+ }
128
+ function isRequest(value) {
129
+ return isPolyfillType("Request", value) || isExternalRequest(value);
130
+ }
131
+ function isExternalRequest(value) {
132
+ let expect = "[object Request]";
133
+ return (Object.prototype.toString.call(value) === expect || String(value) === expect)
134
+ && "arrayBuffer" in value
135
+ && typeof value.arrayBuffer === "function";
136
+ }
137
+ function createPhonyPayload(request) {
138
+ let payload = Object.create(Payload.prototype);
139
+ payload.type = request.headers.get("Content-Type") || "";
140
+ try {
141
+ payload.promise = request.arrayBuffer();
142
+ }
143
+ catch (e) {
144
+ try {
145
+ payload.promise = request.text();
146
+ }
147
+ catch (e) {
148
+ if (payload.type.slice(0, 19).toLowerCase() === "multipart/form-data") {
149
+ try {
150
+ payload.promise = request.formData().then(r => (new Payload(r, payload.type)).promise);
151
+ }
152
+ catch (e) {
153
+ // pass
154
+ }
155
+ }
156
+ }
157
+ }
158
+ if (!payload.promise) {
159
+ throw new TypeError("Failed to construct 'Request': Failed to read request body data, underlying data is in an invalid format.");
160
+ }
161
+ return payload;
162
+ }
163
+ function clearCache(req) {
164
+ // Search for a '_' parameter in the query string
165
+ let reParamSearch = /([?&])_=[^&]*/;
166
+ if (reParamSearch.test(req.url)) {
167
+ // If it already exists then set the value with the current time
168
+ state(req).url = req.url.replace(reParamSearch, "$1_=" + (new Date()).getTime());
169
+ }
170
+ else {
171
+ // Otherwise add a new '_' parameter to the end with the current time
172
+ let reQueryString = /\?/;
173
+ state(req).url += (reQueryString.test(req.url) ? "&" : "?") + "_=" + (new Date()).getTime();
174
+ }
175
+ }
176
+ // HTTP methods whose capitalization should be normalized
177
+ const methods = ["CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"];
178
+ function normalizeMethod(method) {
179
+ let upcased = method.toUpperCase();
180
+ return methods.indexOf(upcased) > -1 ? upcased : method;
181
+ }
182
+ const RequestE = (typeof Request !== "undefined" && Request) || RequestP;
183
+
184
+ export { RequestE as Request, RequestP, normalizeMethod };
@@ -0,0 +1,90 @@
1
+ import { BodyImpl, initBody } from './BodyImpl.js';
2
+ import { createHeaders, Headers as HeadersE } from './HeadersP.js';
3
+ import { checkArgsLength, setState, _Symbol } from '../utils.js';
4
+
5
+ class ResponseP extends BodyImpl {
6
+ static json(data, init) {
7
+ checkArgsLength(arguments.length, 1, "Response", "json");
8
+ let response = new ResponseP(typeof data === "string" ? data : JSON.stringify(data), init);
9
+ response.headers.set("Content-Type", "application/json");
10
+ return response;
11
+ }
12
+ static error() {
13
+ let response = new ResponseP(null, { status: 200, statusText: "" });
14
+ state(response).status = 0;
15
+ state(response).type = "error";
16
+ return response;
17
+ }
18
+ static redirect(url, status = 302) {
19
+ checkArgsLength(arguments.length, 1, "Response", "redirect");
20
+ if ([301, 302, 303, 307, 308].indexOf(status) === -1) {
21
+ throw new RangeError("Failed to execute 'redirect' on 'Response': Invalid status code");
22
+ }
23
+ return new ResponseP(null, { status, headers: { location: "" + url } });
24
+ }
25
+ constructor(body, init) {
26
+ super();
27
+ setState(this, "__Response__", new ResponseState());
28
+ const s = state(this);
29
+ let _init = init !== null && init !== void 0 ? init : {};
30
+ if (typeof _init !== "object") {
31
+ throw new TypeError("Failed to construct 'Response': The provided value is not of type 'ResponseInit'.");
32
+ }
33
+ if (_init.headers !== undefined) {
34
+ state(this).headers = createHeaders(_init.headers);
35
+ }
36
+ let status = _init.status === undefined ? 200 : _init.status;
37
+ if (status < 200 || status > 599)
38
+ throw new RangeError(`Failed to construct 'Response': The status provided (${+status}) is outside the range [200, 599].`);
39
+ s.status = status;
40
+ s.statusText = _init.statusText === undefined ? "" : "" + _init.statusText;
41
+ initBody(this, body);
42
+ let payload = this.__Body__.payload;
43
+ if (!this.headers.has("Content-Type") && payload && payload.type) {
44
+ this.headers.set("Content-Type", payload.type);
45
+ }
46
+ }
47
+ get headers() {
48
+ if (!state(this).headers) {
49
+ state(this).headers = new HeadersE();
50
+ }
51
+ return state(this).headers;
52
+ }
53
+ get ok() { return this.status >= 200 && this.status < 300; }
54
+ get redirected() { return false; }
55
+ get status() { return state(this).status; }
56
+ get statusText() { return state(this).statusText; }
57
+ get type() { return state(this).type; }
58
+ get url() { return state(this).url; }
59
+ clone() {
60
+ if (this.bodyUsed) {
61
+ throw new TypeError("Failed to execute 'clone' on 'Response': Response body is already used");
62
+ }
63
+ let response = new ResponseP(null, {
64
+ headers: createHeaders(this.headers),
65
+ status: this.status,
66
+ statusText: this.statusText,
67
+ });
68
+ state(response).url = this.url;
69
+ response.__Body__.payload = this.__Body__.payload;
70
+ return response;
71
+ }
72
+ /** @internal */ toString() { return "[object Response]"; }
73
+ /** @internal */ get [_Symbol.toStringTag]() { return "Response"; }
74
+ /** @internal */ get __MPHTTPX__() { return { chain: ["Response", "Body"] }; }
75
+ }
76
+ /** @internal */
77
+ class ResponseState {
78
+ constructor() {
79
+ this.status = 200;
80
+ this.statusText = "";
81
+ this.type = "default";
82
+ this.url = "";
83
+ }
84
+ }
85
+ function state(target) {
86
+ return target.__Response__;
87
+ }
88
+ const ResponseE = (typeof Response !== "undefined" && Response) || ResponseP;
89
+
90
+ export { ResponseE as Response, ResponseP };
@@ -0,0 +1,125 @@
1
+ import { Payload } from './BodyImpl.js';
2
+ import { RequestP } from './RequestP.js';
3
+ import { ResponseP } from './ResponseP.js';
4
+ import { checkArgsLength, DOMException as DOMExceptionE } from '../utils.js';
5
+ import { parseHeaders, isHeaders, normalizeName, normalizeValue } from './HeadersP.js';
6
+
7
+ const mp = { XMLHttpRequest: (typeof XMLHttpRequest !== "undefined" && XMLHttpRequest) || undefined };
8
+ function setXMLHttpRequestClass(XHRClass) { mp.XMLHttpRequest = XHRClass; }
9
+ function fetchP(input, init) {
10
+ checkArgsLength(arguments.length, 1, "Window", "fetch");
11
+ if (new.target === fetchP) {
12
+ throw new TypeError("fetch is not a constructor");
13
+ }
14
+ return new Promise((resolve, reject) => {
15
+ const request = new RequestP(input, init);
16
+ const signal = request.__Request__.signal;
17
+ if (signal && signal.aborted) {
18
+ return reject(signal.reason);
19
+ }
20
+ let XMLHttpRequestClass = mp.XMLHttpRequest || (() => { throw new ReferenceError("XMLHttpRequest is not defined"); })();
21
+ let xhr = new XMLHttpRequestClass();
22
+ let aborted = false;
23
+ let payload = request.__Body__.payload;
24
+ let response;
25
+ xhr.onload = () => {
26
+ let options = {
27
+ headers: parseHeaders(xhr.getAllResponseHeaders() || ""),
28
+ status: xhr.status,
29
+ statusText: xhr.statusText,
30
+ };
31
+ // This check if specifically for when a user fetches a file locally from the file system
32
+ // Only if the status is out of a normal range
33
+ if (request.url.indexOf("file://") === 0 && (xhr.status < 200 || xhr.status > 599)) {
34
+ options.status = 200;
35
+ }
36
+ setTimeout(() => {
37
+ response = new ResponseP("response" in xhr ? xhr.response : xhr.responseText, options);
38
+ response.__Response__.url = "responseURL" in xhr ? xhr.responseURL : (options.headers.get("X-Request-URL") || "");
39
+ let _payload = response.__Body__.payload;
40
+ let _contentType = response.headers.get("Content-Type") || "";
41
+ if (_payload && _contentType) {
42
+ _payload.type = _contentType;
43
+ }
44
+ if (!aborted)
45
+ resolve(response);
46
+ else
47
+ reject(createAbortException());
48
+ });
49
+ };
50
+ xhr.onabort = () => { setTimeout(() => { reject(createAbortException()); }); };
51
+ xhr.onerror = () => { setTimeout(() => { reject(new TypeError("Failed to fetch")); }); };
52
+ xhr.ontimeout = () => { setTimeout(() => { reject(new DOMExceptionE("The request timed out.", "TimeoutError")); }); };
53
+ xhr.open(request.method, fixUrl(request.url), true);
54
+ if (request.credentials === "include") {
55
+ xhr.withCredentials = true;
56
+ }
57
+ else if (request.credentials === "omit") {
58
+ xhr.withCredentials = false;
59
+ }
60
+ if ("responseType" in xhr) {
61
+ xhr.responseType = "arraybuffer";
62
+ }
63
+ if (init && typeof init === "object" && init.headers && typeof init.headers === "object" && !isHeaders(init.headers)) {
64
+ let headers = init.headers;
65
+ let names = [];
66
+ Object.getOwnPropertyNames(headers).forEach(name => {
67
+ names.push(normalizeName(name));
68
+ xhr.setRequestHeader(name, normalizeValue(headers[name]));
69
+ });
70
+ request.headers.forEach((value, name) => {
71
+ if (names.indexOf(name) === -1) {
72
+ xhr.setRequestHeader(name, value);
73
+ }
74
+ });
75
+ }
76
+ else {
77
+ request.headers.forEach((value, name) => {
78
+ xhr.setRequestHeader(name, value);
79
+ });
80
+ }
81
+ if (signal) {
82
+ const abortFn = () => {
83
+ if (response && !response.bodyUsed) {
84
+ let _payload = response.__Body__.payload = new Payload();
85
+ _payload.promise = Promise.reject(createAbortException());
86
+ }
87
+ aborted = true;
88
+ xhr.abort();
89
+ removeFn();
90
+ };
91
+ const removeFn = () => {
92
+ signal.removeEventListener("abort", abortFn);
93
+ };
94
+ signal.addEventListener("abort", abortFn);
95
+ xhr.onreadystatechange = () => { if (xhr.readyState === 4)
96
+ removeFn(); };
97
+ }
98
+ if (!payload)
99
+ xhr.send();
100
+ else
101
+ payload.promise.then(body => {
102
+ if (!aborted)
103
+ xhr.send(body !== "" ? body : undefined);
104
+ else
105
+ reject(createAbortException());
106
+ })
107
+ .catch(e => {
108
+ console.error(e);
109
+ reject(new TypeError("Failed to fetch"));
110
+ });
111
+ });
112
+ }
113
+ function createAbortException() {
114
+ return new DOMExceptionE("The user aborted a request.", "AbortError");
115
+ }
116
+ const locationSupported = typeof location !== "undefined" && !!location;
117
+ const fixUrl = (url) => { if (url === "" && locationSupported && (location === null || location === void 0 ? void 0 : location.href)) {
118
+ return location.href;
119
+ }
120
+ else {
121
+ return url;
122
+ } };
123
+ const fetchE = (typeof fetch !== "undefined" && fetch) || fetchP;
124
+
125
+ export { fetchE as fetch, fetchP, setXMLHttpRequestClass };
@@ -0,0 +1,164 @@
1
+ import { TextEncoder as TextEncoderE } from '../encoding/TextEncoderP.js';
2
+ import { TextDecoder as TextDecoderE, isArrayBuffer } from '../encoding/TextDecoderP.js';
3
+ import { isSequence, setState, _Symbol, isPolyfillType } from '../utils.js';
4
+
5
+ const encode = TextEncoderE.prototype.encode.bind(new TextEncoderE());
6
+ const decode = TextDecoderE.prototype.decode.bind(new TextDecoderE());
7
+ const mp = { ReadableStream: (typeof ReadableStream !== "undefined" && ReadableStream) || undefined };
8
+ function setReadableStreamClass(RSClass) { mp.ReadableStream = RSClass; }
9
+ class BlobP {
10
+ constructor(blobParts = [], options) {
11
+ if (!isSequence(blobParts)) {
12
+ throw new TypeError(`Failed to construct 'Blob/File': The provided value cannot be converted to a sequence.`);
13
+ }
14
+ let _blobParts = Array.isArray(blobParts) ? blobParts : Array.from(blobParts);
15
+ let tasks = [];
16
+ let size = 0;
17
+ for (let i = 0; i < _blobParts.length; ++i) {
18
+ let chunk = _blobParts[i];
19
+ if (isBlob(chunk)) {
20
+ size += chunk.size;
21
+ tasks.push(chunk.arrayBuffer().then(r => new Uint8Array(r)));
22
+ }
23
+ else {
24
+ let bytes = (isArrayBuffer(chunk) || ArrayBuffer.isView(chunk))
25
+ ? BufferSource_toUint8Array(chunk)
26
+ : encode(chunk);
27
+ size += bytes.length;
28
+ tasks.push(Promise.resolve(bytes));
29
+ }
30
+ }
31
+ setState(this, "__Blob__", new BlobState(Promise.all(tasks).then(chunks => concat(chunks))));
32
+ state(this).size = size;
33
+ state(this).type = normalizeType(options === null || options === void 0 ? void 0 : options.type);
34
+ }
35
+ get size() { return state(this).size; }
36
+ get type() { return state(this).type; }
37
+ arrayBuffer() {
38
+ return state(this).promise.then(r => clone(r.buffer).buffer);
39
+ }
40
+ bytes() {
41
+ return state(this).promise.then(r => clone(r.buffer));
42
+ }
43
+ slice(start, end, contentType) {
44
+ let _start = start !== null && start !== void 0 ? start : 0, _end = end !== null && end !== void 0 ? end : this.size;
45
+ let blob = Object.create(BlobP.prototype);
46
+ setState(blob, "__Blob__", new BlobState(state(this).promise.then(r => clone(r.slice(_start, _end)))));
47
+ state(blob).size = calcSlicedSize(this.size, _start, _end);
48
+ state(blob).type = normalizeType(contentType);
49
+ return blob;
50
+ }
51
+ stream() {
52
+ let ReadableStreamClass = mp.ReadableStream || (() => { throw new ReferenceError("ReadableStream is not defined"); })();
53
+ let position = 0;
54
+ try {
55
+ return new ReadableStreamClass({
56
+ type: "bytes",
57
+ autoAllocateChunkSize: 524288,
58
+ pull: (function (controller) {
59
+ var _a;
60
+ let v = (_a = controller.byobRequest) === null || _a === void 0 ? void 0 : _a.view;
61
+ let chunk = this.slice(position, (v === null || v === void 0 ? void 0 : v.byteLength) ? (position + v.byteLength) : undefined);
62
+ return chunk.arrayBuffer().then((function (buffer) {
63
+ var _a;
64
+ let uint8array = new Uint8Array(buffer);
65
+ let bytesRead = uint8array.byteLength;
66
+ position += bytesRead;
67
+ v.set(uint8array);
68
+ (_a = controller.byobRequest) === null || _a === void 0 ? void 0 : _a.respond(bytesRead);
69
+ if (position >= this.size)
70
+ controller.close();
71
+ }).bind(this));
72
+ }).bind(this),
73
+ });
74
+ }
75
+ catch (e) {
76
+ return new ReadableStreamClass({
77
+ pull: (function (controller) {
78
+ let chunk = this.slice(position, position + 524288);
79
+ return chunk.arrayBuffer().then((function (buffer) {
80
+ position += buffer.byteLength;
81
+ let uint8array = new Uint8Array(buffer);
82
+ controller.enqueue(uint8array);
83
+ if (position == this.size)
84
+ controller.close();
85
+ }).bind(this));
86
+ }).bind(this),
87
+ });
88
+ }
89
+ }
90
+ text() {
91
+ return state(this).promise.then(r => decode(r));
92
+ }
93
+ /** @internal */ toString() { return "[object Blob]"; }
94
+ /** @internal */ get [_Symbol.toStringTag]() { return "Blob"; }
95
+ /** @internal */ get __MPHTTPX__() { return { chain: ["Blob"] }; }
96
+ }
97
+ /** @internal */
98
+ class BlobState {
99
+ constructor(promise) {
100
+ this.size = 0;
101
+ this.type = "";
102
+ this.promise = promise;
103
+ }
104
+ }
105
+ function state(target) {
106
+ return target.__Blob__;
107
+ }
108
+ function BufferSource_toUint8Array(buf) {
109
+ return isArrayBuffer(buf)
110
+ ? new Uint8Array(buf)
111
+ : new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
112
+ }
113
+ function normalizeType(type) {
114
+ let rawType = "" + (type !== undefined ? type : "");
115
+ return /[^\u0020-\u007E]/.test(rawType) ? "" : rawType.toLowerCase();
116
+ }
117
+ function clone(buf) {
118
+ let sourceArray = BufferSource_toUint8Array(buf);
119
+ let cloneArray = new Uint8Array(new ArrayBuffer(sourceArray.byteLength));
120
+ cloneArray.set(sourceArray);
121
+ return cloneArray;
122
+ }
123
+ function concat(chunks) {
124
+ let totalByteLength = chunks.reduce((acc, cur) => acc + cur.byteLength, 0);
125
+ let result = new Uint8Array(totalByteLength);
126
+ chunks.reduce((offset, chunk) => { result.set(chunk, offset); return offset + chunk.byteLength; }, 0);
127
+ return result;
128
+ }
129
+ function calcSlicedSize(size, start, end) {
130
+ const normalizeNumer = (n) => {
131
+ let num = Number(n);
132
+ if (isNaN(num))
133
+ num = 0;
134
+ if (num >= 0) {
135
+ num = Math.min(num, size);
136
+ }
137
+ else {
138
+ num = Math.max(0, num + size);
139
+ }
140
+ return num;
141
+ };
142
+ return Math.max(0, normalizeNumer(end) - normalizeNumer(start));
143
+ }
144
+ function isBlob(value, strict = false) {
145
+ return isPolyfillType("Blob", value, strict) || isExternalBlob(value, strict);
146
+ }
147
+ function isExternalBlob(value, strict = false) {
148
+ let expects = ["[object Blob]"];
149
+ if (!strict)
150
+ expects.push("[object File]");
151
+ return (expects.indexOf(Object.prototype.toString.call(value)) > -1 || expects.indexOf(String(value)) > -1)
152
+ && "size" in value
153
+ && typeof value.size === "number"
154
+ && "arrayBuffer" in value
155
+ && typeof value.arrayBuffer === "function";
156
+ }
157
+ const BlobE = (function () { try {
158
+ return new Blob(['ä']).size === 2;
159
+ }
160
+ catch (e) {
161
+ return false;
162
+ } })() ? Blob : BlobP;
163
+
164
+ export { BlobE as Blob, BlobP, decode, encode, isBlob, setReadableStreamClass };
@@ -0,0 +1,50 @@
1
+ import { Blob as BlobE, BlobP } from './BlobP.js';
2
+ import { checkArgsLength, setState, _Symbol } from '../utils.js';
3
+
4
+ function createFileClass(BlobClass) {
5
+ class FileClass extends BlobClass {
6
+ constructor(fileBits, fileName, options) {
7
+ checkArgsLength(arguments.length, 2, "File");
8
+ super(fileBits, options);
9
+ setState(this, "__File__", new FileState());
10
+ state(this).lastModified = +((options === null || options === void 0 ? void 0 : options.lastModified) ? new Date(options.lastModified) : new Date()) || 0;
11
+ state(this).name = "" + fileName;
12
+ }
13
+ get lastModified() { return state(this).lastModified; }
14
+ get name() { return state(this).name; }
15
+ get webkitRelativePath() { return ""; }
16
+ /** @internal */ toString() { return "[object File]"; }
17
+ /** @internal */ get [_Symbol.toStringTag]() { return "File"; }
18
+ }
19
+ if (Object.is(BlobClass, BlobP)) {
20
+ Object.defineProperty(FileClass.prototype, "__MPHTTPX__", {
21
+ configurable: true,
22
+ get: function () { return { chain: ["File", "Blob"] }; },
23
+ });
24
+ }
25
+ return FileClass;
26
+ }
27
+ /** @internal */
28
+ class FileState {
29
+ constructor() {
30
+ this.lastModified = 0;
31
+ this.name = "";
32
+ }
33
+ }
34
+ function state(target) {
35
+ return target.__File__;
36
+ }
37
+ const FilePolyfill = createFileClass(BlobP);
38
+ const FileE = (function () { try {
39
+ new File([], "");
40
+ return true;
41
+ }
42
+ catch (e) {
43
+ return false;
44
+ } })()
45
+ ? File
46
+ : Object.is(BlobE, BlobP)
47
+ ? FilePolyfill
48
+ : createFileClass(BlobE);
49
+
50
+ export { FileE as File, FilePolyfill as FileP };