@whatwg-node/node-fetch 0.8.0-alpha-20241212154840-0a0effe808a6614e0a3afd853126a38641485756 → 0.8.0-alpha-20250917012053-36c9ccdc3e94ee8d0961f17398a9053fa55df37b

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.
Files changed (73) hide show
  1. package/cjs/AbortError.js +5 -3
  2. package/cjs/Blob.js +21 -19
  3. package/cjs/Body.js +146 -76
  4. package/cjs/FormData.js +54 -41
  5. package/cjs/Headers.js +54 -15
  6. package/cjs/ReadableStream.js +62 -26
  7. package/cjs/Request.js +9 -16
  8. package/cjs/Response.js +56 -10
  9. package/cjs/TextEncoderDecoder.js +6 -5
  10. package/cjs/TextEncoderDecoderStream.js +2 -6
  11. package/cjs/TransformStream.js +2 -1
  12. package/cjs/URL.js +10 -66
  13. package/cjs/URLSearchParams.js +1 -117
  14. package/cjs/WritableStream.js +35 -111
  15. package/cjs/fetch.js +37 -8
  16. package/cjs/fetchCurl.js +30 -61
  17. package/cjs/fetchNodeHttp.js +60 -64
  18. package/cjs/index.js +1 -7
  19. package/cjs/utils.js +76 -55
  20. package/esm/AbortError.js +5 -3
  21. package/esm/Blob.js +6 -4
  22. package/esm/Body.js +134 -63
  23. package/esm/FormData.js +54 -41
  24. package/esm/Headers.js +54 -15
  25. package/esm/ReadableStream.js +57 -21
  26. package/esm/Request.js +7 -14
  27. package/esm/Response.js +55 -9
  28. package/esm/TextEncoderDecoder.js +1 -0
  29. package/esm/TextEncoderDecoderStream.js +2 -6
  30. package/esm/TransformStream.js +2 -1
  31. package/esm/URL.js +9 -64
  32. package/esm/URLSearchParams.js +1 -115
  33. package/esm/WritableStream.js +33 -109
  34. package/esm/fetch.js +35 -6
  35. package/esm/fetchCurl.js +28 -59
  36. package/esm/fetchNodeHttp.js +55 -59
  37. package/esm/index.js +0 -3
  38. package/esm/utils.js +70 -53
  39. package/package.json +4 -5
  40. package/typings/AbortError.d.cts +1 -1
  41. package/typings/AbortError.d.ts +1 -1
  42. package/typings/Blob.d.cts +5 -4
  43. package/typings/Blob.d.ts +5 -4
  44. package/typings/Body.d.cts +11 -6
  45. package/typings/Body.d.ts +11 -6
  46. package/typings/Headers.d.cts +1 -1
  47. package/typings/Headers.d.ts +1 -1
  48. package/typings/ReadableStream.d.cts +8 -2
  49. package/typings/ReadableStream.d.ts +8 -2
  50. package/typings/Request.d.cts +9 -10
  51. package/typings/Request.d.ts +9 -10
  52. package/typings/Response.d.cts +6 -5
  53. package/typings/Response.d.ts +6 -5
  54. package/typings/TextEncoderDecoder.d.cts +2 -1
  55. package/typings/TextEncoderDecoder.d.ts +2 -1
  56. package/typings/URL.d.cts +12 -16
  57. package/typings/URL.d.ts +12 -16
  58. package/typings/URLSearchParams.d.cts +4 -21
  59. package/typings/URLSearchParams.d.ts +4 -21
  60. package/typings/WritableStream.d.cts +1 -1
  61. package/typings/WritableStream.d.ts +1 -1
  62. package/typings/index.d.cts +0 -3
  63. package/typings/index.d.ts +0 -3
  64. package/typings/utils.d.cts +13 -8
  65. package/typings/utils.d.ts +13 -8
  66. package/cjs/AbortController.js +0 -18
  67. package/cjs/AbortSignal.js +0 -85
  68. package/esm/AbortController.js +0 -14
  69. package/esm/AbortSignal.js +0 -81
  70. package/typings/AbortController.d.cts +0 -8
  71. package/typings/AbortController.d.ts +0 -8
  72. package/typings/AbortSignal.d.cts +0 -15
  73. package/typings/AbortSignal.d.ts +0 -15
