Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove node-fetch in favour of native fetch #826

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"dependencies": {
"@sinclair/typebox": "^0.32.27",
"@types/tough-cookie": "^4.0.2",
"node-fetch": "^2.6.1",
"tough-cookie": "^4.1.2",
"tough-cookie-file-store": "^2.0.3"
},
Expand All @@ -72,7 +71,6 @@
"@tsconfig/node12": "12.1.3",
"@types/har-format": "^1.2.15",
"@types/jest": "^29.5.13",
"@types/node-fetch": "2.6.11",
"@typescript-eslint/eslint-plugin": "5.62.0",
"@typescript-eslint/parser": "5.62.0",
"eslint": "8.57.1",
Expand Down
1 change: 0 additions & 1 deletion src/env-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { URLSearchParams } from "url";
import fetch from "node-fetch";

export default {
fetch,
Expand Down
2 changes: 0 additions & 2 deletions src/env-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { URLSearchParams } from "url";
import fetch from "node-fetch";
import type { RequestInfo, RequestInit, Response } from "node-fetch";

// This let's us still only require the file if we need it, at runtime.
let fetchDevelFunc: (url: RequestInfo, init?: RequestInit) => Promise<Response>;
Expand Down
8 changes: 3 additions & 5 deletions src/lib/fetchDevel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* istanbul ignore file */
import nodeFetch, { Headers } from "node-fetch";
import fs from "fs";
import crypto from "crypto";

Expand All @@ -11,8 +10,7 @@ class FakeResponse {
Object.keys(props).forEach((key) => (this[key] = props[key]));
const rawHeaders = this.headers;
this.headers = new Headers(rawHeaders);
// node-fetch extension, needed to handle multiple set-cookie headers
this.headers.raw = () => rawHeaders;
this.headers.getSetCookie = () => rawHeaders["set-cookie"];
}

async json() {
Expand All @@ -34,7 +32,7 @@ const cache = {};

async function fetchDevel(url, fetchOptions) {
if (process.env.FETCH_DEVEL === "nocache")
return await nodeFetch(url, fetchOptions);
return await fetch(url, fetchOptions);

// Use query2 for all our tests / fixtures / cache
url = url.replace(
Expand Down Expand Up @@ -70,7 +68,7 @@ async function fetchDevel(url, fetchOptions) {
contentObj = JSON.parse(contentJson);
} catch (error) {
if (error.code === "ENOENT") {
const res = await nodeFetch(origUrl, fetchOptions);
const res = await fetch(origUrl, fetchOptions);

contentObj = {
request: {
Expand Down
7 changes: 3 additions & 4 deletions src/lib/getCrumb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { RequestInfo, RequestInit, Response } from "node-fetch";
import type { ExtendedCookieJar } from "./cookieJar";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: we have to ignore this for csm output.
Expand Down Expand Up @@ -70,7 +69,7 @@ export async function _getCrumb(
};

const response = await fetch(url, fetchOptions);
await processSetCookieHeader(response.headers.raw()["set-cookie"], url);
await processSetCookieHeader(response.headers.getSetCookie(), url);

// logger.debug(response.headers.raw());
// logger.debug(cookieJar);
Expand Down Expand Up @@ -151,7 +150,7 @@ export async function _getCrumb(
// Set-Cookie: CFC=AQABCAFkWkdkjEMdLwQ9&s=AQAAAClxdtC-&g=ZFj24w; Expires=Wed, 8 May 2024 01:18:54 GMT; Domain=consent.yahoo.com; Path=/; Secure
if (
!(await processSetCookieHeader(
collectConsentSubmitResponse.headers.raw()["set-cookie"],
collectConsentSubmitResponse.headers.getSetCookie(),
consentLocation,
))
)
Expand Down Expand Up @@ -189,7 +188,7 @@ export async function _getCrumb(

if (
!(await processSetCookieHeader(
copyConsentResponse.headers.raw()["set-cookie"],
copyConsentResponse.headers.getSetCookie(),
collectConsentSubmitResponseLocation,
))
)
Expand Down
1 change: 0 additions & 1 deletion src/lib/yahooFinanceFetch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as util from "util";
import { jest } from "@jest/globals";
import { Headers } from "node-fetch";

import Queue from "./queue.js";
import _yahooFinanceFetch, {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/yahooFinanceFetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { RequestInfo, RequestInit, Response } from "node-fetch";
import Queue from "./queue.js";

import type { YahooFinanceOptions } from "./options.js";
Expand Down Expand Up @@ -134,7 +133,7 @@ async function yahooFinanceFetch(

const response = (await queue.add(() => fetchFunc(url, fetchOptions))) as any;

const setCookieHeaders = response.headers.raw()["set-cookie"];
const setCookieHeaders = response.headers.getSetCookie();
if (setCookieHeaders) {
if (!this._opts.cookieJar) throw new Error("No cookieJar set");
this._opts.cookieJar.setFromSetCookieHeaders(setCookieHeaders, url);
Expand Down
46 changes: 0 additions & 46 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1760,14 +1760,6 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.1.tgz#283f669ff76d7b8260df8ab7a4262cc83d988256"
integrity sha512-fZQQafSREFyuZcdWFAExYjBiCL7AUCdgsk80iO0q4yihYYdcIiH28CcuPTGFgLOCC8RlW49GSQxdHwZP+I7CNg==

"@types/[email protected]":
version "2.6.11"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.11.tgz#9b39b78665dae0e82a08f02f4967d62c66f95d24"
integrity sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==
dependencies:
"@types/node" "*"
form-data "^4.0.0"

"@types/node@*":
version "22.7.9"
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.7.9.tgz#2bf2797b5e84702d8262ea2cf843c3c3c880d0e9"
Expand Down Expand Up @@ -2132,11 +2124,6 @@ asap@^2.0.0:
resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46"
integrity sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

babel-jest@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.7.0.tgz#f4369919225b684c56085998ac63dbd05be020d5"
Expand Down Expand Up @@ -2537,13 +2524,6 @@ columnify@~1.5.4:
strip-ansi "^3.0.0"
wcwidth "^1.0.0"

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

common-ancestor-path@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/common-ancestor-path/-/common-ancestor-path-1.0.1.tgz#4f7d2d1394d91b7abdf51871c62f71eadb0182a7"
Expand Down Expand Up @@ -2770,11 +2750,6 @@ del@^6.0.0:
rimraf "^3.0.2"
slash "^3.0.0"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

delegates@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
Expand Down Expand Up @@ -3227,15 +3202,6 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.1.1.tgz#c4b489e80096d9df1dfc97c79871aea7c617c469"
integrity sha512-zAoAQiudy+r5SvnSw3KJy5os/oRJYHzrzja/tBDqrZtNhUw8bt6y8OBzMWcjWr+8liV8Eb6yOhw8WZ7VFZ5ZzA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

from2@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/from2/-/from2-2.3.0.tgz#8bfb5502bde4a4d36cfdeea007fcca21d7e382af"
Expand Down Expand Up @@ -4756,18 +4722,6 @@ micromatch@^4.0.4:
braces "^3.0.3"
picomatch "^2.3.1"

[email protected]:
version "1.45.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.45.0.tgz#cceeda21ccd7c3a745eba2decd55d4b73e7879ea"
integrity sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==

mime-types@^2.1.12:
version "2.1.28"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.28.tgz#1160c4757eab2c5363888e005273ecf79d2a0ecd"
integrity sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==
dependencies:
mime-db "1.45.0"

mime@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/mime/-/mime-3.0.0.tgz#b374550dca3a0c18443b0c950a6a58f1931cf7a7"
Expand Down