Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/format-libraries-0-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@cosyte/pathways": patch
---

Pathways now parses with `@cosyte/hl7` 0.2, `@cosyte/fhir` 0.2 and `@cosyte/x12` 0.2, so a parse or write code from HL7 v2 or FHIR carries its library's prefix in logs, stored errors, ACK problems and `pathways test` output: `HL7_NO_MSH_SEGMENT` where it was `NO_MSH_SEGMENT`, `FHIR_MALFORMED_JSON` where it was `MALFORMED_JSON`.

An HL7 batch's envelope warnings are `HL7_BATCH_COUNT_MISMATCH` and `HL7_BATCH_MISSING_TRAILER`, where they were `BATCH_COUNT_MISMATCH` and `BATCH_MISSING_TRAILER`, and an HL7 v2 output refused for a character its set cannot hold fails with `HL7_ENCODE_UNENCODABLE_CHARACTER`. The default ACK's ERR-3.2 for condition 207 now reads "Application error", the text of HL7 table 0357, where it read "Application internal error". C-CDA and `xml` channels still read with `@cosyte/ccda` 0.1.

A transform receives the 0.2 models. In an HL7 v2 dot-path a field repetition now counts from 1: `PID.3[1]` is the first identifier, and `PID.3[0]` is refused; a segment occurrence such as `OBX[0]` still counts from 0. `setField` still changes the message in place but is deprecated: `withField` returns a new message, which the transform returns. `XML_GROUP_TABLES` is empty, so `toHl7Xml` takes every structure's groups in `groups`. A FHIR resource is deep-frozen, so an assignment to it throws, and it carries the reader's `warnings`. The starter's transform, the examples and the guides now use `withField`.

