in ide/deploy/serve.js [146:207]
async fetch(req) {
const {pathname} = parse(req.url);
const filePath = `${assetsDirectory}${pathname === "/" ? "/index.html" : pathname}`;
// Check if file exists locally
try {
// console.debug(`Asset dir: ${assetsDirectory} - Filepath is ${filePath}`);
const fileStats = statSync(filePath);
if (fileStats.isFile()) {
const mimeType = flags.mimeType || getMimeType(filePath);
const headers = {
"Content-Type": mimeType,
};
if (flags.cacheTime) {
headers["Cache-Control"] = `max-age=${flags.cacheTime}`;
}
console.log(`[200] - Serving file ${pathname} from filesystem`);
return new Response(readFileSync(filePath), {headers});
}
} catch (err) {
let shouldExclude = (excludedAssets.indexOf(pathname) !== -1);
// File not found, fall back to proxy
if (flags.proxyHost && !shouldExclude) {
const destProxyUrl = `${flags.proxyHost}${pathname}`;
console.log(`[ P ] - Proxying request ${req.method} to '${destProxyUrl}'`);
const newHeaders = JSON.parse(JSON.stringify(req.headers));
const excludedHeaders = [
'host', 'origin',
// 'accept-encoding',
'sec-fetch-mode', 'sec-fetch-dest', 'sec-ch-ua',
'sec-ch-ua-mobile', 'sec-ch-ua-platform', 'sec-fetch-site'
];
for (const header of excludedHeaders) {
delete newHeaders[header];
}
const init = {
method: req.method,
headers: newHeaders,
};
if (req.method.toLowerCase() !== 'get') {
let body = await toBuffer(req.body);
body = body.toString('utf8');
if (body !== undefined) {
init['body'] = body;
}
}
const respP = await fetch(destProxyUrl, init);
console.log(`[${respP.status}] - Sending response with status ${respP.statusText}`);
return respP;
} else {
console.log(`[404] - File ${pathname} not found`);
return new Response("File not found", {status: 404});
}
}
},