forked from node-fetch/node-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody.js
570 lines (506 loc) · 13 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// @ts-check
/**
* Body.js
*
* Body interface provides common methods for Request and Response
*/
import Stream from 'stream';
import {types} from 'util';
import {Blob} from '@web-std/blob';
import WebStreams from 'web-streams-polyfill';
import {FetchError} from './errors/fetch-error.js';
import {FetchBaseError} from './errors/base.js';
import {formDataIterator, getBoundary, getFormDataLength} from './utils/form-data.js';
import {isBlob, isURLSearchParameters, isFormData, isMultipartFormDataStream, isReadableStream} from './utils/is.js';
import * as utf8 from './utils/utf8.js';
const {readableHighWaterMark} = new Stream.Readable();
const {ReadableStream} = WebStreams;
const INTERNALS = Symbol('Body internals');
/**
* Body mixin
*
* Ref: https://fetch.spec.whatwg.org/#body
*
* @param {BodyInit} body Readable stream
* @param Object opts Response options
* @return Void
*/
export default class Body {
/**
* @param {BodyInit|Stream} body
* @param {{size?:number}} options
*/
constructor(body, {
size = 0
} = {}) {
const state = {
/** @type {null|ReadableStream<Uint8Array>} */
body: null,
/** @type {string|null} */
type: null,
/** @type {number|null} */
size: null,
/** @type {null|string} */
boundary: null,
disturbed: false,
/** @type {null|Error} */
error: null
};
this[INTERNALS] = state;
if (body === null) {
// Body is undefined or null
state.body = null;
state.size = 0;
} else if (isURLSearchParameters(body)) {
// Body is a URLSearchParams
const bytes = utf8.encode(body.toString());
state.body = fromBytes(bytes);
state.size = bytes.byteLength;
state.type = 'application/x-www-form-urlencoded;charset=UTF-8';
} else if (isBlob(body)) {
// Body is blob
state.size = body.size;
state.type = body.type || null;
state.body = body.stream();
} else if (body instanceof Uint8Array) {
// Body is Buffer
state.body = fromBytes(body);
state.size = body.byteLength;
} else if (types.isAnyArrayBuffer(body)) {
// Body is ArrayBuffer
const bytes = new Uint8Array(body);
state.body = fromBytes(bytes);
state.size = bytes.byteLength;
} else if (ArrayBuffer.isView(body)) {
// Body is ArrayBufferView
const bytes = new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
state.body = fromBytes(bytes);
state.size = bytes.byteLength;
} else if (isReadableStream(body)) {
// Body is stream
state.body = body;
} else if (isFormData(body)) {
// Body is an instance of formdata-node
const boundary = `NodeFetchFormDataBoundary${getBoundary()}`;
state.type = `multipart/form-data; boundary=${boundary}`;
state.size = getFormDataLength(body, boundary);
state.body = fromAsyncIterable(formDataIterator(body, boundary));
} else if (isMultipartFormDataStream(body)) {
state.type = `multipart/form-data; boundary=${body.getBoundary()}`;
state.size = body.hasKnownLength() ? body.getLengthSync() : null;
state.body = fromStream(body);
} else if (body instanceof Stream) {
state.body = fromStream(body);
} else {
// None of the above
// coerce to string then buffer
const bytes = utf8.encode(String(body));
state.type = 'text/plain;charset=UTF-8';
state.size = bytes.byteLength;
state.body = fromBytes(bytes);
}
this.size = size;
// if (body instanceof Stream) {
// body.on('error', err => {
// const error = err instanceof FetchBaseError ?
// err :
// new FetchError(`Invalid response body while trying to fetch ${this.url}: ${err.message}`, 'system', err);
// this[INTERNALS].error = error;
// });
// }
}
/** @type {Headers|undefined} */
/* c8 ignore next 3 */
get headers() {
return null;
}
get body() {
return this[INTERNALS].body;
}
get bodyUsed() {
return this[INTERNALS].disturbed;
}
/**
* Decode response as ArrayBuffer
*
* @return {Promise<ArrayBuffer>}
*/
async arrayBuffer() {
const {buffer, byteOffset, byteLength} = await consumeBody(this);
return buffer.slice(byteOffset, byteOffset + byteLength);
}
/**
* Return raw response as Blob
*
* @return Promise
*/
async blob() {
const ct = (this.headers && this.headers.get('content-type')) || (this[INTERNALS].body && this[INTERNALS].type) || '';
const buf = await consumeBody(this);
return new Blob([buf], {
type: ct
});
}
/**
* Decode response as json
*
* @return Promise
*/
async json() {
return JSON.parse(await this.text());
}
/**
* Decode response as text
*
* @return Promise
*/
async text() {
const buffer = await consumeBody(this);
return utf8.decode(buffer);
}
}
// In browsers, all properties are enumerable.
Object.defineProperties(Body.prototype, {
body: {enumerable: true},
bodyUsed: {enumerable: true},
arrayBuffer: {enumerable: true},
blob: {enumerable: true},
json: {enumerable: true},
text: {enumerable: true}
});
/**
* Consume and convert an entire Body to a Buffer.
*
* Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
*
* @param {Body & {url?:string}} data
* @return {Promise<Uint8Array>}
*/
async function consumeBody(data) {
const state = data[INTERNALS];
if (state.disturbed) {
throw new TypeError(`body used already for: ${data.url}`);
}
state.disturbed = true;
if (state.error) {
throw state.error;
}
const {body} = state;
// Body is null
if (body === null) {
return new Uint8Array(0);
}
// Body is stream
// get ready to actually consume the body
const [buffer, chunks, limit] = data.size > 0 ?
[new Uint8Array(data.size), null, data.size] :
[null, [], Infinity];
let offset = 0;
const source = streamIterator(body);
try {
for await (const chunk of source) {
const bytes = chunk instanceof Uint8Array ?
chunk :
Buffer.from(chunk);
if (offset + bytes.byteLength > limit) {
const error = new FetchError(`content size at ${data.url} over limit: ${limit}`, 'max-size');
source.throw(error);
throw error;
} else if (buffer) {
buffer.set(bytes, offset);
} else {
chunks.push(bytes);
}
offset += bytes.byteLength;
}
if (buffer) {
if (offset < buffer.byteLength) {
throw new FetchError(`Premature close of server response while trying to fetch ${data.url}`);
} else {
return buffer;
}
} else {
return writeBytes(new Uint8Array(offset), chunks);
}
} catch (error) {
if (error instanceof FetchBaseError) {
throw error;
} else if (error && error.name === 'AbortError') {
throw error;
} else {
// Other errors, such as incorrect content-encoding
throw new FetchError(`Invalid response body while trying to fetch ${data.url}: ${error.message}`, 'system', error);
}
}
}
/**
* Clone body given Res/Req instance
*
* @param {Body} instance Response or Request instance
* @return {ReadableStream<Uint8Array>}
*/
export const clone = instance => {
const {body} = instance;
// Don't allow cloning a used body
if (instance.bodyUsed) {
throw new Error('cannot clone body after it is used');
}
const [left, right] = body.tee();
instance[INTERNALS].body = left;
return right;
};
/**
* Performs the operation "extract a `Content-Type` value from |object|" as
* specified in the specification:
* https://fetch.spec.whatwg.org/#concept-bodyinit-extract
*
* This function assumes that instance.body is present.
*
* @param {Body} source Any options.body input
* @returns {string | null}
*/
export const extractContentType = source => source[INTERNALS].type;
/**
* The Fetch Standard treats this as if "total bytes" is a property on the body.
* For us, we have to explicitly get it with a function.
*
* ref: https://fetch.spec.whatwg.org/#concept-body-total-bytes
*
* @param {Body} source - Body object from the Body instance.
* @returns {number | null}
*/
export const getTotalBytes = source => source[INTERNALS].size;
/**
* Write a Body to a Node.js WritableStream (e.g. http.Request) object.
*
* @param {Stream.Writable} dest - The stream to write to.
* @param {Body} source - Body object from the Body instance.
* @returns {void}
*/
export const writeToStream = (dest, {body}) => {
if (body === null) {
// Body is null
dest.end();
} else {
Stream.Readable.from(streamIterator(body)).pipe(dest);
}
};
/**
* @template T
* @implements {AsyncGenerator<T, void, void>}
*/
class StreamIterableIterator {
/**
* @param {ReadableStream<T>} stream
*/
constructor(stream) {
this.stream = stream;
this.reader = null;
this.state = null;
}
/**
* @returns {AsyncGenerator<T, void, void>}
*/
[Symbol.asyncIterator]() {
return this;
}
getReader() {
if (this.reader) {
return this.reader;
}
const reader = this.stream.getReader();
this.reader = reader;
return reader;
}
/**
* @returns {Promise<IteratorResult<T, void>>}
*/
next() {
return /** @type {Promise<IteratorResult<T, void>>} */ (this.getReader().read());
}
async return() {
if (this.reader) {
await this.reader.cancel();
}
return {done: true, value: undefined};
}
async throw(error) {
await this.getReader().cancel(error);
return {done: true, value: undefined};
}
}
/**
* @template T
* @param {ReadableStream<T>} stream
*/
export const streamIterator = stream => new StreamIterableIterator(stream);
/**
* @param {Uint8Array} buffer
* @param {Uint8Array[]} chunks
*/
const writeBytes = (buffer, chunks) => {
let offset = 0;
for (const chunk of chunks) {
buffer.set(chunk, offset);
offset += chunk.byteLength;
}
return buffer;
};
/**
* @param {Uint8Array} bytes
* @returns {ReadableStream<Uint8Array>}
*/
// @ts-ignore
const fromBytes = bytes => new ReadableStream({
start(controller) {
controller.enqueue(bytes);
controller.close();
}
});
/**
* @param {AsyncIterable<Uint8Array>} content
* @returns {ReadableStream<Uint8Array>}
*/
export const fromAsyncIterable = content =>
// @ts-ignore
new ReadableStream(new AsyncIterablePump(content));
/**
* @implements {UnderlyingSource<Uint8Array>}
*/
class AsyncIterablePump {
/**
* @param {AsyncIterable<Uint8Array>} source
*/
constructor(source) {
this.source = source[Symbol.asyncIterator]();
}
/**
* @param {ReadableStreamController<Uint8Array>} controller
*/
async pull(controller) {
try {
while (controller.desiredSize > 0) {
// eslint-disable-next-line no-await-in-loop
const next = await this.source.next();
if (next.done) {
controller.close();
break;
} else {
controller.enqueue(next.value);
}
}
} catch (error) {
controller.error(error);
}
}
cancel(reason) {
if (reason) {
if (typeof this.source.throw === 'function') {
this.source.throw(reason);
} else if (typeof this.source.return === 'function') {
this.source.return();
}
} else if (typeof this.source.return === 'function') {
this.source.return();
}
}
}
/**
* @param {Stream & {readableHighWaterMark?:number}} source
* @returns {ReadableStream<Uint8Array>}
*/
export const fromStream = source => {
const pump = new StreamPump(source);
const stream =
/** @type {ReadableStream<Uint8Array>} */(new ReadableStream(pump, pump));
return stream;
};
/**
* @implements {WebStreams.UnderlyingSource<Uint8Array>}
*/
class StreamPump {
/**
* @param {Stream & {
* readableHighWaterMark?: number
* readable?:boolean,
* resume?: () => void,
* pause?: () => void
* destroy?: (error?:Error) => void
* }} stream
*/
constructor(stream) {
this.highWaterMark = stream.readableHighWaterMark || readableHighWaterMark;
this.accumalatedSize = 0;
this.stream = stream;
this.enqueue = this.enqueue.bind(this);
this.error = this.error.bind(this);
this.close = this.close.bind(this);
}
size(chunk) {
return chunk.byteLength;
}
/**
* @param {ReadableStreamController<Uint8Array>} controller
*/
start(controller) {
this.controller = controller;
this.stream.on('data', this.enqueue);
this.stream.once('error', this.error);
this.stream.once('end', this.close);
this.stream.once('close', this.close);
}
pull() {
this.resume();
}
cancel(reason) {
if (this.stream.destroy) {
this.stream.destroy(reason);
}
this.stream.off('data', this.enqueue);
this.stream.off('error', this.error);
this.stream.off('end', this.close);
this.stream.off('close', this.close);
}
/**
* @param {Uint8Array|string} chunk
*/
enqueue(chunk) {
if (this.controller) {
try {
const bytes = chunk instanceof Uint8Array ?
chunk :
Buffer.from(chunk);
const available = this.controller.desiredSize - bytes.byteLength;
this.controller.enqueue(bytes);
if (available <= 0) {
this.pause();
}
} catch {
this.controller.error(new Error('Could not create Buffer, chunk must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object'));
this.cancel();
}
}
}
pause() {
if (this.stream.pause) {
this.stream.pause();
}
}
resume() {
if (this.stream.readable && this.stream.resume) {
this.stream.resume();
}
}
close() {
if (this.controller) {
this.controller.close();
delete this.controller;
}
}
error(error) {
if (this.controller) {
this.controller.error(error);
delete this.controller;
}
}
}