@whatwg-node/node-fetch 0.0.1-alpha-20221228115139-6b81aa1 → 0.0.1-alpha-20221230080146-72104f6

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 CHANGED
@@ -28,7 +28,9 @@ export declare class PonyfillBody implements Body {
28
28
  get body(): PonyfillReadableStream<Uint8Array> | null;
29
29
  arrayBuffer(): Promise<ArrayBuffer>;
30
30
  blob(): Promise<PonyfillBlob>;
31
- formData(): Promise<PonyfillFormData>;
31
+ formData(opts?: {
32
+ formDataLimits: FormDataLimits;
33
+ }): Promise<PonyfillFormData>;
32
34
  json(): Promise<any>;
33
35
  text(): Promise<string>;
34
36
  }
package/FormData.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { PonyfillBlob } from './Blob';
2
+ import { PonyfillReadableStream } from './ReadableStream';
2
3
  export declare class PonyfillFormData implements FormData {
3
4
  private map;
4
5
  append(name: string, value: PonyfillBlob | string, fileName?: string): void;
@@ -7,6 +8,7 @@ export declare class PonyfillFormData implements FormData {
7
8
  getAll(name: string): FormDataEntryValue[];
8
9
  has(name: string): boolean;
9
10
  set(name: string, value: PonyfillBlob | string, fileName?: string): void;
10
- private getNormalizedFile;
11
+ [Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
11
12
  forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
13
+ stream(boundary?: string): PonyfillReadableStream<Uint8Array>;
12
14
  }
package/index.js CHANGED
@@ -33,7 +33,9 @@ class PonyfillAbortSignal extends events.EventTarget {
33
33
  this._onabort = null;
34
34
  }
35
35
  throwIfAborted() {
36
- throw new PonyfillAbortError();
36
+ if (this.aborted) {
37
+ throw new PonyfillAbortError();
38
+ }
37
39
  }
38
40
  get onabort() {
39
41
  return this._onabort;
@@ -235,7 +237,7 @@ class PonyfillFormData {
235
237
  values = [];
236
238
  this.map.set(name, values);
237
239
  }
238
- const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
240
+ const entry = value instanceof PonyfillBlob ? getNormalizedFile(name, value, fileName) : value;
239
241
  values.push(entry);
240
242
  }
241
243
  delete(name) {
@@ -252,25 +254,67 @@ class PonyfillFormData {
252
254
  return this.map.has(name);
253
255
  }
254
256
  set(name, value, fileName) {
255
- const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
257
+ const entry = value instanceof PonyfillBlob ? getNormalizedFile(name, value, fileName) : value;
256
258
  this.map.set(name, [entry]);
257
259
  }
258
- getNormalizedFile(name, blob, fileName) {
259
- if (blob instanceof PonyfillFile) {
260
- if (fileName != null) {
261
- return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
260
+ *[Symbol.iterator]() {
261
+ for (const [key, values] of this.map) {
262
+ for (const value of values) {
263
+ yield [key, value];
262
264
  }
263
- return blob;
264
265
  }
265
- return new PonyfillFile([blob], fileName || name, { type: blob.type });
266
266
  }
267
267
  forEach(callback) {
268
- for (const [key, values] of this.map) {
269
- for (const value of values) {
270
- callback(value, key, this);
268
+ for (const [key, value] of this) {
269
+ callback(value, key, this);
270
+ }
271
+ }
272
+ stream(boundary = '---') {
273
+ const entries = [];
274
+ return new PonyfillReadableStream({
275
+ start: async (controller) => {
276
+ controller.enqueue(Buffer.from(`--${boundary}\r\n`));
277
+ this.forEach((value, key) => {
278
+ entries.push([key, value]);
279
+ });
280
+ },
281
+ pull: async (controller) => {
282
+ const entry = entries.shift();
283
+ if (entry) {
284
+ const [key, value] = entry;
285
+ if (value instanceof PonyfillBlob) {
286
+ let filenamePart = '';
287
+ if (value.name) {
288
+ filenamePart = `; filename="${value.name}"`;
289
+ }
290
+ controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
291
+ controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
292
+ controller.enqueue(Buffer.from(await value.arrayBuffer()));
293
+ }
294
+ else {
295
+ controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
296
+ controller.enqueue(Buffer.from(value));
297
+ }
298
+ if (entries.length === 0) {
299
+ controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
300
+ controller.close();
301
+ }
302
+ else {
303
+ controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
304
+ }
305
+ }
271
306
  }
307
+ });
308
+ }
309
+ }
310
+ function getNormalizedFile(name, blob, fileName) {
311
+ if (blob instanceof PonyfillFile) {
312
+ if (fileName != null) {
313
+ return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
272
314
  }
315
+ return blob;
273
316
  }
317
+ return new PonyfillFile([blob], fileName || name, { type: blob.type });
274
318
  }
275
319
 
276
320
  var BodyInitType;
@@ -290,81 +334,11 @@ class PonyfillBody {
290
334
  this._body = null;
291
335
  this.contentType = null;
292
336
  this.contentLength = null;
293
- if (this.bodyInit == null) {
294
- this._body = null;
295
- }
296
- else if (typeof this.bodyInit === 'string') {
297
- this.bodyType = BodyInitType.String;
298
- const buffer = Buffer.from(this.bodyInit);
299
- this.contentType = 'text/plain;charset=UTF-8';
300
- this.contentLength = buffer.length;
301
- this._body = new PonyfillReadableStream(stream.Readable.from(buffer));
302
- }
303
- else if (this.bodyInit instanceof PonyfillReadableStream) {
304
- this.bodyType = BodyInitType.ReadableStream;
305
- this._body = this.bodyInit;
306
- }
307
- else if (this.bodyInit instanceof PonyfillBlob) {
308
- this.bodyType = BodyInitType.Blob;
309
- const blobStream = this.bodyInit.stream();
310
- this.contentType = this.bodyInit.type;
311
- this.contentLength = this.bodyInit.size;
312
- this._body = new PonyfillReadableStream(blobStream);
313
- }
314
- else if (this.bodyInit instanceof PonyfillFormData) {
315
- this.bodyType = BodyInitType.FormData;
316
- const boundary = Math.random().toString(36).substr(2);
317
- const formData = this.bodyInit;
318
- this.contentType = `multipart/form-data; boundary=${boundary}`;
319
- this._body = new PonyfillReadableStream({
320
- start: async (controller) => {
321
- controller.enqueue(Buffer.from(`--${boundary}\r\n`));
322
- const entries = [];
323
- formData.forEach((value, key) => {
324
- entries.push([key, value]);
325
- });
326
- for (const i in entries) {
327
- const [key, value] = entries[i];
328
- if (value instanceof PonyfillBlob) {
329
- let filenamePart = '';
330
- if (value.name) {
331
- filenamePart = `; filename="${value.name}"`;
332
- }
333
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
334
- controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
335
- controller.enqueue(Buffer.from(await value.arrayBuffer()));
336
- }
337
- else {
338
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
339
- controller.enqueue(Buffer.from(value));
340
- }
341
- if (Number(i) === entries.length - 1) {
342
- controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
343
- }
344
- else {
345
- controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
346
- }
347
- }
348
- controller.close();
349
- },
350
- });
351
- }
352
- else if ('buffer' in this.bodyInit) {
353
- this.contentLength = this.bodyInit.byteLength;
354
- this._body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit));
355
- }
356
- else if (this.bodyInit instanceof ArrayBuffer) {
357
- this.bodyType = BodyInitType.ArrayBuffer;
358
- this.contentLength = this.bodyInit.byteLength;
359
- this._body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit)));
360
- }
361
- else if (this.bodyInit instanceof stream.Readable) {
362
- this.bodyType = BodyInitType.Readable;
363
- this._body = new PonyfillReadableStream(this.bodyInit);
364
- }
365
- else {
366
- throw new Error('Unknown body type');
367
- }
337
+ const { body, contentType, contentLength, bodyType, } = processBodyInit(bodyInit);
338
+ this._body = body;
339
+ this.contentType = contentType;
340
+ this.contentLength = contentLength;
341
+ this.bodyType = bodyType;
368
342
  }
369
343
  get body() {
370
344
  if (this._body != null) {
@@ -410,7 +384,7 @@ class PonyfillBody {
410
384
  }
411
385
  return new PonyfillBlob(chunks);
412
386
  }
413
- formData() {
387
+ formData(opts) {
414
388
  if (this.bodyType === BodyInitType.FormData) {
415
389
  return Promise.resolve(this.bodyInit);
416
390
  }
@@ -418,7 +392,10 @@ class PonyfillBody {
418
392
  if (this._body == null) {
419
393
  return Promise.resolve(formData);
420
394
  }
421
- const formDataLimits = this.options.formDataLimits;
395
+ const formDataLimits = {
396
+ ...this.options.formDataLimits,
397
+ ...opts === null || opts === void 0 ? void 0 : opts.formDataLimits,
398
+ };
422
399
  return new Promise((resolve, reject) => {
423
400
  var _a;
424
401
  const bb = busboy({
@@ -483,6 +460,88 @@ class PonyfillBody {
483
460
  return blob.text();
484
461
  }
485
462
  }
463
+ function processBodyInit(bodyInit) {
464
+ if (bodyInit == null) {
465
+ return {
466
+ body: null,
467
+ contentType: null,
468
+ contentLength: null,
469
+ };
470
+ }
471
+ if (typeof bodyInit === 'string') {
472
+ const buffer = Buffer.from(bodyInit);
473
+ const readable = stream.Readable.from(buffer);
474
+ const body = new PonyfillReadableStream(readable);
475
+ return {
476
+ bodyType: BodyInitType.String,
477
+ contentType: 'text/plain;charset=UTF-8',
478
+ contentLength: buffer.length,
479
+ body,
480
+ };
481
+ }
482
+ if (bodyInit instanceof PonyfillReadableStream) {
483
+ return {
484
+ bodyType: BodyInitType.ReadableStream,
485
+ body: bodyInit,
486
+ contentType: null,
487
+ contentLength: null,
488
+ };
489
+ }
490
+ if (bodyInit instanceof PonyfillBlob) {
491
+ const readable = bodyInit.stream();
492
+ const body = new PonyfillReadableStream(readable);
493
+ return {
494
+ bodyType: BodyInitType.Blob,
495
+ contentType: bodyInit.type,
496
+ contentLength: bodyInit.size,
497
+ body,
498
+ };
499
+ }
500
+ if (bodyInit instanceof PonyfillFormData) {
501
+ const boundary = Math.random().toString(36).substr(2);
502
+ const contentType = `multipart/form-data; boundary=${boundary}`;
503
+ const body = bodyInit.stream(boundary);
504
+ return {
505
+ bodyType: BodyInitType.FormData,
506
+ contentType,
507
+ contentLength: null,
508
+ body,
509
+ };
510
+ }
511
+ if ('buffer' in bodyInit) {
512
+ const contentLength = bodyInit.byteLength;
513
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
514
+ const readable = stream.Readable.from(buffer);
515
+ const body = new PonyfillReadableStream(readable);
516
+ return {
517
+ contentLength,
518
+ contentType: null,
519
+ body,
520
+ };
521
+ }
522
+ if (bodyInit instanceof ArrayBuffer) {
523
+ const contentLength = bodyInit.byteLength;
524
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
525
+ const readable = stream.Readable.from(buffer);
526
+ const body = new PonyfillReadableStream(readable);
527
+ return {
528
+ bodyType: BodyInitType.ArrayBuffer,
529
+ contentType: null,
530
+ contentLength,
531
+ body,
532
+ };
533
+ }
534
+ if (bodyInit instanceof stream.Readable) {
535
+ const body = new PonyfillReadableStream(bodyInit);
536
+ return {
537
+ bodyType: BodyInitType.Readable,
538
+ contentType: null,
539
+ contentLength: null,
540
+ body,
541
+ };
542
+ }
543
+ throw new Error('Unknown body type');
544
+ }
486
545
 
487
546
  class PonyfillHeaders {
488
547
  constructor(headersInit) {
@@ -502,7 +561,7 @@ class PonyfillHeaders {
502
561
  }
503
562
  else if ('get' in headersInit) {
504
563
  headersInit.forEach((value, key) => {
505
- this.set(key, value);
564
+ this.append(key, value);
506
565
  });
507
566
  }
508
567
  else {
@@ -730,7 +789,8 @@ const fetchPonyfill = (info, init) => {
730
789
  // signal: fetchRequest.signal will be added when v14 reaches EOL
731
790
  method: fetchRequest.method,
732
791
  headers: nodeHeaders,
733
- }, nodeResponse => {
792
+ });
793
+ nodeRequest.once('response', nodeResponse => {
734
794
  if (nodeResponse.headers.location) {
735
795
  if (fetchRequest.redirect === 'error') {
736
796
  const redirectError = new Error('Redirects are not allowed');
@@ -758,7 +818,7 @@ const fetchPonyfill = (info, init) => {
758
818
  });
759
819
  resolve(ponyfillResponse);
760
820
  });
761
- nodeRequest.on('error', reject);
821
+ nodeRequest.once('error', reject);
762
822
  if (nodeReadable) {
763
823
  nodeReadable.pipe(nodeRequest);
764
824
  }
package/index.mjs CHANGED
@@ -27,7 +27,9 @@ class PonyfillAbortSignal extends EventTarget {
27
27
  this._onabort = null;
28
28
  }
29
29
  throwIfAborted() {
30
- throw new PonyfillAbortError();
30
+ if (this.aborted) {
31
+ throw new PonyfillAbortError();
32
+ }
31
33
  }
32
34
  get onabort() {
33
35
  return this._onabort;
@@ -229,7 +231,7 @@ class PonyfillFormData {
229
231
  values = [];
230
232
  this.map.set(name, values);
231
233
  }
232
- const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
234
+ const entry = value instanceof PonyfillBlob ? getNormalizedFile(name, value, fileName) : value;
233
235
  values.push(entry);
234
236
  }
235
237
  delete(name) {
@@ -246,25 +248,67 @@ class PonyfillFormData {
246
248
  return this.map.has(name);
247
249
  }
248
250
  set(name, value, fileName) {
249
- const entry = value instanceof PonyfillBlob ? this.getNormalizedFile(name, value, fileName) : value;
251
+ const entry = value instanceof PonyfillBlob ? getNormalizedFile(name, value, fileName) : value;
250
252
  this.map.set(name, [entry]);
251
253
  }
252
- getNormalizedFile(name, blob, fileName) {
253
- if (blob instanceof PonyfillFile) {
254
- if (fileName != null) {
255
- return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
254
+ *[Symbol.iterator]() {
255
+ for (const [key, values] of this.map) {
256
+ for (const value of values) {
257
+ yield [key, value];
256
258
  }
257
- return blob;
258
259
  }
259
- return new PonyfillFile([blob], fileName || name, { type: blob.type });
260
260
  }
261
261
  forEach(callback) {
262
- for (const [key, values] of this.map) {
263
- for (const value of values) {
264
- callback(value, key, this);
262
+ for (const [key, value] of this) {
263
+ callback(value, key, this);
264
+ }
265
+ }
266
+ stream(boundary = '---') {
267
+ const entries = [];
268
+ return new PonyfillReadableStream({
269
+ start: async (controller) => {
270
+ controller.enqueue(Buffer.from(`--${boundary}\r\n`));
271
+ this.forEach((value, key) => {
272
+ entries.push([key, value]);
273
+ });
274
+ },
275
+ pull: async (controller) => {
276
+ const entry = entries.shift();
277
+ if (entry) {
278
+ const [key, value] = entry;
279
+ if (value instanceof PonyfillBlob) {
280
+ let filenamePart = '';
281
+ if (value.name) {
282
+ filenamePart = `; filename="${value.name}"`;
283
+ }
284
+ controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
285
+ controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
286
+ controller.enqueue(Buffer.from(await value.arrayBuffer()));
287
+ }
288
+ else {
289
+ controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
290
+ controller.enqueue(Buffer.from(value));
291
+ }
292
+ if (entries.length === 0) {
293
+ controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
294
+ controller.close();
295
+ }
296
+ else {
297
+ controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
298
+ }
299
+ }
265
300
  }
301
+ });
302
+ }
303
+ }
304
+ function getNormalizedFile(name, blob, fileName) {
305
+ if (blob instanceof PonyfillFile) {
306
+ if (fileName != null) {
307
+ return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
266
308
  }
309
+ return blob;
267
310
  }
311
+ return new PonyfillFile([blob], fileName || name, { type: blob.type });
268
312
  }
269
313
 
270
314
  var BodyInitType;
@@ -284,81 +328,11 @@ class PonyfillBody {
284
328
  this._body = null;
285
329
  this.contentType = null;
286
330
  this.contentLength = null;
287
- if (this.bodyInit == null) {
288
- this._body = null;
289
- }
290
- else if (typeof this.bodyInit === 'string') {
291
- this.bodyType = BodyInitType.String;
292
- const buffer = Buffer.from(this.bodyInit);
293
- this.contentType = 'text/plain;charset=UTF-8';
294
- this.contentLength = buffer.length;
295
- this._body = new PonyfillReadableStream(Readable.from(buffer));
296
- }
297
- else if (this.bodyInit instanceof PonyfillReadableStream) {
298
- this.bodyType = BodyInitType.ReadableStream;
299
- this._body = this.bodyInit;
300
- }
301
- else if (this.bodyInit instanceof PonyfillBlob) {
302
- this.bodyType = BodyInitType.Blob;
303
- const blobStream = this.bodyInit.stream();
304
- this.contentType = this.bodyInit.type;
305
- this.contentLength = this.bodyInit.size;
306
- this._body = new PonyfillReadableStream(blobStream);
307
- }
308
- else if (this.bodyInit instanceof PonyfillFormData) {
309
- this.bodyType = BodyInitType.FormData;
310
- const boundary = Math.random().toString(36).substr(2);
311
- const formData = this.bodyInit;
312
- this.contentType = `multipart/form-data; boundary=${boundary}`;
313
- this._body = new PonyfillReadableStream({
314
- start: async (controller) => {
315
- controller.enqueue(Buffer.from(`--${boundary}\r\n`));
316
- const entries = [];
317
- formData.forEach((value, key) => {
318
- entries.push([key, value]);
319
- });
320
- for (const i in entries) {
321
- const [key, value] = entries[i];
322
- if (value instanceof PonyfillBlob) {
323
- let filenamePart = '';
324
- if (value.name) {
325
- filenamePart = `; filename="${value.name}"`;
326
- }
327
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
328
- controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
329
- controller.enqueue(Buffer.from(await value.arrayBuffer()));
330
- }
331
- else {
332
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
333
- controller.enqueue(Buffer.from(value));
334
- }
335
- if (Number(i) === entries.length - 1) {
336
- controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
337
- }
338
- else {
339
- controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
340
- }
341
- }
342
- controller.close();
343
- },
344
- });
345
- }
346
- else if ('buffer' in this.bodyInit) {
347
- this.contentLength = this.bodyInit.byteLength;
348
- this._body = new PonyfillReadableStream(Readable.from(this.bodyInit));
349
- }
350
- else if (this.bodyInit instanceof ArrayBuffer) {
351
- this.bodyType = BodyInitType.ArrayBuffer;
352
- this.contentLength = this.bodyInit.byteLength;
353
- this._body = new PonyfillReadableStream(Readable.from(Buffer.from(this.bodyInit)));
354
- }
355
- else if (this.bodyInit instanceof Readable) {
356
- this.bodyType = BodyInitType.Readable;
357
- this._body = new PonyfillReadableStream(this.bodyInit);
358
- }
359
- else {
360
- throw new Error('Unknown body type');
361
- }
331
+ const { body, contentType, contentLength, bodyType, } = processBodyInit(bodyInit);
332
+ this._body = body;
333
+ this.contentType = contentType;
334
+ this.contentLength = contentLength;
335
+ this.bodyType = bodyType;
362
336
  }
363
337
  get body() {
364
338
  if (this._body != null) {
@@ -404,7 +378,7 @@ class PonyfillBody {
404
378
  }
405
379
  return new PonyfillBlob(chunks);
406
380
  }
407
- formData() {
381
+ formData(opts) {
408
382
  if (this.bodyType === BodyInitType.FormData) {
409
383
  return Promise.resolve(this.bodyInit);
410
384
  }
@@ -412,7 +386,10 @@ class PonyfillBody {
412
386
  if (this._body == null) {
413
387
  return Promise.resolve(formData);
414
388
  }
415
- const formDataLimits = this.options.formDataLimits;
389
+ const formDataLimits = {
390
+ ...this.options.formDataLimits,
391
+ ...opts === null || opts === void 0 ? void 0 : opts.formDataLimits,
392
+ };
416
393
  return new Promise((resolve, reject) => {
417
394
  var _a;
418
395
  const bb = busboy({
@@ -477,6 +454,88 @@ class PonyfillBody {
477
454
  return blob.text();
478
455
  }
479
456
  }
457
+ function processBodyInit(bodyInit) {
458
+ if (bodyInit == null) {
459
+ return {
460
+ body: null,
461
+ contentType: null,
462
+ contentLength: null,
463
+ };
464
+ }
465
+ if (typeof bodyInit === 'string') {
466
+ const buffer = Buffer.from(bodyInit);
467
+ const readable = Readable.from(buffer);
468
+ const body = new PonyfillReadableStream(readable);
469
+ return {
470
+ bodyType: BodyInitType.String,
471
+ contentType: 'text/plain;charset=UTF-8',
472
+ contentLength: buffer.length,
473
+ body,
474
+ };
475
+ }
476
+ if (bodyInit instanceof PonyfillReadableStream) {
477
+ return {
478
+ bodyType: BodyInitType.ReadableStream,
479
+ body: bodyInit,
480
+ contentType: null,
481
+ contentLength: null,
482
+ };
483
+ }
484
+ if (bodyInit instanceof PonyfillBlob) {
485
+ const readable = bodyInit.stream();
486
+ const body = new PonyfillReadableStream(readable);
487
+ return {
488
+ bodyType: BodyInitType.Blob,
489
+ contentType: bodyInit.type,
490
+ contentLength: bodyInit.size,
491
+ body,
492
+ };
493
+ }
494
+ if (bodyInit instanceof PonyfillFormData) {
495
+ const boundary = Math.random().toString(36).substr(2);
496
+ const contentType = `multipart/form-data; boundary=${boundary}`;
497
+ const body = bodyInit.stream(boundary);
498
+ return {
499
+ bodyType: BodyInitType.FormData,
500
+ contentType,
501
+ contentLength: null,
502
+ body,
503
+ };
504
+ }
505
+ if ('buffer' in bodyInit) {
506
+ const contentLength = bodyInit.byteLength;
507
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
508
+ const readable = Readable.from(buffer);
509
+ const body = new PonyfillReadableStream(readable);
510
+ return {
511
+ contentLength,
512
+ contentType: null,
513
+ body,
514
+ };
515
+ }
516
+ if (bodyInit instanceof ArrayBuffer) {
517
+ const contentLength = bodyInit.byteLength;
518
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
519
+ const readable = Readable.from(buffer);
520
+ const body = new PonyfillReadableStream(readable);
521
+ return {
522
+ bodyType: BodyInitType.ArrayBuffer,
523
+ contentType: null,
524
+ contentLength,
525
+ body,
526
+ };
527
+ }
528
+ if (bodyInit instanceof Readable) {
529
+ const body = new PonyfillReadableStream(bodyInit);
530
+ return {
531
+ bodyType: BodyInitType.Readable,
532
+ contentType: null,
533
+ contentLength: null,
534
+ body,
535
+ };
536
+ }
537
+ throw new Error('Unknown body type');
538
+ }
480
539
 
481
540
  class PonyfillHeaders {
482
541
  constructor(headersInit) {
@@ -496,7 +555,7 @@ class PonyfillHeaders {
496
555
  }
497
556
  else if ('get' in headersInit) {
498
557
  headersInit.forEach((value, key) => {
499
- this.set(key, value);
558
+ this.append(key, value);
500
559
  });
501
560
  }
502
561
  else {
@@ -724,7 +783,8 @@ const fetchPonyfill = (info, init) => {
724
783
  // signal: fetchRequest.signal will be added when v14 reaches EOL
725
784
  method: fetchRequest.method,
726
785
  headers: nodeHeaders,
727
- }, nodeResponse => {
786
+ });
787
+ nodeRequest.once('response', nodeResponse => {
728
788
  if (nodeResponse.headers.location) {
729
789
  if (fetchRequest.redirect === 'error') {
730
790
  const redirectError = new Error('Redirects are not allowed');
@@ -752,7 +812,7 @@ const fetchPonyfill = (info, init) => {
752
812
  });
753
813
  resolve(ponyfillResponse);
754
814
  });
755
- nodeRequest.on('error', reject);
815
+ nodeRequest.once('error', reject);
756
816
  if (nodeReadable) {
757
817
  nodeReadable.pipe(nodeRequest);
758
818
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.1-alpha-20221228115139-6b81aa1",
3
+ "version": "0.0.1-alpha-20221230080146-72104f6",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
package/utils.d.ts CHANGED
@@ -1,2 +1 @@
1
- export declare function headersToJSON(headers: Headers): Record<string, string>;
2
1
  export declare function getHeadersObj(headers: Headers): Record<string, string>;