Files
heygo/worker/lib/responses.ts
T

89 lines
3.1 KiB
TypeScript

export function htmlResponse(body: string, init: ResponseInit = {}): Response {
const headers = new Headers(init.headers);
if (!headers.has('content-type')) {
headers.set('content-type', 'text/html; charset=utf-8');
}
return new Response(body, {
...init,
headers,
});
}
function withCookieVary(headers: Headers): Headers {
const vary = headers.get('vary');
const varies = vary?.split(',').map((value) => value.trim().toLowerCase()) ?? [];
if (!varies.includes('cookie')) {
headers.set('vary', vary ? `${vary}, Cookie` : 'Cookie');
}
return headers;
}
function privateNoStoreHeaders(headersInit?: HeadersInit): Headers {
const headers = new Headers(headersInit);
headers.set('cache-control', 'no-store');
return withCookieVary(headers);
}
export function withPrivateNoStoreHeaders(response: Response): Response {
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: privateNoStoreHeaders(response.headers),
});
}
export function privateHtmlResponse(body: string, init: ResponseInit = {}): Response {
return htmlResponse(body, {
...init,
headers: privateNoStoreHeaders(init.headers),
});
}
export function privateRedirectResponse(location: string, status = 302): Response {
return new Response(null, {
status,
headers: privateNoStoreHeaders({ location }),
});
}
export function publicNotFoundResponse(): Response {
return htmlResponse(
'<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Not found</title></head><body><h1>Not found</h1><p>This short link does not exist.</p></body></html>',
{ status: 404 },
);
}
export function publicBadRequestResponse(message = 'Invalid short link'): Response {
return htmlResponse(
`<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Invalid short link</title></head><body><h1>Invalid short link</h1><p>${escapeHtml(message)}</p></body></html>`,
{ status: 400 },
);
}
export const PRIVATE_LOGIN_URL = 'https://heygo.cc/app/login';
export const PRIVATE_APP_URL = 'https://heygo.cc/app/private';
export function privateLoginRequiredResponse(): Response {
return privateHtmlResponse(
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Login required</title></head><body><h1>Not found</h1><p><a href="${PRIVATE_LOGIN_URL}">Login to use your private links</a>.</p></body></html>`,
{ status: 404 },
);
}
export function privateAliasNotFoundResponse(): Response {
return privateHtmlResponse(
`<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Not found</title></head><body><h1>Not found</h1><p>This private link does not exist.</p><p><a href="${PRIVATE_APP_URL}">Create this private link</a>.</p></body></html>`,
{ status: 404 },
);
}
export function escapeHtml(value: string): string {
return value
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}