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

replace axios with node-fetch@2 #157

Merged
merged 1 commit into from
Oct 17, 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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"devDependencies": {
"@types/adm-zip": "^0.5.5",
"@types/node": "^20.11.19",
"@types/node-fetch": "^2.6.11",
"@types/semver": "^7.5.7",
"@types/tar": "^6.1.11",
"@typescript-eslint/eslint-plugin": "^7.0.2",
Expand All @@ -65,11 +66,12 @@
"@inquirer/prompts": "^4.1.0",
"@types/http-proxy": "^1.17.15",
"adm-zip": "^0.5.10",
"axios": "^1.6.7",
"child_process": "^1.0.2",
"ckb-transaction-dumper": "^0.4.0",
"commander": "^12.0.0",
"http-proxy": "^1.18.1",
"https-proxy-agent": "^7.0.5",
"node-fetch": "2",
"semver": "^7.6.0",
"tar": "^6.2.1"
}
Expand Down
15 changes: 13 additions & 2 deletions src/cfg/setting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AxiosProxyConfig } from 'axios';
import * as fs from 'fs';
import * as path from 'path';
import envPaths from './env-path';
Expand All @@ -12,8 +11,20 @@
export const packageSrcPath = path.dirname(require.main!.filename);
export const packageRootPath = path.resolve(packageSrcPath, '../');

export interface ProxyBasicCredentials {
username: string;
password: string;
}

export interface ProxyConfig {
host: string;
port: number;
auth?: ProxyBasicCredentials;
protocol?: string;
}

export interface Settings {
proxy?: AxiosProxyConfig;
proxy?: ProxyConfig;
rpc: {
proxyPort: number;
};
Expand Down Expand Up @@ -143,7 +154,7 @@
return `${getCKBBinaryInstallPath(version)}/ckb`;
}

