{ "version": 3, "sources": ["../../../server/index.ts"], "sourcesContent": ["/**\r\n * Main file\r\n * Pokemon Showdown - http://pokemonshowdown.com/\r\n *\r\n * This is the main Pokemon Showdown app, and the file that the\r\n * `pokemon-showdown` script runs if you start Pokemon Showdown normally.\r\n *\r\n * This file sets up our SockJS server, which handles communication\r\n * between users and your server, and also sets up globals. You can\r\n * see details in their corresponding files, but here's an overview:\r\n *\r\n * Users - from users.ts\r\n *\r\n * Most of the communication with users happens in users.ts, we just\r\n * forward messages between the sockets.js and users.ts.\r\n *\r\n * It exports the global tables `Users.users` and `Users.connections`.\r\n *\r\n * Rooms - from rooms.ts\r\n *\r\n * Every chat room and battle is a room, and what they do is done in\r\n * rooms.ts. There's also a global room which every user is in, and\r\n * handles miscellaneous things like welcoming the user.\r\n *\r\n * It exports the global table `Rooms.rooms`.\r\n *\r\n * Dex - from sim/dex.ts\r\n *\r\n * Handles getting data about Pokemon, items, etc.\r\n *\r\n * Ladders - from ladders.ts and ladders-remote.ts\r\n *\r\n * Handles Elo rating tracking for players.\r\n *\r\n * Chat - from chat.ts\r\n *\r\n * Handles chat and parses chat commands like /me and /ban\r\n *\r\n * Sockets - from sockets.js\r\n *\r\n * Used to abstract out network connections. sockets.js handles\r\n * the actual server and connection set-up.\r\n *\r\n * @license MIT\r\n */\r\n\r\n// NOTE: This file intentionally doesn't use too many modern JavaScript\r\n// features, so that it doesn't crash old versions of Node.js, so we\r\n// can successfully print the \"We require Node.js 8+\" message.\r\n\r\n// Check for version\r\nconst nodeVersion = parseInt(process.versions.node);\r\nif (isNaN(nodeVersion) || nodeVersion < 14) {\r\n\tthrow new Error(\"We require Node.js version 14 or later; you're using \" + process.version);\r\n}\r\n\r\nimport {FS, Repl} from '../lib';\r\n\r\n/*********************************************************\r\n * Set up most of our globals\r\n * This is in a function because swc runs `import` before any code,\r\n * and many of our imports require the `Config` global to be set up.\r\n *********************************************************/\r\nfunction setupGlobals() {\r\n\tconst ConfigLoader = require('./config-loader');\r\n\tglobal.Config = ConfigLoader.Config;\r\n\r\n\tconst {Monitor} = require('./monitor');\r\n\tglobal.Monitor = Monitor;\r\n\tglobal.__version = {head: ''};\r\n\tvoid Monitor.version().then((hash: any) => {\r\n\t\tglobal.__version.tree = hash;\r\n\t});\r\n\r\n\tif (Config.watchconfig) {\r\n\t\tFS('config/config.js').onModify(() => {\r\n\t\t\ttry {\r\n\t\t\t\tglobal.Config = ConfigLoader.load(true);\r\n\t\t\t\t// ensure that battle prefixes configured via the chat plugin are not overwritten\r\n\t\t\t\t// by battle prefixes manually specified in config.js\r\n\t\t\t\tChat.plugins['username-prefixes']?.prefixManager.refreshConfig(true);\r\n\t\t\t\tMonitor.notice('Reloaded ../config/config.js');\r\n\t\t\t} catch (e: any) {\r\n\t\t\t\tMonitor.adminlog(\"Error reloading ../config/config.js: \" + e.stack);\r\n\t\t\t}\r\n\t\t});\r\n\t}\r\n\r\n\tconst {Dex} = require('../sim/dex');\r\n\tglobal.Dex = Dex;\r\n\tglobal.toID = Dex.toID;\r\n\r\n\tconst {Teams} = require('../sim/teams');\r\n\tglobal.Teams = Teams;\r\n\r\n\tconst {LoginServer} = require('./loginserver');\r\n\tglobal.LoginServer = LoginServer;\r\n\r\n\tconst {Ladders} = require('./ladders');\r\n\tglobal.Ladders = Ladders;\r\n\r\n\tconst {Chat} = require('./chat');\r\n\tglobal.Chat = Chat;\r\n\r\n\tconst {Users} = require('./users');\r\n\tglobal.Users = Users;\r\n\r\n\tconst {Punishments} = require('./punishments');\r\n\tglobal.Punishments = Punishments;\r\n\r\n\tconst {Rooms} = require('./rooms');\r\n\tglobal.Rooms = Rooms;\r\n\t// We initialize the global room here because roomlogs.ts needs the Rooms global\r\n\tRooms.global = new Rooms.GlobalRoomState();\r\n\r\n\tconst Verifier = require('./verifier');\r\n\tglobal.Verifier = Verifier;\r\n\tVerifier.PM.spawn();\r\n\r\n\tconst {Tournaments} = require('./tournaments');\r\n\tglobal.Tournaments = Tournaments;\r\n\r\n\tconst {IPTools} = require('./ip-tools');\r\n\tglobal.IPTools = IPTools;\r\n\tvoid IPTools.loadHostsAndRanges();\r\n}\r\nsetupGlobals();\r\n\r\nif (Config.crashguard) {\r\n\t// graceful crash - allow current battles to finish before restarting\r\n\tprocess.on('uncaughtException', (err: Error) => {\r\n\t\tMonitor.crashlog(err, 'The main process');\r\n\t});\r\n\r\n\tprocess.on('unhandledRejection', err => {\r\n\t\tMonitor.crashlog(err as any, 'A main process Promise');\r\n\t});\r\n}\r\n\r\n/*********************************************************\r\n * Start networking processes to be connected to\r\n *********************************************************/\r\n\r\nimport {Sockets} from './sockets';\r\nglobal.Sockets = Sockets;\r\n\r\nexport function listen(port: number, bindAddress: string, workerCount: number) {\r\n\tSockets.listen(port, bindAddress, workerCount);\r\n}\r\n\r\nif (require.main === module) {\r\n\t// Launch the server directly when app.js is the main module. Otherwise,\r\n\t// in the case of app.js being imported as a module (e.g. unit tests),\r\n\t// postpone launching until app.listen() is called.\r\n\tlet port;\r\n\tfor (const arg of process.argv) {\r\n\t\tif (/^[0-9]+$/.test(arg)) {\r\n\t\t\tport = parseInt(arg);\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\tSockets.listen(port);\r\n}\r\n\r\n/*********************************************************\r\n * Set up our last global\r\n *********************************************************/\r\n\r\nimport * as TeamValidatorAsync from './team-validator-async';\r\nglobal.TeamValidatorAsync = TeamValidatorAsync;\r\nTeamValidatorAsync.PM.spawn();\r\n\r\n/*********************************************************\r\n * Start up the REPL server\r\n *********************************************************/\r\n\r\n// eslint-disable-next-line no-eval\r\nRepl.start('app', cmd => eval(cmd));\r\n\r\n/*********************************************************\r\n * Fully initialized, run startup hook\r\n *********************************************************/\r\n\r\nif (Config.startuphook) {\r\n\tprocess.nextTick(Config.startuphook);\r\n}\r\n\r\nif (Config.ofemain) {\r\n\ttry {\r\n\t\trequire.resolve('node-oom-heapdump');\r\n\t} catch (e: any) {\r\n\t\tif (e.code !== 'MODULE_NOT_FOUND') throw e; // should never happen\r\n\t\tthrow new Error(\r\n\t\t\t'node-oom-heapdump is not installed, but it is a required dependency if Config.ofe is set to true! ' +\r\n\t\t\t'Run npm install node-oom-heapdump and restart the server.'\r\n\t\t);\r\n\t}\r\n\r\n\t// Create a heapdump if the process runs out of memory.\r\n\tglobal.nodeOomHeapdump = (require as any)('node-oom-heapdump')({\r\n\t\taddTimestamp: true,\r\n\t});\r\n}\r\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAwDA,iBAAuB;AAuFvB,qBAAsB;AAyBtB,yBAAoC;AAxKpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmDA,MAAM,cAAc,SAAS,QAAQ,SAAS,IAAI;AAClD,IAAI,MAAM,WAAW,KAAK,cAAc,IAAI;AAC3C,QAAM,IAAI,MAAM,0DAA0D,QAAQ,OAAO;AAC1F;AASA,SAAS,eAAe;AACvB,QAAM,eAAe,QAAQ,iBAAiB;AAC9C,SAAO,SAAS,aAAa;AAE7B,QAAM,EAAC,SAAAA,SAAO,IAAI,QAAQ,WAAW;AACrC,SAAO,UAAUA;AACjB,SAAO,YAAY,EAAC,MAAM,GAAE;AAC5B,OAAKA,SAAQ,QAAQ,EAAE,KAAK,CAAC,SAAc;AAC1C,WAAO,UAAU,OAAO;AAAA,EACzB,CAAC;AAED,MAAI,OAAO,aAAa;AACvB,uBAAG,kBAAkB,EAAE,SAAS,MAAM;AACrC,UAAI;AACH,eAAO,SAAS,aAAa,KAAK,IAAI;AAGtC,aAAK,QAAQ,mBAAmB,GAAG,cAAc,cAAc,IAAI;AACnE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