Jelajahi Sumber

feat: add debug mod

KernelDeimos 10 bulan lalu
induk
melakukan
16b1649ff6

+ 13 - 0
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.

+ 60 - 0
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 };

+ 7 - 0
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.

+ 85 - 0
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(() => {
+    });
+});

+ 8 - 0
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);
+    }
+}

+ 12 - 0
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"
+}

+ 2 - 0
mods/mods_enabled/.gitignore

@@ -0,0 +1,2 @@
+*
+!.gitignore