function deepMerge(target: any, source: any): any {

Check warning on line 157 in src/cfg/setting.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 157 in src/cfg/setting.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 157 in src/cfg/setting.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 157 in src/cfg/setting.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 157 in src/cfg/setting.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 157 in src/cfg/setting.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
for (const key in source) {
if (source[key] && typeof source[key] === 'object') {
if (!target[key]) {
Expand Down
11 changes: 5 additions & 6 deletions src/cmd/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CKB } from '../util/ckb';
import { AxiosRequestConfig } from 'axios';
import { ckbDevnetMinerAccount } from '../cfg/account';
import { NetworkOption, Network } from '../util/type';
import { buildTestnetTxLink } from '../util/link';
import { validateNetworkOpt } from '../util/validator';
import { Request } from '../util/request';
import { RequestInit } from 'node-fetch';

export interface DepositOptions extends NetworkOption {}

Expand Down Expand Up @@ -61,7 +61,7 @@ async function sendClaimRequest(toAddress: string) {
const url = 'https://faucet-api.nervos.org/claim_events'; // Replace 'YOUR_API_ENDPOINT' with the actual API endpoint

const headers = {
'User-Agent': 'axios-requests/2.31.0',
'User-Agent': 'node-fetch-requests/v2',
'Accept-Encoding': 'gzip, deflate',
Accept: '*/*',
Connection: 'keep-alive',
Expand All @@ -75,15 +75,14 @@ async function sendClaimRequest(toAddress: string) {
},
});

const config: AxiosRequestConfig = {
const config: RequestInit = {
method: 'post',
url: url,
headers: headers,
data: body,
body,
};

try {
const response = await Request.send(config);
const response = await Request.send(url, config);
console.log('send claim request, status: ', response.status); // Handle the response data here
} catch (error) {
console.error('Error:', error);
Expand Down
7 changes: 3 additions & 4 deletions src/node/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ export async function downloadCKBBinaryAndUnzip(version: string) {

export async function downloadAndSaveCKBBinary(version: string, tempFilePath: string) {
const downloadURL = buildDownloadUrl(version);
const response = await Request.get(downloadURL, {
responseType: 'arraybuffer',
});
fs.writeFileSync(tempFilePath, response.data);
const response = await Request.send(downloadURL);
const arrayBuffer = await response.arrayBuffer();
fs.writeFileSync(tempFilePath, Buffer.from(arrayBuffer));
}

export async function unZipFile(filePath: string, extractDir: string, useTar: boolean = false) {
Expand Down
7 changes: 3 additions & 4 deletions src/tools/moleculec-es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ export class MoleculecES {

static async downloadAndSaveMoleculeES(version: string, tempFilePath: string) {
const downloadURL = MoleculecES.buildDownloadUrl(version);
const response = await Request.get(downloadURL, {
responseType: 'arraybuffer',
});
fs.writeFileSync(tempFilePath, response.data);
const response = await Request.send(downloadURL);
const arrayBuffer = await response.arrayBuffer();
fs.writeFileSync(tempFilePath, Buffer.from(arrayBuffer));
}

static buildDownloadUrl(version: string): string {
Expand Down
32 changes: 18 additions & 14 deletions src/util/request.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import axios, { AxiosProxyConfig, AxiosRequestConfig } from 'axios';
import { readSettings } from '../cfg/setting';
import { ProxyConfig, readSettings } from '../cfg/setting';
import { HttpsProxyAgent } from 'https-proxy-agent';
import fetch, { RequestInit } from 'node-fetch';

export class Request {
static proxy = readSettings().proxy;

static async send(_config: AxiosRequestConfig) {
const config = this.proxy ? { ...{ proxy: this.proxy }, ..._config } : _config;
return await axios(config);
}

static async get(url: string, _config?: AxiosRequestConfig) {
const config = this.proxy ? { ...{ proxy: this.proxy }, ..._config } : _config;
console.log(config);
return await axios.get(url, config);
static async send(url: string, options: RequestInit = {}) {
const agent = this.proxy ? new HttpsProxyAgent(this.proxyConfigToUrl(this.proxy)) : undefined;
const opt: RequestInit = { ...{ agent }, ...options };
try {
const response = await fetch(url, opt);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}, URL: ${response.url}`);
}
return await response;
} catch (error: unknown) {
throw new Error(`fetch error! ${(error as Error).message}`);
}
}

static parseProxyUrl(url: string): AxiosProxyConfig {
static parseProxyUrl(url: string): ProxyConfig {
const parsedUrl = new URL(url);

const proxyConfig: AxiosProxyConfig = {
const proxyConfig: ProxyConfig = {
host: parsedUrl.hostname,
port: parseInt(parsedUrl.port, 10),
};
Expand All @@ -37,7 +41,7 @@ export class Request {
return proxyConfig;
}

static proxyConfigToUrl(proxy: AxiosProxyConfig): string {
static proxyConfigToUrl(proxy: ProxyConfig): string {
const protocol = proxy.protocol ? `${proxy.protocol}://` : '';
const auth = proxy.auth ? `${proxy.auth.username}:${proxy.auth.password}@` : '';
const port = proxy.port ? `:${proxy.port}` : '';
Expand Down
56 changes: 36 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,14 @@
dependencies:
"@types/node" "*"

"@types/node-fetch@^2.6.11":
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@*", "@types/node@^20.11.19":
version "20.11.19"
resolved "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz"
Expand Down Expand Up @@ -711,6 +719,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-4.0.0-beta.5.tgz#8d2452c52adedebc3a3e28465d858c11ca315873"
integrity sha512-G965FqalsNyrPqgEGON7nIx1e/OVENSgiEIzyC63haUMuvNnwIgIjMs52hlTCKhkBny7A2ORNlfY9Zu+jmGk1Q==

agent-base@^7.0.2:
version "7.1.1"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-7.1.1.tgz#bdbded7dfb096b751a2a087eeeb9664725b2e317"
integrity sha512-H0TSyFNDMomMNJQBn8wFV5YC/2eJ+VXECwOadZJT554xP6cODZHPX3H9QMQECxvrgiSOP1pHjy1sMWQVYJOUOA==
dependencies:
debug "^4.3.4"

ajv@^6.12.4:
version "6.12.6"
resolved "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz"
Expand Down Expand Up @@ -785,15 +800,6 @@ asynckit@^0.4.0:
resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.6.7:
version "1.6.7"
resolved "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz"
integrity sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==
dependencies:
follow-redirects "^1.15.4"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

b4a@^1.0.1:
version "1.6.6"
resolved "https://registry.npmjs.org/b4a/-/b4a-1.6.6.tgz"
Expand Down Expand Up @@ -1149,6 +1155,13 @@ cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

debug@4:
version "4.3.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52"
integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ==
dependencies:
ms "^2.1.3"

[email protected], debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz"
Expand Down Expand Up @@ -1493,11 +1506,6 @@ follow-redirects@^1.0.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

follow-redirects@^1.15.4:
version "1.15.5"
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz"
integrity sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz"
Expand Down Expand Up @@ -1636,6 +1644,14 @@ http-proxy@^1.18.1:
follow-redirects "^1.0.0"
requires-port "^1.0.0"

https-proxy-agent@^7.0.5:
version "7.0.5"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.5.tgz#9e8b5013873299e11fab6fd548405da2d6c602b2"
integrity sha512-1e4Wqeblerz+tMKPIq2EMGiiWW1dIjZOksyHWSUm1rmuvw/how9hBHZ38lAGj5ID4Ik6EdkOw7NmWPy6LAwalw==
dependencies:
agent-base "^7.0.2"
debug "4"

human-signals@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/human-signals/-/human-signals-5.0.0.tgz"
Expand Down Expand Up @@ -2000,6 +2016,11 @@ [email protected]:
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

ms@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

mute-stream@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/mute-stream/-/mute-stream-1.0.0.tgz"
Expand All @@ -2020,7 +2041,7 @@ natural-compare@^1.4.0:
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

node-fetch@^2.6.12:
node-fetch@2, node-fetch@^2.6.12:
version "2.7.0"
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
Expand Down Expand Up @@ -2148,11 +2169,6 @@ prettier@^3.2.5:
resolved "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz"
integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

punycode@^2.1.0:
version "2.3.1"
resolved "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz"
Expand Down
Loading