@whatwg-node/node-fetch 0.5.7-alpha-20240301155147-161016a5c5b719972dd96f47adc3ee3be99e11d4 → 0.5.7-alpha-20240301175500-daf2a278109a97989c16979e2b3487a638d9daae

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/fetchCurl.js CHANGED
@@ -73,6 +73,7 @@ function fetchCurl(fetchRequest) {
73
73
  curlHandle.close();
74
74
  });
75
75
  curlHandle.once('stream', function streamListener(stream, status, headersBuf) {
76
+ const pipedStream = stream.pipe(new node_stream_1.PassThrough());
76
77
  const headersFlat = headersBuf
77
78
  .toString('utf8')
78
79
  .split(/\r?\n|\r/g)
@@ -80,7 +81,7 @@ function fetchCurl(fetchRequest) {
80
81
  if (headerFilter && !headerFilter.startsWith('HTTP/')) {
81
82
  if (fetchRequest.redirect === 'error' &&
82
83
  (headerFilter.includes('location') || headerFilter.includes('Location'))) {
83
- stream.destroy();
84
+ pipedStream.destroy();
84
85
  reject(new Error('redirect is not allowed'));
85
86
  }
86
87
  return true;
@@ -88,14 +89,22 @@ function fetchCurl(fetchRequest) {
88
89
  return false;
89
90
  });
90
91
  const headersInit = headersFlat.map(headerFlat => headerFlat.split(/:\s(.+)/).slice(0, 2));
91
- const ponyfillResponse = new Response_js_1.PonyfillResponse(stream, {
92
+ pipedStream.on('pause', () => {
93
+ stream.pause();
94
+ });
95
+ pipedStream.on('resume', () => {
96
+ stream.resume();
97
+ });
98
+ pipedStream.on('close', () => {
99
+ stream.destroy();
100
+ });
101
+ const ponyfillResponse = new Response_js_1.PonyfillResponse(pipedStream, {
92
102
  status,
93
103
  headers: headersInit,
94
104
  url: fetchRequest.url,
95
105
  });
96
- utils_js_1.readableCleanupRegistry.register(ponyfillResponse, stream);
97
106
  resolve(ponyfillResponse);
98
- streamResolved = stream;
107
+ streamResolved = pipedStream;
99
108
  });
100
109
  curlHandle.perform();
101
110
  });
@@ -5,6 +5,7 @@ const http_1 = require("http");
5
5
  const https_1 = require("https");
6
6
  const stream_1 = require("stream");
7
7
  const zlib_1 = require("zlib");
8
+ const AbortError_js_1 = require("./AbortError.js");
8
9
  const Request_js_1 = require("./Request.js");
9
10
  const Response_js_1 = require("./Response.js");
10
11
  const URL_js_1 = require("./URL.js");
@@ -69,15 +70,29 @@ function fetchNodeHttp(fetchRequest) {
69
70
  return;
70
71
  }
71
72
  }
73
+ if (responseBody === nodeResponse) {
74
+ responseBody = nodeResponse.pipe(new stream_1.PassThrough());
75
+ responseBody.on('pause', () => {
76
+ nodeResponse.pause();
77
+ });
78
+ responseBody.on('resume', () => {
79
+ nodeResponse.resume();
80
+ });
81
+ responseBody.on('close', () => {
82
+ nodeResponse.destroy();
83
+ });
84
+ fetchRequest['_signal']?.addEventListener('abort', () => {
85
+ if (!nodeResponse.destroyed) {
86
+ responseBody.emit('error', new AbortError_js_1.PonyfillAbortError());
87
+ }
88
+ });
89
+ }
72
90
  const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
73
91
  status: nodeResponse.statusCode,
74
92
  statusText: nodeResponse.statusMessage,
75
93
  headers: nodeResponse.headers,
76
94
  url: fetchRequest.url,
77
95
  });
