Skip to content

Commit dda0a9d

Browse files
committed
Add detailed types to lookup tables
1 parent 31ceb1f commit dda0a9d

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

src/lookup-tables.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@
1111
//
1212
// Last updated: 2026-03-25
1313

14-
export const TLS_VERSIONS: Record<number, string> = {
14+
// Lookup table type: known numeric keys return definite string literals,
15+
// arbitrary numeric keys return string | undefined.
16+
type LookupTable<T extends Record<number, string>> = Readonly<T> & { readonly [key: number]: string | undefined };
17+
function lookupTable<const T extends Record<number, string>>(table: T): LookupTable<T> {
18+
return table as LookupTable<T>;
19+
}
20+
21+
export const TLS_VERSIONS = lookupTable({
1522
0x0300: 'SSL 3.0',
1623
0x0301: 'TLS 1.0',
1724
0x0302: 'TLS 1.1',
1825
0x0303: 'TLS 1.2',
1926
0x0304: 'TLS 1.3',
20-
};
27+
});
2128

2229
// https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-4
23-
export const CIPHER_SUITES: Record<number, string> = {
30+
export const CIPHER_SUITES = lookupTable({
2431
0x0000: 'TLS_NULL_WITH_NULL_NULL',
2532
0x0001: 'TLS_RSA_WITH_NULL_MD5',
2633
0x0002: 'TLS_RSA_WITH_NULL_SHA',
@@ -382,10 +389,10 @@ export const CIPHER_SUITES: Record<number, string> = {
382389
0xD002: 'TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384',
383390
0xD003: 'TLS_ECDHE_PSK_WITH_AES_128_CCM_8_SHA256',
384391
0xD005: 'TLS_ECDHE_PSK_WITH_AES_128_CCM_SHA256',
385-
};
392+
});
386393

387394
// https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml
388-
export const EXTENSIONS: Record<number, string> = {
395+
export const EXTENSIONS = lookupTable({
389396
0: 'server_name',
390397
1: 'max_fragment_length',
391398
2: 'client_certificate_url',
@@ -452,10 +459,10 @@ export const EXTENSIONS: Record<number, string> = {
452459
65281: 'renegotiation_info',
453460
// Non-IANA but widely deployed
454461
17513: 'application_settings',
455-
};
462+
});
456463

457464
// https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
458-
export const SUPPORTED_GROUPS: Record<number, string> = {
465+
export const SUPPORTED_GROUPS = lookupTable({
459466
1: 'sect163k1',
460467
2: 'sect163r1',
461468
3: 'sect163r2',
@@ -516,11 +523,11 @@ export const SUPPORTED_GROUPS: Record<number, string> = {
516523
// Legacy explicit curves
517524
65281: 'arbitrary_explicit_prime_curves',
518525
65282: 'arbitrary_explicit_char2_curves',
519-
};
526+
});
520527

521528
// https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-signaturescheme
522529
// Also includes legacy TLS 1.2 (hash, signature) byte-pair values from RFC 5246 section 7.4.1.4.1
523-
export const SIGNATURE_ALGORITHMS: Record<number, string> = {
530+
export const SIGNATURE_ALGORITHMS = lookupTable({
524531
// Legacy TLS 1.2 byte-pair schemes (hash << 8 | sig): hash 2=SHA-1, 3=SHA-224, 4=SHA-256,
525532
// 5=SHA-384, 6=SHA-512; sig 1=RSA, 2=DSA, 3=ECDSA
526533
0x0201: 'rsa_pkcs1_sha1',
@@ -581,36 +588,36 @@ export const SIGNATURE_ALGORITHMS: Record<number, string> = {
581588
0x091A: 'slhdsa_shake_192f',
582589
0x091B: 'slhdsa_shake_256s',
583590
0x091C: 'slhdsa_shake_256f',
584-
};
591+
});
585592

586593
// https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-9
587-
export const EC_POINT_FORMATS: Record<number, string> = {
594+
export const EC_POINT_FORMATS = lookupTable({
588595
0: 'uncompressed',
589596
1: 'ansiX962_compressed_prime',
590597
2: 'ansiX962_compressed_char2',
591-
};
598+
});
592599

593600
// https://www.iana.org/assignments/comp-meth-ids/comp-meth-ids.xhtml
594-
export const COMPRESSION_METHODS: Record<number, string> = {
601+
export const COMPRESSION_METHODS = lookupTable({
595602
0: 'null',
596603
1: 'DEFLATE',
597-
};
604+
});
598605

599606
// RFC 8446 section 4.2.9
600-
export const PSK_KEY_EXCHANGE_MODES: Record<number, string> = {
607+
export const PSK_KEY_EXCHANGE_MODES = lookupTable({
601608
0: 'psk_ke',
602609
1: 'psk_dhe_ke',
603-
};
610+
});
604611

605612
// https://www.iana.org/assignments/tls-certificate-compression/tls-certificate-compression.xhtml
606-
export const CERTIFICATE_COMPRESSION_ALGORITHMS: Record<number, string> = {
613+
export const CERTIFICATE_COMPRESSION_ALGORITHMS = lookupTable({
607614
1: 'zlib',
608615
2: 'brotli',
609616
3: 'zstd',
610-
};
617+
});
611618

612619
// RFC 6066 / RFC 6961
613-
export const CERTIFICATE_STATUS_TYPES: Record<number, string> = {
620+
export const CERTIFICATE_STATUS_TYPES = lookupTable({
614621
1: 'ocsp',
615622
2: 'ocsp_multi',
616-
};
623+
});

0 commit comments

Comments
 (0)