2023-05-23 18:53:53 +00:00
|
|
|
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,
|
2023-01-08 11:24:12 +00:00
|
|
|
private env?: string,
|
2022-05-17 09:53:17 +00:00
|
|
|
) {}
|
|
|
|
|
2023-05-23 18:53:53 +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 (
|
2023-11-15 15:27:17 +00:00
|
|
|
const { pattern } of this.hook.spaceFunctions
|
2023-05-23 18:53:53 +00:00
|
|
|
) {
|
2023-11-15 15:27:17 +00:00
|
|
|
if (path.match(pattern)) {
|
2023-05-23 18:53:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-17 09:53:17 +00:00
|
|
|
performOperation(
|
2022-09-12 12:50:37 +00:00
|
|
|
type: NamespaceOperation,
|
2023-05-23 18:53:53 +00:00
|
|
|
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
|
|
|
|
) {
|
2023-07-02 09:25:32 +00:00
|
|
|
// console.log("Going to match agains pattern", pattern, path);
|
2022-11-24 11:04:00 +00:00
|
|
|
if (
|
2023-05-23 18:53:53 +00:00
|
|
|
operation === type && path.match(pattern) &&
|
2023-01-08 11:24:12 +00:00
|
|
|
(!this.env || (env && env === this.env))
|
2022-11-24 11:04:00 +00:00
|
|
|
) {
|
2023-05-23 18:53:53 +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[] = [];
|
2023-11-15 15:13:44 +00:00
|
|
|
for (const { plug, name, operation, env } of this.hook.spaceFunctions) {
|
|
|
|
if (
|
|
|
|
operation === "listFiles" && (!this.env || (env && env === this.env))
|
|
|
|
) {
|
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,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
2023-07-02 09:25:32 +00:00
|
|
|
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) {
|
2023-07-02 09:25:32 +00:00
|
|
|
return result;
|
2022-05-17 09:53:17 +00:00
|
|
|
}
|
2023-05-23 18:53:53 +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,
|
2023-05-23 18:53:53 +00:00
|
|
|
data: Uint8Array,
|
2022-10-10 12:50:21 +00:00
|
|
|
selfUpdate?: boolean,
|
2023-07-02 09:25:32 +00:00
|
|
|
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,
|
2023-07-02 09:25:32 +00:00
|
|
|
data,
|
2022-10-10 12:50:21 +00:00
|
|
|
selfUpdate,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta,
|
2022-05-17 09:53:17 +00:00
|
|
|
);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
return this.wrapped.writeFile(
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
selfUpdate,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta,
|
2023-05-23 18:53:53 +00:00
|
|
|
);
|
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-09-05 09:47:30 +00:00
|
|
|
}
|
2022-05-17 09:53:17 +00:00
|
|
|
}
|