Here's the README.md for your runtime package:
Environment-aware code execution for Node.js, Deno, Bun, and browsers
- 🌐 Universal - Works in Node.js, Deno, Bun, and browsers
- ⚡ Dual APIs - Both async (
runtime) and sync (runtimeSync) versions - 🔧 Extensible - Add custom environment detectors
- 🛡 Type Safe - Full TypeScript support
- 📦 Zero Dependencies - Lightweight and self-contained
- 🔍 Smart Detection - Accurate environment identification
-
Deno:
deno add jsr:@dep/runtime
-
Node.js (18+):
npx jsr add @dep/runtime
-
Bun:
bunx jsr add @dep/runtime
Then import as an ES module:
import { runtime, runtimeSync, isNode, isDeno } from '@dep/runtime';import { isNode, isDeno, isBun, isBrowser } from '@dep/runtime';
console.log(isNode()); // true in Node.js
console.log(isDeno()); // true in Deno
console.log(isBun()); // true in Bun
console.log(isBrowser()); // true in browsersimport { runtime } from '@dep/runtime';
const result = await runtime({
node: () => 'Running in Node.js 🟢',
deno: () => 'Running in Deno 🦕',
bun: () => 'Running in Bun 🧁',
browser: () => 'Running in browser 🌐',
});
console.log(result); // Output depends on current environmentimport { runtimeSync } from '@dep/runtime';
const result = runtimeSync({
node: () => 42,
deno: () => 100,
browser: () => 'Browser value',
});
console.log(result); // Returns appropriate value synchronouslyimport { runtime } from '@dep/runtime';
// Add custom environment detectors
const result = await runtime(
{
cloudflare: () => 'Running in Cloudflare Workers ⚡',
electron: () => 'Running in Electron 🔌',
},
{
cloudflare: () => typeof WebSocketPair !== 'undefined',
electron: () =>
typeof process !== 'undefined' && process.versions?.electron,
}
);import { runtimeSync } from '@dep/runtime';
// runtimeSync handles both sync and async runners
const result = runtimeSync({
node: async () => await fetchData(), // Returns Promise
deno: () => 'sync value', // Returns string
browser: () => 42, // Returns number
});Asynchronously executes the appropriate runner for the current environment.
Synchronously executes the appropriate runner (handles both sync and async functions).
isNode()- Detects Node.js runtimeisDeno()- Detects Deno runtimeisBun()- Detects Bun runtimeisBrowser()- Detects browser environment
MIT License – see LICENSE for details.
Author: Estarlin R (estarlincito.com)