@whatwg-node/node-fetch 0.2.0-alpha-20230210105523-366e74c → 0.3.0

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/Blob.d.ts CHANGED
@@ -5,6 +5,7 @@ export declare class PonyfillBlob implements Blob {
5
5
  type: string;
6
6
  private encoding;
7
7
  constructor(blobParts: BlobPart[], options?: BlobOptions);
8
+ buffer(): Promise<Buffer>;
8
9
  arrayBuffer(): Promise<ArrayBuffer>;
9
10
  text(): Promise<string>;
10
11
  get size(): number;
package/Body.d.ts CHANGED
@@ -30,6 +30,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
30
30
  private generateBody;
31
31
  get body(): PonyfillReadableStream<Uint8Array> | null;
32
32
  arrayBuffer(): Promise<ArrayBuffer>;
33
+ _collectChunksFromReadable(): Promise<Uint8Array[]>;
33
34
  blob(): Promise<PonyfillBlob>;
34
35
  formData(opts?: {
35
36
  formDataLimits: FormDataLimits;
package/index.js CHANGED
@@ -209,7 +209,7 @@ function getHeadersObj(headers) {
209
209
  });
210
210
  return obj;
211
211
  }
212
- function uint8ArrayToBuffer(uint8array) {
212
+ function uint8ArrayToArrayBuffer(uint8array) {
213
213
  return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
214
214
  }
215
215
 
@@ -241,7 +241,7 @@ class PonyfillBlob {
241
241
  this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
242
242
  this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
243
243
  }
244
- async arrayBuffer() {
244
+ async buffer() {
245
245
  const bufferChunks = [];
246
246
  for (const blobPart of this.blobParts) {
247
247
  if (isBlob(blobPart)) {
@@ -254,7 +254,11 @@ class PonyfillBlob {
254
254
  bufferChunks.push(buf);
255
255
  }
256
256
  }
257
- return uint8ArrayToBuffer(Buffer.concat(bufferChunks));
257
+ return Buffer.concat(bufferChunks);
258
+ }
259
+ async arrayBuffer() {
260
+ const buffer = await this.buffer();
261
+ return uint8ArrayToArrayBuffer(buffer);
258
262
  }
259
263
  async text() {
260
264
  let text = '';
@@ -555,11 +559,30 @@ class PonyfillBody {
555
559
  }
556
560
  if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
557
561
  const typedBodyInit = this.bodyInit;
558
- return uint8ArrayToBuffer(typedBodyInit);
562
+ return uint8ArrayToArrayBuffer(typedBodyInit);
563
+ }
564
+ if (this.bodyType === BodyInitType.String) {
565
+ const buffer = Buffer.from(this.bodyInit);
566
+ return uint8ArrayToArrayBuffer(buffer);
567
+ }
568
+ if (this.bodyType === BodyInitType.Blob) {
569
+ const blob = this.bodyInit;
570
+ const arrayBuffer = await blob.arrayBuffer();
571
+ return arrayBuffer;
559
572
  }
560
573
  const blob = await this.blob();
561
574
  return blob.arrayBuffer();
562
575
  }
576
+ async _collectChunksFromReadable() {
577
+ const chunks = [];
578
+ const _body = this.generateBody();
579
+ if (_body) {
580
+ for await (const chunk of _body.readable) {
581
+ chunks.push(chunk);
582
+ }
583
+ }
584
+ return chunks;
585
+ }
563
586
  async blob() {
564
587
  if (this.bodyType === BodyInitType.Blob) {
565
588
  return this.bodyInit;
@@ -579,13 +602,7 @@ class PonyfillBody {
579
602
  type: this.contentType || '',
580
603
  });
581
604
  }
582
- const chunks = [];
583
- const _body = this.generateBody();
584
- if (_body) {
585
- for await (const chunk of _body.readable) {
586
- chunks.push(chunk);
587
- }
588
- }
605
+ const chunks = await this._collectChunksFromReadable();
589
606
  return new PonyfillBlob(chunks, {
590
607
  type: this.contentType || '',
591
608
  });
@@ -658,14 +675,24 @@ class PonyfillBody {
658
675
  if (this.bodyType === BodyInitType.Buffer) {
659
676
  return this.bodyInit;
660
677
  }
678
+ if (this.bodyType === BodyInitType.String) {
679
+ return Buffer.from(this.bodyInit);
680
+ }
661
681
  if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
662
682
  const bodyInitTyped = this.bodyInit;
663
683
  const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
664
684
  return buffer;
665
685
  }
666
- const blob = await this.blob();
667
- const arrayBuffer = await blob.arrayBuffer();
668
- return Buffer.from(arrayBuffer, undefined, arrayBuffer.byteLength);
686
+ if (this.bodyType === BodyInitType.Blob) {
687
+ if (this.bodyInit instanceof PonyfillBlob) {
688
+ return this.bodyInit.buffer();
689
+ }
690
+ const bodyInitTyped = this.bodyInit;
691
+ const buffer = Buffer.from(await bodyInitTyped.arrayBuffer(), undefined, bodyInitTyped.size);
692
+ return buffer;
693
+ }
694
+ const chunks = await this._collectChunksFromReadable();
695
+ return Buffer.concat(chunks);
669
696
  }
670
697
  async json() {
671
698
  const text = await this.text();
@@ -675,16 +702,8 @@ class PonyfillBody {
675
702
  if (this.bodyType === BodyInitType.String) {
676
703
  return this.bodyInit;
677
704
  }
678
- if (this.bodyType === BodyInitType.Buffer) {
679
- return this.bodyInit.toString('utf-8');
680
- }
681
- if (this.bodyType === BodyInitType.ArrayBuffer || this.bodyType === BodyInitType.Uint8Array) {
682
- const bodyInitTyped = this.bodyInit;
683
- const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
684
- return buffer.toString('utf-8');
685
- }
686
- const blob = await this.blob();
687
- return blob.text();
705
+ const buffer = await this.buffer();
706
+ return buffer.toString('utf-8');
688
707
  }
689
708
  }
690
709
  function processBodyInit(bodyInit) {
@@ -1157,14 +1176,16 @@ class PonyfillURLSearchParams {
1157
1176
  }
1158
1177
  }
1159
1178
  *entries() {
1160
- for (const key in this.keys()) {
1179
+ for (const key of this.keys()) {
1161
1180
  const value = this.params[key];
1162
1181
  if (Array.isArray(value)) {
1163
1182
  for (const item of value) {
1164
1183
  yield [key, item];
1165
1184
  }
1166
1185
  }
1167
- yield [key, value];
1186
+ else {
1187
+ yield [key, value];
1188
+ }
1168
1189
  }
1169
1190
  }
1170
1191
  *values() {
package/index.mjs CHANGED
@@ -203,7 +203,7 @@ function getHeadersObj(headers) {
203
203
  });
204
204
  return obj;
205
205
  }
206
- function uint8ArrayToBuffer(uint8array) {
206
+ function uint8ArrayToArrayBuffer(uint8array) {
207
207
  return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
208
208
  }
209
209
 
@@ -235,7 +235,7 @@ class PonyfillBlob {
235
235
  this.type = (options === null || options === void 0 ? void 0 : options.type) || 'application/octet-stream';
236
236
  this.encoding = (options === null || options === void 0 ? void 0 : options.encoding) || 'utf8';
237
237
  }
238
- async arrayBuffer() {
238
+ async buffer() {
239
239
  const bufferChunks = [];
240
240
  for (const blobPart of this.blobParts) {
241
241
  if (isBlob(blobPart)) {
@@ -248,7 +248,11 @@ class PonyfillBlob {
248
248
  bufferChunks.push(buf);
249
249
  }
250
250
  }
251
- return uint8ArrayToBuffer(Buffer.concat(bufferChunks));
251
+ return Buffer.concat(bufferChunks);
252
+ }
253
+ async arrayBuffer() {
254
+ const buffer = await this.buffer();
255
+ return uint8ArrayToArrayBuffer(buffer);
252
256
  }
253
257
  async text() {
254
258
  let text = '';
@@ -549,11 +553,30 @@ class PonyfillBody {
549
553
  }
550
554
  if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
551
555
  const typedBodyInit = this.bodyInit;
552
- return uint8ArrayToBuffer(typedBodyInit);
556
+ return uint8ArrayToArrayBuffer(typedBodyInit);
557
+ }
558
+ if (this.bodyType === BodyInitType.String) {
559
+ const buffer = Buffer.from(this.bodyInit);
560
+ return uint8ArrayToArrayBuffer(buffer);
561
+ }
562
+ if (this.bodyType === BodyInitType.Blob) {
563
+ const blob = this.bodyInit;
564
+ const arrayBuffer = await blob.arrayBuffer();
565
+ return arrayBuffer;
553
566
  }
554
567
  const blob = await this.blob();
555
568
  return blob.arrayBuffer();
556
569
  }
570
+ async _collectChunksFromReadable() {
571
+ const chunks = [];
572
+ const _body = this.generateBody();
573
+ if (_body) {
574
+ for await (const chunk of _body.readable) {
575
+ chunks.push(chunk);
576
+ }
577
+ }
578
+ return chunks;
579
+ }
557
580
  async blob() {
558
581
  if (this.bodyType === BodyInitType.Blob) {
559
582
  return this.bodyInit;
@@ -573,13 +596,7 @@ class PonyfillBody {
573
596
  type: this.contentType || '',
574
597
  });
575
598
  }
576
- const chunks = [];
577
- const _body = this.generateBody();
578
- if (_body) {
579
- for await (const chunk of _body.readable) {
580
- chunks.push(chunk);
581
- }
582
- }
599
+ const chunks = await this._collectChunksFromReadable();
583
600
  return new PonyfillBlob(chunks, {
584
601
  type: this.contentType || '',
585
602
  });
@@ -652,14 +669,24 @@ class PonyfillBody {
652
669
  if (this.bodyType === BodyInitType.Buffer) {
653
670
  return this.bodyInit;
654
671
  }
672
+ if (this.bodyType === BodyInitType.String) {
673
+ return Buffer.from(this.bodyInit);
674
+ }
655
675
  if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
656
676
  const bodyInitTyped = this.bodyInit;
657
677
  const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
658
678
  return buffer;
659
679
  }
660
- const blob = await this.blob();
661
- const arrayBuffer = await blob.arrayBuffer();
662
- return Buffer.from(arrayBuffer, undefined, arrayBuffer.byteLength);
680
+ if (this.bodyType === BodyInitType.Blob) {
681
+ if (this.bodyInit instanceof PonyfillBlob) {
682
+ return this.bodyInit.buffer();
683
+ }
684
+ const bodyInitTyped = this.bodyInit;
685
+ const buffer = Buffer.from(await bodyInitTyped.arrayBuffer(), undefined, bodyInitTyped.size);
686
+ return buffer;
687
+ }
688
+ const chunks = await this._collectChunksFromReadable();
689
+ return Buffer.concat(chunks);
663
690
  }
664
691
  async json() {
665
692
  const text = await this.text();
@@ -669,16 +696,8 @@ class PonyfillBody {
669
696
  if (this.bodyType === BodyInitType.String) {
670
697
  return this.bodyInit;
671
698
  }
672
- if (this.bodyType === BodyInitType.Buffer) {
673
- return this.bodyInit.toString('utf-8');
674
- }
675
- if (this.bodyType === BodyInitType.ArrayBuffer || this.bodyType === BodyInitType.Uint8Array) {
676
- const bodyInitTyped = this.bodyInit;
677
- const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
678
- return buffer.toString('utf-8');
679
- }
680
- const blob = await this.blob();
681
- return blob.text();
699
+ const buffer = await this.buffer();
700
+ return buffer.toString('utf-8');
682
701
  }
683
702
  }
684
703
  function processBodyInit(bodyInit) {
@@ -1151,14 +1170,16 @@ class PonyfillURLSearchParams {
1151
1170
  }
1152
1171
  }
1153
1172
  *entries() {
1154
- for (const key in this.keys()) {
1173
+ for (const key of this.keys()) {
1155
1174
  const value = this.params[key];
1156
1175
  if (Array.isArray(value)) {
1157
1176
  for (const item of value) {
1158
1177
  yield [key, item];
1159
1178
  }
1160
1179
  }
1161
- yield [key, value];
1180
+ else {
1181
+ yield [key, value];
1182
+ }
1162
1183
  }
1163
1184
  }
1164
1185
  *values() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.2.0-alpha-20230210105523-366e74c",
3
+ "version": "0.3.0",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
package/utils.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
2
- export declare function uint8ArrayToBuffer(uint8array: Uint8Array): ArrayBuffer;
2
+ export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;