-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
159 lines (156 loc) · 4.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* global module */
/* global EventTarget, AbortController, DOMException */
const sHeaders = Symbol("headers");
const sRespHeaders = Symbol("response headers");
const sAbortController = Symbol("AbortController");
const sMethod = Symbol("method");
const sURL = Symbol("URL");
const sMIME = Symbol("MIME");
const sDispatch = Symbol("dispatch");
const sErrored = Symbol("errored");
const sTimeout = Symbol("timeout");
const sTimedOut = Symbol("timedOut");
const sIsResponseText = Symbol("isResponseText");
const XMLHttpRequestShim = class XMLHttpRequest extends EventTarget {
constructor() {
super();
this.readyState = this.constructor.UNSENT;
this.response = null;
this.responseType = "";
this.responseURL = "";
this.status = 0;
this.statusText = "";
this.timeout = 0;
this.withCredentials = false;
this[sHeaders] = Object.create(null);
this[sHeaders].accept = "*/*";
this[sRespHeaders] = Object.create(null);
this[sAbortController] = new AbortController();
this[sMethod] = "";
this[sURL] = "";
this[sMIME] = "";
this[sErrored] = false;
this[sTimeout] = 0;
this[sTimedOut] = false;
this[sIsResponseText] = true;
}
static get UNSENT() {
return 0;
}
static get OPENED() {
return 1;
}
static get HEADERS_RECEIVED() {
return 2;
}
static get LOADING() {
return 3;
}
static get DONE() {
return 4;
}
get responseText() {
if (this[sErrored]) return null;
if (this.readyState < this.constructor.HEADERS_RECEIVED) return "";
if (this[sIsResponseText]) return this.response;
throw new DOMException("Response type not set to text", "InvalidStateError");
}
get responseXML() {
throw new Error("XML not supported");
}
[sDispatch](evt) {
const attr = `on${evt.type}`;
if (typeof this[attr] === "function") {
this.addEventListener(evt.type, this[attr].bind(this), {
once: true
});
}
this.dispatchEvent(evt);
}
abort() {
this[sAbortController].abort();
this.status = 0;
this.readyState = this.constructor.UNSENT;
}
open(method, url) {
this.status = 0;
this[sMethod] = method;
this[sURL] = url;
this.readyState = this.constructor.OPENED;
}
setRequestHeader(header, value) {
header = String(header).toLowerCase();
if (typeof this[sHeaders][header] === "undefined") {
this[sHeaders][header] = String(value);
} else {
this[sHeaders][header] += `, ${value}`;
}
}
overrideMimeType(mimeType) {
this[sMIME] = String(mimeType);
}
getAllResponseHeaders() {
if (this[sErrored] || this.readyState < this.constructor.HEADERS_RECEIVED) return "";
return Object.entries(this[sRespHeaders]).map(([header, value]) => `${header}: ${value}`).join("\r\n");
}
getResponseHeader(headerName) {
const value = this[sRespHeaders][String(headerName).toLowerCase()];
return typeof value === "string" ? value : null;
}
send(body = null) {
if (this.timeout > 0) {
this[sTimeout] = setTimeout(() => {
this[sTimedOut] = true;
this[sAbortController].abort();
}, this.timeout);
}
const responseType = this.responseType || "text";
this[sIsResponseText] = responseType === "text";
fetch(this[sURL], {
method: this[sMethod] || "GET",
signal: this[sAbortController].signal,
headers: this[sHeaders],
credentials: this.withCredentials ? "include" : "same-origin",
body
}).finally(() => {
this.readyState = this.constructor.DONE;
clearTimeout(this[sTimeout]);
this[sDispatch](new CustomEvent("loadstart"));
}).then(async resp => {
this.responseURL = resp.url;
this.status = resp.status;
this.statusText = resp.statusText;
const finalMIME = this[sMIME] || this[sRespHeaders]["content-type"] || "text/plain";
Object.assign(this[sRespHeaders], resp.headers);
switch (responseType) {
case "text":
this.response = await resp.text();
break;
case "blob":
this.response = new Blob([await resp.arrayBuffer()], { type: finalMIME });
break;
case "arraybuffer":
this.response = await resp.arrayBuffer();
break;
case "json":
this.response = await resp.json();
break;
}
this[sDispatch](new CustomEvent("load"));
}, err => {
let eventName = "abort";
if (err.name !== "AbortError") {
this[sErrored] = true;
eventName = "error";
} else if (this[sTimedOut]) {
eventName = "timeout";
}
this[sDispatch](new CustomEvent(eventName));
}).finally(() => this[sDispatch](new CustomEvent("loadend")));
}
}
if (typeof module === "object" && module.exports) {
module.exports = XMLHttpRequestShim;
} else {
(globalThis || self).XMLHttpRequestShim = XMLHttpRequestShim;
}