fastify-hl7 1.0.0-beta.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.
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
+ };
27
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
+ return new (P || (P = Promise))(function (resolve, reject) {
30
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
34
+ });
35
+ };
36
+ var __generator = (this && this.__generator) || function (thisArg, body) {
37
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
38
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
+ function verb(n) { return function (v) { return step([n, v]); }; }
40
+ function step(op) {
41
+ if (f) throw new TypeError("Generator is already executing.");
42
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
43
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
44
+ if (y = 0, t) op = [op[0] & 2, t.value];
45
+ switch (op[0]) {
46
+ case 0: case 1: t = op; break;
47
+ case 4: _.label++; return { value: op[1], done: false };
48
+ case 5: _.label++; y = op[1]; op = [0]; continue;
49
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
+ default:
51
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
+ if (t[2]) _.ops.pop();
56
+ _.trys.pop(); continue;
57
+ }
58
+ op = body.call(thisArg, _);
59
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
61
+ }
62
+ };
63
+ var __importDefault = (this && this.__importDefault) || function (mod) {
64
+ return (mod && mod.__esModule) ? mod : { "default": mod };
65
+ };
66
+ Object.defineProperty(exports, "__esModule", { value: true });
67
+ var fastify_plugin_1 = __importDefault(require("fastify-plugin"));
68
+ var node_hl7_server_1 = __importDefault(require("node-hl7-server"));
69
+ var hL7Server_js_1 = require("./class/hL7Server.js");
70
+ var hL7Client_js_1 = require("./class/hL7Client.js");
71
+ var errors_js_1 = require("./errors.js");
72
+ var validation_js_1 = require("./validation.js");
73
+ __exportStar(require("./types.js"), exports);
74
+ /**
75
+ * @since 1.0.0
76
+ * @param fastify
77
+ * @param _opts
78
+ * @param connection
79
+ */
80
+ var decorateFastifyInstance = function (fastify, _opts, connection) {
81
+ if (typeof fastify.hl7 !== 'undefined') {
82
+ throw new errors_js_1.errors.FASTIFY_HL7_ERR_SETUP_ERRORS('Already registered.');
83
+ }
84
+ if (typeof fastify.hl7 === 'undefined') {
85
+ fastify.log.trace('[fastify-hl7] Decorate Fastify');
86
+ fastify.decorate('hl7', connection);
87
+ }
88
+ };
89
+ var fastifyHL7 = (0, fastify_plugin_1.default)(function (fastify, opts) { return __awaiter(void 0, void 0, void 0, function () {
90
+ var generatedOpts, serverInstance, server, client;
91
+ return __generator(this, function (_a) {
92
+ switch (_a.label) {
93
+ case 0: return [4 /*yield*/, (0, validation_js_1.validateOpts)(opts)];
94
+ case 1:
95
+ generatedOpts = _a.sent();
96
+ opts = __assign(__assign({}, opts), generatedOpts);
97
+ serverInstance = (typeof opts.enableServer !== 'undefined' && opts.enableServer) ? new node_hl7_server_1.default(opts.serverOptions) : undefined;
98
+ if (typeof serverInstance !== 'undefined') {
99
+ // Server Functions
100
+ server = new hL7Server_js_1.HL7Server(serverInstance);
101
+ // before we close fastify, make sure all server instances are closed
102
+ fastify.addHook('preClose', function () { return __awaiter(void 0, void 0, void 0, function () {
103
+ return __generator(this, function (_a) {
104
+ switch (_a.label) {
105
+ case 0:
106
+ if (!(typeof server !== 'undefined')) return [3 /*break*/, 2];
107
+ return [4 /*yield*/, server.closeAll()];
108
+ case 1:
109
+ _a.sent();
110
+ _a.label = 2;
111
+ case 2: return [2 /*return*/];
112
+ }
113
+ });
114
+ }); });
115
+ }
116
+ client = new hL7Client_js_1.HL7Client();
117
+ // run these before fastify closes
118
+ fastify.addHook('preClose', function () { return __awaiter(void 0, void 0, void 0, function () {
119
+ return __generator(this, function (_a) {
120
+ switch (_a.label) {
121
+ case 0:
122
+ if (!(typeof client !== 'undefined')) return [3 /*break*/, 2];
123
+ return [4 /*yield*/, client.closeAll()];
124
+ case 1:
125
+ _a.sent();
126
+ _a.label = 2;
127
+ case 2: return [2 /*return*/];
128
+ }
129
+ });
130
+ }); });
131
+ decorateFastifyInstance(fastify, opts, {
132
+ _serverInstance: serverInstance,
133
+ buildBatch: function (props) {
134
+ return client.buildBatch(props);
135
+ },
136
+ buildFileBatch: function (props) {
137
+ return client.buildFileBatch(props);
138
+ },
139
+ buildMessage: function (props) {
140
+ return client.buildMessage(props);
141
+ },
142
+ closeServer: function (port) {
143
+ return __awaiter(this, void 0, void 0, function () {
144
+ return __generator(this, function (_a) {
145
+ switch (_a.label) {
146
+ case 0:
147
+ if (!(typeof server !== 'undefined')) return [3 /*break*/, 2];
148
+ return [4 /*yield*/, server.close(port)];
149
+ case 1: return [2 /*return*/, _a.sent()];
150
+ case 2: throw new errors_js_1.errors.FASTIFY_HL7_ERR_USAGE('server was not started. re-register plugin with enableServer set to true.');
151
+ }
152
+ });
153
+ });
154
+ },
155
+ closeServerAll: function () { return __awaiter(void 0, void 0, void 0, function () {
156
+ return __generator(this, function (_a) {
157
+ switch (_a.label) {
158
+ case 0:
159
+ if (!(typeof server !== 'undefined')) return [3 /*break*/, 2];
160
+ return [4 /*yield*/, server.closeAll()];
161
+ case 1: return [2 /*return*/, _a.sent()];
162
+ case 2: throw new errors_js_1.errors.FASTIFY_HL7_ERR_USAGE('server was not started. re-register plugin with enableServer set to true.');
163
+ }
164
+ });
165
+ }); },
166
+ createClient: function (name, props) {
167
+ client.createClient(name, props);
168
+ },
169
+ createInbound: function (name, props, handler) {
170
+ if (typeof server !== 'undefined') {
171
+ return server.createInbound(name, props, handler);
172
+ }
173
+ throw new errors_js_1.errors.FASTIFY_HL7_ERR_USAGE('server was not started. re-register plugin with enableServer set to true.');
174
+ },
175
+ createOutbound: function (name, props, handler) {
176
+ return client.createOutbound(name, props, handler);
177
+ },
178
+ processHL7: function (text) {
179
+ return client.processHL7(text);
180
+ },
181
+ readFile: function (fullFilePath) {
182
+ return client.readFile(fullFilePath);
183
+ },
184
+ readFileBuffer: function (fileBuffer) {
185
+ return client.readFileBuffer(fileBuffer);
186
+ }
187
+ });
188
+ return [2 /*return*/];
189
+ }
190
+ });
191
+ }); });
192
+ exports.default = fastifyHL7;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.validateOpts = void 0;
40
+ /**
41
+ * @since 1.0.0
42
+ * @param opts
43
+ */
44
+ var validateOpts = function (opts) { return __awaiter(void 0, void 0, void 0, function () {
45
+ return __generator(this, function (_a) {
46
+ // Mandatory, Defaulted
47
+ if (typeof opts.enableServer === 'undefined') {
48
+ opts.enableServer = true;
49
+ }
50
+ return [2 /*return*/, opts];
51
+ });
52
+ }); };
53
+ exports.validateOpts = validateOpts;
package/lib/esm/api.js ADDED
@@ -0,0 +1,5 @@
1
+ import fastifyHL7 from '.';
2
+ import { HL7Client } from './class/hL7Client.js';
3
+ import { HL7Server } from './class/hL7Server.js';
4
+ export default fastifyHL7;
5
+ export { fastifyHL7, HL7Client, HL7Server };
@@ -0,0 +1,159 @@
1
+ import Client, { Batch, FileBatch, HL7Outbound, isBatch, Message } from 'node-hl7-client';
2
+ import { errors } from '../errors';
3
+ export class HL7Client {
4
+ /** @internal */
5
+ _clientConnections;
6
+ constructor() {
7
+ this._clientConnections = [];
8
+ }
9
+ /**
10
+ * Close All Connections for this Client
11
+ * @since 1.0.0
12
+ */
13
+ async closeAll() {
14
+ this._clientConnections.forEach(outbound => {
15
+ outbound.ports.map(async (port) => {
16
+ await port.connection.close();
17
+ });
18
+ });
19
+ return true;
20
+ }
21
+ /**
22
+ * Build a HL7 Batch
23
+ * @description Create a properly formatted HL7 Batch.
24
+ * @since 1.0.0
25
+ * @param props
26
+ */
27
+ buildBatch(props) {
28
+ if (typeof props !== 'undefined' && typeof props.text !== 'undefined') {
29
+ throw new errors.FASTIFY_HL7_ERR_USAGE('Use processMessage method. This is for building.');
30
+ }
31
+ return new Batch({ ...props });
32
+ }
33
+ /**
34
+ * Build a HL7 File Batch
35
+ * @description Create a properly formatted HL7 File Batch.
36
+ * @since 1.0.0
37
+ * @param props
38
+ */
39
+ buildFileBatch(props) {
40
+ if (typeof props !== 'undefined' && (typeof props.fullFilePath !== 'undefined' || typeof props.fileBuffer !== 'undefined')) {
41
+ throw new errors.FASTIFY_HL7_ERR_USAGE('Use readFile or readFileBuffer method. This is for building.');
42
+ }
43
+ return new FileBatch({ ...props });
44
+ }
45
+ /**
46
+ * Build a HL7 Message
47
+ * @description Create a properly formatted HL7 message.
48
+ * @since 1.0.0
49
+ * @param props
50
+ */
51
+ buildMessage(props) {
52
+ if (typeof props !== 'undefined' && typeof props.text !== 'undefined') {
53
+ throw new errors.FASTIFY_HL7_ERR_USAGE('Use processMessage method. This is for building.');
54
+ }
55
+ return new Message({ ...props });
56
+ }
57
+ /**
58
+ *
59
+ * @param name
60
+ * @param props
61
+ */
62
+ createClient(name, props) {
63
+ const nameFormat = /[ `!@#$%^&*()+\-=\[\]{};':"\\|,.<>\/?~]/; //eslint-disable-line
64
+ if (nameFormat.test(name)) {
65
+ throw new errors.FASTIFY_HL7_ERR_USAGE('name must not contain certain characters: `!@#$%^&*()+\\-=\\[\\]{};\':"\\\\|,.<>\\/?~.');
66
+ }
67
+ // make sure that this does not exist already...
68
+ this._clientConnections.forEach((connections) => {
69
+ if (connections.name === name) {
70
+ throw new errors.FASTIFY_HL7_ERR_USAGE('name must be unique.');
71
+ }
72
+ // nto in the Client class yet
73
+ // if (client.getHost() === props.host) {
74
+ // throw new errors.FASTIFY_HL7_ERR_USAGE(`host is already a pointer. Name is: ${connections.name}`)
75
+ // }
76
+ });
77
+ // new client
78
+ const client = new Client(props);
79
+ // add it to the collection
80
+ this._clientConnections.push({
81
+ name,
82
+ client,
83
+ ports: []
84
+ });
85
+ }
86
+ /**
87
+ * Create an HL7 Outbound Connection
88
+ * @description Connect to an HL7 Server/Broker
89
+ * @since 1.0.0
90
+ * @param name The name stored within created client connections.
91
+ * @param props
92
+ * @param handler
93
+ */
94
+ createOutbound(name, props, handler) {
95
+ const nameFormat = /[ `!@#$%^&*()+\-=\[\]{};':"\\|,.<>\/?~]/; //eslint-disable-line
96
+ if (nameFormat.test(name)) {
97
+ throw new errors.FASTIFY_HL7_ERR_USAGE('name must not contain certain characters: `!@#$%^&*()+\\-=\\[\\]{};\':"\\\\|,.<>\\/?~.');
98
+ }
99
+ const getConnection = this._clientConnections.find(client => client.name === name);
100
+ if (typeof getConnection !== 'undefined') {
101
+ // make sure port is not used all ready
102
+ getConnection.ports.forEach((outbound) => {
103
+ if (outbound.port === props.port.toString()) {
104
+ throw new errors.FASTIFY_HL7_ERR_USAGE(`port ${props.port} is already used with this client. Choose a new outgoing port.`);
105
+ }
106
+ });
107
+ // create outbound port to the server in getConnection.client
108
+ const outbound = new HL7Outbound(getConnection.client, props, handler);
109
+ // add it to the array of known ports. need to know this, so we can get it later if needed.
110
+ getConnection.ports.push({ port: props.port.toString(), connection: outbound });
111
+ // return it right away. the user might do something with it.
112
+ return outbound;
113
+ }
114
+ throw new errors.FASTIFY_HL7_ERR_USAGE('No valid client. Improper setup of a outbound connection.');
115
+ }
116
+ /**
117
+ * Process a HL7
118
+ * @description A HL7 message that could either be a Message (MSH) or Batch (BHS)
119
+ * @since 1.0.0
120
+ * @param text Raw HL& String
121
+ */
122
+ processHL7(text) {
123
+ if (isBatch(text)) {
124
+ return new Batch({ text });
125
+ }
126
+ else {
127
+ return new Message({ text });
128
+ }
129
+ }
130
+ /**
131
+ * Read File
132
+ * @description Pass the correct path of the file you want to read.
133
+ * @since 1.0.0
134
+ * @param fullFilePath
135
+ * @returns FileBatch
136
+ * @example
137
+ *
138
+ * fastify.hl7.readFile( path.join('temp/', 'hl7.readTestBHS.20231208.hl7') )
139
+ *
140
+ */
141
+ readFile(fullFilePath) {
142
+ return new FileBatch({ fullFilePath });
143
+ }
144
+ /**
145
+ * Read a File Buffer
146
+ * @description Translate an already Buffered HL7 FHS segment to decode it.
147
+ * @since 1.0.0
148
+ * @param fileBuffer
149
+ * @returns FileBatch
150
+ * @example
151
+ *
152
+ * const fileBuffer = fs.readFileSync(path.join('temp/', 'hl7.readTestBHS.20231208.hl7'))
153
+ * fastify.hl7.readFileBuffer(fileBuffer)
154
+ *
155
+ */
156
+ readFileBuffer(fileBuffer) {
157
+ return new FileBatch({ fileBuffer });
158
+ }
159
+ }
@@ -0,0 +1,52 @@
1
+ import { HL7Inbound } from 'node-hl7-server';
2
+ import { errors } from '../errors';
3
+ export class HL7Server {
4
+ _server;
5
+ _serverInboundConnections;
6
+ constructor(server) {
7
+ this._server = server;
8
+ this._serverInboundConnections = [];
9
+ }
10
+ /**
11
+ * Close Inbound connection.
12
+ * @since 1.0.0
13
+ * @param port
14
+ */
15
+ async close(port) {
16
+ const inbound = this._serverInboundConnections.find(server => server.port === port);
17
+ if (typeof inbound !== 'undefined') {
18
+ return await inbound.server.close(); // close the server for all inbound connections
19
+ }
20
+ throw new errors.FASTIFY_HL7_ERR_USAGE(`No inbound server listening on port: ${port}`);
21
+ }
22
+ /**
23
+ * Close all Inbound connections.
24
+ * @since 1.0.0
25
+ */
26
+ async closeAll() {
27
+ this._serverInboundConnections.map(async (inbound) => {
28
+ await inbound.server.close();
29
+ });
30
+ return true;
31
+ }
32
+ /**
33
+ * Create Inbound connection.
34
+ * @since 1.0.0
35
+ * @param name
36
+ * @param props
37
+ * @param handler
38
+ */
39
+ createInbound(name, props, handler) {
40
+ const nameFormat = /[ `!@#$%^&*()+\-=\[\]{};':"\\|,.<>\/?~]/; //eslint-disable-line
41
+ if (nameFormat.test(name)) {
42
+ throw new errors.FASTIFY_HL7_ERR_USAGE('name must not contain certain characters: `!@#$%^&*()+\\-=\\[\\]{};\':"\\\\|,.<>\\/?~.');
43
+ }
44
+ const inbound = new HL7Inbound(this._server, props, handler);
45
+ this._serverInboundConnections.push({
46
+ name,
47
+ port: props.port.toString(),
48
+ server: inbound
49
+ });
50
+ return inbound;
51
+ }
52
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import createError from '@fastify/error';
2
+ export const errors = {
3
+ /** Error if there is an setup error of the plugin itself. */
4
+ FASTIFY_HL7_ERR_SETUP_ERRORS: createError('FASTIFY_HL7_ERR_SETUP_ERRORS', 'Setup error: %s'),
5
+ /** If an invalid usage error was done, this error would pop up. */
6
+ FASTIFY_HL7_ERR_USAGE: createError('FASTIFY_HL7_ERR_USAGE', 'Usage error: %s')
7
+ };
@@ -0,0 +1,99 @@
1
+ import fp from 'fastify-plugin';
2
+ import Server from 'node-hl7-server';
3
+ import { HL7Server } from './class/hL7Server.js';
4
+ import { HL7Client } from './class/hL7Client.js';
5
+ import { errors } from './errors.js';
6
+ import { validateOpts } from './validation.js';
7
+ export * from './types.js';
8
+ /**
9
+ * @since 1.0.0
10
+ * @param fastify
11
+ * @param _opts
12
+ * @param connection
13
+ */
14
+ const decorateFastifyInstance = (fastify, _opts, connection) => {
15
+ if (typeof fastify.hl7 !== 'undefined') {
16
+ throw new errors.FASTIFY_HL7_ERR_SETUP_ERRORS('Already registered.');
17
+ }
18
+ if (typeof fastify.hl7 === 'undefined') {
19
+ fastify.log.trace('[fastify-hl7] Decorate Fastify');
20
+ fastify.decorate('hl7', connection);
21
+ }
22
+ };
23
+ const fastifyHL7 = fp(async (fastify, opts) => {
24
+ const generatedOpts = await validateOpts(opts);
25
+ opts = { ...opts, ...generatedOpts };
26
+ // Create server.
27
+ // Since there can only be one server per IP address
28
+ // (i.e., the host this is running on.) There can only be more than one.
29
+ // A server can host many HL7 Inbound connections via different ports, this is fine.
30
+ // Clients are different as a app could talk to different servers, on many ports.
31
+ // So we need to do something different.
32
+ const serverInstance = (typeof opts.enableServer !== 'undefined' && opts.enableServer) ? new Server(opts.serverOptions) : undefined;
33
+ // Server Functions
34
+ let server;
35
+ if (typeof serverInstance !== 'undefined') {
36
+ // Server Functions
37
+ server = new HL7Server(serverInstance);
38
+ // before we close fastify, make sure all server instances are closed
39
+ fastify.addHook('preClose', async () => {
40
+ if (typeof server !== 'undefined') {
41
+ await server.closeAll();
42
+ }
43
+ });
44
+ }
45
+ // Client Functions
46
+ const client = new HL7Client();
47
+ // run these before fastify closes
48
+ fastify.addHook('preClose', async () => {
49
+ if (typeof client !== 'undefined') {
50
+ await client.closeAll();
51
+ }
52
+ });
53
+ decorateFastifyInstance(fastify, opts, {
54
+ _serverInstance: serverInstance,
55
+ buildBatch: function (props) {
56
+ return client.buildBatch(props);
57
+ },
58
+ buildFileBatch: function (props) {
59
+ return client.buildFileBatch(props);
60
+ },
61
+ buildMessage: function (props) {
62
+ return client.buildMessage(props);
63
+ },
64
+ closeServer: async function (port) {
65
+ if (typeof server !== 'undefined') {
66
+ return await server.close(port);
67
+ }
68
+ throw new errors.FASTIFY_HL7_ERR_USAGE('server was not started. re-register plugin with enableServer set to true.');
69
+ },
70
+ closeServerAll: async () => {
71
+ if (typeof server !== 'undefined') {
72
+ return await server.closeAll();
73
+ }
74
+ throw new errors.FASTIFY_HL7_ERR_USAGE('server was not started. re-register plugin with enableServer set to true.');
75
+ },
76
+ createClient: function (name, props) {
77
+ client.createClient(name, props);
78
+ },
79
+ createInbound: function (name, props, handler) {
80
+ if (typeof server !== 'undefined') {
81
+ return server.createInbound(name, props, handler);
82
+ }
83
+ throw new errors.FASTIFY_HL7_ERR_USAGE('server was not started. re-register plugin with enableServer set to true.');
84
+ },
85
+ createOutbound: function (name, props, handler) {
86
+ return client.createOutbound(name, props, handler);
87
+ },
88
+ processHL7: function (text) {
89
+ return client.processHL7(text);
90
+ },
91
+ readFile: function (fullFilePath) {
92
+ return client.readFile(fullFilePath);
93
+ },
94
+ readFileBuffer: function (fileBuffer) {
95
+ return client.readFileBuffer(fileBuffer);
96
+ }
97
+ });
98
+ });
99
+ export default fastifyHL7;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @since 1.0.0
3
+ * @param opts
4
+ */
5
+ export const validateOpts = async (opts) => {
6
+ // Mandatory, Defaulted
7
+ if (typeof opts.enableServer === 'undefined') {
8
+ opts.enableServer = true;
9
+ }
10
+ return opts;
11
+ };
@@ -0,0 +1,7 @@
1
+ import { HL7 } from 'fastify';
2
+ import fastifyHL7 from '.';
3
+ import { AServers, FastifyHL7Options } from './decorate.js';
4
+ import { HL7Client } from './class/hL7Client.js';
5
+ import { HL7Server } from './class/hL7Server.js';
6
+ export default fastifyHL7;
7
+ export { fastifyHL7, HL7, FastifyHL7Options, HL7Client, HL7Server, AServers };