From 16b1649ff62fd87a4dda5d2e1c68941c864c5da4 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Sun, 23 Jun 2024 17:35:05 -0400 Subject: [PATCH] feat: add debug mod --- mods/README.md | 13 +++ .../kdmod/CustomPuterService.js | 60 +++++++++++++ mods/mods_available/kdmod/README.md | 7 ++ mods/mods_available/kdmod/gui/main.js | 85 +++++++++++++++++++ mods/mods_available/kdmod/module.js | 8 ++ mods/mods_available/kdmod/package.json | 12 +++ mods/mods_enabled/.gitignore | 2 + 7 files changed, 187 insertions(+) create mode 100644 mods/README.md create mode 100644 mods/mods_available/kdmod/CustomPuterService.js create mode 100644 mods/mods_available/kdmod/README.md create mode 100644 mods/mods_available/kdmod/gui/main.js create mode 100644 mods/mods_available/kdmod/module.js create mode 100644 mods/mods_available/kdmod/package.json create mode 100644 mods/mods_enabled/.gitignore diff --git a/mods/README.md b/mods/README.md new file mode 100644 index 00000000..61796e50 --- /dev/null +++ b/mods/README.md @@ -0,0 +1,13 @@ +# Puter Mods + +A list of Puter mods which may be expanded in the future. + +**Contributions of new mods are welcome.** + +## kdmod + +- **location:** [./kdmod](./kdmod) +- **description:** + > "kernel dev mod"; specifically for the devex needs of + > GitHub user KernelDeimos and provided in case anyone else + > finds it of any use. diff --git a/mods/mods_available/kdmod/CustomPuterService.js b/mods/mods_available/kdmod/CustomPuterService.js new file mode 100644 index 00000000..bdcc7dd8 --- /dev/null +++ b/mods/mods_available/kdmod/CustomPuterService.js @@ -0,0 +1,60 @@ +const path = require('path'); + +class CustomPuterService extends use.Service { + async _init () { + const svc_commands = this.services.get('commands'); + this._register_commands(svc_commands); + + const svc_puterHomepage = this.services.get('puter-homepage'); + svc_puterHomepage.register_script('/custom-gui/main.js'); + } + ['__on_install.routes'] (_, { app }) { + const require = this.require; + const express = require('express'); + const path_ = require('path'); + + app.use('/custom-gui', + express.static(path.join(__dirname, 'gui'))); + } + async ['__on_boot.consolidation'] () { + const then = Date.now(); + this.tod_widget = () => { + const s = 5 - Math.floor( + (Date.now() - then) / 1000); + const lines = [ + "\x1B[36;1mKDMOD ENABLED\x1B[0m" + + ` (👁️ ${s}s)` + ]; + // It would be super cool to be able to use this here + // surrounding_box('33;1', lines); + return lines; + } + + const svc_devConsole = this.services.get('dev-console', { optional: true }); + if ( ! svc_devConsole ) return; + svc_devConsole.add_widget(this.tod_widget); + + setTimeout(() => { + svc_devConsole.remove_widget(this.tod_widget); + }, 5000) + } + + _register_commands (commands) { + commands.registerCommands('o', [ + { + id: 'k', + description: '', + handler: async (_, log) => { + const svc_devConsole = this.services.get('dev-console', { optional: true }); + if ( ! svc_devConsole ) return; + svc_devConsole.remove_widget(this.tod_widget); + const lines = this.tod_widget(); + for ( const line of lines ) log.log(line); + this.tod_widget = null; + } + } + ]); + } +} + +module.exports = { CustomPuterService }; \ No newline at end of file diff --git a/mods/mods_available/kdmod/README.md b/mods/mods_available/kdmod/README.md new file mode 100644 index 00000000..d7fa3de3 --- /dev/null +++ b/mods/mods_available/kdmod/README.md @@ -0,0 +1,7 @@ +# Kernel Dev Mod + +This mod makes testing and debugging easier. + +## Current Features: +- A service-script adds `reqex` to the `window` object in the client, + which contains a bunch of example requests to internal API endpoints. diff --git a/mods/mods_available/kdmod/gui/main.js b/mods/mods_available/kdmod/gui/main.js new file mode 100644 index 00000000..ce69189d --- /dev/null +++ b/mods/mods_available/kdmod/gui/main.js @@ -0,0 +1,85 @@ +const request_examples = [ + { + name: 'entity storage app read', + fetch: async (args) => { + return await fetch(`${window.api_origin}/drivers/call`, { + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${puter.authToken}`, + }, + body: JSON.stringify({ + interface: 'puter-apps', + method: 'read', + args, + }), + method: "POST", + }); + }, + out: async (resp) => { + const data = await resp.json(); + if ( ! data.success ) return data; + return data.result; + }, + exec: async function exec (...a) { + const resp = await this.fetch(...a); + return await this.out(resp); + }, + }, + { + name: 'entity storage app select all', + fetch: async () => { + return await fetch(`${window.api_origin}/drivers/call`, { + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${puter.authToken}`, + }, + body: JSON.stringify({ + interface: 'puter-apps', + method: 'select', + args: { predicate: [] }, + }), + method: "POST", + }); + }, + out: async (resp) => { + const data = await resp.json(); + if ( ! data.success ) return data; + return data.result; + }, + exec: async function exec (...a) { + const resp = await this.fetch(...a); + return await this.out(resp); + }, + }, + { + name: 'grant permission from a user to a user', + fetch: async (user, perm) => { + return await fetch(`${window.api_origin}/auth/grant-user-user`, { + "headers": { + "Content-Type": "application/json", + "Authorization": `Bearer ${puter.authToken}`, + }, + "body": JSON.stringify({ + target_username: user, + permission: perm, + }), + "method": "POST", + }); + }, + out: async (resp) => { + const data = await resp.json(); + return data; + }, + exec: async function exec (...a) { + const resp = await this.fetch(...a); + return await this.out(resp); + }, + } +]; + +globalThis.reqex = request_examples; + +globalThis.service_script(api => { + api.on_ready(() => { + }); +}); diff --git a/mods/mods_available/kdmod/module.js b/mods/mods_available/kdmod/module.js new file mode 100644 index 00000000..1079827a --- /dev/null +++ b/mods/mods_available/kdmod/module.js @@ -0,0 +1,8 @@ +module.exports = class BillingModule extends use.Module { + install (context) { + const services = context.get('services'); + + const { CustomPuterService } = require('./CustomPuterService.js'); + services.registerService('__custom-puter', CustomPuterService); + } +} diff --git a/mods/mods_available/kdmod/package.json b/mods/mods_available/kdmod/package.json new file mode 100644 index 00000000..258b91ed --- /dev/null +++ b/mods/mods_available/kdmod/package.json @@ -0,0 +1,12 @@ +{ + "name": "custom-puter-mod", + "version": "1.0.0", + "description": "", + "main": "module.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/mods/mods_enabled/.gitignore b/mods/mods_enabled/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/mods/mods_enabled/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore