This package has been deprecated

Author message:

use @chubbyts/chubbyts-framework instead

@chubbyjs/chubbyjs-framework

1.2.0 • Public • Published

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/chubbyjs-framework@1.2.0 \
    @chubbyjs/chubbyjs-framework-router-path-to-regexp@1.0.1 \
    @chubbyjs/chubbyjs-http-message@1.1.1

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/chubbyjs-node-psr-http-message-bridge@1.2.3
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/chubbyjs-uwebsockets-psr-http-message-bridge@1.1.1
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

Package Sidebar

Install

npm i @chubbyjs/chubbyjs-framework

Weekly Downloads

3

Version

1.2.0

License

MIT

Unpacked Size

46.5 kB

Total Files

57

Last publish

Collaborators

  • dominikzogg