WIP: add mimetypes to response headers
This commit is contained in:
parent
693d0c0501
commit
e6fb0354e0
@ -1,6 +1,7 @@
|
|||||||
import * as esbuild from 'esbuild';
|
import * as esbuild from 'esbuild';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import http from 'node:http';
|
import http from 'node:http';
|
||||||
|
import path from 'node:path';
|
||||||
|
|
||||||
const colors = {
|
const colors = {
|
||||||
reset: '\x1b[0m',
|
reset: '\x1b[0m',
|
||||||
@ -18,29 +19,32 @@ const svgUseCacheBust = {
|
|||||||
name: 'svgUseCacheBust',
|
name: 'svgUseCacheBust',
|
||||||
setup(build) {
|
setup(build) {
|
||||||
build.onStart(() => {
|
build.onStart(() => {
|
||||||
const version = Date.now();
|
console.log("BUILD START");
|
||||||
console.log(`Adding cache buster ${version}`);
|
|
||||||
const file = fs.readFileSync('./src/scenario-side_show.svg', { encoding: 'utf-8' });
|
// const version = Date.now();
|
||||||
const newFile = file.replaceAll('%%VERSION%%', version);
|
// const file = fs.readFileSync('./src/scenario-side_show.svg', { encoding: 'utf-8' });
|
||||||
fs.writeFileSync('./public/assets/images/scenario-side_show.svg', newFile);
|
// const newFile = file.replaceAll('%%VERSION%%', version);
|
||||||
const files = fs.readdirSync('./public/assets/images').filter(fn => regex.test(fn));
|
// fs.writeFileSync('./public/assets/images/scenario-side_show.svg', newFile);
|
||||||
files.forEach(fn => fs.unlinkSync(`./public/assets/images/${fn}`));
|
// const files = fs.readdirSync('./public/assets/images').filter(fn => regex.test(fn));
|
||||||
fs.copyFileSync('./public/assets/images/mapsheets.svg', `./public/assets/images/mapsheets.${version}.svg`);
|
// files.forEach(fn => fs.unlinkSync(`./public/assets/images/${fn}`));
|
||||||
|
// fs.copyFileSync('./public/assets/images/mapsheets.svg', `./public/assets/images/mapsheets.${version}.svg`);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
const paths = ['/esbuild', '/index.js', '/map.js', '/soldier_record_block.js'];
|
||||||
|
|
||||||
const ctx = await esbuild.context({
|
const ctx = await esbuild.context({
|
||||||
entryPoints: ['src/*.js'],
|
entryPoints: ['src/*.js'],
|
||||||
bundle: true,
|
bundle: true,
|
||||||
outdir: 'public',
|
outdir: 'build',
|
||||||
plugins: [svgUseCacheBust],
|
plugins: [svgUseCacheBust],
|
||||||
});
|
});
|
||||||
|
|
||||||
await ctx.watch();
|
await ctx.watch();
|
||||||
|
|
||||||
const { host, port } = await ctx.serve({
|
const { host, port } = await ctx.serve({
|
||||||
servedir: 'public',
|
servedir: 'build',
|
||||||
port: 3000,
|
port: 3000,
|
||||||
onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
|
onRequest: function({ remoteAddress, method, path, status, timeInMS }) {
|
||||||
let statusColor = colors.red;
|
let statusColor = colors.red;
|
||||||
@ -64,6 +68,7 @@ http.createServer((req, res) => {
|
|||||||
headers: req.headers,
|
headers: req.headers,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (paths.includes(req.url)) {
|
||||||
const proxyReq = http.request(options, proxyRes => {
|
const proxyReq = http.request(options, proxyRes => {
|
||||||
for (const k in proxyRes.headers) {
|
for (const k in proxyRes.headers) {
|
||||||
res.setHeader(k, proxyRes.headers[k]);
|
res.setHeader(k, proxyRes.headers[k]);
|
||||||
@ -74,5 +79,42 @@ http.createServer((req, res) => {
|
|||||||
proxyRes.pipe(res, { end: true });
|
proxyRes.pipe(res, { end: true });
|
||||||
})
|
})
|
||||||
|
|
||||||
req.pipe(proxyReq, { end: true });
|
return req.pipe(proxyReq, { end: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(host, port, req.method, req.url, res.statusCode);
|
||||||
|
|
||||||
|
const serverUrl = `http://localhost:${port}`;
|
||||||
|
const url = new URL(`${serverUrl}${req.url}`);
|
||||||
|
const dir = 'public';
|
||||||
|
|
||||||
|
const filePath = path.normalize(
|
||||||
|
path.join(dir, url.pathname === '/' ? 'index.html' : url.pathname)
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const k in res.headers) {
|
||||||
|
res.setHeader(k, res.headers[k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let contentType;
|
||||||
|
|
||||||
|
if (req.url.endsWith('.svg')) {
|
||||||
|
contentType = 'image/svg+xml';
|
||||||
|
} else if (req.url.endsWith('.png')) {
|
||||||
|
contentType = 'image/png';
|
||||||
|
} else if (req.url.endsWith('.jpg')) {
|
||||||
|
contentType = 'image/jpeg';
|
||||||
|
} else if (req.url.endsWith('.css')) {
|
||||||
|
contentType = 'text/css';
|
||||||
|
} else {
|
||||||
|
contentType = 'text/html';
|
||||||
|
}
|
||||||
|
|
||||||
|
res.writeHead(res.statusCode, {
|
||||||
|
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||||
|
'Content-Type': contentType
|
||||||
|
});
|
||||||
|
|
||||||
|
const readStream = fs.createReadStream(filePath, { autoClose: true });
|
||||||
|
readStream.pipe(res, { end: true });
|
||||||
}).listen(8080);
|
}).listen(8080);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user