1
0
silverbullet/common/spaces/plug_space_primitives.ts

133 lines
3.1 KiB
TypeScript
Raw Normal View History

import { SpacePrimitives } from "../../common/spaces/space_primitives.ts";
2022-10-15 17:02:56 +00:00
import { FileMeta } from "../../common/types.ts";
2023-01-13 14:41:29 +00:00
import {
NamespaceOperation,
PageNamespaceHook,
} from "../hooks/page_namespace.ts";
import {
base64DecodeDataUrl,
base64EncodedDataUrl,
} from "../../plugos/asset_bundle/base64.ts";
import { mime } from "../deps.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
) {}
// 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
) {
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: string; 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 {
data: base64DecodeDataUrl(result.data),
meta: result.meta,
};
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,
lastModified?: number,
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,
base64EncodedDataUrl(
mime.getType(name) || "application/octet-stream",
data,
),
selfUpdate,
2022-05-17 09:53:17 +00:00
);
if (result) {
return result;
}
return this.wrapped.writeFile(
name,
data,
selfUpdate,
lastModified,
);
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
}