Skip to content

Commit 16b1649

Browse files
committed
feat: add debug mod
1 parent a094ae5 commit 16b1649

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed

mods/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Puter Mods
2+
3+
A list of Puter mods which may be expanded in the future.
4+
5+
**Contributions of new mods are welcome.**
6+
7+
## kdmod
8+
9+
- **location:** [./kdmod](./kdmod)
10+
- **description:**
11+
> "kernel dev mod"; specifically for the devex needs of
12+
> GitHub user KernelDeimos and provided in case anyone else
13+
> finds it of any use.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const path = require('path');
2+
3+
class CustomPuterService extends use.Service {
4+
async _init () {
5+
const svc_commands = this.services.get('commands');
6+
this._register_commands(svc_commands);
7+
8+
const svc_puterHomepage = this.services.get('puter-homepage');
9+
svc_puterHomepage.register_script('/custom-gui/main.js');
10+
}
11+
['__on_install.routes'] (_, { app }) {
12+
const require = this.require;
13+
const express = require('express');
14+
const path_ = require('path');
15+
16+
app.use('/custom-gui',
17+
express.static(path.join(__dirname, 'gui')));
18+
}
19+
async ['__on_boot.consolidation'] () {
20+
const then = Date.now();
21+
this.tod_widget = () => {
22+
const s = 5 - Math.floor(
23+
(Date.now() - then) / 1000);
24+
const lines = [
25+
"\x1B[36;1mKDMOD ENABLED\x1B[0m" +
26+
` (👁️ ${s}s)`
27+
];
28+
// It would be super cool to be able to use this here
29+
// surrounding_box('33;1', lines);
30+
return lines;
31+
}
32+
33+
const svc_devConsole = this.services.get('dev-console', { optional: true });
34+
if ( ! svc_devConsole ) return;
35+
svc_devConsole.add_widget(this.tod_widget);
36+
37+
setTimeout(() => {
38+
svc_devConsole.remove_widget(this.tod_widget);
39+
}, 5000)
40+
}
41+
42+
_register_commands (commands) {
43+
commands.registerCommands('o', [
44+
{
45+
id: 'k',
46+
description: '',
47+
handler: async (_, log) => {
48+
const svc_devConsole = this.services.get('dev-console', { optional: true });
49+
if ( ! svc_devConsole ) return;
50+
svc_devConsole.remove_widget(this.tod_widget);
51+
const lines = this.tod_widget();
52+
for ( const line of lines ) log.log(line);
53+
this.tod_widget = null;
54+
}
55+
}
56+
]);
57+
}
58+
}
59+
60+
module.exports = { CustomPuterService };

mods/mods_available/kdmod/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Kernel Dev Mod
2+
3+
This mod makes testing and debugging easier.
4+
5+
## Current Features:
6+
- A service-script adds `reqex` to the `window` object in the client,
7+
which contains a bunch of example requests to internal API endpoints.

mods/mods_available/kdmod/gui/main.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const request_examples = [
2+
{
3+
name: 'entity storage app read',
4+
fetch: async (args) => {
5+
return await fetch(`${window.api_origin}/drivers/call`, {
6+
headers: {
7+
"Content-Type": "application/json",
8+
"Authorization": `Bearer ${puter.authToken}`,
9+
},
10+
body: JSON.stringify({
11+
interface: 'puter-apps',
12+
method: 'read',
13+
args,
14+
}),
15+
method: "POST",
16+
});
17+
},
18+
out: async (resp) => {
19+
const data = await resp.json();
20+
if ( ! data.success ) return data;
21+
return data.result;
22+
},
23+
exec: async function exec (...a) {
24+
const resp = await this.fetch(...a);
25+
return await this.out(resp);
26+
},
27+
},
28+
{
29+
name: 'entity storage app select all',
30+
fetch: async () => {
31+
return await fetch(`${window.api_origin}/drivers/call`, {
32+
headers: {
33+
"Content-Type": "application/json",
34+
"Authorization": `Bearer ${puter.authToken}`,
35+
},
36+
body: JSON.stringify({
37+
interface: 'puter-apps',
38+
method: 'select',
39+
args: { predicate: [] },
40+
}),
41+
method: "POST",
42+
});
43+
},
44+
out: async (resp) => {
45+
const data = await resp.json();
46+
if ( ! data.success ) return data;
47+
return data.result;
48+
},
49+
exec: async function exec (...a) {
50+
const resp = await this.fetch(...a);
51+
return await this.out(resp);
52+
},
53+
},
54+
{
55+
name: 'grant permission from a user to a user',
56+
fetch: async (user, perm) => {
57+
return await fetch(`${window.api_origin}/auth/grant-user-user`, {
58+
"headers": {
59+
"Content-Type": "application/json",
60+
"Authorization": `Bearer ${puter.authToken}`,
61+
},
62+
"body": JSON.stringify({
63+
target_username: user,
64+
permission: perm,
65+
}),
66+
"method": "POST",
67+
});
68+
},
69+
out: async (resp) => {
70+
const data = await resp.json();
71+
return data;
72+
},
73+
exec: async function exec (...a) {
74+
const resp = await this.fetch(...a);
75+
return await this.out(resp);
76+
},
77+
}
78+
];
79+
80+
globalThis.reqex = request_examples;
81+
82+
globalThis.service_script(api => {
83+
api.on_ready(() => {
84+
});
85+
});

mods/mods_available/kdmod/module.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = class BillingModule extends use.Module {
2+
install (context) {
3+
const services = context.get('services');
4+
5+
const { CustomPuterService } = require('./CustomPuterService.js');
6+
services.registerService('__custom-puter', CustomPuterService);
7+
}
8+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "custom-puter-mod",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "module.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC"
12+
}

mods/mods_enabled/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)