Cleanup and progress

This commit is contained in:
Zef Hemel
2022-03-21 15:21:34 +01:00
parent 7e591c6f44
commit a916088215
31 changed files with 707 additions and 143 deletions
+7 -1
View File
@@ -6,6 +6,8 @@ import { Server } from "socket.io";
import { SocketServer } from "./api_server";
import * as path from "path";
import * as fs from "fs";
import { SilverBulletHooks } from "../common/manifest";
import { System } from "../plugbox/runtime";
describe("Server test", () => {
let io: Server,
@@ -38,7 +40,11 @@ describe("Server test", () => {
const port = httpServer.address().port;
// @ts-ignore
clientSocket = new Client(`http://localhost:${port}`);
socketServer = new SocketServer(tmpDir, io);
socketServer = new SocketServer(
tmpDir,
io,
new System<SilverBulletHooks>()
);
clientSocket.on("connect", done);
await socketServer.init();
});
+11 -12
View File
@@ -4,9 +4,7 @@ import * as path from "path";
import { IndexApi } from "./index_api";
import { PageApi } from "./page_api";
import { System } from "../plugbox/runtime";
import { createSandbox } from "../plugbox/node_sandbox";
import { NuggetHook } from "../webapp/types";
import corePlug from "../webapp/generated/core.plug.json";
import { SilverBulletHooks } from "../common/manifest";
import pageIndexSyscalls from "./syscalls/page_index";
export class ClientConnection {
@@ -25,12 +23,16 @@ export class SocketServer {
private apis = new Map<string, ApiProvider>();
readonly rootPath: string;
private serverSocket: Server;
system: System<NuggetHook>;
system: System<SilverBulletHooks>;
constructor(rootPath: string, serverSocket: Server) {
constructor(
rootPath: string,
serverSocket: Server,
system: System<SilverBulletHooks>
) {
this.rootPath = path.resolve(rootPath);
this.serverSocket = serverSocket;
this.system = new System<NuggetHook>();
this.system = system;
}
async registerApi(name: string, apiProvider: ApiProvider) {
@@ -52,12 +54,6 @@ export class SocketServer {
)
);
let plug = await this.system.load(
"core",
corePlug,
createSandbox(this.system)
);
this.serverSocket.on("connection", (socket) => {
const clientConn = new ClientConnection(socket);
@@ -112,6 +108,9 @@ export class SocketServer {
});
});
}
console.log("Sending the sytem to the client");
socket.emit("loadSystem", this.system.toJSON());
});
}
+35
View File
@@ -0,0 +1,35 @@
import { Express } from "express";
import { System } from "../plugbox/runtime";
import { SilverBulletHooks } from "../common/manifest";
import { exposeSystem } from "../plugbox/endpoints";
import { readFile } from "fs/promises";
export class ExpressServer {
app: Express;
system: System<SilverBulletHooks>;
private rootPath: string;
constructor(
app: Express,
rootPath: string,
distDir: string,
system: System<SilverBulletHooks>
) {
this.app = app;
this.rootPath = rootPath;
this.system = system;
app.use(exposeSystem(this.system));
// Fallback, serve index.html
let cachedIndex: string | undefined = undefined;
app.get("/*", async (req, res) => {
if (!cachedIndex) {
cachedIndex = await readFile(`${distDir}/index.html`, "utf8");
}
res.status(200).header("Content-Type", "text/html").send(cachedIndex);
});
}
async init() {}
}
+17 -3
View File
@@ -10,20 +10,20 @@ import path from "path";
import { stat } from "fs/promises";
import { Cursor, cursorEffect } from "../webapp/cursorEffect";
import { System } from "../plugbox/runtime";
import { NuggetHook } from "../webapp/types";
import { SilverBulletHooks } from "../common/manifest";
export class PageApi implements ApiProvider {
openPages: Map<string, Page>;
pageStore: DiskStorage;
rootPath: string;
connectedSockets: Set<Socket>;
private system: System<NuggetHook>;
private system: System<SilverBulletHooks>;
constructor(
rootPath: string,
connectedSockets: Set<Socket>,
openPages: Map<string, Page>,
system: System<NuggetHook>
system: System<SilverBulletHooks>
) {
this.pageStore = new DiskStorage(rootPath);
this.rootPath = rootPath;
@@ -34,6 +34,20 @@ export class PageApi implements ApiProvider {
async init(): Promise<void> {
this.fileWatcher();
this.system.on({
plugUpdated: (plugName, plugDef) => {
console.log("Plug updated on disk, broadcasting to all clients");
this.connectedSockets.forEach((socket) => {
socket.emit("plugUpdated", plugName, plugDef);
});
},
plugRemoved: (plugName) => {
console.log("Plug removed on disk, broadcasting to all clients");
this.connectedSockets.forEach((socket) => {
socket.emit("plugRemoved", plugName);
});
},
});
}
broadcastCursors(page: Page) {
+28 -13
View File
@@ -1,10 +1,13 @@
import express from "express";
import { readFile } from "fs/promises";
import http from "http";
import { Server } from "socket.io";
import { SocketServer } from "./api_server";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { System } from "../plugbox/runtime";
import { SilverBulletHooks } from "../common/manifest";
import { ExpressServer } from "./express_server";
import { DiskPlugLoader } from "../plugbox/plug_loader";
let args = yargs(hideBin(process.argv))
.option("debug", {
@@ -16,8 +19,12 @@ let args = yargs(hideBin(process.argv))
})
.parse();
const pagesPath = args._[0] as string;
const app = express();
const server = http.createServer(app);
const system = new System<SilverBulletHooks>();
const io = new Server(server, {
cors: {
methods: "GET,HEAD,PUT,OPTIONS,POST,DELETE",
@@ -29,18 +36,26 @@ const port = args.port;
const distDir = `${__dirname}/../webapp`;
app.use("/", express.static(distDir));
let socketServer = new SocketServer(args._[0] as string, io);
socketServer.init();
// Fallback, serve index.html
let cachedIndex: string | undefined = undefined;
app.get("/*", async (req, res) => {
if (!cachedIndex) {
cachedIndex = await readFile(`${distDir}/index.html`, "utf8");
}
res.status(200).header("Content-Type", "text/html").send(cachedIndex);
let socketServer = new SocketServer(pagesPath, io, system);
socketServer.init().catch((e) => {
console.error(e);
});
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
const expressServer = new ExpressServer(app, pagesPath, distDir, system);
expressServer
.init()
.then(async () => {
let plugLoader = new DiskPlugLoader(
system,
`${__dirname}/../../plugs/dist`
);
await plugLoader.loadPlugs();
plugLoader.watcher();
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
})
.catch((e) => {
console.error(e);
});
+15
View File
@@ -0,0 +1,15 @@
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 };
},
};
}