@soga/baidu-ua 0.0.2 → 0.1.14

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/README.md ADDED
@@ -0,0 +1 @@
1
+ ### @soga/baidu-ua
package/dist/main.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare const getBaiduUA: (host_id: number, is_large_file: boolean) => Promise<{
2
+ ua: string;
3
+ finishBaiduUA: () => void;
4
+ }>;
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 ADDED
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.removeBaiduUA = exports.resetBaiduUA = exports.getBaiduUA = void 0;
4
+ const userAgents = {
5
+ app: [
6
+ 'netdisk;4.46.6;PC;PC-Mac',
7
+ 'netdisk;5.3.4;PC;PC-Windows',
8
+ 'netdisk;12.23.6;android-android',
9
+ 'netdisk;12.24.50;iphone16Pro;ios-iphone',
10
+ 'netdisk;12.24.100;iPadPro;ios-ipad',
11
+ 'pan.baidu.com',
12
+ ],
13
+ web: [
14
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
15
+ 'Mozilla/5.0 (Linux; Android 12; 2206122SC Build/SKQ1.220303.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/4343 MMWEBSDK/20221011 Mobile Safari/537.36 MMWEBID/5638 MicroMessenger/8.0.30.2260(0x28001E3B) WeChat/arm64 Weixin NetType/WIFI Language/en ABI/arm64 MiniProgramEnv/android',
16
+ ],
17
+ };
18
+ class BaiduUA {
19
+ ua_map = {};
20
+ constructor() {
21
+ this.resetUaMap(userAgents.app, userAgents.web);
22
+ }
23
+ async resetUaMap(app_ua_list, web_ua_list) {
24
+ this.ua_map = {};
25
+ for (const ua of app_ua_list) {
26
+ this.ua_map[ua] = {
27
+ ua,
28
+ is_web: false,
29
+ start_time: 0,
30
+ order_time: Date.now(),
31
+ };
32
+ }
33
+ for (const ua of web_ua_list) {
34
+ this.ua_map[ua] = {
35
+ ua,
36
+ is_web: true,
37
+ start_time: 0,
38
+ order_time: Date.now(),
39
+ };
40
+ }
41
+ }
42
+ async getBest(is_large_file) {
43
+ const list = is_large_file
44
+ ? Object.values(this.ua_map).filter((item) => !item.is_web)
45
+ : Object.values(this.ua_map);
46
+ const free_list = list.filter((item) => item.start_time == 0);
47
+ let item;
48
+ if (free_list.length > 0) {
49
+ const web_list = free_list.filter((item) => item.is_web);
50
+ if (web_list.length > 0) {
51
+ const sorted_list = web_list.sort((a, b) => a.order_time - b.order_time);
52
+ item = sorted_list[0];
53
+ }
54
+ else {
55
+ const sorted_list = free_list.sort((a, b) => a.order_time - b.order_time);
56
+ item = sorted_list[0];
57
+ }
58
+ }
59
+ 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
+ });
71
+ }
72
+ item.start_time = Date.now();
73
+ console.log('get best ua: ', item.ua.slice(0, 24));
74
+ return item;
75
+ }
76
+ finishUA(ua) {
77
+ if (!this.ua_map[ua])
78
+ return;
79
+ this.ua_map[ua].start_time = 0;
80
+ this.ua_map[ua].order_time = Date.now();
81
+ }
82
+ }
83
+ const instanceMap = new Map();
84
+ const getInstance = (host_id) => {
85
+ if (!instanceMap.has(host_id)) {
86
+ instanceMap.set(host_id, new BaiduUA());
87
+ }
88
+ return instanceMap.get(host_id);
89
+ };
90
+ const getBaiduUA = async (host_id, is_large_file) => {
91
+ const ins = getInstance(host_id);
92
+ const { ua } = await ins.getBest(is_large_file);
93
+ return {
94
+ ua,
95
+ finishBaiduUA: () => {
96
+ ins.finishUA(ua);
97
+ },
98
+ };
99
+ };
100
+ exports.getBaiduUA = getBaiduUA;
101
+ const resetBaiduUA = (app_ua_list, web_ua_list) => {
102
+ userAgents.app = app_ua_list;
103
+ userAgents.web = web_ua_list;
104
+ instanceMap.forEach((ins) => {
105
+ ins.resetUaMap(app_ua_list, web_ua_list);
106
+ });
107
+ };
108
+ exports.resetBaiduUA = resetBaiduUA;
109
+ const removeBaiduUA = (host_id) => {
110
+ instanceMap.delete(host_id);
111
+ };
112
+ exports.removeBaiduUA = removeBaiduUA;
package/package.json CHANGED
@@ -1,15 +1,35 @@
1
1
  {
2
2
  "name": "@soga/baidu-ua",
3
- "version": "0.0.2",
4
- "main": "index.js",
5
- "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
7
- },
3
+ "version": "0.1.14",
8
4
  "publishConfig": {
9
5
  "access": "public"
10
6
  },
7
+ "description": "",
8
+ "main": "dist/main.js",
9
+ "types": "dist/main.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "build": "rimraf dist && tsc",
15
+ "dev": "tsc --watch",
16
+ "lint": "eslint . --ext .ts",
17
+ "prepublishOnly": "npm run build"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^22.5.1",
21
+ "@typescript-eslint/eslint-plugin": "^8.3.0",
22
+ "@typescript-eslint/parser": "^8.3.0",
23
+ "eslint": "^9.9.1",
24
+ "eslint-config-prettier": "^9.1.0",
25
+ "eslint-plugin-prettier": "^5.2.1",
26
+ "prettier": "^3.3.3",
27
+ "rimraf": "^6.0.1",
28
+ "ts-node": "^10.9.2",
29
+ "typescript": "^5.5.4"
30
+ },
11
31
  "keywords": [],
12
32
  "author": "",
13
33
  "license": "ISC",
14
- "description": ""
34
+ "gitHead": "99f8fe90e0f34b6a645e6b2c6682071f74c9a0dc"
15
35
  }