SilverBullet pivot to become an offline-first PWA (#403)

This commit is contained in:
Zef Hemel
2023-05-23 20:53:53 +02:00
committed by GitHub
parent b256269897
commit 5f484bed57
389 changed files with 4484 additions and 291129 deletions
+2 -3
View File
@@ -1,6 +1,5 @@
import { Hook, Manifest } from "../types.ts";
import { Cron } from "https://cdn.jsdelivr.net/gh/hexagon/croner@4/src/croner.js";
import { safeRun } from "../util.ts";
import { System } from "../system.ts";
export type CronHookT = {
cron?: string | string[];
@@ -51,13 +50,13 @@ export class CronHook implements Hook<CronHookT> {
this.tasks.push(
new Cron(cronDef, () => {
// console.log("Now acting on cron", cronDef);
safeRun(async () => {
(async () => {
try {
await plug.invoke(name, [cronDef]);
} catch (e: any) {
console.error("Execution of cron function failed", e);
}
});
})().catch(console.error);
}),
);
}
+36 -36
View File
@@ -6,43 +6,43 @@ import { System } from "../system.ts";
import { Application } from "../../server/deps.ts";
import { assertEquals } from "../../test_deps.ts";
Deno.test("Run a plugos endpoint server", async () => {
const system = new System<EndpointHookT>("server");
await system.load(
{
name: "test",
functions: {
testhandler: {
http: {
path: "/",
},
code: `(() => {
return {
default: (req) => {
console.log("Req", req);
return {status: 200, body: [1, 2, 3], headers: {"Content-type": "application/json"}};
}
};
})()`,
},
},
} as Manifest<EndpointHookT>,
createSandbox,
);
// Deno.test("Run a plugos endpoint server", async () => {
// const system = new System<EndpointHookT>("server");
// await system.load(
// {
// name: "test",
// functions: {
// testhandler: {
// http: {
// path: "/",
// },
// code: `(() => {
// return {
// default: (req) => {
// console.log("Req", req);
// return {status: 200, body: [1, 2, 3], headers: {"Content-type": "application/json"}};
// }
// };
// })()`,
// },
// },
// } as Manifest<EndpointHookT>,
// createSandbox,
// );
const app = new Application();
const port = 3123;
// const app = new Application();
// const port = 3123;
system.addHook(new EndpointHook(app, "/_"));
// system.addHook(new EndpointHook(app, "/_"));
const controller = new AbortController();
app.listen({ port: port, signal: controller.signal });
// const controller = new AbortController();
// app.listen({ port: port, signal: controller.signal });
const res = await fetch(`http://localhost:${port}/_/test/?name=Pete`);
assertEquals(res.status, 200);
assertEquals(res.headers.get("Content-type"), "application/json");
assertEquals(await res.json(), [1, 2, 3]);
console.log("Aborting");
controller.abort();
await system.unloadAll();
});
// const res = await fetch(`http://localhost:${port}/_/test/?name=Pete`);
// assertEquals(res.status, 200);
// assertEquals(res.headers.get("Content-type"), "application/json");
// assertEquals(await res.json(), [1, 2, 3]);
// console.log("Aborting");
// controller.abort();
// await system.unloadAll();
// });
+5 -7
View File
@@ -1,6 +1,5 @@
import type { Hook, Manifest } from "../types.ts";
import { System } from "../system.ts";
import { safeRun } from "../util.ts";
// System events:
// - plug:load (plugName: string)
@@ -48,14 +47,15 @@ export class EventHook implements Hook<EventHookT> {
}
const responses: any[] = [];
for (const plug of this.system.loadedPlugs.values()) {
const manifest = await plug.manifest;
for (
const [name, functionDef] of Object.entries(
plug.manifest!.functions,
manifest!.functions,
)
) {
if (functionDef.events && functionDef.events.includes(eventName)) {
// Only dispatch functions that can run in this environment
if (plug.canInvoke(name)) {
if (await plug.canInvoke(name)) {
const result = await plug.invoke(name, [data]);
if (result !== undefined) {
responses.push(result);
@@ -80,10 +80,8 @@ export class EventHook implements Hook<EventHookT> {
apply(system: System<EventHookT>): void {
this.system = system;
this.system.on({
plugLoaded: (plug) => {
safeRun(async () => {
await this.dispatchEvent("plug:load", plug.name);
});
plugLoaded: async (plug) => {
await this.dispatchEvent("plug:load", plug.name);
},
});
}