mllp-node-sl7 2.1.1
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/.eslintrc.js +52 -0
- package/LICENSE +13 -0
- package/README.md +98 -0
- package/RELEASENOTES.md +20 -0
- package/package.json +58 -0
- package/src/decoder.d.ts +3 -0
- package/src/decoder.js +45 -0
- package/src/decoder.ts +20 -0
- package/src/example.js +54 -0
- package/src/index.d.ts +76 -0
- package/src/index.js +326 -0
- package/src/index.ts +461 -0
- package/test/fixtures/LargeA08.txt +1 -0
- package/test/fixtures/test.txt +1 -0
- package/test/test.js +112 -0
- package/tsconfig.json +103 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import net from "net";
|
|
2
|
+
import EventEmitter from "events";
|
|
3
|
+
import { Message } from "sb-sl7/dist/message";
|
|
4
|
+
import decoder from "./decoder";
|
|
5
|
+
|
|
6
|
+
// The header is a vertical tab character <VT> its hex value is 0x0b.
|
|
7
|
+
// The trailer is a field separator character <FS> (hex 0x1c) immediately followed by a carriage return <CR> (hex 0x0d)
|
|
8
|
+
|
|
9
|
+
const VT = String.fromCharCode(0x0b);
|
|
10
|
+
const VTi = 0x0b;
|
|
11
|
+
const FS = String.fromCharCode(0x1c); // const FSi = 0x1c;
|
|
12
|
+
const CR = String.fromCharCode(0x0d); // const CRi = 0x0d;
|
|
13
|
+
|
|
14
|
+
export interface Renderable {
|
|
15
|
+
render(): string | Buffer;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isARenderable(obj: any): obj is Renderable {
|
|
19
|
+
return (
|
|
20
|
+
typeof obj === "object" &&
|
|
21
|
+
Object.prototype.hasOwnProperty.call(obj, "render") &&
|
|
22
|
+
typeof obj.render === "function"
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getPayload(hl7Data: Buffer | Renderable | string): string | Buffer {
|
|
27
|
+
if (typeof hl7Data === "object" && isARenderable(hl7Data)) {
|
|
28
|
+
return hl7Data.render();
|
|
29
|
+
}
|
|
30
|
+
return hl7Data;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function mllpSendMessage(
|
|
34
|
+
receivingHost: string,
|
|
35
|
+
receivingPort: number,
|
|
36
|
+
hl7Data: Buffer | Renderable | string,
|
|
37
|
+
callback: (err: Error | null, response: string | null) => void,
|
|
38
|
+
logger?: (msg: string) => void
|
|
39
|
+
) {
|
|
40
|
+
// Render Message if it is an object:
|
|
41
|
+
const log = logger || console.log;
|
|
42
|
+
const payload: string | Buffer = getPayload(hl7Data);
|
|
43
|
+
|
|
44
|
+
// Continue with Sending:
|
|
45
|
+
const sendingClient = net.connect(
|
|
46
|
+
{
|
|
47
|
+
host: receivingHost,
|
|
48
|
+
port: receivingPort,
|
|
49
|
+
},
|
|
50
|
+
() => {
|
|
51
|
+
log(`Sending data to ${receivingHost}:${receivingPort}`);
|
|
52
|
+
sendingClient.write(VT, () => {
|
|
53
|
+
sendingClient.write(payload, () => {
|
|
54
|
+
sendingClient.write(FS + CR);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const terminate = () => {
|
|
61
|
+
log(`closing connection with ${receivingHost}:${receivingPort}`);
|
|
62
|
+
sendingClient.end();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
sendingClient.on("data", (rawAckData) => {
|
|
66
|
+
log(`${receivingHost}:${receivingPort} ACKED data`);
|
|
67
|
+
|
|
68
|
+
const ackData = rawAckData
|
|
69
|
+
.toString() // Buffer -> String
|
|
70
|
+
.replace(VT, "")
|
|
71
|
+
.split("\r")[1] // Ack data
|
|
72
|
+
.replace(FS, "")
|
|
73
|
+
.replace(CR, "");
|
|
74
|
+
|
|
75
|
+
callback(null, ackData);
|
|
76
|
+
terminate();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
sendingClient.on("error", (error) => {
|
|
80
|
+
log(`${receivingHost}:${receivingPort} couldn't process data`);
|
|
81
|
+
|
|
82
|
+
callback(error, null);
|
|
83
|
+
terminate();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface MessageResponseEvent {
|
|
88
|
+
id: string;
|
|
89
|
+
ack: string | Renderable | unknown;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface IncomingMessageEvent extends MessageResponseEvent {
|
|
93
|
+
msg: string;
|
|
94
|
+
hl7: Message;
|
|
95
|
+
buffer: Buffer;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface MLLPConnectionState {
|
|
99
|
+
host: string;
|
|
100
|
+
port: number;
|
|
101
|
+
connected: boolean;
|
|
102
|
+
remote: null | string;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface OpenSocket {
|
|
106
|
+
sock: net.Socket;
|
|
107
|
+
org: Buffer;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// noinspection JSUnusedGlobalSymbols
|
|
111
|
+
/**
|
|
112
|
+
* @constructor MLLPServer
|
|
113
|
+
* @param {string} host a resolvable hostname or IP Address
|
|
114
|
+
* @param {number} port a valid free port for the server to listen on.
|
|
115
|
+
* @param defaultLogger
|
|
116
|
+
* @param {number} timeout after which the answer is sended.
|
|
117
|
+
* @param {string} defaultCharset for Message decoding
|
|
118
|
+
*
|
|
119
|
+
* @fires MLLPServer#hl7
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* const server = new MLLPServer('hl7server.mydomain', 3333, console.log);
|
|
123
|
+
*
|
|
124
|
+
* server.on('hl7', function(message) {
|
|
125
|
+
* console.log("Message: " + message);
|
|
126
|
+
* // INSERT Unmarshalling or Processing here
|
|
127
|
+
* });
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* <caption>An ACK is sent back to the server</caption>
|
|
131
|
+
* MSH|^~\&|SOMELAB|SOMELAB|SOMELAB|SOMELAB|20080511103530||ORU^R01|Q335939501T337311002|P|2.3|||
|
|
132
|
+
* MSA|AA|Q335939501T337311002
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
export class MLLPServer extends EventEmitter {
|
|
136
|
+
protected readonly HOST: string;
|
|
137
|
+
|
|
138
|
+
protected readonly PORT: number;
|
|
139
|
+
|
|
140
|
+
protected readonly TIMEOUT: number;
|
|
141
|
+
|
|
142
|
+
protected message: Buffer;
|
|
143
|
+
|
|
144
|
+
protected logger: (msg: string, ...data: unknown[]) => void;
|
|
145
|
+
|
|
146
|
+
protected charset: string;
|
|
147
|
+
|
|
148
|
+
private readonly TIMEOUTS: Record<string, NodeJS.Timeout>;
|
|
149
|
+
|
|
150
|
+
private readonly openEvents: Record<string, OpenSocket>;
|
|
151
|
+
|
|
152
|
+
protected Server: net.Server;
|
|
153
|
+
|
|
154
|
+
protected connectionEventState: MLLPConnectionState;
|
|
155
|
+
|
|
156
|
+
private readonly openConnections: net.Socket[] = [];
|
|
157
|
+
|
|
158
|
+
constructor(
|
|
159
|
+
host: string,
|
|
160
|
+
port: number,
|
|
161
|
+
defaultLogger?: (msg: string) => void,
|
|
162
|
+
timeout?: number,
|
|
163
|
+
defaultCharset?: string
|
|
164
|
+
) {
|
|
165
|
+
super();
|
|
166
|
+
this.HOST = host || "127.0.0.1";
|
|
167
|
+
this.PORT = port || 6969;
|
|
168
|
+
this.message = Buffer.from("", "binary");
|
|
169
|
+
this.TIMEOUT = timeout || 600;
|
|
170
|
+
this.logger = defaultLogger || console.log;
|
|
171
|
+
this.TIMEOUTS = {};
|
|
172
|
+
this.openEvents = {};
|
|
173
|
+
this.charset =
|
|
174
|
+
defaultCharset !== undefined ? `${defaultCharset}` : "UNICODE UTF-8";
|
|
175
|
+
this.connectionEventState = {
|
|
176
|
+
host: this.HOST,
|
|
177
|
+
port: this.PORT,
|
|
178
|
+
connected: false,
|
|
179
|
+
remote: null,
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
this.Server = net.createServer((sock) => {
|
|
184
|
+
this.logger(`CONNECTED: ${sock.remoteAddress}:${sock.remotePort}`);
|
|
185
|
+
this.addSocket(sock);
|
|
186
|
+
|
|
187
|
+
sock.on("end", () => {
|
|
188
|
+
// This should not happen, but if it does, tell everyone who is interested
|
|
189
|
+
this.emit("hl7-closed", this.updateState());
|
|
190
|
+
this.logger("server disconnected", this.HOST, this.PORT);
|
|
191
|
+
this.removeSocket(sock);
|
|
192
|
+
});
|
|
193
|
+
|
|
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
|
+
/*
|
|
249
|
+
* handling incoming Data. Currently only one message per Connection is supported.
|
|
250
|
+
*/
|
|
251
|
+
sock.on("data", (data) => {
|
|
252
|
+
this.message = Buffer.concat([this.message, data]);
|
|
253
|
+
|
|
254
|
+
while (this.message.indexOf(FS + CR) > -1) {
|
|
255
|
+
let subBuffer = this.message.subarray(
|
|
256
|
+
0,
|
|
257
|
+
this.message.indexOf(FS + CR)
|
|
258
|
+
);
|
|
259
|
+
this.message = this.message.subarray(
|
|
260
|
+
this.message.indexOf(FS + CR) + 2
|
|
261
|
+
);
|
|
262
|
+
if (subBuffer.indexOf(VTi) > -1) {
|
|
263
|
+
subBuffer = subBuffer.subarray(subBuffer.indexOf(VTi) + 1);
|
|
264
|
+
}
|
|
265
|
+
handleIncomingMessage(subBuffer);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (this.message.indexOf(VTi) > 0) {
|
|
269
|
+
// got a new Message indicator - but there is something before that message - handle as (not proper closed) message:
|
|
270
|
+
const unwrappedBuffer = this.message.subarray(
|
|
271
|
+
0,
|
|
272
|
+
this.message.indexOf(VTi)
|
|
273
|
+
);
|
|
274
|
+
this.message = this.message.subarray(this.message.indexOf(VTi) + 1);
|
|
275
|
+
handleIncomingMessage(unwrappedBuffer);
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// emit incoming errors on the Sock to the outside world
|
|
280
|
+
sock.on("error", (err) => {
|
|
281
|
+
this.emit("hl7-error", err);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
sock.on("close", () => {
|
|
285
|
+
this.logger(`CLOSED: ${sock.remoteAddress} ${sock.remotePort}`);
|
|
286
|
+
// Tell the outside world out connection state:
|
|
287
|
+
this.removeSocket(sock);
|
|
288
|
+
this.emit("hl7-disconnected", this.updateState());
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
if (this.HOST !== "0.0.0.0") {
|
|
293
|
+
this.Server.listen(this.PORT, this.HOST, () => {
|
|
294
|
+
this.logger(`Listen now to ${this.HOST}:${this.PORT}`);
|
|
295
|
+
setImmediate(() => {
|
|
296
|
+
this.emit("hl7-ready", this.updateState());
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
} else {
|
|
300
|
+
this.Server.listen(this.PORT, () => {
|
|
301
|
+
this.logger(`Listen now to [any]:${this.PORT}`);
|
|
302
|
+
setImmediate(() => {
|
|
303
|
+
this.emit("hl7-ready", this.updateState());
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
this.Server.on("close", () => {
|
|
309
|
+
this.emit("hl7-closed", { port: this.PORT, host: this.HOST });
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
this.Server.on("error", (err) => {
|
|
313
|
+
this.logger(`Error during MLLP Connection`, err);
|
|
314
|
+
});
|
|
315
|
+
} catch (e) {
|
|
316
|
+
this.logger(`Error Listen to ${this.HOST}:${this.PORT}`, e);
|
|
317
|
+
throw new Error(`Error Listen to ${this.HOST}:${this.PORT}`);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private updateState(): MLLPConnectionState {
|
|
322
|
+
this.connectionEventState.connected = this.openConnections.length > 0;
|
|
323
|
+
let info = "";
|
|
324
|
+
for (let i = 0; i < this.openConnections.length; i += 1) {
|
|
325
|
+
const openConnection = this.openConnections[i];
|
|
326
|
+
info = `${info}${info.length > 0 ? ", " : ""}${
|
|
327
|
+
openConnection.remoteAddress
|
|
328
|
+
}:${openConnection.remotePort}`;
|
|
329
|
+
}
|
|
330
|
+
this.connectionEventState.remote = info;
|
|
331
|
+
return this.connectionEventState;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private addSocket(sock: net.Socket): void {
|
|
335
|
+
this.openConnections.push(sock);
|
|
336
|
+
this.emit("hl7-connected", this.updateState());
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
private removeSocket(sock: net.Socket): void {
|
|
340
|
+
const idx = this.openConnections.indexOf(sock);
|
|
341
|
+
if (idx >= 0) {
|
|
342
|
+
this.openConnections.splice(idx, 1);
|
|
343
|
+
this.emit("hl7-state", this.updateState());
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
public port(): number {
|
|
348
|
+
return this.PORT;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
public isConnected(): boolean {
|
|
352
|
+
return this.connectionEventState.connected;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
public currentRemote(): string | null {
|
|
356
|
+
return this.connectionEventState.remote;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
public static createResponseHeader(data: Message | string | object): string {
|
|
360
|
+
return Message.createResponse(data).render();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private handleAck(event: IncomingMessageEvent, mode: any): void {
|
|
364
|
+
if (this.openEvents[event.id] !== undefined) {
|
|
365
|
+
const inMsg = this.openEvents[event.id];
|
|
366
|
+
delete this.openEvents[event.id];
|
|
367
|
+
|
|
368
|
+
// prevent Timeout:
|
|
369
|
+
if (this.TIMEOUTS[event.id] !== undefined) {
|
|
370
|
+
clearTimeout(this.TIMEOUTS[event.id]);
|
|
371
|
+
delete this.TIMEOUTS[event.id];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
try {
|
|
375
|
+
let ack;
|
|
376
|
+
if (isARenderable(event.ack)) {
|
|
377
|
+
ack = event.ack.render();
|
|
378
|
+
} else if (
|
|
379
|
+
typeof event.ack === "string" &&
|
|
380
|
+
event.ack.length === 2 &&
|
|
381
|
+
event.hl7
|
|
382
|
+
) {
|
|
383
|
+
ack = Message.createResponse(event.hl7, event.ack).render();
|
|
384
|
+
} else if (typeof event.ack === "string") {
|
|
385
|
+
ack = event.ack;
|
|
386
|
+
} else {
|
|
387
|
+
ack = Message.createResponse(event.hl7, "AA").render();
|
|
388
|
+
}
|
|
389
|
+
inMsg.sock.write(VT + ack + FS + CR);
|
|
390
|
+
} catch (e) {
|
|
391
|
+
this.logger(`Error sending response in mode ${mode}`, e);
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
this.logger(
|
|
395
|
+
`Response already send! Cannot process another response in mode ${mode}`,
|
|
396
|
+
event
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
public response(event: MessageResponseEvent) {
|
|
402
|
+
if (this.openEvents[event.id] !== undefined) {
|
|
403
|
+
const inMsg = this.openEvents[event.id];
|
|
404
|
+
this.handleAck(
|
|
405
|
+
{
|
|
406
|
+
hl7: new Message(inMsg.org),
|
|
407
|
+
msg: inMsg.org.toString(),
|
|
408
|
+
buffer: inMsg.org,
|
|
409
|
+
...event,
|
|
410
|
+
},
|
|
411
|
+
"direct"
|
|
412
|
+
);
|
|
413
|
+
} else {
|
|
414
|
+
this.logger(
|
|
415
|
+
`Response already send! Cannot process another response directly.`,
|
|
416
|
+
event
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
public sendResponse(msgId: string, ack: string) {
|
|
422
|
+
this.response({ id: msgId, ack });
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
public send(
|
|
426
|
+
receivingHost: string,
|
|
427
|
+
receivingPort: number,
|
|
428
|
+
hl7Data: Buffer | Renderable | string,
|
|
429
|
+
callback: (err: Error | null, response: string | null) => void
|
|
430
|
+
) {
|
|
431
|
+
mllpSendMessage(
|
|
432
|
+
receivingHost,
|
|
433
|
+
receivingPort,
|
|
434
|
+
hl7Data,
|
|
435
|
+
callback,
|
|
436
|
+
this.logger
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
public close(done?: (err?: Error) => void) {
|
|
441
|
+
// Do not accept new Connections:
|
|
442
|
+
this.Server.close(done);
|
|
443
|
+
// close all open Connections:
|
|
444
|
+
this.openConnections.forEach(MLLPServer.closeSocket);
|
|
445
|
+
this.openConnections.length = 0;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private static closeSocket(socket: net.Socket): void {
|
|
449
|
+
let timeout: NodeJS.Timeout | undefined = setTimeout(() => {
|
|
450
|
+
socket.destroy();
|
|
451
|
+
timeout = undefined;
|
|
452
|
+
}, 2000);
|
|
453
|
+
socket.end(() => {
|
|
454
|
+
// Ended
|
|
455
|
+
if (timeout !== undefined) {
|
|
456
|
+
clearTimeout(timeout);
|
|
457
|
+
timeout = undefined;
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
MSH|^~\&|IPM|SITE1|SYSTEM|SITE1|20180921090546||ADT^A08|566320014|P|2.4|||AL|NE|||
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
MSH|^~\&|XXXX|C|SOMELAB|SOMELAB|20080511103530||ORU^R01|Q335939501T337311002|P|2.3|||
|
package/test/test.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var assert = require('chai').assert;
|
|
2
|
+
var fs = require('fs');
|
|
3
|
+
var mllp = require('../src/index.js');
|
|
4
|
+
var net = require('net');
|
|
5
|
+
|
|
6
|
+
var VT = String.fromCharCode(0x0b);
|
|
7
|
+
var FS = String.fromCharCode(0x1c);
|
|
8
|
+
var CR = String.fromCharCode(0x0d);
|
|
9
|
+
|
|
10
|
+
describe('test server with client data exchange', function () {
|
|
11
|
+
var hl7 = '';
|
|
12
|
+
var server;
|
|
13
|
+
|
|
14
|
+
before(function () {
|
|
15
|
+
hl7 = fs.readFileSync('./test/fixtures/test.txt').toString().split('\n').join('\r');
|
|
16
|
+
|
|
17
|
+
server = new mllp.MLLPServer('127.0.0.1', 1234);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('sending and receiving HL7 messages', function () {
|
|
21
|
+
var error;
|
|
22
|
+
var data;
|
|
23
|
+
|
|
24
|
+
// Sending
|
|
25
|
+
beforeEach(function (done) {
|
|
26
|
+
server.send('127.0.0.1', 1234, hl7, function (err, ackData) {
|
|
27
|
+
error = err;
|
|
28
|
+
data = ackData;
|
|
29
|
+
done();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Receiving
|
|
34
|
+
it('receives an HL7 ACK message without error', function () {
|
|
35
|
+
assert.equal(error, null);
|
|
36
|
+
assert.equal(data, 'MSA|AA|Q335939501T337311002');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('receives an HL7 message', function () {
|
|
40
|
+
server.on('hl7', function (data) {
|
|
41
|
+
assert.equal(hl7, data);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('sending HL7 message that errors', function () {
|
|
47
|
+
var error;
|
|
48
|
+
var data;
|
|
49
|
+
|
|
50
|
+
// Sending
|
|
51
|
+
beforeEach(function (done) {
|
|
52
|
+
// port 9999 is bogus
|
|
53
|
+
server.send('127.0.0.1', 9999, hl7, function (err, ackData) {
|
|
54
|
+
error = err;
|
|
55
|
+
data = ackData;
|
|
56
|
+
done();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Receiving
|
|
61
|
+
it('receives an error response', function () {
|
|
62
|
+
assert.isNotNull(error);
|
|
63
|
+
assert.equal(data, null);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('not passing in specific host and port', function () {
|
|
68
|
+
beforeEach(function () {
|
|
69
|
+
server = new mllp.MLLPServer();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('uses sane defaults', function (done) {
|
|
73
|
+
new net.connect({
|
|
74
|
+
// defaults that the library uses
|
|
75
|
+
host: '127.0.0.1',
|
|
76
|
+
port: 6969
|
|
77
|
+
}, function () {
|
|
78
|
+
// no need for assertion... this is the success callback
|
|
79
|
+
// if it doesn't get invoked, the test will not pass.
|
|
80
|
+
done();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe("sends a large message for data exchange", function () {
|
|
87
|
+
var hl7Message = '';
|
|
88
|
+
|
|
89
|
+
before(function () {
|
|
90
|
+
hl7Message = fs.readFileSync('./test/fixtures/LargeA08.txt').toString().split('\n').join('\r');
|
|
91
|
+
|
|
92
|
+
server = new mllp.MLLPServer('127.0.0.1', 1235);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("sending a large A08 Message and Receiving an Ack Back", function () {
|
|
96
|
+
var ack, error;
|
|
97
|
+
|
|
98
|
+
beforeEach(function (done) {
|
|
99
|
+
server.send("127.0.0.1", 1235, hl7Message, function (err, ackData) {
|
|
100
|
+
error = err;
|
|
101
|
+
ack = ackData;
|
|
102
|
+
done();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("receives a HL7 Message", function () {
|
|
107
|
+
server.on('hl7', function (data) {
|
|
108
|
+
assert.equal(hl7Message, data.msg);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs", /* Specify what module code is generated. */
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
36
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
37
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
38
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
39
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
40
|
+
|
|
41
|
+
/* JavaScript Support */
|
|
42
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
43
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
44
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
45
|
+
|
|
46
|
+
/* Emit */
|
|
47
|
+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
48
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
49
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
50
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
51
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
52
|
+
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
|
53
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
54
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
55
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
56
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
57
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
58
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
59
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
60
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
61
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
62
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
63
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
64
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
65
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
66
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
67
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
68
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
69
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
70
|
+
|
|
71
|
+
/* Interop Constraints */
|
|
72
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
73
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
74
|
+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
|
75
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
76
|
+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
|
77
|
+
|
|
78
|
+
/* Type Checking */
|
|
79
|
+
"strict": true, /* Enable all strict type-checking options. */
|
|
80
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
81
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
82
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
83
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
84
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
85
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
86
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
87
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
88
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
89
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
90
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
91
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
92
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
93
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
94
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
95
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
96
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
97
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
98
|
+
|
|
99
|
+
/* Completeness */
|
|
100
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
101
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
102
|
+
}
|
|
103
|
+
}
|