Name which body settles data_coding in resolveBody
Mirror / push (push) Has been cancelled
Test / lint (pull_request) Successful in 23s
Test / test (18) (pull_request) Successful in 38s
Test / test (20) (pull_request) Successful in 33s
Test / test (26) (pull_request) Successful in 32s
Test / test (22) (pull_request) Successful in 31s
Test / test (24) (pull_request) Successful in 37s
Mirror / push (push) Has been cancelled
Test / lint (pull_request) Successful in 23s
Test / test (18) (pull_request) Successful in 38s
Test / test (20) (pull_request) Successful in 33s
Test / test (26) (pull_request) Successful in 32s
Test / test (22) (pull_request) Successful in 31s
Test / test (24) (pull_request) Successful in 37s
This commit is contained in:
+43
-53
@@ -71,78 +71,68 @@ type ResolvedBody = {
|
|||||||
tlvs: TlvInputs | undefined;
|
tlvs: TlvInputs | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What the PDU's data_coding describes, and so what may set it: short_message wherever it holds an
|
||||||
|
* octet, since messageOctets() reads it there, and message_payload only where it does not.
|
||||||
|
*/
|
||||||
|
type CodingSource = 'caller' | 'message_payload' | 'short_message';
|
||||||
|
|
||||||
function codingOf(params: Record<string, ParamValue | undefined>): number | undefined {
|
function codingOf(params: Record<string, ParamValue | undefined>): number | undefined {
|
||||||
return typeof params.data_coding === 'number' ? params.data_coding : undefined;
|
return typeof params.data_coding === 'number' ? params.data_coding : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** messageOctets() reads short_message wherever it holds an octet, and the TLV only where it does not. */
|
/** Only the short_message the command's own table will write, since writeParams() ignores any other. */
|
||||||
function carriesOctets(value: ParamValue | undefined): boolean {
|
function resolveShortMessage(
|
||||||
return Buffer.isBuffer(value) && value.length > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The short_message the command's own table will write, since writeParams() ignores any other. */
|
|
||||||
function writtenBody(definition: CommandDefinition, value: ParamValue | undefined): ParamValue | undefined {
|
|
||||||
return definition.params?.short_message === undefined ? undefined : value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Encoded in place, settling data_coding where `settles` says no mandatory field will carry it. */
|
|
||||||
function resolveCarried(
|
|
||||||
resolved: ResolvedBody,
|
|
||||||
inputs: TlvInputs | undefined,
|
|
||||||
dataCoding: number | undefined,
|
|
||||||
settles: boolean,
|
|
||||||
): VoidResult {
|
|
||||||
const text = inputs?.message_payload?.tagValue;
|
|
||||||
|
|
||||||
if (typeof text !== 'string') return {};
|
|
||||||
|
|
||||||
const encoded = encodeBody(text, dataCoding);
|
|
||||||
|
|
||||||
if (encoded.err) return { err: new Error(`TLV "message_payload": ${encoded.err.message}`) };
|
|
||||||
|
|
||||||
if (settles) resolved.params.data_coding = encoded.dataCoding;
|
|
||||||
|
|
||||||
resolved.tlvs = { ...resolved.tlvs, message_payload: { tagValue: encoded.buffer } };
|
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
/** data_coding names the alphabet of the body, and short_message settles it where it carries octets. */
|
|
||||||
function resolveBody(
|
|
||||||
params: Record<string, ParamValue | undefined>,
|
params: Record<string, ParamValue | undefined>,
|
||||||
tlvs: TlvInputs | undefined,
|
|
||||||
definition: CommandDefinition,
|
definition: CommandDefinition,
|
||||||
): Result<ResolvedBody> {
|
): Result<{ params: Record<string, ParamValue | undefined>; source: CodingSource }> {
|
||||||
const message = writtenBody(definition, params.short_message);
|
const message = definition.params?.short_message === undefined ? undefined : params.short_message;
|
||||||
const resolved: ResolvedBody = { params: { ...params }, tlvs };
|
|
||||||
|
|
||||||
if (Buffer.isBuffer(message) && params.sm_length === undefined) {
|
if (Buffer.isBuffer(message)) {
|
||||||
resolved.params.sm_length = message.length;
|
return {
|
||||||
|
params: params.sm_length === undefined ? { ...params, sm_length: message.length } : params,
|
||||||
|
source: message.length > 0 ? 'caller' : 'message_payload',
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof message === 'string') {
|
if (typeof message !== 'string') return { params, source: 'message_payload' };
|
||||||
|
|
||||||
const encoded = encodeBody(message, codingOf(params));
|
const encoded = encodeBody(message, codingOf(params));
|
||||||
|
|
||||||
if (encoded.err) {
|
if (encoded.err) {
|
||||||
return { err: new Error(`Parameter "short_message" of "${definition.command}": ${encoded.err.message}`) };
|
return { err: new Error(`Parameter "short_message" of "${definition.command}": ${encoded.err.message}`) };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (carriesOctets(encoded.buffer)) resolved.params.data_coding = encoded.dataCoding;
|
const written = { ...params, short_message: encoded.buffer, sm_length: encoded.buffer.length };
|
||||||
|
|
||||||
resolved.params.short_message = encoded.buffer;
|
if (encoded.buffer.length === 0) return { params: written, source: 'message_payload' };
|
||||||
resolved.params.sm_length = encoded.buffer.length;
|
|
||||||
|
return { params: { ...written, data_coding: encoded.dataCoding }, source: 'short_message' };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only octets the command's own table will write can settle the alphabet the PDU declares.
|
function resolveBody(
|
||||||
const settles = !carriesOctets(writtenBody(definition, resolved.params.short_message));
|
params: Record<string, ParamValue | undefined>,
|
||||||
const carried = resolveCarried(
|
tlvs: TlvInputs | undefined,
|
||||||
resolved,
|
definition: CommandDefinition,
|
||||||
tlvs,
|
): Result<ResolvedBody> {
|
||||||
settles ? codingOf(params) : codingOf(resolved.params),
|
const shortMessage = resolveShortMessage(params, definition);
|
||||||
settles,
|
|
||||||
);
|
|
||||||
|
|
||||||
return carried.err ? { err: carried.err } : resolved;
|
if (shortMessage.err) return { err: shortMessage.err };
|
||||||
|
|
||||||
|
const text = tlvs?.message_payload?.tagValue;
|
||||||
|
|
||||||
|
if (typeof text !== 'string') return { params: shortMessage.params, tlvs };
|
||||||
|
|
||||||
|
const encoded = encodeBody(text, codingOf(shortMessage.params));
|
||||||
|
|
||||||
|
if (encoded.err) return { err: new Error(`TLV "message_payload": ${encoded.err.message}`) };
|
||||||
|
|
||||||
|
return {
|
||||||
|
params: shortMessage.source === 'message_payload'
|
||||||
|
? { ...shortMessage.params, data_coding: encoded.dataCoding }
|
||||||
|
: shortMessage.params,
|
||||||
|
tlvs: { ...tlvs, message_payload: { tagValue: encoded.buffer } },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeParams(
|
function writeParams(
|
||||||
|
|||||||
@@ -199,13 +199,6 @@ next work ([decision](docs/decisions.md#internals-and-tests)).
|
|||||||
|
|
||||||
### Locality — next, ahead of everything below; 5–6 today, and the gate is 7
|
### Locality — next, ahead of everything below; 5–6 today, and the gate is 7
|
||||||
|
|
||||||
- [ ] **Replace `resolveBody`'s `settles` boolean with the decision it stands for.** One boolean
|
|
||||||
chooses both whether to overwrite `data_coding` and which params to read it from, across four
|
|
||||||
helpers all named some abstraction of "body". Return a named source — `'short_message' |
|
|
||||||
'payload' | 'caller'` — and branch once. Ranked hardest by three readers and picked by one as
|
|
||||||
the unit they would least want to touch, because a mistake here does not throw, does not fail
|
|
||||||
the types, and reaches the peer as somebody's message rendered wrong.
|
|
||||||
|
|
||||||
### Correctness
|
### Correctness
|
||||||
|
|
||||||
- [ ] **Settle what a repeated tag not marked `multiple` reads as, and pin it in a test.** A vendor
|
- [ ] **Settle what a repeated tag not marked `multiple` reads as, and pin it in a test.** A vendor
|
||||||
|
|||||||
Reference in New Issue
Block a user