Derive the notation names from one table and refuse an smsIdFormat that is not the pair

This commit is contained in:
2026-08-30 22:45:02 +02:00
parent 8e5b9bb556
commit 696e018b77
12 changed files with 111 additions and 56 deletions
+25 -20
View File
@@ -1,32 +1,37 @@
/** The notation a peer writes message ids in. */
export type SmsIdFormat = 'decimal' | 'hex';
/** The notation per place the peer writes an id. An omitted place is left as it arrived. */
export type SmsIdFormats = {
receipt?: SmsIdFormat | undefined;
submitResp?: SmsIdFormat | undefined;
};
export function isSmsIdFormat(value: unknown): value is SmsIdFormat {
return value === 'decimal' || value === 'hex';
}
// SMPP 3.4 caps message_id at 64 octets, and BigInt on a longer string is a peer-controlled cost.
const maxIdLength = 64;
const notations = {
decimal: { digits: /^[0-9]+$/, prefix: '' },
hex: { digits: /^[0-9a-f]+$/i, prefix: '0x' },
};
/** The notation a peer writes message ids in. */
export type SmsIdNotation = keyof typeof notations;
/** The notation per place the peer writes an id. An omitted place is left as it arrived. */
export type SmsIdFormat = {
receipt?: SmsIdNotation | undefined;
submitResp?: SmsIdNotation | undefined;
};
export const smsIdNotations: string[] = Object.keys(notations);
export const smsIdPlaces: (keyof SmsIdFormat)[] = ['receipt', 'submitResp'];
export function isSmsIdNotation(value: unknown): value is SmsIdNotation {
return typeof value === 'string' && Object.hasOwn(notations, value);
}
// SMPP 3.4 caps message_id at 64 octets, and BigInt on a longer string is a peer-controlled cost.
const maxIdLength = 64;
/**
* The id as a plain decimal value, so an SMSC that answers a submit in one notation and writes the
* receipt in another still correlates. An id the notation cannot read is left as it arrived.
* receipt in another still correlates. An id the notation cannot read is left as it arrived, which
* is what leaves a `<base>-<n>` id whole for DlrMerger.
*/
export function normaliseSmsId(id: string, format: SmsIdFormat | undefined): string {
if (format === undefined || id.length > maxIdLength) return id;
export function normaliseSmsId(id: string, notation: SmsIdNotation | undefined): string {
if (notation === undefined || id.length > maxIdLength) return id;
const { digits, prefix } = notations[format];
const { digits, prefix } = notations[notation];
return digits.test(id) ? BigInt(`${prefix}${id}`).toString(10) : id;
}