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

Feature: ctx.http 使用 fetch 重构 #1181

Closed
Tracked by #1358
idranme opened this issue Aug 24, 2023 · 28 comments
Closed
Tracked by #1358

Feature: ctx.http 使用 fetch 重构 #1181

idranme opened this issue Aug 24, 2023 · 28 comments
Labels
feature 新特性 investigating 正在调查

Comments

@idranme
Copy link

idranme commented Aug 24, 2023

Describe the problem related to the feature request

node16 将于 9 月 11 日停止支持。参见 https://endoflife.date/nodejs

Describe the solution you'd like

ctx.http(method, url, config)
我们可以选择向后兼容 axios 的配置项,或直接使用 fetch 的配置项。

Describe alternatives you've considered

No response

Additional context

No response

@idranme idranme added the feature 新特性 label Aug 24, 2023
@idranme
Copy link
Author

idranme commented Aug 24, 2023

如果选择向后兼容的话,有一个相关的库或许可以参考 https://github.com/dragon-fish/fexios

@shigma
Copy link
Member

shigma commented Aug 24, 2023

Node 16 停止支持不代表 axios 停止支持。

目前 fetch 应该还不支持代理,这会导致超过 50% Koishi 用户无法使用。

@idranme
Copy link
Author

idranme commented Aug 24, 2023

能用的,相关 issue nodejs/node#42814

@idranme
Copy link
Author

idranme commented Aug 24, 2023

@idranme
Copy link
Author

idranme commented Aug 24, 2023

import { ProxyAgent } from 'undici'

const client = new ProxyAgent('http://127.0.0.1:7890/')

const res = await fetch('https://google.com', {
  dispatcher: client
})
const text = await res.text()
console.log(text)

dispatcher 虽然没有类型提示,但是也能用。

下面这个的 dispatcher 有类型提示:

import { fetch, ProxyAgent } from 'undici'

const client = new ProxyAgent('http://127.0.0.1:7890/')

const res = await fetch('https://google.com', {
  dispatcher: client
})
const text = await res.text()
console.log(text)

@simon300000
Copy link
Member

这有什么优点吗?

@shigma
Copy link
Member

shigma commented Aug 24, 2023

这有什么优点吗?

优点是原生了,可以减少不超过 1M 的 bundle size。

同时 axios 不支持 http proxy over https,目前只能使用 socks proxy。如果 fetch 支持的话有望解决这个问题(目前 fetch 到底能不能支持各种协议的 proxy 尚不明确)。

@simon300000
Copy link
Member

simon300000 commented Aug 24, 2023

这有什么优点吗?

优点是原生了,可以减少不超过 1M 的 bundle size。

同时 axios 不支持 http proxy over https,目前只能使用 socks proxy。如果 fetch 支持的话有望解决这个问题(目前 fetch 到底能不能支持各种协议的 proxy 尚不明确)。

按照他说的办法,看起来要再带一个undici

@shigma
Copy link
Member

shigma commented Aug 24, 2023

undici 看起来比 axios 还大 🤣

@idranme
Copy link
Author

idranme commented Aug 24, 2023

不使用 undici 的 fetch 也是可以的,只不过缺失一点类型提示。如果使用 undici 的 fetch,甚至能在 Node 16.8+ 上跑。

@idranme
Copy link
Author

idranme commented Aug 24, 2023

这有什么优点吗?

优点是原生了,可以减少不超过 1M 的 bundle size。

同时 axios 不支持 http proxy over https,目前只能使用 socks proxy。如果 fetch 支持的话有望解决这个问题(目前 fetch 到底能不能支持各种协议的 proxy 尚不明确)。

目前只支持 http 和 https 协议的 proxy

@idranme
Copy link
Author

idranme commented Aug 24, 2023

封装一下 undici,利用打包器摇树应该能解决体积问题。

@idranme
Copy link
Author

idranme commented Aug 24, 2023

undici:
image

axios:
image

@shigma
Copy link
Member

shigma commented Aug 24, 2023

目前只支持 http 和 https 协议的 proxy

那还是不太行,至少等 socks 支持出来吧。我说的 50% 依赖代理的用户基本上全是 socks,你这一改第二天一半机器人全死完了。

@shigma
Copy link
Member

shigma commented Aug 24, 2023

image image image

@idranme
Copy link
Author

idranme commented Aug 24, 2023

image image image

啊这

@shigma
Copy link
Member

shigma commented Aug 24, 2023

封装一下 undici,利用打包器摇树应该能解决体积问题。

说话好歹得有基本常识吧。摇树仅仅在库层面对打包有意义,Koishi 作为一个框架,选用什么依赖肯定是完全打进去,用户根本没有 bundle 这一个环节,哪来的摇树?

@shigma
Copy link
Member

shigma commented Aug 24, 2023

啊这

这俩体积都能接受的。只是放个图对比一下而已。其实没有太大可比性。

主要还是看功能。我还是看好 fetch 的未来的。

@shigma shigma added the investigating 正在调查 label Aug 24, 2023
@idranme
Copy link
Author

idranme commented Aug 24, 2023

目前只支持 http 和 https 协议的 proxy

那还是不太行,至少等 socks 支持出来吧。我说的 50% 依赖代理的用户基本上全是 socks,你这一改第二天一半机器人全死完了。

https://github.com/Kaciras/fetch-socks
目前有个可用的

@idranme
Copy link
Author

idranme commented Aug 24, 2023

或者等 nodejs/undici#2224TooTallNate/proxy-agents#239

@idranme
Copy link
Author

idranme commented Aug 24, 2023

封装一下 undici,利用打包器摇树应该能解决体积问题。

说话好歹得有基本常识吧。摇树仅仅在库层面对打包有意义,Koishi 作为一个框架,选用什么依赖肯定是完全打进去,用户根本没有 bundle 这一个环节,哪来的摇树?

soga

@idranme
Copy link
Author

idranme commented Aug 24, 2023

还有一种选项,参照 https://github.com/Kaciras/fetch-socks 自己写一个 socks 的 agent。

毕竟那个包依赖了一个 undici。

@MaikoTan
Copy link
Member

This can be implemented by a hacky way since node.js's fetch API is based on undici itself comme ici : nodejs/node#43187 (comment)
But it is too hacky to be supported.

@simon300000
Copy link
Member

simon300000 commented Aug 25, 2023

两个都加进去 可以让用户选☝️🤓

@shigma
Copy link
Member

shigma commented Aug 25, 2023

支持不好就老老实实等着,别到时候体积增加了功能减少了一堆插件还 breaking,这不完全是为了一己私欲瞎搞吗?

每次有点小道消息就催着 breaking,axios 又不是没法用了。Koishi 又不是你一个人的软件。

@idranme idranme closed this as completed Aug 25, 2023
@shigma
Copy link
Member

shigma commented Aug 25, 2023

可以不用关,有什么进展持续关注一下。等到能用了就可以做。

@idranme idranme reopened this Aug 25, 2023
@idranme
Copy link
Author

idranme commented Aug 25, 2023

可以不用关,有什么进展持续关注一下。等到能用了就可以做。

嗯嗯

@shigma
Copy link
Member

shigma commented Feb 15, 2024

https://github.com/cordiverse/undios

@shigma shigma closed this as completed Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 新特性 investigating 正在调查
Projects
None yet
Development

No branches or pull requests

4 participants