mllp-node-sl7 2.1.2 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/RELEASENOTES.md CHANGED
@@ -22,3 +22,6 @@ This is the initial release of this library.
22
22
  # v2.1.2 - Juli 24 , 2023
23
23
  - switched to @sourceblock-ug/sb-sl7, Version 2.0.17; added cleanup on handleAck();
24
24
  - added default Response as Param to Constructor
25
+
26
+ # v2.1.3 - Mar 28 , 2024
27
+ - fixed a Bug with the Buffer handling on parallel Socket-Connections
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mllp-node-sl7",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "MLLP (HL7) implementation in Node.js",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.ts",
package/src/index.d.ts CHANGED
@@ -52,7 +52,6 @@ export declare class MLLPServer extends EventEmitter {
52
52
  protected readonly HOST: string;
53
53
  protected readonly PORT: number;
54
54
  protected readonly TIMEOUT: number;
55
- protected message: Buffer;
56
55
  protected logger: (msg: string, ...data: unknown[]) => void;
57
56
  protected charset: string;
58
57
  private readonly TIMEOUTS;
@@ -62,6 +61,7 @@ export declare class MLLPServer extends EventEmitter {
62
61
  private readonly openConnections;
63
62
  private readonly timeoutAck;
64
63
  constructor(host: string, port: number, defaultLogger?: (msg: string) => void, timeout?: number, defaultCharset?: string, timeoutAck?: string);
64
+ private handleIncomingMessage;
65
65
  private updateState;
66
66
  private addSocket;
67
67
  private removeSocket;
package/src/index.js CHANGED
@@ -95,7 +95,6 @@ class MLLPServer extends events_1.default {
95
95
  this.openConnections = [];
96
96
  this.HOST = host || "127.0.0.1";
97
97
  this.PORT = port || 6969;
98
- this.message = Buffer.from("", "binary");
99
98
  this.TIMEOUT = timeout || 600;
100
99
  this.logger = defaultLogger || console.log;
101
100
  this.TIMEOUTS = {};
@@ -114,6 +113,7 @@ class MLLPServer extends events_1.default {
114
113
  };
115
114
  try {
116
115
  this.Server = net_1.default.createServer((sock) => {
116
+ let message = Buffer.from("", "binary");
117
117
  this.logger(`CONNECTED: ${sock.remoteAddress}:${sock.remotePort}`);
118
118
  this.addSocket(sock);
119
119
  sock.on("end", () => {
@@ -122,69 +122,24 @@ class MLLPServer extends events_1.default {
122
122
  this.logger("server disconnected", this.HOST, this.PORT);
123
123
  this.removeSocket(sock);
124
124
  });
125
- const handleIncomingMessage = (messageBuffer) => {
126
- let messageString = messageBuffer.toString();
127
- let data2 = new sb_sl7_1.Message(messageString);
128
- let msgId = data2.getString("MSH-10");
129
- let encoding = data2.getString("MSH-18");
130
- if (encoding === undefined || encoding === null || encoding === "") {
131
- // use Default:
132
- encoding = this.charset;
133
- }
134
- if (encoding !== "UNICODE UTF-8") {
135
- // Decoding needed:
136
- messageString = (0, decoder_1.default)(messageBuffer, encoding);
137
- data2 = new sb_sl7_1.Message(messageString);
138
- msgId = data2.getString("MSH-10");
139
- }
140
- this.logger(`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`);
141
- const event = {
142
- id: msgId,
143
- ack: this.timeoutAck,
144
- msg: messageString,
145
- hl7: data2,
146
- buffer: messageBuffer,
147
- };
148
- if (this.openEvents[msgId] === undefined) {
149
- // Using a Timeout if no response has been sended within the timeout.
150
- this.TIMEOUTS[msgId] = setTimeout(() => {
151
- this.handleAck(event, "timeout");
152
- }, this.TIMEOUT);
153
- this.openEvents[msgId] = { sock, org: this.message }; // save socket and Message for Response
154
- /**
155
- * MLLP HL7 Event. Fired when a HL7 Message is received.
156
- * @event MLLPServer#hl7
157
- * @type {string}
158
- * @property {object} Event with the message string (msg), the Msg-ID, the default ACK and the parsed HL7 Object
159
- */
160
- this.emit("hl7", event);
161
- }
162
- else {
163
- // The Message ID is currently already in Progress... send a direkt REJECT-Message
164
- const ackMsg = sb_sl7_1.Message.createResponse(event.hl7, "AR", "Message already in progress");
165
- ackMsg.cleanup();
166
- const ack = ackMsg.render();
167
- sock.write(VT + ack + FS + CR);
168
- }
169
- };
170
125
  /*
171
126
  * handling incoming Data. Currently only one message per Connection is supported.
172
127
  */
173
128
  sock.on("data", (data) => {
174
- this.message = Buffer.concat([this.message, data]);
175
- while (this.message.indexOf(FS + CR) > -1) {
176
- let subBuffer = this.message.subarray(0, this.message.indexOf(FS + CR));
177
- this.message = this.message.subarray(this.message.indexOf(FS + CR) + 2);
129
+ message = Buffer.concat([message, data]);
130
+ while (message.indexOf(FS + CR) > -1) {
131
+ let subBuffer = message.subarray(0, message.indexOf(FS + CR));
132
+ message = message.subarray(message.indexOf(FS + CR) + 2);
178
133
  if (subBuffer.indexOf(VTi) > -1) {
179
134
  subBuffer = subBuffer.subarray(subBuffer.indexOf(VTi) + 1);
180
135
  }
181
- handleIncomingMessage(subBuffer);
136
+ this.handleIncomingMessage(subBuffer, sock);
182
137
  }
183
- if (this.message.indexOf(VTi) > 0) {
138
+ if (message.indexOf(VTi) > 0) {
184
139
  // got a new Message indicator - but there is something before that message - handle as (not proper closed) message:
185
- const unwrappedBuffer = this.message.subarray(0, this.message.indexOf(VTi));
186
- this.message = this.message.subarray(this.message.indexOf(VTi) + 1);
187
- handleIncomingMessage(unwrappedBuffer);
140
+ const unwrappedBuffer = message.subarray(0, message.indexOf(VTi));
141
+ message = message.subarray(message.indexOf(VTi) + 1);
142
+ this.handleIncomingMessage(unwrappedBuffer, sock);
188
143
  }
189
144
  });
190
145
  // emit incoming errors on the Sock to the outside world
@@ -226,6 +181,52 @@ class MLLPServer extends events_1.default {
226
181
  throw new Error(`Error Listen to ${this.HOST}:${this.PORT}`);
227
182
  }
228
183
  }
184
+ handleIncomingMessage(messageBuffer, sock) {
185
+ let messageString = messageBuffer.toString();
186
+ let data2 = new sb_sl7_1.Message(messageString);
187
+ let msgId = data2.getString("MSH-10");
188
+ let encoding = data2.getString("MSH-18");
189
+ if (encoding === undefined || encoding === null || encoding === "") {
190
+ // use Default:
191
+ encoding = this.charset;
192
+ }
193
+ if (encoding !== "UNICODE UTF-8") {
194
+ // Decoding needed:
195
+ messageString = (0, decoder_1.default)(messageBuffer, encoding);
196
+ data2 = new sb_sl7_1.Message(messageString);
197
+ msgId = data2.getString("MSH-10");
198
+ }
199
+ this.logger(`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`);
200
+ const event = {
201
+ id: msgId,
202
+ ack: this.timeoutAck,
203
+ msg: messageString,
204
+ hl7: data2,
205
+ buffer: messageBuffer,
206
+ };
207
+ if (this.openEvents[msgId] === undefined) {
208
+ // Using a Timeout if no response has been sended within the timeout.
209
+ this.TIMEOUTS[msgId] = setTimeout(() => {
210
+ this.handleAck(event, "timeout");
211
+ }, this.TIMEOUT);
212
+ this.openEvents[msgId] = { sock, org: messageBuffer }; // save socket and Message for Response
213
+ /**
214
+ * MLLP HL7 Event. Fired when a HL7 Message is received.
215
+ * @event MLLPServer#hl7
216
+ * @type {string}
217
+ * @property {object} Event with the message string (msg), the Msg-ID, the default ACK and the parsed HL7 Object
218
+ */
219
+ this.emit("hl7", event);
220
+ }
221
+ else {
222
+ // The Message ID is currently already in Progress... send a direkt REJECT-Message
223
+ const ackMsg = sb_sl7_1.Message.createResponse(event.hl7, "AR", "Message already in progress");
224
+ ackMsg.cleanup();
225
+ const ack = ackMsg.render();
226
+ sock.write(VT + ack + FS + CR);
227
+ }
228
+ }
229
+ ;
229
230
  updateState() {
230
231
  this.connectionEventState.connected = this.openConnections.length > 0;
231
232
  let info = "";
package/src/index.ts CHANGED
@@ -140,8 +140,6 @@ export class MLLPServer extends EventEmitter {
140
140
 
141
141
  protected readonly TIMEOUT: number;
142
142
 
143
- protected message: Buffer;
144
-
145
143
  protected logger: (msg: string, ...data: unknown[]) => void;
146
144
 
147
145
  protected charset: string;
@@ -169,7 +167,6 @@ export class MLLPServer extends EventEmitter {
169
167
  super();
170
168
  this.HOST = host || "127.0.0.1";
171
169
  this.PORT = port || 6969;
172
- this.message = Buffer.from("", "binary");
173
170
  this.TIMEOUT = timeout || 600;
174
171
  this.logger = defaultLogger || console.log;
175
172
  this.TIMEOUTS = {};
@@ -189,6 +186,7 @@ export class MLLPServer extends EventEmitter {
189
186
 
190
187
  try {
191
188
  this.Server = net.createServer((sock) => {
189
+ let message: Buffer = Buffer.from("", "binary");
192
190
  this.logger(`CONNECTED: ${sock.remoteAddress}:${sock.remotePort}`);
193
191
  this.addSocket(sock);
194
192
 
@@ -199,90 +197,26 @@ export class MLLPServer extends EventEmitter {
199
197
  this.removeSocket(sock);
200
198
  });
201
199
 
202
- const handleIncomingMessage = (messageBuffer: Buffer) => {
203
- let messageString = messageBuffer.toString();
204
- let data2 = new Message(messageString);
205
- let msgId: string = data2.getString("MSH-10");
206
- let encoding = data2.getString("MSH-18");
207
- if (encoding === undefined || encoding === null || encoding === "") {
208
- // use Default:
209
- encoding = this.charset;
210
- }
211
-
212
- if (encoding !== "UNICODE UTF-8") {
213
- // Decoding needed:
214
- messageString = decoder(messageBuffer, encoding);
215
- data2 = new Message(messageString);
216
- msgId = data2.getString("MSH-10");
217
- }
218
- this.logger(
219
- `Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`
220
- );
221
-
222
- const event: IncomingMessageEvent = {
223
- id: msgId,
224
- ack: this.timeoutAck,
225
- msg: messageString,
226
- hl7: data2,
227
- buffer: messageBuffer,
228
- };
229
-
230
- if (this.openEvents[msgId] === undefined) {
231
- // Using a Timeout if no response has been sended within the timeout.
232
- this.TIMEOUTS[msgId] = setTimeout(() => {
233
- this.handleAck(event, "timeout");
234
- }, this.TIMEOUT);
235
-
236
- this.openEvents[msgId] = { sock, org: this.message }; // save socket and Message for Response
237
-
238
- /**
239
- * MLLP HL7 Event. Fired when a HL7 Message is received.
240
- * @event MLLPServer#hl7
241
- * @type {string}
242
- * @property {object} Event with the message string (msg), the Msg-ID, the default ACK and the parsed HL7 Object
243
- */
244
- this.emit("hl7", event);
245
- } else {
246
- // The Message ID is currently already in Progress... send a direkt REJECT-Message
247
- const ackMsg = Message.createResponse(
248
- event.hl7,
249
- "AR",
250
- "Message already in progress"
251
- );
252
- ackMsg.cleanup();
253
- const ack = ackMsg.render();
254
- sock.write(VT + ack + FS + CR);
255
- }
256
- };
257
-
258
200
  /*
259
201
  * handling incoming Data. Currently only one message per Connection is supported.
260
202
  */
261
203
  sock.on("data", (data) => {
262
- this.message = Buffer.concat([this.message, data]);
263
-
264
- while (this.message.indexOf(FS + CR) > -1) {
265
- let subBuffer = this.message.subarray(
266
- 0,
267
- this.message.indexOf(FS + CR)
268
- );
269
- this.message = this.message.subarray(
270
- this.message.indexOf(FS + CR) + 2
271
- );
204
+ message = Buffer.concat([message, data]);
205
+
206
+ while (message.indexOf(FS + CR) > -1) {
207
+ let subBuffer = message.subarray(0, message.indexOf(FS + CR));
208
+ message = message.subarray(message.indexOf(FS + CR) + 2);
272
209
  if (subBuffer.indexOf(VTi) > -1) {
273
210
  subBuffer = subBuffer.subarray(subBuffer.indexOf(VTi) + 1);
274
211
  }
275
- handleIncomingMessage(subBuffer);
212
+ this.handleIncomingMessage(subBuffer, sock);
276
213
  }
277
214
 
278
- if (this.message.indexOf(VTi) > 0) {
215
+ if (message.indexOf(VTi) > 0) {
279
216
  // got a new Message indicator - but there is something before that message - handle as (not proper closed) message:
280
- const unwrappedBuffer = this.message.subarray(
281
- 0,
282
- this.message.indexOf(VTi)
283
- );
284
- this.message = this.message.subarray(this.message.indexOf(VTi) + 1);
285
- handleIncomingMessage(unwrappedBuffer);
217
+ const unwrappedBuffer = message.subarray(0, message.indexOf(VTi));
218
+ message = message.subarray(message.indexOf(VTi) + 1);
219
+ this.handleIncomingMessage(unwrappedBuffer, sock);
286
220
  }
287
221
  });
288
222
 
@@ -328,6 +262,60 @@ export class MLLPServer extends EventEmitter {
328
262
  }
329
263
  }
330
264
 
265
+ private handleIncomingMessage(messageBuffer: Buffer, sock: net.Socket) {
266
+ let messageString = messageBuffer.toString();
267
+ let data2 = new Message(messageString);
268
+ let msgId: string = data2.getString("MSH-10");
269
+ let encoding = data2.getString("MSH-18");
270
+ if (encoding === undefined || encoding === null || encoding === "") {
271
+ // use Default:
272
+ encoding = this.charset;
273
+ }
274
+
275
+ if (encoding !== "UNICODE UTF-8") {
276
+ // Decoding needed:
277
+ messageString = decoder(messageBuffer, encoding);
278
+ data2 = new Message(messageString);
279
+ msgId = data2.getString("MSH-10");
280
+ }
281
+ this.logger(`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`);
282
+
283
+ const event: IncomingMessageEvent = {
284
+ id: msgId,
285
+ ack: this.timeoutAck,
286
+ msg: messageString,
287
+ hl7: data2,
288
+ buffer: messageBuffer,
289
+ };
290
+
291
+ if (this.openEvents[msgId] === undefined) {
292
+ // Using a Timeout if no response has been sended within the timeout.
293
+ this.TIMEOUTS[msgId] = setTimeout(() => {
294
+ this.handleAck(event, "timeout");
295
+ }, this.TIMEOUT);
296
+
297
+ this.openEvents[msgId] = { sock, org: messageBuffer }; // save socket and Message for Response
298
+
299
+ /**
300
+ * MLLP HL7 Event. Fired when a HL7 Message is received.
301
+ * @event MLLPServer#hl7
302
+ * @type {string}
303
+ * @property {object} Event with the message string (msg), the Msg-ID, the default ACK and the parsed HL7 Object
304
+ */
305
+ this.emit("hl7", event);
306
+ } else {
307
+ // The Message ID is currently already in Progress... send a direkt REJECT-Message
308
+ const ackMsg = Message.createResponse(
309
+ event.hl7,
310
+ "AR",
311
+ "Message already in progress"
312
+ );
313
+ ackMsg.cleanup();
314
+ const ack = ackMsg.render();
315
+ sock.write(VT + ack + FS + CR);
316
+ }
317
+ }
318
+
331
319
  private updateState(): MLLPConnectionState {
332
320
  this.connectionEventState.connected = this.openConnections.length > 0;
333
321
  let info = "";