Refuse a body the PDU's own data_coding cannot carry (#98)

* Regression tests for a body the PDU's own data_coding cannot carry

* Refuse a body the PDU's own data_coding cannot carry, in the codec both send paths build through

* Record the codec's body refusal, and drop two todo entries it and the interop run close

* Regression tests for the aliased body TLV and the data_coding short_message settles

* Find the body TLV by tag id, leave data_coding to the field that will be read, and correct the record

* Record the two architecture findings this change does not close

* Regression tests for a duplicated body tag and a short_message the wire never carries

* Resolve every body TLV against the field that will be read, and move TLV writing to its table

* Regression tests for a body only the TLV carries and an empty short_message

* Let only octets the command writes settle the alphabet, and stop shadowing the TLV table
This commit is contained in:
2026-09-09 19:33:36 +02:00
committed by GitHub
parent c06cc0648b
commit ef13290230
9 changed files with 476 additions and 102 deletions
+21 -2
View File
@@ -1,7 +1,7 @@
import type { Result } from './result.ts';
import type { EncodingName } from './defs/encodings.ts';
import { detect, encodingByDataCoding, encodings } from './defs/encodings.ts';
import { hasUdh } from './defs/constants.ts';
import { detect, encodingByDataCoding, encodings, unencodable, unencodableText } from './defs/encodings.ts';
import { consts, hasUdh } from './defs/constants.ts';
import { udhLength } from './udh.ts';
/** A single SMS carries 1120 bits, whatever the alphabet. */
@@ -27,6 +27,25 @@ export function encodeMessage(
return { buffer: encodings[resolved].encode(message), encoding: resolved };
}
/** A message body as octets, under the alphabet `dataCoding` resolves to, or one detected for it. */
export function encodeBody(
text: string,
dataCoding: number | undefined,
): Result<{ buffer: Buffer; dataCoding: number }> {
if (dataCoding === undefined) {
const detected = encodeMessage(text);
return { buffer: detected.buffer, dataCoding: consts.ENCODING[detected.encoding] };
}
const encoding = encodingByDataCoding(dataCoding);
const lost = unencodable(text, encoding);
return lost
? { err: new Error(`data_coding ${String(dataCoding)} resolves to ${encoding}, which cannot carry ${unencodableText(lost)}; pass a Buffer of octets, or a data_coding whose alphabet carries them`) }
: { buffer: encodings[encoding].encode(text), dataCoding };
}
export function decodeMessage(
buffer: Buffer,
dataCoding: number,