1
0
silverbullet/server/hooks/plug_space_primitives.ts

129 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Plug } from "../../plugos/plug.ts";
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 "../../common/spaces/space_primitives.ts";
2022-10-15 17:02:56 +00:00
import { FileMeta } from "../../common/types.ts";
import { NamespaceOperation, PageNamespaceHook } from "./page_namespace.ts";
2022-10-19 07:52:29 +00:00
import { base64DecodeDataUrl } from "../../plugos/asset_bundle/base64.ts";
2022-05-17 09:53:17 +00:00
export class PlugSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
private hook: PageNamespaceHook,
private env?: string,
2022-05-17 09:53:17 +00:00
) {}
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 {
2022-11-24 11:04:00 +00:00
for (
const { operation, pattern, plug, name, env } of this.hook.spaceFunctions
) {
if (
operation === type && pageName.match(pattern) &&
(!this.env || (env && env === this.env))
2022-11-24 11:04:00 +00:00
) {
2022-05-17 09:53:17 +00:00
return plug.invoke(name, [pageName, ...args]);
}
}
return false;
}
2022-09-12 12:50:37 +00:00
async fetchFileList(): Promise<FileMeta[]> {
2022-10-15 17:02:56 +00:00
const allFiles: FileMeta[] = [];
for (const { 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 {
2022-10-15 17:02:56 +00:00
for (const 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-10-15 17:02:56 +00:00
const result = await this.wrapped.fetchFileList();
for (const pm of result) {
2022-09-12 12:50:37 +00:00
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-10-19 07:52:29 +00:00
async readFile(
2022-09-12 12:50:37 +00:00
name: string,
encoding: FileEncoding,
2022-09-12 12:50:37 +00:00
): Promise<{ data: FileData; meta: FileMeta }> {
2022-10-19 07:52:29 +00:00
const wantArrayBuffer = encoding === "arraybuffer";
const result: { data: FileData; meta: FileMeta } | false = await this
.performOperation(
"readFile",
name,
wantArrayBuffer ? "dataurl" : encoding,
);
2022-05-17 09:53:17 +00:00
if (result) {
2022-10-19 07:52:29 +00:00
if (wantArrayBuffer) {
return {
data: base64DecodeDataUrl(result.data as string),
meta: result.meta,
};
} else {
return result;
}
2022-05-17 09:53:17 +00:00
}
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> {
2022-10-15 17:02:56 +00:00
const 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,
2022-09-12 12:50:37 +00:00
): Promise<FileMeta> {
2022-10-15 17:02:56 +00:00
const 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> {
2022-10-15 17:02:56 +00:00
const 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[],
2022-05-17 09:53:17 +00:00
): Promise<any> {
return this.wrapped.invokeFunction(plug, env, name, args);
}
}