{ "version": 3, "sources": ["../../server/config-loader.ts"], "sourcesContent": ["/**\n * Config loader\n * Pokemon Showdown - http://pokemonshowdown.com/\n *\n * @license MIT\n */\n\nimport * as defaults from '../config/config-example';\nimport type { GroupInfo, EffectiveGroupSymbol } from './user-groups';\nimport { ProcessManager, FS } from '../lib';\n\nexport type ConfigType = typeof defaults & {\n\tgroups: { [symbol: string]: GroupInfo },\n\tgroupsranking: EffectiveGroupSymbol[],\n\tgreatergroupscache: { [combo: string]: GroupSymbol },\n\t[k: string]: any,\n};\n/** Map */\nconst FLAG_PRESETS = new Map([\n\t['--no-security', ['nothrottle', 'noguestsecurity', 'noipchecks']],\n]);\n\nconst CONFIG_PATH = FS('./config/config.js').path;\n\nexport function load(invalidate = false) {\n\tif (invalidate) delete require.cache[CONFIG_PATH];\n\tconst config = ({ ...defaults, ...require(CONFIG_PATH) }) as ConfigType;\n\t// config.routes is nested - we need to ensure values are set for its keys as well.\n\tconfig.routes = { ...defaults.routes, ...config.routes };\n\n\t// Automatically stop startup if better-sqlite3 isn't installed and SQLite is enabled\n\tif (config.usesqlite) {\n\t\ttry {\n\t\t\trequire('better-sqlite3');\n\t\t} catch {\n\t\t\tthrow new Error(`better-sqlite3 is not installed or could not be loaded, but Config.usesqlite is enabled.`);\n\t\t}\n\t}\n\n\tfor (const [preset, values] of FLAG_PRESETS) {\n\t\tif (process.argv.includes(preset)) {\n\t\t\tfor (const value of values) config[value] = true;\n\t\t}\n\t}\n\n\tcacheGroupData(config);\n\treturn config;\n}\n\nexport function cacheGroupData(config: ConfigType) {\n\tif (config.groups) {\n\t\t// Support for old config groups format.\n\t\t// Should be removed soon.\n\t\treportError(\n\t\t\t`You are using a deprecated version of user group specification in config.\\n` +\n\t\t\t`Support for this will be removed soon.\\n` +\n\t\t\t`Please ensure that you update your config.js to the new format (see config-example.js, line 457).\\n`\n\t\t);\n\t} else {\n\t\tconfig.punishgroups = Object.create(null);\n\t\tconfig.groups = Object.create(null);\n\t\tconfig.groupsranking = [];\n\t\tconfig.greatergroupscache = Object.create(null);\n\t}\n\n\tconst groups = config.groups;\n\tconst punishgroups = config.punishgroups;\n\tconst cachedGroups: { [k: string]: 'processing' | true } = {};\n\n\tfunction isPermission(key: string) {\n\t\treturn !['symbol', 'id', 'name', 'rank', 'globalGroupInPersonalRoom'].includes(key);\n\t}\n\tfunction cacheGroup(symbol: string, groupData: GroupInfo) {\n\t\tif (cachedGroups[symbol] === 'processing') {\n\t\t\tthrow new Error(`Cyclic inheritance in group config for symbol \"${symbol}\"`);\n\t\t}\n\t\tif (cachedGroups[symbol] === true) return;\n\n\t\tfor (const key in groupData) {\n\t\t\tif (isPermission(key)) {\n\t\t\t\tconst jurisdiction = groupData[key as 'jurisdiction'];\n\t\t\t\tif (typeof jurisdiction === 'string' && jurisdiction.includes('s')) {\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'`);\n\t\t\t\t\tdelete groupData[key as 'jurisdiction'];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (groupData['inherit']) {\n\t\t\tcachedGroups[symbol] = 'processing';\n\t\t\tconst inheritGroup = groups[groupData['inherit']];\n\t\t\tcacheGroup(groupData['inherit'], inheritGroup);\n\t\t\t// Add lower group permissions to higher ranked groups,\n\t\t\t// preserving permissions specifically declared for the higher group.\n\t\t\tfor (const key in inheritGroup) {\n\t\t\t\tif (key in groupData) continue;\n\t\t\t\tif (!isPermission(key)) continue;\n\t\t\t\t(groupData as any)[key] = (inheritGroup as any)[key];\n\t\t\t}\n\t\t\tdelete groupData['inherit'];\n\t\t}\n\t\tcachedGroups[symbol] = true;\n\t}\n\n\tif (config.grouplist) { // Using new groups format.\n\t\tconst grouplist = config.grouplist as any;\n\t\tconst numGroups = grouplist.length;\n\t\tfor (let i = 0; i < numGroups; i++) {\n\t\t\tconst groupData = grouplist[i];\n\n\t\t\t// punish groups\n\t\t\tif (groupData.punishgroup) {\n\t\t\t\tpunishgroups[groupData.id] = groupData;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tgroupData.rank = numGroups - i - 1;\n\t\t\tgroups[groupData.symbol] = groupData;\n\t\t\tconfig.groupsranking.unshift(groupData.symbol);\n\t\t}\n\t}\n\n\tfor (const sym in groups) {\n\t\tconst groupData = groups[sym];\n\t\tcacheGroup(sym, groupData);\n\t}\n\n\t// hardcode default punishgroups.\n\tif (!punishgroups.locked) {\n\t\tpunishgroups.locked = {\n\t\t\tname: 'Locked',\n\t\t\tid: 'locked',\n\t\t\tsymbol: '\\u203d',\n\t\t};\n\t}\n\tif (!punishgroups.muted) {\n\t\tpunishgroups.muted = {\n\t\t\tname: 'Muted',\n\t\t\tid: 'muted',\n\t\t\tsymbol: '!',\n\t\t};\n\t}\n}\n\nexport function checkRipgrepAvailability() {\n\tif (Config.ripgrepmodlog === undefined) {\n\t\tconst cwd = FS.ROOT_PATH;\n\t\tConfig.ripgrepmodlog = (async () => {\n\t\t\ttry {\n\t\t\t\tawait ProcessManager.exec(['rg', '--version'], { cwd });\n\t\t\t\tawait ProcessManager.exec(['tac', '--version'], { cwd });\n\t\t\t\treturn true;\n\t\t\t} catch {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t})();\n\t}\n\treturn Config.ripgrepmodlog;\n}\n\nfunction reportError(msg: string) {\n\t// This module generally loads before Monitor, so we put this in a setImmediate to wait for it to load.\n\t// Most child processes don't have Monitor.error, but the main process should always have them, and Config\n\t// errors should always be the same across processes, so this is a neat way to avoid unnecessary logging.\n\tsetImmediate(() => global.Monitor?.error?.(msg));\n}\nexport const Config = load();\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,eAA0B;AAE1B,iBAAmC;AATnC;AAAA;AAAA;AAAA;AAAA;AAAA;AAkBA,MAAM,eAAe,oBAAI,IAAI;AAAA,EAC5B,CAAC,iBAAiB,C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