Commit 240478c
authored
refactor [NET-1606]: Bundle
This pull request introduces significant improvements to the
`@streamr/utils` package, focusing on robust browser and Node.js
environment support, build configuration enhancements, and codebase
modernization. The main changes include a new dual build and export
system for browser and Node.js, environment variable handling
refactoring, and the replacement of Node.js-specific APIs with
cross-platform polyfills.
### Changes
**Build and packaging enhancements:**
- Added a dual build system using a new `rollup.config.mts` to generate
separate browser and Node.js bundles, with appropriate module resolution
and aliasing for each environment. (`packages/utils/rollup.config.mts`)
- Updated `package.json` to use the `exports` field for conditional
exports (browser vs. Node.js), adjusted the `files` array, and added new
build scripts and dependencies for Rollup and polyfills.
(`packages/utils/package.json`)
[[1]](diffhunk://#diff-21f8eb1d7a149d42ab0454ea905d44f624ff1d0aeb6d7a88d75b42962ca6b053L10-R36)
[[2]](diffhunk://#diff-21f8eb1d7a149d42ab0454ea905d44f624ff1d0aeb6d7a88d75b42962ca6b053R47-R69)
- Introduced a `prebuild` and `postbuild` step to streamline the build
process and ensure clean output. (`packages/utils/package.json`)
**Cross-platform and polyfill improvements:**
- Replaced direct Node.js API usage (e.g., `crypto`, `os`) with
cross-platform abstractions and browser polyfills (e.g., `buffer-shim`,
`path-browserify`, custom `os` and `env` shims for browsers).
(`packages/utils/src/browser/crypto.ts`,
`packages/utils/src/browser/env.ts`, `packages/utils/src/browser/os.ts`,
`packages/utils/src/exports-browser.ts`,
`packages/utils/src/keyToArrayIndex.ts`,
`packages/utils/karma.config.ts`)
[[1]](diffhunk://#diff-285b8e8fa583afc20697242820c6adb2419720d1c2056c6ecb7d8617060c1a51R1-R23)
[[2]](diffhunk://#diff-245bac5c3851ffe8bc18571e59cf298ecb92e78d093c47fb8b1cef8c740174feR1-R5)
[[3]](diffhunk://#diff-0106994557e39509eb1269938f670aa46dea2f4ab6e6e0e7c06124b973cfa268R1-R6)
[[4]](diffhunk://#diff-394788f4b1559a7010d4d4a1ae7f96a11cb992f358a46090cfb9c1eebfbfac05L1-R1)
[[5]](diffhunk://#diff-394788f4b1559a7010d4d4a1ae7f96a11cb992f358a46090cfb9c1eebfbfac05L28-R28)
F8929faeL1)
- Removed the old `crossPlatformCrypto.ts` in favor of new
environment-specific implementations.
(`packages/utils/src/crossPlatformCrypto.ts`)
**Environment variable handling:**
- Refactored all environment variable access to use a unified `env`
abstraction, ensuring safe access in both Node.js and browser contexts.
(`packages/utils/src/Logger.ts`, `packages/utils/src/browser/env.ts`)
[[1]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4R5)
[[2]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L21-R36)
[[3]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L46-R45)
[[4]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L98-R97)
[[5]](diffhunk://#diff-312c84735c8ba55bd72f753e86af89e6c8f1b058ac23ac2290d447c64b7fd6d4L117-R116)
[[6]](diffhunk://#diff-91ff14206456eacaae372406d303b78f2a1ca01c710b6f0f005a8ee20b2cf50eR1-R19)
**Testing and configuration:**
- Updated Jest and Karma configurations to support new module aliases
and environment-specific code paths. (`packages/utils/jest.config.ts`,
`packages/utils/karma.config.ts`)
([packages/utils/jest.config.tsL1-R11](diffhunk://#diff-138963f3d6083e08b3091ffdcd032a655c4738ba0a41476560753dd9f6b1efeaL1-R11),
F8929faeL1)
**Code modernization and cleanup:**
- Updated cryptographic utilities to use new cross-platform imports and
types, improving maintainability and browser compatibility.
(`packages/utils/src/SigningUtil.ts`, `packages/utils/src/exports.ts`)
[[1]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L9-R9)
[[2]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L21-L23)
[[3]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L61-R57)
[[4]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L94-R90)
[[5]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L170-R166)
[[6]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L202-R200)
[[7]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L232-R231)
[[8]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L246-R244)
[[9]](diffhunk://#diff-264c1a909477860ce2ed2c73f193874422c99397476f8d74ec602ccd11b80512L260-R258)
[[10]](diffhunk://#diff-d700ca1a1d7a0254f811405cb99a0f7eba3b8ec8396aede7362c8c842fbb7a56L56-R56)
**Additional minor improvements:**
- Added `@streamr/utils` to the Docker build bootstrap step.
(`Dockerfile.node`)
These changes collectively make the `@streamr/utils` package more
robust, easier to consume in different environments, and simpler to
maintain.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Modernizes `@streamr/utils` for first-class browser and Node.js
support with dual bundles, conditional exports, and polyfilled APIs.
>
> - New Rollup config (`packages/utils/rollup.config.mts`) outputs
`exports-browser.*` and `exports-nodejs.*`; `package.json` uses
conditional `exports` and updated build scripts
> - Introduces env/crypto platform shims: `src/nodejs/{env,crypto}.ts`
and `src/browser/{env,crypto,os}.ts`; removes
`src/crossPlatformCrypto.ts`
> - Replaces Node-only APIs with cross-platform imports: `Logger` reads
from `@/env`; hashing via `computeMd5`; updated `SigningUtil` to use
`@/crypto`
> - Adds browser entry `src/exports-browser.ts` (includes `buffer-shim`)
and path aliases for both envs; updates Jest/Karma configs and tsconfigs
(`tsconfig.{node,browser,jest,karma}.json`)
> - Updates `packages/utils/package.json` fields (`main/module/types`,
`files`) and dependencies (Rollup, plugins, polyfills)
> - Docker build now bootstraps `@streamr/utils` in `Dockerfile.node`
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
b3774ce. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->utils separately for browser and NodeJS (#3321)1 parent 58d67f8 commit 240478c
File tree
25 files changed
+2184
-701
lines changed- packages/utils
- src
- browser
- nodejs
- test
25 files changed
+2184
-701
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
10 | 16 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
12 | 27 | | |
13 | | - | |
| 28 | + | |
| 29 | + | |
14 | 30 | | |
15 | 31 | | |
16 | 32 | | |
17 | 33 | | |
18 | 34 | | |
| 35 | + | |
19 | 36 | | |
20 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
21 | 40 | | |
22 | 41 | | |
23 | 42 | | |
| |||
28 | 47 | | |
29 | 48 | | |
30 | 49 | | |
| 50 | + | |
| 51 | + | |
31 | 52 | | |
32 | 53 | | |
| 54 | + | |
| 55 | + | |
33 | 56 | | |
34 | 57 | | |
| 58 | + | |
35 | 59 | | |
36 | 60 | | |
37 | 61 | | |
38 | 62 | | |
| 63 | + | |
| 64 | + | |
39 | 65 | | |
40 | 66 | | |
41 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
42 | 73 | | |
43 | 74 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 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 | + | |
| 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 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
22 | | - | |
23 | 22 | | |
24 | | - | |
| 23 | + | |
25 | 24 | | |
26 | 25 | | |
27 | | - | |
| 26 | + | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | | - | |
| 30 | + | |
32 | 31 | | |
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
36 | | - | |
37 | | - | |
| 35 | + | |
| 36 | + | |
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
| |||
43 | 42 | | |
44 | 43 | | |
45 | 44 | | |
46 | | - | |
| 45 | + | |
47 | 46 | | |
48 | 47 | | |
49 | 48 | | |
| |||
95 | 94 | | |
96 | 95 | | |
97 | 96 | | |
98 | | - | |
| 97 | + | |
99 | 98 | | |
100 | 99 | | |
101 | 100 | | |
| |||
114 | 113 | | |
115 | 114 | | |
116 | 115 | | |
117 | | - | |
| 116 | + | |
118 | 117 | | |
119 | 118 | | |
120 | 119 | | |
| |||
0 commit comments