- Delete old Vite+Svelte frontend - Initialize new SvelteKit project with TypeScript - Configure Tailwind CSS v4 + DaisyUI - Implement JWT authentication with auto-refresh - Create login page with form validation (Zod) - Add protected route guards - Update Docker configuration for single-stage build - Add E2E tests with Playwright (6/11 passing) - Fix Svelte 5 reactivity with $state() runes Known issues: - 5 E2E tests failing (timing/async issues) - Token refresh implementation needs debugging - Validation error display timing
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import crypto from 'node:crypto';
|
|
|
|
const hashes = Object.create(null);
|
|
|
|
//TODO shorter?
|
|
const hash_length = 12;
|
|
|
|
/**
|
|
* replaces +/= in base64 output so they don't interfere
|
|
*
|
|
* @param {string} input
|
|
* @returns {string} base64 hash safe to use in any context
|
|
*/
|
|
export function safeBase64Hash(input) {
|
|
if (hashes[input]) {
|
|
return hashes[input];
|
|
}
|
|
//TODO if performance really matters, use a faster one like xx-hash etc.
|
|
// should be evenly distributed because short input length and similarities in paths could cause collisions otherwise
|
|
// OR DON'T USE A HASH AT ALL, what about a simple counter?
|
|
const md5 = crypto.createHash('md5');
|
|
md5.update(input);
|
|
const hash = toSafe(md5.digest('base64')).slice(0, hash_length);
|
|
hashes[input] = hash;
|
|
return hash;
|
|
}
|
|
|
|
/** @type {Record<string, string>} */
|
|
const replacements = {
|
|
'+': '-',
|
|
'/': '_',
|
|
'=': ''
|
|
};
|
|
|
|
const replaceRE = new RegExp(`[${Object.keys(replacements).join('')}]`, 'g');
|
|
|
|
/**
|
|
* @param {string} base64
|
|
* @returns {string}
|
|
*/
|
|
function toSafe(base64) {
|
|
return base64.replace(replaceRE, (x) => replacements[x]);
|
|
}
|