Files
plainleaf/client/plugos/syscalls/system.ts
T
Zef HemelandGitHub 91e76d0c78 Rust backend/CLI replacement (previously Go) (#2010)
* Rust backend: initial setup and space primitives implementation + tests

* Rust: initial basic server setup

* Rust server: auth layer

* Rust: added specific auth layers

* Rust: shell and proxy endpoints

* Rust: SSR and Prometheus metrics

* Rust: runtime API

* Rust: basic working silverbullet binary server

* Rust: fix client file caching

* Rust: in dev mode, update version number dynamically

* Rust: build-rs Makefile target

* Rust: auth and start of 'sb' CLI

* Rust: headless Chrome runtime runner

* Rust: basic e2e tests for Rust build

* Rust: docker builds

* Rust: forgot to commit silverbullet-bin code

* Rust: cross compile and chrome driver fixes

* Rust: waiting for ready status fixes

* Rust: sb-CLI port

* Rust: use native build toolchain

* Rust: fix build chain

* Rust: edge release builds

* Rust: FreeBSD and armv7 builds

* Rust: edge-upgrade from edge-rust release

* Rust: fix FreeBSD build

* Rust: fix FreeBSD build

* Rust: get rid of Go code

* refactor: rename library crates to server- prefix convention

* Rust: refactoring, moving crypto around

* Rust: more refactoring

* Runtime: internal renaming

* Fix: cursor placement after paste on Webkit browsers

* Tweak how version numbers are generated and consumed

* Cleanup CI situation

* Fix CI

* Fix e2e

* ci: website image uses :edge channel (was :v2) post-merge

* Rust: restore manifest.json endpoint

* Rust: ready to merge
2026-06-16 08:40:41 +02:00

163 lines
5.3 KiB
TypeScript

import type { SysCallMapping } from "../system.ts";
import type { Client } from "../../client.ts";
import { version as publicVersion } from "../../../version.json";
import type { CommandDef } from "@silverbulletmd/silverbullet/type/manifest";
import type { SyscallMeta } from "@silverbulletmd/silverbullet/type/index";
export function systemSyscalls(
client: Client,
readOnlyMode: boolean,
): SysCallMapping {
const api: SysCallMapping = {
"system.invokeFunction": (
_ctx,
fullName: string, // plug.function
...args: any[]
) => {
const [plugName, functionName] = fullName.split(".");
if (!plugName || !functionName) {
throw Error(`Invalid function name ${fullName}`);
}
const plug = client.clientSystem.system.loadedPlugs.get(plugName);
if (!plug) {
throw Error(`Plug ${plugName} not found`);
}
const functionDef = plug.manifest!.functions[functionName];
if (!functionDef) {
throw Error(`Function ${functionName} not found`);
}
return plug.invoke(functionName, args);
},
"system.invokeFunctionOnServer": (
ctx,
fullName: string, // plug.function
...args: any[]
) => {
console.warn(
"Calling deprecated system.invokeFunctionOnServer, use system.invokeFunction instead",
);
return api["system.invokeFunction"](ctx, fullName, ...args);
},
"system.serverSyscall": (_ctx, name: string, ...args: any[]) => {
console.warn(
"Calling deprecated system.serverSyscall, use syscall instead",
);
return client.clientSystem.localSyscall(name, args);
},
"system.invokeCommand": (_ctx, name: string, args?: string[]) => {
console.warn("Deprecated, use editor.invokeCommand instead");
return client.runCommandByName(name, args);
},
"system.listCommands": (): { [key: string]: CommandDef } => {
const commandHook = client.clientSystem.commandHook;
const allCommands: { [key: string]: CommandDef } = {};
for (const [cmd, def] of commandHook.buildAllCommands()) {
allCommands[cmd] = {
name: def.name,
contexts: def.contexts,
priority: def.priority,
key: def.key,
mac: def.mac,
hide: def.hide,
requireMode: def.requireMode,
requireEditor: def.requireEditor,
menu: def.menu,
menuMac: def.menuMac,
menuWindows: def.menuWindows,
menuLinux: def.menuLinux,
};
}
return allCommands;
},
"system.listSyscalls": (): SyscallMeta[] => {
const syscalls: SyscallMeta[] = [];
for (const [name, info] of client.clientSystem.system
.registeredSyscalls) {
syscalls.push({
name,
requiredPermissions: info.requiredPermissions,
argCount: Math.max(0, info.callback.length - 1),
});
}
return syscalls;
},
"system.reloadPlugs": () => {
if (!client) {
throw new Error("Not supported");
}
return client.loadPlugs();
},
"system.loadPlug": async (_ctx, path: string) => {
const meta = await client.space.spacePrimitives.getFileMeta(path);
await client.clientSystem.loadPlugFromPath(path, meta.lastModified);
await client.dispatchAppEvent("plugs:loaded");
},
"system.unloadPlug": (_ctx, path: string) => {
return client.clientSystem.system.unloadByPath(path);
},
"system.reloadConfig": (): Record<string, any> => {
console.warn("system.reloadConfig is deprecated, it's now a no-op");
return client.config.values;
},
"system.loadSpaceScripts": async () => {
console.warn("DEPRECATED: used system.loadScripts instead");
await client.clientSystem.loadLuaScripts();
},
"system.loadScripts": async () => {
await client.clientSystem.loadLuaScripts();
},
"system.loadSpaceStyles": async () => {
if (!client) {
throw new Error("Not supported on server");
}
await client.loadCustomStyles();
},
"system.wipeClient": async (_ctx, logout = false) => {
await client.wipeClient();
if (logout) {
location.href = ".logout";
} else {
alert("Client wiped, feel free to navigate elsewhere");
}
},
// DEPRECATED
"system.cleanDatabases": (): boolean => {
console.warn(
"system.cleanDatabses is deprecated, use Client: Wipe instead",
);
return false;
},
"system.getMode": () => {
return readOnlyMode ? "ro" : "rw";
},
"system.getURLPrefix": () => {
const url = new URL(document.baseURI);
return url.pathname;
},
"system.getBaseURI": () => {
return document.baseURI;
},
"system.getVersion": () => {
return publicVersion;
},
"system.getConfig": (_ctx, key: string, defaultValue: any = undefined) => {
return client.config.get(key, defaultValue);
},
// DEPRECATED
"system.getEnv": () => {
console.warn(
"system.getEnv is deprecated, you can assume the env to always be the client",
);
return null;
},
"system.getSpaceConfig": (_ctx, key, defaultValue?) => {
console.warn(
"system.getSpaceConfig is deprecated, use system.getConfig instead",
);
return client.config.get(key, defaultValue);
},
};
return api;
}