Commit 07b4924
authored
refactor: Side-effect free
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]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L33-R68)
[[2]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L54-L78)
[[3]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L91-R99)
- Improved the logger method wrapping logic to ensure consistent logging
behavior in browser environments.
[[1]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L33-R68)
[[2]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L54-L78)
**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]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L34-L38)
[[2]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L318-L326)
**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.utils (#3322)1 parent 240478c commit 07b4924
File tree
4 files changed
+47
-46
lines changed- packages/utils/src
4 files changed
+47
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | 33 | | |
59 | 34 | | |
60 | 35 | | |
| |||
76 | 51 | | |
77 | 52 | | |
78 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
79 | 87 | | |
80 | 88 | | |
81 | 89 | | |
| |||
88 | 96 | | |
89 | 97 | | |
90 | 98 | | |
91 | | - | |
| 99 | + | |
92 | 100 | | |
93 | 101 | | |
94 | 102 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | | - | |
36 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
37 | 43 | | |
38 | | - | |
39 | 44 | | |
40 | 45 | | |
41 | 46 | | |
| |||
315 | 320 | | |
316 | 321 | | |
317 | 322 | | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | 323 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | 1 | | |
5 | | - | |
| 2 | + | |
6 | 3 | | |
7 | 4 | | |
8 | 5 | | |
9 | | - | |
| 6 | + | |
10 | 7 | | |
11 | 8 | | |
12 | 9 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
| 10 | + | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
0 commit comments