mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-24 10:28:03 +01:00
Use UUIDs in group database schema
This commit is contained in:
@@ -1,14 +1,21 @@
|
||||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { v4 as generateUUID } from 'uuid';
|
||||
|
||||
import { strictAssert } from '../util/assert';
|
||||
import { isValidGuid } from '../util/isValidGuid';
|
||||
|
||||
export type UUIDStringType = `${string}-${string}-${string}-${string}-${string}`;
|
||||
|
||||
export const isValidUuid = (value: unknown): value is UUIDStringType =>
|
||||
typeof value === 'string' &&
|
||||
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i.test(
|
||||
value
|
||||
);
|
||||
|
||||
export class UUID {
|
||||
constructor(protected readonly value: string) {
|
||||
strictAssert(isValidGuid(value), `Invalid UUID: ${value}`);
|
||||
strictAssert(isValidUuid(value), `Invalid UUID: ${value}`);
|
||||
}
|
||||
|
||||
public toString(): UUIDStringType {
|
||||
@@ -41,4 +48,24 @@ export class UUID {
|
||||
);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public static generate(): UUID {
|
||||
return new UUID(generateUUID());
|
||||
}
|
||||
|
||||
public static cast(value: UUIDStringType): never;
|
||||
public static cast(value: string): UUIDStringType;
|
||||
|
||||
public static cast(value: string): UUIDStringType {
|
||||
return new UUID(value.toLowerCase()).toString();
|
||||
}
|
||||
|
||||
// For testing
|
||||
public static fromPrefix(value: string): UUID {
|
||||
let padded = value;
|
||||
while (padded.length < 8) {
|
||||
padded += '0';
|
||||
}
|
||||
return new UUID(`${padded}-0000-4000-8000-${'0'.repeat(12)}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user