refactor [NET-1606]: Make `utils` package work without polyfilling
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.mtsto generate separate browser and Node.js bundles, with appropriate module resolution and aliasing for each environment. (packages/utils/rollup.config.mts) - Updated
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] - Introduced a
prebuildandpostbuildstep 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, 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) - Removed the old
crossPlatformCrypto.tsin favor of new environment-specific implementations. (packages/utils/src/crossPlatformCrypto.ts)
Environment variable handling:
- Refactored all environment variable access to use a unified
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:
- 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, 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] [2] [3] [4] [5] [6] [7] [8] [9] [10]
Additional minor improvements:
- Added
@streamr/utilsto 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.
[!NOTE] Modernizes
@streamr/utilsto be environment-agnostic with separate Node and browser builds and conditional exports.
- Introduces dual build pipeline via
rollup.config.mtsandpackage.jsonexportsfor browser/Node outputs- Replaces Node-only APIs with cross-platform abstractions: new
@/crypto,@/env, browser shims (buffer-shim,path-browserify, customos), removescrossPlatformCrypto- Refactors
Logger,SigningUtil,keyToArrayIndex, andexportsto use new env/crypto utilities- Updates Jest/Karma and TS configs for path aliases and environment-specific sources; adds utils to Docker bootstrap
Written by Cursor Bugbot for commit ab28235acfafdec0bbdb7dbe1e8693138c46a1f1. This will update automatically on new commits. Configure here.