mllp-node-sl7 2.1.1 → 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 +7 -0
- package/package.json +2 -2
- package/src/index.d.ts +5 -3
- package/src/index.js +73 -59
- package/src/index.ts +84 -80
package/RELEASENOTES.md
CHANGED
|
@@ -18,3 +18,10 @@ This is the initial release of this library.
|
|
|
18
18
|
- added Socket-Termination on server.close()
|
|
19
19
|
- Switched to eslint & prettier and Updated code accordingly
|
|
20
20
|
- updated dependencies
|
|
21
|
+
|
|
22
|
+
# v2.1.2 - Juli 24 , 2023
|
|
23
|
+
- switched to @sourceblock-ug/sb-sl7, Version 2.0.17; added cleanup on handleAck();
|
|
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.
|
|
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",
|
|
@@ -53,6 +53,6 @@
|
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"iconv-lite": "^0.6.0",
|
|
56
|
-
"sb-sl7": "^2.0.
|
|
56
|
+
"@sourceblock-ug/sb-sl7": "^2.0.17"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import net from "net";
|
|
5
5
|
import EventEmitter from "events";
|
|
6
|
-
import { Message } from "sb-sl7
|
|
6
|
+
import { Message } from "@sourceblock-ug/sb-sl7";
|
|
7
7
|
export interface Renderable {
|
|
8
8
|
render(): string | Buffer;
|
|
9
9
|
}
|
|
@@ -30,6 +30,7 @@ export interface MLLPConnectionState {
|
|
|
30
30
|
* @param defaultLogger
|
|
31
31
|
* @param {number} timeout after which the answer is sended.
|
|
32
32
|
* @param {string} defaultCharset for Message decoding
|
|
33
|
+
* @param {string} timeoutAck like AA or AE - default is AA
|
|
33
34
|
*
|
|
34
35
|
* @fires MLLPServer#hl7
|
|
35
36
|
*
|
|
@@ -51,7 +52,6 @@ export declare class MLLPServer extends EventEmitter {
|
|
|
51
52
|
protected readonly HOST: string;
|
|
52
53
|
protected readonly PORT: number;
|
|
53
54
|
protected readonly TIMEOUT: number;
|
|
54
|
-
protected message: Buffer;
|
|
55
55
|
protected logger: (msg: string, ...data: unknown[]) => void;
|
|
56
56
|
protected charset: string;
|
|
57
57
|
private readonly TIMEOUTS;
|
|
@@ -59,7 +59,9 @@ export declare class MLLPServer extends EventEmitter {
|
|
|
59
59
|
protected Server: net.Server;
|
|
60
60
|
protected connectionEventState: MLLPConnectionState;
|
|
61
61
|
private readonly openConnections;
|
|
62
|
-
|
|
62
|
+
private readonly timeoutAck;
|
|
63
|
+
constructor(host: string, port: number, defaultLogger?: (msg: string) => void, timeout?: number, defaultCharset?: string, timeoutAck?: string);
|
|
64
|
+
private handleIncomingMessage;
|
|
63
65
|
private updateState;
|
|
64
66
|
private addSocket;
|
|
65
67
|
private removeSocket;
|
package/src/index.js
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.MLLPServer = exports.mllpSendMessage = void 0;
|
|
7
7
|
const net_1 = __importDefault(require("net"));
|
|
8
8
|
const events_1 = __importDefault(require("events"));
|
|
9
|
-
const
|
|
9
|
+
const sb_sl7_1 = require("@sourceblock-ug/sb-sl7");
|
|
10
10
|
const decoder_1 = __importDefault(require("./decoder"));
|
|
11
11
|
// The header is a vertical tab character <VT> its hex value is 0x0b.
|
|
12
12
|
// The trailer is a field separator character <FS> (hex 0x1c) immediately followed by a carriage return <CR> (hex 0x0d)
|
|
@@ -71,6 +71,7 @@ exports.mllpSendMessage = mllpSendMessage;
|
|
|
71
71
|
* @param defaultLogger
|
|
72
72
|
* @param {number} timeout after which the answer is sended.
|
|
73
73
|
* @param {string} defaultCharset for Message decoding
|
|
74
|
+
* @param {string} timeoutAck like AA or AE - default is AA
|
|
74
75
|
*
|
|
75
76
|
* @fires MLLPServer#hl7
|
|
76
77
|
*
|
|
@@ -89,16 +90,19 @@ exports.mllpSendMessage = mllpSendMessage;
|
|
|
89
90
|
*
|
|
90
91
|
*/
|
|
91
92
|
class MLLPServer extends events_1.default {
|
|
92
|
-
constructor(host, port, defaultLogger, timeout, defaultCharset) {
|
|
93
|
+
constructor(host, port, defaultLogger, timeout, defaultCharset, timeoutAck) {
|
|
93
94
|
super();
|
|
94
95
|
this.openConnections = [];
|
|
95
96
|
this.HOST = host || "127.0.0.1";
|
|
96
97
|
this.PORT = port || 6969;
|
|
97
|
-
this.message = Buffer.from("", "binary");
|
|
98
98
|
this.TIMEOUT = timeout || 600;
|
|
99
99
|
this.logger = defaultLogger || console.log;
|
|
100
100
|
this.TIMEOUTS = {};
|
|
101
101
|
this.openEvents = {};
|
|
102
|
+
this.timeoutAck =
|
|
103
|
+
typeof timeoutAck === "string" && timeoutAck.length === 2
|
|
104
|
+
? timeoutAck.toUpperCase()
|
|
105
|
+
: "AA";
|
|
102
106
|
this.charset =
|
|
103
107
|
defaultCharset !== undefined ? `${defaultCharset}` : "UNICODE UTF-8";
|
|
104
108
|
this.connectionEventState = {
|
|
@@ -109,6 +113,7 @@ class MLLPServer extends events_1.default {
|
|
|
109
113
|
};
|
|
110
114
|
try {
|
|
111
115
|
this.Server = net_1.default.createServer((sock) => {
|
|
116
|
+
let message = Buffer.from("", "binary");
|
|
112
117
|
this.logger(`CONNECTED: ${sock.remoteAddress}:${sock.remotePort}`);
|
|
113
118
|
this.addSocket(sock);
|
|
114
119
|
sock.on("end", () => {
|
|
@@ -117,67 +122,24 @@ class MLLPServer extends events_1.default {
|
|
|
117
122
|
this.logger("server disconnected", this.HOST, this.PORT);
|
|
118
123
|
this.removeSocket(sock);
|
|
119
124
|
});
|
|
120
|
-
const handleIncomingMessage = (messageBuffer) => {
|
|
121
|
-
let messageString = messageBuffer.toString();
|
|
122
|
-
let data2 = new message_1.Message(messageString);
|
|
123
|
-
let msgId = data2.getString("MSH-10");
|
|
124
|
-
let encoding = data2.getString("MSH-18");
|
|
125
|
-
if (encoding === undefined || encoding === null || encoding === "") {
|
|
126
|
-
// use Default:
|
|
127
|
-
encoding = this.charset;
|
|
128
|
-
}
|
|
129
|
-
if (encoding !== "UNICODE UTF-8") {
|
|
130
|
-
// Decoding needed:
|
|
131
|
-
messageString = (0, decoder_1.default)(messageBuffer, encoding);
|
|
132
|
-
data2 = new message_1.Message(messageString);
|
|
133
|
-
msgId = data2.getString("MSH-10");
|
|
134
|
-
}
|
|
135
|
-
this.logger(`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`);
|
|
136
|
-
const event = {
|
|
137
|
-
id: msgId,
|
|
138
|
-
ack: "AA",
|
|
139
|
-
msg: messageString,
|
|
140
|
-
hl7: data2,
|
|
141
|
-
buffer: messageBuffer,
|
|
142
|
-
};
|
|
143
|
-
if (this.openEvents[msgId] === undefined) {
|
|
144
|
-
// Using a Timeout if no response has been sended within the timeout.
|
|
145
|
-
this.TIMEOUTS[msgId] = setTimeout(() => {
|
|
146
|
-
this.handleAck(event, "timeout");
|
|
147
|
-
}, this.TIMEOUT);
|
|
148
|
-
this.openEvents[msgId] = { sock, org: this.message }; // save socket and Message for Response
|
|
149
|
-
/**
|
|
150
|
-
* MLLP HL7 Event. Fired when a HL7 Message is received.
|
|
151
|
-
* @event MLLPServer#hl7
|
|
152
|
-
* @type {string}
|
|
153
|
-
* @property {object} Event with the message string (msg), the Msg-ID, the default ACK and the parsed HL7 Object
|
|
154
|
-
*/
|
|
155
|
-
this.emit("hl7", event);
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
// The Message ID is currently already in Progress... send a direkt REJECT-Message
|
|
159
|
-
const ack = message_1.Message.createResponse(event.hl7, "AR", "Message already in progress").render();
|
|
160
|
-
sock.write(VT + ack + FS + CR);
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
125
|
/*
|
|
164
126
|
* handling incoming Data. Currently only one message per Connection is supported.
|
|
165
127
|
*/
|
|
166
128
|
sock.on("data", (data) => {
|
|
167
|
-
|
|
168
|
-
while (
|
|
169
|
-
let subBuffer =
|
|
170
|
-
|
|
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);
|
|
171
133
|
if (subBuffer.indexOf(VTi) > -1) {
|
|
172
134
|
subBuffer = subBuffer.subarray(subBuffer.indexOf(VTi) + 1);
|
|
173
135
|
}
|
|
174
|
-
handleIncomingMessage(subBuffer);
|
|
136
|
+
this.handleIncomingMessage(subBuffer, sock);
|
|
175
137
|
}
|
|
176
|
-
if (
|
|
138
|
+
if (message.indexOf(VTi) > 0) {
|
|
177
139
|
// got a new Message indicator - but there is something before that message - handle as (not proper closed) message:
|
|
178
|
-
const unwrappedBuffer =
|
|
179
|
-
|
|
180
|
-
handleIncomingMessage(unwrappedBuffer);
|
|
140
|
+
const unwrappedBuffer = message.subarray(0, message.indexOf(VTi));
|
|
141
|
+
message = message.subarray(message.indexOf(VTi) + 1);
|
|
142
|
+
this.handleIncomingMessage(unwrappedBuffer, sock);
|
|
181
143
|
}
|
|
182
144
|
});
|
|
183
145
|
// emit incoming errors on the Sock to the outside world
|
|
@@ -219,6 +181,52 @@ class MLLPServer extends events_1.default {
|
|
|
219
181
|
throw new Error(`Error Listen to ${this.HOST}:${this.PORT}`);
|
|
220
182
|
}
|
|
221
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
|
+
;
|
|
222
230
|
updateState() {
|
|
223
231
|
this.connectionEventState.connected = this.openConnections.length > 0;
|
|
224
232
|
let info = "";
|
|
@@ -250,7 +258,9 @@ class MLLPServer extends events_1.default {
|
|
|
250
258
|
return this.connectionEventState.remote;
|
|
251
259
|
}
|
|
252
260
|
static createResponseHeader(data) {
|
|
253
|
-
|
|
261
|
+
const message = sb_sl7_1.Message.createResponse(data);
|
|
262
|
+
message.cleanup();
|
|
263
|
+
return message.render();
|
|
254
264
|
}
|
|
255
265
|
handleAck(event, mode) {
|
|
256
266
|
if (this.openEvents[event.id] !== undefined) {
|
|
@@ -269,13 +279,17 @@ class MLLPServer extends events_1.default {
|
|
|
269
279
|
else if (typeof event.ack === "string" &&
|
|
270
280
|
event.ack.length === 2 &&
|
|
271
281
|
event.hl7) {
|
|
272
|
-
|
|
282
|
+
const ackMsg = sb_sl7_1.Message.createResponse(event.hl7, event.ack);
|
|
283
|
+
ackMsg.cleanup();
|
|
284
|
+
ack = ackMsg.render();
|
|
273
285
|
}
|
|
274
286
|
else if (typeof event.ack === "string") {
|
|
275
287
|
ack = event.ack;
|
|
276
288
|
}
|
|
277
289
|
else {
|
|
278
|
-
|
|
290
|
+
const ackMsg = sb_sl7_1.Message.createResponse(event.hl7, this.timeoutAck);
|
|
291
|
+
ackMsg.cleanup();
|
|
292
|
+
ack = ackMsg.render();
|
|
279
293
|
}
|
|
280
294
|
inMsg.sock.write(VT + ack + FS + CR);
|
|
281
295
|
}
|
|
@@ -290,7 +304,7 @@ class MLLPServer extends events_1.default {
|
|
|
290
304
|
response(event) {
|
|
291
305
|
if (this.openEvents[event.id] !== undefined) {
|
|
292
306
|
const inMsg = this.openEvents[event.id];
|
|
293
|
-
this.handleAck(Object.assign({ hl7: new
|
|
307
|
+
this.handleAck(Object.assign({ hl7: new sb_sl7_1.Message(inMsg.org), msg: inMsg.org.toString(), buffer: inMsg.org }, event), "direct");
|
|
294
308
|
}
|
|
295
309
|
else {
|
|
296
310
|
this.logger(`Response already send! Cannot process another response directly.`, event);
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import net from "net";
|
|
2
2
|
import EventEmitter from "events";
|
|
3
|
-
import { Message } from "sb-sl7
|
|
3
|
+
import { Message } from "@sourceblock-ug/sb-sl7";
|
|
4
4
|
import decoder from "./decoder";
|
|
5
5
|
|
|
6
6
|
// The header is a vertical tab character <VT> its hex value is 0x0b.
|
|
@@ -115,6 +115,7 @@ interface OpenSocket {
|
|
|
115
115
|
* @param defaultLogger
|
|
116
116
|
* @param {number} timeout after which the answer is sended.
|
|
117
117
|
* @param {string} defaultCharset for Message decoding
|
|
118
|
+
* @param {string} timeoutAck like AA or AE - default is AA
|
|
118
119
|
*
|
|
119
120
|
* @fires MLLPServer#hl7
|
|
120
121
|
*
|
|
@@ -139,8 +140,6 @@ export class MLLPServer extends EventEmitter {
|
|
|
139
140
|
|
|
140
141
|
protected readonly TIMEOUT: number;
|
|
141
142
|
|
|
142
|
-
protected message: Buffer;
|
|
143
|
-
|
|
144
143
|
protected logger: (msg: string, ...data: unknown[]) => void;
|
|
145
144
|
|
|
146
145
|
protected charset: string;
|
|
@@ -155,21 +154,27 @@ export class MLLPServer extends EventEmitter {
|
|
|
155
154
|
|
|
156
155
|
private readonly openConnections: net.Socket[] = [];
|
|
157
156
|
|
|
157
|
+
private readonly timeoutAck: string;
|
|
158
|
+
|
|
158
159
|
constructor(
|
|
159
160
|
host: string,
|
|
160
161
|
port: number,
|
|
161
162
|
defaultLogger?: (msg: string) => void,
|
|
162
163
|
timeout?: number,
|
|
163
|
-
defaultCharset?: string
|
|
164
|
+
defaultCharset?: string,
|
|
165
|
+
timeoutAck?: string
|
|
164
166
|
) {
|
|
165
167
|
super();
|
|
166
168
|
this.HOST = host || "127.0.0.1";
|
|
167
169
|
this.PORT = port || 6969;
|
|
168
|
-
this.message = Buffer.from("", "binary");
|
|
169
170
|
this.TIMEOUT = timeout || 600;
|
|
170
171
|
this.logger = defaultLogger || console.log;
|
|
171
172
|
this.TIMEOUTS = {};
|
|
172
173
|
this.openEvents = {};
|
|
174
|
+
this.timeoutAck =
|
|
175
|
+
typeof timeoutAck === "string" && timeoutAck.length === 2
|
|
176
|
+
? timeoutAck.toUpperCase()
|
|
177
|
+
: "AA";
|
|
173
178
|
this.charset =
|
|
174
179
|
defaultCharset !== undefined ? `${defaultCharset}` : "UNICODE UTF-8";
|
|
175
180
|
this.connectionEventState = {
|
|
@@ -181,6 +186,7 @@ export class MLLPServer extends EventEmitter {
|
|
|
181
186
|
|
|
182
187
|
try {
|
|
183
188
|
this.Server = net.createServer((sock) => {
|
|
189
|
+
let message: Buffer = Buffer.from("", "binary");
|
|
184
190
|
this.logger(`CONNECTED: ${sock.remoteAddress}:${sock.remotePort}`);
|
|
185
191
|
this.addSocket(sock);
|
|
186
192
|
|
|
@@ -191,88 +197,26 @@ export class MLLPServer extends EventEmitter {
|
|
|
191
197
|
this.removeSocket(sock);
|
|
192
198
|
});
|
|
193
199
|
|
|
194
|
-
const handleIncomingMessage = (messageBuffer: Buffer) => {
|
|
195
|
-
let messageString = messageBuffer.toString();
|
|
196
|
-
let data2 = new Message(messageString);
|
|
197
|
-
let msgId: string = data2.getString("MSH-10");
|
|
198
|
-
let encoding = data2.getString("MSH-18");
|
|
199
|
-
if (encoding === undefined || encoding === null || encoding === "") {
|
|
200
|
-
// use Default:
|
|
201
|
-
encoding = this.charset;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (encoding !== "UNICODE UTF-8") {
|
|
205
|
-
// Decoding needed:
|
|
206
|
-
messageString = decoder(messageBuffer, encoding);
|
|
207
|
-
data2 = new Message(messageString);
|
|
208
|
-
msgId = data2.getString("MSH-10");
|
|
209
|
-
}
|
|
210
|
-
this.logger(
|
|
211
|
-
`Message:\r\n${messageString.replace(/\r/g, "\n")}\r\n\r\n`
|
|
212
|
-
);
|
|
213
|
-
|
|
214
|
-
const event: IncomingMessageEvent = {
|
|
215
|
-
id: msgId,
|
|
216
|
-
ack: "AA",
|
|
217
|
-
msg: messageString,
|
|
218
|
-
hl7: data2,
|
|
219
|
-
buffer: messageBuffer,
|
|
220
|
-
};
|
|
221
|
-
|
|
222
|
-
if (this.openEvents[msgId] === undefined) {
|
|
223
|
-
// Using a Timeout if no response has been sended within the timeout.
|
|
224
|
-
this.TIMEOUTS[msgId] = setTimeout(() => {
|
|
225
|
-
this.handleAck(event, "timeout");
|
|
226
|
-
}, this.TIMEOUT);
|
|
227
|
-
|
|
228
|
-
this.openEvents[msgId] = { sock, org: this.message }; // save socket and Message for Response
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* MLLP HL7 Event. Fired when a HL7 Message is received.
|
|
232
|
-
* @event MLLPServer#hl7
|
|
233
|
-
* @type {string}
|
|
234
|
-
* @property {object} Event with the message string (msg), the Msg-ID, the default ACK and the parsed HL7 Object
|
|
235
|
-
*/
|
|
236
|
-
this.emit("hl7", event);
|
|
237
|
-
} else {
|
|
238
|
-
// The Message ID is currently already in Progress... send a direkt REJECT-Message
|
|
239
|
-
const ack = Message.createResponse(
|
|
240
|
-
event.hl7,
|
|
241
|
-
"AR",
|
|
242
|
-
"Message already in progress"
|
|
243
|
-
).render();
|
|
244
|
-
sock.write(VT + ack + FS + CR);
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
200
|
/*
|
|
249
201
|
* handling incoming Data. Currently only one message per Connection is supported.
|
|
250
202
|
*/
|
|
251
203
|
sock.on("data", (data) => {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
while (
|
|
255
|
-
let subBuffer =
|
|
256
|
-
|
|
257
|
-
this.message.indexOf(FS + CR)
|
|
258
|
-
);
|
|
259
|
-
this.message = this.message.subarray(
|
|
260
|
-
this.message.indexOf(FS + CR) + 2
|
|
261
|
-
);
|
|
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);
|
|
262
209
|
if (subBuffer.indexOf(VTi) > -1) {
|
|
263
210
|
subBuffer = subBuffer.subarray(subBuffer.indexOf(VTi) + 1);
|
|
264
211
|
}
|
|
265
|
-
handleIncomingMessage(subBuffer);
|
|
212
|
+
this.handleIncomingMessage(subBuffer, sock);
|
|
266
213
|
}
|
|
267
214
|
|
|
268
|
-
if (
|
|
215
|
+
if (message.indexOf(VTi) > 0) {
|
|
269
216
|
// got a new Message indicator - but there is something before that message - handle as (not proper closed) message:
|
|
270
|
-
const unwrappedBuffer =
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
);
|
|
274
|
-
this.message = this.message.subarray(this.message.indexOf(VTi) + 1);
|
|
275
|
-
handleIncomingMessage(unwrappedBuffer);
|
|
217
|
+
const unwrappedBuffer = message.subarray(0, message.indexOf(VTi));
|
|
218
|
+
message = message.subarray(message.indexOf(VTi) + 1);
|
|
219
|
+
this.handleIncomingMessage(unwrappedBuffer, sock);
|
|
276
220
|
}
|
|
277
221
|
});
|
|
278
222
|
|
|
@@ -318,6 +262,60 @@ export class MLLPServer extends EventEmitter {
|
|
|
318
262
|
}
|
|
319
263
|
}
|
|
320
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
|
+
|
|
321
319
|
private updateState(): MLLPConnectionState {
|
|
322
320
|
this.connectionEventState.connected = this.openConnections.length > 0;
|
|
323
321
|
let info = "";
|
|
@@ -357,7 +355,9 @@ export class MLLPServer extends EventEmitter {
|
|
|
357
355
|
}
|
|
358
356
|
|
|
359
357
|
public static createResponseHeader(data: Message | string | object): string {
|
|
360
|
-
|
|
358
|
+
const message = Message.createResponse(data);
|
|
359
|
+
message.cleanup();
|
|
360
|
+
return message.render();
|
|
361
361
|
}
|
|
362
362
|
|
|
363
363
|
private handleAck(event: IncomingMessageEvent, mode: any): void {
|
|
@@ -380,11 +380,15 @@ export class MLLPServer extends EventEmitter {
|
|
|
380
380
|
event.ack.length === 2 &&
|
|
381
381
|
event.hl7
|
|
382
382
|
) {
|
|
383
|
-
|
|
383
|
+
const ackMsg = Message.createResponse(event.hl7, event.ack);
|
|
384
|
+
ackMsg.cleanup();
|
|
385
|
+
ack = ackMsg.render();
|
|
384
386
|
} else if (typeof event.ack === "string") {
|
|
385
387
|
ack = event.ack;
|
|
386
388
|
} else {
|
|
387
|
-
|
|
389
|
+
const ackMsg = Message.createResponse(event.hl7, this.timeoutAck);
|
|
390
|
+
ackMsg.cleanup();
|
|
391
|
+
ack = ackMsg.render();
|
|
388
392
|
}
|
|
389
393
|
inMsg.sock.write(VT + ack + FS + CR);
|
|
390
394
|
} catch (e) {
|