This commit is contained in:
Jesse Wierzbinski 2023-09-14 15:22:27 -10:00
parent aad3ee78d1
commit 8162a5050c
No known key found for this signature in database
GPG key ID: F9A1E418934E40B0
19 changed files with 922 additions and 84 deletions

View file

@ -8,10 +8,12 @@ export interface ConfigType {
password: string;
database: string;
};
http: {
port: number;
base_url: string;
};
validation: {
max_displayname_size: number;
max_bio_size: number;
@ -28,11 +30,98 @@ export interface ConfigType {
email_blacklist: string[];
url_scheme_whitelist: string[];
};
activitypub: {
use_tombstones: boolean;
};
[key: string]: unknown;
}
export const configDefaults: ConfigType = {
http: {
port: 3000,
base_url: "http://0.0.0.0",
},
database: {
host: "localhost",
port: 5432,
username: "postgres",
password: "postgres",
database: "fediproject",
},
validation: {
max_displayname_size: 50,
max_bio_size: 6000,
max_note_size: 5000,
max_avatar_size: 5_000_000,
max_header_size: 5_000_000,
max_media_size: 40_000_000,
max_media_attachments: 4,
max_media_description_size: 1000,
max_username_size: 30,
username_blacklist: [
".well-known",
"~",
"about",
"activities",
"api",
"auth",
"dev",
"inbox",
"internal",
"main",
"media",
"nodeinfo",
"notice",
"oauth",
"objects",
"proxy",
"push",
"registration",
"relay",
"settings",
"status",
"tag",
"users",
"web",
"search",
"mfa",
],
blacklist_tempmail: false,
email_blacklist: [],
url_scheme_whitelist: [
"http",
"https",
"ftp",
"dat",
"dweb",
"gopher",
"hyper",
"ipfs",
"ipns",
"irc",
"xmpp",
"ircs",
"magnet",
"mailto",
"mumble",
"ssb",
],
},
activitypub: {
use_tombstones: true,
},
};
export const getConfig = () => {
return data as ConfigType;
return {
...configDefaults,
...(data as ConfigType),
};
};
export const getHost = () => {

54
utils/request.ts Normal file
View file

@ -0,0 +1,54 @@
/**
* Takes a request, and turns FormData or query parameters
* into a JSON object as would be returned by req.json()
* This is a translation layer that allows clients to use
* either FormData, query parameters, or JSON in the request
* @param request The request to parse
*/
export async function parseRequest<T>(request: Request): Promise<Partial<T>> {
const formData = await request.formData();
const query = new URL(request.url).searchParams;
// if request contains a JSON body
if (request.headers.get("Content-Type")?.includes("application/json")) {
return (await request.json()) as T;
}
// If request contains FormData
if (request.headers.get("Content-Type")?.includes("multipart/form-data")) {
if ([...formData.entries()].length > 0) {
const data: Record<string, string | File> = {};
for (const [key, value] of formData.entries()) {
// If object, parse as JSON
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-base-to-string
data[key] = JSON.parse(value.toString());
} catch {
// If a file, set as a file
if (value instanceof File) {
data[key] = value;
}
// Otherwise, set as a string
// eslint-disable-next-line @typescript-eslint/no-base-to-string
data[key] = value.toString();
}
}
return data as T;
}
}
if ([...query.entries()].length > 0) {
const data: Record<string, string> = {};
for (const [key, value] of query.entries()) {
data[key] = value.toString();
}
return data as T;
}
return {};
}