chore: npm run format + lint

This commit is contained in:
2025-12-30 11:23:55 -05:00
parent 55bee20e5d
commit d71b60bd7e
7 changed files with 134 additions and 116 deletions
+1 -1
View File
@@ -78,4 +78,4 @@ export const roles = {
1: 'Moderator', 1: 'Moderator',
255: 'Administrator', 255: 'Administrator',
403: 'Banned' 403: 'Banned'
} };
@@ -1,6 +1,6 @@
export interface Pagination { export interface Pagination {
page: number; page: number;
pageSize: number; pageSize: number;
totalCount: number; totalCount: number;
totalPages: number; totalPages: number;
} }
+24 -9
View File
@@ -6,12 +6,22 @@ const REFRESH_TOKEN_KEY = 'refreshToken';
export function getToken() { export function getToken() {
if (!browser) return null; if (!browser) return null;
return document.cookie.split('; ').find(c => c.startsWith(`${TOKEN_KEY}=`))?.split('=')[1] || null; return (
document.cookie
.split('; ')
.find((c) => c.startsWith(`${TOKEN_KEY}=`))
?.split('=')[1] || null
);
} }
export function getRefreshToken() { export function getRefreshToken() {
if (!browser) return null; if (!browser) return null;
return document.cookie.split('; ').find(c => c.startsWith(`${REFRESH_TOKEN_KEY}=`))?.split('=')[1] || null; return (
document.cookie
.split('; ')
.find((c) => c.startsWith(`${REFRESH_TOKEN_KEY}=`))
?.split('=')[1] || null
);
} }
export function setTokens(token: string, refreshToken: string) { export function setTokens(token: string, refreshToken: string) {
@@ -67,19 +77,24 @@ export function startTokenRefreshInterval() {
} }
export function getServerToken(cookies: Cookies) { export function getServerToken(cookies: Cookies) {
return cookies.get('token'); return cookies.get('token');
} }
export function getServerRefreshToken(cookies: Cookies) { export function getServerRefreshToken(cookies: Cookies) {
return cookies.get('refreshToken'); return cookies.get('refreshToken');
} }
export function setServerTokens(cookies: Cookies, token: string, refreshToken: string) { export function setServerTokens(cookies: Cookies, token: string, refreshToken: string) {
cookies.set('token', token, { path: '/', httpOnly: true, sameSite: 'strict', secure: true }); cookies.set('token', token, { path: '/', httpOnly: true, sameSite: 'strict', secure: true });
cookies.set('refreshToken', refreshToken, { path: '/', httpOnly: true, sameSite: 'strict', secure: true }); cookies.set('refreshToken', refreshToken, {
path: '/',
httpOnly: true,
sameSite: 'strict',
secure: true
});
} }
export function clearServerTokens(cookies: Cookies) { export function clearServerTokens(cookies: Cookies) {
cookies.delete('token', { path: '/' }); cookies.delete('token', { path: '/' });
cookies.delete('refreshToken', { path: '/' }); cookies.delete('refreshToken', { path: '/' });
} }
+83 -83
View File
@@ -1,87 +1,87 @@
export async function encryptAES(content: string, password: string): Promise<string> { export async function encryptAES(content: string, password: string): Promise<string> {
const enc = new TextEncoder(); const enc = new TextEncoder();
const keyMaterial = await window.crypto.subtle.importKey( const keyMaterial = await window.crypto.subtle.importKey(
'raw', 'raw',
enc.encode(password), enc.encode(password),
{ name: 'PBKDF2' }, { name: 'PBKDF2' },
false, false,
['deriveBits', 'deriveKey'] ['deriveBits', 'deriveKey']
); );
const salt = window.crypto.getRandomValues(new Uint8Array(16)); const salt = window.crypto.getRandomValues(new Uint8Array(16));
const key = await window.crypto.subtle.deriveKey( const key = await window.crypto.subtle.deriveKey(
{ {
name: 'PBKDF2', name: 'PBKDF2',
salt: salt, salt: salt,
iterations: 100000, iterations: 100000,
hash: 'SHA-256' hash: 'SHA-256'
}, },
keyMaterial, keyMaterial,
{ name: 'AES-GCM', length: 256 }, { name: 'AES-GCM', length: 256 },
false, false,
['encrypt'] ['encrypt']
); );
const iv = window.crypto.getRandomValues(new Uint8Array(12)); const iv = window.crypto.getRandomValues(new Uint8Array(12));
const encryptedContent = await window.crypto.subtle.encrypt( const encryptedContent = await window.crypto.subtle.encrypt(
{ {
name: 'AES-GCM', name: 'AES-GCM',
iv: iv iv: iv
}, },
key, key,
enc.encode(content) enc.encode(content)
); );
const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength); const combined = new Uint8Array(salt.byteLength + iv.byteLength + encryptedContent.byteLength);
combined.set(salt, 0); combined.set(salt, 0);
combined.set(iv, salt.byteLength); combined.set(iv, salt.byteLength);
combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength); combined.set(new Uint8Array(encryptedContent), salt.byteLength + iv.byteLength);
const result = { const result = {
version: 1, version: 1,
kdf: 'PBKDF2', kdf: 'PBKDF2',
iterations: 100000, iterations: 100000,
hash: 'SHA-256', hash: 'SHA-256',
algorithm: 'AES-GCM', algorithm: 'AES-GCM',
data: btoa(String.fromCharCode(...combined)) data: btoa(String.fromCharCode(...combined))
}; };
return JSON.stringify(result); return JSON.stringify(result);
} }
export async function decryptAES(result: string, password: string): Promise<string | null> { export async function decryptAES(result: string, password: string): Promise<string | null> {
try { try {
const enc = new TextEncoder(); const enc = new TextEncoder();
const parsed = JSON.parse(result); const parsed = JSON.parse(result);
const combined = Uint8Array.from(atob(parsed.data), c => c.charCodeAt(0)); const combined = Uint8Array.from(atob(parsed.data), (c) => c.charCodeAt(0));
const salt = combined.slice(0, 16); const salt = combined.slice(0, 16);
const iv = combined.slice(16, 28); const iv = combined.slice(16, 28);
const data = combined.slice(28); const data = combined.slice(28);
const keyMaterial = await window.crypto.subtle.importKey( const keyMaterial = await window.crypto.subtle.importKey(
'raw', 'raw',
enc.encode(password), enc.encode(password),
{ name: 'PBKDF2' }, { name: 'PBKDF2' },
false, false,
['deriveBits', 'deriveKey'] ['deriveBits', 'deriveKey']
); );
const key = await window.crypto.subtle.deriveKey( const key = await window.crypto.subtle.deriveKey(
{ {
name: 'PBKDF2', name: 'PBKDF2',
salt: salt, salt: salt,
iterations: parsed.iterations, iterations: parsed.iterations,
hash: parsed.hash hash: parsed.hash
}, },
keyMaterial, keyMaterial,
{ name: 'AES-GCM', length: 256 }, { name: 'AES-GCM', length: 256 },
false, false,
['decrypt'] ['decrypt']
); );
const decryptedContent = await window.crypto.subtle.decrypt( const decryptedContent = await window.crypto.subtle.decrypt(
{ {
name: 'AES-GCM', name: 'AES-GCM',
iv: iv iv: iv
}, },
key, key,
data data
); );
const dec = new TextDecoder(); const dec = new TextDecoder();
return dec.decode(decryptedContent); return dec.decode(decryptedContent);
} catch { } catch {
return null; return null;
} }
} }
+10 -6
View File
@@ -38,10 +38,12 @@
{ value: 315360000000, label: 'Expire in 10 Years' } { value: 315360000000, label: 'Expire in 10 Years' }
]; ];
const visibilityOptions = data.options.visibilities.map((visibility: { value: number; displayName: string }) => ({ const visibilityOptions = data.options.visibilities.map(
value: visibility.value, (visibility: { value: number; displayName: string }) => ({
label: visibility.displayName value: visibility.value,
})); label: visibility.displayName
})
);
async function handleSubmit(event: Event) { async function handleSubmit(event: Event) {
event.preventDefault(); event.preventDefault();
@@ -91,7 +93,7 @@
<main <main
class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white" class="flex min-h-screen w-full flex-col items-center justify-center bg-neutral-950 text-white"
> >
<div class="w-[95%] max-w-5xl rounded border-2 border-neutral-800 bg-neutral-900 p-6"> <div class="w-[95%] max-w-7xl rounded border-2 border-neutral-800 bg-neutral-900 p-6">
<h1 class="mb-6 text-center text-4xl font-bold">Create a Paste</h1> <h1 class="mb-6 text-center text-4xl font-bold">Create a Paste</h1>
<form class="mt-4" on:submit|preventDefault={handleSubmit}> <form class="mt-4" on:submit|preventDefault={handleSubmit}>
<input <input
@@ -109,7 +111,9 @@
spellcheck="false" spellcheck="false"
autocomplete="off" autocomplete="off"
bind:value={content} bind:value={content}
maxlength={data.options.maxContentSize ? Math.floor(data.options.maxContentSize / 4) : undefined} maxlength={data.options.maxContentSize
? Math.floor(data.options.maxContentSize / 4)
: undefined}
></textarea> ></textarea>
<Dropdown <Dropdown
options={syntaxOptions} options={syntaxOptions}
-1
View File
@@ -1,7 +1,6 @@
@import 'tailwindcss'; @import 'tailwindcss';
@import 'highlight.js/styles/github-dark.css'; @import 'highlight.js/styles/github-dark.css';
* { * {
box-sizing: border-box; box-sizing: border-box;
} }
@@ -3,14 +3,14 @@ import { getServerToken } from '$lib/utils/auth';
import type { User } from '$lib/types/user'; import type { User } from '$lib/types/user';
export const load: PageServerLoad = async ({ params, fetch, cookies }) => { export const load: PageServerLoad = async ({ params, fetch, cookies }) => {
const { username } = params; const { username } = params;
const token = getServerToken(cookies); const token = getServerToken(cookies);
const res = await fetch(`/api/user/${username}`, { const res = await fetch(`/api/user/${username}`, {
headers: { headers: {
Authorization: token ? `Bearer ${token}` : '' Authorization: token ? `Bearer ${token}` : ''
} }
}); });
const user = await res.json() as User; const user = (await res.json()) as User;
return { user }; return { user };
} };