mirror of
https://github.com/HeyPuter/puter.git
synced 2026-08-28 17:07:16 +00:00
dev: make a module where utilities will go
This commit is contained in:
@@ -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,
|
||||
],
|
||||
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
string: require('./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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// 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,
|
||||
};
|
||||
@@ -57,7 +57,6 @@ extension.
|
||||
|
||||
**Imports:**
|
||||
- `../../services/BaseService` (use.BaseService)
|
||||
- `../../api/eggspress.js`
|
||||
- `../../util/context.js`
|
||||
- `../../services/BaseService.js`
|
||||
- `../../config.js`
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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`,
|
||||
|
||||
@@ -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<void>} 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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_ ) {
|
||||
|
||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
// 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,
|
||||
};
|
||||
// This file is a legacy alias
|
||||
module.exports = require('../modules/core/lib/string.js');
|
||||
|
||||
Reference in New Issue
Block a user