[6a3a178] | 1 | "use strict";
|
---|
| 2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
---|
| 3 | if (k2 === undefined) k2 = k;
|
---|
| 4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
---|
| 5 | }) : (function(o, m, k, k2) {
|
---|
| 6 | if (k2 === undefined) k2 = k;
|
---|
| 7 | o[k2] = m[k];
|
---|
| 8 | }));
|
---|
| 9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
---|
| 10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
---|
| 11 | }) : function(o, v) {
|
---|
| 12 | o["default"] = v;
|
---|
| 13 | });
|
---|
| 14 | var __importStar = (this && this.__importStar) || function (mod) {
|
---|
| 15 | if (mod && mod.__esModule) return mod;
|
---|
| 16 | var result = {};
|
---|
| 17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
---|
| 18 | __setModuleDefault(result, mod);
|
---|
| 19 | return result;
|
---|
| 20 | };
|
---|
| 21 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
---|
| 22 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
---|
| 23 | };
|
---|
| 24 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 25 | exports.Namespace = exports.Socket = exports.Server = void 0;
|
---|
| 26 | const http = require("http");
|
---|
| 27 | const fs_1 = require("fs");
|
---|
| 28 | const zlib_1 = require("zlib");
|
---|
| 29 | const accepts = require("accepts");
|
---|
| 30 | const stream_1 = require("stream");
|
---|
| 31 | const path = require("path");
|
---|
| 32 | const engine = require("engine.io");
|
---|
| 33 | const client_1 = require("./client");
|
---|
| 34 | const events_1 = require("events");
|
---|
| 35 | const namespace_1 = require("./namespace");
|
---|
| 36 | Object.defineProperty(exports, "Namespace", { enumerable: true, get: function () { return namespace_1.Namespace; } });
|
---|
| 37 | const parent_namespace_1 = require("./parent-namespace");
|
---|
| 38 | const socket_io_adapter_1 = require("socket.io-adapter");
|
---|
| 39 | const parser = __importStar(require("socket.io-parser"));
|
---|
| 40 | const debug_1 = __importDefault(require("debug"));
|
---|
| 41 | const socket_1 = require("./socket");
|
---|
| 42 | Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return socket_1.Socket; } });
|
---|
| 43 | const debug = debug_1.default("socket.io:server");
|
---|
| 44 | const clientVersion = require("../package.json").version;
|
---|
| 45 | const dotMapRegex = /\.map/;
|
---|
| 46 | class Server extends events_1.EventEmitter {
|
---|
| 47 | constructor(srv, opts = {}) {
|
---|
| 48 | super();
|
---|
| 49 | /**
|
---|
| 50 | * @private
|
---|
| 51 | */
|
---|
| 52 | this._nsps = new Map();
|
---|
| 53 | this.parentNsps = new Map();
|
---|
| 54 | if ("object" === typeof srv &&
|
---|
| 55 | srv instanceof Object &&
|
---|
| 56 | !srv.listen) {
|
---|
| 57 | opts = srv;
|
---|
| 58 | srv = undefined;
|
---|
| 59 | }
|
---|
| 60 | this.path(opts.path || "/socket.io");
|
---|
| 61 | this.connectTimeout(opts.connectTimeout || 45000);
|
---|
| 62 | this.serveClient(false !== opts.serveClient);
|
---|
| 63 | this._parser = opts.parser || parser;
|
---|
| 64 | this.encoder = new this._parser.Encoder();
|
---|
| 65 | this.adapter(opts.adapter || socket_io_adapter_1.Adapter);
|
---|
| 66 | this.sockets = this.of("/");
|
---|
| 67 | this.opts = opts;
|
---|
| 68 | if (srv)
|
---|
| 69 | this.attach(srv);
|
---|
| 70 | }
|
---|
| 71 | serveClient(v) {
|
---|
| 72 | if (!arguments.length)
|
---|
| 73 | return this._serveClient;
|
---|
| 74 | this._serveClient = v;
|
---|
| 75 | return this;
|
---|
| 76 | }
|
---|
| 77 | /**
|
---|
| 78 | * Executes the middleware for an incoming namespace not already created on the server.
|
---|
| 79 | *
|
---|
| 80 | * @param name - name of incoming namespace
|
---|
| 81 | * @param auth - the auth parameters
|
---|
| 82 | * @param fn - callback
|
---|
| 83 | *
|
---|
| 84 | * @private
|
---|
| 85 | */
|
---|
| 86 | _checkNamespace(name, auth, fn) {
|
---|
| 87 | if (this.parentNsps.size === 0)
|
---|
| 88 | return fn(false);
|
---|
| 89 | const keysIterator = this.parentNsps.keys();
|
---|
| 90 | const run = () => {
|
---|
| 91 | const nextFn = keysIterator.next();
|
---|
| 92 | if (nextFn.done) {
|
---|
| 93 | return fn(false);
|
---|
| 94 | }
|
---|
| 95 | nextFn.value(name, auth, (err, allow) => {
|
---|
| 96 | if (err || !allow) {
|
---|
| 97 | run();
|
---|
| 98 | }
|
---|
| 99 | else {
|
---|
| 100 | fn(this.parentNsps.get(nextFn.value).createChild(name));
|
---|
| 101 | }
|
---|
| 102 | });
|
---|
| 103 | };
|
---|
| 104 | run();
|
---|
| 105 | }
|
---|
| 106 | path(v) {
|
---|
| 107 | if (!arguments.length)
|
---|
| 108 | return this._path;
|
---|
| 109 | this._path = v.replace(/\/$/, "");
|
---|
| 110 | const escapedPath = this._path.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
|
---|
| 111 | this.clientPathRegex = new RegExp("^" +
|
---|
| 112 | escapedPath +
|
---|
| 113 | "/socket\\.io(\\.min|\\.msgpack\\.min)?\\.js(\\.map)?$");
|
---|
| 114 | return this;
|
---|
| 115 | }
|
---|
| 116 | connectTimeout(v) {
|
---|
| 117 | if (v === undefined)
|
---|
| 118 | return this._connectTimeout;
|
---|
| 119 | this._connectTimeout = v;
|
---|
| 120 | return this;
|
---|
| 121 | }
|
---|
| 122 | adapter(v) {
|
---|
| 123 | if (!arguments.length)
|
---|
| 124 | return this._adapter;
|
---|
| 125 | this._adapter = v;
|
---|
| 126 | for (const nsp of this._nsps.values()) {
|
---|
| 127 | nsp._initAdapter();
|
---|
| 128 | }
|
---|
| 129 | return this;
|
---|
| 130 | }
|
---|
| 131 | /**
|
---|
| 132 | * Attaches socket.io to a server or port.
|
---|
| 133 | *
|
---|
| 134 | * @param srv - server or port
|
---|
| 135 | * @param opts - options passed to engine.io
|
---|
| 136 | * @return self
|
---|
| 137 | * @public
|
---|
| 138 | */
|
---|
| 139 | listen(srv, opts = {}) {
|
---|
| 140 | return this.attach(srv, opts);
|
---|
| 141 | }
|
---|
| 142 | /**
|
---|
| 143 | * Attaches socket.io to a server or port.
|
---|
| 144 | *
|
---|
| 145 | * @param srv - server or port
|
---|
| 146 | * @param opts - options passed to engine.io
|
---|
| 147 | * @return self
|
---|
| 148 | * @public
|
---|
| 149 | */
|
---|
| 150 | attach(srv, opts = {}) {
|
---|
| 151 | if ("function" == typeof srv) {
|
---|
| 152 | const msg = "You are trying to attach socket.io to an express " +
|
---|
| 153 | "request handler function. Please pass a http.Server instance.";
|
---|
| 154 | throw new Error(msg);
|
---|
| 155 | }
|
---|
| 156 | // handle a port as a string
|
---|
| 157 | if (Number(srv) == srv) {
|
---|
| 158 | srv = Number(srv);
|
---|
| 159 | }
|
---|
| 160 | if ("number" == typeof srv) {
|
---|
| 161 | debug("creating http server and binding to %d", srv);
|
---|
| 162 | const port = srv;
|
---|
| 163 | srv = http.createServer((req, res) => {
|
---|
| 164 | res.writeHead(404);
|
---|
| 165 | res.end();
|
---|
| 166 | });
|
---|
| 167 | srv.listen(port);
|
---|
| 168 | }
|
---|
| 169 | // merge the options passed to the Socket.IO server
|
---|
| 170 | Object.assign(opts, this.opts);
|
---|
| 171 | // set engine.io path to `/socket.io`
|
---|
| 172 | opts.path = opts.path || this._path;
|
---|
| 173 | this.initEngine(srv, opts);
|
---|
| 174 | return this;
|
---|
| 175 | }
|
---|
| 176 | /**
|
---|
| 177 | * Initialize engine
|
---|
| 178 | *
|
---|
| 179 | * @param srv - the server to attach to
|
---|
| 180 | * @param opts - options passed to engine.io
|
---|
| 181 | * @private
|
---|
| 182 | */
|
---|
| 183 | initEngine(srv, opts) {
|
---|
| 184 | // initialize engine
|
---|
| 185 | debug("creating engine.io instance with opts %j", opts);
|
---|
| 186 | this.eio = engine.attach(srv, opts);
|
---|
| 187 | // attach static file serving
|
---|
| 188 | if (this._serveClient)
|
---|
| 189 | this.attachServe(srv);
|
---|
| 190 | // Export http server
|
---|
| 191 | this.httpServer = srv;
|
---|
| 192 | // bind to engine events
|
---|
| 193 | this.bind(this.eio);
|
---|
| 194 | }
|
---|
| 195 | /**
|
---|
| 196 | * Attaches the static file serving.
|
---|
| 197 | *
|
---|
| 198 | * @param srv http server
|
---|
| 199 | * @private
|
---|
| 200 | */
|
---|
| 201 | attachServe(srv) {
|
---|
| 202 | debug("attaching client serving req handler");
|
---|
| 203 | const evs = srv.listeners("request").slice(0);
|
---|
| 204 | srv.removeAllListeners("request");
|
---|
| 205 | srv.on("request", (req, res) => {
|
---|
| 206 | if (this.clientPathRegex.test(req.url)) {
|
---|
| 207 | this.serve(req, res);
|
---|
| 208 | }
|
---|
| 209 | else {
|
---|
| 210 | for (let i = 0; i < evs.length; i++) {
|
---|
| 211 | evs[i].call(srv, req, res);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | });
|
---|
| 215 | }
|
---|
| 216 | /**
|
---|
| 217 | * Handles a request serving of client source and map
|
---|
| 218 | *
|
---|
| 219 | * @param req
|
---|
| 220 | * @param res
|
---|
| 221 | * @private
|
---|
| 222 | */
|
---|
| 223 | serve(req, res) {
|
---|
| 224 | const filename = req.url.replace(this._path, "");
|
---|
| 225 | const isMap = dotMapRegex.test(filename);
|
---|
| 226 | const type = isMap ? "map" : "source";
|
---|
| 227 | // Per the standard, ETags must be quoted:
|
---|
| 228 | // https://tools.ietf.org/html/rfc7232#section-2.3
|
---|
| 229 | const expectedEtag = '"' + clientVersion + '"';
|
---|
| 230 | const weakEtag = "W/" + expectedEtag;
|
---|
| 231 | const etag = req.headers["if-none-match"];
|
---|
| 232 | if (etag) {
|
---|
| 233 | if (expectedEtag === etag || weakEtag === etag) {
|
---|
| 234 | debug("serve client %s 304", type);
|
---|
| 235 | res.writeHead(304);
|
---|
| 236 | res.end();
|
---|
| 237 | return;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | debug("serve client %s", type);
|
---|
| 241 | res.setHeader("Cache-Control", "public, max-age=0");
|
---|
| 242 | res.setHeader("Content-Type", "application/" + (isMap ? "json" : "javascript"));
|
---|
| 243 | res.setHeader("ETag", expectedEtag);
|
---|
| 244 | if (!isMap) {
|
---|
| 245 | res.setHeader("X-SourceMap", filename.substring(1) + ".map");
|
---|
| 246 | }
|
---|
| 247 | Server.sendFile(filename, req, res);
|
---|
| 248 | }
|
---|
| 249 | /**
|
---|
| 250 | * @param filename
|
---|
| 251 | * @param req
|
---|
| 252 | * @param res
|
---|
| 253 | * @private
|
---|
| 254 | */
|
---|
| 255 | static sendFile(filename, req, res) {
|
---|
| 256 | const readStream = fs_1.createReadStream(path.join(__dirname, "../client-dist/", filename));
|
---|
| 257 | const encoding = accepts(req).encodings(["br", "gzip", "deflate"]);
|
---|
| 258 | const onError = (err) => {
|
---|
| 259 | if (err) {
|
---|
| 260 | res.end();
|
---|
| 261 | }
|
---|
| 262 | };
|
---|
| 263 | switch (encoding) {
|
---|
| 264 | case "br":
|
---|
| 265 | res.writeHead(200, { "content-encoding": "br" });
|
---|
| 266 | readStream.pipe(zlib_1.createBrotliCompress()).pipe(res);
|
---|
| 267 | stream_1.pipeline(readStream, zlib_1.createBrotliCompress(), res, onError);
|
---|
| 268 | break;
|
---|
| 269 | case "gzip":
|
---|
| 270 | res.writeHead(200, { "content-encoding": "gzip" });
|
---|
| 271 | stream_1.pipeline(readStream, zlib_1.createGzip(), res, onError);
|
---|
| 272 | break;
|
---|
| 273 | case "deflate":
|
---|
| 274 | res.writeHead(200, { "content-encoding": "deflate" });
|
---|
| 275 | stream_1.pipeline(readStream, zlib_1.createDeflate(), res, onError);
|
---|
| 276 | break;
|
---|
| 277 | default:
|
---|
| 278 | res.writeHead(200);
|
---|
| 279 | stream_1.pipeline(readStream, res, onError);
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | /**
|
---|
| 283 | * Binds socket.io to an engine.io instance.
|
---|
| 284 | *
|
---|
| 285 | * @param {engine.Server} engine engine.io (or compatible) server
|
---|
| 286 | * @return self
|
---|
| 287 | * @public
|
---|
| 288 | */
|
---|
| 289 | bind(engine) {
|
---|
| 290 | this.engine = engine;
|
---|
| 291 | this.engine.on("connection", this.onconnection.bind(this));
|
---|
| 292 | return this;
|
---|
| 293 | }
|
---|
| 294 | /**
|
---|
| 295 | * Called with each incoming transport connection.
|
---|
| 296 | *
|
---|
| 297 | * @param {engine.Socket} conn
|
---|
| 298 | * @return self
|
---|
| 299 | * @private
|
---|
| 300 | */
|
---|
| 301 | onconnection(conn) {
|
---|
| 302 | debug("incoming connection with id %s", conn.id);
|
---|
| 303 | const client = new client_1.Client(this, conn);
|
---|
| 304 | if (conn.protocol === 3) {
|
---|
| 305 | // @ts-ignore
|
---|
| 306 | client.connect("/");
|
---|
| 307 | }
|
---|
| 308 | return this;
|
---|
| 309 | }
|
---|
| 310 | /**
|
---|
| 311 | * Looks up a namespace.
|
---|
| 312 | *
|
---|
| 313 | * @param {String|RegExp|Function} name nsp name
|
---|
| 314 | * @param fn optional, nsp `connection` ev handler
|
---|
| 315 | * @public
|
---|
| 316 | */
|
---|
| 317 | of(name, fn) {
|
---|
| 318 | if (typeof name === "function" || name instanceof RegExp) {
|
---|
| 319 | const parentNsp = new parent_namespace_1.ParentNamespace(this);
|
---|
| 320 | debug("initializing parent namespace %s", parentNsp.name);
|
---|
| 321 | if (typeof name === "function") {
|
---|
| 322 | this.parentNsps.set(name, parentNsp);
|
---|
| 323 | }
|
---|
| 324 | else {
|
---|
| 325 | this.parentNsps.set((nsp, conn, next) => next(null, name.test(nsp)), parentNsp);
|
---|
| 326 | }
|
---|
| 327 | if (fn) {
|
---|
| 328 | // @ts-ignore
|
---|
| 329 | parentNsp.on("connect", fn);
|
---|
| 330 | }
|
---|
| 331 | return parentNsp;
|
---|
| 332 | }
|
---|
| 333 | if (String(name)[0] !== "/")
|
---|
| 334 | name = "/" + name;
|
---|
| 335 | let nsp = this._nsps.get(name);
|
---|
| 336 | if (!nsp) {
|
---|
| 337 | debug("initializing namespace %s", name);
|
---|
| 338 | nsp = new namespace_1.Namespace(this, name);
|
---|
| 339 | this._nsps.set(name, nsp);
|
---|
| 340 | }
|
---|
| 341 | if (fn)
|
---|
| 342 | nsp.on("connect", fn);
|
---|
| 343 | return nsp;
|
---|
| 344 | }
|
---|
| 345 | /**
|
---|
| 346 | * Closes server connection
|
---|
| 347 | *
|
---|
| 348 | * @param [fn] optional, called as `fn([err])` on error OR all conns closed
|
---|
| 349 | * @public
|
---|
| 350 | */
|
---|
| 351 | close(fn) {
|
---|
| 352 | for (const socket of this.sockets.sockets.values()) {
|
---|
| 353 | socket._onclose("server shutting down");
|
---|
| 354 | }
|
---|
| 355 | this.engine.close();
|
---|
| 356 | if (this.httpServer) {
|
---|
| 357 | this.httpServer.close(fn);
|
---|
| 358 | }
|
---|
| 359 | else {
|
---|
| 360 | fn && fn();
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | /**
|
---|
| 364 | * Sets up namespace middleware.
|
---|
| 365 | *
|
---|
| 366 | * @return self
|
---|
| 367 | * @public
|
---|
| 368 | */
|
---|
| 369 | use(fn) {
|
---|
| 370 | this.sockets.use(fn);
|
---|
| 371 | return this;
|
---|
| 372 | }
|
---|
| 373 | /**
|
---|
| 374 | * Targets a room when emitting.
|
---|
| 375 | *
|
---|
| 376 | * @param name
|
---|
| 377 | * @return self
|
---|
| 378 | * @public
|
---|
| 379 | */
|
---|
| 380 | to(name) {
|
---|
| 381 | this.sockets.to(name);
|
---|
| 382 | return this;
|
---|
| 383 | }
|
---|
| 384 | /**
|
---|
| 385 | * Targets a room when emitting.
|
---|
| 386 | *
|
---|
| 387 | * @param name
|
---|
| 388 | * @return self
|
---|
| 389 | * @public
|
---|
| 390 | */
|
---|
| 391 | in(name) {
|
---|
| 392 | this.sockets.in(name);
|
---|
| 393 | return this;
|
---|
| 394 | }
|
---|
| 395 | /**
|
---|
| 396 | * Sends a `message` event to all clients.
|
---|
| 397 | *
|
---|
| 398 | * @return self
|
---|
| 399 | * @public
|
---|
| 400 | */
|
---|
| 401 | send(...args) {
|
---|
| 402 | this.sockets.emit("message", ...args);
|
---|
| 403 | return this;
|
---|
| 404 | }
|
---|
| 405 | /**
|
---|
| 406 | * Sends a `message` event to all clients.
|
---|
| 407 | *
|
---|
| 408 | * @return self
|
---|
| 409 | * @public
|
---|
| 410 | */
|
---|
| 411 | write(...args) {
|
---|
| 412 | this.sockets.emit("message", ...args);
|
---|
| 413 | return this;
|
---|
| 414 | }
|
---|
| 415 | /**
|
---|
| 416 | * Gets a list of socket ids.
|
---|
| 417 | *
|
---|
| 418 | * @public
|
---|
| 419 | */
|
---|
| 420 | allSockets() {
|
---|
| 421 | return this.sockets.allSockets();
|
---|
| 422 | }
|
---|
| 423 | /**
|
---|
| 424 | * Sets the compress flag.
|
---|
| 425 | *
|
---|
| 426 | * @param compress - if `true`, compresses the sending data
|
---|
| 427 | * @return self
|
---|
| 428 | * @public
|
---|
| 429 | */
|
---|
| 430 | compress(compress) {
|
---|
| 431 | this.sockets.compress(compress);
|
---|
| 432 | return this;
|
---|
| 433 | }
|
---|
| 434 | /**
|
---|
| 435 | * Sets a modifier for a subsequent event emission that the event data may be lost if the client is not ready to
|
---|
| 436 | * receive messages (because of network slowness or other issues, or because they’re connected through long polling
|
---|
| 437 | * and is in the middle of a request-response cycle).
|
---|
| 438 | *
|
---|
| 439 | * @return self
|
---|
| 440 | * @public
|
---|
| 441 | */
|
---|
| 442 | get volatile() {
|
---|
| 443 | this.sockets.volatile;
|
---|
| 444 | return this;
|
---|
| 445 | }
|
---|
| 446 | /**
|
---|
| 447 | * Sets a modifier for a subsequent event emission that the event data will only be broadcast to the current node.
|
---|
| 448 | *
|
---|
| 449 | * @return self
|
---|
| 450 | * @public
|
---|
| 451 | */
|
---|
| 452 | get local() {
|
---|
| 453 | this.sockets.local;
|
---|
| 454 | return this;
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
| 457 | exports.Server = Server;
|
---|
| 458 | /**
|
---|
| 459 | * Expose main namespace (/).
|
---|
| 460 | */
|
---|
| 461 | const emitterMethods = Object.keys(events_1.EventEmitter.prototype).filter(function (key) {
|
---|
| 462 | return typeof events_1.EventEmitter.prototype[key] === "function";
|
---|
| 463 | });
|
---|
| 464 | emitterMethods.forEach(function (fn) {
|
---|
| 465 | Server.prototype[fn] = function () {
|
---|
| 466 | return this.sockets[fn].apply(this.sockets, arguments);
|
---|
| 467 | };
|
---|
| 468 | });
|
---|
| 469 | module.exports = (srv, opts) => new Server(srv, opts);
|
---|
| 470 | module.exports.Server = Server;
|
---|