2022-10-10 12:50:21 +00:00
|
|
|
import { Plug } from "../../plugos/plug.ts";
|
|
|
|
import { System } from "../../plugos/system.ts";
|
|
|
|
import { Hook, Manifest } from "../../plugos/types.ts";
|
2022-05-17 09:53:17 +00:00
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
export type NamespaceOperation =
|
|
|
|
| "readFile"
|
|
|
|
| "writeFile"
|
|
|
|
| "listFiles"
|
|
|
|
| "getFileMeta"
|
|
|
|
| "deleteFile";
|
2022-05-17 09:53:17 +00:00
|
|
|
|
|
|
|
export type PageNamespaceDef = {
|
|
|
|
pattern: string;
|
2022-09-12 12:50:37 +00:00
|
|
|
operation: NamespaceOperation;
|
2022-05-17 09:53:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type PageNamespaceHookT = {
|
|
|
|
pageNamespace?: PageNamespaceDef;
|
|
|
|
};
|
|
|
|
|
|
|
|
type SpaceFunction = {
|
2022-09-12 12:50:37 +00:00
|
|
|
operation: NamespaceOperation;
|
2022-05-17 09:53:17 +00:00
|
|
|
pattern: RegExp;
|
|
|
|
plug: Plug<PageNamespaceHookT>;
|
|
|
|
name: string;
|
2022-11-24 11:04:00 +00:00
|
|
|
env?: string;
|
2022-05-17 09:53:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class PageNamespaceHook implements Hook<PageNamespaceHookT> {
|
|
|
|
spaceFunctions: SpaceFunction[] = [];
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
apply(system: System<PageNamespaceHookT>): void {
|
|
|
|
system.on({
|
|
|
|
plugLoaded: () => {
|
|
|
|
this.updateCache(system);
|
|
|
|
},
|
|
|
|
plugUnloaded: () => {
|
|
|
|
this.updateCache(system);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCache(system: System<PageNamespaceHookT>) {
|
|
|
|
this.spaceFunctions = [];
|
2022-11-24 11:04:00 +00:00
|
|
|
for (const plug of system.loadedPlugs.values()) {
|
2022-05-17 09:53:17 +00:00
|
|
|
if (plug.manifest?.functions) {
|
2022-10-10 12:50:21 +00:00
|
|
|
for (
|
2022-11-24 11:04:00 +00:00
|
|
|
const [funcName, funcDef] of Object.entries(
|
2022-10-10 12:50:21 +00:00
|
|
|
plug.manifest.functions,
|
|
|
|
)
|
|
|
|
) {
|
2022-05-17 09:53:17 +00:00
|
|
|
if (funcDef.pageNamespace) {
|
|
|
|
this.spaceFunctions.push({
|
|
|
|
operation: funcDef.pageNamespace.operation,
|
|
|
|
pattern: new RegExp(funcDef.pageNamespace.pattern),
|
|
|
|
plug,
|
|
|
|
name: funcName,
|
2022-11-24 11:04:00 +00:00
|
|
|
env: funcDef.env,
|
2022-05-17 09:53:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validateManifest(manifest: Manifest<PageNamespaceHookT>): string[] {
|
2022-11-24 11:04:00 +00:00
|
|
|
const errors: string[] = [];
|
2022-05-17 09:53:17 +00:00
|
|
|
if (!manifest.functions) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-07-06 14:47:50 +00:00
|
|
|
for (const [funcName, funcDef] of Object.entries(manifest.functions)) {
|
2022-05-17 09:53:17 +00:00
|
|
|
if (funcDef.pageNamespace) {
|
|
|
|
if (!funcDef.pageNamespace.pattern) {
|
|
|
|
errors.push(`Function ${funcName} has a namespace but no pattern`);
|
|
|
|
}
|
|
|
|
if (!funcDef.pageNamespace.operation) {
|
|
|
|
errors.push(`Function ${funcName} has a namespace but no operation`);
|
|
|
|
}
|
|
|
|
if (
|
2022-07-06 10:18:33 +00:00
|
|
|
![
|
2022-09-12 12:50:37 +00:00
|
|
|
"readFile",
|
|
|
|
"writeFile",
|
|
|
|
"getFileMeta",
|
|
|
|
"listFiles",
|
|
|
|
"deleteFile",
|
2022-07-06 10:18:33 +00:00
|
|
|
].includes(funcDef.pageNamespace.operation)
|
2022-05-17 09:53:17 +00:00
|
|
|
) {
|
|
|
|
errors.push(
|
2022-10-10 12:50:21 +00:00
|
|
|
`Function ${funcName} has an invalid operation ${funcDef.pageNamespace.operation}`,
|
2022-05-17 09:53:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
}
|