|
| 1 | +import constants from './constants' |
| 2 | + |
| 3 | +import { Route } from '@secjs/utils/src/Classes/Route' |
| 4 | +import { SecRequest } from './Context/Request/SecRequest' |
| 5 | +import { SecResponse } from './Context/Response/SecResponse' |
| 6 | +import { createServer, IncomingMessage, Server } from 'http' |
| 7 | +import { InternalRouteContract, SecHandlerContract } from '@secjs/contracts' |
| 8 | + |
| 9 | +export class SecJS { |
| 10 | + private nodeServer: Server |
| 11 | + private routeUtils = new Route() |
| 12 | + private routes: InternalRouteContract[] = [] |
| 13 | + |
| 14 | + private async getBody(request: IncomingMessage): Promise<any> { |
| 15 | + const buffers = [] |
| 16 | + |
| 17 | + for await (const chunk of request) { |
| 18 | + buffers.push(chunk) |
| 19 | + } |
| 20 | + |
| 21 | + if (!buffers.length) return {} |
| 22 | + |
| 23 | + return JSON.parse(Buffer.concat(buffers).toString()) |
| 24 | + } |
| 25 | + |
| 26 | + private getRoute(url: string, method: string) { |
| 27 | + return ( |
| 28 | + this.routes.find( |
| 29 | + route => |
| 30 | + route.matcher.test(this.routeUtils.removeQueryParams(url)) && |
| 31 | + route.method === method, |
| 32 | + ) || constants.DEFAULT_ROUTE |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + private createRouteHandler( |
| 37 | + path: string, |
| 38 | + method: string, |
| 39 | + secHandler: SecHandlerContract, |
| 40 | + ): void { |
| 41 | + this.routes.push({ |
| 42 | + path, |
| 43 | + method: method.toUpperCase(), |
| 44 | + handler: secHandler, |
| 45 | + params: this.routeUtils.getParamsName(path), |
| 46 | + matcher: this.routeUtils.createMatcher(path), |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + listen(port?: number, cb?: () => void): void { |
| 51 | + this.nodeServer = createServer(async (request: any, response: any) => { |
| 52 | + const { url, method } = request |
| 53 | + |
| 54 | + const route = this.getRoute(url, method) |
| 55 | + |
| 56 | + request.route = route |
| 57 | + request.body = await this.getBody(request) |
| 58 | + |
| 59 | + return route.handler({ |
| 60 | + next: () => console.log('next'), |
| 61 | + request: new SecRequest(request), |
| 62 | + response: new SecResponse(response), |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + this.nodeServer.listen(port || constants.PORT, cb) |
| 67 | + } |
| 68 | + |
| 69 | + close(cb?: (err?: Error) => void): void { |
| 70 | + this.nodeServer.close(cb) |
| 71 | + } |
| 72 | + |
| 73 | + get(route: string, secHandler: SecHandlerContract): void { |
| 74 | + this.createRouteHandler(route, this.get.name, secHandler) |
| 75 | + } |
| 76 | + |
| 77 | + post(route: string, secHandler: SecHandlerContract): void { |
| 78 | + this.createRouteHandler(route, this.post.name, secHandler) |
| 79 | + } |
| 80 | + |
| 81 | + put(route: string, secHandler: SecHandlerContract): void { |
| 82 | + this.createRouteHandler(route, this.put.name, secHandler) |
| 83 | + } |
| 84 | + |
| 85 | + delete(route: string, secHandler: SecHandlerContract): void { |
| 86 | + this.createRouteHandler(route, this.delete.name, secHandler) |
| 87 | + } |
| 88 | +} |
0 commit comments