78
- if (responseBody === nodeResponse) {
79
- utils_js_1.readableCleanupRegistry.register(ponyfillResponse, nodeResponse);
80
- }
81
96
  resolve(ponyfillResponse);
82
97
  });
83
98
  nodeRequest.once('error', reject);
package/cjs/utils.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.readableCleanupRegistry = exports.isNodeReadable = exports.isArrayBufferView = exports.fakePromise = exports.defaultHeadersSerializer = exports.getHeadersObj = void 0;
4
- const node_stream_1 = require("node:stream");
3
+ exports.isNodeReadable = exports.isArrayBufferView = exports.fakePromise = exports.defaultHeadersSerializer = exports.getHeadersObj = void 0;
5
4
  function isHeadersInstance(obj) {
6
5
  return obj?.forEach != null;
7
6
  }
@@ -72,8 +71,3 @@ function isNodeReadable(obj) {
72
71
  return obj != null && obj.pipe != null;
73
72
  }
74
73
  exports.isNodeReadable = isNodeReadable;
75
- exports.readableCleanupRegistry = new FinalizationRegistry(readable => {
76
- if (!readable.readableFlowing) {
77
- readable.pipe(new node_stream_1.PassThrough());
78
- }
79
- });
package/esm/fetchCurl.js CHANGED
@@ -1,6 +1,6 @@
1
- import { Readable } from 'node:stream';
1
+ import { PassThrough, Readable } from 'node:stream';
2
2
  import { PonyfillResponse } from './Response.js';
3
- import { defaultHeadersSerializer, isNodeReadable, readableCleanupRegistry } from './utils.js';
3
+ import { defaultHeadersSerializer, isNodeReadable } from './utils.js';
4
4
  export function fetchCurl(fetchRequest) {
5
5
  const { Curl, CurlFeature, CurlPause, CurlProgressFunc } = globalThis['libcurl'];
6
6
  const curlHandle = new Curl();
@@ -70,6 +70,7 @@ export function fetchCurl(fetchRequest) {
70
70
  curlHandle.close();
71
71
  });
