@whatwg-node/node-fetch 0.5.0-alpha-20230529144043-3df0e1e → 0.5.0-alpha-20230530162136-224230c

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,5 +1,4 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.PonyfillBlob = void 0;
5
4
  const ReadableStream_js_1 = require("./ReadableStream.js");
@@ -29,7 +28,6 @@ function isBlob(obj) {
29
28
  class PonyfillBlob {
30
29
  constructor(blobParts, options) {
31
30
  this.blobParts = blobParts;
32
- this[_a] = 'Blob';
33
31
  this.type = options?.type || 'application/octet-stream';
34
32
  this.encoding = options?.encoding || 'utf8';
35
33
  }
@@ -119,4 +117,3 @@ class PonyfillBlob {
119
117
  }
120
118
  }
121
119
  exports.PonyfillBlob = PonyfillBlob;
122
- _a = Symbol.toStringTag;
package/cjs/Body.js CHANGED
@@ -232,12 +232,13 @@ function processBodyInit(bodyInit) {
232
232
  };
233
233
  }
234
234
  if (typeof bodyInit === 'string') {
235
+ const buffer = Buffer.from(bodyInit, 'utf-8');
235
236
  return {
236
237
  bodyType: BodyInitType.String,
237
238
  contentType: 'text/plain;charset=UTF-8',
238
- contentLength: Buffer.byteLength(bodyInit),
239
+ contentLength: buffer.byteLength,
239
240
  bodyFactory() {
240
- const readable = stream_1.Readable.from(bodyInit);
241
+ const readable = stream_1.Readable.from(buffer);
241
242
  return new ReadableStream_js_1.PonyfillReadableStream(readable);
242
243
  },
243
244
  };
@@ -324,6 +325,17 @@ function processBodyInit(bodyInit) {
324
325
  },
325
326
  };
326
327
  }
328
+ if ('stream' in bodyInit) {
329
+ return {
330
+ contentType: bodyInit.type,
331
+ contentLength: bodyInit.size,
332
+ bodyFactory() {
333
+ const bodyStream = bodyInit.stream();
334
+ const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
335
+ return body;
336
+ },
337
+ };
338
+ }
327
339
  if ('sort' in bodyInit) {
328
340
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
329
341
  return {
@@ -347,17 +359,6 @@ function processBodyInit(bodyInit) {
347
359
  },
348
360
  };
349
361
  }
350
- if ('stream' in bodyInit) {
351
- return {
352
- contentType: bodyInit.type,
353
- contentLength: bodyInit.size,
354
- bodyFactory() {
355
- const bodyStream = bodyInit.stream();
356
- const body = new ReadableStream_js_1.PonyfillReadableStream(bodyStream);
357
- return body;
358
- },
359
- };
360
- }
361
362
  if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
