1
0
silverbullet/common/spaces/space.ts

271 lines
7.7 KiB
TypeScript
Raw Normal View History

import { FileData, FileEncoding, SpacePrimitives } from "./space_primitives.ts";
2023-01-13 14:41:29 +00:00
import { AttachmentMeta, PageMeta } from "../types.ts";
import { EventEmitter } from "../../plugos/event.ts";
import { Plug } from "../../plugos/plug.ts";
import { plugPrefix } from "./constants.ts";
import { safeRun } from "../util.ts";
2023-01-13 14:41:29 +00:00
import {
FileMeta,
ProxyFileSystem,
} from "../../plug-api/plugos-syscall/types.ts";
2022-04-07 13:21:30 +00:00
const pageWatchInterval = 2000;
2022-04-05 15:02:17 +00:00
export type SpaceEvents = {
pageCreated: (meta: PageMeta) => void;
pageChanged: (meta: PageMeta) => void;
pageDeleted: (name: string) => void;
pageListUpdated: (pages: Set<PageMeta>) => void;
};
2023-01-13 14:41:29 +00:00
export class Space extends EventEmitter<SpaceEvents>
implements ProxyFileSystem {
2022-04-07 13:21:30 +00:00
pageMetaCache = new Map<string, PageMeta>();
watchedPages = new Set<string>();
private initialPageListLoad = true;
private saving = false;
2023-01-13 14:41:29 +00:00
constructor(readonly spacePrimitives: SpacePrimitives) {
2022-04-07 13:21:30 +00:00
super();
}
2023-01-13 14:41:29 +00:00
// Filesystem interface implementation
async readFile(path: string, encoding: "dataurl" | "utf8"): Promise<string> {
return (await this.spacePrimitives.readFile(path, encoding)).data as string;
}
getFileMeta(path: string): Promise<FileMeta> {
return this.spacePrimitives.getFileMeta(path);
}
writeFile(
path: string,
text: string,
encoding: "dataurl" | "utf8",
): Promise<FileMeta> {
return this.spacePrimitives.writeFile(path, encoding, text);
}
deleteFile(path: string): Promise<void> {
return this.spacePrimitives.deleteFile(path);
}
async listFiles(path: string): Promise<FileMeta[]> {
return (await this.spacePrimitives.fetchFileList()).filter((f) =>
f.name.startsWith(path)
);
}
// The more domain-specific methods
2022-04-26 17:04:36 +00:00
public async updatePageList() {
2022-10-15 17:02:56 +00:00
const newPageList = await this.fetchPageList();
const deletedPages = new Set<string>(this.pageMetaCache.keys());
2022-09-12 12:50:37 +00:00
newPageList.forEach((meta) => {
2022-04-26 17:04:36 +00:00
const pageName = meta.name;
const oldPageMeta = this.pageMetaCache.get(pageName);
const newPageMeta: PageMeta = { ...meta };
2022-04-26 17:04:36 +00:00
if (
!oldPageMeta &&
(pageName.startsWith(plugPrefix) || !this.initialPageListLoad)
) {
this.emit("pageCreated", newPageMeta);
} else if (
oldPageMeta &&
2022-09-12 12:50:37 +00:00
oldPageMeta.lastModified !== newPageMeta.lastModified
2022-04-26 17:04:36 +00:00
) {
this.emit("pageChanged", newPageMeta);
2022-04-07 13:21:30 +00:00
}
2022-04-26 17:04:36 +00:00
// Page found, not deleted
deletedPages.delete(pageName);
2022-04-07 13:21:30 +00:00
2022-04-26 17:04:36 +00:00
// Update in cache
this.pageMetaCache.set(pageName, newPageMeta);
2022-04-07 13:21:30 +00:00
});
2022-04-26 17:04:36 +00:00
for (const deletedPage of deletedPages) {
this.pageMetaCache.delete(deletedPage);
this.emit("pageDeleted", deletedPage);
}
this.emit("pageListUpdated", this.listPages());
this.initialPageListLoad = false;
2022-04-07 13:21:30 +00:00
}
watch() {
setInterval(() => {
safeRun(async () => {
if (this.saving) {
return;
}
for (const pageName of this.watchedPages) {
const oldMeta = this.pageMetaCache.get(pageName);
if (!oldMeta) {
// No longer in cache, meaning probably deleted let's unwatch
this.watchedPages.delete(pageName);
continue;
}
// This seems weird, but simply fetching it will compare to local cache and trigger an event if necessary
await this.getPageMeta(pageName);
2022-04-07 13:21:30 +00:00
}
});
}, pageWatchInterval);
2022-04-26 17:04:36 +00:00
this.updatePageList().catch(console.error);
2022-04-07 13:21:30 +00:00
}
2022-10-15 17:02:56 +00:00
async deletePage(name: string): Promise<void> {
2022-04-07 13:21:30 +00:00
await this.getPageMeta(name); // Check if page exists, if not throws Error
2023-01-13 14:41:29 +00:00
await this.spacePrimitives.deleteFile(`${name}.md`);
2022-04-07 13:21:30 +00:00
this.pageMetaCache.delete(name);
this.emit("pageDeleted", name);
this.emit("pageListUpdated", new Set([...this.pageMetaCache.values()]));
}
async getPageMeta(name: string): Promise<PageMeta> {
2022-10-15 17:02:56 +00:00
const oldMeta = this.pageMetaCache.get(name);
const newMeta = fileMetaToPageMeta(
2023-01-13 14:41:29 +00:00
await this.spacePrimitives.getFileMeta(`${name}.md`),
2022-09-12 12:50:37 +00:00
);
if (oldMeta) {
if (oldMeta.lastModified !== newMeta.lastModified) {
// Changed on disk, trigger event
this.emit("pageChanged", newMeta);
}
}
return this.metaCacher(name, newMeta);
2022-04-07 13:21:30 +00:00
}
2022-04-05 15:02:17 +00:00
invokeFunction(
plug: Plug<any>,
env: string,
name: string,
2022-10-10 16:19:08 +00:00
args: any[],
2022-04-07 13:21:30 +00:00
): Promise<any> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.invokeFunction(plug, env, name, args);
2022-04-07 13:21:30 +00:00
}
listPages(): PageMeta[] {
return [...new Set(this.pageMetaCache.values())];
2022-04-07 13:21:30 +00:00
}
2022-09-12 12:50:37 +00:00
async listPlugs(): Promise<string[]> {
2023-01-13 14:41:29 +00:00
const files = await this.spacePrimitives.fetchFileList();
return files
2022-09-12 12:50:37 +00:00
.filter((fileMeta) => fileMeta.name.endsWith(".plug.json"))
.map((fileMeta) => fileMeta.name);
2022-04-07 13:21:30 +00:00
}
proxySyscall(plug: Plug<any>, name: string, args: any[]): Promise<any> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.proxySyscall(plug, name, args);
2022-04-07 13:21:30 +00:00
}
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
2023-01-13 14:41:29 +00:00
const pageData = await this.spacePrimitives.readFile(
`${name}.md`,
"utf8",
);
2022-10-15 17:02:56 +00:00
const previousMeta = this.pageMetaCache.get(name);
const newMeta = fileMetaToPageMeta(pageData.meta);
if (previousMeta) {
2022-09-12 12:50:37 +00:00
if (previousMeta.lastModified !== newMeta.lastModified) {
// Page changed since last cached metadata, trigger event
2022-09-12 12:50:37 +00:00
this.emit("pageChanged", newMeta);
}
}
2022-10-15 17:02:56 +00:00
const meta = this.metaCacher(name, newMeta);
2022-09-12 12:50:37 +00:00
return {
text: pageData.data as string,
meta: meta,
};
2022-04-07 13:21:30 +00:00
}
watchPage(pageName: string) {
this.watchedPages.add(pageName);
}
unwatchPage(pageName: string) {
this.watchedPages.delete(pageName);
}
async writePage(
name: string,
text: string,
2022-10-10 16:19:08 +00:00
selfUpdate?: boolean,
2022-04-07 13:21:30 +00:00
): Promise<PageMeta> {
try {
this.saving = true;
2022-10-10 16:19:08 +00:00
const pageMeta = fileMetaToPageMeta(
2023-01-13 14:41:29 +00:00
await this.spacePrimitives.writeFile(
`${name}.md`,
"utf8",
text,
selfUpdate,
),
2022-04-07 13:21:30 +00:00
);
if (!selfUpdate) {
this.emit("pageChanged", pageMeta);
}
return this.metaCacher(name, pageMeta);
} finally {
this.saving = false;
}
}
2022-09-12 12:50:37 +00:00
async fetchPageList(): Promise<PageMeta[]> {
2023-01-13 14:41:29 +00:00
return (await this.spacePrimitives.fetchFileList())
2022-09-12 12:50:37 +00:00
.filter((fileMeta) => fileMeta.name.endsWith(".md"))
.map(fileMetaToPageMeta);
2022-04-07 13:21:30 +00:00
}
2022-09-12 12:50:37 +00:00
async fetchAttachmentList(): Promise<AttachmentMeta[]> {
2023-01-13 14:41:29 +00:00
return (await this.spacePrimitives.fetchFileList()).filter(
2022-09-12 12:50:37 +00:00
(fileMeta) =>
!fileMeta.name.endsWith(".md") &&
!fileMeta.name.endsWith(".plug.json") &&
fileMeta.name !== "data.db",
2022-09-12 12:50:37 +00:00
);
}
2022-09-12 12:50:37 +00:00
/**
* Reads an attachment
* @param name path of the attachment
* @param encoding how the return value is expected to be encoded
* @returns
*/
readAttachment(
2022-09-05 14:15:01 +00:00
name: string,
2022-10-10 16:19:08 +00:00
encoding: FileEncoding,
2022-09-12 12:50:37 +00:00
): Promise<{ data: FileData; meta: AttachmentMeta }> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.readFile(name, encoding);
}
2022-09-12 12:50:37 +00:00
getAttachmentMeta(name: string): Promise<AttachmentMeta> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.getFileMeta(name);
}
2022-09-12 12:50:37 +00:00
writeAttachment(
name: string,
2022-09-12 12:50:37 +00:00
encoding: FileEncoding,
data: FileData,
2022-10-10 16:19:08 +00:00
selfUpdate?: boolean | undefined,
): Promise<AttachmentMeta> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.writeFile(name, encoding, data, selfUpdate);
}
2022-09-12 12:50:37 +00:00
deleteAttachment(name: string): Promise<void> {
2023-01-13 14:41:29 +00:00
return this.spacePrimitives.deleteFile(name);
}
2022-09-12 12:50:37 +00:00
private metaCacher(name: string, meta: PageMeta): PageMeta {
if (meta.lastModified !== 0) {
// Don't cache metadata for pages with a 0 lastModified timestamp (usualy dynamically generated pages)
this.pageMetaCache.set(name, meta);
}
2022-09-12 12:50:37 +00:00
return meta;
2022-04-07 13:21:30 +00:00
}
2022-04-05 15:02:17 +00:00
}
2022-09-12 12:50:37 +00:00
function fileMetaToPageMeta(fileMeta: FileMeta): PageMeta {
return {
...fileMeta,
name: fileMeta.name.substring(0, fileMeta.name.length - 3),
} as PageMeta;
}