package/cjs/AbortError.js CHANGED
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillAbortError = void 0;
4
- class PonyfillAbortError extends DOMException {
4
+ class PonyfillAbortError extends Error {
5
5
  constructor(reason) {
6
6
  let message = 'The operation was aborted';
7
7
  if (reason) {
8
8
  message += ` reason: ${reason}`;
9
9
  }
10
- super(message, 'AbortError');
11
- this.cause = reason;
10
+ super(message, {
11
+ cause: reason,
12
+ });
13
+ this.name = 'AbortError';
12
14
  }
13
15
  get reason() {
14
16
  return this.cause;
package/cjs/Blob.js CHANGED
@@ -10,20 +10,22 @@ exports.hasStreamMethod = hasStreamMethod;
10
10
  exports.hasBlobSignature = hasBlobSignature;
11
11
  exports.isArrayBuffer = isArrayBuffer;
12
12
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
13
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
14
+ const node_buffer_1 = require("node:buffer");
13
15
  const ReadableStream_js_1 = require("./ReadableStream.js");
14
16
  const utils_js_1 = require("./utils.js");
15
17
  function getBlobPartAsBuffer(blobPart) {
16
18
  if (typeof blobPart === 'string') {
17
- return Buffer.from(blobPart);
19
+ return node_buffer_1.Buffer.from(blobPart);
18
20
  }
19
- else if (Buffer.isBuffer(blobPart)) {
21
+ else if (node_buffer_1.Buffer.isBuffer(blobPart)) {
20
22
  return blobPart;
21
23
  }
22
24
  else if ((0, utils_js_1.isArrayBufferView)(blobPart)) {
23
- return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
25
+ return node_buffer_1.Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
24
26
  }
25
27
  else {
26
- return Buffer.from(blobPart);
28
+ return node_buffer_1.Buffer.from(blobPart);
27
29
  }
28
30
  }
29
31
  function hasBufferMethod(obj) {
@@ -81,13 +83,13 @@ class PonyfillBlob {
81
83
  }
82
84
  if (hasBytesMethod(blobPart)) {
83
85
  return blobPart.bytes().then(bytes => {
84
- this._buffer = Buffer.from(bytes);
86
+ this._buffer = node_buffer_1.Buffer.from(bytes);
85
87
  return this._buffer;
86
88
  });
87
89
  }
88
90
  if (hasArrayBufferMethod(blobPart)) {
89
91
  return blobPart.arrayBuffer().then(arrayBuf => {
90
- this._buffer = Buffer.from(arrayBuf, undefined, blobPart.size);
92
+ this._buffer = node_buffer_1.Buffer.from(arrayBuf, undefined, blobPart.size);
91
93
  return this._buffer;
92
94
  });
93
95
  }
@@ -104,13 +106,13 @@ class PonyfillBlob {
104
106
  }
105
107
  else if (hasArrayBufferMethod(blobPart)) {
106
108
  jobs.push(blobPart.arrayBuffer().then(arrayBuf => {
107
- bufferChunks[i] = Buffer.from(arrayBuf, undefined, blobPart.size);
109
+ bufferChunks[i] = node_buffer_1.Buffer.from(arrayBuf, undefined, blobPart.size);
108
110
  }));
109
111
  return undefined;
110
112
  }
111
113
  else if (hasBytesMethod(blobPart)) {
112
114
  jobs.push(blobPart.bytes().then(bytes => {
113
- bufferChunks[i] = Buffer.from(bytes);
115
+ bufferChunks[i] = node_buffer_1.Buffer.from(bytes);
114
116
  }));
115
117
  return undefined;
116
118
  }
@@ -119,13 +121,13 @@ class PonyfillBlob {
119
121
  }
120
122
  });
121
123
  if (jobs.length > 0) {
122
- return Promise.all(jobs).then(() => Buffer.concat(bufferChunks, this._size || undefined));
124
+ return Promise.all(jobs).then(() => node_buffer_1.Buffer.concat(bufferChunks, this._size || undefined));
123
125
  }
124
- return (0, utils_js_1.fakePromise)(Buffer.concat(bufferChunks, this._size || undefined));
126
+ return (0, utils_js_1.fakePromise)(node_buffer_1.Buffer.concat(bufferChunks, this._size || undefined));
125
127
  }
126
128
  arrayBuffer() {
127
129
  if (this._buffer) {
128
- // @ts-expect-error - `Buffer` and `ArrayBuffer` are compatible
130
+ // @ts-ignore - Mismatch between Buffer and ArrayBuffer
129
131
  return (0, utils_js_1.fakePromise)(this._buffer);
130
132
  }
131
133
  if (this.blobParts.length === 1) {
@@ -136,7 +138,7 @@ class PonyfillBlob {
136
138
  return this.blobParts[0].arrayBuffer();
137
139
  }
138
140
  }
139
- // @ts-expect-error - `Buffer` and `ArrayBuffer` are compatible
141
+ // @ts-ignore - Mismatch between Buffer and ArrayBuffer
140
142
  return this.buffer();
141
143
  }
142
144
  bytes() {
@@ -144,13 +146,13 @@ class PonyfillBlob {
144
146
  return (0, utils_js_1.fakePromise)(this._buffer);
145
147
  }
146
148
  if (this.blobParts.length === 1) {
147
- if (Buffer.isBuffer(this.blobParts[0])) {
149
+ if (node_buffer_1.Buffer.isBuffer(this.blobParts[0])) {
148
150
  this._buffer = this.blobParts[0];
149
- return (0, utils_js_1.fakePromise)(this.blobParts[0]);
151
+ return (0, utils_js_1.fakePromise)(this._buffer);
150
152
  }
151
153
  if (this.blobParts[0] instanceof Uint8Array) {
152
- this._buffer = Buffer.from(this.blobParts[0]);
153
- return (0, utils_js_1.fakePromise)(this.blobParts[0]);
154
+ this._buffer = node_buffer_1.Buffer.from(this.blobParts[0]);
155
+ return (0, utils_js_1.fakePromise)(this._buffer);
154
156
  }
155
157
  if (hasBytesMethod(this.blobParts[0])) {
156
158
  return this.blobParts[0].bytes();
@@ -209,7 +211,7 @@ class PonyfillBlob {
209
211
  this._size = 0;
210
212
  for (const blobPart of this.blobParts) {
211
213
  if (typeof blobPart === 'string') {
212
- this._size += Buffer.byteLength(blobPart);
214
+ this._size += node_buffer_1.Buffer.byteLength(blobPart);
213
215
  }
214
216
  else if (hasSizeProperty(blobPart)) {
215
217
  this._size += blobPart.size;
@@ -266,13 +268,13 @@ class PonyfillBlob {
266
268
  }
267
269
  if (hasBytesMethod(blobPart)) {
268
270
  return blobPart.bytes().then(bytes => {
269
- const buf = Buffer.from(bytes);
271
+ const buf = node_buffer_1.Buffer.from(bytes);
270
272
  controller.enqueue(buf);
271
273
  });
272
274
  }
273
275
  if (hasArrayBufferMethod(blobPart)) {
274
276
  return blobPart.arrayBuffer().then(arrayBuffer => {
275
- const buf = Buffer.from(arrayBuffer, undefined, blobPart.size);
277
+ const buf = node_buffer_1.Buffer.from(arrayBuffer, undefined, blobPart.size);
276
278
  controller.enqueue(buf);
277
279
  });
278
280
  }
package/cjs/Body.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillBody = void 0;
4
- const tslib_1 = require("tslib");
5
- const stream_1 = require("stream");
6
- const busboy_1 = tslib_1.__importDefault(require("busboy"));
4
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
5
+ const node_buffer_1 = require("node:buffer");
6
+ const node_stream_1 = require("node:stream");
7
+ const busboy_1 = require("@fastify/busboy");
8
+ const promise_helpers_1 = require("@whatwg-node/promise-helpers");
7
9
  const Blob_js_1 = require("./Blob.js");
8
10
  const File_js_1 = require("./File.js");
9
11
  const FormData_js_1 = require("./FormData.js");
@@ -34,14 +36,16 @@ class PonyfillBody {
34
36
  this.contentLength = contentLength;
35
37
  this.bodyType = bodyType;
36
38
  this._buffer = buffer;
39
+ this._signal = options.signal;
37
40
  }
38
41
  bodyType;
39
42
  _bodyFactory = () => null;
40
43
  _generatedBody = null;
41
44
  _buffer;
45
+ _signal;
42
46
  generateBody() {
43
47
  if (this._generatedBody?.readable?.destroyed && this._buffer) {
44
- this._generatedBody.readable = stream_1.Readable.from(this._buffer);
48
+ this._generatedBody.readable = node_stream_1.Readable.from(this._buffer);
45
49
  }
46
50
  if (this._generatedBody) {
47
51
  return this._generatedBody;
@@ -101,48 +105,66 @@ class PonyfillBody {
101
105
  return null;
102
106
  }
103
107
  _chunks = null;
104
- _collectChunksFromReadable() {
105
- if (this._chunks) {
106
- return (0, utils_js_1.fakePromise)(this._chunks);
107
- }
108
+ _doCollectChunksFromReadableJob() {
108
109
  if (this.bodyType === BodyInitType.AsyncIterable) {
109
- const iterator = this.bodyInit[Symbol.asyncIterator]();
110
- const collectValue = () => {
111
- return iterator.next().then(({ value, done }) => {
112
- this._chunks ||= [];
113
- if (value) {
114
- this._chunks.push(value);
115
- }
116
- if (!done) {
117
- return collectValue();
118
- }
110
+ if (Array.fromAsync) {
111
+ return (0, promise_helpers_1.handleMaybePromise)(() => Array.fromAsync(this.bodyInit), chunks => {
112
+ this._chunks = chunks;
119
113
  return this._chunks;
120
114
  });
121
- };
115
+ }
116
+ const iterator = this.bodyInit[Symbol.asyncIterator]();
117
+ const chunks = [];
118
+ const collectValue = () => (0, promise_helpers_1.handleMaybePromise)(() => iterator.next(), ({ value, done }) => {
119
+ if (value) {
120
+ chunks.push(value);
121
+ }
122
+ if (!done) {
123
+ return collectValue();
124
+ }
125
+ this._chunks = chunks;
126
+ return this._chunks;
127
+ });
122
128
  return collectValue();
123
129
  }
124
130
  const _body = this.generateBody();
125
131
  if (!_body) {
126
- return (0, utils_js_1.fakePromise)([]);
132
+ this._chunks = [];
133
+ return (0, utils_js_1.fakePromise)(this._chunks);
127
134
  }
128
- this._chunks = [];
129
- _body.readable.on('data', chunk => {
130
- this._chunks.push(chunk);
131
- });
135
+ if (_body.readable.destroyed) {
136
+ return (0, utils_js_1.fakePromise)((this._chunks = []));
137
+ }
138
+ const chunks = [];
132
139
  return new Promise((resolve, reject) => {
133
- _body.readable.once('end', () => {
134
- resolve(this._chunks);
140
+ _body.readable.on('data', chunk => {
141
+ chunks.push(chunk);
135
142
  });
136
- _body.readable.once('error', e => {
137
- reject(e);
143
+ _body.readable.once('error', reject);
144
+ _body.readable.once('end', () => {
145
+ resolve((this._chunks = chunks));
138
146
  });
139
147
  });
140
148
  }
149
+ _collectChunksFromReadable() {
150
+ if (this._chunks) {
151
+ return (0, utils_js_1.fakePromise)(this._chunks);
152
+ }
153
+ this._chunks ||= this._doCollectChunksFromReadableJob();
154
+ return this._chunks;
155
+ }
141
156
  _blob = null;
142
157
  blob() {
143
158
  if (this._blob) {
144
159
  return (0, utils_js_1.fakePromise)(this._blob);
145
160
  }
161
+ if (this.bodyType === BodyInitType.String) {
162
+ this._text = this.bodyInit;
163
+ this._blob = new Blob_js_1.PonyfillBlob([this._text], {
164
+ type: this.contentType || 'text/plain;charset=UTF-8',
165
+ size: this.contentLength,
166
+ });
167
+ }
146
168
  if (this.bodyType === BodyInitType.Blob) {
147
169
  this._blob = this.bodyInit;
148
170
  return (0, utils_js_1.fakePromise)(this._blob);
@@ -154,13 +176,13 @@ class PonyfillBody {
154
176
  });
155
177
  return (0, utils_js_1.fakePromise)(this._blob);
156
178
  }
157
- return this._collectChunksFromReadable().then(chunks => {
179
+ return (0, utils_js_1.fakePromise)((0, promise_helpers_1.handleMaybePromise)(() => this._collectChunksFromReadable(), chunks => {
158
180
  this._blob = new Blob_js_1.PonyfillBlob(chunks, {
159
181
  type: this.contentType || '',
160
182
  size: this.contentLength,
161
183
  });
162
184
  return this._blob;
163
- });
185
+ }));
164
186
  }
165
187
  _formData = null;
166
188
  formData(opts) {
@@ -181,61 +203,108 @@ class PonyfillBody {
181
203
  ...opts?.formDataLimits,
182
204
  };
183
205
  return new Promise((resolve, reject) => {
184
- const bb = (0, busboy_1.default)({
206
+ const stream = this.body?.readable;
207
+ if (!stream) {
208
+ return reject(new Error('No stream available'));
209
+ }
210
+ // form data file that is currently being processed, it's
211
+ // important to keep track of it in case the stream ends early
212
+ let currFile = null;
213
+ const bb = new busboy_1.Busboy({
185
214
  headers: {
215
+ 'content-length': typeof this.contentLength === 'number'
216
+ ? this.contentLength.toString()
217
+ : this.contentLength || '',
186
218
  'content-type': this.contentType || '',
187
219
  },
188
220
  limits: formDataLimits,
189
- defParamCharset: 'utf-8',
221
+ defCharset: 'utf-8',
190
222
  });
191
- bb.on('field', (name, value, { nameTruncated, valueTruncated }) => {
192
- if (nameTruncated) {
193
- reject(new Error(`Field name size exceeded: ${formDataLimits?.fieldNameSize} bytes`));
223
+ if (this._signal) {
224
+ (0, node_stream_1.addAbortSignal)(this._signal, bb);
225
+ }
226
+ let completed = false;
227
+ const complete = (err) => {
228
+ if (completed)
229
+ return;
230
+ completed = true;
231
+ stream.unpipe(bb);
232
+ bb.destroy();
233
+ if (currFile) {
234
+ currFile.destroy();
235
+ currFile = null;
236
+ }
237
+ if (err) {
238
+ reject(err);
239
+ }
240
+ else {
241
+ // no error occured, this is a successful end/complete/finish
242
+ resolve(this._formData);
243
+ }
244
+ };
245
+ // we dont need to listen to the stream close event because bb will close or error when necessary
246
+ // stream.on('close', complete);
247
+ // stream can be aborted, for example
248
+ stream.on('error', complete);
249
+ bb.on('field', (name, value, fieldnameTruncated, valueTruncated) => {
250
+ if (fieldnameTruncated) {
251
+ return complete(new Error(`Field name size exceeded: ${formDataLimits?.fieldNameSize} bytes`));
194
252
  }
195
253
  if (valueTruncated) {
196
- reject(new Error(`Field value size exceeded: ${formDataLimits?.fieldSize} bytes`));
254
+ return complete(new Error(`Field value size exceeded: ${formDataLimits?.fieldSize} bytes`));
197
255
  }
198
256
  this._formData.set(name, value);
199
257
  });
200
- bb.on('fieldsLimit', () => {
201
- reject(new Error(`Fields limit exceeded: ${formDataLimits?.fields}`));
202
- });
203
- bb.on('file', (name, fileStream, { filename, mimeType }) => {
258
+ bb.on('file', (name, fileStream, filename, _transferEncoding, mimeType) => {
259
+ currFile = fileStream;
204
260
  const chunks = [];
205
- fileStream.on('limit', () => {
206
- reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
207
- });
208
261
  fileStream.on('data', chunk => {
209
262
  chunks.push(chunk);
210
263
  });
264
+ fileStream.on('error', complete);
265
+ fileStream.on('limit', () => {
266
+ complete(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
267
+ });
211
268
  fileStream.on('close', () => {
212
269
  if (fileStream.truncated) {
213
- reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
270
+ complete(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
214
271
  }
272
+ currFile = null;
215
273
  const file = new File_js_1.PonyfillFile(chunks, filename, { type: mimeType });
216
274
  this._formData.set(name, file);
217
275
  });
218
276
  });
277
+ bb.on('fieldsLimit', () => {
278
+ complete(new Error(`Fields limit exceeded: ${formDataLimits?.fields}`));
279
+ });
219
280
  bb.on('filesLimit', () => {
220
- reject(new Error(`Files limit exceeded: ${formDataLimits?.files}`));
281
+ complete(new Error(`Files limit exceeded: ${formDataLimits?.files}`));
221
282
  });
222
283
  bb.on('partsLimit', () => {
223
- reject(new Error(`Parts limit exceeded: ${formDataLimits?.parts}`));
224
- });
225
- bb.on('close', () => {
226
- resolve(this._formData);
227
- });
228
- bb.on('error', (err = 'An error occurred while parsing the form data') => {
229
- const errMessage = err.message || err.toString();
230
- reject(new TypeError(errMessage, err.cause));
284
+ complete(new Error(`Parts limit exceeded: ${formDataLimits?.parts}`));
231
285
  });
232
- _body?.readable.pipe(bb);
286
+ bb.on('end', complete);
287
+ bb.on('finish', complete);
288
+ bb.on('close', complete);
289
+ bb.on('error', complete);
290
+ stream.pipe(bb);
233
291
  });
234
292
  }
235
293
  buffer() {
236
294
  if (this._buffer) {
237
295
  return (0, utils_js_1.fakePromise)(this._buffer);
238
296
  }
297
+ if (this._text) {
298
+ this._buffer = node_buffer_1.Buffer.from(this._text, 'utf-8');
299
+ return (0, utils_js_1.fakePromise)(this._buffer);
300
+ }
301
+ if (this.bodyType === BodyInitType.String) {
302
+ return this.text().then(text => {
303
+ this._text = text;
304
+ this._buffer = node_buffer_1.Buffer.from(text, 'utf-8');
305
+ return this._buffer;
306
+ });
307
+ }
239
308
  if (this.bodyType === BodyInitType.Blob) {
240
309
  if ((0, Blob_js_1.hasBufferMethod)(this.bodyInit)) {
241
310
  return this.bodyInit.buffer().then(buf => {
@@ -245,31 +314,31 @@ class PonyfillBody {
245
314
  }
246
315
  if ((0, Blob_js_1.hasBytesMethod)(this.bodyInit)) {
247
316
  return this.bodyInit.bytes().then(bytes => {
248
- this._buffer = Buffer.from(bytes);
317
+ this._buffer = node_buffer_1.Buffer.from(bytes);
249
318
  return this._buffer;
250
319
  });
251
320
  }
252
321
  if ((0, Blob_js_1.hasArrayBufferMethod)(this.bodyInit)) {
253
322
  return this.bodyInit.arrayBuffer().then(buf => {
254
- this._buffer = Buffer.from(buf, undefined, buf.byteLength);
323
+ this._buffer = node_buffer_1.Buffer.from(buf, undefined, buf.byteLength);
255
324
  return this._buffer;
256
325
  });
257
326
  }
258
327
  }
259
- return this._collectChunksFromReadable().then(chunks => {
328
+ return (0, utils_js_1.fakePromise)((0, promise_helpers_1.handleMaybePromise)(() => this._collectChunksFromReadable(), chunks => {
260
329
  if (chunks.length === 1) {
261
330
  this._buffer = chunks[0];
262
331
  return this._buffer;
263
332
  }
264
- this._buffer = Buffer.concat(chunks);
333
+ this._buffer = node_buffer_1.Buffer.concat(chunks);
265
334
  return this._buffer;
266
- });
335
+ }));
267
336
  }
268
337
  bytes() {
269
338
  return this.buffer();
270
339
  }
271
340
  arrayBuffer() {
272
- // @ts-expect-error - `Buffer` and `ArrayBuffer` are compatible
341
+ // @ts-ignore - Mismatch between Buffer and ArrayBuffer
273
342
  return this.buffer();
274
343
  }
275
344
  _json = null;
@@ -315,80 +384,81 @@ function processBodyInit(bodyInit) {
315
384
  };
316
385
  }
317
386
  if (typeof bodyInit === 'string') {
318
- const buffer = Buffer.from(bodyInit);
319
- const contentLength = buffer.byteLength;
387
+ const contentLength = node_buffer_1.Buffer.byteLength(bodyInit);
320
388
  return {
321
389
  bodyType: BodyInitType.String,
322
390
  contentType: 'text/plain;charset=UTF-8',
323
391
  contentLength,
324
- buffer,
325
392
  bodyFactory() {
326
- const readable = stream_1.Readable.from(buffer);
393
+ const readable = node_stream_1.Readable.from(node_buffer_1.Buffer.from(bodyInit, 'utf-8'));
327
394
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
328
395
  },
329
396
  };
330
397
  }
331
- if (Buffer.isBuffer(bodyInit)) {
398
+ if (node_buffer_1.Buffer.isBuffer(bodyInit)) {
399
+ const buffer = bodyInit;
332
400
  return {
333
401
  bodyType: BodyInitType.Buffer,
334
402
  contentType: null,
335
403
  contentLength: bodyInit.length,
336
404
  buffer: bodyInit,
337
405
  bodyFactory() {
338
- const readable = stream_1.Readable.from(bodyInit);
406
+ const readable = node_stream_1.Readable.from(buffer);
339
407
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
340
408
  return body;
341
409
  },
342
410
  };
343
411
  }
344
412
  if ((0, utils_js_1.isArrayBufferView)(bodyInit)) {
345
- const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
413
+ const buffer = node_buffer_1.Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
346
414
  return {
347
415
  bodyType: BodyInitType.Buffer,
348
416
  contentLength: bodyInit.byteLength,
349
417
  contentType: null,
350
418
  buffer,
351
419
  bodyFactory() {
352
- const readable = stream_1.Readable.from(buffer);
420
+ const readable = node_stream_1.Readable.from(buffer);
353
421
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
354
422
  return body;
355
423
  },
356
424
  };
357
425
  }
358
426
  if (bodyInit instanceof ReadableStream_js_1.PonyfillReadableStream && bodyInit.readable != null) {
427
+ const readableStream = bodyInit;
359
428
  return {
360
429
  bodyType: BodyInitType.ReadableStream,
361
- bodyFactory: () => bodyInit,
430
+ bodyFactory: () => readableStream,
362
431
  contentType: null,
363
432
  contentLength: null,
364
433
  };
365
434
  }
366
435
  if (isBlob(bodyInit)) {
436
+ const blob = bodyInit;
367
437
  return {
368
438
  bodyType: BodyInitType.Blob,
369
439
  contentType: bodyInit.type,
370
440
  contentLength: bodyInit.size,
371
441
  bodyFactory() {
372
- return bodyInit.stream();
442
+ return blob.stream();
373
443
  },
374
444
  };
375
445
  }
376
446
  if (bodyInit instanceof ArrayBuffer) {
377
447
  const contentLength = bodyInit.byteLength;
378
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
448
+ const buffer = node_buffer_1.Buffer.from(bodyInit, undefined, bodyInit.byteLength);
379
449
  return {
380
450
  bodyType: BodyInitType.Buffer,
381
451
  contentType: null,
382
452
  contentLength,
383
453
  buffer,
384
454
  bodyFactory() {
385
- const readable = stream_1.Readable.from(buffer);
455
+ const readable = node_stream_1.Readable.from(buffer);
386
456
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
387
457
  return body;
388
458
  },
389
459
  };
390
460
  }
391
- if (bodyInit instanceof stream_1.Readable) {
461
+ if (bodyInit instanceof node_stream_1.Readable) {
392
462
  return {
393
463
  bodyType: BodyInitType.Readable,
394
464
  contentType: null,
@@ -406,7 +476,7 @@ function processBodyInit(bodyInit) {
406
476
  contentType,
407
477
  contentLength: null,
408
478
  bodyFactory() {
409
- const body = new ReadableStream_js_1.PonyfillReadableStream(stream_1.Readable.from(bodyInit.toString()));
479
+ const body = new ReadableStream_js_1.PonyfillReadableStream(node_stream_1.Readable.from(bodyInit.toString()));
410
480
  return body;
411
481
  },
412
482
  };
@@ -438,7 +508,7 @@ function processBodyInit(bodyInit) {
438
508
  contentLength: null,
439
509
  bodyType: BodyInitType.AsyncIterable,
440
510
  bodyFactory() {
441
- const readable = stream_1.Readable.from(bodyInit);
511
+ const readable = node_stream_1.Readable.from(bodyInit);
442
512
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
443
513
  },
444
514
  };
@@ -449,7 +519,7 @@ function isFormData(value) {
449
519
  return value?.forEach != null;
450
520
  }
451
521
  function isBlob(value) {
452
- return value?.stream != null;
522
+ return value?.stream != null && typeof value.stream === 'function';
453
523
  }
454
524
  function isURLSearchParams(value) {
455
525
  return value?.sort != null;