Skip to content

Instantly share code, notes, and snippets.

@vikingmute
Created March 23, 2025 07:35
Show Gist options
  • Save vikingmute/6d40dabf54eeaaccacba655a8d96e09d to your computer and use it in GitHub Desktop.
Save vikingmute/6d40dabf54eeaaccacba655a8d96e09d to your computer and use it in GitHub Desktop.
​// 打印原始 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