swaf/dist/components/WebSocketServerComponent.js

73 lines
11 KiB
JavaScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import ApplicationComponent from "../ApplicationComponent";
import { Server as WebSocketServer } from "ws";
import Logger from "../Logger";
import cookie from "cookie";
import cookieParser from "cookie-parser";
import config from "config";
export default class WebSocketServerComponent extends ApplicationComponent {
constructor(application, expressAppComponent, storeComponent) {
super();
this.expressAppComponent = expressAppComponent;
this.application = application;
this.storeComponent = storeComponent;
}
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
const listeners = this.application.getWebSocketListeners();
this.wss = new WebSocketServer({
server: this.expressAppComponent.getServer(),
}, () => {
Logger.info(`Websocket server started over webserver.`);
}).on('error', (err) => {
Logger.error(err, 'An error occurred in the websocket server.');
}).on('connection', (socket, request) => {
const listener = request.url ? listeners[request.url] : null;
if (!listener) {
socket.close(1002, `Path not found ${request.url}`);
return;
}
else if (!request.headers.cookie) {
socket.close(1002, `Can't process request without cookies.`);
return;
}
Logger.debug(`Websocket on ${request.url}`);
const cookies = cookie.parse(request.headers.cookie);
const sid = cookieParser.signedCookie(cookies['connect.sid'], config.get('session.secret'));
if (!sid) {
socket.close(1002);
return;
}
const store = this.storeComponent.getStore();
store.get(sid, (err, session) => {
if (err || !session) {
Logger.error(err, 'Error while initializing session in websocket.');
socket.close(1011);
return;
}
session.id = sid;
store.createSession(request, session);
listener.handle(socket, request, session).catch(err => {
Logger.error(err, 'Error in websocket listener.');
});
});
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
if (this.wss) {
yield this.close('WebSocket server', this.wss, this.wss.close);
}
});
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiV2ViU29ja2V0U2VydmVyQ29tcG9uZW50LmpzIiwic291cmNlUm9vdCI6Ii4vIiwic291cmNlcyI6WyJjb21wb25lbnRzL1dlYlNvY2tldFNlcnZlckNvbXBvbmVudC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7QUFBQSxPQUFPLG9CQUFvQixNQUFNLHlCQUF5QixDQUFDO0FBRTNELE9BQWtCLEVBQUMsTUFBTSxJQUFJLGVBQWUsRUFBQyxNQUFNLElBQUksQ0FBQztBQUN4RCxPQUFPLE1BQU0sTUFBTSxXQUFXLENBQUM7QUFDL0IsT0FBTyxNQUFNLE1BQU0sUUFBUSxDQUFDO0FBQzVCLE9BQU8sWUFBWSxNQUFNLGVBQWUsQ0FBQztBQUN6QyxPQUFPLE1BQU0sTUFBTSxRQUFRLENBQUM7QUFNNUIsTUFBTSxDQUFDLE9BQU8sT0FBTyx3QkFBeUIsU0FBUSxvQkFBMEI7SUFPNUUsWUFBWSxXQUF3QixFQUFFLG1CQUF3QyxFQUFFLGNBQThCO1FBQzFHLEtBQUssRUFBRSxDQUFDO1FBQ1IsSUFBSSxDQUFDLG1CQUFtQixHQUFHLG1CQUFtQixDQUFDO1FBQy9DLElBQUksQ0FBQyxXQUFXLEdBQUcsV0FBVyxDQUFDO1FBQy9CLElBQUksQ0FBQyxjQUFjLEdBQUcsY0FBYyxDQUFDO0lBQ3pDLENBQUM7SUFFWSxLQUFLLENBQUMsR0FBWSxFQUFFLE1BQWM7O1lBQzNDLE1BQU0sU0FBUyxHQUF1QyxJQUFJLENBQUMsV0FBVyxDQUFDLHFCQUFxQixFQUFFLENBQUM7WUFDL0YsSUFBSSxDQUFDLEdBQUcsR0FBRyxJQUFJLGVBQWUsQ0FBQztnQkFDM0IsTUFBTSxFQ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