1
0
silverbullet/web/client_system.ts

253 lines
8.2 KiB
TypeScript
Raw Normal View History

2023-07-14 14:48:35 +00:00
import { PlugNamespaceHook } from "../common/hooks/plug_namespace.ts";
2023-07-14 11:44:30 +00:00
import { Manifest, SilverBulletHooks } from "../common/manifest.ts";
import buildMarkdown from "../common/markdown_parser/parser.ts";
import { CronHook } from "../plugos/hooks/cron.ts";
import { EventHook } from "../plugos/hooks/event.ts";
import { createSandbox } from "../plugos/environments/webworker_sandbox.ts";
import assetSyscalls from "../plugos/syscalls/asset.ts";
import { eventSyscalls } from "../plugos/syscalls/event.ts";
import { System } from "../plugos/system.ts";
2023-07-14 14:56:20 +00:00
import type { Client } from "./client.ts";
2023-07-14 11:44:30 +00:00
import { CodeWidgetHook } from "./hooks/code_widget.ts";
import { CommandHook } from "./hooks/command.ts";
import { SlashCommandHook } from "./hooks/slash_command.ts";
import { clientStoreSyscalls } from "./syscalls/clientStore.ts";
import { debugSyscalls } from "./syscalls/debug.ts";
import { editorSyscalls } from "./syscalls/editor.ts";
import { sandboxFetchSyscalls } from "./syscalls/fetch.ts";
import { markdownSyscalls } from "../common/syscalls/markdown.ts";
2023-07-14 11:44:30 +00:00
import { shellSyscalls } from "./syscalls/shell.ts";
import { spaceSyscalls } from "./syscalls/space.ts";
import { syncSyscalls } from "./syscalls/sync.ts";
import { systemSyscalls } from "./syscalls/system.ts";
import { yamlSyscalls } from "../common/syscalls/yaml.ts";
2023-07-14 11:44:30 +00:00
import { Space } from "./space.ts";
import {
loadMarkdownExtensions,
MDExt,
} from "../common/markdown_parser/markdown_ext.ts";
2023-08-10 16:32:41 +00:00
import { MQHook } from "../plugos/hooks/mq.ts";
import { mqSyscalls } from "../plugos/syscalls/mq.ts";
2023-08-28 15:12:15 +00:00
import { mqProxySyscalls } from "./syscalls/mq.proxy.ts";
import { dataStoreProxySyscalls } from "./syscalls/datastore.proxy.ts";
import { dataStoreSyscalls } from "../plugos/syscalls/datastore.ts";
import { DataStore } from "../plugos/lib/datastore.ts";
import { MessageQueue } from "../plugos/lib/mq.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 "./syscalls/code_widget.ts";
2023-11-15 09:08:21 +00:00
import { clientCodeWidgetSyscalls } from "./syscalls/client_code_widget.ts";
2023-12-12 13:15:21 +00:00
import { KVPrimitivesManifestCache } from "../plugos/manifest_cache.ts";
import { deepObjectMerge } from "$sb/lib/json.ts";
2023-12-21 17:37:50 +00:00
import { Query } from "$sb/types.ts";
const plugNameExtractRegex = /\/(.+)\.plug\.js$/;
2023-07-14 11:44:30 +00:00
export class ClientSystem {
commandHook: CommandHook;
slashCommandHook: SlashCommandHook;
2023-07-14 14:48:35 +00:00
namespaceHook: PlugNamespaceHook;
2023-07-14 11:44:30 +00:00
codeWidgetHook: CodeWidgetHook;
mdExtensions: MDExt[] = [];
2023-08-27 12:13:18 +00:00
system: System<SilverBulletHooks>;
2023-07-14 11:44:30 +00:00
constructor(
2023-08-26 06:31:51 +00:00
private client: Client,
private mq: MessageQueue,
private ds: DataStore,
2023-07-14 11:44:30 +00:00
private eventHook: EventHook,
) {
2023-08-27 12:13:18 +00:00
// Only set environment to "client" when running in thin client mode, otherwise we run everything locally (hybrid)
this.system = new System(
client.syncMode ? undefined : "client",
{
manifestCache: new KVPrimitivesManifestCache<SilverBulletHooks>(
ds.kv,
"manifest",
),
},
);
2023-08-27 12:13:18 +00:00
2023-07-14 11:44:30 +00:00
this.system.addHook(this.eventHook);
2023-07-14 14:48:35 +00:00
// Plug page namespace hook
this.namespaceHook = new PlugNamespaceHook();
this.system.addHook(this.namespaceHook);
2023-07-14 11:44:30 +00:00
// Cron hook
const cronHook = new CronHook(this.system);
this.system.addHook(cronHook);
// Code widget hook
this.codeWidgetHook = new CodeWidgetHook();
this.system.addHook(this.codeWidgetHook);
2023-08-10 16:32:41 +00:00
// MQ hook
2023-08-29 19:17:29 +00:00
if (client.syncMode) {
// Process MQ messages locally
2023-08-28 15:12:15 +00:00
this.system.addHook(new MQHook(this.system, this.mq));
}
2023-08-10 16:32:41 +00:00
2023-07-14 11:44:30 +00:00
// Command hook
this.commandHook = new CommandHook();
this.commandHook.on({
commandsUpdated: (commandMap) => {
this.client.ui?.viewDispatch({
2023-07-14 11:44:30 +00:00
type: "update-commands",
commands: commandMap,
});
},
});
this.system.addHook(this.commandHook);
// Slash command hook
2023-08-26 06:31:51 +00:00
this.slashCommandHook = new SlashCommandHook(this.client);
2023-07-14 11:44:30 +00:00
this.system.addHook(this.slashCommandHook);
2023-12-12 13:15:21 +00:00
this.system.on({
plugLoaded: (plug) => {
// Apply plug overrides
const manifestOverrides = this.client.settings.plugOverrides;
if (manifestOverrides && manifestOverrides[plug.manifest!.name]) {
plug.manifest = deepObjectMerge(
plug.manifest,
manifestOverrides[plug.manifest!.name],
);
2023-12-12 13:16:57 +00:00
// console.log("New manifest", plug.manifest);
2023-12-12 13:15:21 +00:00
}
},
});
this.eventHook.addLocalListener(
"file:changed",
async (path: string, _selfUpdate, _oldHash, newHash) => {
if (path.startsWith("_plug/") && path.endsWith(".plug.js")) {
const plugName = plugNameExtractRegex.exec(path)![1];
console.log("Plug updated, reloading", plugName, "from", path);
this.system.unload(path);
const plug = await this.system.load(
new URL(`/${path}`, location.href),
plugName,
newHash,
createSandbox,
);
if ((plug.manifest! as Manifest).syntax) {
// If there are syntax extensions, rebuild the markdown parser immediately
this.updateMarkdownParser();
}
this.client.debouncedPlugsUpdatedEvent();
2023-08-29 19:17:29 +00:00
}
},
);
2023-08-27 09:02:24 +00:00
// Debugging
// this.eventHook.addLocalListener("file:listed", (files) => {
// console.log("New file list", files);
// });
2023-08-28 15:12:15 +00:00
// this.eventHook.addLocalListener("file:changed", (file) => {
// console.log("File changed", file);
// });
2023-08-27 09:02:24 +00:00
2023-08-28 15:12:15 +00:00
// this.eventHook.addLocalListener("file:created", (file) => {
// console.log("File created", file);
// });
2023-08-27 09:02:24 +00:00
2023-08-28 15:12:15 +00:00
// this.eventHook.addLocalListener("file:deleted", (file) => {
// console.log("File deleted", file);
// });
2023-07-14 11:44:30 +00:00
}
async init() {
2023-07-14 11:44:30 +00:00
// Slash command hook
2023-08-26 06:31:51 +00:00
this.slashCommandHook = new SlashCommandHook(this.client);
2023-07-14 11:44:30 +00:00
this.system.addHook(this.slashCommandHook);
// Syscalls available to all plugs
this.system.registerSyscalls(
[],
eventSyscalls(this.eventHook),
2023-08-26 06:31:51 +00:00
editorSyscalls(this.client),
spaceSyscalls(this.client),
2023-08-29 19:17:29 +00:00
systemSyscalls(this.system, this.client),
2023-07-14 11:44:30 +00:00
markdownSyscalls(buildMarkdown(this.mdExtensions)),
assetSyscalls(this.system),
yamlSyscalls(),
handlebarsSyscalls(),
2023-10-31 09:33:38 +00:00
codeWidgetSyscalls(this.codeWidgetHook),
2023-11-15 09:08:21 +00:00
clientCodeWidgetSyscalls(),
languageSyscalls(),
2023-08-29 19:17:29 +00:00
this.client.syncMode
// In sync mode handle locally
? mqSyscalls(this.mq)
// In non-sync mode proxy to server
: mqProxySyscalls(this.client),
this.client.syncMode
? dataStoreSyscalls(this.ds)
: dataStoreProxySyscalls(this.client),
2023-07-14 11:44:30 +00:00
debugSyscalls(),
2023-08-26 06:31:51 +00:00
syncSyscalls(this.client),
clientStoreSyscalls(this.ds),
2023-07-14 11:44:30 +00:00
);
// Syscalls that require some additional permissions
this.system.registerSyscalls(
["fetch"],
2023-08-26 06:31:51 +00:00
sandboxFetchSyscalls(this.client),
2023-07-14 11:44:30 +00:00
);
this.system.registerSyscalls(
["shell"],
2023-08-26 06:31:51 +00:00
shellSyscalls(this.client),
2023-07-14 11:44:30 +00:00
);
}
async reloadPlugsFromSpace(space: Space) {
console.log("Loading plugs");
await space.updatePageList();
await this.system.unloadAll();
console.log("(Re)loading plugs");
await Promise.all((await space.listPlugs()).map(async (plugMeta) => {
2023-07-14 11:44:30 +00:00
try {
const plugName = plugNameExtractRegex.exec(plugMeta.name)![1];
2023-07-14 11:44:30 +00:00
await this.system.load(
new URL(plugMeta.name, location.origin),
plugName,
plugMeta.lastModified,
2023-07-14 11:44:30 +00:00
createSandbox,
);
} catch (e: any) {
console.error(
"Could not load plug",
plugMeta.name,
"error:",
e.message,
);
2023-07-14 11:44:30 +00:00
}
}));
}
updateMarkdownParser() {
// Load all syntax extensions
this.mdExtensions = loadMarkdownExtensions(this.system);
// And reload the syscalls to use the new syntax extensions
this.system.registerSyscalls(
[],
markdownSyscalls(buildMarkdown(this.mdExtensions)),
);
}
localSyscall(name: string, args: any[]) {
2023-12-21 17:37:50 +00:00
return this.system.localSyscall("editor", name, args);
}
queryObjects<T>(tag: string, query: Query): Promise<T[]> {
return this.system.localSyscall(
"index",
"system.invokeFunction",
["queryObjects", tag, query],
);
2023-07-14 11:44:30 +00:00
}
}