@whatwg-node/node-fetch 0.7.21 → 0.7.22-alpha-20250526163200-dc90c3024c7fa99ad5eb4e46eeee8988f7e003df

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/cjs/Body.js CHANGED
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillBody = void 0;
4
4
  /* eslint-disable @typescript-eslint/ban-ts-comment */
5
5
  const node_buffer_1 = require("node:buffer");
6
- const node_http_1 = require("node:http");
7
6
  const node_stream_1 = require("node:stream");
8
7
  const busboy_1 = require("@fastify/busboy");
9
8
  const promise_helpers_1 = require("@whatwg-node/promise-helpers");
@@ -33,7 +32,7 @@ class PonyfillBody {
33
32
  this.bodyInit = bodyInit;
34
33
  this.options = options;
35
34
  this._signal = options.signal || null;
36
- const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit, options?.signal);
35
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
37
36
  this._bodyFactory = bodyFactory;
38
37
  this.contentType = contentType;
39
38
  this.contentLength = contentLength;
@@ -109,14 +108,14 @@ class PonyfillBody {
109
108
  _doCollectChunksFromReadableJob() {
110
109
  if (this.bodyType === BodyInitType.AsyncIterable) {
111
110
  if (Array.fromAsync) {
112
- return (0, promise_helpers_1.handleMaybePromise)(() => Array.fromAsync(this.bodyInit), chunks => {
111
+ return Array.fromAsync(this.bodyInit).then(chunks => {
113
112
  this._chunks = chunks;
114
113
  return this._chunks;
115
114
  });
116
115
  }
117
116
  const iterator = this.bodyInit[Symbol.asyncIterator]();
118
117
  const chunks = [];
119
- const collectValue = () => (0, promise_helpers_1.handleMaybePromise)(() => iterator.next(), ({ value, done }) => {
118
+ const collectValue = () => iterator.next().then(({ value, done }) => {
120
119
  if (value) {
121
120
  chunks.push(value);
122
121
  }
@@ -131,17 +130,30 @@ class PonyfillBody {
131
130
  const _body = this.generateBody();
132
131
  if (!_body) {
133
132
  this._chunks = [];
134
- return (0, utils_js_1.fakePromise)(this._chunks);
133
+ return this._chunks;
135
134
  }
136
- return _body.readable.toArray().then(chunks => {
137
- this._chunks = chunks;
135
+ if (_body.readable.destroyed) {
136
+ // If the stream is already destroyed, we can resolve immediately
137
+ this._chunks = [];
138
138
  return this._chunks;
139
+ }
140
+ const chunks = [];
141
+ const deferred = (0, promise_helpers_1.createDeferredPromise)();
142
+ _body.readable.on('data', function nextChunk(chunk) {
143
+ chunks.push(chunk);
144
+ });
145
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
146
+ const _this = this;
147
+ _body.readable.once('end', function endOfStream() {
148
+ _this._chunks = chunks;
149
+ deferred.resolve(chunks);
150
+ });
151
+ _body.readable.once('error', function errorOnStream(err) {
152
+ deferred.reject(err);
139
153
  });
154
+ return deferred.promise;
140
155
  }
141
156
  _collectChunksFromReadable() {
142
- if (this._chunks) {
143
- return (0, utils_js_1.fakePromise)(this._chunks);
144
- }
145
157
  this._chunks ||= this._doCollectChunksFromReadableJob();
146
158
  return this._chunks;
147
159
  }
@@ -168,13 +180,15 @@ class PonyfillBody {
168
180
  });
169
181
  return (0, utils_js_1.fakePromise)(this._blob);
170
182
  }
171
- return (0, utils_js_1.fakePromise)((0, promise_helpers_1.handleMaybePromise)(() => this._collectChunksFromReadable(), chunks => {
183
+ return (0, utils_js_1.fakePromise)()
184
+ .then(() => this._collectChunksFromReadable())
185
+ .then(chunks => {
172
186
  this._blob = new Blob_js_1.PonyfillBlob(chunks, {
173
187
  type: this.contentType || '',
174
188
  size: this.contentLength,
175
189
  });
176
190
  return this._blob;
177
- }));
191
+ });
178
192
  }
179
193
  _formData = null;
180
194
  formData(opts) {
@@ -317,14 +331,16 @@ class PonyfillBody {
317
331
  });
318
332
  }
319
333
  }
320
- return (0, utils_js_1.fakePromise)((0, promise_helpers_1.handleMaybePromise)(() => this._collectChunksFromReadable(), chunks => {
334
+ return (0, utils_js_1.fakePromise)()
335
+ .then(() => this._collectChunksFromReadable())
336
+ .then(chunks => {
321
337
  if (chunks.length === 1) {
322
338
  this._buffer = chunks[0];
323
339
  return this._buffer;
324
340
  }
325
341
  this._buffer = node_buffer_1.Buffer.concat(chunks);
326
342
  return this._buffer;
327
- }));
343
+ });
328
344
  }
329
345
  bytes() {
330
346
  return this.buffer();
@@ -367,7 +383,7 @@ class PonyfillBody {
367
383
  }
368
384
  }
369
385
  exports.PonyfillBody = PonyfillBody;
370
- function processBodyInit(bodyInit, signal) {
386
+ function processBodyInit(bodyInit) {
371
387
  if (bodyInit == null) {
372
388
  return {
373
389
  bodyFactory: () => null,
@@ -450,20 +466,6 @@ function processBodyInit(bodyInit, signal) {
450
466
  },
451
467
  };
452
468
  }
453
- if (bodyInit instanceof node_http_1.IncomingMessage) {
454
- const passThrough = (0, utils_js_1.wrapIncomingMessageWithPassthrough)({
455
- incomingMessage: bodyInit,
456
- signal,
457
- });
458
- return {
459
- bodyType: BodyInitType.Readable,
460
- contentType: null,
461
- contentLength: null,
462
- bodyFactory() {
463
- return new ReadableStream_js_1.PonyfillReadableStream(passThrough);
464
- },
465
- };
466
- }
467
469
  if (bodyInit instanceof node_stream_1.Readable) {
468
470
  return {
469
471
  bodyType: BodyInitType.Readable,
@@ -118,7 +118,7 @@ function fetchNodeHttp(fetchRequest) {
118
118
  resolve(ponyfillResponse);
119
119
  });
120
120
  if (fetchRequest['_buffer'] != null) {
121
- (0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.safeWrite)(fetchRequest['_buffer'], nodeRequest, signal), () => (0, utils_js_1.endStream)(nodeRequest), reject);
121
+ (0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.safeWrite)(fetchRequest['_buffer'], nodeRequest), () => (0, utils_js_1.endStream)(nodeRequest), reject);
122
122
  }
123
123
  else {
124
124
  const nodeReadable = (fetchRequest.body != null
package/cjs/utils.js CHANGED
@@ -70,11 +70,9 @@ function endStream(stream) {
70
70
  // @ts-expect-error Avoid arguments adaptor trampoline https://v8.dev/blog/adaptor-frame
71
71
  return stream.end(null, null, null);
72
72
  }
73
- function safeWrite(chunk, stream, signal) {
73
+ function safeWrite(chunk, stream) {
74
74
  const result = stream.write(chunk);
75
75
  if (!result) {
76
- return (0, node_events_1.once)(stream, 'drain', {
77
- signal,
78
- });
76
+ return (0, node_events_1.once)(stream, 'drain');
79
77
  }
80
78
  }
package/esm/Body.js CHANGED
@@ -1,14 +1,13 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment */
2
2
  import { Buffer } from 'node:buffer';
3
- import { IncomingMessage } from 'node:http';
4
3
  import { addAbortSignal, Readable } from 'node:stream';
5
4
  import { Busboy } from '@fastify/busboy';
6
- import { handleMaybePromise } from '@whatwg-node/promise-helpers';
5
+ import { createDeferredPromise } from '@whatwg-node/promise-helpers';
7
6
  import { hasArrayBufferMethod, hasBufferMethod, hasBytesMethod, PonyfillBlob } from './Blob.js';
8
7
  import { PonyfillFile } from './File.js';
9
8
  import { getStreamFromFormData, PonyfillFormData } from './FormData.js';
10
9
  import { PonyfillReadableStream } from './ReadableStream.js';
11
- import { fakePromise, isArrayBufferView, wrapIncomingMessageWithPassthrough } from './utils.js';
10
+ import { fakePromise, isArrayBufferView } from './utils.js';
12
11
  var BodyInitType;
13
12
  (function (BodyInitType) {
14
13
  BodyInitType["ReadableStream"] = "ReadableStream";
@@ -30,7 +29,7 @@ export class PonyfillBody {
30
29
  this.bodyInit = bodyInit;
31
30
  this.options = options;
32
31
  this._signal = options.signal || null;
33
- const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit, options?.signal);
32
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
34
33
  this._bodyFactory = bodyFactory;
35
34
  this.contentType = contentType;
36
35
  this.contentLength = contentLength;
@@ -106,14 +105,14 @@ export class PonyfillBody {
106
105
  _doCollectChunksFromReadableJob() {
107
106
  if (this.bodyType === BodyInitType.AsyncIterable) {
108
107
  if (Array.fromAsync) {
109
- return handleMaybePromise(() => Array.fromAsync(this.bodyInit), chunks => {
108
+ return Array.fromAsync(this.bodyInit).then(chunks => {
110
109
  this._chunks = chunks;
111
110
  return this._chunks;
112
111
  });
113
112
  }
114
113
  const iterator = this.bodyInit[Symbol.asyncIterator]();
115
114
  const chunks = [];
116
- const collectValue = () => handleMaybePromise(() => iterator.next(), ({ value, done }) => {
115
+ const collectValue = () => iterator.next().then(({ value, done }) => {
117
116
  if (value) {
118
117
  chunks.push(value);
119
118
  }
@@ -128,17 +127,30 @@ export class PonyfillBody {
128
127
  const _body = this.generateBody();
129
128
  if (!_body) {
130
129
  this._chunks = [];
131
- return fakePromise(this._chunks);
130
+ return this._chunks;
132
131
  }
133
- return _body.readable.toArray().then(chunks => {
134
- this._chunks = chunks;
132
+ if (_body.readable.destroyed) {
133
+ // If the stream is already destroyed, we can resolve immediately
134
+ this._chunks = [];
135
135
  return this._chunks;
136
+ }
137
+ const chunks = [];
138
+ const deferred = createDeferredPromise();
139
+ _body.readable.on('data', function nextChunk(chunk) {
140
+ chunks.push(chunk);
141
+ });
142
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
143
+ const _this = this;
144
+ _body.readable.once('end', function endOfStream() {
145
+ _this._chunks = chunks;
146
+ deferred.resolve(chunks);
147
+ });
148
+ _body.readable.once('error', function errorOnStream(err) {
149
+ deferred.reject(err);
136
150
  });
151
+ return deferred.promise;
137
152
  }
138
153
  _collectChunksFromReadable() {
139
- if (this._chunks) {
140
- return fakePromise(this._chunks);
141
- }
142
154
  this._chunks ||= this._doCollectChunksFromReadableJob();
143
155
  return this._chunks;
144
156
  }
@@ -165,13 +177,15 @@ export class PonyfillBody {
165
177
  });
166
178
  return fakePromise(this._blob);
167
179
  }
168
- return fakePromise(handleMaybePromise(() => this._collectChunksFromReadable(), chunks => {
180
+ return fakePromise()
181
+ .then(() => this._collectChunksFromReadable())
182
+ .then(chunks => {
169
183
  this._blob = new PonyfillBlob(chunks, {
170
184
  type: this.contentType || '',
171
185
  size: this.contentLength,
172
186
  });
173
187
  return this._blob;
174
- }));
188
+ });
175
189
  }
176
190
  _formData = null;
177
191
  formData(opts) {
@@ -314,14 +328,16 @@ export class PonyfillBody {
314
328
  });
315
329
  }
316
330
  }
317
- return fakePromise(handleMaybePromise(() => this._collectChunksFromReadable(), chunks => {
331
+ return fakePromise()
332
+ .then(() => this._collectChunksFromReadable())
333
+ .then(chunks => {
318
334
  if (chunks.length === 1) {
319
335
  this._buffer = chunks[0];
320
336
  return this._buffer;
321
337
  }
322
338
  this._buffer = Buffer.concat(chunks);
323
339
  return this._buffer;
324
- }));
340
+ });
325
341
  }
326
342
  bytes() {
327
343
  return this.buffer();
@@ -363,7 +379,7 @@ export class PonyfillBody {
363
379
  });
364
380
  }
365
381
  }
366
- function processBodyInit(bodyInit, signal) {
382
+ function processBodyInit(bodyInit) {
367
383
  if (bodyInit == null) {
368
384
  return {
369
385
  bodyFactory: () => null,
@@ -446,20 +462,6 @@ function processBodyInit(bodyInit, signal) {
446
462
  },
447
463
  };
448
464
  }
449
- if (bodyInit instanceof IncomingMessage) {
450
- const passThrough = wrapIncomingMessageWithPassthrough({
451
- incomingMessage: bodyInit,
452
- signal,
453
- });
454
- return {
455
- bodyType: BodyInitType.Readable,
456
- contentType: null,
457
- contentLength: null,
458
- bodyFactory() {
459
- return new PonyfillReadableStream(passThrough);
460
- },
461
- };
462
- }
463
465
  if (bodyInit instanceof Readable) {
464
466
  return {
465
467
  bodyType: BodyInitType.Readable,
@@ -115,7 +115,7 @@ export function fetchNodeHttp(fetchRequest) {
115
115
  resolve(ponyfillResponse);
116
116
  });
117
117
  if (fetchRequest['_buffer'] != null) {
118
- handleMaybePromise(() => safeWrite(fetchRequest['_buffer'], nodeRequest, signal), () => endStream(nodeRequest), reject);
118
+ handleMaybePromise(() => safeWrite(fetchRequest['_buffer'], nodeRequest), () => endStream(nodeRequest), reject);
119
119
  }
120
120
  else {
121
121
  const nodeReadable = (fetchRequest.body != null
package/esm/utils.js CHANGED
@@ -57,11 +57,9 @@ export function endStream(stream) {
57
57
  // @ts-expect-error Avoid arguments adaptor trampoline https://v8.dev/blog/adaptor-frame
58
58
  return stream.end(null, null, null);
59
59
  }
60
- export function safeWrite(chunk, stream, signal) {
60
+ export function safeWrite(chunk, stream) {
61
61
  const result = stream.write(chunk);
62
62
  if (!result) {
63
- return once(stream, 'drain', {
64
- signal,
65
- });
63
+ return once(stream, 'drain');
66
64
  }
67
65
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.7.21",
3
+ "version": "0.7.22-alpha-20250526163200-dc90c3024c7fa99ad5eb4e46eeee8988f7e003df",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -16,4 +16,4 @@ export declare function wrapIncomingMessageWithPassthrough({ incomingMessage, si
16
16
  export declare function endStream(stream: {
17
17
  end: () => void;
18
18
  }): void;
19
- export declare function safeWrite(chunk: any, stream: Writable, signal?: AbortSignal | undefined): Promise<any> | undefined;
19
+ export declare function safeWrite(chunk: any, stream: Writable): Promise<any[]> | undefined;
@@ -16,4 +16,4 @@ export declare function wrapIncomingMessageWithPassthrough({ incomingMessage, si
16
16
  export declare function endStream(stream: {
17
17
  end: () => void;
18
18
  }): void;
19
- export declare function safeWrite(chunk: any, stream: Writable, signal?: AbortSignal | undefined): Promise<any> | undefined;
19
+ export declare function safeWrite(chunk: any, stream: Writable): Promise<any[]> | undefined;