Replacing URLs by user ids in origins
This commit is contained in:
1 parent
f07bf07c3f
commit
c8644c8817
3 files changed
+92
-43
No files matched your search
@@ -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));
|
||||
Reference in new issue
Block a user