♻️🩹 backend: add company_id to editing_session_key (#525)

This commit is contained in:
Eric Doughty-Papassideris
2024-08-25 22:26:29 +02:00
parent 3b81a21e08
commit 9b0dcc1068
5 changed files with 123 additions and 25 deletions
+32
View File
@@ -12,3 +12,35 @@ export function timeuuidToDate(time_str: string): number {
return parseInt(time_string, 16);
}
/** Remove `-`s from a formatted UUID to get a hex a string */
export function hexFromFormatted(uuid: string) {
const result = uuid.replace(/-+/g, "");
if (result.length !== 32)
throw new Error(`Invalid UUID (${JSON.stringify(uuid)}). Wrong number of digits`);
return result;
}
/** Add `-`s back in a hex string to make a formatted UUID */
export function formattedFromHex(hex: string) {
const idMatch = hex.match(
/^([a-z0-f]{8})([a-z0-f]{4})([a-z0-f]{4})([a-z0-f]{4})([a-z0-f]{12})$/i,
);
if (!idMatch)
throw new Error(`Invalid UUID hex (${JSON.stringify(hex)}). Wrong number of digits`);
const [, ...parts] = idMatch;
return parts.join("-");
}
/** Convert a UUID formatted or hex string into a binary buffer */
export function bufferFromUUIDString(uuidOrHex: string) {
return Buffer.from(hexFromFormatted(uuidOrHex), "hex");
}
/** In a buffer with concatenated UUIDs in binary, extract the `index`th and return as formatted UUID */
export function formattedUUIDInBufferArray(buffer: Buffer, index: number) {
if (buffer.length < (index + 1) * 16)
throw new Error(`Cannot get UUID ${JSON.stringify(index)} because the buffer is too small`);
const slice = buffer.subarray(index * 16, (index + 1) * 16);
return formattedFromHex(slice.toString("hex").toLowerCase());
}