@whatwg-node/node-fetch 0.5.12-alpha-20240531124053-0e72e3d9233a6ec341e6e573d51cf27d31020c75 → 0.5.12

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
@@ -27,6 +27,11 @@ class PonyfillBody {
27
27
  this.contentLength = null;
28
28
  this._bodyFactory = () => null;
29
29
  this._generatedBody = null;
30
+ this._chunks = null;
31
+ this._blob = null;
32
+ this._formData = null;
33
+ this._json = null;
34
+ this._text = null;
30
35
  const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
31
36
  this._bodyFactory = bodyFactory;
32
37
  this.contentType = contentType;
@@ -35,6 +40,9 @@ class PonyfillBody {
35
40
  this._buffer = buffer;
36
41
  }
37
42
  generateBody() {
43
+ if (this._generatedBody?.readable?.destroyed && this._buffer) {
44
+ this._generatedBody.readable = stream_1.Readable.from(this._buffer);
45
+ }
38
46
  if (this._generatedBody) {
39
47
  return this._generatedBody;
40
48
  }
@@ -69,17 +77,20 @@ class PonyfillBody {
69
77
  return null;
70
78
  }
71
79
  _collectChunksFromReadable() {
80
+ if (this._chunks) {
81
+ return (0, utils_js_1.fakePromise)(this._chunks);
82
+ }
72
83
  const _body = this.generateBody();
73
84
  if (!_body) {
74
85
  return (0, utils_js_1.fakePromise)([]);
75
86
  }
76
- const chunks = [];
87
+ this._chunks = [];
77
88
  _body.readable.on('data', chunk => {
78
- chunks.push(chunk);
89
+ this._chunks.push(chunk);
79
90
  });
80
91
  return new Promise((resolve, reject) => {
81
92
  _body.readable.once('end', () => {
82
- resolve(chunks);
93
+ resolve(this._chunks);
83
94
  });
84
95
  _body.readable.once('error', e => {
85
96
  reject(e);
@@ -87,31 +98,40 @@ class PonyfillBody {
87
98
  });
88
99
  }
89
100
  blob() {
101
+ if (this._blob) {
102
+ return (0, utils_js_1.fakePromise)(this._blob);
103
+ }
90
104
  if (this.bodyType === BodyInitType.Blob) {
91
- return (0, utils_js_1.fakePromise)(this.bodyInit);
105
+ this._blob = this.bodyInit;
106
+ return (0, utils_js_1.fakePromise)(this._blob);
92
107
  }
93
108
  if (this._buffer) {
94
- const blob = new Blob_js_1.PonyfillBlob([this._buffer], {
109
+ this._blob = new Blob_js_1.PonyfillBlob([this._buffer], {
95
110
  type: this.contentType || '',
96
111
  size: this.contentLength,
97
112
  });
98
- return (0, utils_js_1.fakePromise)(blob);
113
+ return (0, utils_js_1.fakePromise)(this._blob);
99
114
  }
100
115
  return this._collectChunksFromReadable().then(chunks => {
101
- return new Blob_js_1.PonyfillBlob(chunks, {
116
+ this._blob = new Blob_js_1.PonyfillBlob(chunks, {
102
117
  type: this.contentType || '',
103
118
  size: this.contentLength,
104
119
  });
120
+ return this._blob;
105
121
  });
106
122
  }
107
123
  formData(opts) {
124
+ if (this._formData) {
125
+ return (0, utils_js_1.fakePromise)(this._formData);
126
+ }
108
127
  if (this.bodyType === BodyInitType.FormData) {
109
- return (0, utils_js_1.fakePromise)(this.bodyInit);
128
+ this._formData = this.bodyInit;
129
+ return (0, utils_js_1.fakePromise)(this._formData);
110
130
  }
111
- const formData = new FormData_js_1.PonyfillFormData();
131
+ this._formData = new FormData_js_1.PonyfillFormData();
112
132
  const _body = this.generateBody();
113
133
  if (_body == null) {
114
- return (0, utils_js_1.fakePromise)(formData);
134
+ return (0, utils_js_1.fakePromise)(this._formData);
115
135
  }
116
136
  const formDataLimits = {
117
137
  ...this.options.formDataLimits,
@@ -132,7 +152,7 @@ class PonyfillBody {
132
152
  if (valueTruncated) {
133
153
  reject(new Error(`Field value size exceeded: ${formDataLimits?.fieldSize} bytes`));
134
154
  }
135
- formData.set(name, value);
155
+ this._formData.set(name, value);
136
156
  });
137
157
  bb.on('fieldsLimit', () => {
138
158
  reject(new Error(`Fields limit exceeded: ${formDataLimits?.fields}`));
@@ -150,7 +170,7 @@ class PonyfillBody {
150
170
  reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
151
171
  }
152
172
  const file = new File_js_1.PonyfillFile(chunks, filename, { type: mimeType });
153
- formData.set(name, file);
173
+ this._formData.set(name, file);
154
174
  });
155
175
  });
156
176
  bb.on('filesLimit', () => {
@@ -160,7 +180,7 @@ class PonyfillBody {
160
180
  reject(new Error(`Parts limit exceeded: ${formDataLimits?.parts}`));
161
181
  });
162
182
  bb.on('close', () => {
163
- resolve(formData);
183
+ resolve(this._formData);
164
184
  });
165
185
  bb.on('error', (err = 'An error occurred while parsing the form data') => {
166
186
  const errMessage = err.message || err.toString();
@@ -175,30 +195,47 @@ class PonyfillBody {
175
195
  }
176
196
  if (this.bodyType === BodyInitType.Blob) {
177
197
  if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) {
178
- return this.bodyInit.arrayBuffer();
198
+ return this.bodyInit.arrayBuffer().then(buf => {
199
+ this._buffer = buf;
200
+ return this._buffer;
201
+ });
179
202
  }
180
203
  const bodyInitTyped = this.bodyInit;
181
- return bodyInitTyped
182
- .arrayBuffer()
183
- .then(arrayBuffer => Buffer.from(arrayBuffer, undefined, bodyInitTyped.size));
204
+ return bodyInitTyped.arrayBuffer().then(arrayBuffer => {
205
+ this._buffer = Buffer.from(arrayBuffer, undefined, bodyInitTyped.size);
206
+ return this._buffer;
207
+ });
184
208
  }
185
- return this._collectChunksFromReadable().then(function concatCollectedChunksFromReadable(chunks) {
209
+ return this._collectChunksFromReadable().then(chunks => {
186
210
  if (chunks.length === 1) {
187
- return chunks[0];
211
+ this._buffer = chunks[0];
212
+ return this._buffer;
188
213
  }
189
- return Buffer.concat(chunks);
214
+ this._buffer = Buffer.concat(chunks);
215
+ return this._buffer;
190
216
  });
191
217
  }
192
218
  json() {
193
- return this.text().then(function parseTextAsJson(text) {
194
- return JSON.parse(text);
219
+ if (this._json) {
220
+ return (0, utils_js_1.fakePromise)(this._json);
221
+ }
222
+ return this.text().then(text => {
223
+ this._json = JSON.parse(text);
224
+ return this._json;
195
225
  });
196
226
  }
197
227
  text() {
228
+ if (this._text) {
229
+ return (0, utils_js_1.fakePromise)(this._text);
230
+ }
198
231
  if (this.bodyType === BodyInitType.String) {
199
- return (0, utils_js_1.fakePromise)(this.bodyInit);
232
+ this._text = this.bodyInit;
233
+ return (0, utils_js_1.fakePromise)(this._text);
200
234
  }
201
- return this.arrayBuffer().then(buffer => buffer.toString('utf-8'));
235
+ return this.arrayBuffer().then(buffer => {
236
+ this._text = buffer.toString('utf-8');
237
+ return this._text;
238
+ });
202
239
  }
203
240
  }
204
241
  exports.PonyfillBody = PonyfillBody;
package/cjs/FormData.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStreamFromFormData = exports.PonyfillFormData = void 0;
3
+ exports.PonyfillFormData = void 0;
4
+ exports.getStreamFromFormData = getStreamFromFormData;
4
5
  const ReadableStream_js_1 = require("./ReadableStream.js");
5
6
  class PonyfillFormData {
6
7
  constructor() {
@@ -115,7 +116,6 @@ function getStreamFromFormData(formData, boundary = '---') {
115
116
  },
116
117
  });
117
118
  }
118
- exports.getStreamFromFormData = getStreamFromFormData;
119
119
  function getNormalizedFile(name, blob, fileName) {
120
120
  Object.defineProperty(blob, 'name', {
121
121
  configurable: true,
package/cjs/Headers.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PonyfillHeaders = exports.isHeadersLike = void 0;
3
+ exports.PonyfillHeaders = void 0;
4
+ exports.isHeadersLike = isHeadersLike;
4
5
  const util_1 = require("util");
5
6
  function isHeadersLike(headers) {
6
7
  return headers?.get && headers?.forEach;
7
8
  }
8
- exports.isHeadersLike = isHeadersLike;
9
9
  class PonyfillHeaders {
10
10
  constructor(headersInit) {
11
11
  this.headersInit = headersInit;
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.PonyfillReadableStream = void 0;
5
4
  const stream_1 = require("stream");
@@ -53,7 +52,6 @@ function isReadableStream(obj) {
53
52
  class PonyfillReadableStream {
54
53
  constructor(underlyingSource) {
55
54
  this.locked = false;
56
- this[_a] = 'ReadableStream';
57
55
  if (underlyingSource instanceof PonyfillReadableStream && underlyingSource.readable != null) {
58
56
  this.readable = underlyingSource.readable;
59
57
  }
@@ -174,4 +172,3 @@ class PonyfillReadableStream {
174
172
  }
175
173
  }
176
174
  exports.PonyfillReadableStream = PonyfillReadableStream;
177
- _a = Symbol.toStringTag;
package/cjs/Request.js CHANGED
@@ -85,7 +85,7 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
85
85
  return this._signal;
86
86
  }
87
87
  clone() {
88
- return new PonyfillRequest(this);
88
+ return this;
89
89
  }
90
90
  }
91
91
  exports.PonyfillRequest = PonyfillRequest;
package/cjs/Response.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.PonyfillResponse = void 0;
5
4
  const http_1 = require("http");
@@ -9,7 +8,6 @@ const JSON_CONTENT_TYPE = 'application/json; charset=utf-8';
9
8
  class PonyfillResponse extends Body_js_1.PonyfillBody {
10
9
  constructor(body, init) {
11
10
  super(body || null, init);
12
- this[_a] = 'Response';
13
11
  this.headers =
14
12
  init?.headers && (0, Headers_js_1.isHeadersLike)(init.headers)
15
13
  ? init.headers
@@ -42,7 +40,7 @@ class PonyfillResponse extends Body_js_1.PonyfillBody {
42
40
  return this.status >= 200 && this.status < 300;
43
41
  }
44
42
  clone() {
45
- return new PonyfillResponse(this.body, this);
43
+ return this;
46
44
  }
47
45
  static error() {
48
46
  return new PonyfillResponse(null, {
@@ -73,4 +71,3 @@ class PonyfillResponse extends Body_js_1.PonyfillBody {
73
71
  }
74
72
  }
75
73
  exports.PonyfillResponse = PonyfillResponse;
76
- _a = Symbol.toStringTag;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PonyfillBtoa = exports.PonyfillTextDecoder = exports.PonyfillTextEncoder = void 0;
3
+ exports.PonyfillTextDecoder = exports.PonyfillTextEncoder = void 0;
4
+ exports.PonyfillBtoa = PonyfillBtoa;
4
5
  const utils_js_1 = require("./utils.js");
5
6
  class PonyfillTextEncoder {
6
7
  constructor(encoding = 'utf-8') {
@@ -43,4 +44,3 @@ exports.PonyfillTextDecoder = PonyfillTextDecoder;
43
44
  function PonyfillBtoa(input) {
44
45
  return Buffer.from(input, 'binary').toString('base64');
45
46
  }
46
- exports.PonyfillBtoa = PonyfillBtoa;
package/cjs/fetch.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchPonyfill = void 0;
3
+ exports.fetchPonyfill = fetchPonyfill;
4
4
  const fs_1 = require("fs");
5
5
  const url_1 = require("url");
6
6
  const fetchCurl_js_1 = require("./fetchCurl.js");
@@ -76,4 +76,3 @@ function fetchPonyfill(info, init) {
76
76
  }
77
77
  return (0, fetchNodeHttp_js_1.fetchNodeHttp)(fetchRequest);
78
78
  }
79
- exports.fetchPonyfill = fetchPonyfill;
package/cjs/fetchCurl.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchCurl = void 0;
3
+ exports.fetchCurl = fetchCurl;
4
4
  const stream_1 = require("stream");
5
5
  const Response_js_1 = require("./Response.js");
6
6
  const utils_js_1 = require("./utils.js");
@@ -117,4 +117,3 @@ function fetchCurl(fetchRequest) {
117
117
  curlHandle.perform();
118
118
  });
119
119
  }
120
- exports.fetchCurl = fetchCurl;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchNodeHttp = void 0;
3
+ exports.fetchNodeHttp = fetchNodeHttp;
4
4
  const http_1 = require("http");
5
5
  const https_1 = require("https");
6
6
  const stream_1 = require("stream");
@@ -109,4 +109,3 @@ function fetchNodeHttp(fetchRequest) {
109
109
  }
110
110
  });
111
111
  }
112
- exports.fetchNodeHttp = fetchNodeHttp;
package/cjs/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.URLSearchParams = exports.URL = exports.btoa = exports.TextDecoder = exports.TextEncoder = exports.Blob = exports.FormData = exports.File = exports.ReadableStream = exports.Response = exports.Request = exports.Body = exports.Headers = exports.fetch = exports.patchReadableFromWeb = void 0;
4
- var utils_js_1 = require("./utils.js");
5
- Object.defineProperty(exports, "patchReadableFromWeb", { enumerable: true, get: function () { return utils_js_1.patchReadableFromWeb; } });
3
+ exports.URLSearchParams = exports.URL = exports.btoa = exports.TextDecoder = exports.TextEncoder = exports.Blob = exports.FormData = exports.File = exports.ReadableStream = exports.Response = exports.Request = exports.Body = exports.Headers = exports.fetch = void 0;
6
4
  var fetch_js_1 = require("./fetch.js");
7
5
  Object.defineProperty(exports, "fetch", { enumerable: true, get: function () { return fetch_js_1.fetchPonyfill; } });
8
6
  var Headers_js_1 = require("./Headers.js");
package/cjs/utils.js CHANGED
@@ -1,27 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNodeReadable = exports.isArrayBufferView = exports.fakePromise = exports.defaultHeadersSerializer = exports.getHeadersObj = exports.patchReadableFromWeb = void 0;
4
- const stream_1 = require("stream");
3
+ exports.getHeadersObj = getHeadersObj;
4
+ exports.defaultHeadersSerializer = defaultHeadersSerializer;
5
+ exports.fakePromise = fakePromise;
6
+ exports.isArrayBufferView = isArrayBufferView;
7
+ exports.isNodeReadable = isNodeReadable;
5
8
  function isHeadersInstance(obj) {
6
9
  return obj?.forEach != null;
7
10
  }
8
- function patchReadableFromWeb() {
9
- try {
10
- const originalReadableFromWeb = stream_1.Readable.fromWeb;
11
- if (originalReadableFromWeb.name !== 'ReadableFromWebPatchedByWhatWgNode') {
12
- stream_1.Readable.fromWeb = function ReadableFromWebPatchedByWhatWgNode(stream) {
13
- if (stream.readable != null) {
14
- return stream.readable;
15
- }
16
- return originalReadableFromWeb(stream);
17
- };
18
- }
19
- }
20
- catch (e) {
21
- console.warn('Could not patch Readable.fromWeb, so this might break Readable.fromWeb usage with the whatwg-node and the integrations like Fastify', e);
22
- }
23
- }
24
- exports.patchReadableFromWeb = patchReadableFromWeb;
25
11
  function getHeadersObj(headers) {
26
12
  if (headers == null || !isHeadersInstance(headers)) {
27
13
  return headers;
@@ -32,7 +18,6 @@ function getHeadersObj(headers) {
32
18
  });
33
19
  return obj;
34
20
  }
35
- exports.getHeadersObj = getHeadersObj;
36
21
  function defaultHeadersSerializer(headers, onContentLength) {
37
22
  const headerArray = [];
38
23
  headers.forEach((value, key) => {
@@ -43,7 +28,6 @@ function defaultHeadersSerializer(headers, onContentLength) {
43
28
  });
44
29
  return headerArray;
45
30
  }
46
- exports.defaultHeadersSerializer = defaultHeadersSerializer;
47
31
  function isPromise(val) {
48
32
  return val?.then != null;
49
33
  }
@@ -80,12 +64,9 @@ function fakePromise(value) {
80
64
  [Symbol.toStringTag]: 'Promise',
81
65
  };
82
66
  }
83
- exports.fakePromise = fakePromise;
84
67
  function isArrayBufferView(obj) {
85
68
  return obj != null && obj.buffer != null && obj.byteLength != null && obj.byteOffset != null;
86
69
  }
87
- exports.isArrayBufferView = isArrayBufferView;
88
70
  function isNodeReadable(obj) {
89
71
  return obj != null && obj.pipe != null;
90
72
  }
91
- exports.isNodeReadable = isNodeReadable;
package/esm/Body.js CHANGED
@@ -23,6 +23,11 @@ export class PonyfillBody {
23
23
  this.contentLength = null;
24
24
  this._bodyFactory = () => null;
25
25
  this._generatedBody = null;
26
+ this._chunks = null;
27
+ this._blob = null;
28
+ this._formData = null;
29
+ this._json = null;
30
+ this._text = null;
26
31
  const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
27
32
  this._bodyFactory = bodyFactory;
28
33
  this.contentType = contentType;
@@ -31,6 +36,9 @@ export class PonyfillBody {
31
36
  this._buffer = buffer;
32
37
  }
33
38
  generateBody() {
39
+ if (this._generatedBody?.readable?.destroyed && this._buffer) {
40
+ this._generatedBody.readable = Readable.from(this._buffer);
41
+ }
34
42
  if (this._generatedBody) {
35
43
  return this._generatedBody;
36
44
  }
@@ -65,17 +73,20 @@ export class PonyfillBody {
65
73
  return null;
66
74
  }
67
75
  _collectChunksFromReadable() {
76
+ if (this._chunks) {
77
+ return fakePromise(this._chunks);
78
+ }
68
79
  const _body = this.generateBody();
69
80
  if (!_body) {
70
81
  return fakePromise([]);
71
82
  }
72
- const chunks = [];
83
+ this._chunks = [];
73
84
  _body.readable.on('data', chunk => {
74
- chunks.push(chunk);
85
+ this._chunks.push(chunk);
75
86
  });
76
87
  return new Promise((resolve, reject) => {
77
88
  _body.readable.once('end', () => {
78
- resolve(chunks);
89
+ resolve(this._chunks);
79
90
  });
80
91
  _body.readable.once('error', e => {
81
92
  reject(e);
@@ -83,31 +94,40 @@ export class PonyfillBody {
83
94
  });
84
95
  }
85
96
  blob() {
97
+ if (this._blob) {
98
+ return fakePromise(this._blob);
99
+ }
86
100
  if (this.bodyType === BodyInitType.Blob) {
87
- return fakePromise(this.bodyInit);
101
+ this._blob = this.bodyInit;
102
+ return fakePromise(this._blob);
88
103
  }
89
104
  if (this._buffer) {
90
- const blob = new PonyfillBlob([this._buffer], {
105
+ this._blob = new PonyfillBlob([this._buffer], {
91
106
  type: this.contentType || '',
92
107
  size: this.contentLength,
93
108
  });
94
- return fakePromise(blob);
109
+ return fakePromise(this._blob);
95
110
  }
96
111
  return this._collectChunksFromReadable().then(chunks => {
97
- return new PonyfillBlob(chunks, {
112
+ this._blob = new PonyfillBlob(chunks, {
98
113
  type: this.contentType || '',
99
114
  size: this.contentLength,
100
115
  });
116
+ return this._blob;
101
117
  });
102
118
  }
103
119
  formData(opts) {
120
+ if (this._formData) {
121
+ return fakePromise(this._formData);
122
+ }
104
123
  if (this.bodyType === BodyInitType.FormData) {
105
- return fakePromise(this.bodyInit);
124
+ this._formData = this.bodyInit;
125
+ return fakePromise(this._formData);
106
126
  }
107
- const formData = new PonyfillFormData();
127
+ this._formData = new PonyfillFormData();
108
128
  const _body = this.generateBody();
109
129
  if (_body == null) {
110
- return fakePromise(formData);
130
+ return fakePromise(this._formData);
111
131
  }
112
132
  const formDataLimits = {
113
133
  ...this.options.formDataLimits,
@@ -128,7 +148,7 @@ export class PonyfillBody {
128
148
  if (valueTruncated) {
129
149
  reject(new Error(`Field value size exceeded: ${formDataLimits?.fieldSize} bytes`));
130
150
  }
131
- formData.set(name, value);
151
+ this._formData.set(name, value);
132
152
  });
133
153
  bb.on('fieldsLimit', () => {
134
154
  reject(new Error(`Fields limit exceeded: ${formDataLimits?.fields}`));
@@ -146,7 +166,7 @@ export class PonyfillBody {
146
166
  reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
147
167
  }
148
168
  const file = new PonyfillFile(chunks, filename, { type: mimeType });
149
- formData.set(name, file);
169
+ this._formData.set(name, file);
150
170
  });
151
171
  });
152
172
  bb.on('filesLimit', () => {
@@ -156,7 +176,7 @@ export class PonyfillBody {
156
176
  reject(new Error(`Parts limit exceeded: ${formDataLimits?.parts}`));
157
177
  });
158
178
  bb.on('close', () => {
159
- resolve(formData);
179
+ resolve(this._formData);
160
180
  });
161
181
  bb.on('error', (err = 'An error occurred while parsing the form data') => {
162
182
  const errMessage = err.message || err.toString();
@@ -171,30 +191,47 @@ export class PonyfillBody {
171
191
  }
172
192
  if (this.bodyType === BodyInitType.Blob) {
173
193
  if (this.bodyInit instanceof PonyfillBlob) {
174
- return this.bodyInit.arrayBuffer();
194
+ return this.bodyInit.arrayBuffer().then(buf => {
195
+ this._buffer = buf;
196
+ return this._buffer;
197
+ });
175
198
  }
176
199
  const bodyInitTyped = this.bodyInit;
177
- return bodyInitTyped
178
- .arrayBuffer()
179
- .then(arrayBuffer => Buffer.from(arrayBuffer, undefined, bodyInitTyped.size));
200
+ return bodyInitTyped.arrayBuffer().then(arrayBuffer => {
201
+ this._buffer = Buffer.from(arrayBuffer, undefined, bodyInitTyped.size);
202
+ return this._buffer;
203
+ });
180
204
  }
181
- return this._collectChunksFromReadable().then(function concatCollectedChunksFromReadable(chunks) {
205
+ return this._collectChunksFromReadable().then(chunks => {
182
206
  if (chunks.length === 1) {
183
- return chunks[0];
207
+ this._buffer = chunks[0];
208
+ return this._buffer;
184
209
  }
185
- return Buffer.concat(chunks);
210
+ this._buffer = Buffer.concat(chunks);
211
+ return this._buffer;
186
212
  });
187
213
  }
188
214
  json() {
189
- return this.text().then(function parseTextAsJson(text) {
190
- return JSON.parse(text);
215
+ if (this._json) {
216
+ return fakePromise(this._json);
217
+ }
218
+ return this.text().then(text => {
219
+ this._json = JSON.parse(text);
220
+ return this._json;
191
221
  });
192
222
  }
193
223
  text() {
224
+ if (this._text) {
225
+ return fakePromise(this._text);
226
+ }
194
227
  if (this.bodyType === BodyInitType.String) {
195
- return fakePromise(this.bodyInit);
228
+ this._text = this.bodyInit;
229
+ return fakePromise(this._text);
196
230
  }
197
- return this.arrayBuffer().then(buffer => buffer.toString('utf-8'));
231
+ return this.arrayBuffer().then(buffer => {
232
+ this._text = buffer.toString('utf-8');
233
+ return this._text;
234
+ });
198
235
  }
199
236
  }
200
237
  function processBodyInit(bodyInit) {
@@ -1,4 +1,3 @@
1
- var _a;
2
1
  import { Readable } from 'stream';
3
2
  function createController(desiredSize, readable) {
4
3
  let chunks = [];
@@ -50,7 +49,6 @@ function isReadableStream(obj) {
50
49
  export class PonyfillReadableStream {
51
50
  constructor(underlyingSource) {
52
51
  this.locked = false;
53
- this[_a] = 'ReadableStream';
54
52
  if (underlyingSource instanceof PonyfillReadableStream && underlyingSource.readable != null) {
55
53
  this.readable = underlyingSource.readable;
56
54
  }
@@ -170,4 +168,3 @@ export class PonyfillReadableStream {
170
168
  return isReadableStream(instance);
171
169
  }
172
170
  }
173
- _a = Symbol.toStringTag;
package/esm/Request.js CHANGED
@@ -82,7 +82,7 @@ export class PonyfillRequest extends PonyfillBody {
82
82
  return this._signal;
83
83
  }
84
84
  clone() {
85
- return new PonyfillRequest(this);
85
+ return this;
86
86
  }
87
87
  }
88
88
  _a = Symbol.toStringTag;
package/esm/Response.js CHANGED
@@ -1,4 +1,3 @@
1
- var _a;
2
1
  import { STATUS_CODES } from 'http';
3
2
  import { PonyfillBody } from './Body.js';
4
3
  import { isHeadersLike, PonyfillHeaders } from './Headers.js';
@@ -6,7 +5,6 @@ const JSON_CONTENT_TYPE = 'application/json; charset=utf-8';
6
5
  export class PonyfillResponse extends PonyfillBody {
7
6
  constructor(body, init) {
8
7
  super(body || null, init);
9
- this[_a] = 'Response';
10
8
  this.headers =
11
9
  init?.headers && isHeadersLike(init.headers)
12
10
  ? init.headers
@@ -39,7 +37,7 @@ export class PonyfillResponse extends PonyfillBody {
39
37
  return this.status >= 200 && this.status < 300;
40
38
  }
41
39
  clone() {
42
- return new PonyfillResponse(this.body, this);
40
+ return this;
43
41
  }
44
42
  static error() {
45
43
  return new PonyfillResponse(null, {
@@ -69,4 +67,3 @@ export class PonyfillResponse extends PonyfillBody {
69
67
  return new PonyfillResponse(JSON.stringify(data), init);
70
68
  }
71
69
  }
72
- _a = Symbol.toStringTag;
package/esm/index.js CHANGED
@@ -1,4 +1,3 @@
1
- export { patchReadableFromWeb } from './utils.js';
2
1
  export { fetchPonyfill as fetch } from './fetch.js';
3
2
  export { PonyfillHeaders as Headers } from './Headers.js';
4
3
  export { PonyfillBody as Body } from './Body.js';
package/esm/utils.js CHANGED
@@ -1,23 +1,6 @@
1
- import { Readable } from 'stream';
2
1
  function isHeadersInstance(obj) {
3
2
  return obj?.forEach != null;
4
3
  }
5
- export function patchReadableFromWeb() {
6
- try {
7
- const originalReadableFromWeb = Readable.fromWeb;
8
- if (originalReadableFromWeb.name !== 'ReadableFromWebPatchedByWhatWgNode') {
9
- Readable.fromWeb = function ReadableFromWebPatchedByWhatWgNode(stream) {
10
- if (stream.readable != null) {
11
- return stream.readable;
12
- }
13
- return originalReadableFromWeb(stream);
14
- };
15
- }
16
- }
17
- catch (e) {
18
- console.warn('Could not patch Readable.fromWeb, so this might break Readable.fromWeb usage with the whatwg-node and the integrations like Fastify', e);
19
- }
20
- }
21
4
  export function getHeadersObj(headers) {
22
5
  if (headers == null || !isHeadersInstance(headers)) {
23
6
  return headers;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.5.12-alpha-20240531124053-0e72e3d9233a6ec341e6e573d51cf27d31020c75",
3
+ "version": "0.5.12",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -8,7 +8,7 @@
8
8
  "@whatwg-node/events": "^0.1.0",
9
9
  "busboy": "^1.6.0",
10
10
  "fast-querystring": "^1.1.1",
11
- "tslib": "^2.3.1"
11
+ "tslib": "^2.6.3"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  interface BlobOptions {
3
2
  /**
4
3
  * @default 'utf8'
package/typings/Blob.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  interface BlobOptions {
3
2
  /**
4
3
  * @default 'utf8'
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { Readable } from 'stream';
4
2
  import { PonyfillBlob } from './Blob.cjs';
5
3
  import { PonyfillFormData } from './FormData.cjs';
@@ -30,12 +28,17 @@ export declare class PonyfillBody<TJSON = any> implements Body {
30
28
  private _buffer?;
31
29
  private generateBody;
32
30
  get body(): PonyfillReadableStream<Uint8Array> | null;
31
+ _chunks: Uint8Array[] | null;
33
32
  _collectChunksFromReadable(): Promise<Uint8Array[]>;
33
+ _blob: PonyfillBlob | null;
34
34
  blob(): Promise<PonyfillBlob>;
35
+ _formData: PonyfillFormData | null;
35
36
  formData(opts?: {
36
37
  formDataLimits: FormDataLimits;
37
38
  }): Promise<PonyfillFormData>;
38
39
  arrayBuffer(): Promise<Buffer>;
40
+ _json: TJSON | null;
39
41
  json(): Promise<TJSON>;
42
+ _text: string | null;
40
43
  text(): Promise<string>;
41
44
  }
package/typings/Body.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import { Readable } from 'stream';
4
2
  import { PonyfillBlob } from './Blob.js';
5
3
  import { PonyfillFormData } from './FormData.js';
@@ -30,12 +28,17 @@ export declare class PonyfillBody<TJSON = any> implements Body {
30
28
  private _buffer?;
31
29
  private generateBody;
32
30
  get body(): PonyfillReadableStream<Uint8Array> | null;
31
+ _chunks: Uint8Array[] | null;
33
32
  _collectChunksFromReadable(): Promise<Uint8Array[]>;
33
+ _blob: PonyfillBlob | null;
34
34
  blob(): Promise<PonyfillBlob>;
35
+ _formData: PonyfillFormData | null;
35
36
  formData(opts?: {
36
37
  formDataLimits: FormDataLimits;
37
38
  }): Promise<PonyfillFormData>;
38
39
  arrayBuffer(): Promise<Buffer>;
40
+ _json: TJSON | null;
39
41
  json(): Promise<TJSON>;
42
+ _text: string | null;
40
43
  text(): Promise<string>;
41
44
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from 'stream';
3
2
  export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
4
3
  readable: Readable;
@@ -17,5 +16,4 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
17
16
  readable: ReadableStream<T2>;
18
17
  }): ReadableStream<T2>;
19
18
  static [Symbol.hasInstance](instance: unknown): instance is PonyfillReadableStream<unknown>;
20
- [Symbol.toStringTag]: string;
21
19
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from 'stream';
3
2
  export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
4
3
  readable: Readable;
@@ -17,5 +16,4 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
17
16
  readable: ReadableStream<T2>;
18
17
  }): ReadableStream<T2>;
19
18
  static [Symbol.hasInstance](instance: unknown): instance is PonyfillReadableStream<unknown>;
20
- [Symbol.toStringTag]: string;
21
19
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Agent } from 'http';
3
2
  import { BodyPonyfillInit, PonyfillBody, PonyfillBodyOptions } from './Body.cjs';
4
3
  import { PonyfillHeadersInit } from './Headers.cjs';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Agent } from 'http';
3
2
  import { BodyPonyfillInit, PonyfillBody, PonyfillBodyOptions } from './Body.js';
4
3
  import { PonyfillHeadersInit } from './Headers.js';
@@ -15,9 +15,8 @@ export declare class PonyfillResponse<TJSON = any> extends PonyfillBody<TJSON> i
15
15
  url: string;
16
16
  redirected: boolean;
17
17
  type: ResponseType;
18
- clone(): PonyfillResponse<any>;
18
+ clone(): this;
19
19
  static error(): PonyfillResponse<any>;
20
20
  static redirect(url: string, status?: number): PonyfillResponse<any>;
21
21
  static json<T = any>(data: T, init?: ResponsePonyfilInit): PonyfillResponse<T>;
22
- [Symbol.toStringTag]: string;
23
22
  }
@@ -15,9 +15,8 @@ export declare class PonyfillResponse<TJSON = any> extends PonyfillBody<TJSON> i
15
15
  url: string;
16
16
  redirected: boolean;
17
17
  type: ResponseType;
18
- clone(): PonyfillResponse<any>;
18
+ clone(): this;
19
19
  static error(): PonyfillResponse<any>;
20
20
  static redirect(url: string, status?: number): PonyfillResponse<any>;
21
21
  static json<T = any>(data: T, init?: ResponsePonyfilInit): PonyfillResponse<T>;
22
- [Symbol.toStringTag]: string;
23
22
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export declare class PonyfillTextEncoder implements TextEncoder {
3
2
  encoding: BufferEncoding;
4
3
  constructor(encoding?: BufferEncoding);
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export declare class PonyfillTextEncoder implements TextEncoder {
3
2
  encoding: BufferEncoding;
4
3
  constructor(encoding?: BufferEncoding);
package/typings/URL.d.cts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="packages/node-fetch/src/declarations.js" />
2
1
  import FastUrl from '@kamilkisiela/fast-url-parser';
3
2
  import { PonyfillBlob } from './Blob.cjs';
4
3
  import { PonyfillURLSearchParams } from './URLSearchParams.cjs';
package/typings/URL.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="packages/node-fetch/src/declarations.js" />
2
1
  import FastUrl from '@kamilkisiela/fast-url-parser';
3
2
  import { PonyfillBlob } from './Blob.js';
4
3
  import { PonyfillURLSearchParams } from './URLSearchParams.js';
@@ -1,4 +1,3 @@
1
- export { patchReadableFromWeb } from './utils.cjs';
2
1
  export { fetchPonyfill as fetch } from './fetch.cjs';
3
2
  export { PonyfillHeaders as Headers } from './Headers.cjs';
4
3
  export { PonyfillBody as Body } from './Body.cjs';
@@ -1,4 +1,3 @@
1
- export { patchReadableFromWeb } from './utils.js';
2
1
  export { fetchPonyfill as fetch } from './fetch.js';
3
2
  export { PonyfillHeaders as Headers } from './Headers.js';
4
3
  export { PonyfillBody as Body } from './Body.js';
@@ -1,6 +1,4 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from 'stream';
3
- export declare function patchReadableFromWeb(): void;
4
2
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
5
3
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
6
4
  export declare function fakePromise<T>(value: T): Promise<T>;
@@ -1,6 +1,4 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from 'stream';
3
- export declare function patchReadableFromWeb(): void;
4
2
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
5
3
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
6
4
  export declare function fakePromise<T>(value: T): Promise<T>;