Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
const http = require('http');
const url = require('url');
const uws = require('uws');
const native = uws.native;
//
// uws v0.12.0+ needs a set "no operation" callback.
//
if (native.setNoop) native.setNoop(() => {});
/**
* Server of µWebSockets transformer.
*
* @runat server
* @api private
*/
module.exports = function server() {
const opts = Object.assign({
perMessageDeflate: !!this.primus.options.compression,
maxPayload: this.primus.options.maxLength
_uwsInit() {
const maxMessageSize = this._getOption('maxMessageSize');
const perMessageDeflate = this._getOption('perMessageDeflate');
this.serverGroup = uws.native.server.group.create(perMessageDeflate, maxMessageSize);
this.noDelay = this._getOption('noDelay');
uws.native.server.group.onDisconnection(this.serverGroup, (external, code, message, socketWrapper) => {
if (socketWrapper) {
socketWrapper.close();
}
});
uws.native.server.group.onMessage(this.serverGroup, (message, socketWrapper) => {
const parseResults = socketWrapper.parseMessage(message);
const parsedMessages = this._handleParseErrors(socketWrapper, parseResults);
if (parsedMessages.length > 0) {
socketWrapper.onMessage(parsedMessages);
}
});
uws.native.server.group.onPing(this.serverGroup, () => { });
uws.native.server.group.onPong(this.serverGroup, () => { });
uws.native.server.group.onConnection(this.serverGroup, this._onConnection.bind(this));
this.pingInterval = setInterval(() => {
uws.native.server.group.broadcast(this.serverGroup, this.pingMessage, true);
close() {
clearInterval(this.pingInterval);
this.server.removeAllListeners('request');
this.server.removeAllListeners('upgrade');
if (this.serverGroup) {
uws.native.server.group.close(this.serverGroup);
}
this.serverGroup = null;
this.server.close(() => {
this.emit('close');
});
}
}
sendPrepared(preparedMessage) {
this.flush();
uws.native.server.sendPrepared(this.external, preparedMessage);
}
/**