Created
March 23, 2025 07:35
-
-
Save vikingmute/6d40dabf54eeaaccacba655a8d96e09d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 打印原始 URL 用于调试 | |
console.log('Request URL:', request.url); | |
// 使用 new URL 解析请求路径,避免手动切片 | |
const url = new URL(request.url); | |
let filePath = decodeURIComponent(url.pathname); // 提取路径部分并解码 | |
// 在 Windows 上,pathname 可能以斜杠开头(如 /C:/path),需要清理 | |
if (path.sep === '\\' && filePath.startsWith('/')) { | |
filePath = filePath.slice(1); // 移除开头的斜杠 | |
} | |
// 打印解析后的文件路径 | |
console.log('Decoded File Path:', filePath); | |
// 规范化路径,确保跨平台兼容性 | |
const normalizedPath = path.normalize(filePath); | |
// 转换为 file:// URL | |
const fileUrl = pathToFileURL(normalizedPath).toString(); | |
console.log('File URL:', fileUrl); | |
// 使用 net.fetch 获取文件并返回 | |
return await net.fetch(fileUrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment