1
0
silverbullet/common/spaces/plug_space_primitives.ts

124 lines
2.9 KiB
TypeScript
Raw Normal View History

import { SpacePrimitives } from "../../common/spaces/space_primitives.ts";
2023-08-20 15:51:00 +00:00
import { FileMeta } from "$sb/types.ts";
2023-01-13 14:41:29 +00:00
import {
NamespaceOperation,
2023-07-14 14:48:35 +00:00
PlugNamespaceHook,
} from "../hooks/plug_namespace.ts";
2022-05-17 09:53:17 +00:00
export class PlugSpacePrimitives implements SpacePrimitives {
constructor(
private wrapped: SpacePrimitives,
2023-07-14 14:48:35 +00:00
private hook: PlugNamespaceHook,
private env?: string,
2022-05-17 09:53:17 +00: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 (
const { pattern, env } of this.hook.spaceFunctions
) {
if (
path.match(pattern) &&
(!this.env || (env && env === this.env))
) {
return true;
}
}
return false;
}
2022-05-17 09:53:17 +00:00
performOperation(
2022-09-12 12:50:37 +00:00
type: NamespaceOperation,
path: string,
2022-05-17 09:53:17 +00:00
...args: any[]
): Promise<any> | false {
2022-11-24 11:04:00 +00:00
for (
const { operation, pattern, plug, name, env } of this.hook.spaceFunctions
) {
// console.log("Going to match agains pattern", pattern, path);
2022-11-24 11:04:00 +00:00
if (
operation === type && path.match(pattern) &&
(!this.env || (env && env === this.env))
2022-11-24 11:04:00 +00:00
) {
return plug.invoke(name, [path, ...args]);
2022-05-17 09:53:17 +00:00
}
}
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
}
}
}
2023-01-13 14:41:29 +00:00
const files = await this.wrapped.fetchFileList();
for (const pm of files) {
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,
): Promise<{ data: Uint8Array; meta: FileMeta }> {
const result: { data: Uint8Array; meta: FileMeta } | false = await this
2022-10-19 07:52:29 +00:00
.performOperation(
"readFile",
name,
);
2022-05-17 09:53:17 +00:00
if (result) {
return result;
2022-05-17 09:53:17 +00:00
}
return this.wrapped.readFile(name);
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,
data: Uint8Array,
selfUpdate?: boolean,
meta?: FileMeta,
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,
data,
selfUpdate,
meta,
2022-05-17 09:53:17 +00:00
);
if (result) {
return result;
}
return this.wrapped.writeFile(
name,
data,
selfUpdate,
meta,
);
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
}