Cordis Meta-Framework

4.0.0-rc.3 · active · verified Sun Apr 19

Cordis is an actively developed meta-framework for building modern applications, emphasizing a concept of "Spatiotemporal Composability" and a highly modular, plugin-based architecture. Currently in release candidate phase (v4.0.0-rc.3), it aims to provide a structured and opinionated layer on top of underlying JavaScript runtime environments, facilitating the development of complex systems by handling common concerns like service orchestration, lifecycle management, and resource allocation. Its "everything is a plugin" philosophy promotes high cohesion, low coupling, and extensive extensibility, allowing developers to customize and extend core functionalities through well-defined plugin interfaces. The framework is written in TypeScript, providing strong type safety across its API. While an exact release cadence isn't specified, its `cordiverse` organization shows continuous, active development across its ecosystem. It differentiates itself by offering a robust context and service management system designed for flexible application composition, particularly useful in environments requiring dynamic loading and unloading of modules or services.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a Cordis application, registering a custom service with dependency injection, and utilizing context for configuration. It shows how to define a service, make it available within the context, and instantiate it, including creating a child context with overridden configurations.

import { Context, Service } from 'cordis';

interface MyConfig {
  greeting: string;
}

class GreeterService extends Service {
  static inject = ['config']; // Declare dependencies

  constructor(ctx: Context, public config: MyConfig) {
    super(ctx, 'greeter'); // Register service as 'greeter'
    this.logger.info(`GreeterService initialized with config: ${this.config.greeting}`);
  }

  greet(name: string): string {
    return `${this.config.greeting}, ${name}!`;
  }
}

// Main application context
async function bootstrap() {
  const app = new Context();

  // Register the configuration for the greeter service
  app.provide('config', { greeting: 'Hello from Cordis' });

  // Register the GreeterService as a plugin
  app.plugin(GreeterService);

  // Access the greeter service from the context
  const greeter = app.greeter as GreeterService; // Type assertion for convenience
  if (greeter) {
    console.log(greeter.greet('World'));
  } else {
    console.error('Greeter service not found!');
  }

  // Demonstrate a child context with different config
  const childCtx = app.createContext();
  childCtx.provide('config', { greeting: 'Greetings, developer' });
  childCtx.plugin(GreeterService);
  const childGreeter = childCtx.greeter as GreeterService;
  if (childGreeter) {
    console.log(childGreeter.greet('Cordis User'));
  }

  await app.start();
  console.log('Cordis application started successfully.');
  await app.stop();
  console.log('Cordis application stopped.');
}

bootstrap().catch(console.error);

view raw JSON →