2022-03-21 14:21:34 +00:00
|
|
|
import { EventEmitter } from "../common/event";
|
|
|
|
import { Manifest } from "../common/manifest";
|
2022-03-31 12:28:07 +00:00
|
|
|
import { safeRun } from "./util";
|
|
|
|
import { Plug } from "../plugos/plug";
|
2022-04-01 15:07:08 +00:00
|
|
|
import { PageMeta } from "../common/types";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
export type SpaceEvents = {
|
|
|
|
pageCreated: (meta: PageMeta) => void;
|
|
|
|
pageChanged: (meta: PageMeta) => void;
|
|
|
|
pageDeleted: (name: string) => void;
|
|
|
|
pageListUpdated: (pages: Set<PageMeta>) => void;
|
2022-03-23 14:41:12 +00:00
|
|
|
plugLoaded: (plugName: string, plug: Manifest) => void;
|
|
|
|
plugUnloaded: (plugName: string) => void;
|
2022-03-31 12:28:07 +00:00
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
type PlugMeta = {
|
|
|
|
name: string;
|
|
|
|
version: number;
|
2022-03-20 08:56:28 +00:00
|
|
|
};
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
const pageWatchInterval = 2000;
|
|
|
|
const plugWatchInterval = 5000;
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
export class Space extends EventEmitter<SpaceEvents> {
|
2022-03-31 12:28:07 +00:00
|
|
|
pageUrl: string;
|
|
|
|
pageMetaCache = new Map<string, PageMeta>();
|
|
|
|
plugMetaCache = new Map<string, PlugMeta>();
|
|
|
|
watchedPages = new Set<string>();
|
|
|
|
saving = false;
|
|
|
|
private plugUrl: string;
|
|
|
|
private initialPageListLoad = true;
|
|
|
|
private initialPlugListLoad = true;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
constructor(url: string) {
|
2022-03-20 08:56:28 +00:00
|
|
|
super();
|
2022-03-31 12:28:07 +00:00
|
|
|
this.pageUrl = url + "/fs";
|
|
|
|
this.plugUrl = url + "/plug";
|
|
|
|
this.watch();
|
|
|
|
this.pollPlugs();
|
|
|
|
this.updatePageListAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public watchPage(pageName: string) {
|
|
|
|
this.watchedPages.add(pageName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public unwatchPage(pageName: string) {
|
|
|
|
this.watchedPages.delete(pageName);
|
|
|
|
}
|
|
|
|
|
|
|
|
watch() {
|
|
|
|
setInterval(() => {
|
|
|
|
safeRun(async () => {
|
|
|
|
if (this.saving) {
|
|
|
|
return;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
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;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
const newMeta = await this.getPageMeta(pageName);
|
|
|
|
if (oldMeta.lastModified !== newMeta.lastModified) {
|
|
|
|
console.log("Page", pageName, "changed on disk, emitting event");
|
|
|
|
this.emit("pageChanged", newMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, pageWatchInterval);
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
safeRun(this.pollPlugs.bind(this));
|
|
|
|
}, plugWatchInterval);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
public updatePageListAsync() {
|
|
|
|
safeRun(async () => {
|
|
|
|
let req = await fetch(this.pageUrl, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
|
|
|
|
let deletedPages = new Set<string>(this.pageMetaCache.keys());
|
|
|
|
((await req.json()) as any[]).forEach((meta: any) => {
|
|
|
|
const pageName = meta.name;
|
|
|
|
const oldPageMeta = this.pageMetaCache.get(pageName);
|
|
|
|
const newPageMeta = {
|
|
|
|
name: pageName,
|
|
|
|
lastModified: meta.lastModified,
|
|
|
|
};
|
|
|
|
if (!oldPageMeta && !this.initialPageListLoad) {
|
|
|
|
this.emit("pageCreated", newPageMeta);
|
|
|
|
} else if (
|
|
|
|
oldPageMeta &&
|
|
|
|
oldPageMeta.lastModified !== newPageMeta.lastModified
|
|
|
|
) {
|
|
|
|
this.emit("pageChanged", newPageMeta);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
// Page found, not deleted
|
|
|
|
deletedPages.delete(pageName);
|
|
|
|
|
|
|
|
// Update in cache
|
|
|
|
this.pageMetaCache.set(pageName, newPageMeta);
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
2022-03-31 12:28:07 +00:00
|
|
|
|
|
|
|
for (const deletedPage of deletedPages) {
|
|
|
|
this.pageMetaCache.delete(deletedPage);
|
|
|
|
this.emit("pageDeleted", deletedPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.emit("pageListUpdated", new Set([...this.pageMetaCache.values()]));
|
|
|
|
this.initialPageListLoad = false;
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
public async listPages(): Promise<Set<PageMeta>> {
|
|
|
|
// this.updatePageListAsync();
|
|
|
|
return new Set([...this.pageMetaCache.values()]);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
private responseToMetaCacher(name: string, res: Response): PageMeta {
|
|
|
|
const meta = {
|
|
|
|
name,
|
|
|
|
lastModified: +(res.headers.get("Last-Modified") || "0"),
|
|
|
|
};
|
|
|
|
this.pageMetaCache.set(name, meta);
|
|
|
|
return meta;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
public async readPage(
|
|
|
|
name: string
|
|
|
|
): Promise<{ text: string; meta: PageMeta }> {
|
|
|
|
let res = await fetch(`${this.pageUrl}/${name}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
text: await res.text(),
|
|
|
|
meta: this.responseToMetaCacher(name, res),
|
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
public async writePage(
|
|
|
|
name: string,
|
|
|
|
text: string,
|
|
|
|
selfUpdate?: boolean
|
|
|
|
): Promise<PageMeta> {
|
|
|
|
try {
|
|
|
|
this.saving = true;
|
|
|
|
let res = await fetch(`${this.pageUrl}/${name}`, {
|
|
|
|
method: "PUT",
|
|
|
|
body: text,
|
|
|
|
});
|
|
|
|
const newMeta = this.responseToMetaCacher(name, res);
|
|
|
|
if (!selfUpdate) {
|
|
|
|
this.emit("pageChanged", newMeta);
|
|
|
|
}
|
|
|
|
return newMeta;
|
|
|
|
} finally {
|
|
|
|
this.saving = false;
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
public async deletePage(name: string): Promise<void> {
|
|
|
|
let req = await fetch(`${this.pageUrl}/${name}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
});
|
|
|
|
if (req.status !== 200) {
|
|
|
|
throw Error(`Failed to delete page: ${req.statusText}`);
|
|
|
|
}
|
|
|
|
this.pageMetaCache.delete(name);
|
|
|
|
this.emit("pageDeleted", name);
|
|
|
|
this.emit("pageListUpdated", new Set([...this.pageMetaCache.values()]));
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
private async getPageMeta(name: string): Promise<PageMeta> {
|
|
|
|
let res = await fetch(`${this.pageUrl}/${name}`, {
|
|
|
|
method: "OPTIONS",
|
|
|
|
});
|
|
|
|
return this.responseToMetaCacher(name, res);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
async remoteSyscall(
|
|
|
|
plug: Plug<any>,
|
|
|
|
name: string,
|
|
|
|
args: any[]
|
|
|
|
): Promise<any> {
|
|
|
|
let req = await fetch(`${this.plugUrl}/${plug.name}/syscall/${name}`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(args),
|
|
|
|
});
|
|
|
|
if (req.status !== 200) {
|
|
|
|
let error = await req.text();
|
|
|
|
throw Error(error);
|
|
|
|
}
|
|
|
|
if (req.headers.get("Content-length") === "0") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return await req.json();
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
async remoteInvoke(plug: Plug<any>, name: string, args: any[]): Promise<any> {
|
|
|
|
let req = await fetch(`${this.plugUrl}/${plug.name}/function/${name}`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(args),
|
|
|
|
});
|
|
|
|
if (req.status !== 200) {
|
|
|
|
let error = await req.text();
|
|
|
|
throw Error(error);
|
|
|
|
}
|
|
|
|
if (req.headers.get("Content-length") === "0") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return await req.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async pollPlugs(): Promise<void> {
|
|
|
|
const newPlugs = await this.loadPlugs();
|
|
|
|
let deletedPlugs = new Set<string>(this.plugMetaCache.keys());
|
|
|
|
for (const newPlugMeta of newPlugs) {
|
|
|
|
const oldPlugMeta = this.plugMetaCache.get(newPlugMeta.name);
|
|
|
|
if (
|
|
|
|
!oldPlugMeta ||
|
|
|
|
(oldPlugMeta && oldPlugMeta.version !== newPlugMeta.version)
|
|
|
|
) {
|
|
|
|
this.emit(
|
|
|
|
"plugLoaded",
|
|
|
|
newPlugMeta.name,
|
|
|
|
await this.loadPlug(newPlugMeta.name)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Page found, not deleted
|
|
|
|
deletedPlugs.delete(newPlugMeta.name);
|
|
|
|
|
|
|
|
// Update in cache
|
|
|
|
this.plugMetaCache.set(newPlugMeta.name, newPlugMeta);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const deletedPlug of deletedPlugs) {
|
|
|
|
this.plugMetaCache.delete(deletedPlug);
|
|
|
|
this.emit("plugUnloaded", deletedPlug);
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
private async loadPlugs(): Promise<PlugMeta[]> {
|
|
|
|
let res = await fetch(`${this.plugUrl}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
return (await res.json()) as PlugMeta[];
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
private async loadPlug(name: string): Promise<Manifest> {
|
|
|
|
let res = await fetch(`${this.plugUrl}/${name}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
return (await res.json()) as Manifest;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
}
|