Replacing URLs by user ids in origins

This commit is contained in:
vdv committed 2023-03-04 16:07:22 +01:00
1 parent f07bf07c3f
commit c8644c8817
3 files changed
+92 -43

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
export const userId = (params: { username: string; database: string }) => {
const { username, database } = params;
const dbUrl = new URL(database);
return `@${username}>${dbUrl.hostname}`;
};
export const parseUserId = (id: string) => {
const matches = id.match(/^@([^>]+)>(.+)$/);
if (!matches) {
return null;
}
return { username: matches[1], database: matches[2] };
};
function hex2a(hex: string) {
var str = '';
for (var i = 0; i < hex.length; i += 2) {
var v = parseInt(hex.substr(i, 2), 16);
if (v) str += String.fromCharCode(v);
}
return str;
}
export const parseDbUrl = (urlString: string) => {
origin;
const url = new URL(urlString);
const match = url.pathname.match(/\/userdb-(.*)\//);
if (!match || match.length < 2) {
return { username: '', database: url.hostname };
}
const [_, encodedUser] = match;
return { username: hex2a(encodedUser), database: urlString };
};
export const dbUrlToUserId = (urlString: string) =>
userId(parseDbUrl(urlString));