@soga/baidu-ua 0.1.16 → 0.2.1

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/dist/main.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- export declare const getBaiduUA: (host_id: number, is_large_file: boolean) => Promise<{
1
+ export declare const getBaiduUA: (host_id: number, file_size?: number) => Promise<{
2
2
  ua: string;
3
3
  finishBaiduUA: () => void;
4
4
  }>;
5
5
  export declare const resetBaiduUA: (app_ua_list: string[], web_ua_list: string[]) => void;
6
- export declare const removeBaiduUA: (host_id: number) => void;
package/dist/main.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeBaiduUA = exports.resetBaiduUA = exports.getBaiduUA = void 0;
3
+ exports.resetBaiduUA = exports.getBaiduUA = void 0;
4
4
  const userAgents = {
5
5
  app: [
6
6
  'netdisk;4.46.6;PC;PC-Mac',
@@ -27,7 +27,7 @@ class BaiduUA {
27
27
  ua,
28
28
  is_web: false,
29
29
  start_time: 0,
30
- order_time: Date.now(),
30
+ finish_time: Date.now(),
31
31
  };
32
32
  }
33
33
  for (const ua of web_ua_list) {
@@ -35,7 +35,7 @@ class BaiduUA {
35
35
  ua,
36
36
  is_web: true,
37
37
  start_time: 0,
38
- order_time: Date.now(),
38
+ finish_time: Date.now(),
39
39
  };
40
40
  }
41
41
  }
@@ -53,29 +53,32 @@ class BaiduUA {
53
53
  if (free_list.length > 0) {
54
54
  const web_list = free_list.filter((item) => item.is_web);
55
55
  if (web_list.length > 0) {
56
- const sorted_list = web_list.sort((a, b) => a.order_time - b.order_time);
56
+ const sorted_list = web_list.sort((a, b) => a.finish_time - b.finish_time);
57
57
  item = sorted_list[0];
58
58
  }
59
59
  else {
60
- const sorted_list = free_list.sort((a, b) => a.order_time - b.order_time);
60
+ const sorted_list = free_list.sort((a, b) => a.finish_time - b.finish_time);
61
61
  item = sorted_list[0];
62
62
  }
63
63
  }
64
64
  else {
65
- const sorted_list = list.sort((a, b) => a.order_time - b.order_time);
66
- item = sorted_list[0];
67
- await new Promise((resolve) => {
68
- let times = 0;
69
- const interval = setInterval(() => {
70
- if (item.start_time == 0 || times++ > 80) {
71
- clearInterval(interval);
72
- resolve(null);
73
- }
74
- }, 100);
75
- });
65
+ let times = 0;
66
+ while (list.filter((item) => item.start_time == 0).length < 1 &&
67
+ times++ < 50) {
68
+ await new Promise((resolve) => setTimeout(resolve, 400));
69
+ }
70
+ const free_list = list
71
+ .filter((item) => item.start_time == 0)
72
+ .sort((a, b) => a.finish_time - b.finish_time);
73
+ if (free_list.length > 0) {
74
+ item = free_list[0];
75
+ }
76
+ else {
77
+ const sorted_list = list.sort((a, b) => a.finish_time - b.finish_time);
78
+ item = sorted_list[0];
79
+ }
76
80
  }
77
81
  item.start_time = Date.now();
78
- console.log('get best ua : ', item.ua.slice(0, 24));
79
82
  this.getting = false;
80
83
  return item;
81
84
  }
@@ -83,7 +86,7 @@ class BaiduUA {
83
86
  if (!this.ua_map[ua])
84
87
  return;
85
88
  this.ua_map[ua].start_time = 0;
86
- this.ua_map[ua].order_time = Date.now();
89
+ this.ua_map[ua].finish_time = Date.now();
87
90
  }
88
91
  }
89
92
  const instanceMap = new Map();
@@ -93,8 +96,9 @@ const getInstance = (host_id) => {
93
96
  }
94
97
  return instanceMap.get(host_id);
95
98
  };
96
- const getBaiduUA = async (host_id, is_large_file) => {
99
+ const getBaiduUA = async (host_id, file_size = Infinity) => {
97
100
  const ins = getInstance(host_id);
101
+ const is_large_file = file_size >= 50 * 1024 * 1024;
98
102
  const { ua } = await ins.getBest(is_large_file);
99
103
  return {
100
104
  ua,
@@ -112,7 +116,3 @@ const resetBaiduUA = (app_ua_list, web_ua_list) => {
112
116
  });
113
117
  };
114
118
  exports.resetBaiduUA = resetBaiduUA;
115
- const removeBaiduUA = (host_id) => {
116
- instanceMap.delete(host_id);
117
- };
118
- exports.removeBaiduUA = removeBaiduUA;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soga/baidu-ua",
3
- "version": "0.1.16",
3
+ "version": "0.2.1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -31,5 +31,5 @@
31
31
  "keywords": [],
32
32
  "author": "",
33
33
  "license": "ISC",
34
- "gitHead": "1f22188c2926f8657c17bafeac7fa6bbcd66a599"
34
+ "gitHead": "92e93cbd89d863f10407c93b88db5ec5029ed426"
35
35
  }