362
363
  return {
363
364
  contentType: null,
package/cjs/FormData.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- var _a;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.getStreamFromFormData = exports.PonyfillFormData = void 0;
5
4
  const File_js_1 = require("./File.js");
@@ -7,10 +6,6 @@ const ReadableStream_js_1 = require("./ReadableStream.js");
7
6
  class PonyfillFormData {
8
7
  constructor() {
9
8
  this.map = new Map();
10
- this[_a] = 'FormData';
11
- Object.defineProperty(this.constructor, 'name', {
12
- value: 'FormData',
13
- });
14
9
  }
15
10
  append(name, value, fileName) {
16
11
  let values = this.map.get(name);
@@ -69,7 +64,6 @@ class PonyfillFormData {
69
64
  }
70
65
  }
71
66
  exports.PonyfillFormData = PonyfillFormData;
72
- _a = Symbol.toStringTag;
73
67
  function getStreamFromFormData(formData, boundary = '---') {
74
68
  const entries = [];
75
69
  let sentInitialHeader = false;
package/cjs/fetch.js CHANGED
@@ -1,19 +1,107 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchPonyfill = void 0;
4
- const fetch_legacy_1 = require("./fetch-legacy");
5
- const fetch_undici_1 = require("./fetch-undici");
6
- function getNodeMajorVersion() {
7
- const version = process.version;
8
- const match = version.match(/^v(\d+)/);
9
- if (!match) {
10
- throw new Error(`Unable to parse Node.js version: ${version}`);
11
- }
12
- return parseInt(match[1]);
4
+ const fs_1 = require("fs");
5
+ const stream_1 = require("stream");
6
+ const url_1 = require("url");
7
+ const node_libcurl_1 = require("node-libcurl");
8
+ const fetch_1 = require("@whatwg-node/fetch");
9
+ const Blob_js_1 = require("./Blob.js");
10
+ const Request_js_1 = require("./Request.js");
11
+ const Response_js_1 = require("./Response.js");
12
+ const URL_js_1 = require("./URL.js");
13
+ function getResponseForFile(url) {
14
+ const path = (0, url_1.fileURLToPath)(url);
15
+ const readable = (0, fs_1.createReadStream)(path);
16
+ return new Response_js_1.PonyfillResponse(readable);
13
17
  }
14
- if (getNodeMajorVersion() >= 19) {
15
- exports.fetchPonyfill = fetch_undici_1.fetchViaUndici;
18
+ function getResponseForDataUri(url) {
19
+ const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
20
+ const data = decodeURIComponent(datas.join(','));
21
+ if (mimeType.endsWith(BASE64_SUFFIX)) {
22
+ const buffer = Buffer.from(data, 'base64url');
23
+ const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
24
+ const file = new Blob_js_1.PonyfillBlob([buffer], { type: realMimeType });
25
+ return new Response_js_1.PonyfillResponse(file, {
26
+ status: 200,
27
+ statusText: 'OK',
28
+ });
29
+ }
30
+ return new Response_js_1.PonyfillResponse(data, {
31
+ status: 200,
32
+ statusText: 'OK',
33
+ headers: {
34
+ 'content-type': mimeType,
35
+ },
36
+ });
16
37
  }
17
- else {
18
- exports.fetchPonyfill = fetch_legacy_1.fetchLegacy;
38
+ const BASE64_SUFFIX = ';base64';
39
+ function fetchPonyfill(info, init) {
40
+ if (typeof info === 'string' || 'href' in info) {
41
+ const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
42
+ return fetchPonyfill(ponyfillRequest);
43
+ }
44
+ const fetchRequest = info;
45
+ const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
46
+ if (url.protocol === 'data:') {
47
+ const response = getResponseForDataUri(url);
48
+ return Promise.resolve(response);
49
+ }
50
+ if (url.protocol === 'file:') {
51
+ const response = getResponseForFile(fetchRequest.url);
52
+ return Promise.resolve(response);
53
+ }
54
+ const nodeReadable = (fetchRequest.body != null
55
+ ? 'pipe' in fetchRequest.body
56
+ ? fetchRequest.body
57
+ : stream_1.Readable.from(fetchRequest.body)
58
+ : null);
59
+ const curlyHeaders = [];
60
+ let size = undefined;
61
+ fetchRequest.headers.forEach((value, key) => {
62
+ curlyHeaders.push(`${key}: ${value}`);
63
+ if (key === 'content-length') {
64
+ size = Number(value);
65
+ }
66
+ });
67
+ const curlyOptions = {
68
+ // we want the unparsed binary response to be returned as a stream to us
69
+ curlyStreamResponse: true,
70
+ curlyResponseBodyParser: false,
71
+ upload: nodeReadable != null,
72
+ transferEncoding: false,
73
+ curlyStreamUpload: nodeReadable,
74
+ // this will just make libcurl use their own progress function (which is pretty neat)
75
+ // curlyProgressCallback() { return CurlProgressFunc.Continue },
76
+ // verbose: true,
77
+ httpHeader: curlyHeaders,
78
+ customRequest: fetchRequest.method,
79
+ };
80
+ if (size != null) {
81
+ curlyOptions.inFileSize = size;
82
+ }
83
+ return node_libcurl_1.curly[fetchRequest.method.toLowerCase()](fetchRequest.url, curlyOptions).then(curlyResult => {
84
+ const responseHeaders = new fetch_1.Headers();
85
+ curlyResult.headers.forEach(headerInfo => {
86
+ for (const key in headerInfo) {
87
+ if (key !== 'result') {
88
+ responseHeaders.append(key, headerInfo[key]);
89
+ }
90
+ }
91
+ });
92
+ curlyResult.data.on('error', (err) => {
93
+ if (err.isCurlError && err.code === node_libcurl_1.CurlCode.CURLE_ABORTED_BY_CALLBACK) {
94
+ // this is expected
95
+ }
96
+ else {
97
+ throw err;
98
+ }
99
+ });
100
+ return new Response_js_1.PonyfillResponse(curlyResult.data, {
101
+ status: curlyResult.statusCode,
102
+ headers: responseHeaders,
103
+ url: info.url,
104
+ });
105
+ });
19
106
  }
107
+ exports.fetchPonyfill = fetchPonyfill;
package/cjs/utils.js CHANGED
@@ -1,10 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getResponseForDataUri = exports.getResponseForFile = exports.uint8ArrayToArrayBuffer = exports.getHeadersObj = void 0;
4
- const fs_1 = require("fs");
5
- const url_1 = require("url");
6
- const Blob_1 = require("./Blob");
7
- const Response_1 = require("./Response");
3
+ exports.uint8ArrayToArrayBuffer = exports.getHeadersObj = void 0;
8
4
  function getHeadersObj(headers) {
9
5
  if (headers == null || !('forEach' in headers)) {
10
6
  return headers;
@@ -20,31 +16,3 @@ function uint8ArrayToArrayBuffer(uint8array) {
20
16
  return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
21
17
  }
22
18
  exports.uint8ArrayToArrayBuffer = uint8ArrayToArrayBuffer;
23
- function getResponseForFile(url) {
24
- const path = (0, url_1.fileURLToPath)(url);
25
- const readable = (0, fs_1.createReadStream)(path);
26
- return new Response_1.PonyfillResponse(readable);
27
- }
28
- exports.getResponseForFile = getResponseForFile;
29
- const BASE64_SUFFIX = ';base64';
30
- function getResponseForDataUri(url) {
31
- const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
32
- const data = decodeURIComponent(datas.join(','));
33
- if (mimeType.endsWith(BASE64_SUFFIX)) {
34
- const buffer = Buffer.from(data, 'base64url');
35
- const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
36
- const file = new Blob_1.PonyfillBlob([buffer], { type: realMimeType });
37
- return new Response_1.PonyfillResponse(file, {
38
- status: 200,
39
- statusText: 'OK',
40
- });
41
- }
42
- return new Response_1.PonyfillResponse(data, {
43
- status: 200,
44
- statusText: 'OK',
45
- headers: {
46
- 'content-type': mimeType,
47
- },
48
- });
49
- }
50
- exports.getResponseForDataUri = getResponseForDataUri;
package/esm/Blob.js CHANGED
@@ -1,4 +1,3 @@
1
- var _a;
2
1
  import { PonyfillReadableStream } from './ReadableStream.js';
3
2
  import { uint8ArrayToArrayBuffer } from './utils.js';
4
3
  function getBlobPartAsBuffer(blobPart) {
@@ -26,7 +25,6 @@ function isBlob(obj) {
26
25
  export class PonyfillBlob {
27
26
  constructor(blobParts, options) {
28
27
  this.blobParts = blobParts;
29
- this[_a] = 'Blob';
30
28
  this.type = options?.type || 'application/octet-stream';
31
29
  this.encoding = options?.encoding || 'utf8';
32
30
  }
@@ -115,4 +113,3 @@ export class PonyfillBlob {
115
113
  throw new Error('Not implemented');
116
114
  }
117
115
  }
118
- _a = Symbol.toStringTag;
package/esm/Body.js CHANGED
@@ -227,12 +227,13 @@ function processBodyInit(bodyInit) {
227
227
  };
228
228
  }
229
229
  if (typeof bodyInit === 'string') {
230
+ const buffer = Buffer.from(bodyInit, 'utf-8');
230
231
  return {
231
232
  bodyType: BodyInitType.String,
232
233
  contentType: 'text/plain;charset=UTF-8',
233
- contentLength: Buffer.byteLength(bodyInit),
234
+ contentLength: buffer.byteLength,
234
235
  bodyFactory() {
235
- const readable = Readable.from(bodyInit);
236
+ const readable = Readable.from(buffer);
236
237
  return new PonyfillReadableStream(readable);
237
238
  },
238
239
  };
@@ -319,6 +320,17 @@ function processBodyInit(bodyInit) {
319
320
  },
320
321
  };
321
322
  }
323
+ if ('stream' in bodyInit) {
324
+ return {
325
+ contentType: bodyInit.type,
326
+ contentLength: bodyInit.size,
327
+ bodyFactory() {
328
+ const bodyStream = bodyInit.stream();
329
+ const body = new PonyfillReadableStream(bodyStream);
330
+ return body;
331
+ },
332
+ };
333
+ }
322
334
  if ('sort' in bodyInit) {
323
335
  const contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
324
336
  return {
@@ -342,17 +354,6 @@ function processBodyInit(bodyInit) {
342
354
  },
343
355
  };
344
356
  }
345
- if ('stream' in bodyInit) {
346
- return {
347
- contentType: bodyInit.type,
348
- contentLength: bodyInit.size,
349
- bodyFactory() {
350
- const bodyStream = bodyInit.stream();
351
- const body = new PonyfillReadableStream(bodyStream);
352
- return body;
353
- },
354
- };
355
- }
356
357
  if (bodyInit[Symbol.iterator] || bodyInit[Symbol.asyncIterator]) {
357
358
  return {
358
359
  contentType: null,
package/esm/FormData.js CHANGED
@@ -1,13 +1,8 @@
1
- var _a;
2
1
  import { PonyfillFile } from './File.js';
3
2
  import { PonyfillReadableStream } from './ReadableStream.js';
4
3
  export class PonyfillFormData {
5
4
  constructor() {
6
5
  this.map = new Map();
7
- this[_a] = 'FormData';
8
- Object.defineProperty(this.constructor, 'name', {
9
- value: 'FormData',
10
- });
11
6
  }
12
7
  append(name, value, fileName) {
13
8
  let values = this.map.get(name);
@@ -65,7 +60,6 @@ export class PonyfillFormData {
65
60
  }
66
61
  }
67
62
  }
68
- _a = Symbol.toStringTag;
69
63
  export function getStreamFromFormData(formData, boundary = '---') {
70
64
  const entries = [];
71
65
  let sentInitialHeader = false;
package/esm/fetch.js CHANGED
@@ -1,17 +1,103 @@
1
- import { fetchLegacy } from './fetch-legacy';
2
- import { fetchViaUndici } from './fetch-undici';
3
- function getNodeMajorVersion() {
4
- const version = process.version;
5
- const match = version.match(/^v(\d+)/);
6
- if (!match) {
7
- throw new Error(`Unable to parse Node.js version: ${version}`);
8
- }
9
- return parseInt(match[1]);
1
+ import { createReadStream } from 'fs';
2
+ import { Readable } from 'stream';
3
+ import { fileURLToPath } from 'url';
4
+ import { CurlCode, curly } from 'node-libcurl';
5
+ import { Headers } from '@whatwg-node/fetch';
6
+ import { PonyfillBlob } from './Blob.js';
7
+ import { PonyfillRequest } from './Request.js';
8
+ import { PonyfillResponse } from './Response.js';
9
+ import { PonyfillURL } from './URL.js';
10
+ function getResponseForFile(url) {
11
+ const path = fileURLToPath(url);
12
+ const readable = createReadStream(path);
13
+ return new PonyfillResponse(readable);
10
14
  }
11
- export let fetchPonyfill;
12
- if (getNodeMajorVersion() >= 19) {
13
- fetchPonyfill = fetchViaUndici;
15
+ function getResponseForDataUri(url) {
16
+ const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
17
+ const data = decodeURIComponent(datas.join(','));
18
+ if (mimeType.endsWith(BASE64_SUFFIX)) {
19
+ const buffer = Buffer.from(data, 'base64url');
20
+ const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
21
+ const file = new PonyfillBlob([buffer], { type: realMimeType });
22
+ return new PonyfillResponse(file, {
23
+ status: 200,
24
+ statusText: 'OK',
25
+ });
26
+ }
27
+ return new PonyfillResponse(data, {
28
+ status: 200,
29
+ statusText: 'OK',
30
+ headers: {
31
+ 'content-type': mimeType,
32
+ },
33
+ });
14
34
  }
15
- else {
16
- fetchPonyfill = fetchLegacy;
35
+ const BASE64_SUFFIX = ';base64';
36
+ export function fetchPonyfill(info, init) {
37
+ if (typeof info === 'string' || 'href' in info) {
38
+ const ponyfillRequest = new PonyfillRequest(info, init);
39
+ return fetchPonyfill(ponyfillRequest);
40
+ }
41
+ const fetchRequest = info;
42
+ const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
43
+ if (url.protocol === 'data:') {
44
+ const response = getResponseForDataUri(url);
45
+ return Promise.resolve(response);
46
+ }
47
+ if (url.protocol === 'file:') {
48
+ const response = getResponseForFile(fetchRequest.url);
49
+ return Promise.resolve(response);
50
+ }
51
+ const nodeReadable = (fetchRequest.body != null
52
+ ? 'pipe' in fetchRequest.body
53
+ ? fetchRequest.body
54
+ : Readable.from(fetchRequest.body)
55
+ : null);
56
+ const curlyHeaders = [];
57
+ let size = undefined;
58
+ fetchRequest.headers.forEach((value, key) => {
59
+ curlyHeaders.push(`${key}: ${value}`);
60
+ if (key === 'content-length') {
61
+ size = Number(value);
62
+ }
63
+ });
64
+ const curlyOptions = {
65
+ // we want the unparsed binary response to be returned as a stream to us
66
+ curlyStreamResponse: true,
67
+ curlyResponseBodyParser: false,
68
+ upload: nodeReadable != null,
69
+ transferEncoding: false,
70
+ curlyStreamUpload: nodeReadable,
71
+ // this will just make libcurl use their own progress function (which is pretty neat)
72
+ // curlyProgressCallback() { return CurlProgressFunc.Continue },
73
+ // verbose: true,
74
+ httpHeader: curlyHeaders,
75
+ customRequest: fetchRequest.method,
76
+ };
77
+ if (size != null) {
78
+ curlyOptions.inFileSize = size;
79
+ }
80
+ return curly[fetchRequest.method.toLowerCase()](fetchRequest.url, curlyOptions).then(curlyResult => {
81
+ const responseHeaders = new Headers();
82
+ curlyResult.headers.forEach(headerInfo => {
83
+ for (const key in headerInfo) {
84
+ if (key !== 'result') {
85
+ responseHeaders.append(key, headerInfo[key]);
86
+ }
87
+ }
88
+ });
89
+ curlyResult.data.on('error', (err) => {
90
+ if (err.isCurlError && err.code === CurlCode.CURLE_ABORTED_BY_CALLBACK) {
91
+ // this is expected
92
+ }
93
+ else {
94
+ throw err;
95
+ }
96
+ });
97
+ return new PonyfillResponse(curlyResult.data, {
98
+ status: curlyResult.statusCode,
99
+ headers: responseHeaders,
100
+ url: info.url,
101
+ });
102
+ });
17
103
  }
package/esm/utils.js CHANGED
@@ -1,7 +1,3 @@
1
- import { createReadStream } from 'fs';
2
- import { fileURLToPath } from 'url';
3
- import { PonyfillBlob } from './Blob';
4
- import { PonyfillResponse } from './Response';
5
1
  export function getHeadersObj(headers) {
6
2
  if (headers == null || !('forEach' in headers)) {
7
3
  return headers;
@@ -15,29 +11,3 @@ export function getHeadersObj(headers) {
15
11
  export function uint8ArrayToArrayBuffer(uint8array) {
16
12
  return uint8array.buffer.slice(uint8array.byteOffset, uint8array.byteOffset + uint8array.byteLength);
17
13
  }
18
- export function getResponseForFile(url) {
19
- const path = fileURLToPath(url);
20
- const readable = createReadStream(path);
21
- return new PonyfillResponse(readable);
22
- }
23
- const BASE64_SUFFIX = ';base64';
24
- export function getResponseForDataUri(url) {
25
- const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
26
- const data = decodeURIComponent(datas.join(','));
27
- if (mimeType.endsWith(BASE64_SUFFIX)) {
28
- const buffer = Buffer.from(data, 'base64url');
29
- const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
30
- const file = new PonyfillBlob([buffer], { type: realMimeType });
31
- return new PonyfillResponse(file, {
32
- status: 200,
33
- statusText: 'OK',
34
- });
35
- }
36
- return new PonyfillResponse(data, {
37
- status: 200,
38
- statusText: 'OK',
39
- headers: {
40
- 'content-type': mimeType,
41
- },
42
- });
43
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.5.0-alpha-20230529144043-3df0e1e",
3
+ "version": "0.5.0-alpha-20230530162136-224230c",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -8,8 +8,8 @@
8
8
  "busboy": "^1.6.0",
9
9
  "fast-querystring": "^1.1.1",
10
10
  "fast-url-parser": "^1.1.3",
11
- "tslib": "^2.3.1",
12
- "undici": "^5.22.1"
11
+ "node-libcurl": "^3.0.0",
12
+ "tslib": "^2.3.1"
13
13
  },
14
14
  "repository": {
15
15
  "type": "git",
@@ -22,7 +22,6 @@ export declare class PonyfillBlob implements Blob {
22
22
  get size(): number;
23
23
  stream(): any;
24
24
  slice(): any;
25
- [Symbol.toStringTag]: string;
26
25
  }
27
26
  export interface PonyfillBlob {
28
27
  prototype: Blob;
package/typings/Blob.d.ts CHANGED
@@ -22,7 +22,6 @@ export declare class PonyfillBlob implements Blob {
22
22
  get size(): number;
23
23
  stream(): any;
24
24
  slice(): any;
25
- [Symbol.toStringTag]: string;
26
25
  }
27
26
  export interface PonyfillBlob {
28
27
  prototype: Blob;
@@ -2,7 +2,6 @@ import { PonyfillBlob } from './Blob.cjs';
2
2
  import { PonyfillReadableStream } from './ReadableStream.cjs';
3
3
  export declare class PonyfillFormData implements FormData {
4
4
  private map;
5
- constructor();
6
5
  append(name: string, value: PonyfillBlob | string, fileName?: string): void;
7
6
  delete(name: string): void;
8
7
  get(name: string): FormDataEntryValue | null;
@@ -14,6 +13,5 @@ export declare class PonyfillFormData implements FormData {
14
13
  keys(): IterableIterator<string>;
15
14
  values(): IterableIterator<FormDataEntryValue>;
16
15
  forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
17
- [Symbol.toStringTag]: string;
18
16
  }
19
17
  export declare function getStreamFromFormData(formData: FormData, boundary?: string): PonyfillReadableStream<Uint8Array>;
@@ -2,7 +2,6 @@ import { PonyfillBlob } from './Blob.js';
2
2
  import { PonyfillReadableStream } from './ReadableStream.js';
3
3
  export declare class PonyfillFormData implements FormData {
4
4
  private map;
5
- constructor();
6
5
  append(name: string, value: PonyfillBlob | string, fileName?: string): void;
7
6
  delete(name: string): void;
8
7
  get(name: string): FormDataEntryValue | null;
@@ -14,6 +13,5 @@ export declare class PonyfillFormData implements FormData {
14
13
  keys(): IterableIterator<string>;
15
14
  values(): IterableIterator<FormDataEntryValue>;
16
15
  forEach(callback: (value: FormDataEntryValue, key: string, parent: this) => void): void;
17
- [Symbol.toStringTag]: string;
18
16
  }
19
17
  export declare function getStreamFromFormData(formData: FormData, boundary?: string): PonyfillReadableStream<Uint8Array>;
@@ -1,3 +1,3 @@
1
- import { fetchLegacy } from './fetch-legacy';
2
- import { fetchViaUndici } from './fetch-undici';
3
- export declare let fetchPonyfill: typeof fetchLegacy | typeof fetchViaUndici;
1
+ import { PonyfillRequest, RequestPonyfillInit } from './Request.cjs';
2
+ import { PonyfillResponse } from './Response.cjs';
3
+ export declare function fetchPonyfill<TResponseJSON = any, TRequestJSON = any>(info: string | PonyfillRequest<TRequestJSON> | URL, init?: RequestPonyfillInit): Promise<PonyfillResponse<TResponseJSON>>;
@@ -1,3 +1,3 @@
1
- import { fetchLegacy } from './fetch-legacy';
2
- import { fetchViaUndici } from './fetch-undici';
3
- export declare let fetchPonyfill: typeof fetchLegacy | typeof fetchViaUndici;
1
+ import { PonyfillRequest, RequestPonyfillInit } from './Request.js';
2
+ import { PonyfillResponse } from './Response.js';
3
+ export declare function fetchPonyfill<TResponseJSON = any, TRequestJSON = any>(info: string | PonyfillRequest<TRequestJSON> | URL, init?: RequestPonyfillInit): Promise<PonyfillResponse<TResponseJSON>>;
@@ -1,5 +1,2 @@
1
- import { PonyfillResponse } from './Response';
2
1
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
3
2
  export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;
4
- export declare function getResponseForFile(url: string): PonyfillResponse<any>;
5
- export declare function getResponseForDataUri(url: URL): PonyfillResponse<any>;
@@ -1,5 +1,2 @@
1
- import { PonyfillResponse } from './Response';
2
1
  export declare function getHeadersObj(headers: Headers): Record<string, string>;
3
2
  export declare function uint8ArrayToArrayBuffer(uint8array: Uint8Array): ArrayBuffer;
4
- export declare function getResponseForFile(url: string): PonyfillResponse<any>;
5
- export declare function getResponseForDataUri(url: URL): PonyfillResponse<any>;
@@ -1,120 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchLegacy = void 0;
4
- const http_1 = require("http");
5
- const https_1 = require("https");
6
- const stream_1 = require("stream");
7
- const zlib_1 = require("zlib");
8
- const AbortError_js_1 = require("./AbortError.js");
9
- const Request_js_1 = require("./Request.js");
10
- const Response_js_1 = require("./Response.js");
11
- const URL_js_1 = require("./URL.js");
12
- const utils_js_1 = require("./utils.js");
13
- function getRequestFnForProtocol(protocol) {
14
- switch (protocol) {
15
- case 'http:':
16
- return http_1.request;
17
- case 'https:':
18
- return https_1.request;
19
- }
20
- throw new Error(`Unsupported protocol: ${protocol}`);
21
- }
22
- function fetchLegacy(info, init) {
23
- if (typeof info === 'string' || 'href' in info) {
24
- const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
25
- return fetchLegacy(ponyfillRequest);
26
- }
27
- const fetchRequest = info;
28
- return new Promise((resolve, reject) => {
29
- try {
30
- const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
31
- if (url.protocol === 'data:') {
32
- const response = (0, utils_js_1.getResponseForDataUri)(url);
33
- resolve(response);
34
- return;
35
- }
36
- if (url.protocol === 'file:') {
37
- const response = (0, utils_js_1.getResponseForFile)(fetchRequest.url);
38
- resolve(response);
39
- return;
40
- }
41
- const requestFn = getRequestFnForProtocol(url.protocol);
42
- const nodeReadable = (fetchRequest.body != null
43
- ? 'pipe' in fetchRequest.body
44
- ? fetchRequest.body
45
- : stream_1.Readable.from(fetchRequest.body)
46
- : null);
47
- const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
48
- const nodeHeaders = headersSerializer(fetchRequest.headers);
49
- const nodeRequest = requestFn(fetchRequest.url, {
50
- method: fetchRequest.method,
51
- headers: nodeHeaders,
52
- signal: fetchRequest.signal,
53
- });
54
- // TODO: will be removed after v16 reaches EOL
55
- fetchRequest.signal?.addEventListener('abort', () => {
56
- if (!nodeRequest.aborted) {
57
- nodeRequest.abort();
58
- }
59
- });
60
- // TODO: will be removed after v16 reaches EOL
61
- nodeRequest.once('abort', (reason) => {
62
- reject(new AbortError_js_1.PonyfillAbortError(reason));
63
- });
64
- nodeRequest.once('response', nodeResponse => {
65
- let responseBody = nodeResponse;
66
- const contentEncoding = nodeResponse.headers['content-encoding'];
67
- switch (contentEncoding) {
68
- case 'x-gzip':
69
- case 'gzip':
70
- responseBody = nodeResponse.pipe((0, zlib_1.createGunzip)());
71
- break;
72
- case 'x-deflate':
73
- case 'deflate':
74
- responseBody = nodeResponse.pipe((0, zlib_1.createInflate)());
75
- break;
76
- case 'br':
77
- responseBody = nodeResponse.pipe((0, zlib_1.createBrotliDecompress)());
78
- break;
79
- }
80
- if (nodeResponse.headers.location) {
81
- if (fetchRequest.redirect === 'error') {
82
- const redirectError = new Error('Redirects are not allowed');
83
- reject(redirectError);
84
- nodeResponse.resume();
85
- return;
86
- }
87
- if (fetchRequest.redirect === 'follow') {
88
- const redirectedUrl = new URL_js_1.PonyfillURL(nodeResponse.headers.location, url);
89
- const redirectResponse$ = fetchLegacy(redirectedUrl, info);
90
- resolve(redirectResponse$.then(redirectResponse => {
91
- redirectResponse.redirected = true;
92
- return redirectResponse;
93
- }));
94
- nodeResponse.resume();
95
- return;
96
- }
97
- }
98
- const responseHeaders = nodeResponse.headers;
99
- const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
100
- status: nodeResponse.statusCode,
101
- statusText: nodeResponse.statusMessage,
102
- headers: responseHeaders,
103
- url: info.url,
104
- });
105
- resolve(ponyfillResponse);
106
- });
107
- nodeRequest.once('error', reject);
108
- if (nodeReadable) {
109
- nodeReadable.pipe(nodeRequest);
110
- }
111
- else {
112
- nodeRequest.end();
113
- }
114
- }
115
- catch (e) {
116
- reject(e);
117
- }
118
- });
119
- }
120
- exports.fetchLegacy = fetchLegacy;
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetchViaUndici = void 0;
4
- const stream_1 = require("stream");
5
- const zlib_1 = require("zlib");
6
- const undici_1 = require("undici");
7
- const Request_js_1 = require("./Request.js");
8
- const Response_js_1 = require("./Response.js");
9
- const URL_js_1 = require("./URL.js");
10
- const utils_js_1 = require("./utils.js");
11
- async function fetchViaUndici(info, init) {
12
- if (typeof info === 'string' || 'href' in info) {
13
- const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
14
- return fetchViaUndici(ponyfillRequest);
15
- }
16
- const fetchRequest = info;
17
- const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
18
- if (url.protocol === 'data:') {
19
- const response = (0, utils_js_1.getResponseForDataUri)(url);
20
- return response;
21
- }
22
- if (url.protocol === 'file:') {
23
- const response = (0, utils_js_1.getResponseForFile)(fetchRequest.url);
24
- return response;
25
- }
26
- const requestBody = (fetchRequest['bodyInit'] != null
27
- ? fetchRequest['bodyInit']
28
- : fetchRequest.body != null
29
- ? 'pipe' in fetchRequest.body
30
- ? fetchRequest.body
31
- : stream_1.Readable.from(fetchRequest.body)
32
- : null);
33
- const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
34
- const nodeHeaders = headersSerializer(fetchRequest.headers);
35
- if (requestBody?.[Symbol.toStringTag] === 'FormData') {
36
- delete nodeHeaders['content-type'];
37
- }
38
- const undiciData = await (0, undici_1.request)(fetchRequest.url, {
39
- method: fetchRequest.method,
40
- headers: nodeHeaders,
41
- body: requestBody,
42
- signal: fetchRequest.signal,
43
- maxRedirections: fetchRequest.redirect === 'follow' ? 20 : 0,
44
- });
45
- if (fetchRequest.redirect === 'error' && undiciData.headers.location) {
46
- const redirectError = new Error('Redirects are not allowed');
47
- throw redirectError;
48
- }
49
- let responseBody = undiciData.body;
50
- const contentEncoding = undiciData.headers['content-encoding'];
51
- switch (contentEncoding) {
52
- case 'x-gzip':
53
- case 'gzip':
54
- responseBody = responseBody.pipe((0, zlib_1.createGunzip)());
55
- break;
56
- case 'x-deflate':
57
- case 'deflate':
58
- responseBody = responseBody.pipe((0, zlib_1.createInflate)());
59
- break;
60
- case 'br':
61
- responseBody = responseBody.pipe((0, zlib_1.createBrotliDecompress)());
62
- break;
63
- }
64
- const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
65
- status: undiciData.statusCode,
66
- headers: undiciData.headers,
67
- url: fetchRequest.url,
68
- });
69
- return ponyfillResponse;
70
- }
71
- exports.fetchViaUndici = fetchViaUndici;
@@ -1,116 +0,0 @@
1
- import { request as httpRequest } from 'http';
2
- import { request as httpsRequest } from 'https';
3
- import { Readable } from 'stream';
4
- import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
5
- import { PonyfillAbortError } from './AbortError.js';
6
- import { PonyfillRequest } from './Request.js';
7
- import { PonyfillResponse } from './Response.js';
8
- import { PonyfillURL } from './URL.js';
9
- import { getHeadersObj, getResponseForDataUri, getResponseForFile } from './utils.js';
10
- function getRequestFnForProtocol(protocol) {
11
- switch (protocol) {
12
- case 'http:':
13
- return httpRequest;
14
- case 'https:':
15
- return httpsRequest;
16
- }
17
- throw new Error(`Unsupported protocol: ${protocol}`);
18
- }
19
- export function fetchLegacy(info, init) {
20
- if (typeof info === 'string' || 'href' in info) {
21
- const ponyfillRequest = new PonyfillRequest(info, init);
22
- return fetchLegacy(ponyfillRequest);
23
- }
24
- const fetchRequest = info;
25
- return new Promise((resolve, reject) => {
26
- try {
27
- const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
28
- if (url.protocol === 'data:') {
29
- const response = getResponseForDataUri(url);
30
- resolve(response);
31
- return;
32
- }
33
- if (url.protocol === 'file:') {
34
- const response = getResponseForFile(fetchRequest.url);
35
- resolve(response);
36
- return;
37
- }
38
- const requestFn = getRequestFnForProtocol(url.protocol);
39
- const nodeReadable = (fetchRequest.body != null
40
- ? 'pipe' in fetchRequest.body
41
- ? fetchRequest.body
42
- : Readable.from(fetchRequest.body)
43
- : null);
44
- const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
45
- const nodeHeaders = headersSerializer(fetchRequest.headers);
46
- const nodeRequest = requestFn(fetchRequest.url, {
47
- method: fetchRequest.method,
48
- headers: nodeHeaders,
49
- signal: fetchRequest.signal,
50
- });
51
- // TODO: will be removed after v16 reaches EOL
52
- fetchRequest.signal?.addEventListener('abort', () => {
53
- if (!nodeRequest.aborted) {
54
- nodeRequest.abort();
55
- }
56
- });
57
- // TODO: will be removed after v16 reaches EOL
58
- nodeRequest.once('abort', (reason) => {
59
- reject(new PonyfillAbortError(reason));
60
- });
61
- nodeRequest.once('response', nodeResponse => {
62
- let responseBody = nodeResponse;
63
- const contentEncoding = nodeResponse.headers['content-encoding'];
64
- switch (contentEncoding) {
65
- case 'x-gzip':
66
- case 'gzip':
67
- responseBody = nodeResponse.pipe(createGunzip());
68
- break;
69
- case 'x-deflate':
70
- case 'deflate':
71
- responseBody = nodeResponse.pipe(createInflate());
72
- break;
73
- case 'br':
74
- responseBody = nodeResponse.pipe(createBrotliDecompress());
75
- break;
76
- }
77
- if (nodeResponse.headers.location) {
78
- if (fetchRequest.redirect === 'error') {
79
- const redirectError = new Error('Redirects are not allowed');
80
- reject(redirectError);
81
- nodeResponse.resume();
82
- return;
83
- }
84
- if (fetchRequest.redirect === 'follow') {
85
- const redirectedUrl = new PonyfillURL(nodeResponse.headers.location, url);
86
- const redirectResponse$ = fetchLegacy(redirectedUrl, info);
87
- resolve(redirectResponse$.then(redirectResponse => {
88
- redirectResponse.redirected = true;
89
- return redirectResponse;
90
- }));
91
- nodeResponse.resume();
92
- return;
93
- }
94
- }
95
- const responseHeaders = nodeResponse.headers;
96
- const ponyfillResponse = new PonyfillResponse(responseBody, {
97
- status: nodeResponse.statusCode,
98
- statusText: nodeResponse.statusMessage,
99
- headers: responseHeaders,
100
- url: info.url,
101
- });
102
- resolve(ponyfillResponse);
103
- });
104
- nodeRequest.once('error', reject);
105
- if (nodeReadable) {
106
- nodeReadable.pipe(nodeRequest);
107
- }
108
- else {
109
- nodeRequest.end();
110
- }
111
- }
112
- catch (e) {
113
- reject(e);
114
- }
115
- });
116
- }
@@ -1,67 +0,0 @@
1
- import { Readable } from 'stream';
2
- import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
3
- import { request } from 'undici';
4
- import { PonyfillRequest } from './Request.js';
5
- import { PonyfillResponse } from './Response.js';
6
- import { PonyfillURL } from './URL.js';
7
- import { getHeadersObj, getResponseForDataUri, getResponseForFile } from './utils.js';
8
- export async function fetchViaUndici(info, init) {
9
- if (typeof info === 'string' || 'href' in info) {
10
- const ponyfillRequest = new PonyfillRequest(info, init);
11
- return fetchViaUndici(ponyfillRequest);
12
- }
13
- const fetchRequest = info;
14
- const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
15
- if (url.protocol === 'data:') {
16
- const response = getResponseForDataUri(url);
17
- return response;
18
- }
19
- if (url.protocol === 'file:') {
20
- const response = getResponseForFile(fetchRequest.url);
21
- return response;
22
- }
23
- const requestBody = (fetchRequest['bodyInit'] != null
24
- ? fetchRequest['bodyInit']
25
- : fetchRequest.body != null
26
- ? 'pipe' in fetchRequest.body
27
- ? fetchRequest.body
28
- : Readable.from(fetchRequest.body)
29
- : null);
30
- const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
31
- const nodeHeaders = headersSerializer(fetchRequest.headers);
32
- if (requestBody?.[Symbol.toStringTag] === 'FormData') {
33
- delete nodeHeaders['content-type'];
34
- }
35
- const undiciData = await request(fetchRequest.url, {
36
- method: fetchRequest.method,
37
- headers: nodeHeaders,
38
- body: requestBody,
39
- signal: fetchRequest.signal,
40
- maxRedirections: fetchRequest.redirect === 'follow' ? 20 : 0,
41
- });
42
- if (fetchRequest.redirect === 'error' && undiciData.headers.location) {
43
- const redirectError = new Error('Redirects are not allowed');
44
- throw redirectError;
45
- }
46
- let responseBody = undiciData.body;
47
- const contentEncoding = undiciData.headers['content-encoding'];
48
- switch (contentEncoding) {
49
- case 'x-gzip':
50
- case 'gzip':
51
- responseBody = responseBody.pipe(createGunzip());
52
- break;
53
- case 'x-deflate':
54
- case 'deflate':
55
- responseBody = responseBody.pipe(createInflate());
56
- break;
57
- case 'br':
58
- responseBody = responseBody.pipe(createBrotliDecompress());
59
- break;
60
- }
61
- const ponyfillResponse = new PonyfillResponse(responseBody, {
62
- status: undiciData.statusCode,
63
- headers: undiciData.headers,
64
- url: fetchRequest.url,
65
- });
66
- return ponyfillResponse;
67
- }
@@ -1,3 +0,0 @@
1
- import { PonyfillRequest, RequestPonyfillInit } from './Request.cjs';
2
- import { PonyfillResponse } from './Response.cjs';
3
- export declare function fetchLegacy<TResponseJSON = any, TRequestJSON = any>(info: string | PonyfillRequest<TRequestJSON> | URL, init?: RequestPonyfillInit): Promise<PonyfillResponse<TResponseJSON>>;
@@ -1,3 +0,0 @@
1
- import { PonyfillRequest, RequestPonyfillInit } from './Request.js';
2
- import { PonyfillResponse } from './Response.js';
3
- export declare function fetchLegacy<TResponseJSON = any, TRequestJSON = any>(info: string | PonyfillRequest<TRequestJSON> | URL, init?: RequestPonyfillInit): Promise<PonyfillResponse<TResponseJSON>>;
@@ -1,3 +0,0 @@
1
- import { PonyfillRequest, RequestPonyfillInit } from './Request.cjs';
2
- import { PonyfillResponse } from './Response.cjs';
3
- export declare function fetchViaUndici<TResponseJSON = any, TRequestJSON = any>(info: string | PonyfillRequest<TRequestJSON> | URL, init?: RequestPonyfillInit): Promise<PonyfillResponse<TResponseJSON>>;
@@ -1,3 +0,0 @@
1
- import { PonyfillRequest, RequestPonyfillInit } from './Request.js';
2
- import { PonyfillResponse } from './Response.js';
3
- export declare function fetchViaUndici<TResponseJSON = any, TRequestJSON = any>(info: string | PonyfillRequest<TRequestJSON> | URL, init?: RequestPonyfillInit): Promise<PonyfillResponse<TResponseJSON>>;