@whatwg-node/node-fetch 0.0.1-alpha-20221005131815-b0ce77a → 0.0.1-alpha-20221225155816-6b54cb4

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/Blob.d.ts CHANGED
@@ -4,3 +4,7 @@ export declare class PonyfillBlob extends NodeBlob implements Blob {
4
4
  stream(): any;
5
5
  slice(...args: any[]): any;
6
6
  }
7
+ export interface PonyfillBlob {
8
+ prototype: Blob;
9
+ new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
10
+ }
package/Body.d.ts CHANGED
@@ -3,8 +3,8 @@ import { PonyfillBlob } from './Blob';
3
3
  import { Readable } from 'stream';
4
4
  import { PonyfillFormData } from './FormData';
5
5
  import { PonyfillReadableStream } from './ReadableStream';
6
- export declare type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
7
- export declare class BodyPonyfill implements Body {
6
+ export type BodyPonyfillInit = XMLHttpRequestBodyInit | Readable | PonyfillReadableStream<Uint8Array>;
7
+ export declare class PonyfillBody implements Body {
8
8
  private bodyInit;
9
9
  bodyUsed: boolean;
10
10
  contentType: string | null;
package/Headers.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare type PonyfillHeadersInit = [string, string][] | Record<string, string | string[] | undefined> | Headers;
1
+ export type PonyfillHeadersInit = [string, string][] | Record<string, string | string[] | undefined> | Headers;
2
2
  export declare class PonyfillHeaders implements Headers {
3
3
  private map;
4
4
  constructor(headersInit?: PonyfillHeadersInit);
@@ -5,6 +5,9 @@ export declare class PonyfillReadableStream<T> implements ReadableStream<T> {
5
5
  constructor(underlyingSource?: UnderlyingSource<T> | Readable | ReadableStream<T> | PonyfillReadableStream<T>);
6
6
  cancel(reason?: any): Promise<void>;
7
7
  locked: boolean;
8
+ getReader(options: {
9
+ mode: 'byob';
10
+ }): ReadableStreamBYOBReader;
8
11
  getReader(): ReadableStreamDefaultReader<T>;
9
12
  [Symbol.asyncIterator](): AsyncIterableIterator<any>;
10
13
  tee(): [ReadableStream<T>, ReadableStream<T>];
package/Request.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { BodyPonyfill, BodyPonyfillInit } from './Body';
1
+ import { PonyfillBody, BodyPonyfillInit } from './Body';
2
2
  import { PonyfillHeadersInit } from './Headers';
3
- export declare type RequestPonyfillInit = Omit<RequestInit, 'body' | 'headers'> & {
3
+ export type RequestPonyfillInit = Omit<RequestInit, 'body' | 'headers'> & {
4
4
  body?: BodyPonyfillInit | null;
5
5
  headers?: PonyfillHeadersInit;
6
6
  };
7
- export declare class PonyfillRequest extends BodyPonyfill implements Request {
7
+ export declare class PonyfillRequest extends PonyfillBody implements Request {
8
8
  constructor(input: RequestInfo | URL, options?: RequestPonyfillInit);
9
9
  get body(): import("./ReadableStream").PonyfillReadableStream<Uint8Array> | null;
10
10
  private postProcessedBody;
package/Response.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { BodyPonyfill, BodyPonyfillInit } from './Body';
1
+ import { PonyfillBody, BodyPonyfillInit } from './Body';
2
2
  import { PonyfillHeadersInit } from './Headers';
3
- export declare type ResponsePonyfilInit = Omit<ResponseInit, 'headers'> & {
3
+ export type ResponsePonyfilInit = Omit<ResponseInit, 'headers'> & {
4
4
  url?: string;
5
5
  redirected?: boolean;
6
6
  headers?: PonyfillHeadersInit;
7
7
  };
8
- export declare class PonyfillResponse extends BodyPonyfill implements Response {
8
+ export declare class PonyfillResponse extends PonyfillBody implements Response {
9
9
  constructor(body?: BodyPonyfillInit | null, init?: ResponsePonyfilInit);
10
10
  headers: Headers;
11
11
  get ok(): boolean;
@@ -17,4 +17,5 @@ export declare class PonyfillResponse extends BodyPonyfill implements Response {
17
17
  clone(): PonyfillResponse;
18
18
  static error(): PonyfillResponse;
19
19
  static redirect(url: string, status?: number): PonyfillResponse;
20
+ static json(data: any, init?: RequestInit): PonyfillResponse;
20
21
  }
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ export declare class PonyfillTextEncoder implements TextEncoder {
3
+ encoding: BufferEncoding;
4
+ constructor(encoding?: BufferEncoding);
5
+ encode(input: string): Buffer;
6
+ encodeInto(source: string, destination: Uint8Array): TextEncoderEncodeIntoResult;
7
+ }
8
+ export declare class PonyfillTextDecoder implements TextDecoder {
9
+ encoding: BufferEncoding;
10
+ fatal: boolean;
11
+ ignoreBOM: boolean;
12
+ constructor(encoding: BufferEncoding, options: TextDecoderOptions);
13
+ decode(input: Uint8Array): string;
14
+ }
15
+ export declare function PonyfillBtoa(input: string): string;
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { fetchPonyfill as fetch } from './fetch';
2
2
  export { PonyfillHeaders as Headers } from './Headers';
3
+ export { PonyfillBody as Body } from './Body';
3
4
  export { PonyfillRequest as Request, RequestPonyfillInit as RequestInit } from './Request';
4
5
  export { PonyfillResponse as Response, ResponsePonyfilInit as ResponseInit } from './Response';
5
6
  export { PonyfillReadableStream as ReadableStream } from './ReadableStream';
@@ -9,3 +10,4 @@ export { PonyfillAbortController as AbortController } from './AbortController';
9
10
  export { PonyfillAbortSignal as AbortSignal } from './AbortSignal';
10
11
  export { PonyfillAbortError as AbortError } from './AbortError';
11
12
  export { PonyfillBlob as Blob } from './Blob';
13
+ export { PonyfillTextEncoder as TextEncoder, PonyfillTextDecoder as TextDecoder, PonyfillBtoa as btoa, } from './TextEncoderDecoder';
package/index.js CHANGED
@@ -7,6 +7,8 @@ const https = require('https');
7
7
  const events = require('@whatwg-node/events');
8
8
  const buffer = require('buffer');
9
9
  const stream = require('stream');
10
+ const url = require('url');
11
+ const fs = require('fs');
10
12
 
11
13
  // Will be removed after v14 reaches EOL
12
14
  class PonyfillAbortError extends Error {
@@ -125,7 +127,7 @@ class PonyfillReadableStream {
125
127
  this.readable.destroy(reason);
126
128
  return Promise.resolve();
127
129
  }
128
- getReader() {
130
+ getReader(_options) {
129
131
  const iterator = this.readable[Symbol.asyncIterator]();
130
132
  return {
131
133
  read() {
@@ -227,8 +229,8 @@ class PonyfillFormData {
227
229
  }
228
230
  getNormalizedFile(name, blob, fileName) {
229
231
  if (blob instanceof PonyfillFile) {
230
- if (fileName == null) {
231
- return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified });
232
+ if (fileName != null) {
233
+ return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
232
234
  }
233
235
  return blob;
234
236
  }
@@ -252,7 +254,7 @@ var BodyInitType;
252
254
  BodyInitType["String"] = "String";
253
255
  BodyInitType["Readable"] = "Readable";
254
256
  })(BodyInitType || (BodyInitType = {}));
255
- class BodyPonyfill {
257
+ class PonyfillBody {
256
258
  constructor(bodyInit) {
257
259
  this.bodyInit = bodyInit;
258
260
  this.bodyUsed = false;
@@ -463,7 +465,7 @@ class PonyfillHeaders {
463
465
  function isRequest(input) {
464
466
  return input[Symbol.toStringTag] === 'Request';
465
467
  }
466
- class PonyfillRequest extends BodyPonyfill {
468
+ class PonyfillRequest extends PonyfillBody {
467
469
  constructor(input, options) {
468
470
  let url;
469
471
  let bodyInit = null;
@@ -529,7 +531,7 @@ class PonyfillRequest extends BodyPonyfill {
529
531
  }
530
532
  }
531
533
 
532
- class PonyfillResponse extends BodyPonyfill {
534
+ class PonyfillResponse extends PonyfillBody {
533
535
  constructor(body, init) {
534
536
  super(body || null);
535
537
  this.headers = new PonyfillHeaders();
@@ -570,6 +572,15 @@ class PonyfillResponse extends BodyPonyfill {
570
572
  status,
571
573
  });
572
574
  }
575
+ static json(data, init = {}) {
576
+ return new PonyfillResponse(JSON.stringify(data), {
577
+ ...init,
578
+ headers: {
579
+ 'Content-Type': 'application/json',
580
+ ...init === null || init === void 0 ? void 0 : init.headers,
581
+ },
582
+ });
583
+ }
573
584
  }
574
585
 
575
586
  function getHeadersObj(headers) {
@@ -590,6 +601,10 @@ const fetchPonyfill = (info, init) => {
590
601
  const fetchRequest = info;
591
602
  return new Promise((resolve, reject) => {
592
603
  try {
604
+ if (fetchRequest.url.startsWith('file://')) {
605
+ resolve(new PonyfillResponse(fs.createReadStream(url.fileURLToPath(fetchRequest.url))));
606
+ return;
607
+ }
593
608
  const requestFn = fetchRequest.url.startsWith('https') ? https.request : http.request;
594
609
  const nodeReadable = fetchRequest.readable();
595
610
  const nodeHeaders = getHeadersObj(fetchRequest.headers);
@@ -640,14 +655,52 @@ const fetchPonyfill = (info, init) => {
640
655
  });
641
656
  };
642
657
 
658
+ class PonyfillTextEncoder {
659
+ constructor(encoding = 'utf-8') {
660
+ this.encoding = encoding;
661
+ }
662
+ encode(input) {
663
+ return Buffer.from(input, this.encoding);
664
+ }
665
+ encodeInto(source, destination) {
666
+ const buffer = this.encode(source);
667
+ const copied = buffer.copy(destination);
668
+ return {
669
+ read: copied,
670
+ written: copied,
671
+ };
672
+ }
673
+ }
674
+ class PonyfillTextDecoder {
675
+ constructor(encoding = 'utf-8', options) {
676
+ this.encoding = encoding;
677
+ this.fatal = false;
678
+ this.ignoreBOM = false;
679
+ if (options) {
680
+ this.fatal = options.fatal || false;
681
+ this.ignoreBOM = options.ignoreBOM || false;
682
+ }
683
+ }
684
+ decode(input) {
685
+ return Buffer.from(input).toString(this.encoding);
686
+ }
687
+ }
688
+ function PonyfillBtoa(input) {
689
+ return Buffer.from(input, 'binary').toString('base64');
690
+ }
691
+
643
692
  exports.AbortController = PonyfillAbortController;
644
693
  exports.AbortError = PonyfillAbortError;
645
694
  exports.AbortSignal = PonyfillAbortSignal;
646
695
  exports.Blob = PonyfillBlob;
696
+ exports.Body = PonyfillBody;
647
697
  exports.File = PonyfillFile;
648
698
  exports.FormData = PonyfillFormData;
649
699
  exports.Headers = PonyfillHeaders;
650
700
  exports.ReadableStream = PonyfillReadableStream;
651
701
  exports.Request = PonyfillRequest;
652
702
  exports.Response = PonyfillResponse;
703
+ exports.TextDecoder = PonyfillTextDecoder;
704
+ exports.TextEncoder = PonyfillTextEncoder;
705
+ exports.btoa = PonyfillBtoa;
653
706
  exports.fetch = fetchPonyfill;
package/index.mjs CHANGED
@@ -3,6 +3,8 @@ import { request } from 'https';
3
3
  import { EventTarget, CustomEvent } from '@whatwg-node/events';
4
4
  import { Blob } from 'buffer';
5
5
  import { Readable } from 'stream';
6
+ import { fileURLToPath } from 'url';
7
+ import { createReadStream } from 'fs';
6
8
 
7
9
  // Will be removed after v14 reaches EOL
8
10
  class PonyfillAbortError extends Error {
@@ -121,7 +123,7 @@ class PonyfillReadableStream {
121
123
  this.readable.destroy(reason);
122
124
  return Promise.resolve();
123
125
  }
124
- getReader() {
126
+ getReader(_options) {
125
127
  const iterator = this.readable[Symbol.asyncIterator]();
126
128
  return {
127
129
  read() {
@@ -223,8 +225,8 @@ class PonyfillFormData {
223
225
  }
224
226
  getNormalizedFile(name, blob, fileName) {
225
227
  if (blob instanceof PonyfillFile) {
226
- if (fileName == null) {
227
- return new PonyfillFile([blob], name, { type: blob.type, lastModified: blob.lastModified });
228
+ if (fileName != null) {
229
+ return new PonyfillFile([blob], fileName, { type: blob.type, lastModified: blob.lastModified });
228
230
  }
229
231
  return blob;
230
232
  }
@@ -248,7 +250,7 @@ var BodyInitType;
248
250
  BodyInitType["String"] = "String";
249
251
  BodyInitType["Readable"] = "Readable";
250
252
  })(BodyInitType || (BodyInitType = {}));
251
- class BodyPonyfill {
253
+ class PonyfillBody {
252
254
  constructor(bodyInit) {
253
255
  this.bodyInit = bodyInit;
254
256
  this.bodyUsed = false;
@@ -459,7 +461,7 @@ class PonyfillHeaders {
459
461
  function isRequest(input) {
460
462
  return input[Symbol.toStringTag] === 'Request';
461
463
  }
462
- class PonyfillRequest extends BodyPonyfill {
464
+ class PonyfillRequest extends PonyfillBody {
463
465
  constructor(input, options) {
464
466
  let url;
465
467
  let bodyInit = null;
@@ -525,7 +527,7 @@ class PonyfillRequest extends BodyPonyfill {
525
527
  }
526
528
  }
527
529
 
528
- class PonyfillResponse extends BodyPonyfill {
530
+ class PonyfillResponse extends PonyfillBody {
529
531
  constructor(body, init) {
530
532
  super(body || null);
531
533
  this.headers = new PonyfillHeaders();
@@ -566,6 +568,15 @@ class PonyfillResponse extends BodyPonyfill {
566
568
  status,
567
569
  });
568
570
  }
571
+ static json(data, init = {}) {
572
+ return new PonyfillResponse(JSON.stringify(data), {
573
+ ...init,
574
+ headers: {
575
+ 'Content-Type': 'application/json',
576
+ ...init === null || init === void 0 ? void 0 : init.headers,
577
+ },
578
+ });
579
+ }
569
580
  }
570
581
 
571
582
  function getHeadersObj(headers) {
@@ -586,6 +597,10 @@ const fetchPonyfill = (info, init) => {
586
597
  const fetchRequest = info;
587
598
  return new Promise((resolve, reject) => {
588
599
  try {
600
+ if (fetchRequest.url.startsWith('file://')) {
601
+ resolve(new PonyfillResponse(createReadStream(fileURLToPath(fetchRequest.url))));
602
+ return;
603
+ }
589
604
  const requestFn = fetchRequest.url.startsWith('https') ? request : request$1;
590
605
  const nodeReadable = fetchRequest.readable();
591
606
  const nodeHeaders = getHeadersObj(fetchRequest.headers);
@@ -636,4 +651,38 @@ const fetchPonyfill = (info, init) => {
636
651
  });
637
652
  };
638
653
 
639
- export { PonyfillAbortController as AbortController, PonyfillAbortError as AbortError, PonyfillAbortSignal as AbortSignal, PonyfillBlob as Blob, PonyfillFile as File, PonyfillFormData as FormData, PonyfillHeaders as Headers, PonyfillReadableStream as ReadableStream, PonyfillRequest as Request, PonyfillResponse as Response, fetchPonyfill as fetch };
654
+ class PonyfillTextEncoder {
655
+ constructor(encoding = 'utf-8') {
656
+ this.encoding = encoding;
657
+ }
658
+ encode(input) {
659
+ return Buffer.from(input, this.encoding);
660
+ }
661
+ encodeInto(source, destination) {
662
+ const buffer = this.encode(source);
663
+ const copied = buffer.copy(destination);
664
+ return {
665
+ read: copied,
666
+ written: copied,
667
+ };
668
+ }
669
+ }
670
+ class PonyfillTextDecoder {
671
+ constructor(encoding = 'utf-8', options) {
672
+ this.encoding = encoding;
673
+ this.fatal = false;
674
+ this.ignoreBOM = false;
675
+ if (options) {
676
+ this.fatal = options.fatal || false;
677
+ this.ignoreBOM = options.ignoreBOM || false;
678
+ }
679
+ }
680
+ decode(input) {
681
+ return Buffer.from(input).toString(this.encoding);
682
+ }
683
+ }
684
+ function PonyfillBtoa(input) {
685
+ return Buffer.from(input, 'binary').toString('base64');
686
+ }
687
+
688
+ export { PonyfillAbortController as AbortController, PonyfillAbortError as AbortError, PonyfillAbortSignal as AbortSignal, PonyfillBlob as Blob, PonyfillBody as Body, PonyfillFile as File, PonyfillFormData as FormData, PonyfillHeaders as Headers, PonyfillReadableStream as ReadableStream, PonyfillRequest as Request, PonyfillResponse as Response, PonyfillTextDecoder as TextDecoder, PonyfillTextEncoder as TextEncoder, PonyfillBtoa as btoa, fetchPonyfill as fetch };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.1-alpha-20221005131815-b0ce77a",
3
+ "version": "0.0.1-alpha-20221225155816-6b54cb4",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {