dsh-klbq 0.1.0 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +13 -0
  2. package/lib/dakanu.js +68 -15
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -60,6 +60,19 @@
60
60
  用系统自带的 Edge / Chrome 无头截图(两趟:先 `--dump-dom` 量内容高度,再按高度截图),
61
61
  不引入 canvas / puppeteer 之类的重依赖。找不到浏览器时可用环境变量 `KLBQ_BROWSER` 指定路径。
62
62
 
63
+ ## 更新日志
64
+
65
+ ### 0.1.1 — 取数提速
66
+
67
+ dakanu 站点有两个 A 记录,快慢差 **5 倍**(实测搜索接口 5.37s vs 0.93s)。
68
+ 旧版按 DNS 顺序依次尝试,会稳定命中慢的那个,用户感知就是「查一次要八九秒」。
69
+
70
+ - **双 IP 并行对冲**:两个 IP 同时发请求,谁先成功用谁,耗时取决于较快者
71
+ - **连接复用**:一次查询的「搜索 + 详情」共用同一条 TLS 连接,省掉第二次握手
72
+ - **结果缓存**:搜索映射缓存 10 分钟、详情缓存 90 秒,重复查询不再走网络
73
+
74
+ 实测:搜索 3565ms → ~700ms,详情 5555ms → ~1000ms,重复查询整链 ~2s(原 ~9s)。
75
+
63
76
  ## 许可
64
77
 
65
78
  MIT
package/lib/dakanu.js CHANGED
@@ -22,6 +22,30 @@ const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
22
22
 
23
23
  let ipCache = { at: 0, ips: [] };
24
24
 
25
+ // 连接复用:一次查询要走「搜索 + 详情」两次请求,共用一条 TLS 连接能省掉
26
+ // 第二次 0.4~0.7 秒的握手开销。注意 servername 保持空字符串(见 httpsGetOnce 的说明)。
27
+ // 结果缓存:dakanu 后端不稳定(同一接口实测 0.6~25 秒都有),
28
+ // 同一个玩家短时间内重复查询没必要再走网络。搜索映射缓存 10 分钟(昵称极少变),
29
+ // 详情缓存 90 秒(战绩不会秒变)。
30
+ const resultCache = new Map();
31
+ const CACHE_TTL_SEARCH = 10 * 60 * 1000;
32
+ const CACHE_TTL_SUMMONER = 90 * 1000;
33
+ const CACHE_MAX_ENTRIES = 200;
34
+
35
+ function cached(key, ttlMs, loader) {
36
+ const hit = resultCache.get(key);
37
+ if (hit && Date.now() - hit.at < ttlMs) return Promise.resolve(hit.value);
38
+ return loader().then((value) => {
39
+ if (resultCache.size >= CACHE_MAX_ENTRIES) {
40
+ const oldest = resultCache.keys().next().value;
41
+ if (oldest !== undefined) resultCache.delete(oldest);
42
+ }
43
+ resultCache.set(key, { at: Date.now(), value });
44
+ return value;
45
+ });
46
+ }
47
+ const keepAliveAgent = new https.Agent({ keepAlive: true, maxSockets: 4, keepAliveMsecs: 30000 });
48
+
25
49
  /** 解析站点 IP:系统 DNS 没被拦(只有 SNI 被拦),失败退回内置列表 */
26
50
  async function resolveSiteIps() {
27
51
  const now = Date.now();
@@ -45,6 +69,7 @@ function httpsGetOnce(ip, pathname, { timeoutMs = 20000 } = {}) {
45
69
  method: 'GET',
46
70
  servername: '', // 关键:空字符串,不能是 undefined
47
71
  rejectUnauthorized: false, // 证书签给域名;这条通道只取公开数据
72
+ agent: keepAliveAgent, // 复用连接:省下每次 0.4~0.7 秒的 TLS 握手
48
73
  headers: {
49
74
  host: SITE_HOST,
50
75
  'user-agent': UA,
@@ -64,27 +89,55 @@ function httpsGetOnce(ip, pathname, { timeoutMs = 20000 } = {}) {
64
89
  });
65
90
  }
66
91
 
67
- /** 依次尝试各 IP,返回第一个非 5xx 响应 */
92
+ /**
93
+ * 并行对冲取数。
94
+ *
95
+ * 为什么不能「按 DNS 顺序依次尝试」:站点两个 A 记录**快慢差 5 倍**
96
+ * (实测 43.169.13.133 搜索接口 5.4s,43.169.14.133 只要 0.9s)。
97
+ * 顺序尝试会稳定选中那个慢的,用户看到的就是「查一次要八九秒」。
98
+ * 改成两个 IP 同时发、谁先成功用谁,慢 IP 只做备份,总耗时取决于快的那个。
99
+ */
68
100
  export async function dakanuGet(pathname, opts = {}) {
69
- let lastError = null;
70
- for (const ip of await resolveSiteIps()) {
71
- try {
72
- const res = await httpsGetOnce(ip, pathname, opts);
73
- if (res.status >= 500) { lastError = new Error(`HTTP ${res.status}`); continue; }
74
- return res;
75
- } catch (error) {
101
+ const ips = await resolveSiteIps();
102
+ if (ips.length === 1) return httpsGetOnce(ips[0], pathname, opts);
103
+
104
+ return new Promise((resolve, reject) => {
105
+ let settled = false;
106
+ let pending = ips.length;
107
+ let lastError = null;
108
+
109
+ const fail = (error) => {
76
110
  lastError = error;
111
+ pending -= 1;
112
+ if (pending === 0 && !settled) reject(lastError ?? new Error('dakanu 请求失败'));
113
+ };
114
+
115
+ for (const ip of ips) {
116
+ httpsGetOnce(ip, pathname, opts).then((res) => {
117
+ if (settled) return;
118
+ if (res.status >= 500) { fail(new Error(`HTTP ${res.status}`)); return; }
119
+ settled = true;
120
+ resolve(res);
121
+ }).catch((error) => {
122
+ if (settled) return;
123
+ fail(error);
124
+ });
77
125
  }
78
- }
79
- throw lastError ?? new Error('dakanu 请求失败');
126
+ });
80
127
  }
81
128
 
82
129
  async function dakanuJson(pathname) {
83
- const res = await dakanuGet(pathname);
84
- let json = null;
85
- try { json = JSON.parse(res.text); } catch { /* 下面统一报错 */ }
86
- if (!json) throw new Error(`返回非 JSON(HTTP ${res.status})`);
87
- return json;
130
+ // 缓存放在这一层最省事:搜索和详情都从这里出去,按 pathname 分档过期。
131
+ const ttl = pathname.startsWith('/api/search/players') ? CACHE_TTL_SEARCH
132
+ : pathname.startsWith('/api/summoner/') ? CACHE_TTL_SUMMONER
133
+ : 60 * 1000;
134
+ return cached(pathname, ttl, async () => {
135
+ const res = await dakanuGet(pathname);
136
+ let json = null;
137
+ try { json = JSON.parse(res.text); } catch { /* 下面统一报错 */ }
138
+ if (!json) throw new Error(`返回非 JSON(HTTP ${res.status})`);
139
+ return json;
140
+ });
88
141
  }
89
142
 
90
143
  /** 按昵称搜索玩家 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-klbq",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "卡拉彼丘(Strinova)战绩查询:按昵称从 dakanu.com 取数并渲染长图(排位段位/总览/模式/角色/最近对局),作为 DSH 工具 klbq_query 暴露给 agent",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",