@soga/baidu-ua 0.3.0 → 1.0.0

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.
@@ -0,0 +1,29 @@
1
+ declare const defaultUserAgents: {
2
+ app: string[];
3
+ web: string[];
4
+ };
5
+ declare class BaiduUA {
6
+ private ua_map;
7
+ constructor();
8
+ resetUaMap(app_ua_list: string[], web_ua_list: string[]): Promise<void>;
9
+ private getting;
10
+ getBest(is_large_file: boolean): Promise<UAItem>;
11
+ finishUA(ua: string): void;
12
+ }
13
+ declare const getDownloadBaiduUA: (host_id: number, file_size?: number) => Promise<{
14
+ ua: string;
15
+ finish: () => void;
16
+ }>;
17
+ declare const getUploadBaiduUA: (host_id: number) => Promise<{
18
+ ua: string;
19
+ finish: () => void;
20
+ }>;
21
+ declare const resetBaiduUA: (app_ua_list: string[], web_ua_list: string[]) => void;
22
+ type UAItem = {
23
+ ua: string;
24
+ is_web: boolean;
25
+ start_time: number;
26
+ finish_time: number;
27
+ };
28
+
29
+ export { BaiduUA, type UAItem, defaultUserAgents, getDownloadBaiduUA, getUploadBaiduUA, resetBaiduUA };
@@ -0,0 +1,29 @@
1
+ declare const defaultUserAgents: {
2
+ app: string[];
3
+ web: string[];
4
+ };
5
+ declare class BaiduUA {
6
+ private ua_map;
7
+ constructor();
8
+ resetUaMap(app_ua_list: string[], web_ua_list: string[]): Promise<void>;
9
+ private getting;
10
+ getBest(is_large_file: boolean): Promise<UAItem>;
11
+ finishUA(ua: string): void;
12
+ }
13
+ declare const getDownloadBaiduUA: (host_id: number, file_size?: number) => Promise<{
14
+ ua: string;
15
+ finish: () => void;
16
+ }>;
17
+ declare const getUploadBaiduUA: (host_id: number) => Promise<{
18
+ ua: string;
19
+ finish: () => void;
20
+ }>;
21
+ declare const resetBaiduUA: (app_ua_list: string[], web_ua_list: string[]) => void;
22
+ type UAItem = {
23
+ ua: string;
24
+ is_web: boolean;
25
+ start_time: number;
26
+ finish_time: number;
27
+ };
28
+
29
+ export { BaiduUA, type UAItem, defaultUserAgents, getDownloadBaiduUA, getUploadBaiduUA, resetBaiduUA };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var e,t=Object.defineProperty,i=Object.getOwnPropertyDescriptor,a=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,n={};((e,i)=>{for(var a in i)t(e,a,{get:i[a],enumerable:!0})})(n,{BaiduUA:()=>o,defaultUserAgents:()=>r,getDownloadBaiduUA:()=>l,getUploadBaiduUA:()=>m,resetBaiduUA:()=>h}),module.exports=(e=n,((e,n,r,o)=>{if(n&&"object"==typeof n||"function"==typeof n)for(let f of a(n))s.call(e,f)||f===r||t(e,f,{get:()=>n[f],enumerable:!(o=i(n,f))||o.enumerable});return e})(t({},"__esModule",{value:!0}),e));var r={app:["netdisk;4.46.6;PC;PC-Mac","netdisk;5.3.4;PC;PC-Windows","netdisk;12.23.6;android-android","netdisk;12.24.50;iphone16Pro;ios-iphone","netdisk;12.24.100;iPadPro;ios-ipad","pan.baidu.com"],web:["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","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"]},o=class{ua_map={};constructor(){this.resetUaMap(r.app,r.web)}async resetUaMap(e,t){this.ua_map={};for(const t of e)this.ua_map[t]={ua:t,is_web:!1,start_time:0,finish_time:Date.now()};for(const e of t)this.ua_map[e]={ua:e,is_web:!0,start_time:0,finish_time:Date.now()}}getting=!1;async getBest(e){for(;this.getting;)await new Promise(e=>setTimeout(e,50));this.getting=!0;const t=e?Object.values(this.ua_map).filter(e=>!e.is_web):Object.values(this.ua_map),i=t.filter(e=>0==e.start_time);let a;if(i.length>0){const e=i.filter(e=>e.is_web);if(e.length>0){a=e.sort((e,t)=>e.finish_time-t.finish_time)[0]}else{a=i.sort((e,t)=>e.finish_time-t.finish_time)[0]}}else{let e=0;for(;t.filter(e=>0==e.start_time).length<1&&e++<50;)await new Promise(e=>setTimeout(e,400));const i=t.filter(e=>0==e.start_time).sort((e,t)=>e.finish_time-t.finish_time);if(i.length>0)a=i[0];else{a=t.sort((e,t)=>e.finish_time-t.finish_time)[0]}}return a.start_time=Date.now(),this.getting=!1,a}finishUA(e){this.ua_map[e]&&(this.ua_map[e].start_time=0,this.ua_map[e].finish_time=Date.now())}},f=new Map,u=(e,t)=>{const i=`${t}_${e}`;if(!f.has(i)){const e=new o;return f.set(i,e),e}return f.get(i)},l=async(e,t=1/0)=>{const i=u(e,"download"),a=t>=52428800,{ua:s}=await i.getBest(a);return{ua:s,finish:()=>{i.finishUA(s)}}},m=async e=>{const t=u(e,"upload"),{ua:i}=await t.getBest(!1);return{ua:i,finish:()=>{t.finishUA(i)}}},h=(e,t)=>{r.app=e,r.web=t,f.forEach(i=>{i.resetUaMap(e,t)})};
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ var i={app:["netdisk;4.46.6;PC;PC-Mac","netdisk;5.3.4;PC;PC-Windows","netdisk;12.23.6;android-android","netdisk;12.24.50;iphone16Pro;ios-iphone","netdisk;12.24.100;iPadPro;ios-ipad","pan.baidu.com"],web:["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","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"]},t=class{ua_map={};constructor(){this.resetUaMap(i.app,i.web)}async resetUaMap(i,t){this.ua_map={};for(const t of i)this.ua_map[t]={ua:t,is_web:!1,start_time:0,finish_time:Date.now()};for(const i of t)this.ua_map[i]={ua:i,is_web:!0,start_time:0,finish_time:Date.now()}}getting=!1;async getBest(i){for(;this.getting;)await new Promise(i=>setTimeout(i,50));this.getting=!0;const t=i?Object.values(this.ua_map).filter(i=>!i.is_web):Object.values(this.ua_map),e=t.filter(i=>0==i.start_time);let s;if(e.length>0){const i=e.filter(i=>i.is_web);if(i.length>0){s=i.sort((i,t)=>i.finish_time-t.finish_time)[0]}else{s=e.sort((i,t)=>i.finish_time-t.finish_time)[0]}}else{let i=0;for(;t.filter(i=>0==i.start_time).length<1&&i++<50;)await new Promise(i=>setTimeout(i,400));const e=t.filter(i=>0==i.start_time).sort((i,t)=>i.finish_time-t.finish_time);if(e.length>0)s=e[0];else{s=t.sort((i,t)=>i.finish_time-t.finish_time)[0]}}return s.start_time=Date.now(),this.getting=!1,s}finishUA(i){this.ua_map[i]&&(this.ua_map[i].start_time=0,this.ua_map[i].finish_time=Date.now())}},e=new Map,s=(i,s)=>{const a=`${s}_${i}`;if(!e.has(a)){const i=new t;return e.set(a,i),i}return e.get(a)},a=async(i,t=1/0)=>{const e=s(i,"download"),a=t>=52428800,{ua:n}=await e.getBest(a);return{ua:n,finish:()=>{e.finishUA(n)}}},n=async i=>{const t=s(i,"upload"),{ua:e}=await t.getBest(!1);return{ua:e,finish:()=>{t.finishUA(e)}}},o=(t,s)=>{i.app=t,i.web=s,e.forEach(i=>{i.resetUaMap(t,s)})};export{t as BaiduUA,i as defaultUserAgents,a as getDownloadBaiduUA,n as getUploadBaiduUA,o as resetBaiduUA};
package/package.json CHANGED
@@ -1,21 +1,28 @@
1
1
  {
2
2
  "name": "@soga/baidu-ua",
3
- "version": "0.3.0",
3
+ "version": "1.0.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
7
  "description": "",
8
- "main": "dist/main.js",
9
- "types": "dist/main.d.ts",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts",
10
11
  "files": [
11
12
  "dist"
12
13
  ],
14
+ "scripts": {
15
+ "build": "rimraf dist && tsup src/index.ts --format cjs,esm --dts --minify terser",
16
+ "prepublishOnly": "npm run build"
17
+ },
13
18
  "keywords": [],
14
19
  "author": "",
15
20
  "license": "ISC",
16
- "scripts": {
17
- "build": "rimraf dist && tsc",
18
- "dev": "tsc --watch",
19
- "lint": "eslint . --ext .ts"
21
+ "devDependencies": {
22
+ "@soga/typescript-config": "latest",
23
+ "rimraf": "^6.0.1",
24
+ "terser": "^5.43.1",
25
+ "tsup": "^8.5.0",
26
+ "typescript": "^5.8.3"
20
27
  }
21
- }
28
+ }
package/README.md DELETED
@@ -1 +0,0 @@
1
- ### @soga/baidu-ua
package/dist/main.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export declare const getBaiduUA: (host_id: number, file_size?: number) => Promise<{
2
- ua: string;
3
- finishBaiduUA: () => void;
4
- }>;
5
- export declare const resetBaiduUA: (app_ua_list: string[], web_ua_list: string[]) => void;
package/dist/main.js DELETED
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- 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
- finish_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
- finish_time: Date.now(),
39
- };
40
- }
41
- }
42
- getting = false;
43
- async getBest(is_large_file) {
44
- while (this.getting) {
45
- await new Promise((resolve) => setTimeout(resolve, 100));
46
- }
47
- this.getting = true;
48
- const list = is_large_file
49
- ? Object.values(this.ua_map).filter((item) => !item.is_web)
50
- : Object.values(this.ua_map);
51
- const free_list = list.filter((item) => item.start_time == 0);
52
- let item;
53
- if (free_list.length > 0) {
54
- const web_list = free_list.filter((item) => item.is_web);
55
- if (web_list.length > 0) {
56
- const sorted_list = web_list.sort((a, b) => a.finish_time - b.finish_time);
57
- item = sorted_list[0];
58
- }
59
- else {
60
- const sorted_list = free_list.sort((a, b) => a.finish_time - b.finish_time);
61
- item = sorted_list[0];
62
- }
63
- }
64
- else {
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
- }
80
- }
81
- item.start_time = Date.now();
82
- this.getting = false;
83
- return item;
84
- }
85
- finishUA(ua) {
86
- if (!this.ua_map[ua])
87
- return;
88
- this.ua_map[ua].start_time = 0;
89
- this.ua_map[ua].finish_time = Date.now();
90
- }
91
- }
92
- const instanceMap = new Map();
93
- const getInstance = (host_id) => {
94
- if (!instanceMap.has(host_id)) {
95
- instanceMap.set(host_id, new BaiduUA());
96
- }
97
- return instanceMap.get(host_id);
98
- };
99
- const getBaiduUA = async (host_id, file_size = Infinity) => {
100
- const ins = getInstance(host_id);
101
- const is_large_file = file_size >= 50 * 1024 * 1024;
102
- const { ua } = await ins.getBest(is_large_file);
103
- return {
104
- ua,
105
- finishBaiduUA: () => {
106
- ins.finishUA(ua);
107
- },
108
- };
109
- };
110
- exports.getBaiduUA = getBaiduUA;
111
- const resetBaiduUA = (app_ua_list, web_ua_list) => {
112
- userAgents.app = app_ua_list;
113
- userAgents.web = web_ua_list;
114
- instanceMap.forEach((ins) => {
115
- ins.resetUaMap(app_ua_list, web_ua_list);
116
- });
117
- };
118
- exports.resetBaiduUA = resetBaiduUA;