UNPKG

6.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.WsContextCreator = void 0;
4const constants_1 = require("@nestjs/common/constants");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const constants_2 = require("@nestjs/core/guards/constants");
7const context_utils_1 = require("@nestjs/core/helpers/context-utils");
8const handler_metadata_storage_1 = require("@nestjs/core/helpers/handler-metadata-storage");
9const constants_3 = require("../constants");
10const ws_exception_1 = require("../errors/ws-exception");
11const ws_params_factory_1 = require("../factories/ws-params-factory");
12const ws_metadata_constants_1 = require("./ws-metadata-constants");
13class WsContextCreator {
14 constructor(wsProxy, exceptionFiltersContext, pipesContextCreator, pipesConsumer, guardsContextCreator, guardsConsumer, interceptorsContextCreator, interceptorsConsumer) {
15 this.wsProxy = wsProxy;
16 this.exceptionFiltersContext = exceptionFiltersContext;
17 this.pipesContextCreator = pipesContextCreator;
18 this.pipesConsumer = pipesConsumer;
19 this.guardsContextCreator = guardsContextCreator;
20 this.guardsConsumer = guardsConsumer;
21 this.interceptorsContextCreator = interceptorsContextCreator;
22 this.interceptorsConsumer = interceptorsConsumer;
23 this.contextUtils = new context_utils_1.ContextUtils();
24 this.wsParamsFactory = new ws_params_factory_1.WsParamsFactory();
25 this.handlerMetadataStorage = new handler_metadata_storage_1.HandlerMetadataStorage();
26 }
27 create(instance, callback, moduleKey, methodName) {
28 const contextType = 'ws';
29 const { argsLength, paramtypes, getParamsMetadata } = this.getMetadata(instance, methodName, contextType);
30 const exceptionHandler = this.exceptionFiltersContext.create(instance, callback, moduleKey);
31 const pipes = this.pipesContextCreator.create(instance, callback, moduleKey);
32 const guards = this.guardsContextCreator.create(instance, callback, moduleKey);
33 const interceptors = this.interceptorsContextCreator.create(instance, callback, moduleKey);
34 const paramsMetadata = getParamsMetadata(moduleKey);
35 const paramsOptions = paramsMetadata
36 ? this.contextUtils.mergeParamsMetatypes(paramsMetadata, paramtypes)
37 : [];
38 const fnApplyPipes = this.createPipesFn(pipes, paramsOptions);
39 const fnCanActivate = this.createGuardsFn(guards, instance, callback, contextType);
40 const handler = (initialArgs, args) => async () => {
41 if (fnApplyPipes) {
42 await fnApplyPipes(initialArgs, ...args);
43 return callback.apply(instance, initialArgs);
44 }
45 return callback.apply(instance, args);
46 };
47 return this.wsProxy.create(async (...args) => {
48 const initialArgs = this.contextUtils.createNullArray(argsLength);
49 fnCanActivate && (await fnCanActivate(args));
50 return this.interceptorsConsumer.intercept(interceptors, args, instance, callback, handler(initialArgs, args), contextType);
51 }, exceptionHandler);
52 }
53 reflectCallbackParamtypes(instance, callback) {
54 return Reflect.getMetadata(constants_1.PARAMTYPES_METADATA, instance, callback.name);
55 }
56 createGuardsFn(guards, instance, callback, contextType) {
57 const canActivateFn = async (args) => {
58 const canActivate = await this.guardsConsumer.tryActivate(guards, args, instance, callback, contextType);
59 if (!canActivate) {
60 throw new ws_exception_1.WsException(constants_2.FORBIDDEN_MESSAGE);
61 }
62 };
63 return guards.length ? canActivateFn : null;
64 }
65 getMetadata(instance, methodName, contextType) {
66 const cacheMetadata = this.handlerMetadataStorage.get(instance, methodName);
67 if (cacheMetadata) {
68 return cacheMetadata;
69 }
70 const metadata = this.contextUtils.reflectCallbackMetadata(instance, methodName, constants_3.PARAM_ARGS_METADATA) || ws_metadata_constants_1.DEFAULT_CALLBACK_METADATA;
71 const keys = Object.keys(metadata);
72 const argsLength = this.contextUtils.getArgumentsLength(keys, metadata);
73 const paramtypes = this.contextUtils.reflectCallbackParamtypes(instance, methodName);
74 const contextFactory = this.contextUtils.getContextFactory(contextType, instance, instance[methodName]);
75 const getParamsMetadata = (moduleKey) => this.exchangeKeysForValues(keys, metadata, moduleKey, this.wsParamsFactory, contextFactory);
76 const handlerMetadata = {
77 argsLength,
78 paramtypes,
79 getParamsMetadata,
80 };
81 this.handlerMetadataStorage.set(instance, methodName, handlerMetadata);
82 return handlerMetadata;
83 }
84 exchangeKeysForValues(keys, metadata, moduleContext, paramsFactory, contextFactory) {
85 this.pipesContextCreator.setModuleContext(moduleContext);
86 return keys.map(key => {
87 const { index, data, pipes: pipesCollection } = metadata[key];
88 const pipes = this.pipesContextCreator.createConcreteContext(pipesCollection);
89 const type = this.contextUtils.mapParamType(key);
90 if (key.includes(constants_1.CUSTOM_ROUTE_ARGS_METADATA)) {
91 const { factory } = metadata[key];
92 const customExtractValue = this.contextUtils.getCustomFactory(factory, data, contextFactory);
93 return { index, extractValue: customExtractValue, type, data, pipes };
94 }
95 const numericType = Number(type);
96 const extractValue = (...args) => paramsFactory.exchangeKeyForValue(numericType, data, args);
97 return { index, extractValue, type: numericType, data, pipes };
98 });
99 }
100 createPipesFn(pipes, paramsOptions) {
101 const pipesFn = async (args, ...params) => {
102 const resolveParamValue = async (param) => {
103 const { index, extractValue, type, data, metatype, pipes: paramPipes, } = param;
104 const value = extractValue(...params);
105 args[index] = await this.getParamValue(value, { metatype, type, data }, pipes.concat(paramPipes));
106 };
107 await Promise.all(paramsOptions.map(resolveParamValue));
108 };
109 return paramsOptions.length ? pipesFn : null;
110 }
111 async getParamValue(value, { metatype, type, data }, pipes) {
112 return (0, shared_utils_1.isEmpty)(pipes)
113 ? value
114 : this.pipesConsumer.apply(value, { metatype, type, data }, pipes);
115 }
116}
117exports.WsContextCreator = WsContextCreator;