network
network copied to clipboard
refactor: Side-effect free `utils`
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
utilsrefactoring). Other packages will follow to make the whole thing much more convenient to use.
Changes
Logger improvements:
- Refactored the
Loggerclass to encapsulate the root logger initialization in a static method, removing the globalrootLoggervariable 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.getInstancemethod by replacing the statickeyTypeToInstancemap 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.tsto create newTextEncoderandTextDecoderinstances 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
executeSafePromisefunction 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.