source: frontend/node_modules/webpack-dev-server/lib/servers/SockJSServer.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.4 KB
Line 
1"use strict";
2
3const sockjs = require("sockjs");
4const BaseServer = require("./BaseServer");
5
6/** @typedef {import("../Server").WebSocketServerConfiguration} WebSocketServerConfiguration */
7/** @typedef {import("../Server").ClientConnection} ClientConnection */
8
9// Workaround for sockjs@~0.3.19
10// sockjs will remove Origin header, however Origin header is required for checking host.
11// See https://github.com/webpack/webpack-dev-server/issues/1604 for more information
12{
13 // @ts-ignore
14 const SockjsSession = require("sockjs/lib/transport").Session;
15 const decorateConnection = SockjsSession.prototype.decorateConnection;
16
17 /**
18 * @param {import("http").IncomingMessage} req
19 */
20 // eslint-disable-next-line func-names
21 SockjsSession.prototype.decorateConnection = function (req) {
22 decorateConnection.call(this, req);
23
24 const connection = this.connection;
25
26 if (
27 connection.headers &&
28 !("origin" in connection.headers) &&
29 "origin" in req.headers
30 ) {
31 connection.headers.origin = req.headers.origin;
32 }
33 };
34}
35
36module.exports = class SockJSServer extends BaseServer {
37 // options has: error (function), debug (function), server (http/s server), path (string)
38 /**
39 * @param {import("../Server")} server
40 */
41 constructor(server) {
42 super(server);
43
44 const webSocketServerOptions =
45 /** @type {NonNullable<WebSocketServerConfiguration["options"]>} */
46 (
47 /** @type {WebSocketServerConfiguration} */
48 (this.server.options.webSocketServer).options
49 );
50
51 /**
52 * @param {NonNullable<WebSocketServerConfiguration["options"]>} options
53 * @returns {string}
54 */
55 const getSockjsUrl = (options) => {
56 if (typeof options.sockjsUrl !== "undefined") {
57 return options.sockjsUrl;
58 }
59
60 return "/__webpack_dev_server__/sockjs.bundle.js";
61 };
62
63 this.implementation = sockjs.createServer({
64 // Use provided up-to-date sockjs-client
65 sockjs_url: getSockjsUrl(webSocketServerOptions),
66 // Default logger is very annoy. Limit useless logs.
67 /**
68 * @param {string} severity
69 * @param {string} line
70 */
71 log: (severity, line) => {
72 if (severity === "error") {
73 this.server.logger.error(line);
74 } else if (severity === "info") {
75 this.server.logger.log(line);
76 } else {
77 this.server.logger.debug(line);
78 }
79 },
80 });
81
82 /**
83 * @param {import("sockjs").ServerOptions & { path?: string }} options
84 * @returns {string | undefined}
85 */
86 const getPrefix = (options) => {
87 if (typeof options.prefix !== "undefined") {
88 return options.prefix;
89 }
90
91 return options.path;
92 };
93
94 const options = {
95 ...webSocketServerOptions,
96 prefix: getPrefix(webSocketServerOptions),
97 };
98
99 this.implementation.installHandlers(
100 /** @type {import("http").Server} */ (this.server.server),
101 options
102 );
103
104 this.implementation.on("connection", (client) => {
105 // @ts-ignore
106 // Implement the the same API as for `ws`
107 client.send = client.write;
108 // @ts-ignore
109 client.terminate = client.close;
110
111 this.clients.push(/** @type {ClientConnection} */ (client));
112
113 client.on("close", () => {
114 this.clients.splice(
115 this.clients.indexOf(/** @type {ClientConnection} */ (client)),
116 1
117 );
118 });
119 });
120
121 // @ts-ignore
122 this.implementation.close = (callback) => {
123 callback();
124 };
125 }
126};
Note: See TracBrowser for help on using the repository browser.