local: add content-type header

This commit is contained in:
CanadaHonk 2023-01-27 18:41:13 +00:00
parent 6132120dad
commit bfddd24a2a
2 changed files with 19 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import { basename, dirname, extname, join } from 'path'; import { basename, dirname, extname, join } from 'path';
import { readFile } from 'fs/promises'; import { readFile } from 'fs/promises';
import { log } from '../logger.js'; import { log } from '../logger.js';
import mimeType from '../mimeType.js';
const generatePath = (pathname, indexFile) => { const generatePath = (pathname, indexFile) => {
if (pathname === '/') return indexFile; if (pathname === '/') return indexFile;
@ -19,17 +20,29 @@ export default async (CDP, { sessionId, url: givenPath, localUrl }) => {
const url = new URL(request.url); const url = new URL(request.url);
const path = join(basePath, generatePath(url.pathname, indexFile)); const path = join(basePath, generatePath(url.pathname, indexFile));
const ext = extname(path).slice(1);
let error = false; let error = false;
const body = await readFile(path, 'utf8').catch(() => false); const body = await readFile(path, 'utf8').catch(() => false);
if (!body) error = 404; if (!body) error = 404;
if (error) return await CDP.sendMessage('Fetch.fulfillRequest', {
requestId,
responseCode: error,
body: Buffer.from('').toString('base64') // CDP uses base64 encoding for request body
});
return await CDP.sendMessage('Fetch.fulfillRequest', { return await CDP.sendMessage('Fetch.fulfillRequest', {
requestId, requestId,
responseCode: error || 200, responseCode: 200,
body: Buffer.from(error ? '' : body).toString('base64'), // CDP uses base64 encoding for request body body: Buffer.from(body).toString('base64'), // CDP uses base64 encoding for request body
// need to add our own headers or not? responseHeaders: [
{
name: 'Content-Type',
value: mimeType(ext)
}
]
}); });
} }
}); });

View File

@ -2,6 +2,7 @@ import { basename, dirname, extname, join } from 'path';
import { readFile } from 'fs/promises'; import { readFile } from 'fs/promises';
import { createServer } from 'http'; import { createServer } from 'http';
import { log } from '../logger.js'; import { log } from '../logger.js';
import mimeType from '../mimeType.js';
const generatePath = (pathname, indexFile) => { const generatePath = (pathname, indexFile) => {
if (pathname === '/') return indexFile; if (pathname === '/') return indexFile;
@ -18,23 +19,20 @@ export default async ({ url: givenPath, localUrl }) => {
const server = createServer(async (req, res) => { const server = createServer(async (req, res) => {
const url = new URL(`http://localhost:${port}` + decodeURI(req.url)); const url = new URL(`http://localhost:${port}` + decodeURI(req.url));
const path = join(basePath, generatePath(url.pathname, indexFile)); const path = join(basePath, generatePath(url.pathname, indexFile));
const ext = extname(path).slice(1);
console.log('SERVER', url, path);
let error = false; let error = false;
const body = await readFile(path, 'utf8').catch(() => false); const body = await readFile(path, 'utf8').catch(() => false);
if (!body) error = 404; if (!body) error = 404;
console.log('SERVER', error);
if (error) { if (error) {
res.writeHead(error); res.writeHead(error);
return res.end(); return res.end();
} }
res.writeHead(200, { res.writeHead(200, {
'Content-Type': mimeType(ext)
}); });
res.end(body, 'utf8'); res.end(body, 'utf8');