signal-desktop/ts/util/normalizeAci.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-09-14 17:04:48 +00:00
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { AciString } from '../types/ServiceId';
import type { LoggerType } from '../types/Logging';
import * as log from '../logging/log';
import { isAciString } from './isAciString';
import { strictAssert } from './assert';
2023-09-14 17:04:48 +00:00
export function normalizeAci(
rawAci: string,
context: string,
logger?: Pick<LoggerType, 'warn'>
): AciString;
export function normalizeAci(
rawAci: string | undefined | null,
context: string,
logger?: Pick<LoggerType, 'warn'>
): AciString | undefined;
export function normalizeAci(
rawAci: string | undefined | null,
context: string,
logger: Pick<LoggerType, 'warn'> = log
): AciString | undefined {
if (rawAci == null) {
return undefined;
}
const result = rawAci.toLowerCase();
strictAssert(!result.startsWith('pni:'), 'ACI should not start with PNI:');
2023-09-14 17:04:48 +00:00
if (!isAciString(result)) {
logger.warn(
2023-09-27 23:14:55 +00:00
`Normalizing invalid aci: ${rawAci} to ${result} in context "${context}"`
2023-09-14 17:04:48 +00:00
);
// Cast anyway we don't want to throw here
return result as AciString;
}
return result;
}