@whatwg-node/node-fetch 0.0.1-alpha-20221228115139-6b81aa1 → 0.0.1-alpha-20221230075257-cc3a93b

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