EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
agentVersionTxt.js
Go to the documentation of this file.
1/**
2 * @file routes/agentVersionTxt.js
3 * @module routes/agentVersionTxt
4 * @description
5 * Agent version text file endpoint for update checks.
6 * Serves plain text version number from distribution folder for agent auto-update checks.
7 *
8 * **Core Features:**
9 * - Unauthenticated endpoint for agent update checks
10 * - Supports external and local distribution paths
11 * - Returns plain text version string (e.g., "1.0.12")
12 * - Used by agents to compare current version with latest
13 *
14 * **Path Resolution:**
15 * - Primary: `/opt/apps/IBG_HUB_dist/agent/version.txt`
16 * - Fallback: `../agent/dist/version.txt` (development)
17 * @requires express
18 * @requires fs
19 * @requires path
20 * @author RMM-PSA Development Team
21 * @date 2026-03-12
22 * @since 1.0.0
23 */
24const express = require('express');
25const router = express.Router();
26const fs = require('fs');
27const path = require('path');
28
29/**
30 * Resolve agent distribution path with fallback.
31 * Checks external production path first, then local development path.
32 * @param {string} relPath Relative path within distribution folder
33 * @returns {string | null} Resolved absolute path, or null if not found
34 * @private
35 */
36function resolveDistPath(relPath) {
37 const externalPath = path.resolve('/opt/apps/IBG_HUB_dist/agent', relPath);
38 const localPath = path.resolve(__dirname, '../agent/dist', relPath);
39 if (fs.existsSync(externalPath)) return externalPath;
40 if (fs.existsSync(localPath)) return localPath;
41 return null;
42}
43
44/**
45 * @api {get} /agent/version.txt Get agent version (text)
46 * @apiName GetAgentVersionTxt
47 * @apiGroup AgentUpdate
48 * @apiDescription
49 * Retrieve current agent version as plain text file.
50 * Used by agents for automated update checks. No authentication required.
51 * Resolves from production or development distribution paths.
52 * @apiSuccess {string} version Plain text version string (e.g., "1.0.12")
53 * @apiError (404) {String} error="version.txt not found" Version file missing
54 * @apiError (500) {String} error="Failed to read version.txt" File read error
55 * @apiExample {curl} Example usage:
56 * curl -X GET https://api.example.com/agent/version.txt
57 * @apiSuccessExample {text} Success-Response:
58 * HTTP/1.1 200 OK
59 * Content-Type: text/plain
60 *
61 * 1.0.12
62 */
63router.get('/version.txt', (req, res) => {
64 const versionPath = resolveDistPath('version.txt');
65 if (!versionPath || !fs.existsSync(versionPath)) {
66 return res.status(404).send('version.txt not found');
67 }
68 try {
69 const version = fs.readFileSync(versionPath, 'utf8').trim();
70 res.type('text/plain').send(version);
71 } catch (err) {
72 console.error('[AgentVersion] Failed to read version.txt:', err);
73 res.status(500).send('Failed to read version.txt');
74 }
75});
76
77module.exports = router;