mllp-node-sl7 2.1.3 → 2.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mllp-node-sl7",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
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
@@ -7,7 +7,7 @@ import { Message } from "@sourceblock-ug/sb-sl7";
7
7
  export interface Renderable {
8
8
  render(): string | Buffer;
9
9
  }
10
- export declare function mllpSendMessage(receivingHost: string, receivingPort: number, hl7Data: Buffer | Renderable | string, callback: (err: Error | null, response: string | null) => void, logger?: (msg: string) => void): void;
10
+ export declare function mllpSendMessage(receivingHost: string, receivingPort: number, hl7Data: Buffer | Renderable | string, callback: (err: Error | null, response: string | null, responseMessage: string | null) => void, logger?: (msg: string) => void): void;
11
11
  export interface MessageResponseEvent {
12
12
  id: string;
13
13
  ack: string | Renderable | unknown;
@@ -70,8 +70,8 @@ export declare class MLLPServer extends EventEmitter {
70
70
  currentRemote(): string | null;
71
71
  static createResponseHeader(data: Message | string | object): string;
72
72
  private handleAck;
73
- response(event: MessageResponseEvent): void;
74
- sendResponse(msgId: string, ack: string): void;
73
+ response(event: MessageResponseEvent): boolean;
74
+ sendResponse(msgId: string, ack: string): boolean;
75
75
  send(receivingHost: string, receivingPort: number, hl7Data: Buffer | Renderable | string, callback: (err: Error | null, response: string | null) => void): void;
76
76
  close(done?: (err?: Error) => void): void;
77
77
  private static closeSocket;
package/src/index.js CHANGED
@@ -45,20 +45,19 @@ function mllpSendMessage(receivingHost, receivingPort, hl7Data, callback, logger
45
45
  log(`closing connection with ${receivingHost}:${receivingPort}`);
46
46
  sendingClient.end();
47
47
  };
48
- sendingClient.on("data", (rawAckData) => {
48
+ const cleanup = (msg) => {
49
+ return msg.replace(VT, '').replace(FS, '').replace(CR, '');
50
+ };
51
+ sendingClient.on('data', (rawAckData) => {
49
52
  log(`${receivingHost}:${receivingPort} ACKED data`);
50
- const ackData = rawAckData
51
- .toString() // Buffer -> String
52
- .replace(VT, "")
53
- .split("\r")[1] // Ack data
54
- .replace(FS, "")
55
- .replace(CR, "");
56
- callback(null, ackData);
53
+ const responseMessage = rawAckData.toString().replace(VT, '');
54
+ const ackData = responseMessage.split('\r')[1]; // Ack data
55
+ callback(null, cleanup(ackData), cleanup(responseMessage));
57
56
  terminate();
58
57
  });
59
- sendingClient.on("error", (error) => {
58
+ sendingClient.on('error', (error) => {
60
59
  log(`${receivingHost}:${receivingPort} couldn't process data`);
61
- callback(error, null);
60
+ callback(error, null, null);
62
61
  terminate();
63
62
  });
64
63
  }
@@ -197,6 +196,9 @@ class MLLPServer extends events_1.default {
197
196
  msgId = data2.getString("MSH-10");
198
197
  }
199
198
  this.logger(`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`);
199
+ if (msgId === "") {
200
+ msgId = Math.random().toString(36).substring(2);
201
+ }
200
202
  const event = {
201
203
  id: msgId,
202
204
  ack: this.timeoutAck,
@@ -226,7 +228,6 @@ class MLLPServer extends events_1.default {
226
228
  sock.write(VT + ack + FS + CR);
227
229
  }
228
230
  }
229
- ;
230
231
  updateState() {
231
232
  this.connectionEventState.connected = this.openConnections.length > 0;
232
233
  let info = "";
@@ -292,6 +293,7 @@ class MLLPServer extends events_1.default {
292
293
  ack = ackMsg.render();
293
294
  }
294
295
  inMsg.sock.write(VT + ack + FS + CR);
296
+ return true;
295
297
  }
296
298
  catch (e) {
297
299
  this.logger(`Error sending response in mode ${mode}`, e);
@@ -300,18 +302,18 @@ class MLLPServer extends events_1.default {
300
302
  else {
301
303
  this.logger(`Response already send! Cannot process another response in mode ${mode}`, event);
302
304
  }
305
+ return false;
303
306
  }
304
307
  response(event) {
305
308
  if (this.openEvents[event.id] !== undefined) {
306
309
  const inMsg = this.openEvents[event.id];
307
- this.handleAck(Object.assign({ hl7: new sb_sl7_1.Message(inMsg.org), msg: inMsg.org.toString(), buffer: inMsg.org }, event), "direct");
308
- }
309
- else {
310
- this.logger(`Response already send! Cannot process another response directly.`, event);
310
+ return this.handleAck(Object.assign({ hl7: new sb_sl7_1.Message(inMsg.org), msg: inMsg.org.toString(), buffer: inMsg.org }, event), "direct");
311
311
  }
312
+ this.logger(`Response already send! Cannot process another response directly.`, event);
313
+ return false;
312
314
  }
313
315
  sendResponse(msgId, ack) {
314
- this.response({ id: msgId, ack });
316
+ return this.response({ id: msgId, ack });
315
317
  }
316
318
  send(receivingHost, receivingPort, hl7Data, callback) {
317
319
  mllpSendMessage(receivingHost, receivingPort, hl7Data, callback, this.logger);
package/src/index.ts CHANGED
@@ -34,7 +34,11 @@ export function mllpSendMessage(
34
34
  receivingHost: string,
35
35
  receivingPort: number,
36
36
  hl7Data: Buffer | Renderable | string,
37
- callback: (err: Error | null, response: string | null) => void,
37
+ callback: (
38
+ err: Error | null,
39
+ response: string | null,
40
+ responseMessage: string | null
41
+ ) => void,
38
42
  logger?: (msg: string) => void
39
43
  ) {
40
44
  // Render Message if it is an object:
@@ -62,24 +66,24 @@ export function mllpSendMessage(
62
66
  sendingClient.end();
63
67
  };
64
68
 
69
+ const cleanup = (msg: string): string => {
70
+ return msg.replace(VT, "").replace(FS, "").replace(CR, "");
71
+ };
72
+
65
73
  sendingClient.on("data", (rawAckData) => {
66
74
  log(`${receivingHost}:${receivingPort} ACKED data`);
67
75
 
68
- const ackData = rawAckData
69
- .toString() // Buffer -> String
70
- .replace(VT, "")
71
- .split("\r")[1] // Ack data
72
- .replace(FS, "")
73
- .replace(CR, "");
76
+ const responseMessage = rawAckData.toString().replace(VT, "");
77
+ const ackData = responseMessage.split("\r")[1]; // Ack data
74
78
 
75
- callback(null, ackData);
79
+ callback(null, cleanup(ackData), cleanup(responseMessage));
76
80
  terminate();
77
81
  });
78
82
 
79
83
  sendingClient.on("error", (error) => {
80
84
  log(`${receivingHost}:${receivingPort} couldn't process data`);
81
85
 
82
- callback(error, null);
86
+ callback(error, null, null);
83
87
  terminate();
84
88
  });
85
89
  }
@@ -280,6 +284,10 @@ export class MLLPServer extends EventEmitter {
280
284
  }
281
285
  this.logger(`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`);
282
286
 
287
+ if (msgId === "") {
288
+ msgId = Math.random().toString(36).substring(2);
289
+ }
290
+
283
291
  const event: IncomingMessageEvent = {
284
292
  id: msgId,
285
293
  ack: this.timeoutAck,
@@ -360,7 +368,7 @@ export class MLLPServer extends EventEmitter {
360
368
  return message.render();
361
369
  }
362
370
 
363
- private handleAck(event: IncomingMessageEvent, mode: any): void {
371
+ private handleAck(event: IncomingMessageEvent, mode: any): boolean {
364
372
  if (this.openEvents[event.id] !== undefined) {
365
373
  const inMsg = this.openEvents[event.id];
366
374
  delete this.openEvents[event.id];
@@ -391,6 +399,7 @@ export class MLLPServer extends EventEmitter {
391
399
  ack = ackMsg.render();
392
400
  }
393
401
  inMsg.sock.write(VT + ack + FS + CR);
402
+ return true;
394
403
  } catch (e) {
395
404
  this.logger(`Error sending response in mode ${mode}`, e);
396
405
  }
@@ -400,12 +409,13 @@ export class MLLPServer extends EventEmitter {
400
409
  event
401
410
  );
402
411
  }
412
+ return false;
403
413
  }
404
414
 
405
- public response(event: MessageResponseEvent) {
415
+ public response(event: MessageResponseEvent): boolean {
406
416
  if (this.openEvents[event.id] !== undefined) {
407
417
  const inMsg = this.openEvents[event.id];
408
- this.handleAck(
418
+ return this.handleAck(
409
419
  {
410
420
  hl7: new Message(inMsg.org),
411
421
  msg: inMsg.org.toString(),
@@ -414,16 +424,16 @@ export class MLLPServer extends EventEmitter {
414
424
  },
415
425
  "direct"
416
426
  );
417
- } else {
418
- this.logger(
419
- `Response already send! Cannot process another response directly.`,
420
- event
421
- );
422
427
  }
428
+ this.logger(
429
+ `Response already send! Cannot process another response directly.`,
430
+ event
431
+ );
432
+ return false;
423
433
  }
424
434
 
425
- public sendResponse(msgId: string, ack: string) {
426
- this.response({ id: msgId, ack });
435
+ public sendResponse(msgId: string, ack: string): boolean {
436
+ return this.response({ id: msgId, ack });
427
437
  }
428
438
 
429
439
  public send(