HL7-1: add comments to code

This commit is contained in:
Markus Thielker 2025-07-30 13:06:17 +02:00
parent ad48df97e5
commit 3898715915
4 changed files with 37 additions and 8 deletions

View file

@ -1,23 +1,25 @@
// define available configurations
interface Config {
port: number;
prefixes: string[];
poolSize: number;
}
// construct config object from environment variables
export const config: Config = {
port: parseInt(process.env.PORT || '8080', 10),
prefixes: (process.env.PREFIXES || 'STA').split(","),
poolSize: parseInt(process.env.POOL_SIZE || '100', 10),
};
// validate config
if (isNaN(config.port) || config.port < 1024 || config.port > 49151) {
throw new Error('Invalid PORT environment variable (1024 - 49151)');
}
if (config.prefixes.length === 0) {
throw new Error('Invalid PREFIXES environment variable (length > 1)');
}
if (isNaN(config.poolSize) || config.poolSize < config.prefixes.length) {
throw new Error('Invalid PORT environment variable (poolSize >= prefix.length)');
throw new Error('Invalid pool size environment variable (poolSize >= prefix.length)');
}

View file

@ -17,6 +17,7 @@ for (const prefix of config.prefixes) {
}
}
// mixes the array randomly to avoid getting the same ID reassigned
function shake(arr: any[]) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
@ -71,9 +72,11 @@ wss.on('connection', (ws) => {
console.log(`Client connected. Assigning ID: ${stationId}`);
// listen for messages and defined message handling
ws.on('message', (message) => {
try {
// parse every message to type Message
const parsedMessage: Message = JSON.parse(message.toString());
console.log(`Received message from ${stationId}:`, parsedMessage);
@ -81,6 +84,7 @@ wss.on('connection', (ws) => {
if (parsedMessage.type === MessageType.send_hl7v2) {
const { message } = parsedMessage.payload;
// TODO: validate message
// get sender and recipient ID
@ -98,6 +102,7 @@ wss.on('connection', (ws) => {
// Find the recipient's WebSocket connection in our map.
const recipientWs = clients.get(recipientId);
// check if recipient exists and is connected
if (recipientWs && recipientWs.readyState === WebSocket.OPEN) {
// The recipient is connected. Forward the message.
@ -136,6 +141,7 @@ wss.on('connection', (ws) => {
if (stationId) {
availableIds.push(stationId);
}
// shake IDs to avoid getting the same ID reassigned
availableIds = shake(availableIds);
});