@ubercode/dcmtk 0.9.1 → 0.9.3

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.
@@ -1723,9 +1723,11 @@ declare class DicomReceiver extends EventEmitter<DicomReceiverEventMap> {
1723
1723
  private wireFileReceived;
1724
1724
  /** Moves a received file, opens it as DicomInstance, and emits FILE_RECEIVED. */
1725
1725
  private handleFileReceived;
1726
+ /** Inner handler: move file, parse DICOM, emit FILE_RECEIVED or error. */
1727
+ private moveAndEmitFile;
1726
1728
  /** Returns worker to idle pool on association complete, emits summary. */
1727
1729
  private wireAssociationComplete;
1728
- /** Awaits pending file operations, emits ASSOCIATION_COMPLETE, resets worker state. */
1730
+ /** Awaits ALL pending file operations, emits ASSOCIATION_COMPLETE, resets worker state. */
1729
1731
  private finalizeAssociation;
1730
1732
  /** Emits the ASSOCIATION_COMPLETE event with transfer stats. */
1731
1733
  private emitAssociationComplete;
@@ -1723,9 +1723,11 @@ declare class DicomReceiver extends EventEmitter<DicomReceiverEventMap> {
1723
1723
  private wireFileReceived;
1724
1724
  /** Moves a received file, opens it as DicomInstance, and emits FILE_RECEIVED. */
1725
1725
  private handleFileReceived;
1726
+ /** Inner handler: move file, parse DICOM, emit FILE_RECEIVED or error. */
1727
+ private moveAndEmitFile;
1726
1728
  /** Returns worker to idle pool on association complete, emits summary. */
1727
1729
  private wireAssociationComplete;
1728
- /** Awaits pending file operations, emits ASSOCIATION_COMPLETE, resets worker state. */
1730
+ /** Awaits ALL pending file operations, emits ASSOCIATION_COMPLETE, resets worker state. */
1729
1731
  private finalizeAssociation;
1730
1732
  /** Emits the ASSOCIATION_COMPLETE event with transfer stats. */
1731
1733
  private emitAssociationComplete;
package/dist/index.cjs CHANGED
@@ -36716,18 +36716,44 @@ var DicomReceiver = class _DicomReceiver extends events.EventEmitter {
36716
36716
  /** Wires FILE_RECEIVED from dcmrecv worker to handleFileReceived. */
36717
36717
  wireFileReceived(worker) {
36718
36718
  worker.dcmrecv.onFileReceived((data) => {
36719
- const promise = this.handleFileReceived(worker, data);
36719
+ const assocId = worker.associationId;
36720
+ const assocDir = worker.associationDir;
36721
+ if (assocDir === void 0 || assocId === void 0) {
36722
+ this.emit("error", {
36723
+ error: new Error(`DicomReceiver: FILE_RECEIVED with no active association (worker state: ${worker.state})`),
36724
+ filePath: data.filePath,
36725
+ callingAE: data.callingAE,
36726
+ calledAE: data.calledAE,
36727
+ source: data.source
36728
+ });
36729
+ return;
36730
+ }
36731
+ const promise = this.handleFileReceived(worker, data, assocId, assocDir);
36720
36732
  worker.pendingFiles.push(promise);
36721
36733
  void promise.finally(() => removePending(worker.pendingFiles, promise));
36722
36734
  });
36723
36735
  }
36724
36736
  /** Moves a received file, opens it as DicomInstance, and emits FILE_RECEIVED. */
36725
- async handleFileReceived(worker, data) {
36726
- if (worker.associationDir === void 0 || worker.associationId === void 0) return;
36737
+ async handleFileReceived(worker, data, assocId, assocDir) {
36738
+ try {
36739
+ await this.moveAndEmitFile(worker, data, assocId, assocDir);
36740
+ } catch (thrown) {
36741
+ const error = thrown instanceof Error ? thrown : new Error(String(thrown));
36742
+ this.emit("error", {
36743
+ error,
36744
+ filePath: data.filePath,
36745
+ associationId: assocId,
36746
+ associationDir: assocDir,
36747
+ callingAE: data.callingAE,
36748
+ calledAE: data.calledAE,
36749
+ source: data.source
36750
+ });
36751
+ }
36752
+ }
36753
+ /** Inner handler: move file, parse DICOM, emit FILE_RECEIVED or error. */
36754
+ async moveAndEmitFile(worker, data, assocId, assocDir) {
36727
36755
  const srcPath = data.filePath;
36728
- const destPath = path__namespace.join(worker.associationDir, path__namespace.basename(srcPath));
36729
- const assocId = worker.associationId;
36730
- const assocDir = worker.associationDir;
36756
+ const destPath = path__namespace.join(assocDir, path__namespace.basename(srcPath));
36731
36757
  const moveResult = await moveFile(srcPath, destPath);
36732
36758
  const finalPath = moveResult.ok ? destPath : srcPath;
36733
36759
  const fileSize = await statFileSafe(finalPath);
@@ -36763,9 +36789,9 @@ var DicomReceiver = class _DicomReceiver extends events.EventEmitter {
36763
36789
  void this.finalizeAssociation(worker, data);
36764
36790
  });
36765
36791
  }
36766
- /** Awaits pending file operations, emits ASSOCIATION_COMPLETE, resets worker state. */
36792
+ /** Awaits ALL pending file operations, emits ASSOCIATION_COMPLETE, resets worker state. */
36767
36793
  async finalizeAssociation(worker, data) {
36768
- if (worker.pendingFiles.length > 0) {
36794
+ while (worker.pendingFiles.length > 0) {
36769
36795
  await Promise.all(worker.pendingFiles);
36770
36796
  }
36771
36797
  this.emitAssociationComplete(worker, data);