refactor [NET-1606]: Make utils package work without polyfilling
#3321
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces significant improvements to the
@streamr/utilspackage, 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:
rollup.config.mtsto generate separate browser and Node.js bundles, with appropriate module resolution and aliasing for each environment. (packages/utils/rollup.config.mts)package.jsonto use theexportsfield for conditional exports (browser vs. Node.js), adjusted thefilesarray, and added new build scripts and dependencies for Rollup and polyfills. (packages/utils/package.json) [1] [2]prebuildandpostbuildstep to streamline the build process and ensure clean output. (packages/utils/package.json)Cross-platform and polyfill improvements:
crypto,os) with cross-platform abstractions and browser polyfills (e.g.,buffer-shim,path-browserify, customosandenvshims 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] [2] [3] [4] [5] F8929faeL1)crossPlatformCrypto.tsin favor of new environment-specific implementations. (packages/utils/src/crossPlatformCrypto.ts)Environment variable handling:
envabstraction, ensuring safe access in both Node.js and browser contexts. (packages/utils/src/Logger.ts,packages/utils/src/browser/env.ts) [1] [2] [3] [4] [5] [6]Testing and configuration:
packages/utils/jest.config.ts,packages/utils/karma.config.ts) (packages/utils/jest.config.tsL1-R11, F8929faeL1)Code modernization and cleanup:
packages/utils/src/SigningUtil.ts,packages/utils/src/exports.ts) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]Additional minor improvements:
@streamr/utilsto the Docker build bootstrap step. (Dockerfile.node)These changes collectively make the
@streamr/utilspackage more robust, easier to consume in different environments, and simpler to maintain.Note
Modernizes
@streamr/utilsto be environment-agnostic with separate Node and browser builds and conditional exports.rollup.config.mtsandpackage.jsonexportsfor browser/Node outputs@/crypto,@/env, browser shims (buffer-shim,path-browserify, customos), removescrossPlatformCryptoLogger,SigningUtil,keyToArrayIndex, andexportsto use new env/crypto utilitiesWritten by Cursor Bugbot for commit ab28235. This will update automatically on new commits. Configure here.