network icon indicating copy to clipboard operation
network copied to clipboard

refactor: Side-effect free `utils`

Open mondoreale opened this issue 1 month ago • 1 comments

This pull request refactors and improves utility modules in the codebase, focusing on the Logger and SigningUtil classes, as well as minor optimizations in binary utilities and error handling. The main changes include a redesign of the logger initialization for better encapsulation, simplification of the signing utility instantiation logic, and minor performance and correctness tweaks.

[!NOTE] This is just the first step (alongside the recent utils refactoring). Other packages will follow to make the whole thing much more convenient to use.

Changes

Logger improvements:

  • Refactored the Logger class to encapsulate the root logger initialization in a static method, removing the global rootLogger variable and improving encapsulation. The logger now lazily initializes the root logger as needed. [1] [2] [3]
  • Improved the logger method wrapping logic to ensure consistent logging behavior in browser environments. [1] [2]

Signing utility simplification:

  • Simplified the SigningUtil.getInstance method by replacing the static keyTypeToInstance map with a switch statement that directly instantiates the correct class for each key type. This removes unnecessary static instances and clarifies the instantiation logic. [1] [2]

Utility optimizations:

  • Updated binaryUtils.ts to create new TextEncoder and TextDecoder instances on each call instead of using shared instances, preventing potential state issues and improving reliability.

Error handling improvements:

  • Moved the creation of the logger instance inside the executeSafePromise function to ensure a fresh logger is used for each invocation, and removed the unnecessary top-level logger instance.

Future steps

  • Propagate the same technique throughout the other packages.

mondoreale avatar Dec 29 '25 23:12 mondoreale