@whatwg-node/node-fetch 0.5.0-alpha-20230710180612-32e574f → 0.5.0-alpha-20231025080723-49677d8

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
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillBlob = void 0;
4
+ /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
4
5
  const ReadableStream_js_1 = require("./ReadableStream.js");
5
6
  const utils_js_1 = require("./utils.js");
6
7
  function getBlobPartAsBuffer(blobPart) {
@@ -10,10 +11,7 @@ function getBlobPartAsBuffer(blobPart) {
10
11
  else if (Buffer.isBuffer(blobPart)) {
11
12
  return blobPart;
12
13
  }
13
- else if (blobPart instanceof Uint8Array) {
14
- return Buffer.from(blobPart);
15
- }
16
- else if ('buffer' in blobPart) {
14
+ else if ((0, utils_js_1.isArrayBufferView)(blobPart)) {
17
15
  return Buffer.from(blobPart.buffer, blobPart.byteOffset, blobPart.byteLength);
18
16
  }
19
17
  else {
@@ -21,93 +19,115 @@ function getBlobPartAsBuffer(blobPart) {
21
19
  }
22
20
  }
23
21
  function isBlob(obj) {
24
- return obj != null && typeof obj === 'object' && obj.arrayBuffer != null;
22
+ return obj != null && obj.arrayBuffer != null;
25
23
  }
26
24
  // Will be removed after v14 reaches EOL
27
25
  // Needed because v14 doesn't have .stream() implemented
28
26
  class PonyfillBlob {
29
27
  constructor(blobParts, options) {
30
28
  this.blobParts = blobParts;
29
+ this._size = null;
31
30
  this.type = options?.type || 'application/octet-stream';
32
31
  this.encoding = options?.encoding || 'utf8';
32
+ this._size = options?.size || null;
33
+ if (blobParts.length === 1 && isBlob(blobParts[0])) {
34
+ return blobParts[0];
35
+ }
33
36
  }
34
- async buffer() {
35
- const bufferChunks = [];
36
- for (const blobPart of this.blobParts) {
37
+ arrayBuffer() {
38
+ if (this.blobParts.length === 1) {
39
+ const blobPart = this.blobParts[0];
37
40
  if (isBlob(blobPart)) {
38
- const arrayBuf = await blobPart.arrayBuffer();
39
- const buf = Buffer.from(arrayBuf, undefined, blobPart.size);
40
- bufferChunks.push(buf);
41
+ return blobPart.arrayBuffer();
42
+ }
43
+ return (0, utils_js_1.fakePromise)(getBlobPartAsBuffer(blobPart));
44
+ }
45
+ const jobs = [];
46
+ const bufferChunks = this.blobParts.map((blobPart, i) => {
47
+ if (isBlob(blobPart)) {
48
+ jobs.push(blobPart.arrayBuffer().then(arrayBuf => {
49
+ bufferChunks[i] = Buffer.from(arrayBuf, undefined, blobPart.size);
50
+ }));
51
+ return undefined;
41
52
  }
42
53
  else {
43
- const buf = getBlobPartAsBuffer(blobPart);
44
- bufferChunks.push(buf);
54
+ return getBlobPartAsBuffer(blobPart);
45
55
  }
56
+ });
57
+ if (jobs.length > 0) {
58
+ return Promise.all(jobs).then(() => Buffer.concat(bufferChunks, this._size || undefined));
46
59
  }
47
- return Buffer.concat(bufferChunks);
60
+ return (0, utils_js_1.fakePromise)(Buffer.concat(bufferChunks, this._size || undefined));
48
61
  }
49
- async arrayBuffer() {
50
- const buffer = await this.buffer();
51
- return (0, utils_js_1.uint8ArrayToArrayBuffer)(buffer);
52
- }
53
- async text() {
54
- let text = '';
55
- for (const blobPart of this.blobParts) {
62
+ text() {
63
+ if (this.blobParts.length === 1) {
64
+ const blobPart = this.blobParts[0];
56
65
  if (typeof blobPart === 'string') {
57
- text += blobPart;
58
- }
59
- else if ('text' in blobPart) {
60
- text += await blobPart.text();
66
+ return (0, utils_js_1.fakePromise)(blobPart);
61
67
  }
62
- else {
63
- const buf = getBlobPartAsBuffer(blobPart);
64
- text += buf.toString(this.encoding);
68
+ if (isBlob(blobPart)) {
69
+ return blobPart.text();
65
70
  }
71
+ const buf = getBlobPartAsBuffer(blobPart);
72
+ return (0, utils_js_1.fakePromise)(buf.toString(this.encoding));
66
73
  }
67
- return text;
74
+ return this.arrayBuffer().then(buf => buf.toString(this.encoding));
68
75
  }
69
76
  get size() {
70
- let size = 0;
71
- for (const blobPart of this.blobParts) {
72
- if (typeof blobPart === 'string') {
73
- size += Buffer.byteLength(blobPart);
74
- }
75
- else if (isBlob(blobPart)) {
76
- size += blobPart.size;
77
- }
78
- else if ('length' in blobPart) {
79
- size += blobPart.length;
80
- }
81
- else if ('byteLength' in blobPart) {
82
- size += blobPart.byteLength;
77
+ if (this._size == null) {
78
+ this._size = 0;
79
+ for (const blobPart of this.blobParts) {
80
+ if (typeof blobPart === 'string') {
81
+ this._size += Buffer.byteLength(blobPart);
82
+ }
83
+ else if (isBlob(blobPart)) {
84
+ this._size += blobPart.size;
85
+ }
86
+ else if ((0, utils_js_1.isArrayBufferView)(blobPart)) {
87
+ this._size += blobPart.byteLength;
88
+ }
83
89
  }
84
90
  }
85
- return size;
91
+ return this._size;
86
92
  }
87
93
  stream() {
88
- let partQueue = [];
94
+ if (this.blobParts.length === 1) {
95
+ const blobPart = this.blobParts[0];
96
+ if (isBlob(blobPart)) {
97
+ return blobPart.stream();
98
+ }
99
+ const buf = getBlobPartAsBuffer(blobPart);
100
+ return new ReadableStream_js_1.PonyfillReadableStream({
101
+ start: controller => {
102
+ controller.enqueue(buf);
103
+ controller.close();
104
+ },
105
+ });
106
+ }
107
+ let blobPartIterator;
89
108
  return new ReadableStream_js_1.PonyfillReadableStream({
90
109
  start: controller => {
91
- partQueue = [...this.blobParts];
92
- if (partQueue.length === 0) {
110
+ if (this.blobParts.length === 0) {
93
111
  controller.close();
112
+ return;
94
113
  }
114
+ blobPartIterator = this.blobParts[Symbol.iterator]();
95
115
  },
96
- pull: async (controller) => {
97
- const blobPart = partQueue.pop();
116
+ pull: controller => {
117
+ const { value: blobPart, done } = blobPartIterator.next();
118
+ if (done) {
119
+ controller.close();
120
+ return;
121
+ }
98
122
  if (blobPart) {
99
123
  if (isBlob(blobPart)) {
100
- const arrayBuffer = await blobPart.arrayBuffer();
101
- const buf = Buffer.from(arrayBuffer, undefined, blobPart.size);
102
- controller.enqueue(buf);
124
+ return blobPart.arrayBuffer().then(arrayBuffer => {
125
+ const buf = Buffer.from(arrayBuffer, undefined, blobPart.size);
126
+ controller.enqueue(buf);
127
+ });
103
128
  }
104
- else {
105
- const buf = getBlobPartAsBuffer(blobPart);
106
- controller.enqueue(buf);
107
- }
108
- }
109
- else {
110
- controller.close();
129
+ const buf = getBlobPartAsBuffer(blobPart);
130
+ controller.enqueue(buf);
111
131
  }
112
132
  },
113
133
  });
package/cjs/Body.js CHANGED
@@ -14,11 +14,9 @@ var BodyInitType;
14
14
  BodyInitType["ReadableStream"] = "ReadableStream";
15
15
  BodyInitType["Blob"] = "Blob";
16
16
  BodyInitType["FormData"] = "FormData";
17
- BodyInitType["ArrayBuffer"] = "ArrayBuffer";
18
17
  BodyInitType["String"] = "String";
19
18
  BodyInitType["Readable"] = "Readable";
20
19
  BodyInitType["Buffer"] = "Buffer";
21
- BodyInitType["Uint8Array"] = "Uint8Array";
22
20
  })(BodyInitType || (BodyInitType = {}));
23
21
  class PonyfillBody {
24
22
  constructor(bodyInit, options = {}) {
@@ -29,11 +27,12 @@ class PonyfillBody {
29
27
  this.contentLength = null;
30
28
  this._bodyFactory = () => null;
31
29
  this._generatedBody = null;
32
- const { bodyFactory, contentType, contentLength, bodyType } = processBodyInit(bodyInit);
30
+ const { bodyFactory, contentType, contentLength, bodyType, buffer } = processBodyInit(bodyInit);
33
31
  this._bodyFactory = bodyFactory;
34
32
  this.contentType = contentType;
35
33
  this.contentLength = contentLength;
36
34
  this.bodyType = bodyType;
35
+ this._buffer = buffer;
37
36
  }
38
37
  generateBody() {
39
38
  if (this._generatedBody) {
@@ -69,68 +68,50 @@ class PonyfillBody {
69
68
  }
70
69
  return null;
71
70
  }
72
- async arrayBuffer() {
73
- if (this.bodyType === BodyInitType.ArrayBuffer) {
74
- return this.bodyInit;
75
- }
76
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.Buffer) {
77
- const typedBodyInit = this.bodyInit;
78
- return (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.uint8ArrayToArrayBuffer)(buffer);
83
- }
84
- if (this.bodyType === BodyInitType.Blob) {
85
- const blob = this.bodyInit;
86
- const arrayBuffer = await blob.arrayBuffer();
87
- return arrayBuffer;
88
- }
89
- const blob = await this.blob();
90
- return blob.arrayBuffer();
91
- }
92
- async _collectChunksFromReadable() {
93
- const chunks = [];
71
+ _collectChunksFromReadable() {
94
72
  const _body = this.generateBody();
95
- if (_body) {
96
- for await (const chunk of _body.readable) {
97
- chunks.push(chunk);
98
- }
73
+ if (!_body) {
74
+ return (0, utils_js_1.fakePromise)([]);
99
75
  }
100
- return chunks;
76
+ const chunks = [];
77
+ _body.readable.on('data', chunk => {
78
+ chunks.push(chunk);
79
+ });
80
+ return new Promise((resolve, reject) => {
81
+ _body.readable.once('end', () => {
82
+ resolve(chunks);
83
+ });
84
+ _body.readable.once('error', e => {
85
+ reject(e);
86
+ });
87
+ });
101
88
  }
102
- async blob() {
89
+ blob() {
103
90
  if (this.bodyType === BodyInitType.Blob) {
104
- return this.bodyInit;
91
+ return (0, utils_js_1.fakePromise)(this.bodyInit);
105
92
  }
106
- if (this.bodyType === BodyInitType.String ||
107
- this.bodyType === BodyInitType.Buffer ||
108
- this.bodyType === BodyInitType.Uint8Array) {
109
- const bodyInitTyped = this.bodyInit;
110
- return new Blob_js_1.PonyfillBlob([bodyInitTyped], {
93
+ if (this._buffer) {
94
+ const blob = new Blob_js_1.PonyfillBlob([this._buffer], {
111
95
  type: this.contentType || '',
96
+ size: this.contentLength,
112
97
  });
98
+ return (0, utils_js_1.fakePromise)(blob);
113
99
  }
114
- if (this.bodyType === BodyInitType.ArrayBuffer) {
115
- const bodyInitTyped = this.bodyInit;
116
- const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
117
- return new Blob_js_1.PonyfillBlob([buf], {
100
+ return this._collectChunksFromReadable().then(chunks => {
101
+ return new Blob_js_1.PonyfillBlob(chunks, {
118
102
  type: this.contentType || '',
103
+ size: this.contentLength,
119
104
  });
120
- }
121
- const chunks = await this._collectChunksFromReadable();
122
- return new Blob_js_1.PonyfillBlob(chunks, {
123
- type: this.contentType || '',
124
105
  });
125
106
  }
126
107
  formData(opts) {
127
108
  if (this.bodyType === BodyInitType.FormData) {
128
- return Promise.resolve(this.bodyInit);
109
+ return (0, utils_js_1.fakePromise)(this.bodyInit);
129
110
  }
130
111
  const formData = new FormData_js_1.PonyfillFormData();
131
112
  const _body = this.generateBody();
132
113
  if (_body == null) {
133
- return Promise.resolve(formData);
114
+ return (0, utils_js_1.fakePromise)(formData);
134
115
  }
135
116
  const formDataLimits = {
136
117
  ...this.options.formDataLimits,
@@ -162,7 +143,7 @@ class PonyfillBody {
162
143
  reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
163
144
  });
164
145
  fileStream.on('data', chunk => {
165
- chunks.push(Buffer.from(chunk));
146
+ chunks.push(chunk);
166
147
  });
167
148
  fileStream.on('close', () => {
168
149
  if (fileStream.truncated) {
@@ -187,39 +168,36 @@ class PonyfillBody {
187
168
  _body?.readable.pipe(bb);
188
169
  });
189
170
  }
190
- async buffer() {
191
- if (this.bodyType === BodyInitType.Buffer) {
192
- return this.bodyInit;
193
- }
194
- if (this.bodyType === BodyInitType.String) {
195
- return Buffer.from(this.bodyInit);
196
- }
197
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
198
- const bodyInitTyped = this.bodyInit;
199
- const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
200
- return buffer;
171
+ arrayBuffer() {
172
+ if (this._buffer) {
173
+ return (0, utils_js_1.fakePromise)(this._buffer);
201
174
  }
202
175
  if (this.bodyType === BodyInitType.Blob) {
203
176
  if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) {
204
- return this.bodyInit.buffer();
177
+ return this.bodyInit.arrayBuffer();
205
178
  }
206
179
  const bodyInitTyped = this.bodyInit;
207
- const buffer = Buffer.from(await bodyInitTyped.arrayBuffer(), undefined, bodyInitTyped.size);
208
- return buffer;
180
+ return bodyInitTyped
181
+ .arrayBuffer()
182
+ .then(arrayBuffer => Buffer.from(arrayBuffer, undefined, bodyInitTyped.size));
209
183
  }
210
- const chunks = await this._collectChunksFromReadable();
211
- return Buffer.concat(chunks);
184
+ return this._collectChunksFromReadable().then(function concatCollectedChunksFromReadable(chunks) {
185
+ if (chunks.length === 1) {
186
+ return chunks[0];
187
+ }
188
+ return Buffer.concat(chunks);
189
+ });
212
190
  }
213
- async json() {
214
- const text = await this.text();
215
- return JSON.parse(text);
191
+ json() {
192
+ return this.text().then(function parseTextAsJson(text) {
193
+ return JSON.parse(text);
194
+ });
216
195
  }
217
- async text() {
196
+ text() {
218
197
  if (this.bodyType === BodyInitType.String) {
219
- return this.bodyInit;
198
+ return (0, utils_js_1.fakePromise)(this.bodyInit);
220
199
  }
221
- const buffer = await this.buffer();
222
- return buffer.toString('utf-8');
200
+ return this.arrayBuffer().then(buffer => buffer.toString('utf-8'));
223
201
  }
224
202
  }
225
203
  exports.PonyfillBody = PonyfillBody;
@@ -238,18 +216,19 @@ function processBodyInit(bodyInit) {
238
216
  bodyType: BodyInitType.String,
239
217
  contentType: 'text/plain;charset=UTF-8',
240
218
  contentLength,
219
+ buffer,
241
220
  bodyFactory() {
242
221
  const readable = stream_1.Readable.from(buffer);
243
222
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
244
223
  },
245
224
  };
246
225
  }
247
- if (bodyInit instanceof Buffer) {
248
- const contentLength = bodyInit.byteLength;
226
+ if (Buffer.isBuffer(bodyInit)) {
249
227
  return {
250
228
  bodyType: BodyInitType.Buffer,
251
- contentLength,
252
229
  contentType: null,
230
+ contentLength: bodyInit.length,
231
+ buffer: bodyInit,
253
232
  bodyFactory() {
254
233
  const readable = stream_1.Readable.from(bodyInit);
255
234
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -257,6 +236,20 @@ function processBodyInit(bodyInit) {
257
236
  },
258
237
  };
259
238
  }
239
+ if ((0, utils_js_1.isArrayBufferView)(bodyInit)) {
240
+ const buffer = Buffer.from(bodyInit.buffer, bodyInit.byteOffset, bodyInit.byteLength);
241
+ return {
242
+ bodyType: BodyInitType.Buffer,
243
+ contentLength: bodyInit.byteLength,
244
+ contentType: null,
245
+ buffer,
246
+ bodyFactory() {
247
+ const readable = stream_1.Readable.from(buffer);
248
+ const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
249
+ return body;
250
+ },
251
+ };
252
+ }
260
253
  if (bodyInit instanceof ReadableStream_js_1.PonyfillReadableStream) {
261
254
  return {
262
255
  bodyType: BodyInitType.ReadableStream,
@@ -265,7 +258,7 @@ function processBodyInit(bodyInit) {
265
258
  contentLength: null,
266
259
  };
267
260
  }
268
- if (bodyInit instanceof Blob_js_1.PonyfillBlob) {
261
+ if (isBlob(bodyInit)) {
269
262
  return {
270
263
  bodyType: BodyInitType.Blob,
271
264
  contentType: bodyInit.type,
@@ -275,40 +268,15 @@ function processBodyInit(bodyInit) {
275
268
  },
276
269
  };
277
270
  }
278
- if (bodyInit instanceof Uint8Array) {
279
- const contentLength = bodyInit.byteLength;
280
- return {
281
- bodyType: BodyInitType.Uint8Array,
282
- contentLength,
283
- contentType: null,
284
- bodyFactory() {
285
- const readable = stream_1.Readable.from(bodyInit);
286
- const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
287
- return body;
288
- },
289
- };
290
- }
291
- if ('buffer' in bodyInit) {
292
- const contentLength = bodyInit.byteLength;
293
- return {
294
- contentLength,
295
- contentType: null,
296
- bodyFactory() {
297
- const buffer = Buffer.from(bodyInit);
298
- const readable = stream_1.Readable.from(buffer);
299
- const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
300
- return body;
301
- },
302
- };
303
- }
304
271
  if (bodyInit instanceof ArrayBuffer) {
305
272
  const contentLength = bodyInit.byteLength;
273
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
306
274
  return {
307
- bodyType: BodyInitType.ArrayBuffer,
275
+ bodyType: BodyInitType.Buffer,
308
276
  contentType: null,
309
277
  contentLength,
278
+ buffer,
310
279
  bodyFactory() {
311
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
312
280
  const readable = stream_1.Readable.from(buffer);
313
281
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
314
282
  return body;
@@ -326,18 +294,7 @@ function processBodyInit(bodyInit) {
326
294
  },
327
295
  };
328
296
  }
329
- if ('stream' in bodyInit) {
330
- return {
331
- contentType: bodyInit.type,
332
- contentLength: bodyInit.size,
333
- bodyFactory() {
334
- const bodyStream = bodyInit.stream();
335
- const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
336
- return body;
337
- },
338
- };
339
- }
340
- if ('sort' in bodyInit) {
297
+ if (isURLSearchParams(bodyInit)) {
341
298
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
342
299
  return {
343
300
  bodyType: BodyInitType.String,
@@ -349,10 +306,11 @@ function processBodyInit(bodyInit) {
349
306
  },
350
307
  };
351
308
  }
352
- if ('forEach' in bodyInit) {
309
+ if (isFormData(bodyInit)) {
353
310
  const boundary = Math.random().toString(36).substr(2);
354
311
  const contentType = `multipart/form-data; boundary=${boundary}`;
355
312
  return {
313
+ bodyType: BodyInitType.FormData,
356
314
  contentType,
357
315
  contentLength: null,
358
316
  bodyFactory() {
@@ -372,3 +330,12 @@ function processBodyInit(bodyInit) {
372
330
  }
373
331
  throw new Error('Unknown body type');
374
332
  }
333
+ function isFormData(value) {
334
+ return value?.forEach != null;
335
+ }
336
+ function isBlob(value) {
337
+ return value?.stream != null;
338
+ }
339
+ function isURLSearchParams(value) {
340
+ return value?.sort != null;
341
+ }
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
  }
package/cjs/Headers.js CHANGED
@@ -24,7 +24,7 @@ class PonyfillHeaders {
24
24
  }
25
25
  const normalized = key.toLowerCase();
26
26
  if (Array.isArray(this.headersInit)) {
27
- return this.headersInit.find(header => header[0] === normalized)?.[1] || null;
27
+ return this.headersInit.find(header => header[0].toLowerCase() === normalized)?.[1] || null;
28
28
  }
29
29
  else if (isHeadersLike(this.headersInit)) {
30
30
  return this.headersInit.get(normalized);
@@ -36,23 +36,29 @@ function createController(desiredSize, readable) {
36
36
  _flush() {
37
37
  flushed = true;
38
38
  if (chunks.length > 0) {
39
- const concatenated = Buffer.concat(chunks);
39
+ const concatenated = chunks.length > 1 ? Buffer.concat(chunks) : chunks[0];
40
40
  readable.push(concatenated);
41
41
  chunks = [];
42
42
  }
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)) {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PonyfillBtoa = exports.PonyfillTextDecoder = exports.PonyfillTextEncoder = void 0;
4
+ const utils_js_1 = require("./utils.js");
4
5
  class PonyfillTextEncoder {
5
6
  constructor(encoding = 'utf-8') {
6
7
  this.encoding = encoding;
@@ -29,6 +30,12 @@ class PonyfillTextDecoder {
29
30
  }
30
31
  }
31
32
  decode(input) {
33
+ if (Buffer.isBuffer(input)) {
34
+ return input.toString(this.encoding);
35
+ }
36
+ if ((0, utils_js_1.isArrayBufferView)(input)) {
37
+ return Buffer.from(input.buffer, input.byteOffset, input.byteLength).toString(this.encoding);
38
+ }
32
39
  return Buffer.from(input).toString(this.encoding);
33
40
  }
34
41
  }
@@ -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;