At a Glance
- Why
- Modern development requires running code across different environments (Node.js, browsers, Deno, etc.). Managing execution boundaries manually is error-prone and creates tight coupling.
- Benefit
- Provides a unified abstraction layer that handles runtime differences automatically, enabling truly portable code that works across environments without modification.
- For
- Library authors, tool builders, and teams building portable systems that need to work across multiple JavaScript/TypeScript runtimes without environment-specific code.
- Use
- Import the context layer in your code, write against its unified API, and deploy to any supported runtime. The layer handles adaptation and compatibility concerns.
Architecture
Context Diagram
How context interacts with the surrounding environment:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Developer β
β (writes portable code) β
βββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββ
β
β uses unified API
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β context β
β (execution boundary) β
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Adapters β β Runtime β β Capability β β
β β β β Detection β β Registry β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββ¬βββββββββββββββ¬βββββββββββββββ¬βββββββββββββββ¬ββββββββββββ
β β β β
βΌ βΌ βΌ βΌ
ββββββββββ ββββββββββ ββββββββββ ββββββββββ
β Node.jsβ βBrowser β β Deno β β Bun β
ββββββββββ ββββββββββ ββββββββββ ββββββββββ
Internal Architecture
Key modules inside context:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β context Core β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β β β Runtime Detection β β β β β’ Identifies current environment β β β β β’ Feature capability checks β β β β β’ Version compatibility matrix β β β βββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ β β β β β βΌ β β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β β β Adapter Layer β β β β ββββββββββββ ββββββββββββ ββββββββββββ β β β β β I/O β β FS β β HTTP β β β β β β Adapter β β Adapter β β Adapter β ... β β β β ββββββββββββ ββββββββββββ ββββββββββββ β β β βββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ β β β β β βΌ β β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β β β Unified API Interface β β β β β’ Consistent method signatures β β β β β’ Standardized error handling β β β β β’ Promise-based async operations β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
How to Use
-
Install the package
Add context to your project using your preferred package manager:
npm install @gitbrainlab/context -
Import the context layer
Import the unified API in your code:
import { context } from '@gitbrainlab/context'; -
Write portable code
Use the context API instead of runtime-specific APIs. For example, use
context.fs.readFile()instead of Node'sfs.readFileor Deno'sDeno.readTextFile. - Deploy anywhere Your code now works across Node.js, browsers, Deno, and other supported runtimes without modification.
- Extend with adapters Add custom adapters for specialized functionality or new runtime environments as needed.