@nestjs/websockets
Version:
WebSockets for NestJS: gateways with message handlers, guards, pipes and interceptors, on Socket.IO or ws.
19 lines (18 loc) • 864 B
JavaScript
import { GATEWAY_METADATA, GATEWAY_OPTIONS, PORT_METADATA, } from '../constants.js';
import { isObject } from '@nestjs/common/internal';
export function WebSocketGateway(portOrOptions, options) {
// Told apart by the options, not by the port: a port that is not an integer,
// e.g. Number(process.env.PORT) with the variable unset, is kept, so that
// the gateway is refused at startup instead of losing its options
const isOptions = isObject(portOrOptions);
// eslint-disable-next-line prefer-const
let [port, opt] = isOptions
? [0, portOrOptions]
: [portOrOptions ?? 0, options];
opt = opt || {};
return (target) => {
Reflect.defineMetadata(GATEWAY_METADATA, true, target);
Reflect.defineMetadata(PORT_METADATA, port, target);
Reflect.defineMetadata(GATEWAY_OPTIONS, opt, target);
};
}