提交 ab37ff32 编写于 作者: YXL76's avatar YXL76

use `axios` to replace `request`

上级 123e1375
此差异已折叠。
...@@ -44,8 +44,7 @@ ...@@ -44,8 +44,7 @@
"axios": "^0.20.0", "axios": "^0.20.0",
"express": "^4.17.1", "express": "^4.17.1",
"express-fileupload": "^1.1.9", "express-fileupload": "^1.1.9",
"pac-proxy-agent": "^4.0.0", "pac-proxy-agent": "^4.0.0"
"request": "^2.88.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "14.11.2", "@types/node": "14.11.2",
......
const encrypt = require('./crypto') const encrypt = require('./crypto')
const request = require('request') const axios = require('axios')
const queryString = require('querystring') const queryString = require('querystring')
const PacProxyAgent = require('pac-proxy-agent') const PacProxyAgent = require('pac-proxy-agent')
const zlib = require('zlib') const zlib = require('zlib')
const http = require('http')
const https = require('https')
// request.debug = true // 开启可看到更详细信息 // request.debug = true // 开启可看到更详细信息
...@@ -114,23 +116,23 @@ const createRequest = (method, url, data, options) => { ...@@ -114,23 +116,23 @@ const createRequest = (method, url, data, options) => {
method: method, method: method,
url: url, url: url,
headers: headers, headers: headers,
body: queryString.stringify(data), data: queryString.stringify(data),
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
} }
if (options.crypto === 'eapi') settings.encoding = null if (options.crypto === 'eapi') settings.encoding = null
if (/\.pac$/i.test(options.proxy)) { if (/\.pac$/i.test(options.proxy)) {
settings.agent = new PacProxyAgent(options.proxy) settings.httpAgent = new PacProxyAgent(options.proxy)
settings.httpsAgent = new PacProxyAgent(options.proxy)
} else { } else {
settings.proxy = options.proxy settings.proxy = options.proxy
} }
request(settings, (err, res, body) => { axios(settings)
if (err) { .then((res) => {
answer.status = 502 const body = res.data
answer.body = { code: 502, msg: err.stack }
reject(answer)
} else {
answer.cookie = (res.headers['set-cookie'] || []).map((x) => answer.cookie = (res.headers['set-cookie'] || []).map((x) =>
x.replace(/\s*Domain=[^(;|$)]+;*/, ''), x.replace(/\s*Domain=[^(;|$)]+;*/, ''),
) )
...@@ -172,8 +174,12 @@ const createRequest = (method, url, data, options) => { ...@@ -172,8 +174,12 @@ const createRequest = (method, url, data, options) => {
100 < answer.status && answer.status < 600 ? answer.status : 400 100 < answer.status && answer.status < 600 ? answer.status : 400
if (answer.status == 200) resolve(answer) if (answer.status == 200) resolve(answer)
else reject(answer) else reject(answer)
} })
}) .catch((err) => {
answer.status = 502
answer.body = { code: 502, msg: err }
reject(answer)
})
}) })
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册