2023-08-26 06:31:51 +00:00
|
|
|
import { PlugNamespaceHook } from "../common/hooks/plug_namespace.ts";
|
|
|
|
import { SilverBulletHooks } from "../common/manifest.ts";
|
|
|
|
import { loadMarkdownExtensions } from "../common/markdown_parser/markdown_ext.ts";
|
|
|
|
import buildMarkdown from "../common/markdown_parser/parser.ts";
|
|
|
|
import { EventedSpacePrimitives } from "../common/spaces/evented_space_primitives.ts";
|
|
|
|
import { PlugSpacePrimitives } from "../common/spaces/plug_space_primitives.ts";
|
2024-01-14 12:38:39 +00:00
|
|
|
import { createSandbox } from "../plugos/sandboxes/web_worker_sandbox.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { CronHook } from "../plugos/hooks/cron.ts";
|
|
|
|
import { EventHook } from "../plugos/hooks/event.ts";
|
|
|
|
import { MQHook } from "../plugos/hooks/mq.ts";
|
|
|
|
import assetSyscalls from "../plugos/syscalls/asset.ts";
|
|
|
|
import { eventSyscalls } from "../plugos/syscalls/event.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { mqSyscalls } from "../plugos/syscalls/mq.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { System } from "../plugos/system.ts";
|
|
|
|
import { Space } from "../web/space.ts";
|
|
|
|
import { debugSyscalls } from "../web/syscalls/debug.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { markdownSyscalls } from "../common/syscalls/markdown.ts";
|
2023-08-28 15:12:15 +00:00
|
|
|
import { spaceSyscalls } from "./syscalls/space.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { systemSyscalls } from "../web/syscalls/system.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { yamlSyscalls } from "../common/syscalls/yaml.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { sandboxFetchSyscalls } from "../plugos/syscalls/fetch.ts";
|
2023-12-17 14:25:44 +00:00
|
|
|
import { shellSyscalls } from "./syscalls/shell.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
import { SpacePrimitives } from "../common/spaces/space_primitives.ts";
|
2023-08-29 19:17:29 +00:00
|
|
|
import { base64EncodedDataUrl } from "../plugos/asset_bundle/base64.ts";
|
|
|
|
import { Plug } from "../plugos/plug.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { DataStore } from "../plugos/lib/datastore.ts";
|
|
|
|
import { dataStoreSyscalls } from "../plugos/syscalls/datastore.ts";
|
|
|
|
import { DataStoreMQ } from "../plugos/lib/mq.datastore.ts";
|
|
|
|
import { languageSyscalls } from "../common/syscalls/language.ts";
|
|
|
|
import { handlebarsSyscalls } from "../common/syscalls/handlebars.ts";
|
2023-10-31 09:33:38 +00:00
|
|
|
import { codeWidgetSyscalls } from "../web/syscalls/code_widget.ts";
|
|
|
|
import { CodeWidgetHook } from "../web/hooks/code_widget.ts";
|
2023-12-06 17:44:48 +00:00
|
|
|
import { KVPrimitivesManifestCache } from "../plugos/manifest_cache.ts";
|
2023-12-10 12:23:42 +00:00
|
|
|
import { KvPrimitives } from "../plugos/lib/kv_primitives.ts";
|
2023-12-17 14:25:44 +00:00
|
|
|
import { ShellBackend } from "./shell_backend.ts";
|
2024-01-11 12:20:50 +00:00
|
|
|
import { ensureSpaceIndex } from "../common/space_index.ts";
|
2024-01-20 18:16:07 +00:00
|
|
|
import { FileMeta } from "$sb/types.ts";
|
|
|
|
import { buildQueryFunctions } from "../common/query_functions.ts";
|
2023-08-26 06:31:51 +00:00
|
|
|
|
2024-01-15 15:43:12 +00:00
|
|
|
// // Important: load this before the actual plugs
|
|
|
|
// import {
|
|
|
|
// createSandbox as noSandboxFactory,
|
|
|
|
// runWithSystemLock,
|
|
|
|
// } from "../plugos/sandboxes/no_sandbox.ts";
|
|
|
|
|
|
|
|
// // Load list of builtin plugs
|
|
|
|
// import { plug as plugIndex } from "../dist_plug_bundle/_plug/index.plug.js";
|
|
|
|
// import { plug as plugFederation } from "../dist_plug_bundle/_plug/federation.plug.js";
|
|
|
|
// import { plug as plugQuery } from "../dist_plug_bundle/_plug/query.plug.js";
|
|
|
|
// import { plug as plugSearch } from "../dist_plug_bundle/_plug/search.plug.js";
|
|
|
|
// import { plug as plugTasks } from "../dist_plug_bundle/_plug/tasks.plug.js";
|
|
|
|
// import { plug as plugTemplate } from "../dist_plug_bundle/_plug/template.plug.js";
|
|
|
|
|
2023-08-27 12:13:18 +00:00
|
|
|
const fileListInterval = 30 * 1000; // 30s
|
|
|
|
|
2023-12-18 13:39:52 +00:00
|
|
|
const plugNameExtractRegex = /([^/]+)\.plug\.js$/;
|
2023-12-06 17:44:48 +00:00
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
export class ServerSystem {
|
2023-12-06 17:44:48 +00:00
|
|
|
system!: System<SilverBulletHooks>;
|
2023-12-13 16:52:56 +00:00
|
|
|
public spacePrimitives!: SpacePrimitives;
|
2023-12-10 12:23:42 +00:00
|
|
|
// denoKv!: Deno.Kv;
|
2023-08-29 19:17:29 +00:00
|
|
|
listInterval?: number;
|
2023-10-03 12:16:33 +00:00
|
|
|
ds!: DataStore;
|
2024-01-20 18:16:07 +00:00
|
|
|
allKnownPages = new Set<string>();
|
2023-08-26 06:31:51 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private baseSpacePrimitives: SpacePrimitives,
|
2023-12-10 12:23:42 +00:00
|
|
|
readonly kvPrimitives: KvPrimitives,
|
2023-12-17 14:25:44 +00:00
|
|
|
private shellBackend: ShellBackend,
|
2023-08-26 06:31:51 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always needs to be invoked right after construction
|
2023-11-02 11:46:33 +00:00
|
|
|
async init(awaitIndex = false) {
|
2024-01-20 18:16:07 +00:00
|
|
|
this.ds = new DataStore(
|
|
|
|
this.kvPrimitives,
|
|
|
|
buildQueryFunctions(this.allKnownPages),
|
|
|
|
);
|
2023-12-06 17:44:48 +00:00
|
|
|
|
|
|
|
this.system = new System(
|
|
|
|
"server",
|
|
|
|
{
|
2023-12-10 12:23:42 +00:00
|
|
|
manifestCache: new KVPrimitivesManifestCache(
|
|
|
|
this.kvPrimitives,
|
|
|
|
"manifest",
|
|
|
|
),
|
2023-12-06 17:44:48 +00:00
|
|
|
plugFlushTimeout: 5 * 60 * 1000, // 5 minutes
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
// Event hook
|
|
|
|
const eventHook = new EventHook();
|
|
|
|
this.system.addHook(eventHook);
|
|
|
|
|
|
|
|
// Cron hook
|
|
|
|
const cronHook = new CronHook(this.system);
|
|
|
|
this.system.addHook(cronHook);
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
const mq = new DataStoreMQ(this.ds);
|
2023-08-26 06:31:51 +00:00
|
|
|
|
2023-11-15 08:31:44 +00:00
|
|
|
setInterval(() => {
|
|
|
|
// Timeout after 5s, retries 3 times, otherwise drops the message (no DLQ)
|
|
|
|
mq.requeueTimeouts(5000, 3, true).catch(console.error);
|
|
|
|
}, 20000); // Look to requeue every 20s
|
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
const plugNamespaceHook = new PlugNamespaceHook();
|
|
|
|
this.system.addHook(plugNamespaceHook);
|
|
|
|
|
|
|
|
this.system.addHook(new MQHook(this.system, mq));
|
|
|
|
|
2023-10-31 09:33:38 +00:00
|
|
|
const codeWidgetHook = new CodeWidgetHook();
|
|
|
|
|
|
|
|
this.system.addHook(codeWidgetHook);
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
this.spacePrimitives = new EventedSpacePrimitives(
|
|
|
|
new PlugSpacePrimitives(
|
|
|
|
this.baseSpacePrimitives,
|
|
|
|
plugNamespaceHook,
|
2023-08-26 06:31:51 +00:00
|
|
|
),
|
2023-10-03 12:16:33 +00:00
|
|
|
eventHook,
|
2023-08-26 06:31:51 +00:00
|
|
|
);
|
2023-12-28 15:14:30 +00:00
|
|
|
const space = new Space(this.spacePrimitives, eventHook);
|
2023-08-26 06:31:51 +00:00
|
|
|
|
|
|
|
// Add syscalls
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
eventSyscalls(eventHook),
|
|
|
|
spaceSyscalls(space),
|
|
|
|
assetSyscalls(this.system),
|
|
|
|
yamlSyscalls(),
|
2023-08-29 19:17:29 +00:00
|
|
|
systemSyscalls(this.system),
|
2023-08-26 06:31:51 +00:00
|
|
|
mqSyscalls(mq),
|
2023-10-03 12:16:33 +00:00
|
|
|
languageSyscalls(),
|
|
|
|
handlebarsSyscalls(),
|
|
|
|
dataStoreSyscalls(this.ds),
|
2023-08-26 06:31:51 +00:00
|
|
|
debugSyscalls(),
|
2023-10-31 09:33:38 +00:00
|
|
|
codeWidgetSyscalls(codeWidgetHook),
|
2023-08-26 06:31:51 +00:00
|
|
|
markdownSyscalls(buildMarkdown([])), // Will later be replaced with markdown extensions
|
|
|
|
);
|
|
|
|
|
|
|
|
// Syscalls that require some additional permissions
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
["fetch"],
|
|
|
|
sandboxFetchSyscalls(),
|
|
|
|
);
|
|
|
|
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
["shell"],
|
2023-12-17 14:25:44 +00:00
|
|
|
shellSyscalls(this.shellBackend),
|
2023-08-26 06:31:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await this.loadPlugs();
|
|
|
|
|
|
|
|
// Load markdown syscalls based on all new syntax (if any)
|
|
|
|
this.system.registerSyscalls(
|
|
|
|
[],
|
|
|
|
markdownSyscalls(buildMarkdown(loadMarkdownExtensions(this.system))),
|
|
|
|
);
|
2023-08-27 12:13:18 +00:00
|
|
|
|
2023-08-27 16:05:14 +00:00
|
|
|
this.listInterval = setInterval(() => {
|
2024-01-15 15:43:12 +00:00
|
|
|
// runWithSystemLock(this.system, async () => {
|
|
|
|
// await space.updatePageList();
|
|
|
|
// });
|
2023-12-22 14:55:50 +00:00
|
|
|
space.updatePageList().catch(console.error);
|
2023-08-27 12:13:18 +00:00
|
|
|
}, fileListInterval);
|
|
|
|
|
2024-01-15 15:43:12 +00:00
|
|
|
eventHook.addLocalListener("file:changed", async (path, localChange) => {
|
|
|
|
if (!localChange && path.endsWith(".md")) {
|
|
|
|
const pageName = path.slice(0, -3);
|
|
|
|
const data = await this.spacePrimitives.readFile(path);
|
|
|
|
console.log("Outside page change: reindexing", pageName);
|
|
|
|
// Change made outside of editor, trigger reindex
|
|
|
|
await eventHook.dispatchEvent("page:index_text", {
|
|
|
|
name: pageName,
|
|
|
|
text: new TextDecoder().decode(data.data),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.startsWith("_plug/") && path.endsWith(".plug.js")) {
|
|
|
|
console.log("Plug updated, reloading:", path);
|
|
|
|
this.system.unload(path);
|
|
|
|
await this.loadPlugFromSpace(path);
|
|
|
|
}
|
2023-08-27 12:13:18 +00:00
|
|
|
});
|
2023-08-30 20:36:27 +00:00
|
|
|
|
2024-01-20 18:16:07 +00:00
|
|
|
eventHook.addLocalListener(
|
|
|
|
"file:listed",
|
|
|
|
(allFiles: FileMeta[]) => {
|
|
|
|
// Update list of known pages
|
|
|
|
this.allKnownPages.clear();
|
|
|
|
allFiles.forEach((f) => {
|
|
|
|
if (f.name.endsWith(".md")) {
|
|
|
|
this.allKnownPages.add(f.name.slice(0, -3));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-01-11 12:20:50 +00:00
|
|
|
// Ensure a valid index
|
|
|
|
const indexPromise = ensureSpaceIndex(this.ds, this.system);
|
|
|
|
if (awaitIndex) {
|
|
|
|
await indexPromise;
|
2023-08-30 20:36:27 +00:00
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
|
2024-01-15 15:43:12 +00:00
|
|
|
// await runWithSystemLock(this.system, async () => {
|
2023-10-03 12:16:33 +00:00
|
|
|
await eventHook.dispatchEvent("system:ready");
|
2024-01-15 15:43:12 +00:00
|
|
|
// });
|
2023-08-26 06:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadPlugs() {
|
2024-01-15 15:43:12 +00:00
|
|
|
// await this.system.load("index", noSandboxFactory(plugIndex));
|
|
|
|
// await this.system.load("federation", noSandboxFactory(plugFederation));
|
|
|
|
// await this.system.load("query", noSandboxFactory(plugQuery));
|
|
|
|
// await this.system.load("search", noSandboxFactory(plugSearch));
|
|
|
|
// await this.system.load("tasks", noSandboxFactory(plugTasks));
|
|
|
|
// await this.system.load("template", noSandboxFactory(plugTemplate));
|
|
|
|
|
2023-08-29 19:17:29 +00:00
|
|
|
for (const { name } of await this.spacePrimitives.fetchFileList()) {
|
2023-12-18 13:39:52 +00:00
|
|
|
if (plugNameExtractRegex.test(name)) {
|
2023-08-29 19:17:29 +00:00
|
|
|
await this.loadPlugFromSpace(name);
|
2023-08-26 06:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 19:17:29 +00:00
|
|
|
async loadPlugFromSpace(path: string): Promise<Plug<SilverBulletHooks>> {
|
2023-12-06 17:44:48 +00:00
|
|
|
const { meta, data } = await this.spacePrimitives.readFile(path);
|
|
|
|
const plugName = path.match(plugNameExtractRegex)![1];
|
2023-08-29 19:17:29 +00:00
|
|
|
return this.system.load(
|
2023-12-06 17:44:48 +00:00
|
|
|
plugName,
|
2024-01-15 15:43:12 +00:00
|
|
|
createSandbox(
|
|
|
|
// Base64 encoding this to support `deno compile` mode
|
|
|
|
new URL(base64EncodedDataUrl("application/javascript", data)),
|
|
|
|
),
|
2023-12-06 17:44:48 +00:00
|
|
|
meta.lastModified,
|
2023-08-29 19:17:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-26 06:31:51 +00:00
|
|
|
async close() {
|
2023-08-27 16:05:14 +00:00
|
|
|
clearInterval(this.listInterval);
|
2023-08-26 06:31:51 +00:00
|
|
|
await this.system.unloadAll();
|
|
|
|
}
|
|
|
|
}
|