Skip to content
This repository has been archived by the owner on Jul 10, 2022. It is now read-only.

A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

License

Notifications You must be signed in to change notification settings

chubbyjs-legacy/chubbyjs-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chubbyjs-framework

CI Coverage Status Infection MSI npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

Requirements

Suggest

Http-Message

Router

Server

Installation

Through NPM as @chubbyjs/chubbyjs-framework.

npm i @chubbyjs/[email protected] \
    @chubbyjs/[email protected] \
    @chubbyjs/[email protected]

Usage

App

import PathToRegexpRouteMatcher from '@chubbyjs/chubbyjs-framework-router-path-to-regexp/dist/PathToRegexpRouteMatcher';
import Application from '@chubbyjs/chubbyjs-framework/dist/Application';
import ErrorMiddleware from '@chubbyjs/chubbyjs-framework/dist/Middleware/ErrorMiddleware';
import RouteMatcherMiddleware from '@chubbyjs/chubbyjs-framework/dist/Middleware/RouteMatcherMiddleware';
import CallbackRequestHandler from '@chubbyjs/chubbyjs-framework/dist/RequestHandler/CallbackRequestHandler';
import Route from '@chubbyjs/chubbyjs-framework/dist/Router/Route';
import Routes from '@chubbyjs/chubbyjs-framework/dist/Router/Routes';
import ResponseFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ResponseFactory';
import ResponseInterface from '@chubbyjs/psr-http-message/dist/ResponseInterface';
import ServerRequestInterface from '@chubbyjs/psr-http-message/dist/ServerRequestInterface';

const responseFactory = new ResponseFactory();

const app = new Application([
    new ErrorMiddleware(responseFactory, true),
    new RouteMatcherMiddleware(
        new PathToRegexpRouteMatcher(new Routes([
            Route.get('/hello/:name([a-z]+)', 'hello', new CallbackRequestHandler(
                async (request: ServerRequestInterface): Promise<ResponseInterface> => {
                    const response = responseFactory.createResponse(200);
                    response.getBody().end(`Hello, ${request.getAttribute('name')}`);

                    return response;
                },
            )),
        ])),
        responseFactory,
    ),
]);

Server

Node

npm i @chubbyjs/[email protected]
import Application from '@chubbyjs/chubbyjs-framework/dist/Application';
import ServerRequestFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ServerRequestFactory';
import StreamFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/StreamFactory';
import UriFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/UriFactory';
import NodeResponseEmitter from '@chubbyjs/chubbyjs-node-psr-http-message-bridge/dist/NodeResponseEmitter';
import PsrRequestFactory from '@chubbyjs/chubbyjs-node-psr-http-message-bridge/dist/PsrRequestFactory';
import { createServer, IncomingMessage, ServerResponse } from 'http';

const app = new Application([...]);

const psrRequestFactory = new PsrRequestFactory(
    new ServerRequestFactory(),
    new UriFactory(),
    new StreamFactory(),
);

const nodeResponseEmitter = new NodeResponseEmitter();

const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
    const serverRequest = psrRequestFactory.create(req);
    const response = await app.handle(serverRequest);

    nodeResponseEmitter.emit(response, res);
});

server.listen(8080, '0.0.0.0');

uWebSockets.js

npm i @chubbyjs/[email protected]
import Application from '@chubbyjs/chubbyjs-framework/dist/Application';
import ServerRequestFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/ServerRequestFactory';
import StreamFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/StreamFactory';
import UriFactory from '@chubbyjs/chubbyjs-http-message/dist/Factory/UriFactory';
import PsrRequestFactory from '@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/dist/PsrRequestFactory';
import UwebsocketResponseEmitter from '@chubbyjs/chubbyjs-uwebsockets-psr-http-message-bridge/dist/UwebsocketResponseEmitter';
import { HttpRequest, HttpResponse } from 'uWebSockets.js';

const app = new Application([...]);

const psrRequestFactory = new PsrRequestFactory(
    new ServerRequestFactory(),
    new UriFactory(),
    new StreamFactory(),
);

const uwebsocketResponseEmitter = new UwebsocketResponseEmitter();

require('uWebSockets.js')
    .App()
    .any('/*', async (res: HttpResponse, req: HttpRequest) => {
        const serverRequest = psrRequestFactory.create(req, res);
        const response = await app.handle(serverRequest);

        uwebsocketResponseEmitter.emit(response, res);
    })
    .listen('0.0.0.0', 8080, (listenSocket: unknown) => {
        if (listenSocket) {
            console.log('Listening to port 0.0.0.0:8080');
        }
    });

Copyright

Dominik Zogg 2021

About

A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published