72
72
  curlHandle.once('stream', function streamListener(stream, status, headersBuf) {
73
+ const pipedStream = stream.pipe(new PassThrough());
73
74
  const headersFlat = headersBuf
74
75
  .toString('utf8')
75
76
  .split(/\r?\n|\r/g)
@@ -77,7 +78,7 @@ export function fetchCurl(fetchRequest) {
77
78
  if (headerFilter && !headerFilter.startsWith('HTTP/')) {
78
79
  if (fetchRequest.redirect === 'error' &&
79
80
  (headerFilter.includes('location') || headerFilter.includes('Location'))) {
80
- stream.destroy();
81
+ pipedStream.destroy();
81
82
  reject(new Error('redirect is not allowed'));
82
83
  }
83
84
  return true;
@@ -85,14 +86,22 @@ export function fetchCurl(fetchRequest) {
85
86
  return false;
86
87
  });
87
88
  const headersInit = headersFlat.map(headerFlat => headerFlat.split(/:\s(.+)/).slice(0, 2));
88
- const ponyfillResponse = new PonyfillResponse(stream, {
89
+ pipedStream.on('pause', () => {
90
+ stream.pause();
91
+ });
92
+ pipedStream.on('resume', () => {
93
+ stream.resume();
94
+ });
95
+ pipedStream.on('close', () => {
96
+ stream.destroy();
97
+ });
98
+ const ponyfillResponse = new PonyfillResponse(pipedStream, {
89
99
  status,
90
100
  headers: headersInit,
91
101
  url: fetchRequest.url,
92
102
  });
93
- readableCleanupRegistry.register(ponyfillResponse, stream);
94
103
  resolve(ponyfillResponse);
95
- streamResolved = stream;
104
+ streamResolved = pipedStream;
96
105
  });
97
106
  curlHandle.perform();
98
107
  });
@@ -1,11 +1,12 @@
1
1
  import { request as httpRequest } from 'http';
2
2
  import { request as httpsRequest } from 'https';
3
- import { Readable } from 'stream';
3
+ import { PassThrough, Readable } from 'stream';
4
4
  import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
5
+ import { PonyfillAbortError } from './AbortError.js';
5
6
  import { PonyfillRequest } from './Request.js';
6
7
  import { PonyfillResponse } from './Response.js';
7
8
  import { PonyfillURL } from './URL.js';
8
- import { getHeadersObj, isNodeReadable, readableCleanupRegistry } from './utils.js';
9
+ import { getHeadersObj, isNodeReadable } from './utils.js';
9
10
  function getRequestFnForProtocol(url) {
10
11
  if (url.startsWith('http:')) {
11
12
  return httpRequest;
@@ -66,15 +67,29 @@ export function fetchNodeHttp(fetchRequest) {
66
67
  return;
67
68
  }
68
69
  }
70
+ if (responseBody === nodeResponse) {
71
+ responseBody = nodeResponse.pipe(new PassThrough());
72
+ responseBody.on('pause', () => {
73
+ nodeResponse.pause();
74
+ });
75
+ responseBody.on('resume', () => {
76
+ nodeResponse.resume();
77
+ });
78
+ responseBody.on('close', () => {
79
+ nodeResponse.destroy();
80
+ });
81
+ fetchRequest['_signal']?.addEventListener('abort', () => {
82
+ if (!nodeResponse.destroyed) {
83
+ responseBody.emit('error', new PonyfillAbortError());
84
+ }
85
+ });
86
+ }
69
87
  const ponyfillResponse = new PonyfillResponse(responseBody, {
70
88
  status: nodeResponse.statusCode,
71
89
  statusText: nodeResponse.statusMessage,
72
90
  headers: nodeResponse.headers,
73
91
  url: fetchRequest.url,
74
92
  });
75
- if (responseBody === nodeResponse) {
76
- readableCleanupRegistry.register(ponyfillResponse, nodeResponse);
77
- }
78
93
  resolve(ponyfillResponse);
79
94
  });
80
95
  nodeRequest.once('error', reject);
package/esm/utils.js CHANGED
@@ -1,4 +1,3 @@
1
- import { PassThrough } from 'node:stream';
2
1
  function isHeadersInstance(obj) {
3
2
  return obj?.forEach != null;
4
3
  }
@@ -64,8 +63,3 @@ export function isArrayBufferView(obj) {
64
63
  export function isNodeReadable(obj) {
65
64
  return obj != null && obj.pipe != null;
66
65
  }
67
- export const readableCleanupRegistry = new FinalizationRegistry(readable => {
68
- if (!readable.readableFlowing) {
69
- readable.pipe(new PassThrough());
70
- }
71
- });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.5.7-alpha-20240301155147-161016a5c5b719972dd96f47adc3ee3be99e11d4",
3
+ "version": "0.5.7-alpha-20240301175500-daf2a278109a97989c16979e2b3487a638d9daae",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -5,4 +5,3 @@ export declare function defaultHeadersSerializer(headers: Headers, onContentLeng
5
5
  export declare function fakePromise<T>(value: T): Promise<T>;
6
6
  export declare function isArrayBufferView(obj: any): obj is ArrayBufferView;
7
7
  export declare function isNodeReadable(obj: any): obj is Readable;
8
- export declare const readableCleanupRegistry: FinalizationRegistry<Readable>;
@@ -5,4 +5,3 @@ export declare function defaultHeadersSerializer(headers: Headers, onContentLeng
5
5
  export declare function fakePromise<T>(value: T): Promise<T>;
6
6
  export declare function isArrayBufferView(obj: any): obj is ArrayBufferView;
7
7
  export declare function isNodeReadable(obj: any): obj is Readable;
8
- export declare const readableCleanupRegistry: FinalizationRegistry<Readable>;