From 9c19c0527425a6162d1f49c007e41c34baccb81c Mon Sep 17 00:00:00 2001 From: David Ventura Date: Wed, 1 May 2024 12:41:37 +0200 Subject: [PATCH] include files at build time --- http_server.ts | 37 +++++++++++++++++++++++-------------- package.json | 4 ++-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/http_server.ts b/http_server.ts index 262e913..26ab7fa 100644 --- a/http_server.ts +++ b/http_server.ts @@ -7,6 +7,11 @@ import { discoverDevices } from "./discovery.js"; import { DevSerial, SendDevStatus } from "./impl.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 responses: Record = {}; const audioResponses: Record = {}; @@ -16,19 +21,23 @@ const sessions: Record = {}; const nameFile = "cameras.txt"; // Reads the simple mapping of camera names from the text file. -const cameraNames = Object.assign({}, - ...(existsSync(nameFile) ? readFileSync(nameFile, "utf8") : "").toString() - .replace(/\r\n/g, "\n").split("\n") - .filter(l => !l.startsWith("#")) - .filter(l => l.trim() != "") - .map(l => { let kv = l.split("="); return ({ [kv[0]]: kv[1] }); })); +const cameraNames = Object.assign( + {}, + ...(existsSync(nameFile) ? readFileSync(nameFile, "utf8") : "") + .toString() + .replace(/\r\n/g, "\n") + .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). const cameraName = (id: string): string => cameraNames[id] || id; -// Page's favicon. -const favicon = readFileSync("cam.ico.gz"); - // The HTTP server. export const serveHttp = (opts: opt, port: number, with_audio: boolean) => { const server = http.createServer((req, res) => { @@ -45,11 +54,11 @@ export const serveHttp = (opts: opt, port: number, with_audio: boolean) => { res.end("Nothing online"); return; } - const ui = readFileSync("asd.html").toString() - .replace(/\${id}/g, devId) - .replace(/\${name}/g, cameraName(devId)) - .replace(/\${audio}/g, with_audio.toString()) - .replace(/\${debug}/g, opts.debug.toString()); + const ui = html_template + .toString() + .replace(/\${id}/g, devId) + .replace(/\${name}/g, cameraName(devId)) + .replace(/\${audio}/g, with_audio.toString()); res.end(ui); return; } diff --git a/package.json b/package.json index 6f0e78e..be45afe 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,13 @@ "scripts": { "test": "mocha tests", "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": { "@types/yargs": "^17.0.32", + "esbuild": "^0.20.2", "mocha": "^10.2.0", "ts-node": "^10.9.2", - "esbuild": "^0.20.2", "typescript": "^5.3.3" }, "dependencies": {