@whatwg-node/node-fetch 0.7.19 → 0.7.20-alpha-20250512180730-0c5bd3cc4fb4349409e46f6c13c6774715600d41

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.
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillReadableStream = void 0;
4
4
  const node_buffer_1 = require("node:buffer");
5
+ const node_events_1 = require("node:events");
5
6
  const node_stream_1 = require("node:stream");
7
+ const promises_1 = require("node:stream/promises");
6
8
  const promise_helpers_1 = require("@whatwg-node/promise-helpers");
7
9
  const utils_js_1 = require("./utils.js");
8
10
  function createController(desiredSize, readable) {
@@ -125,12 +127,14 @@ class PonyfillReadableStream {
125
127
  }
126
128
  cancel(reason) {
127
129
  this.readable.destroy(reason);
128
- return new Promise(resolve => this.readable.once('end', resolve));
130
+ // @ts-expect-error - we know it is void
131
+ return (0, node_events_1.once)(this.readable, 'close');
129
132
  }
130
133
  locked = false;
131
134
  getReader(_options) {
132
135
  const iterator = this.readable[Symbol.asyncIterator]();
133
136
  this.locked = true;
137
+ const thisReadable = this.readable;
134
138
  return {
135
139
  read() {
136
140
  return iterator.next();
@@ -159,10 +163,12 @@ class PonyfillReadableStream {
159
163
  this.locked = false;
160
164
  return (0, utils_js_1.fakePromise)();
161
165
  },
162
- closed: new Promise((resolve, reject) => {
163
- this.readable.once('end', resolve);
164
- this.readable.once('error', reject);
165
- }),
166
+ get closed() {
167
+ return Promise.race([
168
+ (0, node_events_1.once)(thisReadable, 'end'),
169
+ (0, node_events_1.once)(thisReadable, 'error').then(err => Promise.reject(err)),
170
+ ]);
171
+ },
166
172
  };
167
173
  }
168
174
  [Symbol.asyncIterator]() {
@@ -202,10 +208,8 @@ class PonyfillReadableStream {
202
208
  }
203
209
  pipeTo(destination) {
204
210
  if (isPonyfillWritableStream(destination)) {
205
- return new Promise((resolve, reject) => {
206
- this.readable.pipe(destination.writable);
207
- destination.writable.once('finish', resolve);
208
- destination.writable.once('error', reject);
211
+ return (0, promises_1.pipeline)(this.readable, destination.writable, {
212
+ end: true,
209
213
  });
210
214
  }
211
215
  else {
@@ -7,9 +7,7 @@ class PonyfillTextDecoderStream extends TransformStream_js_1.PonyfillTransformSt
7
7
  textDecoder;
8
8
  constructor(encoding, options) {
9
9
  super({
10
- transform: (chunk, controller) => {
11
- controller.enqueue(this.textDecoder.decode(chunk, { stream: true }));
12
- },
10
+ transform: (chunk, controller) => controller.enqueue(this.textDecoder.decode(chunk, { stream: true })),
13
11
  });
14
12
  this.textDecoder = new TextEncoderDecoder_js_1.PonyfillTextDecoder(encoding, options);
15
13
  }
@@ -28,9 +26,7 @@ class PonyfillTextEncoderStream extends TransformStream_js_1.PonyfillTransformSt
28
26
  textEncoder;
29
27
  constructor(encoding) {
30
28
  super({
31
- transform: (chunk, controller) => {
32
- controller.enqueue(this.textEncoder.encode(chunk));
33
- },
29
+ transform: (chunk, controller) => controller.enqueue(this.textEncoder.encode(chunk)),
34
30
  });
35
31
  this.textEncoder = new TextEncoderDecoder_js_1.PonyfillTextEncoder(encoding);
36
32
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillWritableStream = void 0;
4
+ const node_events_1 = require("node:events");
4
5
  const node_stream_1 = require("node:stream");
5
6
  const promise_helpers_1 = require("@whatwg-node/promise-helpers");
6
7
  const utils_js_1 = require("./utils.js");
@@ -62,19 +63,15 @@ class PonyfillWritableStream {
62
63
  getWriter() {
63
64
  const writable = this.writable;
64
65
  return {
65
- closed: new Promise(resolve => {
66
- writable.once('close', () => {
67
- resolve(undefined);
68
- });
69
- }),
66
+ get closed() {
67
+ return (0, node_events_1.once)(writable, 'close');
68
+ },
70
69
  get desiredSize() {
71
70
  return writable.writableLength;
72
71
  },
73
- ready: new Promise(resolve => {
74
- writable.once('drain', () => {
75
- resolve(undefined);
76
- });
77
- }),
72
+ get ready() {
73
+ return (0, node_events_1.once)(writable, 'drain');
74
+ },
78
75
  releaseLock() {
79
76
  // no-op
80
77
  },
@@ -95,10 +92,8 @@ class PonyfillWritableStream {
95
92
  return (0, utils_js_1.fakePromise)().then(() => (0, utils_js_1.endStream)(writable));
96
93
  },
97
94
  abort(reason) {
98
- return new Promise(resolve => {
99
- writable.destroy(reason);
100
- writable.once('close', resolve);
101
- });
95
+ writable.destroy(reason);
96
+ return (0, node_events_1.once)(writable, 'close');
102
97
  },
103
98
  };
104
99
  }
@@ -112,10 +107,8 @@ class PonyfillWritableStream {
112
107
  return (0, utils_js_1.fakePromise)().then(() => (0, utils_js_1.endStream)(this.writable));
113
108
  }
114
109
  abort(reason) {
115
- return new Promise(resolve => {
116
- this.writable.destroy(reason);
117
- this.writable.once('close', resolve);
118
- });
110
+ this.writable.destroy(reason);
111
+ return (0, node_events_1.once)(this.writable, 'close');
119
112
  }
120
113
  locked = false;
121
114
  }
@@ -108,7 +108,7 @@ function fetchNodeHttp(fetchRequest) {
108
108
  resolve(ponyfillResponse);
109
109
  });
110
110
  if (fetchRequest['_buffer'] != null) {
111
- (0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.safeWrite)(fetchRequest['_buffer'], nodeRequest), () => (0, utils_js_1.endStream)(nodeRequest), reject);
111
+ (0, promise_helpers_1.handleMaybePromise)(() => (0, utils_js_1.safeWrite)(fetchRequest['_buffer'], nodeRequest, fetchRequest.signal), () => (0, utils_js_1.endStream)(nodeRequest), reject);
112
112
  }
113
113
  else {
114
114
  const nodeReadable = (fetchRequest.body != null
package/cjs/utils.js CHANGED
@@ -10,6 +10,7 @@ exports.shouldRedirect = shouldRedirect;
10
10
  exports.wrapIncomingMessageWithPassthrough = wrapIncomingMessageWithPassthrough;
11
11
  exports.endStream = endStream;
12
12
  exports.safeWrite = safeWrite;
13
+ const node_events_1 = require("node:events");
13
14
  const node_stream_1 = require("node:stream");
14
15
  const promises_1 = require("node:stream/promises");
15
16
  function isHeadersInstance(obj) {
@@ -64,9 +65,11 @@ function endStream(stream) {
64
65
  // @ts-expect-error Avoid arguments adaptor trampoline https://v8.dev/blog/adaptor-frame
65
66
  return stream.end(null, null, null);
66
67
  }
67
- function safeWrite(chunk, stream) {
68
+ function safeWrite(chunk, stream, signal) {
68
69
  const result = stream.write(chunk);
69
70
  if (!result) {
70
- return new Promise(resolve => stream.once('drain', resolve));
71
+ return (0, node_events_1.once)(stream, 'drain', {
72
+ signal,
73
+ });
71
74
  }
72
75
  }
@@ -1,5 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
+ import { once } from 'node:events';
2
3
  import { Readable } from 'node:stream';
4
+ import { pipeline } from 'node:stream/promises';
3
5
  import { handleMaybePromise } from '@whatwg-node/promise-helpers';
4
6
  import { fakePromise } from './utils.js';
5
7
  function createController(desiredSize, readable) {
@@ -122,12 +124,14 @@ export class PonyfillReadableStream {
122
124
  }
123
125
  cancel(reason) {
124
126
  this.readable.destroy(reason);
125
- return new Promise(resolve => this.readable.once('end', resolve));
127
+ // @ts-expect-error - we know it is void
128
+ return once(this.readable, 'close');
126
129
  }
127
130
  locked = false;
128
131
  getReader(_options) {
129
132
  const iterator = this.readable[Symbol.asyncIterator]();
130
133
  this.locked = true;
134
+ const thisReadable = this.readable;
131
135
  return {
132
136
  read() {
133
137
  return iterator.next();
@@ -156,10 +160,12 @@ export class PonyfillReadableStream {
156
160
  this.locked = false;
157
161
  return fakePromise();
158
162
  },
159
- closed: new Promise((resolve, reject) => {
160
- this.readable.once('end', resolve);
161
- this.readable.once('error', reject);
162
- }),
163
+ get closed() {
164
+ return Promise.race([
165
+ once(thisReadable, 'end'),
166
+ once(thisReadable, 'error').then(err => Promise.reject(err)),
167
+ ]);
168
+ },
163
169
  };
164
170
  }
165
171
  [Symbol.asyncIterator]() {
@@ -199,10 +205,8 @@ export class PonyfillReadableStream {
199
205
  }
200
206
  pipeTo(destination) {
201
207
  if (isPonyfillWritableStream(destination)) {
202
- return new Promise((resolve, reject) => {
203
- this.readable.pipe(destination.writable);
204
- destination.writable.once('finish', resolve);
205
- destination.writable.once('error', reject);
208
+ return pipeline(this.readable, destination.writable, {
209
+ end: true,
206
210
  });
207
211
  }
208
212
  else {
@@ -4,9 +4,7 @@ export class PonyfillTextDecoderStream extends PonyfillTransformStream {
4
4
  textDecoder;
5
5
  constructor(encoding, options) {
6
6
  super({
7
- transform: (chunk, controller) => {
8
- controller.enqueue(this.textDecoder.decode(chunk, { stream: true }));
9
- },
7
+ transform: (chunk, controller) => controller.enqueue(this.textDecoder.decode(chunk, { stream: true })),
10
8
  });
11
9
  this.textDecoder = new PonyfillTextDecoder(encoding, options);
12
10
  }
@@ -24,9 +22,7 @@ export class PonyfillTextEncoderStream extends PonyfillTransformStream {
24
22
  textEncoder;
25
23
  constructor(encoding) {
26
24
  super({
27
- transform: (chunk, controller) => {
28
- controller.enqueue(this.textEncoder.encode(chunk));
29
- },
25
+ transform: (chunk, controller) => controller.enqueue(this.textEncoder.encode(chunk)),
30
26
  });
31
27
  this.textEncoder = new PonyfillTextEncoder(encoding);
32
28
  }
@@ -1,3 +1,4 @@
1
+ import { once } from 'node:events';
1
2
  import { Writable } from 'node:stream';
2
3
  import { fakeRejectPromise } from '@whatwg-node/promise-helpers';
3
4
  import { endStream, fakePromise, safeWrite } from './utils.js';
@@ -59,19 +60,15 @@ export class PonyfillWritableStream {
59
60
  getWriter() {
60
61
  const writable = this.writable;
61
62
  return {
62
- closed: new Promise(resolve => {
63
- writable.once('close', () => {
64
- resolve(undefined);
65
- });
66
- }),
63
+ get closed() {
64
+ return once(writable, 'close');
65
+ },
67
66
  get desiredSize() {
68
67
  return writable.writableLength;
69
68
  },
70
- ready: new Promise(resolve => {
71
- writable.once('drain', () => {
72
- resolve(undefined);
73
- });
74
- }),
69
+ get ready() {
70
+ return once(writable, 'drain');
71
+ },
75
72
  releaseLock() {
76
73
  // no-op
77
74
  },
@@ -92,10 +89,8 @@ export class PonyfillWritableStream {
92
89
  return fakePromise().then(() => endStream(writable));
93
90
  },
94
91
  abort(reason) {
95
- return new Promise(resolve => {
96
- writable.destroy(reason);
97
- writable.once('close', resolve);
98
- });
92
+ writable.destroy(reason);
93
+ return once(writable, 'close');
99
94
  },
100
95
  };
101
96
  }
@@ -109,10 +104,8 @@ export class PonyfillWritableStream {
109
104
  return fakePromise().then(() => endStream(this.writable));
110
105
  }
111
106
  abort(reason) {
112
- return new Promise(resolve => {
113
- this.writable.destroy(reason);
114
- this.writable.once('close', resolve);
115
- });
107
+ this.writable.destroy(reason);
108
+ return once(this.writable, 'close');
116
109
  }
117
110
  locked = false;
118
111
  }
@@ -105,7 +105,7 @@ export function fetchNodeHttp(fetchRequest) {
105
105
  resolve(ponyfillResponse);
106
106
  });
107
107
  if (fetchRequest['_buffer'] != null) {
108
- handleMaybePromise(() => safeWrite(fetchRequest['_buffer'], nodeRequest), () => endStream(nodeRequest), reject);
108
+ handleMaybePromise(() => safeWrite(fetchRequest['_buffer'], nodeRequest, fetchRequest.signal), () => endStream(nodeRequest), reject);
109
109
  }
110
110
  else {
111
111
  const nodeReadable = (fetchRequest.body != null
package/esm/utils.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { once } from 'node:events';
1
2
  import { PassThrough } from 'node:stream';
2
3
  import { pipeline } from 'node:stream/promises';
3
4
  function isHeadersInstance(obj) {
@@ -51,9 +52,11 @@ export function endStream(stream) {
51
52
  // @ts-expect-error Avoid arguments adaptor trampoline https://v8.dev/blog/adaptor-frame
52
53
  return stream.end(null, null, null);
53
54
  }
54
- export function safeWrite(chunk, stream) {
55
+ export function safeWrite(chunk, stream, signal) {
55
56
  const result = stream.write(chunk);
56
57
  if (!result) {
57
- return new Promise(resolve => stream.once('drain', resolve));
58
+ return once(stream, 'drain', {
59
+ signal,
60
+ });
58
61
  }
59
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.7.19",
3
+ "version": "0.7.20-alpha-20250512180730-0c5bd3cc4fb4349409e46f6c13c6774715600d41",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage } from 'node:http';
2
- import { PassThrough, Readable } from 'node:stream';
2
+ import { PassThrough, Readable, Writable } from 'node:stream';
3
3
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
4
4
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
5
5
  export { fakePromise } from '@whatwg-node/promise-helpers';
@@ -16,7 +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: {
20
- write: (chunk: any) => boolean;
21
- once: (event: string, listener: () => void) => void;
22
- }): Promise<void> | undefined;
19
+ export declare function safeWrite(chunk: any, stream: Writable, signal?: AbortSignal | undefined): Promise<any> | undefined;
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage } from 'node:http';
2
- import { PassThrough, Readable } from 'node:stream';
2
+ import { PassThrough, Readable, Writable } from 'node:stream';
3
3
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
4
4
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
5
5
  export { fakePromise } from '@whatwg-node/promise-helpers';
@@ -16,7 +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: {
20
- write: (chunk: any) => boolean;
21
- once: (event: string, listener: () => void) => void;
22
- }): Promise<void> | undefined;
19
+ export declare function safeWrite(chunk: any, stream: Writable, signal?: AbortSignal | undefined): Promise<any> | undefined;