2022-04-05 15:02:17 +00:00
|
|
|
import { PageMeta } from "../../common/types";
|
|
|
|
import { Plug } from "../../plugos/plug";
|
2022-04-07 13:21:30 +00:00
|
|
|
import { SpacePrimitives } from "./space_primitives";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-07 13:21:30 +00:00
|
|
|
export class HttpSpacePrimitives implements SpacePrimitives {
|
2022-03-31 12:28:07 +00:00
|
|
|
pageUrl: string;
|
|
|
|
private plugUrl: string;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
constructor(url: string) {
|
|
|
|
this.pageUrl = url + "/fs";
|
|
|
|
this.plugUrl = url + "/plug";
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:04:50 +00:00
|
|
|
public async fetchPageList(): Promise<{
|
|
|
|
pages: Set<PageMeta>;
|
|
|
|
nowTimestamp: number;
|
|
|
|
}> {
|
2022-04-06 13:39:20 +00:00
|
|
|
let req = await fetch(this.pageUrl, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
2022-03-31 12:28:07 +00:00
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
let result = new Set<PageMeta>();
|
|
|
|
((await req.json()) as any[]).forEach((meta: any) => {
|
|
|
|
const pageName = meta.name;
|
|
|
|
result.add({
|
|
|
|
name: pageName,
|
|
|
|
lastModified: meta.lastModified,
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-07 12:04:50 +00:00
|
|
|
return {
|
|
|
|
pages: result,
|
|
|
|
nowTimestamp: +req.headers.get("Now-Timestamp")!,
|
|
|
|
};
|
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",
|
|
|
|
});
|
2022-04-06 13:39:20 +00:00
|
|
|
if (res.headers.get("X-Status") === "404") {
|
|
|
|
throw new Error(`Page not found`);
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
return {
|
|
|
|
text: await res.text(),
|
2022-04-06 13:39:20 +00:00
|
|
|
meta: this.responseToMeta(name, res),
|
2022-03-31 12:28:07 +00:00
|
|
|
};
|
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,
|
2022-04-06 13:39:20 +00:00
|
|
|
lastModified?: number
|
2022-03-31 12:28:07 +00:00
|
|
|
): Promise<PageMeta> {
|
2022-04-06 13:39:20 +00:00
|
|
|
// TODO: lastModified ignored for now
|
|
|
|
let res = await fetch(`${this.pageUrl}/${name}`, {
|
|
|
|
method: "PUT",
|
|
|
|
body: text,
|
|
|
|
headers: lastModified
|
|
|
|
? {
|
|
|
|
"Last-Modified": "" + lastModified,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
});
|
|
|
|
const newMeta = this.responseToMeta(name, res);
|
|
|
|
return newMeta;
|
2022-03-31 12:28:07 +00:00
|
|
|
}
|
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}`);
|
|
|
|
}
|
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",
|
|
|
|
});
|
2022-04-06 13:39:20 +00:00
|
|
|
if (res.headers.get("X-Status") === "404") {
|
|
|
|
throw new Error(`Page not found`);
|
|
|
|
}
|
|
|
|
return this.responseToMeta(name, res);
|
2022-04-05 15:02:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 13:39:20 +00:00
|
|
|
private responseToMeta(name: string, res: Response): PageMeta {
|
2022-04-05 15:02:17 +00:00
|
|
|
const meta = {
|
|
|
|
name,
|
|
|
|
lastModified: +(res.headers.get("Last-Modified") || "0"),
|
|
|
|
};
|
|
|
|
return meta;
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|