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