1
0
silverbullet/packages/server/hooks/plug_space_primitives.ts

113 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-05-17 09:53:17 +00:00
import { Plug } from "@plugos/plugos/plug";
2022-09-05 14:15:01 +00:00
import {
2022-09-12 12:50:37 +00:00
FileData,
FileEncoding,
2022-09-05 14:15:01 +00:00
SpacePrimitives,
} from "@silverbulletmd/common/spaces/space_primitives";
2022-09-12 12:50:37 +00:00
import {
AttachmentMeta,
FileMeta,
PageMeta,
} from "@silverbulletmd/common/types";
import { PageNamespaceHook, NamespaceOperation } from "./page_namespace";
2022-05-17 09:53:17 +00:00
export class PlugSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
private hook: PageNamespaceHook
) {}
performOperation(
2022-09-12 12:50:37 +00:00
type: NamespaceOperation,
2022-05-17 09:53:17 +00:00
pageName: string,
...args: any[]
): Promise<any> | false {
for (let { operation, pattern, plug, name } of this.hook.spaceFunctions) {
if (operation === type && pageName.match(pattern)) {
return plug.invoke(name, [pageName, ...args]);
}
}
return false;
}
2022-09-12 12:50:37 +00:00
async fetchFileList(): Promise<FileMeta[]> {
let allFiles: FileMeta[] = [];
2022-05-17 09:53:17 +00:00
for (let { plug, name, operation } of this.hook.spaceFunctions) {
2022-09-12 12:50:37 +00:00
if (operation === "listFiles") {
2022-07-11 11:51:04 +00:00
try {
for (let pm of await plug.invoke(name, [])) {
2022-09-12 12:50:37 +00:00
allFiles.push(pm);
2022-07-11 11:51:04 +00:00
}
} catch (e) {
2022-09-12 12:50:37 +00:00
console.error("Error listing files", e);
2022-05-17 09:53:17 +00:00
}
}
}
2022-09-12 12:50:37 +00:00
let result = await this.wrapped.fetchFileList();
for (let pm of result) {
allFiles.push(pm);
2022-05-17 09:53:17 +00:00
}
2022-09-12 12:50:37 +00:00
return allFiles;
2022-05-17 09:53:17 +00:00
}
2022-09-12 12:50:37 +00:00
readFile(
name: string,
encoding: FileEncoding
): Promise<{ data: FileData; meta: FileMeta }> {
let result = this.performOperation("readFile", name);
2022-05-17 09:53:17 +00:00
if (result) {
return result;
}
2022-09-12 12:50:37 +00:00
return this.wrapped.readFile(name, encoding);
2022-05-17 09:53:17 +00:00
}
2022-09-12 12:50:37 +00:00
getFileMeta(name: string): Promise<FileMeta> {
let result = this.performOperation("getFileMeta", name);
2022-05-17 09:53:17 +00:00
if (result) {
return result;
}
2022-09-12 12:50:37 +00:00
return this.wrapped.getFileMeta(name);
2022-05-17 09:53:17 +00:00
}
2022-09-12 12:50:37 +00:00
writeFile(
2022-05-17 09:53:17 +00:00
name: string,
2022-09-12 12:50:37 +00:00
encoding: FileEncoding,
data: FileData,
selfUpdate?: boolean
): Promise<FileMeta> {
2022-05-17 09:53:17 +00:00
let result = this.performOperation(
2022-09-12 12:50:37 +00:00
"writeFile",
2022-05-17 09:53:17 +00:00
name,
2022-09-12 12:50:37 +00:00
encoding,
data,
selfUpdate
2022-05-17 09:53:17 +00:00
);
if (result) {
return result;
}
2022-09-12 12:50:37 +00:00
return this.wrapped.writeFile(name, encoding, data, selfUpdate);
2022-05-17 09:53:17 +00:00
}
2022-09-12 12:50:37 +00:00
deleteFile(name: string): Promise<void> {
let result = this.performOperation("deleteFile", name);
2022-05-17 09:53:17 +00:00
if (result) {
return result;
}
2022-09-12 12:50:37 +00:00
return this.wrapped.deleteFile(name);
}
2022-05-17 09:53:17 +00:00
proxySyscall(plug: Plug<any>, name: string, args: any[]): Promise<any> {
return this.wrapped.proxySyscall(plug, name, args);
}
invokeFunction(
plug: Plug<any>,
env: string,
name: string,
args: any[]
): Promise<any> {
return this.wrapped.invokeFunction(plug, env, name, args);
}
}