Refactoring

This commit is contained in:
Zef Hemel
2022-03-25 12:03:06 +01:00
parent b51730d33f
commit 08e6c3bad8
52 changed files with 796 additions and 326 deletions
+19 -1
View File
@@ -46,7 +46,7 @@ export class SocketServer {
public async init() {
const indexApi = new IndexApi(this.rootPath);
await this.registerApi("index", indexApi);
this.system.registerSyscalls(pageIndexSyscalls(indexApi.db));
this.system.registerSyscalls("indexer", [], pageIndexSyscalls(indexApi.db));
await this.registerApi(
"page",
new PageApi(
@@ -118,6 +118,24 @@ export class SocketServer {
});
}
onCall(
"invokeFunction",
(plugName: string, name: string, ...args: any[]): Promise<any> => {
let plug = this.system.loadedPlugs.get(plugName);
if (!plug) {
throw new Error(`Plug ${plugName} not loaded`);
}
console.log(
"Invoking function",
name,
"for plug",
plugName,
"as requested over socket"
);
return plug.invoke(name, args);
}
);
console.log("Sending the sytem to the client");
socket.emit("loadSystem", this.system.toJSON());
});
+9 -8
View File
@@ -36,12 +36,13 @@ export class IndexApi implements ApiProvider {
api() {
const syscalls = pageIndexSyscalls(this.db);
const nullContext = { plug: null };
return {
clearPageIndexForPage: async (
clientConn: ClientConnection,
page: string
) => {
return syscalls["indexer.clearPageIndexForPage"](page);
return syscalls.clearPageIndexForPage(nullContext, page);
},
set: async (
clientConn: ClientConnection,
@@ -49,41 +50,41 @@ export class IndexApi implements ApiProvider {
key: string,
value: any
) => {
return syscalls["indexer.set"](page, key, value);
return syscalls.set(nullContext, page, key, value);
},
get: async (clientConn: ClientConnection, page: string, key: string) => {
return syscalls["indexer.get"](page, key);
return syscalls.get(nullContext, page, key);
},
delete: async (
clientConn: ClientConnection,
page: string,
key: string
) => {
return syscalls["indexer.delete"](page, key);
return syscalls.delete(nullContext, page, key);
},
scanPrefixForPage: async (
clientConn: ClientConnection,
page: string,
prefix: string
) => {
return syscalls["indexer.scanPrefixForPage"](page, prefix);
return syscalls.scanPrefixForPage(nullContext, page, prefix);
},
scanPrefixGlobal: async (
clientConn: ClientConnection,
prefix: string
) => {
return syscalls["indexer.scanPrefixGlobal"](prefix);
return syscalls.scanPrefixGlobal(nullContext, prefix);
},
deletePrefixForPage: async (
clientConn: ClientConnection,
page: string,
prefix: string
) => {
return syscalls["indexer.deletePrefixForPage"](page, prefix);
return syscalls.deletePrefixForPage(nullContext, page, prefix);
},
clearPageIndex: async (clientConn: ClientConnection) => {
return syscalls["indexer.clearPageIndex"]();
return syscalls.clearPageIndex(nullContext);
},
};
}
+5 -1
View File
@@ -11,6 +11,7 @@ import { stat } from "fs/promises";
import { Cursor, cursorEffect } from "../webapp/cursorEffect";
import { SilverBulletHooks } from "../common/manifest";
import { System } from "../plugbox/system";
import { EventFeature } from "../plugbox/feature/event";
export class PageApi implements ApiProvider {
openPages: Map<string, Page>;
@@ -18,6 +19,7 @@ export class PageApi implements ApiProvider {
rootPath: string;
connectedSockets: Set<Socket>;
private system: System<SilverBulletHooks>;
private eventFeature: EventFeature;
constructor(
rootPath: string,
@@ -30,6 +32,8 @@ export class PageApi implements ApiProvider {
this.openPages = openPages;
this.connectedSockets = connectedSockets;
this.system = system;
this.eventFeature = new EventFeature();
system.addFeature(this.eventFeature);
}
async init(): Promise<void> {
@@ -222,7 +226,7 @@ export class PageApi implements ApiProvider {
);
await this.flushPageToDisk(pageName, page);
await this.system.dispatchEvent("page:index", {
await this.eventFeature.dispatchEvent("page:index", {
name: pageName,
text: page.text.sliceString(0),
});
+4 -4
View File
@@ -8,7 +8,7 @@ import { SilverBulletHooks } from "../common/manifest";
import { ExpressServer } from "./express_server";
import { DiskPlugLoader } from "../plugbox/plug_loader";
import { NodeCronFeature } from "../plugbox/feature/node_cron";
import shellSyscalls from "./syscalls/shell";
import shellSyscalls from "../plugbox/syscall/shell.node";
import { System } from "../plugbox/system";
let args = yargs(hideBin(process.argv))
@@ -53,9 +53,9 @@ expressServer
`${__dirname}/../../plugs/dist`
);
await plugLoader.loadPlugs();
plugLoader.watcher();
system.registerSyscalls(shellSyscalls(pagesPath));
system.addFeature(new NodeCronFeature());
plugLoader.watcher();
system.registerSyscalls("shell", ["shell"], shellSyscalls(pagesPath));
system.addFeature(new NodeCronFeature());
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
+13 -12
View File
@@ -1,4 +1,5 @@
import { Knex } from "knex";
import { SysCallMapping } from "../../plugbox/system";
type IndexItem = {
page: string;
@@ -11,12 +12,12 @@ export type KV = {
value: any;
};
export default function (db: Knex) {
const apiObj = {
"indexer.clearPageIndexForPage": async (page: string) => {
export default function (db: Knex): SysCallMapping {
const apiObj: SysCallMapping = {
clearPageIndexForPage: async (ctx, page: string) => {
await db<IndexItem>("page_index").where({ page }).del();
},
"indexer.set": async (page: string, key: string, value: any) => {
set: async (ctx, page: string, key: string, value: any) => {
let changed = await db<IndexItem>("page_index")
.where({ page, key })
.update("value", JSON.stringify(value));
@@ -28,12 +29,12 @@ export default function (db: Knex) {
});
}
},
"indexer.batchSet": async (page: string, kvs: KV[]) => {
batchSet: async (ctx, page: string, kvs: KV[]) => {
for (let { key, value } of kvs) {
await apiObj["indexer.set"](page, key, value);
await apiObj.set(ctx, page, key, value);
}
},
"indexer.get": async (page: string, key: string) => {
get: async (ctx, page: string, key: string) => {
let result = await db<IndexItem>("page_index")
.where({ page, key })
.select("value");
@@ -43,10 +44,10 @@ export default function (db: Knex) {
return null;
}
},
"indexer.delete": async (page: string, key: string) => {
delete: async (ctx, page: string, key: string) => {
await db<IndexItem>("page_index").where({ page, key }).del();
},
"indexer.scanPrefixForPage": async (page: string, prefix: string) => {
scanPrefixForPage: async (ctx, page: string, prefix: string) => {
return (
await db<IndexItem>("page_index")
.where({ page })
@@ -58,7 +59,7 @@ export default function (db: Knex) {
value: JSON.parse(value),
}));
},
"indexer.scanPrefixGlobal": async (prefix: string) => {
scanPrefixGlobal: async (ctx, prefix: string) => {
return (
await db<IndexItem>("page_index")
.andWhereLike("key", `${prefix}%`)
@@ -69,13 +70,13 @@ export default function (db: Knex) {
value: JSON.parse(value),
}));
},
"indexer.deletePrefixForPage": async (page: string, prefix: string) => {
deletePrefixForPage: async (ctx, page: string, prefix: string) => {
return db<IndexItem>("page_index")
.where({ page })
.andWhereLike("key", `${prefix}%`)
.del();
},
"indexer.clearPageIndex": async () => {
clearPageIndex: async () => {
return db<IndexItem>("page_index").del();
},
};
-15
View File
@@ -1,15 +0,0 @@
import { promisify } from "util";
import { execFile } from "child_process";
const execFilePromise = promisify(execFile);
export default function (cwd: string) {
return {
"shell.run": async (cmd: string, args: string[]) => {
let { stdout, stderr } = await execFilePromise(cmd, args, {
cwd: cwd,
});
return { stdout, stderr };
},
};
}