diff --git a/__tests__/hl7.build.test.ts b/__tests__/hl7.build.test.ts index be2cd79..1b5d3de 100644 --- a/__tests__/hl7.build.test.ts +++ b/__tests__/hl7.build.test.ts @@ -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) + }) }) diff --git a/src/index.ts b/src/index.ts index eb9c751..a941314 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/utils/normalizedBuilder.ts b/src/utils/normalizedBuilder.ts index 172ba24..adceb59 100644 --- a/src/utils/normalizedBuilder.ts +++ b/src/utils/normalizedBuilder.ts @@ -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' @@ -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 @@ -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') { diff --git a/src/utils/normalizedClient.ts b/src/utils/normalizedClient.ts index 660f5f6..f43b907 100644 --- a/src/utils/normalizedClient.ts +++ b/src/utils/normalizedClient.ts @@ -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 */