Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/dicom-codec/src/utils/processTimer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { isNode, isBrowser } = require("browser-or-node");
const { isNode, isBrowser, isWebWorker } = require("browser-or-node");
/**
* Wrapper for process timer to capture process timestamp.
*
Expand All @@ -13,6 +13,11 @@ function processTimer(processName, loggerInstance) {
let processDurationTime;
const NS_SEC = 1000000000;

/**
*
* @return {[number, number]|number}
* @param previousTime{[number, number]}
*/
Comment on lines +16 to +20

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Make hrtime return shape consistent across all branches.

Line 50 returns 0, but downstream code assumes a tuple and indexes time[0]/time[1] (Line 54). In unsupported runtimes this can still crash. Keep the contract tuple-only and align JSDoc accordingly.

Proposed fix
   /**
-   *
-   * `@return` {[number, number]|number}
-   * `@param` previousTime{[number, number]}
+   * `@param` {[number, number]} [previousTime]
+   * `@returns` {[number, number]}
    */
   function hrtime(previousTime) {
@@
-    // Return [0, 0] to avoid crash in other environments?
-    return 0;
+    // Fallback: preserve tuple contract to avoid downstream crashes.
+    return [0, 0];
   }

Also applies to: 49-50

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/dicom-codec/src/utils/processTimer.js` around lines 16 - 20, The
hrtime function has an inconsistent return type that causes crashes when the
return value is indexed. The JSDoc currently indicates the return type can be
either a tuple [number, number] or a plain number, but the code at line 54
always indexes into the result with [0] and [1]. Update the JSDoc in the comment
block to only specify [number, number] as the return type, and then locate the
return statement around line 50 that currently returns the numeric value 0 and
change it to return a tuple format [0, 0] to maintain consistency with the
expected contract that all code paths return a tuple.

function hrtime(previousTime) {
// node
if (isNode) {
Expand All @@ -32,6 +37,16 @@ function processTimer(processName, loggerInstance) {
}
}

// browser web worker
if (isWebWorker) {
if (previousTime) {
return [Math.abs(performance.now() - previousTime[0]), 0];
} else {
return [performance.now(), 0];
}
}

// Return [0, 0] to avoid crash in other environments?
return 0;
}

Expand Down