1
0
silverbullet/common/spaces/http_space_primitives.ts

229 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-10-15 17:02:56 +00:00
import { FileMeta } from "../types.ts";
import { Plug } from "../../plugos/plug.ts";
import { FileData, FileEncoding, SpacePrimitives } from "./space_primitives.ts";
2022-10-19 07:52:29 +00:00
import {
base64DecodeDataUrl,
2023-01-13 14:41:29 +00:00
base64Encode,
2022-10-19 07:52:29 +00:00
base64EncodedDataUrl,
} from "../../plugos/asset_bundle/base64.ts";
import { mime } from "../../plugos/deps.ts";
2022-04-07 13:21:30 +00:00
export class HttpSpacePrimitives implements SpacePrimitives {
2023-01-13 14:41:29 +00:00
private fsUrl: string;
private plugUrl: string;
2023-01-13 14:41:29 +00:00
constructor(
url: string,
readonly user?: string,
readonly password?: string,
readonly base64Put?: boolean,
) {
2022-09-12 12:50:37 +00:00
this.fsUrl = url + "/fs";
this.plugUrl = url + "/plug";
2022-04-29 16:54:27 +00:00
}
private async authenticatedFetch(
url: string,
2023-01-13 14:41:29 +00:00
options: Record<string, any>,
2022-04-29 16:54:27 +00:00
): Promise<Response> {
2023-01-13 14:41:29 +00:00
if (this.user && this.password) {
// Explicitly set an auth cookie
if (!options.headers) {
options.headers = {};
}
options.headers["cookie"] = `auth=${
btoa(`${this.user}:${this.password}`)
}`;
}
2022-10-15 17:02:56 +00:00
const result = await fetch(url, options);
2023-01-13 14:41:29 +00:00
if (result.status === 401 || result.redirected) {
2022-12-22 10:21:12 +00:00
// Invalid credentials, reloading the browser should trigger authentication
2023-01-13 14:41:29 +00:00
if (typeof location !== "undefined") {
location.reload();
}
2022-04-29 16:54:27 +00:00
throw Error("Unauthorized");
}
return result;
}
2023-01-13 14:41:29 +00:00
async fetchFileList(): Promise<FileMeta[]> {
2022-10-15 17:02:56 +00:00
const req = await this.authenticatedFetch(this.fsUrl, {
method: "GET",
});
2022-10-15 17:02:56 +00:00
return req.json();
}
2022-09-12 12:50:37 +00:00
async readFile(
name: string,
encoding: FileEncoding,
2022-09-12 12:50:37 +00:00
): Promise<{ data: FileData; meta: FileMeta }> {
2023-01-13 14:41:29 +00:00
const res = await this.authenticatedFetch(
`${this.fsUrl}/${encodeURI(name)}`,
{
method: "GET",
},
);
2022-09-12 12:50:37 +00:00
if (res.status === 404) {
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-19 07:52:29 +00:00
data = await res.arrayBuffer();
}
2022-09-12 12:50:37 +00:00
break;
case "dataurl":
{
2022-10-19 07:52:29 +00:00
data = base64EncodedDataUrl(
mime.getType(name) || "application/octet-stream",
new Uint8Array(await res.arrayBuffer()),
);
}
2022-09-12 12:50:37 +00:00
break;
2023-01-13 14:41:29 +00:00
case "utf8":
2022-09-12 12:50:37 +00:00
data = await res.text();
break;
}
return {
2022-09-12 12:50:37 +00:00
data: data,
meta: this.responseToMeta(name, res),
};
}
2022-09-12 12:50:37 +00:00
async writeFile(
name: string,
2022-09-12 12:50:37 +00:00
encoding: FileEncoding,
data: FileData,
): Promise<FileMeta> {
let body: any = null;
switch (encoding) {
case "arraybuffer":
2023-01-13 14:41:29 +00:00
// actually we want an Uint8Array
body = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
break;
case "utf8":
2022-09-12 12:50:37 +00:00
body = data;
break;
case "dataurl":
2022-10-19 07:52:29 +00:00
data = base64DecodeDataUrl(data as string);
2022-09-12 12:50:37 +00:00
break;
}
2023-01-13 14:41:29 +00:00
const headers: Record<string, string> = {
"Content-Type": "application/octet-stream",
};
if (this.base64Put) {
headers["X-Content-Base64"] = "true";
headers["Content-Type"] = "text/plain";
body = base64Encode(body);
}
const res = await this.authenticatedFetch(
`${this.fsUrl}/${encodeURI(name)}`,
{
method: "PUT",
headers,
body,
2022-09-12 12:50:37 +00:00
},
2023-01-13 14:41:29 +00:00
);
2022-09-12 12:50:37 +00:00
const newMeta = this.responseToMeta(name, res);
return newMeta;
}
2022-09-12 12:50:37 +00:00
async deleteFile(name: string): Promise<void> {
2023-01-13 14:41:29 +00:00
const req = await this.authenticatedFetch(
`${this.fsUrl}/${encodeURI(name)}`,
{
method: "DELETE",
},
);
if (req.status !== 200) {
2022-09-12 12:50:37 +00:00
throw Error(`Failed to delete file: ${req.statusText}`);
}
}
2022-09-12 12:50:37 +00:00
async getFileMeta(name: string): Promise<FileMeta> {
2023-01-13 14:41:29 +00:00
const res = await this.authenticatedFetch(
`${this.fsUrl}/${encodeURI(name)}`,
{
method: "OPTIONS",
},
);
2022-09-12 12:50:37 +00:00
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("X-Content-Length")!,
2022-09-12 12:50:37 +00:00
contentType: res.headers.get("Content-type")!,
lastModified: +(res.headers.get("X-Last-Modified") || "0"),
2022-09-12 12:50:37 +00:00
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-10-15 17:02:56 +00:00
const req = await this.authenticatedFetch(
2022-04-29 16:54:27 +00:00
`${this.plugUrl}/${plug.name}/syscall/${name}`,
{
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(args),
},
2022-04-29 16:54:27 +00:00
);
if (req.status !== 200) {
2022-10-15 17:02:56 +00:00
const 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 invokeFunction(
plug: Plug<any>,
env: string,
name: string,
args: any[],
2022-04-05 15:02:17 +00:00
): Promise<any> {
// Invoke locally
if (!env || env === "client") {
return plug.invoke(name, args);
}
// Or dispatch to server
2022-10-15 17:02:56 +00:00
const req = await this.authenticatedFetch(
2022-04-29 16:54:27 +00:00
`${this.plugUrl}/${plug.name}/function/${name}`,
{
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify(args),
},
2022-04-29 16:54:27 +00:00
);
if (req.status !== 200) {
2022-10-15 17:02:56 +00:00
const error = await req.text();
throw Error(error);
}
if (req.headers.get("Content-length") === "0") {
return;
}
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();
}
}
}