Skip to content

Commit

Permalink
feat(axios): support ctx.http.agent()
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 1, 2023
1 parent 3eb23f4 commit b85b574
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
23 changes: 23 additions & 0 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context } from 'cordis'
import { Agent } from 'agent-base'
import { base64ToArrayBuffer, Dict, pick, trimSlash } from 'cosmokit'
import { ClientRequestArgs } from 'http'
import mimedb from 'mime-db'
Expand All @@ -25,6 +26,8 @@ export interface Quester {
}

export class Quester {
agents: Dict<Agent> = Object.create(null)

constructor(ctx: Context, config: Context.Config) {
return Quester.create(config.request)
}
Expand All @@ -40,6 +43,17 @@ export class Quester {
})
}

agent(url: string, persist = true) {
if (!url) return
if (this.agents[url]) return this.agents[url]
const { protocol } = new URL(url)
const callback = Quester.proxies[protocol.slice(0, -1)]
if (this.agents[url]) return this.agents[url]
const agent = callback(url)
if (persist) this.agents[url] = agent
return agent
}

get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
return this('GET', url, config)
}
Expand Down Expand Up @@ -101,6 +115,15 @@ export namespace Quester {
export type Method = types.Method
export type AxiosResponse = types.AxiosResponse
export type AxiosRequestConfig = types.AxiosRequestConfig
export type CreateAgent = (opts: string) => Agent

export const proxies: Dict<CreateAgent> = Object.create(null)

export function defineAgent(protocols: string[], callback: CreateAgent) {
for (const protocol of protocols) {
proxies[protocol] = callback
}
}

export interface File {
mime?: string
Expand Down
34 changes: 7 additions & 27 deletions packages/satori/src/axios.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Quester, Schema } from '@satorijs/core'
import { base64ToArrayBuffer, defineProperty, Dict } from 'cosmokit'
import { base64ToArrayBuffer, defineProperty } from 'cosmokit'
import { ClientRequestArgs } from 'http'
import { Agent } from 'agent-base'
import { WebSocket } from 'ws'
import { fromBuffer } from 'file-type'
import { basename } from 'path'
Expand Down Expand Up @@ -30,7 +29,7 @@ Quester.prototype.file = async function file(this: Quester, url: string) {

Quester.prototype.ws = function ws(this: Quester, url: string, options: ClientRequestArgs = {}) {
return new WebSocket(url, {
agent: getAgent(this.config.proxyAgent),
agent: this.agent(this.config.proxyAgent),
handshakeTimeout: this.config.timeout,
...options,
headers: {
Expand All @@ -43,8 +42,8 @@ Quester.prototype.ws = function ws(this: Quester, url: string, options: ClientRe
const _prepare = Quester.prototype.prepare
Quester.prototype.prepare = function prepare(this: Quester) {
const options = _prepare.call(this)
options.httpAgent = getAgent(this.config.proxyAgent)
options.httpsAgent = getAgent(this.config.proxyAgent)
options.httpAgent = this.agent(this.config.proxyAgent)
options.httpsAgent = this.agent(this.config.proxyAgent)
return options
}

Expand All @@ -53,25 +52,6 @@ defineProperty(Quester, 'Config', Schema.object({
proxyAgent: Schema.string().description('使用的代理服务器地址。'),
}).description('请求设置'))

type CreateAgent = (opts: string) => Agent

const agents: Dict<Agent> = Object.create(null)
const proxies: Dict<CreateAgent> = Object.create(null)

export function register(protocols: string[], callback: CreateAgent) {
for (const protocol of protocols) {
proxies[protocol] = callback
}
}

register(['http'], createHttpProxyAgent)
register(['https'], createHttpsProxyAgent)
register(['socks', 'socks4', 'socks4a', 'socks5', 'socks5h'], createSocksProxyAgent)

export function getAgent(url: string) {
if (!url) return
if (agents[url]) return agents[url]
const { protocol } = new URL(url)
const callback = proxies[protocol.slice(0, -1)]
return agents[url] ||= callback(url)
}
Quester.defineAgent(['http'], createHttpProxyAgent)
Quester.defineAgent(['https'], createHttpsProxyAgent)
Quester.defineAgent(['socks', 'socks4', 'socks4a', 'socks5', 'socks5h'], createSocksProxyAgent)

0 comments on commit b85b574

Please sign in to comment.