UNPKG

@nestjs/websockets

Version:

Nest - modern, fast, powerful node.js web framework (@websockets)

131 lines (130 loc) 6.95 kB
import { CUSTOM_ROUTE_ARGS_METADATA, isEmptyArray, PARAMTYPES_METADATA, } from '@nestjs/common/internal'; import { ContextUtils, FORBIDDEN_MESSAGE, HandlerMetadataStorage, STATIC_CONTEXT, } from '@nestjs/core/internal'; import { MESSAGE_METADATA, PARAM_ARGS_METADATA } from '../constants.js'; import { WsException } from '../errors/ws-exception.js'; import { WsParamsFactory } from '../factories/ws-params-factory.js'; import { DEFAULT_CALLBACK_METADATA } from './ws-metadata-constants.js'; export class WsContextCreator { wsProxy; exceptionFiltersContext; pipesContextCreator; pipesConsumer; guardsContextCreator; guardsConsumer; interceptorsContextCreator; interceptorsConsumer; contextUtils = new ContextUtils(); wsParamsFactory = new WsParamsFactory(); handlerMetadataStorage = new HandlerMetadataStorage(); constructor(wsProxy, exceptionFiltersContext, pipesContextCreator, pipesConsumer, guardsContextCreator, guardsConsumer, interceptorsContextCreator, interceptorsConsumer) { this.wsProxy = wsProxy; this.exceptionFiltersContext = exceptionFiltersContext; this.pipesContextCreator = pipesContextCreator; this.pipesConsumer = pipesConsumer; this.guardsContextCreator = guardsContextCreator; this.guardsConsumer = guardsConsumer; this.interceptorsContextCreator = interceptorsContextCreator; this.interceptorsConsumer = interceptorsConsumer; } create(instance, callback, moduleKey, methodName, contextId = STATIC_CONTEXT, inquirerId) { const contextType = 'ws'; const { argsLength, paramtypes, getParamsMetadata } = this.getMetadata(instance, methodName, contextType); const exceptionHandler = this.exceptionFiltersContext.create(instance, callback, moduleKey, contextId, inquirerId); const pipes = this.pipesContextCreator.create(instance, callback, moduleKey, contextId, inquirerId); const guards = this.guardsContextCreator.create(instance, callback, moduleKey, contextId, inquirerId); const interceptors = this.interceptorsContextCreator.create(instance, callback, moduleKey, contextId, inquirerId); const paramsMetadata = getParamsMetadata(moduleKey); const paramsOptions = paramsMetadata ? this.contextUtils.mergeParamsMetatypes(paramsMetadata, paramtypes) : []; const fnApplyPipes = this.createPipesFn(pipes, paramsOptions); const fnCanActivate = this.createGuardsFn(guards, instance, callback, contextType); const handler = (initialArgs, args) => async () => { if (fnApplyPipes) { await fnApplyPipes(initialArgs, ...args); return callback.apply(instance, initialArgs); } return callback.apply(instance, args); }; const targetPattern = this.reflectCallbackPattern(callback); return this.wsProxy.create(async (...args) => { args.push(targetPattern); const initialArgs = this.contextUtils.createNullArray(argsLength); fnCanActivate && (await fnCanActivate(args)); return this.interceptorsConsumer.intercept(interceptors, args, instance, callback, handler(initialArgs, args), contextType); }, exceptionHandler, targetPattern); } reflectCallbackParamtypes(instance, callback) { return Reflect.getMetadata(PARAMTYPES_METADATA, instance, callback.name); } reflectCallbackPattern(callback) { return Reflect.getMetadata(MESSAGE_METADATA, callback); } createGuardsFn(guards, instance, callback, contextType) { const canActivateFn = async (args) => { const canActivate = await this.guardsConsumer.tryActivate(guards, args, instance, callback, contextType); if (!canActivate) { throw new WsException(FORBIDDEN_MESSAGE); } }; return guards.length ? canActivateFn : null; } getMetadata(instance, methodName, contextType) { const cacheMetadata = this.handlerMetadataStorage.get(instance, methodName); if (cacheMetadata) { return cacheMetadata; } const metadata = this.contextUtils.reflectCallbackMetadata(instance, methodName, PARAM_ARGS_METADATA) || DEFAULT_CALLBACK_METADATA; const keys = Object.keys(metadata); const argsLength = this.contextUtils.getArgumentsLength(keys, metadata); const paramtypes = this.contextUtils.reflectCallbackParamtypes(instance, methodName); const contextFactory = this.contextUtils.getContextFactory(contextType, instance, instance[methodName]); const getParamsMetadata = (moduleKey) => this.exchangeKeysForValues(keys, metadata, moduleKey, this.wsParamsFactory, contextFactory); const handlerMetadata = { argsLength, paramtypes, getParamsMetadata, }; this.handlerMetadataStorage.set(instance, methodName, handlerMetadata); return handlerMetadata; } exchangeKeysForValues(keys, metadata, moduleContext, paramsFactory, contextFactory) { this.pipesContextCreator.setModuleContext(moduleContext); return keys.map(key => { const { index, data, pipes: pipesCollection, schema } = metadata[key]; const pipes = this.pipesContextCreator.createConcreteContext(pipesCollection); const type = this.contextUtils.mapParamType(key); if (key.includes(CUSTOM_ROUTE_ARGS_METADATA)) { const { factory } = metadata[key]; const customExtractValue = this.contextUtils.getCustomFactory(factory, data, contextFactory); return { index, extractValue: customExtractValue, type, data, pipes, schema, }; } const numericType = Number(type); const extractValue = (...args) => paramsFactory.exchangeKeyForValue(numericType, data, args); return { index, extractValue, type: numericType, data, pipes, schema }; }); } createPipesFn(pipes, paramsOptions) { const pipesFn = async (args, ...params) => { const resolveParamValue = async (param) => { const { index, extractValue, type, data, metatype, pipes: paramPipes, schema, } = param; const value = extractValue(...params); args[index] = await this.getParamValue(value, { metatype, type, data, schema }, pipes.concat(paramPipes)); }; await Promise.all(paramsOptions.map(resolveParamValue)); }; return paramsOptions.length ? pipesFn : null; } async getParamValue(value, metadata, pipes) { return isEmptyArray(pipes) ? value : this.pipesConsumer.apply(value, metadata, pipes); } }