123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- "use strict";
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- var desc = Object.getOwnPropertyDescriptor(m, k);
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
- desc = { enumerable: true, get: function() { return m[k]; } };
- }
- Object.defineProperty(o, k2, desc);
- }) : (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- o[k2] = m[k];
- }));
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
- Object.defineProperty(o, "default", { enumerable: true, value: v });
- }) : function(o, v) {
- o["default"] = v;
- });
- var __importStar = (this && this.__importStar) || function (mod) {
- if (mod && mod.__esModule) return mod;
- var result = {};
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
- __setModuleDefault(result, mod);
- return result;
- };
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Namespace = exports.Socket = exports.Server = void 0;
- const http = require("http");
- const fs_1 = require("fs");
- const zlib_1 = require("zlib");
- const accepts = require("accepts");
- const stream_1 = require("stream");
- const path = require("path");
- const engine_io_1 = require("engine.io");
- const client_1 = require("./client");
- const events_1 = require("events");
- const namespace_1 = require("./namespace");
- Object.defineProperty(exports, "Namespace", { enumerable: true, get: function () { return namespace_1.Namespace; } });
- const parent_namespace_1 = require("./parent-namespace");
- const socket_io_adapter_1 = require("socket.io-adapter");
- const parser = __importStar(require("socket.io-parser"));
- const debug_1 = __importDefault(require("debug"));
- const socket_1 = require("./socket");
- Object.defineProperty(exports, "Socket", { enumerable: true, get: function () { return socket_1.Socket; } });
- const typed_events_1 = require("./typed-events");
- const uws_1 = require("./uws");
- const debug = (0, debug_1.default)("socket.io:server");
- const clientVersion = require("../package.json").version;
- const dotMapRegex = /\.map/;
-
- class Server extends typed_events_1.StrictEventEmitter {
- constructor(srv, opts = {}) {
- super();
-
-
- this._nsps = new Map();
- this.parentNsps = new Map();
- if ("object" === typeof srv &&
- srv instanceof Object &&
- !srv.listen) {
- opts = srv;
- srv = undefined;
- }
- this.path(opts.path || "/socket.io");
- this.connectTimeout(opts.connectTimeout || 45000);
- this.serveClient(false !== opts.serveClient);
- this._parser = opts.parser || parser;
- this.encoder = new this._parser.Encoder();
- this.adapter(opts.adapter || socket_io_adapter_1.Adapter);
- this.sockets = this.of("/");
- this.opts = opts;
- if (srv || typeof srv == "number")
- this.attach(srv);
- }
- serveClient(v) {
- if (!arguments.length)
- return this._serveClient;
- this._serveClient = v;
- return this;
- }
-
-
- _checkNamespace(name, auth, fn) {
- if (this.parentNsps.size === 0)
- return fn(false);
- const keysIterator = this.parentNsps.keys();
- const run = () => {
- const nextFn = keysIterator.next();
- if (nextFn.done) {
- return fn(false);
- }
- nextFn.value(name, auth, (err, allow) => {
- if (err || !allow) {
- return run();
- }
- if (this._nsps.has(name)) {
-
- debug("dynamic namespace %s already exists", name);
- return fn(this._nsps.get(name));
- }
- const namespace = this.parentNsps.get(nextFn.value).createChild(name);
- debug("dynamic namespace %s was created", name);
-
- this.sockets.emitReserved("new_namespace", namespace);
- fn(namespace);
- });
- };
- run();
- }
- path(v) {
- if (!arguments.length)
- return this._path;
- this._path = v.replace(/\/$/, "");
- const escapedPath = this._path.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
- this.clientPathRegex = new RegExp("^" +
- escapedPath +
- "/socket\\.io(\\.msgpack|\\.esm)?(\\.min)?\\.js(\\.map)?(?:\\?|$)");
- return this;
- }
- connectTimeout(v) {
- if (v === undefined)
- return this._connectTimeout;
- this._connectTimeout = v;
- return this;
- }
- adapter(v) {
- if (!arguments.length)
- return this._adapter;
- this._adapter = v;
- for (const nsp of this._nsps.values()) {
- nsp._initAdapter();
- }
- return this;
- }
-
-
- listen(srv, opts = {}) {
- return this.attach(srv, opts);
- }
-
-
- attach(srv, opts = {}) {
- if ("function" == typeof srv) {
- const msg = "You are trying to attach socket.io to an express " +
- "request handler function. Please pass a http.Server instance.";
- throw new Error(msg);
- }
-
- if (Number(srv) == srv) {
- srv = Number(srv);
- }
- if ("number" == typeof srv) {
- debug("creating http server and binding to %d", srv);
- const port = srv;
- srv = http.createServer((req, res) => {
- res.writeHead(404);
- res.end();
- });
- srv.listen(port);
- }
-
- Object.assign(opts, this.opts);
-
- opts.path = opts.path || this._path;
- this.initEngine(srv, opts);
- return this;
- }
- attachApp(app , opts = {}) {
-
- Object.assign(opts, this.opts);
-
- opts.path = opts.path || this._path;
-
- debug("creating uWebSockets.js-based engine with opts %j", opts);
- const engine = new engine_io_1.uServer(opts);
- engine.attach(app, opts);
-
- this.bind(engine);
- if (this._serveClient) {
-
- app.get(`${this._path}/*`, (res, req) => {
- if (!this.clientPathRegex.test(req.getUrl())) {
- req.setYield(true);
- return;
- }
- const filename = req
- .getUrl()
- .replace(this._path, "")
- .replace(/\?.*$/, "")
- .replace(/^\//, "");
- const isMap = dotMapRegex.test(filename);
- const type = isMap ? "map" : "source";
-
-
- const expectedEtag = '"' + clientVersion + '"';
- const weakEtag = "W/" + expectedEtag;
- const etag = req.getHeader("if-none-match");
- if (etag) {
- if (expectedEtag === etag || weakEtag === etag) {
- debug("serve client %s 304", type);
- res.writeStatus("304 Not Modified");
- res.end();
- return;
- }
- }
- debug("serve client %s", type);
- res.writeHeader("cache-control", "public, max-age=0");
- res.writeHeader("content-type", "application/" + (isMap ? "json" : "javascript"));
- res.writeHeader("etag", expectedEtag);
- const filepath = path.join(__dirname, "../client-dist/", filename);
- (0, uws_1.serveFile)(res, filepath);
- });
- }
- (0, uws_1.patchAdapter)(app);
- }
-
-
- initEngine(srv, opts) {
-
- debug("creating engine.io instance with opts %j", opts);
- this.eio = (0, engine_io_1.attach)(srv, opts);
-
- if (this._serveClient)
- this.attachServe(srv);
-
- this.httpServer = srv;
-
- this.bind(this.eio);
- }
-
-
- attachServe(srv) {
- debug("attaching client serving req handler");
- const evs = srv.listeners("request").slice(0);
- srv.removeAllListeners("request");
- srv.on("request", (req, res) => {
- if (this.clientPathRegex.test(req.url)) {
- this.serve(req, res);
- }
- else {
- for (let i = 0; i < evs.length; i++) {
- evs[i].call(srv, req, res);
- }
- }
- });
- }
-
-
- serve(req, res) {
- const filename = req.url.replace(this._path, "").replace(/\?.*$/, "");
- const isMap = dotMapRegex.test(filename);
- const type = isMap ? "map" : "source";
-
-
- const expectedEtag = '"' + clientVersion + '"';
- const weakEtag = "W/" + expectedEtag;
- const etag = req.headers["if-none-match"];
- if (etag) {
- if (expectedEtag === etag || weakEtag === etag) {
- debug("serve client %s 304", type);
- res.writeHead(304);
- res.end();
- return;
- }
- }
- debug("serve client %s", type);
- res.setHeader("Cache-Control", "public, max-age=0");
- res.setHeader("Content-Type", "application/" + (isMap ? "json" : "javascript"));
- res.setHeader("ETag", expectedEtag);
- Server.sendFile(filename, req, res);
- }
-
-
- static sendFile(filename, req, res) {
- const readStream = (0, fs_1.createReadStream)(path.join(__dirname, "../client-dist/", filename));
- const encoding = accepts(req).encodings(["br", "gzip", "deflate"]);
- const onError = (err) => {
- if (err) {
- res.end();
- }
- };
- switch (encoding) {
- case "br":
- res.writeHead(200, { "content-encoding": "br" });
- readStream.pipe((0, zlib_1.createBrotliCompress)()).pipe(res);
- (0, stream_1.pipeline)(readStream, (0, zlib_1.createBrotliCompress)(), res, onError);
- break;
- case "gzip":
- res.writeHead(200, { "content-encoding": "gzip" });
- (0, stream_1.pipeline)(readStream, (0, zlib_1.createGzip)(), res, onError);
- break;
- case "deflate":
- res.writeHead(200, { "content-encoding": "deflate" });
- (0, stream_1.pipeline)(readStream, (0, zlib_1.createDeflate)(), res, onError);
- break;
- default:
- res.writeHead(200);
- (0, stream_1.pipeline)(readStream, res, onError);
- }
- }
-
-
- bind(engine) {
- this.engine = engine;
- this.engine.on("connection", this.onconnection.bind(this));
- return this;
- }
-
-
- onconnection(conn) {
- debug("incoming connection with id %s", conn.id);
- const client = new client_1.Client(this, conn);
- if (conn.protocol === 3) {
-
- client.connect("/");
- }
- return this;
- }
-
-
- of(name, fn) {
- if (typeof name === "function" || name instanceof RegExp) {
- const parentNsp = new parent_namespace_1.ParentNamespace(this);
- debug("initializing parent namespace %s", parentNsp.name);
- if (typeof name === "function") {
- this.parentNsps.set(name, parentNsp);
- }
- else {
- this.parentNsps.set((nsp, conn, next) => next(null, name.test(nsp)), parentNsp);
- }
- if (fn) {
-
- parentNsp.on("connect", fn);
- }
- return parentNsp;
- }
- if (String(name)[0] !== "/")
- name = "/" + name;
- let nsp = this._nsps.get(name);
- if (!nsp) {
- debug("initializing namespace %s", name);
- nsp = new namespace_1.Namespace(this, name);
- this._nsps.set(name, nsp);
- if (name !== "/") {
-
- this.sockets.emitReserved("new_namespace", nsp);
- }
- }
- if (fn)
- nsp.on("connect", fn);
- return nsp;
- }
-
-
- close(fn) {
- for (const socket of this.sockets.sockets.values()) {
- socket._onclose("server shutting down");
- }
- this.engine.close();
-
- (0, uws_1.restoreAdapter)();
- if (this.httpServer) {
- this.httpServer.close(fn);
- }
- else {
- fn && fn();
- }
- }
-
-
- use(fn) {
- this.sockets.use(fn);
- return this;
- }
-
-
- to(room) {
- return this.sockets.to(room);
- }
-
-
- in(room) {
- return this.sockets.in(room);
- }
-
-
- except(room) {
- return this.sockets.except(room);
- }
-
-
- send(...args) {
- this.sockets.emit("message", ...args);
- return this;
- }
-
-
- write(...args) {
- this.sockets.emit("message", ...args);
- return this;
- }
-
-
- serverSideEmit(ev, ...args) {
- return this.sockets.serverSideEmit(ev, ...args);
- }
-
-
- allSockets() {
- return this.sockets.allSockets();
- }
-
-
- compress(compress) {
- return this.sockets.compress(compress);
- }
-
-
- get volatile() {
- return this.sockets.volatile;
- }
-
-
- get local() {
- return this.sockets.local;
- }
-
-
- timeout(timeout) {
- return this.sockets.timeout(timeout);
- }
-
-
- fetchSockets() {
- return this.sockets.fetchSockets();
- }
-
-
- socketsJoin(room) {
- return this.sockets.socketsJoin(room);
- }
-
-
- socketsLeave(room) {
- return this.sockets.socketsLeave(room);
- }
-
-
- disconnectSockets(close = false) {
- return this.sockets.disconnectSockets(close);
- }
- }
- exports.Server = Server;
-
- const emitterMethods = Object.keys(events_1.EventEmitter.prototype).filter(function (key) {
- return typeof events_1.EventEmitter.prototype[key] === "function";
- });
- emitterMethods.forEach(function (fn) {
- Server.prototype[fn] = function () {
- return this.sockets[fn].apply(this.sockets, arguments);
- };
- });
- module.exports = (srv, opts) => new Server(srv, opts);
- module.exports.Server = Server;
- module.exports.Namespace = namespace_1.Namespace;
- module.exports.Socket = socket_1.Socket;
- var socket_2 = require("./socket");
|