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.
- package/dist/cjs/dev.js +55 -0
- package/dist/cjs/encoding/TextDecoderP.js +179 -0
- package/dist/cjs/encoding/TextEncoderP.js +121 -0
- package/dist/cjs/event-system/AbortControllerP.js +28 -0
- package/dist/cjs/event-system/AbortSignalP.js +117 -0
- package/dist/cjs/event-system/CustomEventP.js +35 -0
- package/dist/cjs/event-system/EventP.js +125 -0
- package/dist/cjs/event-system/EventTargetP.js +181 -0
- package/dist/cjs/event-system/ProgressEventP.js +54 -0
- package/dist/cjs/fetch-api/BodyImpl.js +176 -0
- package/dist/cjs/fetch-api/HeadersP.js +184 -0
- package/dist/cjs/fetch-api/RequestP.js +188 -0
- package/dist/cjs/fetch-api/ResponseP.js +93 -0
- package/dist/cjs/fetch-api/fetchP.js +129 -0
- package/dist/cjs/file-system/BlobP.js +171 -0
- package/dist/cjs/file-system/FileP.js +53 -0
- package/dist/cjs/file-system/FileReaderP.js +201 -0
- package/dist/cjs/fixes.js +243 -0
- package/dist/cjs/index.js +59 -0
- package/dist/cjs/network/FormDataP.js +308 -0
- package/dist/cjs/network/URLSearchParamsP.js +235 -0
- package/dist/cjs/polyfill.js +87 -0
- package/dist/cjs/utils.js +70 -0
- package/dist/dev.d.ts +145 -0
- package/dist/esm/dev.js +13 -0
- package/dist/esm/encoding/TextDecoderP.js +175 -0
- package/dist/esm/encoding/TextEncoderP.js +118 -0
- package/dist/esm/event-system/AbortControllerP.js +25 -0
- package/dist/esm/event-system/AbortSignalP.js +112 -0
- package/dist/esm/event-system/CustomEventP.js +32 -0
- package/dist/esm/event-system/EventP.js +121 -0
- package/dist/esm/event-system/EventTargetP.js +174 -0
- package/dist/esm/event-system/ProgressEventP.js +51 -0
- package/dist/esm/fetch-api/BodyImpl.js +172 -0
- package/dist/esm/fetch-api/HeadersP.js +176 -0
- package/dist/esm/fetch-api/RequestP.js +184 -0
- package/dist/esm/fetch-api/ResponseP.js +90 -0
- package/dist/esm/fetch-api/fetchP.js +125 -0
- package/dist/esm/file-system/BlobP.js +164 -0
- package/dist/esm/file-system/FileP.js +50 -0
- package/dist/esm/file-system/FileReaderP.js +197 -0
- package/dist/esm/fixes.js +239 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/network/FormDataP.js +301 -0
- package/dist/esm/network/URLSearchParamsP.js +231 -0
- package/dist/esm/polyfill.js +85 -0
- package/dist/esm/utils.js +60 -0
- package/dist/fetch-xhr-shim.cjs.min.js +1 -0
- package/dist/fetch-xhr-shim.esm.min.js +1 -0
- package/dist/index.d.ts +300 -0
- package/package.json +59 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { checkArgsLength, setState, className, _Symbol } from '../utils.js';
|
|
2
|
+
|
|
3
|
+
class EventP {
|
|
4
|
+
static get NONE() { return 0; }
|
|
5
|
+
static get CAPTURING_PHASE() { return 1; }
|
|
6
|
+
static get AT_TARGET() { return 2; }
|
|
7
|
+
static get BUBBLING_PHASE() { return 3; }
|
|
8
|
+
constructor(type, eventInitDict) {
|
|
9
|
+
checkArgsLength(arguments.length, 1, "Event");
|
|
10
|
+
setState(this, "__Event__", new EventState());
|
|
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);
|
|
16
|
+
Object.defineProperty(this, "isTrusted", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function isTrusted() { return s.isTrusted === "YES"; },
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
get NONE() { return 0; }
|
|
22
|
+
get CAPTURING_PHASE() { return 1; }
|
|
23
|
+
get AT_TARGET() { return 2; }
|
|
24
|
+
get BUBBLING_PHASE() { return 3; }
|
|
25
|
+
get bubbles() { return state(this).bubbles; }
|
|
26
|
+
get cancelBubble() { return state(this).cancelBubble; }
|
|
27
|
+
set cancelBubble(value) { if (value) {
|
|
28
|
+
state(this).cancelBubble = true;
|
|
29
|
+
} }
|
|
30
|
+
get cancelable() { return state(this).cancelable; }
|
|
31
|
+
get composed() { return state(this).composed; }
|
|
32
|
+
get currentTarget() { return state(this).currentTarget; }
|
|
33
|
+
get defaultPrevented() { return state(this).defaultPrevented; }
|
|
34
|
+
get eventPhase() { return state(this).eventPhase; }
|
|
35
|
+
get returnValue() { return state(this).returnValue; }
|
|
36
|
+
set returnValue(value) { if (!value) {
|
|
37
|
+
this.preventDefault();
|
|
38
|
+
} }
|
|
39
|
+
get srcElement() { return state(this).target; }
|
|
40
|
+
get target() { return state(this).target; }
|
|
41
|
+
get timeStamp() { return state(this).timeStamp; }
|
|
42
|
+
get type() { return state(this).type; }
|
|
43
|
+
composedPath() {
|
|
44
|
+
let path = this.target ? [this.target] : [];
|
|
45
|
+
if (this.currentTarget && this.currentTarget !== this.target)
|
|
46
|
+
path.push(this.currentTarget);
|
|
47
|
+
return path;
|
|
48
|
+
}
|
|
49
|
+
initEvent(type, bubbles, cancelable) {
|
|
50
|
+
checkArgsLength(arguments.length, 1, className(this), "initEvent");
|
|
51
|
+
let s = state(this);
|
|
52
|
+
if (s.eventDispatched)
|
|
53
|
+
return;
|
|
54
|
+
s.type = "" + type;
|
|
55
|
+
s.bubbles = !!bubbles;
|
|
56
|
+
s.cancelable = !!cancelable;
|
|
57
|
+
}
|
|
58
|
+
preventDefault() {
|
|
59
|
+
let s = state(this);
|
|
60
|
+
if (s.passive)
|
|
61
|
+
return console.warn(`Ignoring 'preventDefault()' call on event of type '${this.type}' from a listener registered as 'passive'.`);
|
|
62
|
+
if (this.cancelable) {
|
|
63
|
+
s.defaultPrevented = true;
|
|
64
|
+
s.returnValue = false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
stopImmediatePropagation() {
|
|
68
|
+
state(this).immediatePropagationStopped = true;
|
|
69
|
+
this.cancelBubble = true;
|
|
70
|
+
}
|
|
71
|
+
stopPropagation() {
|
|
72
|
+
this.cancelBubble = true;
|
|
73
|
+
}
|
|
74
|
+
/** @internal */ toString() { return "[object Event]"; }
|
|
75
|
+
/** @internal */ get [_Symbol.toStringTag]() { return "Event"; }
|
|
76
|
+
/** @internal */ get __MPHTTPX__() { return { chain: ["Event"] }; }
|
|
77
|
+
}
|
|
78
|
+
/** @internal */
|
|
79
|
+
class EventState {
|
|
80
|
+
constructor() {
|
|
81
|
+
this.bubbles = false;
|
|
82
|
+
this.cancelBubble = false;
|
|
83
|
+
this.cancelable = false;
|
|
84
|
+
this.composed = false;
|
|
85
|
+
this.currentTarget = null;
|
|
86
|
+
this.defaultPrevented = false;
|
|
87
|
+
this.eventPhase = 0 /* NONE */;
|
|
88
|
+
this.returnValue = true;
|
|
89
|
+
this.target = null;
|
|
90
|
+
this.timeStamp = (new Date()).getTime() - timeStamp;
|
|
91
|
+
this.type = "";
|
|
92
|
+
this.passive = false;
|
|
93
|
+
this.eventDispatched = false;
|
|
94
|
+
this.immediatePropagationStopped = false;
|
|
95
|
+
Object.defineProperty(this, "isTrusted", createTrustedPropertyDescriptor());
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function state(target) {
|
|
99
|
+
return target.__Event__;
|
|
100
|
+
}
|
|
101
|
+
const timeStamp = (new Date()).getTime();
|
|
102
|
+
function createTrustedPropertyDescriptor() {
|
|
103
|
+
let name = createTrustedPropertyDescriptor.name;
|
|
104
|
+
let idx = name.length;
|
|
105
|
+
let isTrusted = "NO";
|
|
106
|
+
return {
|
|
107
|
+
configurable: false,
|
|
108
|
+
enumerable: true,
|
|
109
|
+
get: function () { return isTrusted; },
|
|
110
|
+
set: function (value) { if (name === value.slice(0, idx)) {
|
|
111
|
+
isTrusted = value.slice(idx + 1);
|
|
112
|
+
} },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function Event_setTrusted(event, isTrusted) {
|
|
116
|
+
let s = state(event) || {};
|
|
117
|
+
s.isTrusted = `${createTrustedPropertyDescriptor.name}:${isTrusted ? "YES" : "NO"}`;
|
|
118
|
+
}
|
|
119
|
+
const EventE = (typeof Event !== "undefined" && Event) || EventP;
|
|
120
|
+
|
|
121
|
+
export { EventE as Event, EventP, Event_setTrusted };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Event_setTrusted } from './EventP.js';
|
|
2
|
+
import { setState, checkArgsLength, className, isPolyfillType, _Symbol } from '../utils.js';
|
|
3
|
+
|
|
4
|
+
class EventTargetP {
|
|
5
|
+
constructor() {
|
|
6
|
+
setState(this, "__EventTarget__", new EventTargetState());
|
|
7
|
+
}
|
|
8
|
+
addEventListener(type, callback, options) {
|
|
9
|
+
checkArgsLength(arguments.length, 2, className(this), "addEventListener");
|
|
10
|
+
if (typeof callback !== "function" && typeof callback !== "object" && callback !== undefined) {
|
|
11
|
+
throw new TypeError(`Failed to execute 'addEventListener' on '${className(this)}': parameter 2 is not of type 'Object'.`);
|
|
12
|
+
}
|
|
13
|
+
let s = state(this) || { executors: [] };
|
|
14
|
+
let executor = new Executor(type, callback);
|
|
15
|
+
let capture = executor.options.capture = typeof options === "boolean" ? options : !!(options === null || options === void 0 ? void 0 : options.capture);
|
|
16
|
+
if (!s.executors.some(x => x.equals(executor))) {
|
|
17
|
+
s.executors.push(executor);
|
|
18
|
+
if (options && typeof options === "object") {
|
|
19
|
+
executor.options.once = !!options.once;
|
|
20
|
+
executor.options.passive = !!options.passive;
|
|
21
|
+
const signal = options.signal;
|
|
22
|
+
if (signal && isEventTarget(signal) && !signal.aborted) {
|
|
23
|
+
executor.options.signal = signal;
|
|
24
|
+
whenAbort(this, executor, signal);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (capture) {
|
|
28
|
+
let f = (v) => !!v.options.capture ? 0 : 1;
|
|
29
|
+
s.executors = s.executors.sort((a, b) => f(a) - f(b));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
dispatchEvent(event) {
|
|
34
|
+
checkArgsLength(arguments.length, 1, className(this), "dispatchEvent");
|
|
35
|
+
if (isPolyfillType("Event", event)) {
|
|
36
|
+
Event_setTrusted(event, false);
|
|
37
|
+
}
|
|
38
|
+
else if (!isEvent(event)) {
|
|
39
|
+
throw new TypeError(`Failed to execute 'dispatchEvent' on '${className(this)}': parameter 1 is not of type 'Event'.`);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
console.warn(`WARNING: undefined behavior when executing 'dispatchEvent' on '${className(this)}': parameter 1 is not of type 'Event(P)'.`);
|
|
43
|
+
}
|
|
44
|
+
return EventTarget_dispatchEvent(this, event);
|
|
45
|
+
}
|
|
46
|
+
removeEventListener(type, callback, options) {
|
|
47
|
+
checkArgsLength(arguments.length, 2, className(this), "removeEventListener");
|
|
48
|
+
if (typeof callback !== "function" && typeof callback !== "object" && callback !== undefined) {
|
|
49
|
+
throw new TypeError(`Failed to execute 'removeEventListener' on '${className(this)}': parameter 2 is not of type 'Object'.`);
|
|
50
|
+
}
|
|
51
|
+
let s = state(this) || { executors: [] };
|
|
52
|
+
let executor = new Executor(type, callback);
|
|
53
|
+
executor.options.capture = typeof options === "boolean" ? options : !!(options === null || options === void 0 ? void 0 : options.capture);
|
|
54
|
+
if (s.executors.some(x => x.equals(executor))) {
|
|
55
|
+
s.executors = s.executors.filter(x => !x.equals(executor));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** @internal */ toString() { return "[object EventTarget]"; }
|
|
59
|
+
/** @internal */ get [_Symbol.toStringTag]() { return "EventTarget"; }
|
|
60
|
+
/** @internal */ get __MPHTTPX__() { return { chain: ["EventTarget"] }; }
|
|
61
|
+
}
|
|
62
|
+
/** @internal */
|
|
63
|
+
class EventTargetState {
|
|
64
|
+
constructor() {
|
|
65
|
+
this.executors = [];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function state(target) {
|
|
69
|
+
return target.__EventTarget__;
|
|
70
|
+
}
|
|
71
|
+
function isEvent(value) {
|
|
72
|
+
const predicate = (str) => { return "[object " === str.slice(0, 8) && str.slice(-6) === "Event]"; };
|
|
73
|
+
return !!value
|
|
74
|
+
&& typeof value === "object"
|
|
75
|
+
&& (predicate(Object.prototype.toString.call(value)) || predicate(String(value)))
|
|
76
|
+
&& "type" in value
|
|
77
|
+
&& typeof value.type === "string";
|
|
78
|
+
}
|
|
79
|
+
function whenAbort(target, executor, signal) {
|
|
80
|
+
const onAbort = () => {
|
|
81
|
+
state(target).executors = state(target).executors.filter(x => !x.equals(executor));
|
|
82
|
+
signal.removeEventListener("abort", onAbort);
|
|
83
|
+
};
|
|
84
|
+
signal.addEventListener("abort", onAbort);
|
|
85
|
+
}
|
|
86
|
+
/** @internal */
|
|
87
|
+
class Executor {
|
|
88
|
+
constructor(type, callback) {
|
|
89
|
+
this.options = {};
|
|
90
|
+
this.type = "" + type;
|
|
91
|
+
this.callback = extract(callback);
|
|
92
|
+
}
|
|
93
|
+
equals(executor) {
|
|
94
|
+
return Object.is(this.type, executor.type)
|
|
95
|
+
&& Object.is(this.callback, executor.callback)
|
|
96
|
+
&& Object.is(this.options.capture, executor.options.capture);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function extract(cb) {
|
|
100
|
+
return typeof cb === "function" ? cb : isEventListenerObject(cb) ? cb.handleEvent : cb;
|
|
101
|
+
}
|
|
102
|
+
function isEventListenerObject(cb) {
|
|
103
|
+
return !!cb && typeof cb === "object" && "handleEvent" in cb && typeof cb.handleEvent === "function";
|
|
104
|
+
}
|
|
105
|
+
function EventTarget_dispatchEvent(target, event) {
|
|
106
|
+
const s = state(target) || {};
|
|
107
|
+
const evs = event.__Event__ || {};
|
|
108
|
+
evs.target = target;
|
|
109
|
+
evs.currentTarget = target;
|
|
110
|
+
evs.eventPhase = 2 /* AT_TARGET */;
|
|
111
|
+
evs.eventDispatched = true;
|
|
112
|
+
let onceIndexes = [];
|
|
113
|
+
if (!Array.isArray(s.executors)) {
|
|
114
|
+
s.executors = [];
|
|
115
|
+
}
|
|
116
|
+
for (let i = 0; i < s.executors.length; ++i) {
|
|
117
|
+
if (evs.immediatePropagationStopped)
|
|
118
|
+
break;
|
|
119
|
+
let executor = s.executors[i];
|
|
120
|
+
if (executor.type !== event.type)
|
|
121
|
+
continue;
|
|
122
|
+
if (executor.options.once)
|
|
123
|
+
onceIndexes.push(i);
|
|
124
|
+
evs.passive = !!executor.options.passive;
|
|
125
|
+
try {
|
|
126
|
+
let cb = executor.callback;
|
|
127
|
+
if (typeof cb === "function")
|
|
128
|
+
cb.call(target, event);
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
console.error(e);
|
|
132
|
+
}
|
|
133
|
+
evs.passive = false;
|
|
134
|
+
}
|
|
135
|
+
if (onceIndexes.length > 0) {
|
|
136
|
+
s.executors = s.executors.reduce((acc, cur, index) => {
|
|
137
|
+
if (onceIndexes.indexOf(index) === -1)
|
|
138
|
+
acc.push(cur);
|
|
139
|
+
return acc;
|
|
140
|
+
}, []);
|
|
141
|
+
}
|
|
142
|
+
evs.currentTarget = null;
|
|
143
|
+
evs.eventPhase = 0 /* NONE */;
|
|
144
|
+
evs.eventDispatched = false;
|
|
145
|
+
return !(event.cancelable && event.defaultPrevented);
|
|
146
|
+
}
|
|
147
|
+
function attachFn(target, handlers) {
|
|
148
|
+
return function attach(type) {
|
|
149
|
+
const fnName = ("on" + type);
|
|
150
|
+
const callback = target[fnName];
|
|
151
|
+
const listener = handlers[fnName];
|
|
152
|
+
typeof callback === "function"
|
|
153
|
+
? EventTargetP.prototype.addEventListener.call(target, type, listener)
|
|
154
|
+
: EventTargetP.prototype.removeEventListener.call(target, type, listener);
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function executeFn(target, cb, ev) {
|
|
158
|
+
if (typeof cb === "function")
|
|
159
|
+
cb.call(target, ev);
|
|
160
|
+
}
|
|
161
|
+
function isEventTarget(value) {
|
|
162
|
+
return isPolyfillType("EventTarget", value) || isExternalEventTarget(value);
|
|
163
|
+
}
|
|
164
|
+
function isExternalEventTarget(value) {
|
|
165
|
+
return !!value
|
|
166
|
+
&& typeof value === "object"
|
|
167
|
+
&& "addEventListener" in value
|
|
168
|
+
&& typeof value.addEventListener === "function"
|
|
169
|
+
&& "removeEventListener" in value
|
|
170
|
+
&& typeof value.removeEventListener === "function";
|
|
171
|
+
}
|
|
172
|
+
const EventTargetE = (typeof EventTarget !== "undefined" && EventTarget) || EventTargetP;
|
|
173
|
+
|
|
174
|
+
export { EventTargetE as EventTarget, EventTargetP, EventTarget_dispatchEvent, attachFn, executeFn, isEventTarget };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { setState, _Symbol } from '../utils.js';
|
|
2
|
+
import { Event_setTrusted, EventP } from './EventP.js';
|
|
3
|
+
import { EventTarget_dispatchEvent } from './EventTargetP.js';
|
|
4
|
+
|
|
5
|
+
const fields = ["loaded", "total"];
|
|
6
|
+
class ProgressEventP extends EventP {
|
|
7
|
+
constructor(type, eventInitDict) {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
super(type, eventInitDict);
|
|
10
|
+
setState(this, "__ProgressEvent__", new ProgressEventState());
|
|
11
|
+
const s = state(this);
|
|
12
|
+
s.lengthComputable = !!(eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.lengthComputable);
|
|
13
|
+
s.loaded = Number((_a = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.loaded) !== null && _a !== void 0 ? _a : 0);
|
|
14
|
+
s.total = Number((_b = eventInitDict === null || eventInitDict === void 0 ? void 0 : eventInitDict.total) !== null && _b !== void 0 ? _b : 0);
|
|
15
|
+
for (let i = 0; i < fields.length; ++i) {
|
|
16
|
+
let field = fields[i];
|
|
17
|
+
if (isNaN(this[field])) {
|
|
18
|
+
throw new TypeError(`Failed to construct 'ProgressEvent': Failed to read the '${field}' property from 'ProgressEventInit': The provided double value is non-finite.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
get lengthComputable() { return state(this).lengthComputable; }
|
|
23
|
+
get loaded() { return state(this).loaded; }
|
|
24
|
+
get total() { return state(this).total; }
|
|
25
|
+
/** @internal */ toString() { return "[object ProgressEvent]"; }
|
|
26
|
+
/** @internal */ get [_Symbol.toStringTag]() { return "ProgressEvent"; }
|
|
27
|
+
/** @internal */ get __MPHTTPX__() { return { chain: ["ProgressEvent", "Event"] }; }
|
|
28
|
+
}
|
|
29
|
+
/** @internal */
|
|
30
|
+
class ProgressEventState {
|
|
31
|
+
constructor() {
|
|
32
|
+
this.lengthComputable = false;
|
|
33
|
+
this.loaded = 0;
|
|
34
|
+
this.total = 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function state(target) {
|
|
38
|
+
return target.__ProgressEvent__;
|
|
39
|
+
}
|
|
40
|
+
function emitProgressEvent(target, type, loaded = 0, total = 0) {
|
|
41
|
+
let event = new ProgressEventP(type, {
|
|
42
|
+
lengthComputable: total > 0,
|
|
43
|
+
loaded,
|
|
44
|
+
total,
|
|
45
|
+
});
|
|
46
|
+
event.__Event__.target = target;
|
|
47
|
+
Event_setTrusted(event, true);
|
|
48
|
+
EventTarget_dispatchEvent(target, event);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { ProgressEventP, emitProgressEvent };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { setState, className, _Symbol, isObjectType } from '../utils.js';
|
|
2
|
+
import { isArrayBuffer } from '../encoding/TextDecoderP.js';
|
|
3
|
+
import { isURLSearchParams } from '../network/URLSearchParamsP.js';
|
|
4
|
+
import { encode, isBlob, decode, Blob as BlobE } from '../file-system/BlobP.js';
|
|
5
|
+
import { isFormData, FormData_toBlob, extractBoundary, createFormDataFromBinaryText } from '../network/FormDataP.js';
|
|
6
|
+
|
|
7
|
+
class Payload {
|
|
8
|
+
constructor(body, contentType) {
|
|
9
|
+
this._type = "";
|
|
10
|
+
if (contentType) {
|
|
11
|
+
this.type = contentType;
|
|
12
|
+
}
|
|
13
|
+
if (typeof body === "string") {
|
|
14
|
+
this.promise = Promise.resolve(body);
|
|
15
|
+
this.type = "text/plain;charset=UTF-8";
|
|
16
|
+
this.calcLength = () => encode(body).length;
|
|
17
|
+
}
|
|
18
|
+
else if (isURLSearchParams(body)) {
|
|
19
|
+
let _body = body.toString();
|
|
20
|
+
this.promise = Promise.resolve(_body);
|
|
21
|
+
this.type = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
22
|
+
this.calcLength = () => encode(_body).length;
|
|
23
|
+
}
|
|
24
|
+
else if (isArrayBuffer(body)) {
|
|
25
|
+
this.promise = Promise.resolve(body.slice(0));
|
|
26
|
+
this.length = body.byteLength;
|
|
27
|
+
}
|
|
28
|
+
else if (ArrayBuffer.isView(body)) {
|
|
29
|
+
let _body = body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength);
|
|
30
|
+
this.promise = Promise.resolve(_body);
|
|
31
|
+
this.length = _body.byteLength;
|
|
32
|
+
}
|
|
33
|
+
else if (isBlob(body)) {
|
|
34
|
+
this.promise = body.arrayBuffer();
|
|
35
|
+
this.type = body.type;
|
|
36
|
+
this.length = body.size;
|
|
37
|
+
}
|
|
38
|
+
else if (isFormData(body)) {
|
|
39
|
+
let _body = FormData_toBlob(body, contentType ? extractBoundary(contentType) : undefined);
|
|
40
|
+
this.promise = _body.arrayBuffer();
|
|
41
|
+
this.type = _body.type;
|
|
42
|
+
this.length = _body.size;
|
|
43
|
+
}
|
|
44
|
+
else if (body === null || body === undefined) {
|
|
45
|
+
this.promise = Promise.resolve("");
|
|
46
|
+
this.length = 0;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
let _body = "" + body;
|
|
50
|
+
this.promise = Promise.resolve(_body);
|
|
51
|
+
this.type = "text/plain;charset=UTF-8";
|
|
52
|
+
this.calcLength = () => encode(_body).length;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
get type() { return this._type; }
|
|
56
|
+
set type(value) { if (!this._type)
|
|
57
|
+
this._type = value; }
|
|
58
|
+
get size() {
|
|
59
|
+
var _a;
|
|
60
|
+
if (typeof this.length !== "number" && this.calcLength)
|
|
61
|
+
this.length = this.calcLength();
|
|
62
|
+
return (_a = this.length) !== null && _a !== void 0 ? _a : 0;
|
|
63
|
+
}
|
|
64
|
+
text() { return this.promise.then(r => typeof r === "string" ? r : decode(r)); }
|
|
65
|
+
arrayBuffer() { return this.promise.then(r => isArrayBuffer(r) ? r : encode(r).buffer); }
|
|
66
|
+
}
|
|
67
|
+
class BodyImpl {
|
|
68
|
+
/** @internal */
|
|
69
|
+
constructor() {
|
|
70
|
+
if (new.target === BodyImpl) {
|
|
71
|
+
throw new TypeError("Failed to construct 'Body': Illegal constructor");
|
|
72
|
+
}
|
|
73
|
+
setState(this, "__Body__", new BodyState());
|
|
74
|
+
}
|
|
75
|
+
get bodyUsed() { return state(this).bodyUsed; }
|
|
76
|
+
;
|
|
77
|
+
get body() {
|
|
78
|
+
throw new TypeError(`Failed to access 'body' on '${className(this)}': property not implemented.`);
|
|
79
|
+
}
|
|
80
|
+
arrayBuffer() {
|
|
81
|
+
const kind = "arrayBuffer";
|
|
82
|
+
return consumed(this, kind) || read(this, kind);
|
|
83
|
+
}
|
|
84
|
+
blob() {
|
|
85
|
+
const kind = "blob";
|
|
86
|
+
return consumed(this, kind) || read(this, kind);
|
|
87
|
+
}
|
|
88
|
+
bytes() {
|
|
89
|
+
const kind = "bytes";
|
|
90
|
+
return consumed(this, kind) || read(this, kind);
|
|
91
|
+
}
|
|
92
|
+
formData() {
|
|
93
|
+
const kind = "formData";
|
|
94
|
+
return consumed(this, kind) || read(this, kind);
|
|
95
|
+
}
|
|
96
|
+
json() {
|
|
97
|
+
const kind = "json";
|
|
98
|
+
return consumed(this, kind) || read(this, kind);
|
|
99
|
+
}
|
|
100
|
+
text() {
|
|
101
|
+
const kind = "text";
|
|
102
|
+
return consumed(this, kind) || read(this, kind);
|
|
103
|
+
}
|
|
104
|
+
/** @internal */ toString() { return "[object Body]"; }
|
|
105
|
+
/** @internal */ get [_Symbol.toStringTag]() { return "Body"; }
|
|
106
|
+
/** @internal */ get __MPHTTPX__() { return { chain: ["Body"] }; }
|
|
107
|
+
}
|
|
108
|
+
/** @internal */
|
|
109
|
+
class BodyState {
|
|
110
|
+
constructor() {
|
|
111
|
+
this.bodyUsed = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function state(target) {
|
|
115
|
+
return target.__Body__;
|
|
116
|
+
}
|
|
117
|
+
function initBody(instance, body) {
|
|
118
|
+
const b = instance;
|
|
119
|
+
if (isGlobalReadableStream(body) || isOtherReadableStream(body)) {
|
|
120
|
+
throw new TypeError(`Failed to construct '${className(b)}': ReadableStream not implemented.`);
|
|
121
|
+
}
|
|
122
|
+
if (body !== null && body !== undefined) {
|
|
123
|
+
state(b).payload = new Payload(body);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function read(body, kind) {
|
|
127
|
+
let payload = state(body).payload || new Payload();
|
|
128
|
+
if (kind === "json") {
|
|
129
|
+
return payload.text().then(r => JSON.parse(r));
|
|
130
|
+
}
|
|
131
|
+
else if (kind === "text") {
|
|
132
|
+
return payload.text();
|
|
133
|
+
}
|
|
134
|
+
else if (kind === "arrayBuffer") {
|
|
135
|
+
return payload.arrayBuffer();
|
|
136
|
+
}
|
|
137
|
+
else if (kind === "bytes") {
|
|
138
|
+
return payload.arrayBuffer().then(r => new Uint8Array(r));
|
|
139
|
+
}
|
|
140
|
+
else if (kind === "blob") {
|
|
141
|
+
return payload.promise.then(r => new BlobE([r]));
|
|
142
|
+
}
|
|
143
|
+
else if (kind === "formData") {
|
|
144
|
+
return payload.text().then(r => createFormDataFromBinaryText(r, extractBoundary(payload.type)));
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
return payload.promise;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function consumed(body, kind) {
|
|
151
|
+
if (!state(body).payload)
|
|
152
|
+
return;
|
|
153
|
+
if (!body.bodyUsed) {
|
|
154
|
+
state(body).bodyUsed = true;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
return Promise.reject(new TypeError(`Failed to execute '${kind}' on '${className(body)}': body stream already read`));
|
|
158
|
+
}
|
|
159
|
+
function isGlobalReadableStream(value) {
|
|
160
|
+
return !!value &&
|
|
161
|
+
typeof value === "object" &&
|
|
162
|
+
typeof ReadableStream !== "undefined" &&
|
|
163
|
+
ReadableStream &&
|
|
164
|
+
ReadableStream.prototype.isPrototypeOf(value);
|
|
165
|
+
}
|
|
166
|
+
function isOtherReadableStream(value) {
|
|
167
|
+
return (isObjectType("ReadableStream", value) || String(value) === "[object ReadableStream]")
|
|
168
|
+
&& "getReader" in value
|
|
169
|
+
&& typeof value.getReader === "function";
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export { BodyImpl, Payload, initBody };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { setState, isSequence, _Symbol, isPolyfillType, checkArgsLength } from '../utils.js';
|
|
2
|
+
|
|
3
|
+
class HeadersP {
|
|
4
|
+
constructor(init) {
|
|
5
|
+
setState(this, "__Headers__", new HeadersState());
|
|
6
|
+
if (init !== undefined) {
|
|
7
|
+
if (isHeaders(init)) {
|
|
8
|
+
init.forEach((value, name) => { Headers_append(this, name, value, ""); }, this);
|
|
9
|
+
}
|
|
10
|
+
else if (isSequence(init)) {
|
|
11
|
+
let _init = Array.isArray(init) ? init : Array.from(init);
|
|
12
|
+
for (let i = 0; i < _init.length; ++i) {
|
|
13
|
+
let item = _init[i];
|
|
14
|
+
if (isSequence(item)) {
|
|
15
|
+
let pair = Array.isArray(item) ? item : Array.from(item);
|
|
16
|
+
if (pair.length === 2) {
|
|
17
|
+
Headers_append(this, pair[0], pair[1]);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
throw new TypeError("Failed to construct 'Headers': Invalid value");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
throw new TypeError("Failed to construct 'Headers': The provided value cannot be converted to a sequence.");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
if (init && typeof init === "object") {
|
|
30
|
+
Object.getOwnPropertyNames(init).forEach(name => { Headers_append(this, name, init[name]); }, this);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
throw new TypeError("Failed to construct 'Headers': The provided value is not of type '(record<ByteString, ByteString> or sequence<sequence<ByteString>>)'.");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
append(name, value) {
|
|
39
|
+
checkArgsFn(arguments.length, 2, "append");
|
|
40
|
+
Headers_append(this, name, value, "append");
|
|
41
|
+
}
|
|
42
|
+
delete(name) {
|
|
43
|
+
checkArgsFn(arguments.length, 1, "delete");
|
|
44
|
+
delete state(this).dict[normalizeName(name, throwsFn("delete"))];
|
|
45
|
+
}
|
|
46
|
+
get(name) {
|
|
47
|
+
var _a;
|
|
48
|
+
checkArgsFn(arguments.length, 1, "get");
|
|
49
|
+
return (_a = state(this).dict[normalizeName(name, throwsFn("get"))]) !== null && _a !== void 0 ? _a : null;
|
|
50
|
+
}
|
|
51
|
+
getSetCookie() {
|
|
52
|
+
let value = this.get("Set-Cookie");
|
|
53
|
+
return value ? value.split(", ") : [];
|
|
54
|
+
}
|
|
55
|
+
has(name) {
|
|
56
|
+
checkArgsFn(arguments.length, 1, "has");
|
|
57
|
+
return state(this).dict.hasOwnProperty(normalizeName(name, throwsFn("has")));
|
|
58
|
+
}
|
|
59
|
+
set(name, value) {
|
|
60
|
+
checkArgsFn(arguments.length, 2, "set");
|
|
61
|
+
state(this).dict[normalizeName(name, throwsFn("set"))] = normalizeValue(value);
|
|
62
|
+
}
|
|
63
|
+
forEach(callbackfn, thisArg) {
|
|
64
|
+
checkArgsFn(arguments.length, 1, "forEach");
|
|
65
|
+
if (typeof callbackfn !== "function") {
|
|
66
|
+
throw new TypeError("Failed to execute 'forEach' on 'Headers': parameter 1 is not of type 'Function'.");
|
|
67
|
+
}
|
|
68
|
+
let dict = state(this).dict;
|
|
69
|
+
let names = Object.getOwnPropertyNames(dict);
|
|
70
|
+
for (let i = 0; i < names.length; ++i) {
|
|
71
|
+
let name = names[i];
|
|
72
|
+
callbackfn.call(thisArg, dict[name], name, this);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
entries() {
|
|
76
|
+
let array = [];
|
|
77
|
+
this.forEach((value, name) => { array.push([name, value]); });
|
|
78
|
+
return array.values();
|
|
79
|
+
}
|
|
80
|
+
keys() {
|
|
81
|
+
let array = [];
|
|
82
|
+
this.forEach((value, name) => { array.push([name, value]); });
|
|
83
|
+
return array.map(x => x[0]).values();
|
|
84
|
+
}
|
|
85
|
+
values() {
|
|
86
|
+
let array = [];
|
|
87
|
+
this.forEach((value, name) => { array.push([name, value]); });
|
|
88
|
+
return array.map(x => x[1]).values();
|
|
89
|
+
}
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
/** @internal */ [_Symbol.iterator]() {
|
|
92
|
+
return this.entries();
|
|
93
|
+
}
|
|
94
|
+
/** @internal */ toString() { return "[object Headers]"; }
|
|
95
|
+
/** @internal */ get [_Symbol.toStringTag]() { return "Headers"; }
|
|
96
|
+
/** @internal */ get __MPHTTPX__() { return { chain: ["Headers"] }; }
|
|
97
|
+
}
|
|
98
|
+
/** @internal */
|
|
99
|
+
class HeadersState {
|
|
100
|
+
constructor() {
|
|
101
|
+
this.dict = {};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function state(target) {
|
|
105
|
+
return target.__Headers__;
|
|
106
|
+
}
|
|
107
|
+
function Headers_append(headers, name, value, kind = "constructor") {
|
|
108
|
+
let _name = normalizeName(name, kind ? throwsFn(kind) : undefined);
|
|
109
|
+
let _value = normalizeValue(value);
|
|
110
|
+
let dict = state(headers).dict;
|
|
111
|
+
let oldValue = dict[_name];
|
|
112
|
+
dict[_name] = oldValue !== undefined ? `${oldValue}, ${_value}` : _value;
|
|
113
|
+
}
|
|
114
|
+
function throwsFn(kind) {
|
|
115
|
+
return () => {
|
|
116
|
+
throw new TypeError(`Failed to ${(kind && kind !== "constructor") ? ("execute '" + kind + "' on") : "construct"} 'Headers': Invalid name`);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function checkArgsFn(actual, expect, funcName) {
|
|
120
|
+
checkArgsLength(actual, expect, "Headers", funcName);
|
|
121
|
+
}
|
|
122
|
+
function isHeaders(value) {
|
|
123
|
+
return isPolyfillType("Headers", value) || isExternalHeaders(value);
|
|
124
|
+
}
|
|
125
|
+
function isExternalHeaders(value) {
|
|
126
|
+
let expect = "[object Headers]";
|
|
127
|
+
return (Object.prototype.toString.call(value) === expect || String(value) === expect)
|
|
128
|
+
&& "forEach" in value
|
|
129
|
+
&& typeof value.forEach === "function";
|
|
130
|
+
}
|
|
131
|
+
function normalizeName(name, throwError) {
|
|
132
|
+
if (typeof name !== "string") {
|
|
133
|
+
name = "" + name;
|
|
134
|
+
}
|
|
135
|
+
if (throwError && (/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(name) || name === "")) {
|
|
136
|
+
throwError();
|
|
137
|
+
}
|
|
138
|
+
return name.toLowerCase();
|
|
139
|
+
}
|
|
140
|
+
function normalizeValue(value) {
|
|
141
|
+
return typeof value === "string" ? value : ("" + value);
|
|
142
|
+
}
|
|
143
|
+
function parseHeaders(rawHeaders) {
|
|
144
|
+
let headers = new HeadersE();
|
|
145
|
+
let preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, " ");
|
|
146
|
+
preProcessedHeaders
|
|
147
|
+
.split("\r")
|
|
148
|
+
.map(header => header.indexOf("\n") === 0 ? header.substring(1, header.length) : header)
|
|
149
|
+
.forEach(line => {
|
|
150
|
+
let parts = line.split(":");
|
|
151
|
+
let name = parts.shift().trim();
|
|
152
|
+
if (name) {
|
|
153
|
+
let value = parts.join(":").trim();
|
|
154
|
+
try {
|
|
155
|
+
headers.append(name, value);
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
console.warn(`SyntaxError: Response.headers: '${name}' is not a valid HTTP header field name.`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return headers;
|
|
163
|
+
}
|
|
164
|
+
function createHeaders(headers) {
|
|
165
|
+
if (isPolyfillType("Headers", headers)) {
|
|
166
|
+
if (!Object.is(HeadersE, HeadersP)) {
|
|
167
|
+
let _headers = new HeadersE();
|
|
168
|
+
headers.forEach((v, k) => { _headers.append(k, v); });
|
|
169
|
+
return _headers;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return new HeadersE(headers);
|
|
173
|
+
}
|
|
174
|
+
const HeadersE = (typeof Headers !== "undefined" && Headers) || HeadersP;
|
|
175
|
+
|
|
176
|
+
export { HeadersE as Headers, HeadersP, createHeaders, isHeaders, normalizeName, normalizeValue, parseHeaders };
|