include files at build time

This commit is contained in:
David Ventura
2024-05-01 12:44:52 +02:00
parent a547546055
commit 9c19c05274
2 changed files with 25 additions and 16 deletions
+23 -14
View File
@@ -7,6 +7,11 @@ import { discoverDevices } from "./discovery.js";
import { DevSerial, SendDevStatus } from "./impl.js"; import { DevSerial, SendDevStatus } from "./impl.js";
import { Handlers, makeSession, Session, startVideoStream } from "./session.js"; import { Handlers, makeSession, Session, startVideoStream } from "./session.js";
// @ts-expect-error TS2307
import favicon from "./cam.ico.gz";
// @ts-expect-error TS2307
import html_template from "./asd.html";
const BOUNDARY = "a very good boundary line"; const BOUNDARY = "a very good boundary line";
const responses: Record<string, http.ServerResponse[]> = {}; const responses: Record<string, http.ServerResponse[]> = {};
const audioResponses: Record<string, http.ServerResponse[]> = {}; const audioResponses: Record<string, http.ServerResponse[]> = {};
@@ -16,19 +21,23 @@ const sessions: Record<string, Session> = {};
const nameFile = "cameras.txt"; const nameFile = "cameras.txt";
// Reads the simple mapping of camera names from the text file. // Reads the simple mapping of camera names from the text file.
const cameraNames = Object.assign({}, const cameraNames = Object.assign(
...(existsSync(nameFile) ? readFileSync(nameFile, "utf8") : "").toString() {},
.replace(/\r\n/g, "\n").split("\n") ...(existsSync(nameFile) ? readFileSync(nameFile, "utf8") : "")
.filter(l => !l.startsWith("#")) .toString()
.filter(l => l.trim() != "") .replace(/\r\n/g, "\n")
.map(l => { let kv = l.split("="); return ({ [kv[0]]: kv[1] }); })); .split("\n")
.filter((l) => !l.startsWith("#"))
.filter((l) => l.trim() != "")
.map((l) => {
let kv = l.split("=");
return { [kv[0]]: kv[1] };
}),
);
// Returns the camera name (custom name, if it exists, otherwise its ID). // Returns the camera name (custom name, if it exists, otherwise its ID).
const cameraName = (id: string): string => cameraNames[id] || id; const cameraName = (id: string): string => cameraNames[id] || id;
// Page's favicon.
const favicon = readFileSync("cam.ico.gz");
// The HTTP server. // The HTTP server.
export const serveHttp = (opts: opt, port: number, with_audio: boolean) => { export const serveHttp = (opts: opt, port: number, with_audio: boolean) => {
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
@@ -45,11 +54,11 @@ export const serveHttp = (opts: opt, port: number, with_audio: boolean) => {
res.end("Nothing online"); res.end("Nothing online");
return; return;
} }
const ui = readFileSync("asd.html").toString() const ui = html_template
.replace(/\${id}/g, devId) .toString()
.replace(/\${name}/g, cameraName(devId)) .replace(/\${id}/g, devId)
.replace(/\${audio}/g, with_audio.toString()) .replace(/\${name}/g, cameraName(devId))
.replace(/\${debug}/g, opts.debug.toString()); .replace(/\${audio}/g, with_audio.toString());
res.end(ui); res.end(ui);
return; return;
} }
+2 -2
View File
@@ -3,13 +3,13 @@
"scripts": { "scripts": {
"test": "mocha tests", "test": "mocha tests",
"tsc": "tsc", "tsc": "tsc",
"build": "esbuild cmd/bin.ts --bundle --platform=node --outfile=dist/bin.cjs --target=node12" "build": "esbuild cmd/bin.ts --bundle --platform=node --outfile=dist/bin.cjs --target=node12 --loader:.gz=binary --loader:.html=text"
}, },
"devDependencies": { "devDependencies": {
"@types/yargs": "^17.0.32", "@types/yargs": "^17.0.32",
"esbuild": "^0.20.2",
"mocha": "^10.2.0", "mocha": "^10.2.0",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"esbuild": "^0.20.2",
"typescript": "^5.3.3" "typescript": "^5.3.3"
}, },
"dependencies": { "dependencies": {