mm_https 1.6.8 → 1.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +12 -2
  2. package/package.json +4 -4
  3. package/test.js +40 -0
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  const http = require('http');
7
7
  const https = require('https');
8
- const HttpProxyAgent = require('https-proxy-agent');
8
+ const { ProxyAgent } = require('proxy-agent');
9
9
  const fs = require("fs");
10
10
  const {
11
11
  createGunzip,
@@ -42,6 +42,16 @@ class Http {
42
42
  */
43
43
  this._boundary = "";
44
44
 
45
+ /**
46
+ * 代理agent缓存,复用连接
47
+ */
48
+ var proxy_url = this._config.proxy;
49
+ this._agent = proxy_url ? new ProxyAgent({
50
+ getProxyForUrl: function () {
51
+ return proxy_url;
52
+ }
53
+ }) : null;
54
+
45
55
  /**
46
56
  * headers协议头
47
57
  */
@@ -199,7 +209,7 @@ Http.prototype.httpOptions = function (options) {
199
209
  type,
200
210
  method: method || "GET",
201
211
  headers: http_headers,
202
- agent: cfg.proxy ? new HttpProxyAgent(cfg.proxy) : null,
212
+ agent: this._agent,
203
213
  rejectUnauthorized: cfg.rejectUnauthorized,
204
214
  timeout: options.timeout || cfg.timeout || 0,
205
215
  stream_callback: stream_callback,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_https",
3
- "version": "1.6.8",
3
+ "version": "1.6.9",
4
4
  "description": "超级美眉HTTP请求模块 - 支持自动Cookie管理、代理设置、文件下载等功能",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,11 +30,11 @@
30
30
  },
31
31
  "homepage": "https://gitee.com/qiuwenwu91/mm_https#readme",
32
32
  "engines": {
33
- "node": ">=12.0.0"
33
+ "node": ">=20.0.0"
34
34
  },
35
35
  "dependencies": {
36
- "https-proxy-agent": "^9.1.0",
37
36
  "iconv-lite": "^0.7.3",
38
- "mm_expand": "^2.5.1"
37
+ "mm_expand": "^2.5.1",
38
+ "proxy-agent": "^8.0.2"
39
39
  }
40
40
  }
package/test.js CHANGED
@@ -91,6 +91,34 @@ function startServer() {
91
91
  return server;
92
92
  }
93
93
 
94
+ /**
95
+ * 启动本地HTTP代理服务器(仅支持绝对URL转发,用于测试http代理)
96
+ * @returns {http.Server} 代理服务器实例
97
+ */
98
+ function startProxyServer() {
99
+ var proxy = http.createServer(function (req, res) {
100
+ proxy.forwarded = (proxy.forwarded || 0) + 1;
101
+ var url = new URL(req.url);
102
+ var options = {
103
+ hostname: url.hostname,
104
+ port: url.port || 80,
105
+ path: url.pathname + url.search,
106
+ method: req.method,
107
+ headers: Object.assign({}, req.headers, { host: url.host })
108
+ };
109
+ var p_req = http.request(options, function (p_res) {
110
+ res.writeHead(p_res.statusCode, p_res.headers);
111
+ p_res.pipe(res);
112
+ });
113
+ p_req.on('error', function () {
114
+ res.writeHead(502, { 'Content-Type': 'text/plain' });
115
+ res.end('proxy error');
116
+ });
117
+ req.pipe(p_req);
118
+ });
119
+ return proxy;
120
+ }
121
+
94
122
  /**
95
123
  * 主测试流程
96
124
  */
@@ -99,6 +127,9 @@ async function main() {
99
127
  var server = startServer();
100
128
  await new Promise(function (resolve) { server.listen(0, resolve); });
101
129
  server_port = server.address().port;
130
+ var proxy = startProxyServer();
131
+ await new Promise(function (resolve) { proxy.listen(0, resolve); });
132
+ var proxy_port = proxy.address().port;
102
133
  var client = new Http({ proxy: null });
103
134
  var base = 'http://127.0.0.1:' + server_port;
104
135
  var tmp_dir = path.join(__dirname, 'tmp_test');
@@ -198,11 +229,20 @@ async function main() {
198
229
  var reader2 = new Readable({ read: function () { this.push(def); this.push(null); } });
199
230
  out = await client.undata(reader2, null, 'utf8');
200
231
  assert('undata deflate解压', out === 'hello inflate');
232
+
233
+ // 12. 代理
234
+ console.log('\n[PROXY]');
235
+ var proxy_client = new Http({ proxy: 'http://127.0.0.1:' + proxy_port });
236
+ res = await proxy_client.get(base + '/get?page=1');
237
+ assert('代理GET返回200', res.status === 200);
238
+ assert('代理GET内容正确', res.body.indexOf('ok') !== -1);
239
+ assert('请求确实经过代理', proxy.forwarded > 0);
201
240
  } catch (e) {
202
241
  console.error('测试异常:', e);
203
242
  fail_count++;
204
243
  } finally {
205
244
  server.close();
245
+ proxy.close();
206
246
  fs.rmSync(tmp_dir, { recursive: true, force: true });
207
247
  console.log('\n== 测试结果: 通过 ' + pass_count + ', 失败 ' + fail_count + ' ==');
208
248
  if (fail_count > 0) {