@whatwg-node/node-fetch 0.0.1-alpha-20221228082016-9ef2266 → 0.0.1-alpha-20221228083733-4041c71

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/Body.d.ts CHANGED
@@ -7,11 +7,12 @@ export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReada
7
7
  export declare class PonyfillBody implements Body {
8
8
  private bodyInit;
9
9
  bodyUsed: boolean;
10
- readonly body: PonyfillReadableStream<Uint8Array> | null;
10
+ private _body;
11
11
  contentType: string | null;
12
12
  contentLength: number | null;
13
13
  constructor(bodyInit: BodyPonyfillInit | null);
14
14
  private bodyType?;
15
+ get body(): PonyfillReadableStream<Uint8Array> | null;
15
16
  arrayBuffer(): Promise<ArrayBuffer>;
16
17
  blob(): Promise<PonyfillBlob>;
17
18
  formData(): Promise<PonyfillFormData>;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { Readable, Writable } from 'stream';
2
+ import { Readable } from 'stream';
3
3
  export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
4
4
  readable: Readable;
5
5
  constructor(underlyingSource?: UnderlyingSource<T> | Readable | ReadableStream<T> | PonyfillReadableStream<T>);
@@ -16,6 +16,5 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
16
16
  writable: WritableStream<T>;
17
17
  readable: ReadableStream<T2>;
18
18
  }): ReadableStream<T2>;
19
- pipe(writable: Writable): Writable;
20
- on(event: string, listener: (...args: any[]) => void): this;
19
+ static [Symbol.hasInstance](instance: unknown): instance is PonyfillReadableStream<unknown>;
21
20
  }
package/index.js CHANGED
@@ -201,13 +201,8 @@ class PonyfillReadableStream {
201
201
  this.pipeTo(writable);
202
202
  return readable;
203
203
  }
204
- // Trick Fastify
205
- pipe(writable) {
206
- return this.readable.pipe(writable);
207
- }
208
- on(event, listener) {
209
- this.readable.on(event, listener);
210
- return this;
204
+ static [Symbol.hasInstance](instance) {
205
+ return instance != null && typeof instance === 'object' && 'getReader' in instance;
211
206
  }
212
207
  }
213
208
 
