Skip to content

s42core is a Node.js library designed to facilitate rapid application development with features like clustering, event domains, Redis and MongoDB access, dependency management, and routing controllers. Built with TypeScript for simplicity and high performance.

Notifications You must be signed in to change notification settings

stock42/s42-core

Repository files navigation

s42-core

Author: César Casas LinkedIn: César Casas Website: s42core.com

Overview

s42-core is a powerful and flexible library built on Bun.js, designed to simplify the development of applications, especially those using microservices and cell-based architectures. This library supports the creation of modular and reusable software components and streamlines the implementation of high-performance monorepos.

Key Features

Microservices and Cells

s42-core enables the creation of scalable and modular applications with independent microservices or cells. This architecture ensures maintainability and simplifies updates.

High Performance

Applications built with s42-core benefit from exceptional performance, leveraging the speed of Bun.js and efficient design patterns.

Real-time Communication

Supports Server-Sent Events (SSE) for real-time updates, making it ideal for notifications, live data feeds, and collaborative applications.

Cluster Management

Easily manage worker processes, enabling efficient utilization of multicore systems, with support for development features like file watching.

Integrated Redis and MongoDB Support

Provides utilities for managing Redis and MongoDB connections and operations, streamlining data management in modern applications.

Documentation

Detailed documentation for each class and module is available:


Installation

Install s42-core using your preferred package manager:

npm install s42-core

Usage Examples

Using EventsDomain for Microservices Communication

The EventsDomain class enables seamless event-based communication between microservices. For example, you can use it to emit an event from a user registration service and listen to it in an email notification service.

import { EventsDomain, RedisClient } from 's42-core';

const redisInstance = RedisClient.getInstance('redis://localhost:6379');
const eventsDomain = EventsDomain.getInstance(redisInstance, 'service-uuid');

// Emit an event
eventsDomain.emitEvent('user.registered', {
  email: '[email protected]',
  name: 'John Doe',
});

// Listen to an event
eventsDomain.listenEvent('user.registered', (payload) => {
  console.info('User registered:', payload);
});

Creating Controllers

Controllers handle HTTP requests and middleware. Here’s an example of creating a simple controller:

import { Controller } from 's42-core';

const userController = new Controller('POST', '/users', async (req, res) => {
  const userData = req.body;
  console.info('User data received:', userData);
  res.json({ success: true, data: userData });
});

Integrating Controllers with RouteControllers

RouteControllers organize and manage multiple controllers efficiently:

import { RouteControllers, Controller } from 's42-core';

const healthController = new Controller('GET', '/health', async (req, res) => {
  res.text('OK');
});

const router = new RouteControllers([userController, healthController]);

// Use the router in your server
server.start({ RouteControllers: router });

Server-Sent Events (SSE)

Easily implement real-time communication with the SSE class:

import { SSE, Controller } from 's42-core';

const sseController = new Controller('GET', '/events', async (req) => {
  const sse = new SSE(req);
  setInterval(() => {
    sse.send({ eventName: 'time', eventPayload: { time: new Date().toISOString() } });
  }, 1000);
  return sse.getResponse();
});

Cluster Management

The Cluster class simplifies worker process management:

import { Cluster } from 's42-core';

const cluster = new Cluster({ name: 'example-cluster', maxCPU: 4, watch: true });

cluster.start('./worker.js', (error) => {
  if (error) console.error('Cluster failed:', error);
});

cluster.onWorkerMessage((message) => {
  console.info('Message from worker:', message);
});

License

s42-core is licensed under the MIT License. See the LICENSE file for more details.


Powered by César Casas - LinkedIn.

About

s42core is a Node.js library designed to facilitate rapid application development with features like clustering, event domains, Redis and MongoDB access, dependency management, and routing controllers. Built with TypeScript for simplicity and high performance.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published