From 2453baa234be7ab3e447ffaba15e26a425086213 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Wed, 4 Dec 2024 11:27:54 -0500 Subject: [PATCH] dev: make a module where utilities will go --- src/backend/exports.js | 2 + src/backend/src/modules/core/Core2Module.js | 16 +++++ src/backend/src/modules/core/lib/__lib__.js | 3 + src/backend/src/modules/core/lib/string.js | 57 ++++++++++++++++++ src/backend/src/modules/web/README.md | 1 - .../src/modules/web/WebServerService.js | 9 ++- src/backend/src/services/BaseService.js | 9 ++- src/backend/src/services/Container.js | 5 +- src/backend/src/util/strutil.js | 59 +------------------ 9 files changed, 98 insertions(+), 63 deletions(-) create mode 100644 src/backend/src/modules/core/Core2Module.js create mode 100644 src/backend/src/modules/core/lib/__lib__.js create mode 100644 src/backend/src/modules/core/lib/string.js diff --git a/src/backend/exports.js b/src/backend/exports.js index 3367710e2..5023c713a 100644 --- a/src/backend/exports.js +++ b/src/backend/exports.js @@ -29,6 +29,7 @@ const { TestDriversModule } = require("./src/modules/test-drivers/TestDriversMod const { PuterAIModule } = require("./src/modules/puterai/PuterAIModule.js"); const { BroadcastModule } = require("./src/modules/broadcast/BroadcastModule.js"); const { WebModule } = require("./src/modules/web/WebModule.js"); +const { Core2Module } = require("./src/modules/core/Core2Module.js"); module.exports = { @@ -46,6 +47,7 @@ module.exports = { EssentialModules: [ CoreModule, + Core2Module, WebModule, ], diff --git a/src/backend/src/modules/core/Core2Module.js b/src/backend/src/modules/core/Core2Module.js new file mode 100644 index 000000000..d011b30b1 --- /dev/null +++ b/src/backend/src/modules/core/Core2Module.js @@ -0,0 +1,16 @@ +const { AdvancedBase } = require("@heyputer/putility"); + +class Core2Module extends AdvancedBase { + async install (context) { + // === LIBS === // + const useapi = context.get('useapi'); + useapi.def('std', require('./lib/__lib__.js'), { assign: true }); + + // === SERVICES === // + // const services = context.get('services'); + } +} + +module.exports = { + Core2Module, +}; diff --git a/src/backend/src/modules/core/lib/__lib__.js b/src/backend/src/modules/core/lib/__lib__.js new file mode 100644 index 000000000..f180c157a --- /dev/null +++ b/src/backend/src/modules/core/lib/__lib__.js @@ -0,0 +1,3 @@ +module.exports = { + string: require('./string.js'), +}; diff --git a/src/backend/src/modules/core/lib/string.js b/src/backend/src/modules/core/lib/string.js new file mode 100644 index 000000000..f8885729c --- /dev/null +++ b/src/backend/src/modules/core/lib/string.js @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2024 Puter Technologies Inc. + * + * This file is part of Puter. + * + * Puter is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +// Convenience function for quoting strings in error messages. +// Turns a string like this: some`value` +// Into a string like this: `some\`value\`` +const quot = (str) => { + if ( str === undefined ) return '[undefined]'; + if ( str === null ) return '[null]'; + if ( typeof str === 'function' ) return '[function]'; + if ( typeof str === 'object' ) return '[object]'; + if ( typeof str === 'number' ) return '(' + str + ')'; + + str = '' + str; + + str = str.replace(/["`]/g, m => m === '"' ? "`" : '"'); + str = JSON.stringify('' + str); + str = str.replace(/["`]/g, m => m === '"' ? "`" : '"'); + return str; +} + +const osclink = (url, text) => { + if ( ! text ) text = url; + return `\x1B]8;;${url}\x1B\\${text}\x1B]8;;\x1B\\`; +} + +const format_as_usd = (amount) => { + if ( amount < 0.01 ) { + if ( amount < 0.00001 ) { + // scientific notation + return '$' + amount.toExponential(2); + } + return '$' + amount.toFixed(5); + } + return '$' + amount.toFixed(2); +} + +module.exports = { + quot, + osclink, + format_as_usd, +}; diff --git a/src/backend/src/modules/web/README.md b/src/backend/src/modules/web/README.md index 726e41e91..d2eb27c36 100644 --- a/src/backend/src/modules/web/README.md +++ b/src/backend/src/modules/web/README.md @@ -57,7 +57,6 @@ extension. **Imports:** - `../../services/BaseService` (use.BaseService) -- `../../api/eggspress.js` - `../../util/context.js` - `../../services/BaseService.js` - `../../config.js` diff --git a/src/backend/src/modules/web/WebServerService.js b/src/backend/src/modules/web/WebServerService.js index bbd393c96..d50a8ddc4 100644 --- a/src/backend/src/modules/web/WebServerService.js +++ b/src/backend/src/modules/web/WebServerService.js @@ -18,7 +18,7 @@ * along with this program. If not, see . */ const express = require('express'); -const eggspress = require("../../api/eggspress.js"); +const eggspress = require("./lib/eggspress.js"); const { Context, ContextExpressMiddleware } = require("../../util/context.js"); const BaseService = require("../../services/BaseService.js"); @@ -27,7 +27,6 @@ const https = require('https') var http = require('http'); const fs = require('fs'); const auth = require('../../middleware/auth.js'); -const { osclink } = require('../../util/strutil.js'); const { surrounding_box, es_import_promise } = require('../../fun/dev-console-ui-utils.js'); const relative_require = require; @@ -38,6 +37,10 @@ const relative_require = require; * It also validates the host header and IP addresses to prevent security vulnerabilities. */ class WebServerService extends BaseService { + static USE = { + strutil: 'std.string', + } + static MODULES = { https: require('https'), http: require('http'), @@ -198,7 +201,7 @@ class WebServerService extends BaseService { */ this.startup_widget = () => { - const link = `\x1B[34;1m${osclink(url)}\x1B[0m`; + const link = `\x1B[34;1m${this.strutil.osclink(url)}\x1B[0m`; const lines = [ `Puter is now live at: ${link}`, `Type web:dismiss to un-stick this message`, diff --git a/src/backend/src/services/BaseService.js b/src/backend/src/services/BaseService.js index 5e7e676e1..2e74b0d39 100644 --- a/src/backend/src/services/BaseService.js +++ b/src/backend/src/services/BaseService.js @@ -40,7 +40,7 @@ const NOOP = async () => {}; */ class BaseService extends concepts.Service { constructor (service_resources, ...a) { - const { services, config, my_config, name, args } = service_resources; + const { services, config, my_config, name, args, context } = service_resources; super(service_resources, ...a); this.args = args; @@ -48,6 +48,7 @@ class BaseService extends concepts.Service { this.services = services; this.config = my_config; this.global_config = config; + this.context = context; if ( this.global_config.server_id === '' ) { this.global_config.server_id = 'local'; @@ -63,6 +64,12 @@ class BaseService extends concepts.Service { * @returns {Promise} A promise that resolves when initialization is complete. */ async construct () { + console.log('CLASS', this.constructor.name); + const useapi = this.context.get('useapi'); + const use = this._get_merged_static_object('USE'); + for ( const [key, value] of Object.entries(use) ) { + this[key] = useapi.use(value); + } await (this._construct || NOOP).call(this, this.args); } diff --git a/src/backend/src/services/Container.js b/src/backend/src/services/Container.js index 6ac89d4f1..c3500be8c 100644 --- a/src/backend/src/services/Container.js +++ b/src/backend/src/services/Container.js @@ -77,7 +77,10 @@ class Container { const my_config = config.services?.[name] || {}; const instance = cls.getInstance ? cls.getInstance({ services: this, config, my_config, name, args }) - : new cls({ services: this, config, my_config, name, args }) ; + : new cls({ + context: Context.get(), + services: this, config, my_config, name, args + }) ; this.instances_[name] = instance; if ( this.modname_ ) { diff --git a/src/backend/src/util/strutil.js b/src/backend/src/util/strutil.js index 4701fc475..2388e63c0 100644 --- a/src/backend/src/util/strutil.js +++ b/src/backend/src/util/strutil.js @@ -1,57 +1,2 @@ -/* - * Copyright (C) 2024 Puter Technologies Inc. - * - * This file is part of Puter. - * - * Puter is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published - * by the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -// Convenience function for quoting strings in error messages. -// Turns a string like this: some`value` -// Into a string like this: `some\`value\`` -const quot = (str) => { - if ( str === undefined ) return '[undefined]'; - if ( str === null ) return '[null]'; - if ( typeof str === 'function' ) return '[function]'; - if ( typeof str === 'object' ) return '[object]'; - if ( typeof str === 'number' ) return '(' + str + ')'; - - str = '' + str; - - str = str.replace(/["`]/g, m => m === '"' ? "`" : '"'); - str = JSON.stringify('' + str); - str = str.replace(/["`]/g, m => m === '"' ? "`" : '"'); - return str; -} - -const osclink = (url, text) => { - if ( ! text ) text = url; - return `\x1B]8;;${url}\x1B\\${text}\x1B]8;;\x1B\\`; -} - -const format_as_usd = (amount) => { - if ( amount < 0.01 ) { - if ( amount < 0.00001 ) { - // scientific notation - return '$' + amount.toExponential(2); - } - return '$' + amount.toFixed(5); - } - return '$' + amount.toFixed(2); -} - -module.exports = { - quot, - osclink, - format_as_usd, -}; \ No newline at end of file +// This file is a legacy alias +module.exports = require('../modules/core/lib/string.js');