mm_https 1.5.6 → 1.5.8

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.
package/index.js CHANGED
@@ -23,24 +23,7 @@ const {
23
23
  class Http {
24
24
  constructor(config) {
25
25
  this.config = Object.assign({
26
- headers: {
27
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
28
- // 'accept-encoding': 'gzip, deflate, br',
29
- 'Accept-Encoding': 'gzip, deflate',
30
- 'Accept-Language': 'zh-CN,zh;q=0.9',
31
- 'Cache-Control': 'no-cache',
32
- 'Connection': 'keep-alive',
33
- // 'Pragma': 'no-cache',
34
- // 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
35
- // 'sec-ch-ua-mobile': '?0',
36
- // 'sec-ch-ua-platform': '"Windows"',
37
- // 'sec-fetch-dest': 'document',
38
- // 'sec-fetch-mode': 'navigate',
39
- // 'sec-fetch-site': 'none',
40
- // 'sec-fetch-user': '?1',
41
- // 'Upgrade-Insecure-Requests': '1',
42
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
43
- },
26
+ headers: "",
44
27
  rejectUnauthorized: false,
45
28
  cookie: "",
46
29
  proxy: null
@@ -48,10 +31,29 @@ class Http {
48
31
  }, config);
49
32
 
50
33
  /**
51
- * cookie缓存
34
+ * cookies 缓存
52
35
  */
53
- this.cookies = {
36
+ this.cookies = {};
54
37
 
38
+ /**
39
+ * headers 协议头
40
+ */
41
+ this.headers = {
42
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
43
+ 'Accept-Encoding': 'gzip, deflate',
44
+ 'Accept-Language': 'zh-CN,zh;q=0.9',
45
+ 'Cache-Control': 'no-cache',
46
+ 'Connection': 'keep-alive',
47
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
48
+ };
49
+
50
+ if (this.config.cookie) {
51
+ this.set_cookie(this.config.cookie);
52
+ }
53
+
54
+ if (this.config.headers) {
55
+ var headers = this.change_headers(config.headers);
56
+ Object.assign(this.headers, headers);
55
57
  }
56
58
  }
57
59
  }
@@ -79,13 +81,40 @@ Http.prototype.get_cookie = function() {
79
81
  return cookie
80
82
  }
81
83
 
84
+ /**
85
+ * 转换协议头
86
+ * @param {Object|String} headers 协议头
87
+ */
88
+ Http.prototype.change_headers = function(headers) {
89
+ if (headers) {
90
+ if (typeof(headers) == "string") {
91
+ if (headers.indexOf("{") === 0) {
92
+ return headers.toJson();
93
+ } else {
94
+ var dict = {};
95
+ var lines = headers.split('\n');
96
+ for (var i = 0; i < lines.length; i++) {
97
+ var line = lines[i].trim();
98
+ if (line) {
99
+ dict[line.left(":").trim()] = line.right(":").trim();
100
+ }
101
+ }
102
+ return dict;
103
+ }
104
+ } else {
105
+ return headers;
106
+ }
107
+ }
108
+ return {}
109
+ }
110
+
82
111
  Http.prototype.options = function(options) {
83
112
  return {
84
113
  href: "",
85
114
  method: "GET",
86
115
  body: {},
87
116
  proxy: "http://127.0.0.1:10809",
88
- headers: Object.assign({}, this.config.headers)
117
+ headers: Object.assign({}, this.headers)
89
118
  }
90
119
  }
91
120
 
@@ -109,7 +138,7 @@ Http.prototype.http_options = function(options) {
109
138
  body,
110
139
  type,
111
140
  method: method || "GET",
112
- headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
141
+ headers: headers ? Object.assign({}, this.headers, headers) : Object.assign({}, this.headers),
113
142
  agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
114
143
  cookie: this.get_cookie()
115
144
  }, u);
@@ -272,9 +301,11 @@ Http.prototype.request = function(options) {
272
301
  var {
273
302
  headers
274
303
  } = res;
304
+
275
305
  if (res.statusCode == 200) {
276
306
  var chunks = [];
277
307
  var encoding = headers['content-encoding'];
308
+
278
309
  res.on('data', function(d) {
279
310
  chunks.push(d);
280
311
  }).on('end', function(de) {
@@ -321,6 +352,7 @@ Http.prototype.request = function(options) {
321
352
  });
322
353
 
323
354
  req.on("error", function(err) {
355
+ console.log("错误", err);
324
356
  resolve({
325
357
  status: 0,
326
358
  message: err.message
@@ -338,7 +370,7 @@ Http.prototype.request = function(options) {
338
370
  * @description GET请求
339
371
  * @param {String} url 请求地址
340
372
  * @param {Object} headers 请求头
341
- * @param {Object} cookie 服务缓存
373
+ * @param {String} cookie 服务缓存
342
374
  * @return {Object} 响应的结果
343
375
  */
344
376
  Http.prototype.get = async function(url, headers, cookie) {
@@ -356,8 +388,8 @@ Http.prototype.get = async function(url, headers, cookie) {
356
388
  * @param {String} url 请求地址
357
389
  * @param {Object} body 提交参
358
390
  * @param {Object} headers 请求头
359
- * @param {Number} type 传参方式 json, form, text
360
- * @param {Object} cookie 服务缓存
391
+ * @param {String} type 传参方式 json, form, text
392
+ * @param {String} cookie 服务缓存
361
393
  * @return {Object} 响应的结果
362
394
  */
363
395
  Http.prototype.post = async function(url, body, headers, type, cookie) {
@@ -377,7 +409,7 @@ Http.prototype.post = async function(url, body, headers, type, cookie) {
377
409
  * @param {String} filename 下载地址
378
410
  * @param {String} auto 自动类型
379
411
  * @param {Object} headers 协议头
380
- * @param {Object} cookie COOKIE
412
+ * @param {String} cookie 服务缓存
381
413
  * @return {String} 成功返回保存路径,失败返回null
382
414
  */
383
415
  Http.prototype.download = async function(url, filename, auto, headers, cookie) {
@@ -428,4 +460,4 @@ if (global.$) {
428
460
  /**
429
461
  * @description 导入构造函数
430
462
  */
431
- module.exports = Http;
463
+ module.exports = Http;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_https",
3
- "version": "1.5.6",
3
+ "version": "1.5.8",
4
4
  "description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,7 +32,7 @@
32
32
  "homepage": "https://github.com/qiuwenwu/mm_https#readme",
33
33
  "dependencies": {
34
34
  "https-proxy-agent": "^5.0.1",
35
- "iconv-lite": "^0.5.0",
36
- "mm_expand": "^1.4.7"
35
+ "iconv-lite": "^0.5.2",
36
+ "mm_expand": "^1.5.6"
37
37
  }
38
38
  }
package/test.js CHANGED
@@ -141,7 +141,14 @@ var fs = require("fs");
141
141
 
142
142
 
143
143
  async function download(){
144
- var hp = new Http();
144
+ var hp = new Http({
145
+ headers: `
146
+ Referer: https://www.aieliantong.com/admin/index
147
+ X-Auth-Token: xxx
148
+ `,
149
+ cookie: "Hm_lvt_c73f94a74e41202e67d251e6e3f591cb=1697871513; Hm_lpvt_c73f94a74e41202e67d251e6e3f591cb=1698886851; mm:uuid=3QSuThREzVFdRW6/YrT0o0WpriBhXhMdnLmhtJOXwBg="
150
+ });
151
+ console.log(hp.headers, hp.cookies);
145
152
  var url = "https://www.copipi.com/p/224/224/becce1fc21d723614b598f5377d5d676.jpg";
146
153
  "./二维码/中文".addDir();
147
154
  var filename = "./二维码/qrcode.jpg".fullname();