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

refactor: migrate axios to fetch #564

Merged
merged 1 commit into from
Nov 9, 2023
Merged

refactor: migrate axios to fetch #564

merged 1 commit into from
Nov 9, 2023

Conversation

homura
Copy link
Collaborator

@homura homura commented Nov 8, 2023

Description

Fixes #487

This pull request updates the @ckb-lumos/rpc library to use fetch instead of axios. This change was made to avoid modifying axios's adapter, which could potentially cause issues. By choosing a dependency without the state, Lumos can function better as a library. The fetch is not stateful, and it's widely supported in several runtimes, including the modern browsers, NodeJS, Deno, and Bun

Recently, a project using Axios and Lumos together experienced build failures, resulting in an error message that reads:

ERROR in ./node_modules/@vespaiach/axios-fetch-adapter/index.js 2:0-43
Module not found: Error: Package path ./lib/core/settle is not exported from package 

This error was caused by

/* istanbul ignore next */ const fetchAdapter = require("@vespaiach/axios-fetch-adapter");
/* istanbul ignore next */ axios.defaults.adapter = fetchAdapter.default;

You may notice the httpAgent and httpsAgent were removed from the creation of axios in this pull request, but don't worry, they have never been used in Lumos

#node: CKBComponents.Node = {
url: "",
};

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change)

How Has This Been Tested?

  • unit test

Sorry, something went wrong.

@homura homura requested a review from zhangyouxin November 8, 2023 14:12
Copy link

vercel bot commented Nov 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
lumos-website ✅ Ready (Inspect) Visit Preview Nov 8, 2023 2:12pm

Copy link

codecov bot commented Nov 8, 2023

Codecov Report

Merging #564 (9fc2d67) into develop (a243f11) will increase coverage by 0.04%.
The diff coverage is 80.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #564      +/-   ##
===========================================
+ Coverage    86.96%   87.00%   +0.04%     
===========================================
  Files          115      114       -1     
  Lines        23102    23094       -8     
  Branches      2358     2352       -6     
===========================================
+ Hits         20091    20094       +3     
+ Misses        2967     2956      -11     
  Partials        44       44              
Files Coverage Δ
packages/rpc/src/method.ts 86.81% <88.88%> (+7.98%) ⬆️
packages/rpc/src/index.ts 74.43% <66.66%> (-0.57%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a243f11...9fc2d67. Read the comment docs.

Copy link
Contributor

@zhangyouxin zhangyouxin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this PR also resolves axios adapter problem when making some http(s) API calls in chrome extension environment also?

@homura
Copy link
Collaborator Author

homura commented Nov 9, 2023

Looks like this PR also resolves axios adapter problem when making some http(s) API calls in chrome extension environment also?

you're right, it also works well in ServiceWorker

@homura homura merged commit 93ff1b4 into develop Nov 9, 2023
@homura homura deleted the rm-axios branch November 9, 2023 01:59
@github-actions github-actions bot mentioned this pull request Nov 9, 2023
@homura
Copy link
Collaborator Author

homura commented Nov 27, 2023

You may notice the httpAgent and httpsAgent were removed from the creation of axios in this pull request, but don't worry, they have never been used in Lumos

#node: CKBComponents.Node = {
url: "",
};

I suddenly realized it is wrong, the http(s)Agent may be passed through setNode

public setNode(node: CKBComponents.Node): CKBComponents.Node {
Object.assign(this.node, node);
return this.node;
}

so that we still need to support the http(s)Agent. To support the agent, Lumos should make the fetch customizable, like this

// example for working with keepalive
const customizedFetch: typeof fetch = (request, init) => {
  return globalThis.fetch(request, {
    ...init,
    keepalive: true,
  });
};


const rpc = new RPC(url, { fetch: customizedFetch })
// or if you are still working with NodeJS
import fetch from 'node-fetch'

new PRC(url, {
  fetch: (request, init) => {
    return fetch(request, { ...init, agent: customAgent })
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use one HTTP client instead of mixed use of multiple clients
2 participants