WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

dep-ts/runtime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Here's the README.md for your runtime package:

@dep/runtime 🚀

Environment-aware code execution for Node.js, Deno, Bun, and browsers

JSR version

Features ✨

  • 🌐 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

Installation 📦

  • 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';

Usage 🎯

Basic Environment Detection

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 browsers

Async Runtime Execution

import { 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 environment

Sync Runtime Execution

import { runtimeSync } from '@dep/runtime';

const result = runtimeSync({
  node: () => 42,
  deno: () => 100,
  browser: () => 'Browser value',
});

console.log(result); // Returns appropriate value synchronously

Custom Environments

import { 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,
  }
);

Mixed Sync/Async Runners

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
});

API Reference 🧩

runtime(runners, customEnvs?)

Asynchronously executes the appropriate runner for the current environment.

runtimeSync(runners, customEnvs?)

Synchronously executes the appropriate runner (handles both sync and async functions).

Environment Detectors

  • isNode() - Detects Node.js runtime
  • isDeno() - Detects Deno runtime
  • isBun() - Detects Bun runtime
  • isBrowser() - Detects browser environment

License 📄

MIT License – see LICENSE for details.

Author: Estarlin R (estarlincito.com)