2022-04-05 15:02:17 +00:00
|
|
|
import { EventEmitter } from "../../common/event";
|
|
|
|
import { PageMeta } from "../../common/types";
|
|
|
|
import { safeRun } from "../util";
|
|
|
|
import { Plug } from "../../plugos/plug";
|
|
|
|
import { Manifest } from "../../common/manifest";
|
|
|
|
import { PlugMeta, Space, SpaceEvents } from "./space";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
const pageWatchInterval = 2000;
|
|
|
|
const plugWatchInterval = 5000;
|
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
export class HttpRestSpace extends EventEmitter<SpaceEvents> implements Space {
|
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;
|
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();
|
|
|
|
}
|
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
watchPage(pageName: string) {
|
2022-03-31 12:28:07 +00:00
|
|
|
this.watchedPages.add(pageName);
|
|
|
|
}
|
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
unwatchPage(pageName: string) {
|
2022-03-31 12:28:07 +00:00
|
|
|
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-04-05 15:02:17 +00:00
|
|
|
async listPages(): Promise<Set<PageMeta>> {
|
2022-03-31 12:28:07 +00:00
|
|
|
return new Set([...this.pageMetaCache.values()]);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
|
2022-03-31 12:28:07 +00:00
|
|
|
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-04-05 15:02:17 +00:00
|
|
|
async writePage(
|
2022-03-31 12:28:07 +00:00
|
|
|
name: string,
|
|
|
|
text: string,
|
2022-04-05 15:02:17 +00:00
|
|
|
selfUpdate?: boolean,
|
|
|
|
withMeta?: PageMeta
|
2022-03-31 12:28:07 +00:00
|
|
|
): Promise<PageMeta> {
|
2022-04-05 15:02:17 +00:00
|
|
|
// TODO: withMeta ignored for now
|
2022-03-31 12:28:07 +00:00
|
|
|
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-04-05 15:02:17 +00:00
|
|
|
async deletePage(name: string): Promise<void> {
|
2022-03-31 12:28:07 +00:00
|
|
|
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-04-05 15:02:17 +00:00
|
|
|
async proxySyscall(plug: Plug<any>, name: string, args: any[]): Promise<any> {
|
2022-03-31 12:28:07 +00:00
|
|
|
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-04-05 15:02:17 +00:00
|
|
|
async invokeFunction(
|
|
|
|
plug: Plug<any>,
|
|
|
|
env: string,
|
|
|
|
name: string,
|
|
|
|
args: any[]
|
|
|
|
): Promise<any> {
|
|
|
|
// Invoke locally
|
|
|
|
if (!env || env === "client") {
|
|
|
|
return plug.invoke(name, args);
|
|
|
|
}
|
|
|
|
// Or dispatch to server
|
2022-03-31 12:28:07 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
async getPageMeta(name: string): Promise<PageMeta> {
|
|
|
|
let res = await fetch(`${this.pageUrl}/${name}`, {
|
|
|
|
method: "OPTIONS",
|
|
|
|
});
|
|
|
|
return this.responseToMetaCacher(name, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async listPlugs(): Promise<PlugMeta[]> {
|
|
|
|
let res = await fetch(`${this.plugUrl}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
return (await res.json()) as PlugMeta[];
|
|
|
|
}
|
|
|
|
|
|
|
|
public async loadPlug(name: string): Promise<Manifest> {
|
|
|
|
let res = await fetch(`${this.plugUrl}/${name}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
return (await res.json()) as Manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-31 12:28:07 +00:00
|
|
|
private async pollPlugs(): Promise<void> {
|
2022-04-05 15:02:17 +00:00
|
|
|
const newPlugs = await this.listPlugs();
|
2022-03-31 12:28:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|