Skip to content

Commit

Permalink
feat(Bugs5382#100): support ae type in send response (Bugs5382#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bugs5382 authored Aug 20, 2024
2 parents 67e2d1b + 2414674 commit 5e09595
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 13 deletions.
102 changes: 101 additions & 1 deletion __tests__/hl7.end2end.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('node hl7 end to end - client', () => {

describe('server/client sanity checks', () => {

test('...simple connect', async () => {
test('...simple connect .. send AA', async () => {

let dfd = createDeferred<void>()

Expand Down Expand Up @@ -58,6 +58,102 @@ describe('node hl7 end to end - client', () => {

})

test('...simple connect .. send AR', async () => {

let dfd = createDeferred<void>()

const server = new Server({bindAddress: '0.0.0.0'})
const listener = server.createInbound({port: 3000}, async (req, res) => {
const messageReq = req.getMessage()
expect(messageReq.get('MSH.12').toString()).toBe('2.7')
await res.sendResponse('AR')
const messageRes = res.getAckMessage()
expect(messageRes?.get('MSA.1').toString()).toBe('AR')
})

await expectEvent(listener, 'listen')

const client = new Client({ host: '0.0.0.0' })

const outbound = client.createConnection({ port: 3000 }, async (res) => {
const messageRes = res.getMessage()
expect(messageRes.get('MSA.1').toString()).toBe('AR')
dfd.resolve()
})

await expectEvent(outbound, 'connect')

let message = new Message({
messageHeader: {
msh_9_1: 'ADT',
msh_9_2: 'A01',
msh_10: 'CONTROL_ID',
msh_11_1: 'D'
}
})

await outbound.sendMessage(message)

await dfd.promise

expect(client.totalSent()).toEqual(1)
expect(client.totalAck()).toEqual(1)

await outbound.close()
await listener.close()

client.closeAll()

})

test('...simple connect .. send AE', async () => {

let dfd = createDeferred<void>()

const server = new Server({bindAddress: '0.0.0.0'})
const listener = server.createInbound({port: 3000}, async (req, res) => {
const messageReq = req.getMessage()
expect(messageReq.get('MSH.12').toString()).toBe('2.7')
await res.sendResponse('AE')
const messageRes = res.getAckMessage()
expect(messageRes?.get('MSA.1').toString()).toBe('AE')
})

await expectEvent(listener, 'listen')

const client = new Client({ host: '0.0.0.0' })

const outbound = client.createConnection({ port: 3000 }, async (res) => {
const messageRes = res.getMessage()
expect(messageRes.get('MSA.1').toString()).toBe('AE')
dfd.resolve()
})

await expectEvent(outbound, 'connect')

let message = new Message({
messageHeader: {
msh_9_1: 'ADT',
msh_9_2: 'A01',
msh_10: 'CONTROL_ID',
msh_11_1: 'D'
}
})

await outbound.sendMessage(message)

await dfd.promise

expect(client.totalSent()).toEqual(1)
expect(client.totalAck()).toEqual(1)

await outbound.close()
await listener.close()

client.closeAll()

})

test('...simple connect ... MSH 9.3 override', async () => {

let dfd = createDeferred<void>()
Expand Down Expand Up @@ -153,9 +249,11 @@ describe('node hl7 end to end - client', () => {
client.closeAll()

})

})

describe('server/client failure checks', () => {

test('...host does not exist, error out', async () => {

const client = new Client({ host: '0.0.0.0' })
Expand Down Expand Up @@ -289,6 +387,7 @@ describe('node hl7 end to end - client', () => {
})

describe("server/client large data checks", () => {

test("...large encapsulated data", async () => {
let dfd = createDeferred<void>();

Expand Down Expand Up @@ -353,5 +452,6 @@ describe('node hl7 end to end - client', () => {

client.closeAll();
});

});
});
5 changes: 2 additions & 3 deletions src/server/inbound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Server } from './server.js'

/**
* Inbound Handler
* @description The handler that will handle the user parsing a received message by the client to the server.
* @remarks The handler that will handle the user parsing a received message by the client to the server.
* @since 1.0.0
* @example
* In this example, we are processing the results in an async handler.
Expand Down Expand Up @@ -47,7 +47,6 @@ export interface Inbound extends EventEmitter {
/**
* Inbound Listener Class
* @since 1.0.0
* @extends EventEmitter
*/
export class Inbound extends EventEmitter implements Inbound {
/** @internal */
Expand Down Expand Up @@ -202,7 +201,7 @@ export class Inbound extends EventEmitter implements Inbound {
// create the inbound request
const req = new InboundRequest(messageParsed, { type: 'file' })
// create the send response function
const res = new SendResponse(socket, message, this._opt.overrideMSH)
const res = new SendResponse(socket, messageParsed, this._opt.overrideMSH)
// on a response sent, tell the inbound listener
void this._handler(req, res)
})
Expand Down
31 changes: 25 additions & 6 deletions src/server/modules/sendResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,36 @@ export class SendResponse extends EventEmitter {
* If you are to confirm to the end user (client) that the message they sent was good and processed successfully.
* you would send an "AA" style message (Application Accept).
* Otherwise, send an "AR" (Application Reject) to tell the client the data was
* no accept.ed/processed.
* ```
* not accepted/processed or send an "AE"
* (Application Error) to tell the client your overall application had an error.
* ```ts
* const server = new Server({bindAddress: '0.0.0.0'})
* const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => {
* const messageReq = req.getMessage()
* await res.sendResponse("AA")
* })
* ```
* "AE" (Application Error) will be sent if there is a problem creating either an "AA" or "AR" message from the orginial message sent.
*
* or
*
* const server = new Server({bindAddress: '0.0.0.0'})
* const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => {
* const messageReq = req.getMessage()
* await res.sendResponse("AR")
* })
*
* or
*
* const server = new Server({bindAddress: '0.0.0.0'})
* const IB_ADT = server.createInbound({port: LISTEN_PORT}, async (req, res) => {
* const messageReq = req.getMessage()
* await res.sendResponse("AE")
* })
*```
*
* "AE" (Application Error) will be automatically sent if there is a problem creating either an "AA" or "AR"
* message from the original message sent because the original message structure sent wrong in the first place.
*/
async sendResponse (type: 'AA' | 'AR'): Promise<void> {
async sendResponse (type: 'AA' | 'AR' | 'AE'): Promise<void> {
try {
this._ack = this._createAckMessage(type, this._message)
this._socket.write(Buffer.from(`${PROTOCOL_MLLP_HEADER}${this._ack.toString()}${PROTOCOL_MLLP_FOOTER}`))
Expand All @@ -69,7 +88,7 @@ export class SendResponse extends EventEmitter {
/**
* Get the Ack Message
* @since 2.2.0
* @description Get the acknowledged message that was sent to the client.
* @remarks Get the acknowledged message that was sent to the client.
* This could return undefined if accessed prior to sending the response
*/
getAckMessage (): Message | undefined {
Expand Down
5 changes: 2 additions & 3 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { ServerOptions, normalizeServerOptions, ListenerOptions } from '../utils

/**
* Server Class
* @description Start Server listening on a network address.
* @remarks Start Server listening on a network address.
* {@link ServerOptions} Link to the options that can be passed into props.
* @since 1.0.0
* @extends EventEmitter
*/
export class Server extends EventEmitter {
/** @internal */
Expand Down Expand Up @@ -43,7 +42,7 @@ export class Server extends EventEmitter {
}

/** This creates an instance of a HL7 server.
* @description You would specify your port and what it will do when it gets a message.
* @remarks You would specify your port and what it will do when it gets a message.
* @since 1.0.0
* @example
*```
Expand Down

0 comments on commit 5e09595

Please sign in to comment.