3.4 KiB
3.4 KiB
SmartEnv Hints
Architecture Overview
- Single main class
Smartenvthat provides all functionality - Uses dependency injection pattern with plugins imported from smartenv.plugins.ts
- Utilizes @push.rocks/smartpromise for async operations
- BREAKING CHANGE:
runtimeEnvnow returns'node' | 'deno' | 'bun' | 'browser'(previously only 'node' | 'browser')
Runtime Detection System
Detection Order (Critical!)
The order matters to avoid false positives:
- Deno - Check
globalThis.Deno?.versionfirst (Deno has aprocessobject for compatibility) - Bun - Check
globalThis.Bun?.versionsecond (Bun also has aprocessobject) - Node.js - Check
globalThis.process?.versions?.nodethird (must be specific to avoid Deno/Bun) - Browser - Check
globalThis.window && globalThis.documentas fallback
Runtime Properties
runtimeEnv: TRuntimeType- Returns 'node' | 'deno' | 'bun' | 'browser'isNode: boolean- True only for Node.js (excludes Deno and Bun)isDeno: boolean- True only for Deno runtimeisBun: boolean- True only for Bun runtimeisBrowser: boolean- True only for browser environment
Version Getters
nodeVersion: string- Node.js version (returns 'undefined' in other runtimes)denoVersion: string- Deno version (returns 'undefined' in other runtimes)bunVersion: string- Bun version (returns 'undefined' in other runtimes)
Module Loading API
New: getSafeModuleFor()
Flexible module loading that supports runtime targeting:
// Load for specific runtime
await env.getSafeModuleFor('node', 'path')
// Load for multiple runtimes
await env.getSafeModuleFor(['node', 'deno'], 'path')
// Load for all server-side runtimes (shorthand for ['node', 'deno', 'bun'])
await env.getSafeModuleFor('server', 'path')
// Browser loading requires getFunction
await env.getSafeModuleFor('browser', 'url', () => window.someLibrary)
Parameters:
target: TRuntimeTarget | TRuntimeTarget[]- Runtime(s) to load formoduleNameOrUrl: string- Module name or URLgetFunction?: () => any- Required for browser module loading
Returns: Module or undefined if runtime doesn't match
Legacy Methods (Still Supported)
getSafeNodeModule()- Now works for Node.js, Deno, and BungetSafeWebModule()- Browser-only module loading
Key Implementation Details
- Dynamic module loading using Function constructor for server-side modules
- Script tag injection for browser module loading with duplicate prevention
- OS detection uses the native 'os' module loaded dynamically (works in Node.js, Deno, Bun)
- All detection uses
globalThisfor robustness across environments
Testing Approach
- Tests use @git.zone/tstest with tap-based testing across all runtimes
test.node.ts- Node.js specific tests, verifies it's not confused with Deno/Buntest.deno.ts- Deno runtime teststest.bun.ts- Bun runtime teststest.chrome.ts- Browser environment tests- Each test file verifies proper runtime detection and module loading
Important Notes
- The
getSafeNodeModuleuses dynamic import via Function constructor to avoid bundler issues - Browser module loading tracks loaded scripts to prevent duplicate loads
- All OS detection methods are async and return false in browser environments
- The package is isomorphic and designed for use in all four runtime contexts
- Runtime detection checks globals in specific order to avoid false positives from compatibility layers