@npio/filesystem
Version:
A free visual website editor, powered with your own SolidJS components.
61 lines (51 loc) • 1.51 kB
text/typescript
import type { FetchEvent } from "@solidjs/start/server";
import { getRequestEvent } from "solid-js/web";
import { Readable } from "stream";
import { createError, setResponseHeader } from "h3";
import type { H3Event } from "h3";
export const maxCacheAge = 365 * 24 * 60 * 60;
export const commonCacheAge = 24 * 60 * 60;
export const promisifyStream = (stream: Readable) =>
new Promise<void>((resolve, reject) => {
stream.on("error", reject);
stream.on("end", resolve);
});
const fontFileEndings = /\.(woff2?|otf|ttf)$/;
export const isFont = (name: string) => !!name.match(fontFileEndings);
/**
* Converts irregular content types to better ones
* @param type
* @returns
*/
export const contentType = (type: string) => {
if (type === "application/vnd.oasis.opendocument.formula-template") {
type = "font/otf";
}
return type;
};
/**
* Depending on the order of imported types,
* the event.nativeEvent types do not match with the ones,
* required by h3 utilities.
*/
export const h3Event = (event: FetchEvent) => event.nativeEvent as H3Event;
export const assertBodySize = ({
maxSize,
event = getRequestEvent()!,
}: {
maxSize: number;
message?: string;
event?: FetchEvent;
}) => {
const bodySize = parseFloat(
event.request.headers.get("content-length") ?? "0",
);
if (bodySize <= maxSize) {
return;
}
setResponseHeader(h3Event(event), "Connection", "close");
throw createError({
message: "Content Too Large",
statusCode: 413,
});
};