Files
Desktop/ts/types/NotificationProfile-node.node.ts
T
automated-signal fd6fdff55f Setup knip
Co-authored-by: Jamie <113370520+jamiebuilds-signal@users.noreply.github.com>
2026-04-13 15:10:11 -07:00

53 lines
1.4 KiB
TypeScript

// Copyright 2025 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
// Note: this is a dangerous import; it will break storybook
import { getRandomBytes } from '../Crypto.node.ts';
import * as Bytes from '../Bytes.std.ts';
import { createLogger } from '../logging/log.std.ts';
import { NOTIFICATION_PROFILE_ID_LENGTH } from './NotificationProfile.std.ts';
import type { NotificationProfileIdString } from './NotificationProfile.std.ts';
import type { LoggerType } from './Logging.std.ts';
const log = createLogger('NotificationProfile-node');
export function generateNotificationProfileId(): NotificationProfileIdString {
return normalizeNotificationProfileId(
Bytes.toHex(getRandomBytes(NOTIFICATION_PROFILE_ID_LENGTH)),
'generateNotificationProfileId'
);
}
function isNotificationProfileId(
value?: string
): value is NotificationProfileIdString {
if (!value) {
return false;
}
const bytes = Bytes.fromHex(value);
return bytes.byteLength === NOTIFICATION_PROFILE_ID_LENGTH;
}
export function normalizeNotificationProfileId(
id: string,
context: string,
logger: Pick<LoggerType, 'warn'> = log
): NotificationProfileIdString {
const result = id.toLowerCase();
if (!isNotificationProfileId(result)) {
logger.warn(
'Normalizing invalid notification profile id: ' +
`${id} to ${result} in context "${context}"`
);
return result as unknown as NotificationProfileIdString;
}
return result;
}