@whatwg-node/node-fetch 0.5.0-alpha-20230529144043-3df0e1e → 0.5.0-alpha-20230529145853-8db501a

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.
@@ -6,6 +6,7 @@ const https_1 = require("https");
6
6
  const stream_1 = require("stream");
7
7
  const zlib_1 = require("zlib");
8
8
  const AbortError_js_1 = require("./AbortError.js");
9
+ const non_http_fetch_js_1 = require("./non-http-fetch.js");
9
10
  const Request_js_1 = require("./Request.js");
10
11
  const Response_js_1 = require("./Response.js");
11
12
  const URL_js_1 = require("./URL.js");
@@ -29,12 +30,12 @@ function fetchLegacy(info, init) {
29
30
  try {
30
31
  const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
31
32
  if (url.protocol === 'data:') {
32
- const response = (0, utils_js_1.getResponseForDataUri)(url);
33
+ const response = (0, non_http_fetch_js_1.getResponseForDataUri)(url);
33
34
  resolve(response);
34
35
  return;
35
36
  }
36
37
  if (url.protocol === 'file:') {
37
- const response = (0, utils_js_1.getResponseForFile)(fetchRequest.url);
38
+ const response = (0, non_http_fetch_js_1.getResponseForFile)(fetchRequest.url);
38
39
  resolve(response);
39
40
  return;
40
41
  }
@@ -4,6 +4,7 @@ exports.fetchViaUndici = void 0;
4
4
  const stream_1 = require("stream");
5
5
  const zlib_1 = require("zlib");
6
6
  const undici_1 = require("undici");
7
+ const non_http_fetch_js_1 = require("./non-http-fetch.js");
7
8
  const Request_js_1 = require("./Request.js");
8
9
  const Response_js_1 = require("./Response.js");
9
10
  const URL_js_1 = require("./URL.js");
@@ -16,11 +17,11 @@ async function fetchViaUndici(info, init) {
16
17
  const fetchRequest = info;
17
18
  const url = new URL_js_1.PonyfillURL(fetchRequest.url, 'http://localhost');
18
19
  if (url.protocol === 'data:') {
19
- const response = (0, utils_js_1.getResponseForDataUri)(url);
20
+ const response = (0, non_http_fetch_js_1.getResponseForDataUri)(url);
20
21
  return response;
21
22
  }
22
23
  if (url.protocol === 'file:') {
23
- const response = (0, utils_js_1.getResponseForFile)(fetchRequest.url);
24
+ const response = (0, non_http_fetch_js_1.getResponseForFile)(fetchRequest.url);
24
25
  return response;
25
26
  }
26
27
  const requestBody = (fetchRequest['bodyInit'] != null
package/cjs/fetch.js CHANGED
@@ -1,8 +1,8 @@
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");
4
+ const fetch_legacy_js_1 = require("./fetch-legacy.js");
5
+ const fetch_undici_js_1 = require("./fetch-undici.js");
6
6
  function getNodeMajorVersion() {
7
7
  const version = process.version;
8
8
  const match = version.match(/^v(\d+)/);
@@ -11,9 +11,4 @@ function getNodeMajorVersion() {
11
11
  }
12
12
  return parseInt(match[1]);
13
13
  }
14
- if (getNodeMajorVersion() >= 19) {
15
- exports.fetchPonyfill = fetch_undici_1.fetchViaUndici;
16
- }
17
- else {
18
- exports.fetchPonyfill = fetch_legacy_1.fetchLegacy;
19
- }
14
+ exports.fetchPonyfill = getNodeMajorVersion() >= 19 ? fetch_undici_js_1.fetchViaUndici : fetch_legacy_js_1.fetchLegacy;
@@ -0,0 +1,35 @@
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;
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;
@@ -3,10 +3,11 @@ import { request as httpsRequest } from 'https';
3
3
  import { Readable } from 'stream';
4
4
  import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
5
5
  import { PonyfillAbortError } from './AbortError.js';
6
+ import { getResponseForDataUri, getResponseForFile } from './non-http-fetch.js';
6
7
  import { PonyfillRequest } from './Request.js';
7
8
  import { PonyfillResponse } from './Response.js';
8
9
  import { PonyfillURL } from './URL.js';
9
- import { getHeadersObj, getResponseForDataUri, getResponseForFile } from './utils.js';
10
+ import { getHeadersObj } from './utils.js';
10
11
  function getRequestFnForProtocol(protocol) {
11
12
  switch (protocol) {
12
13
  case 'http:':
@@ -1,10 +1,11 @@
1
1
  import { Readable } from 'stream';
2
2
  import { createBrotliDecompress, createGunzip, createInflate } from 'zlib';
3
3
  import { request } from 'undici';
4
+ import { getResponseForDataUri, getResponseForFile } from './non-http-fetch.js';
4
5
  import { PonyfillRequest } from './Request.js';
5
6
  import { PonyfillResponse } from './Response.js';
6
7
  import { PonyfillURL } from './URL.js';
7
- import { getHeadersObj, getResponseForDataUri, getResponseForFile } from './utils.js';
8
+ import { getHeadersObj } from './utils.js';
8
9
  export async function fetchViaUndici(info, init) {
9
10
  if (typeof info === 'string' || 'href' in info) {
10
11
  const ponyfillRequest = new PonyfillRequest(info, init);
package/esm/fetch.js CHANGED
@@ -1,5 +1,5 @@
1
- import { fetchLegacy } from './fetch-legacy';
2
- import { fetchViaUndici } from './fetch-undici';
1
+ import { fetchLegacy } from './fetch-legacy.js';
2
+ import { fetchViaUndici } from './fetch-undici.js';
3
3
  function getNodeMajorVersion() {
4
4
  const version = process.version;
5
5
  const match = version.match(/^v(\d+)/);
@@ -8,10 +8,4 @@ function getNodeMajorVersion() {
8
8
  }
9
9
  return parseInt(match[1]);
10
10
  }
11
- export let fetchPonyfill;
12
- if (getNodeMajorVersion() >= 19) {
13
- fetchPonyfill = fetchViaUndici;
14
- }
15
- else {
16
- fetchPonyfill = fetchLegacy;
17
- }
11
+ export const fetchPonyfill = getNodeMajorVersion() >= 19 ? fetchViaUndici : fetchLegacy;
@@ -0,0 +1,30 @@
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
+ }
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-20230529145853-8db501a",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "dependencies": {
@@ -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 { fetchLegacy } from './fetch-legacy.cjs';
2
+ import { fetchViaUndici } from './fetch-undici.cjs';
3
+ export declare const fetchPonyfill: typeof fetchLegacy | typeof fetchViaUndici;
@@ -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 { fetchLegacy } from './fetch-legacy.js';
2
+ import { fetchViaUndici } from './fetch-undici.js';
3
+ export declare const fetchPonyfill: typeof fetchLegacy | typeof fetchViaUndici;
@@ -0,0 +1,3 @@
1
+ import { PonyfillResponse } from './Response';
2
+ export declare function getResponseForFile(url: string): PonyfillResponse<any>;
3
+ export declare function getResponseForDataUri(url: URL): PonyfillResponse<any>;
@@ -0,0 +1,3 @@
1
+ import { PonyfillResponse } from './Response';
2
+ export declare function getResponseForFile(url: string): PonyfillResponse<any>;
3
+ 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,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>;