refactor(calculate-version): 將驗證常數抽離為具名常數

- config 將每段長度上限與允許字元抽為 MAX_REPO_NAME_LENGTH / REPO_SEGMENT_PATTERN(regex 提升至模組層級,避免每次重編譯)
- releases 將片段截斷長度抽為 API_ERROR_SNIPPET_LENGTH

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 14:11:36 +08:00
co-authored by Claude Opus 4.8
parent 8ee3638be5
commit d8a86e90ea
2 changed files with 11 additions and 4 deletions
+7 -3
View File
@@ -55,12 +55,16 @@ function assertHttpUrl(name, value) {
}
}
// 驗證 repository 為 owner/repo 格式(僅允許字母數字與 . _ -,且拒絕 . 與 .. 路徑穿越段
// repository 每段名稱的最大長度與允許字元(字母數字與 . _ -
const MAX_REPO_NAME_LENGTH = 100;
const REPO_SEGMENT_PATTERN = /^[A-Za-z0-9._-]+$/;
// 驗證 repository 為 owner/repo 格式(拒絕 . 與 .. 路徑穿越段、空段、過長或含非法字元的段)
function assertRepository(name, value) {
const parts = value.split('/');
const isRepoFormatValid = parts.length === 2
&& parts.every((part) => part.length > 0 && part.length <= 100
&& /^[A-Za-z0-9._-]+$/.test(part)
&& parts.every((part) => part.length > 0 && part.length <= MAX_REPO_NAME_LENGTH
&& REPO_SEGMENT_PATTERN.test(part)
&& part !== '.' && part !== '..');
if (!isRepoFormatValid) {
throw new Error(`${name} 格式錯誤,必須為 owner/repo`);
+4 -1
View File
@@ -3,6 +3,9 @@
// 每頁取得的 release 筆數
const RELEASES_PER_PAGE = 10;
// JSON 解析失敗時,附在錯誤訊息中的回應內容片段最大字元數
const API_ERROR_SNIPPET_LENGTH = 200;
/**
* 以分頁方式取得指定 Gitea repo 的所有 release,並回傳合併後的陣列。
*
@@ -64,7 +67,7 @@ async function fetchReleases(baseUrl, options = {}) {
pageJson = JSON.parse(text);
} catch {
// 以字元(而非 UTF-16 碼元)截斷回傳內容片段,避免拆分多位元組字元造成亂碼
const contentSnippet = Array.from(text).slice(0, 200).join('');
const contentSnippet = Array.from(text).slice(0, API_ERROR_SNIPPET_LENGTH).join('');
throw new Error(`release API 回傳資料無法解析 (page=${page}),回應內容片段:「${contentSnippet}`);
}