Skip to content

Commit

Permalink
refactor(src):优化url判断、清理临时文件逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
fangxuling committed Dec 11, 2024
1 parent d9e71aa commit 3269a4f
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,28 @@ import * as os from 'os';

config();

// 添加新函数
// 验证URL是否有效
function isValidUrl(urlString: string): boolean {
try {
new URL(urlString);
return true;
// 支持标准协议
if (urlString.match(/^(http|https|ftp|ssh|file):\/\//)) {
new URL(urlString);
return true;
} // 支持 scp 格式的 SSH URL

if (urlString.match(/^git@[^:]+:/)) {
return true;
} // 支持本地文件路径

if (
urlString.startsWith('file://') ||
urlString.startsWith('/') ||
/^[a-zA-Z]:\\/.test(urlString)
) {
return true;
}

return false;
} catch {
return false;
}
Expand Down Expand Up @@ -139,6 +156,11 @@ async function getContentFromUrl(urlString: string): Promise<string> {
} catch (secondError) {
// 简化错误信息
throw new Error(`无法从 URL 获取内容: ${urlString}`);
} finally {
// 确保删除临时文件
if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
}
}
}
}
Expand Down

0 comments on commit 3269a4f

Please sign in to comment.