From c3a4223bbf80a023bf509ab199e1c985959a1ef6 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Thu, 4 Apr 2024 22:10:56 -0400 Subject: [PATCH] Add service to check EOL date of node --- packages/backend/src/SelfhostedModule.js | 3 + .../services/ComplainAboutVersionsService.js | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 packages/backend/src/services/ComplainAboutVersionsService.js diff --git a/packages/backend/src/SelfhostedModule.js b/packages/backend/src/SelfhostedModule.js index de6bf0fd..490a6de0 100644 --- a/packages/backend/src/SelfhostedModule.js +++ b/packages/backend/src/SelfhostedModule.js @@ -6,6 +6,9 @@ class SelfhostedModule extends AdvancedBase { const DefaultUserService = require('./services/DefaultUserService'); services.registerService('__default-user', DefaultUserService); + + const ComplainAboutVersionsService = require('./services/ComplainAboutVersionsService'); + services.registerService('complain-about-versions', ComplainAboutVersionsService); } } diff --git a/packages/backend/src/services/ComplainAboutVersionsService.js b/packages/backend/src/services/ComplainAboutVersionsService.js new file mode 100644 index 00000000..4a65ba49 --- /dev/null +++ b/packages/backend/src/services/ComplainAboutVersionsService.js @@ -0,0 +1,87 @@ +const { BaseService } = require("../../exports"); +const { surrounding_box } = require("../fun/dev-console-ui-utils"); + +class ComplainAboutVersionsService extends BaseService { + static DESCRIPTION = ` + This service doesn't mandate a specific version of node.js, + but it will complain (create a sticky notification in the + dev console) if you're using something that's past EOL. + + This is mostly just for fun, because it feels cool when the + system calls people out for using old versions of node. + That said, maybe one day we'll come across some nuanced error + that only happens an a recently EOL'd node version. + `; + + static MODULES = { + axios: require('axios'), + } + + async _init () { + const eol_data = await this.get_eol_data_(); + + const [major] = process.versions.node.split('.'); + const current_version_data = eol_data.find( + ({ cycle }) => cycle === major + ); + + if ( ! current_version_data ) { + this.log.warn( + `failed to check ${current_version} in the EOL database` + ); + return; + } + + const eol_date = new Date(current_version_data.eol); + const cur_date_obj = new Date(); + + if ( cur_date_obj < eol_date ) { + this.log.info('node.js version looks good'); + return; + } + + let timeago = (() => { + let years = cur_date_obj.getFullYear() - eol_date.getFullYear(); + let months = cur_date_obj.getMonth() - eol_date.getMonth(); + let days = cur_date_obj.getDate() - eol_date.getDate(); + + let str = ''; + while ( years > 0 ) { + years -= 1; + months += 12; + } + if ( months > 0 ) { + str += `at least ${months} month${months > 1 ? 's' : ''}`; + } else { + str += `a few days`; + } + return str; + })(); + + const widget_lines = []; + widget_lines.push( + `Node.js version ${major} is past EOL by ${timeago};`, + `Everything should work, but you should still upgrade.`, + ); + surrounding_box('31;1', widget_lines); + + const svc_devConsole = this.services.get('dev-console'); + svc_devConsole.add_widget(() => widget_lines); + } + + async get_eol_data_ () { + const require = this.require; + const axios = require('axios'); + const url = 'https://endoflife.date/api/nodejs.json' + let data; + try { + ({ data } = await axios.get(url)); + return data; + } catch (e) { + this.log.error(e); + return []; + } + } +} + +module.exports = ComplainAboutVersionsService;