silverbullet/common/spaces/plug_space_primitives.ts

132 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2024-07-30 23:33:33 +08:00
import type { SpacePrimitives } from "$common/spaces/space_primitives.ts";
import type { NamespaceOperation } from "$lib/plugos/namespace.ts";
import type { FileMeta } from "../../plug-api/types.ts";
import type { PlugNamespaceHook } from "../hooks/plug_namespace.ts";
2022-05-17 17:53:17 +08:00
export class PlugSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
2023-07-14 22:48:35 +08:00
private hook: PlugNamespaceHook,
private env?: string,
2022-05-17 17:53:17 +08:00
) {}
// Used e.g. by the sync engine to see if it should sync a certain path (likely not the case when we have a plug space override)
public isLikelyHandled(path: string): boolean {
for (
2023-11-15 23:27:17 +08:00
const { pattern } of this.hook.spaceFunctions
) {
2023-11-15 23:27:17 +08:00
if (path.match(pattern)) {
return true;
}
}
return false;
}
2022-05-17 17:53:17 +08:00
performOperation(
2022-09-12 20:50:37 +08:00
type: NamespaceOperation,
path: string,
2022-05-17 17:53:17 +08:00
...args: any[]
): Promise<any> | false {
2022-11-24 19:04:00 +08:00
for (
const { operation, pattern, plug, name, env } of this.hook.spaceFunctions
) {
2023-12-20 21:56:15 +08:00
// console.log(
// "Going to match agains pattern",
// operation,
// pattern,
// path,
// this.env,
// env,
// );
2022-11-24 19:04:00 +08:00
if (
operation === type && path.match(pattern) &&
2023-12-20 21:56:15 +08:00
// Both envs are set, and they don't match
(!this.env || !env || env === this.env)
2022-11-24 19:04:00 +08:00
) {
return plug.invoke(name, [path, ...args]);
2022-05-17 17:53:17 +08:00
}
}
return false;
}
2022-09-12 20:50:37 +08:00
async fetchFileList(): Promise<FileMeta[]> {
2022-10-16 01:02:56 +08:00
const allFiles: FileMeta[] = [];
for (const { plug, name, operation, env } of this.hook.spaceFunctions) {
if (
operation === "listFiles" && (!this.env || (env && env === this.env))
) {
2022-07-11 19:51:04 +08:00
try {
2022-10-16 01:02:56 +08:00
for (const pm of await plug.invoke(name, [])) {
2022-09-12 20:50:37 +08:00
allFiles.push(pm);
2022-07-11 19:51:04 +08:00
}
} catch (e: any) {
if (!e.message.includes("not available")) {
// Don't report "not available in" environments errors
console.error("Error listing files", e);
}
2022-05-17 17:53:17 +08:00
}
}
}
2023-01-13 22:41:29 +08:00
const files = await this.wrapped.fetchFileList();
for (const pm of files) {
2022-09-12 20:50:37 +08:00
allFiles.push(pm);
2022-05-17 17:53:17 +08:00
}
2022-09-12 20:50:37 +08:00
return allFiles;
2022-05-17 17:53:17 +08:00
}
2022-10-19 15:52:29 +08:00
async readFile(
2022-09-12 20:50:37 +08:00
name: string,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
const result: { data: Uint8Array; meta: FileMeta } | false = await this
2022-10-19 15:52:29 +08:00
.performOperation(
"readFile",
name,
);
2022-05-17 17:53:17 +08:00
if (result) {
return result;
2022-05-17 17:53:17 +08:00
}
return this.wrapped.readFile(name);
2022-05-17 17:53:17 +08:00
}
2022-09-12 20:50:37 +08:00
getFileMeta(name: string): Promise<FileMeta> {
2022-10-16 01:02:56 +08:00
const result = this.performOperation("getFileMeta", name);
2022-05-17 17:53:17 +08:00
if (result) {
return result;
}
2022-09-12 20:50:37 +08:00
return this.wrapped.getFileMeta(name);
2022-05-17 17:53:17 +08:00
}
2022-09-12 20:50:37 +08:00
writeFile(
2022-05-17 17:53:17 +08:00
name: string,
data: Uint8Array,
selfUpdate?: boolean,
meta?: FileMeta,
2022-09-12 20:50:37 +08:00
): Promise<FileMeta> {
2022-10-16 01:02:56 +08:00
const result = this.performOperation(
2022-09-12 20:50:37 +08:00
"writeFile",
2022-05-17 17:53:17 +08:00
name,
data,
selfUpdate,
meta,
2022-05-17 17:53:17 +08:00
);
if (result) {
return result;
}
return this.wrapped.writeFile(
name,
data,
selfUpdate,
meta,
);
2022-05-17 17:53:17 +08:00
}
2022-09-12 20:50:37 +08:00
deleteFile(name: string): Promise<void> {
2022-10-16 01:02:56 +08:00
const result = this.performOperation("deleteFile", name);
2022-05-17 17:53:17 +08:00
if (result) {
return result;
}
2022-09-12 20:50:37 +08:00
return this.wrapped.deleteFile(name);
}
2022-05-17 17:53:17 +08:00
}