UNPKG

4.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SocketModule = void 0;
4const guards_consumer_1 = require("@nestjs/core/guards/guards-consumer");
5const guards_context_creator_1 = require("@nestjs/core/guards/guards-context-creator");
6const load_adapter_1 = require("@nestjs/core/helpers/load-adapter");
7const interceptors_consumer_1 = require("@nestjs/core/interceptors/interceptors-consumer");
8const interceptors_context_creator_1 = require("@nestjs/core/interceptors/interceptors-context-creator");
9const pipes_consumer_1 = require("@nestjs/core/pipes/pipes-consumer");
10const pipes_context_creator_1 = require("@nestjs/core/pipes/pipes-context-creator");
11const iterare_1 = require("iterare");
12const constants_1 = require("./constants");
13const exception_filters_context_1 = require("./context/exception-filters-context");
14const ws_context_creator_1 = require("./context/ws-context-creator");
15const ws_proxy_1 = require("./context/ws-proxy");
16const socket_server_provider_1 = require("./socket-server-provider");
17const sockets_container_1 = require("./sockets-container");
18const web_sockets_controller_1 = require("./web-sockets-controller");
19class SocketModule {
20 constructor() {
21 this.socketsContainer = new sockets_container_1.SocketsContainer();
22 }
23 register(container, applicationConfig, graphInspector, appOptions, httpServer) {
24 this.applicationConfig = applicationConfig;
25 this.appOptions = appOptions;
26 this.httpServer = httpServer;
27 const contextCreator = this.getContextCreator(container);
28 const serverProvider = new socket_server_provider_1.SocketServerProvider(this.socketsContainer, applicationConfig);
29 this.webSocketsController = new web_sockets_controller_1.WebSocketsController(serverProvider, applicationConfig, contextCreator, graphInspector, this.appOptions);
30 const modules = container.getModules();
31 modules.forEach(({ providers }, moduleName) => this.connectAllGateways(providers, moduleName));
32 }
33 connectAllGateways(providers, moduleName) {
34 (0, iterare_1.iterate)(providers.values())
35 .filter(wrapper => wrapper && !wrapper.isNotMetatype)
36 .forEach(wrapper => this.connectGatewayToServer(wrapper, moduleName));
37 }
38 connectGatewayToServer(wrapper, moduleName) {
39 const { instance, metatype } = wrapper;
40 const metadataKeys = Reflect.getMetadataKeys(metatype);
41 if (!metadataKeys.includes(constants_1.GATEWAY_METADATA)) {
42 return;
43 }
44 if (!this.isAdapterInitialized) {
45 this.initializeAdapter();
46 }
47 this.webSocketsController.connectGatewayToServer(instance, metatype, moduleName, wrapper.id);
48 }
49 async close() {
50 if (!this.applicationConfig) {
51 return;
52 }
53 const adapter = this.applicationConfig.getIoAdapter();
54 if (!adapter) {
55 return;
56 }
57 const servers = this.socketsContainer.getAll();
58 await Promise.all((0, iterare_1.iterate)(servers.values())
59 .filter(({ server }) => server)
60 .map(async ({ server }) => adapter.close(server)));
61 await adapter?.dispose();
62 this.socketsContainer.clear();
63 }
64 initializeAdapter() {
65 const adapter = this.applicationConfig.getIoAdapter();
66 if (adapter) {
67 this.isAdapterInitialized = true;
68 return;
69 }
70 const { IoAdapter } = (0, load_adapter_1.loadAdapter)('@nestjs/platform-socket.io', 'WebSockets', () => require('@nestjs/platform-socket.io'));
71 const ioAdapter = new IoAdapter(this.httpServer);
72 this.applicationConfig.setIoAdapter(ioAdapter);
73 this.isAdapterInitialized = true;
74 }
75 getContextCreator(container) {
76 return new ws_context_creator_1.WsContextCreator(new ws_proxy_1.WsProxy(), new exception_filters_context_1.ExceptionFiltersContext(container), new pipes_context_creator_1.PipesContextCreator(container), new pipes_consumer_1.PipesConsumer(), new guards_context_creator_1.GuardsContextCreator(container), new guards_consumer_1.GuardsConsumer(), new interceptors_context_creator_1.InterceptorsContextCreator(container), new interceptors_consumer_1.InterceptorsConsumer());
77 }
78}
79exports.SocketModule = SocketModule;