Skip to content

Commit

Permalink
feat: update file batch options
Browse files Browse the repository at this point in the history
* can accept the file location, or
* the buffer of the file directly
* unit tests for the above work
* removed old interface that was not needed (ParserProcessRawData)
  • Loading branch information
Bugs5382 committed Dec 16, 2023
1 parent cb539b6 commit 9561962
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
33 changes: 31 additions & 2 deletions __tests__/hl7.build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,17 +899,46 @@ describe('node hl7 client - builder tests', () => {

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})
message.toFile('readTestMSH', true, 'temp/')

const batch = new Batch({text: hl7_batch})
batch.toFile('readTestBHS', true, 'temp/')

await sleep(2)

})

test.todo('...read file with MSH only')
beforeEach(async () => {
await sleep(1)
})

test.todo('...read file BSH and 1xMSH only')
test('...test parsing - file path', async() => {
const fileBatch_one = new FileBatch({ fullFilePath: path.join('temp/', `hl7.readTestMSH.20081231.hl7`)})
expect(fileBatch_one._opt.text).toContain(hl7_string)

const fileBatch_two = new FileBatch({ fullFilePath: path.join('temp/', `hl7.readTestBHS.20231208.hl7`)})
expect(fileBatch_two._opt.text).toContain(hl7_batch)
})

test('...test parsing - buffer', async() => {
const fileBatch_one = new FileBatch({ fileBuffer: fs.readFileSync(path.join('temp/', `hl7.readTestMSH.20081231.hl7`))})
expect(fileBatch_one._opt.text).toContain(hl7_string)

const fileBatch_two = new FileBatch({ fileBuffer: fs.readFileSync(path.join('temp/', `hl7.readTestBHS.20231208.hl7`))})
expect(fileBatch_two._opt.text).toContain(hl7_batch)
})

})

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type { HL7_2_7_MSH } from './specification/2.7.js'
/** HL7 Class **/
export { HL7_2_7_BHS, HL7_2_7_FHS, HL7_SPEC, HL7_SPEC_BASE, HL7_2_7 }

export type { ClientOptions, ClientListenerOptions, ParserProcessRawData } from './utils/normalizedClient.js'
export type { ClientOptions, ClientListenerOptions } from './utils/normalizedClient.js'
export type { ClientBuilderFileOptions, ClientBuilderBatchOptions, ClientBuilderMessageOptions, ClientBuilderOptions } from './utils/normalizedBuilder.js'
export type { HL7Error, HL7FatalError, HL7ParserError } from './utils/exception.js'

Expand Down
28 changes: 24 additions & 4 deletions src/utils/normalizedBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs'
import { HL7_2_7 } from '../specification/2.7.js'
import { FHS, BHS, MSH } from '../specification/specification.js'
import { ParserPlan } from './parserPlan.js'
Expand Down Expand Up @@ -90,18 +91,23 @@ export interface ClientBuilderFileOptions extends ClientBuilderOptions {
* @default hl7
*/
extension?: string
/**
* FHS Header Options
/** The file as a buffer passed onto the constructor
* @since 1.0.0 */
fileBuffer?: Buffer
/** If you are providing the full file path, please set it here.
* @since 1.0.0 */
fullFilePath?: string
/** FHS Header Options
* @since 1.0.0
*/
fileHeader?: FHS
/**
* Location where the file will be saved.
/** Location where the file will be saved.
* If this is not set,
* the files will get save it in the same directory of the executing file that is calling the function.
* If running this package inside a DOCKER/KUBERNETES node,
* if the container is destroyed and the files are not saved on a folder mounted outside the node,
* the files will be lost on restart.
* @since 1.0.0
* @default ""
*/
location?: string
Expand Down Expand Up @@ -180,6 +186,20 @@ export function normalizedClientFileBuilderOptions (raw?: ClientBuilderFileOptio
throw new Error('The extension for file save must be 3 characters long.')
}

if (typeof props.fullFilePath !== 'undefined' && typeof props.fileBuffer !== 'undefined') {
throw new Error('You can not have specified a file path and a buffer. Please choose one or the other.')
}

const regex = /\n/mg
const subst = '\\r'
if (typeof props.fullFilePath !== 'undefined' && typeof props.fileBuffer === 'undefined') {

const fileBuffer = fs.readFileSync(props.fullFilePath)
props.text = fileBuffer.toString().replace(regex, subst)
} else if (typeof props.fullFilePath === 'undefined' && typeof props.fileBuffer !== 'undefined') {
props.text = props.fileBuffer.toString().replace(regex, subst)
}

if (props.text === '') {
props.text = `FHS${props.separatorField}${props.separatorComponent}${props.separatorRepetition}${props.separatorEscape}${props.separatorSubComponent}`
} else if (typeof props.text !== 'undefined') {
Expand Down
5 changes: 0 additions & 5 deletions src/utils/normalizedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ const DEFAULT_LISTEN_CLIENT_OPTS = {
waitAck: true
}

export interface ParserProcessRawData {
/** Data that needs to be processed. */
data: string
}

export interface ClientOptions {
/** Max wait time, in milliseconds, for a connection attempt
* @default 10_000 */
Expand Down

0 comments on commit 9561962

Please sign in to comment.