silverbullet/server/server_system.ts

246 lines
7.7 KiB
TypeScript
Raw Permalink Normal View History

import { PlugNamespaceHook } from "$common/hooks/plug_namespace.ts";
2024-07-30 23:33:33 +08:00
import type { SilverBulletHooks } from "../lib/manifest.ts";
import { EventedSpacePrimitives } from "$common/spaces/evented_space_primitives.ts";
import { PlugSpacePrimitives } from "$common/spaces/plug_space_primitives.ts";
import { createSandbox } from "../lib/plugos/sandboxes/web_worker_sandbox.ts";
import { CronHook } from "../lib/plugos/hooks/cron.ts";
2024-07-30 23:33:33 +08:00
import type { EventHook } from "../common/hooks/event.ts";
import { MQHook } from "../lib/plugos/hooks/mq.ts";
import assetSyscalls from "../lib/plugos/syscalls/asset.ts";
import { eventSyscalls } from "../lib/plugos/syscalls/event.ts";
import { mqSyscalls } from "../lib/plugos/syscalls/mq.ts";
import { System } from "../lib/plugos/system.ts";
import { Space } from "../common/space.ts";
import { markdownSyscalls } from "$common/syscalls/markdown.ts";
import { spaceReadSyscalls, spaceWriteSyscalls } from "./syscalls/space.ts";
import { systemSyscalls } from "$common/syscalls/system.ts";
import { jsonschemaSyscalls } from "$common/syscalls/jsonschema.ts";
import { yamlSyscalls } from "$common/syscalls/yaml.ts";
import { sandboxFetchSyscalls } from "../lib/plugos/syscalls/fetch.ts";
import { shellSyscalls } from "./syscalls/shell.ts";
2024-07-30 23:33:33 +08:00
import type { SpacePrimitives } from "$common/spaces/space_primitives.ts";
import type { Plug } from "../lib/plugos/plug.ts";
import { DataStore } from "$lib/data/datastore.ts";
import {
dataStoreReadSyscalls,
dataStoreWriteSyscalls,
} from "../lib/plugos/syscalls/datastore.ts";
import { languageSyscalls } from "$common/syscalls/language.ts";
import { templateSyscalls } from "$common/syscalls/template.ts";
2023-10-31 17:33:38 +08:00
import { codeWidgetSyscalls } from "../web/syscalls/code_widget.ts";
import { CodeWidgetHook } from "../web/hooks/code_widget.ts";
import { KVPrimitivesManifestCache } from "$lib/plugos/manifest_cache.ts";
2024-07-30 23:33:33 +08:00
import type { KvPrimitives } from "$lib/data/kv_primitives.ts";
import type { ShellBackend } from "./shell_backend.ts";
import { ensureSpaceIndex } from "$common/space_index.ts";
2024-07-30 23:33:33 +08:00
import type { FileMeta } from "../plug-api/types.ts";
import { CommandHook } from "$common/hooks/command.ts";
import { CommonSystem } from "$common/common_system.ts";
2024-07-30 23:33:33 +08:00
import type { DataStoreMQ } from "$lib/data/mq.datastore.ts";
import { plugPrefix } from "$common/spaces/constants.ts";
import { base64EncodedDataUrl } from "$lib/crypto.ts";
2024-08-15 22:39:06 +08:00
import type { ConfigContainer } from "../type/config.ts";
2023-08-27 20:13:18 +08:00
const fileListInterval = 30 * 1000; // 30s
2023-12-18 21:39:52 +08:00
const plugNameExtractRegex = /([^/]+)\.plug\.js$/;
export class ServerSystem extends CommonSystem {
2023-08-30 03:17:29 +08:00
listInterval?: number;
2023-08-26 14:31:51 +08:00
constructor(
public spacePrimitives: SpacePrimitives,
private kvPrimitives: KvPrimitives,
private shellBackend: ShellBackend,
mq: DataStoreMQ,
ds: DataStore,
eventHook: EventHook,
readOnlyMode: boolean,
enableSpaceScript: boolean,
private configContainer: ConfigContainer,
2023-08-26 14:31:51 +08:00
) {
super(mq, ds, eventHook, readOnlyMode, enableSpaceScript);
2023-08-26 14:31:51 +08:00
}
// Always needs to be invoked right after construction
2023-11-02 19:46:33 +08:00
async init(awaitIndex = false) {
this.system = new System(
"server",
{
manifestCache: new KVPrimitivesManifestCache(
this.kvPrimitives,
"manifest",
),
plugFlushTimeout: 5 * 60 * 1000, // 5 minutes
},
);
this.ds = new DataStore(this.kvPrimitives);
2024-02-04 22:32:14 +08:00
2023-08-26 14:31:51 +08:00
// Event hook
this.system.addHook(this.eventHook);
2023-08-26 14:31:51 +08:00
// Command hook, just for introspection
this.commandHook = new CommandHook(
this.readOnlyMode,
this.spaceScriptCommands,
);
this.system.addHook(this.commandHook);
2023-08-26 14:31:51 +08:00
// Cron hook
const cronHook = new CronHook(this.system);
this.system.addHook(cronHook);
const plugNamespaceHook = new PlugNamespaceHook();
this.system.addHook(plugNamespaceHook);
this.system.addHook(new MQHook(this.system, this.mq));
2023-08-26 14:31:51 +08:00
2023-10-31 17:33:38 +08:00
const codeWidgetHook = new CodeWidgetHook();
this.system.addHook(codeWidgetHook);
2024-07-10 17:32:07 +08:00
const eventedSpacePrimitives = new EventedSpacePrimitives(
new PlugSpacePrimitives(
this.spacePrimitives,
plugNamespaceHook,
2023-08-26 14:31:51 +08:00
),
this.eventHook,
{},
2024-07-10 17:32:07 +08:00
);
// Disable events until everything is set up
eventedSpacePrimitives.enabled = false;
this.spacePrimitives = eventedSpacePrimitives;
const space = new Space(this.spacePrimitives, this.eventHook);
2023-08-26 14:31:51 +08:00
// Add syscalls
this.system.registerSyscalls(
[],
eventSyscalls(this.eventHook),
spaceReadSyscalls(space),
2023-08-26 14:31:51 +08:00
assetSyscalls(this.system),
yamlSyscalls(),
systemSyscalls(
this.system,
this.readOnlyMode,
this,
this.configContainer,
),
mqSyscalls(this.mq),
languageSyscalls(),
jsonschemaSyscalls(),
templateSyscalls(this.ds),
dataStoreReadSyscalls(this.ds),
2023-10-31 17:33:38 +08:00
codeWidgetSyscalls(codeWidgetHook),
2024-01-24 20:34:12 +08:00
markdownSyscalls(),
2023-08-26 14:31:51 +08:00
);
if (!this.readOnlyMode) {
// Write mode only
this.system.registerSyscalls(
[],
spaceWriteSyscalls(space),
dataStoreWriteSyscalls(this.ds),
);
// Syscalls that require some additional permissions
this.system.registerSyscalls(
["fetch"],
sandboxFetchSyscalls(),
);
this.system.registerSyscalls(
["shell"],
shellSyscalls(this.shellBackend),
);
}
2023-08-26 14:31:51 +08:00
await this.loadPlugs();
await this.loadSpaceScripts();
2023-08-28 00:05:14 +08:00
this.listInterval = setInterval(() => {
2023-12-22 22:55:50 +08:00
space.updatePageList().catch(console.error);
2023-08-27 20:13:18 +08:00
}, fileListInterval);
this.eventHook.addLocalListener(
"file:changed",
async (path, localChange) => {
2024-05-31 01:55:35 +08:00
if (!localChange) {
console.log("Outside file change: reindexing", path);
// Change made outside of editor, trigger reindex
2024-05-31 01:55:35 +08:00
if (path.endsWith(".md")) {
const pageName = path.slice(0, -3);
const data = await this.spacePrimitives.readFile(path);
await this.eventHook.dispatchEvent("page:index_text", {
name: pageName,
text: new TextDecoder().decode(data.data),
});
}
}
if (path.startsWith(plugPrefix) && path.endsWith(".plug.js")) {
console.log("Plug updated, reloading:", path);
this.system.unload(path);
await this.loadPlugFromSpace(path);
}
},
);
2023-08-31 04:36:27 +08:00
this.eventHook.addLocalListener(
"file:listed",
(allFiles: FileMeta[]) => {
2024-05-28 02:33:41 +08:00
// Update list of known pages and attachments
this.allKnownFiles.clear();
allFiles.forEach((f) => {
2024-05-28 02:33:41 +08:00
if (!f.name.startsWith(plugPrefix)) {
this.allKnownFiles.add(f.name);
}
});
},
);
2024-07-10 17:32:07 +08:00
// All setup, enable eventing
eventedSpacePrimitives.enabled = true;
space.updatePageList().catch(console.error);
// Ensure a valid index
const indexPromise = ensureSpaceIndex(this.ds, this.system);
if (awaitIndex) {
await indexPromise;
2023-08-31 04:36:27 +08:00
}
await this.eventHook.dispatchEvent("system:ready");
2023-08-26 14:31:51 +08:00
}
async loadPlugs() {
2023-08-30 03:17:29 +08:00
for (const { name } of await this.spacePrimitives.fetchFileList()) {
2023-12-18 21:39:52 +08:00
if (plugNameExtractRegex.test(name)) {
2023-08-30 03:17:29 +08:00
await this.loadPlugFromSpace(name);
2023-08-26 14:31:51 +08:00
}
}
}
2023-08-30 03:17:29 +08:00
async loadPlugFromSpace(path: string): Promise<Plug<SilverBulletHooks>> {
const { meta, data } = await this.spacePrimitives.readFile(path);
const plugName = path.match(plugNameExtractRegex)![1];
2023-08-30 03:17:29 +08:00
return this.system.load(
plugName,
createSandbox(
// Base64 encoding this to support `deno compile` mode
new URL(base64EncodedDataUrl("application/javascript", data)),
),
meta.lastModified,
2023-08-30 03:17:29 +08:00
);
}
2023-08-26 14:31:51 +08:00
async close() {
2023-08-28 00:05:14 +08:00
clearInterval(this.listInterval);
2023-08-26 14:31:51 +08:00
await this.system.unloadAll();
}
}