@soga/baidu-ua 0.1.14 → 0.1.23

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,11 +35,16 @@ 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
  }
42
+ getting = false;
42
43
  async getBest(is_large_file) {
44
+ while (this.getting) {
45
+ await new Promise((resolve) => setTimeout(resolve, 100));
46
+ }
47
+ this.getting = true;
43
48
  const list = is_large_file
44
49
  ? Object.values(this.ua_map).filter((item) => !item.is_web)
45
50
  : Object.values(this.ua_map);
@@ -48,36 +53,40 @@ class BaiduUA {
48
53
  if (free_list.length > 0) {
49
54
  const web_list = free_list.filter((item) => item.is_web);
50
55
  if (web_list.length > 0) {
51
- 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);
52
57
  item = sorted_list[0];
53
58
  }
54
59
  else {
55
- 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);
56
61
  item = sorted_list[0];
57
62
  }
58
63
  }
59
64
  else {
60
- const sorted_list = list.sort((a, b) => a.order_time - b.order_time);
61
- item = sorted_list[0];
62
- await new Promise((resolve) => {
63
- let times = 0;
64
- const interval = setInterval(() => {
65
- if (item.start_time == 0 || times++ > 80) {
66
- clearInterval(interval);
67
- resolve(null);
68
- }
69
- }, 100);
70
- });
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
+ }
71
80
  }
72
81
  item.start_time = Date.now();
73
- console.log('get best ua: ', item.ua.slice(0, 24));
82
+ this.getting = false;
74
83
  return item;
75
84
  }
76
85
  finishUA(ua) {
77
86
  if (!this.ua_map[ua])
78
87
  return;
79
88
  this.ua_map[ua].start_time = 0;
80
- this.ua_map[ua].order_time = Date.now();
89
+ this.ua_map[ua].finish_time = Date.now();
81
90
  }
82
91
  }
83
92
  const instanceMap = new Map();
@@ -87,8 +96,9 @@ const getInstance = (host_id) => {
87
96
  }
88
97
  return instanceMap.get(host_id);
89
98
  };
90
- const getBaiduUA = async (host_id, is_large_file) => {
99
+ const getBaiduUA = async (host_id, file_size = Infinity) => {
91
100
  const ins = getInstance(host_id);
101
+ const is_large_file = file_size >= 50 * 1024 * 1024;
92
102
  const { ua } = await ins.getBest(is_large_file);
93
103
  return {
94
104
  ua,
@@ -106,7 +116,3 @@ const resetBaiduUA = (app_ua_list, web_ua_list) => {
106
116
  });
107
117
  };
108
118
  exports.resetBaiduUA = resetBaiduUA;
109
- const removeBaiduUA = (host_id) => {
110
- instanceMap.delete(host_id);
111
- };
112
- exports.removeBaiduUA = removeBaiduUA;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soga/baidu-ua",
3
- "version": "0.1.14",
3
+ "version": "0.1.23",
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": "99f8fe90e0f34b6a645e6b2c6682071f74c9a0dc"
34
+ "gitHead": "4a8d47620964c62bbbcdb10ed7cc2b177c4a10e6"
35
35
  }