dsh-wakatime 1.2.1 → 1.2.3
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.en.md +16 -3
- package/README.md +17 -4
- package/lib/user-agent.d.ts +26 -20
- package/lib/user-agent.js +46 -34
- package/lib/wakatime.d.ts +9 -0
- package/lib/wakatime.js +37 -0
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
[中文](./README.md) | **English**
|
|
8
8
|
|
|
9
|
-
[](https://github.com/dsh-plugin-collection/dsh-wakatime)
|
|
10
10
|
[](LICENSE)
|
|
11
11
|
[](https://github.com/deepseek-ai/deepseek-harness)
|
|
12
12
|
|
|
@@ -42,8 +42,21 @@ All four panels are fed by this plugin:
|
|
|
42
42
|
| **Editors** | Derived from the heartbeat `User-Agent`, so "working in DSH" shows up as its own editor |
|
|
43
43
|
| **Operating Systems** | Also from the `User-Agent` parenthesised section `(linux-<kernel>-<arch>)` |
|
|
44
44
|
| **Models** | Also from the model token inside `User-Agent` (e.g. `deepseek/4.1-flash-eco`) — WakaTime has **no** `ai_model` heartbeat field; this panel reads the UA only |
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
| **Machines** | The `X-Machine-Name` HTTP header (URL-encoded value). The heartbeat JSON has **no** `machine_name` field — this is the same channel the official CLI uses |
|
|
46
|
+
|
|
47
|
+
> **Why the model token comes first (instead of copying the official template)**
|
|
48
|
+
>
|
|
49
|
+
> The official `wakatime-cli` template is `wakatime/<ver> (<OS>-<core>-<platform>) <runtime> <plugin>`, and this plugin copied it at first. But the `user_agents` endpoint returns **the server's parse result for each UA** (`ai_model` / `ai_model_version` / `editor`), and after testing a dozen variants the finding is: **when a UA starts with `wakatime/<ver>`, the server treats wakatime itself as the model** — `ai_model='Wakatime'`, `ai_model_version=<plugin version>` — and ignores the real model token after it. Copying the official template therefore always makes the Models panel show `Wakatime`.
|
|
50
|
+
>
|
|
51
|
+
> Putting the model token first is the shape that measurably works:
|
|
52
|
+
>
|
|
53
|
+
> | UA | ai_model | ai_model_version | editor |
|
|
54
|
+
> |---|---|---|---|
|
|
55
|
+
> | `wakatime/1.2.1 (…) node/… deepseek/… dsh/…` | ❌ `Wakatime` | `1.2.1` | None |
|
|
56
|
+
> | `deepseek/4.1-flash-eco dsh/0.2.2-rc.5 (linux-…) node/… dsh-wakatime/1.2.3` | ✅ `DeepSeek` | `4.1-flash-eco` | ✅ `Dsh` |
|
|
57
|
+
> | `dsh/0.2.2-rc.5 (linux-…) node/… dsh-wakatime/1.2.3` (when the model is not recognised) | ✅ `None` | None | ✅ `Dsh` |
|
|
58
|
+
>
|
|
59
|
+
> The cost is losing the `wakatime/` header. The parenthesised section is still recognised (the server substring-matches `(linux-…-x86_64)`), but `editor` only resolves to `Dsh` because of the trailing `dsh-wakatime/<ver>` token — **that token cannot be dropped**. OS names are lowercase and arch uses `x86_64` / `aarch64`, taken from 50 real official-CLI UAs in this account.
|
|
47
60
|
|
|
48
61
|
## Install
|
|
49
62
|
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
**中文** | [English](./README.en.md)
|
|
8
8
|
|
|
9
|
-
[](https://github.com/dsh-plugin-collection/dsh-wakatime)
|
|
10
10
|
[](LICENSE)
|
|
11
11
|
[](https://github.com/deepseek-ai/deepseek-harness)
|
|
12
12
|
|
|
@@ -40,10 +40,23 @@ DSH 里的活动原本不会出现在 WakaTime 上:[WakaTime](https://wakatime
|
|
|
40
40
|
| **Categories** | 工具调用映射(见[类别映射](#类别映射))——`coding` / `browsing` / `researching` 等 |
|
|
41
41
|
| **Languages** | 按文件扩展名推断 |
|
|
42
42
|
| **Editors** | 由心跳的 `User-Agent` 推出,所以「在 DSH 里干活」在面板上单独成了一类 |
|
|
43
|
-
| **Operating Systems** | 同样来自 `User-Agent` 的括号段 `(linux-<内核>-<arch>)` |
|
|
43
|
+
| **Operating Systems** | 同样来自 `User-Agent` 的括号段 `(linux-<内核>-<arch>)`(实测服务端对括号段做子串识别,不依赖 `wakatime/` 头) |
|
|
44
44
|
| **Models** | 同样来自 `User-Agent` 里的模型 token(如 `deepseek/4.1-flash-eco`)——WakaTime **没有** `ai_model` 心跳字段,这个面板只认 UA |
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
| **Machines** | HTTP 头 `X-Machine-Name`(值 URL 转义)。心跳 JSON 里**没有** `machine_name` 字段,这是官方 CLI 用的同一个通道 |
|
|
46
|
+
|
|
47
|
+
> **为什么 UA 的模型 token 在最前面(而不是照抄官方模板)**
|
|
48
|
+
>
|
|
49
|
+
> 官方 `wakatime-cli` 的模板是 `wakatime/<ver> (<OS>-<core>-<platform>) <runtime> <plugin>`,本插件最初也照抄了。但 `user_agents` 端点会返回**服务端对每条 UA 的解析结果**(`ai_model` / `ai_model_version` / `editor`),实测十几种变体后发现:**UA 以 `wakatime/<ver>` 开头时,服务端会把 wakatime 自己当成模型**——`ai_model='Wakatime'`、`ai_model_version=<插件版本>`,后面真正的模型 token 全被忽略。照抄官方模板必然得到 Models 面板显示 `Wakatime` 的结果。
|
|
50
|
+
>
|
|
51
|
+
> 把模型 token 提到首位则是实测可用的形状:
|
|
52
|
+
>
|
|
53
|
+
> | UA | ai_model | ai_model_version | editor |
|
|
54
|
+
> |---|---|---|---|
|
|
55
|
+
> | `wakatime/1.2.1 (…) node/… deepseek/… dsh/…` | ❌ `Wakatime` | `1.2.1` | None |
|
|
56
|
+
> | `deepseek/4.1-flash-eco dsh/0.2.2-rc.5 (linux-…) node/… dsh-wakatime/1.2.3` | ✅ `DeepSeek` | `4.1-flash-eco` | ✅ `Dsh` |
|
|
57
|
+
> | `dsh/0.2.2-rc.5 (linux-…) node/… dsh-wakatime/1.2.3`(模型识别不出时) | ✅ `None` | None | ✅ `Dsh` |
|
|
58
|
+
>
|
|
59
|
+
> 代价是没有 `wakatime/` 头。实测括号段仍被服务端识别(它对 `(linux-…-x86_64)` 做子串匹配),`editor` 则靠在末尾放 `dsh-wakatime/<ver>` 才回落成 `Dsh`——末尾那个 token **不能省**。OS 名用小写、arch 用 `x86_64` / `aarch64`,取自本账号里官方 CLI 的 50 条 UA 样本。
|
|
47
60
|
|
|
48
61
|
## 安装
|
|
49
62
|
|
package/lib/user-agent.d.ts
CHANGED
|
@@ -75,31 +75,37 @@ export interface UserAgentParts {
|
|
|
75
75
|
kernelRelease?: string;
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
|
-
*
|
|
78
|
+
* 拼 User-Agent。
|
|
79
79
|
*
|
|
80
|
-
*
|
|
81
|
-
* `aiUserAgentWithModelTokenAndEditor`)输出同构:
|
|
80
|
+
* 形状(**实测形状**,不是照抄官方模板,原因见下):
|
|
82
81
|
*
|
|
83
82
|
* ```
|
|
84
|
-
*
|
|
83
|
+
* <modelToken|dsh/0.0.0> dsh/<hostVer> (<OS>-<core>-<platform>) node/<ver> dsh-wakatime/<pluginVer>
|
|
85
84
|
* ```
|
|
86
85
|
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
86
|
+
* ## 为什么不是官方模板
|
|
87
|
+
*
|
|
88
|
+
* 官方 `wakatime-cli` 的模板是 `wakatime/%s (%s-%s-%s) %s %s`,本插件最初也照抄了
|
|
89
|
+
* 它。但 `user_agents` 端点会返回**服务端对每条 UA 的解析结果**(`ai_model` /
|
|
90
|
+
* `ai_model_version` / `editor`),实测十几种变体后得到两条硬结论:
|
|
91
|
+
*
|
|
92
|
+
* 1. **UA 以 `wakatime/<ver>` 开头时,服务端把 wakatime 自己当成模型**:
|
|
93
|
+
* `ai_model='Wakatime'`、`ai_model_version=<插件版本>`,后面的模型 token 全被
|
|
94
|
+
* 忽略。照抄官方模板必然得到这个结果——Models 面板显示的会是 `Wakatime`。
|
|
95
|
+
* 2. **模型 token 放在首位才能被识别**:
|
|
96
|
+
* `deepseek/4.1-flash-eco dsh/<v> (linux-…) node/<v> dsh-wakatime/<v>`
|
|
97
|
+
* → `ai_model='DeepSeek'`、`ai_model_version='4.1-flash-eco'`、`editor='Dsh'`。
|
|
98
|
+
*
|
|
99
|
+
* 代价:没有 `wakatime/` 头,官方 `splitUserAgent` 切不出 header;但实测括号段
|
|
100
|
+
* 仍被服务端识别(它对 `(linux-…-x86_64)` 做子串匹配),`editor` 则要靠在末尾
|
|
101
|
+
* 放 `dsh-wakatime/<ver>` 才回落成 `Dsh`——所以末尾那个 token **不能省**。
|
|
102
|
+
*
|
|
103
|
+
* ## 无模型时
|
|
104
|
+
*
|
|
105
|
+
* 模型识别不出(如 `dsh-opendesign-go` 没有版本号)时,首位放 `dsh/<hostVer>`:
|
|
106
|
+
* 宁可让这条心跳不计入任何模型,也不发一个解析不了的 token(那只会往
|
|
107
|
+
* `user_agents` 表里灌垃圾条目)。宿主版本探测不到时用 `0.0.0` 占位——它必须
|
|
108
|
+
* 占住首位(否则括号段会落到首位、被当成模型名),又明显不是真版本号。
|
|
103
109
|
*
|
|
104
110
|
* @param parts - 见 {@link UserAgentParts}。
|
|
105
111
|
* @returns 单行 UA 字符串。
|
package/lib/user-agent.js
CHANGED
|
@@ -199,31 +199,37 @@ export function currentUserAgent(model) {
|
|
|
199
199
|
});
|
|
200
200
|
}
|
|
201
201
|
/**
|
|
202
|
-
*
|
|
202
|
+
* 拼 User-Agent。
|
|
203
203
|
*
|
|
204
|
-
*
|
|
205
|
-
* `aiUserAgentWithModelTokenAndEditor`)输出同构:
|
|
204
|
+
* 形状(**实测形状**,不是照抄官方模板,原因见下):
|
|
206
205
|
*
|
|
207
206
|
* ```
|
|
208
|
-
*
|
|
207
|
+
* <modelToken|dsh/0.0.0> dsh/<hostVer> (<OS>-<core>-<platform>) node/<ver> dsh-wakatime/<pluginVer>
|
|
209
208
|
* ```
|
|
210
209
|
*
|
|
211
|
-
*
|
|
210
|
+
* ## 为什么不是官方模板
|
|
212
211
|
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* - **括号段 `OS-core-platform`**:官方三项依次是 OS 名、CPU 核信息、平台架构;
|
|
217
|
-
* 只有 OS 参与面板归类,后两项仅用于区分机器,这里用内核 release 与 Node
|
|
218
|
-
* 版本充当(不引入新的系统信息采集)。
|
|
219
|
-
* - **products 段**:官方是 `<runtime> <plugin 链>`;模型 token 由
|
|
220
|
-
* `userAgentWithPrependedAgent` **前置**到 products 首位,故这里同样放在
|
|
221
|
-
* runtime 之前。识别不出的模型一律不拼——发出一个解析不了的 token 只会
|
|
222
|
-
* 污染 `user_agents` 表,不会点亮任何面板。
|
|
212
|
+
* 官方 `wakatime-cli` 的模板是 `wakatime/%s (%s-%s-%s) %s %s`,本插件最初也照抄了
|
|
213
|
+
* 它。但 `user_agents` 端点会返回**服务端对每条 UA 的解析结果**(`ai_model` /
|
|
214
|
+
* `ai_model_version` / `editor`),实测十几种变体后得到两条硬结论:
|
|
223
215
|
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
216
|
+
* 1. **UA 以 `wakatime/<ver>` 开头时,服务端把 wakatime 自己当成模型**:
|
|
217
|
+
* `ai_model='Wakatime'`、`ai_model_version=<插件版本>`,后面的模型 token 全被
|
|
218
|
+
* 忽略。照抄官方模板必然得到这个结果——Models 面板显示的会是 `Wakatime`。
|
|
219
|
+
* 2. **模型 token 放在首位才能被识别**:
|
|
220
|
+
* `deepseek/4.1-flash-eco dsh/<v> (linux-…) node/<v> dsh-wakatime/<v>`
|
|
221
|
+
* → `ai_model='DeepSeek'`、`ai_model_version='4.1-flash-eco'`、`editor='Dsh'`。
|
|
222
|
+
*
|
|
223
|
+
* 代价:没有 `wakatime/` 头,官方 `splitUserAgent` 切不出 header;但实测括号段
|
|
224
|
+
* 仍被服务端识别(它对 `(linux-…-x86_64)` 做子串匹配),`editor` 则要靠在末尾
|
|
225
|
+
* 放 `dsh-wakatime/<ver>` 才回落成 `Dsh`——所以末尾那个 token **不能省**。
|
|
226
|
+
*
|
|
227
|
+
* ## 无模型时
|
|
228
|
+
*
|
|
229
|
+
* 模型识别不出(如 `dsh-opendesign-go` 没有版本号)时,首位放 `dsh/<hostVer>`:
|
|
230
|
+
* 宁可让这条心跳不计入任何模型,也不发一个解析不了的 token(那只会往
|
|
231
|
+
* `user_agents` 表里灌垃圾条目)。宿主版本探测不到时用 `0.0.0` 占位——它必须
|
|
232
|
+
* 占住首位(否则括号段会落到首位、被当成模型名),又明显不是真版本号。
|
|
227
233
|
*
|
|
228
234
|
* @param parts - 见 {@link UserAgentParts}。
|
|
229
235
|
* @returns 单行 UA 字符串。
|
|
@@ -234,22 +240,28 @@ export function buildUserAgent(parts) {
|
|
|
234
240
|
const nodeVersion = parts.nodeVersion ?? process.versions.node;
|
|
235
241
|
const arch = archName(parts.arch ?? process.arch);
|
|
236
242
|
const os = osName(platform);
|
|
237
|
-
//
|
|
238
|
-
//
|
|
239
|
-
|
|
240
|
-
const header = `wakatime/${parts.pluginVersion} (${os}-${kernel}-${arch}) node/${nodeVersion}`;
|
|
241
|
-
// products = [模型 token?] + editor 链。模型放首位是照官方
|
|
242
|
-
// `userAgentWithPrependedAgent` 的输出顺序(模型 → editor → 其余)。
|
|
243
|
-
// 探测不到宿主版本时省略 `dsh/` 段:宁可少一个 token,也不发一个编出来的版本
|
|
244
|
-
// (Editors 面板此时回退到 `node/…`,仍能归类,不会污染 user_agents 表)。
|
|
245
|
-
const products = [];
|
|
243
|
+
// 宿主版本探测不到时用 `0.0.0`:必须占住首 token 的位置(否则括号段会落到
|
|
244
|
+
// 首位、被服务端当成模型名),同时它明显不是真实版本,不会冒充具体版本号。
|
|
245
|
+
const host = parts.hostVersion === '' ? '0.0.0' : parts.hostVersion;
|
|
246
246
|
const token = parts.model === undefined ? '' : aiModelToken(parts.model, parts.complexity ?? '');
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
//
|
|
252
|
-
|
|
247
|
+
// ── 为什么模型在最前面、而不是官方那样跟在 `wakatime/` 头之后 ──
|
|
248
|
+
//
|
|
249
|
+
// 实测(`user_agents` 端点会返回服务端解析结果,见 1.2.2 的 CHANGELOG):
|
|
250
|
+
// 只要串以 `wakatime/<ver>` 开头,服务端就把**那个 wakatime 自己**当成模型
|
|
251
|
+
// (`ai_model='Wakatime'`、`ai_model_version=<插件版本>`),后面的模型 token
|
|
252
|
+
// 一律忽略。这正是面板里出现 `Wakatime 100%` 的原因。
|
|
253
|
+
//
|
|
254
|
+
// 把模型 token 提到首位则是实测可用的形状:
|
|
255
|
+
// deepseek/4.1-flash-eco dsh/0.2.2-rc.5 (linux-…) node/24.22.0 dsh-wakatime/1.2.2
|
|
256
|
+
// → ai_model='DeepSeek' ai_model_version='4.1-flash-eco' editor='Dsh'
|
|
257
|
+
// 代价是丢了 `wakatime/` 头——官方 `splitUserAgent` 靠它切 header/products,
|
|
258
|
+
// 没有它 OS 括号段理论上解析不到。**但实测括号段仍在**(服务端自己对
|
|
259
|
+
// `(linux-…-x86_64)` 做子串识别),代价是 `editor` 字段要靠末尾的
|
|
260
|
+
// `dsh-wakatime/<ver>` 才回落成 `Dsh`——所以这个 token 不能省。
|
|
261
|
+
const head = token === '' ? `dsh/${host}` : `${token} dsh/${host}`;
|
|
262
|
+
const system = `(${os}-${kernel}-${arch})`;
|
|
263
|
+
const tail = `node/${nodeVersion} dsh-wakatime/${parts.pluginVersion}`;
|
|
264
|
+
return `${head} ${system} ${tail}`;
|
|
253
265
|
}
|
|
254
266
|
/**
|
|
255
267
|
* 构建期注入的本插件版本占位。
|
|
@@ -258,7 +270,7 @@ export function buildUserAgent(parts) {
|
|
|
258
270
|
* 源码里**绝不手写版本号**——UA 曾长期停在 `1.0.0`,而包已经发到 `1.1.0`,
|
|
259
271
|
* 就是手写造成的脱钩。未注入时(vitest 直接跑源码)为 undefined。
|
|
260
272
|
*/
|
|
261
|
-
const INJECTED_PLUGIN_VERSION = "1.2.
|
|
273
|
+
const INJECTED_PLUGIN_VERSION = "1.2.3";
|
|
262
274
|
/** 取本插件版本:构建期注入值优先,未注入时回退 `0.0.0`(测试环境)。 */
|
|
263
275
|
export function pluginVersion() {
|
|
264
276
|
return typeof INJECTED_PLUGIN_VERSION === 'string' && INJECTED_PLUGIN_VERSION !== ''
|
package/lib/wakatime.d.ts
CHANGED
|
@@ -132,6 +132,8 @@ export declare class WakaTimeClient {
|
|
|
132
132
|
private apiKey;
|
|
133
133
|
private baseUrl;
|
|
134
134
|
private userAgent;
|
|
135
|
+
/** 本机名(Machines 面板的归属来源,经 `X-Machine-Name` 头发送)。 */
|
|
136
|
+
private machineName;
|
|
135
137
|
private retryAfter;
|
|
136
138
|
private authFailRetryAfter;
|
|
137
139
|
private proxyUrl;
|
|
@@ -162,6 +164,13 @@ export declare class WakaTimeClient {
|
|
|
162
164
|
* @returns 形如 `经代理 http://host:port(来源:设置页)` 或 `直连(来源:无配置)`。
|
|
163
165
|
*/
|
|
164
166
|
describeProxy(): string;
|
|
167
|
+
/**
|
|
168
|
+
* `X-Machine-Name` 头;机器名取不到时返回空对象(不发这个头)。
|
|
169
|
+
*
|
|
170
|
+
* 值必须 URL 转义:官方 CLI 用 `url.QueryEscape`,主机名里的非 ASCII
|
|
171
|
+
* 字符若不转义会直接破坏请求头。
|
|
172
|
+
*/
|
|
173
|
+
private machineHeader;
|
|
165
174
|
sendHeartbeat(payload: WakaTimeHeartbeat): Promise<HeartbeatSendResult>;
|
|
166
175
|
sendHeartbeats(payloads: WakaTimeHeartbeat[]): Promise<HeartbeatSendResult>;
|
|
167
176
|
/**
|
package/lib/wakatime.js
CHANGED
|
@@ -1,7 +1,25 @@
|
|
|
1
1
|
import { WakaTimeError, WakaTimeAuthError, WakaTimeRateLimitError } from './types.js';
|
|
2
2
|
import { currentUserAgent } from './user-agent.js';
|
|
3
|
+
import { hostname as osHostname } from 'node:os';
|
|
3
4
|
/** 默认请求超时:挂起的服务端不能让 flush 永不返回、并发请求越积越多。 */
|
|
4
5
|
const DEFAULT_REQUEST_TIMEOUT_MS = 10_000;
|
|
6
|
+
/**
|
|
7
|
+
* 取本机名,供 `X-Machine-Name` 头使用。
|
|
8
|
+
*
|
|
9
|
+
* 官方 CLI 在 `pkg/api/option.go` 的 `WithHostname` 里做同样的事:
|
|
10
|
+
* 把 hostname URL 转义后塞进 `X-Machine-Name` 头。**心跳 JSON 结构里没有
|
|
11
|
+
* machine_name 字段**(`heartbeat.go` 里零命中),所以这是唯一的通道。
|
|
12
|
+
* 取不到就返回空串——调用方据此不发这个头,让服务端继续显示 Unknown
|
|
13
|
+
* Hostname,而不是发一个空字符串上去。
|
|
14
|
+
*/
|
|
15
|
+
function safeHostname() {
|
|
16
|
+
try {
|
|
17
|
+
return osHostname() ?? '';
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return '';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
5
23
|
/**
|
|
6
24
|
* 把 fetch 抛出的错误展开成一行可诊断的文本。
|
|
7
25
|
*
|
|
@@ -211,6 +229,8 @@ export class WakaTimeClient {
|
|
|
211
229
|
apiKey;
|
|
212
230
|
baseUrl;
|
|
213
231
|
userAgent;
|
|
232
|
+
/** 本机名(Machines 面板的归属来源,经 `X-Machine-Name` 头发送)。 */
|
|
233
|
+
machineName;
|
|
214
234
|
retryAfter = 0;
|
|
215
235
|
authFailRetryAfter = 0;
|
|
216
236
|
proxyUrl;
|
|
@@ -227,6 +247,10 @@ export class WakaTimeClient {
|
|
|
227
247
|
// HTTP 头只是兜底:真正的归属信息逐条写在心跳的 `user_agent` 字段里
|
|
228
248
|
// (批量上报时一个头无法区分不同会话/不同模型的条目)。构造规则见 user-agent.ts。
|
|
229
249
|
this.userAgent = currentUserAgent();
|
|
250
|
+
// 机器名走 HTTP 头 `X-Machine-Name`(官方 CLI 的 WithHostname 就是这么做的),
|
|
251
|
+
// 心跳结构里没有 machine_name 字段。没有这个头,Machines 面板永远是
|
|
252
|
+
// `Unknown Hostname`。取不到就不发,让服务端继续显示 Unknown 而不是发空串。
|
|
253
|
+
this.machineName = safeHostname();
|
|
230
254
|
const resolved = resolveProxy(process.env, config.cfg_proxy, config.proxy_url);
|
|
231
255
|
this.proxyUrl = resolved.proxy;
|
|
232
256
|
this.noProxy = usableNoProxy(config.no_proxy) ?? process.env.NO_PROXY ?? '';
|
|
@@ -281,6 +305,17 @@ export class WakaTimeClient {
|
|
|
281
305
|
// 必须是脱敏后的地址:这里会进宿主日志,也会被用户贴进 issue。
|
|
282
306
|
return this.proxyUrl === null ? `直连(来源:${sourceLabel})` : `经代理 ${maskProxyUrl(this.proxyUrl)}(来源:${sourceLabel})`;
|
|
283
307
|
}
|
|
308
|
+
/**
|
|
309
|
+
* `X-Machine-Name` 头;机器名取不到时返回空对象(不发这个头)。
|
|
310
|
+
*
|
|
311
|
+
* 值必须 URL 转义:官方 CLI 用 `url.QueryEscape`,主机名里的非 ASCII
|
|
312
|
+
* 字符若不转义会直接破坏请求头。
|
|
313
|
+
*/
|
|
314
|
+
machineHeader() {
|
|
315
|
+
if (this.machineName === '')
|
|
316
|
+
return {};
|
|
317
|
+
return { 'X-Machine-Name': encodeURIComponent(this.machineName) };
|
|
318
|
+
}
|
|
284
319
|
async sendHeartbeat(payload) {
|
|
285
320
|
return this.sendHeartbeats([payload]);
|
|
286
321
|
}
|
|
@@ -320,6 +355,7 @@ export class WakaTimeClient {
|
|
|
320
355
|
'Authorization': `Basic ${btoa(this.apiKey + ':')}`,
|
|
321
356
|
'Content-Type': 'application/json',
|
|
322
357
|
'User-Agent': batchUserAgent ?? this.userAgent,
|
|
358
|
+
...this.machineHeader(),
|
|
323
359
|
},
|
|
324
360
|
body,
|
|
325
361
|
signal: AbortSignal.timeout(this.requestTimeoutMs),
|
|
@@ -472,6 +508,7 @@ export class WakaTimeClient {
|
|
|
472
508
|
'Content-Type': 'application/json',
|
|
473
509
|
'Content-Length': String(Buffer.byteLength(body)),
|
|
474
510
|
'User-Agent': userAgent,
|
|
511
|
+
...this.machineHeader(),
|
|
475
512
|
'Host': parsed.host,
|
|
476
513
|
},
|
|
477
514
|
socket,
|