Skip to content

Commit

Permalink
fix: 修复因 URI 解析异常导致程序退出的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
idootop committed Sep 5, 2024
1 parent ac07e5b commit a041ee9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v3.1.0

- 修复因 URI 解析异常导致程序退出的问题 [issue#12](https://github.com/idootop/mi-gpt-tts/issues/12)

# v3.0.0

## 🚨 风险预警
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mi-gpt-tts",
"version": "3.0.0",
"version": "3.1.0",
"type": "module",
"license": "MIT",
"description": "适用于 MiGPT 的 TTS 模块,支持火山引擎、微软必应、OpenAI 等 TTS 服务。",
Expand Down
18 changes: 16 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const server = http.createServer((req, res) => {
// 校验 secret path
const secretPath = req.url.split("/")[1];
if (secretPath !== kSecretPath) {
console.log("❌ 非法请求" + decodeURI(req.url));
console.log("❌ 非法请求: " + req.url);
return response("❌ 非法请求");
}

Expand All @@ -30,7 +30,13 @@ const server = http.createServer((req, res) => {
const { pathname } = new URL("http://127.0.0.1" + req.url);
const filePath = `public${pathname}`;

console.log("🔥 " + decodeURI(req.url));
let uri = withCatch(() => decodeURI(req.url));
if (!uri) {
console.log("❌ 请求路径解析异常: " + req.url);
return response("❌ 请求路径解析异常");
}

console.log("🔥 " + uri);

if (pathname.startsWith("/api/speakers")) {
apiSpeakers(req, res);
Expand Down Expand Up @@ -65,3 +71,11 @@ function exists(path) {
return false;
}
}

function withCatch(task, onError) {
try {
return task();
} catch (error) {
return onError?.(error);
}
}

0 comments on commit a041ee9

Please sign in to comment.