@whatwg-node/node-fetch 0.5.0-alpha-20230529145853-8db501a → 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,14 +1,107 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchPonyfill = void 0;
4
- const fetch_legacy_js_1 = require("./fetch-legacy.js");
5
- const fetch_undici_js_1 = require("./fetch-undici.js");
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
- exports.fetchPonyfill = getNodeMajorVersion() >= 19 ? fetch_undici_js_1.fetchViaUndici : fetch_legacy_js_1.fetchLegacy;
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
+ });
37
+ }
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
+ });
106
+ }
107
+ exports.fetchPonyfill = fetchPonyfill;
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,11 +1,103 @@
1
- import { fetchLegacy } from './fetch-legacy.js';
2
- import { fetchViaUndici } from './fetch-undici.js';
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);
14
+ }
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
+ });
34
+ }
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
+ });
10
103
  }
11
- export const fetchPonyfill = getNodeMajorVersion() >= 19 ? fetchViaUndici : fetchLegacy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.5.0-alpha-20230529145853-8db501a",
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.cjs';
2
- import { fetchViaUndici } from './fetch-undici.cjs';
3
- export declare const 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.js';
2
- import { fetchViaUndici } from './fetch-undici.js';
3
- export declare const 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,121 +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 non_http_fetch_js_1 = require("./non-http-fetch.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
- const utils_js_1 = require("./utils.js");
14
- function getRequestFnForProtocol(protocol) {
15
- switch (protocol) {
16
- case 'http:':
17
- return http_1.request;
18
- case 'https:':
19
- return https_1.request;
20
- }
21
- throw new Error(`Unsupported protocol: ${protocol}`);
22
- }
23
- function fetchLegacy(info, init) {
24
- if (typeof info === 'string' || 'href' in info) {
25
- const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
26
- return fetchLegacy(ponyfillRequest);
27
- }
28
- const fetchRequest = info;
29
- return new Promise((resolve, reject) => {
30
- try {
31
- const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
32
- if (url.protocol === 'data:') {
33
- const response = (0, non_http_fetch_js_1.getResponseForDataUri)(url);
34
- resolve(response);
35
- return;
36
- }
37
- if (url.protocol === 'file:') {
38
- const response = (0, non_http_fetch_js_1.getResponseForFile)(fetchRequest.url);
39
- resolve(response);
40
- return;
41
- }
42
- const requestFn = getRequestFnForProtocol(url.protocol);
43
- const nodeReadable = (fetchRequest.body != null
44
- ? 'pipe' in fetchRequest.body
45
- ? fetchRequest.body
46
- : stream_1.Readable.from(fetchRequest.body)
47
- : null);
48
- const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
49
- const nodeHeaders = headersSerializer(fetchRequest.headers);
50
- const nodeRequest = requestFn(fetchRequest.url, {
51
- method: fetchRequest.method,
52
- headers: nodeHeaders,
53
- signal: fetchRequest.signal,
54
- });
55
- // TODO: will be removed after v16 reaches EOL
56
- fetchRequest.signal?.addEventListener('abort', () => {
57
- if (!nodeRequest.aborted) {
58
- nodeRequest.abort();
59
- }
60
- });
61
- // TODO: will be removed after v16 reaches EOL
62
- nodeRequest.once('abort', (reason) => {
63
- reject(new AbortError_js_1.PonyfillAbortError(reason));
64
- });
65
- nodeRequest.once('response', nodeResponse => {
66
- let responseBody = nodeResponse;
67
- const contentEncoding = nodeResponse.headers['content-encoding'];
68
- switch (contentEncoding) {
69
- case 'x-gzip':
70
- case 'gzip':
71
- responseBody = nodeResponse.pipe((0, zlib_1.createGunzip)());
72
- break;
73
- case 'x-deflate':
74
- case 'deflate':
75
- responseBody = nodeResponse.pipe((0, zlib_1.createInflate)());
76
- break;
77
- case 'br':
78
- responseBody = nodeResponse.pipe((0, zlib_1.createBrotliDecompress)());
79
- break;
80
- }
81
- if (nodeResponse.headers.location) {
82
- if (fetchRequest.redirect === 'error') {
83
- const redirectError = new Error('Redirects are not allowed');
84
- reject(redirectError);
85
- nodeResponse.resume();
86
- return;
87
- }
88
- if (fetchRequest.redirect === 'follow') {
89
- const redirectedUrl = new URL_js_1.PonyfillURL(nodeResponse.headers.location, url);
90
- const redirectResponse$ = fetchLegacy(redirectedUrl, info);
91
- resolve(redirectResponse$.then(redirectResponse => {
92
- redirectResponse.redirected = true;
93
- return redirectResponse;
94
- }));
95
- nodeResponse.resume();
96
- return;
97
- }
98
- }
99
- const responseHeaders = nodeResponse.headers;
100
- const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
101
- status: nodeResponse.statusCode,
102
- statusText: nodeResponse.statusMessage,
103
- headers: responseHeaders,
104
- url: info.url,
105
- });
106
- resolve(ponyfillResponse);
107
- });
108
- nodeRequest.once('error', reject);
109
- if (nodeReadable) {
110
- nodeReadable.pipe(nodeRequest);
111
- }
112
- else {
113
- nodeRequest.end();
114
- }
115
- }
116
- catch (e) {
117
- reject(e);
118
- }
119
- });
120
- }
121
- exports.fetchLegacy = fetchLegacy;
@@ -1,72 +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 non_http_fetch_js_1 = require("./non-http-fetch.js");
8
- const Request_js_1 = require("./Request.js");
9
- const Response_js_1 = require("./Response.js");
10
- const URL_js_1 = require("./URL.js");
11
- const utils_js_1 = require("./utils.js");
12
- async function fetchViaUndici(info, init) {
13
- if (typeof info === 'string' || 'href' in info) {
14
- const ponyfillRequest = new Request_js_1.PonyfillRequest(info, init);
15
- return fetchViaUndici(ponyfillRequest);
16
- }
17
- const fetchRequest = info;
18
- const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
19
- if (url.protocol === 'data:') {
20
- const response = (0, non_http_fetch_js_1.getResponseForDataUri)(url);
21
- return response;
22
- }
23
- if (url.protocol === 'file:') {
24
- const response = (0, non_http_fetch_js_1.getResponseForFile)(fetchRequest.url);
25
- return response;
26
- }
27
- const requestBody = (fetchRequest['bodyInit'] != null
28
- ? fetchRequest['bodyInit']
29
- : fetchRequest.body != null
30
- ? 'pipe' in fetchRequest.body
31
- ? fetchRequest.body
32
- : stream_1.Readable.from(fetchRequest.body)
33
- : null);
34
- const headersSerializer = fetchRequest.headersSerializer || utils_js_1.getHeadersObj;
35
- const nodeHeaders = headersSerializer(fetchRequest.headers);
36
- if (requestBody?.[Symbol.toStringTag] === 'FormData') {
37
- delete nodeHeaders['content-type'];
38
- }
39
- const undiciData = await (0, undici_1.request)(fetchRequest.url, {
40
- method: fetchRequest.method,
41
- headers: nodeHeaders,
42
- body: requestBody,
43
- signal: fetchRequest.signal,
44
- maxRedirections: fetchRequest.redirect === 'follow' ? 20 : 0,
45
- });
46
- if (fetchRequest.redirect === 'error' && undiciData.headers.location) {
47
- const redirectError = new Error('Redirects are not allowed');
48
- throw redirectError;
49
- }
50
- let responseBody = undiciData.body;
51
- const contentEncoding = undiciData.headers['content-encoding'];
52
- switch (contentEncoding) {
53
- case 'x-gzip':
54
- case 'gzip':
55
- responseBody = responseBody.pipe((0, zlib_1.createGunzip)());
56
- break;
57
- case 'x-deflate':
58
- case 'deflate':
59
- responseBody = responseBody.pipe((0, zlib_1.createInflate)());
60
- break;
61
- case 'br':
62
- responseBody = responseBody.pipe((0, zlib_1.createBrotliDecompress)());
63
- break;
64
- }
65
- const ponyfillResponse = new Response_js_1.PonyfillResponse(responseBody, {
66
- status: undiciData.statusCode,
67
- headers: undiciData.headers,
68
- url: fetchRequest.url,
69
- });
70
- return ponyfillResponse;
71
- }
72
- exports.fetchViaUndici = fetchViaUndici;
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getResponseForDataUri = exports.getResponseForFile = 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");
8
- function getResponseForFile(url) {
9
- const path = (0, url_1.fileURLToPath)(url);
10
- const readable = (0, fs_1.createReadStream)(path);
11
- return new Response_1.PonyfillResponse(readable);
12
- }
13
- exports.getResponseForFile = getResponseForFile;
14
- const BASE64_SUFFIX = ';base64';
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 blob = new Blob_1.PonyfillBlob([buffer], { type: realMimeType });
22
- return new Response_1.PonyfillResponse(blob, {
23
- status: 200,
24
- statusText: 'OK',
25
- });
26
- }
27
- return new Response_1.PonyfillResponse(data, {
28
- status: 200,
29
- statusText: 'OK',
30
- headers: {
31
- 'content-type': mimeType,
32
- },
33
- });
34
- }
35
- exports.getResponseForDataUri = getResponseForDataUri;
@@ -1,117 +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 { getResponseForDataUri, getResponseForFile } from './non-http-fetch.js';
7
- import { PonyfillRequest } from './Request.js';
8
- import { PonyfillResponse } from './Response.js';
9
- import { PonyfillURL } from './URL.js';
10
- import { getHeadersObj } from './utils.js';
11
- function getRequestFnForProtocol(protocol) {
12
- switch (protocol) {
13
- case 'http:':
14
- return httpRequest;
15
- case 'https:':
16
- return httpsRequest;
17
- }
18
- throw new Error(`Unsupported protocol: ${protocol}`);
19
- }
20
- export function fetchLegacy(info, init) {
21
- if (typeof info === 'string' || 'href' in info) {
22
- const ponyfillRequest = new PonyfillRequest(info, init);
23
- return fetchLegacy(ponyfillRequest);
24
- }
25
- const fetchRequest = info;
26
- return new Promise((resolve, reject) => {
27
- try {
28
- const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
29
- if (url.protocol === 'data:') {
30
- const response = getResponseForDataUri(url);
31
- resolve(response);
32
- return;
33
- }
34
- if (url.protocol === 'file:') {
35
- const response = getResponseForFile(fetchRequest.url);
36
- resolve(response);
37
- return;
38
- }
39
- const requestFn = getRequestFnForProtocol(url.protocol);
40
- const nodeReadable = (fetchRequest.body != null
41
- ? 'pipe' in fetchRequest.body
42
- ? fetchRequest.body
43
- : Readable.from(fetchRequest.body)
44
- : null);
45
- const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
46
- const nodeHeaders = headersSerializer(fetchRequest.headers);
47
- const nodeRequest = requestFn(fetchRequest.url, {
48
- method: fetchRequest.method,
49
- headers: nodeHeaders,
50
- signal: fetchRequest.signal,
51
- });
52
- // TODO: will be removed after v16 reaches EOL
53
- fetchRequest.signal?.addEventListener('abort', () => {
54
- if (!nodeRequest.aborted) {
55
- nodeRequest.abort();
56
- }
57
- });
58
- // TODO: will be removed after v16 reaches EOL
59
- nodeRequest.once('abort', (reason) => {
60
- reject(new PonyfillAbortError(reason));
61
- });
62
- nodeRequest.once('response', nodeResponse => {
63
- let responseBody = nodeResponse;
64
- const contentEncoding = nodeResponse.headers['content-encoding'];
65
- switch (contentEncoding) {
66
- case 'x-gzip':
67
- case 'gzip':
68
- responseBody = nodeResponse.pipe(createGunzip());
69
- break;
70
- case 'x-deflate':
71
- case 'deflate':
72
- responseBody = nodeResponse.pipe(createInflate());
73
- break;
74
- case 'br':
75
- responseBody = nodeResponse.pipe(createBrotliDecompress());
76
- break;
77
- }
78
- if (nodeResponse.headers.location) {
79
- if (fetchRequest.redirect === 'error') {
80
- const redirectError = new Error('Redirects are not allowed');
81
- reject(redirectError);
82
- nodeResponse.resume();
83
- return;
84
- }
85
- if (fetchRequest.redirect === 'follow') {
86
- const redirectedUrl = new PonyfillURL(nodeResponse.headers.location, url);
87
- const redirectResponse$ = fetchLegacy(redirectedUrl, info);
88
- resolve(redirectResponse$.then(redirectResponse => {
89
- redirectResponse.redirected = true;
90
- return redirectResponse;
91
- }));
92
- nodeResponse.resume();
93
- return;
94
- }
95
- }
96
- const responseHeaders = nodeResponse.headers;
97
- const ponyfillResponse = new PonyfillResponse(responseBody, {
98
- status: nodeResponse.statusCode,
99
- statusText: nodeResponse.statusMessage,
100
- headers: responseHeaders,
101
- url: info.url,
102
- });
103
- resolve(ponyfillResponse);
104
- });
105
- nodeRequest.once('error', reject);
106
- if (nodeReadable) {
107
- nodeReadable.pipe(nodeRequest);
108
- }
109
- else {
110
- nodeRequest.end();
111
- }
112
- }
113
- catch (e) {
114
- reject(e);
115
- }
116
- });
117
- }
@@ -1,68 +0,0 @@
1
- import { Readable } from 'stream';
2
- import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
3
- import { request } from 'undici';
4
- import { getResponseForDataUri, getResponseForFile } from './non-http-fetch.js';
5
- import { PonyfillRequest } from './Request.js';
6
- import { PonyfillResponse } from './Response.js';
7
- import { PonyfillURL } from './URL.js';
8
- import { getHeadersObj } from './utils.js';
9
- export async function fetchViaUndici(info, init) {
10
- if (typeof info === 'string' || 'href' in info) {
11
- const ponyfillRequest = new PonyfillRequest(info, init);
12
- return fetchViaUndici(ponyfillRequest);
13
- }
14
- const fetchRequest = info;
15
- const url = new PonyfillURL(fetchRequest.url, 'http://localhost');
16
- if (url.protocol === 'data:') {
17
- const response = getResponseForDataUri(url);
18
- return response;
19
- }
20
- if (url.protocol === 'file:') {
21
- const response = getResponseForFile(fetchRequest.url);
22
- return response;
23
- }
24
- const requestBody = (fetchRequest['bodyInit'] != null
25
- ? fetchRequest['bodyInit']
26
- : fetchRequest.body != null
27
- ? 'pipe' in fetchRequest.body
28
- ? fetchRequest.body
29
- : Readable.from(fetchRequest.body)
30
- : null);
31
- const headersSerializer = fetchRequest.headersSerializer || getHeadersObj;
32
- const nodeHeaders = headersSerializer(fetchRequest.headers);
33
- if (requestBody?.[Symbol.toStringTag] === 'FormData') {
34
- delete nodeHeaders['content-type'];
35
- }
36
- const undiciData = await request(fetchRequest.url, {
37
- method: fetchRequest.method,
38
- headers: nodeHeaders,
39
- body: requestBody,
40
- signal: fetchRequest.signal,
41
- maxRedirections: fetchRequest.redirect === 'follow' ? 20 : 0,
42
- });
43
- if (fetchRequest.redirect === 'error' && undiciData.headers.location) {
44
- const redirectError = new Error('Redirects are not allowed');
45
- throw redirectError;
46
- }
47
- let responseBody = undiciData.body;
48
- const contentEncoding = undiciData.headers['content-encoding'];
49
- switch (contentEncoding) {
50
- case 'x-gzip':
51
- case 'gzip':
52
- responseBody = responseBody.pipe(createGunzip());
53
- break;
54
- case 'x-deflate':
55
- case 'deflate':
56
- responseBody = responseBody.pipe(createInflate());
57
- break;
58
- case 'br':
59
- responseBody = responseBody.pipe(createBrotliDecompress());
60
- break;
61
- }
62
- const ponyfillResponse = new PonyfillResponse(responseBody, {
63
- status: undiciData.statusCode,
64
- headers: undiciData.headers,
65
- url: fetchRequest.url,
66
- });
67
- return ponyfillResponse;
68
- }
@@ -1,30 +0,0 @@
1
- import { createReadStream } from 'fs';
2
- import { fileURLToPath } from 'url';
3
- import { PonyfillBlob } from './Blob';
4
- import { PonyfillResponse } from './Response';
5
- export function getResponseForFile(url) {
6
- const path = fileURLToPath(url);
7
- const readable = createReadStream(path);
8
- return new PonyfillResponse(readable);
9
- }
10
- const BASE64_SUFFIX = ';base64';
11
- export function getResponseForDataUri(url) {
12
- const [mimeType = 'text/plain', ...datas] = url.pathname.split(',');
13
- const data = decodeURIComponent(datas.join(','));
14
- if (mimeType.endsWith(BASE64_SUFFIX)) {
15
- const buffer = Buffer.from(data, 'base64url');
16
- const realMimeType = mimeType.slice(0, -BASE64_SUFFIX.length);
17
- const blob = new PonyfillBlob([buffer], { type: realMimeType });
18
- return new PonyfillResponse(blob, {
19
- status: 200,
20
- statusText: 'OK',
21
- });
22
- }
23
- return new PonyfillResponse(data, {
24
- status: 200,
25
- statusText: 'OK',
26
- headers: {
27
- 'content-type': mimeType,
28
- },
29
- });
30
- }
@@ -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>>;
@@ -1,3 +0,0 @@
1
- import { PonyfillResponse } from './Response';
2
- export declare function getResponseForFile(url: string): PonyfillResponse<any>;
3
- export declare function getResponseForDataUri(url: URL): PonyfillResponse<any>;
@@ -1,3 +0,0 @@
1
- import { PonyfillResponse } from './Response';
2
- export declare function getResponseForFile(url: string): PonyfillResponse<any>;
3
- export declare function getResponseForDataUri(url: URL): PonyfillResponse<any>;