Skip to content

Commit

Permalink
Fixes #4576 - Windows 1252 character encoding (#4594)
Browse files Browse the repository at this point in the history
* Fixes #4576 - Windows 1252 character encoding

* Add encoding as Endpoint.address query param

* Added cli options
  • Loading branch information
codyebberson committed May 27, 2024
1 parent cb88f36 commit 2dcd87f
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 64 deletions.
122 changes: 86 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@medplum/core": "3.1.6",
"@medplum/hl7": "3.1.6",
"dcmjs-dimse": "0.1.27",
"iconv-lite": "0.6.3",
"node-windows": "1.0.0-beta.8",
"ws": "8.17.0"
},
Expand Down
1 change: 1 addition & 0 deletions packages/agent/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ export class App {
const client = new Hl7Client({
host: address.hostname,
port: Number.parseInt(address.port, 10),
encoding: address.searchParams.get('encoding') ?? undefined,
});

client
Expand Down
3 changes: 2 additions & 1 deletion packages/agent/src/hl7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export class AgentHl7Channel extends BaseChannel {
}
this.started = true;
const address = new URL(this.getEndpoint().address as string);
const encoding = address.searchParams.get('encoding') ?? undefined;
this.log.info(`Channel starting on ${address}...`);
this.server.start(Number.parseInt(address.port, 10));
this.server.start(Number.parseInt(address.port, 10), encoding);
this.log.info('Channel started successfully');
}

Expand Down
1 change: 1 addition & 0 deletions packages/cli/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const options = {
'commander',
'dotenv',
'fast-glob',
'iconv-lite',
'node-fetch',
'tar',
],
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"commander": "12.1.0",
"dotenv": "16.4.5",
"fast-glob": "3.3.2",
"iconv-lite": "0.6.3",
"node-fetch": "2.7.0",
"tar": "7.1.0"
},
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/hl7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const send = createMedplumCommand('send')
.argument('[body]', 'Optional HL7 message body')
.option('--generate-example', 'Generate a sample HL7 message')
.option('--file <file>', 'Read the HL7 message from a file')
.option('--encoding <encoding>', 'The encoding to use')
.action(async (host, port, body, options) => {
if (options.generateExample) {
body = generateSampleHl7Message();
Expand All @@ -25,6 +26,7 @@ const send = createMedplumCommand('send')
const client = new Hl7Client({
host,
port: Number.parseInt(port, 10),
encoding: options.encoding,
});

try {
Expand All @@ -38,15 +40,16 @@ const send = createMedplumCommand('send')
const listen = createMedplumCommand('listen')
.description('Starts an HL7 v2 MLLP server')
.argument('<port>')
.action(async (port) => {
.option('--encoding <encoding>', 'The encoding to use')
.action(async (port, options) => {
const server = new Hl7Server((connection) => {
connection.addEventListener('message', ({ message }) => {
console.log(message.toString().replaceAll('\r', '\n'));
connection.send(message.buildAck());
});
});

server.start(Number.parseInt(port, 10));
server.start(Number.parseInt(port, 10), options.encoding);
console.log('Listening on port ' + port);
});

Expand Down
6 changes: 6 additions & 0 deletions packages/hl7/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Server and client.

## Character Encoding

List of iconv-lite character encodings

See: https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings

## License

Apache 2.0. Copyright &copy; Medplum 2023
2 changes: 1 addition & 1 deletion packages/hl7/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const options = {
tsconfig: 'tsconfig.json',
minify: true,
sourcemap: true,
external: ['@medplum/core', 'dataloader', 'rfc6902'],
external: ['@medplum/core', 'iconv-lite'],
};

esbuild
Expand Down
3 changes: 2 additions & 1 deletion packages/hl7/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"test": "jest"
},
"dependencies": {
"@medplum/core": "3.1.6"
"@medplum/core": "3.1.6",
"iconv-lite": "0.6.3"
},
"devDependencies": {
"@medplum/fhirtypes": "3.1.6"
Expand Down
5 changes: 4 additions & 1 deletion packages/hl7/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import { Hl7Connection } from './connection';
export interface Hl7ClientOptions {
host: string;
port: number;
encoding?: string;
}

export class Hl7Client extends Hl7Base {
options: Hl7ClientOptions;
host: string;
port: number;
encoding?: string;
connection?: Hl7Connection;

constructor(options: Hl7ClientOptions) {
super();
this.options = options;
this.host = this.options.host;
this.port = this.options.port;
this.encoding = this.options.encoding;
}

connect(): Promise<Hl7Connection> {
Expand All @@ -28,7 +31,7 @@ export class Hl7Client extends Hl7Base {

return new Promise((resolve, reject) => {
const socket = connect({ host: this.host, port: this.port }, () => {
this.connection = new Hl7Connection(socket);
this.connection = new Hl7Connection(socket, this.encoding);
socket.off('error', reject);
resolve(this.connection);
});
Expand Down
Loading

0 comments on commit 2dcd87f

Please sign in to comment.