forked from dollarshaveclub/node-fetch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
body.js
614 lines (541 loc) · 14.9 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/**
* body.js
*
* Body interface provides common methods for Request and Response
*/
import { ReadableStream, TransformStream } from "stream/web";
import { Blob } from "buffer";
import FetchError from './fetch-error.js';
import Stream, { PassThrough } from "stream";
import Busboy from "busboy";
import FormData from "formdata-node";
export const INTERNALS = Symbol('Body internals');
export function getTypeOfBody(body) {
if (body == null) {
return "null";
} else if (typeof body === 'string') {
return "String";
} else if (isURLSearchParams(body)) {
return "URLSearchParams";
} else if (body instanceof Blob) {
return "Blob";
} else if (Buffer.isBuffer(body)) {
return "Buffer";
} else if (Object.prototype.toString.call(body) === '[object ArrayBuffer]') {
return "ArrayBuffer"
} else if (ArrayBuffer.isView(body)) {
return "ArrayBufferView";
} else if (body.toString() ==='[object FormData]' || Object.prototype.toString.call(body) === '[object FormData]') {
return "FormData";
} else if (body instanceof Stream) {
return "Stream"
} else if (
body instanceof ReadableStream ||
// Allow detecting a "ReadableStream" from a different install/realm
(body.constructor.name === "ReadableStream" && typeof body.getReader == "function")
) {
return "ReadableStream";
} else {
return "other";
}
}
function readableNodeToWeb(nodeStream, instance) {
return new ReadableStream({
start(controller) {
nodeStream.pause();
nodeStream.on('data', chunk => {
// TODO: Should we only do Buffer.from() if chunk is a UInt8Array?
// Potentially it makes more sense for down-stream consumers of fetch to cast to Buffer, instead?
// if(isUInt8Array(chunk)) {
controller.enqueue(new Uint8Array(Buffer.from(chunk)));
// HELP WANTED: The node-web-streams package pauses the nodeStream here, however,
// if we do that, then it gets permanently paused. Why?
// nodeStream.pause();
});
nodeStream.on('end', () => {
controller.close();
const pending = controller.byobRequest;
if (pending) {
pending.respond(0);
}
});
nodeStream.on('error', (err) => {
controller.error(new FetchError(`Invalid response body while trying to fetch ${instance.url}: ${err.message}`, 'system', err))
});
},
pull(controller) {
nodeStream.resume();
},
cancel(reason) {
nodeStream.pause();
},
type: "bytes"
});
}
export function createReadableStream(instance) {
const body = getInstanceBody(instance);
const bodyType = getTypeOfBody(body);
if (bodyType === "null") {
return null;
}
if (bodyType === 'ReadableStream') {
return body.pipeThrough(new TransformStream({
transform(chunk, controller) {
// TODO: Should we only do Buffer.from() if chunk is a UInt8Array?
// Potentially it makes more sense for down-stream consumers of fetch to cast to Buffer, instead?
// if(isUInt8Array(chunk)) {
const array = new Uint8Array(Buffer.from(chunk));
if(array.length > 0) controller.enqueue(array);
}
}));
}
if (bodyType === "Stream") {
body.pause();
return readableNodeToWeb(body, instance);
}
const readable = new ReadableStream({
async start(controller) {
let array = undefined;
switch (bodyType) {
case "String":
// body is a string:
array = new Uint8Array(Buffer.from(body));
break;
case "URLSearchParams":
// body is a URLSearchParams
array = new Uint8Array(Buffer.from(body.toString()));
break;
case "Blob":
// body is blob
array = new Uint8Array(await body.arrayBuffer());
break;
case "Buffer":
// body is Buffer
array = new Uint8Array(Buffer.from(body));
break;
case "ArrayBuffer":
// body is ArrayBuffer
array = new Uint8Array(Buffer.from(body));
break;
case "ArrayBufferView":
// body is ArrayBufferView
array = new Uint8Array(Buffer.from(body.buffer));
break;
case "FormData":
array = new Uint8Array(Buffer.from(body.toString()));
break;
case "other":
array = new Uint8Array(Buffer.from(String(body)));
break;
default:
throw new Error("createReadableStream received an instance body that getTypeOfBody could not understand");
}
if(array && array.length > 0) controller.enqueue(array);
controller.close();
},
type: "bytes"
});
return readable;
}
/**
* Body mixin
*
* Ref: https://fetch.spec.whatwg.org/#body
*
* @param Stream body Readable stream
* @param Object opts Response options
* @return Void
*/
export default function Body(body, {
size = 0,
timeout = 0,
name = "Body"
} = {}) {
this.size = size;
this.timeout = timeout;
this[INTERNALS] = {
body: body,
readableStream: null,
disturbed: false,
name
};
const bodyType = getTypeOfBody(body);
if (bodyType !== 'ReadableStream') {
this[INTERNALS].readableStream = createReadableStream(this);
} else {
this[INTERNALS].readableStream = body;
}
}
Body.prototype = {
// NOTE: Firefox and Chrome return `undefined` if initial body is undefined, when looking at Request.body, they always return undefined.
get body() {
return getInstanceReadableStream(this);
},
get bodyUsed() {
return this[INTERNALS].disturbed;
},
/**
* Decode response as ArrayBuffer
*
* @return Promise
*/
arrayBuffer() {
return consumeBody.call(this).then(buf => {
var ab = new ArrayBuffer(buf.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buf.length; ++i) {
view[i] = buf[i];
}
return ab;
});
},
/**
* Return raw response as Blob
*
* @return Promise
*/
blob() {
let ct = this.headers && this.headers.get('content-type') || '';
return consumeBody.call(this).then(buf => {
return new Blob([buf], {type: ct.toLowerCase()});
});
},
/**
* Decode response as json
*
* @return Promise
*/
json() {
return consumeBody.call(this).then((buffer) => {
try {
return JSON.parse(buffer.toString());
} catch (err) {
return Promise.reject(new FetchError(`invalid json response body at ${this.url} reason: ${err.message}`, 'invalid-json'));
}
})
},
/**
* Decode response as text
*
* @return Promise
*/
text() {
return consumeBody.call(this).then(buffer => buffer.toString());
},
/**
* Decode response as buffer (non-spec api)
*
* @return Promise
*/
buffer() {
return consumeBody.call(this);
},
formData() {
return consumeBody.call(this).then(buffer => {
return new Promise((resolve, reject) => {
var formdata = new FormData();
var busboy = new Busboy({headers: {
'content-type': this.headers.get('content-type'),
}});
busboy.on('field', (fieldname, val) => formdata.append(fieldname, val));
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
let val = "";
file.on('data', (data) => val += data);
file.on('end', () => formdata.append(fieldname, val, filename));
});
busboy.on('finish', () => resolve(formdata));
writeToStream(busboy, this);
});
});
},
};
// 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 }
});
Body.mixIn = function (proto) {
for (const name of Object.getOwnPropertyNames(Body.prototype)) {
// istanbul ignore else: future proof
if (!(name in proto)) {
const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
Object.defineProperty(proto, name, desc);
}
}
};
/**
* Consume and convert an entire Body to a Buffer.
*
* Ref: https://fetch.spec.whatwg.org/#concept-body-consume-body
*
* @return Promise
*/
function consumeBody() {
const instance = this;
if (instance[INTERNALS].disturbed) {
return Promise.reject(new TypeError(`body used already for: ${instance.url}`));
}
instance[INTERNALS].disturbed = true;
let resTimeout;
const promise = new Promise((resolve, reject) => {
const readable = getInstanceReadableStream(instance);
if (readable == null) {
return resolve(Buffer.alloc(0));
}
const reader = readable.getReader();
let timedOut = false;
// allow timeout on slow response body
if (instance.timeout) {
resTimeout = setTimeout(() => {
timedOut = true;
reject(new FetchError(`Response timeout while trying to fetch ${instance.url} (over ${instance.timeout}ms)`, 'body-timeout'));
}, instance.timeout);
}
let buffers = [];
let totalBytes = 0;
function push() {
reader.read().then(function(read) {
let bufferedData;
let chunk;
if (timedOut) {
return;
}
if (read.done) {
try {
bufferedData = Buffer.concat(buffers, totalBytes)
} catch(err) {
// handle streams that have accumulated too much data (issue #414)
reject(new FetchError(`Could not create Buffer from response body for ${instance.url}: ${err.message}`, 'system', err));
return;
}
resolve(bufferedData);
return;
}
chunk = Buffer.from(read.value);
if (instance.size && totalBytes + chunk.length > instance.size) {
reject(new FetchError(`content size at ${instance.url} over limit: ${instance.size}`, 'max-size'));
return;
}
buffers.push(chunk);
totalBytes += chunk.length;
push();
}, function (err) {
reject(err);
});
}
push();
});
promise.then(
() => resTimeout && clearTimeout(resTimeout),
() => resTimeout && clearTimeout(resTimeout)
);
return promise;
}
/**
* Detect a URLSearchParams object
* ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143
*
* @param Object obj Object to detect by type or brand
* @return String
*/
function isURLSearchParams(obj) {
// Duck-typing as a necessary condition.
if (typeof obj !== 'object' ||
typeof obj.append !== 'function' ||
typeof obj.delete !== 'function' ||
typeof obj.get !== 'function' ||
typeof obj.getAll !== 'function' ||
typeof obj.has !== 'function' ||
typeof obj.set !== 'function') {
return false;
}
// Brand-checking and more duck-typing as optional condition.
return obj.constructor.name === 'URLSearchParams' ||
Object.prototype.toString.call(obj) === '[object URLSearchParams]' ||
typeof obj.sort === 'function';
}
/**
* Clone body given Res/Req instance
*
* @param Mixed instance Response or Request instance
* @return Mixed
*/
export function cloneBody(instance) {
const body = getInstanceBody(instance);
const bodyType = getTypeOfBody(body);
// don't allow cloning a used body
if (instance.bodyUsed) {
throw new Error('cannot clone body after it is used');
}
// check that body is a stream and not form-data object
// note: we can't clone the form-data object without having it as a dependency
if (bodyType === "Stream") {
// tee instance body
let p1 = new PassThrough();
let p2 = new PassThrough();
// set instance body to teed body and return the other teed body
instance[INTERNALS].body = p1;
instance[INTERNALS].readableStream = createReadableStream(instance);
body.pipe(p1);
body.pipe(p2);
// body.resume();
return p2;
} else if(bodyType === "ReadableStream") {
let [p1, p2] = body.tee();
// set instance body to teed body and return the other teed body
instance[INTERNALS].body = p1;
instance[INTERNALS].readableStream = p1;
return p2;
}
// Note the early returns
return body;
}
/**
* 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 Mixed instance Response or Request instance
*/
export function extractContentType(instance) {
const body = getInstanceBody(instance);
const bodyType = getTypeOfBody(body);
switch(bodyType) {
case "String":
case "other":
return 'text/plain;charset=UTF-8';
case "URLSearchParams":
return 'application/x-www-form-urlencoded;charset=UTF-8';
case "Blob":
return body.type || null;
case "FormData":
return `multipart/form-data;boundary=${body.boundary}`;
default:
return null;
}
}
/**
* 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 instance Instance of Body
* @return Number? Number of bytes, or null if not possible
*/
export function getTotalBytes(instance) {
const body = getInstanceBody(instance);
const bodyType = getTypeOfBody(body);
switch (bodyType) {
case "null":
return 0;
case "String":
return Buffer.byteLength(body);
case "URLSearchParams":
case "other":
return Buffer.byteLength(String(body));
case "Blob":
return body.size;
case "Buffer":
return body.length;
case "ArrayBuffer":
case "ArrayBufferView":
return body.byteLength;
case "FormData":
if (
(body._lengthRetrievers && body._lengthRetrievers.length == 0) || // 1.x
(body.hasKnownLength && body.hasKnownLength())
) { // 2.x
return body.getLengthSync();
}
default:
return null;
}
}
/**
* Write a Body to a Node.js (e.g. http.Request) object.
*
* @param Body instance Instance of Body
* @return Void
*/
export function writeToStream(dest, instance) {
const body = getInstanceBody(instance);
const bodyType = getTypeOfBody(body);
switch(bodyType) {
case "null":
dest.end();
break;
case "Stream":
body.pipe(dest);
break;
case "ReadableStream":
const [out1, out2] = body.tee();
const reader = out2.getReader();
function push() {
reader.read().then(function(read) {
if (read.done) {
dest.end();
return;
}
// TODO: Should we only do Buffer.from() if chunk is a UInt8Array?
// if(isUInt8Array(chunk)) {
dest.write(Buffer.from(read.value));
push();
});
}
instance[INTERNALS].body = out1;
push();
break;
case "String":
dest.write(body);
dest.end();
break;
// case "URLSearchParams":
// dest.write(body.toString());
// dest.end();
// break;
case "Blob":
body.arrayBuffer().then(buf => {
dest.write(Buffer.from(buf));
dest.end();
});
break;
case "Buffer":
dest.write(body);
dest.end();
break;
case "ArrayBuffer":
dest.write(Buffer.from(body));
dest.end();
break;
case "ArrayBufferView":
dest.write(Buffer.from(body.buffer, body.byteOffset, body.byteLength));
dest.end();
break;
case "FormData":
body.stream.pipe(dest);
break;
default:
dest.write(String(body));
dest.end();
break;
}
}
export function getInstanceName(instance) {
return instance[INTERNALS].name;
}
export function getInstanceBody(instance) {
return instance[INTERNALS].body;
}
export function setInstanceBody(instance, newBody) {
return instance[INTERNALS].body = newBody;
}
export function getInstanceReadableStream(instance) {
return instance[INTERNALS].readableStream;
}