@whatwg-node/node-fetch 0.0.3 → 0.0.5-alpha-20230206135508-d9b0cd5

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
@@ -4,7 +4,7 @@ import { Readable } from 'stream';
4
4
  import { PonyfillBlob } from './Blob';
5
5
  import { PonyfillFormData } from './FormData';
6
6
  import { PonyfillReadableStream } from './ReadableStream';
7
- export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
7
+ export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array> | AsyncIterable<Uint8Array>;
8
8
  export interface FormDataLimits {
9
9
  fieldNameSize?: number;
10
10
  fieldSize?: number;
package/FormData.d.ts CHANGED
@@ -10,5 +10,5 @@ export declare class PonyfillFormData implements FormData {
10
10
  set(name: string, value: PonyfillBlob | string, fileName?: string): void;
11
11
  [Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
12
12
  forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
13
- stream(boundary?: string): PonyfillReadableStream<Uint8Array>;
14
13
  }
14
+ export declare function getStreamFromFormData(formData: FormData, boundary?: string): PonyfillReadableStream<Uint8Array>;
package/index.js CHANGED
@@ -32,6 +32,7 @@ class PonyfillAbortError extends Error {
32
32
 
33
33
  function createController(desiredSize, readable) {
34
34
  let chunks = [];
35
+ let _closed = false;
35
36
  return {
36
37
  desiredSize,
37
38
  enqueue(chunk) {
@@ -42,6 +43,7 @@ function createController(desiredSize, readable) {
42
43
  this._flush();
43
44
  }
44
45
  readable.push(null);
46
+ _closed = true;
45
47
  },
46
48
  error(error) {
47
49
  if (chunks.length > 0) {
@@ -49,6 +51,9 @@ function createController(desiredSize, readable) {
49
51
  }
50
52
  readable.destroy(error);
51
53
  },
54
+ get _closed() {
55
+ return _closed;
56
+ },
52
57
  _flush() {
53
58
  if (chunks.length > 0) {
54
59
  const concatenated = Buffer.concat(chunks);
@@ -105,7 +110,9 @@ class PonyfillReadableStream {
105
110
  started = true;
106
111
  await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
107
112
  }
108
- await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
113
+ if (!controller._closed) {
114
+ await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
115
+ }
109
116
  controller._flush();
110
117
  },
111
118
  async destroy(err, callback) {
@@ -292,47 +299,58 @@ class PonyfillFormData {
292
299
  callback(value, key, this);
293
300
  }
294
301
  }
295
- stream(boundary = '---') {
296
- const entries = [];
297
- return new PonyfillReadableStream({
298
- start: async (controller) => {
299
- controller.enqueue(Buffer.from(`--${boundary}\r\n`));
300
- this.forEach((value, key) => {
301
- entries.push([key, value]);
302
- });
303
- },
304
- pull: async (controller) => {
305
- const entry = entries.shift();
306
- if (entry) {
307
- const [key, value] = entry;
308
- if (value instanceof PonyfillFile) {
309
- let filenamePart = '';
310
- if (value.name) {
311
- filenamePart = `; filename="${value.name}"`;
312
- }
313
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
314
- controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
315
- controller.enqueue(Buffer.from(await value.arrayBuffer()));
316
- }
317
- else {
318
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
319
- controller.enqueue(Buffer.from(value));
320
- }
321
- if (entries.length === 0) {
322
- controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
323
- controller.close();
302
+ }
303
+ function getStreamFromFormData(formData, boundary = '---') {
304
+ const entries = [];
305
+ let sentInitialHeader = false;
306
+ return new PonyfillReadableStream({
307
+ start: controller => {
308
+ formData.forEach((value, key) => {
309
+ if (!sentInitialHeader) {
310
+ controller.enqueue(Buffer.from(`--${boundary}\r\n`));
311
+ sentInitialHeader = true;
312
+ }
313
+ entries.push([key, value]);
314
+ });
315
+ if (!sentInitialHeader) {
316
+ controller.enqueue(Buffer.from(`--${boundary}--\r\n`));
317
+ controller.close();
318
+ }
319
+ },
320
+ pull: async (controller) => {
321
+ const entry = entries.shift();
322
+ if (entry) {
323
+ const [key, value] = entry;
324
+ if (typeof value === 'string') {
325
+ controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
326
+ controller.enqueue(Buffer.from(value));
327
+ }
328
+ else {
329
+ let filenamePart = '';
330
+ if (value.name) {
331
+ filenamePart = `; filename="${value.name}"`;
324
332
  }
325
- else {
326
- controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
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
+ const entryStream = value.stream();
336
+ for await (const chunk of entryStream) {
337
+ controller.enqueue(chunk);
327
338
  }
328
339
  }
329
- else {
340
+ if (entries.length === 0) {
330
341
  controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
331
342
  controller.close();
332
343
  }
333
- },
334
- });
335
- }
344
+ else {
345
+ controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
346
+ }
347
+ }
348
+ else {
349
+ controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
350
+ controller.close();
351
+ }
352
+ },
353
+ });
336
354
  }
337
355
  function getNormalizedFile(name, blob, fileName) {
338
356
  if (blob instanceof PonyfillFile) {
@@ -559,10 +577,9 @@ function processBodyInit(bodyInit) {
559
577
  return {
560
578
  bodyType: BodyInitType.String,
561
579
  contentType: 'text/plain;charset=UTF-8',
562
- contentLength: bodyInit.length,
580
+ contentLength: Buffer.byteLength(bodyInit),
563
581
  bodyFactory() {
564
- const buffer = Buffer.from(bodyInit);
565
- const readable = stream.Readable.from(buffer);
582
+ const readable = stream.Readable.from(bodyInit);
566
583
  return new PonyfillReadableStream(readable);
567
584
  },
568
585
  };
@@ -585,16 +602,6 @@ function processBodyInit(bodyInit) {
585
602
  },
586
603
  };
587
604
  }
588
- if (bodyInit instanceof PonyfillFormData) {
589
- const boundary = Math.random().toString(36).substr(2);
590
- const contentType = `multipart/form-data; boundary=${boundary}`;
591
- return {
592
- bodyType: BodyInitType.FormData,
593
- contentType,
594
- contentLength: null,
595
- bodyFactory: () => bodyInit.stream(boundary),
596
- };
597
- }
598
605
  if (bodyInit instanceof Buffer) {
599
606
  const contentLength = bodyInit.length;
600
607
  return {
@@ -689,12 +696,17 @@ function processBodyInit(bodyInit) {
689
696
  contentType,
690
697
  contentLength: null,
691
698
  bodyFactory() {
692
- const formData = new PonyfillFormData();
693
- bodyInit.forEach((value, key) => {
694
- formData.append(key, value);
695
- });
696
- const body = formData.stream(boundary);
697
- return body;
699
+ return getStreamFromFormData(bodyInit, boundary);
700
+ },
701
+ };
702
+ }
703
+ if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
704
+ return {
705
+ contentType: null,
706
+ contentLength: null,
707
+ bodyFactory() {
708
+ const readable = stream.Readable.from(bodyInit);
709
+ return new PonyfillReadableStream(readable);
698
710
  },
699
711
  };
700
712
  }
package/index.mjs CHANGED
@@ -26,6 +26,7 @@ class PonyfillAbortError extends Error {
26
26
 
27
27
  function createController(desiredSize, readable) {
28
28
  let chunks = [];
29
+ let _closed = false;
29
30
  return {
30
31
  desiredSize,
31
32
  enqueue(chunk) {
@@ -36,6 +37,7 @@ function createController(desiredSize, readable) {
36
37
  this._flush();
37
38
  }
38
39
  readable.push(null);
40
+ _closed = true;
39
41
  },
40
42
  error(error) {
41
43
  if (chunks.length > 0) {
@@ -43,6 +45,9 @@ function createController(desiredSize, readable) {
43
45
  }
44
46
  readable.destroy(error);
45
47
  },
48
+ get _closed() {
49
+ return _closed;
50
+ },
46
51
  _flush() {
47
52
  if (chunks.length > 0) {
48
53
  const concatenated = Buffer.concat(chunks);
@@ -99,7 +104,9 @@ class PonyfillReadableStream {
99
104
  started = true;
100
105
  await ((_a = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.start) === null || _a === void 0 ? void 0 : _a.call(underlyingSource, controller));
101
106
  }
102
- await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
107
+ if (!controller._closed) {
108
+ await ((_b = underlyingSource === null || underlyingSource === void 0 ? void 0 : underlyingSource.pull) === null || _b === void 0 ? void 0 : _b.call(underlyingSource, controller));
109
+ }
103
110
  controller._flush();
104
111
  },
105
112
  async destroy(err, callback) {
@@ -286,47 +293,58 @@ class PonyfillFormData {
286
293
  callback(value, key, this);
287
294
  }
288
295
  }
289
- stream(boundary = '---') {
290
- const entries = [];
291
- return new PonyfillReadableStream({
292
- start: async (controller) => {
293
- controller.enqueue(Buffer.from(`--${boundary}\r\n`));
294
- this.forEach((value, key) => {
295
- entries.push([key, value]);
296
- });
297
- },
298
- pull: async (controller) => {
299
- const entry = entries.shift();
300
- if (entry) {
301
- const [key, value] = entry;
302
- if (value instanceof PonyfillFile) {
303
- let filenamePart = '';
304
- if (value.name) {
305
- filenamePart = `; filename="${value.name}"`;
306
- }
307
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"${filenamePart}\r\n`));
308
- controller.enqueue(Buffer.from(`Content-Type: ${value.type || 'application/octet-stream'}\r\n\r\n`));
309
- controller.enqueue(Buffer.from(await value.arrayBuffer()));
310
- }
311
- else {
312
- controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
313
- controller.enqueue(Buffer.from(value));
314
- }
315
- if (entries.length === 0) {
316
- controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
317
- controller.close();
296
+ }
297
+ function getStreamFromFormData(formData, boundary = '---') {
298
+ const entries = [];
299
+ let sentInitialHeader = false;
300
+ return new PonyfillReadableStream({
301
+ start: controller => {
302
+ formData.forEach((value, key) => {
303
+ if (!sentInitialHeader) {
304
+ controller.enqueue(Buffer.from(`--${boundary}\r\n`));
305
+ sentInitialHeader = true;
306
+ }
307
+ entries.push([key, value]);
308
+ });
309
+ if (!sentInitialHeader) {
310
+ controller.enqueue(Buffer.from(`--${boundary}--\r\n`));
311
+ controller.close();
312
+ }
313
+ },
314
+ pull: async (controller) => {
315
+ const entry = entries.shift();
316
+ if (entry) {
317
+ const [key, value] = entry;
318
+ if (typeof value === 'string') {
319
+ controller.enqueue(Buffer.from(`Content-Disposition: form-data; name="${key}"\r\n\r\n`));
320
+ controller.enqueue(Buffer.from(value));
321
+ }
322
+ else {
323
+ let filenamePart = '';
324
+ if (value.name) {
325
+ filenamePart = `; filename="${value.name}"`;
318
326
  }
319
- else {
320
- controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
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
+ const entryStream = value.stream();
330
+ for await (const chunk of entryStream) {
331
+ controller.enqueue(chunk);
321
332
  }
322
333
  }
323
- else {
334
+ if (entries.length === 0) {
324
335
  controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
325
336
  controller.close();
326
337
  }
327
- },
328
- });
329
- }
338
+ else {
339
+ controller.enqueue(Buffer.from(`\r\n--${boundary}\r\n`));
340
+ }
341
+ }
342
+ else {
343
+ controller.enqueue(Buffer.from(`\r\n--${boundary}--\r\n`));
344
+ controller.close();
345
+ }
346
+ },
347
+ });
330
348
  }
331
349
  function getNormalizedFile(name, blob, fileName) {
332
350
  if (blob instanceof PonyfillFile) {
@@ -553,10 +571,9 @@ function processBodyInit(bodyInit) {
553
571
  return {
554
572
  bodyType: BodyInitType.String,
555
573
  contentType: 'text/plain;charset=UTF-8',
556
- contentLength: bodyInit.length,
574
+ contentLength: Buffer.byteLength(bodyInit),
557
575
  bodyFactory() {
558
- const buffer = Buffer.from(bodyInit);
559
- const readable = Readable.from(buffer);
576
+ const readable = Readable.from(bodyInit);
560
577
  return new PonyfillReadableStream(readable);
561
578
  },
562
579
  };
@@ -579,16 +596,6 @@ function processBodyInit(bodyInit) {
579
596
  },
580
597
  };
581
598
  }
582
- if (bodyInit instanceof PonyfillFormData) {
583
- const boundary = Math.random().toString(36).substr(2);
584
- const contentType = `multipart/form-data; boundary=${boundary}`;
585
- return {
586
- bodyType: BodyInitType.FormData,
587
- contentType,
588
- contentLength: null,
589
- bodyFactory: () => bodyInit.stream(boundary),
590
- };
591
- }
592
599
  if (bodyInit instanceof Buffer) {
593
600
  const contentLength = bodyInit.length;
594
601
  return {
@@ -683,12 +690,17 @@ function processBodyInit(bodyInit) {
683
690
  contentType,
684
691
  contentLength: null,
685
692
  bodyFactory() {
686
- const formData = new PonyfillFormData();
687
- bodyInit.forEach((value, key) => {
688
- formData.append(key, value);
689
- });
690
- const body = formData.stream(boundary);
691
- return body;
693
+ return getStreamFromFormData(bodyInit, boundary);
694
+ },
695
+ };
696
+ }
697
+ if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
698
+ return {
699
+ contentType: null,
700
+ contentLength: null,
701
+ bodyFactory() {
702
+ const readable = Readable.from(bodyInit);
703
+ return new PonyfillReadableStream(readable);
692
704
  },
693
705
  };
694
706
  }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.3",
3
+ "version": "0.0.5-alpha-20230206135508-d9b0cd5",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
7
  "@types/node": "^18.0.6"
8
8
  },
9
9
  "dependencies": {
10
- "@whatwg-node/events": "0.0.2",
11
- "busboy": "1.6.0",
10
+ "@whatwg-node/events": "^0.0.2",
11
+ "busboy": "^1.6.0",
12
12
  "tslib": "^2.3.1"
13
13
  },
14
14
  "repository": {