From 50a1d658e82a01363fa74aa85eb541469270b8dc Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 19 Apr 2023 15:57:22 -0500 Subject: [PATCH] Only read version tag files once on startup (#174) We already read it once for the `/health-check` endpoint and cached the response but this way we can use `getVersionTags()` everywhere without worrying about it. Also, it's no longer `async` so we can use it in things like Express route paths and CDN asset tags more easily. --- server/lib/get-version-tags.js | 34 ++++++++++++++++----------------- server/routes/install-routes.js | 2 +- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/server/lib/get-version-tags.js b/server/lib/get-version-tags.js index 2d925f37..ebb49049 100644 --- a/server/lib/get-version-tags.js +++ b/server/lib/get-version-tags.js @@ -2,33 +2,31 @@ const assert = require('assert'); const path = require('path'); -const { readFile } = require('fs').promises; +const fs = require('fs'); const packageInfo = require('../../package.json'); assert(packageInfo.version); -async function getVersionTags() { - let commit; - let version; - let versionDate; - let packageVersion = packageInfo.version; +const packageVersion = packageInfo.version; + +function readFileSync(path) { try { - [commit, version, versionDate] = await Promise.all([ - readFile(path.join(__dirname, '../../dist/GIT_COMMIT'), 'utf8'), - readFile(path.join(__dirname, '../../dist/VERSION'), 'utf8'), - readFile(path.join(__dirname, '../../dist/VERSION_DATE'), 'utf8'), - ]); + return fs.readFileSync(path, 'utf8'); } catch (err) { - console.warn('Unable to read version tags', err); - commit = 'Not specified'; - version = 'Not specified'; - versionDate = 'Not specified'; + console.warn(`Unable to read version tags path=${path}`, err); + return null; } +} + +const commit = readFileSync(path.join(__dirname, '../../dist/GIT_COMMIT'), 'utf8').trim(); +const version = readFileSync(path.join(__dirname, '../../dist/VERSION'), 'utf8').trim(); +const versionDate = readFileSync(path.join(__dirname, '../../dist/VERSION_DATE'), 'utf8').trim(); +function getVersionTags() { return { - commit: commit.trim(), - version: version.trim(), - versionDate: versionDate.trim(), + commit, + version, + versionDate, packageVersion, }; } diff --git a/server/routes/install-routes.js b/server/routes/install-routes.js index b147aa34..fac7e2e7 100644 --- a/server/routes/install-routes.js +++ b/server/routes/install-routes.js @@ -25,7 +25,7 @@ function installRoutes(app) { identifyRoute('health-check'), asyncHandler(async function (req, res) { if (!healthCheckResponse) { - const versionTags = await getVersionTags(); + const versionTags = getVersionTags(); const responseObject = { ok: true, ...versionTags,