2022-10-10 12:50:21 +00:00
|
|
|
import { AttachmentMeta, FileMeta, PageMeta } from "../types.ts";
|
|
|
|
import { Plug } from "../../plugos/plug.ts";
|
|
|
|
import { FileData, FileEncoding, SpacePrimitives } from "./space_primitives.ts";
|
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;
|
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-12 12:50:37 +00:00
|
|
|
this.fsUrl = url + "/fs";
|
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-09-12 12:50:37 +00:00
|
|
|
public async fetchFileList(): Promise<FileMeta[]> {
|
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-09-12 12:50:37 +00:00
|
|
|
let result: FileMeta[] = await req.json();
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
return result;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
async readFile(
|
|
|
|
name: string,
|
|
|
|
encoding: FileEncoding
|
|
|
|
): Promise<{ data: FileData; meta: FileMeta }> {
|
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-09-12 12:50:37 +00:00
|
|
|
if (res.status === 404) {
|
2022-04-06 13:39:20 +00:00
|
|
|
throw new Error(`Page not found`);
|
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
let data: FileData | null = null;
|
|
|
|
switch (encoding) {
|
|
|
|
case "arraybuffer":
|
2022-10-10 12:50:21 +00:00
|
|
|
{
|
|
|
|
let abBlob = await res.blob();
|
|
|
|
data = await abBlob.arrayBuffer();
|
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
break;
|
|
|
|
case "dataurl":
|
2022-10-10 12:50:21 +00:00
|
|
|
{
|
|
|
|
let dUBlob = await res.blob();
|
|
|
|
data = arrayBufferToDataUrl(await dUBlob.arrayBuffer());
|
|
|
|
}
|
2022-09-12 12:50:37 +00:00
|
|
|
break;
|
|
|
|
case "string":
|
|
|
|
data = await res.text();
|
|
|
|
break;
|
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
return {
|
2022-09-12 12:50:37 +00:00
|
|
|
data: data,
|
|
|
|
meta: this.responseToMeta(name, res),
|
2022-03-31 12:28:07 +00:00
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
async writeFile(
|
2022-03-31 12:28:07 +00:00
|
|
|
name: string,
|
2022-09-12 12:50:37 +00:00
|
|
|
encoding: FileEncoding,
|
|
|
|
data: FileData,
|
|
|
|
selfUpdate?: boolean
|
|
|
|
): Promise<FileMeta> {
|
|
|
|
let body: any = null;
|
|
|
|
|
|
|
|
switch (encoding) {
|
|
|
|
case "arraybuffer":
|
|
|
|
case "string":
|
|
|
|
body = data;
|
|
|
|
break;
|
|
|
|
case "dataurl":
|
|
|
|
data = dataUrlToArrayBuffer(data as string);
|
|
|
|
break;
|
|
|
|
}
|
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",
|
2022-09-12 12:50:37 +00:00
|
|
|
headers: {
|
|
|
|
"Content-type": "application/octet-stream",
|
|
|
|
},
|
|
|
|
body,
|
2022-04-06 13:39:20 +00:00
|
|
|
});
|
2022-09-12 12:50:37 +00:00
|
|
|
const newMeta = this.responseToMeta(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-09-12 12:50:37 +00:00
|
|
|
async deleteFile(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) {
|
2022-09-12 12:50:37 +00:00
|
|
|
throw Error(`Failed to delete file: ${req.statusText}`);
|
2022-03-31 12:28:07 +00:00
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 12:50:37 +00:00
|
|
|
async getFileMeta(name: string): Promise<FileMeta> {
|
|
|
|
let res = await this.authenticatedFetch(`${this.fsUrl}/${name}`, {
|
|
|
|
method: "OPTIONS",
|
|
|
|
});
|
|
|
|
if (res.status === 404) {
|
|
|
|
throw new Error(`File not found`);
|
|
|
|
}
|
|
|
|
return this.responseToMeta(name, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
private responseToMeta(name: string, res: Response): FileMeta {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
size: +res.headers.get("Content-length")!,
|
|
|
|
contentType: res.headers.get("Content-type")!,
|
|
|
|
lastModified: +(res.headers.get("Last-Modified") || "0"),
|
|
|
|
perm: (res.headers.get("X-Permission") as "rw" | "ro") || "rw",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plugs
|
|
|
|
|
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-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-03-20 08:56:28 +00:00
|
|
|
}
|
2022-09-05 14:15:01 +00:00
|
|
|
|
|
|
|
function dataUrlToArrayBuffer(dataUrl: string): ArrayBuffer {
|
|
|
|
var binary_string = window.atob(dataUrl.split(",")[1]);
|
|
|
|
var len = binary_string.length;
|
|
|
|
var bytes = new Uint8Array(len);
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
bytes[i] = binary_string.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return bytes.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
function arrayBufferToDataUrl(buffer: ArrayBuffer): string {
|
|
|
|
var binary = "";
|
|
|
|
var bytes = new Uint8Array(buffer);
|
|
|
|
var len = bytes.byteLength;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
binary += String.fromCharCode(bytes[i]);
|
|
|
|
}
|
|
|
|
return `data:application/octet-stream,${window.btoa(binary)}`;
|
|
|
|
}
|