Bugs5382 / node-hl7-client

A pure Node.js HL7 Client that allows for communication to a HL7 Broker/Server that can send properly formatted HL7 messages with ease.It can also parse and then you can extract message segments out.
MIT License
14 stars 4 forks source link

fix: parsing fails if message ends with line break #96

Closed jtarvainen closed 3 months ago

jtarvainen commented 4 months ago

If the inbound message ends with a line break ('\n', '\r', '\r\n'), message parsing fails with a HL7FatalError ("Segment must have a name."). This is especially problematic since the original message is stored as a private variable in the response, which also causes res.sendResponse('AA') to fail if the original message contains a trailing line break.

For example, this message cannot be parsed:

new Message({ text: 'MSH|^~\\&|device||Host||20240101000000+0000||OUL^R22^OUL_R22|2|P|2.5.1|||NE|AL||UNICODE UTF-8|||LAB-01^IHE\r' });

But this can:

new Message({ text: 'MSH|^~\\&|device||Host||20240101000000+0000||OUL^R22^OUL_R22|2|P|2.5.1|||NE|AL||UNICODE UTF-8|||LAB-01^IHE' });

I would suggest that the inbound message string be automatically trimmed (String.prototype.trim()) upon reception (i.e. before it's set in req._message and res._message) to remove any trailing line breaks.

Also, it'd be great if the library would log the error in the res.sendResponse() catch block before sending an "AE" message (the "AA" message creation failure is currently silent).

Thanks!

Bugs5382 commented 3 months ago

@jtarvainen Will review!

Bugs5382 commented 3 months ago

@jtarvainen Let's get the node-hl7-client package fixed first before I tackle this.

Bugs5382 commented 3 months ago

@jtarvainen So I moved it here cause the server doesn't do the parsing anyway. This is just a result of the node-hl7 client not doing it correctly and now corrected in #97

https://github.com/Bugs5382/node-hl7-client/blob/adb09115964b7857e33c6b11b1db84bc92beeefe/src/utils/normalizedBuilder.ts#L144

And the unit test:

https://github.com/Bugs5382/node-hl7-client/blob/adb09115964b7857e33c6b11b1db84bc92beeefe/__tests__/hl7.sanity.test.ts#L224

and

https://github.com/Bugs5382/node-hl7-client/blob/adb09115964b7857e33c6b11b1db84bc92beeefe/__tests__/hl7.sanity.test.ts#L226-L231

All pass, @jtarvainen

jtarvainen commented 3 months ago

Looks good to me!

Bugs5382 commented 3 months ago

@jtarvainen This package has been released! Be sure to leave me a star!