UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

120 lines (119 loc) 4.54 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpServer = void 0; const http = __importStar(require("http")); const url = __importStar(require("url")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const events_1 = require("events"); class HttpServer extends events_1.EventEmitter { host; port; registeredPaths; server; sockets; constructor(options) { super(); this.host = options?.host ?? process.env.HTTP_HOST ?? '0.0.0.0'; this.port = options?.port ?? parseInt(process.env.HTTP_PORT ?? '80'); this.registeredPaths = new Map(); this.server = http.createServer((req, res) => { this.emit('access', req.method + ' ' + req.url); const parsedUrl = url.parse(req.url ?? ''); parsedUrl.pathname ??= ''; const requestCallback = this.registeredPaths.get(parsedUrl.pathname); if (requestCallback) { requestCallback(req, res); return; } let pathname = `./html${parsedUrl.pathname}`; const ext = path.parse(pathname).ext; const map = { '.ico': 'image/x-icon', '.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.css': 'text/css', '.png': 'image/png', '.jpg': 'image/jpeg', '.wav': 'audio/wav', '.mp3': 'audio/mpeg', '.svg': 'image/svg+xml', '.pdf': 'application/pdf', '.doc': 'application/msword', }; fs.access(pathname, fs.constants.R_OK, (err) => { if (err) { res.statusCode = 404; res.end(`File ${pathname} not found!`); this.emit('error', `File ${pathname} not found!`); return; } if (fs.statSync(pathname).isDirectory()) { pathname += `/index${ext}`; } fs.readFile(pathname, (error, data) => { if (error) { res.statusCode = 500; res.end(`Error getting the file: ${error}`); this.emit('error', `Error getting the file: ${error}`); } else { res.setHeader('Content-type', map[ext] ?? 'text/plain'); res.setHeader('Access-Control-Allow-Origin', '*'); res.end(data); } }); }); }); this.server.on('error', (err) => { this.emit('error', err); }); this.server.listen(this.port, this.host); this.sockets = {}; let idTracker = 0; this.server.on('connection', (socket) => { const socketID = idTracker++; this.sockets[socketID] = socket; socket.on('close', () => { delete this.sockets[socketID]; }); }); } getServer() { return this.server; } onRequest(pathName, callback) { this.registeredPaths.set(pathName, callback); } close() { this.server.close(); for (const key in this.sockets) { this.sockets[key]?.destroy(); } } } exports.HttpServer = HttpServer;