Skip to content

Commit

Permalink
feat: reading file is no issue
Browse files Browse the repository at this point in the history
* server needs to be fixed now to read it, but this is completed
* normal lint fixes as well
  • Loading branch information
Bugs5382 committed Dec 20, 2023
1 parent 20ae45c commit da74a9c
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 9 deletions.
104 changes: 104 additions & 0 deletions __tests__/hl7.end2end.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,108 @@ describe('node hl7 end to end', () => {

})

describe('...send file with one message, get proper ACK', () => {

let LISTEN_PORT: number

const hl7_string: string = "MSH|^~\\&|||||20081231||ADT^A01^ADT_A01|12345||2.7\rEVN||20081231"

beforeAll(async () => {

fs.readdir("temp/", (err, files) => {
if (err) return;
for (const file of files) {
fs.unlink(path.join("temp/", file), (err) => {
if (err) throw err;
});
}
})

await sleep(2)

const message = new Message({text: hl7_string, date: "8"})
message.toFile('readFileTestMSH', true, 'temp/')

await sleep(2)

})

beforeEach(async () => {
LISTEN_PORT = await portfinder.getPortPromise({
port: 3000,
stopPort: 65353
})
})

test('...no tls', async () => {

const server = new Server({bindAddress: '0.0.0.0'})
const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => {
const messageReq = req.getMessage()
expect(messageReq.get('MSH.12').toString()).toBe('2.7')
await res.sendResponse("AA")
})

await sleep(5)

const client = new Client({host: '0.0.0.0'})
const OB_ADT = client.createOutbound({ port: LISTEN_PORT }, async (res) => {
const messageRes = res.getMessage()
expect(messageRes.get('MSA.1').toString()).toBe('AA')
})

await sleep(5)

const fileBatch = await OB_ADT.readFile(`temp/hl7.readFileTestMSH.20081231.hl7`)

await OB_ADT.sendMessage(fileBatch)

await sleep(10)

await OB_ADT.close()
await IB_ADT.close()

})

test('...tls', async () => {

const server = new Server(
{
bindAddress: '0.0.0.0',
tls:
{
key: fs.readFileSync(path.join('certs/', 'server-key.pem')),
cert: fs.readFileSync(path.join('certs/', 'server-crt.pem')),
rejectUnauthorized: false
}
})
const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => {
const messageReq = req.getMessage()
expect(messageReq.get('MSH.12').toString()).toBe('2.7')
await res.sendResponse("AA")
})

await sleep(5)

const client = new Client({host: '0.0.0.0', tls: { rejectUnauthorized: false }})
const OB_ADT = client.createOutbound({ port: LISTEN_PORT }, async (res) => {
const messageRes = res.getMessage()
expect(messageRes.get('MSA.1').toString()).toBe('AA')
})

await sleep(5)

const fileBatch = await OB_ADT.readFile(`temp/hl7.readFileTestMSH.20081231.hl7`)

await OB_ADT.sendMessage(fileBatch)

await sleep(10)

await OB_ADT.close()
await IB_ADT.close()

})

})

})
4 changes: 2 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as CORE from "./index.js"
export * as HL7 from './hl7.js'
export * as CORE from './index.js'
export * as HL7 from './hl7.js'
1 change: 0 additions & 1 deletion src/builder/modules/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ValueNode } from './valueNode.js'

/** @internal */
export class Component extends ValueNode {

/** @internal */
constructor (parent: NodeBase, key: string, text: string) {
super(parent, key, text, Delimiters.SubComponent)
Expand Down
26 changes: 24 additions & 2 deletions src/client/hl7Outbound.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import EventEmitter from 'events'
import EventEmitter from 'node:events'
import fs from 'node:fs'
import net, { Socket } from 'node:net'
import tls from 'node:tls'
import { Batch } from '../builder/batch.js'
import { FileBatch } from '../builder/fileBatch.js'
import { Message } from '../builder/message.js'
import { CR, FS, VT } from '../utils/constants.js'
import { ReadyState } from '../utils/enum.js'
Expand Down Expand Up @@ -95,6 +97,26 @@ export class HL7Outbound extends EventEmitter {
return true
}

/**
* Read a file.
* @description We need to read a file.
* We are not doing anything else other than getting the {@link Buffer} of the file,
* so we can pass it onto the File Batch class to send it to the {@link sendMessage} method as a separate step
* @since 1.0.0
* @param fullFilePath The full file path of the file we need to read.
*/
async readFile (fullFilePath: string): Promise<FileBatch> {
try {
const regex = /\n/mg
const subst = '\\r'
const fileBuffer = fs.readFileSync(fullFilePath)
const text = fileBuffer.toString().replace(regex, subst)
return new FileBatch({ text })
} catch (e: any) {
throw new HL7FatalError(500, `Unable to read file: ${fullFilePath}`)
}
}

/** Send a HL7 Message to the Listener
* @description This function sends a message/batch/file batch to the remote side.
* It has the ability, if set to auto-retry (defaulted to 1 re-connect before connection closes)
Expand All @@ -117,7 +139,7 @@ export class HL7Outbound extends EventEmitter {
*
* ```
*/
async sendMessage (message: Message | Batch): Promise<boolean> {
async sendMessage (message: Message | Batch | FileBatch): Promise<boolean> {
let attempts = 0
const maxAttempts = typeof this._opt.maxAttempts === 'undefined' ? this._main._opt.maxAttempts : this._opt.maxAttempts

Expand Down
8 changes: 4 additions & 4 deletions src/utils/normalizedBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MSH } from '../specification/specification'
import { ParserPlan } from './parserPlan.js'

const DEFAULT_CLIENT_BUILDER_OPTS = {
date: "14",
date: '14',
newLine: '\r',
parsing: false,
separatorComponent: '^',
Expand Down Expand Up @@ -121,7 +121,7 @@ export function normalizedClientMessageBuilderOptions (raw?: ClientBuilderMessag
}

if (props.date !== '8' && props.date !== '12' && props.date !== '14') {
props.date = "14"
props.date = '14'
}

if (props.text === '') {
Expand Down Expand Up @@ -155,7 +155,7 @@ export function normalizedClientBatchBuilderOptions (raw?: ClientBuilderOptions)
}

if (props.date !== '8' && props.date !== '12' && props.date !== '14') {
props.date = "14"
props.date = '14'
}

if (props.text === '') {
Expand Down Expand Up @@ -195,7 +195,7 @@ export function normalizedClientFileBuilderOptions (raw?: ClientBuilderFileOptio
}

if (props.date !== '8' && props.date !== '12' && props.date !== '14') {
props.date = "14"
props.date = '14'
}

const regex = /\n/mg
Expand Down

0 comments on commit da74a9c

Please sign in to comment.