1
This commit is contained in:
@@ -57,41 +57,37 @@ module.exports = {
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
// 确保目录存在
|
||||
if (!fs.existsSync(jsRootDir)) {
|
||||
fs.mkdirSync(jsRootDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 3. 如果文件已存在,直接返回本地文件(文件内容已是替换后的,无需再次注入)
|
||||
if (fs.existsSync(localFilePath)) {
|
||||
ctx.type = 'application/javascript; charset=utf-8';
|
||||
ctx.body = fs.createReadStream(localFilePath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 文件不存在:从远程下载并保存(带钩子注入)
|
||||
const response = await axios.get(urlStr, {
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
ctx.status = 502;
|
||||
ctx.body = { code: 502, message: '下载远程 JS 失败' };
|
||||
return;
|
||||
}
|
||||
|
||||
const patched = injectOnMessageArrivedHook(Buffer.from(response.data));
|
||||
|
||||
fs.writeFileSync(localFilePath, patched);
|
||||
|
||||
ctx.type = 'application/javascript; charset=utf-8';
|
||||
ctx.body = patched;
|
||||
} catch (error) {
|
||||
console.error('[static/boss] 处理失败:', error);
|
||||
ctx.status = 500;
|
||||
ctx.body = { code: 500, message: '静态资源代理失败', error: error.message };
|
||||
// 确保目录存在
|
||||
if (!fs.existsSync(jsRootDir)) {
|
||||
fs.mkdirSync(jsRootDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 3. 如果文件已存在,直接返回本地文件(文件内容已是替换后的,无需再次注入)
|
||||
if (fs.existsSync(localFilePath)) {
|
||||
ctx.type = 'application/javascript; charset=utf-8';
|
||||
ctx.body = fs.createReadStream(localFilePath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 4. 文件不存在:从远程下载并保存(带钩子注入)
|
||||
const response = await axios.get(urlStr, {
|
||||
responseType: 'arraybuffer',
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
ctx.status = 502;
|
||||
ctx.body = { code: 502, message: '下载远程 JS 失败' };
|
||||
return;
|
||||
}
|
||||
|
||||
const patched = injectOnMessageArrivedHook(Buffer.from(response.data));
|
||||
|
||||
fs.writeFileSync(localFilePath, patched);
|
||||
|
||||
ctx.type = 'application/javascript; charset=utf-8';
|
||||
ctx.body = patched;
|
||||
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user