@whatwg-node/node-fetch 0.7.15-alpha-20250322121446-0a8732f1758546d618a797e18a1da953697eb27c → 0.7.15-alpha-20250322150325-5bf305acd3e2ca23ba360e88d5e6b33587051b71

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
@@ -4,6 +4,7 @@ exports.PonyfillBody = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  /* eslint-disable @typescript-eslint/ban-ts-comment */
6
6
  const node_buffer_1 = require("node:buffer");
7
+ const node_http_1 = require("node:http");
7
8
  const node_stream_1 = require("node:stream");
8
9
  const busboy_1 = tslib_1.__importDefault(require("busboy"));
9
10
  const promise_helpers_1 = require("@whatwg-node/promise-helpers");
@@ -31,7 +32,7 @@ class PonyfillBody {
31
32
  constructor(bodyInit, options = {}) {
32
33
  this.bodyInit = bodyInit;
33
34
  this.options = options;
34
- const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
35
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit, options?.signal);
35
36
  this._bodyFactory = bodyFactory;
36
37
  this.contentType = contentType;
37
38
  this.contentLength = contentLength;
@@ -130,7 +131,7 @@ class PonyfillBody {
130
131
  }
131
132
  const _body = this.generateBody();
132
133
  this._chunks = [];
133
- if (!_body || _body.readable?.destroyed) {
134
+ if (!_body) {
134
135
  return (0, utils_js_1.fakePromise)(this._chunks);
135
136
  }
136
137
  _body.readable.on('data', chunk => {
@@ -310,7 +311,7 @@ class PonyfillBody {
310
311
  }
311
312
  }
312
313
  exports.PonyfillBody = PonyfillBody;
313
- function processBodyInit(bodyInit) {
314
+ function processBodyInit(bodyInit, signal) {
314
315
  if (bodyInit == null) {
315
316
  return {
316
317
  bodyFactory: () => null,
@@ -333,13 +334,14 @@ function processBodyInit(bodyInit) {
333
334
  };
334
335
  }
335
336
  if (node_buffer_1.Buffer.isBuffer(bodyInit)) {
337
+ const buffer = bodyInit;
336
338
  return {
337
339
  bodyType: BodyInitType.Buffer,
338
340
  contentType: null,
339
341
  contentLength: bodyInit.length,
340
342
  buffer: bodyInit,
341
343
  bodyFactory() {
342
- const readable = node_stream_1.Readable.from(bodyInit);
344
+ const readable = node_stream_1.Readable.from(buffer);
343
345
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
344
346
  return body;
345
347
  },
@@ -360,20 +362,22 @@ function processBodyInit(bodyInit) {
360
362
  };
361
363
  }
362
364
  if (bodyInit instanceof ReadableStream_js_1.PonyfillReadableStream && bodyInit.readable != null) {
365
+ const readableStream = bodyInit;
363
366
  return {
364
367
  bodyType: BodyInitType.ReadableStream,
365
- bodyFactory: () => bodyInit,
368
+ bodyFactory: () => readableStream,
366
369
  contentType: null,
367
370
  contentLength: null,
368
371
  };
369
372
  }
370
373
  if (isBlob(bodyInit)) {
374
+ const blob = bodyInit;
371
375
  return {
372
376
  bodyType: BodyInitType.Blob,
373
377
  contentType: bodyInit.type,
374
378
  contentLength: bodyInit.size,
375
379
  bodyFactory() {
376
- return bodyInit.stream();
380
+ return blob.stream();
377
381
  },
378
382
  };
379
383
  }
@@ -392,6 +396,21 @@ function processBodyInit(bodyInit) {
392
396
  },
393
397
  };
394
398
  }
399
+ if (bodyInit instanceof node_http_1.IncomingMessage) {
400
+ const passthrough = (bodyInit = bodyInit.pipe(new node_stream_1.PassThrough(), {
401
+ end: true,
402
+ // @ts-expect-error - `signal` is not in the type definition
403
+ signal,
404
+ }));
405
+ return {
406
+ bodyType: BodyInitType.Readable,
407
+ contentType: null,
408
+ contentLength: null,
409
+ bodyFactory() {
410
+ return new ReadableStream_js_1.PonyfillReadableStream(passthrough);
411
+ },
412
+ };
413
+ }
395
414
  if (bodyInit instanceof node_stream_1.Readable) {
396
415
  return {
397
416
  bodyType: BodyInitType.Readable,
package/esm/Body.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/ban-ts-comment */
2
2
  import { Buffer } from 'node:buffer';
3
- import { Readable } from 'node:stream';
3
+ import { IncomingMessage } from 'node:http';
4
+ import { PassThrough, Readable } from 'node:stream';
4
5
  import busboy from 'busboy';
5
6
  import { handleMaybePromise } from '@whatwg-node/promise-helpers';
6
7
  import { hasArrayBufferMethod, hasBufferMethod, hasBytesMethod, PonyfillBlob } from './Blob.js';
@@ -27,7 +28,7 @@ export class PonyfillBody {
27
28
  constructor(bodyInit, options = {}) {
28
29
  this.bodyInit = bodyInit;
29
30
  this.options = options;
30
- const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
31
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit, options?.signal);
31
32
  this._bodyFactory = bodyFactory;
32
33
  this.contentType = contentType;
33
34
  this.contentLength = contentLength;
@@ -126,7 +127,7 @@ export class PonyfillBody {
126
127
  }
127
128
  const _body = this.generateBody();
128
129
  this._chunks = [];
129
- if (!_body || _body.readable?.destroyed) {
130
+ if (!_body) {
130
131
  return fakePromise(this._chunks);
131
132
  }
132
133
  _body.readable.on('data', chunk => {
@@ -305,7 +306,7 @@ export class PonyfillBody {
305
306
  });
306
307
  }
307
308
  }
308
- function processBodyInit(bodyInit) {
309
+ function processBodyInit(bodyInit, signal) {
309
310
  if (bodyInit == null) {
310
311
  return {
311
312
  bodyFactory: () => null,
@@ -328,13 +329,14 @@ function processBodyInit(bodyInit) {
328
329
  };
329
330
  }
330
331
  if (Buffer.isBuffer(bodyInit)) {
332
+ const buffer = bodyInit;
331
333
  return {
332
334
  bodyType: BodyInitType.Buffer,
333
335
  contentType: null,
334
336
  contentLength: bodyInit.length,
335
337
  buffer: bodyInit,
336
338
  bodyFactory() {
337
- const readable = Readable.from(bodyInit);
339
+ const readable = Readable.from(buffer);
338
340
  const body = new PonyfillReadableStream(readable);
339
341
  return body;
340
342
  },
@@ -355,20 +357,22 @@ function processBodyInit(bodyInit) {
355
357
  };
356
358
  }
357
359
  if (bodyInit instanceof PonyfillReadableStream && bodyInit.readable != null) {
360
+ const readableStream = bodyInit;
358
361
  return {
359
362
  bodyType: BodyInitType.ReadableStream,
360
- bodyFactory: () => bodyInit,
363
+ bodyFactory: () => readableStream,
361
364
  contentType: null,
362
365
  contentLength: null,
363
366
  };
364
367
  }
365
368
  if (isBlob(bodyInit)) {
369
+ const blob = bodyInit;
366
370
  return {
367
371
  bodyType: BodyInitType.Blob,
368
372
  contentType: bodyInit.type,
369
373
  contentLength: bodyInit.size,
370
374
  bodyFactory() {
371
- return bodyInit.stream();
375
+ return blob.stream();
372
376
  },
373
377
  };
374
378
  }
@@ -387,6 +391,21 @@ function processBodyInit(bodyInit) {
387
391
  },
388
392
  };
389
393
  }
394
+ if (bodyInit instanceof IncomingMessage) {
395
+ const passthrough = (bodyInit = bodyInit.pipe(new PassThrough(), {
396
+ end: true,
397
+ // @ts-expect-error - `signal` is not in the type definition
398
+ signal,
399
+ }));
400
+ return {
401
+ bodyType: BodyInitType.Readable,
402
+ contentType: null,
403
+ contentLength: null,
404
+ bodyFactory() {
405
+ return new PonyfillReadableStream(passthrough);
406
+ },
407
+ };
408
+ }
390
409
  if (bodyInit instanceof Readable) {
391
410
  return {
392
411
  bodyType: BodyInitType.Readable,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.7.15-alpha-20250322121446-0a8732f1758546d618a797e18a1da953697eb27c",
3
+ "version": "0.7.15-alpha-20250322150325-5bf305acd3e2ca23ba360e88d5e6b33587051b71",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -16,6 +16,7 @@ export interface FormDataLimits {
16
16
  }
17
17
  export interface PonyfillBodyOptions {
18
18
  formDataLimits?: FormDataLimits;
19
+ signal?: AbortSignal;
19
20
  }
20
21
  export declare class PonyfillBody<TJSON = any> implements Body {
21
22
  private bodyInit;
package/typings/Body.d.ts CHANGED
@@ -16,6 +16,7 @@ export interface FormDataLimits {
16
16
  }
17
17
  export interface PonyfillBodyOptions {
18
18
  formDataLimits?: FormDataLimits;
19
+ signal?: AbortSignal;
19
20
  }
20
21
  export declare class PonyfillBody<TJSON = any> implements Body {
21
22
  private bodyInit;