@@ -299,36 +294,36 @@ class PonyfillBody {
299
294
  constructor(bodyInit) {
300
295
  this.bodyInit = bodyInit;
301
296
  this.bodyUsed = false;
302
- this.body = null;
297
+ this._body = null;
303
298
  this.contentType = null;
304
299
  this.contentLength = null;
305
300
  if (this.bodyInit == null) {
306
- this.body = null;
301
+ this._body = null;
307
302
  }
308
303
  else if (typeof this.bodyInit === 'string') {
309
304
  this.bodyType = BodyInitType.String;
310
305
  const buffer = Buffer.from(this.bodyInit);
311
306
  this.contentType = 'text/plain;charset=UTF-8';
312
307
  this.contentLength = buffer.length;
313
- this.body = new PonyfillReadableStream(stream.Readable.from(buffer));
308
+ this._body = new PonyfillReadableStream(stream.Readable.from(buffer));
314
309
  }
315
310
  else if (this.bodyInit instanceof PonyfillReadableStream) {
316
311
  this.bodyType = BodyInitType.ReadableStream;
317
- this.body = this.bodyInit;
312
+ this._body = this.bodyInit;
318
313
  }
319
314
  else if (this.bodyInit instanceof PonyfillBlob) {
320
315
  this.bodyType = BodyInitType.Blob;
321
316
  const blobStream = this.bodyInit.stream();
322
317
  this.contentType = this.bodyInit.type;
323
318
  this.contentLength = this.bodyInit.size;
324
- this.body = new PonyfillReadableStream(blobStream);
319
+ this._body = new PonyfillReadableStream(blobStream);
325
320
  }
326
321
  else if (this.bodyInit instanceof PonyfillFormData) {
327
322
  this.bodyType = BodyInitType.FormData;
328
323
  const boundary = Math.random().toString(36).substr(2);
329
324
  const formData = this.bodyInit;
330
325
  this.contentType = `multipart/form-data; boundary=${boundary}`;
331
- this.body = new PonyfillReadableStream({
326
+ this._body = new PonyfillReadableStream({
332
327
  start: async (controller) => {
333
328
  controller.enqueue(Buffer.from(`--${boundary}\r\n`));
334
329
  const entries = [];
@@ -363,21 +358,46 @@ class PonyfillBody {
363
358
  }
364
359
  else if ('buffer' in this.bodyInit) {
365
360
  this.contentLength = this.bodyInit.byteLength;
366
- this.body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit));
361
+ this._body = new PonyfillReadableStream(stream.Readable.from(this.bodyInit));
367
362
  }
368
363
  else if (this.bodyInit instanceof ArrayBuffer) {
369
364
  this.bodyType = BodyInitType.ArrayBuffer;
370
365
  this.contentLength = this.bodyInit.byteLength;
371
- this.body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit)));
366
+ this._body = new PonyfillReadableStream(stream.Readable.from(Buffer.from(this.bodyInit)));
372
367
  }
373
368
  else if (this.bodyInit instanceof stream.Readable) {
374
369
  this.bodyType = BodyInitType.Readable;
375
- this.body = new PonyfillReadableStream(this.bodyInit);
370
+ this._body = new PonyfillReadableStream(this.bodyInit);
376
371
  }
377
372
  else {
378
373
  throw new Error('Unknown body type');
379
374
  }
380
375
  }
376
+ get body() {
377
+ if (this._body != null) {
378
+ const ponyfillReadableStream = this._body;
379
+ const readable = this._body.readable;
380
+ return new Proxy(this._body.readable, {
381
+ get(_, prop) {
382
+ if (prop in ponyfillReadableStream) {
383
+ const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
384
+ if (typeof ponyfillReadableStreamProp === 'function') {
385
+ return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
386
+ }
387
+ return ponyfillReadableStreamProp;
388
+ }
389
+ if (prop in readable) {
390
+ const readableProp = readable[prop];
391
+ if (typeof readableProp === 'function') {
392
+ return readableProp.bind(readable);
393
+ }
394
+ return readableProp;
395
+ }
396
+ }
397
+ });
398
+ }
399
+ return null;
400
+ }
381
401
  async arrayBuffer() {
382
402
  if (this.bodyType === BodyInitType.ArrayBuffer) {
383
403
  return this.bodyInit;
@@ -390,8 +410,8 @@ class PonyfillBody {
390
410
  return this.bodyInit;
391
411
  }
392
412
  const chunks = [];
393
- if (this.body) {
394
- for await (const chunk of this.body.readable) {
413
+ if (this._body) {
414
+ for await (const chunk of this._body.readable) {
395
415
  chunks.push(chunk);
396
416
  }
397
417
  }
@@ -418,8 +438,8 @@ class PonyfillBody {
418
438
  if (this.bodyType === BodyInitType.Readable) {
419
439
  return this.bodyInit;
420
440
  }
421
- if (this.body != null) {
422
- return this.body.readable;
441
+ if (this._body != null) {
442
+ return this._body.readable;
423
443
  }
424
444
  return null;
425
445
  }
@@ -648,7 +668,7 @@ const fetchPonyfill = (info, init) => {
648
668
  return;
649
669
  }
650
670
  const requestFn = getRequestFnForProtocol(protocol);
651
- const nodeReadable = fetchRequest.readable();
671
+ const nodeReadable = fetchRequest.body;
652
672
  const nodeHeaders = getHeadersObj(fetchRequest.headers);
653
673
  const abortListener = function abortListener(event) {
654
674
  nodeRequest.destroy();
package/index.mjs CHANGED
@@ -197,13 +197,8 @@ class PonyfillReadableStream {
197
197
  this.pipeTo(writable);
198
198
  return readable;
199
199
  }
200
- // Trick Fastify
201
- pipe(writable) {
202
- return this.readable.pipe(writable);
203
- }
204
- on(event, listener) {
205
- this.readable.on(event, listener);
206
- return this;
200
+ static [Symbol.hasInstance](instance) {
201
+ return instance != null && typeof instance === 'object' && 'getReader' in instance;
207
202
  }
208
203
  }
209
204
 
@@ -295,36 +290,36 @@ class PonyfillBody {
295
290
  constructor(bodyInit) {
296
291
  this.bodyInit = bodyInit;
297
292
  this.bodyUsed = false;
298
- this.body = null;
293
+ this._body = null;
299
294
  this.contentType = null;
300
295
  this.contentLength = null;
301
296
  if (this.bodyInit == null) {
302
- this.body = null;
297
+ this._body = null;
303
298
  }
304
299
  else if (typeof this.bodyInit === 'string') {
305
300
  this.bodyType = BodyInitType.String;
306
301
  const buffer = Buffer.from(this.bodyInit);
307
302
  this.contentType = 'text/plain;charset=UTF-8';
308
303
  this.contentLength = buffer.length;
309
- this.body = new PonyfillReadableStream(Readable.from(buffer));
304
+ this._body = new PonyfillReadableStream(Readable.from(buffer));
310
305
  }
311
306
  else if (this.bodyInit instanceof PonyfillReadableStream) {
312
307
  this.bodyType = BodyInitType.ReadableStream;
313
- this.body = this.bodyInit;
308
+ this._body = this.bodyInit;
314
309
  }
315
310
  else if (this.bodyInit instanceof PonyfillBlob) {
316
311
  this.bodyType = BodyInitType.Blob;
317
312
  const blobStream = this.bodyInit.stream();
318
313
  this.contentType = this.bodyInit.type;
319
314
  this.contentLength = this.bodyInit.size;
320
- this.body = new PonyfillReadableStream(blobStream);
315
+ this._body = new PonyfillReadableStream(blobStream);
321
316
  }
322
317
  else if (this.bodyInit instanceof PonyfillFormData) {
323
318
  this.bodyType = BodyInitType.FormData;
324
319
  const boundary = Math.random().toString(36).substr(2);
325
320
  const formData = this.bodyInit;
326
321
  this.contentType = `multipart/form-data; boundary=${boundary}`;
327
- this.body = new PonyfillReadableStream({
322
+ this._body = new PonyfillReadableStream({
328
323
  start: async (controller) => {
329
324
  controller.enqueue(Buffer.from(`--${boundary}\r\n`));
330
325
  const entries = [];
@@ -359,21 +354,46 @@ class PonyfillBody {
359
354
  }
360
355
  else if ('buffer' in this.bodyInit) {
361
356
  this.contentLength = this.bodyInit.byteLength;
362
- this.body = new PonyfillReadableStream(Readable.from(this.bodyInit));
357
+ this._body = new PonyfillReadableStream(Readable.from(this.bodyInit));
363
358
  }
364
359
  else if (this.bodyInit instanceof ArrayBuffer) {
365
360
  this.bodyType = BodyInitType.ArrayBuffer;
366
361
  this.contentLength = this.bodyInit.byteLength;
367
- this.body = new PonyfillReadableStream(Readable.from(Buffer.from(this.bodyInit)));
362
+ this._body = new PonyfillReadableStream(Readable.from(Buffer.from(this.bodyInit)));
368
363
  }
369
364
  else if (this.bodyInit instanceof Readable) {
370
365
  this.bodyType = BodyInitType.Readable;
371
- this.body = new PonyfillReadableStream(this.bodyInit);
366
+ this._body = new PonyfillReadableStream(this.bodyInit);
372
367
  }
373
368
  else {
374
369
  throw new Error('Unknown body type');
375
370
  }
376
371
  }
372
+ get body() {
373
+ if (this._body != null) {
374
+ const ponyfillReadableStream = this._body;
375
+ const readable = this._body.readable;
376
+ return new Proxy(this._body.readable, {
377
+ get(_, prop) {
378
+ if (prop in ponyfillReadableStream) {
379
+ const ponyfillReadableStreamProp = ponyfillReadableStream[prop];
380
+ if (typeof ponyfillReadableStreamProp === 'function') {
381
+ return ponyfillReadableStreamProp.bind(ponyfillReadableStream);
382
+ }
383
+ return ponyfillReadableStreamProp;
384
+ }
385
+ if (prop in readable) {
386
+ const readableProp = readable[prop];
387
+ if (typeof readableProp === 'function') {
388
+ return readableProp.bind(readable);
389
+ }
390
+ return readableProp;
391
+ }
392
+ }
393
+ });
394
+ }
395
+ return null;
396
+ }
377
397
  async arrayBuffer() {
378
398
  if (this.bodyType === BodyInitType.ArrayBuffer) {
379
399
  return this.bodyInit;
@@ -386,8 +406,8 @@ class PonyfillBody {
386
406
  return this.bodyInit;
387
407
  }
388
408
  const chunks = [];
389
- if (this.body) {
390
- for await (const chunk of this.body.readable) {
409
+ if (this._body) {
410
+ for await (const chunk of this._body.readable) {
391
411
  chunks.push(chunk);
392
412
  }
393
413
  }
@@ -414,8 +434,8 @@ class PonyfillBody {
414
434
  if (this.bodyType === BodyInitType.Readable) {
415
435
  return this.bodyInit;
416
436
  }
417
- if (this.body != null) {
418
- return this.body.readable;
437
+ if (this._body != null) {
438
+ return this._body.readable;
419
439
  }
420
440
  return null;
421
441
  }
@@ -644,7 +664,7 @@ const fetchPonyfill = (info, init) => {
644
664
  return;
645
665
  }
646
666
  const requestFn = getRequestFnForProtocol(protocol);
647
- const nodeReadable = fetchRequest.readable();
667
+ const nodeReadable = fetchRequest.body;
648
668
  const nodeHeaders = getHeadersObj(fetchRequest.headers);
649
669
  const abortListener = function abortListener(event) {
650
670
  nodeRequest.destroy();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.1-alpha-20221228082016-9ef2266",
3
+ "version": "0.0.1-alpha-20221228083733-4041c71",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {