@whatwg-node/node-fetch 0.0.4 → 0.0.5-alpha-20230206135558-1370a81

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) {
@@ -584,16 +602,6 @@ function processBodyInit(bodyInit) {
584
602
  },
585
603
  };
586
604
  }
587
- if (bodyInit instanceof PonyfillFormData) {
588
- const boundary = Math.random().toString(36).substr(2);
589
- const contentType = `multipart/form-data; boundary=${boundary}`;
590
- return {
591
- bodyType: BodyInitType.FormData,
592
- contentType,
593
- contentLength: null,
594
- bodyFactory: () => bodyInit.stream(boundary),
595
- };
596
- }
597
605
  if (bodyInit instanceof Buffer) {
598
606
  const contentLength = bodyInit.length;
599
607
  return {
@@ -688,12 +696,17 @@ function processBodyInit(bodyInit) {
688
696
  contentType,
689
697
  contentLength: null,
690
698
  bodyFactory() {
691
- const formData = new PonyfillFormData();
692
- bodyInit.forEach((value, key) => {
693
- formData.append(key, value);
694
- });
695
- const body = formData.stream(boundary);
696
- 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);
697
710
  },
698
711
  };
699
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) {
@@ -578,16 +596,6 @@ function processBodyInit(bodyInit) {
578
596
  },
579
597
  };
580
598
  }
581
- if (bodyInit instanceof PonyfillFormData) {
582
- const boundary = Math.random().toString(36).substr(2);
583
- const contentType = `multipart/form-data; boundary=${boundary}`;
584
- return {
585
- bodyType: BodyInitType.FormData,
586
- contentType,
587
- contentLength: null,
588
- bodyFactory: () => bodyInit.stream(boundary),
589
- };
590
- }
591
599
  if (bodyInit instanceof Buffer) {
592
600
  const contentLength = bodyInit.length;
593
601
  return {
@@ -682,12 +690,17 @@ function processBodyInit(bodyInit) {
682
690
  contentType,
683
691
  contentLength: null,
684
692
  bodyFactory() {
685
- const formData = new PonyfillFormData();
686
- bodyInit.forEach((value, key) => {
687
- formData.append(key, value);
688
- });
689
- const body = formData.stream(boundary);
690
- 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);
691
704
  },
692
705
  };
693
706
  }
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.4",
3
+ "version": "0.0.5-alpha-20230206135558-1370a81",
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": {