Author: César Casas LinkedIn: César Casas Website: s42core.com
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.
s42-core
enables the creation of scalable and modular applications with independent microservices or cells. This architecture ensures maintainability and simplifies updates.
Applications built with s42-core
benefit from exceptional performance, leveraging the speed of Bun.js and efficient design patterns.
Supports Server-Sent Events (SSE) for real-time updates, making it ideal for notifications, live data feeds, and collaborative applications.
Easily manage worker processes, enabling efficient utilization of multicore systems, with support for development features like file watching.
Provides utilities for managing Redis and MongoDB connections and operations, streamlining data management in modern applications.
Detailed documentation for each class and module is available:
- CLUSTER.md
- CONTROLLER.md
- DEPENDENCIES.md
- EVENTSDOMAIN.md
- JSONPARSE.md
- MONGODB.md
- REDISDB.md
- ROUTECONTROLLERS.md
- SSE.md
- TEST.md
Install s42-core
using your preferred package manager:
npm install s42-core
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);
});
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 });
});
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 });
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();
});
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);
});
s42-core
is licensed under the MIT License. See the LICENSE file for more details.
Powered by César Casas - LinkedIn.