@whatwg-node/node-fetch 0.0.1-alpha-20221228082016-9ef2266 → 0.0.1-alpha-20221228110205-1d6dcac
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/Body.d.ts +16 -2
- package/ReadableStream.d.ts +2 -3
- package/Request.d.ts +2 -2
- package/Response.d.ts +2 -2
- package/index.js +117 -30
- package/index.mjs +115 -30
- package/package.json +2 -1
package/Body.d.ts
CHANGED
@@ -4,14 +4,28 @@ import { Readable } from 'stream';
|
|
4
4
|
import { PonyfillFormData } from './FormData';
|
5
5
|
import { PonyfillReadableStream } from './ReadableStream';
|
6
6
|
export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
|
7
|
+
export interface FormDataLimits {
|
8
|
+
fieldNameSize?: number;
|
9
|
+
fieldSize?: number;
|
10
|
+
fields?: number;
|
11
|
+
fileSize?: number;
|
12
|
+
files?: number;
|
13
|
+
parts?: number;
|
14
|
+
headerSize?: number;
|
15
|
+
}
|
16
|
+
export interface PonyfillBodyOptions {
|
17
|
+
formDataLimits?: FormDataLimits;
|
18
|
+
}
|
7
19
|
export declare class PonyfillBody implements Body {
|
8
20
|
private bodyInit;
|
21
|
+
private options;
|
9
22
|
bodyUsed: boolean;
|
10
|
-
|
23
|
+
private _body;
|
11
24
|
contentType: string | null;
|
12
25
|
contentLength: number | null;
|
13
|
-
constructor(bodyInit: BodyPonyfillInit | null);
|
26
|
+
constructor(bodyInit: BodyPonyfillInit | null, options?: PonyfillBodyOptions);
|
14
27
|
private bodyType?;
|
28
|
+
get body(): PonyfillReadableStream<Uint8Array> | null;
|
15
29
|
arrayBuffer(): Promise<ArrayBuffer>;
|
16
30
|
blob(): Promise<PonyfillBlob>;
|
17
31
|
formData(): Promise<PonyfillFormData>;
|
package/ReadableStream.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
/// <reference types="node" />
|
2
|
-
import { Readable
|
2
|
+
import { Readable } from 'stream';
|
3
3
|
export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
|
4
4
|
readable: Readable;
|
5
5
|
constructor(underlyingSource?: UnderlyingSource<T> | Readable | ReadableStream<T> | PonyfillReadableStream<T>);
|
@@ -16,6 +16,5 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
|
|
16
16
|
writable: WritableStream<T>;
|
17
17
|
readable: ReadableStream<T2>;
|
18
18
|
}): ReadableStream<T2>;
|
19
|
-
|
20
|
-
on(event: string, listener: (...args: any[]) => void): this;
|
19
|
+
static [Symbol.hasInstance](instance: unknown): instance is PonyfillReadableStream<unknown>;
|
21
20
|
}
|
package/Request.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { PonyfillBody, BodyPonyfillInit } from './Body';
|
1
|
+
import { PonyfillBody, BodyPonyfillInit, PonyfillBodyOptions } from './Body';
|
2
2
|
import { PonyfillHeadersInit } from './Headers';
|
3
|
-
export type RequestPonyfillInit = Omit<RequestInit, 'body' | 'headers'> & {
|
3
|
+
export type RequestPonyfillInit = PonyfillBodyOptions & Omit<RequestInit, 'body' | 'headers'> & {
|
4
4
|
body?: BodyPonyfillInit | null;
|
5
5
|
headers?: PonyfillHeadersInit;
|
6
6
|
};
|
package/Response.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import { PonyfillBody, BodyPonyfillInit } from './Body';
|
1
|
+
import { PonyfillBody, BodyPonyfillInit, PonyfillBodyOptions } from './Body';
|
2
2
|
import { PonyfillHeadersInit } from './Headers';
|
3
|
-
export type ResponsePonyfilInit = Omit<ResponseInit, 'headers'> & {
|
3
|
+
export type ResponsePonyfilInit = PonyfillBodyOptions & Omit<ResponseInit, 'headers'> & {
|
4
4
|
url?: string;
|
5
5
|
redirected?: boolean;
|
6
6
|
headers?: PonyfillHeadersInit;
|
package/index.js
CHANGED
@@ -2,11 +2,14 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6
|
+
|
5
7
|
const http = require('http');
|
6
8
|
const https = require('https');
|
7
9
|
const events = require('@whatwg-node/events');
|
8
10
|
const buffer = require('buffer');
|
9
11
|
const stream = require('stream');
|
12
|
+
const busboy = _interopDefault(require('busboy'));
|
10
13
|
const url = require('url');
|
11
14
|
const fs = require('fs');
|
12
15
|
|
@@ -201,13 +204,8 @@ class PonyfillReadableStream {
|
|
201
204
|
this.pipeTo(writable);
|
202
205
|
return readable;
|
203
206
|
}
|
204
|
-
|
205
|
-
|
206
|
-
return this.readable.pipe(writable);
|
207
|
-
}
|
208
|
-
on(event, listener) {
|
209
|
-
this.readable.on(event, listener);
|
210
|
-
return this;
|
207
|
+
static [Symbol.hasInstance](instance) {
|
208
|
+
return instance != null && typeof instance === 'object' && 'getReader' in instance;
|
211
209
|
}
|
212
210
|
}
|
213
211
|
|
@@ -296,39 +294,40 @@ var BodyInitType;
|
|
296
294
|
BodyInitType["Readable"] = "Readable";
|
297
295
|
})(BodyInitType || (BodyInitType = {}));
|
298
296
|
class PonyfillBody {
|
299
|
-
constructor(bodyInit) {
|
297
|
+
constructor(bodyInit, options = {}) {
|
300
298
|
this.bodyInit = bodyInit;
|
299
|
+
this.options = options;
|
301
300
|
this.bodyUsed = false;
|
302
|
-
this.
|
301
|
+
this._body = null;
|
303
302
|
this.contentType = null;
|
304
303
|
this.contentLength = null;
|
305
304
|
if (this.bodyInit == null) {
|
306
|
-
this.
|
305
|
+
this._body = null;
|
307
306
|
}
|
308
307
|
else if (typeof this.bodyInit === 'string') {
|
309
308
|
this.bodyType = BodyInitType.String;
|
310
309
|
const buffer = Buffer.from(this.bodyInit);
|
311
310
|
this.contentType = 'text/plain;charset=UTF-8';
|
312
311
|
this.contentLength = buffer.length;
|
313
|
-
this.
|
312
|
+
this._body = new PonyfillReadableStream(stream.Readable.from(buffer));
|
314
313
|
}
|
315
314
|
else if (this.bodyInit instanceof PonyfillReadableStream) {
|
316
315
|
this.bodyType = BodyInitType.ReadableStream;
|
317
|
-
this.
|
316
|
+
this._body = this.bodyInit;
|
318
317
|
}
|
319
318
|
else if (this.bodyInit instanceof PonyfillBlob) {
|
320
319
|
this.bodyType = BodyInitType.Blob;
|
321
320
|
const blobStream = this.bodyInit.stream();
|
322
321
|
this.contentType = this.bodyInit.type;
|
323
322
|
this.contentLength = this.bodyInit.size;
|
324
|
-
this.
|
323
|
+
this._body = new PonyfillReadableStream(blobStream);
|
325
324
|
}
|
326
325
|
else if (this.bodyInit instanceof PonyfillFormData) {
|
327
326
|
this.bodyType = BodyInitType.FormData;
|
328
327
|
const boundary = Math.random().toString(36).substr(2);
|
329
328
|
const formData = this.bodyInit;
|
330
329
|
this.contentType = `multipart/form-data; boundary=${boundary}`;
|
331
|
-
this.
|
330
|
+
this._body = new PonyfillReadableStream({
|
332
331
|
start: async (controller) => {
|
333
332
|
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
334
333
|
const entries = [];
|
@@ -363,21 +362,46 @@ class PonyfillBody {
|
|
363
362
|
}
|
364
363
|
else if ('buffer' in this.bodyInit) {
|
365
364
|
this.contentLength = this.bodyInit.byteLength;
|
366
|
-
this.
|
365
|
+
this._body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit));
|
367
366
|
}
|
368
367
|
else if (this.bodyInit instanceof ArrayBuffer) {
|
369
368
|
this.bodyType = BodyInitType.ArrayBuffer;
|
370
369
|
this.contentLength = this.bodyInit.byteLength;
|
371
|
-
this.
|
370
|
+
this._body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit)));
|
372
371
|
}
|
373
372
|
else if (this.bodyInit instanceof stream.Readable) {
|
374
373
|
this.bodyType = BodyInitType.Readable;
|
375
|
-
this.
|
374
|
+
this._body = new PonyfillReadableStream(this.bodyInit);
|
376
375
|
}
|
377
376
|
else {
|
378
377
|
throw new Error('Unknown body type');
|
379
378
|
}
|
380
379
|
}
|
380
|
+
get body() {
|
381
|
+
if (this._body != null) {
|
382
|
+
const ponyfillReadableStream = this._body;
|
383
|
+
const readable = this._body.readable;
|
384
|
+
return new Proxy(this._body.readable, {
|
385
|
+
get(_, prop) {
|
386
|
+
if (prop in ponyfillReadableStream) {
|
387
|
+
const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
|
388
|
+
if (typeof ponyfillReadableStreamProp === 'function') {
|
389
|
+
return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
|
390
|
+
}
|
391
|
+
return ponyfillReadableStreamProp;
|
392
|
+
}
|
393
|
+
if (prop in readable) {
|
394
|
+
const readableProp = readable[prop];
|
395
|
+
if (typeof readableProp === 'function') {
|
396
|
+
return readableProp.bind(readable);
|
397
|
+
}
|
398
|
+
return readableProp;
|
399
|
+
}
|
400
|
+
}
|
401
|
+
});
|
402
|
+
}
|
403
|
+
return null;
|
404
|
+
}
|
381
405
|
async arrayBuffer() {
|
382
406
|
if (this.bodyType === BodyInitType.ArrayBuffer) {
|
383
407
|
return this.bodyInit;
|
@@ -390,18 +414,73 @@ class PonyfillBody {
|
|
390
414
|
return this.bodyInit;
|
391
415
|
}
|
392
416
|
const chunks = [];
|
393
|
-
if (this.
|
394
|
-
for await (const chunk of this.
|
417
|
+
if (this._body) {
|
418
|
+
for await (const chunk of this._body.readable) {
|
395
419
|
chunks.push(chunk);
|
396
420
|
}
|
397
421
|
}
|
398
422
|
return new PonyfillBlob(chunks);
|
399
423
|
}
|
400
|
-
|
424
|
+
formData() {
|
401
425
|
if (this.bodyType === BodyInitType.FormData) {
|
402
|
-
return this.bodyInit;
|
403
|
-
}
|
404
|
-
|
426
|
+
return Promise.resolve(this.bodyInit);
|
427
|
+
}
|
428
|
+
const formData = new PonyfillFormData();
|
429
|
+
if (this._body == null) {
|
430
|
+
return Promise.resolve(formData);
|
431
|
+
}
|
432
|
+
const formDataLimits = this.options.formDataLimits;
|
433
|
+
return new Promise((resolve, reject) => {
|
434
|
+
var _a;
|
435
|
+
const bb = busboy({
|
436
|
+
headers: {
|
437
|
+
'content-type': this.contentType || ''
|
438
|
+
},
|
439
|
+
limits: formDataLimits,
|
440
|
+
defParamCharset: 'utf-8'
|
441
|
+
});
|
442
|
+
bb.on('field', (name, value, { nameTruncated, valueTruncated }) => {
|
443
|
+
if (nameTruncated) {
|
444
|
+
reject(new Error(`Field name size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldNameSize} bytes`));
|
445
|
+
}
|
446
|
+
if (valueTruncated) {
|
447
|
+
reject(new Error(`Field value size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldSize} bytes`));
|
448
|
+
}
|
449
|
+
formData.set(name, value);
|
450
|
+
});
|
451
|
+
bb.on('fieldsLimit', () => {
|
452
|
+
reject(new Error(`Fields limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fields}`));
|
453
|
+
});
|
454
|
+
bb.on('file', (name, fileStream, { filename, mimeType }) => {
|
455
|
+
const chunks = [];
|
456
|
+
fileStream.on('limit', () => {
|
457
|
+
reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`));
|
458
|
+
});
|
459
|
+
fileStream.on('data', (chunk) => {
|
460
|
+
chunks.push(Buffer.from(chunk));
|
461
|
+
});
|
462
|
+
fileStream.on('close', () => {
|
463
|
+
if (fileStream.truncated) {
|
464
|
+
reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`));
|
465
|
+
}
|
466
|
+
const file = new PonyfillFile(chunks, filename, { type: mimeType });
|
467
|
+
formData.set(name, file);
|
468
|
+
});
|
469
|
+
});
|
470
|
+
bb.on('filesLimit', () => {
|
471
|
+
reject(new Error(`Files limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.files}`));
|
472
|
+
});
|
473
|
+
bb.on('partsLimit', () => {
|
474
|
+
reject(new Error(`Parts limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.parts}`));
|
475
|
+
});
|
476
|
+
bb.on('close', () => {
|
477
|
+
resolve(formData);
|
478
|
+
});
|
479
|
+
bb.on('error', err => {
|
480
|
+
reject(err);
|
481
|
+
});
|
482
|
+
(_a = this._body) === null || _a === void 0 ? void 0 : _a.readable.pipe(bb);
|
483
|
+
});
|
405
484
|
}
|
406
485
|
async json() {
|
407
486
|
const text = await this.text();
|
@@ -418,8 +497,8 @@ class PonyfillBody {
|
|
418
497
|
if (this.bodyType === BodyInitType.Readable) {
|
419
498
|
return this.bodyInit;
|
420
499
|
}
|
421
|
-
if (this.
|
422
|
-
return this.
|
500
|
+
if (this._body != null) {
|
501
|
+
return this._body.readable;
|
423
502
|
}
|
424
503
|
return null;
|
425
504
|
}
|
@@ -520,7 +599,7 @@ class PonyfillRequest extends PonyfillBody {
|
|
520
599
|
bodyInit = options.body || null;
|
521
600
|
requestInit = options;
|
522
601
|
}
|
523
|
-
super(bodyInit);
|
602
|
+
super(bodyInit, options);
|
524
603
|
this.destination = '';
|
525
604
|
this.priority = 'auto';
|
526
605
|
this.cache = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.cache) || 'default';
|
@@ -540,16 +619,24 @@ class PonyfillRequest extends PonyfillBody {
|
|
540
619
|
this.headers.set('connection', 'keep-alive');
|
541
620
|
}
|
542
621
|
}
|
543
|
-
|
622
|
+
const contentTypeInHeaders = this.headers.get('content-type');
|
623
|
+
if (!contentTypeInHeaders) {
|
544
624
|
if (this.contentType) {
|
545
625
|
this.headers.set('content-type', this.contentType);
|
546
626
|
}
|
547
627
|
}
|
548
|
-
|
628
|
+
else {
|
629
|
+
this.contentType = contentTypeInHeaders;
|
630
|
+
}
|
631
|
+
const contentLengthInHeaders = this.headers.get('content-length');
|
632
|
+
if (!contentLengthInHeaders) {
|
549
633
|
if (this.contentLength) {
|
550
634
|
this.headers.set('content-length', this.contentLength.toString());
|
551
635
|
}
|
552
636
|
}
|
637
|
+
else {
|
638
|
+
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
639
|
+
}
|
553
640
|
}
|
554
641
|
clone() {
|
555
642
|
return new Request(this);
|
@@ -558,7 +645,7 @@ class PonyfillRequest extends PonyfillBody {
|
|
558
645
|
|
559
646
|
class PonyfillResponse extends PonyfillBody {
|
560
647
|
constructor(body, init) {
|
561
|
-
super(body || null);
|
648
|
+
super(body || null, init);
|
562
649
|
this.headers = new PonyfillHeaders();
|
563
650
|
this.status = 200;
|
564
651
|
this.statusText = 'OK';
|
@@ -648,7 +735,7 @@ const fetchPonyfill = (info, init) => {
|
|
648
735
|
return;
|
649
736
|
}
|
650
737
|
const requestFn = getRequestFnForProtocol(protocol);
|
651
|
-
const nodeReadable = fetchRequest.
|
738
|
+
const nodeReadable = fetchRequest.body;
|
652
739
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
653
740
|
const abortListener = function abortListener(event) {
|
654
741
|
nodeRequest.destroy();
|
package/index.mjs
CHANGED
@@ -3,6 +3,7 @@ import { request } from 'https';
|
|
3
3
|
import { EventTarget, CustomEvent } from '@whatwg-node/events';
|
4
4
|
import { Blob } from 'buffer';
|
5
5
|
import { Readable } from 'stream';
|
6
|
+
import busboy from 'busboy';
|
6
7
|
import { fileURLToPath } from 'url';
|
7
8
|
import { createReadStream } from 'fs';
|
8
9
|
|
@@ -197,13 +198,8 @@ class PonyfillReadableStream {
|
|
197
198
|
this.pipeTo(writable);
|
198
199
|
return readable;
|
199
200
|
}
|
200
|
-
|
201
|
-
|
202
|
-
return this.readable.pipe(writable);
|
203
|
-
}
|
204
|
-
on(event, listener) {
|
205
|
-
this.readable.on(event, listener);
|
206
|
-
return this;
|
201
|
+
static [Symbol.hasInstance](instance) {
|
202
|
+
return instance != null && typeof instance === 'object' && 'getReader' in instance;
|
207
203
|
}
|
208
204
|
}
|
209
205
|
|
@@ -292,39 +288,40 @@ var BodyInitType;
|
|
292
288
|
BodyInitType["Readable"] = "Readable";
|
293
289
|
})(BodyInitType || (BodyInitType = {}));
|
294
290
|
class PonyfillBody {
|
295
|
-
constructor(bodyInit) {
|
291
|
+
constructor(bodyInit, options = {}) {
|
296
292
|
this.bodyInit = bodyInit;
|
293
|
+
this.options = options;
|
297
294
|
this.bodyUsed = false;
|
298
|
-
this.
|
295
|
+
this._body = null;
|
299
296
|
this.contentType = null;
|
300
297
|
this.contentLength = null;
|
301
298
|
if (this.bodyInit == null) {
|
302
|
-
this.
|
299
|
+
this._body = null;
|
303
300
|
}
|
304
301
|
else if (typeof this.bodyInit === 'string') {
|
305
302
|
this.bodyType = BodyInitType.String;
|
306
303
|
const buffer = Buffer.from(this.bodyInit);
|
307
304
|
this.contentType = 'text/plain;charset=UTF-8';
|
308
305
|
this.contentLength = buffer.length;
|
309
|
-
this.
|
306
|
+
this._body = new PonyfillReadableStream(Readable.from(buffer));
|
310
307
|
}
|
311
308
|
else if (this.bodyInit instanceof PonyfillReadableStream) {
|
312
309
|
this.bodyType = BodyInitType.ReadableStream;
|
313
|
-
this.
|
310
|
+
this._body = this.bodyInit;
|
314
311
|
}
|
315
312
|
else if (this.bodyInit instanceof PonyfillBlob) {
|
316
313
|
this.bodyType = BodyInitType.Blob;
|
317
314
|
const blobStream = this.bodyInit.stream();
|
318
315
|
this.contentType = this.bodyInit.type;
|
319
316
|
this.contentLength = this.bodyInit.size;
|
320
|
-
this.
|
317
|
+
this._body = new PonyfillReadableStream(blobStream);
|
321
318
|
}
|
322
319
|
else if (this.bodyInit instanceof PonyfillFormData) {
|
323
320
|
this.bodyType = BodyInitType.FormData;
|
324
321
|
const boundary = Math.random().toString(36).substr(2);
|
325
322
|
const formData = this.bodyInit;
|
326
323
|
this.contentType = `multipart/form-data; boundary=${boundary}`;
|
327
|
-
this.
|
324
|
+
this._body = new PonyfillReadableStream({
|
328
325
|
start: async (controller) => {
|
329
326
|
controller.enqueue(Buffer.from(`--${boundary}\r\n`));
|
330
327
|
const entries = [];
|
@@ -359,21 +356,46 @@ class PonyfillBody {
|
|
359
356
|
}
|
360
357
|
else if ('buffer' in this.bodyInit) {
|
361
358
|
this.contentLength = this.bodyInit.byteLength;
|
362
|
-
this.
|
359
|
+
this._body = new PonyfillReadableStream(Readable.from(this.bodyInit));
|
363
360
|
}
|
364
361
|
else if (this.bodyInit instanceof ArrayBuffer) {
|
365
362
|
this.bodyType = BodyInitType.ArrayBuffer;
|
366
363
|
this.contentLength = this.bodyInit.byteLength;
|
367
|
-
this.
|
364
|
+
this._body = new PonyfillReadableStream(Readable.from(Buffer.from(this.bodyInit)));
|
368
365
|
}
|
369
366
|
else if (this.bodyInit instanceof Readable) {
|
370
367
|
this.bodyType = BodyInitType.Readable;
|
371
|
-
this.
|
368
|
+
this._body = new PonyfillReadableStream(this.bodyInit);
|
372
369
|
}
|
373
370
|
else {
|
374
371
|
throw new Error('Unknown body type');
|
375
372
|
}
|
376
373
|
}
|
374
|
+
get body() {
|
375
|
+
if (this._body != null) {
|
376
|
+
const ponyfillReadableStream = this._body;
|
377
|
+
const readable = this._body.readable;
|
378
|
+
return new Proxy(this._body.readable, {
|
379
|
+
get(_, prop) {
|
380
|
+
if (prop in ponyfillReadableStream) {
|
381
|
+
const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
|
382
|
+
if (typeof ponyfillReadableStreamProp === 'function') {
|
383
|
+
return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
|
384
|
+
}
|
385
|
+
return ponyfillReadableStreamProp;
|
386
|
+
}
|
387
|
+
if (prop in readable) {
|
388
|
+
const readableProp = readable[prop];
|
389
|
+
if (typeof readableProp === 'function') {
|
390
|
+
return readableProp.bind(readable);
|
391
|
+
}
|
392
|
+
return readableProp;
|
393
|
+
}
|
394
|
+
}
|
395
|
+
});
|
396
|
+
}
|
397
|
+
return null;
|
398
|
+
}
|
377
399
|
async arrayBuffer() {
|
378
400
|
if (this.bodyType === BodyInitType.ArrayBuffer) {
|
379
401
|
return this.bodyInit;
|
@@ -386,18 +408,73 @@ class PonyfillBody {
|
|
386
408
|
return this.bodyInit;
|
387
409
|
}
|
388
410
|
const chunks = [];
|
389
|
-
if (this.
|
390
|
-
for await (const chunk of this.
|
411
|
+
if (this._body) {
|
412
|
+
for await (const chunk of this._body.readable) {
|
391
413
|
chunks.push(chunk);
|
392
414
|
}
|
393
415
|
}
|
394
416
|
return new PonyfillBlob(chunks);
|
395
417
|
}
|
396
|
-
|
418
|
+
formData() {
|
397
419
|
if (this.bodyType === BodyInitType.FormData) {
|
398
|
-
return this.bodyInit;
|
399
|
-
}
|
400
|
-
|
420
|
+
return Promise.resolve(this.bodyInit);
|
421
|
+
}
|
422
|
+
const formData = new PonyfillFormData();
|
423
|
+
if (this._body == null) {
|
424
|
+
return Promise.resolve(formData);
|
425
|
+
}
|
426
|
+
const formDataLimits = this.options.formDataLimits;
|
427
|
+
return new Promise((resolve, reject) => {
|
428
|
+
var _a;
|
429
|
+
const bb = busboy({
|
430
|
+
headers: {
|
431
|
+
'content-type': this.contentType || ''
|
432
|
+
},
|
433
|
+
limits: formDataLimits,
|
434
|
+
defParamCharset: 'utf-8'
|
435
|
+
});
|
436
|
+
bb.on('field', (name, value, { nameTruncated, valueTruncated }) => {
|
437
|
+
if (nameTruncated) {
|
438
|
+
reject(new Error(`Field name size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldNameSize} bytes`));
|
439
|
+
}
|
440
|
+
if (valueTruncated) {
|
441
|
+
reject(new Error(`Field value size exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fieldSize} bytes`));
|
442
|
+
}
|
443
|
+
formData.set(name, value);
|
444
|
+
});
|
445
|
+
bb.on('fieldsLimit', () => {
|
446
|
+
reject(new Error(`Fields limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fields}`));
|
447
|
+
});
|
448
|
+
bb.on('file', (name, fileStream, { filename, mimeType }) => {
|
449
|
+
const chunks = [];
|
450
|
+
fileStream.on('limit', () => {
|
451
|
+
reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`));
|
452
|
+
});
|
453
|
+
fileStream.on('data', (chunk) => {
|
454
|
+
chunks.push(Buffer.from(chunk));
|
455
|
+
});
|
456
|
+
fileStream.on('close', () => {
|
457
|
+
if (fileStream.truncated) {
|
458
|
+
reject(new Error(`File size limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.fileSize} bytes`));
|
459
|
+
}
|
460
|
+
const file = new PonyfillFile(chunks, filename, { type: mimeType });
|
461
|
+
formData.set(name, file);
|
462
|
+
});
|
463
|
+
});
|
464
|
+
bb.on('filesLimit', () => {
|
465
|
+
reject(new Error(`Files limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.files}`));
|
466
|
+
});
|
467
|
+
bb.on('partsLimit', () => {
|
468
|
+
reject(new Error(`Parts limit exceeded: ${formDataLimits === null || formDataLimits === void 0 ? void 0 : formDataLimits.parts}`));
|
469
|
+
});
|
470
|
+
bb.on('close', () => {
|
471
|
+
resolve(formData);
|
472
|
+
});
|
473
|
+
bb.on('error', err => {
|
474
|
+
reject(err);
|
475
|
+
});
|
476
|
+
(_a = this._body) === null || _a === void 0 ? void 0 : _a.readable.pipe(bb);
|
477
|
+
});
|
401
478
|
}
|
402
479
|
async json() {
|
403
480
|
const text = await this.text();
|
@@ -414,8 +491,8 @@ class PonyfillBody {
|
|
414
491
|
if (this.bodyType === BodyInitType.Readable) {
|
415
492
|
return this.bodyInit;
|
416
493
|
}
|
417
|
-
if (this.
|
418
|
-
return this.
|
494
|
+
if (this._body != null) {
|
495
|
+
return this._body.readable;
|
419
496
|
}
|
420
497
|
return null;
|
421
498
|
}
|
@@ -516,7 +593,7 @@ class PonyfillRequest extends PonyfillBody {
|
|
516
593
|
bodyInit = options.body || null;
|
517
594
|
requestInit = options;
|
518
595
|
}
|
519
|
-
super(bodyInit);
|
596
|
+
super(bodyInit, options);
|
520
597
|
this.destination = '';
|
521
598
|
this.priority = 'auto';
|
522
599
|
this.cache = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.cache) || 'default';
|
@@ -536,16 +613,24 @@ class PonyfillRequest extends PonyfillBody {
|
|
536
613
|
this.headers.set('connection', 'keep-alive');
|
537
614
|
}
|
538
615
|
}
|
539
|
-
|
616
|
+
const contentTypeInHeaders = this.headers.get('content-type');
|
617
|
+
if (!contentTypeInHeaders) {
|
540
618
|
if (this.contentType) {
|
541
619
|
this.headers.set('content-type', this.contentType);
|
542
620
|
}
|
543
621
|
}
|
544
|
-
|
622
|
+
else {
|
623
|
+
this.contentType = contentTypeInHeaders;
|
624
|
+
}
|
625
|
+
const contentLengthInHeaders = this.headers.get('content-length');
|
626
|
+
if (!contentLengthInHeaders) {
|
545
627
|
if (this.contentLength) {
|
546
628
|
this.headers.set('content-length', this.contentLength.toString());
|
547
629
|
}
|
548
630
|
}
|
631
|
+
else {
|
632
|
+
this.contentLength = parseInt(contentLengthInHeaders, 10);
|
633
|
+
}
|
549
634
|
}
|
550
635
|
clone() {
|
551
636
|
return new Request(this);
|
@@ -554,7 +639,7 @@ class PonyfillRequest extends PonyfillBody {
|
|
554
639
|
|
555
640
|
class PonyfillResponse extends PonyfillBody {
|
556
641
|
constructor(body, init) {
|
557
|
-
super(body || null);
|
642
|
+
super(body || null, init);
|
558
643
|
this.headers = new PonyfillHeaders();
|
559
644
|
this.status = 200;
|
560
645
|
this.statusText = 'OK';
|
@@ -644,7 +729,7 @@ const fetchPonyfill = (info, init) => {
|
|
644
729
|
return;
|
645
730
|
}
|
646
731
|
const requestFn = getRequestFnForProtocol(protocol);
|
647
|
-
const nodeReadable = fetchRequest.
|
732
|
+
const nodeReadable = fetchRequest.body;
|
648
733
|
const nodeHeaders = getHeadersObj(fetchRequest.headers);
|
649
734
|
const abortListener = function abortListener(event) {
|
650
735
|
nodeRequest.destroy();
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@whatwg-node/node-fetch",
|
3
|
-
"version": "0.0.1-alpha-
|
3
|
+
"version": "0.0.1-alpha-20221228110205-1d6dcac",
|
4
4
|
"description": "Fetch API implementation for Node",
|
5
5
|
"sideEffects": false,
|
6
6
|
"peerDependencies": {
|
@@ -8,6 +8,7 @@
|
|
8
8
|
},
|
9
9
|
"dependencies": {
|
10
10
|
"@whatwg-node/events": "0.0.2",
|
11
|
+
"busboy": "1.6.0",
|
11
12
|
"tslib": "^2.3.1"
|
12
13
|
},
|
13
14
|
"repository": {
|