UNPKG

@duongtrungnguyen/nestro

Version:
81 lines 3.48 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index); var gateway_service_exports = {}; __export(gateway_service_exports, { GatewayService: () => GatewayService }); module.exports = __toCommonJS(gateway_service_exports); var import_common = require("@nestjs/common"); var import_route_handle = require("./route-handle.service"); var import_http_proxy = require("./http-proxy.service"); var import_ws_proxy = require("./ws-proxy.service"); var import_utils = require("../utils"); let GatewayService = class { constructor(httpProxyService, wsProxyService, routeHandleService) { this.httpProxyService = httpProxyService; this.wsProxyService = wsProxyService; this.routeHandleService = routeHandleService; } async proxyRequest(req, res) { const routeConfig = this.routeHandleService.findMatchingRoute(req.path); if (!routeConfig) { res.status(import_common.HttpStatus.NOT_FOUND).json({ message: "Proxy failed because route not found", timestamp: (/* @__PURE__ */ new Date()).toISOString(), statusCode: import_common.HttpStatus.NOT_FOUND }); return; } const isAllowed = await this.routeHandleService.checkGuards(routeConfig.guards, req, res); if (!isAllowed) return; if ((0, import_utils.isSocketRequest)(req)) { return await this.wsProxyService.proxyRequest(req, res, routeConfig); } await this.httpProxyService.proxyRequest(req, res, routeConfig); } executeWithOptions(req, res, options) { if ((0, import_utils.isSocketRequest)(req)) { this.wsProxyService.handleProxy(req, res, options); return; } this.httpProxyService.handleProxy(req, res, { buffer: this.httpProxyService.getRequestBuffer(req), ...options }); } }; GatewayService = __decorateClass([ (0, import_common.Injectable)(), __decorateParam(0, (0, import_common.Inject)(import_http_proxy.HttpProxyService)), __decorateParam(1, (0, import_common.Inject)(import_ws_proxy.WsProxyService)), __decorateParam(2, (0, import_common.Inject)(import_route_handle.RouteHandleService)) ], GatewayService); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { GatewayService }); //# sourceMappingURL=gateway.service.js.map