feat: 更新资源管理器和协议处理逻辑,增强错误处理和缓存机制,优化CLI测试用例
This commit is contained in:
@ -347,6 +347,14 @@ class PackageProtocol extends ResourceProtocol {
|
||||
* @returns {Promise<string>} 解析后的绝对路径
|
||||
*/
|
||||
async resolvePath (relativePath, params = null) {
|
||||
// 生成缓存键
|
||||
const cacheKey = `resolve:${relativePath}:${params ? params.toString() : ''}`
|
||||
|
||||
// 检查缓存
|
||||
if (this.cache.has(cacheKey)) {
|
||||
return this.cache.get(cacheKey)
|
||||
}
|
||||
|
||||
// 获取包根目录
|
||||
const packageRoot = await this.getPackageRoot()
|
||||
|
||||
@ -359,9 +367,12 @@ class PackageProtocol extends ResourceProtocol {
|
||||
|
||||
// 安全检查:确保路径在包根目录内
|
||||
if (!fullPath.startsWith(packageRoot)) {
|
||||
throw new Error(`Path traversal detected: ${relativePath}`)
|
||||
throw new Error(`路径安全检查失败: ${relativePath}`)
|
||||
}
|
||||
|
||||
// 存储到缓存
|
||||
this.cache.set(cacheKey, fullPath)
|
||||
|
||||
return fullPath
|
||||
}
|
||||
|
||||
@ -452,9 +463,7 @@ class PackageProtocol extends ResourceProtocol {
|
||||
/**
|
||||
* 加载资源内容
|
||||
*/
|
||||
async loadContent (resourcePath, queryParams) {
|
||||
const resolvedPath = await this.resolvePath(resourcePath, queryParams)
|
||||
|
||||
async loadContent (resolvedPath, queryParams) {
|
||||
try {
|
||||
await fsPromises.access(resolvedPath)
|
||||
const content = await fsPromises.readFile(resolvedPath, 'utf8')
|
||||
@ -469,12 +478,12 @@ class PackageProtocol extends ResourceProtocol {
|
||||
size: content.length,
|
||||
lastModified: stats.mtime,
|
||||
absolutePath: resolvedPath,
|
||||
relativePath: resourcePath
|
||||
relativePath: path.relative(await this.getPackageRoot(), resolvedPath)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
throw new Error(`包资源不存在: ${resourcePath} (解析为: ${resolvedPath})`)
|
||||
throw new Error(`包资源不存在: ${resolvedPath}`)
|
||||
}
|
||||
throw new Error(`加载包资源失败: ${error.message}`)
|
||||
}
|
||||
|
||||
@ -153,7 +153,8 @@ class PromptProtocol extends ResourceProtocol {
|
||||
|
||||
// 使用 glob 查找匹配的文件
|
||||
const files = await glob(searchPattern, {
|
||||
ignore: ['**/node_modules/**', '**/.git/**']
|
||||
ignore: ['**/node_modules/**', '**/.git/**'],
|
||||
absolute: true
|
||||
})
|
||||
|
||||
if (files.length === 0) {
|
||||
@ -224,7 +225,9 @@ class PromptProtocol extends ResourceProtocol {
|
||||
const packageRoot = await this.packageProtocol.getPackageRoot()
|
||||
const cleanPath = packagePath.replace('@package://', '')
|
||||
const searchPattern = path.join(packageRoot, cleanPath)
|
||||
const files = await glob(searchPattern)
|
||||
const files = await glob(searchPattern, {
|
||||
ignore: ['**/node_modules/**', '**/.git/**']
|
||||
})
|
||||
return files.length > 0
|
||||
} else {
|
||||
// 单个文件:检查文件是否存在
|
||||
|
||||
@ -189,7 +189,20 @@ class ResourceManager {
|
||||
throw new Error('ResourceManager未初始化')
|
||||
}
|
||||
|
||||
return this.registry.protocols[protocol]
|
||||
const handler = this.protocolHandlers.get(protocol)
|
||||
if (handler && typeof handler.getProtocolInfo === 'function') {
|
||||
return handler.getProtocolInfo()
|
||||
}
|
||||
|
||||
const protocolConfig = this.registry.protocols[protocol]
|
||||
if (protocolConfig) {
|
||||
return {
|
||||
name: protocol,
|
||||
...protocolConfig
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user