Files
plainleaf/client/lib/util.ts
T

44 lines
1.4 KiB
TypeScript

import type { FileMeta } from "@silverbulletmd/silverbullet/type/index";
export function utcDateString(mtime: number): string {
return new Date(mtime).toUTCString();
}
export function authCookieName(host: string) {
return `auth_${host.replaceAll(/\W/g, "_")}`;
}
export function fileMetaToHeaders(fileMeta: FileMeta) {
return {
"Content-Type": fileMeta.contentType,
"X-Last-Modified": `${fileMeta.lastModified}`,
"X-Created": `${fileMeta.created}`,
"Cache-Control": "no-cache",
"X-Permission": fileMeta.perm,
"X-Content-Length": `${fileMeta.size}`,
};
}
export function headersToFileMeta(
name: string,
headers: Headers,
): FileMeta | undefined {
if (headers.has("X-Last-Modified")) {
// If this header is set, we need to pull out the rest also
return {
name,
// The server may set a custom X-Content-Length header in case a GET request was sent with X-Get-Meta, in which case the body may be omitted
size: headers.has("X-Content-Length")
? +headers.get("X-Content-Length")!
: +headers.get("Content-Length")!,
contentType: headers.get("X-Content-Type") ?? headers.get("Content-type")!,
created: +(headers.get("X-Created") || "0"),
lastModified: +(headers.get("X-Last-Modified") || "0"),
perm: (headers.get("X-Permission") as "rw" | "ro") || "ro",
};
} else {
// Otherwise: no file meta in headers
return undefined;
}
}