What a caller must change: compare against the prefixed codes; add 1 to every field repetition index in a dot-path your transforms use; build a FHIR resource with `complex`, `list` and `primitive` rather than editing the one a transform receives; pass `groups` to `toHl7Xml` where you read `XML_GROUP_TABLES`; and install `@cosyte/hl7` 0.2 and `@cosyte/fhir` 0.2 where a project imports them beside Pathways.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ version is 0.0.x, and a release may still change a key or an API, which its chan
- [Quickstart](#quickstart)
- [Why this exists](#why-this-exists)
- [What the latest release ships](#what-the-latest-release-ships)
- [In the next release](#in-the-next-release)
- [Status](#status)
- [Build from source](#build-from-source)
- [Layout](#layout)
Expand Down Expand Up @@ -229,6 +230,19 @@ What follows is in 0.0.6, the version on npm:

---

## In the next release

These are on `main` and land in the next release, not in 0.0.6:

- **The 0.2 parsers for HL7 v2, FHIR and X12.** Pathways parses with `@cosyte/hl7`, `@cosyte/fhir`
and `@cosyte/x12` 0.2. Their codes carry a prefix wherever Pathways reports one
(`HL7_NO_MSH_SEGMENT`, `FHIR_MALFORMED_JSON`, `HL7_BATCH_COUNT_MISMATCH`), and a transform
receives the 0.2 models: an HL7 v2 dot-path counts a field repetition from 1, `withField`
replaces `setField`, and a FHIR resource is frozen. C-CDA stays on `@cosyte/ccda` 0.1.
<!-- changesets: format-libraries-0-2 -->

---

## Status

Early, and released: 0.0.6 is on npm. Each release is published with provenance from this
Expand Down
6 changes: 2 additions & 4 deletions bench/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ import { fileURLToPath } from "node:url";
export const TRANSFORMS = {
// The starter's kind of edit: address each message to the lab, and send it on.
hl7Address: [
"export default (msg: { setField(path: string, value: string): void }) => {",
' msg.setField("MSH.5", "LAB");',
" return msg;",
"};",
"export default (msg: { withField(path: string, value: string): unknown }) =>",
' msg.withField("MSH.5", "LAB");',
"",
].join("\n"),
};
Expand Down
5 changes: 1 addition & 4 deletions bench/profile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ function engineSection(bin, quick) {
/** The channel's transform as the worker would load it from the TypeScript in bench/engine.mjs. */
function transformOf(channel) {
if (channel.transform === TRANSFORMS.hl7Address) {
return (msg) => {
msg.setField("MSH.5", "LAB");
return msg;
};
return (msg) => msg.withField("MSH.5", "LAB");
}
return (message) => [message];
}
Expand Down
14 changes: 6 additions & 8 deletions docs-content/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ nothing, the message's pass id, which no command can look up.
> `readonly` **problem**: `string` \| `undefined`

For an `unreadable` message, what was wrong, as a stable code: one of `@cosyte/hl7`'s parse
codes (such as `NO_MSH_SEGMENT`), `NO_CONTROL_ID`, `MULTIPLE_MESSAGES` or
codes (such as `HL7_NO_MSH_SEGMENT`), `NO_CONTROL_ID`, `MULTIPLE_MESSAGES` or
`MLLP_TRAILING_BYTES`.

##### reason
Expand Down Expand Up @@ -949,7 +949,7 @@ The pieces stored as new messages that are not failed.

> `readonly` **warnings**: readonly `string`[]

The envelope's warning codes, such as `BATCH_COUNT_MISMATCH`, each once.
The envelope's warning codes, such as `HL7_BATCH_COUNT_MISMATCH`, each once.

***

Expand Down Expand Up @@ -5290,7 +5290,7 @@ What a source can tell the engine about a message it hands over. Every field is

```ts
await sink.receive(body, { duplicateKey: `${sender}|${controlId}` });
await sink.receive(body, { failure: "inbound: NO_MSH_SEGMENT" });
await sink.receive(body, { failure: "inbound: HL7_NO_MSH_SEGMENT" });
await sink.receive(body, { idempotencyKey: `queue|${deliveryTag}` });
await sink.receive(body, { idempotencyKey: `queue|${deliveryTag}`, resumed: true });
await sink.receive(body, { source: { queue: "inbound", deliveryTag } });
Expand Down Expand Up @@ -7096,7 +7096,7 @@ The channel's name.

> `readonly` **code**: `string` \| `undefined`

The error's code, when it has one, such as a parser's `NO_MSH_SEGMENT`.
The error's code, when it has one, such as a parser's `HL7_NO_MSH_SEGMENT`.

##### kind

Expand Down Expand Up @@ -7219,8 +7219,7 @@ import { defaultAckBuilder, type AckBuilder } from "@cosyte/pathways";
import { parseHL7 } from "@cosyte/hl7";

export default ((request) => {
const ack = parseHL7(Buffer.from(defaultAckBuilder(request)));
ack.setField("MSH.3", "PATHWAYS");
const ack = parseHL7(Buffer.from(defaultAckBuilder(request))).withField("MSH.3", "PATHWAYS");
return Buffer.from(ack.toString());
}) satisfies AckBuilder;
```
Expand Down Expand Up @@ -8637,8 +8636,7 @@ import type { Transform } from "@cosyte/pathways";

export default ((msg) => {
if (!msg.is("ADT^A01")) return null;
msg.setField("MSH.5", "LAB");
return msg;
return msg.withField("MSH.5", "LAB");
}) satisfies Transform;
```

Expand Down
4 changes: 1 addition & 3 deletions docs-content/example-file-to-sftp.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ import type { Transform } from "@cosyte/pathways";
export default ((msg) => {
if (!msg.is("ORM^O01")) return null;
// MSH-5 and MSH-6 are the receiving application and facility.
msg.setField("MSH.5", "LAB");
msg.setField("MSH.6", "REFLAB");
return msg;
return msg.withField("MSH.5", "LAB").withField("MSH.6", "REFLAB");
}) satisfies Transform;
```

Expand Down
4 changes: 1 addition & 3 deletions docs-content/example-filter-and-route.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ export default ((msg) => {
if (msg.get(`OBX[${String(i)}].11`) !== "F") return null;
}
// MSH-5 and MSH-6 are the receiving application and facility.
msg.setField("MSH.5", "EHR");
msg.setField("MSH.6", "HOSPITAL");
return msg;
return msg.withField("MSH.5", "EHR").withField("MSH.6", "HOSPITAL");
}) satisfies Transform;
```

Expand Down
4 changes: 1 addition & 3 deletions docs-content/example-mllp-to-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ import type { Transform } from "@cosyte/pathways";
export default ((msg) => {
if (!msg.is("ADT^A01") && !msg.is("ADT^A04") && !msg.is("ADT^A08")) return null;
// MSH-5 and MSH-6 are the receiving application and facility.
msg.setField("MSH.5", "EHR");
msg.setField("MSH.6", "HOSPITAL");
return msg;
return msg.withField("MSH.5", "EHR").withField("MSH.6", "HOSPITAL");
}) satisfies Transform;
```

Expand Down
4 changes: 1 addition & 3 deletions docs-content/example-replay-after-failure.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ export default (async (msg) => {
// MSH-9.2 is the trigger event, such as A01.
const code = codes[msg.get("MSH.9.2") ?? ""];
if (code === undefined) throw new Error("MSH-9.2 is not in event-codes.json");
msg.setField("EVN.4", code);
msg.setField("MSH.5", "REGISTRY");
return msg;
return msg.withField("EVN.4", code).withField("MSH.5", "REGISTRY");
}) satisfies Transform;
```

Expand Down
2 changes: 1 addition & 1 deletion docs-content/example-vet-lab-to-fhir.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Its messages are synthetic, made by `@cosyte/synth`, and are on GitHub with the
"test": "pathways test"
},
"dependencies": {
"@cosyte/fhir": "^0.1.12",
"@cosyte/fhir": "^0.2.0",
"@cosyte/transform": "^0.2.4"
},
"devDependencies": {
Expand Down
67 changes: 42 additions & 25 deletions docs-content/example-vet-lab-to-v2-xml.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ about a real results service, and what it does not.

- An [MLLP source](./send-and-receive-mllp.md) that stores each message before it sends the ACK.
- A transform that writes each lab result with `toHl7Xml` from `@cosyte/hl7`: the segments placed in
the `ORU_R01` groups `@cosyte/hl7` ships for HL7 v2.5, and each field's components named for its
data type from a table in the transform. Every other message is filtered out.
`ORU_R01` groups from a table in the transform, made up for this example, and each field's
components named for its data type from another table there. Every other message is filtered out.
- An [HTTP destination](./send-and-receive-http.md#settle-by-an-hl7-v2-xml-acknowledgement) with
`ack: xml` that POSTs the document as `application/xml` over TLS, presents a client certificate,
and settles each delivery by the acknowledgement's `MSA.1`: `AA` delivered, `AE` retried, `AR`
failed for good, and an acknowledgement whose `MSA.2` names another message retried.
- Three small scripts to try it with no other system: `scripts/dev-certs.ts` makes a development
CA, server certificate and client certificate with `openssl`; `scripts/lab.ts` stands in for the
results service, requires the client certificate, reads each POST with `parseHL7Xml` and answers
with an acknowledgement written by `toHl7Xml`, passing the `ACK` structure `@cosyte/hl7` ships no
table for; and `scripts/send.ts` sends files over MLLP.
with an acknowledgement written by `toHl7Xml`, passing the `ACK` structure, since `@cosyte/hl7`
ships no group table; and `scripts/send.ts` sends files over MLLP.

The MLLP source runs plain TCP on the loopback (`plaintext: true`), for this example only. A lab
system on another machine connects over TLS: see [Secure endpoints](./secure-endpoints.md).
Expand Down Expand Up @@ -88,12 +88,12 @@ The destination sends them in order, one at a time, and the stand-in prints each
its answer, never the content:

```text
POST /results: 2205 bytes to data/lab/1.xml, answered AA
POST /results: 2205 bytes to data/lab/2.xml, answered AE
POST /results: 2205 bytes to data/lab/3.xml, answered AA
POST /results: 2513 bytes to data/lab/4.xml, answered AR
POST /results: 2849 bytes to data/lab/5.xml, answered AA to another control id
POST /results: 2849 bytes to data/lab/6.xml, answered AA
POST /results: 2106 bytes to data/lab/1.xml, answered AA
POST /results: 2106 bytes to data/lab/2.xml, answered AE
POST /results: 2106 bytes to data/lab/3.xml, answered AA
POST /results: 2404 bytes to data/lab/4.xml, answered AR
POST /results: 2730 bytes to data/lab/5.xml, answered AA to another control id
POST /results: 2730 bytes to data/lab/6.xml, answered AA
```

- The first result is accepted and delivered.
Expand Down Expand Up @@ -146,7 +146,7 @@ Its messages are synthetic, made by `@cosyte/synth`, and are on GitHub with the
"test": "pathways test"
},
"dependencies": {
"@cosyte/hl7": "^0.1.14"
"@cosyte/hl7": "^0.2.0"
},
"devDependencies": {
"@cosyte/pathways": "^0.0.6"
Expand Down Expand Up @@ -207,7 +207,7 @@ destinations:
// Writes each veterinary lab result (ORU^R01) in the HL7 v2 XML encoding with @cosyte/hl7's
// writer, and filters every other message out. The HTTP destination POSTs the document and settles
// the delivery by the XML acknowledgement the service answers with.
import { toHl7Xml, XML_GROUP_TABLES } from "@cosyte/hl7";
import { toHl7Xml, type XmlMessageStructure } from "@cosyte/hl7";
import type { Transform } from "@cosyte/pathways";

/**
Expand Down Expand Up @@ -251,19 +251,36 @@ const DATA_TYPES: Readonly<Record<string, string>> = {
"OBX.14": "TS",
};

/**
* The groups the XML encoding places these results' segments in (ORU_R01.REPORT and the rest). The
* names and their order come from the receiver's schema, and @cosyte/hl7 ships none, so the writer
* is given them here. These are this example's own, made up for it and not a copy of any published
* structure: write yours from your receiver's schema. A segment with no place in them is refused.
*/
const GROUPS: XmlMessageStructure = {
structure: "ORU_R01",
items: [
"MSH",
{
group: "REPORT+",
items: [
"PID",
"NK1*",
{ group: "ORDER+", items: ["OBR", { group: "RESULT*", items: ["OBX"] }] },
],
},
],
};

export default ((msg) => {
if (!msg.is("ORU^R01")) return null;

// The XML encoding places the segments in the message structure's groups
// (ORU_R01.PATIENT_RESULT and the rest), whose names come from each version's published schema.
// @cosyte/hl7 ships them for ORU_R01 in v2.5, which these results are. For another version or
// structure, pass the table your receiver's schema gives as groups, the way scripts/lab.ts passes
// the ACK's: this transform fails such a result rather than guess its groups.
const groups = XML_GROUP_TABLES[msg.version ?? ""]?.["ORU_R01"];
if (groups === undefined) {
// The groups above are for the v2.5 results this example sends. For another version, pass the
// table your receiver's schema gives for it: this transform fails such a result rather than guess.
if (msg.version !== "2.5") {
throw new Error("no ORU_R01 group table for this result's HL7 version (MSH-12)");
}
return toHl7Xml(msg, { dataTypes: DATA_TYPES, groups });
return toHl7Xml(msg, { dataTypes: DATA_TYPES, groups: GROUPS });
}) satisfies Transform;
```

Expand Down Expand Up @@ -459,9 +476,9 @@ const ANSWERS = ["AA", "AE", "AR", "other-id"] as const;
type Answer = (typeof ANSWERS)[number];

/**
* The ACK message structure, as the HL7 v2.5 standard gives it. @cosyte/hl7 ships group tables for
* ORU_R01 only, so the writer is given this one: an ACK has no groups, and the table also makes the
* writer refuse a segment that has no place in it.
* The ACK message structure, as the HL7 v2.5 standard gives it. @cosyte/hl7 ships no group table,
* so the writer is given this one: an ACK has no groups, and the table also makes the writer refuse
* a segment that has no place in it.
*/
const ACK_STRUCTURE: XmlMessageStructure = {
structure: "ACK",
Expand Down Expand Up @@ -495,8 +512,8 @@ mkdirSync(OUT, { recursive: true });
/** The XML acknowledgement to the message in `body`, with the answer `answer`. */
function acknowledge(body: string, answer: Answer): string {
const message = parseHL7Xml(body);
const ack = buildAck(message, { code: answer === "other-id" ? "AA" : answer });
if (answer === "other-id") ack.setField("MSA.2", "SYN-OTHER-0001");
const built = buildAck(message, { code: answer === "other-id" ? "AA" : answer });
const ack = answer === "other-id" ? built.withField("MSA.2", "SYN-OTHER-0001") : built;
return toHl7Xml(ack, { dataTypes: ACK_DATA_TYPES, groups: ACK_STRUCTURE });
}

Expand Down
4 changes: 2 additions & 2 deletions docs-content/logs-and-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ a tcp destination's framing cannot carry.
## MLLP problem codes

When an MLLP source stores a message as failed, the reason is one of `@cosyte/hl7`'s parse codes (such
as `NO_MSH_SEGMENT`), `NO_CONTROL_ID`, `MULTIPLE_MESSAGES` or `MLLP_TRAILING_BYTES`. An ACK builder
as `HL7_NO_MSH_SEGMENT`), `NO_CONTROL_ID`, `MULTIPLE_MESSAGES` or `MLLP_TRAILING_BYTES`. An ACK builder
receives it as `problem`. `MULTIPLE_MESSAGES` means the frame held a second message header (`MSH`,
`FHS` or `BHS`, in any case, behind or inside the id any bytes that are not printable ASCII, or a
whole header such as `MSH|^~\&|` anywhere on a line): a frame is one message. On an `hl7v2` channel the engine stores a body from any other source that holds
Expand All @@ -240,7 +240,7 @@ Besides the operating system's codes (`EACCES`, `EEXIST` and the like), a `sourc
A file source with `batch:` sets a file aside with the `reason` `batch-too-large`, `batch-empty` or
`batch-incomplete`, and stores it as a failed message with the error `batch: BATCH_FILE_TOO_LARGE`,
`batch: BATCH_EMPTY` or `batch: BATCH_INCOMPLETE`. A piece of an HL7 batch that cannot be read is
stored as failed with `batch: ` and the parser's code, such as `batch: NO_MSH_SEGMENT`. An HTTP
stored as failed with `batch: ` and the parser's code, such as `batch: HL7_NO_MSH_SEGMENT`. An HTTP
source with `batch:` answers 422 with `BATCH_EMPTY`, `BATCH_INCOMPLETE`, or `BATCH_KEY_REUSED` for an
`Idempotency-Key` used before for another body, and stores nothing.

Expand Down
3 changes: 1 addition & 2 deletions docs-content/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export default ((msg) => {
// Only admissions go to the lab. Every other message is filtered out.
if (!msg.is("ADT^A01")) return null;
// MSH-5 is the receiving application.
msg.setField("MSH.5", "LAB");
return msg;
return msg.withField("MSH.5", "LAB");
}) satisfies Transform;
```

Expand Down
6 changes: 3 additions & 3 deletions docs-content/read-batch-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import type { Hl7Message, TransformContext } from "@cosyte/pathways";

export default (msg: Hl7Message, ctx: TransformContext): Hl7Message => {
if (ctx.source?.kind === "file" && ctx.source.batch !== undefined) {
msg.setField("MSH.10", `${ctx.source.batch.hash}-${String(ctx.source.batch.piece)}`);
return msg.withField("MSH.10", `${ctx.source.batch.hash}-${String(ctx.source.batch.piece)}`);
}
return msg;
};
Expand All @@ -92,7 +92,7 @@ with the code `BATCH_FILE_TOO_LARGE`. The file source's own `maxSize` still appl
## Pieces and files that cannot be read

- With `split: hl7`, a piece `@cosyte/hl7` cannot read (text before the first `MSH`, a header too
short) is stored as a failed message, with an error such as `batch: NO_MSH_SEGMENT` and no
short) is stored as a failed message, with an error such as `batch: HL7_NO_MSH_SEGMENT` and no
content. The pieces after it are stored as usual.
- A file with no piece at all, such as one of blank lines, is failed whole with `BATCH_EMPTY`.

Expand Down Expand Up @@ -159,7 +159,7 @@ one message with one ACK.
Each batch read to the end is recorded in the [audit log](./read-the-audit-log.md) as a `batch` entry
with counts only: `pieces`, `stored`, `failed` (stored as failed), and `duplicates` (stored before,
by an earlier run or an earlier request). A file failed whole is recorded `refused`, with its
`reason`. An envelope warning, such as `BATCH_COUNT_MISMATCH` on a batch taken with `accept`, is
`reason`. An envelope warning, such as `HL7_BATCH_COUNT_MISMATCH` on a batch taken with `accept`, is
in `warnings`. No entry, log line or error names a file or holds message text.

## Related
Expand Down
Loading
Loading