From 5df1a6fcbb843b0deacb6e730004cbb330920bdc Mon Sep 17 00:00:00 2001 From: _LittleC_ <26459759+XxLittleCxX@users.noreply.github.com> Date: Sat, 3 Feb 2024 00:55:27 +0800 Subject: [PATCH] feat(axios): support keep alive (#235) --- packages/axios/package.json | 2 +- packages/axios/src/index.ts | 11 ++++++----- packages/core/src/index.ts | 1 + packages/satori/package.json | 6 +++--- packages/satori/src/index.ts | 12 ++++++------ 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/axios/package.json b/packages/axios/package.json index 2bdbb724..e4b81ff8 100644 --- a/packages/axios/package.json +++ b/packages/axios/package.json @@ -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", diff --git a/packages/axios/src/index.ts b/packages/axios/src/index.ts index 2c64a0a1..7d9f89e4 100644 --- a/packages/axios/src/index.ts +++ b/packages/axios/src/index.ts @@ -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' @@ -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 } @@ -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 = Object.create(null) export const proxies: Dict = Object.create(null) @@ -195,6 +195,7 @@ export namespace Quester { endpoint?: string timeout?: number proxyAgent?: string + keepAlive?: boolean } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 7b725a9d..aa09ce95 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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) { diff --git a/packages/satori/package.json b/packages/satori/package.json index b238ede3..af3061a8 100644 --- a/packages/satori/package.json +++ b/packages/satori/package.json @@ -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" } } diff --git a/packages/satori/src/index.ts b/packages/satori/src/index.ts index 15581b81..f98d0f4a 100644 --- a/packages/satori/src/index.ts +++ b/packages/satori/src/index.ts @@ -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' @@ -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)