hurrydeer-browser 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +209 -2
- package/cli/check-SYUBZTQ6.js +17 -0
- package/cli/chunk-4HBEUKEZ.js +311 -0
- package/cli/index.js +844 -0
- package/package.json +62 -4
- package/skills/hurrydeer-browser/SKILL.md +150 -0
- package/skills/hurrydeer-browser/references/application-management.md +42 -0
- package/skills/hurrydeer-browser/references/browser-profile-management.md +67 -0
- package/skills/hurrydeer-browser/references/group-management.md +24 -0
- package/skills/hurrydeer-browser/references/lifecycle.md +36 -0
- package/skills/hurrydeer-browser/references/mcp.md +74 -0
- package/skills/hurrydeer-browser/references/proxy-management.md +25 -0
- package/skills/hurrydeer-browser/references/rpa.md +23 -0
- package/skills/hurrydeer-browser/references/tool-intent-map.md +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HurryDeer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,210 @@
|
|
|
1
|
-
#
|
|
1
|
+
# hurrydeer-browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
HurryDeer 指纹浏览器桌面端命令行工具。
|
|
4
|
+
通过桌面端内置的本地 HTTP API 操作浏览器环境:启动/关闭环境、管理环境与分组、代理、
|
|
5
|
+
RPA 工作流,并可为 AI Agent 安装 skill、以 stdio 方式启动 MCP 服务。
|
|
6
|
+
|
|
7
|
+
> **定位说明**:本 CLI 是纯客户端工具——API 服务、端口(默认 50325)与 API Key 均由
|
|
8
|
+
> HurryDeer 桌面端提供。CLI 使用桌面端【API & MCP】界面创建的 API Key 连接桌面端端口,
|
|
9
|
+
> 自身不内置任何服务。
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install -g hurrydeer-browser
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
要求 Node.js >= 18,且已安装 HurryDeer 桌面端(内置本地 API 服务)。
|
|
18
|
+
|
|
19
|
+
安装后可用三个等价命令名:`hd`、`hurrydeer`、`hurrydeer-browser`。
|
|
20
|
+
|
|
21
|
+
## 前置条件:获取 API Key 与端口
|
|
22
|
+
|
|
23
|
+
CLI 本身不内置服务——**API 服务由 HurryDeer 桌面端提供**,端口也由桌面端决定(默认 `50325`)。使用前请先在桌面端完成:
|
|
24
|
+
|
|
25
|
+
1. 启动 HurryDeer 桌面端并登录账号。
|
|
26
|
+
2. 打开桌面端左侧菜单的 **【API & MCP】** 界面。
|
|
27
|
+
3. 在 **API Keys** 区域点击「新建密钥」,生成 `sk-...` 格式的 API Key,点击复制。
|
|
28
|
+
4. 同一界面可查看本地 API 端口与 MCP 服务状态(MCP 默认监听 `127.0.0.1:8765`,连接时需携带 `Authorization: Bearer sk-...`)。
|
|
29
|
+
|
|
30
|
+
拿到 Key 后即可启动 CLI runtime:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
hd start -k <你的API_KEY>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
若已设置环境变量 `HURRYDEER_API_KEY`,可直接 `hd start`(无需 `-k`)。
|
|
37
|
+
|
|
38
|
+
## 快速开始
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
hd start -k <API_KEY> # 启动 HurryDeer 桌面端 runtime,等待本地 API 就绪
|
|
42
|
+
hd status # 查看 runtime 运行状态(PID + API 健康检查)
|
|
43
|
+
hd get-browser-list # 列出全部浏览器环境
|
|
44
|
+
hd open-browser <profile_id> # 打开指定环境
|
|
45
|
+
hd get-opened-browser # 列出已打开的环境
|
|
46
|
+
hd close-browser <profile_id> # 关闭指定环境
|
|
47
|
+
hd stop # 退出桌面端 runtime
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 参数形态
|
|
51
|
+
|
|
52
|
+
业务命令的 `[params]` 支持两种等价写法:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# 1. 单值简写(按命令自动映射到 profile_id / group_id / proxy_id 等字段)
|
|
56
|
+
hd open-browser 8f2c1a9e-....
|
|
57
|
+
|
|
58
|
+
# 2. JSON 字符串(完整参数,字段说明见 hd <command> --help)
|
|
59
|
+
hd open-browser '{"profile_id":"8f2c1a9e-....","openUrls":["https://example.com"]}'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## 命令参考
|
|
63
|
+
|
|
64
|
+
### 生命周期
|
|
65
|
+
|
|
66
|
+
| 命令 | 说明 |
|
|
67
|
+
| --- | --- |
|
|
68
|
+
| `hd start -k <API_KEY> [-p <port>]` | 启动桌面端 runtime(API Key 在桌面端【API & MCP】界面创建;port 为桌面端本地 API 端口,默认 50325) |
|
|
69
|
+
| `hd stop [-y]` | 优雅退出桌面端 runtime |
|
|
70
|
+
| `hd restart` | 重启 runtime |
|
|
71
|
+
| `hd status` | 查看 runtime 状态 |
|
|
72
|
+
| `hd update` | 升级本 CLI 到 npm 最新版本 |
|
|
73
|
+
|
|
74
|
+
### 浏览器环境
|
|
75
|
+
|
|
76
|
+
| 命令 | 说明 |
|
|
77
|
+
| --- | --- |
|
|
78
|
+
| `hd open-browser [profile_id]` | 打开环境(JSON 可覆盖 proxy/fingerprint/openUrls) |
|
|
79
|
+
| `hd close-browser [profile_id]` | 关闭环境 |
|
|
80
|
+
| `hd create-browser '<json>'` | 创建环境 |
|
|
81
|
+
| `hd update-browser '<json>'` | 更新环境(JSON 需含 profile_id) |
|
|
82
|
+
| `hd delete-browser [profile_id]` | 删除环境(JSON 可含 permanent) |
|
|
83
|
+
| `hd get-browser-list ['{"group_id":".."}']` | 环境列表(可按分组过滤) |
|
|
84
|
+
| `hd get-opened-browser` | 已打开的环境列表 |
|
|
85
|
+
| `hd get-browser-active [profile_id]` | 查询单个环境运行状态 |
|
|
86
|
+
| `hd close-all-profiles` | 关闭全部已打开环境 |
|
|
87
|
+
|
|
88
|
+
### 分组 / 代理 / 应用 / 同步 / 系统
|
|
89
|
+
|
|
90
|
+
| 命令 | 说明 |
|
|
91
|
+
| --- | --- |
|
|
92
|
+
| `hd get-group-list` | 分组列表 |
|
|
93
|
+
| `hd create-group [group_name]` | 创建分组 |
|
|
94
|
+
| `hd delete-group [group_id]` | 删除分组 |
|
|
95
|
+
| `hd get-proxy-list` | 代理列表 |
|
|
96
|
+
| `hd create-proxy '<json>'` | 创建代理 |
|
|
97
|
+
| `hd delete-proxy [proxy_id]` | 删除代理 |
|
|
98
|
+
| `hd get-extension-list` | 应用(扩展)列表 |
|
|
99
|
+
| `hd create-extension '<json>'` | 添加应用(扩展) |
|
|
100
|
+
| `hd get-sync-queue` | 本地→云端同步队列 |
|
|
101
|
+
| `hd process-sync-queue` | 立即触发一次同步 |
|
|
102
|
+
| `hd get-system-cache` | API 缓存统计 |
|
|
103
|
+
| `hd invalidate-cache ['{"tag":".."}']` | 失效缓存 |
|
|
104
|
+
| `hd check-status` | 本地 API 健康检查 |
|
|
105
|
+
|
|
106
|
+
### 账号(经桌面端代理远端 API)
|
|
107
|
+
|
|
108
|
+
| 命令 | 说明 |
|
|
109
|
+
| --- | --- |
|
|
110
|
+
| `hd get-auth-status` | 桌面端登录态 |
|
|
111
|
+
| `hd get-user-profile` | 当前账号资料 |
|
|
112
|
+
| `hd get-billing` | 计费/套餐信息 |
|
|
113
|
+
| `hd get-team-members` | 团队成员 |
|
|
114
|
+
| `hd verify-quota '<json>'` | 校验配额 |
|
|
115
|
+
|
|
116
|
+
### RPA
|
|
117
|
+
|
|
118
|
+
| 命令 | 说明 |
|
|
119
|
+
| --- | --- |
|
|
120
|
+
| `hd rpa-get-workflow-list` | 本地 RPA 工作流列表 |
|
|
121
|
+
| `hd rpa-create-workflow '<json>'` | 创建/保存工作流 |
|
|
122
|
+
|
|
123
|
+
### 其它
|
|
124
|
+
|
|
125
|
+
| 命令 | 说明 |
|
|
126
|
+
| --- | --- |
|
|
127
|
+
| `hd mcp` | 以 stdio 启动 MCP 服务(供 Claude Code / Codex / Cursor 等 Agent 调用) |
|
|
128
|
+
| `hd install-skill [agent]` | 安装 HurryDeer skill 到 AI Agent(claude\|codex\|cursor\|opencode\|trae\|all) |
|
|
129
|
+
| `hd complete [--shell bash\|zsh]` | 输出 shell 补全脚本 |
|
|
130
|
+
|
|
131
|
+
所有业务命令支持 `-k/--api-key`(桌面端【API & MCP】界面创建的 `sk-...` 密钥)、`-p/--port`(桌面端本地 API 端口,默认 50325)。
|
|
132
|
+
|
|
133
|
+
## 典型工作流示例
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
# 1. 启动 runtime(Key 来自桌面端【API & MCP】界面)
|
|
137
|
+
hd start -k sk-xxxxxxxx
|
|
138
|
+
|
|
139
|
+
# 2. 创建分组与环境
|
|
140
|
+
hd create-group "营销账号A"
|
|
141
|
+
hd create-browser '{"name":"account-01","group_id":"<group_id>","os":"windows"}'
|
|
142
|
+
|
|
143
|
+
# 3. 创建并绑定代理,打开环境
|
|
144
|
+
hd create-proxy '{"name":"proxy-1","type":"http","host":"1.2.3.4","port":8080,"username":"u","password":"p"}'
|
|
145
|
+
hd open-browser <profile_id>
|
|
146
|
+
|
|
147
|
+
# 4. 查询状态与账号信息
|
|
148
|
+
hd get-browser-active <profile_id>
|
|
149
|
+
hd get-auth-status
|
|
150
|
+
hd get-browser-list '{"group_id":"<group_id>"}'
|
|
151
|
+
|
|
152
|
+
# 5. 收尾
|
|
153
|
+
hd close-browser <profile_id>
|
|
154
|
+
hd stop
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Shell 补全
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
# bash
|
|
161
|
+
hd complete --shell bash > /etc/bash_completion.d/hd
|
|
162
|
+
# zsh
|
|
163
|
+
hd complete --shell zsh > ~/.zsh/_hd
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## MCP 配置示例
|
|
167
|
+
|
|
168
|
+
两种接入方式(均需先在桌面端【API & MCP】界面创建 API Key):
|
|
169
|
+
|
|
170
|
+
**方式一:CLI 内置 stdio MCP(推荐,随本包安装)**
|
|
171
|
+
|
|
172
|
+
```json
|
|
173
|
+
{
|
|
174
|
+
"mcpServers": {
|
|
175
|
+
"hurrydeer-browser": {
|
|
176
|
+
"command": "hd",
|
|
177
|
+
"args": ["mcp"]
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**方式二:桌面端内置 HTTP MCP 服务**(默认监听 `127.0.0.1:8765`,可在【API & MCP】界面查看端口与开关;请求需携带 `Authorization: Bearer sk-...`)
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"mcpServers": {
|
|
188
|
+
"hurrydeer-browser": {
|
|
189
|
+
"url": "http://127.0.0.1:8765/mcp",
|
|
190
|
+
"headers": { "Authorization": "Bearer sk-xxxxxxxx" }
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## 环境变量
|
|
197
|
+
|
|
198
|
+
| 变量 | 说明 |
|
|
199
|
+
| --- | --- |
|
|
200
|
+
| `HURRYDEER_APP_PATH` | 桌面端可执行文件路径(`hd start` 定位用) |
|
|
201
|
+
| `HURRYDEER_API_KEY` | API Key(在桌面端【API & MCP】界面创建,`sk-...` 格式;设置后 `hd start` 可省略 `-k`) |
|
|
202
|
+
| `HURRYDEER_API_PORT` | 本地 API 端口覆盖(默认 50325) |
|
|
203
|
+
| `HURRYDEER_API_HOST` | 本地 API 主机覆盖(默认 127.0.0.1) |
|
|
204
|
+
| `HURRYDEER_API_TIMEOUT` | 请求超时毫秒(默认 5000) |
|
|
205
|
+
|
|
206
|
+
端口解析优先级:命令行 `-p` > PID 文件(`~/.hurrydeerCli/pid.json`)> 环境变量 > 默认 50325。
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
checkUpdateNpmPackage,
|
|
4
|
+
getRuntimeStatus,
|
|
5
|
+
greaterThanVersion,
|
|
6
|
+
hasRunning,
|
|
7
|
+
promptUpdateIfNeeded,
|
|
8
|
+
updateNpmPackage
|
|
9
|
+
} from "./chunk-4HBEUKEZ.js";
|
|
10
|
+
export {
|
|
11
|
+
checkUpdateNpmPackage,
|
|
12
|
+
getRuntimeStatus,
|
|
13
|
+
greaterThanVersion,
|
|
14
|
+
hasRunning,
|
|
15
|
+
promptUpdateIfNeeded,
|
|
16
|
+
updateNpmPackage
|
|
17
|
+
};
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/core/check.ts
|
|
4
|
+
import { spawnSync } from "child_process";
|
|
5
|
+
import { createRequire } from "module";
|
|
6
|
+
|
|
7
|
+
// src/store/index.ts
|
|
8
|
+
var store = {
|
|
9
|
+
apiKey: "",
|
|
10
|
+
apiPort: 0,
|
|
11
|
+
baseUrl: "http://127.0.0.1:50325",
|
|
12
|
+
pid: 0,
|
|
13
|
+
status: "stopped"
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// src/tools/index.ts
|
|
17
|
+
import fs from "fs";
|
|
18
|
+
import os from "os";
|
|
19
|
+
import path from "path";
|
|
20
|
+
import { execSync } from "child_process";
|
|
21
|
+
import readline from "readline";
|
|
22
|
+
var RED = "\x1B[31m";
|
|
23
|
+
var GREEN = "\x1B[32m";
|
|
24
|
+
var YELLOW = "\x1B[33m";
|
|
25
|
+
var RESET = "\x1B[0m";
|
|
26
|
+
function logInfo(msg) {
|
|
27
|
+
console.log(msg);
|
|
28
|
+
}
|
|
29
|
+
function logSuccess(msg) {
|
|
30
|
+
console.log(`${GREEN}${msg}${RESET}`);
|
|
31
|
+
}
|
|
32
|
+
function logError(msg) {
|
|
33
|
+
console.error(`${RED}${msg}${RESET}`);
|
|
34
|
+
}
|
|
35
|
+
function logWarning(msg) {
|
|
36
|
+
console.log(`${YELLOW}${msg}${RESET}`);
|
|
37
|
+
}
|
|
38
|
+
var CLI_HOME = path.join(os.homedir(), ".hurrydeerCli");
|
|
39
|
+
var PID_FILE = path.join(CLI_HOME, "pid.json");
|
|
40
|
+
var CONFIG_FILE = path.join(CLI_HOME, "config.json");
|
|
41
|
+
function ensureHome() {
|
|
42
|
+
try {
|
|
43
|
+
fs.mkdirSync(CLI_HOME, { recursive: true });
|
|
44
|
+
} catch {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function writePidFile(data) {
|
|
48
|
+
ensureHome();
|
|
49
|
+
fs.writeFileSync(PID_FILE, JSON.stringify(data), "utf-8");
|
|
50
|
+
}
|
|
51
|
+
function readPidFile() {
|
|
52
|
+
try {
|
|
53
|
+
if (!fs.existsSync(PID_FILE)) return null;
|
|
54
|
+
return JSON.parse(fs.readFileSync(PID_FILE, "utf-8"));
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function removePidFile() {
|
|
60
|
+
try {
|
|
61
|
+
fs.rmSync(PID_FILE, { force: true });
|
|
62
|
+
} catch {
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function readConfig() {
|
|
66
|
+
try {
|
|
67
|
+
if (!fs.existsSync(CONFIG_FILE)) return {};
|
|
68
|
+
return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8"));
|
|
69
|
+
} catch {
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function isRunning(pid) {
|
|
74
|
+
if (!pid) return false;
|
|
75
|
+
try {
|
|
76
|
+
if (process.platform === "win32") {
|
|
77
|
+
const out2 = execSync(`tasklist /fi "PID eq ${pid}"`, { encoding: "utf-8" });
|
|
78
|
+
return out2.includes(String(pid));
|
|
79
|
+
}
|
|
80
|
+
const out = execSync(`ps -p ${pid}`, { encoding: "utf-8" });
|
|
81
|
+
return out.includes(String(pid));
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function createLoading(text) {
|
|
87
|
+
const frames = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
88
|
+
let timer = null;
|
|
89
|
+
let stopped = false;
|
|
90
|
+
const loading = {
|
|
91
|
+
start() {
|
|
92
|
+
if (!process.stdout.isTTY) {
|
|
93
|
+
process.stdout.write(`${text}...
|
|
94
|
+
`);
|
|
95
|
+
return loading;
|
|
96
|
+
}
|
|
97
|
+
let i = 0;
|
|
98
|
+
timer = setInterval(() => {
|
|
99
|
+
if (stopped) return;
|
|
100
|
+
process.stdout.write(`\r${frames[i % frames.length]} ${text}`);
|
|
101
|
+
i++;
|
|
102
|
+
}, 80);
|
|
103
|
+
return loading;
|
|
104
|
+
},
|
|
105
|
+
stop() {
|
|
106
|
+
stopped = true;
|
|
107
|
+
if (timer) clearInterval(timer);
|
|
108
|
+
if (process.stdout.isTTY) process.stdout.write("\r\x1B[K");
|
|
109
|
+
return loading;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return loading;
|
|
113
|
+
}
|
|
114
|
+
function promptYesNo(question) {
|
|
115
|
+
return new Promise((resolve) => {
|
|
116
|
+
if (!process.stdin.isTTY) {
|
|
117
|
+
logWarning("[!] non-interactive session, skip confirmation");
|
|
118
|
+
return resolve(null);
|
|
119
|
+
}
|
|
120
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
121
|
+
rl.question(`${question} [y/N] `, (answer) => {
|
|
122
|
+
rl.close();
|
|
123
|
+
resolve(/^y(es)?$/i.test(answer.trim()) ? "y" : "n");
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// src/cliShared.ts
|
|
129
|
+
function formatJsonValueForHelp(value, indent) {
|
|
130
|
+
const pad = " ".repeat(indent);
|
|
131
|
+
const childPad = " ".repeat(indent + 1);
|
|
132
|
+
if (value === null || typeof value !== "object") {
|
|
133
|
+
return JSON.stringify(value);
|
|
134
|
+
}
|
|
135
|
+
if (Array.isArray(value)) {
|
|
136
|
+
if (value.length === 0) return "[]";
|
|
137
|
+
const lines2 = value.map((item) => `${childPad}${formatJsonValueForHelp(item, indent + 1)}`);
|
|
138
|
+
return `[
|
|
139
|
+
${lines2.join(",\n")}
|
|
140
|
+
${pad}]`;
|
|
141
|
+
}
|
|
142
|
+
const entries = Object.entries(value);
|
|
143
|
+
if (entries.length === 0) return "{}";
|
|
144
|
+
const lines = entries.map(([key, val]) => {
|
|
145
|
+
if (["enum", "oneOf", "required"].includes(key) && Array.isArray(val)) {
|
|
146
|
+
return `${childPad}${JSON.stringify(key)}: ${JSON.stringify(val)}`;
|
|
147
|
+
}
|
|
148
|
+
return `${childPad}${JSON.stringify(key)}: ${formatJsonValueForHelp(val, indent + 1)}`;
|
|
149
|
+
});
|
|
150
|
+
return `{
|
|
151
|
+
${lines.join(",\n")}
|
|
152
|
+
${pad}}`;
|
|
153
|
+
}
|
|
154
|
+
function formatSchemaPropertiesForHelp(properties) {
|
|
155
|
+
return formatJsonValueForHelp(properties, 0);
|
|
156
|
+
}
|
|
157
|
+
var DEFAULT_HOST = process.env.HURRYDEER_API_HOST || "127.0.0.1";
|
|
158
|
+
var DEFAULT_PORT = 50325;
|
|
159
|
+
var TIMEOUT_MS = Number(process.env.HURRYDEER_API_TIMEOUT || 5e3);
|
|
160
|
+
function resolvePort() {
|
|
161
|
+
if (store.apiPort > 0) return store.apiPort;
|
|
162
|
+
const pid = readPidFile();
|
|
163
|
+
if (pid && Number(pid.apiPort) > 0) return Number(pid.apiPort);
|
|
164
|
+
const env = Number(process.env.HURRYDEER_API_PORT || 0);
|
|
165
|
+
if (env > 0) return env;
|
|
166
|
+
return DEFAULT_PORT;
|
|
167
|
+
}
|
|
168
|
+
function baseUrl() {
|
|
169
|
+
return `http://${DEFAULT_HOST}:${resolvePort()}`;
|
|
170
|
+
}
|
|
171
|
+
async function request(method, path2, options = {}) {
|
|
172
|
+
const { query, body, timeoutMs, silent } = options;
|
|
173
|
+
let url = `${baseUrl()}${path2}`;
|
|
174
|
+
if (query && Object.keys(query).length > 0) {
|
|
175
|
+
url += "?" + new URLSearchParams(query).toString();
|
|
176
|
+
}
|
|
177
|
+
const headers = { "Content-Type": "application/json" };
|
|
178
|
+
const apiKey = store.apiKey || readPidFile()?.apiKey || process.env.HURRYDEER_API_KEY || "";
|
|
179
|
+
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
|
|
180
|
+
let status, parsed;
|
|
181
|
+
try {
|
|
182
|
+
const res = await fetch(url, {
|
|
183
|
+
method,
|
|
184
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0,
|
|
185
|
+
signal: AbortSignal.timeout(timeoutMs || TIMEOUT_MS),
|
|
186
|
+
headers
|
|
187
|
+
});
|
|
188
|
+
try {
|
|
189
|
+
parsed = await res.json();
|
|
190
|
+
} catch {
|
|
191
|
+
throw new Error(`\u672C\u5730 API \u8FD4\u56DE\u4E86\u975E JSON \u54CD\u5E94\uFF08HTTP ${res.status}\uFF09\uFF1A${url}`);
|
|
192
|
+
}
|
|
193
|
+
status = res.status;
|
|
194
|
+
} catch (err) {
|
|
195
|
+
if (!silent && (err?.cause?.code === "ECONNREFUSED" || err?.name === "TimeoutError" || err?.cause?.name === "AbortError")) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`\u65E0\u6CD5\u8FDE\u63A5 HurryDeer \u684C\u9762\u7AEF\u672C\u5730 API\uFF08${baseUrl()}\uFF09\u3002
|
|
198
|
+
\u8BF7\u786E\u8BA4\u684C\u9762\u5E94\u7528\u5DF2\u542F\u52A8\uFF08hd start\uFF09\uFF0C\u6216\u7528 -p <\u7AEF\u53E3> \u6307\u5B9A\u975E\u9ED8\u8BA4\u7AEF\u53E3\u3002`
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
throw err;
|
|
202
|
+
}
|
|
203
|
+
if (status >= 400 || parsed && parsed.success === false) {
|
|
204
|
+
const msg = parsed && (parsed.error || parsed.message) ? parsed.error || parsed.message : `HTTP ${status}`;
|
|
205
|
+
const err = new Error(msg);
|
|
206
|
+
err.apiStatus = status;
|
|
207
|
+
err.apiBody = parsed;
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
return parsed;
|
|
211
|
+
}
|
|
212
|
+
async function health(timeoutMs = 2e3) {
|
|
213
|
+
return request("GET", "/api/health", { timeoutMs, silent: true });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/core/check.ts
|
|
217
|
+
var req = createRequire(import.meta.url);
|
|
218
|
+
var pkg = req("../package.json");
|
|
219
|
+
async function hasRunning() {
|
|
220
|
+
const pid = readPidFile();
|
|
221
|
+
if (pid && isRunning(pid.pid)) return true;
|
|
222
|
+
try {
|
|
223
|
+
await health();
|
|
224
|
+
return true;
|
|
225
|
+
} catch {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async function getRuntimeStatus() {
|
|
230
|
+
const pid = readPidFile();
|
|
231
|
+
const procAlive = !!(pid && pid.pid && isRunning(pid.pid));
|
|
232
|
+
let apiOk = false;
|
|
233
|
+
let api = null;
|
|
234
|
+
try {
|
|
235
|
+
if (pid && Number(pid.apiPort) > 0 && !store.apiPort) store.apiPort = Number(pid.apiPort);
|
|
236
|
+
const h = await health();
|
|
237
|
+
apiOk = true;
|
|
238
|
+
api = { baseUrl: `http://127.0.0.1:${store.apiPort || 50325}`, health: h };
|
|
239
|
+
} catch {
|
|
240
|
+
}
|
|
241
|
+
store.status = apiOk || procAlive ? "running" : "stopped";
|
|
242
|
+
if (pid && pid.pid) store.pid = pid.pid;
|
|
243
|
+
return {
|
|
244
|
+
running: apiOk || procAlive,
|
|
245
|
+
process: procAlive && pid ? { pid: pid.pid } : null,
|
|
246
|
+
api
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function greaterThanVersion(a, b) {
|
|
250
|
+
const pa = String(a).replace(/^v/, "").split(".");
|
|
251
|
+
const pb = String(b).replace(/^v/, "").split(".");
|
|
252
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
253
|
+
const na = Number(pa[i] || 0);
|
|
254
|
+
const nb = Number(pb[i] || 0);
|
|
255
|
+
if (na !== nb) return na > nb;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
async function checkUpdateNpmPackage() {
|
|
260
|
+
try {
|
|
261
|
+
const res = await fetch(`https://registry.npmjs.org/${pkg.name}/latest`, {
|
|
262
|
+
signal: AbortSignal.timeout(3e4)
|
|
263
|
+
});
|
|
264
|
+
if (!res.ok) return null;
|
|
265
|
+
const data = await res.json();
|
|
266
|
+
return typeof data?.version === "string" ? data.version : null;
|
|
267
|
+
} catch {
|
|
268
|
+
}
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
function updateNpmPackage() {
|
|
272
|
+
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
273
|
+
const result = spawnSync(npmCmd, ["install", "-g", `${pkg.name}@latest`], { stdio: "inherit", shell: process.platform === "win32" });
|
|
274
|
+
return result.status === 0;
|
|
275
|
+
}
|
|
276
|
+
async function promptUpdateIfNeeded() {
|
|
277
|
+
const latest = await checkUpdateNpmPackage();
|
|
278
|
+
if (!latest || !greaterThanVersion(latest, pkg.version)) return;
|
|
279
|
+
logWarning(`[i] \u53D1\u73B0\u65B0\u7248\u672C ${pkg.name}@${latest}\uFF08\u5F53\u524D ${pkg.version}\uFF09\uFF0C\u53EF\u8FD0\u884C: hd update`);
|
|
280
|
+
const answer = await promptYesNo(`\u7ACB\u5373\u5347\u7EA7\u5230 ${latest}\uFF1F`);
|
|
281
|
+
if (answer === "y") {
|
|
282
|
+
if (updateNpmPackage()) logInfo(`[\u2713] \u5DF2\u5347\u7EA7\u5230 ${pkg.name}@${latest}`);
|
|
283
|
+
else logWarning("[!] \u5347\u7EA7\u5931\u8D25\uFF0C\u53EF\u624B\u52A8\u6267\u884C: npm install -g " + pkg.name + "@latest");
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export {
|
|
288
|
+
store,
|
|
289
|
+
logInfo,
|
|
290
|
+
logSuccess,
|
|
291
|
+
logError,
|
|
292
|
+
logWarning,
|
|
293
|
+
writePidFile,
|
|
294
|
+
readPidFile,
|
|
295
|
+
removePidFile,
|
|
296
|
+
readConfig,
|
|
297
|
+
isRunning,
|
|
298
|
+
createLoading,
|
|
299
|
+
promptYesNo,
|
|
300
|
+
formatSchemaPropertiesForHelp,
|
|
301
|
+
resolvePort,
|
|
302
|
+
baseUrl,
|
|
303
|
+
request,
|
|
304
|
+
health,
|
|
305
|
+
hasRunning,
|
|
306
|
+
getRuntimeStatus,
|
|
307
|
+
greaterThanVersion,
|
|
308
|
+
checkUpdateNpmPackage,
|
|
309
|
+
updateNpmPackage,
|
|
310
|
+
promptUpdateIfNeeded
|
|
311
|
+
};
|