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

feat(axios): support keep alive #235

Merged
merged 2 commits into from
Feb 2, 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: 1 addition & 1 deletion packages/axios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"@types/mime-db": "^1.43.5",
"@types/ws": "^8.5.10",
"agent-base": "^6.0.2"
"agent-base": "^7.1.0"
},
"dependencies": {
"axios": "^1.6.2",
Expand Down
11 changes: 6 additions & 5 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Agent } from 'agent-base'
import { arrayBufferToBase64, base64ToArrayBuffer, Dict, pick, trimSlash } from 'cosmokit'
import { ClientRequestArgs } from 'http'
import { AgentOptions, ClientRequestArgs } from 'http'
import mimedb from 'mime-db'
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios'
import * as Axios from 'axios'
Expand Down Expand Up @@ -71,9 +71,9 @@ export class Quester {
if (!url) return
if (Quester.agents[url]) return Quester.agents[url]
const { protocol } = new URL(url)
const callback = Quester.proxies[protocol.slice(0, -1)]
if (!callback) return
const agent = callback(url)
const Callback = Quester.proxies[protocol.slice(0, -1)]
if (!Callback) return
const agent = new Callback(url, { keepAlive: this.config.keepAlive ?? false })
if (persist) Quester.agents[url] = agent
return agent
}
Expand Down Expand Up @@ -167,7 +167,7 @@ export namespace Quester {
export type Method = Axios.Method
export type AxiosResponse = Axios.AxiosResponse
export type AxiosRequestConfig = Axios.AxiosRequestConfig
export type CreateAgent = (opts: string) => Agent
export type CreateAgent = new (uri: string, opts: AgentOptions) => Agent

export const agents: Dict<Agent> = Object.create(null)
export const proxies: Dict<CreateAgent> = Object.create(null)
Expand Down Expand Up @@ -195,6 +195,7 @@ export namespace Quester {
endpoint?: string
timeout?: number
proxyAgent?: string
keepAlive?: boolean
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare module 'cordis-axios' {
defineProperty(Quester, 'Config', Schema.object({
timeout: Schema.natural().role('ms').description('等待连接建立的最长时间。'),
proxyAgent: Schema.string().description('使用的代理服务器地址。'),
keepAlive: Schema.boolean().description('是否保持连接。'),
}).description('请求设置'))

Quester.createConfig = function createConfig(this, endpoint) {
Expand Down
6 changes: 3 additions & 3 deletions packages/satori/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
"@satorijs/core": "3.4.5",
"@types/ws": "^8.5.10",
"file-type": "^16.5.4",
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.1",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.1",
"parseurl": "^1.3.3",
"path-to-regexp": "^6.2.1",
"socks-proxy-agent": "^5.0.1",
"socks-proxy-agent": "^8.0.2",
"ws": "^8.14.2"
}
}
12 changes: 6 additions & 6 deletions packages/satori/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { basename } from 'path'
import { promises as fs } from 'fs'
import { fileURLToPath } from 'url'
import FileType from 'file-type'
import createHttpProxyAgent from 'http-proxy-agent'
import createHttpsProxyAgent from 'https-proxy-agent'
import createSocksProxyAgent from 'socks-proxy-agent'
import { HttpProxyAgent } from 'http-proxy-agent'
import { HttpsProxyAgent } from 'https-proxy-agent'
import { SocksProxyAgent } from 'socks-proxy-agent'

export * from '@satorijs/core'
export * from 'cosmokit'
Expand Down Expand Up @@ -50,6 +50,6 @@ Quester.prototype.prepare = function prepare(this: Quester) {
return options
}

Quester.defineAgent(['http'], createHttpProxyAgent)
Quester.defineAgent(['https'], createHttpsProxyAgent)
Quester.defineAgent(['socks', 'socks4', 'socks4a', 'socks5', 'socks5h'], createSocksProxyAgent)
Quester.defineAgent(['http'], HttpProxyAgent)
Quester.defineAgent(['https'], HttpsProxyAgent)
Quester.defineAgent(['socks', 'socks4', 'socks4a', 'socks5', 'socks5h'], SocksProxyAgent)
Loading