@ubercode/dcmtk 0.12.1 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tools.cjs CHANGED
@@ -542,6 +542,7 @@ var Dcm2jsonOptionsSchema = zod.z.object({
542
542
  signal: zod.z.instanceof(AbortSignal).optional(),
543
543
  directOnly: zod.z.boolean().optional(),
544
544
  charsetAssume: zod.z.string().min(1).optional(),
545
+ charsetFallback: zod.z.string().min(1).optional(),
545
546
  verbosity: zod.z.enum(["verbose", "debug"]).optional()
546
547
  }).strict().optional();
547
548
  var VERBOSITY_FLAGS2 = { verbose: "-v", debug: "-d" };
@@ -555,27 +556,34 @@ function buildXmlOpts(options) {
555
556
  const result = {};
556
557
  if (options?.verbosity !== void 0) result["verbosity"] = options.verbosity;
557
558
  if (options?.charsetAssume !== void 0) result["charsetAssume"] = options.charsetAssume;
559
+ if (options?.charsetFallback !== void 0) result["charsetFallback"] = options.charsetFallback;
558
560
  return result;
559
561
  }
562
+ function isCharsetError(stderrOutput) {
563
+ return stderrOutput.includes("convert character encoding") || stderrOutput.includes("Illegal byte sequence");
564
+ }
565
+ async function runXmlAndParse(binary, args, execOpts) {
566
+ const xmlResult = await execCommand(binary, args, execOpts);
567
+ if (!xmlResult.ok) return err(xmlResult.error);
568
+ if (xmlResult.value.exitCode !== 0) {
569
+ return err(createToolError("dcm2xml", args, xmlResult.value.exitCode, xmlResult.value.stderr));
570
+ }
571
+ const jsonResult = xmlToJson(xmlResult.value.stdout);
572
+ if (!jsonResult.ok) return err(jsonResult.error);
573
+ return ok({ data: jsonResult.value, source: "xml" });
574
+ }
560
575
  async function tryXmlPath(inputPath, timeoutMs, signal, opts) {
561
576
  const xmlBinary = resolveBinary("dcm2xml");
562
- if (!xmlBinary.ok) {
563
- return err(xmlBinary.error);
564
- }
577
+ if (!xmlBinary.ok) return err(xmlBinary.error);
565
578
  const charsetArgs = opts?.charsetAssume !== void 0 ? ["+Ca", opts.charsetAssume] : [];
566
579
  const xmlArgs = [...buildVerbosityArgs(opts?.verbosity), ...charsetArgs, "-nat", inputPath];
567
- const xmlResult = await execCommand(xmlBinary.value, xmlArgs, { timeoutMs, signal });
568
- if (!xmlResult.ok) {
569
- return err(xmlResult.error);
580
+ const execOpts = signal !== void 0 ? { timeoutMs, signal } : { timeoutMs };
581
+ const result = await runXmlAndParse(xmlBinary.value, xmlArgs, execOpts);
582
+ if (!result.ok && opts?.charsetFallback !== void 0 && isCharsetError(result.error.message)) {
583
+ const fallbackArgs = [...buildVerbosityArgs(opts.verbosity), "+Ca", opts.charsetFallback, "-nat", inputPath];
584
+ return runXmlAndParse(xmlBinary.value, fallbackArgs, execOpts);
570
585
  }
571
- if (xmlResult.value.exitCode !== 0) {
572
- return err(createToolError("dcm2xml", xmlArgs, xmlResult.value.exitCode, xmlResult.value.stderr));
573
- }
574
- const jsonResult = xmlToJson(xmlResult.value.stdout);
575
- if (!jsonResult.ok) {
576
- return err(jsonResult.error);
577
- }
578
- return ok({ data: jsonResult.value, source: "xml" });
586
+ return result;
579
587
  }
580
588
  async function tryDirectPath(inputPath, timeoutMs, signal, verbosity) {
581
589
  const jsonBinary = resolveBinary("dcm2json");