@nestjs/websockets 11.1.16 → 12.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adapters/index.d.ts +1 -1
- package/adapters/index.js +1 -4
- package/adapters/ws-adapter.d.ts +2 -2
- package/adapters/ws-adapter.js +10 -12
- package/constants.js +13 -16
- package/context/exception-filters-context.d.ts +3 -3
- package/context/exception-filters-context.js +7 -12
- package/context/ws-context-creator.d.ts +7 -16
- package/context/ws-context-creator.js +38 -30
- package/context/ws-metadata-constants.js +5 -8
- package/context/ws-proxy.d.ts +1 -1
- package/context/ws-proxy.js +8 -12
- package/decorators/ack.decorator.js +4 -7
- package/decorators/connected-socket.decorator.js +3 -6
- package/decorators/gateway-server.decorator.js +3 -7
- package/decorators/index.d.ts +6 -6
- package/decorators/index.js +6 -9
- package/decorators/message-body.decorator.d.ts +27 -1
- package/decorators/message-body.decorator.js +4 -7
- package/decorators/socket-gateway.decorator.d.ts +1 -1
- package/decorators/socket-gateway.decorator.js +5 -8
- package/decorators/subscribe-message.decorator.js +4 -8
- package/enums/ws-paramtype.enum.js +2 -5
- package/errors/index.d.ts +1 -1
- package/errors/index.js +1 -4
- package/errors/invalid-socket-port.exception.d.ts +1 -1
- package/errors/invalid-socket-port.exception.js +2 -6
- package/errors/ws-exception.js +6 -9
- package/exceptions/base-ws-exception-filter.d.ts +1 -1
- package/exceptions/base-ws-exception-filter.js +12 -15
- package/exceptions/index.d.ts +1 -1
- package/exceptions/index.js +1 -4
- package/exceptions/ws-exceptions-handler.d.ts +4 -4
- package/exceptions/ws-exceptions-handler.js +8 -16
- package/factories/server-and-event-streams-factory.d.ts +1 -1
- package/factories/server-and-event-streams-factory.js +5 -9
- package/factories/ws-params-factory.js +7 -11
- package/gateway-metadata-explorer.d.ts +2 -2
- package/gateway-metadata-explorer.js +15 -18
- package/index.d.ts +6 -6
- package/index.js +6 -9
- package/interfaces/gateway-metadata.interface.d.ts +1 -1
- package/interfaces/gateway-metadata.interface.js +1 -2
- package/interfaces/hooks/index.d.ts +3 -3
- package/interfaces/hooks/index.js +3 -6
- package/interfaces/hooks/on-gateway-connection.interface.js +1 -2
- package/interfaces/hooks/on-gateway-disconnect.interface.d.ts +1 -1
- package/interfaces/hooks/on-gateway-disconnect.interface.js +1 -2
- package/interfaces/hooks/on-gateway-init.interface.js +1 -2
- package/interfaces/index.d.ts +5 -5
- package/interfaces/index.js +5 -8
- package/interfaces/nest-gateway.interface.d.ts +1 -1
- package/interfaces/nest-gateway.interface.js +1 -2
- package/interfaces/server-and-event-streams-host.interface.js +1 -2
- package/interfaces/web-socket-server.interface.js +1 -2
- package/interfaces/websockets-entrypoint-metadata.interface.js +1 -2
- package/interfaces/ws-response.interface.js +1 -2
- package/internal.d.ts +8 -0
- package/internal.js +8 -0
- package/package.json +18 -4
- package/socket-module.d.ts +5 -8
- package/socket-module.js +26 -33
- package/socket-server-provider.d.ts +4 -4
- package/socket-server-provider.js +9 -11
- package/sockets-container.d.ts +1 -1
- package/sockets-container.js +3 -9
- package/tsconfig.build.json +1 -7
- package/utils/compare-element.util.js +1 -4
- package/utils/index.d.ts +1 -1
- package/utils/index.js +1 -4
- package/utils/param.utils.d.ts +3 -3
- package/utils/param.utils.js +40 -17
- package/web-sockets-controller.d.ts +9 -10
- package/web-sockets-controller.js +41 -28
package/adapters/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './ws-adapter';
|
|
1
|
+
export * from './ws-adapter.js';
|
package/adapters/index.js
CHANGED
package/adapters/ws-adapter.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { INestApplicationContext, WebSocketAdapter } from '@nestjs/common';
|
|
2
|
-
import { WsMessageHandler } from '@nestjs/common/interfaces';
|
|
1
|
+
import type { INestApplicationContext, WebSocketAdapter } from '@nestjs/common';
|
|
3
2
|
import { Observable } from 'rxjs';
|
|
3
|
+
import type { WsMessageHandler } from '@nestjs/common';
|
|
4
4
|
export interface BaseWsInstance {
|
|
5
5
|
on: (event: string, callback: Function) => void;
|
|
6
6
|
close: Function;
|
package/adapters/ws-adapter.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class AbstractWsAdapter {
|
|
1
|
+
import { NestApplication } from '@nestjs/core';
|
|
2
|
+
import { CONNECTION_EVENT, DISCONNECT_EVENT } from '../constants.js';
|
|
3
|
+
import { isFunction } from '@nestjs/common/internal';
|
|
4
|
+
export class AbstractWsAdapter {
|
|
5
|
+
httpServer;
|
|
6
|
+
_forceCloseConnections;
|
|
8
7
|
set forceCloseConnections(value) {
|
|
9
8
|
this._forceCloseConnections = value;
|
|
10
9
|
}
|
|
@@ -12,7 +11,7 @@ class AbstractWsAdapter {
|
|
|
12
11
|
return this._forceCloseConnections;
|
|
13
12
|
}
|
|
14
13
|
constructor(appOrHttpServer) {
|
|
15
|
-
if (appOrHttpServer && appOrHttpServer instanceof
|
|
14
|
+
if (appOrHttpServer && appOrHttpServer instanceof NestApplication) {
|
|
16
15
|
this.httpServer = appOrHttpServer.getUnderlyingHttpServer();
|
|
17
16
|
}
|
|
18
17
|
else {
|
|
@@ -20,15 +19,14 @@ class AbstractWsAdapter {
|
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
bindClientConnect(server, callback) {
|
|
23
|
-
server.on(
|
|
22
|
+
server.on(CONNECTION_EVENT, callback);
|
|
24
23
|
}
|
|
25
24
|
bindClientDisconnect(client, callback) {
|
|
26
|
-
client.on(
|
|
25
|
+
client.on(DISCONNECT_EVENT, callback);
|
|
27
26
|
}
|
|
28
27
|
async close(server) {
|
|
29
|
-
const isCallable = server &&
|
|
28
|
+
const isCallable = server && isFunction(server.close);
|
|
30
29
|
isCallable && (await new Promise(resolve => server.close(resolve)));
|
|
31
30
|
}
|
|
32
31
|
async dispose() { }
|
|
33
32
|
}
|
|
34
|
-
exports.AbstractWsAdapter = AbstractWsAdapter;
|
package/constants.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
exports.DISCONNECT_EVENT = 'disconnect';
|
|
15
|
-
exports.CLOSE_EVENT = 'close';
|
|
16
|
-
exports.ERROR_EVENT = 'error';
|
|
1
|
+
import { ROUTE_ARGS_METADATA } from '@nestjs/common/internal';
|
|
2
|
+
export const MESSAGE_MAPPING_METADATA = 'websockets:message_mapping';
|
|
3
|
+
export const MESSAGE_METADATA = 'message';
|
|
4
|
+
export const GATEWAY_SERVER_METADATA = 'websockets:is_socket';
|
|
5
|
+
export const GATEWAY_METADATA = 'websockets:is_gateway';
|
|
6
|
+
export const NAMESPACE_METADATA = 'namespace';
|
|
7
|
+
export const PORT_METADATA = 'port';
|
|
8
|
+
export const GATEWAY_OPTIONS = 'websockets:gateway_options';
|
|
9
|
+
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
|
10
|
+
export const CONNECTION_EVENT = 'connection';
|
|
11
|
+
export const DISCONNECT_EVENT = 'disconnect';
|
|
12
|
+
export const CLOSE_EVENT = 'close';
|
|
13
|
+
export const ERROR_EVENT = 'error';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { WsExceptionsHandler } from '../exceptions/ws-exceptions-handler.js';
|
|
2
|
+
import { BaseExceptionFilterContext } from '@nestjs/core/internal';
|
|
3
|
+
import type { NestContainer } from '@nestjs/core';
|
|
4
4
|
/**
|
|
5
5
|
* @publicApi
|
|
6
6
|
*/
|
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const constants_1 = require("@nestjs/common/constants");
|
|
5
|
-
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
|
6
|
-
const base_exception_filter_context_1 = require("@nestjs/core/exceptions/base-exception-filter-context");
|
|
7
|
-
const ws_exceptions_handler_1 = require("../exceptions/ws-exceptions-handler");
|
|
1
|
+
import { WsExceptionsHandler } from '../exceptions/ws-exceptions-handler.js';
|
|
2
|
+
import { EXCEPTION_FILTERS_METADATA, isEmptyArray, } from '@nestjs/common/internal';
|
|
3
|
+
import { BaseExceptionFilterContext } from '@nestjs/core/internal';
|
|
8
4
|
/**
|
|
9
5
|
* @publicApi
|
|
10
6
|
*/
|
|
11
|
-
class ExceptionFiltersContext extends
|
|
7
|
+
export class ExceptionFiltersContext extends BaseExceptionFilterContext {
|
|
12
8
|
constructor(container) {
|
|
13
9
|
super(container);
|
|
14
10
|
}
|
|
15
11
|
create(instance, callback, moduleKey) {
|
|
16
12
|
this.moduleContext = moduleKey;
|
|
17
|
-
const exceptionHandler = new
|
|
18
|
-
const filters = this.createContext(instance, callback,
|
|
19
|
-
if ((
|
|
13
|
+
const exceptionHandler = new WsExceptionsHandler();
|
|
14
|
+
const filters = this.createContext(instance, callback, EXCEPTION_FILTERS_METADATA);
|
|
15
|
+
if (isEmptyArray(filters)) {
|
|
20
16
|
return exceptionHandler;
|
|
21
17
|
}
|
|
22
18
|
exceptionHandler.setCustomFilters(filters.reverse());
|
|
@@ -26,4 +22,3 @@ class ExceptionFiltersContext extends base_exception_filter_context_1.BaseExcept
|
|
|
26
22
|
return [];
|
|
27
23
|
}
|
|
28
24
|
}
|
|
29
|
-
exports.ExceptionFiltersContext = ExceptionFiltersContext;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { GuardsContextCreator } from '@nestjs/core/
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { InterceptorsConsumer, InterceptorsContextCreator } from '@nestjs/core/interceptors';
|
|
8
|
-
import { PipesConsumer, PipesContextCreator } from '@nestjs/core/pipes';
|
|
9
|
-
import { WsParamsFactory } from '../factories/ws-params-factory';
|
|
10
|
-
import { ExceptionFiltersContext } from './exception-filters-context';
|
|
11
|
-
import { WsProxy } from './ws-proxy';
|
|
1
|
+
import type { ArgumentMetadata, ContextType, PipeTransform } from '@nestjs/common';
|
|
2
|
+
import { type Controller } from '@nestjs/common/internal';
|
|
3
|
+
import { type ExecutionContextHost, type GuardsConsumer, type GuardsContextCreator, type InterceptorsConsumer, type InterceptorsContextCreator, type ParamProperties, type ParamsMetadata, type PipesConsumer, type PipesContextCreator } from '@nestjs/core/internal';
|
|
4
|
+
import { WsParamsFactory } from '../factories/ws-params-factory.js';
|
|
5
|
+
import { ExceptionFiltersContext } from './exception-filters-context.js';
|
|
6
|
+
import { WsProxy } from './ws-proxy.js';
|
|
12
7
|
type WsParamProperties = ParamProperties & {
|
|
13
8
|
metatype?: any;
|
|
14
9
|
};
|
|
@@ -39,10 +34,6 @@ export declare class WsContextCreator {
|
|
|
39
34
|
createPipesFn(pipes: PipeTransform[], paramsOptions: (ParamProperties & {
|
|
40
35
|
metatype?: unknown;
|
|
41
36
|
})[]): ((args: unknown[], ...params: unknown[]) => Promise<void>) | null;
|
|
42
|
-
getParamValue<T>(value: T,
|
|
43
|
-
metatype: any;
|
|
44
|
-
type: any;
|
|
45
|
-
data: any;
|
|
46
|
-
}, pipes: PipeTransform[]): Promise<any>;
|
|
37
|
+
getParamValue<T>(value: T, metadata: ArgumentMetadata, pipes: PipeTransform[]): Promise<any>;
|
|
47
38
|
}
|
|
48
39
|
export {};
|
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { CUSTOM_ROUTE_ARGS_METADATA, isEmptyArray, PARAMTYPES_METADATA, } from '@nestjs/common/internal';
|
|
2
|
+
import { ContextUtils, FORBIDDEN_MESSAGE, HandlerMetadataStorage, } from '@nestjs/core/internal';
|
|
3
|
+
import { MESSAGE_METADATA, PARAM_ARGS_METADATA } from '../constants.js';
|
|
4
|
+
import { WsException } from '../errors/ws-exception.js';
|
|
5
|
+
import { WsParamsFactory } from '../factories/ws-params-factory.js';
|
|
6
|
+
import { DEFAULT_CALLBACK_METADATA } from './ws-metadata-constants.js';
|
|
7
|
+
export class WsContextCreator {
|
|
8
|
+
wsProxy;
|
|
9
|
+
exceptionFiltersContext;
|
|
10
|
+
pipesContextCreator;
|
|
11
|
+
pipesConsumer;
|
|
12
|
+
guardsContextCreator;
|
|
13
|
+
guardsConsumer;
|
|
14
|
+
interceptorsContextCreator;
|
|
15
|
+
interceptorsConsumer;
|
|
16
|
+
contextUtils = new ContextUtils();
|
|
17
|
+
wsParamsFactory = new WsParamsFactory();
|
|
18
|
+
handlerMetadataStorage = new HandlerMetadataStorage();
|
|
14
19
|
constructor(wsProxy, exceptionFiltersContext, pipesContextCreator, pipesConsumer, guardsContextCreator, guardsConsumer, interceptorsContextCreator, interceptorsConsumer) {
|
|
15
20
|
this.wsProxy = wsProxy;
|
|
16
21
|
this.exceptionFiltersContext = exceptionFiltersContext;
|
|
@@ -20,9 +25,6 @@ class WsContextCreator {
|
|
|
20
25
|
this.guardsConsumer = guardsConsumer;
|
|
21
26
|
this.interceptorsContextCreator = interceptorsContextCreator;
|
|
22
27
|
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
28
|
}
|
|
27
29
|
create(instance, callback, moduleKey, methodName) {
|
|
28
30
|
const contextType = 'ws';
|
|
@@ -53,16 +55,16 @@ class WsContextCreator {
|
|
|
53
55
|
}, exceptionHandler, targetPattern);
|
|
54
56
|
}
|
|
55
57
|
reflectCallbackParamtypes(instance, callback) {
|
|
56
|
-
return Reflect.getMetadata(
|
|
58
|
+
return Reflect.getMetadata(PARAMTYPES_METADATA, instance, callback.name);
|
|
57
59
|
}
|
|
58
60
|
reflectCallbackPattern(callback) {
|
|
59
|
-
return Reflect.getMetadata(
|
|
61
|
+
return Reflect.getMetadata(MESSAGE_METADATA, callback);
|
|
60
62
|
}
|
|
61
63
|
createGuardsFn(guards, instance, callback, contextType) {
|
|
62
64
|
const canActivateFn = async (args) => {
|
|
63
65
|
const canActivate = await this.guardsConsumer.tryActivate(guards, args, instance, callback, contextType);
|
|
64
66
|
if (!canActivate) {
|
|
65
|
-
throw new
|
|
67
|
+
throw new WsException(FORBIDDEN_MESSAGE);
|
|
66
68
|
}
|
|
67
69
|
};
|
|
68
70
|
return guards.length ? canActivateFn : null;
|
|
@@ -72,7 +74,7 @@ class WsContextCreator {
|
|
|
72
74
|
if (cacheMetadata) {
|
|
73
75
|
return cacheMetadata;
|
|
74
76
|
}
|
|
75
|
-
const metadata = this.contextUtils.reflectCallbackMetadata(instance, methodName,
|
|
77
|
+
const metadata = this.contextUtils.reflectCallbackMetadata(instance, methodName, PARAM_ARGS_METADATA) || DEFAULT_CALLBACK_METADATA;
|
|
76
78
|
const keys = Object.keys(metadata);
|
|
77
79
|
const argsLength = this.contextUtils.getArgumentsLength(keys, metadata);
|
|
78
80
|
const paramtypes = this.contextUtils.reflectCallbackParamtypes(instance, methodName);
|
|
@@ -89,34 +91,40 @@ class WsContextCreator {
|
|
|
89
91
|
exchangeKeysForValues(keys, metadata, moduleContext, paramsFactory, contextFactory) {
|
|
90
92
|
this.pipesContextCreator.setModuleContext(moduleContext);
|
|
91
93
|
return keys.map(key => {
|
|
92
|
-
const { index, data, pipes: pipesCollection } = metadata[key];
|
|
94
|
+
const { index, data, pipes: pipesCollection, schema } = metadata[key];
|
|
93
95
|
const pipes = this.pipesContextCreator.createConcreteContext(pipesCollection);
|
|
94
96
|
const type = this.contextUtils.mapParamType(key);
|
|
95
|
-
if (key.includes(
|
|
97
|
+
if (key.includes(CUSTOM_ROUTE_ARGS_METADATA)) {
|
|
96
98
|
const { factory } = metadata[key];
|
|
97
99
|
const customExtractValue = this.contextUtils.getCustomFactory(factory, data, contextFactory);
|
|
98
|
-
return {
|
|
100
|
+
return {
|
|
101
|
+
index,
|
|
102
|
+
extractValue: customExtractValue,
|
|
103
|
+
type,
|
|
104
|
+
data,
|
|
105
|
+
pipes,
|
|
106
|
+
schema,
|
|
107
|
+
};
|
|
99
108
|
}
|
|
100
109
|
const numericType = Number(type);
|
|
101
110
|
const extractValue = (...args) => paramsFactory.exchangeKeyForValue(numericType, data, args);
|
|
102
|
-
return { index, extractValue, type: numericType, data, pipes };
|
|
111
|
+
return { index, extractValue, type: numericType, data, pipes, schema };
|
|
103
112
|
});
|
|
104
113
|
}
|
|
105
114
|
createPipesFn(pipes, paramsOptions) {
|
|
106
115
|
const pipesFn = async (args, ...params) => {
|
|
107
116
|
const resolveParamValue = async (param) => {
|
|
108
|
-
const { index, extractValue, type, data, metatype, pipes: paramPipes, } = param;
|
|
117
|
+
const { index, extractValue, type, data, metatype, pipes: paramPipes, schema, } = param;
|
|
109
118
|
const value = extractValue(...params);
|
|
110
|
-
args[index] = await this.getParamValue(value, { metatype, type, data }, pipes.concat(paramPipes));
|
|
119
|
+
args[index] = await this.getParamValue(value, { metatype, type, data, schema }, pipes.concat(paramPipes));
|
|
111
120
|
};
|
|
112
121
|
await Promise.all(paramsOptions.map(resolveParamValue));
|
|
113
122
|
};
|
|
114
123
|
return paramsOptions.length ? pipesFn : null;
|
|
115
124
|
}
|
|
116
|
-
async getParamValue(value,
|
|
117
|
-
return (
|
|
125
|
+
async getParamValue(value, metadata, pipes) {
|
|
126
|
+
return isEmptyArray(pipes)
|
|
118
127
|
? value
|
|
119
|
-
: this.pipesConsumer.apply(value,
|
|
128
|
+
: this.pipesConsumer.apply(value, metadata, pipes);
|
|
120
129
|
}
|
|
121
130
|
}
|
|
122
|
-
exports.WsContextCreator = WsContextCreator;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
[`${ws_paramtype_enum_1.WsParamtype.ACK}:2`]: { index: 2, data: undefined, pipes: [] },
|
|
7
|
-
[`${ws_paramtype_enum_1.WsParamtype.PAYLOAD}:1`]: { index: 1, data: undefined, pipes: [] },
|
|
8
|
-
[`${ws_paramtype_enum_1.WsParamtype.SOCKET}:0`]: { index: 0, data: undefined, pipes: [] },
|
|
1
|
+
import { WsParamtype } from '../enums/ws-paramtype.enum.js';
|
|
2
|
+
export const DEFAULT_CALLBACK_METADATA = {
|
|
3
|
+
[`${WsParamtype.ACK}:2`]: { index: 2, data: undefined, pipes: [] },
|
|
4
|
+
[`${WsParamtype.PAYLOAD}:1`]: { index: 1, data: undefined, pipes: [] },
|
|
5
|
+
[`${WsParamtype.SOCKET}:0`]: { index: 0, data: undefined, pipes: [] },
|
|
9
6
|
};
|
package/context/ws-proxy.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WsExceptionsHandler } from '../exceptions/ws-exceptions-handler';
|
|
1
|
+
import { WsExceptionsHandler } from '../exceptions/ws-exceptions-handler.js';
|
|
2
2
|
export declare class WsProxy {
|
|
3
3
|
create(targetCallback: (...args: unknown[]) => Promise<any>, exceptionsHandler: WsExceptionsHandler, targetPattern?: string): (...args: unknown[]) => Promise<any>;
|
|
4
4
|
handleError<T>(exceptionsHandler: WsExceptionsHandler, args: unknown[], error: T): void;
|
package/context/ws-proxy.js
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const rxjs_1 = require("rxjs");
|
|
6
|
-
const operators_1 = require("rxjs/operators");
|
|
7
|
-
class WsProxy {
|
|
1
|
+
import { EMPTY, isObservable } from 'rxjs';
|
|
2
|
+
import { catchError } from 'rxjs/operators';
|
|
3
|
+
import { ExecutionContextHost } from '@nestjs/core/internal';
|
|
4
|
+
export class WsProxy {
|
|
8
5
|
create(targetCallback, exceptionsHandler, targetPattern) {
|
|
9
6
|
return async (...args) => {
|
|
10
7
|
args = [...args, targetPattern ?? 'unknown'];
|
|
11
8
|
try {
|
|
12
9
|
const result = await targetCallback(...args);
|
|
13
|
-
return !
|
|
10
|
+
return !isObservable(result)
|
|
14
11
|
? result
|
|
15
|
-
: result.pipe(
|
|
12
|
+
: result.pipe(catchError(error => {
|
|
16
13
|
this.handleError(exceptionsHandler, args, error);
|
|
17
|
-
return
|
|
14
|
+
return EMPTY;
|
|
18
15
|
}));
|
|
19
16
|
}
|
|
20
17
|
catch (error) {
|
|
@@ -23,9 +20,8 @@ class WsProxy {
|
|
|
23
20
|
};
|
|
24
21
|
}
|
|
25
22
|
handleError(exceptionsHandler, args, error) {
|
|
26
|
-
const host = new
|
|
23
|
+
const host = new ExecutionContextHost(args);
|
|
27
24
|
host.setType('ws');
|
|
28
25
|
exceptionsHandler.handle(error, host);
|
|
29
26
|
}
|
|
30
27
|
}
|
|
31
|
-
exports.WsProxy = WsProxy;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Ack = Ack;
|
|
4
|
-
const ws_paramtype_enum_1 = require("../enums/ws-paramtype.enum");
|
|
5
|
-
const param_utils_1 = require("../utils/param.utils");
|
|
1
|
+
import { WsParamtype } from '../enums/ws-paramtype.enum.js';
|
|
2
|
+
import { createPipesWsParamDecorator } from '../utils/param.utils.js';
|
|
6
3
|
/**
|
|
7
4
|
* WebSockets `ack` parameter decorator.
|
|
8
5
|
* Extracts the `ack` callback function from the arguments of a ws event.
|
|
@@ -25,6 +22,6 @@ const param_utils_1 = require("../utils/param.utils");
|
|
|
25
22
|
*
|
|
26
23
|
* @publicApi
|
|
27
24
|
*/
|
|
28
|
-
function Ack() {
|
|
29
|
-
return
|
|
25
|
+
export function Ack() {
|
|
26
|
+
return createPipesWsParamDecorator(WsParamtype.ACK)();
|
|
30
27
|
}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.ConnectedSocket = void 0;
|
|
4
|
-
const ws_paramtype_enum_1 = require("../enums/ws-paramtype.enum");
|
|
5
|
-
const param_utils_1 = require("../utils/param.utils");
|
|
1
|
+
import { WsParamtype } from '../enums/ws-paramtype.enum.js';
|
|
2
|
+
import { createWsParamDecorator } from '../utils/param.utils.js';
|
|
6
3
|
/**
|
|
7
4
|
* @publicApi
|
|
8
5
|
*/
|
|
9
|
-
|
|
6
|
+
export const ConnectedSocket = createWsParamDecorator(WsParamtype.SOCKET);
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebSocketServer = void 0;
|
|
4
|
-
const constants_1 = require("../constants");
|
|
1
|
+
import { GATEWAY_SERVER_METADATA } from '../constants.js';
|
|
5
2
|
/**
|
|
6
3
|
* Attaches native Web Socket Server to a given property.
|
|
7
4
|
*
|
|
8
5
|
* @publicApi
|
|
9
6
|
*/
|
|
10
|
-
const WebSocketServer = () => {
|
|
7
|
+
export const WebSocketServer = () => {
|
|
11
8
|
return (target, propertyKey) => {
|
|
12
9
|
Reflect.set(target, propertyKey, null);
|
|
13
|
-
Reflect.defineMetadata(
|
|
10
|
+
Reflect.defineMetadata(GATEWAY_SERVER_METADATA, true, target, propertyKey);
|
|
14
11
|
};
|
|
15
12
|
};
|
|
16
|
-
exports.WebSocketServer = WebSocketServer;
|
package/decorators/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './connected-socket.decorator';
|
|
2
|
-
export * from './gateway-server.decorator';
|
|
3
|
-
export * from './message-body.decorator';
|
|
4
|
-
export * from './socket-gateway.decorator';
|
|
5
|
-
export * from './subscribe-message.decorator';
|
|
6
|
-
export * from './ack.decorator';
|
|
1
|
+
export * from './connected-socket.decorator.js';
|
|
2
|
+
export * from './gateway-server.decorator.js';
|
|
3
|
+
export * from './message-body.decorator.js';
|
|
4
|
+
export * from './socket-gateway.decorator.js';
|
|
5
|
+
export * from './subscribe-message.decorator.js';
|
|
6
|
+
export * from './ack.decorator.js';
|
package/decorators/index.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
tslib_1.__exportStar(require("./socket-gateway.decorator"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./subscribe-message.decorator"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./ack.decorator"), exports);
|
|
1
|
+
export * from './connected-socket.decorator.js';
|
|
2
|
+
export * from './gateway-server.decorator.js';
|
|
3
|
+
export * from './message-body.decorator.js';
|
|
4
|
+
export * from './socket-gateway.decorator.js';
|
|
5
|
+
export * from './subscribe-message.decorator.js';
|
|
6
|
+
export * from './ack.decorator.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PipeTransform, Type } from '@nestjs/common';
|
|
1
|
+
import type { ParameterDecoratorOptions, PipeTransform, Type } from '@nestjs/common';
|
|
2
2
|
/**
|
|
3
3
|
* WebSockets message body parameter decorator.
|
|
4
4
|
*
|
|
@@ -43,3 +43,29 @@ export declare function MessageBody(...pipes: (Type<PipeTransform> | PipeTransfo
|
|
|
43
43
|
* @publicApi
|
|
44
44
|
*/
|
|
45
45
|
export declare function MessageBody(propertyKey: string, ...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator;
|
|
46
|
+
/**
|
|
47
|
+
* WebSockets message body parameter decorator. Extracts a property from the
|
|
48
|
+
* message payload object with additional options.
|
|
49
|
+
*
|
|
50
|
+
* For example, extracting a single param with schema:
|
|
51
|
+
* ```typescript
|
|
52
|
+
* create(@MessageBody('data', { schema: z.string() }) data: string)
|
|
53
|
+
* ```
|
|
54
|
+
* @param propertyKey name of single property to extract from the message payload
|
|
55
|
+
* @param options options object containing additional configuration for the decorator, such as pipes and schema
|
|
56
|
+
*
|
|
57
|
+
* @publicApi
|
|
58
|
+
*/
|
|
59
|
+
export declare function MessageBody(propertyKey: string, options: ParameterDecoratorOptions): ParameterDecorator;
|
|
60
|
+
/**
|
|
61
|
+
* WebSockets message body parameter decorator.
|
|
62
|
+
*
|
|
63
|
+
* For example, passing schema as options:
|
|
64
|
+
* ```typescript
|
|
65
|
+
* create(@MessageBody({ schema: z.object({ data: z.string() }) }) body)
|
|
66
|
+
* ```
|
|
67
|
+
* @param options options object containing additional configuration for the decorator, such as pipes and schema
|
|
68
|
+
*
|
|
69
|
+
* @publicApi
|
|
70
|
+
*/
|
|
71
|
+
export declare function MessageBody(options: ParameterDecoratorOptions): ParameterDecorator;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const param_utils_1 = require("../utils/param.utils");
|
|
6
|
-
function MessageBody(propertyOrPipe, ...pipes) {
|
|
7
|
-
return (0, param_utils_1.createPipesWsParamDecorator)(ws_paramtype_enum_1.WsParamtype.PAYLOAD)(propertyOrPipe, ...pipes);
|
|
1
|
+
import { WsParamtype } from '../enums/ws-paramtype.enum.js';
|
|
2
|
+
import { createPipesWsParamDecorator } from '../utils/param.utils.js';
|
|
3
|
+
export function MessageBody(propertyOrPipe, optionsOrPipe, ...pipes) {
|
|
4
|
+
return createPipesWsParamDecorator(WsParamtype.PAYLOAD)(propertyOrPipe, optionsOrPipe, ...pipes);
|
|
8
5
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GatewayMetadata } from '../interfaces';
|
|
1
|
+
import { GatewayMetadata } from '../interfaces/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Decorator that marks a class as a Nest gateway that enables real-time, bidirectional
|
|
4
4
|
* and event-based communication between the browser and the server.
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.WebSocketGateway = WebSocketGateway;
|
|
4
|
-
const constants_1 = require("../constants");
|
|
5
|
-
function WebSocketGateway(portOrOptions, options) {
|
|
1
|
+
import { GATEWAY_METADATA, GATEWAY_OPTIONS, PORT_METADATA, } from '../constants.js';
|
|
2
|
+
export function WebSocketGateway(portOrOptions, options) {
|
|
6
3
|
const isPortInt = Number.isInteger(portOrOptions);
|
|
7
4
|
// eslint-disable-next-line prefer-const
|
|
8
5
|
let [port, opt] = isPortInt ? [portOrOptions, options] : [0, portOrOptions];
|
|
9
6
|
opt = opt || {};
|
|
10
7
|
return (target) => {
|
|
11
|
-
Reflect.defineMetadata(
|
|
12
|
-
Reflect.defineMetadata(
|
|
13
|
-
Reflect.defineMetadata(
|
|
8
|
+
Reflect.defineMetadata(GATEWAY_METADATA, true, target);
|
|
9
|
+
Reflect.defineMetadata(PORT_METADATA, port, target);
|
|
10
|
+
Reflect.defineMetadata(GATEWAY_OPTIONS, opt, target);
|
|
14
11
|
};
|
|
15
12
|
}
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SubscribeMessage = void 0;
|
|
4
|
-
const constants_1 = require("../constants");
|
|
1
|
+
import { MESSAGE_MAPPING_METADATA, MESSAGE_METADATA } from '../constants.js';
|
|
5
2
|
/**
|
|
6
3
|
* Subscribes to messages that fulfils chosen pattern.
|
|
7
4
|
*
|
|
8
5
|
* @publicApi
|
|
9
6
|
*/
|
|
10
|
-
const SubscribeMessage = (message) => {
|
|
7
|
+
export const SubscribeMessage = (message) => {
|
|
11
8
|
return (target, key, descriptor) => {
|
|
12
|
-
Reflect.defineMetadata(
|
|
13
|
-
Reflect.defineMetadata(
|
|
9
|
+
Reflect.defineMetadata(MESSAGE_MAPPING_METADATA, true, descriptor.value);
|
|
10
|
+
Reflect.defineMetadata(MESSAGE_METADATA, message, descriptor.value);
|
|
14
11
|
return descriptor;
|
|
15
12
|
};
|
|
16
13
|
};
|
|
17
|
-
exports.SubscribeMessage = SubscribeMessage;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WsParamtype = void 0;
|
|
4
|
-
var WsParamtype;
|
|
1
|
+
export var WsParamtype;
|
|
5
2
|
(function (WsParamtype) {
|
|
6
3
|
WsParamtype[WsParamtype["SOCKET"] = 0] = "SOCKET";
|
|
7
4
|
WsParamtype[WsParamtype["PAYLOAD"] = 3] = "PAYLOAD";
|
|
8
5
|
WsParamtype[WsParamtype["ACK"] = 13] = "ACK";
|
|
9
|
-
})(WsParamtype || (
|
|
6
|
+
})(WsParamtype || (WsParamtype = {}));
|
package/errors/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './ws-exception';
|
|
1
|
+
export * from './ws-exception.js';
|
package/errors/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RuntimeException } from '@nestjs/core/
|
|
1
|
+
import { RuntimeException } from '@nestjs/core/internal';
|
|
2
2
|
export declare class InvalidSocketPortException extends RuntimeException {
|
|
3
3
|
constructor(port: number | string, type: any);
|
|
4
4
|
}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.InvalidSocketPortException = void 0;
|
|
4
|
-
const runtime_exception_1 = require("@nestjs/core/errors/exceptions/runtime.exception");
|
|
5
|
-
class InvalidSocketPortException extends runtime_exception_1.RuntimeException {
|
|
1
|
+
import { RuntimeException } from '@nestjs/core/internal';
|
|
2
|
+
export class InvalidSocketPortException extends RuntimeException {
|
|
6
3
|
constructor(port, type) {
|
|
7
4
|
super(`Invalid port (${port}) in gateway ${type}`);
|
|
8
5
|
}
|
|
9
6
|
}
|
|
10
|
-
exports.InvalidSocketPortException = InvalidSocketPortException;
|