forked from JakeChampion/fetch
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBody.js
233 lines (183 loc) · 6.39 KB
/
Body.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { createBlobReader, drainStream, readArrayBufferAsText } from "./utils";
class Body {
constructor(body) {
this.bodyUsed = false;
this._bodyInit = body;
if (!body) {
this._bodyText = "";
return this;
}
if (body instanceof Blob) {
this._bodyBlob = body;
this._mimeType = body.type;
return this;
}
if (body instanceof FormData) {
this._bodyFormData = body;
this._mimeType = "multipart/form-data";
return this;
}
if (body instanceof URLSearchParams) {
// URLSearchParams is not handled natively so we reassign bodyInit for fetch to send it as text
this._bodyText = this._bodyInit = body.toString();
this._mimeType = "application/x-www-form-urlencoded;charset=UTF-8";
return this;
}
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
this._bodyArrayBuffer = body.slice?.(0) ?? body.buffer;
this._mimeType = "application/octet-stream";
return this;
}
if (body instanceof ReadableStream) {
this._bodyReadableStream = body;
this._mimeType = "application/octet-stream";
return this;
}
this._bodyText = body.toString();
this._mimeType = "text/plain;charset=UTF-8";
}
__consumed() {
if (this.bodyUsed) {
return Promise.reject(new TypeError("Already read"));
}
this.bodyUsed = true;
}
async blob() {
const alreadyConsumed = this.__consumed();
if (alreadyConsumed) {
return alreadyConsumed;
}
if (this._bodyBlob) {
return this._bodyBlob;
}
// Currently not supported by React Native. It will throw.
// Blobs cannot be constructed from ArrayBuffers or ArrayBufferViews.
if (this._bodyReadableStream) {
const typedArray = await drainStream(this._bodyReadableStream);
return new Blob([typedArray]);
}
// Currently not supported by React Native. It will throw.
// Blobs cannot be constructed from ArrayBuffers or ArrayBufferViews.
if (this._bodyArrayBuffer) {
if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
return new Blob([this._bodyArrayBuffer.buffer]);
}
return new Blob([this._bodyArrayBuffer]);
}
if (this._bodyFormData) {
throw new Error("Could not read FormData body as blob");
}
return new Blob([this._bodyText]);
}
async arrayBuffer() {
if (this._bodyText) {
const blob = await this.blob();
return createBlobReader(blob).readAsArrayBuffer();
}
const alreadyConsumed = this.__consumed();
if (alreadyConsumed) {
return alreadyConsumed;
}
if (this._bodyReadableStream) {
const typedArray = await drainStream(this._bodyReadableStream);
return typedArray.buffer;
}
if (this._bodyArrayBuffer) {
if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
const {
buffer,
byteOffset,
byteLength,
} = this._bodyArrayBuffer;
return Promise.resolve(
buffer.slice(byteOffset, byteOffset + byteLength)
);
}
return Promise.resolve(this._bodyArrayBuffer);
}
}
async text() {
const alreadyConsumed = this.__consumed();
if (alreadyConsumed) {
return alreadyConsumed;
}
if (this._bodyReadableStream) {
const typedArray = await drainStream(this._bodyReadableStream);
return readArrayBufferAsText(typedArray);
}
if (this._bodyBlob) {
return createBlobReader(this._bodyBlob).readAsText();
}
if (this._bodyArrayBuffer) {
return readArrayBufferAsText(this._bodyArrayBuffer);
}
if (this._bodyFormData) {
throw new Error("Could not read FormData body as text");
}
return this._bodyText;
}
async json() {
const text = await this.text();
return JSON.parse(text);
}
async formData() {
const text = await this.text();
const formData = new FormData();
text.trim()
.split("&")
.forEach((pairs) => {
if (!pairs) {
return;
}
const split = pairs.split("=");
const name = split.shift().replace(/\+/g, " ");
const value = split.join("=").replace(/\+/g, " ");
formData.append(
decodeURIComponent(name),
decodeURIComponent(value)
);
});
return formData;
}
get body() {
if (this._bodyReadableStream) {
return this._bodyReadableStream;
}
if (this._bodyArrayBuffer) {
const typedArray = new Uint8Array(this._bodyArrayBuffer);
return new ReadableStream({
start(controller) {
typedArray.forEach((chunk) => {
controller.enqueue(chunk);
});
controller.close();
},
});
}
if (this._bodyBlob) {
return new ReadableStream({
start: async (controller) => {
const arrayBuffer = await createBlobReader(
this._bodyBlob
).readAsArrayBuffer();
const typedArray = new Uint8Array(arrayBuffer);
typedArray.forEach((chunk) => {
controller.enqueue(chunk);
});
controller.close();
},
});
}
const text = this._bodyFormData?.toString() ?? this._bodyText;
return new ReadableStream({
start: async (controller) => {
const typedArray = new Uint8Array(text);
typedArray.forEach((chunk) => {
controller.enqueue(chunk);
});
controller.close();
},
});
}
}
export default Body;