@whatwg-node/node-fetch 0.4.13 → 0.4.14-rc-20230821132932-8b2ca5b

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/Blob.js CHANGED
@@ -11,10 +11,7 @@ function getBlobPartAsBuffer(blobPart) {
11
11
  else if (Buffer.isBuffer(blobPart)) {
12
12
  return blobPart;
13
13
  }
14
- else if (blobPart instanceof Uint8Array) {
15
- return Buffer.from(blobPart);
16
- }
17
- else if ('buffer' in blobPart) {
14
+ else if ((0, utils_js_1.isArrayBufferView)(blobPart)) {
18
15
  return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
19
16
  }
20
17
  else {
@@ -22,7 +19,7 @@ function getBlobPartAsBuffer(blobPart) {
22
19
  }
23
20
  }
24
21
  function isBlob(obj) {
25
- return obj != null && typeof obj === 'object' && obj.arrayBuffer != null;
22
+ return obj != null && obj.arrayBuffer != null;
26
23
  }
27
24
  // Will be removed after v14 reaches EOL
28
25
  // Needed because v14 doesn't have .stream() implemented
@@ -48,7 +45,7 @@ class PonyfillBlob {
48
45
  return Buffer.concat(bufferChunks);
49
46
  }
50
47
  arrayBuffer() {
51
- return this.buffer().then(utils_js_1.uint8ArrayToArrayBuffer);
48
+ return this.buffer().then(buf => buf.buffer);
52
49
  }
53
50
  async text() {
54
51
  let text = '';
@@ -56,7 +53,7 @@ class PonyfillBlob {
56
53
  if (typeof blobPart === 'string') {
57
54
  text += blobPart;
58
55
  }
59
- else if ('text' in blobPart) {
56
+ else if (isBlob(blobPart)) {
60
57
  text += await blobPart.text();
61
58
  }
62
59
  else {
@@ -75,10 +72,7 @@ class PonyfillBlob {
75
72
  else if (isBlob(blobPart)) {
76
73
  size += blobPart.size;
77
74
  }
78
- else if ('length' in blobPart) {
79
- size += blobPart.length;
80
- }
81
- else if ('byteLength' in blobPart) {
75
+ else if ((0, utils_js_1.isArrayBufferView)(blobPart)) {
82
76
  size += blobPart.byteLength;
83
77
  }
84
78
  }
package/cjs/Body.js CHANGED
@@ -29,11 +29,12 @@ class PonyfillBody {
29
29
  this.contentLength = null;
30
30
  this._bodyFactory = () => null;
31
31
  this._generatedBody = null;
32
- const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit);
32
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
33
33
  this._bodyFactory = bodyFactory;
34
34
  this.contentType = contentType;
35
35
  this.contentLength = contentLength;
36
36
  this.bodyType = bodyType;
37
+ this._buffer = buffer;
37
38
  }
38
39
  generateBody() {
39
40
  if (this._generatedBody) {
@@ -73,13 +74,8 @@ class PonyfillBody {
73
74
  if (this.bodyType === BodyInitType.ArrayBuffer) {
74
75
  return (0, utils_js_1.fakePromise)(this.bodyInit);
75
76
  }
76
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
77
- const typedBodyInit = this.bodyInit;
78
- return (0, utils_js_1.fakePromise)((0, utils_js_1.uint8ArrayToArrayBuffer)(typedBodyInit));
79
- }
80
- if (this.bodyType === BodyInitType.String) {
81
- const buffer = Buffer.from(this.bodyInit);
82
- return (0, utils_js_1.fakePromise)(buffer.buffer);
77
+ if (this._buffer) {
78
+ return (0, utils_js_1.fakePromise)(this._buffer.buffer);
83
79
  }
84
80
  if (this.bodyType === BodyInitType.Blob) {
85
81
  const blob = this.bodyInit;
@@ -111,19 +107,8 @@ class PonyfillBody {
111
107
  if (this.bodyType === BodyInitType.Blob) {
112
108
  return (0, utils_js_1.fakePromise)(this.bodyInit);
113
109
  }
114
- if (this.bodyType === BodyInitType.String ||
115
- this.bodyType === BodyInitType.Buffer ||
116
- this.bodyType === BodyInitType.Uint8Array) {
117
- const bodyInitTyped = this.bodyInit;
118
- const blob = new Blob_js_1.PonyfillBlob([bodyInitTyped], {
119
- type: this.contentType || '',
120
- });
121
- return (0, utils_js_1.fakePromise)(blob);
122
- }
123
- if (this.bodyType === BodyInitType.ArrayBuffer) {
124
- const bodyInitTyped = this.bodyInit;
125
- const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
126
- const blob = new Blob_js_1.PonyfillBlob([buf], {
110
+ if (this._buffer) {
111
+ const blob = new Blob_js_1.PonyfillBlob([this._buffer], {
127
112
  type: this.contentType || '',
128
113
  });
129
114
  return (0, utils_js_1.fakePromise)(blob);
@@ -173,7 +158,7 @@ class PonyfillBody {
173
158
  reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
174
159
  });
175
160
  fileStream.on('data', chunk => {
176
- chunks.push(Buffer.from(chunk));
161
+ chunks.push(chunk);
177
162
  });
178
163
  fileStream.on('close', () => {
179
164
  if (fileStream.truncated) {
@@ -199,16 +184,8 @@ class PonyfillBody {
199
184
  });
200
185
  }
201
186
  buffer() {
202
- if (this.bodyType === BodyInitType.Buffer) {
203
- return (0, utils_js_1.fakePromise)(this.bodyInit);
204
- }
205
- if (this.bodyType === BodyInitType.String) {
206
- return (0, utils_js_1.fakePromise)(Buffer.from(this.bodyInit));
207
- }
208
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
209
- const bodyInitTyped = this.bodyInit;
210
- const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
211
- return (0, utils_js_1.fakePromise)(buffer);
187
+ if (this._buffer) {
188
+ return (0, utils_js_1.fakePromise)(Buffer.from(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength));
212
189
  }
213
190
  if (this.bodyType === BodyInitType.Blob) {
214
191
  if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) {
@@ -247,6 +224,7 @@ function processBodyInit(bodyInit) {
247
224
  bodyType: BodyInitType.String,
248
225
  contentType: 'text/plain;charset=UTF-8',
249
226
  contentLength,
227
+ buffer,
250
228
  bodyFactory() {
251
229
  const readable = stream_1.Readable.from(buffer);
252
230
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -259,6 +237,7 @@ function processBodyInit(bodyInit) {
259
237
  bodyType: BodyInitType.Buffer,
260
238
  contentLength,
261
239
  contentType: null,
240
+ buffer: bodyInit,
262
241
  bodyFactory() {
263
242
  const readable = stream_1.Readable.from(bodyInit);
264
243
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -284,26 +263,15 @@ function processBodyInit(bodyInit) {
284
263
  },
285
264
  };
286
265
  }
287
- if (bodyInit instanceof Uint8Array) {
266
+ if ((0, utils_js_1.isArrayBufferView)(bodyInit)) {
288
267
  const contentLength = bodyInit.byteLength;
268
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
289
269
  return {
290
270
  bodyType: BodyInitType.Uint8Array,
271
+ buffer,
291
272
  contentLength,
292
273
  contentType: null,
293
274
  bodyFactory() {
294
- const readable = stream_1.Readable.from(bodyInit);
295
- const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
296
- return body;
297
- },
298
- };
299
- }
300
- if ('buffer' in bodyInit) {
301
- const contentLength = bodyInit.byteLength;
302
- return {
303
- contentLength,
304
- contentType: null,
305
- bodyFactory() {
306
- const buffer = Buffer.from(bodyInit);
307
275
  const readable = stream_1.Readable.from(buffer);
308
276
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
309
277
  return body;
@@ -312,12 +280,13 @@ function processBodyInit(bodyInit) {
312
280
  }
313
281
  if (bodyInit instanceof ArrayBuffer) {
314
282
  const contentLength = bodyInit.byteLength;
283
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
315
284
  return {
316
285
  bodyType: BodyInitType.ArrayBuffer,
317
286
  contentType: null,
318
287
  contentLength,
288
+ buffer,
319
289
  bodyFactory() {
320
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
321
290
  const readable = stream_1.Readable.from(buffer);
322
291
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
323
292
  return body;
@@ -335,8 +304,9 @@ function processBodyInit(bodyInit) {
335
304
  },
336
305
  };
337
306
  }
338
- if ('stream' in bodyInit) {
307
+ if (isBlob(bodyInit)) {
339
308
  return {
309
+ bodyType: BodyInitType.Blob,
340
310
  contentType: bodyInit.type,
341
311
  contentLength: bodyInit.size,
342
312
  bodyFactory() {
@@ -346,7 +316,7 @@ function processBodyInit(bodyInit) {
346
316
  },
347
317
  };
348
318
  }
349
- if ('sort' in bodyInit) {
319
+ if (isURLSearchParams(bodyInit)) {
350
320
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
351
321
  return {
352
322
  bodyType: BodyInitType.String,
@@ -358,10 +328,11 @@ function processBodyInit(bodyInit) {
358
328
  },
359
329
  };
360
330
  }
361
- if ('forEach' in bodyInit) {
331
+ if (isFormData(bodyInit)) {
362
332
  const boundary = Math.random().toString(36).substr(2);
363
333
  const contentType = `multipart/form-data; boundary=${boundary}`;
364
334
  return {
335
+ bodyType: BodyInitType.FormData,
365
336
  contentType,
366
337
  contentLength: null,
367
338
  bodyFactory() {
@@ -381,3 +352,12 @@ function processBodyInit(bodyInit) {
381
352
  }
382
353
  throw new Error('Unknown body type');
383
354
  }
355
+ function isFormData(value) {
356
+ return value?.forEach != null;
357
+ }
358
+ function isBlob(value) {
359
+ return value?.stream != null;
360
+ }
361
+ function isURLSearchParams(value) {
362
+ return value?.sort != null;
363
+ }
package/cjs/FormData.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getStreamFromFormData = exports.PonyfillFormData = void 0;
4
- const File_js_1 = require("./File.js");
5
4
  const ReadableStream_js_1 = require("./ReadableStream.js");
6
5
  class PonyfillFormData {
7
6
  constructor() {
@@ -118,17 +117,9 @@ function getStreamFromFormData(formData, boundary = '---') {
118
117
  }
119
118
  exports.getStreamFromFormData = getStreamFromFormData;
120
119
  function getNormalizedFile(name, blob, fileName) {
121
- if (blob instanceof File_js_1.PonyfillFile) {
122
- if (fileName != null) {
123
- return new File_js_1.PonyfillFile([blob], fileName, {
124
- type: blob.type,
125
- lastModified: blob.lastModified,
126
- });
127
- }
128
- return blob;
129
- }
130
- return new File_js_1.PonyfillFile([blob], fileName || name, { type: blob.type });
120
+ blob.name = fileName || blob.name || name;
121
+ return blob;
131
122
  }
132
123
  function isBlob(value) {
133
- return value != null && typeof value === 'object' && typeof value.arrayBuffer === 'function';
124
+ return value?.arrayBuffer != null;
134
125
  }
@@ -43,16 +43,22 @@ function createController(desiredSize, readable) {
43
43
  },
44
44
  };
45
45
  }
46
+ function isNodeReadable(obj) {
47
+ return obj?.read != null;
48
+ }
49
+ function isReadableStream(obj) {
50
+ return obj?.getReader != null;
51
+ }
46
52
  class PonyfillReadableStream {
47
53
  constructor(underlyingSource) {
48
54
  this.locked = false;
49
55
  if (underlyingSource instanceof PonyfillReadableStream) {
50
56
  this.readable = underlyingSource.readable;
51
57
  }
52
- else if (underlyingSource && 'read' in underlyingSource) {
58
+ else if (isNodeReadable(underlyingSource)) {
53
59
  this.readable = underlyingSource;
54
60
  }
55
- else if (underlyingSource && 'getReader' in underlyingSource) {
61
+ else if (isReadableStream(underlyingSource)) {
56
62
  let reader;
57
63
  let started = false;
58
64
  this.readable = new stream_1.Readable({
@@ -162,7 +168,7 @@ class PonyfillReadableStream {
162
168
  return readable;
163
169
  }
164
170
  static [Symbol.hasInstance](instance) {
165
- return instance != null && typeof instance === 'object' && 'getReader' in instance;
171
+ return isReadableStream(instance);
166
172
  }
167
173
  }
168
174
  exports.PonyfillReadableStream = PonyfillReadableStream;
package/cjs/Request.js CHANGED
@@ -6,6 +6,9 @@ const Headers_js_1 = require("./Headers.js");
6
6
  function isRequest(input) {
7
7
  return input[Symbol.toStringTag] === 'Request';
8
8
  }
9
+ function isURL(obj) {
10
+ return obj?.href != null;
11
+ }
9
12
  class PonyfillRequest extends Body_js_1.PonyfillBody {
10
13
  constructor(input, options) {
11
14
  let url;
@@ -14,7 +17,7 @@ class PonyfillRequest extends Body_js_1.PonyfillBody {
14
17
  if (typeof input === 'string') {
15
18
  url = input;
16
19
  }
17
- else if ('href' in input) {
20
+ else if (isURL(input)) {
18
21
  url = input.toString();
19
22
  }
20
23
  else if (isRequest(input)) {
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillURLSearchParams = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fast_querystring_1 = tslib_1.__importDefault(require("fast-querystring"));
6
+ function isURLSearchParams(value) {
7
+ return value?.entries != null;
8
+ }
6
9
  class PonyfillURLSearchParams {
7
10
  constructor(init) {
8
11
  if (init) {
@@ -15,7 +18,7 @@ class PonyfillURLSearchParams {
15
18
  this.params[key] = value;
16
19
  }
17
20
  }
18
- else if ('entries' in init) {
21
+ else if (isURLSearchParams(init)) {
19
22
  this.params = {};
20
23
  for (const [key, value] of init.entries()) {
21
24
  this.params[key] = value;
package/cjs/fetch.js CHANGED
@@ -35,8 +35,11 @@ function getResponseForDataUri(url) {
35
35
  },
36
36
  });
37
37
  }
38
+ function isURL(obj) {
39
+ return obj != null && obj.href != null;
40
+ }
38
41
  function fetchPonyfill(info, init) {
39
- if (typeof info === 'string' || 'href' in info) {
42
+ if (typeof info === 'string' || isURL(info)) {
40
43
  const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
41
44
  return fetchPonyfill(ponyfillRequest);
42
45
  }
package/cjs/fetchCurl.js CHANGED
@@ -24,7 +24,7 @@ function fetchCurl(fetchRequest) {
24
24
  }
25
25
  else {
26
26
  const nodeReadable = (fetchRequest.body != null
27
- ? 'pipe' in fetchRequest.body
27
+ ? (0, utils_js_1.isNodeReadable)(fetchRequest.body)
28
28
  ? fetchRequest.body
29
29
  : node_stream_1.Readable.from(fetchRequest.body)
30
30
  : null);
@@ -24,7 +24,7 @@ function fetchNodeHttp(fetchRequest) {
24
24
  try {
25
25
  const requestFn = getRequestFnForProtocol(fetchRequest.url);
26
26
  const nodeReadable = (fetchRequest.body != null
27
- ? 'pipe' in fetchRequest.body
27
+ ? (0, utils_js_1.isNodeReadable)(fetchRequest.body)
28
28
  ? fetchRequest.body
29
29
  : stream_1.Readable.from(fetchRequest.body)
30
30
  : null);
package/cjs/utils.js CHANGED
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fakePromise = exports.defaultHeadersSerializer = exports.uint8ArrayToArrayBuffer = exports.getHeadersObj = void 0;
3
+ exports.isNodeReadable = exports.isArrayBufferView = exports.fakePromise = exports.defaultHeadersSerializer = exports.getHeadersObj = void 0;
4
+ function isHeadersInstance(obj) {
5
+ return obj?.forEach != null;
6
+ }
4
7
  function getHeadersObj(headers) {
5
- if (headers == null || !('forEach' in headers)) {
8
+ if (headers == null || !isHeadersInstance(headers)) {
6
9
  return headers;
7
10
  }
8
11
  const obj = {};
@@ -12,10 +15,6 @@ function getHeadersObj(headers) {
12
15
  return obj;
13
16
  }
14
17
  exports.getHeadersObj = getHeadersObj;
15
- function uint8ArrayToArrayBuffer(uint8array) {
16
- return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
17
- }
18
- exports.uint8ArrayToArrayBuffer = uint8ArrayToArrayBuffer;
19
18
  function defaultHeadersSerializer(headers, onContentLength) {
20
19
  const headerArray = [];
21
20
  headers.forEach((value, key) => {
@@ -61,3 +60,15 @@ function fakePromise(value) {
61
60
  };
62
61
  }
63
62
  exports.fakePromise = fakePromise;
63
+ function isArrayBufferView(obj) {
64
+ return (obj != null &&
65
+ typeof obj === 'object' &&
66
+ obj.buffer != null &&
67
+ obj.byteLength != null &&
68
+ obj.byteOffset != null);
69
+ }
70
+ exports.isArrayBufferView = isArrayBufferView;
71
+ function isNodeReadable(obj) {
72
+ return obj != null && obj.pipe != null;
73
+ }
74
+ exports.isNodeReadable = isNodeReadable;
package/esm/Blob.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
2
2
  import { PonyfillReadableStream } from './ReadableStream.js';
3
- import { uint8ArrayToArrayBuffer } from './utils.js';
3
+ import { isArrayBufferView } from './utils.js';
4
4
  function getBlobPartAsBuffer(blobPart) {
5
5
  if (typeof blobPart === 'string') {
6
6
  return Buffer.from(blobPart);
@@ -8,10 +8,7 @@ function getBlobPartAsBuffer(blobPart) {
8
8
  else if (Buffer.isBuffer(blobPart)) {
9
9
  return blobPart;
10
10
  }
11
- else if (blobPart instanceof Uint8Array) {
12
- return Buffer.from(blobPart);
13
- }
14
- else if ('buffer' in blobPart) {
11
+ else if (isArrayBufferView(blobPart)) {
15
12
  return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
16
13
  }
17
14
  else {
@@ -19,7 +16,7 @@ function getBlobPartAsBuffer(blobPart) {
19
16
  }
20
17
  }
21
18
  function isBlob(obj) {
22
- return obj != null && typeof obj === 'object' && obj.arrayBuffer != null;
19
+ return obj != null && obj.arrayBuffer != null;
23
20
  }
24
21
  // Will be removed after v14 reaches EOL
25
22
  // Needed because v14 doesn't have .stream() implemented
@@ -45,7 +42,7 @@ export class PonyfillBlob {
45
42
  return Buffer.concat(bufferChunks);
46
43
  }
47
44
  arrayBuffer() {
48
- return this.buffer().then(uint8ArrayToArrayBuffer);
45
+ return this.buffer().then(buf => buf.buffer);
49
46
  }
50
47
  async text() {
51
48
  let text = '';
@@ -53,7 +50,7 @@ export class PonyfillBlob {
53
50
  if (typeof blobPart === 'string') {
54
51
  text += blobPart;
55
52
  }
56
- else if ('text' in blobPart) {
53
+ else if (isBlob(blobPart)) {
57
54
  text += await blobPart.text();
58
55
  }
59
56
  else {
@@ -72,10 +69,7 @@ export class PonyfillBlob {
72
69
  else if (isBlob(blobPart)) {
73
70
  size += blobPart.size;
74
71
  }
75
- else if ('length' in blobPart) {
76
- size += blobPart.length;
77
- }
78
- else if ('byteLength' in blobPart) {
72
+ else if (isArrayBufferView(blobPart)) {
79
73
  size += blobPart.byteLength;
80
74
  }
81
75
  }
package/esm/Body.js CHANGED
@@ -4,7 +4,7 @@ import { PonyfillBlob } from './Blob.js';
4
4
  import { PonyfillFile } from './File.js';
5
5
  import { getStreamFromFormData, PonyfillFormData } from './FormData.js';
6
6
  import { PonyfillReadableStream } from './ReadableStream.js';
7
- import { fakePromise, uint8ArrayToArrayBuffer } from './utils.js';
7
+ import { fakePromise, isArrayBufferView } from './utils.js';
8
8
  var BodyInitType;
9
9
  (function (BodyInitType) {
10
10
  BodyInitType["ReadableStream"] = "ReadableStream";
@@ -25,11 +25,12 @@ export class PonyfillBody {
25
25
  this.contentLength = null;
26
26
  this._bodyFactory = () => null;
27
27
  this._generatedBody = null;
28
- const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit);
28
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
29
29
  this._bodyFactory = bodyFactory;
30
30
  this.contentType = contentType;
31
31
  this.contentLength = contentLength;
32
32
  this.bodyType = bodyType;
33
+ this._buffer = buffer;
33
34
  }
34
35
  generateBody() {
35
36
  if (this._generatedBody) {
@@ -69,13 +70,8 @@ export class PonyfillBody {
69
70
  if (this.bodyType === BodyInitType.ArrayBuffer) {
70
71
  return fakePromise(this.bodyInit);
71
72
  }
72
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
73
- const typedBodyInit = this.bodyInit;
74
- return fakePromise(uint8ArrayToArrayBuffer(typedBodyInit));
75
- }
76
- if (this.bodyType === BodyInitType.String) {
77
- const buffer = Buffer.from(this.bodyInit);
78
- return fakePromise(buffer.buffer);
73
+ if (this._buffer) {
74
+ return fakePromise(this._buffer.buffer);
79
75
  }
80
76
  if (this.bodyType === BodyInitType.Blob) {
81
77
  const blob = this.bodyInit;
@@ -107,19 +103,8 @@ export class PonyfillBody {
107
103
  if (this.bodyType === BodyInitType.Blob) {
108
104
  return fakePromise(this.bodyInit);
109
105
  }
110
- if (this.bodyType === BodyInitType.String ||
111
- this.bodyType === BodyInitType.Buffer ||
112
- this.bodyType === BodyInitType.Uint8Array) {
113
- const bodyInitTyped = this.bodyInit;
114
- const blob = new PonyfillBlob([bodyInitTyped], {
115
- type: this.contentType || '',
116
- });
117
- return fakePromise(blob);
118
- }
119
- if (this.bodyType === BodyInitType.ArrayBuffer) {
120
- const bodyInitTyped = this.bodyInit;
121
- const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
122
- const blob = new PonyfillBlob([buf], {
106
+ if (this._buffer) {
107
+ const blob = new PonyfillBlob([this._buffer], {
123
108
  type: this.contentType || '',
124
109
  });
125
110
  return fakePromise(blob);
@@ -169,7 +154,7 @@ export class PonyfillBody {
169
154
  reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
170
155
  });
171
156
  fileStream.on('data', chunk => {
172
- chunks.push(Buffer.from(chunk));
157
+ chunks.push(chunk);
173
158
  });
174
159
  fileStream.on('close', () => {
175
160
  if (fileStream.truncated) {
@@ -195,16 +180,8 @@ export class PonyfillBody {
195
180
  });
196
181
  }
197
182
  buffer() {
198
- if (this.bodyType === BodyInitType.Buffer) {
199
- return fakePromise(this.bodyInit);
200
- }
201
- if (this.bodyType === BodyInitType.String) {
202
- return fakePromise(Buffer.from(this.bodyInit));
203
- }
204
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
205
- const bodyInitTyped = this.bodyInit;
206
- const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
207
- return fakePromise(buffer);
183
+ if (this._buffer) {
184
+ return fakePromise(Buffer.from(this._buffer.buffer, this._buffer.byteOffset, this._buffer.byteLength));
208
185
  }
209
186
  if (this.bodyType === BodyInitType.Blob) {
210
187
  if (this.bodyInit instanceof PonyfillBlob) {
@@ -242,6 +219,7 @@ function processBodyInit(bodyInit) {
242
219
  bodyType: BodyInitType.String,
243
220
  contentType: 'text/plain;charset=UTF-8',
244
221
  contentLength,
222
+ buffer,
245
223
  bodyFactory() {
246
224
  const readable = Readable.from(buffer);
247
225
  return new PonyfillReadableStream(readable);
@@ -254,6 +232,7 @@ function processBodyInit(bodyInit) {
254
232
  bodyType: BodyInitType.Buffer,
255
233
  contentLength,
256
234
  contentType: null,
235
+ buffer: bodyInit,
257
236
  bodyFactory() {
258
237
  const readable = Readable.from(bodyInit);
259
238
  const body = new PonyfillReadableStream(readable);
@@ -279,26 +258,15 @@ function processBodyInit(bodyInit) {
279
258
  },
280
259
  };
281
260
  }
282
- if (bodyInit instanceof Uint8Array) {
261
+ if (isArrayBufferView(bodyInit)) {
283
262
  const contentLength = bodyInit.byteLength;
263
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
284
264
  return {
285
265
  bodyType: BodyInitType.Uint8Array,
266
+ buffer,
286
267
  contentLength,
287
268
  contentType: null,
288
269
  bodyFactory() {
289
- const readable = Readable.from(bodyInit);
290
- const body = new PonyfillReadableStream(readable);
291
- return body;
292
- },
293
- };
294
- }
295
- if ('buffer' in bodyInit) {
296
- const contentLength = bodyInit.byteLength;
297
- return {
298
- contentLength,
299
- contentType: null,
300
- bodyFactory() {
301
- const buffer = Buffer.from(bodyInit);
302
270
  const readable = Readable.from(buffer);
303
271
  const body = new PonyfillReadableStream(readable);
304
272
  return body;
@@ -307,12 +275,13 @@ function processBodyInit(bodyInit) {
307
275
  }
308
276
  if (bodyInit instanceof ArrayBuffer) {
309
277
  const contentLength = bodyInit.byteLength;
278
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
310
279
  return {
311
280
  bodyType: BodyInitType.ArrayBuffer,
312
281
  contentType: null,
313
282
  contentLength,
283
+ buffer,
314
284
  bodyFactory() {
315
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
316
285
  const readable = Readable.from(buffer);
317
286
  const body = new PonyfillReadableStream(readable);
318
287
  return body;
@@ -330,8 +299,9 @@ function processBodyInit(bodyInit) {
330
299
  },
331
300
  };
332
301
  }
333
- if ('stream' in bodyInit) {
302
+ if (isBlob(bodyInit)) {
334
303
  return {
304
+ bodyType: BodyInitType.Blob,
335
305
  contentType: bodyInit.type,
336
306
  contentLength: bodyInit.size,
337
307
  bodyFactory() {
@@ -341,7 +311,7 @@ function processBodyInit(bodyInit) {
341
311
  },
342
312
  };
343
313
  }
344
- if ('sort' in bodyInit) {
314
+ if (isURLSearchParams(bodyInit)) {
345
315
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
346
316
  return {
347
317
  bodyType: BodyInitType.String,
@@ -353,10 +323,11 @@ function processBodyInit(bodyInit) {
353
323
  },
354
324
  };
355
325
  }
356
- if ('forEach' in bodyInit) {
326
+ if (isFormData(bodyInit)) {
357
327
  const boundary = Math.random().toString(36).substr(2);
358
328
  const contentType = `multipart/form-data; boundary=${boundary}`;
359
329
  return {
330
+ bodyType: BodyInitType.FormData,
360
331
  contentType,
361
332
  contentLength: null,
362
333
  bodyFactory() {
@@ -376,3 +347,12 @@ function processBodyInit(bodyInit) {
376
347
  }
377
348
  throw new Error('Unknown body type');
378
349
  }
350
+ function isFormData(value) {
351
+ return value?.forEach != null;
352
+ }
353
+ function isBlob(value) {
354
+ return value?.stream != null;
355
+ }
356
+ function isURLSearchParams(value) {
357
+ return value?.sort != null;
358
+ }
package/esm/FormData.js CHANGED
@@ -1,4 +1,3 @@
1
- import { PonyfillFile } from './File.js';
2
1
  import { PonyfillReadableStream } from './ReadableStream.js';
3
2
  export class PonyfillFormData {
4
3
  constructor() {
@@ -113,17 +112,9 @@ export function getStreamFromFormData(formData, boundary = '---') {
113
112
  });
114
113
  }
115
114
  function getNormalizedFile(name, blob, fileName) {
116
- if (blob instanceof PonyfillFile) {
117
- if (fileName != null) {
118
- return new PonyfillFile([blob], fileName, {
119
- type: blob.type,
120
- lastModified: blob.lastModified,
121
- });
122
- }
123
- return blob;
124
- }
125
- return new PonyfillFile([blob], fileName || name, { type: blob.type });
115
+ blob.name = fileName || blob.name || name;
116
+ return blob;
126
117
  }
127
118
  function isBlob(value) {
128
- return value != null && typeof value === 'object' && typeof value.arrayBuffer === 'function';
119
+ return value?.arrayBuffer != null;
129
120
  }
@@ -40,16 +40,22 @@ function createController(desiredSize, readable) {
40
40
  },
41
41
  };
42
42
  }
43
+ function isNodeReadable(obj) {
44
+ return obj?.read != null;
45
+ }
46
+ function isReadableStream(obj) {
47
+ return obj?.getReader != null;
48
+ }
43
49
  export class PonyfillReadableStream {
44
50
  constructor(underlyingSource) {
45
51
  this.locked = false;
46
52
  if (underlyingSource instanceof PonyfillReadableStream) {
47
53
  this.readable = underlyingSource.readable;
48
54
  }
49
- else if (underlyingSource && 'read' in underlyingSource) {
55
+ else if (isNodeReadable(underlyingSource)) {
50
56
  this.readable = underlyingSource;
51
57
  }
52
- else if (underlyingSource && 'getReader' in underlyingSource) {
58
+ else if (isReadableStream(underlyingSource)) {
53
59
  let reader;
54
60
  let started = false;
55
61
  this.readable = new Readable({
@@ -159,6 +165,6 @@ export class PonyfillReadableStream {
159
165
  return readable;
160
166
  }
161
167
  static [Symbol.hasInstance](instance) {
162
- return instance != null && typeof instance === 'object' && 'getReader' in instance;
168
+ return isReadableStream(instance);
163
169
  }
164
170
  }
package/esm/Request.js CHANGED
@@ -3,6 +3,9 @@ import { isHeadersLike, PonyfillHeaders } from './Headers.js';
3
3
  function isRequest(input) {
4
4
  return input[Symbol.toStringTag] === 'Request';
5
5
  }
6
+ function isURL(obj) {
7
+ return obj?.href != null;
8
+ }
6
9
  export class PonyfillRequest extends PonyfillBody {
7
10
  constructor(input, options) {
8
11
  let url;
@@ -11,7 +14,7 @@ export class PonyfillRequest extends PonyfillBody {
11
14
  if (typeof input === 'string') {
12
15
  url = input;
13
16
  }
14
- else if ('href' in input) {
17
+ else if (isURL(input)) {
15
18
  url = input.toString();
16
19
  }
17
20
  else if (isRequest(input)) {
@@ -1,4 +1,7 @@
1
1
  import FastQuerystring from 'fast-querystring';
2
+ function isURLSearchParams(value) {
3
+ return value?.entries != null;
4
+ }
2
5
  export class PonyfillURLSearchParams {
3
6
  constructor(init) {
4
7
  if (init) {
@@ -11,7 +14,7 @@ export class PonyfillURLSearchParams {
11
14
  this.params[key] = value;
12
15
  }
13
16
  }
14
- else if ('entries' in init) {
17
+ else if (isURLSearchParams(init)) {
15
18
  this.params = {};
16
19
  for (const [key, value] of init.entries()) {
17
20
  this.params[key] = value;
package/esm/fetch.js CHANGED
@@ -32,8 +32,11 @@ function getResponseForDataUri(url) {
32
32
  },
33
33
  });
34
34
  }
35
+ function isURL(obj) {
36
+ return obj != null && obj.href != null;
37
+ }
35
38
  export function fetchPonyfill(info, init) {
36
- if (typeof info === 'string' || 'href' in info) {
39
+ if (typeof info === 'string' || isURL(info)) {
37
40
  const ponyfillRequest = new PonyfillRequest(info, init);
38
41
  return fetchPonyfill(ponyfillRequest);
39
42
  }
package/esm/fetchCurl.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { PonyfillAbortError } from './AbortError.js';
3
3
  import { PonyfillResponse } from './Response.js';
4
- import { defaultHeadersSerializer } from './utils.js';
4
+ import { defaultHeadersSerializer, isNodeReadable } from './utils.js';
5
5
  export function fetchCurl(fetchRequest) {
6
6
  const { Curl, CurlCode, CurlFeature, CurlPause, CurlProgressFunc } = globalThis['libcurl'];
7
7
  const curlHandle = new Curl();
@@ -21,7 +21,7 @@ export function fetchCurl(fetchRequest) {
21
21
  }
22
22
  else {
23
23
  const nodeReadable = (fetchRequest.body != null
24
- ? 'pipe' in fetchRequest.body
24
+ ? isNodeReadable(fetchRequest.body)
25
25
  ? fetchRequest.body
26
26
  : Readable.from(fetchRequest.body)
27
27
  : null);
@@ -6,7 +6,7 @@ import { PonyfillAbortError } from './AbortError.js';
6
6
  import { PonyfillRequest } from './Request.js';
7
7
  import { PonyfillResponse } from './Response.js';
8
8
  import { PonyfillURL } from './URL.js';
9
- import { getHeadersObj } from './utils.js';
9
+ import { getHeadersObj, isNodeReadable } from './utils.js';
10
10
  function getRequestFnForProtocol(url) {
11
11
  if (url.startsWith('http:')) {
12
12
  return httpRequest;
@@ -21,7 +21,7 @@ export function fetchNodeHttp(fetchRequest) {
21
21
  try {
22
22
  const requestFn = getRequestFnForProtocol(fetchRequest.url);
23
23
  const nodeReadable = (fetchRequest.body != null
24
- ? 'pipe' in fetchRequest.body
24
+ ? isNodeReadable(fetchRequest.body)
25
25
  ? fetchRequest.body
26
26
  : Readable.from(fetchRequest.body)
27
27
  : null);
package/esm/utils.js CHANGED
@@ -1,5 +1,8 @@
1
+ function isHeadersInstance(obj) {
2
+ return obj?.forEach != null;
3
+ }
1
4
  export function getHeadersObj(headers) {
2
- if (headers == null || !('forEach' in headers)) {
5
+ if (headers == null || !isHeadersInstance(headers)) {
3
6
  return headers;
4
7
  }
5
8
  const obj = {};
@@ -8,9 +11,6 @@ export function getHeadersObj(headers) {
8
11
  });
9
12
  return obj;
10
13
  }
11
- export function uint8ArrayToArrayBuffer(uint8array) {
12
- return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
13
- }
14
14
  export function defaultHeadersSerializer(headers, onContentLength) {
15
15
  const headerArray = [];
16
16
  headers.forEach((value, key) => {
@@ -54,3 +54,13 @@ export function fakePromise(value) {
54
54
  [Symbol.toStringTag]: 'Promise',
55
55
  };
56
56
  }
57
+ export function isArrayBufferView(obj) {
58
+ return (obj != null &&
59
+ typeof obj === 'object' &&
60
+ obj.buffer != null &&
61
+ obj.byteLength != null &&
62
+ obj.byteOffset != null);
63
+ }
64
+ export function isNodeReadable(obj) {
65
+ return obj != null && obj.pipe != null;
66
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.4.13",
3
+ "version": "0.4.14-rc-20230821132932-8b2ca5b",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -17,7 +17,7 @@ export declare class PonyfillBlob implements Blob {
17
17
  private encoding;
18
18
  constructor(blobParts: BlobPart[], options?: BlobOptions);
19
19
  buffer(): Promise<Buffer>;
20
- arrayBuffer(): Promise<ArrayBuffer>;
20
+ arrayBuffer(): Promise<ArrayBuffer | SharedArrayBuffer>;
21
21
  text(): Promise<string>;
22
22
  get size(): number;
23
23
  stream(): any;
package/typings/Blob.d.ts CHANGED
@@ -17,7 +17,7 @@ export declare class PonyfillBlob implements Blob {
17
17
  private encoding;
18
18
  constructor(blobParts: BlobPart[], options?: BlobOptions);
19
19
  buffer(): Promise<Buffer>;
20
- arrayBuffer(): Promise<ArrayBuffer>;
20
+ arrayBuffer(): Promise<ArrayBuffer | SharedArrayBuffer>;
21
21
  text(): Promise<string>;
22
22
  get size(): number;
23
23
  stream(): any;
@@ -27,6 +27,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
27
27
  private bodyType?;
28
28
  private _bodyFactory;
29
29
  private _generatedBody;
30
+ private _buffer?;
30
31
  private generateBody;
31
32
  get body(): PonyfillReadableStream<Uint8Array> | null;
32
33
  arrayBuffer(): Promise<ArrayBuffer>;
package/typings/Body.d.ts CHANGED
@@ -27,6 +27,7 @@ export declare class PonyfillBody<TJSON = any> implements Body {
27
27
  private bodyType?;
28
28
  private _bodyFactory;
29
29
  private _generatedBody;
30
+ private _buffer?;
30
31
  private generateBody;
31
32
  get body(): PonyfillReadableStream<Uint8Array> | null;
32
33
  arrayBuffer(): Promise<ArrayBuffer>;
@@ -1,4 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'node:stream';
1
3
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
2
- export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;
3
4
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
4
5
  export declare function fakePromise<T>(value: T): Promise<T>;
6
+ export declare function isArrayBufferView(obj: any): obj is ArrayBufferView;
7
+ export declare function isNodeReadable(obj: any): obj is Readable;
@@ -1,4 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'node:stream';
1
3
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
2
- export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;
3
4
  export declare function defaultHeadersSerializer(headers: Headers, onContentLength?: (value: string) => void): string[];
4
5
  export declare function fakePromise<T>(value: T): Promise<T>;
6
+ export declare function isArrayBufferView(obj: any): obj is ArrayBufferView;
7
+ export declare function isNodeReadable(obj: any): obj is Readable;