UNPKG

@duongtrungnguyen/nestro

Version:
122 lines 4.97 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 route_handle_service_exports = {}; __export(route_handle_service_exports, { RouteHandleService: () => RouteHandleService }); module.exports = __toCommonJS(route_handle_service_exports); var import_common = require("@nestjs/common"); var import_rxjs = require("rxjs"); var import_core = require("@nestjs/core"); var import_path_to_regexp = require("path-to-regexp"); var import_constants = require("../constants"); let RouteHandleService = class { constructor(routesConfig, globalGuards, moduleRef) { this.routesConfig = routesConfig; this.globalGuards = globalGuards; this.moduleRef = moduleRef; } findMatchingRoute(path) { return this.routesConfig.find((config) => (0, import_path_to_regexp.match)(config.route)(path)); } async checkGuards(guards = [], req, res) { const allGuards = [...this.globalGuards, ...guards]; for (let i = 0; i < allGuards.length; i++) { const guard = allGuards[i]; const isGlobalGuard = i < this.globalGuards.length; const shouldRun = typeof guard === "function" ? true : isGlobalGuard || this.shouldApplyHook(req, guard.includes, guard.excludes); if (!shouldRun) continue; const instance = typeof guard === "function" ? guard : guard.instance; const context = this.createExecutionContext(req, res); const result = await this.executeGuard(instance, context); if (!result) { if ("switchToWs" in context) { const socket = context.switchToWs().getClient(); socket.emit("error", { message: "Unauthorized", timestamp: (/* @__PURE__ */ new Date()).toISOString() }); socket.disconnect(); } else { res.status(401).json({ message: "Unauthorized", timestamp: (/* @__PURE__ */ new Date()).toISOString(), statusCode: import_common.HttpStatus.UNAUTHORIZED }); } return false; } } return true; } shouldApplyHook(req, includes, excludes) { const isSocket = "handshake" in req; const method = isSocket ? "WS" : import_common.RequestMethod[req.method]; const url = isSocket ? req.handshake.url : req.url; const isMatch = (routes, defaultV = true) => routes?.some((route) => (route.method === method || route.method === import_common.RequestMethod.ALL || method === "WS") && (0, import_path_to_regexp.match)(route.path)(url)) ?? defaultV; return isMatch(includes) && !isMatch(excludes, false); } async executeGuard(Guard, context) { const instance = typeof Guard === "function" ? Guard : await this.moduleRef.resolve(Guard); try { const result = await instance.canActivate(context); if (result instanceof import_rxjs.Observable) { return await (0, import_rxjs.lastValueFrom)(result); } return result; } catch { return false; } } createExecutionContext(req, res) { const isSocket = "handshake" in req; if (isSocket) { return { switchToWs: () => ({ getClient: () => req }) }; } return { switchToHttp: () => ({ getRequest: () => req, getResponse: () => res }) }; } }; RouteHandleService = __decorateClass([ (0, import_common.Injectable)(), __decorateParam(0, (0, import_common.Inject)(import_constants.PROXY_ROUTES_CONFIG)), __decorateParam(1, (0, import_common.Inject)(import_constants.GLOBAL_GUARDS)), __decorateParam(2, (0, import_common.Inject)(import_core.ModuleRef)) ], RouteHandleService); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RouteHandleService }); //# sourceMappingURL=route-handle.service.js.map