hl7v2-net 1.0.2
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/LICENSE +21 -0
- package/cjs/constants.js +7 -0
- package/cjs/helpers/frame-stream.js +137 -0
- package/cjs/helpers/hl7-exchange-error.js +14 -0
- package/cjs/hl7-client.js +112 -0
- package/cjs/hl7-request.js +10 -0
- package/cjs/hl7-response.js +28 -0
- package/cjs/hl7-router.js +104 -0
- package/cjs/hl7-server.js +241 -0
- package/cjs/hl7-socket.js +154 -0
- package/cjs/index.js +11 -0
- package/cjs/package.json +3 -0
- package/cjs/types.js +2 -0
- package/esm/constants.js +4 -0
- package/esm/helpers/frame-stream.js +133 -0
- package/esm/helpers/hl7-exchange-error.js +10 -0
- package/esm/hl7-client.js +107 -0
- package/esm/hl7-request.js +6 -0
- package/esm/hl7-response.js +24 -0
- package/esm/hl7-router.js +100 -0
- package/esm/hl7-server.js +236 -0
- package/esm/hl7-socket.js +149 -0
- package/esm/index.js +8 -0
- package/esm/package.json +3 -0
- package/esm/types.js +1 -0
- package/package.json +66 -0
- package/types/constants.d.ts +4 -0
- package/types/helpers/frame-stream.d.ts +34 -0
- package/types/helpers/hl7-exchange-error.d.ts +12 -0
- package/types/hl7-client.d.ts +37 -0
- package/types/hl7-request.d.ts +7 -0
- package/types/hl7-response.d.ts +13 -0
- package/types/hl7-router.d.ts +12 -0
- package/types/hl7-server.d.ts +115 -0
- package/types/hl7-socket.d.ts +47 -0
- package/types/index.d.cts +8 -0
- package/types/index.d.ts +8 -0
- package/types/types.d.ts +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Panates
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/cjs/constants.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FrameStream = void 0;
|
|
4
|
+
const node_buffer_1 = require("node:buffer");
|
|
5
|
+
const node_stream_1 = require("node:stream");
|
|
6
|
+
class FrameStream extends node_stream_1.Transform {
|
|
7
|
+
constructor(opts) {
|
|
8
|
+
super({
|
|
9
|
+
...opts,
|
|
10
|
+
objectMode: true,
|
|
11
|
+
transform: undefined,
|
|
12
|
+
});
|
|
13
|
+
this._chunks = [];
|
|
14
|
+
this._bufferSize = 0;
|
|
15
|
+
this._customTransform = opts?.transform;
|
|
16
|
+
this._frameDelayMs = opts?.frameDelayMs || (opts?.frameEnd ? 0 : 500);
|
|
17
|
+
this.maxBufferSize = opts?.maxBufferSize;
|
|
18
|
+
if (opts?.frameStart) {
|
|
19
|
+
this._frameStart = node_buffer_1.Buffer.isBuffer(opts.frameStart)
|
|
20
|
+
? opts.frameStart
|
|
21
|
+
: node_buffer_1.Buffer.from(opts.frameStart);
|
|
22
|
+
}
|
|
23
|
+
if (opts?.frameEnd) {
|
|
24
|
+
this._frameEnd = node_buffer_1.Buffer.isBuffer(opts.frameEnd)
|
|
25
|
+
? opts.frameEnd
|
|
26
|
+
: node_buffer_1.Buffer.from(opts.frameEnd);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
_transform(chunk, _encoding, callback) {
|
|
30
|
+
// Reset flush timer
|
|
31
|
+
if (this._flushTimeout) {
|
|
32
|
+
clearTimeout(this._flushTimeout);
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
// console.log(chunk);
|
|
36
|
+
if (!node_buffer_1.Buffer.isBuffer(chunk))
|
|
37
|
+
chunk = node_buffer_1.Buffer.from(chunk);
|
|
38
|
+
if (this._chunkBuffer) {
|
|
39
|
+
this._assertMaxBuffer(chunk.length);
|
|
40
|
+
chunk = node_buffer_1.Buffer.concat([this._chunkBuffer, chunk]);
|
|
41
|
+
this._chunkBuffer = undefined;
|
|
42
|
+
}
|
|
43
|
+
const frameStart = this._frameStart;
|
|
44
|
+
const frameEnd = this._frameEnd;
|
|
45
|
+
if (frameStart || frameEnd) {
|
|
46
|
+
const stxLen = frameStart?.length || 0;
|
|
47
|
+
const etxLen = frameEnd?.length || 0;
|
|
48
|
+
let stxPos = 0;
|
|
49
|
+
let etxPos = 0;
|
|
50
|
+
while (etxPos < chunk.length) {
|
|
51
|
+
/** Find the next stx position */
|
|
52
|
+
stxPos = etxPos;
|
|
53
|
+
if (frameStart) {
|
|
54
|
+
stxPos = chunk.indexOf(frameStart, etxPos);
|
|
55
|
+
if (stxPos < 0) {
|
|
56
|
+
this._chunkBuffer = chunk.subarray(etxPos);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (stxPos < 0)
|
|
61
|
+
stxPos = 0;
|
|
62
|
+
etxPos = frameEnd
|
|
63
|
+
? /** Find the next etx position if frameEnd defined */
|
|
64
|
+
chunk.indexOf(frameEnd, stxPos + stxLen)
|
|
65
|
+
: frameStart
|
|
66
|
+
? /** Find the next stx if frameStart not defined */
|
|
67
|
+
chunk.indexOf(frameStart, stxPos + stxLen)
|
|
68
|
+
: -1;
|
|
69
|
+
if (etxPos < 0) {
|
|
70
|
+
this._chunkBuffer = chunk.subarray(stxPos);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
etxPos += etxLen;
|
|
74
|
+
this._pushChunk(chunk.subarray(stxPos, etxPos));
|
|
75
|
+
this.flushBuffer();
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this._pushChunk(chunk);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (this._chunks.length && this._frameDelayMs)
|
|
83
|
+
this._flushTimeout = setTimeout(() => this.flushBuffer(), this._frameDelayMs).unref();
|
|
84
|
+
callback();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
_pushChunk(chunk) {
|
|
88
|
+
this._assertMaxBuffer(chunk.length);
|
|
89
|
+
this._chunks.push(chunk);
|
|
90
|
+
this._bufferSize += chunk.length;
|
|
91
|
+
}
|
|
92
|
+
_assertMaxBuffer(newChunkLen = 0) {
|
|
93
|
+
if (this.maxBufferSize &&
|
|
94
|
+
this._bufferSize + newChunkLen + (this._chunkBuffer?.length || 0) >
|
|
95
|
+
this.maxBufferSize) {
|
|
96
|
+
this.emit('error', new Error('Max buffer size exceeded'));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
_flush(callback) {
|
|
100
|
+
if (this._flushTimeout) {
|
|
101
|
+
clearTimeout(this._flushTimeout);
|
|
102
|
+
}
|
|
103
|
+
this.flushBuffer();
|
|
104
|
+
callback();
|
|
105
|
+
}
|
|
106
|
+
reset() {
|
|
107
|
+
this._chunks = [];
|
|
108
|
+
this._chunkBuffer = undefined;
|
|
109
|
+
if (this._flushTimeout) {
|
|
110
|
+
clearTimeout(this._flushTimeout);
|
|
111
|
+
this._flushTimeout = undefined;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
flushBuffer() {
|
|
115
|
+
if (this._chunkBuffer) {
|
|
116
|
+
this._chunks.push(this._chunkBuffer);
|
|
117
|
+
this._chunkBuffer = undefined;
|
|
118
|
+
}
|
|
119
|
+
/* c8 ignore next */
|
|
120
|
+
if (this._chunks.length === 0)
|
|
121
|
+
return;
|
|
122
|
+
const combined = this._chunks.length === 1 ? this._chunks[0] : node_buffer_1.Buffer.concat(this._chunks);
|
|
123
|
+
this._chunks = [];
|
|
124
|
+
this._bufferSize = 0;
|
|
125
|
+
if (this._customTransform) {
|
|
126
|
+
this._customTransform(combined, 'buffer', (err, data) => {
|
|
127
|
+
if (err)
|
|
128
|
+
this.emit('error', err);
|
|
129
|
+
else
|
|
130
|
+
this.push(data);
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.push(combined);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.FrameStream = FrameStream;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HL7ExchangeError = void 0;
|
|
4
|
+
const hl7v2_1 = require("hl7v2");
|
|
5
|
+
class HL7ExchangeError extends hl7v2_1.HL7Error {
|
|
6
|
+
constructor(message, args) {
|
|
7
|
+
super(message, args);
|
|
8
|
+
if (args?.request)
|
|
9
|
+
this.request = args.request;
|
|
10
|
+
if (args?.response)
|
|
11
|
+
this.response = args.response;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.HL7ExchangeError = HL7ExchangeError;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Hl7Client = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const node_net_1 = tslib_1.__importDefault(require("node:net"));
|
|
6
|
+
const node_events_async_1 = require("node-events-async");
|
|
7
|
+
const hl7_request_js_1 = require("./hl7-request.js");
|
|
8
|
+
const hl7_response_js_1 = require("./hl7-response.js");
|
|
9
|
+
const hl7_router_js_1 = require("./hl7-router.js");
|
|
10
|
+
const hl7_socket_js_1 = require("./hl7-socket.js");
|
|
11
|
+
class Hl7Client extends node_events_async_1.AsyncEventEmitter {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super();
|
|
14
|
+
this._router = new hl7_router_js_1.HL7Router();
|
|
15
|
+
this._options = options;
|
|
16
|
+
}
|
|
17
|
+
get connected() {
|
|
18
|
+
return this._socket?.connected ?? false;
|
|
19
|
+
}
|
|
20
|
+
get readyState() {
|
|
21
|
+
return this._socket?.readyState || 'closed';
|
|
22
|
+
}
|
|
23
|
+
get connectTimeout() {
|
|
24
|
+
return this._options.connectTimeout;
|
|
25
|
+
}
|
|
26
|
+
set connectTimeout(value) {
|
|
27
|
+
this._options.connectTimeout = value ?? undefined;
|
|
28
|
+
}
|
|
29
|
+
get responseTimeout() {
|
|
30
|
+
return this._options.responseTimeout;
|
|
31
|
+
}
|
|
32
|
+
set responseTimeout(value) {
|
|
33
|
+
this._options.responseTimeout = value ?? undefined;
|
|
34
|
+
}
|
|
35
|
+
get maxBufferSize() {
|
|
36
|
+
return this._options.maxBufferSize || 0;
|
|
37
|
+
}
|
|
38
|
+
set maxBufferSize(value) {
|
|
39
|
+
this._options.maxBufferSize = value;
|
|
40
|
+
if (this._socket)
|
|
41
|
+
this._socket.maxBufferSize = value;
|
|
42
|
+
}
|
|
43
|
+
connect() {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
if (this.connected) {
|
|
46
|
+
resolve();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
let timeoutTimer;
|
|
50
|
+
const tcpSocket = node_net_1.default.connect(this._options);
|
|
51
|
+
const socket = (this._socket = new hl7_socket_js_1.HL7Socket(tcpSocket, this._options));
|
|
52
|
+
socket.on('connect', () => this.emit('connect'));
|
|
53
|
+
socket.on('ready', () => this.emit('ready'));
|
|
54
|
+
socket.on('lookup', listener => this.emit('lookup', listener));
|
|
55
|
+
socket.on('close', () => {
|
|
56
|
+
this._socket = undefined;
|
|
57
|
+
this.emit('close');
|
|
58
|
+
});
|
|
59
|
+
socket.on('error', err => this.emit('error', err));
|
|
60
|
+
socket.on('message', message => this._onMessage(message));
|
|
61
|
+
const onReady = () => {
|
|
62
|
+
clearTimeout(timeoutTimer);
|
|
63
|
+
tcpSocket.removeListener('error', onError);
|
|
64
|
+
resolve();
|
|
65
|
+
};
|
|
66
|
+
const onError = (error) => {
|
|
67
|
+
clearTimeout(timeoutTimer);
|
|
68
|
+
tcpSocket.removeListener('ready', onReady);
|
|
69
|
+
tcpSocket.destroy();
|
|
70
|
+
reject(error);
|
|
71
|
+
};
|
|
72
|
+
tcpSocket.once('ready', onReady);
|
|
73
|
+
tcpSocket.once('error', onError);
|
|
74
|
+
if (this.connectTimeout) {
|
|
75
|
+
timeoutTimer = setTimeout(() => {
|
|
76
|
+
this.emit('error', new Error('Connection timeout'));
|
|
77
|
+
tcpSocket.destroy();
|
|
78
|
+
}, this._options.connectTimeout).unref();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async close(waitRunningHandlers) {
|
|
83
|
+
await this._socket?.close(waitRunningHandlers);
|
|
84
|
+
}
|
|
85
|
+
async sendMessage(message) {
|
|
86
|
+
if (!this.connected)
|
|
87
|
+
await this.connect();
|
|
88
|
+
this._socket.sendMessage(message);
|
|
89
|
+
}
|
|
90
|
+
async sendMessageWaitAck(request) {
|
|
91
|
+
if (!this.connected)
|
|
92
|
+
await this.connect();
|
|
93
|
+
return this._socket.sendMessageWaitAck(request);
|
|
94
|
+
}
|
|
95
|
+
setKeepAlive(enable, initialDelay) {
|
|
96
|
+
this._options.keepAlive = enable;
|
|
97
|
+
this._options.keepAliveInitialDelay = initialDelay;
|
|
98
|
+
this._socket?.setKeepAlive(enable, initialDelay);
|
|
99
|
+
}
|
|
100
|
+
use(handler, priority = 0) {
|
|
101
|
+
this._router.use(handler, priority);
|
|
102
|
+
}
|
|
103
|
+
_onMessage(message) {
|
|
104
|
+
const req = new hl7_request_js_1.HL7Request(this._socket, message);
|
|
105
|
+
const res = new hl7_response_js_1.HL7Response(req);
|
|
106
|
+
this._router.handle(undefined, req, res, error => {
|
|
107
|
+
if (error)
|
|
108
|
+
this.emit('error', error);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.Hl7Client = Hl7Client;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HL7Response = void 0;
|
|
4
|
+
const node_events_async_1 = require("node-events-async");
|
|
5
|
+
class HL7Response extends node_events_async_1.AsyncEventEmitter {
|
|
6
|
+
constructor(req) {
|
|
7
|
+
super();
|
|
8
|
+
this._finished = false;
|
|
9
|
+
this._req = req;
|
|
10
|
+
}
|
|
11
|
+
get socket() {
|
|
12
|
+
return this._req.socket;
|
|
13
|
+
}
|
|
14
|
+
get request() {
|
|
15
|
+
return this._req;
|
|
16
|
+
}
|
|
17
|
+
get finished() {
|
|
18
|
+
return this._finished || !this.socket.connected;
|
|
19
|
+
}
|
|
20
|
+
send(message) {
|
|
21
|
+
if (this.finished || !this.socket.connected)
|
|
22
|
+
return;
|
|
23
|
+
this.socket.sendMessage(message);
|
|
24
|
+
this._finished = true;
|
|
25
|
+
this.emit('finish', message);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.HL7Response = HL7Response;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HL7Router = void 0;
|
|
4
|
+
class HL7Router {
|
|
5
|
+
constructor() {
|
|
6
|
+
this._allHandlers = {};
|
|
7
|
+
this._handlers = [];
|
|
8
|
+
this._errorHandlers = [];
|
|
9
|
+
}
|
|
10
|
+
use(handler, priority = 0) {
|
|
11
|
+
let list = this._allHandlers[priority];
|
|
12
|
+
if (!list) {
|
|
13
|
+
list = [];
|
|
14
|
+
this._allHandlers[priority] = list;
|
|
15
|
+
}
|
|
16
|
+
if (typeof handler === 'function')
|
|
17
|
+
list.push(handler);
|
|
18
|
+
else {
|
|
19
|
+
// noinspection SuspiciousTypeOfGuard
|
|
20
|
+
if (handler instanceof HL7Router) {
|
|
21
|
+
list.push((req, res, next) => {
|
|
22
|
+
handler.handle(undefined, req, res, error => {
|
|
23
|
+
if (!res.finished)
|
|
24
|
+
next(error);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
list.push((err, req, res, next) => {
|
|
28
|
+
handler.handle(err, req, res, error => {
|
|
29
|
+
if (!res.finished)
|
|
30
|
+
next(error);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
} /* c8 ignore else */
|
|
34
|
+
else {
|
|
35
|
+
throw new TypeError('Router handler must be a function or HL7Router');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
this._needPrepare = true;
|
|
39
|
+
}
|
|
40
|
+
handle(error, req, res, callback) {
|
|
41
|
+
this._prepareStack();
|
|
42
|
+
let errIdx = -1;
|
|
43
|
+
let handlerIdx = -1;
|
|
44
|
+
let lastErr;
|
|
45
|
+
let callbackCalled = false;
|
|
46
|
+
const doCallback = (err) => {
|
|
47
|
+
if (callbackCalled)
|
|
48
|
+
return;
|
|
49
|
+
callbackCalled = true;
|
|
50
|
+
res.removeListener('finish', onFinish);
|
|
51
|
+
callback(err);
|
|
52
|
+
};
|
|
53
|
+
const onFinish = () => doCallback();
|
|
54
|
+
res.once('finish', onFinish);
|
|
55
|
+
const next = (err) => {
|
|
56
|
+
lastErr = err || lastErr;
|
|
57
|
+
if (res.finished) {
|
|
58
|
+
doCallback();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
if (err) {
|
|
63
|
+
errIdx++;
|
|
64
|
+
const handler = this._errorHandlers[errIdx];
|
|
65
|
+
if (!handler) {
|
|
66
|
+
doCallback(lastErr);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
handler(err, req, res, next);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
handlerIdx++;
|
|
74
|
+
const handler = this._handlers[handlerIdx];
|
|
75
|
+
if (!handler) {
|
|
76
|
+
doCallback(lastErr);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
handler(req, res, next);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
next(e);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
next(error);
|
|
88
|
+
}
|
|
89
|
+
_prepareStack() {
|
|
90
|
+
if (!this._needPrepare)
|
|
91
|
+
return;
|
|
92
|
+
delete this._needPrepare;
|
|
93
|
+
this._errorHandlers = [];
|
|
94
|
+
this._handlers = [];
|
|
95
|
+
Object.keys(this._allHandlers).forEach(p => {
|
|
96
|
+
const h = this._allHandlers[p];
|
|
97
|
+
if (h.length === 4)
|
|
98
|
+
this._errorHandlers.push(...h);
|
|
99
|
+
else
|
|
100
|
+
this._handlers.push(...h);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.HL7Router = HL7Router;
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HL7Server = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const node_net_1 = tslib_1.__importDefault(require("node:net"));
|
|
6
|
+
const process = tslib_1.__importStar(require("node:process"));
|
|
7
|
+
const node_tls_1 = tslib_1.__importDefault(require("node:tls"));
|
|
8
|
+
const hl7v2_1 = require("hl7v2");
|
|
9
|
+
const node_events_async_1 = require("node-events-async");
|
|
10
|
+
const hl7_exchange_error_js_1 = require("./helpers/hl7-exchange-error.js");
|
|
11
|
+
const hl7_request_js_1 = require("./hl7-request.js");
|
|
12
|
+
const hl7_response_js_1 = require("./hl7-response.js");
|
|
13
|
+
const hl7_router_js_1 = require("./hl7-router.js");
|
|
14
|
+
const hl7_socket_js_1 = require("./hl7-socket.js");
|
|
15
|
+
class HL7Server extends node_events_async_1.AsyncEventEmitter {
|
|
16
|
+
/**
|
|
17
|
+
* Creates a HL7 TCP server
|
|
18
|
+
* @static
|
|
19
|
+
*/
|
|
20
|
+
static createServer(listenerOptions, options) {
|
|
21
|
+
return new HL7Server(node_net_1.default.createServer(listenerOptions), options);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates a secure HL7 TCP server
|
|
25
|
+
* @static
|
|
26
|
+
*/
|
|
27
|
+
static createTlsServer(listenerOptions, options) {
|
|
28
|
+
return new HL7Server(node_tls_1.default.createServer(listenerOptions), options);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
*
|
|
32
|
+
* @constructor
|
|
33
|
+
* @protected
|
|
34
|
+
*/
|
|
35
|
+
constructor(server, options) {
|
|
36
|
+
super();
|
|
37
|
+
this._sockets = new Set();
|
|
38
|
+
this._router = new hl7_router_js_1.HL7Router();
|
|
39
|
+
this._runningHandlers = new Set();
|
|
40
|
+
this.maxBufferPerSocket = options?.maxBufferPerSocket;
|
|
41
|
+
this.responseTimeout = options?.responseTimeout;
|
|
42
|
+
this._server = server;
|
|
43
|
+
this._server.on('connection', socket => this._connectionListener(socket));
|
|
44
|
+
this._server.on('listening', () => this.emit('listening'));
|
|
45
|
+
this._server.on('close', () => this.emit('close'));
|
|
46
|
+
this._server.on('drop', data => this.emit('drop', data));
|
|
47
|
+
this._server.on('error', error => this.emit('error', error, undefined));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
get listening() {
|
|
53
|
+
return this._server.listening;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
get connections() {
|
|
59
|
+
return this._server.connections;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Set this property to reject connections when the server's connection count gets high.
|
|
63
|
+
* It is not recommended to use this option once a socket has been sent to a child with child_process.fork().
|
|
64
|
+
*/
|
|
65
|
+
get maxConnections() {
|
|
66
|
+
return this._server.maxConnections;
|
|
67
|
+
}
|
|
68
|
+
set maxConnections(value) {
|
|
69
|
+
this._server.maxConnections = value;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Set this property to true to begin closing connections once the number of
|
|
73
|
+
* connections reaches the maxConnections threshold.
|
|
74
|
+
* This setting is only effective in cluster mode.
|
|
75
|
+
*/
|
|
76
|
+
get dropMaxConnection() {
|
|
77
|
+
// Added in: NodeJS v23.1.0, v22.12.0
|
|
78
|
+
return this._server.dropMaxConnection;
|
|
79
|
+
}
|
|
80
|
+
set dropMaxConnection(value) {
|
|
81
|
+
// Added in: NodeJS v23.1.0, v22.12.0
|
|
82
|
+
this._server.dropMaxConnection = value;
|
|
83
|
+
}
|
|
84
|
+
listen(...args) {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
const onListening = () => {
|
|
87
|
+
this._server.removeListener('error', onListening);
|
|
88
|
+
resolve();
|
|
89
|
+
};
|
|
90
|
+
const onError = (error) => {
|
|
91
|
+
this._server.removeListener('listening', onListening);
|
|
92
|
+
reject(error);
|
|
93
|
+
};
|
|
94
|
+
this._server.once('listening', onListening);
|
|
95
|
+
this._server.once('error', onError);
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
this._server.listen.call(this._server, ...args);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns the bound `address`, the address `family` name, and `port` of the server
|
|
102
|
+
* as reported by the operating system if listening on an IP socket
|
|
103
|
+
* (useful to find which port was assigned when getting an OS-assigned address):
|
|
104
|
+
* `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
|
|
105
|
+
*
|
|
106
|
+
* For a server listening on a pipe or Unix domain socket, the name is returned
|
|
107
|
+
* as a string.
|
|
108
|
+
*
|
|
109
|
+
* `server.address()` returns `null` before the `'listening'` event has been
|
|
110
|
+
* emitted or after calling `server.close()`.
|
|
111
|
+
*/
|
|
112
|
+
address() {
|
|
113
|
+
return this._server.address();
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Stops the server from accepting new connections and keeps existing
|
|
117
|
+
* connections.
|
|
118
|
+
*/
|
|
119
|
+
async close(waitRunningHandlers) {
|
|
120
|
+
if (!this.listening)
|
|
121
|
+
return;
|
|
122
|
+
this._closing = true;
|
|
123
|
+
/** Wait for running handlers to finish */
|
|
124
|
+
if (waitRunningHandlers && this._runningHandlers.size > 0) {
|
|
125
|
+
await new Promise(resolve => {
|
|
126
|
+
/** Timeout timer will resolve this promise to stop waiting */
|
|
127
|
+
const timer = setTimeout(() => {
|
|
128
|
+
resolve();
|
|
129
|
+
}, waitRunningHandlers).unref();
|
|
130
|
+
Promise.allSettled(Array.from(this._runningHandlers)).then(() => {
|
|
131
|
+
clearTimeout(timer);
|
|
132
|
+
resolve();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
await Promise.allSettled(Array.from(this._sockets.values()).map(socket => socket.close(waitRunningHandlers)));
|
|
137
|
+
return new Promise(resolve => {
|
|
138
|
+
this._server.close(() => resolve());
|
|
139
|
+
}).then(() => {
|
|
140
|
+
delete this._closing;
|
|
141
|
+
this.emit('close');
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
use(handler, priority = 0) {
|
|
145
|
+
this._router.use(handler, priority);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Asynchronously get the number of concurrent connections on the server. Works
|
|
149
|
+
* when sockets were sent to forks.
|
|
150
|
+
*/
|
|
151
|
+
async getConnections() {
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
try {
|
|
154
|
+
this._server.getConnections((error, count) => {
|
|
155
|
+
if (error)
|
|
156
|
+
reject(error);
|
|
157
|
+
else
|
|
158
|
+
resolve(count);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
reject(error);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Opposite of `unref()`, calling `ref()` on a previously `unref`ed server will _not_ let
|
|
168
|
+
* the program exit if it's the only server left (the default behavior).
|
|
169
|
+
* If the server is `ref`ed calling `ref()` again will have no effect.
|
|
170
|
+
*/
|
|
171
|
+
ref() {
|
|
172
|
+
this._server.ref();
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Calling `unref()` on a server will allow the program to exit if this is the only
|
|
177
|
+
* active server in the event system. If the server is already `unref`ed calling`unref()` again will have no effect.
|
|
178
|
+
*/
|
|
179
|
+
unref() {
|
|
180
|
+
this._server.unref();
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
_connectionListener(tcpSocket) {
|
|
184
|
+
const socket = new hl7_socket_js_1.HL7Socket(tcpSocket, {
|
|
185
|
+
maxBufferSize: this.maxBufferPerSocket,
|
|
186
|
+
});
|
|
187
|
+
this._sockets.add(socket);
|
|
188
|
+
socket.on('message', message => this._onMessage(socket, message));
|
|
189
|
+
socket.on('close', () => {
|
|
190
|
+
this._sockets.delete(socket);
|
|
191
|
+
this.emit('disconnect', socket);
|
|
192
|
+
});
|
|
193
|
+
this.emit('connection', socket);
|
|
194
|
+
}
|
|
195
|
+
_onMessage(socket, message) {
|
|
196
|
+
const req = new hl7_request_js_1.HL7Request(socket, message);
|
|
197
|
+
const res = new hl7_response_js_1.HL7Response(req);
|
|
198
|
+
const waitPromise = new Promise(resolve => {
|
|
199
|
+
const timeoutTimer = setTimeout(() => {
|
|
200
|
+
try {
|
|
201
|
+
const error = new hl7_exchange_error_js_1.HL7ExchangeError('Response timeout');
|
|
202
|
+
const ack = req.message.createAck('AE', error);
|
|
203
|
+
res.send(ack);
|
|
204
|
+
}
|
|
205
|
+
finally {
|
|
206
|
+
resolve();
|
|
207
|
+
}
|
|
208
|
+
}, this.responseTimeout || 30000).unref();
|
|
209
|
+
this._router.handle(undefined, req, res, error => {
|
|
210
|
+
clearTimeout(timeoutTimer);
|
|
211
|
+
try {
|
|
212
|
+
if (!res.finished) {
|
|
213
|
+
error =
|
|
214
|
+
error ||
|
|
215
|
+
new hl7_exchange_error_js_1.HL7ExchangeError('No middleware handled the request', {
|
|
216
|
+
request: req.message,
|
|
217
|
+
});
|
|
218
|
+
const ack = req.message.createAck('AE', error);
|
|
219
|
+
if ((process?.env.NODE_ENV || '').startsWith('dev')) {
|
|
220
|
+
const errSeg = ack.getSegment('ERR');
|
|
221
|
+
if (errSeg)
|
|
222
|
+
errSeg.field(hl7v2_1.ERRSegment.DiagnosticInformation).value =
|
|
223
|
+
error.stack;
|
|
224
|
+
}
|
|
225
|
+
res.send(ack);
|
|
226
|
+
}
|
|
227
|
+
if (error)
|
|
228
|
+
this.emit('error', error, socket);
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
resolve();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
this._runningHandlers.add(waitPromise);
|
|
236
|
+
waitPromise.finally(() => {
|
|
237
|
+
this._runningHandlers.delete(waitPromise);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.HL7Server = HL7Server;
|