Platform Library
The@bitwarden/platform library represents the public API of the Platform team at Bitwarden. It provides foundational services, abstractions, and utilities used across all Bitwarden JavaScript projects.
Overview
The platform library serves as the foundation for:- Storage abstractions - Unified interfaces for disk and memory storage
- Messaging services - Cross-component communication infrastructure
- Crypto services - Cryptographic operations and key management
- Background sync - Scheduled synchronization capabilities
- Browser utilities - Platform detection and browser-specific functionality
Installation
Key Components
Background Sync Service
TheBackgroundSyncService manages periodic background synchronization for browser extensions.
Location: libs/platform/src/background-sync/background-sync.service.ts
- Configurable sync intervals (default: 5 minutes / 300,000ms)
- Integration with task scheduler infrastructure
- Automatic task registration and management
Browser Detection
Utilities for detecting browser-specific environments. Location:libs/platform/src/services/browser-service.ts
Platform Abstractions
The platform library works closely with abstractions defined inlibs/common/src/platform/abstractions/:
Storage Service
Location:libs/common/src/platform/abstractions/storage.service.ts
Provides unified storage interfaces:
@bitwarden/storage-core:
ObservableStorageService- Storage with observable updatesAbstractStorageService- Base storage interfaceStorageUpdate- Storage update eventsStorageUpdateType- Types of storage updates
Messaging Service
Location:libs/common/src/platform/abstractions/messaging.service.ts
Enables cross-component messaging:
Common Platform Services
Additional platform abstractions include:- App ID Service - Application identifier management
- Broadcaster Service - Event broadcasting system
- Config Service - Configuration management
- Environment Service - Environment detection and settings
- I18n Service - Internationalization support
- Log Service - Logging infrastructure
- Platform Utils Service - Cross-platform utilities
- System Service - System-level operations
- Translation Service - Text translation
- Validation Service - Data validation
Platform Services
Service implementations are located inlibs/common/src/platform/services/:
Core Services
- App ID Service (
app-id.service.ts) - Manages application identifiers - Console Log Service (
console-log.service.ts) - Console logging implementation - Default Broadcaster Service (
default-broadcaster.service.ts) - Event broadcasting - Default Environment Service (
default-environment.service.ts) - Environment management - Container Service (
container.service.ts) - Dependency injection container
Configuration Services
Location:libs/common/src/platform/services/config/
- Config API Service (
config-api.service.ts) - Server configuration API - Default Config Service (
default-config.service.ts) - Configuration management
FIDO2 Services
Location:libs/common/src/platform/services/fido2/
- FIDO2 Authenticator Service - WebAuthn authenticator
- FIDO2 Client Service - WebAuthn client implementation
Platform Utilities
Misc Utilities
Location:libs/common/src/platform/misc/
Utility functions for common operations:
- Compare Values (
compare-values.ts) - Value comparison utilities - Convert Values (
convert-values.ts) - Type conversion helpers - Flags (
flags.ts) - Feature flag management - Lazy (
lazy.ts) - Lazy initialization patterns - Range with Default (
range-with-default.ts) - Range validation - RxJS Operators (
rxjs-operators.ts) - Custom RxJS operators - Safe URLs (
safe-urls.ts) - URL sanitization - Utils (
utils.ts) - General utility functions
Storage Architecture
The platform library integrates with the storage system through@bitwarden/storage-core:
Storage Locations
- Disk Storage - Persistent storage (local/session storage on web)
- Memory Storage - In-memory cache
- Client-Specific - Platform-specific storage locations
Storage Services
Location:libs/storage-core/src/
- Memory Storage Service (
memory-storage.service.ts) - In-memory storage - Serialized Memory Storage (
serialized-memory-storage.service.ts) - JSON-serialized memory storage - Storage Service Provider (
storage-service.provider.ts) - Storage service factory
Integration with State Management
The platform library provides storage abstractions used by the state management system:Scheduling and Background Tasks
Location:libs/common/src/platform/scheduling/
The platform includes task scheduling infrastructure:
- Task Scheduler Service - Manages scheduled tasks
- Scheduled Task Names - Predefined task identifiers
- Background sync integration
IPC (Inter-Process Communication)
Location:libs/common/src/platform/ipc/
Provides IPC mechanisms for desktop applications:
- Process communication
- Message passing between main and renderer processes
Best Practices
Storage Usage
-
Use appropriate storage locations:
diskfor persistent datamemoryfor temporary/cached data- Client-specific locations when needed (e.g.,
disk-localfor web)
-
Leverage storage abstractions:
- Use
ObservableStorageServicefor reactive storage updates - Subscribe to storage change events when needed
- Use
Background Sync
-
Set appropriate intervals:
- Default 5 minutes is suitable for most cases
- Consider longer intervals for battery-sensitive operations
- Minimum interval enforced: 1ms (falls back to default)
-
Handle sync failures gracefully:
- Implement error handling in sync callbacks
- Log sync failures for debugging
Messaging
- Use MessageSender for events:
- Prefer
MessageSenderover state for event notifications - State observables don’t guarantee emissions for equal values
- Prefer
Related Libraries
- State Management - State provider framework
- Components - UI component library
@bitwarden/storage-core- Storage core implementations@bitwarden/messaging- Messaging infrastructure
Source Code
- Platform Library:
libs/platform/ - Platform Abstractions:
libs/common/src/platform/abstractions/ - Platform Services:
libs/common/src/platform/services/ - Storage Core:
libs/storage-core/