2022-09-05 09:47:30 +00:00
|
|
|
import { AttachmentMeta, PageMeta } from "../types";
|
2022-04-25 08:33:38 +00:00
|
|
|
import { Plug } from "@plugos/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-09-05 09:47:30 +00:00
|
|
|
fsUrl: string;
|
|
|
|
fsaUrl: string;
|
2022-03-31 12:28:07 +00:00
|
|
|
private plugUrl: string;
|
2022-04-29 16:54:27 +00:00
|
|
|
token?: string;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-29 16:54:27 +00:00
|
|
|
constructor(url: string, token?: string) {
|
2022-09-05 09:47:30 +00:00
|
|
|
this.fsUrl = url + "/page";
|
|
|
|
this.fsaUrl = url + "/attachment";
|
2022-03-31 12:28:07 +00:00
|
|
|
this.plugUrl = url + "/plug";
|
2022-04-29 16:54:27 +00:00
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async authenticatedFetch(
|
|
|
|
url: string,
|
|
|
|
options: any
|
|
|
|
): Promise<Response> {
|
|
|
|
if (this.token) {
|
|
|
|
options.headers = options.headers || {};
|
|
|
|
options.headers["Authorization"] = `Bearer ${this.token}`;
|
|
|
|
}
|
|
|
|
let result = await fetch(url, options);
|
|
|
|
if (result.status === 401) {
|
|
|
|
throw Error("Unauthorized");
|
|
|
|
}
|
|
|
|
return result;
|
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-09-05 09:47:30 +00:00
|
|
|
let req = await this.authenticatedFetch(this.fsUrl, {
|
2022-04-06 13:39:20 +00:00
|
|
|
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-05-17 09:53:17 +00:00
|
|
|
perm: "rw",
|
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-09-05 09:47:30 +00:00
|
|
|
let res = await this.authenticatedFetch(`${this.fsUrl}/${name}`, {
|
2022-03-31 12:28:07 +00:00
|
|
|
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-09-05 09:47:30 +00:00
|
|
|
meta: this.responseToPageMeta(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
|
2022-09-05 09:47:30 +00:00
|
|
|
let res = await this.authenticatedFetch(`${this.fsUrl}/${name}`, {
|
2022-04-06 13:39:20 +00:00
|
|
|
method: "PUT",
|
|
|
|
body: text,
|
|
|
|
headers: lastModified
|
|
|
|
? {
|
|
|
|
"Last-Modified": "" + lastModified,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
});
|
2022-09-05 09:47:30 +00:00
|
|
|
const newMeta = this.responseToPageMeta(name, res);
|
2022-04-06 13:39:20 +00:00
|
|
|
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-09-05 09:47:30 +00:00
|
|
|
let req = await this.authenticatedFetch(`${this.fsUrl}/${name}`, {
|
2022-03-31 12:28:07 +00:00
|
|
|
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-04-29 16:54:27 +00:00
|
|
|
let req = await this.authenticatedFetch(
|
|
|
|
`${this.plugUrl}/${plug.name}/syscall/${name}`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(args),
|
|
|
|
}
|
|
|
|
);
|
2022-03-31 12:28:07 +00:00
|
|
|
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-09-05 09:47:30 +00:00
|
|
|
// Attachments
|
|
|
|
public async fetchAttachmentList(): Promise<{
|
|
|
|
attachments: Set<AttachmentMeta>;
|
|
|
|
nowTimestamp: number;
|
|
|
|
}> {
|
|
|
|
let req = await this.authenticatedFetch(this.fsaUrl, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = new Set<AttachmentMeta>();
|
|
|
|
((await req.json()) as any[]).forEach((meta: any) => {
|
|
|
|
const pageName = meta.name;
|
|
|
|
result.add({
|
|
|
|
name: pageName,
|
|
|
|
size: meta.size,
|
|
|
|
lastModified: meta.lastModified,
|
|
|
|
contentType: meta.contentType,
|
|
|
|
perm: "rw",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
attachments: result,
|
|
|
|
nowTimestamp: +req.headers.get("Now-Timestamp")!,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async readAttachment(
|
|
|
|
name: string
|
|
|
|
): Promise<{ buffer: ArrayBuffer; meta: AttachmentMeta }> {
|
|
|
|
let res = await this.authenticatedFetch(`${this.fsaUrl}/${name}`, {
|
|
|
|
method: "GET",
|
|
|
|
});
|
|
|
|
if (res.headers.get("X-Status") === "404") {
|
|
|
|
throw new Error(`Page not found`);
|
|
|
|
}
|
|
|
|
let blob = await res.blob();
|
|
|
|
return {
|
|
|
|
buffer: await blob.arrayBuffer(),
|
|
|
|
meta: this.responseToAttachmentMeta(name, res),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async writeAttachment(
|
|
|
|
name: string,
|
|
|
|
buffer: ArrayBuffer,
|
|
|
|
selfUpdate?: boolean,
|
|
|
|
lastModified?: number
|
|
|
|
): Promise<AttachmentMeta> {
|
|
|
|
// TODO: lastModified ignored for now
|
|
|
|
let res = await this.authenticatedFetch(`${this.fsaUrl}/${name}`, {
|
|
|
|
method: "PUT",
|
|
|
|
body: buffer,
|
|
|
|
headers: {
|
|
|
|
"Last-Modified": lastModified ? "" + lastModified : undefined,
|
|
|
|
"Content-type": "application/octet-stream",
|
|
|
|
"Content-length": "" + buffer.byteLength,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const newMeta = this.responseToAttachmentMeta(name, res);
|
|
|
|
return newMeta;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAttachmentMeta(name: string): Promise<AttachmentMeta> {
|
|
|
|
let res = await this.authenticatedFetch(`${this.fsaUrl}/${name}`, {
|
|
|
|
method: "OPTIONS",
|
|
|
|
});
|
|
|
|
if (res.headers.get("X-Status") === "404") {
|
|
|
|
throw new Error(`Page not found`);
|
|
|
|
}
|
|
|
|
return this.responseToAttachmentMeta(name, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAttachment(name: string): Promise<void> {
|
|
|
|
let req = await this.authenticatedFetch(`${this.fsaUrl}/${name}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
});
|
|
|
|
if (req.status !== 200) {
|
|
|
|
throw Error(`Failed to delete attachment: ${req.statusText}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plugs
|
|
|
|
|
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-04-29 16:54:27 +00:00
|
|
|
let req = await this.authenticatedFetch(
|
|
|
|
`${this.plugUrl}/${plug.name}/function/${name}`,
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(args),
|
|
|
|
}
|
|
|
|
);
|
2022-03-31 12:28:07 +00:00
|
|
|
if (req.status !== 200) {
|
|
|
|
let error = await req.text();
|
|
|
|
throw Error(error);
|
|
|
|
}
|
|
|
|
if (req.headers.get("Content-length") === "0") {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-30 10:57:30 +00:00
|
|
|
if (req.headers.get("Content-type")?.includes("application/json")) {
|
2022-04-25 17:46:08 +00:00
|
|
|
return await req.json();
|
|
|
|
} else {
|
|
|
|
return await req.text();
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 15:02:17 +00:00
|
|
|
async getPageMeta(name: string): Promise<PageMeta> {
|
2022-09-05 09:47:30 +00:00
|
|
|
let res = await this.authenticatedFetch(`${this.fsUrl}/${name}`, {
|
2022-04-05 15:02:17 +00:00
|
|
|
method: "OPTIONS",
|
|
|
|
});
|
2022-04-06 13:39:20 +00:00
|
|
|
if (res.headers.get("X-Status") === "404") {
|
|
|
|
throw new Error(`Page not found`);
|
|
|
|
}
|
2022-09-05 09:47:30 +00:00
|
|
|
return this.responseToPageMeta(name, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private responseToPageMeta(name: string, res: Response): PageMeta {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
lastModified: +(res.headers.get("Last-Modified") || "0"),
|
|
|
|
perm: (res.headers.get("X-Permission") as "rw" | "ro") || "rw",
|
|
|
|
};
|
2022-04-05 15:02:17 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 09:47:30 +00:00
|
|
|
private responseToAttachmentMeta(
|
|
|
|
name: string,
|
|
|
|
res: Response
|
|
|
|
): AttachmentMeta {
|
2022-05-17 09:53:17 +00:00
|
|
|
return {
|
2022-04-05 15:02:17 +00:00
|
|
|
name,
|
|
|
|
lastModified: +(res.headers.get("Last-Modified") || "0"),
|
2022-09-05 09:47:30 +00:00
|
|
|
size: +(res.headers.get("Content-Length") || "0"),
|
|
|
|
contentType:
|
|
|
|
res.headers.get("Content-Type") || "application/octet-stream",
|
2022-05-17 09:53:17 +00:00
|
|
|
perm: (res.headers.get("X-Permission") as "rw" | "ro") || "rw",
|
2022-04-05 15:02:17 +00:00
|
|
|
};
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|