Skip to content

Commit edcc64d

Browse files
Merge pull request nestjs#3543 from nestjs/fix/missing-gateway-options
fix(websockets): add missing options, improve comments, add generic
2 parents 5633df4 + 7391241 commit edcc64d

2 files changed

Lines changed: 98 additions & 12 deletions

File tree

packages/websockets/decorators/socket-gateway.decorator.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import { GATEWAY_METADATA, GATEWAY_OPTIONS, PORT_METADATA } from '../constants';
22
import { GatewayMetadata } from '../interfaces';
33

44
/**
5-
* Defines the Gateway. The gateway is able to inject dependencies through constructor.
6-
* Those dependencies should belong to the same module. Gateway is listening on the specified port.
5+
* Decorator that marks a class as a Nest gateway that enables real-time, bidirectional
6+
* and event-based communication between the browser and the server.
77
*/
88
export function WebSocketGateway(port?: number): ClassDecorator;
9-
export function WebSocketGateway(options?: GatewayMetadata): ClassDecorator;
10-
export function WebSocketGateway(
11-
port?: number,
12-
options?: GatewayMetadata,
13-
): ClassDecorator;
14-
export function WebSocketGateway(
15-
portOrOptions?: number | GatewayMetadata,
16-
options?: GatewayMetadata,
17-
): ClassDecorator {
9+
export function WebSocketGateway<
10+
T extends Record<string, any> = GatewayMetadata
11+
>(options?: T): ClassDecorator;
12+
export function WebSocketGateway<
13+
T extends Record<string, any> = GatewayMetadata
14+
>(port?: number, options?: T): ClassDecorator;
15+
export function WebSocketGateway<
16+
T extends Record<string, any> = GatewayMetadata
17+
>(portOrOptions?: number | T, options?: T): ClassDecorator {
1818
const isPortInt = Number.isInteger(portOrOptions as number);
1919
// tslint:disable-next-line:prefer-const
2020
let [port, opt] = isPortInt ? [portOrOptions, options] : [0, portOrOptions];
2121

22-
opt = opt || {};
22+
opt = opt || ({} as T);
2323
return (target: object) => {
2424
Reflect.defineMetadata(GATEWAY_METADATA, true, target);
2525
Reflect.defineMetadata(PORT_METADATA, port, target);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,97 @@
1+
/**
2+
* @external https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/socket.io/index.d.ts
3+
*/
4+
15
export interface GatewayMetadata {
6+
/**
7+
* The name of a namespace
8+
*/
29
namespace?: string | RegExp;
10+
11+
/**
12+
* The path to ws
13+
* @default '/socket.io'
14+
*/
315
path?: string;
16+
17+
/**
18+
* Should we serve the client file?
19+
* @default true
20+
*/
421
serveClient?: boolean;
22+
23+
/**
24+
* The adapter to use for handling rooms. NOTE: this should be a class,
25+
* not an object
26+
* @default typeof Adapter
27+
*/
528
adapter?: any;
29+
30+
/**
31+
* Accepted origins
32+
* @default '*:*'
33+
*/
634
origins?: string;
35+
736
parser?: any;
37+
38+
/**
39+
* How many milliseconds without a pong packed to consider the connection closed (engine.io)
40+
* @default 60000
41+
*/
842
pingTimeout?: number;
43+
44+
/**
45+
* How many milliseconds before sending a new ping packet (keep-alive) (engine.io)
46+
* @default 25000
47+
*/
948
pingInterval?: number;
49+
50+
/**
51+
* How many bytes or characters a message can be when polling, before closing the session
52+
* (to avoid Dos) (engine.io)
53+
* @default 10E7
54+
*/
55+
maxHttpBufferSize?: number;
56+
57+
/**
58+
* Transports to allow connections to (engine.io)
59+
* @default ['polling','websocket']
60+
*/
1061
transports?: string[];
62+
63+
/**
64+
* Whether to allow transport upgrades (engine.io)
65+
* @default true
66+
*/
67+
allowUpgrades?: boolean;
68+
69+
/**
70+
* parameters of the WebSocket permessage-deflate extension (see ws module).
71+
* Set to false to disable (engine.io)
72+
* @default true
73+
*/
74+
perMessageDeflate?: Object | boolean;
75+
76+
/**
77+
* Parameters of the http compression for the polling transports (see zlib).
78+
* Set to false to disable, or set an object with parameter "threshold:number"
79+
* to only compress data if the byte size is above this value (1024) (engine.io)
80+
* @default true|1024
81+
*/
82+
httpCompression?: Object | boolean;
83+
84+
/**
85+
* Name of the HTTP cookie that contains the client sid to send as part of
86+
* handshake response headers. Set to false to not send one (engine.io)
87+
* @default "io"
88+
*/
89+
cookie?: string | boolean;
90+
91+
/**
92+
* Whether to let engine.io handle the OPTIONS requests.
93+
* You can also pass a custom function to handle the requests
94+
* @default true
95+
*/
96+
handlePreflightRequest?: ((req: any, res: any) => void) | boolean;
1197
}

0 commit comments

Comments
 (0)