@whatwg-node/node-fetch 0.5.0-alpha-20230714132731-d012863 → 0.5.0-alpha-20231025081004-47fb219

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,76 +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
71
  _collectChunksFromReadable() {
72
+ const _body = this.generateBody();
73
+ if (!_body) {
74
+ return (0, utils_js_1.fakePromise)([]);
75
+ }
76
+ const chunks = [];
77
+ _body.readable.on('data', chunk => {
78
+ chunks.push(chunk);
79
+ });
93
80
  return new Promise((resolve, reject) => {
94
- const chunks = [];
95
- const _body = this.generateBody();
96
- if (_body) {
97
- _body.readable.on('data', chunk => {
98
- chunks.push(chunk);
99
- });
100
- _body.readable.on('end', () => {
101
- resolve(chunks);
102
- });
103
- _body.readable.on('error', e => {
104
- reject(e);
105
- });
106
- }
107
- return chunks;
81
+ _body.readable.once('end', () => {
82
+ resolve(chunks);
83
+ });
84
+ _body.readable.once('error', e => {
85
+ reject(e);
86
+ });
108
87
  });
109
88
  }
110
- async blob() {
89
+ blob() {
111
90
  if (this.bodyType === BodyInitType.Blob) {
112
- return this.bodyInit;
91
+ return (0, utils_js_1.fakePromise)(this.bodyInit);
113
92
  }
114
- if (this.bodyType === BodyInitType.String ||
115
- this.bodyType === BodyInitType.Buffer ||
116
- this.bodyType === BodyInitType.Uint8Array) {
117
- const bodyInitTyped = this.bodyInit;
118
- return new Blob_js_1.PonyfillBlob([bodyInitTyped], {
93
+ if (this._buffer) {
94
+ const blob = new Blob_js_1.PonyfillBlob([this._buffer], {
119
95
  type: this.contentType || '',
96
+ size: this.contentLength,
120
97
  });
98
+ return (0, utils_js_1.fakePromise)(blob);
121
99
  }
122
- if (this.bodyType === BodyInitType.ArrayBuffer) {
123
- const bodyInitTyped = this.bodyInit;
124
- const buf = Buffer.from(bodyInitTyped, undefined, bodyInitTyped.byteLength);
125
- return new Blob_js_1.PonyfillBlob([buf], {
100
+ return this._collectChunksFromReadable().then(chunks => {
101
+ return new Blob_js_1.PonyfillBlob(chunks, {
126
102
  type: this.contentType || '',
103
+ size: this.contentLength,
127
104
  });
128
- }
129
- const chunks = await this._collectChunksFromReadable();
130
- return new Blob_js_1.PonyfillBlob(chunks, {
131
- type: this.contentType || '',
132
105
  });
133
106
  }
134
107
  formData(opts) {
135
108
  if (this.bodyType === BodyInitType.FormData) {
136
- return Promise.resolve(this.bodyInit);
109
+ return (0, utils_js_1.fakePromise)(this.bodyInit);
137
110
  }
138
111
  const formData = new FormData_js_1.PonyfillFormData();
139
112
  const _body = this.generateBody();
140
113
  if (_body == null) {
141
- return Promise.resolve(formData);
114
+ return (0, utils_js_1.fakePromise)(formData);
142
115
  }
143
116
  const formDataLimits = {
144
117
  ...this.options.formDataLimits,
@@ -170,7 +143,7 @@ class PonyfillBody {
170
143
  reject(new Error(`File size limit exceeded: ${formDataLimits?.fileSize} bytes`));
171
144
  });
172
145
  fileStream.on('data', chunk => {
173
- chunks.push(Buffer.from(chunk));
146
+ chunks.push(chunk);
174
147
  });
175
148
  fileStream.on('close', () => {
176
149
  if (fileStream.truncated) {
@@ -195,39 +168,36 @@ class PonyfillBody {
195
168
  _body?.readable.pipe(bb);
196
169
  });
197
170
  }
198
- async buffer() {
199
- if (this.bodyType === BodyInitType.Buffer) {
200
- return this.bodyInit;
201
- }
202
- if (this.bodyType === BodyInitType.String) {
203
- return Buffer.from(this.bodyInit);
204
- }
205
- if (this.bodyType === BodyInitType.Uint8Array || this.bodyType === BodyInitType.ArrayBuffer) {
206
- const bodyInitTyped = this.bodyInit;
207
- const buffer = Buffer.from(bodyInitTyped, 'byteOffset' in bodyInitTyped ? bodyInitTyped.byteOffset : undefined, bodyInitTyped.byteLength);
208
- return buffer;
171
+ arrayBuffer() {
172
+ if (this._buffer) {
173
+ return (0, utils_js_1.fakePromise)(this._buffer);
209
174
  }
210
175
  if (this.bodyType === BodyInitType.Blob) {
211
176
  if (this.bodyInit instanceof Blob_js_1.PonyfillBlob) {
212
- return this.bodyInit.buffer();
177
+ return this.bodyInit.arrayBuffer();
213
178
  }
214
179
  const bodyInitTyped = this.bodyInit;
215
- const buffer = Buffer.from(await bodyInitTyped.arrayBuffer(), undefined, bodyInitTyped.size);
216
- return buffer;
180
+ return bodyInitTyped
181
+ .arrayBuffer()
182
+ .then(arrayBuffer => Buffer.from(arrayBuffer, undefined, bodyInitTyped.size));
217
183
  }
218
- const chunks = await this._collectChunksFromReadable();
219
- 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
+ });
220
190
  }
221
- async json() {
222
- const text = await this.text();
223
- return JSON.parse(text);
191
+ json() {
192
+ return this.text().then(function parseTextAsJson(text) {
193
+ return JSON.parse(text);
194
+ });
224
195
  }
225
- async text() {
196
+ text() {
226
197
  if (this.bodyType === BodyInitType.String) {
227
- return this.bodyInit;
198
+ return (0, utils_js_1.fakePromise)(this.bodyInit);
228
199
  }
229
- const buffer = await this.buffer();
230
- return buffer.toString('utf-8');
200
+ return this.arrayBuffer().then(buffer => buffer.toString('utf-8'));
231
201
  }
232
202
  }
233
203
  exports.PonyfillBody = PonyfillBody;
@@ -246,18 +216,19 @@ function processBodyInit(bodyInit) {
246
216
  bodyType: BodyInitType.String,
247
217
  contentType: 'text/plain;charset=UTF-8',
248
218
  contentLength,
219
+ buffer,
249
220
  bodyFactory() {
250
221
  const readable = stream_1.Readable.from(buffer);
251
222
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
252
223
  },
253
224
  };
254
225
  }
255
- if (bodyInit instanceof Buffer) {
256
- const contentLength = bodyInit.byteLength;
226
+ if (Buffer.isBuffer(bodyInit)) {
257
227
  return {
258
228
  bodyType: BodyInitType.Buffer,
259
- contentLength,
260
229
  contentType: null,
230
+ contentLength: bodyInit.length,
231
+ buffer: bodyInit,
261
232
  bodyFactory() {
262
233
  const readable = stream_1.Readable.from(bodyInit);
263
234
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
@@ -265,6 +236,20 @@ function processBodyInit(bodyInit) {
265
236
  },
266
237
  };
267
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
+ }
268
253
  if (bodyInit instanceof ReadableStream_js_1.PonyfillReadableStream) {
269
254
  return {
270
255
  bodyType: BodyInitType.ReadableStream,
@@ -273,7 +258,7 @@ function processBodyInit(bodyInit) {
273
258
  contentLength: null,
274
259
  };
275
260
  }
276
- if (bodyInit instanceof Blob_js_1.PonyfillBlob) {
261
+ if (isBlob(bodyInit)) {
277
262
  return {
278
263
  bodyType: BodyInitType.Blob,
279
264
  contentType: bodyInit.type,
@@ -283,40 +268,15 @@ function processBodyInit(bodyInit) {
283
268
  },
284
269
  };
285
270
  }
286
- if (bodyInit instanceof Uint8Array) {
287
- const contentLength = bodyInit.byteLength;
288
- return {
289
- bodyType: BodyInitType.Uint8Array,
290
- contentLength,
291
- contentType: null,
292
- bodyFactory() {
293
- const readable = stream_1.Readable.from(bodyInit);
294
- const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
295
- return body;
296
- },
297
- };
298
- }
299
- if ('buffer' in bodyInit) {
300
- const contentLength = bodyInit.byteLength;
301
- return {
302
- contentLength,
303
- contentType: null,
304
- bodyFactory() {
305
- const buffer = Buffer.from(bodyInit);
306
- const readable = stream_1.Readable.from(buffer);
307
- const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
308
- return body;
309
- },
310
- };
311
- }
312
271
  if (bodyInit instanceof ArrayBuffer) {
313
272
  const contentLength = bodyInit.byteLength;
273
+ const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
314
274
  return {
315
- bodyType: BodyInitType.ArrayBuffer,
275
+ bodyType: BodyInitType.Buffer,
316
276
  contentType: null,
317
277
  contentLength,
278
+ buffer,
318
279
  bodyFactory() {
319
- const buffer = Buffer.from(bodyInit, undefined, bodyInit.byteLength);
320
280
  const readable = stream_1.Readable.from(buffer);
321
281
  const body = new ReadableStream_js_1.PonyfillReadableStream(readable);
322
282
  return body;
@@ -334,18 +294,7 @@ function processBodyInit(bodyInit) {
334
294
  },
335
295
  };
336
296
  }
337
- if ('stream' in bodyInit) {
338
- return {
339
- contentType: bodyInit.type,
340
- contentLength: bodyInit.size,
341
- bodyFactory() {
342
- const bodyStream = bodyInit.stream();
343
- const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
344
- return body;
345
- },
346
- };
347
- }
348
- if ('sort' in bodyInit) {
297
+ if (isURLSearchParams(bodyInit)) {
349
298
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
350
299
  return {
351
300
  bodyType: BodyInitType.String,
@@ -357,10 +306,11 @@ function processBodyInit(bodyInit) {
357
306
  },
358
307
  };
359
308
  }
360
- if ('forEach' in bodyInit) {
309
+ if (isFormData(bodyInit)) {
361
310
  const boundary = Math.random().toString(36).substr(2);
362
311
  const contentType = `multipart/form-data; boundary=${boundary}`;
363
312
  return {
313
+ bodyType: BodyInitType.FormData,
364
314
  contentType,
365
315
  contentLength: null,
366
316
  bodyFactory() {
@@ -380,3 +330,12 @@ function processBodyInit(bodyInit) {
380
330
  }
381
331
  throw new Error('Unknown body type');
382
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;