{ "version": 3, "sources": ["../../../server/config-loader.ts"], "sourcesContent": ["/**\r\n * Config loader\r\n * Pokemon Showdown - http://pokemonshowdown.com/\r\n *\r\n * @license MIT\r\n */\r\n\r\nimport * as defaults from '../config/config-example';\r\nimport type {GroupInfo, EffectiveGroupSymbol} from './user-groups';\r\nimport {ProcessManager, FS} from '../lib';\r\n\r\nexport type ConfigType = typeof defaults & {\r\n\tgroups: {[symbol: string]: GroupInfo},\r\n\tgroupsranking: EffectiveGroupSymbol[],\r\n\tgreatergroupscache: {[combo: string]: GroupSymbol},\r\n\t[k: string]: any,\r\n};\r\n/** Map */\r\nconst FLAG_PRESETS = new Map([\r\n\t['--no-security', ['nothrottle', 'noguestsecurity', 'noipchecks']],\r\n]);\r\n\r\nconst CONFIG_PATH = FS('./config/config.js').path;\r\n\r\nexport function load(invalidate = false) {\r\n\tif (invalidate) delete require.cache[CONFIG_PATH];\r\n\tconst config = ({...defaults, ...require(CONFIG_PATH)}) as ConfigType;\r\n\t// config.routes is nested - we need to ensure values are set for its keys as well.\r\n\tconfig.routes = {...defaults.routes, ...config.routes};\r\n\r\n\t// Automatically stop startup if better-sqlite3 isn't installed and SQLite is enabled\r\n\tif (config.usesqlite) {\r\n\t\ttry {\r\n\t\t\trequire('better-sqlite3');\r\n\t\t} catch {\r\n\t\t\tthrow new Error(`better-sqlite3 is not installed or could not be loaded, but Config.usesqlite is enabled.`);\r\n\t\t}\r\n\t}\r\n\r\n\tfor (const [preset, values] of FLAG_PRESETS) {\r\n\t\tif (process.argv.includes(preset)) {\r\n\t\t\tfor (const value of values) config[value] = true;\r\n\t\t}\r\n\t}\r\n\r\n\tcacheGroupData(config);\r\n\treturn config;\r\n}\r\n\r\nexport function cacheGroupData(config: ConfigType) {\r\n\tif (config.groups) {\r\n\t\t// Support for old config groups format.\r\n\t\t// Should be removed soon.\r\n\t\treportError(\r\n\t\t\t`You are using a deprecated version of user group specification in config.\\n` +\r\n\t\t\t`Support for this will be removed soon.\\n` +\r\n\t\t\t`Please ensure that you update your config.js to the new format (see config-example.js, line 457).\\n`\r\n\t\t);\r\n\t} else {\r\n\t\tconfig.punishgroups = Object.create(null);\r\n\t\tconfig.groups = Object.create(null);\r\n\t\tconfig.groupsranking = [];\r\n\t\tconfig.greatergroupscache = Object.create(null);\r\n\t}\r\n\r\n\tconst groups = config.groups;\r\n\tconst punishgroups = config.punishgroups;\r\n\tconst cachedGroups: {[k: string]: 'processing' | true} = {};\r\n\r\n\tfunction isPermission(key: string) {\r\n\t\treturn !['symbol', 'id', 'name', 'rank', 'globalGroupInPersonalRoom'].includes(key);\r\n\t}\r\n\tfunction cacheGroup(symbol: string, groupData: GroupInfo) {\r\n\t\tif (cachedGroups[symbol] === 'processing') {\r\n\t\t\tthrow new Error(`Cyclic inheritance in group config for symbol \"${symbol}\"`);\r\n\t\t}\r\n\t\tif (cachedGroups[symbol] === true) return;\r\n\r\n\t\tfor (const key in groupData) {\r\n\t\t\tif (isPermission(key)) {\r\n\t\t\t\tconst jurisdiction = groupData[key as 'jurisdiction'];\r\n\t\t\t\tif (typeof jurisdiction === 'string' && jurisdiction.includes('s')) {\r\n\t\t\t\t\treportError(`Outdated jurisdiction for permission \"${key}\" of group \"${symbol}\": 's' is no longer a supported jurisdiction; we now use 'ipself' and 'altsself'`);\r\n\t\t\t\t\tdelete groupData[key as 'jurisdiction'];\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif (groupData['inherit']) {\r\n\t\t\tcachedGroups[symbol] = 'processing';\r\n\t\t\tconst inheritGroup = groups[groupData['inherit']];\r\n\t\t\tcacheGroup(groupData['inherit'], inheritGroup);\r\n\t\t\t// Add lower group permissions to higher ranked groups,\r\n\t\t\t// preserving permissions specifically declared for the higher group.\r\n\t\t\tfor (const key in inheritGroup) {\r\n\t\t\t\tif (key in groupData) continue;\r\n\t\t\t\tif (!isPermission(key)) continue;\r\n\t\t\t\t(groupData as any)[key] = (inheritGroup as any)[key];\r\n\t\t\t}\r\n\t\t\tdelete groupData['inherit'];\r\n\t\t}\r\n\t\tcachedGroups[symbol] = true;\r\n\t}\r\n\r\n\tif (config.grouplist) { // Using new groups format.\r\n\t\tconst grouplist = config.grouplist as any;\r\n\t\tconst numGroups = grouplist.length;\r\n\t\tfor (let i = 0; i < numGroups; i++) {\r\n\t\t\tconst groupData = grouplist[i];\r\n\r\n\t\t\t// punish groups\r\n\t\t\tif (groupData.punishgroup) {\r\n\t\t\t\tpunishgroups[groupData.id] = groupData;\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\r\n\t\t\tgroupData.rank = numGroups - i - 1;\r\n\t\t\tgroups[groupData.symbol] = groupData;\r\n\t\t\tconfig.groupsranking.unshift(groupData.symbol);\r\n\t\t}\r\n\t}\r\n\r\n\tfor (const sym in groups) {\r\n\t\tconst groupData = groups[sym];\r\n\t\tcacheGroup(sym, groupData);\r\n\t}\r\n\r\n\t// hardcode default punishgroups.\r\n\tif (!punishgroups.locked) {\r\n\t\tpunishgroups.locked = {\r\n\t\t\tname: 'Locked',\r\n\t\t\tid: 'locked',\r\n\t\t\tsymbol: '\\u203d',\r\n\t\t};\r\n\t}\r\n\tif (!punishgroups.muted) {\r\n\t\tpunishgroups.muted = {\r\n\t\t\tname: 'Muted',\r\n\t\t\tid: 'muted',\r\n\t\t\tsymbol: '!',\r\n\t\t};\r\n\t}\r\n}\r\n\r\nexport function checkRipgrepAvailability() {\r\n\tif (Config.ripgrepmodlog === undefined) {\r\n\t\tconst cwd = FS.ROOT_PATH;\r\n\t\tConfig.ripgrepmodlog = (async () => {\r\n\t\t\ttry {\r\n\t\t\t\tawait ProcessManager.exec(['rg', '--version'], {cwd});\r\n\t\t\t\tawait ProcessManager.exec(['tac', '--version'], {cwd});\r\n\t\t\t\treturn true;\r\n\t\t\t} catch {\r\n\t\t\t\treturn false;\r\n\t\t\t}\r\n\t\t})();\r\n\t}\r\n\treturn Config.ripgrepmodlog;\r\n}\r\n\r\n\r\nfunction reportError(msg: string) {\r\n\t// This module generally loads before Monitor, so we put this in a setImmediate to wait for it to load.\r\n\t// Most child processes don't have Monitor.error, but the main process should always have them, and Config\r\n\t// errors should always be the same across processes, so this is a neat way to avoid unnecessary logging.\r\n\t// @ts-ignore typescript doesn't like us accessing Monitor like this\r\n\tsetImmediate(() => global.Monitor?.error?.(msg));\r\n}\r\nexport const Config = load();\r\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,eAA0B;AAE1B,iBAAiC;AATjC;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,MAAM,eAAe,oBAAI,IAAI;AAAA,EAC5B,CAAC,iBAAiB,CAAC,cAAc,mBAAmB,YAAY,CAAC;AAClE,CAAC;AAED,MAAM,kBAAc,eAAG,oBAAoB,EAAE;AAEtC,SAAS,KAAK,aAAa,OAAO;AACxC,MAAI;AAAY,WAAO,QAAQ,MAAM,WAAW;AAChD,QAAM,SAAU,EAAC,GAAG,UAAU,GAAG,QAAQ,WAAW,EAAC;AAErD,SAAO,SAAS,EAAC,GAAG,SAAS,QAAQ,GAAG,OAAO,OAAM;AAGrD,MAAI,OAAO,WAAW;AACrB,QAAI;AACH,cAAQ,gBAAgB;AAAA,IACzB,QAAE;AACD,YAAM,IAAI,MAAM,0FAA0F;AAAA,IAC3G;AAAA,EACD;AAEA,aAAW,CAAC,QAAQ,MAAM,KAAK,cAAc;AAC5C,QAAI,QAAQ,KAAK,SAAS,MAAM,GAAG;AAClC,iBAAW,SAAS;AAAQ,eAAO,KAAK,IAAI;AAAA,IAC7C;AAAA,EACD;AAEA,iBAAe,MAAM;AACrB,SAAO;AACR;AAEO,SAAS,eAAe,QAAoB;AAClD,MAAI,OAAO,QAAQ;AAGlB;AAAA,MACC;AAAA;AAAA;AAAA;AAAA,IAGD;AAAA,EACD,OAAO;AACN,WAAO,eAAe,uBAAO,OAAO,IAAI;AACxC,WAAO,SAAS,uBAAO,OAAO,IAA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