File size: 1,096 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import * as 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]);
}