This commit is contained in:
Shahrad Elahi
2024-05-29 16:23:25 +03:30
parent 9ee534f2bb
commit ae787625b9
170 changed files with 2434 additions and 2643 deletions

View File

@@ -0,0 +1,25 @@
export interface Request extends Omit<RequestInit, 'url'> {
action?: string;
form?: Record<string, string>;
}
export default function fetchAction(request: Request): Promise<Response> {
return fetch(request.action ?? '/', {
...request,
method: request.method ?? 'GET',
headers: {
...request.headers,
'X-Sveltekit-Action': 'true',
},
body: request.form ? createFormData(request.form) : request.body || undefined,
});
}
function createFormData(data: Record<string, string>): FormData {
const form = new FormData();
for (const key in data) {
if (typeof data[key] !== 'string') continue;
form.set(key, data[key]);
}
return form;
}

View File

@@ -0,0 +1,15 @@
import { accessSync, promises } from 'node:fs';
export function fsAccess(path: string): boolean {
try {
accessSync(path);
return true;
} catch (error) {
return false;
}
}
export async function fsTouch(filePath: string): Promise<void> {
const fd = await promises.open(filePath, 'a');
await fd.close();
}

11
web/src/lib/utils/hash.ts Normal file
View File

@@ -0,0 +1,11 @@
import { createHash } from 'crypto';
export function sha256(data: Buffer | string): string {
const hash = createHash('sha256');
hash.update(data);
return hash.digest('hex');
}
export function hex(data: Buffer | string): string {
return Buffer.from(data).toString('hex');
}